blob: 1395551642885f1e8b3f5ac3bb5113426d1094ef [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 Lina5e9f1b2018-11-27 22:49:37 -0800245 EGLContext ctxt = createEglContext(display, config, EGL_NO_CONTEXT, useContextPriority);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700246
247 // if can't create a GL context, we can only abort.
248 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
249
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800250 EGLSurface dummy = EGL_NO_SURFACE;
251 if (!extensions.hasSurfacelessContext()) {
252 dummy = createDummyEglPbufferSurface(display, config, hwcFormat);
253 LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
Peiyong Linf11f39b2018-09-05 14:37:41 -0700254 }
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800255
Peiyong Linf11f39b2018-09-05 14:37:41 -0700256 EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
257 LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
258
259 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
260 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
261
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800262 // now figure out what version of GL did we actually get
Peiyong Linf11f39b2018-09-05 14:37:41 -0700263 GlesVersion version = parseGlesVersion(extensions.getVersion());
264
265 // initialize the renderer while GL is current
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800266 std::unique_ptr<GLESRenderEngine> engine;
Peiyong Linf11f39b2018-09-05 14:37:41 -0700267 switch (version) {
268 case GLES_VERSION_1_0:
269 case GLES_VERSION_1_1:
270 LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
271 break;
272 case GLES_VERSION_2_0:
273 case GLES_VERSION_3_0:
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800274 engine = std::make_unique<GLESRenderEngine>(featureFlags, display, config, ctxt, dummy);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700275 break;
276 }
Peiyong Linf11f39b2018-09-05 14:37:41 -0700277
278 ALOGI("OpenGL ES informations:");
279 ALOGI("vendor : %s", extensions.getVendor());
280 ALOGI("renderer : %s", extensions.getRenderer());
281 ALOGI("version : %s", extensions.getVersion());
282 ALOGI("extensions: %s", extensions.getExtensions());
283 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
284 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
285
Peiyong Linf11f39b2018-09-05 14:37:41 -0700286 return engine;
287}
288
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800289EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700290 status_t err;
291 EGLConfig config;
292
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800293 // First try to get an ES3 config
294 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700295 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800296 // If ES3 fails, try to get an ES2 config
297 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
Peiyong Linf11f39b2018-09-05 14:37:41 -0700298 if (err != NO_ERROR) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800299 // If ES2 still doesn't work, probably because we're on the emulator.
Peiyong Linf11f39b2018-09-05 14:37:41 -0700300 // try a simplified query
301 ALOGW("no suitable EGLConfig found, trying a simpler query");
302 err = selectEGLConfig(display, format, 0, &config);
303 if (err != NO_ERROR) {
304 // this EGL is too lame for android
305 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
306 }
307 }
308 }
309
310 if (logConfig) {
311 // print some debugging info
312 EGLint r, g, b, a;
313 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
314 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
315 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
316 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
317 ALOGI("EGL information:");
318 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
319 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
320 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
321 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
322 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
323 }
324
325 return config;
326}
327
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800328GLESRenderEngine::GLESRenderEngine(uint32_t featureFlags, EGLDisplay display, EGLConfig config,
329 EGLContext ctxt, EGLSurface dummy)
Peiyong Linf11f39b2018-09-05 14:37:41 -0700330 : renderengine::impl::RenderEngine(featureFlags),
Alec Mouri0a9c7b82018-11-16 13:05:25 -0800331 mEGLDisplay(display),
332 mEGLConfig(config),
333 mEGLContext(ctxt),
334 mDummySurface(dummy),
Chia-I Wu93e14df2018-06-04 10:10:17 -0700335 mVpWidth(0),
336 mVpHeight(0),
Peiyong Lin13effd12018-07-24 17:01:47 -0700337 mUseColorManagement(featureFlags & USE_COLOR_MANAGEMENT) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700338 glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
339 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
340
341 glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
342 glPixelStorei(GL_PACK_ALIGNMENT, 4);
343
Chia-I Wub027f802017-11-29 14:00:52 -0800344 const uint16_t protTexData[] = {0};
Mathias Agopian3f844832013-08-07 21:24:32 -0700345 glGenTextures(1, &mProtectedTexName);
346 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
347 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
348 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
Chia-I Wub027f802017-11-29 14:00:52 -0800351 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 -0700352
Chia-I Wub027f802017-11-29 14:00:52 -0800353 // mColorBlindnessCorrection = M;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600354
Peiyong Lin13effd12018-07-24 17:01:47 -0700355 if (mUseColorManagement) {
Valerie Haueb8e0762018-11-06 10:10:42 -0800356 const ColorSpace srgb(ColorSpace::sRGB());
357 const ColorSpace displayP3(ColorSpace::DisplayP3());
358 const ColorSpace bt2020(ColorSpace::BT2020());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700359
360 // no chromatic adaptation needed since all color spaces use D65 for their white points.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700361 mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
362 mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
363 mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
Peiyong Lin9b03c732018-05-17 10:14:02 -0700364 mXyzToSrgb = mat4(srgb.getXYZtoRGB());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700365 mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
366 mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
Valerie Haueb8e0762018-11-06 10:10:42 -0800367
368 // Compute sRGB to Display P3 and BT2020 transform matrix.
369 // NOTE: For now, we are limiting output wide color space support to
370 // Display-P3 and BT2020 only.
371 mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
372 mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
373
374 // Compute Display P3 to sRGB and BT2020 transform matrix.
375 mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
376 mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
377
378 // Compute BT2020 to sRGB and Display P3 transform matrix
379 mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
380 mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600381 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700382}
383
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800384GLESRenderEngine::~GLESRenderEngine() {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700385 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
386 eglTerminate(mEGLDisplay);
387}
Mathias Agopian3f844832013-08-07 21:24:32 -0700388
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800389std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700390 return std::make_unique<GLFramebuffer>(*this);
391}
392
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800393std::unique_ptr<Image> GLESRenderEngine::createImage() {
Peiyong Linf1bada92018-08-29 09:39:31 -0700394 return std::make_unique<GLImage>(*this);
395}
396
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800397void GLESRenderEngine::primeCache() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700398 ProgramCache::getInstance().primeCache(mFeatureFlags & USE_COLOR_MANAGEMENT);
399}
400
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800401bool GLESRenderEngine::isCurrent() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700402 return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
403}
404
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800405base::unique_fd GLESRenderEngine::flush() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700406 if (!GLExtensions::getInstance().hasNativeFenceSync()) {
407 return base::unique_fd();
408 }
409
410 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
411 if (sync == EGL_NO_SYNC_KHR) {
412 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
413 return base::unique_fd();
414 }
415
416 // native fence fd will not be populated until flush() is done.
417 glFlush();
418
419 // get the fence fd
420 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
421 eglDestroySyncKHR(mEGLDisplay, sync);
422 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
423 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
424 }
425
426 return fenceFd;
427}
428
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800429bool GLESRenderEngine::finish() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700430 if (!GLExtensions::getInstance().hasFenceSync()) {
431 ALOGW("no synchronization support");
432 return false;
433 }
434
435 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
436 if (sync == EGL_NO_SYNC_KHR) {
437 ALOGW("failed to create EGL fence sync: %#x", eglGetError());
438 return false;
439 }
440
441 EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
442 2000000000 /*2 sec*/);
443 EGLint error = eglGetError();
444 eglDestroySyncKHR(mEGLDisplay, sync);
445 if (result != EGL_CONDITION_SATISFIED_KHR) {
446 if (result == EGL_TIMEOUT_EXPIRED_KHR) {
447 ALOGW("fence wait timed out");
448 } else {
449 ALOGW("error waiting on EGL fence: %#x", error);
450 }
451 return false;
452 }
453
454 return true;
455}
456
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800457bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700458 if (!GLExtensions::getInstance().hasNativeFenceSync() ||
459 !GLExtensions::getInstance().hasWaitSync()) {
460 return false;
461 }
462
463 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
464 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
465 if (sync == EGL_NO_SYNC_KHR) {
466 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
467 return false;
468 }
469
470 // fenceFd is now owned by EGLSync
471 (void)fenceFd.release();
472
473 // XXX: The spec draft is inconsistent as to whether this should return an
474 // EGLint or void. Ignore the return value for now, as it's not strictly
475 // needed.
476 eglWaitSyncKHR(mEGLDisplay, sync, 0);
477 EGLint error = eglGetError();
478 eglDestroySyncKHR(mEGLDisplay, sync);
479 if (error != EGL_SUCCESS) {
480 ALOGE("failed to wait for EGL native fence sync: %#x", error);
481 return false;
482 }
483
484 return true;
485}
486
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800487void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700488 glClearColor(red, green, blue, alpha);
489 glClear(GL_COLOR_BUFFER_BIT);
490}
491
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800492void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue,
493 float alpha) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700494 size_t c;
495 Rect const* r = region.getArray(&c);
496 Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
497 Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
498 for (size_t i = 0; i < c; i++, r++) {
499 position[i * 6 + 0].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700500 position[i * 6 + 0].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700501 position[i * 6 + 1].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700502 position[i * 6 + 1].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700503 position[i * 6 + 2].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700504 position[i * 6 + 2].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700505 position[i * 6 + 3].x = r->left;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700506 position[i * 6 + 3].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700507 position[i * 6 + 4].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700508 position[i * 6 + 4].y = r->bottom;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700509 position[i * 6 + 5].x = r->right;
Chia-I Wu28e3a252018-09-07 12:05:02 -0700510 position[i * 6 + 5].y = r->top;
Peiyong Lin60bedb52018-09-05 10:47:31 -0700511 }
512 setupFillWithColor(red, green, blue, alpha);
513 drawMesh(mesh);
514}
515
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800516void GLESRenderEngine::setScissor(const Rect& region) {
Alec Mouri05483a02018-09-10 21:03:42 +0000517 // Invert y-coordinate to map to GL-space.
Alec Mouri7e593912018-11-17 04:57:33 +0000518 int32_t canvasHeight = mFboHeight;
Alec Mouri05483a02018-09-10 21:03:42 +0000519 int32_t glBottom = canvasHeight - region.bottom;
520
521 glScissor(region.left, glBottom, region.getWidth(), region.getHeight());
Peiyong Lin60bedb52018-09-05 10:47:31 -0700522 glEnable(GL_SCISSOR_TEST);
523}
524
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800525void GLESRenderEngine::disableScissor() {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700526 glDisable(GL_SCISSOR_TEST);
527}
528
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800529void GLESRenderEngine::genTextures(size_t count, uint32_t* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700530 glGenTextures(count, names);
531}
532
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800533void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700534 glDeleteTextures(count, names);
535}
536
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800537void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
Peiyong Linf1bada92018-08-29 09:39:31 -0700538 const GLImage& glImage = static_cast<const GLImage&>(image);
539 const GLenum target = GL_TEXTURE_EXTERNAL_OES;
540
541 glBindTexture(target, texName);
542 if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700543 glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
Peiyong Linf1bada92018-08-29 09:39:31 -0700544 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700545}
546
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800547status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700548 GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
549 EGLImageKHR eglImage = glFramebuffer->getEGLImage();
550 uint32_t textureName = glFramebuffer->getTextureName();
551 uint32_t framebufferName = glFramebuffer->getFramebufferName();
552
553 // Bind the texture and turn our EGLImage into a texture
554 glBindTexture(GL_TEXTURE_2D, textureName);
555 glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
556
557 // Bind the Framebuffer to render into
558 glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700559 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700560
Alec Mouri05483a02018-09-10 21:03:42 +0000561 mFboHeight = glFramebuffer->getBufferHeight();
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700562
563 uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
564
Peiyong Lin46080ef2018-10-26 18:43:14 -0700565 ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
566 glStatus);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700567
568 return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
569}
570
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800571void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /* framebuffer */) {
Alec Mouri05483a02018-09-10 21:03:42 +0000572 mFboHeight = 0;
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700573
574 // back to main framebuffer
575 glBindFramebuffer(GL_FRAMEBUFFER, 0);
Peiyong Line5a9a7f2018-08-30 15:32:13 -0700576}
577
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800578void GLESRenderEngine::checkErrors() const {
Peiyong Lin60bedb52018-09-05 10:47:31 -0700579 do {
580 // there could be more than one error flag
581 GLenum error = glGetError();
582 if (error == GL_NO_ERROR) break;
583 ALOGE("GL error 0x%04x", int(error));
584 } while (true);
585}
586
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800587status_t GLESRenderEngine::drawLayers(const DisplaySettings& /*settings*/,
588 const std::vector<LayerSettings>& /*layers*/,
589 ANativeWindowBuffer* const /*buffer*/,
590 base::unique_fd* /*displayFence*/) const {
Alec Mouri6e57f682018-09-29 20:45:08 -0700591 return NO_ERROR;
592}
593
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800594void GLESRenderEngine::setViewportAndProjection(size_t vpw, size_t vph, Rect sourceCrop,
595 ui::Transform::orientation_flags rotation) {
Ivan Lozano1f58ac52017-12-14 13:27:10 -0800596 int32_t l = sourceCrop.left;
597 int32_t r = sourceCrop.right;
Chia-I Wu1be50b52018-08-29 10:44:48 -0700598 int32_t b = sourceCrop.bottom;
599 int32_t t = sourceCrop.top;
Alec Mouri7e593912018-11-17 04:57:33 +0000600 std::swap(t, b);
Chia-I Wu1be50b52018-08-29 10:44:48 -0700601 mat4 m = mat4::ortho(l, r, b, t, 0, 1);
Mathias Agopian3f844832013-08-07 21:24:32 -0700602
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700603 // Apply custom rotation to the projection.
604 float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
605 switch (rotation) {
Peiyong Linefefaac2018-08-17 12:27:51 -0700606 case ui::Transform::ROT_0:
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700607 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700608 case ui::Transform::ROT_90:
Chia-I Wub027f802017-11-29 14:00:52 -0800609 m = mat4::rotate(rot90InRadians, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700610 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700611 case ui::Transform::ROT_180:
Chia-I Wub027f802017-11-29 14:00:52 -0800612 m = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700613 break;
Peiyong Linefefaac2018-08-17 12:27:51 -0700614 case ui::Transform::ROT_270:
Chia-I Wub027f802017-11-29 14:00:52 -0800615 m = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)) * m;
Riley Andrewsc3ebe662014-09-04 16:20:31 -0700616 break;
617 default:
618 break;
619 }
620
Mathias Agopian3f844832013-08-07 21:24:32 -0700621 glViewport(0, 0, vpw, vph);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700622 mState.projectionMatrix = m;
Mathias Agopianff2ed702013-09-01 21:36:12 -0700623 mVpWidth = vpw;
624 mVpHeight = vph;
Mathias Agopian3f844832013-08-07 21:24:32 -0700625}
626
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800627void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
628 const half4& color, float cornerRadius) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700629 mState.isPremultipliedAlpha = premultipliedAlpha;
630 mState.isOpaque = opaque;
631 mState.color = color;
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700632 mState.cornerRadius = cornerRadius;
Dan Stoza9e56aa02015-11-02 13:00:03 -0800633
chaviw13fdc492017-06-27 12:40:18 -0700634 if (disableTexture) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700635 mState.textureEnabled = false;
chaviw13fdc492017-06-27 12:40:18 -0700636 }
Fabien Sanglard9d96de42016-10-11 00:15:18 +0000637
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700638 if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700639 glEnable(GL_BLEND);
640 glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
641 } else {
642 glDisable(GL_BLEND);
643 }
644}
645
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800646void GLESRenderEngine::setSourceY410BT2020(bool enable) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700647 mState.isY410BT2020 = enable;
Chia-I Wu131d3762018-01-11 14:35:27 -0800648}
649
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800650void GLESRenderEngine::setSourceDataSpace(Dataspace source) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800651 mDataSpace = source;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600652}
653
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800654void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) {
Chia-I Wu69bf10f2018-02-20 13:04:50 -0800655 mOutputDataSpace = dataspace;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600656}
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600657
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800658void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700659 mState.displayMaxLuminance = maxLuminance;
Peiyong Linfb069302018-04-25 14:34:31 -0700660}
661
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800662void GLESRenderEngine::setupLayerTexturing(const Texture& texture) {
Mathias Agopian49457ac2013-08-14 18:20:17 -0700663 GLuint target = texture.getTextureTarget();
664 glBindTexture(target, texture.getTextureName());
Mathias Agopian3f844832013-08-07 21:24:32 -0700665 GLenum filter = GL_NEAREST;
Mathias Agopian49457ac2013-08-14 18:20:17 -0700666 if (texture.getFiltering()) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700667 filter = GL_LINEAR;
668 }
Mathias Agopian49457ac2013-08-14 18:20:17 -0700669 glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
670 glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
671 glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
672 glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
Mathias Agopian3f844832013-08-07 21:24:32 -0700673
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700674 mState.texture = texture;
675 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700676}
677
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800678void GLESRenderEngine::setupLayerBlackedOut() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700679 glBindTexture(GL_TEXTURE_2D, mProtectedTexName);
Mathias Agopian49457ac2013-08-14 18:20:17 -0700680 Texture texture(Texture::TEXTURE_2D, mProtectedTexName);
681 texture.setDimensions(1, 1); // FIXME: we should get that from somewhere
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700682 mState.texture = texture;
683 mState.textureEnabled = true;
Mathias Agopian3f844832013-08-07 21:24:32 -0700684}
685
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800686void GLESRenderEngine::setColorTransform(const mat4& colorTransform) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700687 mState.colorMatrix = colorTransform;
Dan Stozaf0087992014-10-20 15:46:09 -0700688}
689
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800690void GLESRenderEngine::disableTexturing() {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700691 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700692}
693
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800694void GLESRenderEngine::disableBlending() {
Mathias Agopian3f844832013-08-07 21:24:32 -0700695 glDisable(GL_BLEND);
696}
697
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800698void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700699 mState.isPremultipliedAlpha = true;
700 mState.isOpaque = false;
701 mState.color = half4(r, g, b, a);
702 mState.textureEnabled = false;
Mathias Agopian3f844832013-08-07 21:24:32 -0700703 glDisable(GL_BLEND);
Mathias Agopian3f844832013-08-07 21:24:32 -0700704}
705
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800706void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700707 mState.cropSize = half2(width, height);
708}
709
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800710void GLESRenderEngine::drawMesh(const Mesh& mesh) {
Dan Stoza2713c302018-03-28 17:07:36 -0700711 ATRACE_CALL();
Mathias Agopian3f844832013-08-07 21:24:32 -0700712 if (mesh.getTexCoordsSize()) {
713 glEnableVertexAttribArray(Program::texCoords);
Chia-I Wub027f802017-11-29 14:00:52 -0800714 glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
715 mesh.getByteStride(), mesh.getTexCoords());
Mathias Agopian3f844832013-08-07 21:24:32 -0700716 }
717
Chia-I Wub027f802017-11-29 14:00:52 -0800718 glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
719 mesh.getByteStride(), mesh.getPositions());
Mathias Agopian3f844832013-08-07 21:24:32 -0700720
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700721 if (mState.cornerRadius > 0.0f) {
722 glEnableVertexAttribArray(Program::cropCoords);
723 glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
724 mesh.getByteStride(), mesh.getCropCoords());
725 }
726
Peiyong Lina296b0c2018-04-30 16:55:29 -0700727 // By default, DISPLAY_P3 is the only supported wide color output. However,
728 // when HDR content is present, hardware composer may be able to handle
729 // BT2020 data space, in that case, the output data space is set to be
730 // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
731 // to respect this and convert non-HDR content to HDR format.
Peiyong Lin13effd12018-07-24 17:01:47 -0700732 if (mUseColorManagement) {
733 Description managedState = mState;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700734 Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
735 Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700736 Dataspace outputStandard =
737 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
738 Dataspace outputTransfer =
739 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700740 bool needsXYZConversion = needsXYZTransformMatrix();
741
Valerie Haueb8e0762018-11-06 10:10:42 -0800742 // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
743 // STANDARD_BT2020, it will be treated as STANDARD_BT709
744 if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
745 inputStandard != Dataspace::STANDARD_BT2020) {
746 inputStandard = Dataspace::STANDARD_BT709;
747 }
748
Peiyong Lina296b0c2018-04-30 16:55:29 -0700749 if (needsXYZConversion) {
750 // The supported input color spaces are standard RGB, Display P3 and BT2020.
751 switch (inputStandard) {
752 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700753 managedState.inputTransformMatrix = mDisplayP3ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700754 break;
755 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700756 managedState.inputTransformMatrix = mBt2020ToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700757 break;
758 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700759 managedState.inputTransformMatrix = mSrgbToXyz;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700760 break;
761 }
762
Peiyong Lin9b03c732018-05-17 10:14:02 -0700763 // The supported output color spaces are BT2020, Display P3 and standard RGB.
Peiyong Lina296b0c2018-04-30 16:55:29 -0700764 switch (outputStandard) {
765 case Dataspace::STANDARD_BT2020:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700766 managedState.outputTransformMatrix = mXyzToBt2020;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700767 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700768 case Dataspace::STANDARD_DCI_P3:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700769 managedState.outputTransformMatrix = mXyzToDisplayP3;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700770 break;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700771 default:
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700772 managedState.outputTransformMatrix = mXyzToSrgb;
Peiyong Lin9b03c732018-05-17 10:14:02 -0700773 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700774 }
775 } else if (inputStandard != outputStandard) {
776 // At this point, the input data space and output data space could be both
777 // HDR data spaces, but they match each other, we do nothing in this case.
778 // In addition to the case above, the input data space could be
779 // - scRGB linear
780 // - scRGB non-linear
781 // - sRGB
782 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800783 // - BT2020
Peiyong Lina296b0c2018-04-30 16:55:29 -0700784 // The output data spaces could be
785 // - sRGB
786 // - Display P3
Valerie Haueb8e0762018-11-06 10:10:42 -0800787 // - BT2020
788 switch (outputStandard) {
789 case Dataspace::STANDARD_BT2020:
790 if (inputStandard == Dataspace::STANDARD_BT709) {
791 managedState.outputTransformMatrix = mSrgbToBt2020;
792 } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
793 managedState.outputTransformMatrix = mDisplayP3ToBt2020;
794 }
795 break;
796 case Dataspace::STANDARD_DCI_P3:
797 if (inputStandard == Dataspace::STANDARD_BT709) {
798 managedState.outputTransformMatrix = mSrgbToDisplayP3;
799 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
800 managedState.outputTransformMatrix = mBt2020ToDisplayP3;
801 }
802 break;
803 default:
804 if (inputStandard == Dataspace::STANDARD_DCI_P3) {
805 managedState.outputTransformMatrix = mDisplayP3ToSrgb;
806 } else if (inputStandard == Dataspace::STANDARD_BT2020) {
807 managedState.outputTransformMatrix = mBt2020ToSrgb;
808 }
809 break;
Peiyong Lina296b0c2018-04-30 16:55:29 -0700810 }
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600811 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700812
813 // we need to convert the RGB value to linear space and convert it back when:
814 // - there is a color matrix that is not an identity matrix, or
815 // - there is an output transform matrix that is not an identity matrix, or
816 // - the input transfer function doesn't match the output transfer function.
Peiyong Lin13effd12018-07-24 17:01:47 -0700817 if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
Chia-I Wud49d6692018-06-27 07:17:41 +0800818 inputTransfer != outputTransfer) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700819 managedState.inputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700820 Description::dataSpaceToTransferFunction(inputTransfer);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700821 managedState.outputTransferFunction =
Peiyong Lin46080ef2018-10-26 18:43:14 -0700822 Description::dataSpaceToTransferFunction(outputTransfer);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700823 }
824
Peiyong Lin13effd12018-07-24 17:01:47 -0700825 ProgramCache::getInstance().useProgram(managedState);
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600826
827 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
828
829 if (outputDebugPPMs) {
Peiyong Lin13effd12018-07-24 17:01:47 -0700830 static uint64_t managedColorFrameCount = 0;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600831 std::ostringstream out;
Peiyong Lin13effd12018-07-24 17:01:47 -0700832 out << "/data/texture_out" << managedColorFrameCount++;
Courtney Goeltzenleuchter5d943892017-03-22 13:46:46 -0600833 writePPM(out.str().c_str(), mVpWidth, mVpHeight);
834 }
835 } else {
836 ProgramCache::getInstance().useProgram(mState);
837
838 glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
839 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700840
841 if (mesh.getTexCoordsSize()) {
842 glDisableVertexAttribArray(Program::texCoords);
843 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700844
845 if (mState.cornerRadius > 0.0f) {
846 glDisableVertexAttribArray(Program::cropCoords);
847 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700848}
849
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800850size_t GLESRenderEngine::getMaxTextureSize() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700851 return mMaxTextureSize;
852}
853
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800854size_t GLESRenderEngine::getMaxViewportDims() const {
Peiyong Linf1bada92018-08-29 09:39:31 -0700855 return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
856}
857
Yiwei Zhang5434a782018-12-05 18:06:32 -0800858void GLESRenderEngine::dump(std::string& result) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700859 const GLExtensions& extensions = GLExtensions::getInstance();
860
Yiwei Zhang5434a782018-12-05 18:06:32 -0800861 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
862 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
Peiyong Linf11f39b2018-09-05 14:37:41 -0700863
Yiwei Zhang5434a782018-12-05 18:06:32 -0800864 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
865 extensions.getVersion());
866 StringAppendF(&result, "%s\n", extensions.getExtensions());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700867
Yiwei Zhang5434a782018-12-05 18:06:32 -0800868 StringAppendF(&result, "RenderEngine program cache size: %zu\n",
869 ProgramCache::getInstance().getSize());
Chia-I Wu56d7b0a2018-10-01 15:13:11 -0700870
Yiwei Zhang5434a782018-12-05 18:06:32 -0800871 StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n",
872 dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
873 dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
Mathias Agopian3f844832013-08-07 21:24:32 -0700874}
875
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800876GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
Peiyong Linf11f39b2018-09-05 14:37:41 -0700877 int major, minor;
878 if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
879 if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
880 ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
881 return GLES_VERSION_1_0;
882 }
883 }
884
885 if (major == 1 && minor == 0) return GLES_VERSION_1_0;
886 if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
887 if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
888 if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
889
890 ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
891 return GLES_VERSION_1_0;
892}
893
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800894EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
895 EGLContext shareContext, bool useContextPriority) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800896 EGLint renderableType = 0;
897 if (config == EGL_NO_CONFIG) {
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800898 renderableType = EGL_OPENGL_ES3_BIT;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800899 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
900 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
901 }
902 EGLint contextClientVersion = 0;
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800903 if (renderableType & EGL_OPENGL_ES3_BIT) {
904 contextClientVersion = 3;
905 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800906 contextClientVersion = 2;
907 } else if (renderableType & EGL_OPENGL_ES_BIT) {
908 contextClientVersion = 1;
909 } else {
910 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
911 }
912
913 std::vector<EGLint> contextAttributes;
914 contextAttributes.reserve(5);
915 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
916 contextAttributes.push_back(contextClientVersion);
917 if (useContextPriority) {
918 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
919 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
920 }
921 contextAttributes.push_back(EGL_NONE);
922
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800923 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
924
925 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
926 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
927 // EGL_NO_CONTEXT so that we can abort.
928 if (config != EGL_NO_CONFIG) {
929 return context;
930 }
931 // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should
932 // try to fall back to GLES 2.
933 contextAttributes[1] = 2;
934 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
935 }
936
937 return context;
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800938}
939
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800940EGLSurface GLESRenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config,
941 int hwcFormat) {
Peiyong Lina5e9f1b2018-11-27 22:49:37 -0800942 EGLConfig dummyConfig = config;
943 if (dummyConfig == EGL_NO_CONFIG) {
944 dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
945 }
946 std::vector<EGLint> attributes;
947 attributes.reserve(5);
948 attributes.push_back(EGL_WIDTH);
949 attributes.push_back(1);
950 attributes.push_back(EGL_HEIGHT);
951 attributes.push_back(1);
952 attributes.push_back(EGL_NONE);
953
954 return eglCreatePbufferSurface(display, dummyConfig, attributes.data());
955}
956
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800957bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700958 const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
959 const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
960 return standard == Dataspace::STANDARD_BT2020 &&
Peiyong Lin46080ef2018-10-26 18:43:14 -0700961 (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700962}
963
964// For convenience, we want to convert the input color space to XYZ color space first,
965// and then convert from XYZ color space to output color space when
966// - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
967// HDR content will be tone-mapped to SDR; Or,
968// - there are HDR PQ and HLG contents presented at the same time, where we want to convert
969// HLG content to PQ content.
970// In either case above, we need to operate the Y value in XYZ color space. Thus, when either
971// input data space or output data space is HDR data space, and the input transfer function
972// doesn't match the output transfer function, we would enable an intermediate transfrom to
973// XYZ color space.
Peiyong Lin7e219eb2018-12-03 05:40:42 -0800974bool GLESRenderEngine::needsXYZTransformMatrix() const {
Peiyong Lina296b0c2018-04-30 16:55:29 -0700975 const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
976 const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
977 const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lin46080ef2018-10-26 18:43:14 -0700978 const Dataspace outputTransfer =
979 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
Peiyong Lina296b0c2018-04-30 16:55:29 -0700980
981 return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
982}
983
Peiyong Lin46080ef2018-10-26 18:43:14 -0700984} // namespace gl
985} // namespace renderengine
986} // namespace android