blob: f63471c16a38275606103ed92ebd7b80fad73670 [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 */
bsalomon10805962014-10-08 04:45:09 -07008#include "gl/SkNativeGLContext.h"
bsalomon@google.com6918d482013-03-07 19:09:11 +00009#include "AvailabilityMacros.h"
bsalomon@google.com373a6632011-10-19 20:43:20 +000010
bsalomon10805962014-10-08 04:45:09 -070011SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
12 fOldCGLContext = CGLGetCurrentContext();
13}
bsalomon@google.com57f5d982011-10-24 21:17:53 +000014
bsalomon10805962014-10-08 04:45:09 -070015SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
16 CGLSetCurrentContext(fOldCGLContext);
17}
bsalomon@google.com57f5d982011-10-24 21:17:53 +000018
bsalomon10805962014-10-08 04:45:09 -070019///////////////////////////////////////////////////////////////////////////////
bsalomon@google.com57f5d982011-10-24 21:17:53 +000020
bsalomon10805962014-10-08 04:45:09 -070021SkNativeGLContext::SkNativeGLContext()
bsalomon@google.com373a6632011-10-19 20:43:20 +000022 : fContext(NULL) {
23}
24
bsalomon10805962014-10-08 04:45:09 -070025SkNativeGLContext::~SkNativeGLContext() {
bsalomon@google.com373a6632011-10-19 20:43:20 +000026 this->destroyGLContext();
27}
28
bsalomon10805962014-10-08 04:45:09 -070029void SkNativeGLContext::destroyGLContext() {
bsalomon49f085d2014-09-05 13:34:00 -070030 if (fContext) {
bsalomon@google.com682e1f92013-02-14 15:10:59 +000031 CGLReleaseContext(fContext);
bsalomon@google.com373a6632011-10-19 20:43:20 +000032 }
33}
34
bsalomon10805962014-10-08 04:45:09 -070035const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
bsalomon@google.com682e1f92013-02-14 15:10:59 +000036 SkASSERT(NULL == fContext);
kkinnunen80549fc2014-06-30 06:36:31 -070037 if (kGLES_GrGLStandard == forcedGpuAPI) {
38 return NULL;
39 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000040
bsalomon@google.com682e1f92013-02-14 15:10:59 +000041 CGLPixelFormatAttribute attributes[] = {
bsalomon@google.com6918d482013-03-07 19:09:11 +000042#if MAC_OS_X_VERSION_10_7
bsalomon@google.com1744f972013-02-26 21:46:32 +000043 kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core,
bsalomon@google.com682e1f92013-02-14 15:10:59 +000044#endif
djsollen@google.comc9542ca2013-10-09 18:25:38 +000045 kCGLPFADoubleBuffer,
bsalomon@google.com682e1f92013-02-14 15:10:59 +000046 (CGLPixelFormatAttribute)0
bsalomon@google.com373a6632011-10-19 20:43:20 +000047 };
bsalomon@google.com682e1f92013-02-14 15:10:59 +000048 CGLPixelFormatObj pixFormat;
49 GLint npix;
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000050
bsalomon@google.com682e1f92013-02-14 15:10:59 +000051 CGLChoosePixelFormat(attributes, &pixFormat, &npix);
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000052
bsalomon@google.com682e1f92013-02-14 15:10:59 +000053 if (NULL == pixFormat) {
54 SkDebugf("CGLChoosePixelFormat failed.");
bsalomon@google.com373a6632011-10-19 20:43:20 +000055 return NULL;
56 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000057
bsalomon@google.com682e1f92013-02-14 15:10:59 +000058 CGLCreateContext(pixFormat, NULL, &fContext);
59 CGLReleasePixelFormat(pixFormat);
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000060
bsalomon@google.com373a6632011-10-19 20:43:20 +000061 if (NULL == fContext) {
bsalomon@google.com682e1f92013-02-14 15:10:59 +000062 SkDebugf("CGLCreateContext failed.");
bsalomon@google.com373a6632011-10-19 20:43:20 +000063 return NULL;
64 }
skia.committer@gmail.com044679e2013-02-15 07:16:57 +000065
bsalomon@google.com682e1f92013-02-14 15:10:59 +000066 CGLSetCurrentContext(fContext);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000067
bsalomon@google.com373a6632011-10-19 20:43:20 +000068 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.comfbfcd562012-08-23 18:09:54 +000074
bsalomon@google.com373a6632011-10-19 20:43:20 +000075 return interface;
76}
77
bsalomon10805962014-10-08 04:45:09 -070078void SkNativeGLContext::makeCurrent() const {
bsalomon@google.com682e1f92013-02-14 15:10:59 +000079 CGLSetCurrentContext(fContext);
bsalomon@google.com373a6632011-10-19 20:43:20 +000080}
djsollen@google.comc9542ca2013-10-09 18:25:38 +000081
bsalomon10805962014-10-08 04:45:09 -070082void SkNativeGLContext::swapBuffers() const {
djsollen@google.comc9542ca2013-10-09 18:25:38 +000083 CGLFlushDrawable(fContext);
84}