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.

247 lines
9.1KB

  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. if (sharedContext != 0)
  68. {
  69. renderContext = [[NSOpenGLContext alloc] initWithFormat: format
  70. shareContext: sharedContext];
  71. [view setOpenGLContext: renderContext];
  72. [renderContext setView: view];
  73. }
  74. else
  75. {
  76. renderContext = [view openGLContext];
  77. }
  78. [format release];
  79. viewHolder = new NSViewComponentInternal (view, component);
  80. }
  81. ~WindowedGLContext()
  82. {
  83. makeInactive();
  84. [renderContext setView: nil];
  85. delete viewHolder;
  86. }
  87. bool makeActive() const throw()
  88. {
  89. jassert (renderContext != 0);
  90. [renderContext makeCurrentContext];
  91. return renderContext != 0;
  92. }
  93. bool makeInactive() const throw()
  94. {
  95. if (! isActive())
  96. [NSOpenGLContext clearCurrentContext];
  97. return true;
  98. }
  99. bool isActive() const throw()
  100. {
  101. return [NSOpenGLContext currentContext] == renderContext;
  102. }
  103. const OpenGLPixelFormat getPixelFormat() const { return pixelFormat; }
  104. void* getRawContext() const throw() { return renderContext; }
  105. void updateWindowPosition (int x, int y, int w, int h, int outerWindowHeight)
  106. {
  107. }
  108. void swapBuffers()
  109. {
  110. glFlush();
  111. [renderContext flushBuffer];
  112. }
  113. bool setSwapInterval (const int numFramesPerSwap)
  114. {
  115. [renderContext setValues: (const GLint*) &numFramesPerSwap
  116. forParameter: NSOpenGLCPSwapInterval];
  117. return true;
  118. }
  119. int getSwapInterval() const
  120. {
  121. GLint numFrames = 0;
  122. [renderContext getValues: &numFrames
  123. forParameter: NSOpenGLCPSwapInterval];
  124. return numFrames;
  125. }
  126. void repaint()
  127. {
  128. // we need to invalidate the juce view that holds this gl view, to make it
  129. // cause a repaint callback
  130. NSView* v = (NSView*) viewHolder->view;
  131. NSRect r = [v frame];
  132. // bit of a bodge here.. if we only invalidate the area of the gl component,
  133. // it's completely covered by the NSOpenGLView, so the OS throws away the
  134. // repaint message, thus never causing our paint() callback, and never repainting
  135. // the comp. So invalidating just a little bit around the edge helps..
  136. [[v superview] setNeedsDisplayInRect: NSInsetRect (r, -2.0f, -2.0f)];
  137. }
  138. void* getNativeWindowHandle() const { return viewHolder->view; }
  139. //==============================================================================
  140. juce_UseDebuggingNewOperator
  141. NSOpenGLContext* renderContext;
  142. private:
  143. OpenGLPixelFormat pixelFormat;
  144. NSViewComponentInternal* viewHolder;
  145. //==============================================================================
  146. WindowedGLContext (const WindowedGLContext&);
  147. const WindowedGLContext& operator= (const WindowedGLContext&);
  148. };
  149. //==============================================================================
  150. OpenGLContext* OpenGLContext::createContextForWindow (Component* const component,
  151. const OpenGLPixelFormat& pixelFormat,
  152. const OpenGLContext* const contextToShareWith)
  153. {
  154. WindowedGLContext* c = new WindowedGLContext (component, pixelFormat,
  155. contextToShareWith != 0 ? (NSOpenGLContext*) contextToShareWith->getRawContext() : 0);
  156. if (c->renderContext == 0)
  157. deleteAndZero (c);
  158. return c;
  159. }
  160. void* OpenGLComponent::getNativeWindowHandle() const
  161. {
  162. return context != 0 ? ((WindowedGLContext*) context)->getNativeWindowHandle()
  163. : 0;
  164. }
  165. void juce_glViewport (const int w, const int h)
  166. {
  167. glViewport (0, 0, w, h);
  168. }
  169. void OpenGLPixelFormat::getAvailablePixelFormats (Component* /*component*/,
  170. OwnedArray <OpenGLPixelFormat>& results)
  171. {
  172. /* GLint attribs [64];
  173. int n = 0;
  174. attribs[n++] = AGL_RGBA;
  175. attribs[n++] = AGL_DOUBLEBUFFER;
  176. attribs[n++] = AGL_ACCELERATED;
  177. attribs[n++] = AGL_NO_RECOVERY;
  178. attribs[n++] = AGL_NONE;
  179. AGLPixelFormat p = aglChoosePixelFormat (0, 0, attribs);
  180. while (p != 0)
  181. {
  182. OpenGLPixelFormat* const pf = new OpenGLPixelFormat();
  183. pf->redBits = getAGLAttribute (p, AGL_RED_SIZE);
  184. pf->greenBits = getAGLAttribute (p, AGL_GREEN_SIZE);
  185. pf->blueBits = getAGLAttribute (p, AGL_BLUE_SIZE);
  186. pf->alphaBits = getAGLAttribute (p, AGL_ALPHA_SIZE);
  187. pf->depthBufferBits = getAGLAttribute (p, AGL_DEPTH_SIZE);
  188. pf->stencilBufferBits = getAGLAttribute (p, AGL_STENCIL_SIZE);
  189. pf->accumulationBufferRedBits = getAGLAttribute (p, AGL_ACCUM_RED_SIZE);
  190. pf->accumulationBufferGreenBits = getAGLAttribute (p, AGL_ACCUM_GREEN_SIZE);
  191. pf->accumulationBufferBlueBits = getAGLAttribute (p, AGL_ACCUM_BLUE_SIZE);
  192. pf->accumulationBufferAlphaBits = getAGLAttribute (p, AGL_ACCUM_ALPHA_SIZE);
  193. results.add (pf);
  194. p = aglNextPixelFormat (p);
  195. }*/
  196. //jassertfalse //xxx can't see how you do this in cocoa!
  197. }
  198. #endif