The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
7.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_OPENGLCONTEXT_JUCEHEADER__
  19. #define __JUCE_OPENGLCONTEXT_JUCEHEADER__
  20. #include "juce_OpenGLPixelFormat.h"
  21. #include "../native/juce_OpenGLExtensions.h"
  22. //==============================================================================
  23. /**
  24. */
  25. class JUCE_API OpenGLRenderer
  26. {
  27. public:
  28. OpenGLRenderer() {}
  29. virtual ~OpenGLRenderer() {}
  30. /**
  31. */
  32. virtual void newOpenGLContextCreated() = 0;
  33. /**
  34. */
  35. virtual void renderOpenGL() = 0;
  36. /**
  37. */
  38. virtual void openGLContextClosing() = 0;
  39. };
  40. //==============================================================================
  41. /**
  42. A base class for types of OpenGL context.
  43. An OpenGLComponent will supply its own context for drawing in its window.
  44. */
  45. class JUCE_API OpenGLContext
  46. {
  47. public:
  48. OpenGLContext();
  49. /** Destructor. */
  50. ~OpenGLContext();
  51. //==============================================================================
  52. /** */
  53. void setRenderer (OpenGLRenderer* rendererToUse,
  54. bool shouldAlsoPaintComponent) noexcept;
  55. /** */
  56. void attachTo (Component& component);
  57. /** */
  58. void attachTo (Component& component,
  59. const OpenGLPixelFormat& preferredPixelFormat,
  60. const OpenGLContext* contextToShareWith);
  61. /** */
  62. void detach();
  63. /** */
  64. bool isAttached() const noexcept;
  65. //==============================================================================
  66. /** Makes this context the currently active one. */
  67. bool makeActive() const noexcept;
  68. /** If this context is currently active, it is disactivated. */
  69. bool makeInactive() const noexcept;
  70. /** Returns true if this context is currently active. */
  71. bool isActive() const noexcept;
  72. /** Returns the component to which this context is currently attached, or nullptr. */
  73. Component* getTargetComponent() const noexcept;
  74. /** Returns the context that's currently in active use by the calling thread, or
  75. nullptr if no context is active.
  76. */
  77. static OpenGLContext* getCurrentContext();
  78. //==============================================================================
  79. /** Swaps the buffers (if the context can do this).
  80. There's normally no need to call this directly - the buffers will be swapped
  81. automatically after your OpenGLRenderer::renderOpenGL() method has been called.
  82. */
  83. void swapBuffers();
  84. /** Sets whether the context checks the vertical sync before swapping.
  85. The value is the number of frames to allow between buffer-swapping. This is
  86. fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
  87. and greater numbers indicate that it should swap less often.
  88. Returns true if it sets the value successfully - some platforms won't support
  89. this setting.
  90. */
  91. bool setSwapInterval (int numFramesPerSwap);
  92. /** Returns the current swap-sync interval.
  93. See setSwapInterval() for info about the value returned.
  94. */
  95. int getSwapInterval() const;
  96. /** */
  97. void triggerRepaint();
  98. //==============================================================================
  99. /** Returns the width of this context */
  100. inline int getWidth() const noexcept { return width; }
  101. /** Returns the height of this context */
  102. inline int getHeight() const noexcept { return height; }
  103. /** If this context is backed by a frame buffer, this returns its ID number,
  104. or 0 if the context has no accessible framebuffer.
  105. */
  106. unsigned int getFrameBufferID() const noexcept;
  107. /** Returns true if shaders can be used in this context. */
  108. bool areShadersAvailable() const;
  109. /** This structure holds a set of dynamically loaded GL functions for use on this context. */
  110. OpenGLExtensionFunctions extensions;
  111. /** This retrieves an object that was previously stored with setAssociatedObject().
  112. If no object is found with the given name, this will return nullptr.
  113. This method must only be called from within the GL rendering methods.
  114. @see setAssociatedObject
  115. */
  116. ReferenceCountedObjectPtr<ReferenceCountedObject> getAssociatedObject (const char* name) const;
  117. /** Attaches a named object to the context, which will be deleted when the context is
  118. destroyed.
  119. This allows you to store an object which will be released before the context is
  120. deleted. The main purpose is for caching GL objects such as shader programs, which
  121. will become invalid when the context is deleted.
  122. This method must only be called from within the GL rendering methods.
  123. */
  124. void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
  125. //==============================================================================
  126. /** Returns an OS-dependent handle to some kind of underlting OS-provided GL context.
  127. The exact type of the value returned will depend on the OS and may change
  128. if the implementation changes. If you want to use this, digging around in the
  129. native code is probably the best way to find out what it is.
  130. */
  131. void* getRawContext() const noexcept;
  132. //==============================================================================
  133. /** Draws the currently selected texture into this context at its original size.
  134. @param targetClipArea the target area to draw into (in top-left origin coords)
  135. @param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
  136. anchor position in the target space, and the size must be
  137. the total size of the texture.
  138. @param contextWidth the width of the context or framebuffer that is being drawn into,
  139. used for scaling of the coordinates.
  140. @param contextHeight the height of the context or framebuffer that is being drawn into,
  141. used for vertical flipping of the y coordinates.
  142. */
  143. void copyTexture (const Rectangle<int>& targetClipArea,
  144. const Rectangle<int>& anchorPosAndTextureSize,
  145. int contextWidth, int contextHeight);
  146. #ifndef DOXYGEN
  147. class NativeContext;
  148. #endif
  149. private:
  150. class CachedImage;
  151. class Attachment;
  152. NativeContext* nativeContext;
  153. OpenGLRenderer* renderer;
  154. ScopedPointer<Attachment> attachment;
  155. int width, height;
  156. bool renderComponents;
  157. CachedImage* getCachedImage() const noexcept;
  158. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext);
  159. };
  160. #endif // __JUCE_OPENGLCONTEXT_JUCEHEADER__