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.

243 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. {
  26. createNativeWindow (component);
  27. PIXELFORMATDESCRIPTOR pfd;
  28. initialisePixelFormatDescriptor (pfd, pixelFormat);
  29. const int pixFormat = ChoosePixelFormat (dc, &pfd);
  30. if (pixFormat != 0)
  31. SetPixelFormat (dc, pixFormat, &pfd);
  32. renderContext = wglCreateContext (dc);
  33. if (renderContext != 0)
  34. {
  35. makeActive();
  36. initialiseGLExtensions();
  37. const int wglFormat = wglChoosePixelFormatExtension (pixelFormat);
  38. deactivateCurrentContext();
  39. if (wglFormat != pixFormat && wglFormat != 0)
  40. {
  41. // can't change the pixel format of a window, so need to delete the
  42. // old one and create a new one..
  43. releaseDC();
  44. nativeWindow = nullptr;
  45. createNativeWindow (component);
  46. if (SetPixelFormat (dc, wglFormat, &pfd))
  47. {
  48. deleteRenderContext();
  49. renderContext = wglCreateContext (dc);
  50. }
  51. }
  52. if (contextToShareWith != nullptr)
  53. wglShareLists ((HGLRC) contextToShareWith, renderContext);
  54. component.getTopLevelComponent()->repaint();
  55. component.repaint();
  56. }
  57. }
  58. ~NativeContext()
  59. {
  60. deleteRenderContext();
  61. releaseDC();
  62. }
  63. void initialiseOnRenderThread (OpenGLContext&) {}
  64. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  65. static void deactivateCurrentContext() { wglMakeCurrent (0, 0); }
  66. bool makeActive() const noexcept { return wglMakeCurrent (dc, renderContext) != FALSE; }
  67. bool isActive() const noexcept { return wglGetCurrentContext() == renderContext; }
  68. void swapBuffers() const noexcept { SwapBuffers (dc); }
  69. bool setSwapInterval (int numFramesPerSwap)
  70. {
  71. jassert (isActive()); // this can only be called when the context is active..
  72. return wglSwapIntervalEXT != nullptr && wglSwapIntervalEXT (numFramesPerSwap) != FALSE;
  73. }
  74. int getSwapInterval() const
  75. {
  76. jassert (isActive()); // this can only be called when the context is active..
  77. return wglGetSwapIntervalEXT != nullptr ? wglGetSwapIntervalEXT() : 0;
  78. }
  79. void updateWindowPosition (const Rectangle<int>& bounds)
  80. {
  81. if (nativeWindow != nullptr)
  82. SetWindowPos ((HWND) nativeWindow->getNativeHandle(), 0,
  83. bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(),
  84. SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  85. }
  86. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  87. void* getRawContext() const noexcept { return renderContext; }
  88. unsigned int getFrameBufferID() const noexcept { return 0; }
  89. struct Locker { Locker (NativeContext&) {} };
  90. private:
  91. Component dummyComponent;
  92. ScopedPointer<ComponentPeer> nativeWindow;
  93. HGLRC renderContext;
  94. HDC dc;
  95. #define JUCE_DECLARE_WGL_EXTENSION_FUNCTION(name, returnType, params) \
  96. typedef returnType (__stdcall *type_ ## name) params; type_ ## name name;
  97. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglChoosePixelFormatARB, BOOL, (HDC, const int*, const FLOAT*, UINT, int*, UINT*))
  98. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglSwapIntervalEXT, BOOL, (int))
  99. JUCE_DECLARE_WGL_EXTENSION_FUNCTION (wglGetSwapIntervalEXT, int, ())
  100. #undef JUCE_DECLARE_WGL_EXTENSION_FUNCTION
  101. void initialiseGLExtensions()
  102. {
  103. #define JUCE_INIT_WGL_FUNCTION(name) name = (type_ ## name) OpenGLHelpers::getExtensionFunction (#name);
  104. JUCE_INIT_WGL_FUNCTION (wglChoosePixelFormatARB);
  105. JUCE_INIT_WGL_FUNCTION (wglSwapIntervalEXT);
  106. JUCE_INIT_WGL_FUNCTION (wglGetSwapIntervalEXT);
  107. #undef JUCE_INIT_WGL_FUNCTION
  108. }
  109. void createNativeWindow (Component& component)
  110. {
  111. Component* topComp = component.getTopLevelComponent();
  112. nativeWindow = createNonRepaintingEmbeddedWindowsPeer (&dummyComponent, topComp->getWindowHandle());
  113. updateWindowPosition (topComp->getLocalArea (&component, component.getLocalBounds()));
  114. nativeWindow->setVisible (true);
  115. dc = GetDC ((HWND) nativeWindow->getNativeHandle());
  116. }
  117. void deleteRenderContext()
  118. {
  119. if (renderContext != 0)
  120. {
  121. wglDeleteContext (renderContext);
  122. renderContext = 0;
  123. }
  124. }
  125. void releaseDC()
  126. {
  127. ReleaseDC ((HWND) nativeWindow->getNativeHandle(), dc);
  128. }
  129. static void initialisePixelFormatDescriptor (PIXELFORMATDESCRIPTOR& pfd, const OpenGLPixelFormat& pixelFormat)
  130. {
  131. zerostruct (pfd);
  132. pfd.nSize = sizeof (pfd);
  133. pfd.nVersion = 1;
  134. pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
  135. pfd.iPixelType = PFD_TYPE_RGBA;
  136. pfd.iLayerType = PFD_MAIN_PLANE;
  137. pfd.cColorBits = (BYTE) (pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits);
  138. pfd.cRedBits = (BYTE) pixelFormat.redBits;
  139. pfd.cGreenBits = (BYTE) pixelFormat.greenBits;
  140. pfd.cBlueBits = (BYTE) pixelFormat.blueBits;
  141. pfd.cAlphaBits = (BYTE) pixelFormat.alphaBits;
  142. pfd.cDepthBits = (BYTE) pixelFormat.depthBufferBits;
  143. pfd.cStencilBits = (BYTE) pixelFormat.stencilBufferBits;
  144. pfd.cAccumBits = (BYTE) (pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  145. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits);
  146. pfd.cAccumRedBits = (BYTE) pixelFormat.accumulationBufferRedBits;
  147. pfd.cAccumGreenBits = (BYTE) pixelFormat.accumulationBufferGreenBits;
  148. pfd.cAccumBlueBits = (BYTE) pixelFormat.accumulationBufferBlueBits;
  149. pfd.cAccumAlphaBits = (BYTE) pixelFormat.accumulationBufferAlphaBits;
  150. }
  151. int wglChoosePixelFormatExtension (const OpenGLPixelFormat& pixelFormat) const
  152. {
  153. int format = 0;
  154. if (wglChoosePixelFormatARB != nullptr)
  155. {
  156. int atts[64];
  157. int n = 0;
  158. atts[n++] = WGL_DRAW_TO_WINDOW_ARB; atts[n++] = GL_TRUE;
  159. atts[n++] = WGL_SUPPORT_OPENGL_ARB; atts[n++] = GL_TRUE;
  160. atts[n++] = WGL_DOUBLE_BUFFER_ARB; atts[n++] = GL_TRUE;
  161. atts[n++] = WGL_PIXEL_TYPE_ARB; atts[n++] = WGL_TYPE_RGBA_ARB;
  162. atts[n++] = WGL_ACCELERATION_ARB;
  163. atts[n++] = WGL_FULL_ACCELERATION_ARB;
  164. atts[n++] = WGL_COLOR_BITS_ARB; atts[n++] = pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits;
  165. atts[n++] = WGL_RED_BITS_ARB; atts[n++] = pixelFormat.redBits;
  166. atts[n++] = WGL_GREEN_BITS_ARB; atts[n++] = pixelFormat.greenBits;
  167. atts[n++] = WGL_BLUE_BITS_ARB; atts[n++] = pixelFormat.blueBits;
  168. atts[n++] = WGL_ALPHA_BITS_ARB; atts[n++] = pixelFormat.alphaBits;
  169. atts[n++] = WGL_DEPTH_BITS_ARB; atts[n++] = pixelFormat.depthBufferBits;
  170. atts[n++] = WGL_STENCIL_BITS_ARB; atts[n++] = pixelFormat.stencilBufferBits;
  171. atts[n++] = WGL_ACCUM_RED_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferRedBits;
  172. atts[n++] = WGL_ACCUM_GREEN_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferGreenBits;
  173. atts[n++] = WGL_ACCUM_BLUE_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferBlueBits;
  174. atts[n++] = WGL_ACCUM_ALPHA_BITS_ARB; atts[n++] = pixelFormat.accumulationBufferAlphaBits;
  175. if (pixelFormat.multisamplingLevel > 0
  176. && OpenGLHelpers::isExtensionSupported ("GL_ARB_multisample"))
  177. {
  178. atts[n++] = WGL_SAMPLE_BUFFERS_ARB;
  179. atts[n++] = 1;
  180. atts[n++] = WGL_SAMPLES_ARB;
  181. atts[n++] = pixelFormat.multisamplingLevel;
  182. }
  183. atts[n++] = 0;
  184. jassert (n <= numElementsInArray (atts));
  185. UINT formatsCount = 0;
  186. wglChoosePixelFormatARB (dc, atts, nullptr, 1, &format, &formatsCount);
  187. }
  188. return format;
  189. }
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  191. };
  192. //==============================================================================
  193. bool OpenGLHelpers::isContextActive()
  194. {
  195. return wglGetCurrentContext() != 0;
  196. }