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.

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