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.

315 lines
10.0KB

  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. auto w = roundToInt (lastBounds.getWidth() * glLayer.contentsScale);
  130. auto h = roundToInt (lastBounds.getHeight() * glLayer.contentsScale);
  131. glBlitFramebuffer (0, 0, w, h,
  132. 0, 0, w, h,
  133. GL_COLOR_BUFFER_BIT,
  134. GL_NEAREST);
  135. }
  136. else
  137. {
  138. glResolveMultisampleFramebufferAPPLE();
  139. }
  140. }
  141. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  142. [context presentRenderbuffer: GL_RENDERBUFFER];
  143. if (needToRebuildBuffers)
  144. {
  145. needToRebuildBuffers = false;
  146. freeGLBuffers();
  147. createGLBuffers();
  148. makeActive();
  149. }
  150. }
  151. void updateWindowPosition (Rectangle<int> bounds)
  152. {
  153. view.frame = convertToCGRect (bounds);
  154. glLayer.contentsScale = (CGFloat) (Desktop::getInstance().getDisplays().getPrimaryDisplay()->scale
  155. / component.getDesktopScaleFactor());
  156. if (lastBounds != bounds)
  157. {
  158. lastBounds = bounds;
  159. needToRebuildBuffers = true;
  160. }
  161. }
  162. bool setSwapInterval (int numFramesPerSwap) noexcept
  163. {
  164. swapFrames = numFramesPerSwap;
  165. return false;
  166. }
  167. int getSwapInterval() const noexcept { return swapFrames; }
  168. struct Locker { Locker (NativeContext&) {} };
  169. private:
  170. Component& component;
  171. JuceGLView* view = nil;
  172. CAEAGLLayer* glLayer = nil;
  173. EAGLContext* context = nil;
  174. const OpenGLVersion openGLversion;
  175. const bool useDepthBuffer, useMSAA;
  176. GLuint frameBufferHandle = 0, colorBufferHandle = 0, depthBufferHandle = 0,
  177. msaaColorHandle = 0, msaaBufferHandle = 0;
  178. Rectangle<int> lastBounds;
  179. int swapFrames = 0;
  180. bool needToRebuildBuffers = false;
  181. bool createContext (EAGLRenderingAPI type, void* contextToShare)
  182. {
  183. jassert (context == nil);
  184. context = [EAGLContext alloc];
  185. context = contextToShare != nullptr
  186. ? [context initWithAPI: type sharegroup: [(EAGLContext*) contextToShare sharegroup]]
  187. : [context initWithAPI: type];
  188. return context != nil;
  189. }
  190. void releaseContext()
  191. {
  192. [context release];
  193. context = nil;
  194. }
  195. //==============================================================================
  196. void createGLBuffers()
  197. {
  198. glGenFramebuffers (1, &frameBufferHandle);
  199. glGenRenderbuffers (1, &colorBufferHandle);
  200. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  201. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  202. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
  203. bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
  204. jassert (ok); ignoreUnused (ok);
  205. GLint width, height;
  206. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  207. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  208. if (useMSAA)
  209. {
  210. glGenFramebuffers (1, &msaaBufferHandle);
  211. glGenRenderbuffers (1, &msaaColorHandle);
  212. glBindFramebuffer (GL_FRAMEBUFFER, msaaBufferHandle);
  213. glBindRenderbuffer (GL_RENDERBUFFER, msaaColorHandle);
  214. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_RGBA8, width, height);
  215. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColorHandle);
  216. }
  217. if (useDepthBuffer)
  218. {
  219. glGenRenderbuffers (1, &depthBufferHandle);
  220. glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
  221. if (useMSAA)
  222. glRenderbufferStorageMultisample (GL_RENDERBUFFER, 4, GL_DEPTH_COMPONENT16, width, height);
  223. else
  224. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  225. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
  226. }
  227. jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  228. JUCE_CHECK_OPENGL_ERROR
  229. }
  230. void freeGLBuffers()
  231. {
  232. JUCE_CHECK_OPENGL_ERROR
  233. [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  234. deleteFrameBuffer (frameBufferHandle);
  235. deleteFrameBuffer (msaaBufferHandle);
  236. deleteRenderBuffer (colorBufferHandle);
  237. deleteRenderBuffer (depthBufferHandle);
  238. deleteRenderBuffer (msaaColorHandle);
  239. JUCE_CHECK_OPENGL_ERROR
  240. }
  241. static void deleteFrameBuffer (GLuint& i) { if (i != 0) glDeleteFramebuffers (1, &i); i = 0; }
  242. static void deleteRenderBuffer (GLuint& i) { if (i != 0) glDeleteRenderbuffers (1, &i); i = 0; }
  243. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  244. };
  245. //==============================================================================
  246. bool OpenGLHelpers::isContextActive()
  247. {
  248. return [EAGLContext currentContext] != nil;
  249. }
  250. } // namespace juce