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.

245 lines
9.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class OpenGLContext::NativeContext
  18. {
  19. public:
  20. NativeContext (Component& component,
  21. const OpenGLPixelFormat& pixFormat,
  22. void* contextToShare,
  23. bool shouldUseMultisampling,
  24. OpenGLVersion version)
  25. : lastSwapTime (0), minSwapTimeMs (0), underrunCounter (0)
  26. {
  27. NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
  28. createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
  29. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  30. static MouseForwardingNSOpenGLViewClass cls;
  31. view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  32. pixelFormat: format];
  33. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  34. if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
  35. [view setWantsBestResolutionOpenGLSurface: YES];
  36. #endif
  37. [[NSNotificationCenter defaultCenter] addObserver: view
  38. selector: @selector (_surfaceNeedsUpdate:)
  39. name: NSViewGlobalFrameDidChangeNotification
  40. object: view];
  41. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  42. shareContext: (NSOpenGLContext*) contextToShare] autorelease];
  43. [view setOpenGLContext: renderContext];
  44. [format release];
  45. viewAttachment = NSViewComponent::attachViewToComponent (component, view);
  46. }
  47. ~NativeContext()
  48. {
  49. [[NSNotificationCenter defaultCenter] removeObserver: view];
  50. [renderContext clearDrawable];
  51. [renderContext setView: nil];
  52. [view setOpenGLContext: nil];
  53. renderContext = nil;
  54. }
  55. static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
  56. const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
  57. {
  58. (void) version;
  59. int numAttribs = 0;
  60. #if JUCE_OPENGL3
  61. attribs [numAttribs++] = NSOpenGLPFAOpenGLProfile;
  62. attribs [numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
  63. : NSOpenGLProfileVersionLegacy;
  64. #endif
  65. attribs [numAttribs++] = NSOpenGLPFADoubleBuffer;
  66. attribs [numAttribs++] = NSOpenGLPFAClosestPolicy;
  67. attribs [numAttribs++] = NSOpenGLPFANoRecovery;
  68. attribs [numAttribs++] = NSOpenGLPFAColorSize;
  69. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
  70. attribs [numAttribs++] = NSOpenGLPFAAlphaSize;
  71. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
  72. attribs [numAttribs++] = NSOpenGLPFADepthSize;
  73. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
  74. attribs [numAttribs++] = NSOpenGLPFAStencilSize;
  75. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
  76. attribs [numAttribs++] = NSOpenGLPFAAccumSize;
  77. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
  78. + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
  79. if (shouldUseMultisampling)
  80. {
  81. attribs [numAttribs++] = NSOpenGLPFAMultisample;
  82. attribs [numAttribs++] = NSOpenGLPFASampleBuffers;
  83. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
  84. attribs [numAttribs++] = NSOpenGLPFASamples;
  85. attribs [numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
  86. }
  87. }
  88. void initialiseOnRenderThread (OpenGLContext&) {}
  89. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  90. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  91. void* getRawContext() const noexcept { return static_cast<void*> (renderContext); }
  92. GLuint getFrameBufferID() const noexcept { return 0; }
  93. bool makeActive() const noexcept
  94. {
  95. jassert (renderContext != nil);
  96. if ([renderContext view] != view)
  97. [renderContext setView: view];
  98. if (NSOpenGLContext* context = [view openGLContext])
  99. {
  100. [context makeCurrentContext];
  101. return true;
  102. }
  103. return false;
  104. }
  105. bool isActive() const noexcept
  106. {
  107. return [NSOpenGLContext currentContext] == renderContext;
  108. }
  109. static void deactivateCurrentContext()
  110. {
  111. [NSOpenGLContext clearCurrentContext];
  112. }
  113. struct Locker
  114. {
  115. Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
  116. {
  117. CGLLockContext (cglContext);
  118. }
  119. ~Locker()
  120. {
  121. CGLUnlockContext (cglContext);
  122. }
  123. private:
  124. CGLContextObj cglContext;
  125. };
  126. void swapBuffers()
  127. {
  128. [renderContext flushBuffer];
  129. sleepIfRenderingTooFast();
  130. }
  131. void updateWindowPosition (const Rectangle<int>&) {}
  132. bool setSwapInterval (int numFramesPerSwap)
  133. {
  134. minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
  135. [renderContext setValues: (const GLint*) &numFramesPerSwap
  136. forParameter: NSOpenGLCPSwapInterval];
  137. return true;
  138. }
  139. int getSwapInterval() const
  140. {
  141. GLint numFrames = 0;
  142. [renderContext getValues: &numFrames
  143. forParameter: NSOpenGLCPSwapInterval];
  144. return numFrames;
  145. }
  146. void sleepIfRenderingTooFast()
  147. {
  148. // When our window is entirely occluded by other windows, the system
  149. // fails to correctly implement the swap interval time, so the render
  150. // loop spins at full speed, burning CPU. This hack detects when things
  151. // are going too fast and slows things down if necessary.
  152. if (minSwapTimeMs > 0)
  153. {
  154. const double now = Time::getMillisecondCounterHiRes();
  155. const int elapsed = (int) (now - lastSwapTime);
  156. lastSwapTime = now;
  157. if (isPositiveAndBelow (elapsed, minSwapTimeMs - 3))
  158. {
  159. if (underrunCounter > 3)
  160. Thread::sleep (minSwapTimeMs - elapsed);
  161. else
  162. ++underrunCounter;
  163. }
  164. else
  165. {
  166. underrunCounter = 0;
  167. }
  168. }
  169. }
  170. NSOpenGLContext* renderContext;
  171. NSOpenGLView* view;
  172. ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
  173. double lastSwapTime;
  174. int minSwapTimeMs, underrunCounter;
  175. //==============================================================================
  176. struct MouseForwardingNSOpenGLViewClass : public ObjCClass<NSOpenGLView>
  177. {
  178. MouseForwardingNSOpenGLViewClass() : ObjCClass<NSOpenGLView> ("JUCEGLView_")
  179. {
  180. addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
  181. addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
  182. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
  183. registerClass();
  184. }
  185. private:
  186. static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
  187. static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
  188. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  189. };
  190. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  191. };
  192. //==============================================================================
  193. bool OpenGLHelpers::isContextActive()
  194. {
  195. return CGLGetCurrentContext() != 0;
  196. }