aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/gpu/gl/iOS/SkNativeGLContext_iOS.mm
blob: 1e11c0deb7be8afd32fdbf83314f6b9b6d4717c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

/*
 * Copyright 2012 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gl/SkNativeGLContext.h"
#import <OpenGLES/EAGL.h>

#define EAGLCTX ((EAGLContext*)(fEAGLContext))

SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
    fEAGLContext = [EAGLContext currentContext];
    if (EAGLCTX) {
        [EAGLCTX retain];
    }
}

SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
    if (EAGLCTX) {
        [EAGLContext setCurrentContext:EAGLCTX];
        [EAGLCTX release];
    }
}

///////////////////////////////////////////////////////////////////////////////

SkNativeGLContext::SkNativeGLContext()
    : fEAGLContext(NULL) {
}

SkNativeGLContext::~SkNativeGLContext() {
    this->destroyGLContext();
}

void SkNativeGLContext::destroyGLContext() {
    if ([EAGLContext currentContext] == EAGLCTX) {
        [EAGLContext setCurrentContext:nil];
    }
    [EAGLCTX release];
}

const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
    if (kGL_GrGLStandard == forcedGpuAPI) {
        return NULL;
    }

    fEAGLContext = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    [EAGLContext setCurrentContext:EAGLCTX];
    
    const GrGLInterface* interface = GrGLCreateNativeInterface();
    if (!interface) {
        SkDebugf("Failed to create gl interface");
        this->destroyGLContext();
        return NULL;
    }
    return interface;
}

void SkNativeGLContext::makeCurrent() const {
    if (![EAGLContext setCurrentContext:EAGLCTX]) {
        SkDebugf("Could not set the context.\n");
    }
}

void SkNativeGLContext::swapBuffers() const { }