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.

311 lines
9.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  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. extern "C" GLvoid glResolveMultisampleFramebufferAPPLE();
  30. namespace juce
  31. {
  32. class OpenGLContext::NativeContext
  33. {
  34. public:
  35. NativeContext (Component& c,
  36. const OpenGLPixelFormat& pixFormat,
  37. void* contextToShare,
  38. bool multisampling,
  39. OpenGLVersion version)
  40. : component (c), openGLversion (version),
  41. useDepthBuffer (pixFormat.depthBufferBits > 0),
  42. useMSAA (multisampling)
  43. {
  44. JUCE_AUTORELEASEPOOL
  45. {
  46. if (auto* peer = component.getPeer())
  47. {
  48. auto bounds = peer->getAreaCoveredBy (component);
  49. view = [[JuceGLView alloc] initWithFrame: convertToCGRect (bounds)];
  50. view.opaque = YES;
  51. view.hidden = NO;
  52. view.backgroundColor = [UIColor blackColor];
  53. view.userInteractionEnabled = NO;
  54. glLayer = (CAEAGLLayer*) [view layer];
  55. glLayer.opaque = true;
  56. updateWindowPosition (bounds);
  57. [((UIView*) peer->getNativeHandle()) addSubview: view];
  58. if (version == openGL3_2 && [[UIDevice currentDevice].systemVersion floatValue] >= 7.0)
  59. {
  60. if (! createContext (kEAGLRenderingAPIOpenGLES3, contextToShare))
  61. {
  62. releaseContext();
  63. createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
  64. }
  65. }
  66. else
  67. {
  68. createContext (kEAGLRenderingAPIOpenGLES2, contextToShare);
  69. }
  70. if (context != nil)
  71. {
  72. // I'd prefer to put this stuff in the initialiseOnRenderThread() call, but doing
  73. // so causes mysterious timing-related failures.
  74. [EAGLContext setCurrentContext: context];
  75. createGLBuffers();
  76. deactivateCurrentContext();
  77. }
  78. else
  79. {
  80. jassertfalse;
  81. }
  82. }
  83. else
  84. {
  85. jassertfalse;
  86. }
  87. }
  88. }
  89. ~NativeContext()
  90. {
  91. releaseContext();
  92. [view removeFromSuperview];
  93. [view release];
  94. }
  95. bool initialiseOnRenderThread (OpenGLContext&) { return true; }
  96. void shutdownOnRenderThread()
  97. {
  98. JUCE_CHECK_OPENGL_ERROR
  99. freeGLBuffers();
  100. deactivateCurrentContext();
  101. }
  102. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  103. void* getRawContext() const noexcept { return context; }
  104. GLuint getFrameBufferID() const noexcept { return useMSAA ? msaaBufferHandle : frameBufferHandle; }
  105. bool makeActive() const noexcept
  106. {
  107. if (! [EAGLContext setCurrentContext: context])
  108. return false;
  109. glBindFramebuffer (GL_FRAMEBUFFER, useMSAA ? msaaBufferHandle
  110. : frameBufferHandle);
  111. return true;
  112. }
  113. bool isActive() const noexcept
  114. {
  115. return [EAGLContext currentContext] == context;
  116. }
  117. static void deactivateCurrentContext()
  118. {
  119. [EAGLContext setCurrentContext: nil];
  120. }
  121. void swapBuffers()
  122. {
  123. if (useMSAA)
  124. {
  125. glBindFramebuffer (GL_DRAW_FRAMEBUFFER, frameBufferHandle);
  126. glBindFramebuffer (GL_READ_FRAMEBUFFER, msaaBufferHandle);
  127. if (openGLversion >= openGL3_2)
  128. {
  129. glBlitFramebuffer (0, 0, lastBounds.getWidth(), lastBounds.getHeight(),
  130. 0, 0, lastBounds.getWidth(), lastBounds.getHeight(),
  131. GL_COLOR_BUFFER_BIT, GL_NEAREST);
  132. }
  133. else
  134. {
  135. glResolveMultisampleFramebufferAPPLE();
  136. }
  137. }
  138. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  139. [context presentRenderbuffer: GL_RENDERBUFFER];
  140. if (needToRebuildBuffers)
  141. {
  142. needToRebuildBuffers = false;
  143. freeGLBuffers();
  144. createGLBuffers();
  145. makeActive();
  146. }
  147. }
  148. void updateWindowPosition (Rectangle<int> bounds)
  149. {
  150. view.frame = convertToCGRect (bounds);
  151. glLayer.contentsScale = (CGFloat) (Desktop::getInstance().getDisplays().getMainDisplay().scale
  152. / component.getDesktopScaleFactor());
  153. if (lastBounds != bounds)
  154. {
  155. lastBounds = bounds;
  156. needToRebuildBuffers = true;
  157. }
  158. }
  159. bool setSwapInterval (int numFramesPerSwap) noexcept
  160. {
  161. swapFrames = numFramesPerSwap;
  162. return false;
  163. }
  164. int getSwapInterval() const noexcept { return swapFrames; }
  165. struct Locker { Locker (NativeContext&) {} };
  166. private:
  167. Component& component;
  168. JuceGLView* view = nil;
  169. CAEAGLLayer* glLayer = nil;
  170. EAGLContext* context = nil;
  171. const OpenGLVersion openGLversion;
  172. const bool useDepthBuffer, useMSAA;
  173. GLuint frameBufferHandle = 0, colorBufferHandle = 0, depthBufferHandle = 0,
  174. msaaColorHandle = 0, msaaBufferHandle = 0;
  175. Rectangle<int> lastBounds;
  176. int swapFrames = 0;
  177. bool needToRebuildBuffers = false;
  178. bool createContext (EAGLRenderingAPI type, void* contextToShare)
  179. {
  180. jassert (context == nil);
  181. context = [EAGLContext alloc];
  182. context = contextToShare != nullptr
  183. ? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
  184. : [context initWithAPI: type];
  185. return context != nil;
  186. }
  187. void releaseContext()
  188. {
  189. [context release];
  190. context = nil;
  191. }
  192. //==============================================================================
  193. void createGLBuffers()
  194. {
  195. glGenFramebuffers (1, &frameBufferHandle);
  196. glGenRenderbuffers (1, &colorBufferHandle);
  197. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  198. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  199. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
  200. bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
  201. jassert (ok); ignoreUnused (ok);
  202. GLint width, height;
  203. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  204. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  205. if (useMSAA)
  206. {
  207. glGenFramebuffers (1, &msaaBufferHandle);
  208. glGenRenderbuffers (1, &msaaColorHandle);
  209. glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
  210. glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
  211. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
  212. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
  213. }
  214. if (useDepthBuffer)
  215. {
  216. glGenRenderbuffers (1, &depthBufferHandle);
  217. glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
  218. if (useMSAA)
  219. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
  220. else
  221. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  222. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
  223. }
  224. jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  225. JUCE_CHECK_OPENGL_ERROR
  226. }
  227. void freeGLBuffers()
  228. {
  229. JUCE_CHECK_OPENGL_ERROR
  230. [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  231. deleteFrameBuffer (frameBufferHandle);
  232. deleteFrameBuffer (msaaBufferHandle);
  233. deleteRenderBuffer (colorBufferHandle);
  234. deleteRenderBuffer (depthBufferHandle);
  235. deleteRenderBuffer (msaaColorHandle);
  236. JUCE_CHECK_OPENGL_ERROR
  237. }
  238. static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
  239. static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
  240. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  241. };
  242. //==============================================================================
  243. bool OpenGLHelpers::isContextActive()
  244. {
  245. return [EAGLContext currentContext] != nil;
  246. }
  247. } // namespace juce