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 "SkTypes.h"
9
10 #include "gl/GLTestContext.h"
11 #include "AvailabilityMacros.h"
12
13 #include <OpenGL/OpenGL.h>
14 #include <dlfcn.h>
15
16 namespace {
17 class MacGLTestContext : public sk_gpu_test::GLTestContext {
18 public:
19 MacGLTestContext(MacGLTestContext* shareContext);
20 ~MacGLTestContext() override;
21
22 private:
23 void destroyGLContext();
24
25 void onPlatformMakeCurrent() const override;
26 void onPlatformSwapBuffers() const override;
27 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override;
28
29 CGLContextObj fContext;
30 void* fGLLibrary;
31 };
32
MacGLTestContext(MacGLTestContext * shareContext)33 MacGLTestContext::MacGLTestContext(MacGLTestContext* shareContext)
34 : fContext(nullptr)
35 , fGLLibrary(RTLD_DEFAULT) {
36 CGLPixelFormatAttribute attributes[] = {
37 #if MAC_OS_X_VERSION_10_7
38 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
39 #endif
40 kCGLPFADoubleBuffer,
41 (CGLPixelFormatAttribute)0
42 };
43 CGLPixelFormatObj pixFormat;
44 GLint npix;
45
46 CGLChoosePixelFormat(attributes, &pixFormat, &npix);
47
48 if (nullptr == pixFormat) {
49 SkDebugf("CGLChoosePixelFormat failed.");
50 return;
51 }
52
53 CGLCreateContext(pixFormat, shareContext ? shareContext->fContext : nullptr, &fContext);
54 CGLReleasePixelFormat(pixFormat);
55
56 if (nullptr == fContext) {
57 SkDebugf("CGLCreateContext failed.");
58 return;
59 }
60
61 CGLSetCurrentContext(fContext);
62
63 sk_sp<const GrGLInterface> gl(GrGLCreateNativeInterface());
64 if (nullptr == gl.get()) {
65 SkDebugf("Context could not create GL interface.\n");
66 this->destroyGLContext();
67 return;
68 }
69 if (!gl->validate()) {
70 SkDebugf("Context could not validate GL interface.\n");
71 this->destroyGLContext();
72 return;
73 }
74
75 fGLLibrary = dlopen(
76 "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib",
77 RTLD_LAZY);
78
79 this->init(gl.release());
80 }
81
~MacGLTestContext()82 MacGLTestContext::~MacGLTestContext() {
83 this->teardown();
84 this->destroyGLContext();
85 }
86
destroyGLContext()87 void MacGLTestContext::destroyGLContext() {
88 if (fContext) {
89 CGLReleaseContext(fContext);
90 fContext = nullptr;
91 }
92 if (RTLD_DEFAULT != fGLLibrary) {
93 dlclose(fGLLibrary);
94 }
95 }
96
onPlatformMakeCurrent() const97 void MacGLTestContext::onPlatformMakeCurrent() const {
98 CGLSetCurrentContext(fContext);
99 }
100
onPlatformSwapBuffers() const101 void MacGLTestContext::onPlatformSwapBuffers() const {
102 CGLFlushDrawable(fContext);
103 }
104
onPlatformGetProcAddress(const char * procName) const105 GrGLFuncPtr MacGLTestContext::onPlatformGetProcAddress(const char* procName) const {
106 return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName));
107 }
108
109 } // anonymous namespace
110
111 namespace sk_gpu_test {
CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,GLTestContext * shareContext)112 GLTestContext* CreatePlatformGLTestContext(GrGLStandard forcedGpuAPI,
113 GLTestContext* shareContext) {
114 if (kGLES_GrGLStandard == forcedGpuAPI) {
115 return nullptr;
116 }
117 MacGLTestContext* macShareContext = reinterpret_cast<MacGLTestContext*>(shareContext);
118 MacGLTestContext* ctx = new MacGLTestContext(macShareContext);
119 if (!ctx->isValid()) {
120 delete ctx;
121 return nullptr;
122 }
123 return ctx;
124 }
125 } // namespace sk_gpu_test
126