blob: f04229f16e752f061c707ec069f95240028b610b [file] [log] [blame]
bsalomon@google.com2b64f842012-10-02 15:25:12 +00001
2/*
3 * Copyright 2012 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
9#include "gl/SkNativeGLContext.h"
10#import <OpenGLES/EAGL.h>
11
12#define EAGLCTX ((EAGLContext*)(fEAGLContext))
13
14SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
15 fEAGLContext = [EAGLContext currentContext];
16 if (EAGLCTX) {
17 [EAGLCTX retain];
18 }
19}
20
21SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
22 if (EAGLCTX) {
23 [EAGLContext setCurrentContext:EAGLCTX];
24 [EAGLCTX release];
25 }
26}
27
28///////////////////////////////////////////////////////////////////////////////
29
30SkNativeGLContext::SkNativeGLContext()
31 : fEAGLContext(NULL) {
32}
33
34SkNativeGLContext::~SkNativeGLContext() {
35 this->destroyGLContext();
36}
37
38void SkNativeGLContext::destroyGLContext() {
bsalomona721c812014-08-26 11:35:23 -070039 if (NULL != fEAGLContext) {
40 if ([EAGLContext currentContext] == EAGLCTX) {
41 [EAGLContext setCurrentContext:nil];
42 }
43 [EAGLCTX release];
44 fEAGLContext = NULL;
bsalomon@google.com2b64f842012-10-02 15:25:12 +000045 }
bsalomon@google.com2b64f842012-10-02 15:25:12 +000046}
47
kkinnunen80549fc2014-06-30 06:36:31 -070048const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
49 if (kGL_GrGLStandard == forcedGpuAPI) {
50 return NULL;
51 }
52
bsalomon@google.com2b64f842012-10-02 15:25:12 +000053 fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
54 [EAGLContext setCurrentContext:EAGLCTX];
55
56 const GrGLInterface* interface = GrGLCreateNativeInterface();
57 if (!interface) {
58 SkDebugf("Failed to create gl interface");
59 this->destroyGLContext();
60 return NULL;
61 }
62 return interface;
63}
64
65void SkNativeGLContext::makeCurrent() const {
66 if (![EAGLContext setCurrentContext:EAGLCTX]) {
67 SkDebugf("Could not set the context.\n");
68 }
69}
djsollen@google.comc9542ca2013-10-09 18:25:38 +000070
kkinnunen80549fc2014-06-30 06:36:31 -070071void SkNativeGLContext::swapBuffers() const { }