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.

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