blob: f1415c9757b202ee0f9551d4ae0b28a763dfd866 [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"
Chia-I Wu401ef832017-12-01 10:52:22 -080023#include "Image.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include "Mesh.h"
Chia-I Wub027f802017-11-29 14:00:52 -080025#include "RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070026
Fabien Sanglardc93afd52017-03-13 13:02:42 -070027#include <SurfaceFlinger.h>
Chia-I Wub027f802017-11-29 14:00:52 -080028#include <vector>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070029
Jiyong Park00b15b82017-08-10 20:30:56 +090030extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080031
Mathias Agopian875d8e12013-06-07 15:35:48 -070032// ---------------------------------------------------------------------------
33namespace android {
34// ---------------------------------------------------------------------------
35
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080036std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
37 // initialize EGL for the default display
38 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
39 if (!eglInitialize(display, NULL, NULL)) {
40 LOG_ALWAYS_FATAL("failed to initialize EGL");
41 }
42
Chia-I Wu767d7c92017-11-30 13:22:48 -080043 GLExtensions& extensions(GLExtensions::getInstance());
44 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
45 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
46
Jesse Hall19e87292013-12-23 21:02:15 -080047 // The code assumes that ES2 or later is available if this extension is
48 // supported.
49 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080050 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070051 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080052 }
53
54 EGLint renderableType = 0;
55 if (config == EGL_NO_CONFIG) {
56 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080057 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080058 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
59 }
60 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070061 if (renderableType & EGL_OPENGL_ES2_BIT) {
62 contextClientVersion = 2;
63 } else if (renderableType & EGL_OPENGL_ES_BIT) {
64 contextClientVersion = 1;
65 } else {
66 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
67 }
68
Fabien Sanglardc93afd52017-03-13 13:02:42 -070069 std::vector<EGLint> contextAttributes;
70 contextAttributes.reserve(6);
71 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
72 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070073#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070074 if (SurfaceFlinger::useContextPriority) {
75 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
76 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
77 }
Mathias Agopian875d8e12013-06-07 15:35:48 -070078#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -070079 contextAttributes.push_back(EGL_NONE);
80 contextAttributes.push_back(EGL_NONE);
81
Chia-I Wub027f802017-11-29 14:00:52 -080082 EGLContext ctxt = eglCreateContext(display, config, NULL, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070083
84 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080085 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070086
87 // now figure out what version of GL did we actually get
88 // NOTE: a dummy surface is not needed if KHR_create_context is supported
89
Jesse Hall19e87292013-12-23 21:02:15 -080090 EGLConfig dummyConfig = config;
91 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070092 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080093 }
Chia-I Wub027f802017-11-29 14:00:52 -080094 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -080095 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -080096 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -070097 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
98 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
99
Chia-I Wub027f802017-11-29 14:00:52 -0800100 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
101 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700102
Chia-I Wub027f802017-11-29 14:00:52 -0800103 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700104
105 // initialize the renderer while GL is current
106
Chia-I Wub2c76242017-11-09 17:17:07 -0800107 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700108 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800109 case GLES_VERSION_1_0:
110 case GLES_VERSION_1_1:
111 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
112 break;
113 case GLES_VERSION_2_0:
114 case GLES_VERSION_3_0:
115 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
116 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700117 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800118 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700119
120 ALOGI("OpenGL ES informations:");
121 ALOGI("vendor : %s", extensions.getVendor());
122 ALOGI("renderer : %s", extensions.getRenderer());
123 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800124 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700125 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
126 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700127
128 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
129 eglDestroySurface(display, dummy);
130
131 return engine;
132}
133
Chia-I Wub027f802017-11-29 14:00:52 -0800134RenderEngine::RenderEngine()
135 : mEGLDisplay(EGL_NO_DISPLAY), mEGLConfig(NULL), mEGLContext(EGL_NO_CONTEXT) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700136
137RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800138 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
139 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700140}
141
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800142void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
143 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800144 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700145 mEGLContext = ctxt;
146}
147
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800148EGLDisplay RenderEngine::getEGLDisplay() const {
149 return mEGLDisplay;
150}
151
152EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800153 return mEGLConfig;
154}
155
Chia-I Wu401ef832017-12-01 10:52:22 -0800156bool RenderEngine::supportsImageCrop() const {
157 return GLExtensions::getInstance().hasImageCrop();
158}
159
Chia-I Wuf846a352017-11-10 09:22:52 -0800160bool RenderEngine::setCurrentSurface(const RE::Surface& surface) {
161 bool success = true;
162 EGLSurface eglSurface = surface.getEGLSurface();
163 if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
164 success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
165 if (success && surface.getAsync()) {
166 eglSwapInterval(mEGLDisplay, 0);
167 }
168 }
169
170 return success;
Chia-I Wu7f402902017-11-09 12:51:10 -0800171}
172
173void RenderEngine::resetCurrentSurface() {
174 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
175}
176
Chia-I Wu767fcf72017-11-30 22:07:38 -0800177base::unique_fd RenderEngine::flush() {
178 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
179 return base::unique_fd();
180 }
181
182 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
183 if (sync == EGL_NO_SYNC_KHR) {
184 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
185 return base::unique_fd();
186 }
187
188 // native fence fd will not be populated until flush() is done.
189 glFlush();
190
191 // get the fence fd
192 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
193 eglDestroySyncKHR(mEGLDisplay, sync);
194 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
195 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
196 }
197
198 return fenceFd;
199}
200
201bool RenderEngine::finish() {
202 if (!GLExtensions::getInstance().hasFenceSync()) {
203 ALOGW("no synchronization support");
204 return false;
205 }
206
207 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
208 if (sync == EGL_NO_SYNC_KHR) {
209 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
210 return false;
211 }
212
213 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
214 2000000000 /*2 sec*/);
215 EGLint error = eglGetError();
216 eglDestroySyncKHR(mEGLDisplay, sync);
217 if (result != EGL_CONDITION_SATISFIED_KHR) {
218 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
219 ALOGW("fence wait timed out");
220 } else {
221 ALOGW("error waiting on EGL fence: %#x", error);
222 }
223 return false;
224 }
225
226 return true;
227}
228
229bool RenderEngine::waitFence(base::unique_fd fenceFd) {
230 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
231 !GLExtensions::getInstance().hasWaitSync()) {
232 return false;
233 }
234
235 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
236 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
237 if (sync == EGL_NO_SYNC_KHR) {
238 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
239 return false;
240 }
241
242 // fenceFd is now owned by EGLSync
243 (void)fenceFd.release();
244
245 // XXX: The spec draft is inconsistent as to whether this should return an
246 // EGLint or void. Ignore the return value for now, as it's not strictly
247 // needed.
248 eglWaitSyncKHR(mEGLDisplay, sync, 0);
249 EGLint error = eglGetError();
250 eglDestroySyncKHR(mEGLDisplay, sync);
251 if (error != EGL_SUCCESS) {
252 ALOGE("failed to wait for EGL native fence sync: %#x", error);
253 return false;
254 }
255
256 return true;
257}
258
Mathias Agopian875d8e12013-06-07 15:35:48 -0700259void RenderEngine::checkErrors() const {
260 do {
261 // there could be more than one error flag
262 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800263 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700264 ALOGE("GL error 0x%04x", int(error));
265 } while (true);
266}
267
268RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
269 int major, minor;
270 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
271 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
272 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
273 return GLES_VERSION_1_0;
274 }
275 }
276
277 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
278 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
279 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
280 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
281
282 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
283 return GLES_VERSION_1_0;
284}
285
Chia-I Wub027f802017-11-29 14:00:52 -0800286void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
287 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700288 size_t c;
289 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800290 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700291 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800292 for (size_t i = 0; i < c; i++, r++) {
293 position[i * 6 + 0].x = r->left;
294 position[i * 6 + 0].y = height - r->top;
295 position[i * 6 + 1].x = r->left;
296 position[i * 6 + 1].y = height - r->bottom;
297 position[i * 6 + 2].x = r->right;
298 position[i * 6 + 2].y = height - r->bottom;
299 position[i * 6 + 3].x = r->left;
300 position[i * 6 + 3].y = height - r->top;
301 position[i * 6 + 4].x = r->right;
302 position[i * 6 + 4].y = height - r->bottom;
303 position[i * 6 + 5].x = r->right;
304 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700305 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700306 setupFillWithColor(red, green, blue, alpha);
307 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700308}
309
310void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
311 glClearColor(red, green, blue, alpha);
312 glClear(GL_COLOR_BUFFER_BIT);
313}
314
Chia-I Wub027f802017-11-29 14:00:52 -0800315void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700316 glScissor(left, bottom, right, top);
317 glEnable(GL_SCISSOR_TEST);
318}
319
320void RenderEngine::disableScissor() {
321 glDisable(GL_SCISSOR_TEST);
322}
323
324void RenderEngine::genTextures(size_t count, uint32_t* names) {
325 glGenTextures(count, names);
326}
327
328void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
329 glDeleteTextures(count, names);
330}
331
Chia-I Wu401ef832017-12-01 10:52:22 -0800332void RenderEngine::bindExternalTextureImage(uint32_t texName, const RE::Image& image) {
333 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
334
335 glBindTexture(target, texName);
336 if (image.getEGLImage() != EGL_NO_IMAGE_KHR) {
337 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(image.getEGLImage()));
338 }
339}
340
Mathias Agopiand5556842013-09-19 17:08:37 -0700341void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
342 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
343}
344
Mathias Agopian458197d2013-08-15 14:56:51 -0700345void RenderEngine::dump(String8& result) {
346 const GLExtensions& extensions(GLExtensions::getInstance());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800347
348 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
349 result.appendFormat("%s\n", extensions.getEGLExtensions());
350
Chia-I Wub027f802017-11-29 14:00:52 -0800351 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
352 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800353 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700354}
355
Mathias Agopian3f844832013-08-07 21:24:32 -0700356// ---------------------------------------------------------------------------
357
Chia-I Wueadbaa62017-11-09 11:26:15 -0800358RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800359 RenderEngine& engine, ANativeWindowBuffer* buffer)
360 : mEngine(engine) {
361 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
362 buffer, NULL);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800363 if (mImage == EGL_NO_IMAGE_KHR) {
364 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
365 return;
366 }
367
368 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700369
Chia-I Wub027f802017-11-29 14:00:52 -0800370 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
371 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700372}
373
Chia-I Wueadbaa62017-11-09 11:26:15 -0800374RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
375 if (mImage == EGL_NO_IMAGE_KHR) {
376 return;
377 }
378
Mathias Agopian3f844832013-08-07 21:24:32 -0700379 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700380 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800381 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700382}
383
Chia-I Wueadbaa62017-11-09 11:26:15 -0800384status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700385 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
386}
387
Mathias Agopian875d8e12013-06-07 15:35:48 -0700388// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800389
Chia-I Wub027f802017-11-29 14:00:52 -0800390static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
391 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800392 EGLint numConfigs = -1, n = 0;
393 eglGetConfigs(dpy, NULL, 0, &numConfigs);
394 EGLConfig* const configs = new EGLConfig[numConfigs];
395 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
396
397 if (n) {
398 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800399 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800400 EGLint value = 0;
401 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
402 if (wanted == value) {
403 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800404 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800405 return NO_ERROR;
406 }
407 }
408 } else {
409 // just pick the first one
410 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800411 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800412 return NO_ERROR;
413 }
414 }
Chia-I Wub027f802017-11-29 14:00:52 -0800415 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800416 return NAME_NOT_FOUND;
417}
418
419class EGLAttributeVector {
420 struct Attribute;
421 class Adder;
422 friend class Adder;
423 KeyedVector<Attribute, EGLint> mList;
424 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800425 Attribute() : v(0){};
426 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800427 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800428 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800429 // this places EGL_NONE at the end
430 EGLint lhs(v);
431 EGLint rhs(other.v);
432 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
433 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
434 return lhs < rhs;
435 }
436 };
437 class Adder {
438 friend class EGLAttributeVector;
439 EGLAttributeVector& v;
440 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800441 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
442
Jesse Hall05f8c702013-12-23 20:44:38 -0800443 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800444 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800445 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700446 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800447 }
448 }
Chia-I Wub027f802017-11-29 14:00:52 -0800449 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800450 };
Chia-I Wub027f802017-11-29 14:00:52 -0800451
Jesse Hall05f8c702013-12-23 20:44:38 -0800452public:
Chia-I Wub027f802017-11-29 14:00:52 -0800453 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800454 void remove(EGLint attribute) {
455 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700456 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800457 }
458 }
Chia-I Wub027f802017-11-29 14:00:52 -0800459 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
460 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800461 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800462 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800463};
464
Chia-I Wub027f802017-11-29 14:00:52 -0800465static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
466 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800467 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
468 // it is to be used with WIFI displays
469 status_t err;
470 EGLint wantedAttribute;
471 EGLint wantedAttributeValue;
472
473 EGLAttributeVector attribs;
474 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800475 attribs[EGL_RENDERABLE_TYPE] = renderableType;
476 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
477 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800478 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800479 attribs[EGL_RED_SIZE] = 8;
480 attribs[EGL_GREEN_SIZE] = 8;
481 attribs[EGL_BLUE_SIZE] = 8;
482 attribs[EGL_ALPHA_SIZE] = 8;
483 wantedAttribute = EGL_NONE;
484 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800485 } else {
486 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800487 wantedAttribute = EGL_NATIVE_VISUAL_ID;
488 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800489 }
490
Chia-I Wub027f802017-11-29 14:00:52 -0800491 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800492 if (err == NO_ERROR) {
493 EGLint caveat;
494 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
495 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
496 }
497
498 return err;
499}
500
Chia-I Wub027f802017-11-29 14:00:52 -0800501EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800502 status_t err;
503 EGLConfig config;
504
505 // First try to get an ES2 config
506 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
507 if (err != NO_ERROR) {
508 // If ES2 fails, try ES1
509 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
510 if (err != NO_ERROR) {
511 // still didn't work, probably because we're on the emulator...
512 // try a simplified query
513 ALOGW("no suitable EGLConfig found, trying a simpler query");
514 err = selectEGLConfig(display, format, 0, &config);
515 if (err != NO_ERROR) {
516 // this EGL is too lame for android
517 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
518 }
519 }
520 }
521
Steven Thomasd7f49c52017-07-26 18:48:28 -0700522 if (logConfig) {
523 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800524 EGLint r, g, b, a;
525 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700526 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800527 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700528 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
529 ALOGI("EGL information:");
530 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
531 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
532 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800533 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700534 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
535 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800536
537 return config;
538}
539
Dan Stoza4e637772016-07-28 13:31:51 -0700540void RenderEngine::primeCache() const {
541 // Getting the ProgramCache instance causes it to prime its shader cache,
542 // which is performed in its constructor
543 ProgramCache::getInstance();
544}
545
Jesse Hall05f8c702013-12-23 20:44:38 -0800546// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700547}; // namespace android
548// ---------------------------------------------------------------------------