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.

295 lines
9.7KB

  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 version)
  39. : context (nil), frameBufferHandle (0), colorBufferHandle (0),
  40. depthBufferHandle (0), 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 = (CGFloat) Desktop::getInstance().getDisplays().getMainDisplay().scale;
  59. glLayer.opaque = true;
  60. [((UIView*) peer->getNativeHandle()) addSubview: view];
  61. #if defined (__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
  62. if (version == OpenGLContext::openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
  63. {
  64. if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
  65. {
  66. releaseContext();
  67. createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
  68. }
  69. }
  70. else
  71. #endif
  72. {
  73. createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
  74. }
  75. jassert (context != nil);
  76. // I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
  77. // so causes myserious timing-related failures.
  78. [EAGLContext setCurrentContext: context];
  79. createGLBuffers();
  80. deactivateCurrentContext();
  81. }
  82. }
  83. ~NativeContext()
  84. {
  85. releaseContext();
  86. [view removeFromSuperview];
  87. [view release];
  88. }
  89. void initialiseOnRenderThread (OpenGLContext&) {}
  90. void shutdownOnRenderThread()
  91. {
  92. JUCE_CHECK_OPENGL_ERROR
  93. freeGLBuffers();
  94. deactivateCurrentContext();
  95. }
  96. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  97. void* getRawContext() const noexcept { return context; }
  98. GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
  99. bool makeActive() const noexcept
  100. {
  101. if (! [EAGLContext setCurrentContext: context])
  102. return false;
  103. glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
  104. : frameBufferHandle);
  105. return true;
  106. }
  107. bool isActive() const noexcept
  108. {
  109. return [EAGLContext currentContext] == context;
  110. }
  111. static void deactivateCurrentContext()
  112. {
  113. [EAGLContext setCurrentContext: nil];
  114. }
  115. void swapBuffers()
  116. {
  117. if (useMSAA)
  118. {
  119. glBindFramebuffer (GL_DRAW_FRAMEBUFFER, frameBufferHandle);
  120. glBindFramebuffer (GL_READ_FRAMEBUFFER, msaaBufferHandle);
  121. #if defined (__IPHONE_7_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
  122. glBlitFramebuffer (0, 0, lastWidth, lastHeight, 0, 0, lastWidth, lastHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
  123. #else
  124. glResolveMultisampleFramebufferAPPLE();
  125. #endif
  126. }
  127. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  128. [context presentRenderbuffer: GL_RENDERBUFFER];
  129. if (needToRebuildBuffers)
  130. {
  131. needToRebuildBuffers = false;
  132. freeGLBuffers();
  133. createGLBuffers();
  134. makeActive();
  135. }
  136. }
  137. void updateWindowPosition (const Rectangle<int>& bounds)
  138. {
  139. view.frame = convertToCGRect (bounds);
  140. if (lastWidth != bounds.getWidth() || lastHeight != bounds.getHeight())
  141. {
  142. lastWidth = bounds.getWidth();
  143. lastHeight = bounds.getHeight();
  144. needToRebuildBuffers = true;
  145. }
  146. }
  147. bool setSwapInterval (const int numFramesPerSwap) noexcept
  148. {
  149. swapFrames = numFramesPerSwap;
  150. return false;
  151. }
  152. int getSwapInterval() const noexcept { return swapFrames; }
  153. struct Locker { Locker (NativeContext&) {} };
  154. private:
  155. JuceGLView* view;
  156. CAEAGLLayer* glLayer;
  157. EAGLContext* context;
  158. GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle,
  159. msaaColorHandle, msaaBufferHandle;
  160. int volatile lastWidth, lastHeight;
  161. bool volatile needToRebuildBuffers;
  162. int swapFrames;
  163. bool useDepthBuffer, useMSAA;
  164. bool createContext (EAGLRenderingAPI type, void* contextToShare)
  165. {
  166. jassert (context == nil);
  167. context = [EAGLContext alloc];
  168. context = contextToShare != nullptr
  169. ? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
  170. : [context initWithAPI: type];
  171. return context != nil;
  172. }
  173. void releaseContext()
  174. {
  175. [context release];
  176. context = nil;
  177. }
  178. //==============================================================================
  179. void createGLBuffers()
  180. {
  181. glGenFramebuffers (1, &frameBufferHandle);
  182. glGenRenderbuffers (1, &colorBufferHandle);
  183. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  184. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  185. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
  186. bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
  187. jassert (ok); (void) ok;
  188. GLint width, height;
  189. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  190. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  191. if (useMSAA)
  192. {
  193. glGenFramebuffers (1, &msaaBufferHandle);
  194. glGenRenderbuffers (1, &msaaColorHandle);
  195. glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
  196. glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
  197. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
  198. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
  199. }
  200. if (useDepthBuffer)
  201. {
  202. glGenRenderbuffers (1, &depthBufferHandle);
  203. glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
  204. if (useMSAA)
  205. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
  206. else
  207. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  208. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
  209. }
  210. jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  211. JUCE_CHECK_OPENGL_ERROR
  212. }
  213. void freeGLBuffers()
  214. {
  215. JUCE_CHECK_OPENGL_ERROR
  216. [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  217. deleteFrameBuffer (frameBufferHandle);
  218. deleteFrameBuffer (msaaBufferHandle);
  219. deleteRenderBuffer (colorBufferHandle);
  220. deleteRenderBuffer (depthBufferHandle);
  221. deleteRenderBuffer (msaaColorHandle);
  222. JUCE_CHECK_OPENGL_ERROR
  223. }
  224. static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
  225. static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
  226. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  227. };
  228. //==============================================================================
  229. bool OpenGLHelpers::isContextActive()
  230. {
  231. return [EAGLContext currentContext] != nil;
  232. }