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.

297 lines
12KB

  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_OPENGLCOMPONENT_JUCEHEADER__
  19. #define __JUCE_OPENGLCOMPONENT_JUCEHEADER__
  20. #include "juce_OpenGLContext.h"
  21. #if JUCE_MAC && ! defined (DOXYGEN)
  22. typedef NSViewComponent OpenGLBaseType;
  23. #else
  24. typedef Component OpenGLBaseType;
  25. #endif
  26. class OpenGLGraphicsContext;
  27. //==============================================================================
  28. /**
  29. A component that contains an OpenGL canvas.
  30. Override this, add it to whatever component you want to, and use the renderOpenGL()
  31. method to draw its contents.
  32. Important note! When using a GL component with a background thread, your sub-class
  33. must call stopRenderThread() in its destructor. See stopRenderThread() for
  34. more details.
  35. */
  36. class JUCE_API OpenGLComponent : public OpenGLBaseType
  37. {
  38. public:
  39. //==============================================================================
  40. /** These flags can be combined and passed to the OpenGLComponent constructor to
  41. specify various options.
  42. */
  43. enum OpenGLFlags
  44. {
  45. /** This value can be used if you want your OpenGLComponent to use the
  46. default settings.
  47. */
  48. openGLDefault = 8,
  49. #if JUCE_IOS
  50. openGLES1 = 1, /**< On the iPhone, this selects openGL ES 1.0 */
  51. openGLES2 = 2, /**< On the iPhone, this selects openGL ES 2.0 */
  52. #endif
  53. /** If this flag is enabled, the component will launch a background thread to
  54. perform the rendering. If this flag is not enabled, then renderOpenGL()
  55. will be invoked on the main event thread when the component has been told to
  56. repaint, or after triggerRepaint() has been called.
  57. Important note! When using a background thread, your sub-class MUST call
  58. stopRenderThread() in its destructor. See stopRenderThread() for
  59. more details.
  60. */
  61. useBackgroundThread = 4,
  62. /** If this flag is enabled, then any sub-components of the OpenGLComponent
  63. will be correctly overlaid on top of the GL content, and its paint() method will
  64. be able to render over it. If you're not using sub-components, you can disable
  65. this flag, which will eliminate some overhead.
  66. */
  67. allowSubComponents = 8
  68. };
  69. //==============================================================================
  70. /** Creates an OpenGLComponent.
  71. The flags parameter should be a combination of the values in the
  72. OpenGLFlags enum.
  73. */
  74. OpenGLComponent (int flags = openGLDefault);
  75. /** Destructor. */
  76. ~OpenGLComponent();
  77. //==============================================================================
  78. /** Changes the pixel format used by this component. */
  79. void setPixelFormat (const OpenGLPixelFormat& formatToUse);
  80. /** Specifies an OpenGL context which should be shared with the one that this
  81. component is using.
  82. This is an OpenGL feature that lets two contexts share their texture data.
  83. Note that this pointer is stored by the component, and when the component
  84. needs to recreate its internal context for some reason, the same context
  85. will be used again to share lists. So if you pass a context in here,
  86. don't delete the context while this component is still using it! You can
  87. call shareWith (nullptr) to stop this component from sharing with it.
  88. */
  89. void shareWith (OpenGLContext* contextToShareListsWith);
  90. /** Returns the context that this component is sharing with.
  91. @see shareWith
  92. */
  93. OpenGLContext* getShareContext() const noexcept { return contextToShareListsWith; }
  94. //==============================================================================
  95. /** Flips the openGL buffers over. */
  96. void swapBuffers();
  97. /** Returns true if the component is performing the rendering on a background thread.
  98. This property is specified in the constructor.
  99. */
  100. inline bool isUsingDedicatedThread() const noexcept { return (flags & useBackgroundThread) != 0; }
  101. /** Shuts down the rendering thread.
  102. This must be called by your sub-class's destructor, to make sure that all rendering
  103. callbacks have stopped before your class starts to be destroyed.
  104. */
  105. void stopRenderThread();
  106. //==============================================================================
  107. /** This callback is where your subclass should draw its openGL content.
  108. When this is called, makeCurrentContextActive() will already have been called
  109. for you, so you just need to draw.
  110. */
  111. virtual void renderOpenGL() = 0;
  112. /** This method is called when the component creates a new OpenGL context.
  113. A new context may be created when the component is first used, or when it
  114. is moved to a different window, or when the window is hidden and re-shown,
  115. etc.
  116. You can use this callback as an opportunity to set up things like textures
  117. that your context needs.
  118. New contexts are created on-demand by the makeCurrentContextActive() method - so
  119. if the context is deleted, e.g. by changing the pixel format or window, no context
  120. will be created until the next call to makeCurrentContextActive(), which will
  121. synchronously create one and call this method. This means that if you're using
  122. a non-GUI thread for rendering, you can make sure this method is be called by
  123. your renderer thread.
  124. When this callback happens, the context will already have been made current
  125. using the makeCurrentContextActive() method, so there's no need to call it
  126. again in your code.
  127. */
  128. virtual void newOpenGLContextCreated();
  129. /** This method is called when the component shuts down its OpenGL context.
  130. You can use this callback to delete textures and any other OpenGL objects you
  131. created in the component's context. Be aware: if you are using a render
  132. thread, this may be called on the thread.
  133. When this callback happens, the context will have been made current
  134. using the makeCurrentContextActive() method, so there's no need to call it
  135. again in your code.
  136. */
  137. virtual void releaseOpenGLContext();
  138. //==============================================================================
  139. /** Returns the context that will draw into this component.
  140. This may return 0 if the component is currently invisible or hasn't currently
  141. got a context. The context object can be deleted and a new one created during
  142. the lifetime of this component, and there may be times when it doesn't have one.
  143. @see newOpenGLContextCreated()
  144. */
  145. OpenGLContext* getCurrentContext() const noexcept { return context; }
  146. /** Makes this component the currently active openGL context.
  147. If this returns false, then the context isn't active, so you should avoid
  148. making any calls.
  149. This call may actually create a context if one isn't currently initialised. If
  150. it does this, it will also synchronously call the newOpenGLContextCreated()
  151. method to let you initialise it as necessary.
  152. @see releaseAsRenderingTarget
  153. */
  154. bool makeCurrentRenderingTarget();
  155. /** Stops the current component being the active OpenGL context.
  156. This is the opposite of makeCurrentRenderingTarget()
  157. @see makeCurrentRenderingTarget
  158. */
  159. void releaseAsRenderingTarget();
  160. /** Causes a repaint to be invoked asynchronously.
  161. This has a similar effect to calling repaint(), and triggers a callback to
  162. renderOpenGL(), but unlike repaint(), it does not mark any of the component's
  163. children as needing a redraw, which means that their cached state can be re-used
  164. if possible.
  165. */
  166. void triggerRepaint();
  167. //==============================================================================
  168. /** This returns a critical section that can be used to lock the current context.
  169. Because the context that is used by this component can change, e.g. when the
  170. component is shown or hidden, then if you're rendering to it on a background
  171. thread, this allows you to lock the context for the duration of your rendering
  172. routine.
  173. */
  174. CriticalSection& getContextLock() noexcept { return contextLock; }
  175. /** Delete the context.
  176. You should only need to call this if you've written a custom thread - if so, make
  177. sure that your thread calls this before it terminates.
  178. */
  179. void deleteContext();
  180. /** Tries to synchronously delete and re-create the context.
  181. If the context doesn't exist already, this will try to create one.
  182. If it exists, it'll first delete the existing one, and create a new one.
  183. You may need to call this if you require a temporary context for some reason
  184. before the normal call to newOpenGLContextCreated() is made.
  185. @returns true if a new context has been successfully created - this may not be
  186. possible on all platforms.
  187. */
  188. bool rebuildContext();
  189. /** If this component is backed by a frame buffer, this returns its ID number, or
  190. 0 if the component has no accessible framebuffer.
  191. */
  192. unsigned int getFrameBufferID() const;
  193. //==============================================================================
  194. /** Returns the native handle of an embedded heavyweight window, if there is one.
  195. E.g. On windows, this will return the HWND of the sub-window containing
  196. the opengl context, on the mac it'll be the NSOpenGLView.
  197. */
  198. void* getNativeWindowHandle() const;
  199. /** @internal */
  200. void paint (Graphics&);
  201. private:
  202. const int flags;
  203. class OpenGLComponentRenderThread;
  204. friend class OpenGLComponentRenderThread;
  205. friend class ScopedPointer <OpenGLComponentRenderThread>;
  206. ScopedPointer <OpenGLComponentRenderThread> renderThread;
  207. class OpenGLComponentWatcher;
  208. friend class OpenGLComponentWatcher;
  209. friend class ScopedPointer <OpenGLComponentWatcher>;
  210. ScopedPointer <OpenGLComponentWatcher> componentWatcher;
  211. ScopedPointer <OpenGLContext> context;
  212. OpenGLContext* contextToShareListsWith;
  213. CriticalSection contextLock;
  214. OpenGLPixelFormat preferredPixelFormat;
  215. bool needToUpdateViewport, needToDeleteContext, needToRepaint;
  216. class OpenGLCachedComponentImage;
  217. friend class OpenGLCachedComponentImage;
  218. OpenGLCachedComponentImage* cachedImage;
  219. OpenGLContext* createContext();
  220. void updateContext();
  221. void updateContextPosition();
  222. void recreateContextAsync();
  223. void updateEmbeddedPosition (const Rectangle<int>&);
  224. void startRenderThread();
  225. bool performRender();
  226. void paintSelf (OpenGLGraphicsContext&);
  227. int renderAndSwapBuffers(); // (This method has been deprecated)
  228. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLComponent);
  229. };
  230. #endif // __JUCE_OPENGLCOMPONENT_JUCEHEADER__