| 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 | 12 |
| 13 namespace { | 13 namespace { |
| 14 class MacGLContext : public SkGLContext { | 14 class MacGLContext : public SkGLContext { |
| 15 public: | 15 public: |
| 16 MacGLContext(); | 16 MacGLContext(); |
| 17 | 17 virtual ~MacGLContext() SK_OVERRIDE; |
| 18 virtual ~MacGLContext(); | |
| 19 | |
| 20 virtual void makeCurrent() const SK_OVERRIDE; | 18 virtual void makeCurrent() const SK_OVERRIDE; |
| 21 virtual void swapBuffers() const SK_OVERRIDE; | 19 virtual void swapBuffers() const SK_OVERRIDE; |
| 22 protected: | |
| 23 virtual const GrGLInterface* createGLContext(GrGLStandard forcedGpuAPI) SK_O
VERRIDE; | |
| 24 virtual void destroyGLContext() SK_OVERRIDE; | |
| 25 | 20 |
| 26 private: | 21 private: |
| 22 void destroyGLContext(); |
| 23 |
| 27 CGLContextObj fContext; | 24 CGLContextObj fContext; |
| 28 }; | 25 }; |
| 29 | 26 |
| 30 MacGLContext::MacGLContext() | 27 MacGLContext::MacGLContext() |
| 31 : fContext(NULL) { | 28 : fContext(NULL) { |
| 32 } | |
| 33 | |
| 34 MacGLContext::~MacGLContext() { | |
| 35 this->destroyGLContext(); | |
| 36 } | |
| 37 | |
| 38 void MacGLContext::destroyGLContext() { | |
| 39 if (fContext) { | |
| 40 CGLReleaseContext(fContext); | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 const GrGLInterface* MacGLContext::createGLContext(GrGLStandard forcedGpuAPI) { | |
| 45 SkASSERT(NULL == fContext); | |
| 46 if (kGLES_GrGLStandard == forcedGpuAPI) { | |
| 47 return NULL; | |
| 48 } | |
| 49 | |
| 50 CGLPixelFormatAttribute attributes[] = { | 29 CGLPixelFormatAttribute attributes[] = { |
| 51 #if MAC_OS_X_VERSION_10_7 | 30 #if MAC_OS_X_VERSION_10_7 |
| 52 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core
, | 31 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core
, |
| 53 #endif | 32 #endif |
| 54 kCGLPFADoubleBuffer, | 33 kCGLPFADoubleBuffer, |
| 55 (CGLPixelFormatAttribute)0 | 34 (CGLPixelFormatAttribute)0 |
| 56 }; | 35 }; |
| 57 CGLPixelFormatObj pixFormat; | 36 CGLPixelFormatObj pixFormat; |
| 58 GLint npix; | 37 GLint npix; |
| 59 | 38 |
| 60 CGLChoosePixelFormat(attributes, &pixFormat, &npix); | 39 CGLChoosePixelFormat(attributes, &pixFormat, &npix); |
| 61 | 40 |
| 62 if (NULL == pixFormat) { | 41 if (NULL == pixFormat) { |
| 63 SkDebugf("CGLChoosePixelFormat failed."); | 42 SkDebugf("CGLChoosePixelFormat failed."); |
| 64 return NULL; | 43 return; |
| 65 } | 44 } |
| 66 | 45 |
| 67 CGLCreateContext(pixFormat, NULL, &fContext); | 46 CGLCreateContext(pixFormat, NULL, &fContext); |
| 68 CGLReleasePixelFormat(pixFormat); | 47 CGLReleasePixelFormat(pixFormat); |
| 69 | 48 |
| 70 if (NULL == fContext) { | 49 if (NULL == fContext) { |
| 71 SkDebugf("CGLCreateContext failed."); | 50 SkDebugf("CGLCreateContext failed."); |
| 72 return NULL; | 51 return; |
| 73 } | 52 } |
| 74 | 53 |
| 75 CGLSetCurrentContext(fContext); | 54 CGLSetCurrentContext(fContext); |
| 76 | 55 |
| 77 const GrGLInterface* interface = GrGLCreateNativeInterface(); | 56 fGL.reset(GrGLCreateNativeInterface()); |
| 78 if (NULL == interface) { | 57 if (NULL == fGL.get()) { |
| 79 SkDebugf("Context could not create GL interface.\n"); | 58 SkDebugf("Context could not create GL interface.\n"); |
| 80 this->destroyGLContext(); | 59 this->destroyGLContext(); |
| 81 return NULL; | 60 return; |
| 82 } | 61 } |
| 62 if (!fGL->validate()) { |
| 63 SkDebugf("Context could not validate GL interface.\n"); |
| 64 this->destroyGLContext(); |
| 65 return; |
| 66 } |
| 67 } |
| 83 | 68 |
| 84 return interface; | 69 MacGLContext::~MacGLContext() { |
| 70 this->destroyGLContext(); |
| 71 } |
| 72 |
| 73 void MacGLContext::destroyGLContext() { |
| 74 fGL.reset(NULL); |
| 75 if (fContext) { |
| 76 CGLReleaseContext(fContext); |
| 77 fContext = NULL; |
| 78 } |
| 85 } | 79 } |
| 86 | 80 |
| 87 void MacGLContext::makeCurrent() const { | 81 void MacGLContext::makeCurrent() const { |
| 88 CGLSetCurrentContext(fContext); | 82 CGLSetCurrentContext(fContext); |
| 89 } | 83 } |
| 90 | 84 |
| 91 void MacGLContext::swapBuffers() const { | 85 void MacGLContext::swapBuffers() const { |
| 92 CGLFlushDrawable(fContext); | 86 CGLFlushDrawable(fContext); |
| 93 } | 87 } |
| 94 | 88 |
| 95 } // anonymous namespace | 89 } // anonymous namespace |
| 96 | 90 |
| 97 SkGLContext* SkCreatePlatformGLContext() { | 91 SkGLContext* SkCreatePlatformGLContext(GrGLStandard forcedGpuAPI) { |
| 98 return SkNEW(MacGLContext); | 92 if (kGLES_GrGLStandard == forcedGpuAPI) { |
| 93 return NULL; |
| 94 } |
| 95 MacGLContext* ctx = SkNEW(MacGLContext); |
| 96 if (!ctx->isValid()) { |
| 97 SkDELETE(ctx); |
| 98 return NULL; |
| 99 } |
| 100 return ctx; |
| 99 } | 101 } |
| OLD | NEW |