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
10SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
11    fOldAGLContext = aglGetCurrentContext();
12}
13
14SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
15    aglSetCurrentContext(fOldAGLContext);
16}
17
18///////////////////////////////////////////////////////////////////////////////
19
20SkNativeGLContext::SkNativeGLContext()
21    : fContext(NULL) {
22}
23
24SkNativeGLContext::~SkNativeGLContext() {
25    this->destroyGLContext();
26}
27
28void SkNativeGLContext::destroyGLContext() {
29    if (fContext) {
30        aglDestroyContext(fContext);
31    }
32}
33
34const GrGLInterface* SkNativeGLContext::createGLContext() {
35    GLint major, minor;
36    AGLContext ctx;
37
38    aglGetVersion(&major, &minor);
39    //SkDebugf("---- agl version %d %d\n", major, minor);
40
41    const GLint pixelAttrs[] = {
42        AGL_RGBA,
43        AGL_ACCELERATED,
44        AGL_NONE
45    };
46    AGLPixelFormat format = aglChoosePixelFormat(NULL, 0, pixelAttrs);
47    if (NULL == format) {
48        SkDebugf("Format could not be found.\n");
49        this->destroyGLContext();
50        return NULL;
51    }
52    fContext = aglCreateContext(format, NULL);
53    if (NULL == fContext) {
54        SkDebugf("Context could not be created.\n");
55        this->destroyGLContext();
56        return NULL;
57    }
58    aglDestroyPixelFormat(format);
59
60    aglSetCurrentContext(fContext);
61
62    const GrGLInterface* interface = GrGLCreateNativeInterface();
63    if (NULL == interface) {
64        SkDebugf("Context could not create GL interface.\n");
65        this->destroyGLContext();
66        return NULL;
67    }
68
69    return interface;
70}
71
72void SkNativeGLContext::makeCurrent() const {
73    aglSetCurrentContext(fContext);
74}
75