blob: f00367f91c17a9e8b0b2ca4aeeb0463243ab3569 [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
Peiyong Lincbc184f2018-08-22 13:24:10 -070017#include <renderengine/RenderEngine.h>
Fabien Sanglardc93afd52017-03-13 13:02:42 -070018
Peiyong Lin833074a2018-08-28 11:53:54 -070019#include <vector>
20
Peiyong Lincbc184f2018-08-22 13:24:10 -070021#include <log/log.h>
Lloyd Pique63f9dbf2018-06-21 13:13:07 -070022#include <private/gui/SyncFeatures.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070023#include <renderengine/Image.h>
24#include <renderengine/Mesh.h>
25#include <renderengine/Surface.h>
26#include <ui/Rect.h>
27#include <ui/Region.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070028#include <utils/KeyedVector.h>
29#include "gl/GLES20RenderEngine.h"
30#include "gl/GLExtensions.h"
31#include "gl/ProgramCache.h"
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010032
Peiyong Lin833074a2018-08-28 11:53:54 -070033using namespace android::renderengine::gl;
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010034
Jiyong Park00b15b82017-08-10 20:30:56 +090035extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
Jesse Hall19e87292013-12-23 21:02:15 -080036
Mathias Agopian875d8e12013-06-07 15:35:48 -070037namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070038namespace renderengine {
Mathias Agopian875d8e12013-06-07 15:35:48 -070039
Lloyd Pique144e1162017-12-20 16:44:52 -080040RenderEngine::~RenderEngine() = default;
41
42namespace impl {
43
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080044std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
45 // initialize EGL for the default display
46 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
Peiyong Lin566a3b42018-01-09 18:22:43 -080047 if (!eglInitialize(display, nullptr, nullptr)) {
Chia-I Wud4d9c6f2017-11-09 16:55:31 -080048 LOG_ALWAYS_FATAL("failed to initialize EGL");
49 }
50
Peiyong Lin566a3b42018-01-09 18:22:43 -080051 GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -080052 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
53 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
54
Jesse Hall19e87292013-12-23 21:02:15 -080055 // The code assumes that ES2 or later is available if this extension is
56 // supported.
57 EGLConfig config = EGL_NO_CONFIG;
Chia-I Wu767d7c92017-11-30 13:22:48 -080058 if (!extensions.hasNoConfigContext()) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070059 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -080060 }
61
62 EGLint renderableType = 0;
63 if (config == EGL_NO_CONFIG) {
64 renderableType = EGL_OPENGL_ES2_BIT;
Chia-I Wub027f802017-11-29 14:00:52 -080065 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
Jesse Hall19e87292013-12-23 21:02:15 -080066 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
67 }
68 EGLint contextClientVersion = 0;
Mathias Agopian2185f8b2013-09-18 16:20:26 -070069 if (renderableType & EGL_OPENGL_ES2_BIT) {
70 contextClientVersion = 2;
71 } else if (renderableType & EGL_OPENGL_ES_BIT) {
72 contextClientVersion = 1;
73 } else {
74 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
75 }
76
Fabien Sanglardc93afd52017-03-13 13:02:42 -070077 std::vector<EGLint> contextAttributes;
78 contextAttributes.reserve(6);
79 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
80 contextAttributes.push_back(contextClientVersion);
Peiyong Linb3839ad2018-09-05 15:37:19 -070081 bool useContextPriority = extensions.hasContextPriority() &&
82 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Jorim Jaggi5b15ef62018-01-17 18:31:39 +010083 if (useContextPriority) {
Fabien Sanglardc93afd52017-03-13 13:02:42 -070084 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
85 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
86 }
Fabien Sanglardc93afd52017-03-13 13:02:42 -070087 contextAttributes.push_back(EGL_NONE);
88
Peiyong Lin566a3b42018-01-09 18:22:43 -080089 EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
Mathias Agopian875d8e12013-06-07 15:35:48 -070090
91 // if can't create a GL context, we can only abort.
Chia-I Wub027f802017-11-29 14:00:52 -080092 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
Mathias Agopian875d8e12013-06-07 15:35:48 -070093
94 // now figure out what version of GL did we actually get
95 // NOTE: a dummy surface is not needed if KHR_create_context is supported
96
Jesse Hall19e87292013-12-23 21:02:15 -080097 EGLConfig dummyConfig = config;
98 if (dummyConfig == EGL_NO_CONFIG) {
Steven Thomasd7f49c52017-07-26 18:48:28 -070099 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
Jesse Hall19e87292013-12-23 21:02:15 -0800100 }
Chia-I Wub027f802017-11-29 14:00:52 -0800101 EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
Jesse Hall19e87292013-12-23 21:02:15 -0800102 EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
Chia-I Wub027f802017-11-29 14:00:52 -0800103 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Mathias Agopian875d8e12013-06-07 15:35:48 -0700104 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
105 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
106
Chia-I Wub027f802017-11-29 14:00:52 -0800107 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
108 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
Mathias Agopian875d8e12013-06-07 15:35:48 -0700109
Chia-I Wub027f802017-11-29 14:00:52 -0800110 GlesVersion version = parseGlesVersion(extensions.getVersion());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700111
112 // initialize the renderer while GL is current
113
Chia-I Wub2c76242017-11-09 17:17:07 -0800114 std::unique_ptr<RenderEngine> engine;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700115 switch (version) {
Chia-I Wub027f802017-11-29 14:00:52 -0800116 case GLES_VERSION_1_0:
117 case GLES_VERSION_1_1:
118 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
119 break;
120 case GLES_VERSION_2_0:
121 case GLES_VERSION_3_0:
122 engine = std::make_unique<GLES20RenderEngine>(featureFlags);
123 break;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700124 }
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800125 engine->setEGLHandles(display, config, ctxt);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700126
127 ALOGI("OpenGL ES informations:");
128 ALOGI("vendor : %s", extensions.getVendor());
129 ALOGI("renderer : %s", extensions.getRenderer());
130 ALOGI("version : %s", extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800131 ALOGI("extensions: %s", extensions.getExtensions());
Michael Lentine9ae79d82014-07-30 16:42:12 -0700132 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
133 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
Mathias Agopian875d8e12013-06-07 15:35:48 -0700134
135 eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
136 eglDestroySurface(display, dummy);
137
138 return engine;
139}
140
Chia-I Wu93e14df2018-06-04 10:10:17 -0700141RenderEngine::RenderEngine(uint32_t featureFlags)
142 : mEGLDisplay(EGL_NO_DISPLAY),
143 mEGLConfig(nullptr),
144 mEGLContext(EGL_NO_CONTEXT),
145 mFeatureFlags(featureFlags) {}
Mathias Agopian875d8e12013-06-07 15:35:48 -0700146
147RenderEngine::~RenderEngine() {
Chia-I Wub01450b2017-11-09 16:55:31 -0800148 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
149 eglTerminate(mEGLDisplay);
Mathias Agopian875d8e12013-06-07 15:35:48 -0700150}
151
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800152void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
153 mEGLDisplay = display;
Jesse Hall05f8c702013-12-23 20:44:38 -0800154 mEGLConfig = config;
Mathias Agopian875d8e12013-06-07 15:35:48 -0700155 mEGLContext = ctxt;
156}
157
Chia-I Wu2b6386e2017-11-09 13:03:17 -0800158EGLDisplay RenderEngine::getEGLDisplay() const {
159 return mEGLDisplay;
160}
161
162EGLConfig RenderEngine::getEGLConfig() const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800163 return mEGLConfig;
164}
165
Lloyd Pique63f9dbf2018-06-21 13:13:07 -0700166bool RenderEngine::useNativeFenceSync() const {
167 return SyncFeatures::getInstance().useNativeFenceSync();
168}
169
170bool RenderEngine::useWaitSync() const {
171 return SyncFeatures::getInstance().useWaitSync();
172}
173
Mathias Agopian875d8e12013-06-07 15:35:48 -0700174RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
175 int major, minor;
176 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
177 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
178 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
179 return GLES_VERSION_1_0;
180 }
181 }
182
183 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
184 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
185 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
186 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
187
188 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
189 return GLES_VERSION_1_0;
190}
191
Mathias Agopian458197d2013-08-15 14:56:51 -0700192void RenderEngine::dump(String8& result) {
Peiyong Lin566a3b42018-01-09 18:22:43 -0800193 const GLExtensions& extensions = GLExtensions::getInstance();
Chia-I Wu767d7c92017-11-30 13:22:48 -0800194
195 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
196 result.appendFormat("%s\n", extensions.getEGLExtensions());
197
Chia-I Wub027f802017-11-29 14:00:52 -0800198 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
199 extensions.getVersion());
Chia-I Wu767d7c92017-11-30 13:22:48 -0800200 result.appendFormat("%s\n", extensions.getExtensions());
Mathias Agopian458197d2013-08-15 14:56:51 -0700201}
202
Chia-I Wub027f802017-11-29 14:00:52 -0800203static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
204 EGLint wanted, EGLConfig* outConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800205 EGLint numConfigs = -1, n = 0;
Peiyong Lin566a3b42018-01-09 18:22:43 -0800206 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Jesse Hall05f8c702013-12-23 20:44:38 -0800207 EGLConfig* const configs = new EGLConfig[numConfigs];
208 eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
209
210 if (n) {
211 if (attribute != EGL_NONE) {
Chia-I Wub027f802017-11-29 14:00:52 -0800212 for (int i = 0; i < n; i++) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800213 EGLint value = 0;
214 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
215 if (wanted == value) {
216 *outConfig = configs[i];
Chia-I Wub027f802017-11-29 14:00:52 -0800217 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800218 return NO_ERROR;
219 }
220 }
221 } else {
222 // just pick the first one
223 *outConfig = configs[0];
Chia-I Wub027f802017-11-29 14:00:52 -0800224 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800225 return NO_ERROR;
226 }
227 }
Chia-I Wub027f802017-11-29 14:00:52 -0800228 delete[] configs;
Jesse Hall05f8c702013-12-23 20:44:38 -0800229 return NAME_NOT_FOUND;
230}
231
232class EGLAttributeVector {
233 struct Attribute;
234 class Adder;
235 friend class Adder;
236 KeyedVector<Attribute, EGLint> mList;
237 struct Attribute {
Chia-I Wub027f802017-11-29 14:00:52 -0800238 Attribute() : v(0){};
239 explicit Attribute(EGLint v) : v(v) {}
Jesse Hall05f8c702013-12-23 20:44:38 -0800240 EGLint v;
Chia-I Wub027f802017-11-29 14:00:52 -0800241 bool operator<(const Attribute& other) const {
Jesse Hall05f8c702013-12-23 20:44:38 -0800242 // this places EGL_NONE at the end
243 EGLint lhs(v);
244 EGLint rhs(other.v);
245 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
246 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
247 return lhs < rhs;
248 }
249 };
250 class Adder {
251 friend class EGLAttributeVector;
252 EGLAttributeVector& v;
253 EGLint attribute;
Chia-I Wub027f802017-11-29 14:00:52 -0800254 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
255
Jesse Hall05f8c702013-12-23 20:44:38 -0800256 public:
Chia-I Wub027f802017-11-29 14:00:52 -0800257 void operator=(EGLint value) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800258 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700259 v.mList.add(Attribute(attribute), value);
Jesse Hall05f8c702013-12-23 20:44:38 -0800260 }
261 }
Chia-I Wub027f802017-11-29 14:00:52 -0800262 operator EGLint() const { return v.mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800263 };
Chia-I Wub027f802017-11-29 14:00:52 -0800264
Jesse Hall05f8c702013-12-23 20:44:38 -0800265public:
Chia-I Wub027f802017-11-29 14:00:52 -0800266 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
Jesse Hall05f8c702013-12-23 20:44:38 -0800267 void remove(EGLint attribute) {
268 if (attribute != EGL_NONE) {
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -0700269 mList.removeItem(Attribute(attribute));
Jesse Hall05f8c702013-12-23 20:44:38 -0800270 }
271 }
Chia-I Wub027f802017-11-29 14:00:52 -0800272 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
273 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800274 // cast-operator to (EGLint const*)
Chia-I Wub027f802017-11-29 14:00:52 -0800275 operator EGLint const*() const { return &mList.keyAt(0).v; }
Jesse Hall05f8c702013-12-23 20:44:38 -0800276};
277
Chia-I Wub027f802017-11-29 14:00:52 -0800278static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
279 EGLConfig* config) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800280 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
281 // it is to be used with WIFI displays
282 status_t err;
283 EGLint wantedAttribute;
284 EGLint wantedAttributeValue;
285
286 EGLAttributeVector attribs;
287 if (renderableType) {
Chia-I Wub027f802017-11-29 14:00:52 -0800288 attribs[EGL_RENDERABLE_TYPE] = renderableType;
289 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
290 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
Jesse Hall05f8c702013-12-23 20:44:38 -0800291 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
Chia-I Wub027f802017-11-29 14:00:52 -0800292 attribs[EGL_RED_SIZE] = 8;
293 attribs[EGL_GREEN_SIZE] = 8;
294 attribs[EGL_BLUE_SIZE] = 8;
295 attribs[EGL_ALPHA_SIZE] = 8;
296 wantedAttribute = EGL_NONE;
297 wantedAttributeValue = EGL_NONE;
Jesse Hall05f8c702013-12-23 20:44:38 -0800298 } else {
299 // if no renderable type specified, fallback to a simplified query
Chia-I Wub027f802017-11-29 14:00:52 -0800300 wantedAttribute = EGL_NATIVE_VISUAL_ID;
301 wantedAttributeValue = format;
Jesse Hall05f8c702013-12-23 20:44:38 -0800302 }
303
Chia-I Wub027f802017-11-29 14:00:52 -0800304 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
Jesse Hall05f8c702013-12-23 20:44:38 -0800305 if (err == NO_ERROR) {
306 EGLint caveat;
307 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
308 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
309 }
310
311 return err;
312}
313
Chia-I Wub027f802017-11-29 14:00:52 -0800314EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Jesse Hall05f8c702013-12-23 20:44:38 -0800315 status_t err;
316 EGLConfig config;
317
318 // First try to get an ES2 config
319 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
320 if (err != NO_ERROR) {
321 // If ES2 fails, try ES1
322 err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
323 if (err != NO_ERROR) {
324 // still didn't work, probably because we're on the emulator...
325 // try a simplified query
326 ALOGW("no suitable EGLConfig found, trying a simpler query");
327 err = selectEGLConfig(display, format, 0, &config);
328 if (err != NO_ERROR) {
329 // this EGL is too lame for android
330 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
331 }
332 }
333 }
334
Steven Thomasd7f49c52017-07-26 18:48:28 -0700335 if (logConfig) {
336 // print some debugging info
Chia-I Wub027f802017-11-29 14:00:52 -0800337 EGLint r, g, b, a;
338 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700339 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
Chia-I Wub027f802017-11-29 14:00:52 -0800340 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
Steven Thomasd7f49c52017-07-26 18:48:28 -0700341 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
342 ALOGI("EGL information:");
343 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
344 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
345 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
Chia-I Wub027f802017-11-29 14:00:52 -0800346 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
Steven Thomasd7f49c52017-07-26 18:48:28 -0700347 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
348 }
Jesse Hall05f8c702013-12-23 20:44:38 -0800349
350 return config;
351}
352
Peiyong Lin833074a2018-08-28 11:53:54 -0700353} // namespace impl
354} // namespace renderengine
355} // namespace android