michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "gl/SkNativeGLContext.h" michael@0: #include "AvailabilityMacros.h" michael@0: michael@0: SkNativeGLContext::AutoContextRestore::AutoContextRestore() { michael@0: fOldCGLContext = CGLGetCurrentContext(); michael@0: } michael@0: michael@0: SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { michael@0: CGLSetCurrentContext(fOldCGLContext); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkNativeGLContext::SkNativeGLContext() michael@0: : fContext(NULL) { michael@0: } michael@0: michael@0: SkNativeGLContext::~SkNativeGLContext() { michael@0: this->destroyGLContext(); michael@0: } michael@0: michael@0: void SkNativeGLContext::destroyGLContext() { michael@0: if (NULL != fContext) { michael@0: CGLReleaseContext(fContext); michael@0: } michael@0: } michael@0: michael@0: const GrGLInterface* SkNativeGLContext::createGLContext() { michael@0: SkASSERT(NULL == fContext); michael@0: michael@0: CGLPixelFormatAttribute attributes[] = { michael@0: #if MAC_OS_X_VERSION_10_7 michael@0: kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, michael@0: #endif michael@0: kCGLPFADoubleBuffer, michael@0: (CGLPixelFormatAttribute)0 michael@0: }; michael@0: CGLPixelFormatObj pixFormat; michael@0: GLint npix; michael@0: michael@0: CGLChoosePixelFormat(attributes, &pixFormat, &npix); michael@0: michael@0: if (NULL == pixFormat) { michael@0: SkDebugf("CGLChoosePixelFormat failed."); michael@0: return NULL; michael@0: } michael@0: michael@0: CGLCreateContext(pixFormat, NULL, &fContext); michael@0: CGLReleasePixelFormat(pixFormat); michael@0: michael@0: if (NULL == fContext) { michael@0: SkDebugf("CGLCreateContext failed."); michael@0: return NULL; michael@0: } michael@0: michael@0: CGLSetCurrentContext(fContext); michael@0: michael@0: const GrGLInterface* interface = GrGLCreateNativeInterface(); michael@0: if (NULL == interface) { michael@0: SkDebugf("Context could not create GL interface.\n"); michael@0: this->destroyGLContext(); michael@0: return NULL; michael@0: } michael@0: michael@0: return interface; michael@0: } michael@0: michael@0: void SkNativeGLContext::makeCurrent() const { michael@0: CGLSetCurrentContext(fContext); michael@0: } michael@0: michael@0: void SkNativeGLContext::swapBuffers() const { michael@0: CGLFlushDrawable(fContext); michael@0: }