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.

259 lines
8.3KB

  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. NSOpenGLPFAAccelerated,
  104. NSOpenGLPFAMPSafe,
  105. NSOpenGLPFAClosestPolicy,
  106. NSOpenGLPFANoRecovery,
  107. NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) (pixelFormat.redBits
  108. + pixelFormat.greenBits
  109. + pixelFormat.blueBits),
  110. NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixelFormat.alphaBits,
  111. NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixelFormat.depthBufferBits,
  112. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixelFormat.stencilBufferBits,
  113. NSOpenGLPFAAccumSize, (NSOpenGLPixelFormatAttribute) (pixelFormat.accumulationBufferRedBits
  114. + pixelFormat.accumulationBufferGreenBits
  115. + pixelFormat.accumulationBufferBlueBits
  116. + pixelFormat.accumulationBufferAlphaBits),
  117. NSOpenGLPFASampleBuffers, (NSOpenGLPixelFormatAttribute) 1,
  118. (NSOpenGLPixelFormatAttribute) 0
  119. };
  120. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  121. view = [[ThreadSafeNSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  122. pixelFormat: format];
  123. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  124. shareContext: sharedContext] autorelease];
  125. const GLint swapInterval = 1;
  126. [renderContext setValues: &swapInterval forParameter: NSOpenGLCPSwapInterval];
  127. [view setOpenGLContext: renderContext];
  128. [format release];
  129. component.setView (view);
  130. }
  131. ~WindowedGLContext()
  132. {
  133. deleteContext();
  134. }
  135. void deleteContext()
  136. {
  137. makeInactive();
  138. [renderContext clearDrawable];
  139. [renderContext setView: nil];
  140. [view setOpenGLContext: nil];
  141. renderContext = nil;
  142. }
  143. bool makeActive() const noexcept
  144. {
  145. jassert (renderContext != nil);
  146. if ([renderContext view] != view)
  147. [renderContext setView: view];
  148. [view makeActive];
  149. return isActive();
  150. }
  151. bool makeInactive() const noexcept
  152. {
  153. [view makeInactive];
  154. return true;
  155. }
  156. bool isActive() const noexcept
  157. {
  158. return [NSOpenGLContext currentContext] == renderContext;
  159. }
  160. void* getRawContext() const noexcept { return renderContext; }
  161. unsigned int getFrameBufferID() const { return 0; }
  162. void swapBuffers()
  163. {
  164. [renderContext flushBuffer];
  165. }
  166. bool setSwapInterval (const int numFramesPerSwap)
  167. {
  168. [renderContext setValues: (const GLint*) &numFramesPerSwap
  169. forParameter: NSOpenGLCPSwapInterval];
  170. return true;
  171. }
  172. int getSwapInterval() const
  173. {
  174. GLint numFrames = 0;
  175. [renderContext getValues: &numFrames
  176. forParameter: NSOpenGLCPSwapInterval];
  177. return numFrames;
  178. }
  179. void* getNativeWindowHandle() const { return view; }
  180. //==============================================================================
  181. NSOpenGLContext* renderContext;
  182. ThreadSafeNSOpenGLView* view;
  183. private:
  184. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (WindowedGLContext);
  185. };
  186. //==============================================================================
  187. OpenGLContext* OpenGLComponent::createContext()
  188. {
  189. ScopedPointer<WindowedGLContext> c (new WindowedGLContext (*this, preferredPixelFormat,
  190. contextToShareListsWith != nullptr ? (NSOpenGLContext*) contextToShareListsWith->getRawContext() : nil));
  191. return (c->renderContext != nil) ? c.release() : nullptr;
  192. }
  193. void* OpenGLComponent::getNativeWindowHandle() const
  194. {
  195. return context != nullptr ? static_cast<WindowedGLContext*> (context.get())->getNativeWindowHandle()
  196. : nullptr;
  197. }
  198. void OpenGLComponent::updateEmbeddedPosition (const Rectangle<int>&)
  199. {
  200. }
  201. //==============================================================================
  202. bool OpenGLHelpers::isContextActive()
  203. {
  204. return CGLGetCurrentContext() != 0;
  205. }