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.

254 lines
8.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. END_JUCE_NAMESPACE
  19. #define ThreadSafeNSOpenGLView MakeObjCClassName(ThreadSafeNSOpenGLView)
  20. //==============================================================================
  21. @interface ThreadSafeNSOpenGLView : NSOpenGLView
  22. {
  23. CriticalSection* contextLock;
  24. bool needsUpdate;
  25. }
  26. - (id) initWithFrame: (NSRect) frameRect pixelFormat: (NSOpenGLPixelFormat*) format;
  27. - (bool) makeActive;
  28. - (void) makeInactive;
  29. - (void) reshape;
  30. - (void) rightMouseDown: (NSEvent*) ev;
  31. - (void) rightMouseUp: (NSEvent*) ev;
  32. @end
  33. @implementation ThreadSafeNSOpenGLView
  34. - (id) initWithFrame: (NSRect) frameRect
  35. pixelFormat: (NSOpenGLPixelFormat*) format
  36. {
  37. contextLock = new CriticalSection();
  38. self = [super initWithFrame: frameRect pixelFormat: format];
  39. if (self != nil)
  40. [[NSNotificationCenter defaultCenter] addObserver: self
  41. selector: @selector (_surfaceNeedsUpdate:)
  42. name: NSViewGlobalFrameDidChangeNotification
  43. object: self];
  44. return self;
  45. }
  46. - (void) dealloc
  47. {
  48. [[NSNotificationCenter defaultCenter] removeObserver: self];
  49. delete contextLock;
  50. [super dealloc];
  51. }
  52. - (bool) makeActive
  53. {
  54. const ScopedLock sl (*contextLock);
  55. if ([self openGLContext] == nil)
  56. return false;
  57. [[self openGLContext] makeCurrentContext];
  58. if (needsUpdate)
  59. {
  60. [super update];
  61. needsUpdate = false;
  62. }
  63. return true;
  64. }
  65. - (void) makeInactive
  66. {
  67. const ScopedLock sl (*contextLock);
  68. [NSOpenGLContext clearCurrentContext];
  69. }
  70. - (void) _surfaceNeedsUpdate: (NSNotification*) notification
  71. {
  72. (void) notification;
  73. const ScopedLock sl (*contextLock);
  74. needsUpdate = true;
  75. }
  76. - (void) update
  77. {
  78. const ScopedLock sl (*contextLock);
  79. needsUpdate = true;
  80. }
  81. - (void) reshape
  82. {
  83. const ScopedLock sl (*contextLock);
  84. needsUpdate = true;
  85. }
  86. - (void) rightMouseDown: (NSEvent*) ev { [[self superview] rightMouseDown: ev]; }
  87. - (void) rightMouseUp: (NSEvent*) ev { [[self superview] rightMouseUp: ev]; }
  88. @end
  89. BEGIN_JUCE_NAMESPACE
  90. //==============================================================================
  91. class WindowedGLContext : public OpenGLContext
  92. {
  93. public:
  94. WindowedGLContext (OpenGLComponent& component,
  95. const OpenGLPixelFormat& pixelFormat,
  96. NSOpenGLContext* sharedContext)
  97. : renderContext (nil)
  98. {
  99. extensions.initialise();
  100. NSOpenGLPixelFormatAttribute attribs[] =
  101. {
  102. NSOpenGLPFADoubleBuffer,
  103. NSOpenGLPFAMPSafe,
  104. NSOpenGLPFAClosestPolicy,
  105. NSOpenGLPFANoRecovery,
  106. NSOpenGLPFAColorSize, pixelFormat.redBits + pixelFormat.greenBits + pixelFormat.blueBits,
  107. NSOpenGLPFAAlphaSize, pixelFormat.alphaBits,
  108. NSOpenGLPFADepthSize, pixelFormat.depthBufferBits,
  109. NSOpenGLPFAStencilSize, pixelFormat.stencilBufferBits,
  110. NSOpenGLPFAAccumSize, pixelFormat.accumulationBufferRedBits + pixelFormat.accumulationBufferGreenBits
  111. + pixelFormat.accumulationBufferBlueBits + pixelFormat.accumulationBufferAlphaBits,
  112. pixelFormat.multisamplingLevel > 0 ? NSOpenGLPFASamples : 0, pixelFormat.multisamplingLevel,
  113. 0
  114. };
  115. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  116. view = [[ThreadSafeNSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  117. pixelFormat: format];
  118. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  119. shareContext: sharedContext] autorelease];
  120. const GLint swapInterval = 1;
  121. [renderContext setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];
  122. [view setOpenGLContext: renderContext];
  123. [format release];
  124. component.setView (view);
  125. }
  126. ~WindowedGLContext()
  127. {
  128. properties.clear(); // to release any stored programs, etc that may be held in properties.
  129. makeInactive();
  130. [renderContext clearDrawable];
  131. [renderContext setView: nil];
  132. [view setOpenGLContext: nil];
  133. renderContext = nil;
  134. }
  135. bool makeActive() const noexcept
  136. {
  137. jassert (renderContext != nil);
  138. if ([renderContext view] != view)
  139. [renderContext setView: view];
  140. [view makeActive];
  141. return isActive();
  142. }
  143. bool makeInactive() const noexcept
  144. {
  145. [view makeInactive];
  146. return true;
  147. }
  148. bool isActive() const noexcept
  149. {
  150. return [NSOpenGLContext currentContext] == renderContext;
  151. }
  152. void* getRawContext() const noexcept { return renderContext; }
  153. unsigned int getFrameBufferID() const { return 0; }
  154. int getWidth() const { return [view frame].size.width; }
  155. int getHeight() const { return [view frame].size.height; }
  156. void swapBuffers()
  157. {
  158. [renderContext flushBuffer];
  159. }
  160. bool setSwapInterval (const int numFramesPerSwap)
  161. {
  162. [renderContext setValues: (const GLint*) &numFramesPerSwap
  163. forParameter: NSOpenGLCPSwapInterval];
  164. return true;
  165. }
  166. int getSwapInterval() const
  167. {
  168. GLint numFrames = 0;
  169. [renderContext getValues: &numFrames
  170. forParameter: NSOpenGLCPSwapInterval];
  171. return numFrames;
  172. }
  173. void* getNativeWindowHandle() const { return view; }
  174. //==============================================================================
  175. NSOpenGLContext* renderContext;
  176. ThreadSafeNSOpenGLView* view;
  177. private:
  178. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  179. };
  180. //==============================================================================
  181. OpenGLContext* OpenGLComponent::createContext()
  182. {
  183. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (*this, preferredPixelFormat,
  184. contextToShareListsWith != nullptr ? (NSOpenGLContext*) contextToShareListsWith->getRawContext() : nil));
  185. return (c->renderContext != nil) ? c.release() : nullptr;
  186. }
  187. void* OpenGLComponent::getNativeWindowHandle() const
  188. {
  189. return context != nullptr ? static_cast<WindowedGLContext*> (context.get())->getNativeWindowHandle()
  190. : nullptr;
  191. }
  192. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>&)
  193. {
  194. }
  195. //==============================================================================
  196. bool OpenGLHelpers::isContextActive()
  197. {
  198. return CGLGetCurrentContext() != 0;
  199. }