1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "GLContextProvider.h"
7 #include "GLContextCGL.h"
8 #include "GLLibraryLoader.h"
10 #include "nsIWidget.h"
11 #include <OpenGL/gl.h>
12 #include "gfxFailure.h"
13 #include "mozilla/IntegerRange.h"
14 #include "mozilla/StaticPrefs_gfx.h"
15 #include "mozilla/StaticPrefs_gl.h"
16 #include "mozilla/StaticPrefs_layout.h"
19 #include "mozilla/ProfilerLabels.h"
20 #include "MozFramebuffer.h"
21 #include "mozilla/layers/CompositorOptions.h"
22 #include "mozilla/widget/CompositorWidget.h"
23 #include "ScopedGLHelpers.h"
25 #include <OpenGL/OpenGL.h>
30 using namespace mozilla::gfx;
31 using namespace mozilla::widget;
35 bool EnsureInitialized() {
41 PR_LoadLibrary("/System/Library/Frameworks/OpenGL.framework/OpenGL");
43 NS_WARNING("Couldn't load OpenGL Framework.");
52 const auto& Library() const { return mOGLLibrary; }
55 bool mInitialized = false;
56 PRLibrary* mOGLLibrary = nullptr;
59 CGLLibrary sCGLLibrary;
61 GLContextCGL::GLContextCGL(const GLContextDesc& desc, NSOpenGLContext* context)
62 : GLContext(desc), mContext(context) {
63 CGDisplayRegisterReconfigurationCallback(DisplayReconfigurationCallback,
67 GLContextCGL::~GLContextCGL() {
70 CGDisplayRemoveReconfigurationCallback(DisplayReconfigurationCallback, this);
73 if ([NSOpenGLContext currentContext] == mContext) {
74 // Clear the current context before releasing. If we don't do
75 // this, the next time we call [NSOpenGLContext currentContext],
76 // "invalid context" will be printed to the console.
77 [NSOpenGLContext clearCurrentContext];
83 CGLContextObj GLContextCGL::GetCGLContext() const {
84 return static_cast<CGLContextObj>([mContext CGLContextObj]);
87 bool GLContextCGL::MakeCurrentImpl() const {
89 GLContext::ResetTLSCurrentContext();
91 [mContext makeCurrentContext];
92 MOZ_ASSERT(IsCurrentImpl());
93 // Use non-blocking swap in "ASAP mode".
94 // ASAP mode means that rendering is iterated as fast as possible.
95 // ASAP mode is entered when layout.frame_rate=0 (requires restart).
96 // If swapInt is 1, then glSwapBuffers will block and wait for a vblank
97 // signal. When we're iterating as fast as possible, however, we want a
98 // non-blocking glSwapBuffers, which will happen when swapInt==0.
99 GLint swapInt = StaticPrefs::layout_frame_rate() == 0 ? 0 : 1;
100 [mContext setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
105 bool GLContextCGL::IsCurrentImpl() const {
106 return [NSOpenGLContext currentContext] == mContext;
109 /* static */ void GLContextCGL::DisplayReconfigurationCallback(
110 CGDirectDisplayID aDisplay, CGDisplayChangeSummaryFlags aFlags,
112 if (aFlags & kCGDisplaySetModeFlag) {
113 static_cast<GLContextCGL*>(aUserInfo)->mActiveGPUSwitchMayHaveOccurred =
118 static NSOpenGLContext* CreateWithFormat(
119 const NSOpenGLPixelFormatAttribute* attribs) {
120 NSOpenGLPixelFormat* format =
121 [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
123 NS_WARNING("Failed to create NSOpenGLPixelFormat.");
127 NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:format
128 shareContext:nullptr];
135 // Get the "OpenGL display mask" for a fresh context. The return value of this
136 // function depends on the time at which this function is called.
137 // In practice, on a Macbook Pro with an integrated and a discrete GPU, this
138 // function returns the display mask for the GPU that currently drives the
141 // Quick reference of the concepts involved in the code below:
142 // GPU switch: On Mac devices with an integrated and a discrete GPU, a GPU
143 // switch changes which
144 // GPU drives the internal display. Both GPUs are still usable at all times.
145 // (When the integrated GPU is driving the internal display, using the
146 // discrete GPU can incur a longer warm-up cost.)
147 // Virtual screen: A CGL concept. A "virtual screen" corresponds to a GL
148 // renderer. There's one
149 // for the integrated GPU, one for each discrete GPU, and one for the Apple
150 // software renderer. The list of virtual screens is
151 // per-NSOpenGLPixelFormat; it is filtered down to only the renderers that
152 // support the requirements from the pixel format attributes. Indexes into
153 // this list (such as currentVirtualScreen) cannot be used interchangably
154 // across different NSOpenGLPixelFormat instances.
155 // Display mask: A bitset per GL renderer. Different renderers have disjoint
156 // display masks. The
157 // Apple software renderer has all bits zeroed. For each CGDirectDisplayID,
158 // CGDisplayIDToOpenGLDisplayMask(displayID) returns a single bit in the
160 // CGDirectDisplayID: An ID for each (physical screen, GPU which can drive
161 // this screen) pair. The
162 // current CGDirectDisplayID for an NSScreen object can be obtained using
163 // [[[screen deviceDescription] objectForKey:@"NSScreenNumber"]
164 // unsignedIntValue]; it changes depending on which GPU is currently driving
166 static CGOpenGLDisplayMask GetFreshContextDisplayMask() {
167 NSOpenGLPixelFormatAttribute attribs[] = {NSOpenGLPFAAllowOfflineRenderers,
169 NSOpenGLPixelFormat* pixelFormat =
170 [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
171 MOZ_RELEASE_ASSERT(pixelFormat);
172 NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat
173 shareContext:nullptr];
174 GLint displayMask = 0;
175 [pixelFormat getValues:&displayMask
176 forAttribute:NSOpenGLPFAScreenMask
177 forVirtualScreen:[context currentVirtualScreen]];
178 [pixelFormat release];
180 return static_cast<CGOpenGLDisplayMask>(displayMask);
183 static bool IsSameGPU(CGOpenGLDisplayMask mask1, CGOpenGLDisplayMask mask2) {
184 if ((mask1 & mask2) != 0) {
187 // Both masks can be zero, when using the Apple software renderer.
188 return !mask1 && !mask2;
191 void GLContextCGL::MigrateToActiveGPU() {
192 if (!mActiveGPUSwitchMayHaveOccurred.compareExchange(true, false)) {
196 CGOpenGLDisplayMask newPreferredDisplayMask = GetFreshContextDisplayMask();
197 NSOpenGLPixelFormat* pixelFormat = [mContext pixelFormat];
198 GLint currentVirtualScreen = [mContext currentVirtualScreen];
199 GLint currentDisplayMask = 0;
200 [pixelFormat getValues:¤tDisplayMask
201 forAttribute:NSOpenGLPFAScreenMask
202 forVirtualScreen:currentVirtualScreen];
203 if (IsSameGPU(currentDisplayMask, newPreferredDisplayMask)) {
204 // No "virtual screen" change needed.
208 // Find the "virtual screen" with a display mask that matches
209 // newPreferredDisplayMask, if available, and switch the context over to it.
210 // This code was inspired by equivalent functionality in -[NSOpenGLContext
211 // update] which only kicks in for contexts that present via a CAOpenGLLayer.
212 for (const auto i : IntegerRange([pixelFormat numberOfVirtualScreens])) {
213 GLint displayMask = 0;
214 [pixelFormat getValues:&displayMask
215 forAttribute:NSOpenGLPFAScreenMask
217 if (IsSameGPU(displayMask, newPreferredDisplayMask)) {
218 CGLSetVirtualScreen([mContext CGLContextObj], i);
224 GLenum GLContextCGL::GetPreferredARGB32Format() const { return LOCAL_GL_BGRA; }
226 bool GLContextCGL::SwapBuffers() {
227 AUTO_PROFILER_LABEL("GLContextCGL::SwapBuffers", GRAPHICS);
229 // We do not have a default framebuffer. Just do a flush.
230 // Flushing is necessary if we want our IOSurfaces to have the correct
231 // content once they're picked up by the WindowServer from our CALayers.
237 void GLContextCGL::GetWSIInfo(nsCString* const out) const {
238 out->AppendLiteral("CGL");
241 Maybe<SymbolLoader> GLContextCGL::GetSymbolLoader() const {
242 const auto& lib = sCGLLibrary.Library();
243 return Some(SymbolLoader(*lib));
246 already_AddRefed<GLContext> GLContextProviderCGL::CreateForCompositorWidget(
247 CompositorWidget* aCompositorWidget, bool aHardwareWebRender,
248 bool aForceAccelerated) {
249 CreateContextFlags flags = CreateContextFlags::ALLOW_OFFLINE_RENDERER;
250 if (aForceAccelerated) {
251 flags |= CreateContextFlags::FORBID_SOFTWARE;
253 if (!aHardwareWebRender) {
254 flags |= CreateContextFlags::REQUIRE_COMPAT_PROFILE;
256 nsCString failureUnused;
257 return CreateHeadless({flags}, &failureUnused);
260 static RefPtr<GLContextCGL> CreateOffscreenFBOContext(
261 GLContextCreateDesc desc) {
262 if (!sCGLLibrary.EnsureInitialized()) {
266 NSOpenGLContext* context = nullptr;
268 std::vector<NSOpenGLPixelFormatAttribute> attribs;
269 auto& flags = desc.flags;
271 if (!StaticPrefs::gl_allow_high_power()) {
272 flags &= ~CreateContextFlags::HIGH_POWER;
274 if (flags & CreateContextFlags::ALLOW_OFFLINE_RENDERER ||
275 !(flags & CreateContextFlags::HIGH_POWER)) {
276 // This is really poorly named on Apple's part, but "AllowOfflineRenderers"
277 // means that we want to allow running on the iGPU instead of requiring the
279 attribs.push_back(NSOpenGLPFAAllowOfflineRenderers);
282 if (flags & CreateContextFlags::FORBID_SOFTWARE) {
283 if (flags & CreateContextFlags::FORBID_HARDWARE) {
284 NS_WARNING("Both !hardware and !software.");
287 attribs.push_back(NSOpenGLPFAAccelerated);
289 if (flags & CreateContextFlags::FORBID_HARDWARE) {
290 /* NSOpenGLPFARendererID:
291 * > OpenGL renderers that match the specified ID are preferred.
292 * > Constants to select specific renderers are provided in the
293 * > GLRenderers.h header of the OpenGL framework.
294 * > Of note is kCGLRendererGenericID which selects the Apple software
296 * > The other constants select renderers for specific hardware vendors.
298 attribs.push_back(NSOpenGLPFARendererID);
299 attribs.push_back(kCGLRendererGenericID);
302 if (!(flags & CreateContextFlags::REQUIRE_COMPAT_PROFILE)) {
303 auto coreAttribs = attribs;
304 coreAttribs.push_back(NSOpenGLPFAOpenGLProfile);
305 coreAttribs.push_back(NSOpenGLProfileVersion3_2Core);
306 coreAttribs.push_back(0);
307 context = CreateWithFormat(coreAttribs.data());
311 attribs.push_back(0);
312 context = CreateWithFormat(attribs.data());
316 NS_WARNING("Failed to create NSOpenGLContext.");
320 RefPtr<GLContextCGL> glContext = new GLContextCGL({desc, true}, context);
322 if (flags & CreateContextFlags::PREFER_MULTITHREADED) {
323 CGLEnable(glContext->GetCGLContext(), kCGLCEMPEngine);
328 already_AddRefed<GLContext> GLContextProviderCGL::CreateHeadless(
329 const GLContextCreateDesc& desc, nsACString* const out_failureId) {
330 auto gl = CreateOffscreenFBOContext(desc);
332 *out_failureId = "FEATURE_FAILURE_CGL_FBO"_ns;
337 *out_failureId = "FEATURE_FAILURE_CGL_INIT"_ns;
338 NS_WARNING("Failed during Init.");
345 static RefPtr<GLContext> gGlobalContext;
347 GLContext* GLContextProviderCGL::GetGlobalContext() {
348 static bool triedToCreateContext = false;
349 if (!triedToCreateContext) {
350 triedToCreateContext = true;
352 MOZ_RELEASE_ASSERT(!gGlobalContext);
353 nsCString discardFailureId;
354 RefPtr<GLContext> temp = CreateHeadless({}, &discardFailureId);
355 gGlobalContext = temp;
357 if (!gGlobalContext) {
358 NS_WARNING("Couldn't init gGlobalContext.");
362 return gGlobalContext;
365 void GLContextProviderCGL::Shutdown() { gGlobalContext = nullptr; }
368 } /* namespace mozilla */