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.

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