Home | History | Annotate | Download | only in mac
      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 (NULL != fContext) {
     31         CGLReleaseContext(fContext);
     32     }
     33 }
     34 
     35 const GrGLInterface* SkNativeGLContext::createGLContext() {
     36     SkASSERT(NULL == fContext);
     37 
     38     CGLPixelFormatAttribute attributes[] = {
     39 #if MAC_OS_X_VERSION_10_7
     40         kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
     41 #endif
     42         (CGLPixelFormatAttribute)0
     43     };
     44     CGLPixelFormatObj pixFormat;
     45     GLint npix;
     46 
     47     CGLChoosePixelFormat(attributes, &pixFormat, &npix);
     48 
     49     if (NULL == pixFormat) {
     50         SkDebugf("CGLChoosePixelFormat failed.");
     51         return NULL;
     52     }
     53 
     54     CGLCreateContext(pixFormat, NULL, &fContext);
     55     CGLReleasePixelFormat(pixFormat);
     56 
     57     if (NULL == fContext) {
     58         SkDebugf("CGLCreateContext failed.");
     59         return NULL;
     60     }
     61 
     62     CGLSetCurrentContext(fContext);
     63 
     64     const GrGLInterface* interface = GrGLCreateNativeInterface();
     65     if (NULL == interface) {
     66         SkDebugf("Context could not create GL interface.\n");
     67         this->destroyGLContext();
     68         return NULL;
     69     }
     70 
     71     return interface;
     72 }
     73 
     74 void SkNativeGLContext::makeCurrent() const {
     75     CGLSetCurrentContext(fContext);
     76 }
     77