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.7KB

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