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.

300 lines
10KB

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