blob: e3dbecc55dca4f0af47db7f6ca4d85047319ca75 [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
21#include "RenderEngine.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070022#include "GLES20RenderEngine.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070023#include "GLExtensions.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include "Mesh.h"
Mathias Agopian875d8e12013-06-07 15:35:48 -070025
Fabien Sanglardc93afd52017-03-13 13:02:42 -070026#include <vector>
27#include <SurfaceFlinger.h>
28
Jesse Hall19e87292013-12-23 21:02:15 -080029EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
30
Mathias Agopian875d8e12013-06-07 15:35:48 -070031// ---------------------------------------------------------------------------
32namespace android {
33// ---------------------------------------------------------------------------
34
Jesse Hall19e87292013-12-23 21:02:15 -080035static bool findExtension(const char* exts, const char* name) {
36 if (!exts)
37 return false;
38 size_t len = strlen(name);
Jesse Hall05f8c702013-12-23 20:44:38 -080039
Jesse Hall19e87292013-12-23 21:02:15 -080040 const char* pos = exts;
41 while ((pos = strstr(pos, name)) != NULL) {
42 if (pos[len] == '\0' || pos[len] == ' ')
43 return true;
44 pos += len;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070045 }
46
Jesse Hall19e87292013-12-23 21:02:15 -080047 return false;
48}
49
50RenderEngine* RenderEngine::create(EGLDisplay display, int hwcFormat) {
51 // EGL_ANDROIDX_no_config_context is an experimental extension with no
52 // written specification. It will be replaced by something more formal.
53 // SurfaceFlinger is using it to allow a single EGLContext to render to
54 // both a 16-bit primary display framebuffer and a 32-bit virtual display
55 // framebuffer.
56 //
57 // The code assumes that ES2 or later is available if this extension is
58 // supported.
59 EGLConfig config = EGL_NO_CONFIG;
60 if (!findExtension(
61 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS),
62 "EGL_ANDROIDX_no_config_context")) {
63 config = chooseEglConfig(display, hwcFormat);
64 }
65
66 EGLint renderableType = 0;
67 if (config == EGL_NO_CONFIG) {
68 renderableType = EGL_OPENGL_ES2_BIT;
69 } else if (!eglGetConfigAttrib(display, config,
70 EGL_RENDERABLE_TYPE, &renderableType)) {
71 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
72 }
73 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070074 if (renderableType & EGL_OPENGL_ES2_BIT) {
75 contextClientVersion = 2;
76 } else if (renderableType & EGL_OPENGL_ES_BIT) {
77 contextClientVersion = 1;
78 } else {
79 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
80 }
81
Fabien Sanglardc93afd52017-03-13 13:02:42 -070082 std::vector<EGLint> contextAttributes;
83 contextAttributes.reserve(6);
84 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
85 contextAttributes.push_back(contextClientVersion);
Mathias Agopian875d8e12013-06-07 15:35:48 -070086#ifdef EGL_IMG_context_priority
Fabien Sanglardc93afd52017-03-13 13:02:42 -070087 if (SurfaceFlinger::useContextPriority) {
88 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
89 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
90 }
Mathias Agopian875d8e12013-06-07 15:35:48 -070091#endif
Fabien Sanglardc93afd52017-03-13 13:02:42 -070092 contextAttributes.push_back(EGL_NONE);
93 contextAttributes.push_back(EGL_NONE);
94
95 EGLContext ctxt = eglCreateContext(display, config, NULL,
96 contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070097
98 // if can't create a GL context, we can only abort.
99 LOG_ALWAYS_FATAL_IF(ctxt==EGL_NO_CONTEXT, "EGLContext creation failed");
100
101
102 // now figure out what version of GL did we actually get
103 // NOTE: a dummy surface is not needed if KHR_create_context is supported
104
Jesse Hall19e87292013-12-23 21:02:15 -0800105 EGLConfig dummyConfig = config;
106 if (dummyConfig == EGL_NO_CONFIG) {
107 dummyConfig = chooseEglConfig(display, hwcFormat);
108 }
Mathias Agopian875d8e12013-06-07 15:35:48 -0700109 EGLint attribs[] = { EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE };
Jesse Hall19e87292013-12-23 21:02:15 -0800110 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700111 LOG_ALWAYS_FATAL_IF(dummy==EGL_NO_SURFACE, "can't create dummy pbuffer");
112 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
113 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
114
115 GLExtensions& extensions(GLExtensions::getInstance());
116 extensions.initWithGLStrings(
117 glGetString(GL_VENDOR),
118 glGetString(GL_RENDERER),
119 glGetString(GL_VERSION),
120 glGetString(GL_EXTENSIONS));
121
122 GlesVersion version = parseGlesVersion( extensions.getVersion() );
123
124 // initialize the renderer while GL is current
125
126 RenderEngine* engine = NULL;
127 switch (version) {
128 case GLES_VERSION_1_0:
Mathias Agopian875d8e12013-06-07 15:35:48 -0700129 case GLES_VERSION_1_1:
Fabien Sanglardefb93452016-10-04 14:02:11 -0700130 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700131 break;
132 case GLES_VERSION_2_0:
133 case GLES_VERSION_3_0:
Mathias Agopian3f844832013-08-07 21:24:32 -0700134 engine = new GLES20RenderEngine();
Mathias Agopian875d8e12013-06-07 15:35:48 -0700135 break;
136 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800137 engine->setEGLHandles(config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700138
139 ALOGI("OpenGL ES informations:");
140 ALOGI("vendor : %s", extensions.getVendor());
141 ALOGI("renderer : %s", extensions.getRenderer());
142 ALOGI("version : %s", extensions.getVersion());
143 ALOGI("extensions: %s", extensions.getExtension());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700144 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
145 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700146
147 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
148 eglDestroySurface(display, dummy);
149
150 return engine;
151}
152
Pablo Ceballos53390e12015-08-04 11:25:59 -0700153RenderEngine::RenderEngine() : mEGLConfig(NULL), mEGLContext(EGL_NO_CONTEXT) {
Mathias Agopian875d8e12013-06-07 15:35:48 -0700154}
155
156RenderEngine::~RenderEngine() {
157}
158
Jesse Hall05f8c702013-12-23 20:44:38 -0800159void RenderEngine::setEGLHandles(EGLConfig config, EGLContext ctxt) {
160 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700161 mEGLContext = ctxt;
162}
163
Jesse Hall05f8c702013-12-23 20:44:38 -0800164EGLContext RenderEngine::getEGLConfig() const {
165 return mEGLConfig;
166}
167
Mathias Agopian875d8e12013-06-07 15:35:48 -0700168EGLContext RenderEngine::getEGLContext() const {
169 return mEGLContext;
170}
171
172void RenderEngine::checkErrors() const {
173 do {
174 // there could be more than one error flag
175 GLenum error = glGetError();
176 if (error == GL_NO_ERROR)
177 break;
178 ALOGE("GL error 0x%04x", int(error));
179 } while (true);
180}
181
182RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
183 int major, minor;
184 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
185 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
186 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
187 return GLES_VERSION_1_0;
188 }
189 }
190
191 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
192 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
193 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
194 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
195
196 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
197 return GLES_VERSION_1_0;
198}
199
Mathias Agopian3f844832013-08-07 21:24:32 -0700200void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height,
201 float red, float green, float blue, float alpha) {
202 size_t c;
203 Rect const* r = region.getArray(&c);
204 Mesh mesh(Mesh::TRIANGLES, c*6, 2);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700205 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
Mathias Agopian3f844832013-08-07 21:24:32 -0700206 for (size_t i=0 ; i<c ; i++, r++) {
Mathias Agopian5cdc8992013-08-13 20:51:23 -0700207 position[i*6 + 0].x = r->left;
208 position[i*6 + 0].y = height - r->top;
209 position[i*6 + 1].x = r->left;
210 position[i*6 + 1].y = height - r->bottom;
211 position[i*6 + 2].x = r->right;
212 position[i*6 + 2].y = height - r->bottom;
213 position[i*6 + 3].x = r->left;
214 position[i*6 + 3].y = height - r->top;
215 position[i*6 + 4].x = r->right;
216 position[i*6 + 4].y = height - r->bottom;
217 position[i*6 + 5].x = r->right;
218 position[i*6 + 5].y = height - r->top;
Mathias Agopian3f844832013-08-07 21:24:32 -0700219 }
Mathias Agopian19733a32013-08-28 18:13:56 -0700220 setupFillWithColor(red, green, blue, alpha);
221 drawMesh(mesh);
Mathias Agopian3f844832013-08-07 21:24:32 -0700222}
223
Riley Andrews9707f4d2014-10-23 16:17:04 -0700224void RenderEngine::flush() {
225 glFlush();
226}
227
Mathias Agopian3f844832013-08-07 21:24:32 -0700228void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
229 glClearColor(red, green, blue, alpha);
230 glClear(GL_COLOR_BUFFER_BIT);
231}
232
233void RenderEngine::setScissor(
234 uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
235 glScissor(left, bottom, right, top);
236 glEnable(GL_SCISSOR_TEST);
237}
238
239void RenderEngine::disableScissor() {
240 glDisable(GL_SCISSOR_TEST);
241}
242
243void RenderEngine::genTextures(size_t count, uint32_t* names) {
244 glGenTextures(count, names);
245}
246
247void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
248 glDeleteTextures(count, names);
249}
250
Mathias Agopiand5556842013-09-19 17:08:37 -0700251void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
252 glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
253}
254
Mathias Agopian458197d2013-08-15 14:56:51 -0700255void RenderEngine::dump(String8& result) {
256 const GLExtensions& extensions(GLExtensions::getInstance());
257 result.appendFormat("GLES: %s, %s, %s\n",
258 extensions.getVendor(),
259 extensions.getRenderer(),
260 extensions.getVersion());
261 result.appendFormat("%s\n", extensions.getExtension());
262}
263
Mathias Agopian3f844832013-08-07 21:24:32 -0700264// ---------------------------------------------------------------------------
265
266RenderEngine::BindImageAsFramebuffer::BindImageAsFramebuffer(
267 RenderEngine& engine, EGLImageKHR image) : mEngine(engine)
268{
Mathias Agopian458197d2013-08-15 14:56:51 -0700269 mEngine.bindImageAsFramebuffer(image, &mTexName, &mFbName, &mStatus);
270
Mathias Agopian3f844832013-08-07 21:24:32 -0700271 ALOGE_IF(mStatus != GL_FRAMEBUFFER_COMPLETE_OES,
272 "glCheckFramebufferStatusOES error %d", mStatus);
Mathias Agopian3f844832013-08-07 21:24:32 -0700273}
274
275RenderEngine::BindImageAsFramebuffer::~BindImageAsFramebuffer() {
276 // back to main framebuffer
Mathias Agopian458197d2013-08-15 14:56:51 -0700277 mEngine.unbindFramebuffer(mTexName, mFbName);
Mathias Agopian3f844832013-08-07 21:24:32 -0700278}
279
280status_t RenderEngine::BindImageAsFramebuffer::getStatus() const {
281 return mStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
282}
283
Mathias Agopian875d8e12013-06-07 15:35:48 -0700284// ---------------------------------------------------------------------------
Jesse Hall05f8c702013-12-23 20:44:38 -0800285
286static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs,
287 EGLint attribute, EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800288 EGLint numConfigs = -1, n = 0;
289 eglGetConfigs(dpy, NULL, 0, &numConfigs);
290 EGLConfig* const configs = new EGLConfig[numConfigs];
291 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
292
293 if (n) {
294 if (attribute != EGL_NONE) {
295 for (int i=0 ; i<n ; i++) {
296 EGLint value = 0;
297 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
298 if (wanted == value) {
299 *outConfig = configs[i];
300 delete [] configs;
301 return NO_ERROR;
302 }
303 }
304 } else {
305 // just pick the first one
306 *outConfig = configs[0];
307 delete [] configs;
308 return NO_ERROR;
309 }
310 }
311 delete [] configs;
312 return NAME_NOT_FOUND;
313}
314
315class EGLAttributeVector {
316 struct Attribute;
317 class Adder;
318 friend class Adder;
319 KeyedVector<Attribute, EGLint> mList;
320 struct Attribute {
Pablo Ceballos53390e12015-08-04 11:25:59 -0700321 Attribute() : v(0) {};
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700322 explicit Attribute(EGLint v) : v(v) { }
Jesse Hall05f8c702013-12-23 20:44:38 -0800323 EGLint v;
324 bool operator < (const Attribute& other) const {
325 // this places EGL_NONE at the end
326 EGLint lhs(v);
327 EGLint rhs(other.v);
328 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
329 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
330 return lhs < rhs;
331 }
332 };
333 class Adder {
334 friend class EGLAttributeVector;
335 EGLAttributeVector& v;
336 EGLint attribute;
337 Adder(EGLAttributeVector& v, EGLint attribute)
338 : v(v), attribute(attribute) {
339 }
340 public:
341 void operator = (EGLint value) {
342 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700343 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800344 }
345 }
346 operator EGLint () const { return v.mList[attribute]; }
347 };
348public:
349 EGLAttributeVector() {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700350 mList.add(Attribute(EGL_NONE), EGL_NONE);
Jesse Hall05f8c702013-12-23 20:44:38 -0800351 }
352 void remove(EGLint attribute) {
353 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700354 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800355 }
356 }
357 Adder operator [] (EGLint attribute) {
358 return Adder(*this, attribute);
359 }
360 EGLint operator [] (EGLint attribute) const {
361 return mList[attribute];
362 }
363 // cast-operator to (EGLint const*)
364 operator EGLint const* () const { return &mList.keyAt(0).v; }
365};
366
367
368static status_t selectEGLConfig(EGLDisplay display, EGLint format,
369 EGLint renderableType, EGLConfig* config) {
370 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
371 // it is to be used with WIFI displays
372 status_t err;
373 EGLint wantedAttribute;
374 EGLint wantedAttributeValue;
375
376 EGLAttributeVector attribs;
377 if (renderableType) {
378 attribs[EGL_RENDERABLE_TYPE] = renderableType;
379 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
380 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT|EGL_PBUFFER_BIT;
381 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
382 attribs[EGL_RED_SIZE] = 8;
383 attribs[EGL_GREEN_SIZE] = 8;
384 attribs[EGL_BLUE_SIZE] = 8;
385 wantedAttribute = EGL_NONE;
386 wantedAttributeValue = EGL_NONE;
387 } else {
388 // if no renderable type specified, fallback to a simplified query
389 wantedAttribute = EGL_NATIVE_VISUAL_ID;
390 wantedAttributeValue = format;
391 }
392
393 err = selectConfigForAttribute(display, attribs,
394 wantedAttribute, wantedAttributeValue, config);
395 if (err == NO_ERROR) {
396 EGLint caveat;
397 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
398 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
399 }
400
401 return err;
402}
403
404EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format) {
405 status_t err;
406 EGLConfig config;
407
408 // First try to get an ES2 config
409 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
410 if (err != NO_ERROR) {
411 // If ES2 fails, try ES1
412 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
413 if (err != NO_ERROR) {
414 // still didn't work, probably because we're on the emulator...
415 // try a simplified query
416 ALOGW("no suitable EGLConfig found, trying a simpler query");
417 err = selectEGLConfig(display, format, 0, &config);
418 if (err != NO_ERROR) {
419 // this EGL is too lame for android
420 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
421 }
422 }
423 }
424
425 // print some debugging info
426 EGLint r,g,b,a;
427 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
428 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
429 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
430 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
431 ALOGI("EGL information:");
432 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
433 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
434 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
435 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS)?:"Not Supported");
436 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
437
438 return config;
439}
440
Dan Stoza4e637772016-07-28 13:31:51 -0700441
442void RenderEngine::primeCache() const {
443 // Getting the ProgramCache instance causes it to prime its shader cache,
444 // which is performed in its constructor
445 ProgramCache::getInstance();
446}
447
Jesse Hall05f8c702013-12-23 20:44:38 -0800448// ---------------------------------------------------------------------------
Mathias Agopian875d8e12013-06-07 15:35:48 -0700449}; // namespace android
450// ---------------------------------------------------------------------------