1 /*
2   ==============================================================================
3 
4    This file is part of the JUCE library.
5    Copyright (c) 2020 - Raw Material Software Limited
6 
7    JUCE is an open source library subject to commercial or open-source
8    licensing.
9 
10    By using JUCE, you agree to the terms of both the JUCE 6 End-User License
11    Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
12 
13    End User License Agreement: www.juce.com/juce-6-licence
14    Privacy Policy: www.juce.com/juce-privacy-policy
15 
16    Or: You may also use this code under the terms of the GPL v3 (see
17    www.gnu.org/licenses).
18 
19    JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20    EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21    DISCLAIMED.
22 
23   ==============================================================================
24 */
25 
26 namespace juce
27 {
28 
29 JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wdeprecated-declarations")
30 
31 class OpenGLContext::NativeContext
32 {
33 public:
NativeContext(Component & component,const OpenGLPixelFormat & pixFormat,void * contextToShare,bool shouldUseMultisampling,OpenGLVersion version)34     NativeContext (Component& component,
35                    const OpenGLPixelFormat& pixFormat,
36                    void* contextToShare,
37                    bool shouldUseMultisampling,
38                    OpenGLVersion version)
39     {
40         NSOpenGLPixelFormatAttribute attribs[64] = { 0 };
41         createAttribs (attribs, version, pixFormat, shouldUseMultisampling);
42 
43         NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes: attribs];
44 
45         static MouseForwardingNSOpenGLViewClass cls;
46         view = [cls.createInstance() initWithFrame: NSMakeRect (0, 0, 100.0f, 100.0f)
47                                        pixelFormat: format];
48 
49         if ([view respondsToSelector: @selector (setWantsBestResolutionOpenGLSurface:)])
50             [view setWantsBestResolutionOpenGLSurface: YES];
51 
52         JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wundeclared-selector")
53         [[NSNotificationCenter defaultCenter] addObserver: view
54                                                  selector: @selector (_surfaceNeedsUpdate:)
55                                                      name: NSViewGlobalFrameDidChangeNotification
56                                                    object: view];
57         JUCE_END_IGNORE_WARNINGS_GCC_LIKE
58 
59         renderContext = [[[NSOpenGLContext alloc] initWithFormat: format
60                                                     shareContext: (NSOpenGLContext*) contextToShare] autorelease];
61 
62         [view setOpenGLContext: renderContext];
63         [format release];
64 
65         viewAttachment = NSViewComponent::attachViewToComponent (component, view);
66     }
67 
~NativeContext()68     ~NativeContext()
69     {
70         [[NSNotificationCenter defaultCenter] removeObserver: view];
71         [renderContext clearDrawable];
72         [renderContext setView: nil];
73         [view setOpenGLContext: nil];
74         [view release];
75     }
76 
createAttribs(NSOpenGLPixelFormatAttribute * attribs,OpenGLVersion version,const OpenGLPixelFormat & pixFormat,bool shouldUseMultisampling)77     static void createAttribs (NSOpenGLPixelFormatAttribute* attribs, OpenGLVersion version,
78                                const OpenGLPixelFormat& pixFormat, bool shouldUseMultisampling)
79     {
80         ignoreUnused (version);
81         int numAttribs = 0;
82 
83        #if JUCE_OPENGL3
84         attribs[numAttribs++] = NSOpenGLPFAOpenGLProfile;
85         attribs[numAttribs++] = version >= openGL3_2 ? NSOpenGLProfileVersion3_2Core
86                                                      : NSOpenGLProfileVersionLegacy;
87        #endif
88 
89         attribs[numAttribs++] = NSOpenGLPFADoubleBuffer;
90         attribs[numAttribs++] = NSOpenGLPFAClosestPolicy;
91         attribs[numAttribs++] = NSOpenGLPFANoRecovery;
92         attribs[numAttribs++] = NSOpenGLPFAColorSize;
93         attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.redBits + pixFormat.greenBits + pixFormat.blueBits);
94         attribs[numAttribs++] = NSOpenGLPFAAlphaSize;
95         attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.alphaBits;
96         attribs[numAttribs++] = NSOpenGLPFADepthSize;
97         attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.depthBufferBits;
98         attribs[numAttribs++] = NSOpenGLPFAStencilSize;
99         attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.stencilBufferBits;
100         attribs[numAttribs++] = NSOpenGLPFAAccumSize;
101         attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) (pixFormat.accumulationBufferRedBits + pixFormat.accumulationBufferGreenBits
102                                                                    + pixFormat.accumulationBufferBlueBits + pixFormat.accumulationBufferAlphaBits);
103 
104         if (shouldUseMultisampling)
105         {
106             attribs[numAttribs++] = NSOpenGLPFAMultisample;
107             attribs[numAttribs++] = NSOpenGLPFASampleBuffers;
108             attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) 1;
109             attribs[numAttribs++] = NSOpenGLPFASamples;
110             attribs[numAttribs++] = (NSOpenGLPixelFormatAttribute) pixFormat.multisamplingLevel;
111         }
112     }
113 
initialiseOnRenderThread(OpenGLContext &)114     bool initialiseOnRenderThread (OpenGLContext&)    { return true; }
shutdownOnRenderThread()115     void shutdownOnRenderThread()                     { deactivateCurrentContext(); }
116 
createdOk()117     bool createdOk() const noexcept                   { return getRawContext() != nullptr; }
getRawContext()118     void* getRawContext() const noexcept              { return static_cast<void*> (renderContext); }
getFrameBufferID()119     GLuint getFrameBufferID() const noexcept          { return 0; }
120 
makeActive()121     bool makeActive() const noexcept
122     {
123         jassert (renderContext != nil);
124 
125         if ([renderContext view] != view)
126             [renderContext setView: view];
127 
128         if (NSOpenGLContext* context = [view openGLContext])
129         {
130             [context makeCurrentContext];
131             return true;
132         }
133 
134         return false;
135     }
136 
isActive()137     bool isActive() const noexcept
138     {
139         return [NSOpenGLContext currentContext] == renderContext;
140     }
141 
deactivateCurrentContext()142     static void deactivateCurrentContext()
143     {
144         [NSOpenGLContext clearCurrentContext];
145     }
146 
147     struct Locker
148     {
LockerLocker149         Locker (NativeContext& nc) : cglContext ((CGLContextObj) [nc.renderContext CGLContextObj])
150         {
151             CGLLockContext (cglContext);
152         }
153 
~LockerLocker154         ~Locker()
155         {
156             CGLUnlockContext (cglContext);
157         }
158 
159     private:
160         CGLContextObj cglContext;
161     };
162 
swapBuffers()163     void swapBuffers()
164     {
165         auto now = Time::getMillisecondCounterHiRes();
166         [renderContext flushBuffer];
167 
168         if (minSwapTimeMs > 0)
169         {
170             // When our window is entirely occluded by other windows, flushBuffer
171             // fails to wait for the swap interval, so the render loop spins at full
172             // speed, burning CPU. This hack detects when things are going too fast
173             // and sleeps if necessary.
174 
175             auto swapTime = Time::getMillisecondCounterHiRes() - now;
176             auto frameTime = (int) (now - lastSwapTime);
177 
178             if (swapTime < 0.5 && frameTime < minSwapTimeMs - 3)
179             {
180                 if (underrunCounter > 3)
181                 {
182                     Thread::sleep (2 * (minSwapTimeMs - frameTime));
183                     now = Time::getMillisecondCounterHiRes();
184                 }
185                 else
186                 {
187                     ++underrunCounter;
188                 }
189             }
190             else
191             {
192                 if (underrunCounter > 0)
193                     --underrunCounter;
194             }
195         }
196 
197         lastSwapTime = now;
198     }
199 
updateWindowPosition(Rectangle<int>)200     void updateWindowPosition (Rectangle<int>) {}
201 
setSwapInterval(int numFramesPerSwap)202     bool setSwapInterval (int numFramesPerSwap)
203     {
204         // The macOS OpenGL programming guide says that numFramesPerSwap
205         // can only be 0 or 1.
206         jassert (isPositiveAndBelow (numFramesPerSwap, 2));
207 
208         minSwapTimeMs = (numFramesPerSwap * 1000) / 60;
209 
210         [renderContext setValues: (const GLint*) &numFramesPerSwap
211                    #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
212                     forParameter: NSOpenGLContextParameterSwapInterval];
213                    #else
214                     forParameter: NSOpenGLCPSwapInterval];
215                    #endif
216         return true;
217     }
218 
getSwapInterval()219     int getSwapInterval() const
220     {
221         GLint numFrames = 0;
222         [renderContext getValues: &numFrames
223                    #if defined (MAC_OS_X_VERSION_10_12) && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12
224                     forParameter: NSOpenGLContextParameterSwapInterval];
225                    #else
226                     forParameter: NSOpenGLCPSwapInterval];
227                    #endif
228 
229         return numFrames;
230     }
231 
232     NSOpenGLContext* renderContext = nil;
233     NSOpenGLView* view = nil;
234     ReferenceCountedObjectPtr<ReferenceCountedObject> viewAttachment;
235     double lastSwapTime = 0;
236     int minSwapTimeMs = 0, underrunCounter = 0;
237 
238     //==============================================================================
239     struct MouseForwardingNSOpenGLViewClass  : public ObjCClass<NSOpenGLView>
240     {
MouseForwardingNSOpenGLViewClassMouseForwardingNSOpenGLViewClass241         MouseForwardingNSOpenGLViewClass()  : ObjCClass<NSOpenGLView> ("JUCEGLView_")
242         {
243             addMethod (@selector (rightMouseDown:),      rightMouseDown,     "v@:@");
244             addMethod (@selector (rightMouseUp:),        rightMouseUp,       "v@:@");
245             addMethod (@selector (acceptsFirstMouse:),   acceptsFirstMouse,  "v@:@");
246 
247             registerClass();
248         }
249 
250     private:
rightMouseDownMouseForwardingNSOpenGLViewClass251         static void rightMouseDown (id self, SEL, NSEvent* ev)      { [[(NSOpenGLView*) self superview] rightMouseDown: ev]; }
rightMouseUpMouseForwardingNSOpenGLViewClass252         static void rightMouseUp   (id self, SEL, NSEvent* ev)      { [[(NSOpenGLView*) self superview] rightMouseUp:   ev]; }
acceptsFirstMouseMouseForwardingNSOpenGLViewClass253         static BOOL acceptsFirstMouse (id, SEL, NSEvent*)           { return YES; }
254     };
255 
256 
257     JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NativeContext)
258 };
259 
260 //==============================================================================
isContextActive()261 bool OpenGLHelpers::isContextActive()
262 {
263     return CGLGetCurrentContext() != CGLContextObj();
264 }
265 
266 JUCE_END_IGNORE_WARNINGS_GCC_LIKE
267 
268 } // namespace juce
269