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 "gl/SkNativeGLContext.h" 9 #include "AvailabilityMacros.h" 10 11 SkNativeGLContext::AutoContextRestore::AutoContextRestore() { 12 fOldCGLContext = CGLGetCurrentContext(); 13 } 14 15 SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { 16 CGLSetCurrentContext(fOldCGLContext); 17 } 18 19 /////////////////////////////////////////////////////////////////////////////// 20 21 SkNativeGLContext::SkNativeGLContext() 22 : fContext(NULL) { 23 } 24 25 SkNativeGLContext::~SkNativeGLContext() { 26 this->destroyGLContext(); 27 } 28 29 void SkNativeGLContext::destroyGLContext() { 30 if (fContext) { 31 CGLReleaseContext(fContext); 32 } 33 } 34 35 const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { 36 SkASSERT(NULL == fContext); 37 if (kGLES_GrGLStandard == forcedGpuAPI) { 38 return NULL; 39 } 40 41 CGLPixelFormatAttribute attributes[] = { 42 #if MAC_OS_X_VERSION_10_7 43 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, 44 #endif 45 kCGLPFADoubleBuffer, 46 (CGLPixelFormatAttribute)0 47 }; 48 CGLPixelFormatObj pixFormat; 49 GLint npix; 50 51 CGLChoosePixelFormat(attributes, &pixFormat, &npix); 52 53 if (NULL == pixFormat) { 54 SkDebugf("CGLChoosePixelFormat failed."); 55 return NULL; 56 } 57 58 CGLCreateContext(pixFormat, NULL, &fContext); 59 CGLReleasePixelFormat(pixFormat); 60 61 if (NULL == fContext) { 62 SkDebugf("CGLCreateContext failed."); 63 return NULL; 64 } 65 66 CGLSetCurrentContext(fContext); 67 68 const GrGLInterface* interface = GrGLCreateNativeInterface(); 69 if (NULL == interface) { 70 SkDebugf("Context could not create GL interface.\n"); 71 this->destroyGLContext(); 72 return NULL; 73 } 74 75 return interface; 76 } 77 78 void SkNativeGLContext::makeCurrent() const { 79 CGLSetCurrentContext(fContext); 80 } 81 82 void SkNativeGLContext::swapBuffers() const { 83 CGLFlushDrawable(fContext); 84 } 85