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.

187 lines
7.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. class OpenGLContext::NativeContext
  19. {
  20. public:
  21. NativeContext (Component& component,
  22. const OpenGLPixelFormat& pixFormat,
  23. void* contextToShare)
  24. {
  25. NSOpenGLPixelFormatAttribute attribs[] =
  26. {
  27. NSOpenGLPFADoubleBuffer,
  28. NSOpenGLPFAMPSafe,
  29. NSOpenGLPFAClosestPolicy,
  30. NSOpenGLPFANoRecovery,
  31. NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits),
  32. NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits,
  33. NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits,
  34. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits,
  35. NSOpenGLPFAAccumSize, (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
  36. + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits),
  37. pixFormat.multisamplingLevel > 0 ? NSOpenGLPFASamples : (NSOpenGLPixelFormatAttribute) 0,
  38. (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel,
  39. 0
  40. };
  41. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  42. static MouseForwardingNSOpenGLViewClass cls;
  43. view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  44. pixelFormat: format];
  45. #if defined (MAC_OS_X_VERSION_10_7) && (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7)
  46. if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
  47. [view setWantsBestResolutionOpenGLSurface: YES];
  48. #endif
  49. [[NSNotificationCenter defaultCenter] addObserver: view
  50. selector: @selector (_surfaceNeedsUpdate:)
  51. name: NSViewGlobalFrameDidChangeNotification
  52. object: view];
  53. renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
  54. shareContext: (NSOpenGLContext*) contextToShare] autorelease];
  55. [view setOpenGLContext: renderContext];
  56. [format release];
  57. viewAttachment = NSViewComponent::attachViewToComponent (component, view);
  58. }
  59. ~NativeContext()
  60. {
  61. [[NSNotificationCenter defaultCenter] removeObserver: view];
  62. [renderContext clearDrawable];
  63. [renderContext setView: nil];
  64. [view setOpenGLContext: nil];
  65. renderContext = nil;
  66. }
  67. void initialiseOnRenderThread (OpenGLContext&) {}
  68. void shutdownOnRenderThread() { deactivateCurrentContext(); }
  69. bool createdOk() const noexcept { return getRawContext() != nullptr; }
  70. void* getRawContext() const noexcept { return static_cast <void*> (renderContext); }
  71. GLuint getFrameBufferID() const noexcept { return 0; }
  72. bool makeActive() const noexcept
  73. {
  74. jassert (renderContext != nil);
  75. if ([renderContext view] != view)
  76. [renderContext setView: view];
  77. if (NSOpenGLContext* context = [view openGLContext])
  78. {
  79. [context makeCurrentContext];
  80. return true;
  81. }
  82. return false;
  83. }
  84. bool isActive() const noexcept
  85. {
  86. return [NSOpenGLContext currentContext] == renderContext;
  87. }
  88. static void deactivateCurrentContext()
  89. {
  90. [NSOpenGLContext clearCurrentContext];
  91. }
  92. struct Locker
  93. {
  94. Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
  95. {
  96. CGLLockContext (cglContext);
  97. }
  98. ~Locker()
  99. {
  100. CGLUnlockContext (cglContext);
  101. }
  102. private:
  103. CGLContextObj cglContext;
  104. };
  105. void swapBuffers()
  106. {
  107. [renderContext flushBuffer];
  108. }
  109. void updateWindowPosition (const Rectangle<int>&) {}
  110. bool setSwapInterval (int numFramesPerSwap)
  111. {
  112. [renderContext setValues: (const GLint*) &numFramesPerSwap
  113. forParameter: NSOpenGLCPSwapInterval];
  114. return true;
  115. }
  116. int getSwapInterval() const
  117. {
  118. GLint numFrames = 0;
  119. [renderContext getValues: &numFrames
  120. forParameter: NSOpenGLCPSwapInterval];
  121. return numFrames;
  122. }
  123. NSOpenGLContext* renderContext;
  124. NSOpenGLView* view;
  125. ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
  126. //==============================================================================
  127. struct MouseForwardingNSOpenGLViewClass : public ObjCClass <NSOpenGLView>
  128. {
  129. MouseForwardingNSOpenGLViewClass() : ObjCClass <NSOpenGLView> ("JUCEGLView_")
  130. {
  131. addMethod (@selector (rightMouseDown:), rightMouseDown, "v@:@");
  132. addMethod (@selector (rightMouseUp:), rightMouseUp, "v@:@");
  133. addMethod (@selector (acceptsFirstMouse:), acceptsFirstMouse, "v@:@");
  134. registerClass();
  135. }
  136. private:
  137. static void rightMouseDown (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
  138. static void rightMouseUp (id self, SEL, NSEvent* ev) { [[(NSOpenGLView*) self superview] rightMouseUp: ev]; }
  139. static BOOL acceptsFirstMouse (id, SEL, NSEvent*) { return YES; }
  140. };
  141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
  142. };
  143. //==============================================================================
  144. bool OpenGLHelpers::isContextActive()
  145. {
  146. return CGLGetCurrentContext() != 0;
  147. }