blob: d3bda17eb038913c7062e7a27fb048403f92bbf6 [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
Mathias Agopian875d8e12013-06-07 15:35:48 -0700172void RenderEngine::checkErrors() const {
173 do {
174 // there could be more than one error flag
175 GLenum error = glGetError();
Chia-I Wub027f802017-11-29 14:00:52 -0800176 if (error == GL_NO_ERROR) break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700177 ALOGE("GL error 0x%04x", int(error));
178 } while (true);
179}
180
181RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
182 int major, minor;
183 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
184 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
185 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
186 return GLES_VERSION_1_0;
187 }
188 }
189
190 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
191 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
192 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
193 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
194
195 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
196 return GLES_VERSION_1_0;
197}
198
Chia-I Wub027f802017-11-29 14:00:52 -0800199void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
200 float green, float blue, float alpha) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700201 size_t c;
202 Rect const* r = region.getArray(&c);
Chia-I Wub027f802017-11-29 14:00:52 -0800203 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700204 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Chia-I Wub027f802017-11-29 14:00:52 -0800205 for (size_t i = 0; i < c; i++, r++) {
206 position[i * 6 + 0].x = r->left;
207 position[i * 6 + 0].y = height - r->top;
208 position[i * 6 + 1].x = r->left;
209 position[i * 6 + 1].y = height - r->bottom;
210 position[i * 6 + 2].x = r->right;
211 position[i * 6 + 2].y = height - r->bottom;
212 position[i * 6 + 3].x = r->left;
213 position[i * 6 + 3].y = height - r->top;
214 position[i * 6 + 4].x = r->right;
215 position[i * 6 + 4].y = height - r->bottom;
216 position[i * 6 + 5].x = r->right;
217 position[i * 6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700218 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700219 setupFillWithColor(red, green, blue, alpha);
220 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700221}
222
Chia-I Wub0c041b2017-11-09 11:36:33 -0800223int RenderEngine::flush(bool wait) {
224 // Attempt to create a sync khr object that can produce a sync point. If that
225 // isn't available, create a non-dupable sync object in the fallback path and
226 // wait on it directly.
227 EGLSyncKHR sync;
228 if (!wait) {
229 EGLint syncFd = EGL_NO_NATIVE_FENCE_FD_ANDROID;
230
231 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
232 if (sync != EGL_NO_SYNC_KHR) {
233 // native fence fd will not be populated until flush() is done.
234 glFlush();
235
236 // get the sync fd
237 syncFd = eglDupNativeFenceFDANDROID(mEGLDisplay, sync);
238 if (syncFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
239 ALOGW("failed to dup sync khr object");
240 }
241
242 eglDestroySyncKHR(mEGLDisplay, sync);
243 }
244
245 if (syncFd != EGL_NO_NATIVE_FENCE_FD_ANDROID) {
246 return syncFd;
247 }
248 }
249
250 // fallback or explicit wait
251 sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, NULL);
252 if (sync != EGL_NO_SYNC_KHR) {
Chia-I Wub027f802017-11-29 14:00:52 -0800253 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
254 2000000000 /*2 sec*/);
Chia-I Wub0c041b2017-11-09 11:36:33 -0800255 EGLint eglErr = eglGetError();
256 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
257 ALOGW("fence wait timed out");
258 } else {
Chia-I Wub027f802017-11-29 14:00:52 -0800259 ALOGW_IF(eglErr != EGL_SUCCESS, "error waiting on EGL fence: %#x", eglErr);
Chia-I Wub0c041b2017-11-09 11:36:33 -0800260 }
261 eglDestroySyncKHR(mEGLDisplay, sync);
262 } else {
263 ALOGW("error creating EGL fence: %#x", eglGetError());
264 }
265
266 return -1;
Riley Andrews9707f4d2014-10-23 16:17:04 -0700267}
268
Mathias Agopian3f844832013-08-07 21:24:32 -0700269void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
270 glClearColor(red, green, blue, alpha);
271 glClear(GL_COLOR_BUFFER_BIT);
272}
273
Chia-I Wub027f802017-11-29 14:00:52 -0800274void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700275 glScissor(left, bottom, right, top);
276 glEnable(GL_SCISSOR_TEST);
277}
278
279void RenderEngine::disableScissor() {
280 glDisable(GL_SCISSOR_TEST);
281}
282
283void RenderEngine::genTextures(size_t count, uint32_t* names) {
284 glGenTextures(count, names);
285}
286
287void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
288 glDeleteTextures(count, names);
289}
290
Mathias Agopiand5556842013-09-19 17:08:37 -0700291void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
292 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
293}
294
Mathias Agopian458197d2013-08-15 14:56:51 -0700295void RenderEngine::dump(String8& result) {
296 const GLExtensions& extensions(GLExtensions::getInstance());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800297
298 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
299 result.appendFormat("%s\n", extensions.getEGLExtensions());
300
Chia-I Wub027f802017-11-29 14:00:52 -0800301 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
302 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800303 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700304}
305
Mathias Agopian3f844832013-08-07 21:24:32 -0700306// ---------------------------------------------------------------------------
307
Chia-I Wueadbaa62017-11-09 11:26:15 -0800308RenderEngine::BindNativeBufferAsFramebuffer::BindNativeBufferAsFramebuffer(
Chia-I Wub027f802017-11-29 14:00:52 -0800309 RenderEngine& engine, ANativeWindowBuffer* buffer)
310 : mEngine(engine) {
311 mImage = eglCreateImageKHR(mEngine.mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
312 buffer, NULL);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800313 if (mImage == EGL_NO_IMAGE_KHR) {
314 mStatus = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
315 return;
316 }
317
318 mEngine.bindImageAsFramebuffer(mImage, &mTexName, &mFbName, &mStatus);
Mathias Agopian458197d2013-08-15 14:56:51 -0700319
Chia-I Wub027f802017-11-29 14:00:52 -0800320 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
321 mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700322}
323
Chia-I Wueadbaa62017-11-09 11:26:15 -0800324RenderEngine::BindNativeBufferAsFramebuffer::~BindNativeBufferAsFramebuffer() {
325 if (mImage == EGL_NO_IMAGE_KHR) {
326 return;
327 }
328
Mathias Agopian3f844832013-08-07 21:24:32 -0700329 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700330 mEngine.unbindFramebuffer(mTexName, mFbName);
Chia-I Wueadbaa62017-11-09 11:26:15 -0800331 eglDestroyImageKHR(mEngine.mEGLDisplay, mImage);
Mathias Agopian3f844832013-08-07 21:24:32 -0700332}
333
Chia-I Wueadbaa62017-11-09 11:26:15 -0800334status_t RenderEngine::BindNativeBufferAsFramebuffer::getStatus() const {
Mathias Agopian3f844832013-08-07 21:24:32 -0700335 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
336}
337
Mathias Agopian875d8e12013-06-07 15:35:48 -0700338// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800339
Chia-I Wub027f802017-11-29 14:00:52 -0800340static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
341 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800342 EGLint numConfigs = -1, n = 0;
343 eglGetConfigs(dpy, NULL, 0, &numConfigs);
344 EGLConfig* const configs = new EGLConfig[numConfigs];
345 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
346
347 if (n) {
348 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800349 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800350 EGLint value = 0;
351 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
352 if (wanted == value) {
353 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800354 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800355 return NO_ERROR;
356 }
357 }
358 } else {
359 // just pick the first one
360 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800361 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800362 return NO_ERROR;
363 }
364 }
Chia-I Wub027f802017-11-29 14:00:52 -0800365 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800366 return NAME_NOT_FOUND;
367}
368
369class EGLAttributeVector {
370 struct Attribute;
371 class Adder;
372 friend class Adder;
373 KeyedVector<Attribute, EGLint> mList;
374 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800375 Attribute() : v(0){};
376 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800377 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800378 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800379 // this places EGL_NONE at the end
380 EGLint lhs(v);
381 EGLint rhs(other.v);
382 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
383 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
384 return lhs < rhs;
385 }
386 };
387 class Adder {
388 friend class EGLAttributeVector;
389 EGLAttributeVector& v;
390 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800391 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
392
Jesse Hall05f8c702013-12-23 20:44:38 -0800393 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800394 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800395 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700396 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800397 }
398 }
Chia-I Wub027f802017-11-29 14:00:52 -0800399 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800400 };
Chia-I Wub027f802017-11-29 14:00:52 -0800401
Jesse Hall05f8c702013-12-23 20:44:38 -0800402public:
Chia-I Wub027f802017-11-29 14:00:52 -0800403 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800404 void remove(EGLint attribute) {
405 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700406 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800407 }
408 }
Chia-I Wub027f802017-11-29 14:00:52 -0800409 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
410 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800411 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800412 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800413};
414
Chia-I Wub027f802017-11-29 14:00:52 -0800415static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
416 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800417 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
418 // it is to be used with WIFI displays
419 status_t err;
420 EGLint wantedAttribute;
421 EGLint wantedAttributeValue;
422
423 EGLAttributeVector attribs;
424 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800425 attribs[EGL_RENDERABLE_TYPE] = renderableType;
426 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
427 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800428 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800429 attribs[EGL_RED_SIZE] = 8;
430 attribs[EGL_GREEN_SIZE] = 8;
431 attribs[EGL_BLUE_SIZE] = 8;
432 attribs[EGL_ALPHA_SIZE] = 8;
433 wantedAttribute = EGL_NONE;
434 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800435 } else {
436 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800437 wantedAttribute = EGL_NATIVE_VISUAL_ID;
438 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800439 }
440
Chia-I Wub027f802017-11-29 14:00:52 -0800441 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800442 if (err == NO_ERROR) {
443 EGLint caveat;
444 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
445 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
446 }
447
448 return err;
449}
450
Chia-I Wub027f802017-11-29 14:00:52 -0800451EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800452 status_t err;
453 EGLConfig config;
454
455 // First try to get an ES2 config
456 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
457 if (err != NO_ERROR) {
458 // If ES2 fails, try ES1
459 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
460 if (err != NO_ERROR) {
461 // still didn't work, probably because we're on the emulator...
462 // try a simplified query
463 ALOGW("no suitable EGLConfig found, trying a simpler query");
464 err = selectEGLConfig(display, format, 0, &config);
465 if (err != NO_ERROR) {
466 // this EGL is too lame for android
467 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
468 }
469 }
470 }
471
Steven Thomasd7f49c52017-07-26 18:48:28 -0700472 if (logConfig) {
473 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800474 EGLint r, g, b, a;
475 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700476 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800477 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700478 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
479 ALOGI("EGL information:");
480 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
481 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
482 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800483 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700484 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
485 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800486
487 return config;
488}
489
Dan Stoza4e637772016-07-28 13:31:51 -0700490void RenderEngine::primeCache() const {
491 // Getting the ProgramCache instance causes it to prime its shader cache,
492 // which is performed in its constructor
493 ProgramCache::getInstance();
494}
495
Jesse Hall05f8c702013-12-23 20:44:38 -0800496// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700497}; // namespace android
498// ---------------------------------------------------------------------------