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. 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. {
  76. }
  77. void shutdownOnRenderThread()
  78. {
  79. JUCE_CHECK_OPENGL_ERROR
  80. freeGLBuffers();
  81. }
  82. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  83. void* getRawContext() const noexcept { return glLayer; }
  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. void swapBuffers()
  97. {
  98. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  99. [context presentRenderbuffer: GL_RENDERBUFFER];
  100. if (needToRebuildBuffers)
  101. {
  102. needToRebuildBuffers = false;
  103. freeGLBuffers();
  104. createGLBuffers();
  105. makeActive();
  106. }
  107. }
  108. void updateWindowPosition (const Rectangle<int>& bounds)
  109. {
  110. view.frame = getCGRectFor (bounds);
  111. if (lastWidth != bounds.getWidth() || lastHeight != bounds.getHeight())
  112. {
  113. lastWidth = bounds.getWidth();
  114. lastHeight = bounds.getHeight();
  115. needToRebuildBuffers = true;
  116. }
  117. }
  118. bool setSwapInterval (const int numFramesPerSwap) noexcept
  119. {
  120. swapFrames = numFramesPerSwap;
  121. return false;
  122. }
  123. int getSwapInterval() const noexcept { return swapFrames; }
  124. private:
  125. JuceGLView* view;
  126. CAEAGLLayer* glLayer;
  127. EAGLContext* context;
  128. GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle;
  129. int volatile lastWidth, lastHeight;
  130. bool volatile needToRebuildBuffers;
  131. int swapFrames;
  132. bool useDepthBuffer;
  133. //==============================================================================
  134. static CGRect getCGRectFor (const Rectangle<int>& bounds)
  135. {
  136. return CGRectMake ((CGFloat) bounds.getX(),
  137. (CGFloat) bounds.getY(),
  138. (CGFloat) bounds.getWidth(),
  139. (CGFloat) bounds.getHeight());
  140. }
  141. void createGLBuffers()
  142. {
  143. glGenFramebuffers (1, &frameBufferHandle);
  144. glGenRenderbuffers (1, &colorBufferHandle);
  145. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  146. bool ok = [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: glLayer];
  147. jassert (ok); (void) ok;
  148. if (useDepthBuffer)
  149. {
  150. GLint width, height;
  151. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &width);
  152. glGetRenderbufferParameteriv (GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT, &height);
  153. glGenRenderbuffers (1, &depthBufferHandle);
  154. glBindRenderbuffer (GL_RENDERBUFFER, depthBufferHandle);
  155. glRenderbufferStorage (GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, width, height);
  156. }
  157. glBindRenderbuffer (GL_RENDERBUFFER, colorBufferHandle);
  158. glBindFramebuffer (GL_FRAMEBUFFER, frameBufferHandle);
  159. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, colorBufferHandle);
  160. if (useDepthBuffer)
  161. glFramebufferRenderbuffer (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferHandle);
  162. jassert (glCheckFramebufferStatus (GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
  163. JUCE_CHECK_OPENGL_ERROR
  164. }
  165. void freeGLBuffers()
  166. {
  167. JUCE_CHECK_OPENGL_ERROR
  168. [context renderbufferStorage: GL_RENDERBUFFER fromDrawable: nil];
  169. if (frameBufferHandle != 0)
  170. {
  171. glDeleteFramebuffers (1, &frameBufferHandle);
  172. frameBufferHandle = 0;
  173. }
  174. if (colorBufferHandle != 0)
  175. {
  176. glDeleteRenderbuffers (1, &colorBufferHandle);
  177. colorBufferHandle = 0;
  178. }
  179. if (depthBufferHandle != 0)
  180. {
  181. glDeleteRenderbuffers (1, &depthBufferHandle);
  182. depthBufferHandle = 0;
  183. }
  184. JUCE_CHECK_OPENGL_ERROR
  185. }
  186. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext);
  187. };
  188. //==============================================================================
  189. bool OpenGLHelpers::isContextActive()
  190. {
  191. return [EAGLContext currentContext] != nil;
  192. }