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.

264 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_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. /** Used to select the type of openGL API to use, if more than one choice is available
  38. on a particular platform.
  39. */
  40. enum OpenGLType
  41. {
  42. openGLDefault = 0,
  43. #if JUCE_IOS
  44. openGLES1, /**< On the iPhone, this selects openGL ES 1.0 */
  45. openGLES2 /**< On the iPhone, this selects openGL ES 2.0 */
  46. #endif
  47. };
  48. /** Creates an OpenGLComponent.
  49. If useBackgroundThread is true, the component will launch a background thread
  50. to do the rendering. If false, then renderOpenGL() will be called as part of the
  51. normal paint() method.
  52. */
  53. OpenGLComponent (OpenGLType type = openGLDefault,
  54. bool useBackgroundThread = false);
  55. /** Destructor. */
  56. ~OpenGLComponent();
  57. //==============================================================================
  58. /** Changes the pixel format used by this component.
  59. @see OpenGLPixelFormat::getAvailablePixelFormats()
  60. */
  61. void setPixelFormat (const OpenGLPixelFormat& formatToUse);
  62. /** Returns the pixel format that this component is currently using. */
  63. OpenGLPixelFormat getPixelFormat() const;
  64. /** Specifies an OpenGL context which should be shared with the one that this
  65. component is using.
  66. This is an OpenGL feature that lets two contexts share their texture data.
  67. Note that this pointer is stored by the component, and when the component
  68. needs to recreate its internal context for some reason, the same context
  69. will be used again to share lists. So if you pass a context in here,
  70. don't delete the context while this component is still using it! You can
  71. call shareWith (nullptr) to stop this component from sharing with it.
  72. */
  73. void shareWith (OpenGLContext* contextToShareListsWith);
  74. /** Returns the context that this component is sharing with.
  75. @see shareWith
  76. */
  77. OpenGLContext* getShareContext() const noexcept { return contextToShareListsWith; }
  78. //==============================================================================
  79. /** Flips the openGL buffers over. */
  80. void swapBuffers();
  81. /** Returns true if the component is performing the rendering on a background thread.
  82. This property is specified in the constructor.
  83. */
  84. bool isUsingDedicatedThread() const noexcept { return useThread; }
  85. /** This replaces the normal paint() callback - use it to draw your openGL stuff.
  86. When this is called, makeCurrentContextActive() will already have been called
  87. for you, so you just need to draw.
  88. */
  89. virtual void renderOpenGL() = 0;
  90. /** This method is called when the component creates a new OpenGL context.
  91. A new context may be created when the component is first used, or when it
  92. is moved to a different window, or when the window is hidden and re-shown,
  93. etc.
  94. You can use this callback as an opportunity to set up things like textures
  95. that your context needs.
  96. New contexts are created on-demand by the makeCurrentContextActive() method - so
  97. if the context is deleted, e.g. by changing the pixel format or window, no context
  98. will be created until the next call to makeCurrentContextActive(), which will
  99. synchronously create one and call this method. This means that if you're using
  100. a non-GUI thread for rendering, you can make sure this method is be called by
  101. your renderer thread.
  102. When this callback happens, the context will already have been made current
  103. using the makeCurrentContextActive() method, so there's no need to call it
  104. again in your code.
  105. */
  106. virtual void newOpenGLContextCreated() = 0;
  107. /** This method is called when the component shuts down its OpenGL context.
  108. You can use this callback to delete textures and any other OpenGL objects you
  109. created in the component's context. Be aware: if you are using a render
  110. thread, this may be called on the thread.
  111. When this callback happens, the context will have been made current
  112. using the makeCurrentContextActive() method, so there's no need to call it
  113. again in your code.
  114. */
  115. virtual void releaseOpenGLContext() {}
  116. //==============================================================================
  117. /** Returns the context that will draw into this component.
  118. This may return 0 if the component is currently invisible or hasn't currently
  119. got a context. The context object can be deleted and a new one created during
  120. the lifetime of this component, and there may be times when it doesn't have one.
  121. @see newOpenGLContextCreated()
  122. */
  123. OpenGLContext* getCurrentContext() const noexcept { return context; }
  124. /** Makes this component the currently active openGL context.
  125. If this returns false, then the context isn't active, so you should avoid
  126. making any calls.
  127. This call may actually create a context if one isn't currently initialised. If
  128. it does this, it will also synchronously call the newOpenGLContextCreated()
  129. method to let you initialise it as necessary.
  130. @see releaseAsRenderingTarget
  131. */
  132. bool makeCurrentRenderingTarget();
  133. /** Stops the current component being the active OpenGL context.
  134. This is the opposite of makeCurrentRenderingTarget()
  135. @see makeCurrentRenderingTarget
  136. */
  137. void releaseAsRenderingTarget();
  138. int getRenderingTargetWidth() const { return getWidth(); }
  139. int getRenderingTargetHeight() const { return getHeight(); }
  140. //==============================================================================
  141. /** Calls the rendering callback, and swaps the buffers afterwards.
  142. This is called automatically by paint() when the component needs to be rendered.
  143. Returns true if the operation succeeded.
  144. */
  145. virtual bool renderAndSwapBuffers();
  146. /** This returns a critical section that can be used to lock the current context.
  147. Because the context that is used by this component can change, e.g. when the
  148. component is shown or hidden, then if you're rendering to it on a background
  149. thread, this allows you to lock the context for the duration of your rendering
  150. routine.
  151. */
  152. CriticalSection& getContextLock() noexcept { return contextLock; }
  153. /** Delete the context.
  154. You should only need to call this if you've written a custom thread - if so, make
  155. sure that your thread calls this before it terminates.
  156. */
  157. void deleteContext();
  158. //==============================================================================
  159. /** Returns the native handle of an embedded heavyweight window, if there is one.
  160. E.g. On windows, this will return the HWND of the sub-window containing
  161. the opengl context, on the mac it'll be the NSOpenGLView.
  162. */
  163. void* getNativeWindowHandle() const;
  164. protected:
  165. /** Kicks off a thread to start rendering.
  166. The default implementation creates and manages an internal thread that tries
  167. to render at around 50fps, but this can be overloaded to create a custom thread.
  168. */
  169. virtual void startRenderThread();
  170. /** Cleans up the rendering thread.
  171. Used to shut down the thread that was started by startRenderThread(). If you've
  172. created a custom thread, then you should overload this to clean it up and delete it.
  173. */
  174. virtual void stopRenderThread();
  175. //==============================================================================
  176. /** @internal */
  177. void paint (Graphics& g);
  178. private:
  179. const OpenGLType type;
  180. class OpenGLComponentRenderThread;
  181. friend class OpenGLComponentRenderThread;
  182. friend class ScopedPointer <OpenGLComponentRenderThread>;
  183. ScopedPointer <OpenGLComponentRenderThread> renderThread;
  184. class OpenGLComponentWatcher;
  185. friend class OpenGLComponentWatcher;
  186. friend class ScopedPointer <OpenGLComponentWatcher>;
  187. ScopedPointer <OpenGLComponentWatcher> componentWatcher;
  188. ScopedPointer <OpenGLContext> context;
  189. OpenGLContext* contextToShareListsWith;
  190. CriticalSection contextLock;
  191. OpenGLPixelFormat preferredPixelFormat;
  192. bool needToUpdateViewport, needToDeleteContext, threadStarted;
  193. const bool useThread;
  194. OpenGLContext* createContext();
  195. void updateContext();
  196. void updateContextPosition();
  197. void stopBackgroundThread();
  198. void recreateContextAsync();
  199. void internalRepaint (int x, int y, int w, int h);
  200. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OpenGLComponent);
  201. };
  202. #endif // __JUCE_OPENGLCOMPONENT_JUCEHEADER__