gfx/skia/trunk/src/gpu/gl/mac/SkNativeGLContext_mac.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/gpu/gl/mac/SkNativeGLContext_mac.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,81 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include "gl/SkNativeGLContext.h"
    1.12 +#include "AvailabilityMacros.h"
    1.13 +
    1.14 +SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
    1.15 +    fOldCGLContext = CGLGetCurrentContext();
    1.16 +}
    1.17 +
    1.18 +SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
    1.19 +    CGLSetCurrentContext(fOldCGLContext);
    1.20 +}
    1.21 +
    1.22 +///////////////////////////////////////////////////////////////////////////////
    1.23 +
    1.24 +SkNativeGLContext::SkNativeGLContext()
    1.25 +    : fContext(NULL) {
    1.26 +}
    1.27 +
    1.28 +SkNativeGLContext::~SkNativeGLContext() {
    1.29 +    this->destroyGLContext();
    1.30 +}
    1.31 +
    1.32 +void SkNativeGLContext::destroyGLContext() {
    1.33 +    if (NULL != fContext) {
    1.34 +        CGLReleaseContext(fContext);
    1.35 +    }
    1.36 +}
    1.37 +
    1.38 +const GrGLInterface* SkNativeGLContext::createGLContext() {
    1.39 +    SkASSERT(NULL == fContext);
    1.40 +
    1.41 +    CGLPixelFormatAttribute attributes[] = {
    1.42 +#if MAC_OS_X_VERSION_10_7
    1.43 +        kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
    1.44 +#endif
    1.45 +        kCGLPFADoubleBuffer,
    1.46 +        (CGLPixelFormatAttribute)0
    1.47 +    };
    1.48 +    CGLPixelFormatObj pixFormat;
    1.49 +    GLint npix;
    1.50 +
    1.51 +    CGLChoosePixelFormat(attributes, &pixFormat, &npix);
    1.52 +
    1.53 +    if (NULL == pixFormat) {
    1.54 +        SkDebugf("CGLChoosePixelFormat failed.");
    1.55 +        return NULL;
    1.56 +    }
    1.57 +
    1.58 +    CGLCreateContext(pixFormat, NULL, &fContext);
    1.59 +    CGLReleasePixelFormat(pixFormat);
    1.60 +
    1.61 +    if (NULL == fContext) {
    1.62 +        SkDebugf("CGLCreateContext failed.");
    1.63 +        return NULL;
    1.64 +    }
    1.65 +
    1.66 +    CGLSetCurrentContext(fContext);
    1.67 +
    1.68 +    const GrGLInterface* interface = GrGLCreateNativeInterface();
    1.69 +    if (NULL == interface) {
    1.70 +        SkDebugf("Context could not create GL interface.\n");
    1.71 +        this->destroyGLContext();
    1.72 +        return NULL;
    1.73 +    }
    1.74 +
    1.75 +    return interface;
    1.76 +}
    1.77 +
    1.78 +void SkNativeGLContext::makeCurrent() const {
    1.79 +    CGLSetCurrentContext(fContext);
    1.80 +}
    1.81 +
    1.82 +void SkNativeGLContext::swapBuffers() const {
    1.83 +    CGLFlushDrawable(fContext);
    1.84 +}

mercurial