blob: d5471ad943420b2c87d821549cdd78ee6d2dc316 [file] [log] [blame]
Mathias Agopian875d8e12013-06-07 15:35:48 -07001/*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyn7823e122016-09-29 08:08:05 -070017#include <log/log.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070018#include <ui/Rect.h>
19#include <ui/Region.h>
Mathias Agopian875d8e12013-06-07 15:35:48 -070020
Mathias Agopian3f844832013-08-07 21:24:32 -070021#include "GLES20RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070022#include "GLExtensions.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070023#include "Mesh.h"
Chia-I Wub027f802017-11-29 14:00:52 -080024#include "RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070025
Fabien Sanglardc93afd52017-03-13 13:02:42 -070026#include <SurfaceFlinger.h>
Chia-I Wub027f802017-11-29 14:00:52 -080027#include <vector>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070028
Jiyong Park00b15b82017-08-10 20:30:56 +090029extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080030
Mathias Agopian875d8e12013-06-07 15:35:48 -070031// ---------------------------------------------------------------------------
32namespace android {
33// ---------------------------------------------------------------------------
34
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080035std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
36 // initialize EGL for the default display
37 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
38 if (!eglInitialize(display, NULL, NULL)) {
39 LOG_ALWAYS_FATAL("failed to initialize EGL");
40 }
41
Chia-I Wu767d7c92017-11-30 13:22:48 -080042 GLExtensions& extensions(GLExtensions::getInstance());
43 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
44 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
45
Jesse Hall19e87292013-12-23 21:02:15 -080046 // The code assumes that ES2 or later is available if this extension is
47 // supported.
48 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080049 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070050 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080051 }
52
53 EGLint renderableType = 0;
54 if (config == EGL_NO_CONFIG) {
55 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080056 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080057 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
58 }
59 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070060 if (renderableType & EGL_OPENGL_ES2_BIT) {
61 contextClientVersion = 2;
62 } else if (renderableType & EGL_OPENGL_ES_BIT) {
63 contextClientVersion = 1;
64 } else {
65 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
66 }
67
Fabien Sanglardc93afd52017-03-13 13:02:42 -070068 std::vector<EGLint> contextAttributes;
69 contextAttributes.reserve(6);
70 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
71 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070072#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070073 if (SurfaceFlinger::useContextPriority) {
74 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
75 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
76 }
Mathias Agopian875d8e12013-06-07 15:35:48 -070077#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -070078 contextAttributes.push_back(EGL_NONE);
79 contextAttributes.push_back(EGL_NONE);
80
Chia-I Wub027f802017-11-29 14:00:52 -080081 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070082
83 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080084 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070085
86 // now figure out what version of GL did we actually get
87 // NOTE: a dummy surface is not needed if KHR_create_context is supported
88
Jesse Hall19e87292013-12-23 21:02:15 -080089 EGLConfig dummyConfig = config;
90 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070091 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080092 }
Chia-I Wub027f802017-11-29 14:00:52 -080093 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -080094 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -080095 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -070096 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
97 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
98
Chia-I Wub027f802017-11-29 14:00:52 -080099 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
100 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700101
Chia-I Wub027f802017-11-29 14:00:52 -0800102 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700103
104 // initialize the renderer while GL is current
105
Chia-I Wub2c76242017-11-09 17:17:07 -0800106 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700107 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800108 case GLES_VERSION_1_0:
109 case GLES_VERSION_1_1:
110 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
111 break;
112 case GLES_VERSION_2_0:
113 case GLES_VERSION_3_0:
114 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
115 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700116 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800117 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700118
119 ALOGI("OpenGL ES informations:");
120 ALOGI("vendor : %s", extensions.getVendor());
121 ALOGI("renderer : %s", extensions.getRenderer());
122 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800123 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700124 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
125 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700126
127 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
128 eglDestroySurface(display, dummy);
129
130 return engine;
131}
132
Chia-I Wub027f802017-11-29 14:00:52 -0800133RenderEngine::RenderEngine()
134 : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(NULL), mEGLContext(EGL_NO_CONTEXT) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135
136RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800137 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700139}
140
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800141void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
142 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800143 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700144 mEGLContext = ctxt;
145}
146
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800147EGLDisplay RenderEngine::getEGLDisplay() const {
148 return mEGLDisplay;
149}
150
151EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800152 return mEGLConfig;
153}
154
Chia-I Wuf846a352017-11-10 09:22:52 -0800155bool RenderEngine::setCurrentSurface(const RE::Surface& surface) {
156 bool success = true;
157 EGLSurface eglSurface = surface.getEGLSurface();
158 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
159 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
160 if (success && surface.getAsync()) {
161 eglSwapInterval(mEGLDisplay, 0);
162 }
163 }
164
165 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800166}
167
168void RenderEngine::resetCurrentSurface() {
169 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
170}
171
Chia-I Wu767fcf72017-11-30 22:07:38 -0800172base::unique_fd RenderEngine::flush() {
173 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
174 return base::unique_fd();
175 }
176
177 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
178 if (sync == EGL_NO_SYNC_KHR) {
179 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
180 return base::unique_fd();
181 }
182
183 // native fence fd will not be populated until flush() is done.
184 glFlush();
185
186 // get the fence fd
187 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
188 eglDestroySyncKHR(mEGLDisplay, sync);
189 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
190 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
191 }
192
193 return fenceFd;
194}
195
196bool RenderEngine::finish() {
197 if (!GLExtensions::getInstance().hasFenceSync()) {
198 ALOGW("no synchronization support");
199 return false;
200 }
201
202 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
203 if (sync == EGL_NO_SYNC_KHR) {
204 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
205 return false;
206 }
207
208 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
209 2000000000 /*2 sec*/);
210 EGLint error = eglGetError();
211 eglDestroySyncKHR(mEGLDisplay, sync);
212 if (result != EGL_CONDITION_SATISFIED_KHR) {
213 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
214 ALOGW("fence wait timed out");
215 } else {
216 ALOGW("error waiting on EGL fence: %#x", error);
217 }
218 return false;
219 }
220
221 return true;
222}
223
224bool RenderEngine::waitFence(base::unique_fd fenceFd) {
225 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
226 !GLExtensions::getInstance().hasWaitSync()) {
227 return false;
228 }
229
230 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
231 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
232 if (sync == EGL_NO_SYNC_KHR) {
233 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
234 return false;
235 }
236
237 // fenceFd is now owned by EGLSync
238 (void)fenceFd.release();
239
240 // XXX: The spec draft is inconsistent as to whether this should return an
241 // EGLint or void. Ignore the return value for now, as it's not strictly
242 // needed.
243 eglWaitSyncKHR(mEGLDisplay, sync, 0);
244 EGLint error = eglGetError();
245 eglDestroySyncKHR(mEGLDisplay, sync);
246 if (error != EGL_SUCCESS) {
247 ALOGE("failed to wait for EGL native fence sync: %#x", error);
248 return false;
249 }
250
251 return true;
252}
253
Mathias Agopian875d8e12013-06-07 15:35:48 -0700254void RenderEngine::checkErrors() const {
255 do {
256 // there could be more than one error flag
257 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800258 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700259 ALOGE("GL error 0x%04x", int(error));
260 } while (true);
261}
262
263RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
264 int major, minor;
265 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
266 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
267 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
268 return GLES_VERSION_1_0;
269 }
270 }
271
272 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
273 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
274 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
275 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
276
277 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
278 return GLES_VERSION_1_0;
279}
280
Chia-I Wub027f802017-11-29 14:00:52 -0800281void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
282 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700283 size_t c;
284 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800285 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700286 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800287 for (size_t i = 0; i < c; i++, r++) {
288 position[i * 6 + 0].x = r->left;
289 position[i * 6 + 0].y = height - r->top;
290 position[i * 6 + 1].x = r->left;
291 position[i * 6 + 1].y = height - r->bottom;
292 position[i * 6 + 2].x = r->right;
293 position[i * 6 + 2].y = height - r->bottom;
294 position[i * 6 + 3].x = r->left;
295 position[i * 6 + 3].y = height - r->top;
296 position[i * 6 + 4].x = r->right;
297 position[i * 6 + 4].y = height - r->bottom;
298 position[i * 6 + 5].x = r->right;
299 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700300 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700301 setupFillWithColor(red, green, blue, alpha);
302 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700303}
304
305void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
306 glClearColor(red, green, blue, alpha);
307 glClear(GL_COLOR_BUFFER_BIT);
308}
309
Chia-I Wub027f802017-11-29 14:00:52 -0800310void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700311 glScissor(left, bottom, right, top);
312 glEnable(GL_SCISSOR_TEST);
313}
314
315void RenderEngine::disableScissor() {
316 glDisable(GL_SCISSOR_TEST);
317}
318
319void RenderEngine::genTextures(size_t count, uint32_t* names) {
320 glGenTextures(count, names);
321}
322
323void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
324 glDeleteTextures(count, names);
325}
326
Mathias Agopiand5556842013-09-19 17:08:37 -0700327void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
328 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
329}
330
Mathias Agopian458197d2013-08-15 14:56:51 -0700331void RenderEngine::dump(String8& result) {
332 const GLExtensions& extensions(GLExtensions::getInstance());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800333
334 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
335 result.appendFormat("%s\n", extensions.getEGLExtensions());
336
Chia-I Wub027f802017-11-29 14:00:52 -0800337 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
338 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800339 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700340}
341
Mathias Agopian3f844832013-08-07 21:24:32 -0700342// ---------------------------------------------------------------------------
343
Chia-I Wueadbaa62017-11-09 11:26:15 -0800344RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800345 RenderEngine& engine, ANativeWindowBuffer* buffer)
346 : mEngine(engine) {
347 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
348 buffer, NULL);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800349 if (mImage == EGL_NO_IMAGE_KHR) {
350 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
351 return;
352 }
353
354 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700355
Chia-I Wub027f802017-11-29 14:00:52 -0800356 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
357 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700358}
359
Chia-I Wueadbaa62017-11-09 11:26:15 -0800360RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
361 if (mImage == EGL_NO_IMAGE_KHR) {
362 return;
363 }
364
Mathias Agopian3f844832013-08-07 21:24:32 -0700365 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700366 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800367 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700368}
369
Chia-I Wueadbaa62017-11-09 11:26:15 -0800370status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700371 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
372}
373
Mathias Agopian875d8e12013-06-07 15:35:48 -0700374// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800375
Chia-I Wub027f802017-11-29 14:00:52 -0800376static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
377 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800378 EGLint numConfigs = -1, n = 0;
379 eglGetConfigs(dpy, NULL, 0, &numConfigs);
380 EGLConfig* const configs = new EGLConfig[numConfigs];
381 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
382
383 if (n) {
384 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800385 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800386 EGLint value = 0;
387 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
388 if (wanted == value) {
389 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800390 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800391 return NO_ERROR;
392 }
393 }
394 } else {
395 // just pick the first one
396 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800397 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800398 return NO_ERROR;
399 }
400 }
Chia-I Wub027f802017-11-29 14:00:52 -0800401 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800402 return NAME_NOT_FOUND;
403}
404
405class EGLAttributeVector {
406 struct Attribute;
407 class Adder;
408 friend class Adder;
409 KeyedVector<Attribute, EGLint> mList;
410 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800411 Attribute() : v(0){};
412 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800413 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800414 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800415 // this places EGL_NONE at the end
416 EGLint lhs(v);
417 EGLint rhs(other.v);
418 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
419 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
420 return lhs < rhs;
421 }
422 };
423 class Adder {
424 friend class EGLAttributeVector;
425 EGLAttributeVector& v;
426 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800427 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
428
Jesse Hall05f8c702013-12-23 20:44:38 -0800429 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800430 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800431 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700432 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800433 }
434 }
Chia-I Wub027f802017-11-29 14:00:52 -0800435 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800436 };
Chia-I Wub027f802017-11-29 14:00:52 -0800437
Jesse Hall05f8c702013-12-23 20:44:38 -0800438public:
Chia-I Wub027f802017-11-29 14:00:52 -0800439 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800440 void remove(EGLint attribute) {
441 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700442 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800443 }
444 }
Chia-I Wub027f802017-11-29 14:00:52 -0800445 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
446 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800447 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800448 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800449};
450
Chia-I Wub027f802017-11-29 14:00:52 -0800451static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
452 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800453 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
454 // it is to be used with WIFI displays
455 status_t err;
456 EGLint wantedAttribute;
457 EGLint wantedAttributeValue;
458
459 EGLAttributeVector attribs;
460 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800461 attribs[EGL_RENDERABLE_TYPE] = renderableType;
462 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
463 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800464 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800465 attribs[EGL_RED_SIZE] = 8;
466 attribs[EGL_GREEN_SIZE] = 8;
467 attribs[EGL_BLUE_SIZE] = 8;
468 attribs[EGL_ALPHA_SIZE] = 8;
469 wantedAttribute = EGL_NONE;
470 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800471 } else {
472 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800473 wantedAttribute = EGL_NATIVE_VISUAL_ID;
474 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800475 }
476
Chia-I Wub027f802017-11-29 14:00:52 -0800477 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800478 if (err == NO_ERROR) {
479 EGLint caveat;
480 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
481 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
482 }
483
484 return err;
485}
486
Chia-I Wub027f802017-11-29 14:00:52 -0800487EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800488 status_t err;
489 EGLConfig config;
490
491 // First try to get an ES2 config
492 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
493 if (err != NO_ERROR) {
494 // If ES2 fails, try ES1
495 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
496 if (err != NO_ERROR) {
497 // still didn't work, probably because we're on the emulator...
498 // try a simplified query
499 ALOGW("no suitable EGLConfig found, trying a simpler query");
500 err = selectEGLConfig(display, format, 0, &config);
501 if (err != NO_ERROR) {
502 // this EGL is too lame for android
503 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
504 }
505 }
506 }
507
Steven Thomasd7f49c52017-07-26 18:48:28 -0700508 if (logConfig) {
509 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800510 EGLint r, g, b, a;
511 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700512 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800513 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700514 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
515 ALOGI("EGL information:");
516 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
517 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
518 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800519 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700520 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
521 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800522
523 return config;
524}
525
Dan Stoza4e637772016-07-28 13:31:51 -0700526void RenderEngine::primeCache() const {
527 // Getting the ProgramCache instance causes it to prime its shader cache,
528 // which is performed in its constructor
529 ProgramCache::getInstance();
530}
531
Jesse Hall05f8c702013-12-23 20:44:38 -0800532// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700533}; // namespace android
534// ---------------------------------------------------------------------------