blob: 15bdcbcc984ac7466b77a86e8fa8dac1de36d85a [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -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
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060017//#define LOG_NDEBUG 0
18#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
Mathias Agopian3f844832013-08-07 21:24:32 -070020#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Peiyong Lin7e219eb2018-12-03 05:40:42 -080022#include "GLESRenderEngine.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070023
24#include <math.h>
25#include <fstream>
26#include <sstream>
Peiyong Lincbc184f2018-08-22 13:24:10 -070027
Mathias Agopian3f844832013-08-07 21:24:32 -070028#include <GLES2/gl2.h>
Mathias Agopian458197d2013-08-15 14:56:51 -070029#include <GLES2/gl2ext.h>
Yiwei Zhang5434a782018-12-05 18:06:32 -080030#include <android-base/stringprintf.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070031#include <cutils/compiler.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <renderengine/Mesh.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070033#include <renderengine/Texture.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070034#include <renderengine/private/Description.h>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060035#include <ui/ColorSpace.h>
36#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070037#include <ui/Rect.h>
Peiyong Lin60bedb52018-09-05 10:47:31 -070038#include <ui/Region.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070039#include <utils/KeyedVector.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070040#include <utils/Trace.h>
Peiyong Linf1bada92018-08-29 09:39:31 -070041#include "GLExtensions.h"
Peiyong Line5a9a7f2018-08-30 15:32:13 -070042#include "GLFramebuffer.h"
Peiyong Linf1bada92018-08-29 09:39:31 -070043#include "GLImage.h"
Peiyong Lin833074a2018-08-28 11:53:54 -070044#include "Program.h"
45#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070046
Peiyong Linf11f39b2018-09-05 14:37:41 -070047extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
48
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060049bool checkGlError(const char* op, int lineNumber) {
50 bool errorFound = false;
51 GLint error = glGetError();
52 while (error != GL_NO_ERROR) {
53 errorFound = true;
54 error = glGetError();
55 ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
56 }
57 return errorFound;
58}
59
Courtney Goeltzenleuchter4f20f9c2017-04-06 08:18:34 -060060static constexpr bool outputDebugPPMs = false;
61
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060062void writePPM(const char* basename, GLuint width, GLuint height) {
63 ALOGV("writePPM #%s: %d x %d", basename, width, height);
64
65 std::vector<GLubyte> pixels(width * height * 4);
66 std::vector<GLubyte> outBuffer(width * height * 3);
67
68 // TODO(courtneygo): We can now have float formats, need
69 // to remove this code or update to support.
70 // Make returned pixels fit in uint32_t, one byte per component
71 glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
72 if (checkGlError(__FUNCTION__, __LINE__)) {
73 return;
74 }
75
76 std::string filename(basename);
77 filename.append(".ppm");
78 std::ofstream file(filename.c_str(), std::ios::binary);
79 if (!file.is_open()) {
80 ALOGE("Unable to open file: %s", filename.c_str());
81 ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
82 "surfaceflinger to write debug images");
83 return;
84 }
85
86 file << "P6\n";
87 file << width << "\n";
88 file << height << "\n";
89 file << 255 << "\n";
90
91 auto ptr = reinterpret_cast<char*>(pixels.data());
92 auto outPtr = reinterpret_cast<char*>(outBuffer.data());
93 for (int y = height - 1; y >= 0; y--) {
94 char* data = ptr + y * width * sizeof(uint32_t);
95
96 for (GLuint x = 0; x < width; x++) {
97 // Only copy R, G and B components
98 outPtr[0] = data[0];
99 outPtr[1] = data[1];
100 outPtr[2] = data[2];
101 data += sizeof(uint32_t);
102 outPtr += 3;
103 }
104 }
105 file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
106}
107
Mathias Agopian3f844832013-08-07 21:24:32 -0700108namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -0700109namespace renderengine {
110namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -0700111
Yiwei Zhang5434a782018-12-05 18:06:32 -0800112using base::StringAppendF;
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700113using ui::Dataspace;
114
Peiyong Linf11f39b2018-09-05 14:37:41 -0700115static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
116 EGLint wanted, EGLConfig* outConfig) {
117 EGLint numConfigs = -1, n = 0;
118 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700119 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
120 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
121 configs.resize(n);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700122
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700123 if (!configs.empty()) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700124 if (attribute != EGL_NONE) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700125 for (EGLConfig config : configs) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700126 EGLint value = 0;
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700127 eglGetConfigAttrib(dpy, config, attribute, &value);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700128 if (wanted == value) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700129 *outConfig = config;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700130 return NO_ERROR;
131 }
132 }
133 } else {
134 // just pick the first one
135 *outConfig = configs[0];
Peiyong Linf11f39b2018-09-05 14:37:41 -0700136 return NO_ERROR;
137 }
138 }
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700139
Peiyong Linf11f39b2018-09-05 14:37:41 -0700140 return NAME_NOT_FOUND;
141}
142
143class EGLAttributeVector {
144 struct Attribute;
145 class Adder;
146 friend class Adder;
147 KeyedVector<Attribute, EGLint> mList;
148 struct Attribute {
149 Attribute() : v(0){};
150 explicit Attribute(EGLint v) : v(v) {}
151 EGLint v;
152 bool operator<(const Attribute& other) const {
153 // this places EGL_NONE at the end
154 EGLint lhs(v);
155 EGLint rhs(other.v);
156 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
157 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
158 return lhs < rhs;
159 }
160 };
161 class Adder {
162 friend class EGLAttributeVector;
163 EGLAttributeVector& v;
164 EGLint attribute;
165 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
166
167 public:
168 void operator=(EGLint value) {
169 if (attribute != EGL_NONE) {
170 v.mList.add(Attribute(attribute), value);
171 }
172 }
173 operator EGLint() const { return v.mList[attribute]; }
174 };
175
176public:
177 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
178 void remove(EGLint attribute) {
179 if (attribute != EGL_NONE) {
180 mList.removeItem(Attribute(attribute));
181 }
182 }
183 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
184 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
185 // cast-operator to (EGLint const*)
186 operator EGLint const*() const { return &mList.keyAt(0).v; }
187};
188
189static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
190 EGLConfig* config) {
191 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
192 // it is to be used with WIFI displays
193 status_t err;
194 EGLint wantedAttribute;
195 EGLint wantedAttributeValue;
196
197 EGLAttributeVector attribs;
198 if (renderableType) {
199 attribs[EGL_RENDERABLE_TYPE] = renderableType;
200 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
201 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
202 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
203 attribs[EGL_RED_SIZE] = 8;
204 attribs[EGL_GREEN_SIZE] = 8;
205 attribs[EGL_BLUE_SIZE] = 8;
206 attribs[EGL_ALPHA_SIZE] = 8;
207 wantedAttribute = EGL_NONE;
208 wantedAttributeValue = EGL_NONE;
209 } else {
210 // if no renderable type specified, fallback to a simplified query
211 wantedAttribute = EGL_NATIVE_VISUAL_ID;
212 wantedAttributeValue = format;
213 }
214
215 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
216 if (err == NO_ERROR) {
217 EGLint caveat;
218 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
219 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
220 }
221
222 return err;
223}
224
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800225std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32_t featureFlags) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700226 // initialize EGL for the default display
227 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
228 if (!eglInitialize(display, nullptr, nullptr)) {
229 LOG_ALWAYS_FATAL("failed to initialize EGL");
230 }
231
232 GLExtensions& extensions = GLExtensions::getInstance();
233 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
234 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
235
236 // The code assumes that ES2 or later is available if this extension is
237 // supported.
238 EGLConfig config = EGL_NO_CONFIG;
239 if (!extensions.hasNoConfigContext()) {
240 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
241 }
242
Peiyong Linf11f39b2018-09-05 14:37:41 -0700243 bool useContextPriority = extensions.hasContextPriority() &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700244 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Peiyong Lindc979242018-11-30 22:53:07 -0800245 EGLContext protectedContext = EGL_NO_CONTEXT;
246 if (extensions.hasProtectedContent()) {
247 protectedContext = createEglContext(display, config, nullptr, useContextPriority,
248 Protection::PROTECTED);
249 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
250 }
251
252 EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority,
253 Protection::UNPROTECTED);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700254
255 // if can't create a GL context, we can only abort.
256 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
257
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800258 EGLSurface dummy = EGL_NO_SURFACE;
259 if (!extensions.hasSurfacelessContext()) {
Peiyong Lindc979242018-11-30 22:53:07 -0800260 dummy = createDummyEglPbufferSurface(display, config, hwcFormat, Protection::UNPROTECTED);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800261 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700262 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700263 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
264 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
265
Peiyong Lindc979242018-11-30 22:53:07 -0800266 EGLSurface protectedDummy = EGL_NO_SURFACE;
267 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
268 protectedDummy =
269 createDummyEglPbufferSurface(display, config, hwcFormat, Protection::PROTECTED);
270 ALOGE_IF(protectedDummy == EGL_NO_SURFACE, "can't create protected dummy pbuffer");
271 }
272
Peiyong Linf11f39b2018-09-05 14:37:41 -0700273 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
274 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
275
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800276 // now figure out what version of GL did we actually get
Peiyong Linf11f39b2018-09-05 14:37:41 -0700277 GlesVersion version = parseGlesVersion(extensions.getVersion());
278
279 // initialize the renderer while GL is current
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800280 std::unique_ptr<GLESRenderEngine> engine;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700281 switch (version) {
282 case GLES_VERSION_1_0:
283 case GLES_VERSION_1_1:
284 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
285 break;
286 case GLES_VERSION_2_0:
287 case GLES_VERSION_3_0:
Peiyong Lindc979242018-11-30 22:53:07 -0800288 engine = std::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy,
289 protectedContext, protectedDummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700290 break;
291 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700292
293 ALOGI("OpenGL ES informations:");
294 ALOGI("vendor : %s", extensions.getVendor());
295 ALOGI("renderer : %s", extensions.getRenderer());
296 ALOGI("version : %s", extensions.getVersion());
297 ALOGI("extensions: %s", extensions.getExtensions());
298 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
299 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
300
Peiyong Linf11f39b2018-09-05 14:37:41 -0700301 return engine;
302}
303
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800304EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700305 status_t err;
306 EGLConfig config;
307
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800308 // First try to get an ES3 config
309 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700310 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800311 // If ES3 fails, try to get an ES2 config
312 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700313 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800314 // If ES2 still doesn't work, probably because we're on the emulator.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700315 // try a simplified query
316 ALOGW("no suitable EGLConfig found, trying a simpler query");
317 err = selectEGLConfig(display, format, 0, &config);
318 if (err != NO_ERROR) {
319 // this EGL is too lame for android
320 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
321 }
322 }
323 }
324
325 if (logConfig) {
326 // print some debugging info
327 EGLint r, g, b, a;
328 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
329 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
330 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
331 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
332 ALOGI("EGL information:");
333 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
334 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
335 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
336 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
337 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
338 }
339
340 return config;
341}
342
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800343GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
Peiyong Lindc979242018-11-30 22:53:07 -0800344 EGLContext ctxt, EGLSurface dummy, EGLContext protectedContext,
345 EGLSurface protectedDummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700346 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800347 mEGLDisplay(display),
348 mEGLConfig(config),
349 mEGLContext(ctxt),
350 mDummySurface(dummy),
Peiyong Lindc979242018-11-30 22:53:07 -0800351 mProtectedEGLContext(protectedContext),
352 mProtectedDummySurface(protectedDummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700353 mVpWidth(0),
354 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700355 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700356 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
357 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
358
359 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
360 glPixelStorei(GL_PACK_ALIGNMENT, 4);
361
Peiyong Lindc979242018-11-30 22:53:07 -0800362 // Initialize protected EGL Context.
363 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
364 EGLBoolean success = eglMakeCurrent(display, mProtectedDummySurface, mProtectedDummySurface,
365 mProtectedEGLContext);
366 ALOGE_IF(!success, "can't make protected context current");
367 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
368 glPixelStorei(GL_PACK_ALIGNMENT, 4);
369 success = eglMakeCurrent(display, mDummySurface, mDummySurface, mEGLContext);
370 LOG_ALWAYS_FATAL_IF(!success, "can't make default context current");
371 }
372
Chia-I Wub027f802017-11-29 14:00:52 -0800373 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700374 glGenTextures(1, &mProtectedTexName);
375 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
377 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
379 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800380 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1, 1, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, protTexData);
Mathias Agopianff2ed702013-09-01 21:36:12 -0700381
Chia-I Wub027f802017-11-29 14:00:52 -0800382 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600383
Peiyong Lin13effd12018-07-24 17:01:47 -0700384 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800385 const ColorSpace srgb(ColorSpace::sRGB());
386 const ColorSpace displayP3(ColorSpace::DisplayP3());
387 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700388
389 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700390 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
391 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
392 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700393 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700394 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
395 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800396
397 // Compute sRGB to Display P3 and BT2020 transform matrix.
398 // NOTE: For now, we are limiting output wide color space support to
399 // Display-P3 and BT2020 only.
400 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
401 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
402
403 // Compute Display P3 to sRGB and BT2020 transform matrix.
404 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
405 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
406
407 // Compute BT2020 to sRGB and Display P3 transform matrix
408 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
409 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600410 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700411}
412
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800413GLESRenderEngine::~GLESRenderEngine() {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700414 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
415 eglTerminate(mEGLDisplay);
416}
Mathias Agopian3f844832013-08-07 21:24:32 -0700417
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800418std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700419 return std::make_unique<GLFramebuffer>(*this);
420}
421
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800422std::unique_ptr<Image> GLESRenderEngine::createImage() {
Peiyong Linf1bada92018-08-29 09:39:31 -0700423 return std::make_unique<GLImage>(*this);
424}
425
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800426void GLESRenderEngine::primeCache() const {
Peiyong Lindc979242018-11-30 22:53:07 -0800427 ProgramCache::getInstance().primeCache(mInProtectedContext ? mProtectedEGLContext : mEGLContext,
428 mFeatureFlags & USE_COLOR_MANAGEMENT);
Peiyong Linf1bada92018-08-29 09:39:31 -0700429}
430
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800431bool GLESRenderEngine::isCurrent() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700432 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
433}
434
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800435base::unique_fd GLESRenderEngine::flush() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700436 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
437 return base::unique_fd();
438 }
439
440 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
441 if (sync == EGL_NO_SYNC_KHR) {
442 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
443 return base::unique_fd();
444 }
445
446 // native fence fd will not be populated until flush() is done.
447 glFlush();
448
449 // get the fence fd
450 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
451 eglDestroySyncKHR(mEGLDisplay, sync);
452 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
453 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
454 }
455
456 return fenceFd;
457}
458
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800459bool GLESRenderEngine::finish() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700460 if (!GLExtensions::getInstance().hasFenceSync()) {
461 ALOGW("no synchronization support");
462 return false;
463 }
464
465 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
466 if (sync == EGL_NO_SYNC_KHR) {
467 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
468 return false;
469 }
470
471 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
472 2000000000 /*2 sec*/);
473 EGLint error = eglGetError();
474 eglDestroySyncKHR(mEGLDisplay, sync);
475 if (result != EGL_CONDITION_SATISFIED_KHR) {
476 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
477 ALOGW("fence wait timed out");
478 } else {
479 ALOGW("error waiting on EGL fence: %#x", error);
480 }
481 return false;
482 }
483
484 return true;
485}
486
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800487bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700488 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
489 !GLExtensions::getInstance().hasWaitSync()) {
490 return false;
491 }
492
493 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
494 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
495 if (sync == EGL_NO_SYNC_KHR) {
496 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
497 return false;
498 }
499
500 // fenceFd is now owned by EGLSync
501 (void)fenceFd.release();
502
503 // XXX: The spec draft is inconsistent as to whether this should return an
504 // EGLint or void. Ignore the return value for now, as it's not strictly
505 // needed.
506 eglWaitSyncKHR(mEGLDisplay, sync, 0);
507 EGLint error = eglGetError();
508 eglDestroySyncKHR(mEGLDisplay, sync);
509 if (error != EGL_SUCCESS) {
510 ALOGE("failed to wait for EGL native fence sync: %#x", error);
511 return false;
512 }
513
514 return true;
515}
516
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800517void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700518 glClearColor(red, green, blue, alpha);
519 glClear(GL_COLOR_BUFFER_BIT);
520}
521
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800522void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue,
523 float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700524 size_t c;
525 Rect const* r = region.getArray(&c);
526 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
527 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
528 for (size_t i = 0; i < c; i++, r++) {
529 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700530 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700531 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700532 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700533 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700534 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700535 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700536 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700537 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700538 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700539 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700540 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700541 }
542 setupFillWithColor(red, green, blue, alpha);
543 drawMesh(mesh);
544}
545
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800546void GLESRenderEngine::setScissor(const Rect& region) {
Alec Mouri05483a02018-09-10 21:03:42 +0000547 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000548 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000549 int32_t glBottom = canvasHeight - region.bottom;
550
551 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700552 glEnable(GL_SCISSOR_TEST);
553}
554
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800555void GLESRenderEngine::disableScissor() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700556 glDisable(GL_SCISSOR_TEST);
557}
558
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800559void GLESRenderEngine::genTextures(size_t count, uint32_t* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700560 glGenTextures(count, names);
561}
562
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800563void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700564 glDeleteTextures(count, names);
565}
566
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800567void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700568 const GLImage& glImage = static_cast<const GLImage&>(image);
569 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
570
571 glBindTexture(target, texName);
Peiyong Lindc979242018-11-30 22:53:07 -0800572 glTexParameteri(target, GL_TEXTURE_PROTECTED_EXT, glImage.isProtected() ? GL_TRUE : GL_FALSE);
Peiyong Linf1bada92018-08-29 09:39:31 -0700573 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700574 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700575 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700576}
577
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800578status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700579 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
580 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
581 uint32_t textureName = glFramebuffer->getTextureName();
582 uint32_t framebufferName = glFramebuffer->getFramebufferName();
583
584 // Bind the texture and turn our EGLImage into a texture
585 glBindTexture(GL_TEXTURE_2D, textureName);
Peiyong Lindc979242018-11-30 22:53:07 -0800586 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_PROTECTED_EXT,
587 mInProtectedContext ? GL_TRUE : GL_FALSE);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700588 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
589
590 // Bind the Framebuffer to render into
591 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700592 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700593
Alec Mouri05483a02018-09-10 21:03:42 +0000594 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700595
596 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
597
Peiyong Lin46080ef2018-10-26 18:43:14 -0700598 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
599 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700600
601 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
602}
603
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800604void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000605 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700606
607 // back to main framebuffer
608 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700609}
610
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800611void GLESRenderEngine::checkErrors() const {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700612 do {
613 // there could be more than one error flag
614 GLenum error = glGetError();
615 if (error == GL_NO_ERROR) break;
616 ALOGE("GL error 0x%04x", int(error));
617 } while (true);
618}
619
Peiyong Lindc979242018-11-30 22:53:07 -0800620bool GLESRenderEngine::supportsProtectedContent() const {
621 return GLExtensions::getInstance().hasProtectedContent() &&
622 mProtectedEGLContext != EGL_NO_CONTEXT;
623}
624
625bool GLESRenderEngine::useProtectedContext(bool useProtectedContext) {
626 if (useProtectedContext == mInProtectedContext) {
627 return true;
628 }
629 if (useProtectedContext && mProtectedEGLContext == EGL_NO_CONTEXT) {
630 return false;
631 }
632 const EGLSurface surface = useProtectedContext ? mProtectedDummySurface : mDummySurface;
633 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
634 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
635 if (success) {
636 mInProtectedContext = useProtectedContext;
637 }
638 return success;
639}
640
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800641status_t GLESRenderEngine::drawLayers(const DisplaySettings& /*settings*/,
642 const std::vector<LayerSettings>& /*layers*/,
643 ANativeWindowBuffer* const /*buffer*/,
644 base::unique_fd* /*displayFence*/) const {
Alec Mouri6e57f682018-09-29 20:45:08 -0700645 return NO_ERROR;
646}
647
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800648void GLESRenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
649 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800650 int32_t l = sourceCrop.left;
651 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700652 int32_t b = sourceCrop.bottom;
653 int32_t t = sourceCrop.top;
Alec Mouri7e593912018-11-17 04:57:33 +0000654 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700655 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700656
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700657 // Apply custom rotation to the projection.
658 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
659 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700660 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700661 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700662 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800663 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700664 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700665 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800666 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700667 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700668 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800669 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700670 break;
671 default:
672 break;
673 }
674
Mathias Agopian3f844832013-08-07 21:24:32 -0700675 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700676 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700677 mVpWidth = vpw;
678 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700679}
680
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800681void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
682 const half4& color, float cornerRadius) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700683 mState.isPremultipliedAlpha = premultipliedAlpha;
684 mState.isOpaque = opaque;
685 mState.color = color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700686 mState.cornerRadius = cornerRadius;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800687
chaviw13fdc492017-06-27 12:40:18 -0700688 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700689 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700690 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000691
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700692 if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700693 glEnable(GL_BLEND);
694 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
695 } else {
696 glDisable(GL_BLEND);
697 }
698}
699
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800700void GLESRenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700701 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800702}
703
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800704void GLESRenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800705 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600706}
707
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800708void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800709 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600710}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600711
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800712void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700713 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700714}
715
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800716void GLESRenderEngine::setupLayerTexturing(const Texture& texture) {
Mathias Agopian49457ac2013-08-14 18:20:17 -0700717 GLuint target = texture.getTextureTarget();
718 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700719 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700720 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700721 filter = GL_LINEAR;
722 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700723 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
724 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
725 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
726 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700727
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700728 mState.texture = texture;
729 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700730}
731
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800732void GLESRenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700733 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700734 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
735 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700736 mState.texture = texture;
737 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700738}
739
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800740void GLESRenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700741 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700742}
743
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800744void GLESRenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700745 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700746}
747
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800748void GLESRenderEngine::disableBlending() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700749 glDisable(GL_BLEND);
750}
751
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800752void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700753 mState.isPremultipliedAlpha = true;
754 mState.isOpaque = false;
755 mState.color = half4(r, g, b, a);
756 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700757 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700758}
759
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800760void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700761 mState.cropSize = half2(width, height);
762}
763
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800764void GLESRenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700765 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700766 if (mesh.getTexCoordsSize()) {
767 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800768 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
769 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700770 }
771
Chia-I Wub027f802017-11-29 14:00:52 -0800772 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
773 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700774
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700775 if (mState.cornerRadius > 0.0f) {
776 glEnableVertexAttribArray(Program::cropCoords);
777 glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
778 mesh.getByteStride(), mesh.getCropCoords());
779 }
780
Peiyong Lina296b0c2018-04-30 16:55:29 -0700781 // By default, DISPLAY_P3 is the only supported wide color output. However,
782 // when HDR content is present, hardware composer may be able to handle
783 // BT2020 data space, in that case, the output data space is set to be
784 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
785 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700786 if (mUseColorManagement) {
787 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700788 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
789 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700790 Dataspace outputStandard =
791 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
792 Dataspace outputTransfer =
793 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700794 bool needsXYZConversion = needsXYZTransformMatrix();
795
Valerie Haueb8e0762018-11-06 10:10:42 -0800796 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
797 // STANDARD_BT2020, it will be treated as STANDARD_BT709
798 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
799 inputStandard != Dataspace::STANDARD_BT2020) {
800 inputStandard = Dataspace::STANDARD_BT709;
801 }
802
Peiyong Lina296b0c2018-04-30 16:55:29 -0700803 if (needsXYZConversion) {
804 // The supported input color spaces are standard RGB, Display P3 and BT2020.
805 switch (inputStandard) {
806 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700807 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700808 break;
809 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700810 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700811 break;
812 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700813 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700814 break;
815 }
816
Peiyong Lin9b03c732018-05-17 10:14:02 -0700817 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700818 switch (outputStandard) {
819 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700820 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700821 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700822 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700823 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700824 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700825 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700826 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700827 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700828 }
829 } else if (inputStandard != outputStandard) {
830 // At this point, the input data space and output data space could be both
831 // HDR data spaces, but they match each other, we do nothing in this case.
832 // In addition to the case above, the input data space could be
833 // - scRGB linear
834 // - scRGB non-linear
835 // - sRGB
836 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800837 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700838 // The output data spaces could be
839 // - sRGB
840 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800841 // - BT2020
842 switch (outputStandard) {
843 case Dataspace::STANDARD_BT2020:
844 if (inputStandard == Dataspace::STANDARD_BT709) {
845 managedState.outputTransformMatrix = mSrgbToBt2020;
846 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
847 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
848 }
849 break;
850 case Dataspace::STANDARD_DCI_P3:
851 if (inputStandard == Dataspace::STANDARD_BT709) {
852 managedState.outputTransformMatrix = mSrgbToDisplayP3;
853 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
854 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
855 }
856 break;
857 default:
858 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
859 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
860 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
861 managedState.outputTransformMatrix = mBt2020ToSrgb;
862 }
863 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700864 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600865 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700866
867 // we need to convert the RGB value to linear space and convert it back when:
868 // - there is a color matrix that is not an identity matrix, or
869 // - there is an output transform matrix that is not an identity matrix, or
870 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700871 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800872 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700873 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700874 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700875 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700876 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700877 }
878
Peiyong Lindc979242018-11-30 22:53:07 -0800879 ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext
880 : mEGLContext,
881 managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600882
883 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
884
885 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700886 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600887 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700888 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600889 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
890 }
891 } else {
Peiyong Lindc979242018-11-30 22:53:07 -0800892 ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext
893 : mEGLContext,
894 mState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600895
896 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
897 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700898
899 if (mesh.getTexCoordsSize()) {
900 glDisableVertexAttribArray(Program::texCoords);
901 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700902
903 if (mState.cornerRadius > 0.0f) {
904 glDisableVertexAttribArray(Program::cropCoords);
905 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700906}
907
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800908size_t GLESRenderEngine::getMaxTextureSize() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700909 return mMaxTextureSize;
910}
911
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800912size_t GLESRenderEngine::getMaxViewportDims() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700913 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
914}
915
Yiwei Zhang5434a782018-12-05 18:06:32 -0800916void GLESRenderEngine::dump(std::string& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700917 const GLExtensions& extensions = GLExtensions::getInstance();
Peiyong Lindc979242018-11-30 22:53:07 -0800918 ProgramCache& cache = ProgramCache::getInstance();
Peiyong Linf11f39b2018-09-05 14:37:41 -0700919
Yiwei Zhang5434a782018-12-05 18:06:32 -0800920 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
921 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
Yiwei Zhang5434a782018-12-05 18:06:32 -0800922 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
923 extensions.getVersion());
924 StringAppendF(&result, "%s\n", extensions.getExtensions());
Peiyong Lindc979242018-11-30 22:53:07 -0800925 StringAppendF(&result, "RenderEngine is in protected context : %d\n", mInProtectedContext);
926 StringAppendF(&result, "RenderEngine program cache size for unprotected context: %zu\n",
927 cache.getSize(mEGLContext));
928 StringAppendF(&result, "RenderEngine program cache size for protected context: %zu\n",
929 cache.getSize(mProtectedEGLContext));
Yiwei Zhang5434a782018-12-05 18:06:32 -0800930 StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n",
931 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
932 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700933}
934
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800935GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700936 int major, minor;
937 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
938 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
939 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
940 return GLES_VERSION_1_0;
941 }
942 }
943
944 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
945 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
946 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
947 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
948
949 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
950 return GLES_VERSION_1_0;
951}
952
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800953EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Peiyong Lindc979242018-11-30 22:53:07 -0800954 EGLContext shareContext, bool useContextPriority,
955 Protection protection) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800956 EGLint renderableType = 0;
957 if (config == EGL_NO_CONFIG) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800958 renderableType = EGL_OPENGL_ES3_BIT;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800959 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
960 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
961 }
962 EGLint contextClientVersion = 0;
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800963 if (renderableType & EGL_OPENGL_ES3_BIT) {
964 contextClientVersion = 3;
965 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800966 contextClientVersion = 2;
967 } else if (renderableType & EGL_OPENGL_ES_BIT) {
968 contextClientVersion = 1;
969 } else {
970 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
971 }
972
973 std::vector<EGLint> contextAttributes;
Peiyong Lindc979242018-11-30 22:53:07 -0800974 contextAttributes.reserve(7);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800975 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
976 contextAttributes.push_back(contextClientVersion);
977 if (useContextPriority) {
978 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
979 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
980 }
Peiyong Lindc979242018-11-30 22:53:07 -0800981 if (protection == Protection::PROTECTED) {
982 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
983 contextAttributes.push_back(EGL_TRUE);
984 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800985 contextAttributes.push_back(EGL_NONE);
986
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800987 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
988
989 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
990 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
991 // EGL_NO_CONTEXT so that we can abort.
992 if (config != EGL_NO_CONFIG) {
993 return context;
994 }
995 // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should
996 // try to fall back to GLES 2.
997 contextAttributes[1] = 2;
998 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
999 }
1000
1001 return context;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001002}
1003
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001004EGLSurface GLESRenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
Peiyong Lindc979242018-11-30 22:53:07 -08001005 int hwcFormat, Protection protection) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001006 EGLConfig dummyConfig = config;
1007 if (dummyConfig == EGL_NO_CONFIG) {
1008 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
1009 }
1010 std::vector<EGLint> attributes;
Peiyong Lindc979242018-11-30 22:53:07 -08001011 attributes.reserve(7);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001012 attributes.push_back(EGL_WIDTH);
1013 attributes.push_back(1);
1014 attributes.push_back(EGL_HEIGHT);
1015 attributes.push_back(1);
Peiyong Lindc979242018-11-30 22:53:07 -08001016 if (protection == Protection::PROTECTED) {
1017 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1018 attributes.push_back(EGL_TRUE);
1019 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -08001020 attributes.push_back(EGL_NONE);
1021
1022 return eglCreatePbufferSurface(display, dummyConfig, attributes.data());
1023}
1024
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001025bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
Peiyong Lina296b0c2018-04-30 16:55:29 -07001026 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
1027 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
1028 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -07001029 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -07001030}
1031
1032// For convenience, we want to convert the input color space to XYZ color space first,
1033// and then convert from XYZ color space to output color space when
1034// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
1035// HDR content will be tone-mapped to SDR; Or,
1036// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
1037// HLG content to PQ content.
1038// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
1039// input data space or output data space is HDR data space, and the input transfer function
1040// doesn't match the output transfer function, we would enable an intermediate transfrom to
1041// XYZ color space.
Peiyong Lin7e219eb2018-12-03 05:40:42 -08001042bool GLESRenderEngine::needsXYZTransformMatrix() const {
Peiyong Lina296b0c2018-04-30 16:55:29 -07001043 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
1044 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
1045 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -07001046 const Dataspace outputTransfer =
1047 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -07001048
1049 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
1050}
1051
Peiyong Lin46080ef2018-10-26 18:43:14 -07001052} // namespace gl
1053} // namespace renderengine
1054} // namespace android