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.

247 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. extern ComponentPeer* createNonRepaintingEmbeddedWindowsPeer (Component*, void* parent);
  18. //==============================================================================
  19. class OpenGLContext::NativeContext
  20. {
  21. public:
  22. NativeContext (Component& component,
  23. const OpenGLPixelFormat& pixelFormat,
  24. void* contextToShareWith,
  25. bool /*useMultisampling*/)
  26. {
  27. createNativeWindow (component);
  28. PIXELFORMATDESCRIPTOR pfd;
  29. initialisePixelFormatDescriptor (pfd, pixelFormat);
  30. const int pixFormat = ChoosePixelFormat (dc, &pfd);
  31. if (pixFormat != 0)
  32. SetPixelFormat (dc, pixFormat, &pfd);
  33. renderContext = wglCreateContext (dc);
  34. if (renderContext != 0)
  35. {
  36. makeActive();
  37. initialiseGLExtensions();
  38. const int wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  39. deactivateCurrentContext();
  40. if (wglFormat != pixFormat && wglFormat != 0)
  41. {
  42. // can't change the pixel format of a window, so need to delete the
  43. // old one and create a new one..
  44. releaseDC();
  45. nativeWindow = nullptr;
  46. createNativeWindow (component);
  47. if (SetPixelFormat (dc, wglFormat, &pfd))
  48. {
  49. deleteRenderContext();
  50. renderContext = wglCreateContext (dc);
  51. }
  52. }
  53. if (contextToShareWith != nullptr)
  54. wglShareLists ((HGLRC) contextToShareWith, renderContext);
  55. component.getTopLevelComponent()->repaint();
  56. component.repaint();
  57. }
  58. }
  59. ~NativeContext()
  60. {
  61. deleteRenderContext();
  62. releaseDC();
  63. }
  64. void initialiseOnRenderThread (OpenGLContext&) {}
  65. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  66. static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
  67. bool makeActive() const noexcept { return wglMakeCurrent (dc, renderContext) != FALSE; }
  68. bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
  69. void swapBuffers() const noexcept { SwapBuffers (dc); }
  70. bool setSwapInterval (int numFramesPerSwap)
  71. {
  72. jassert (isActive()); // this can only be called when the context is active..
  73. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  74. }
  75. int getSwapInterval() const
  76. {
  77. jassert (isActive()); // this can only be called when the context is active..
  78. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  79. }
  80. void updateWindowPosition (const Rectangle<int>& bounds)
  81. {
  82. if (nativeWindow != nullptr)
  83. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  84. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  85. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  86. }
  87. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  88. void* getRawContext() const noexcept { return renderContext; }
  89. unsigned int getFrameBufferID() const noexcept { return 0; }
  90. struct Locker { Locker (NativeContext&) {} };
  91. private:
  92. Component dummyComponent;
  93. ScopedPointer<ComponentPeer> nativeWindow;
  94. HGLRC renderContext;
  95. HDC dc;
  96. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  97. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  98. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  99. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  100. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  101. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  102. void initialiseGLExtensions()
  103. {
  104. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  105. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  106. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  107. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  108. #undef JUCE_INIT_WGL_FUNCTION
  109. }
  110. void createNativeWindow (Component& component)
  111. {
  112. Component* topComp = component.getTopLevelComponent();
  113. nativeWindow = createNonRepaintingEmbeddedWindowsPeer (&dummyComponent, topComp->getWindowHandle());
  114. if (ComponentPeer* peer = topComp->getPeer())
  115. updateWindowPosition (peer->getAreaCoveredBy (component));
  116. nativeWindow->setVisible (true);
  117. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  118. }
  119. void deleteRenderContext()
  120. {
  121. if (renderContext != 0)
  122. {
  123. wglDeleteContext (renderContext);
  124. renderContext = 0;
  125. }
  126. }
  127. void releaseDC()
  128. {
  129. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  130. }
  131. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  132. {
  133. zerostruct (pfd);
  134. pfd.nSize = sizeof (pfd);
  135. pfd.nVersion = 1;
  136. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  137. pfd.iPixelType = PFD_TYPE_RGBA;
  138. pfd.iLayerType = PFD_MAIN_PLANE;
  139. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  140. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  141. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  142. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  143. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  144. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  145. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  146. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  147. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  148. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  149. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  150. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  151. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  152. }
  153. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  154. {
  155. int format = 0;
  156. if (wglChoosePixelFormatARB != nullptr)
  157. {
  158. int atts[64];
  159. int n = 0;
  160. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  161. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  162. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  163. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  164. atts[n++] = WGL_ACCELERATION_ARB;
  165. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  166. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  167. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  168. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  169. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  170. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  171. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  172. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  173. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  174. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  175. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  176. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  177. if (pixelFormat.multisamplingLevel > 0
  178. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  179. {
  180. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  181. atts[n++] = 1;
  182. atts[n++] = WGL_SAMPLES_ARB;
  183. atts[n++] = pixelFormat.multisamplingLevel;
  184. }
  185. atts[n++] = 0;
  186. jassert (n <= numElementsInArray (atts));
  187. UINT formatsCount = 0;
  188. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  189. }
  190. return format;
  191. }
  192. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  193. };
  194. //==============================================================================
  195. bool OpenGLHelpers::isContextActive()
  196. {
  197. return wglGetCurrentContext() != 0;
  198. }