blob: ad68c40f57f75a1f587a7ead00ca7b5dda41e914 [file] [log] [blame]
bsalomon@google.com373a6632011-10-19 20:43:20 +00001
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 "SkNativeGLContext.h"
9
bsalomon@google.com57f5d982011-10-24 21:17:53 +000010SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
11 fOldAGLContext = aglGetCurrentContext();
12}
13
14SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
15 aglSetCurrentContext(fOldAGLContext);
16}
17
18///////////////////////////////////////////////////////////////////////////////
19
bsalomon@google.com373a6632011-10-19 20:43:20 +000020SkNativeGLContext::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}