Debian Codegraph @

tools/ganesh/gl/mac/CreatePlatformGLTestContext_mac.cpp

raw
1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "include/core/SkTypes.h"
9
10 #include "include/gpu/ganesh/gl/mac/GrGLMakeMacInterface.h"
11 #include "tools/ganesh/gl/GLTestContext.h"
12
13 #include <OpenGL/OpenGL.h>
14 #include <dlfcn.h>
15
16 // All of NSOpenGL is deprecated.
17 #pragma clang diagnostic push
18 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
19
20 namespace {
21
22 std::function<void()> context_restorer() {
23 auto context = CGLGetCurrentContext();
24 return [context] { CGLSetCurrentContext(context); };
25 }
26
27 class MacGLTestContext : public sk_gpu_test::GLTestContext {
28 public:
29 MacGLTestContext(MacGLTestContext* shareContext);
30 ~MacGLTestContext() override;
31
32 private:
33 void destroyGLContext();
34
35 void onPlatformMakeNotCurrent() const override;
36 void onPlatformMakeCurrent() const override;
37 std::function<void()> onPlatformGetAutoContextRestore() const override;
38 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
39
40 CGLContextObj fContext;
41 void* fGLLibrary;
42 };
43
44 MacGLTestContext::MacGLTestContext(MacGLTestContext* shareContext)
45 : fContext(nullptr)
46 , fGLLibrary(RTLD_DEFAULT) {
47 // We first try to request a Radeon eGPU if one is available.
48 // This will be a Radeon HD7000 and up, which includes all eGPU configs.
49 // If that fails, we try again with only the base parameters.
50 CGLPixelFormatAttribute attributes[] = {
51 // base parameters
52 kCGLPFAOpenGLProfile,
53 (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
54 kCGLPFADoubleBuffer,
55
56 // eGPU parameters
57 kCGLPFAAllowOfflineRenderers, // Enables e-GPU.
58 kCGLPFANoRecovery, // Disallows software rendering.
59 kCGLPFARendererID, (CGLPixelFormatAttribute)kCGLRendererATIRadeonX4000ID, // Select Radeon
60 (CGLPixelFormatAttribute)NULL
61 };
62 static const int kFirstEGPUParameter = 3;
63 SkASSERT(kCGLPFAAllowOfflineRenderers == attributes[kFirstEGPUParameter]);
64
65 CGLPixelFormatObj pixFormat;
66 GLint npix;
67 CGLChoosePixelFormat(attributes, &pixFormat, &npix);
68
69 if (nullptr == pixFormat) {
70 // Move the NULL-termination up to remove the eGPU parameters and try again
71 attributes[kFirstEGPUParameter] = (CGLPixelFormatAttribute)NULL;
72 CGLChoosePixelFormat(attributes, &pixFormat, &npix);
73 }
74 if (nullptr == pixFormat) {
75 SkDebugf("CGLChoosePixelFormat failed.");
76 return;
77 }
78
79 CGLCreateContext(pixFormat, shareContext ? shareContext->fContext : nullptr, &fContext);
80 CGLReleasePixelFormat(pixFormat);
81
82 if (nullptr == fContext) {
83 SkDebugf("CGLCreateContext failed.");
84 return;
85 }
86
87 SkScopeExit restorer(context_restorer());
88 CGLSetCurrentContext(fContext);
89
90 auto gl = GrGLInterfaces::MakeMac();
91 if (!gl) {
92 SkDebugf("Context could not create GL interface.\n");
93 this->destroyGLContext();
94 return;
95 }
96 if (!gl->validate()) {
97 SkDebugf("Context could not validate GL interface.\n");
98 this->destroyGLContext();
99 return;
100 }
101
102 fGLLibrary = dlopen(
103 "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
104 RTLD_LAZY);
105
106 this->init(std::move(gl));
107 }
108
109 MacGLTestContext::~MacGLTestContext() {
110 this->teardown();
111 this->destroyGLContext();
112 }
113
114 void MacGLTestContext::destroyGLContext() {
115 if (fContext) {
116 if (CGLGetCurrentContext() == fContext) {
117 // This will ensure that the context is immediately deleted.
118 CGLSetCurrentContext(nullptr);
119 }
120 CGLReleaseContext(fContext);
121 fContext = nullptr;
122 }
123 if (nullptr != fGLLibrary) {
124 dlclose(fGLLibrary);
125 }
126 }
127
128 void MacGLTestContext::onPlatformMakeNotCurrent() const {
129 CGLSetCurrentContext(nullptr);
130 }
131
132 void MacGLTestContext::onPlatformMakeCurrent() const {
133 CGLSetCurrentContext(fContext);
134 }
135
136 std::function<void()> MacGLTestContext::onPlatformGetAutoContextRestore() const {
137 if (CGLGetCurrentContext() == fContext) {
138 return nullptr;
139 }
140 return context_restorer();
141 }
142
143 GrGLFuncPtr MacGLTestContext::onPlatformGetProcAddress(const char* procName) const {
144 void* handle = (nullptr == fGLLibrary) ? RTLD_DEFAULT : fGLLibrary;
145 return reinterpret_cast<GrGLFuncPtr>(dlsym(handle, procName));
146 }
147
148 } // anonymous namespace
149
150 #pragma clang diagnostic pop
151
152 namespace sk_gpu_test {
153 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
154 GLTestContext* shareContext) {
155 if (kGLES_GrGLStandard == forcedGpuAPI) {
156 return nullptr;
157 }
158 MacGLTestContext* macShareContext = reinterpret_cast<MacGLTestContext*>(shareContext);
159 MacGLTestContext* ctx = new MacGLTestContext(macShareContext);
160 if (!ctx->isValid()) {
161 delete ctx;
162 return nullptr;
163 }
164 return ctx;
165 }
166 } // namespace sk_gpu_test