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