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