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.

296 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. extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component* component, void* parent);
  19. //==============================================================================
  20. class WindowedGLContext : public OpenGLContext
  21. {
  22. public:
  23. WindowedGLContext (Component* const component_,
  24. HGLRC contextToShareWith,
  25. const OpenGLPixelFormat& pixelFormat)
  26. : renderContext (0),
  27. component (component_),
  28. dc (0)
  29. {
  30. initialiseGLExtensions();
  31. jassert (component != nullptr);
  32. createNativeWindow();
  33. PIXELFORMATDESCRIPTOR pfd;
  34. initialisePixelFormatDescriptor (pfd, pixelFormat);
  35. const int format = ChoosePixelFormat (dc, &pfd);
  36. if (format != 0)
  37. SetPixelFormat (dc, format, &pfd);
  38. renderContext = wglCreateContext (dc);
  39. if (renderContext != 0)
  40. {
  41. makeActive();
  42. initialiseGLExtensions();
  43. extensions.initialise();
  44. setPixelFormat (pixelFormat);
  45. if (contextToShareWith != 0)
  46. wglShareLists (contextToShareWith, renderContext);
  47. }
  48. }
  49. ~WindowedGLContext()
  50. {
  51. properties.clear(); // to release any stored programs, etc that may be held in properties.
  52. makeInactive();
  53. if (renderContext != 0)
  54. {
  55. wglDeleteContext (renderContext);
  56. renderContext = 0;
  57. }
  58. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  59. nativeWindow = nullptr;
  60. }
  61. bool makeActive() const noexcept
  62. {
  63. jassert (renderContext != 0);
  64. return wglMakeCurrent (dc, renderContext) != 0;
  65. }
  66. bool makeInactive() const noexcept
  67. {
  68. return (! isActive()) || (wglMakeCurrent (0, 0) != 0);
  69. }
  70. bool isActive() const noexcept
  71. {
  72. return wglGetCurrentContext() == renderContext;
  73. }
  74. void* getRawContext() const noexcept { return renderContext; }
  75. unsigned int getFrameBufferID() const { return 0; }
  76. int getWidth() const { return component->getWidth(); }
  77. int getHeight() const { return component->getHeight(); }
  78. bool setPixelFormat (const OpenGLPixelFormat& pixelFormat)
  79. {
  80. makeActive();
  81. PIXELFORMATDESCRIPTOR pfd;
  82. initialisePixelFormatDescriptor (pfd, pixelFormat);
  83. int format = 0;
  84. if (wglChoosePixelFormatARB != nullptr)
  85. {
  86. int atts[64];
  87. int n = 0;
  88. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  89. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  90. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  91. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  92. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pfd.cColorBits;
  93. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  94. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  95. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  96. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  97. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  98. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  99. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  100. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  101. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  102. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  103. if (pixelFormat.multisamplingLevel > 0
  104. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  105. {
  106. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  107. atts[n++] = 1;
  108. atts[n++] = WGL_SAMPLES_ARB;
  109. atts[n++] = pixelFormat.multisamplingLevel;
  110. }
  111. atts[n++] = 0;
  112. jassert (n <= numElementsInArray (atts));
  113. UINT formatsCount = 0;
  114. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  115. }
  116. if (format == 0)
  117. format = ChoosePixelFormat (dc, &pfd);
  118. if (format != 0)
  119. {
  120. makeInactive();
  121. // win32 can't change the pixel format of a window, so need to delete the
  122. // old one and create a new one..
  123. jassert (nativeWindow != 0);
  124. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  125. nativeWindow = nullptr;
  126. createNativeWindow();
  127. if (SetPixelFormat (dc, format, &pfd))
  128. {
  129. wglDeleteContext (renderContext);
  130. renderContext = wglCreateContext (dc);
  131. jassert (renderContext != 0);
  132. return renderContext != 0;
  133. }
  134. }
  135. return false;
  136. }
  137. void swapBuffers()
  138. {
  139. SwapBuffers (dc);
  140. }
  141. bool setSwapInterval (int numFramesPerSwap)
  142. {
  143. makeActive();
  144. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  145. }
  146. int getSwapInterval() const
  147. {
  148. makeActive();
  149. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  150. }
  151. void* getNativeWindowHandle() const
  152. {
  153. return nativeWindow != nullptr ? nativeWindow->getNativeHandle() : nullptr;
  154. }
  155. //==============================================================================
  156. HGLRC renderContext;
  157. ScopedPointer<ComponentPeer> nativeWindow;
  158. private:
  159. Component* const component;
  160. HDC dc;
  161. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  162. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  163. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  164. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  165. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  166. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  167. void initialiseGLExtensions()
  168. {
  169. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  170. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  171. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  172. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  173. #undef JUCE_INIT_WGL_FUNCTION
  174. }
  175. //==============================================================================
  176. void createNativeWindow()
  177. {
  178. nativeWindow = createNonRepaintingEmbeddedWindowsPeer (component, component->getTopLevelComponent()->getWindowHandle());
  179. nativeWindow->setVisible (true);
  180. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  181. component->getTopLevelComponent()->repaint();
  182. }
  183. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  184. {
  185. zerostruct (pfd);
  186. pfd.nSize = sizeof (pfd);
  187. pfd.nVersion = 1;
  188. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  189. pfd.iPixelType = PFD_TYPE_RGBA;
  190. pfd.iLayerType = PFD_MAIN_PLANE;
  191. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  192. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  193. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  194. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  195. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  196. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  197. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  198. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  199. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  200. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  201. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  202. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  203. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  204. }
  205. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  206. };
  207. //==============================================================================
  208. OpenGLContext* OpenGLComponent::createContext()
  209. {
  210. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (this,
  211. contextToShareListsWith != nullptr ? (HGLRC) contextToShareListsWith->getRawContext() : 0,
  212. preferredPixelFormat));
  213. return (c->renderContext != 0) ? c.release() : nullptr;
  214. }
  215. void* OpenGLComponent::getNativeWindowHandle() const
  216. {
  217. return context != nullptr ? static_cast<WindowedGLContext*> (context.get())->getNativeWindowHandle() : nullptr;
  218. }
  219. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  220. {
  221. const ScopedLock sl (contextLock);
  222. if (context != nullptr)
  223. {
  224. ComponentPeer* peer = static_cast<WindowedGLContext*> (context.get())->nativeWindow;
  225. SetWindowPos ((HWND) peer->getNativeHandle(), 0,
  226. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  227. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  228. }
  229. }
  230. //==============================================================================
  231. bool OpenGLHelpers::isContextActive()
  232. {
  233. return wglGetCurrentContext() != 0;
  234. }