OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2012 Google Inc. | 3 * Copyright 2012 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 #include "gl/SkGLContext.h" | 9 #include "gl/SkGLContext.h" |
10 #import <OpenGLES/EAGL.h> | 10 #import <OpenGLES/EAGL.h> |
| 11 #include <dlfcn.h> |
11 | 12 |
12 #define EAGLCTX ((EAGLContext*)(fEAGLContext)) | 13 #define EAGLCTX ((EAGLContext*)(fEAGLContext)) |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 class IOSGLContext : public SkGLContext { | 17 class IOSGLContext : public SkGLContext { |
17 public: | 18 public: |
18 IOSGLContext(); | 19 IOSGLContext(); |
19 ~IOSGLContext() override; | 20 ~IOSGLContext() override; |
20 void makeCurrent() const override; | |
21 void swapBuffers() const override; | |
22 | 21 |
23 private: | 22 private: |
24 void destroyGLContext(); | 23 void destroyGLContext(); |
25 | 24 |
| 25 void onPlatformMakeCurrent() const override; |
| 26 void onPlatformSwapBuffers() const override; |
| 27 GrGLFuncPtr onPlatformGetProcAddress(const char*) const override; |
| 28 |
26 void* fEAGLContext; | 29 void* fEAGLContext; |
| 30 void* fGLLibrary; |
27 }; | 31 }; |
28 | 32 |
29 IOSGLContext::IOSGLContext() | 33 IOSGLContext::IOSGLContext() |
30 : fEAGLContext(NULL) { | 34 : fEAGLContext(NULL) |
| 35 , fGLLibrary(RTLD_DEFAULT) { |
31 | 36 |
32 fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; | 37 fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2]; |
33 [EAGLContext setCurrentContext:EAGLCTX]; | 38 [EAGLContext setCurrentContext:EAGLCTX]; |
34 | 39 |
35 fGL.reset(GrGLCreateNativeInterface()); | 40 SkAutoTUnref<const GrGLInterface> gl(GrGLCreateNativeInterface()); |
36 if (NULL == fGL.get()) { | 41 if (NULL == gl.get()) { |
37 SkDebugf("Failed to create gl interface"); | 42 SkDebugf("Failed to create gl interface"); |
38 this->destroyGLContext(); | 43 this->destroyGLContext(); |
39 return; | 44 return; |
40 } | 45 } |
41 if (!fGL->validate()) { | 46 if (!gl->validate()) { |
42 SkDebugf("Failed to validate gl interface"); | 47 SkDebugf("Failed to validate gl interface"); |
43 this->destroyGLContext(); | 48 this->destroyGLContext(); |
44 return; | 49 return; |
45 } | 50 } |
| 51 |
| 52 fGLLibrary = dlopen( |
| 53 "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.
dylib", |
| 54 RTLD_LAZY); |
| 55 |
| 56 this->init(gl.detach()); |
46 } | 57 } |
47 | 58 |
48 IOSGLContext::~IOSGLContext() { | 59 IOSGLContext::~IOSGLContext() { |
| 60 this->teardown(); |
49 this->destroyGLContext(); | 61 this->destroyGLContext(); |
50 } | 62 } |
51 | 63 |
52 void IOSGLContext::destroyGLContext() { | 64 void IOSGLContext::destroyGLContext() { |
53 fGL.reset(NULL); | |
54 if (fEAGLContext) { | 65 if (fEAGLContext) { |
55 if ([EAGLContext currentContext] == EAGLCTX) { | 66 if ([EAGLContext currentContext] == EAGLCTX) { |
56 [EAGLContext setCurrentContext:nil]; | 67 [EAGLContext setCurrentContext:nil]; |
57 } | 68 } |
58 [EAGLCTX release]; | 69 [EAGLCTX release]; |
59 fEAGLContext = NULL; | 70 fEAGLContext = NULL; |
60 } | 71 } |
| 72 if (RTLD_DEFAULT != fGLLibrary) { |
| 73 dlclose(fGLLibrary); |
| 74 } |
61 } | 75 } |
62 | 76 |
63 | 77 |
64 void IOSGLContext::makeCurrent() const { | 78 void IOSGLContext::onPlatformMakeCurrent() const { |
65 if (![EAGLContext setCurrentContext:EAGLCTX]) { | 79 if (![EAGLContext setCurrentContext:EAGLCTX]) { |
66 SkDebugf("Could not set the context.\n"); | 80 SkDebugf("Could not set the context.\n"); |
67 } | 81 } |
68 } | 82 } |
69 | 83 |
70 void IOSGLContext::swapBuffers() const { } | 84 void IOSGLContext::onPlatformSwapBuffers() const { } |
| 85 |
| 86 GrGLFuncPtr IOSGLContext::onPlatformGetProcAddress(const char* procName) const { |
| 87 return reinterpret_cast<GrGLFuncPtr>(dlsym(fGLLibrary, procName)); |
| 88 } |
71 | 89 |
72 } // anonymous namespace | 90 } // anonymous namespace |
73 | 91 |
74 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { | 92 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { |
75 if (kGL_GrGLStandard == forcedGpuAPI) { | 93 if (kGL_GrGLStandard == forcedGpuAPI) { |
76 return NULL; | 94 return NULL; |
77 } | 95 } |
78 IOSGLContext* ctx = SkNEW(IOSGLContext); | 96 IOSGLContext* ctx = SkNEW(IOSGLContext); |
79 if (!ctx->isValid()) { | 97 if (!ctx->isValid()) { |
80 SkDELETE(ctx); | 98 SkDELETE(ctx); |
81 return NULL; | 99 return NULL; |
82 } | 100 } |
83 return ctx; | 101 return ctx; |
84 } | 102 } |
85 | 103 |
OLD | NEW |