blob: 8a9e7bd823e53251b735ecd4a1c4fbd2e991ddb3 [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>
Peiyong Lin833074a2018-08-28 11:53:54 -070030#include <cutils/compiler.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070031#include <renderengine/Mesh.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070032#include <renderengine/Texture.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070033#include <renderengine/private/Description.h>
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -060034#include <ui/ColorSpace.h>
35#include <ui/DebugUtils.h>
Dan Stozac1879002014-05-22 15:59:05 -070036#include <ui/Rect.h>
Peiyong Lin60bedb52018-09-05 10:47:31 -070037#include <ui/Region.h>
Chia-I Wu56d7b0a2018-10-01 15:13:11 -070038#include <utils/KeyedVector.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070039#include <utils/String8.h>
40#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
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700112using ui::Dataspace;
113
Peiyong Linf11f39b2018-09-05 14:37:41 -0700114static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
115 EGLint wanted, EGLConfig* outConfig) {
116 EGLint numConfigs = -1, n = 0;
117 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700118 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
119 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
120 configs.resize(n);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700121
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700122 if (!configs.empty()) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700123 if (attribute != EGL_NONE) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700124 for (EGLConfig config : configs) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700125 EGLint value = 0;
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700126 eglGetConfigAttrib(dpy, config, attribute, &value);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700127 if (wanted == value) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700128 *outConfig = config;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700129 return NO_ERROR;
130 }
131 }
132 } else {
133 // just pick the first one
134 *outConfig = configs[0];
Peiyong Linf11f39b2018-09-05 14:37:41 -0700135 return NO_ERROR;
136 }
137 }
Chia-I Wud3b13cb2018-09-13 13:31:26 -0700138
Peiyong Linf11f39b2018-09-05 14:37:41 -0700139 return NAME_NOT_FOUND;
140}
141
142class EGLAttributeVector {
143 struct Attribute;
144 class Adder;
145 friend class Adder;
146 KeyedVector<Attribute, EGLint> mList;
147 struct Attribute {
148 Attribute() : v(0){};
149 explicit Attribute(EGLint v) : v(v) {}
150 EGLint v;
151 bool operator<(const Attribute& other) const {
152 // this places EGL_NONE at the end
153 EGLint lhs(v);
154 EGLint rhs(other.v);
155 if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
156 if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
157 return lhs < rhs;
158 }
159 };
160 class Adder {
161 friend class EGLAttributeVector;
162 EGLAttributeVector& v;
163 EGLint attribute;
164 Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
165
166 public:
167 void operator=(EGLint value) {
168 if (attribute != EGL_NONE) {
169 v.mList.add(Attribute(attribute), value);
170 }
171 }
172 operator EGLint() const { return v.mList[attribute]; }
173 };
174
175public:
176 EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
177 void remove(EGLint attribute) {
178 if (attribute != EGL_NONE) {
179 mList.removeItem(Attribute(attribute));
180 }
181 }
182 Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
183 EGLint operator[](EGLint attribute) const { return mList[attribute]; }
184 // cast-operator to (EGLint const*)
185 operator EGLint const*() const { return &mList.keyAt(0).v; }
186};
187
188static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
189 EGLConfig* config) {
190 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
191 // it is to be used with WIFI displays
192 status_t err;
193 EGLint wantedAttribute;
194 EGLint wantedAttributeValue;
195
196 EGLAttributeVector attribs;
197 if (renderableType) {
198 attribs[EGL_RENDERABLE_TYPE] = renderableType;
199 attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
200 attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
201 attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
202 attribs[EGL_RED_SIZE] = 8;
203 attribs[EGL_GREEN_SIZE] = 8;
204 attribs[EGL_BLUE_SIZE] = 8;
205 attribs[EGL_ALPHA_SIZE] = 8;
206 wantedAttribute = EGL_NONE;
207 wantedAttributeValue = EGL_NONE;
208 } else {
209 // if no renderable type specified, fallback to a simplified query
210 wantedAttribute = EGL_NATIVE_VISUAL_ID;
211 wantedAttributeValue = format;
212 }
213
214 err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
215 if (err == NO_ERROR) {
216 EGLint caveat;
217 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
218 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
219 }
220
221 return err;
222}
223
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800224std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(int hwcFormat, uint32_t featureFlags) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700225 // initialize EGL for the default display
226 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
227 if (!eglInitialize(display, nullptr, nullptr)) {
228 LOG_ALWAYS_FATAL("failed to initialize EGL");
229 }
230
231 GLExtensions& extensions = GLExtensions::getInstance();
232 extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
233 eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
234
235 // The code assumes that ES2 or later is available if this extension is
236 // supported.
237 EGLConfig config = EGL_NO_CONFIG;
238 if (!extensions.hasNoConfigContext()) {
239 config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
240 }
241
Peiyong Linf11f39b2018-09-05 14:37:41 -0700242 bool useContextPriority = extensions.hasContextPriority() &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700243 (featureFlags & RenderEngine::USE_HIGH_PRIORITY_CONTEXT);
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800244 EGLContext ctxt = createEglContext(display, config, EGL_NO_CONTEXT, useContextPriority);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700245
246 // if can't create a GL context, we can only abort.
247 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
248
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800249 EGLSurface dummy = EGL_NO_SURFACE;
250 if (!extensions.hasSurfacelessContext()) {
251 dummy = createDummyEglPbufferSurface(display, config, hwcFormat);
252 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700253 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800254
Peiyong Linf11f39b2018-09-05 14:37:41 -0700255 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
256 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
257
258 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
259 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
260
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800261 // now figure out what version of GL did we actually get
Peiyong Linf11f39b2018-09-05 14:37:41 -0700262 GlesVersion version = parseGlesVersion(extensions.getVersion());
263
264 // initialize the renderer while GL is current
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800265 std::unique_ptr<GLESRenderEngine> engine;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700266 switch (version) {
267 case GLES_VERSION_1_0:
268 case GLES_VERSION_1_1:
269 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
270 break;
271 case GLES_VERSION_2_0:
272 case GLES_VERSION_3_0:
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800273 engine = std::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700274 break;
275 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700276
277 ALOGI("OpenGL ES informations:");
278 ALOGI("vendor : %s", extensions.getVendor());
279 ALOGI("renderer : %s", extensions.getRenderer());
280 ALOGI("version : %s", extensions.getVersion());
281 ALOGI("extensions: %s", extensions.getExtensions());
282 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
283 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
284
Peiyong Linf11f39b2018-09-05 14:37:41 -0700285 return engine;
286}
287
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800288EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700289 status_t err;
290 EGLConfig config;
291
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800292 // First try to get an ES3 config
293 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700294 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800295 // If ES3 fails, try to get an ES2 config
296 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700297 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800298 // If ES2 still doesn't work, probably because we're on the emulator.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700299 // try a simplified query
300 ALOGW("no suitable EGLConfig found, trying a simpler query");
301 err = selectEGLConfig(display, format, 0, &config);
302 if (err != NO_ERROR) {
303 // this EGL is too lame for android
304 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
305 }
306 }
307 }
308
309 if (logConfig) {
310 // print some debugging info
311 EGLint r, g, b, a;
312 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
313 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
314 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
315 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
316 ALOGI("EGL information:");
317 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
318 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
319 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
320 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
321 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
322 }
323
324 return config;
325}
326
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800327GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
328 EGLContext ctxt, EGLSurface dummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700329 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800330 mEGLDisplay(display),
331 mEGLConfig(config),
332 mEGLContext(ctxt),
333 mDummySurface(dummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700334 mVpWidth(0),
335 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700336 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700337 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
338 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
339
340 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
341 glPixelStorei(GL_PACK_ALIGNMENT, 4);
342
Chia-I Wub027f802017-11-29 14:00:52 -0800343 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700344 glGenTextures(1, &mProtectedTexName);
345 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
346 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800350 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 -0700351
Chia-I Wub027f802017-11-29 14:00:52 -0800352 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600353
Peiyong Lin13effd12018-07-24 17:01:47 -0700354 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800355 const ColorSpace srgb(ColorSpace::sRGB());
356 const ColorSpace displayP3(ColorSpace::DisplayP3());
357 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700358
359 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700360 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
361 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
362 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700363 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700364 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
365 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800366
367 // Compute sRGB to Display P3 and BT2020 transform matrix.
368 // NOTE: For now, we are limiting output wide color space support to
369 // Display-P3 and BT2020 only.
370 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
371 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
372
373 // Compute Display P3 to sRGB and BT2020 transform matrix.
374 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
375 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
376
377 // Compute BT2020 to sRGB and Display P3 transform matrix
378 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
379 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600380 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700381}
382
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800383GLESRenderEngine::~GLESRenderEngine() {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700384 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
385 eglTerminate(mEGLDisplay);
386}
Mathias Agopian3f844832013-08-07 21:24:32 -0700387
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800388std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700389 return std::make_unique<GLFramebuffer>(*this);
390}
391
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800392std::unique_ptr<Image> GLESRenderEngine::createImage() {
Peiyong Linf1bada92018-08-29 09:39:31 -0700393 return std::make_unique<GLImage>(*this);
394}
395
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800396void GLESRenderEngine::primeCache() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700397 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
398}
399
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800400bool GLESRenderEngine::isCurrent() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700401 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
402}
403
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800404base::unique_fd GLESRenderEngine::flush() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700405 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
406 return base::unique_fd();
407 }
408
409 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
410 if (sync == EGL_NO_SYNC_KHR) {
411 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
412 return base::unique_fd();
413 }
414
415 // native fence fd will not be populated until flush() is done.
416 glFlush();
417
418 // get the fence fd
419 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
420 eglDestroySyncKHR(mEGLDisplay, sync);
421 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
422 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
423 }
424
425 return fenceFd;
426}
427
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800428bool GLESRenderEngine::finish() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700429 if (!GLExtensions::getInstance().hasFenceSync()) {
430 ALOGW("no synchronization support");
431 return false;
432 }
433
434 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
435 if (sync == EGL_NO_SYNC_KHR) {
436 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
437 return false;
438 }
439
440 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
441 2000000000 /*2 sec*/);
442 EGLint error = eglGetError();
443 eglDestroySyncKHR(mEGLDisplay, sync);
444 if (result != EGL_CONDITION_SATISFIED_KHR) {
445 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
446 ALOGW("fence wait timed out");
447 } else {
448 ALOGW("error waiting on EGL fence: %#x", error);
449 }
450 return false;
451 }
452
453 return true;
454}
455
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800456bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700457 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
458 !GLExtensions::getInstance().hasWaitSync()) {
459 return false;
460 }
461
462 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
463 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
464 if (sync == EGL_NO_SYNC_KHR) {
465 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
466 return false;
467 }
468
469 // fenceFd is now owned by EGLSync
470 (void)fenceFd.release();
471
472 // XXX: The spec draft is inconsistent as to whether this should return an
473 // EGLint or void. Ignore the return value for now, as it's not strictly
474 // needed.
475 eglWaitSyncKHR(mEGLDisplay, sync, 0);
476 EGLint error = eglGetError();
477 eglDestroySyncKHR(mEGLDisplay, sync);
478 if (error != EGL_SUCCESS) {
479 ALOGE("failed to wait for EGL native fence sync: %#x", error);
480 return false;
481 }
482
483 return true;
484}
485
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800486void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700487 glClearColor(red, green, blue, alpha);
488 glClear(GL_COLOR_BUFFER_BIT);
489}
490
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800491void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue,
492 float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700493 size_t c;
494 Rect const* r = region.getArray(&c);
495 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
496 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
497 for (size_t i = 0; i < c; i++, r++) {
498 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700499 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700500 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700501 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700502 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700503 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700504 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700505 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700506 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700507 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700508 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700509 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700510 }
511 setupFillWithColor(red, green, blue, alpha);
512 drawMesh(mesh);
513}
514
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800515void GLESRenderEngine::setScissor(const Rect& region) {
Alec Mouri05483a02018-09-10 21:03:42 +0000516 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000517 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000518 int32_t glBottom = canvasHeight - region.bottom;
519
520 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700521 glEnable(GL_SCISSOR_TEST);
522}
523
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800524void GLESRenderEngine::disableScissor() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700525 glDisable(GL_SCISSOR_TEST);
526}
527
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800528void GLESRenderEngine::genTextures(size_t count, uint32_t* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700529 glGenTextures(count, names);
530}
531
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800532void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700533 glDeleteTextures(count, names);
534}
535
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800536void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700537 const GLImage& glImage = static_cast<const GLImage&>(image);
538 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
539
540 glBindTexture(target, texName);
541 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700542 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700543 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700544}
545
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800546status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700547 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
548 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
549 uint32_t textureName = glFramebuffer->getTextureName();
550 uint32_t framebufferName = glFramebuffer->getFramebufferName();
551
552 // Bind the texture and turn our EGLImage into a texture
553 glBindTexture(GL_TEXTURE_2D, textureName);
554 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
555
556 // Bind the Framebuffer to render into
557 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700558 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700559
Alec Mouri05483a02018-09-10 21:03:42 +0000560 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700561
562 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
563
Peiyong Lin46080ef2018-10-26 18:43:14 -0700564 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
565 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700566
567 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
568}
569
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800570void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000571 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700572
573 // back to main framebuffer
574 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700575}
576
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800577void GLESRenderEngine::checkErrors() const {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700578 do {
579 // there could be more than one error flag
580 GLenum error = glGetError();
581 if (error == GL_NO_ERROR) break;
582 ALOGE("GL error 0x%04x", int(error));
583 } while (true);
584}
585
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800586status_t GLESRenderEngine::drawLayers(const DisplaySettings& /*settings*/,
587 const std::vector<LayerSettings>& /*layers*/,
588 ANativeWindowBuffer* const /*buffer*/,
589 base::unique_fd* /*displayFence*/) const {
Alec Mouri6e57f682018-09-29 20:45:08 -0700590 return NO_ERROR;
591}
592
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800593void GLESRenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
594 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800595 int32_t l = sourceCrop.left;
596 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700597 int32_t b = sourceCrop.bottom;
598 int32_t t = sourceCrop.top;
Alec Mouri7e593912018-11-17 04:57:33 +0000599 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700600 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700601
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700602 // Apply custom rotation to the projection.
603 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
604 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700605 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700606 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700607 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800608 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700609 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700610 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800611 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700612 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700613 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800614 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700615 break;
616 default:
617 break;
618 }
619
Mathias Agopian3f844832013-08-07 21:24:32 -0700620 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700621 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700622 mVpWidth = vpw;
623 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700624}
625
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800626void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
627 const half4& color, float cornerRadius) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700628 mState.isPremultipliedAlpha = premultipliedAlpha;
629 mState.isOpaque = opaque;
630 mState.color = color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700631 mState.cornerRadius = cornerRadius;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800632
chaviw13fdc492017-06-27 12:40:18 -0700633 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700634 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700635 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000636
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700637 if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700638 glEnable(GL_BLEND);
639 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
640 } else {
641 glDisable(GL_BLEND);
642 }
643}
644
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800645void GLESRenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700646 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800647}
648
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800649void GLESRenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800650 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600651}
652
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800653void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800654 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600655}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600656
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800657void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700658 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700659}
660
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800661void GLESRenderEngine::setupLayerTexturing(const Texture& texture) {
Mathias Agopian49457ac2013-08-14 18:20:17 -0700662 GLuint target = texture.getTextureTarget();
663 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700664 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700665 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700666 filter = GL_LINEAR;
667 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700668 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
669 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
670 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
671 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700672
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700673 mState.texture = texture;
674 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700675}
676
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800677void GLESRenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700678 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700679 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
680 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700681 mState.texture = texture;
682 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700683}
684
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800685void GLESRenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700686 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700687}
688
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800689void GLESRenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700690 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700691}
692
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800693void GLESRenderEngine::disableBlending() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700694 glDisable(GL_BLEND);
695}
696
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800697void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700698 mState.isPremultipliedAlpha = true;
699 mState.isOpaque = false;
700 mState.color = half4(r, g, b, a);
701 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700702 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700703}
704
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800705void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700706 mState.cropSize = half2(width, height);
707}
708
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800709void GLESRenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700710 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700711 if (mesh.getTexCoordsSize()) {
712 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800713 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
714 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700715 }
716
Chia-I Wub027f802017-11-29 14:00:52 -0800717 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
718 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700719
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700720 if (mState.cornerRadius > 0.0f) {
721 glEnableVertexAttribArray(Program::cropCoords);
722 glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
723 mesh.getByteStride(), mesh.getCropCoords());
724 }
725
Peiyong Lina296b0c2018-04-30 16:55:29 -0700726 // By default, DISPLAY_P3 is the only supported wide color output. However,
727 // when HDR content is present, hardware composer may be able to handle
728 // BT2020 data space, in that case, the output data space is set to be
729 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
730 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700731 if (mUseColorManagement) {
732 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700733 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
734 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700735 Dataspace outputStandard =
736 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
737 Dataspace outputTransfer =
738 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700739 bool needsXYZConversion = needsXYZTransformMatrix();
740
Valerie Haueb8e0762018-11-06 10:10:42 -0800741 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
742 // STANDARD_BT2020, it will be treated as STANDARD_BT709
743 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
744 inputStandard != Dataspace::STANDARD_BT2020) {
745 inputStandard = Dataspace::STANDARD_BT709;
746 }
747
Peiyong Lina296b0c2018-04-30 16:55:29 -0700748 if (needsXYZConversion) {
749 // The supported input color spaces are standard RGB, Display P3 and BT2020.
750 switch (inputStandard) {
751 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700752 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700753 break;
754 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700755 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700756 break;
757 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700758 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700759 break;
760 }
761
Peiyong Lin9b03c732018-05-17 10:14:02 -0700762 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700763 switch (outputStandard) {
764 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700765 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700766 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700767 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700768 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700769 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700770 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700771 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700772 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700773 }
774 } else if (inputStandard != outputStandard) {
775 // At this point, the input data space and output data space could be both
776 // HDR data spaces, but they match each other, we do nothing in this case.
777 // In addition to the case above, the input data space could be
778 // - scRGB linear
779 // - scRGB non-linear
780 // - sRGB
781 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800782 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700783 // The output data spaces could be
784 // - sRGB
785 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800786 // - BT2020
787 switch (outputStandard) {
788 case Dataspace::STANDARD_BT2020:
789 if (inputStandard == Dataspace::STANDARD_BT709) {
790 managedState.outputTransformMatrix = mSrgbToBt2020;
791 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
792 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
793 }
794 break;
795 case Dataspace::STANDARD_DCI_P3:
796 if (inputStandard == Dataspace::STANDARD_BT709) {
797 managedState.outputTransformMatrix = mSrgbToDisplayP3;
798 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
799 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
800 }
801 break;
802 default:
803 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
804 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
805 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
806 managedState.outputTransformMatrix = mBt2020ToSrgb;
807 }
808 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700809 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600810 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700811
812 // we need to convert the RGB value to linear space and convert it back when:
813 // - there is a color matrix that is not an identity matrix, or
814 // - there is an output transform matrix that is not an identity matrix, or
815 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700816 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800817 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700818 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700819 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700820 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700821 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700822 }
823
Peiyong Lin13effd12018-07-24 17:01:47 -0700824 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600825
826 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
827
828 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700829 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600830 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700831 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600832 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
833 }
834 } else {
835 ProgramCache::getInstance().useProgram(mState);
836
837 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
838 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700839
840 if (mesh.getTexCoordsSize()) {
841 glDisableVertexAttribArray(Program::texCoords);
842 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700843
844 if (mState.cornerRadius > 0.0f) {
845 glDisableVertexAttribArray(Program::cropCoords);
846 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700847}
848
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800849size_t GLESRenderEngine::getMaxTextureSize() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700850 return mMaxTextureSize;
851}
852
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800853size_t GLESRenderEngine::getMaxViewportDims() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700854 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
855}
856
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800857void GLESRenderEngine::dump(String8& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700858 const GLExtensions& extensions = GLExtensions::getInstance();
859
860 result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
861 result.appendFormat("%s\n", extensions.getEGLExtensions());
862
863 result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
864 extensions.getVersion());
865 result.appendFormat("%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700866
867 result.appendFormat("RenderEngine program cache size: %zu\n",
868 ProgramCache::getInstance().getSize());
869
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800870 result.appendFormat("RenderEngine last dataspace conversion: (%s) to (%s)\n",
Peiyong Lin34beb7a2018-03-28 11:57:12 -0700871 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
872 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700873}
874
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800875GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700876 int major, minor;
877 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
878 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
879 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
880 return GLES_VERSION_1_0;
881 }
882 }
883
884 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
885 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
886 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
887 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
888
889 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
890 return GLES_VERSION_1_0;
891}
892
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800893EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
894 EGLContext shareContext, bool useContextPriority) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800895 EGLint renderableType = 0;
896 if (config == EGL_NO_CONFIG) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800897 renderableType = EGL_OPENGL_ES3_BIT;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800898 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
899 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
900 }
901 EGLint contextClientVersion = 0;
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800902 if (renderableType & EGL_OPENGL_ES3_BIT) {
903 contextClientVersion = 3;
904 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800905 contextClientVersion = 2;
906 } else if (renderableType & EGL_OPENGL_ES_BIT) {
907 contextClientVersion = 1;
908 } else {
909 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
910 }
911
912 std::vector<EGLint> contextAttributes;
913 contextAttributes.reserve(5);
914 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
915 contextAttributes.push_back(contextClientVersion);
916 if (useContextPriority) {
917 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
918 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
919 }
920 contextAttributes.push_back(EGL_NONE);
921
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800922 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
923
924 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
925 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
926 // EGL_NO_CONTEXT so that we can abort.
927 if (config != EGL_NO_CONFIG) {
928 return context;
929 }
930 // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should
931 // try to fall back to GLES 2.
932 contextAttributes[1] = 2;
933 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
934 }
935
936 return context;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800937}
938
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800939EGLSurface GLESRenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
940 int hwcFormat) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800941 EGLConfig dummyConfig = config;
942 if (dummyConfig == EGL_NO_CONFIG) {
943 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
944 }
945 std::vector<EGLint> attributes;
946 attributes.reserve(5);
947 attributes.push_back(EGL_WIDTH);
948 attributes.push_back(1);
949 attributes.push_back(EGL_HEIGHT);
950 attributes.push_back(1);
951 attributes.push_back(EGL_NONE);
952
953 return eglCreatePbufferSurface(display, dummyConfig, attributes.data());
954}
955
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800956bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700957 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
958 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
959 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700960 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700961}
962
963// For convenience, we want to convert the input color space to XYZ color space first,
964// and then convert from XYZ color space to output color space when
965// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
966// HDR content will be tone-mapped to SDR; Or,
967// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
968// HLG content to PQ content.
969// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
970// input data space or output data space is HDR data space, and the input transfer function
971// doesn't match the output transfer function, we would enable an intermediate transfrom to
972// XYZ color space.
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800973bool GLESRenderEngine::needsXYZTransformMatrix() const {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700974 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
975 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
976 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700977 const Dataspace outputTransfer =
978 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700979
980 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
981}
982
Peiyong Lin46080ef2018-10-26 18:43:14 -0700983} // namespace gl
984} // namespace renderengine
985} // namespace android