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.

259 lines
8.4KB

  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. } // (juce namespace)
  18. @interface JuceGLView : UIView
  19. {
  20. }
  21. + (Class) layerClass;
  22. @end
  23. @implementation JuceGLView
  24. + (Class) layerClass
  25. {
  26. return [CAEAGLLayer class];
  27. }
  28. @end
  29. namespace juce
  30. {
  31. class OpenGLContext::NativeContext
  32. {
  33. public:
  34. NativeContext (Component& component,
  35. const OpenGLPixelFormat& pixFormat,
  36. void* contextToShare,
  37. bool multisampling,
  38. OpenGLVersion)
  39. : frameBufferHandle (0), colorBufferHandle (0), depthBufferHandle (0),
  40. msaaColorHandle (0), msaaBufferHandle (0),
  41. lastWidth (0), lastHeight (0), needToRebuildBuffers (false),
  42. swapFrames (0), useDepthBuffer (pixFormat.depthBufferBits > 0),
  43. useMSAA (multisampling)
  44. {
  45. JUCE_AUTORELEASEPOOL
  46. {
  47. ComponentPeer* const peer = component.getPeer();
  48. jassert (peer != nullptr);
  49. const Rectangle<int> bounds (peer->getComponent().getLocalArea (&component, component.getLocalBounds()));
  50. lastWidth = bounds.getWidth();
  51. lastHeight = bounds.getHeight();
  52. view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
  53. view.opaque = YES;
  54. view.hidden = NO;
  55. view.backgroundColor = [UIColor blackColor];
  56. view.userInteractionEnabled = NO;
  57. glLayer = (CAEAGLLayer*) [view layer];
  58. glLayer.contentsScale = Desktop::getInstance().getDisplays().getMainDisplay().scale;
  59. glLayer.opaque = true;
  60. [((UIView*) peer->getNativeHandle()) addSubview: view];
  61. context = [EAGLContext alloc];
  62. const NSUInteger type = kEAGLRenderingAPIOpenGLES2;
  63. if (contextToShare != nullptr)
  64. [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]];
  65. else
  66. [context initWithAPI: type];
  67. // I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
  68. // so causes myserious timing-related failures.
  69. [EAGLContext setCurrentContext: context];
  70. createGLBuffers();
  71. deactivateCurrentContext();
  72. }
  73. }
  74. ~NativeContext()
  75. {
  76. [context release];
  77. context = nil;
  78. [view removeFromSuperview];
  79. [view release];
  80. }
  81. void initialiseOnRenderThread (OpenGLContext&) {}
  82. void shutdownOnRenderThread()
  83. {
  84. JUCE_CHECK_OPENGL_ERROR
  85. freeGLBuffers();
  86. deactivateCurrentContext();
  87. }
  88. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  89. void* getRawContext() const noexcept { return context; }
  90. GLuint getFrameBufferID() const noexcept { return frameBufferHandle; }
  91. bool makeActive() const noexcept
  92. {
  93. if (! [EAGLContext setCurrentContext: context])
  94. return false;
  95. glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
  96. : frameBufferHandle);
  97. return true;
  98. }
  99. bool isActive() const noexcept
  100. {
  101. return [EAGLContext currentContext] == context;
  102. }
  103. static void deactivateCurrentContext()
  104. {
  105. [EAGLContext setCurrentContext: nil];
  106. }
  107. void swapBuffers()
  108. {
  109. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  110. [context presentRenderbuffer: GL_RENDERBUFFER];
  111. if (needToRebuildBuffers)
  112. {
  113. needToRebuildBuffers = false;
  114. freeGLBuffers();
  115. createGLBuffers();
  116. makeActive();
  117. }
  118. }
  119. void updateWindowPosition (const Rectangle<int>& bounds)
  120. {
  121. view.frame = convertToCGRect (bounds);
  122. if (lastWidth != bounds.getWidth() || lastHeight != bounds.getHeight())
  123. {
  124. lastWidth = bounds.getWidth();
  125. lastHeight = bounds.getHeight();
  126. needToRebuildBuffers = true;
  127. }
  128. }
  129. bool setSwapInterval (const int numFramesPerSwap) noexcept
  130. {
  131. swapFrames = numFramesPerSwap;
  132. return false;
  133. }
  134. int getSwapInterval() const noexcept { return swapFrames; }
  135. struct Locker { Locker (NativeContext&) {} };
  136. private:
  137. JuceGLView* view;
  138. CAEAGLLayer* glLayer;
  139. EAGLContext* context;
  140. GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle,
  141. msaaColorHandle, msaaBufferHandle;
  142. int volatile lastWidth, lastHeight;
  143. bool volatile needToRebuildBuffers;
  144. int swapFrames;
  145. bool useDepthBuffer, useMSAA;
  146. //==============================================================================
  147. void createGLBuffers()
  148. {
  149. glGenFramebuffers (1, &frameBufferHandle);
  150. glGenRenderbuffers (1, &colorBufferHandle);
  151. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  152. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  153. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
  154. bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
  155. jassert (ok); (void) ok;
  156. GLint width, height;
  157. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  158. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  159. if (useMSAA)
  160. {
  161. glGenFramebuffers (1, &msaaBufferHandle);
  162. glGenRenderbuffers (1, &msaaColorHandle);
  163. glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
  164. glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
  165. glRenderbufferStorageMultisampleAPPLE (GL_RENDERBUFFER, 4, GL_RGBA8_OES, width, height);
  166. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
  167. }
  168. if (useDepthBuffer)
  169. {
  170. glGenRenderbuffers (1, &depthBufferHandle);
  171. glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
  172. if (useMSAA)
  173. glRenderbufferStorageMultisampleAPPLE (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
  174. else
  175. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  176. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
  177. }
  178. jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  179. JUCE_CHECK_OPENGL_ERROR
  180. }
  181. void freeGLBuffers()
  182. {
  183. JUCE_CHECK_OPENGL_ERROR
  184. [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  185. deleteFrameBuffer (frameBufferHandle);
  186. deleteFrameBuffer (msaaBufferHandle);
  187. deleteRenderBuffer (colorBufferHandle);
  188. deleteRenderBuffer (depthBufferHandle);
  189. deleteRenderBuffer (msaaColorHandle);
  190. JUCE_CHECK_OPENGL_ERROR
  191. }
  192. static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
  193. static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  195. };
  196. //==============================================================================
  197. bool OpenGLHelpers::isContextActive()
  198. {
  199. return [EAGLContext currentContext] != nil;
  200. }