blob: 4914ba55333d70df73ccac437b940ac0db7b6085 [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
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000011SkANGLEGLContext::SkANGLEGLContext()
12 : fContext(EGL_NO_CONTEXT)
13 , fDisplay(EGL_NO_DISPLAY)
14 , fSurface(EGL_NO_SURFACE) {
15}
16
17SkANGLEGLContext::~SkANGLEGLContext() {
18 this->destroyGLContext();
19}
20
21void SkANGLEGLContext::destroyGLContext() {
22 if (fDisplay) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000023 eglMakeCurrent(fDisplay, 0, 0, 0);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000024
25 if (fContext) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000026 eglDestroyContext(fDisplay, fContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000027 fContext = EGL_NO_CONTEXT;
28 }
29
30 if (fSurface) {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000031 eglDestroySurface(fDisplay, fSurface);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000032 fSurface = EGL_NO_SURFACE;
33 }
34
35 //TODO should we close the display?
36 fDisplay = EGL_NO_DISPLAY;
37 }
38}
39
kkinnunen80549fc2014-06-30 06:36:31 -070040const GrGLInterface* SkANGLEGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
41 if (kGL_GrGLStandard == forcedGpuAPI) {
42 return NULL;
43 }
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000044
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000045 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000046
47 EGLint majorVersion;
48 EGLint minorVersion;
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000049 eglInitialize(fDisplay, &majorVersion, &minorVersion);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000050
51 EGLint numConfigs;
52 static const EGLint configAttribs[] = {
53 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
54 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
55 EGL_RED_SIZE, 8,
56 EGL_GREEN_SIZE, 8,
57 EGL_BLUE_SIZE, 8,
58 EGL_ALPHA_SIZE, 8,
59 EGL_NONE
60 };
61
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000062 EGLConfig surfaceConfig;
63 eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000064
65 static const EGLint contextAttribs[] = {
66 EGL_CONTEXT_CLIENT_VERSION, 2,
67 EGL_NONE
68 };
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000069 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, contextAttribs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000070
71
72 static const EGLint surfaceAttribs[] = {
73 EGL_WIDTH, 1,
74 EGL_HEIGHT, 1,
75 EGL_NONE
76 };
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000077 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, surfaceAttribs);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000078
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000079 eglMakeCurrent(fDisplay, fSurface, fSurface, fContext);
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000080
81 const GrGLInterface* interface = GrGLCreateANGLEInterface();
82 if (NULL == interface) {
83 SkDebugf("Could not create ANGLE GL interface!\n");
84 this->destroyGLContext();
85 return NULL;
86 }
87
88 return interface;
89}
90
91void SkANGLEGLContext::makeCurrent() const {
robertphillips@google.comd5c8fe62012-04-02 15:04:16 +000092 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
robertphillips@google.comd3b9fbb2012-03-28 16:19:11 +000093 SkDebugf("Could not set the context.\n");
94 }
95}
djsollen@google.comc9542ca2013-10-09 18:25:38 +000096
97void SkANGLEGLContext::swapBuffers() const {
98 if (!eglSwapBuffers(fDisplay, fSurface)) {
99 SkDebugf("Could not complete eglSwapBuffers.\n");
100 }
101}