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.

259 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #ifndef JUCE_OPENGLCONTEXT_H_INCLUDED
  18. #define JUCE_OPENGLCONTEXT_H_INCLUDED
  19. #include "juce_OpenGLPixelFormat.h"
  20. #include "../native/juce_OpenGLExtensions.h"
  21. #include "juce_OpenGLRenderer.h"
  22. //==============================================================================
  23. /**
  24. Creates an OpenGL context, which can be attached to a component.
  25. To render some OpenGL, you should create an instance of an OpenGLContext,
  26. and call attachTo() to make it use a component as its render target.
  27. To provide threaded rendering, you can supply an OpenGLRenderer object that
  28. will be used to render each frame.
  29. Before your target component or OpenGLRenderer is deleted, you MUST call
  30. detach() or delete the OpenGLContext to allow the background thread to
  31. stop and the native resources to be freed safely.
  32. @see OpenGLRenderer
  33. */
  34. class JUCE_API OpenGLContext
  35. {
  36. public:
  37. OpenGLContext();
  38. /** Destructor. */
  39. ~OpenGLContext();
  40. //==============================================================================
  41. /** Gives the context an OpenGLRenderer to use to do the drawing.
  42. The object that you give it will not be owned by the context, so it's the caller's
  43. responsibility to manage its lifetime and make sure that it doesn't get deleted
  44. while the context may be using it. To stop the context using a renderer, just call
  45. this method with a null pointer.
  46. Note: This must be called BEFORE attaching your context to a target component!
  47. */
  48. void setRenderer (OpenGLRenderer* rendererToUse) noexcept;
  49. /** Enables or disables the use of the GL context to perform 2D rendering
  50. of the component to which it is attached.
  51. If this is false, then only your OpenGLRenderer will be used to perform
  52. any rendering. If true, then each time your target's paint() method needs
  53. to be called, an OpenGLGraphicsContext will be used to render it, (after
  54. calling your OpenGLRenderer if there is one).
  55. By default this is set to true. If you're not using any paint() method functionality
  56. and are doing all your rendering in an OpenGLRenderer, you should disable it
  57. to improve performance.
  58. Note: This must be called BEFORE attaching your context to a target component!
  59. */
  60. void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
  61. /** Sets the pixel format which you'd like to use for the target GL surface.
  62. Note: This must be called BEFORE attaching your context to a target component!
  63. */
  64. void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
  65. /** Provides a context with which you'd like this context's resources to be shared.
  66. The object passed-in here is a platform-dependent native context object, and
  67. must not be deleted while this context may still be using it! To turn off sharing,
  68. you can call this method with a null pointer.
  69. Note: This must be called BEFORE attaching your context to a target component!
  70. */
  71. void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
  72. /** Enables multisampling on platforms where this is implemented.
  73. If enabling this, you must call this method before attachTo().
  74. */
  75. void setMultisamplingEnabled (bool) noexcept;
  76. //==============================================================================
  77. /** Attaches the context to a target component.
  78. If the component is not fully visible, this call will wait until the component
  79. is shown before actually creating a native context for it.
  80. When a native context is created, a thread is started, and will be used to call
  81. the OpenGLRenderer methods. The context will be floated above the target component,
  82. and when the target moves, it will track it. If the component is hidden/shown, the
  83. context may be deleted and re-created.
  84. */
  85. void attachTo (Component& component);
  86. /** Detaches the context from its target component and deletes any native resources.
  87. If the context has not been attached, this will do nothing. Otherwise, it will block
  88. until the context and its thread have been cleaned up.
  89. */
  90. void detach();
  91. /** Returns true if the context is attached to a component and is on-screen.
  92. Note that if you call attachTo() for a non-visible component, this method will
  93. return false until the component is made visible.
  94. */
  95. bool isAttached() const noexcept;
  96. /** Returns the component to which this context is currently attached, or nullptr. */
  97. Component* getTargetComponent() const noexcept;
  98. /** Returns the context that's currently in active use by the calling thread, or
  99. nullptr if no context is active.
  100. */
  101. static OpenGLContext* getCurrentContext();
  102. /** Asynchronously causes a repaint to be made. */
  103. void triggerRepaint();
  104. //==============================================================================
  105. /** If this context is backed by a frame buffer, this returns its ID number,
  106. or 0 if the context does not use a framebuffer.
  107. */
  108. unsigned int getFrameBufferID() const noexcept;
  109. /** Returns true if shaders can be used in this context. */
  110. bool areShadersAvailable() const;
  111. /** This structure holds a set of dynamically loaded GL functions for use on this context. */
  112. OpenGLExtensionFunctions extensions;
  113. //==============================================================================
  114. /** This retrieves an object that was previously stored with setAssociatedObject().
  115. If no object is found with the given name, this will return nullptr.
  116. This method must only be called from within the GL rendering methods.
  117. @see setAssociatedObject
  118. */
  119. ReferenceCountedObject* getAssociatedObject (const char* name) const;
  120. /** Attaches a named object to the context, which will be deleted when the context is
  121. destroyed.
  122. This allows you to store an object which will be released before the context is
  123. deleted. The main purpose is for caching GL objects such as shader programs, which
  124. will become invalid when the context is deleted.
  125. This method must only be called from within the GL rendering methods.
  126. */
  127. void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
  128. //==============================================================================
  129. /** Makes this context the currently active one.
  130. You should never need to call this in normal use - the context will already be
  131. active when OpenGLRenderer::renderOpenGL() is invoked.
  132. */
  133. bool makeActive() const noexcept;
  134. /** Returns true if this context is currently active for the calling thread. */
  135. bool isActive() const noexcept;
  136. /** If any context is active on the current thread, this deactivates it.
  137. Note that on some platforms, like Android, this isn't possible.
  138. */
  139. static void deactivateCurrentContext();
  140. //==============================================================================
  141. /** Swaps the buffers (if the context can do this).
  142. There's normally no need to call this directly - the buffers will be swapped
  143. automatically after your OpenGLRenderer::renderOpenGL() method has been called.
  144. */
  145. void swapBuffers();
  146. /** Sets whether the context checks the vertical sync before swapping.
  147. The value is the number of frames to allow between buffer-swapping. This is
  148. fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
  149. and greater numbers indicate that it should swap less often.
  150. By default, this will be set to 1.
  151. Returns true if it sets the value successfully - some platforms won't support
  152. this setting.
  153. */
  154. bool setSwapInterval (int numFramesPerSwap);
  155. /** Returns the current swap-sync interval.
  156. See setSwapInterval() for info about the value returned.
  157. */
  158. int getSwapInterval() const;
  159. //==============================================================================
  160. /** Returns an OS-dependent handle to some kind of underlting OS-provided GL context.
  161. The exact type of the value returned will depend on the OS and may change
  162. if the implementation changes. If you want to use this, digging around in the
  163. native code is probably the best way to find out what it is.
  164. */
  165. void* getRawContext() const noexcept;
  166. //==============================================================================
  167. /** Draws the currently selected texture into this context at its original size.
  168. @param targetClipArea the target area to draw into (in top-left origin coords)
  169. @param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
  170. anchor position in the target space, and the size must be
  171. the total size of the texture.
  172. @param contextWidth the width of the context or framebuffer that is being drawn into,
  173. used for scaling of the coordinates.
  174. @param contextHeight the height of the context or framebuffer that is being drawn into,
  175. used for vertical flipping of the y coordinates.
  176. @param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
  177. (0, 0). If false, it is assumed to be (0, 1)
  178. */
  179. void copyTexture (const Rectangle<int>& targetClipArea,
  180. const Rectangle<int>& anchorPosAndTextureSize,
  181. int contextWidth, int contextHeight,
  182. bool textureOriginIsBottomLeft);
  183. //==============================================================================
  184. #ifndef DOXYGEN
  185. class NativeContext;
  186. #endif
  187. private:
  188. class CachedImage;
  189. class Attachment;
  190. NativeContext* nativeContext;
  191. OpenGLRenderer* renderer;
  192. ScopedPointer<Attachment> attachment;
  193. OpenGLPixelFormat pixelFormat;
  194. void* contextToShareWith;
  195. bool renderComponents, useMultisampling;
  196. CachedImage* getCachedImage() const noexcept;
  197. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
  198. };
  199. #endif // JUCE_OPENGLCONTEXT_H_INCLUDED