blob: d4d7219b5bbbf7a3799e367d7102288cfa382abc [file] [log] [blame]
djsollen@google.com58629292011-11-03 13:08:29 +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"
djsollen@google.com58629292011-11-03 13:08:29 +00009
bsalomon10805962014-10-08 04:45:09 -070010SkNativeGLContext::AutoContextRestore::AutoContextRestore() {
11 fOldEGLContext = eglGetCurrentContext();
12 fOldDisplay = eglGetCurrentDisplay();
13 fOldSurface = eglGetCurrentSurface(EGL_DRAW);
djsollen@google.com58629292011-11-03 13:08:29 +000014
bsalomon10805962014-10-08 04:45:09 -070015}
djsollen@google.com58629292011-11-03 13:08:29 +000016
bsalomon10805962014-10-08 04:45:09 -070017SkNativeGLContext::AutoContextRestore::~AutoContextRestore() {
18 if (fOldDisplay) {
19 eglMakeCurrent(fOldDisplay, fOldSurface, fOldSurface, fOldEGLContext);
20 }
21}
djsollen@google.com58629292011-11-03 13:08:29 +000022
bsalomon10805962014-10-08 04:45:09 -070023///////////////////////////////////////////////////////////////////////////////
djsollen@google.com58629292011-11-03 13:08:29 +000024
bsalomon10805962014-10-08 04:45:09 -070025SkNativeGLContext::SkNativeGLContext()
djsollen@google.com58629292011-11-03 13:08:29 +000026 : fContext(EGL_NO_CONTEXT)
27 , fDisplay(EGL_NO_DISPLAY)
28 , fSurface(EGL_NO_SURFACE) {
29}
30
bsalomon10805962014-10-08 04:45:09 -070031SkNativeGLContext::~SkNativeGLContext() {
djsollen@google.com58629292011-11-03 13:08:29 +000032 this->destroyGLContext();
33}
34
bsalomon10805962014-10-08 04:45:09 -070035void SkNativeGLContext::destroyGLContext() {
djsollen@google.com58629292011-11-03 13:08:29 +000036 if (fDisplay) {
37 eglMakeCurrent(fDisplay, 0, 0, 0);
38
39 if (fContext) {
40 eglDestroyContext(fDisplay, fContext);
41 fContext = EGL_NO_CONTEXT;
42 }
43
44 if (fSurface) {
45 eglDestroySurface(fDisplay, fSurface);
46 fSurface = EGL_NO_SURFACE;
47 }
48
49 //TODO should we close the display?
50 fDisplay = EGL_NO_DISPLAY;
51 }
52}
53
bsalomon10805962014-10-08 04:45:09 -070054const GrGLInterface* SkNativeGLContext::createGLContext(GrGLStandard forcedGpuAPI) {
commit-bot@chromium.orgf5897f82013-09-03 17:50:50 +000055 static const EGLint kEGLContextAttribsForOpenGL[] = {
56 EGL_NONE
57 };
58
59 static const EGLint kEGLContextAttribsForOpenGLES[] = {
60 EGL_CONTEXT_CLIENT_VERSION, 2,
61 EGL_NONE
62 };
63
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000064 static const struct {
65 const EGLint* fContextAttribs;
66 EGLenum fAPI;
67 EGLint fRenderableTypeBit;
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000068 GrGLStandard fStandard;
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000069 } kAPIs[] = {
70 { // OpenGL
71 kEGLContextAttribsForOpenGL,
72 EGL_OPENGL_API,
73 EGL_OPENGL_BIT,
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000074 kGL_GrGLStandard
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000075 },
76 { // OpenGL ES. This seems to work for both ES2 and 3 (when available).
77 kEGLContextAttribsForOpenGLES,
78 EGL_OPENGL_ES_API,
79 EGL_OPENGL_ES2_BIT,
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +000080 kGLES_GrGLStandard
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000081 },
djsollen@google.com58629292011-11-03 13:08:29 +000082 };
83
kkinnunen80549fc2014-06-30 06:36:31 -070084 size_t apiLimit = SK_ARRAY_COUNT(kAPIs);
85 size_t api = 0;
86 if (forcedGpuAPI == kGL_GrGLStandard) {
87 apiLimit = 1;
88 } else if (forcedGpuAPI == kGLES_GrGLStandard) {
89 api = 1;
90 }
91 SkASSERT(forcedGpuAPI == kNone_GrGLStandard || kAPIs[api].fStandard == forcedGpuAPI);
92
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000093 const GrGLInterface* interface = NULL;
djsollen@google.com58629292011-11-03 13:08:29 +000094
tomhudson579b4fb2014-09-10 10:45:42 -070095 for (; NULL == interface && api < apiLimit; ++api) {
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000096 fDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
commit-bot@chromium.orgf5897f82013-09-03 17:50:50 +000097
commit-bot@chromium.org1e627272013-09-06 18:57:14 +000098 EGLint majorVersion;
99 EGLint minorVersion;
100 eglInitialize(fDisplay, &majorVersion, &minorVersion);
djsollen@google.com58629292011-11-03 13:08:29 +0000101
commit-bot@chromium.org1e627272013-09-06 18:57:14 +0000102#if 0
103 SkDebugf("VENDOR: %s\n", eglQueryString(fDisplay, EGL_VENDOR));
104 SkDebugf("APIS: %s\n", eglQueryString(fDisplay, EGL_CLIENT_APIS));
105 SkDebugf("VERSION: %s\n", eglQueryString(fDisplay, EGL_VERSION));
106 SkDebugf("EXTENSIONS %s\n", eglQueryString(fDisplay, EGL_EXTENSIONS));
107#endif
commit-bot@chromium.orgf5897f82013-09-03 17:50:50 +0000108
commit-bot@chromium.org1e627272013-09-06 18:57:14 +0000109 if (!eglBindAPI(kAPIs[api].fAPI)) {
110 continue;
111 }
commit-bot@chromium.orgf5897f82013-09-03 17:50:50 +0000112
commit-bot@chromium.org1e627272013-09-06 18:57:14 +0000113 EGLint numConfigs;
114 const EGLint configAttribs[] = {
115 EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
116 EGL_RENDERABLE_TYPE, kAPIs[api].fRenderableTypeBit,
117 EGL_RED_SIZE, 8,
118 EGL_GREEN_SIZE, 8,
119 EGL_BLUE_SIZE, 8,
120 EGL_ALPHA_SIZE, 8,
121 EGL_NONE
122 };
123
124 EGLConfig surfaceConfig;
125 if (!eglChooseConfig(fDisplay, configAttribs, &surfaceConfig, 1, &numConfigs)) {
126 SkDebugf("eglChooseConfig failed. EGL Error: 0x%08x\n", eglGetError());
127 continue;
128 }
129
130 fContext = eglCreateContext(fDisplay, surfaceConfig, NULL, kAPIs[api].fContextAttribs);
131 if (EGL_NO_CONTEXT == fContext) {
132 SkDebugf("eglCreateContext failed. EGL Error: 0x%08x\n", eglGetError());
133 continue;
134 }
135
136 static const EGLint kSurfaceAttribs[] = {
137 EGL_WIDTH, 1,
138 EGL_HEIGHT, 1,
139 EGL_NONE
140 };
141
142 fSurface = eglCreatePbufferSurface(fDisplay, surfaceConfig, kSurfaceAttribs);
143 if (EGL_NO_SURFACE == fSurface) {
144 SkDebugf("eglCreatePbufferSurface failed. EGL Error: 0x%08x\n", eglGetError());
145 this->destroyGLContext();
146 continue;
147 }
148
149 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
150 SkDebugf("eglMakeCurrent failed. EGL Error: 0x%08x\n", eglGetError());
151 this->destroyGLContext();
152 continue;
153 }
154
155 interface = GrGLCreateNativeInterface();
156 if (NULL == interface) {
157 SkDebugf("Failed to create gl interface.\n");
158 this->destroyGLContext();
159 continue;
160 }
161
commit-bot@chromium.org9e90aed2014-01-16 16:35:09 +0000162 if (!interface->validate()) {
commit-bot@chromium.org1e627272013-09-06 18:57:14 +0000163 interface->unref();
164 interface = NULL;
165 this->destroyGLContext();
166 }
commit-bot@chromium.orgf5897f82013-09-03 17:50:50 +0000167 }
168
djsollen@google.com58629292011-11-03 13:08:29 +0000169 return interface;
170}
171
bsalomon10805962014-10-08 04:45:09 -0700172void SkNativeGLContext::makeCurrent() const {
djsollen@google.com58629292011-11-03 13:08:29 +0000173 if (!eglMakeCurrent(fDisplay, fSurface, fSurface, fContext)) {
174 SkDebugf("Could not set the context.\n");
175 }
176}
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000177
bsalomon10805962014-10-08 04:45:09 -0700178void SkNativeGLContext::swapBuffers() const {
djsollen@google.comc9542ca2013-10-09 18:25:38 +0000179 if (!eglSwapBuffers(fDisplay, fSurface)) {
180 SkDebugf("Could not complete eglSwapBuffers.\n");
181 }
182}