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.

256 lines
9.3KB

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