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.

248 lines
10.0KB

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