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.

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