bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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 | */ |
tomhudson@google.com | 6c8c34e | 2012-02-14 15:31:03 +0000 | [diff] [blame] | 8 | #include "gl/SkNativeGLContext.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 10 | SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
| 11 | fOldAGLContext = aglGetCurrentContext(); |
| 12 | } |
| 13 | |
| 14 | SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { |
| 15 | aglSetCurrentContext(fOldAGLContext); |
| 16 | } |
| 17 | |
| 18 | /////////////////////////////////////////////////////////////////////////////// |
| 19 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 20 | SkNativeGLContext::SkNativeGLContext() |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 21 | : fContext(NULL) { |
| 22 | } |
| 23 | |
| 24 | SkNativeGLContext::~SkNativeGLContext() { |
| 25 | this->destroyGLContext(); |
| 26 | } |
| 27 | |
| 28 | void SkNativeGLContext::destroyGLContext() { |
| 29 | if (fContext) { |
| 30 | aglDestroyContext(fContext); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | const GrGLInterface* SkNativeGLContext::createGLContext() { |
| 35 | GLint major, minor; |
caryclark@google.com | cf6285b | 2012-06-06 12:09:01 +0000 | [diff] [blame] | 36 | // AGLContext ctx; |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 61 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 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 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame^] | 68 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 69 | return interface; |
| 70 | } |
| 71 | |
| 72 | void SkNativeGLContext::makeCurrent() const { |
| 73 | aglSetCurrentContext(fContext); |
| 74 | } |