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.

255 lines
11KB

  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. #include "juce_OpenGLRenderer.h"
  23. //==============================================================================
  24. /**
  25. Creates an OpenGL context, which can be attached to a component.
  26. To render some OpenGL, you should create an instance of an OpenGLContext,
  27. and call attachTo() to make it use a component as its render target.
  28. To provide threaded rendering, you can supply an OpenGLRenderer object that
  29. will be used to render each frame.
  30. Before your target component or OpenGLRenderer is deleted, you MUST call
  31. detach() or delete the OpenGLContext to allow the background thread to
  32. stop and the native resources to be freed safely.
  33. @see OpenGLRenderer
  34. */
  35. class JUCE_API OpenGLContext
  36. {
  37. public:
  38. OpenGLContext();
  39. /** Destructor. */
  40. ~OpenGLContext();
  41. //==============================================================================
  42. /** Gives the context an OpenGLRenderer to use to do the drawing.
  43. The object that you give it will not be owned by the context, so it's the caller's
  44. responsibility to manage its lifetime and make sure that it doesn't get deleted
  45. while the context may be using it. To stop the context using a renderer, just call
  46. this method with a null pointer.
  47. Note: This must be called BEFORE attaching your context to a target component!
  48. */
  49. void setRenderer (OpenGLRenderer* rendererToUse) noexcept;
  50. /** Enables or disables the use of the GL context to perform 2D rendering
  51. of the component to which it is attached.
  52. If this is false, then only your OpenGLRenderer will be used to perform
  53. any rendering. If true, then each time your target's paint() method needs
  54. to be called, an OpenGLGraphicsContext will be used to render it, (after
  55. calling your OpenGLRenderer if there is one).
  56. By default this is set to true. If you're not using any paint() method functionality
  57. and are doing all your rendering in an OpenGLRenderer, you should disable it
  58. to improve performance.
  59. Note: This must be called BEFORE attaching your context to a target component!
  60. */
  61. void setComponentPaintingEnabled (bool shouldPaintComponent) noexcept;
  62. /** Sets the pixel format which you'd like to use for the target GL surface.
  63. Note: This must be called BEFORE attaching your context to a target component!
  64. */
  65. void setPixelFormat (const OpenGLPixelFormat& preferredPixelFormat) noexcept;
  66. /** Provides a context with which you'd like this context's resources to be shared.
  67. The object passed-in here is a platform-dependent native context object, and
  68. must not be deleted while this context may still be using it! To turn off sharing,
  69. you can call this method with a null pointer.
  70. Note: This must be called BEFORE attaching your context to a target component!
  71. */
  72. void setNativeSharedContext (void* nativeContextToShareWith) noexcept;
  73. //==============================================================================
  74. /** Attaches the context to a target component.
  75. If the component is not fully visible, this call will wait until the component
  76. is shown before actually creating a native context for it.
  77. When a native context is created, a thread is started, and will be used to call
  78. the OpenGLRenderer methods. The context will be floated above the target component,
  79. and when the target moves, it will track it. If the component is hidden/shown, the
  80. context may be deleted and re-created.
  81. */
  82. void attachTo (Component& component);
  83. /** Detaches the context from its target component and deletes any native resources.
  84. If the context has not been attached, this will do nothing. Otherwise, it will block
  85. until the context and its thread have been cleaned up.
  86. */
  87. void detach();
  88. /** Returns true if the context is attached to a component and is on-screen.
  89. Note that if you call attachTo() for a non-visible component, this method will
  90. return false until the component is made visible.
  91. */
  92. bool isAttached() const noexcept;
  93. /** Returns the component to which this context is currently attached, or nullptr. */
  94. Component* getTargetComponent() const noexcept;
  95. /** Returns the context that's currently in active use by the calling thread, or
  96. nullptr if no context is active.
  97. */
  98. static OpenGLContext* getCurrentContext();
  99. /** Asynchronously causes a repaint to be made. */
  100. void triggerRepaint();
  101. //==============================================================================
  102. /** If this context is backed by a frame buffer, this returns its ID number,
  103. or 0 if the context does not use a framebuffer.
  104. */
  105. unsigned int getFrameBufferID() const noexcept;
  106. /** Returns true if shaders can be used in this context. */
  107. bool areShadersAvailable() const;
  108. /** This structure holds a set of dynamically loaded GL functions for use on this context. */
  109. OpenGLExtensionFunctions extensions;
  110. //==============================================================================
  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. 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. /** Makes this context the currently active one.
  127. You should never need to call this in normal use - the context will already be
  128. active when OpenGLRenderer::renderOpenGL() is invoked.
  129. */
  130. bool makeActive() const noexcept;
  131. /** Returns true if this context is currently active for the calling thread. */
  132. bool isActive() const noexcept;
  133. /** If any context is active on the current thread, this deactivates it.
  134. Note that on some platforms, like Android, this isn't possible.
  135. */
  136. static void deactivateCurrentContext();
  137. //==============================================================================
  138. /** Swaps the buffers (if the context can do this).
  139. There's normally no need to call this directly - the buffers will be swapped
  140. automatically after your OpenGLRenderer::renderOpenGL() method has been called.
  141. */
  142. void swapBuffers();
  143. /** Sets whether the context checks the vertical sync before swapping.
  144. The value is the number of frames to allow between buffer-swapping. This is
  145. fairly system-dependent, but 0 turns off syncing, 1 makes it swap on frame-boundaries,
  146. and greater numbers indicate that it should swap less often.
  147. By default, this will be set to 1.
  148. Returns true if it sets the value successfully - some platforms won't support
  149. this setting.
  150. */
  151. bool setSwapInterval (int numFramesPerSwap);
  152. /** Returns the current swap-sync interval.
  153. See setSwapInterval() for info about the value returned.
  154. */
  155. int getSwapInterval() const;
  156. //==============================================================================
  157. /** Returns an OS-dependent handle to some kind of underlting OS-provided GL context.
  158. The exact type of the value returned will depend on the OS and may change
  159. if the implementation changes. If you want to use this, digging around in the
  160. native code is probably the best way to find out what it is.
  161. */
  162. void* getRawContext() const noexcept;
  163. //==============================================================================
  164. /** Draws the currently selected texture into this context at its original size.
  165. @param targetClipArea the target area to draw into (in top-left origin coords)
  166. @param anchorPosAndTextureSize the position of this rectangle is the texture's top-left
  167. anchor position in the target space, and the size must be
  168. the total size of the texture.
  169. @param contextWidth the width of the context or framebuffer that is being drawn into,
  170. used for scaling of the coordinates.
  171. @param contextHeight the height of the context or framebuffer that is being drawn into,
  172. used for vertical flipping of the y coordinates.
  173. @param textureOriginIsBottomLeft if true, the texture's origin is treated as being at
  174. (0, 0). If false, it is assumed to be (0, 1)
  175. */
  176. void copyTexture (const Rectangle<int>& targetClipArea,
  177. const Rectangle<int>& anchorPosAndTextureSize,
  178. int contextWidth, int contextHeight,
  179. bool textureOriginIsBottomLeft);
  180. //==============================================================================
  181. #ifndef DOXYGEN
  182. class NativeContext;
  183. #endif
  184. private:
  185. class CachedImage;
  186. class Attachment;
  187. NativeContext* nativeContext;
  188. OpenGLRenderer* renderer;
  189. ScopedPointer<Attachment> attachment;
  190. OpenGLPixelFormat pixelFormat;
  191. void* contextToShareWith;
  192. bool renderComponents;
  193. CachedImage* getCachedImage() const noexcept;
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLContext)
  195. };
  196. #endif // __JUCE_OPENGLCONTEXT_JUCEHEADER__