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.

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