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
7.8KB

  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. initialiseGLExtensions();
  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. NSOpenGLPFASampleBuffers, 1,
  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. deleteContext();
  129. }
  130. void deleteContext()
  131. {
  132. makeInactive();
  133. [renderContext clearDrawable];
  134. [renderContext setView: nil];
  135. [view setOpenGLContext: nil];
  136. renderContext = nil;
  137. }
  138. bool makeActive() const noexcept
  139. {
  140. jassert (renderContext != nil);
  141. if ([renderContext view] != view)
  142. [renderContext setView: view];
  143. [view makeActive];
  144. return isActive();
  145. }
  146. bool makeInactive() const noexcept
  147. {
  148. [view makeInactive];
  149. return true;
  150. }
  151. bool isActive() const noexcept
  152. {
  153. return [NSOpenGLContext currentContext] == renderContext;
  154. }
  155. void* getRawContext() const noexcept { return renderContext; }
  156. unsigned int getFrameBufferID() const { return 0; }
  157. void swapBuffers()
  158. {
  159. [renderContext flushBuffer];
  160. }
  161. bool setSwapInterval (const int numFramesPerSwap)
  162. {
  163. [renderContext setValues: (const GLint*) &numFramesPerSwap
  164. forParameter: NSOpenGLCPSwapInterval];
  165. return true;
  166. }
  167. int getSwapInterval() const
  168. {
  169. GLint numFrames = 0;
  170. [renderContext getValues: &numFrames
  171. forParameter: NSOpenGLCPSwapInterval];
  172. return numFrames;
  173. }
  174. void* getNativeWindowHandle() const { return view; }
  175. //==============================================================================
  176. NSOpenGLContext* renderContext;
  177. ThreadSafeNSOpenGLView* view;
  178. private:
  179. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  180. };
  181. //==============================================================================
  182. OpenGLContext* OpenGLComponent::createContext()
  183. {
  184. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (*this, preferredPixelFormat,
  185. contextToShareListsWith != nullptr ? (NSOpenGLContext*) contextToShareListsWith->getRawContext() : nil));
  186. return (c->renderContext != nil) ? c.release() : nullptr;
  187. }
  188. void* OpenGLComponent::getNativeWindowHandle() const
  189. {
  190. return context != nullptr ? static_cast<WindowedGLContext*> (context.get())->getNativeWindowHandle()
  191. : nullptr;
  192. }
  193. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>&)
  194. {
  195. }
  196. //==============================================================================
  197. bool OpenGLHelpers::isContextActive()
  198. {
  199. return CGLGetCurrentContext() != 0;
  200. }