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.

299 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. //==============================================================================
  20. /**
  21. Creates an OpenGL context, which can be attached to a component.
  22. To render some OpenGL, you should create an instance of an OpenGLContext,
  23. and call attachTo() to make it use a component as its render target.
  24. To provide threaded rendering, you can supply an OpenGLRenderer object that
  25. will be used to render each frame.
  26. Before your target component or OpenGLRenderer is deleted, you MUST call
  27. detach() or delete the OpenGLContext to allow the background thread to
  28. stop and the native resources to be freed safely.
  29. @see OpenGLRenderer
  30. */
  31. class JUCE_API OpenGLContext
  32. {
  33. public:
  34. OpenGLContext();
  35. /** Destructor. */
  36. ~OpenGLContext();
  37. //==============================================================================
  38. /** Gives the context an OpenGLRenderer to use to do the drawing.
  39. The object that you give it will not be owned by the context, so it's the caller's
  40. responsibility to manage its lifetime and make sure that it doesn't get deleted
  41. while the context may be using it. To stop the context using a renderer, just call
  42. this method with a null pointer.
  43. Note: This must be called BEFORE attaching your context to a target component!
  44. */
  45. void setRenderer (OpenGLRenderer*) noexcept;
  46. /** Attaches the context to a target component.
  47. If the component is not fully visible, this call will wait until the component
  48. is shown before actually creating a native context for it.
  49. When a native context is created, a thread is started, and will be used to call
  50. the OpenGLRenderer methods. The context will be floated above the target component,
  51. and when the target moves, it will track it. If the component is hidden/shown, the
  52. context may be deleted and re-created.
  53. */
  54. void attachTo (Component&);
  55. /** Detaches the context from its target component and deletes any native resources.
  56. If the context has not been attached, this will do nothing. Otherwise, it will block
  57. until the context and its thread have been cleaned up.
  58. */
  59. void detach();
  60. /** Returns true if the context is attached to a component and is on-screen.
  61. Note that if you call attachTo() for a non-visible component, this method will
  62. return false until the component is made visible.
  63. */
  64. bool isAttached() const noexcept;
  65. /** Returns the component to which this context is currently attached, or nullptr. */
  66. Component* getTargetComponent() const noexcept;
  67. /** If the given component has an OpenGLContext attached, then this will return it. */
  68. static OpenGLContext* getContextAttachedTo (Component& component) noexcept;
  69. //==============================================================================
  70. /** Sets the pixel format which you'd like to use for the target GL surface.
  71. Note: This must be called BEFORE attaching your context to a target component!
  72. */
  73. void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
  74. /** Provides a context with which you'd like this context's resources to be shared.
  75. The object passed-in here is a platform-dependent native context object, and
  76. must not be deleted while this context may still be using it! To turn off sharing,
  77. you can call this method with a null pointer.
  78. Note: This must be called BEFORE attaching your context to a target component!
  79. */
  80. void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
  81. /** Enables multisampling on platforms where this is implemented.
  82. If enabling this, you must call this method before attachTo().
  83. */
  84. void setMultisamplingEnabled (bool) noexcept;
  85. /** Returns true if shaders can be used in this context. */
  86. bool areShadersAvailable() const;
  87. /** OpenGL versions, used by setOpenGLVersionRequired(). */
  88. enum OpenGLVersion
  89. {
  90. defaultGLVersion = 0,
  91. openGL3_2
  92. };
  93. /** Sets a preference for the version of GL that this context should use, if possible.
  94. Some platforms may ignore this value.
  95. */
  96. void setOpenGLVersionRequired (OpenGLVersion) noexcept;
  97. /** Enables or disables the use of the GL context to perform 2D rendering
  98. of the component to which it is attached.
  99. If this is false, then only your OpenGLRenderer will be used to perform
  100. any rendering. If true, then each time your target's paint() method needs
  101. to be called, an OpenGLGraphicsContext will be used to render it, (after
  102. calling your OpenGLRenderer if there is one).
  103. By default this is set to true. If you're not using any paint() method functionality
  104. and are doing all your rendering in an OpenGLRenderer, you should disable it
  105. to improve performance.
  106. Note: This must be called BEFORE attaching your context to a target component!
  107. */
  108. void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
  109. /** Enables or disables continuous repainting.
  110. If set to true, the context will run a loop, re-rendering itself without waiting
  111. for triggerRepaint() to be called, at a frequency determined by the swap interval
  112. (see setSwapInterval). If false, then after each render callback, it will wait for
  113. another call to triggerRepaint() before rendering again.
  114. This is disabled by default.
  115. @see setSwapInterval
  116. */
  117. void setContinuousRepainting (bool shouldContinuouslyRepaint) noexcept;
  118. /** Asynchronously causes a repaint to be made. */
  119. void triggerRepaint();
  120. //==============================================================================
  121. /** This retrieves an object that was previously stored with setAssociatedObject().
  122. If no object is found with the given name, this will return nullptr.
  123. This method must only be called from within the GL rendering methods.
  124. @see setAssociatedObject
  125. */
  126. ReferenceCountedObject* getAssociatedObject (const char* name) const;
  127. /** Attaches a named object to the context, which will be deleted when the context is
  128. destroyed.
  129. This allows you to store an object which will be released before the context is
  130. deleted. The main purpose is for caching GL objects such as shader programs, which
  131. will become invalid when the context is deleted.
  132. This method must only be called from within the GL rendering methods.
  133. */
  134. void setAssociatedObject (const char* name, ReferenceCountedObject* newObject);
  135. //==============================================================================
  136. /** Makes this context the currently active one.
  137. You should never need to call this in normal use - the context will already be
  138. active when OpenGLRenderer::renderOpenGL() is invoked.
  139. */
  140. bool makeActive() const noexcept;
  141. /** Returns true if this context is currently active for the calling thread. */
  142. bool isActive() const noexcept;
  143. /** If any context is active on the current thread, this deactivates it.
  144. Note that on some platforms, like Android, this isn't possible.
  145. */
  146. static void deactivateCurrentContext();
  147. /** Returns the context that's currently in active use by the calling thread, or
  148. nullptr if no context is active.
  149. */
  150. static OpenGLContext* getCurrentContext();
  151. //==============================================================================
  152. /** Swaps the buffers (if the context can do this).
  153. There's normally no need to call this directly - the buffers will be swapped
  154. automatically after your OpenGLRenderer::renderOpenGL() method has been called.
  155. */
  156. void swapBuffers();
  157. /** Sets whether the context checks the vertical sync before swapping.
  158. The value is the number of frames to allow between buffer-swapping. This is
  159. fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
  160. and greater numbers indicate that it should swap less often.
  161. By default, this will be set to 1.
  162. Returns true if it sets the value successfully - some platforms won't support
  163. this setting.
  164. @see setContinuousRepainting
  165. */
  166. bool setSwapInterval (int numFramesPerSwap);
  167. /** Returns the current swap-sync interval.
  168. See setSwapInterval() for info about the value returned.
  169. */
  170. int getSwapInterval() const;
  171. //==============================================================================
  172. /** Returns the scale factor used by the display that is being rendered.
  173. The scale is that of the display - see Desktop::Displays::Display::scale
  174. Note that this should only be called during an OpenGLRenderer::renderOpenGL()
  175. callback - at other times the value it returns is undefined.
  176. */
  177. double getRenderingScale() const noexcept { return currentRenderScale; }
  178. //==============================================================================
  179. /** If this context is backed by a frame buffer, this returns its ID number,
  180. or 0 if the context does not use a framebuffer.
  181. */
  182. unsigned int getFrameBufferID() const noexcept;
  183. /** Returns an OS-dependent handle to some kind of underlting OS-provided GL context.
  184. The exact type of the value returned will depend on the OS and may change
  185. if the implementation changes. If you want to use this, digging around in the
  186. native code is probably the best way to find out what it is.
  187. */
  188. void* getRawContext() const noexcept;
  189. /** This structure holds a set of dynamically loaded GL functions for use on this context. */
  190. OpenGLExtensionFunctions extensions;
  191. //==============================================================================
  192. /** Draws the currently selected texture into this context at its original size.
  193. @param targetClipArea the target area to draw into (in top-left origin coords)
  194. @param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
  195. anchor position in the target space, and the size must be
  196. the total size of the texture.
  197. @param contextWidth the width of the context or framebuffer that is being drawn into,
  198. used for scaling of the coordinates.
  199. @param contextHeight the height of the context or framebuffer that is being drawn into,
  200. used for vertical flipping of the y coordinates.
  201. @param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
  202. (0, 0). If false, it is assumed to be (0, 1)
  203. */
  204. void copyTexture (const Rectangle<int>& targetClipArea,
  205. const Rectangle<int>& anchorPosAndTextureSize,
  206. int contextWidth, int contextHeight,
  207. bool textureOriginIsBottomLeft);
  208. /** Changes the amount of GPU memory that the internal cache for Images is allowed to use. */
  209. void setImageCacheSize (size_t cacheSizeBytes) noexcept;
  210. /** Returns the amount of GPU memory that the internal cache for Images is allowed to use. */
  211. size_t getImageCacheSize() const noexcept;
  212. //==============================================================================
  213. #ifndef DOXYGEN
  214. class NativeContext;
  215. #endif
  216. private:
  217. class CachedImage;
  218. class Attachment;
  219. NativeContext* nativeContext;
  220. OpenGLRenderer* renderer;
  221. double currentRenderScale;
  222. ScopedPointer<Attachment> attachment;
  223. OpenGLPixelFormat openGLPixelFormat;
  224. void* contextToShareWith;
  225. OpenGLVersion versionRequired;
  226. size_t imageCacheMaxSize;
  227. bool renderComponents, useMultisampling, continuousRepaint;
  228. CachedImage* getCachedImage() const noexcept;
  229. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
  230. };
  231. #endif // JUCE_OPENGLCONTEXT_H_INCLUDED