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.

307 lines
11KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
  21. class OpenGLContext::NativeContext
  22. {
  23. public:
  24. NativeContext (Component& component,
  25. const OpenGLPixelFormat& pixFormat,
  26. void* contextToShare,
  27. bool shouldUseMultisampling,
  28. OpenGLVersion version)
  29. : owner (component)
  30. {
  31. NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
  32. createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
  33. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  34. static MouseForwardingNSOpenGLViewClass cls;
  35. view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  36. pixelFormat: format];
  37. if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
  38. [view setWantsBestResolutionOpenGLSurface: YES];
  39. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
  40. [[NSNotificationCenter defaultCenter] addObserver: view
  41. selector: @selector (_surfaceNeedsUpdate:)
  42. name: NSViewGlobalFrameDidChangeNotification
  43. object: view];
  44. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  45. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  46. shareContext: (NSOpenGLContext*) contextToShare] autorelease];
  47. [view setOpenGLContext: renderContext];
  48. [format release];
  49. viewAttachment = NSViewComponent::attachViewToComponent (component, view);
  50. }
  51. ~NativeContext()
  52. {
  53. [[NSNotificationCenter defaultCenter] removeObserver: view];
  54. [renderContext clearDrawable];
  55. [renderContext setView: nil];
  56. [view setOpenGLContext: nil];
  57. [view release];
  58. }
  59. static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
  60. const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
  61. {
  62. ignoreUnused (version);
  63. int numAttribs = 0;
  64. attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile;
  65. attribs[numAttribs++] = [version]
  66. {
  67. if (version == openGL3_2)
  68. return NSOpenGLProfileVersion3_2Core;
  69. if (version != defaultGLVersion)
  70. if (@available (macOS 10.10, *))
  71. return NSOpenGLProfileVersion4_1Core;
  72. return NSOpenGLProfileVersionLegacy;
  73. }();
  74. attribs[numAttribs++] = NSOpenGLPFADoubleBuffer;
  75. attribs[numAttribs++] = NSOpenGLPFAClosestPolicy;
  76. attribs[numAttribs++] = NSOpenGLPFANoRecovery;
  77. attribs[numAttribs++] = NSOpenGLPFAColorSize;
  78. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
  79. attribs[numAttribs++] = NSOpenGLPFAAlphaSize;
  80. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
  81. attribs[numAttribs++] = NSOpenGLPFADepthSize;
  82. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
  83. attribs[numAttribs++] = NSOpenGLPFAStencilSize;
  84. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
  85. attribs[numAttribs++] = NSOpenGLPFAAccumSize;
  86. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
  87. + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
  88. if (shouldUseMultisampling)
  89. {
  90. attribs[numAttribs++] = NSOpenGLPFAMultisample;
  91. attribs[numAttribs++] = NSOpenGLPFASampleBuffers;
  92. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
  93. attribs[numAttribs++] = NSOpenGLPFASamples;
  94. attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
  95. }
  96. }
  97. InitResult initialiseOnRenderThread (OpenGLContext&) { return InitResult::success; }
  98. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  99. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  100. NSOpenGLView* getNSView() const noexcept { return view; }
  101. NSOpenGLContext* getRawContext() const noexcept { return renderContext; }
  102. GLuint getFrameBufferID() const noexcept { return 0; }
  103. bool makeActive() const noexcept
  104. {
  105. jassert (renderContext != nil);
  106. if ([renderContext view] != view)
  107. [renderContext setView: view];
  108. if (NSOpenGLContext* context = [view openGLContext])
  109. {
  110. [context makeCurrentContext];
  111. return true;
  112. }
  113. return false;
  114. }
  115. bool isActive() const noexcept
  116. {
  117. return [NSOpenGLContext currentContext] == renderContext;
  118. }
  119. static void deactivateCurrentContext()
  120. {
  121. [NSOpenGLContext clearCurrentContext];
  122. }
  123. struct Locker
  124. {
  125. Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
  126. {
  127. CGLLockContext (cglContext);
  128. }
  129. ~Locker()
  130. {
  131. CGLUnlockContext (cglContext);
  132. }
  133. private:
  134. CGLContextObj cglContext;
  135. };
  136. void swapBuffers()
  137. {
  138. auto now = Time::getMillisecondCounterHiRes();
  139. [renderContext flushBuffer];
  140. if (minSwapTimeMs > 0)
  141. {
  142. // When our window is entirely occluded by other windows, flushBuffer
  143. // fails to wait for the swap interval, so the render loop spins at full
  144. // speed, burning CPU. This hack detects when things are going too fast
  145. // and sleeps if necessary.
  146. auto swapTime = Time::getMillisecondCounterHiRes() - now;
  147. auto frameTime = (int) (now - lastSwapTime);
  148. if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
  149. {
  150. if (underrunCounter > 3)
  151. {
  152. Thread::sleep (2 * (minSwapTimeMs - frameTime));
  153. now = Time::getMillisecondCounterHiRes();
  154. }
  155. else
  156. {
  157. ++underrunCounter;
  158. }
  159. }
  160. else
  161. {
  162. if (underrunCounter > 0)
  163. --underrunCounter;
  164. }
  165. }
  166. lastSwapTime = now;
  167. }
  168. void updateWindowPosition (Rectangle<int>)
  169. {
  170. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  171. {
  172. const auto newArea = peer->getAreaCoveredBy (owner);
  173. if (convertToRectInt ([view frame]) != newArea)
  174. [view setFrame: makeNSRect (newArea)];
  175. }
  176. }
  177. bool setSwapInterval (int numFramesPerSwapIn)
  178. {
  179. numFramesPerSwap = numFramesPerSwapIn;
  180. // The macOS OpenGL programming guide says that numFramesPerSwap
  181. // can only be 0 or 1.
  182. jassert (isPositiveAndBelow (numFramesPerSwap, 2));
  183. [renderContext setValues: (const GLint*) &numFramesPerSwap
  184. forParameter: getSwapIntervalParameter()];
  185. updateMinSwapTime();
  186. return true;
  187. }
  188. int getSwapInterval() const
  189. {
  190. GLint numFrames = 0;
  191. [renderContext getValues: &numFrames
  192. forParameter: getSwapIntervalParameter()];
  193. return numFrames;
  194. }
  195. void setNominalVideoRefreshPeriodS (double periodS)
  196. {
  197. jassert (periodS > 0.0);
  198. videoRefreshPeriodS = periodS;
  199. updateMinSwapTime();
  200. }
  201. void updateMinSwapTime()
  202. {
  203. minSwapTimeMs = static_cast<int> (numFramesPerSwap * 1000 * videoRefreshPeriodS);
  204. }
  205. static NSOpenGLContextParameter getSwapIntervalParameter()
  206. {
  207. if (@available (macOS 10.12, *))
  208. return NSOpenGLContextParameterSwapInterval;
  209. return NSOpenGLCPSwapInterval;
  210. }
  211. Component& owner;
  212. NSOpenGLContext* renderContext = nil;
  213. NSOpenGLView* view = nil;
  214. ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
  215. double lastSwapTime = 0;
  216. std::atomic<int> minSwapTimeMs { 0 };
  217. int underrunCounter = 0, numFramesPerSwap = 0;
  218. double videoRefreshPeriodS = 1.0 / 60.0;
  219. //==============================================================================
  220. struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
  221. {
  222. MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
  223. {
  224. addMethod (@selector (rightMouseDown:), rightMouseDown);
  225. addMethod (@selector (rightMouseUp:), rightMouseUp);
  226. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse);
  227. addMethod (@selector (accessibilityHitTest:), accessibilityHitTest);
  228. registerClass();
  229. }
  230. private:
  231. static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
  232. static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
  233. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  234. static id accessibilityHitTest (id self, SEL, NSPoint p) { return [[(NSOpenGLView*) self superview] accessibilityHitTest: p]; }
  235. };
  236. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  237. };
  238. //==============================================================================
  239. bool OpenGLHelpers::isContextActive()
  240. {
  241. return CGLGetCurrentContext() != CGLContextObj();
  242. }
  243. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  244. } // namespace juce