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.

275 lines
10KB

  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. namespace juce
  20. {
  21. #if JUCE_CLANG && ! (defined (MAC_OS_X_VERSION_10_16) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_16)
  22. #pragma clang diagnostic push
  23. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  24. #define JUCE_DEPRECATION_IGNORED 1
  25. #endif
  26. class OpenGLContext::NativeContext
  27. {
  28. public:
  29. NativeContext (Component& component,
  30. const OpenGLPixelFormat& pixFormat,
  31. void* contextToShare,
  32. bool shouldUseMultisampling,
  33. OpenGLVersion version)
  34. {
  35. NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
  36. createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
  37. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  38. static MouseForwardingNSOpenGLViewClass cls;
  39. view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  40. pixelFormat: format];
  41. if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
  42. [view setWantsBestResolutionOpenGLSurface: YES];
  43. [[NSNotificationCenter defaultCenter] addObserver: view
  44. selector: @selector (_surfaceNeedsUpdate:)
  45. name: NSViewGlobalFrameDidChangeNotification
  46. object: view];
  47. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  48. shareContext: (NSOpenGLContext*) contextToShare] autorelease];
  49. [view setOpenGLContext: renderContext];
  50. [format release];
  51. viewAttachment = NSViewComponent::attachViewToComponent (component, view);
  52. }
  53. ~NativeContext()
  54. {
  55. [[NSNotificationCenter defaultCenter] removeObserver: view];
  56. [renderContext clearDrawable];
  57. [renderContext setView: nil];
  58. [view setOpenGLContext: nil];
  59. [view release];
  60. }
  61. static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
  62. const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
  63. {
  64. ignoreUnused (version);
  65. int numAttribs = 0;
  66. #if JUCE_OPENGL3
  67. attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile;
  68. attribs[numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
  69. : NSOpenGLProfileVersionLegacy;
  70. #endif
  71. attribs[numAttribs++] = NSOpenGLPFADoubleBuffer;
  72. attribs[numAttribs++] = NSOpenGLPFAClosestPolicy;
  73. attribs[numAttribs++] = NSOpenGLPFANoRecovery;
  74. attribs[numAttribs++] = NSOpenGLPFAColorSize;
  75. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
  76. attribs[numAttribs++] = NSOpenGLPFAAlphaSize;
  77. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
  78. attribs[numAttribs++] = NSOpenGLPFADepthSize;
  79. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
  80. attribs[numAttribs++] = NSOpenGLPFAStencilSize;
  81. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
  82. attribs[numAttribs++] = NSOpenGLPFAAccumSize;
  83. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
  84. + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
  85. if (shouldUseMultisampling)
  86. {
  87. attribs[numAttribs++] = NSOpenGLPFAMultisample;
  88. attribs[numAttribs++] = NSOpenGLPFASampleBuffers;
  89. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
  90. attribs[numAttribs++] = NSOpenGLPFASamples;
  91. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
  92. }
  93. }
  94. bool initialiseOnRenderThread (OpenGLContext&) { return true; }
  95. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  96. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  97. void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
  98. GLuint getFrameBufferID() const noexcept { return 0; }
  99. bool makeActive() const noexcept
  100. {
  101. jassert (renderContext != nil);
  102. if ([renderContext view] != view)
  103. [renderContext setView: view];
  104. if (NSOpenGLContext* context = [view openGLContext])
  105. {
  106. [context makeCurrentContext];
  107. return true;
  108. }
  109. return false;
  110. }
  111. bool isActive() const noexcept
  112. {
  113. return [NSOpenGLContext currentContext] == renderContext;
  114. }
  115. static void deactivateCurrentContext()
  116. {
  117. [NSOpenGLContext clearCurrentContext];
  118. }
  119. struct Locker
  120. {
  121. Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
  122. {
  123. CGLLockContext (cglContext);
  124. }
  125. ~Locker()
  126. {
  127. CGLUnlockContext (cglContext);
  128. }
  129. private:
  130. CGLContextObj cglContext;
  131. };
  132. void swapBuffers()
  133. {
  134. auto now = Time::getMillisecondCounterHiRes();
  135. [renderContext flushBuffer];
  136. if (minSwapTimeMs > 0)
  137. {
  138. // When our window is entirely occluded by other windows, flushBuffer
  139. // fails to wait for the swap interval, so the render loop spins at full
  140. // speed, burning CPU. This hack detects when things are going too fast
  141. // and sleeps if necessary.
  142. auto swapTime = Time::getMillisecondCounterHiRes() - now;
  143. auto frameTime = (int) (now - lastSwapTime);
  144. if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
  145. {
  146. if (underrunCounter > 3)
  147. {
  148. Thread::sleep (2 * (minSwapTimeMs - frameTime));
  149. now = Time::getMillisecondCounterHiRes();
  150. }
  151. else
  152. {
  153. ++underrunCounter;
  154. }
  155. }
  156. else
  157. {
  158. if (underrunCounter > 0)
  159. --underrunCounter;
  160. }
  161. }
  162. lastSwapTime = now;
  163. }
  164. void updateWindowPosition (Rectangle<int>) {}
  165. bool setSwapInterval (int numFramesPerSwap)
  166. {
  167. // The macOS OpenGL programming guide says that numFramesPerSwap
  168. // can only be 0 or 1.
  169. jassert (isPositiveAndBelow (numFramesPerSwap, 2));
  170. minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
  171. [renderContext setValues: (const GLint*) &numFramesPerSwap
  172. #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
  173. forParameter: NSOpenGLContextParameterSwapInterval];
  174. #else
  175. forParameter: NSOpenGLCPSwapInterval];
  176. #endif
  177. return true;
  178. }
  179. int getSwapInterval() const
  180. {
  181. GLint numFrames = 0;
  182. [renderContext getValues: &numFrames
  183. #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
  184. forParameter: NSOpenGLContextParameterSwapInterval];
  185. #else
  186. forParameter: NSOpenGLCPSwapInterval];
  187. #endif
  188. return numFrames;
  189. }
  190. NSOpenGLContext* renderContext = nil;
  191. NSOpenGLView* view = nil;
  192. ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
  193. double lastSwapTime = 0;
  194. int minSwapTimeMs = 0, underrunCounter = 0;
  195. //==============================================================================
  196. struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
  197. {
  198. MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
  199. {
  200. addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
  201. addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
  202. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
  203. registerClass();
  204. }
  205. private:
  206. static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
  207. static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
  208. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  209. };
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  211. };
  212. //==============================================================================
  213. bool OpenGLHelpers::isContextActive()
  214. {
  215. return CGLGetCurrentContext() != CGLContextObj();
  216. }
  217. #if JUCE_DEPRECATION_IGNORED
  218. #pragma clang diagnostic pop
  219. #undef JUCE_DEPRECATION_IGNORED
  220. #endif
  221. } // namespace juce