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.

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