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.

266 lines
9.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class OpenGLContext::NativeContext
  18. {
  19. public:
  20. NativeContext (Component& component,
  21. const OpenGLPixelFormat& pixFormat,
  22. void* contextToShare,
  23. bool shouldUseMultisampling,
  24. OpenGLVersion version)
  25. : lastSwapTime (0), minSwapTimeMs (0), underrunCounter (0)
  26. {
  27. NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
  28. createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
  29. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  30. static MouseForwardingNSOpenGLViewClass cls;
  31. view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  32. pixelFormat: format];
  33. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  34. if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
  35. [view setWantsBestResolutionOpenGLSurface: YES];
  36. #endif
  37. [[NSNotificationCenter defaultCenter] addObserver: view
  38. selector: @selector (_surfaceNeedsUpdate:)
  39. name: NSViewGlobalFrameDidChangeNotification
  40. object: view];
  41. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  42. shareContext: (NSOpenGLContext*) contextToShare] autorelease];
  43. [view setOpenGLContext: renderContext];
  44. [format release];
  45. viewAttachment = NSViewComponent::attachViewToComponent (component, view);
  46. }
  47. ~NativeContext()
  48. {
  49. [[NSNotificationCenter defaultCenter] removeObserver: view];
  50. [renderContext clearDrawable];
  51. [renderContext setView: nil];
  52. [view setOpenGLContext: nil];
  53. renderContext = nil;
  54. }
  55. static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
  56. const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
  57. {
  58. ignoreUnused (version);
  59. int numAttribs = 0;
  60. #if JUCE_OPENGL3
  61. attribs [numAttribs++] = NSOpenGLPFAOpenGLProfile;
  62. attribs [numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
  63. : NSOpenGLProfileVersionLegacy;
  64. #endif
  65. attribs [numAttribs++] = NSOpenGLPFADoubleBuffer;
  66. attribs [numAttribs++] = NSOpenGLPFAClosestPolicy;
  67. attribs [numAttribs++] = NSOpenGLPFANoRecovery;
  68. attribs [numAttribs++] = NSOpenGLPFAColorSize;
  69. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
  70. attribs [numAttribs++] = NSOpenGLPFAAlphaSize;
  71. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
  72. attribs [numAttribs++] = NSOpenGLPFADepthSize;
  73. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
  74. attribs [numAttribs++] = NSOpenGLPFAStencilSize;
  75. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
  76. attribs [numAttribs++] = NSOpenGLPFAAccumSize;
  77. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
  78. + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
  79. if (shouldUseMultisampling)
  80. {
  81. attribs [numAttribs++] = NSOpenGLPFAMultisample;
  82. attribs [numAttribs++] = NSOpenGLPFASampleBuffers;
  83. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
  84. attribs [numAttribs++] = NSOpenGLPFASamples;
  85. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
  86. }
  87. }
  88. void initialiseOnRenderThread (OpenGLContext&) {}
  89. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  90. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  91. void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
  92. GLuint getFrameBufferID() const noexcept { return 0; }
  93. bool makeActive() const noexcept
  94. {
  95. jassert (renderContext != nil);
  96. if ([renderContext view] != view)
  97. [renderContext setView: view];
  98. if (NSOpenGLContext* context = [view openGLContext])
  99. {
  100. [context makeCurrentContext];
  101. return true;
  102. }
  103. return false;
  104. }
  105. bool isActive() const noexcept
  106. {
  107. return [NSOpenGLContext currentContext] == renderContext;
  108. }
  109. static void deactivateCurrentContext()
  110. {
  111. [NSOpenGLContext clearCurrentContext];
  112. }
  113. struct Locker
  114. {
  115. Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
  116. {
  117. CGLLockContext (cglContext);
  118. }
  119. ~Locker()
  120. {
  121. CGLUnlockContext (cglContext);
  122. }
  123. private:
  124. CGLContextObj cglContext;
  125. };
  126. void swapBuffers()
  127. {
  128. double now = Time::getMillisecondCounterHiRes();
  129. [renderContext flushBuffer];
  130. if (minSwapTimeMs > 0)
  131. {
  132. // When our window is entirely occluded by other windows, flushBuffer
  133. // fails to wait for the swap interval, so the render loop spins at full
  134. // speed, burning CPU. This hack detects when things are going too fast
  135. // and sleeps if necessary.
  136. const double swapTime = Time::getMillisecondCounterHiRes() - now;
  137. const int frameTime = (int) (now - lastSwapTime);
  138. if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
  139. {
  140. if (underrunCounter > 3)
  141. {
  142. Thread::sleep (2 * (minSwapTimeMs - frameTime));
  143. now = Time::getMillisecondCounterHiRes();
  144. }
  145. else
  146. ++underrunCounter;
  147. }
  148. else
  149. {
  150. if (underrunCounter > 0)
  151. --underrunCounter;
  152. }
  153. }
  154. lastSwapTime = now;
  155. }
  156. void updateWindowPosition (const Rectangle<int>&) {}
  157. bool setSwapInterval (int numFramesPerSwap)
  158. {
  159. minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
  160. [renderContext setValues: (const GLint*) &numFramesPerSwap
  161. forParameter: NSOpenGLCPSwapInterval];
  162. return true;
  163. }
  164. int getSwapInterval() const
  165. {
  166. GLint numFrames = 0;
  167. [renderContext getValues: &numFrames
  168. forParameter: NSOpenGLCPSwapInterval];
  169. return numFrames;
  170. }
  171. NSOpenGLContext* renderContext;
  172. NSOpenGLView* view;
  173. ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
  174. double lastSwapTime;
  175. int minSwapTimeMs, underrunCounter;
  176. //==============================================================================
  177. struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
  178. {
  179. MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
  180. {
  181. addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
  182. addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
  183. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
  184. registerClass();
  185. }
  186. private:
  187. static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
  188. static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
  189. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  190. };
  191. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  192. };
  193. //==============================================================================
  194. bool OpenGLHelpers::isContextActive()
  195. {
  196. return CGLGetCurrentContext() != 0;
  197. }
  198. //==============================================================================
  199. void componentPeerAboutToBeRemovedFromScreen (ComponentPeer& peer)
  200. {
  201. Array<Component*> stack;
  202. stack.add (&peer.getComponent());
  203. while (stack.size() != 0)
  204. {
  205. Component& comp = *stack.removeAndReturn (0);
  206. const int n = comp.getNumChildComponents();
  207. for (int i = 0; i < n; ++i)
  208. if (Component* child = comp.getChildComponent (i))
  209. stack.add (child);
  210. if (OpenGLContext* context = OpenGLContext::getContextAttachedTo (comp))
  211. context->detach();
  212. }
  213. }