blob: fea27622a018ae0fa83d9166e8a0d9cfa9963a60 [file] [log] [blame]
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +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/SkANGLEGLContext.h"
10
11SkANGLEGLContext::AutoContextRestore::AutoContextRestore() {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000012 fOldEGLContext = eglGetCurrentContext();
13 fOldDisplay = eglGetCurrentDisplay();
14 fOldSurface = eglGetCurrentSurface(EGL_DRAW);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000015
16}
17
18SkANGLEGLContext::AutoContextRestore::~AutoContextRestore() {
19 if (NULL != fOldDisplay) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000020 eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000021 }
22}
23
24///////////////////////////////////////////////////////////////////////////////
25
26SkANGLEGLContext::SkANGLEGLContext()
27 : fContext(EGL_NO_CONTEXT)
28 , fDisplay(EGL_NO_DISPLAY)
29 , fSurface(EGL_NO_SURFACE) {
30}
31
32SkANGLEGLContext::~SkANGLEGLContext() {
33 this->destroyGLContext();
34}
35
36void SkANGLEGLContext::destroyGLContext() {
37 if (fDisplay) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000038 eglMakeCurrent(fDisplay, 0, 0, 0);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000039
40 if (fContext) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000041 eglDestroyContext(fDisplay, fContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000042 fContext = EGL_NO_CONTEXT;
43 }
44
45 if (fSurface) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000046 eglDestroySurface(fDisplay, fSurface);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000047 fSurface = EGL_NO_SURFACE;
48 }
49
50 //TODO should we close the display?
51 fDisplay = EGL_NO_DISPLAY;
52 }
53}
54
55const GrGLInterface* SkANGLEGLContext::createGLContext() {
56
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000057 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000058
59 EGLint majorVersion;
60 EGLint minorVersion;
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000061 eglInitialize(fDisplay, &majorVersion, &minorVersion);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000062
63 EGLint numConfigs;
64 static const EGLint configAttribs[] = {
65 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
66 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
67 EGL_RED_SIZE, 8,
68 EGL_GREEN_SIZE, 8,
69 EGL_BLUE_SIZE, 8,
70 EGL_ALPHA_SIZE, 8,
71 EGL_NONE
72 };
73
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000074 EGLConfig surfaceConfig;
75 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000076
77 static const EGLint contextAttribs[] = {
78 EGL_CONTEXT_CLIENT_VERSION, 2,
79 EGL_NONE
80 };
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000081 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000082
83
84 static const EGLint surfaceAttribs[] = {
85 EGL_WIDTH, 1,
86 EGL_HEIGHT, 1,
87 EGL_NONE
88 };
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000089 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000090
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000091 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000092
93 const GrGLInterface* interface = GrGLCreateANGLEInterface();
94 if (NULL == interface) {
95 SkDebugf("Could not create ANGLE GL interface!\n");
96 this->destroyGLContext();
97 return NULL;
98 }
99
100 return interface;
101}
102
103void SkANGLEGLContext::makeCurrent() const {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +0000104 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +0000105 SkDebugf("Could not set the context.\n");
106 }
107}