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.

236 lines
8.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. // (This file gets included by juce_mac_NativeCode.mm, rather than being
  24. // compiled on its own).
  25. #if JUCE_INCLUDED_FILE && JUCE_OPENGL
  26. //==============================================================================
  27. class WindowedGLContext : public OpenGLContext
  28. {
  29. public:
  30. WindowedGLContext (Component* const component,
  31. const OpenGLPixelFormat& pixelFormat_,
  32. NSOpenGLContext* sharedContext)
  33. : renderContext (0),
  34. pixelFormat (pixelFormat_)
  35. {
  36. jassert (component != 0);
  37. NSOpenGLPixelFormatAttribute attribs [64];
  38. int n = 0;
  39. attribs[n++] = NSOpenGLPFADoubleBuffer;
  40. attribs[n++] = NSOpenGLPFAAccelerated;
  41. attribs[n++] = NSOpenGLPFAColorSize;
  42. attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.redBits,
  43. pixelFormat.greenBits,
  44. pixelFormat.blueBits);
  45. attribs[n++] = NSOpenGLPFAAlphaSize;
  46. attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.alphaBits;
  47. attribs[n++] = NSOpenGLPFADepthSize;
  48. attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.depthBufferBits;
  49. attribs[n++] = NSOpenGLPFAStencilSize;
  50. attribs[n++] = (NSOpenGLPixelFormatAttribute) pixelFormat.stencilBufferBits;
  51. attribs[n++] = NSOpenGLPFAAccumSize;
  52. attribs[n++] = (NSOpenGLPixelFormatAttribute) jmax (pixelFormat.accumulationBufferRedBits,
  53. pixelFormat.accumulationBufferGreenBits,
  54. pixelFormat.accumulationBufferBlueBits,
  55. pixelFormat.accumulationBufferAlphaBits);
  56. // xxx not sure how to do fullSceneAntiAliasingNumSamples..
  57. attribs[n++] = NSOpenGLPFASampleBuffers;
  58. attribs[n++] = (NSOpenGLPixelFormatAttribute) 1;
  59. attribs[n++] = NSOpenGLPFAClosestPolicy;
  60. attribs[n++] = NSOpenGLPFANoRecovery;
  61. attribs[n++] = (NSOpenGLPixelFormatAttribute) 0;
  62. NSOpenGLPixelFormat* format
  63. = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
  64. NSOpenGLView* view
  65. = [[NSOpenGLView alloc] initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
  66. pixelFormat: format];
  67. renderContext = [view openGLContext];
  68. [format release];
  69. viewHolder = new NSViewComponentInternal (view, component);
  70. }
  71. ~WindowedGLContext()
  72. {
  73. makeInactive();
  74. [renderContext setView: nil];
  75. delete viewHolder;
  76. }
  77. bool makeActive() const throw()
  78. {
  79. jassert (renderContext != 0);
  80. [renderContext makeCurrentContext];
  81. return renderContext != 0;
  82. }
  83. bool makeInactive() const throw()
  84. {
  85. if (! isActive())
  86. [NSOpenGLContext clearCurrentContext];
  87. return true;
  88. }
  89. bool isActive() const throw()
  90. {
  91. return [NSOpenGLContext currentContext] == renderContext;
  92. }
  93. const OpenGLPixelFormat getPixelFormat() const { return pixelFormat; }
  94. void* getRawContext() const throw() { return renderContext; }
  95. void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
  96. {
  97. }
  98. void swapBuffers()
  99. {
  100. glFlush();
  101. [renderContext flushBuffer];
  102. }
  103. bool setSwapInterval (const int numFramesPerSwap)
  104. {
  105. [renderContext setValues: (const GLint*) &numFramesPerSwap
  106. forParameter: NSOpenGLCPSwapInterval];
  107. return true;
  108. }
  109. int getSwapInterval() const
  110. {
  111. GLint numFrames = 0;
  112. [renderContext getValues: &numFrames
  113. forParameter: NSOpenGLCPSwapInterval];
  114. return numFrames;
  115. }
  116. void repaint()
  117. {
  118. // we need to invalidate the juce view that holds this gl view, to make it
  119. // cause a repaint callback
  120. NSView* v = (NSView*) viewHolder->view;
  121. NSRect r = [v frame];
  122. // bit of a bodge here.. if we only invalidate the area of the gl component,
  123. // it's completely covered by the NSOpenGLView, so the OS throws away the
  124. // repaint message, thus never causing our paint() callback, and never repainting
  125. // the comp. So invalidating just a little bit around the edge helps..
  126. [[v superview] setNeedsDisplayInRect: NSInsetRect (r, -2.0f, -2.0f)];
  127. }
  128. void* getNativeWindowHandle() const { return viewHolder->view; }
  129. //==============================================================================
  130. juce_UseDebuggingNewOperator
  131. NSOpenGLContext* renderContext;
  132. private:
  133. OpenGLPixelFormat pixelFormat;
  134. NSViewComponentInternal* viewHolder;
  135. //==============================================================================
  136. WindowedGLContext (const WindowedGLContext&);
  137. const WindowedGLContext& operator= (const WindowedGLContext&);
  138. };
  139. //==============================================================================
  140. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  141. const OpenGLPixelFormat& pixelFormat,
  142. const OpenGLContext* const contextToShareWith)
  143. {
  144. WindowedGLContext* c = new WindowedGLContext (component, pixelFormat,
  145. contextToShareWith != 0 ? (NSOpenGLContext*) contextToShareWith->getRawContext() : 0);
  146. if (c->renderContext == 0)
  147. deleteAndZero (c);
  148. return c;
  149. }
  150. void* OpenGLComponent::getNativeWindowHandle() const
  151. {
  152. return context != 0 ? ((WindowedGLContext*) context)->getNativeWindowHandle()
  153. : 0;
  154. }
  155. void juce_glViewport (const int w, const int h)
  156. {
  157. glViewport (0, 0, w, h);
  158. }
  159. void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
  160. OwnedArray <OpenGLPixelFormat>& results)
  161. {
  162. /* GLint attribs [64];
  163. int n = 0;
  164. attribs[n++] = AGL_RGBA;
  165. attribs[n++] = AGL_DOUBLEBUFFER;
  166. attribs[n++] = AGL_ACCELERATED;
  167. attribs[n++] = AGL_NO_RECOVERY;
  168. attribs[n++] = AGL_NONE;
  169. AGLPixelFormat p = aglChoosePixelFormat (0, 0, attribs);
  170. while (p != 0)
  171. {
  172. OpenGLPixelFormat* const pf = new OpenGLPixelFormat();
  173. pf->redBits = getAGLAttribute (p, AGL_RED_SIZE);
  174. pf->greenBits = getAGLAttribute (p, AGL_GREEN_SIZE);
  175. pf->blueBits = getAGLAttribute (p, AGL_BLUE_SIZE);
  176. pf->alphaBits = getAGLAttribute (p, AGL_ALPHA_SIZE);
  177. pf->depthBufferBits = getAGLAttribute (p, AGL_DEPTH_SIZE);
  178. pf->stencilBufferBits = getAGLAttribute (p, AGL_STENCIL_SIZE);
  179. pf->accumulationBufferRedBits = getAGLAttribute (p, AGL_ACCUM_RED_SIZE);
  180. pf->accumulationBufferGreenBits = getAGLAttribute (p, AGL_ACCUM_GREEN_SIZE);
  181. pf->accumulationBufferBlueBits = getAGLAttribute (p, AGL_ACCUM_BLUE_SIZE);
  182. pf->accumulationBufferAlphaBits = getAGLAttribute (p, AGL_ACCUM_ALPHA_SIZE);
  183. results.add (pf);
  184. p = aglNextPixelFormat (p);
  185. }*/
  186. //jassertfalse //xxx can't see how you do this in cocoa!
  187. }
  188. #endif