blob: f0cb2b6dd02b0a98ce1e6a97f5e4cf3a36d42b95 [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 */
tomhudson@google.com6c8c34e2012-02-14 15:31:03 +00008#include "gl/SkNativeGLContext.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +00009
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
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020SkNativeGLContext::SkNativeGLContext()
bsalomon@google.com373a6632011-10-19 20:43:20 +000021 : 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;
caryclark@google.comcf6285b2012-06-06 12:09:01 +000036 // AGLContext ctx;
bsalomon@google.com373a6632011-10-19 20:43:20 +000037
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);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000061
bsalomon@google.com373a6632011-10-19 20:43:20 +000062 const GrGLInterface* interface = GrGLCreateNativeInterface();
63 if (NULL == interface) {
64 SkDebugf("Context could not create GL interface.\n");
65 this->destroyGLContext();
66 return NULL;
67 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068
bsalomon@google.com373a6632011-10-19 20:43:20 +000069 return interface;
70}
71
72void SkNativeGLContext::makeCurrent() const {
73 aglSetCurrentContext(fContext);
74}