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.

268 lines
9.9KB

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