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 | */ |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 8 | #include "gl/SkNativeGLContext.h" |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 9 | #include "AvailabilityMacros.h" |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 10 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 11 | SkNativeGLContext::AutoContextRestore::AutoContextRestore() { |
| 12 | fOldCGLContext = CGLGetCurrentContext(); |
| 13 | } |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 14 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 15 | SkNativeGLContext::AutoContextRestore::~AutoContextRestore() { |
| 16 | CGLSetCurrentContext(fOldCGLContext); |
| 17 | } |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 18 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 19 | /////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 57f5d98 | 2011-10-24 21:17:53 +0000 | [diff] [blame] | 20 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 21 | SkNativeGLContext::SkNativeGLContext() |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 22 | : fContext(NULL) { |
| 23 | } |
| 24 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 25 | SkNativeGLContext::~SkNativeGLContext() { |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 26 | this->destroyGLContext(); |
| 27 | } |
| 28 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 29 | void SkNativeGLContext::destroyGLContext() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 30 | if (fContext) { |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 31 | CGLReleaseContext(fContext); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 35 | const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) { |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 36 | SkASSERT(NULL == fContext); |
kkinnunen | 80549fc | 2014-06-30 06:36:31 -0700 | [diff] [blame] | 37 | if (kGLES_GrGLStandard == forcedGpuAPI) { |
| 38 | return NULL; |
| 39 | } |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 40 | |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 41 | CGLPixelFormatAttribute attributes[] = { |
bsalomon@google.com | 6918d48 | 2013-03-07 19:09:11 +0000 | [diff] [blame] | 42 | #if MAC_OS_X_VERSION_10_7 |
bsalomon@google.com | 1744f97 | 2013-02-26 21:46:32 +0000 | [diff] [blame] | 43 | kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 44 | #endif |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 45 | kCGLPFADoubleBuffer, |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 46 | (CGLPixelFormatAttribute)0 |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 47 | }; |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 48 | CGLPixelFormatObj pixFormat; |
| 49 | GLint npix; |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 50 | |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 51 | CGLChoosePixelFormat(attributes, &pixFormat, &npix); |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 52 | |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 53 | if (NULL == pixFormat) { |
| 54 | SkDebugf("CGLChoosePixelFormat failed."); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 57 | |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 58 | CGLCreateContext(pixFormat, NULL, &fContext); |
| 59 | CGLReleasePixelFormat(pixFormat); |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 60 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 61 | if (NULL == fContext) { |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 62 | SkDebugf("CGLCreateContext failed."); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 63 | return NULL; |
| 64 | } |
skia.committer@gmail.com | 044679e | 2013-02-15 07:16:57 +0000 | [diff] [blame] | 65 | |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 66 | CGLSetCurrentContext(fContext); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 67 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 68 | const GrGLInterface* interface = GrGLCreateNativeInterface(); |
| 69 | if (NULL == interface) { |
| 70 | SkDebugf("Context could not create GL interface.\n"); |
| 71 | this->destroyGLContext(); |
| 72 | return NULL; |
| 73 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 74 | |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 75 | return interface; |
| 76 | } |
| 77 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 78 | void SkNativeGLContext::makeCurrent() const { |
bsalomon@google.com | 682e1f9 | 2013-02-14 15:10:59 +0000 | [diff] [blame] | 79 | CGLSetCurrentContext(fContext); |
bsalomon@google.com | 373a663 | 2011-10-19 20:43:20 +0000 | [diff] [blame] | 80 | } |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 81 | |
bsalomon | 1080596 | 2014-10-08 04:45:09 -0700 | [diff] [blame^] | 82 | void SkNativeGLContext::swapBuffers() const { |
djsollen@google.com | c9542ca | 2013-10-09 18:25:38 +0000 | [diff] [blame] | 83 | CGLFlushDrawable(fContext); |
| 84 | } |