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.

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