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.

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