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.

244 lines
7.6KB

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