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.

242 lines
7.8KB

  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. END_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. BEGIN_JUCE_NAMESPACE
  31. //==============================================================================
  32. class GLESContext : public OpenGLContext
  33. {
  34. public:
  35. GLESContext (UIView* parentView,
  36. Component* const component_,
  37. const OpenGLPixelFormat& pixelFormat,
  38. const GLESContext* const sharedContext,
  39. NSUInteger apiType)
  40. : component (component_), glLayer (nil), context (nil),
  41. useDepthBuffer (pixelFormat.depthBufferBits > 0),
  42. frameBufferHandle (0), colorBufferHandle (0),
  43. depthBufferHandle (0), lastWidth (0), lastHeight (0)
  44. {
  45. view = [[JuceGLView alloc] initWithFrame: CGRectMake (0, 0, 64, 64)];
  46. view.opaque = YES;
  47. view.hidden = NO;
  48. view.backgroundColor = [UIColor blackColor];
  49. view.userInteractionEnabled = NO;
  50. glLayer = (CAEAGLLayer*) [view layer];
  51. [parentView addSubview: view];
  52. if (sharedContext != nullptr)
  53. context = [[EAGLContext alloc] initWithAPI: apiType
  54. sharegroup: [sharedContext->context sharegroup]];
  55. else
  56. context = [[EAGLContext alloc] initWithAPI: apiType];
  57. createGLBuffers();
  58. }
  59. ~GLESContext()
  60. {
  61. deleteContext();
  62. [view removeFromSuperview];
  63. [view release];
  64. freeGLBuffers();
  65. }
  66. void deleteContext()
  67. {
  68. makeInactive();
  69. [context release];
  70. context = nil;
  71. }
  72. bool makeActive() const noexcept
  73. {
  74. jassert (context != nil);
  75. [EAGLContext setCurrentContext: context];
  76. glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
  77. return true;
  78. }
  79. void swapBuffers()
  80. {
  81. glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
  82. [context presentRenderbuffer: GL_RENDERBUFFER_OES];
  83. }
  84. bool makeInactive() const noexcept
  85. {
  86. return [EAGLContext setCurrentContext: nil];
  87. }
  88. bool isActive() const noexcept
  89. {
  90. return [EAGLContext currentContext] == context;
  91. }
  92. void* getRawContext() const noexcept { return glLayer; }
  93. unsigned int getFrameBufferID() const { return (unsigned int) frameBufferHandle; }
  94. void updateWindowPosition (const Rectangle<int>& bounds)
  95. {
  96. // For some strange reason, the view seems to fail unless its width is a multiple of 8...
  97. view.frame = CGRectMake ((CGFloat) bounds.getX(), (CGFloat) bounds.getY(),
  98. (CGFloat) (bounds.getWidth() & ~7),
  99. (CGFloat) bounds.getHeight());
  100. if (lastWidth != bounds.getWidth() || lastHeight != bounds.getHeight())
  101. {
  102. lastWidth = bounds.getWidth();
  103. lastHeight = bounds.getHeight();
  104. freeGLBuffers();
  105. createGLBuffers();
  106. }
  107. }
  108. bool setSwapInterval (const int numFramesPerSwap)
  109. {
  110. numFrames = numFramesPerSwap;
  111. return true;
  112. }
  113. int getSwapInterval() const
  114. {
  115. return numFrames;
  116. }
  117. //==============================================================================
  118. void createGLBuffers()
  119. {
  120. makeActive();
  121. glGenFramebuffersOES (1, &frameBufferHandle);
  122. glGenRenderbuffersOES (1, &colorBufferHandle);
  123. glGenRenderbuffersOES (1, &depthBufferHandle);
  124. glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
  125. bool ok = [context renderbufferStorage: GL_RENDERBUFFER_OES fromDrawable: glLayer];
  126. jassert (ok);
  127. GLint width, height;
  128. glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_WIDTH_OES, &width);
  129. glGetRenderbufferParameterivOES (GL_RENDERBUFFER_OES, GL_RENDERBUFFER_HEIGHT_OES, &height);
  130. if (useDepthBuffer)
  131. {
  132. glBindRenderbufferOES (GL_RENDERBUFFER_OES, depthBufferHandle);
  133. glRenderbufferStorageOES (GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, width, height);
  134. }
  135. glBindRenderbufferOES (GL_RENDERBUFFER_OES, colorBufferHandle);
  136. glBindFramebufferOES (GL_FRAMEBUFFER_OES, frameBufferHandle);
  137. glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_COLOR_ATTACHMENT0_OES, GL_RENDERBUFFER_OES, colorBufferHandle);
  138. if (useDepthBuffer)
  139. glFramebufferRenderbufferOES (GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthBufferHandle);
  140. jassert (glCheckFramebufferStatusOES (GL_FRAMEBUFFER_OES) == GL_FRAMEBUFFER_COMPLETE_OES);
  141. }
  142. void freeGLBuffers()
  143. {
  144. [context renderbufferStorage: GL_RENDERBUFFER_OES fromDrawable: nil];
  145. if (frameBufferHandle != 0)
  146. {
  147. glDeleteFramebuffersOES (1, &frameBufferHandle);
  148. frameBufferHandle = 0;
  149. }
  150. if (colorBufferHandle != 0)
  151. {
  152. glDeleteRenderbuffersOES (1, &colorBufferHandle);
  153. colorBufferHandle = 0;
  154. }
  155. if (depthBufferHandle != 0)
  156. {
  157. glDeleteRenderbuffersOES (1, &depthBufferHandle);
  158. depthBufferHandle = 0;
  159. }
  160. }
  161. private:
  162. WeakReference<Component> component;
  163. JuceGLView* view;
  164. CAEAGLLayer* glLayer;
  165. EAGLContext* context;
  166. bool useDepthBuffer;
  167. GLuint frameBufferHandle, colorBufferHandle, depthBufferHandle;
  168. int numFrames;
  169. int lastWidth, lastHeight;
  170. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (GLESContext);
  171. };
  172. OpenGLContext* OpenGLComponent::createContext()
  173. {
  174. JUCE_AUTORELEASEPOOL
  175. ComponentPeer* peer = getPeer();
  176. if (peer != nullptr)
  177. return new GLESContext ((UIView*) peer->getNativeHandle(), this, preferredPixelFormat,
  178. dynamic_cast <const GLESContext*> (contextToShareListsWith),
  179. (flags & openGLES2) == 0 ? kEAGLRenderingAPIOpenGLES1
  180. : kEAGLRenderingAPIOpenGLES2);
  181. return nullptr;
  182. }
  183. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>& bounds)
  184. {
  185. if (context != nullptr)
  186. static_cast <GLESContext*> (context.get())->updateWindowPosition (bounds);
  187. }
  188. //==============================================================================
  189. bool OpenGLHelpers::isContextActive()
  190. {
  191. return [EAGLContext currentContext] != nil;
  192. }