| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 17 | //#define LOG_NDEBUG 0 | 
|  | 18 | #undef LOG_TAG | 
|  | 19 | #define LOG_TAG "RenderEngine" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 21 |  | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 22 | #include <sched.h> | 
|  | 23 | #include <cmath> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 24 | #include <fstream> | 
|  | 25 | #include <sstream> | 
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 26 | #include <unordered_set> | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 27 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 28 | #include <GLES2/gl2.h> | 
| Mathias Agopian | 458197d | 2013-08-15 14:56:51 -0700 | [diff] [blame] | 29 | #include <GLES2/gl2ext.h> | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 30 | #include <android-base/stringprintf.h> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 31 | #include <cutils/compiler.h> | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 32 | #include <cutils/properties.h> | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 33 | #include <gui/DebugEGLImageTracker.h> | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 34 | #include <renderengine/Mesh.h> | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 35 | #include <renderengine/Texture.h> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 36 | #include <renderengine/private/Description.h> | 
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 37 | #include <sync/sync.h> | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 38 | #include <ui/ColorSpace.h> | 
|  | 39 | #include <ui/DebugUtils.h> | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 40 | #include <ui/GraphicBuffer.h> | 
| Dan Stoza | c187900 | 2014-05-22 15:59:05 -0700 | [diff] [blame] | 41 | #include <ui/Rect.h> | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 42 | #include <ui/Region.h> | 
| Chia-I Wu | 56d7b0a | 2018-10-01 15:13:11 -0700 | [diff] [blame] | 43 | #include <utils/KeyedVector.h> | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 44 | #include <utils/Trace.h> | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 45 | #include "GLESRenderEngine.h" | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 46 | #include "GLExtensions.h" | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 47 | #include "GLFramebuffer.h" | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 48 | #include "GLImage.h" | 
| Vishnu Nair | 16efdbf | 2019-12-10 11:55:42 -0800 | [diff] [blame] | 49 | #include "GLShadowVertexGenerator.h" | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 50 | #include "Program.h" | 
|  | 51 | #include "ProgramCache.h" | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 52 | #include "filters/BlurFilter.h" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 53 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 54 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); | 
|  | 55 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 56 | bool checkGlError(const char* op, int lineNumber) { | 
|  | 57 | bool errorFound = false; | 
|  | 58 | GLint error = glGetError(); | 
|  | 59 | while (error != GL_NO_ERROR) { | 
|  | 60 | errorFound = true; | 
|  | 61 | error = glGetError(); | 
|  | 62 | ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error); | 
|  | 63 | } | 
|  | 64 | return errorFound; | 
|  | 65 | } | 
|  | 66 |  | 
| Courtney Goeltzenleuchter | 4f20f9c | 2017-04-06 08:18:34 -0600 | [diff] [blame] | 67 | static constexpr bool outputDebugPPMs = false; | 
|  | 68 |  | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 69 | void writePPM(const char* basename, GLuint width, GLuint height) { | 
|  | 70 | ALOGV("writePPM #%s: %d x %d", basename, width, height); | 
|  | 71 |  | 
|  | 72 | std::vector<GLubyte> pixels(width * height * 4); | 
|  | 73 | std::vector<GLubyte> outBuffer(width * height * 3); | 
|  | 74 |  | 
|  | 75 | // TODO(courtneygo): We can now have float formats, need | 
|  | 76 | // to remove this code or update to support. | 
|  | 77 | // Make returned pixels fit in uint32_t, one byte per component | 
|  | 78 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data()); | 
|  | 79 | if (checkGlError(__FUNCTION__, __LINE__)) { | 
|  | 80 | return; | 
|  | 81 | } | 
|  | 82 |  | 
|  | 83 | std::string filename(basename); | 
|  | 84 | filename.append(".ppm"); | 
|  | 85 | std::ofstream file(filename.c_str(), std::ios::binary); | 
|  | 86 | if (!file.is_open()) { | 
|  | 87 | ALOGE("Unable to open file: %s", filename.c_str()); | 
|  | 88 | ALOGE("You may need to do: \"adb shell setenforce 0\" to enable " | 
|  | 89 | "surfaceflinger to write debug images"); | 
|  | 90 | return; | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | file << "P6\n"; | 
|  | 94 | file << width << "\n"; | 
|  | 95 | file << height << "\n"; | 
|  | 96 | file << 255 << "\n"; | 
|  | 97 |  | 
|  | 98 | auto ptr = reinterpret_cast<char*>(pixels.data()); | 
|  | 99 | auto outPtr = reinterpret_cast<char*>(outBuffer.data()); | 
|  | 100 | for (int y = height - 1; y >= 0; y--) { | 
|  | 101 | char* data = ptr + y * width * sizeof(uint32_t); | 
|  | 102 |  | 
|  | 103 | for (GLuint x = 0; x < width; x++) { | 
|  | 104 | // Only copy R, G and B components | 
|  | 105 | outPtr[0] = data[0]; | 
|  | 106 | outPtr[1] = data[1]; | 
|  | 107 | outPtr[2] = data[2]; | 
|  | 108 | data += sizeof(uint32_t); | 
|  | 109 | outPtr += 3; | 
|  | 110 | } | 
|  | 111 | } | 
|  | 112 | file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size()); | 
|  | 113 | } | 
|  | 114 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 115 | namespace android { | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 116 | namespace renderengine { | 
|  | 117 | namespace gl { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 118 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 119 | using base::StringAppendF; | 
| Peiyong Lin | 34beb7a | 2018-03-28 11:57:12 -0700 | [diff] [blame] | 120 | using ui::Dataspace; | 
|  | 121 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 122 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, | 
|  | 123 | EGLint wanted, EGLConfig* outConfig) { | 
|  | 124 | EGLint numConfigs = -1, n = 0; | 
|  | 125 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 126 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); | 
|  | 127 | eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n); | 
|  | 128 | configs.resize(n); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 129 |  | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 130 | if (!configs.empty()) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 131 | if (attribute != EGL_NONE) { | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 132 | for (EGLConfig config : configs) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 133 | EGLint value = 0; | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 134 | eglGetConfigAttrib(dpy, config, attribute, &value); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 135 | if (wanted == value) { | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 136 | *outConfig = config; | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 137 | return NO_ERROR; | 
|  | 138 | } | 
|  | 139 | } | 
|  | 140 | } else { | 
|  | 141 | // just pick the first one | 
|  | 142 | *outConfig = configs[0]; | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 143 | return NO_ERROR; | 
|  | 144 | } | 
|  | 145 | } | 
| Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 146 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 147 | return NAME_NOT_FOUND; | 
|  | 148 | } | 
|  | 149 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 150 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, | 
|  | 151 | EGLConfig* config) { | 
|  | 152 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if | 
|  | 153 | // it is to be used with WIFI displays | 
|  | 154 | status_t err; | 
|  | 155 | EGLint wantedAttribute; | 
|  | 156 | EGLint wantedAttributeValue; | 
|  | 157 |  | 
| Peiyong Lin | 8d5688e | 2020-04-18 00:39:41 -0700 | [diff] [blame] | 158 | std::vector<EGLint> attribs; | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 159 | if (renderableType) { | 
| Peiyong Lin | 8d5688e | 2020-04-18 00:39:41 -0700 | [diff] [blame] | 160 | const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format); | 
|  | 161 | const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102; | 
|  | 162 |  | 
|  | 163 | // Default to 8 bits per channel. | 
|  | 164 | const EGLint tmpAttribs[] = { | 
|  | 165 | EGL_RENDERABLE_TYPE, | 
|  | 166 | renderableType, | 
|  | 167 | EGL_RECORDABLE_ANDROID, | 
|  | 168 | EGL_TRUE, | 
|  | 169 | EGL_SURFACE_TYPE, | 
|  | 170 | EGL_WINDOW_BIT | EGL_PBUFFER_BIT, | 
|  | 171 | EGL_FRAMEBUFFER_TARGET_ANDROID, | 
|  | 172 | EGL_TRUE, | 
|  | 173 | EGL_RED_SIZE, | 
|  | 174 | is1010102 ? 10 : 8, | 
|  | 175 | EGL_GREEN_SIZE, | 
|  | 176 | is1010102 ? 10 : 8, | 
|  | 177 | EGL_BLUE_SIZE, | 
|  | 178 | is1010102 ? 10 : 8, | 
|  | 179 | EGL_ALPHA_SIZE, | 
|  | 180 | is1010102 ? 2 : 8, | 
|  | 181 | EGL_NONE, | 
|  | 182 | }; | 
|  | 183 | std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)), | 
|  | 184 | std::back_inserter(attribs)); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 185 | wantedAttribute = EGL_NONE; | 
|  | 186 | wantedAttributeValue = EGL_NONE; | 
|  | 187 | } else { | 
|  | 188 | // if no renderable type specified, fallback to a simplified query | 
|  | 189 | wantedAttribute = EGL_NATIVE_VISUAL_ID; | 
|  | 190 | wantedAttributeValue = format; | 
|  | 191 | } | 
|  | 192 |  | 
| Peiyong Lin | 8d5688e | 2020-04-18 00:39:41 -0700 | [diff] [blame] | 193 | err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue, | 
|  | 194 | config); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 195 | if (err == NO_ERROR) { | 
|  | 196 | EGLint caveat; | 
|  | 197 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) | 
|  | 198 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | return err; | 
|  | 202 | } | 
|  | 203 |  | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 204 | std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(const RenderEngineCreationArgs& args) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 205 | // initialize EGL for the default display | 
|  | 206 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 
|  | 207 | if (!eglInitialize(display, nullptr, nullptr)) { | 
|  | 208 | LOG_ALWAYS_FATAL("failed to initialize EGL"); | 
|  | 209 | } | 
|  | 210 |  | 
|  | 211 | GLExtensions& extensions = GLExtensions::getInstance(); | 
|  | 212 | extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION), | 
|  | 213 | eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS)); | 
|  | 214 |  | 
|  | 215 | // The code assumes that ES2 or later is available if this extension is | 
|  | 216 | // supported. | 
|  | 217 | EGLConfig config = EGL_NO_CONFIG; | 
|  | 218 | if (!extensions.hasNoConfigContext()) { | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 219 | config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 222 | bool useContextPriority = | 
|  | 223 | extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH; | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 224 | EGLContext protectedContext = EGL_NO_CONTEXT; | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 225 | if (args.enableProtectedContext && extensions.hasProtectedContent()) { | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 226 | protectedContext = createEglContext(display, config, nullptr, useContextPriority, | 
|  | 227 | Protection::PROTECTED); | 
|  | 228 | ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context"); | 
|  | 229 | } | 
|  | 230 |  | 
|  | 231 | EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority, | 
|  | 232 | Protection::UNPROTECTED); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 233 |  | 
|  | 234 | // if can't create a GL context, we can only abort. | 
|  | 235 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); | 
|  | 236 |  | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 237 | EGLSurface dummy = EGL_NO_SURFACE; | 
|  | 238 | if (!extensions.hasSurfacelessContext()) { | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 239 | dummy = createDummyEglPbufferSurface(display, config, args.pixelFormat, | 
|  | 240 | Protection::UNPROTECTED); | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 241 | LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer"); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 242 | } | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 243 | EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt); | 
|  | 244 | LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current"); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 245 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), | 
|  | 246 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); | 
|  | 247 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 248 | EGLSurface protectedDummy = EGL_NO_SURFACE; | 
|  | 249 | if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) { | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 250 | protectedDummy = createDummyEglPbufferSurface(display, config, args.pixelFormat, | 
|  | 251 | Protection::PROTECTED); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 252 | ALOGE_IF(protectedDummy == EGL_NO_SURFACE, "can't create protected dummy pbuffer"); | 
|  | 253 | } | 
|  | 254 |  | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 255 | // now figure out what version of GL did we actually get | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 256 | GlesVersion version = parseGlesVersion(extensions.getVersion()); | 
|  | 257 |  | 
| Lucas Dupin | 453932d | 2020-02-03 20:42:25 -0800 | [diff] [blame] | 258 | LOG_ALWAYS_FATAL_IF(args.supportsBackgroundBlur && version < GLES_VERSION_3_0, | 
|  | 259 | "Blurs require OpenGL ES 3.0. Please unset ro.surface_flinger.supports_background_blur"); | 
|  | 260 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 261 | // initialize the renderer while GL is current | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 262 | std::unique_ptr<GLESRenderEngine> engine; | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 263 | switch (version) { | 
|  | 264 | case GLES_VERSION_1_0: | 
|  | 265 | case GLES_VERSION_1_1: | 
|  | 266 | LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run."); | 
|  | 267 | break; | 
|  | 268 | case GLES_VERSION_2_0: | 
|  | 269 | case GLES_VERSION_3_0: | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 270 | engine = std::make_unique<GLESRenderEngine>(args, display, config, ctxt, dummy, | 
|  | 271 | protectedContext, protectedDummy); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 272 | break; | 
|  | 273 | } | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 274 |  | 
|  | 275 | ALOGI("OpenGL ES informations:"); | 
|  | 276 | ALOGI("vendor    : %s", extensions.getVendor()); | 
|  | 277 | ALOGI("renderer  : %s", extensions.getRenderer()); | 
|  | 278 | ALOGI("version   : %s", extensions.getVersion()); | 
|  | 279 | ALOGI("extensions: %s", extensions.getExtensions()); | 
|  | 280 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); | 
|  | 281 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); | 
|  | 282 |  | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 283 | return engine; | 
|  | 284 | } | 
|  | 285 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 286 | EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 287 | status_t err; | 
|  | 288 | EGLConfig config; | 
|  | 289 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 290 | // First try to get an ES3 config | 
|  | 291 | err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 292 | if (err != NO_ERROR) { | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 293 | // If ES3 fails, try to get an ES2 config | 
|  | 294 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 295 | if (err != NO_ERROR) { | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 296 | // If ES2 still doesn't work, probably because we're on the emulator. | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 297 | // try a simplified query | 
|  | 298 | ALOGW("no suitable EGLConfig found, trying a simpler query"); | 
|  | 299 | err = selectEGLConfig(display, format, 0, &config); | 
|  | 300 | if (err != NO_ERROR) { | 
|  | 301 | // this EGL is too lame for android | 
|  | 302 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); | 
|  | 303 | } | 
|  | 304 | } | 
|  | 305 | } | 
|  | 306 |  | 
|  | 307 | if (logConfig) { | 
|  | 308 | // print some debugging info | 
|  | 309 | EGLint r, g, b, a; | 
|  | 310 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); | 
|  | 311 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); | 
|  | 312 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); | 
|  | 313 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); | 
|  | 314 | ALOGI("EGL information:"); | 
|  | 315 | ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR)); | 
|  | 316 | ALOGI("version   : %s", eglQueryString(display, EGL_VERSION)); | 
|  | 317 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); | 
|  | 318 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); | 
|  | 319 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); | 
|  | 320 | } | 
|  | 321 |  | 
|  | 322 | return config; | 
|  | 323 | } | 
|  | 324 |  | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 325 | GLESRenderEngine::GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, | 
|  | 326 | EGLConfig config, EGLContext ctxt, EGLSurface dummy, | 
|  | 327 | EGLContext protectedContext, EGLSurface protectedDummy) | 
|  | 328 | : renderengine::impl::RenderEngine(args), | 
| Alec Mouri | 0a9c7b8 | 2018-11-16 13:05:25 -0800 | [diff] [blame] | 329 | mEGLDisplay(display), | 
|  | 330 | mEGLConfig(config), | 
|  | 331 | mEGLContext(ctxt), | 
|  | 332 | mDummySurface(dummy), | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 333 | mProtectedEGLContext(protectedContext), | 
|  | 334 | mProtectedDummySurface(protectedDummy), | 
| Chia-I Wu | 93e14df | 2018-06-04 10:10:17 -0700 | [diff] [blame] | 335 | mVpWidth(0), | 
|  | 336 | mVpHeight(0), | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 337 | mFramebufferImageCacheSize(args.imageCacheSize), | 
|  | 338 | mUseColorManagement(args.useColorManagement) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 339 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize); | 
|  | 340 | glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims); | 
|  | 341 |  | 
|  | 342 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | 
|  | 343 | glPixelStorei(GL_PACK_ALIGNMENT, 4); | 
|  | 344 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 345 | // Initialize protected EGL Context. | 
|  | 346 | if (mProtectedEGLContext != EGL_NO_CONTEXT) { | 
|  | 347 | EGLBoolean success = eglMakeCurrent(display, mProtectedDummySurface, mProtectedDummySurface, | 
|  | 348 | mProtectedEGLContext); | 
|  | 349 | ALOGE_IF(!success, "can't make protected context current"); | 
|  | 350 | glPixelStorei(GL_UNPACK_ALIGNMENT, 4); | 
|  | 351 | glPixelStorei(GL_PACK_ALIGNMENT, 4); | 
|  | 352 | success = eglMakeCurrent(display, mDummySurface, mDummySurface, mEGLContext); | 
|  | 353 | LOG_ALWAYS_FATAL_IF(!success, "can't make default context current"); | 
|  | 354 | } | 
|  | 355 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 356 | // mColorBlindnessCorrection = M; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 357 |  | 
| Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 358 | if (mUseColorManagement) { | 
| Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 359 | const ColorSpace srgb(ColorSpace::sRGB()); | 
|  | 360 | const ColorSpace displayP3(ColorSpace::DisplayP3()); | 
|  | 361 | const ColorSpace bt2020(ColorSpace::BT2020()); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 362 |  | 
|  | 363 | // no chromatic adaptation needed since all color spaces use D65 for their white points. | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 364 | mSrgbToXyz = mat4(srgb.getRGBtoXYZ()); | 
|  | 365 | mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ()); | 
|  | 366 | mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ()); | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 367 | mXyzToSrgb = mat4(srgb.getXYZtoRGB()); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 368 | mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB()); | 
|  | 369 | mXyzToBt2020 = mat4(bt2020.getXYZtoRGB()); | 
| Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 370 |  | 
|  | 371 | // Compute sRGB to Display P3 and BT2020 transform matrix. | 
|  | 372 | // NOTE: For now, we are limiting output wide color space support to | 
|  | 373 | // Display-P3 and BT2020 only. | 
|  | 374 | mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz; | 
|  | 375 | mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz; | 
|  | 376 |  | 
|  | 377 | // Compute Display P3 to sRGB and BT2020 transform matrix. | 
|  | 378 | mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz; | 
|  | 379 | mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz; | 
|  | 380 |  | 
|  | 381 | // Compute BT2020 to sRGB and Display P3 transform matrix | 
|  | 382 | mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz; | 
|  | 383 | mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 384 | } | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 385 |  | 
|  | 386 | char value[PROPERTY_VALUE_MAX]; | 
|  | 387 | property_get("debug.egl.traceGpuCompletion", value, "0"); | 
|  | 388 | if (atoi(value)) { | 
|  | 389 | mTraceGpuCompletion = true; | 
|  | 390 | mFlushTracer = std::make_unique<FlushTracer>(this); | 
|  | 391 | } | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 392 |  | 
|  | 393 | if (args.supportsBackgroundBlur) { | 
| Lucas Dupin | 93c3ef1 | 2020-02-26 13:25:25 -0800 | [diff] [blame] | 394 | mBlurFilter = new BlurFilter(*this); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 395 | checkErrors("BlurFilter creation"); | 
|  | 396 | } | 
|  | 397 |  | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 398 | mImageManager = std::make_unique<ImageManager>(this); | 
| Alec Mouri | 349fd2d | 2020-02-27 12:06:10 -0800 | [diff] [blame] | 399 | mImageManager->initThread(); | 
| Alec Mouri | 820c740 | 2019-01-23 13:02:39 -0800 | [diff] [blame] | 400 | mDrawingBuffer = createFramebuffer(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 401 | } | 
|  | 402 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 403 | GLESRenderEngine::~GLESRenderEngine() { | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 404 | // Destroy the image manager first. | 
|  | 405 | mImageManager = nullptr; | 
| Alec Mouri | d43ccab | 2019-03-13 12:23:45 -0700 | [diff] [blame] | 406 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 407 | unbindFrameBuffer(mDrawingBuffer.get()); | 
|  | 408 | mDrawingBuffer = nullptr; | 
|  | 409 | while (!mFramebufferImageCache.empty()) { | 
|  | 410 | EGLImageKHR expired = mFramebufferImageCache.front().second; | 
|  | 411 | mFramebufferImageCache.pop_front(); | 
|  | 412 | eglDestroyImageKHR(mEGLDisplay, expired); | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 413 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 414 | } | 
| Alec Mouri | d43ccab | 2019-03-13 12:23:45 -0700 | [diff] [blame] | 415 | mImageCache.clear(); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 416 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 417 | eglTerminate(mEGLDisplay); | 
|  | 418 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 419 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 420 | std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() { | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 421 | return std::make_unique<GLFramebuffer>(*this); | 
|  | 422 | } | 
|  | 423 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 424 | std::unique_ptr<Image> GLESRenderEngine::createImage() { | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 425 | return std::make_unique<GLImage>(*this); | 
|  | 426 | } | 
|  | 427 |  | 
| Alec Mouri | 820c740 | 2019-01-23 13:02:39 -0800 | [diff] [blame] | 428 | Framebuffer* GLESRenderEngine::getFramebufferForDrawing() { | 
|  | 429 | return mDrawingBuffer.get(); | 
|  | 430 | } | 
|  | 431 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 432 | void GLESRenderEngine::primeCache() const { | 
| Peiyong Lin | 4137a1d | 2019-10-09 10:39:09 -0700 | [diff] [blame] | 433 | ProgramCache::getInstance().primeCache(mInProtectedContext ? mProtectedEGLContext : mEGLContext, | 
|  | 434 | mArgs.useColorManagement, | 
|  | 435 | mArgs.precacheToneMapperShaderOnly); | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 436 | } | 
|  | 437 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 438 | base::unique_fd GLESRenderEngine::flush() { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 439 | ATRACE_CALL(); | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 440 | if (!GLExtensions::getInstance().hasNativeFenceSync()) { | 
|  | 441 | return base::unique_fd(); | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); | 
|  | 445 | if (sync == EGL_NO_SYNC_KHR) { | 
|  | 446 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); | 
|  | 447 | return base::unique_fd(); | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | // native fence fd will not be populated until flush() is done. | 
|  | 451 | glFlush(); | 
|  | 452 |  | 
|  | 453 | // get the fence fd | 
|  | 454 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); | 
|  | 455 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 456 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { | 
|  | 457 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); | 
|  | 458 | } | 
|  | 459 |  | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 460 | // Only trace if we have a valid fence, as current usage falls back to | 
|  | 461 | // calling finish() if the fence fd is invalid. | 
|  | 462 | if (CC_UNLIKELY(mTraceGpuCompletion && mFlushTracer) && fenceFd.get() >= 0) { | 
|  | 463 | mFlushTracer->queueSync(eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr)); | 
|  | 464 | } | 
|  | 465 |  | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 466 | return fenceFd; | 
|  | 467 | } | 
|  | 468 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 469 | bool GLESRenderEngine::finish() { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 470 | ATRACE_CALL(); | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 471 | if (!GLExtensions::getInstance().hasFenceSync()) { | 
|  | 472 | ALOGW("no synchronization support"); | 
|  | 473 | return false; | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr); | 
|  | 477 | if (sync == EGL_NO_SYNC_KHR) { | 
|  | 478 | ALOGW("failed to create EGL fence sync: %#x", eglGetError()); | 
|  | 479 | return false; | 
|  | 480 | } | 
|  | 481 |  | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 482 | if (CC_UNLIKELY(mTraceGpuCompletion && mFlushTracer)) { | 
|  | 483 | mFlushTracer->queueSync(eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr)); | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | return waitSync(sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR); | 
|  | 487 | } | 
|  | 488 |  | 
|  | 489 | bool GLESRenderEngine::waitSync(EGLSyncKHR sync, EGLint flags) { | 
|  | 490 | EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, flags, 2000000000 /*2 sec*/); | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 491 | EGLint error = eglGetError(); | 
|  | 492 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 493 | if (result != EGL_CONDITION_SATISFIED_KHR) { | 
|  | 494 | if (result == EGL_TIMEOUT_EXPIRED_KHR) { | 
|  | 495 | ALOGW("fence wait timed out"); | 
|  | 496 | } else { | 
|  | 497 | ALOGW("error waiting on EGL fence: %#x", error); | 
|  | 498 | } | 
|  | 499 | return false; | 
|  | 500 | } | 
|  | 501 |  | 
|  | 502 | return true; | 
|  | 503 | } | 
|  | 504 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 505 | bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 506 | if (!GLExtensions::getInstance().hasNativeFenceSync() || | 
|  | 507 | !GLExtensions::getInstance().hasWaitSync()) { | 
|  | 508 | return false; | 
|  | 509 | } | 
|  | 510 |  | 
| Peiyong Lin | 35a114c | 2019-04-25 14:37:13 -0700 | [diff] [blame] | 511 | // release the fd and transfer the ownership to EGLSync | 
|  | 512 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE}; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 513 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); | 
|  | 514 | if (sync == EGL_NO_SYNC_KHR) { | 
|  | 515 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); | 
|  | 516 | return false; | 
|  | 517 | } | 
|  | 518 |  | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 519 | // XXX: The spec draft is inconsistent as to whether this should return an | 
|  | 520 | // EGLint or void.  Ignore the return value for now, as it's not strictly | 
|  | 521 | // needed. | 
|  | 522 | eglWaitSyncKHR(mEGLDisplay, sync, 0); | 
|  | 523 | EGLint error = eglGetError(); | 
|  | 524 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 525 | if (error != EGL_SUCCESS) { | 
|  | 526 | ALOGE("failed to wait for EGL native fence sync: %#x", error); | 
|  | 527 | return false; | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | return true; | 
|  | 531 | } | 
|  | 532 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 533 | void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 534 | ATRACE_CALL(); | 
|  | 535 | glDisable(GL_BLEND); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 536 | glClearColor(red, green, blue, alpha); | 
|  | 537 | glClear(GL_COLOR_BUFFER_BIT); | 
|  | 538 | } | 
|  | 539 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 540 | void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue, | 
|  | 541 | float alpha) { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 542 | size_t c; | 
|  | 543 | Rect const* r = region.getArray(&c); | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 544 | Mesh mesh = Mesh::Builder() | 
|  | 545 | .setPrimitive(Mesh::TRIANGLES) | 
|  | 546 | .setVertices(c * 6 /* count */, 2 /* size */) | 
|  | 547 | .build(); | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 548 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); | 
|  | 549 | for (size_t i = 0; i < c; i++, r++) { | 
|  | 550 | position[i * 6 + 0].x = r->left; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 551 | position[i * 6 + 0].y = r->top; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 552 | position[i * 6 + 1].x = r->left; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 553 | position[i * 6 + 1].y = r->bottom; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 554 | position[i * 6 + 2].x = r->right; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 555 | position[i * 6 + 2].y = r->bottom; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 556 | position[i * 6 + 3].x = r->left; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 557 | position[i * 6 + 3].y = r->top; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 558 | position[i * 6 + 4].x = r->right; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 559 | position[i * 6 + 4].y = r->bottom; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 560 | position[i * 6 + 5].x = r->right; | 
| Chia-I Wu | 28e3a25 | 2018-09-07 12:05:02 -0700 | [diff] [blame] | 561 | position[i * 6 + 5].y = r->top; | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 562 | } | 
|  | 563 | setupFillWithColor(red, green, blue, alpha); | 
|  | 564 | drawMesh(mesh); | 
|  | 565 | } | 
|  | 566 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 567 | void GLESRenderEngine::setScissor(const Rect& region) { | 
| Peiyong Lin | 1c09761 | 2019-03-22 16:21:46 -0700 | [diff] [blame] | 568 | glScissor(region.left, region.top, region.getWidth(), region.getHeight()); | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 569 | glEnable(GL_SCISSOR_TEST); | 
|  | 570 | } | 
|  | 571 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 572 | void GLESRenderEngine::disableScissor() { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 573 | glDisable(GL_SCISSOR_TEST); | 
|  | 574 | } | 
|  | 575 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 576 | void GLESRenderEngine::genTextures(size_t count, uint32_t* names) { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 577 | glGenTextures(count, names); | 
|  | 578 | } | 
|  | 579 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 580 | void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 581 | glDeleteTextures(count, names); | 
|  | 582 | } | 
|  | 583 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 584 | void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 585 | ATRACE_CALL(); | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 586 | const GLImage& glImage = static_cast<const GLImage&>(image); | 
|  | 587 | const GLenum target = GL_TEXTURE_EXTERNAL_OES; | 
|  | 588 |  | 
|  | 589 | glBindTexture(target, texName); | 
|  | 590 | if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) { | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 591 | glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage())); | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 592 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 593 | } | 
|  | 594 |  | 
| Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 595 | status_t GLESRenderEngine::bindExternalTextureBuffer(uint32_t texName, | 
|  | 596 | const sp<GraphicBuffer>& buffer, | 
|  | 597 | const sp<Fence>& bufferFence) { | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 598 | if (buffer == nullptr) { | 
|  | 599 | return BAD_VALUE; | 
| Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 600 | } | 
|  | 601 |  | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 602 | ATRACE_CALL(); | 
|  | 603 |  | 
|  | 604 | bool found = false; | 
|  | 605 | { | 
|  | 606 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 607 | auto cachedImage = mImageCache.find(buffer->getId()); | 
|  | 608 | found = (cachedImage != mImageCache.end()); | 
|  | 609 | } | 
|  | 610 |  | 
|  | 611 | // If we couldn't find the image in the cache at this time, then either | 
|  | 612 | // SurfaceFlinger messed up registering the buffer ahead of time or we got | 
|  | 613 | // backed up creating other EGLImages. | 
|  | 614 | if (!found) { | 
|  | 615 | status_t cacheResult = mImageManager->cache(buffer); | 
|  | 616 | if (cacheResult != NO_ERROR) { | 
|  | 617 | return cacheResult; | 
|  | 618 | } | 
|  | 619 | } | 
|  | 620 |  | 
|  | 621 | // Whether or not we needed to cache, re-check mImageCache to make sure that | 
|  | 622 | // there's an EGLImage. The current threading model guarantees that we don't | 
|  | 623 | // destroy a cached image until it's really not needed anymore (i.e. this | 
|  | 624 | // function should not be called), so the only possibility is that something | 
|  | 625 | // terrible went wrong and we should just bind something and move on. | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 626 | { | 
|  | 627 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 628 | auto cachedImage = mImageCache.find(buffer->getId()); | 
| Alec Mouri | d7b3a8b | 2019-03-21 11:44:18 -0700 | [diff] [blame] | 629 |  | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 630 | if (cachedImage == mImageCache.end()) { | 
|  | 631 | // We failed creating the image if we got here, so bail out. | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 632 | ALOGE("Failed to create an EGLImage when rendering"); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 633 | bindExternalTextureImage(texName, *createImage()); | 
|  | 634 | return NO_INIT; | 
|  | 635 | } | 
|  | 636 |  | 
|  | 637 | bindExternalTextureImage(texName, *cachedImage->second); | 
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 638 | } | 
|  | 639 |  | 
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 640 | // Wait for the new buffer to be ready. | 
|  | 641 | if (bufferFence != nullptr && bufferFence->isValid()) { | 
|  | 642 | if (GLExtensions::getInstance().hasWaitSync()) { | 
|  | 643 | base::unique_fd fenceFd(bufferFence->dup()); | 
|  | 644 | if (fenceFd == -1) { | 
|  | 645 | ALOGE("error dup'ing fence fd: %d", errno); | 
|  | 646 | return -errno; | 
|  | 647 | } | 
|  | 648 | if (!waitFence(std::move(fenceFd))) { | 
|  | 649 | ALOGE("failed to wait on fence fd"); | 
|  | 650 | return UNKNOWN_ERROR; | 
|  | 651 | } | 
|  | 652 | } else { | 
|  | 653 | status_t err = bufferFence->waitForever("RenderEngine::bindExternalTextureBuffer"); | 
|  | 654 | if (err != NO_ERROR) { | 
|  | 655 | ALOGE("error waiting for fence: %d", err); | 
|  | 656 | return err; | 
|  | 657 | } | 
|  | 658 | } | 
|  | 659 | } | 
| Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 660 |  | 
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 661 | return NO_ERROR; | 
|  | 662 | } | 
|  | 663 |  | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 664 | void GLESRenderEngine::cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) { | 
|  | 665 | mImageManager->cacheAsync(buffer, nullptr); | 
|  | 666 | } | 
|  | 667 |  | 
|  | 668 | std::shared_ptr<ImageManager::Barrier> GLESRenderEngine::cacheExternalTextureBufferForTesting( | 
|  | 669 | const sp<GraphicBuffer>& buffer) { | 
|  | 670 | auto barrier = std::make_shared<ImageManager::Barrier>(); | 
|  | 671 | mImageManager->cacheAsync(buffer, barrier); | 
|  | 672 | return barrier; | 
|  | 673 | } | 
|  | 674 |  | 
|  | 675 | status_t GLESRenderEngine::cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer) { | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 676 | if (buffer == nullptr) { | 
|  | 677 | return BAD_VALUE; | 
|  | 678 | } | 
|  | 679 |  | 
|  | 680 | { | 
|  | 681 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 682 | if (mImageCache.count(buffer->getId()) > 0) { | 
|  | 683 | // If there's already an image then fail fast here. | 
|  | 684 | return NO_ERROR; | 
|  | 685 | } | 
|  | 686 | } | 
|  | 687 | ATRACE_CALL(); | 
|  | 688 |  | 
|  | 689 | // Create the image without holding a lock so that we don't block anything. | 
|  | 690 | std::unique_ptr<Image> newImage = createImage(); | 
|  | 691 |  | 
|  | 692 | bool created = newImage->setNativeWindowBuffer(buffer->getNativeBuffer(), | 
|  | 693 | buffer->getUsage() & GRALLOC_USAGE_PROTECTED); | 
|  | 694 | if (!created) { | 
|  | 695 | ALOGE("Failed to create image. size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d", | 
|  | 696 | buffer->getWidth(), buffer->getHeight(), buffer->getStride(), buffer->getUsage(), | 
|  | 697 | buffer->getPixelFormat()); | 
|  | 698 | return NO_INIT; | 
|  | 699 | } | 
|  | 700 |  | 
|  | 701 | { | 
|  | 702 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 703 | if (mImageCache.count(buffer->getId()) > 0) { | 
|  | 704 | // In theory it's possible for another thread to recache the image, | 
|  | 705 | // so bail out if another thread won. | 
|  | 706 | return NO_ERROR; | 
|  | 707 | } | 
|  | 708 | mImageCache.insert(std::make_pair(buffer->getId(), std::move(newImage))); | 
|  | 709 | } | 
|  | 710 |  | 
|  | 711 | return NO_ERROR; | 
|  | 712 | } | 
|  | 713 |  | 
| Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 714 | void GLESRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) { | 
| Alec Mouri | 16a9940 | 2019-07-29 16:37:30 -0700 | [diff] [blame] | 715 | mImageManager->releaseAsync(bufferId, nullptr); | 
|  | 716 | } | 
|  | 717 |  | 
|  | 718 | std::shared_ptr<ImageManager::Barrier> GLESRenderEngine::unbindExternalTextureBufferForTesting( | 
|  | 719 | uint64_t bufferId) { | 
|  | 720 | auto barrier = std::make_shared<ImageManager::Barrier>(); | 
|  | 721 | mImageManager->releaseAsync(bufferId, barrier); | 
|  | 722 | return barrier; | 
|  | 723 | } | 
|  | 724 |  | 
|  | 725 | void GLESRenderEngine::unbindExternalTextureBufferInternal(uint64_t bufferId) { | 
|  | 726 | std::unique_ptr<Image> image; | 
|  | 727 | { | 
|  | 728 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 729 | const auto& cachedImage = mImageCache.find(bufferId); | 
|  | 730 |  | 
|  | 731 | if (cachedImage != mImageCache.end()) { | 
|  | 732 | ALOGV("Destroying image for buffer: %" PRIu64, bufferId); | 
|  | 733 | // Move the buffer out of cache first, so that we can destroy | 
|  | 734 | // without holding the cache's lock. | 
|  | 735 | image = std::move(cachedImage->second); | 
|  | 736 | mImageCache.erase(bufferId); | 
|  | 737 | return; | 
|  | 738 | } | 
| Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 739 | } | 
| Alec Mouri | b5c4f35 | 2019-02-19 19:46:38 -0800 | [diff] [blame] | 740 | ALOGV("Failed to find image for buffer: %" PRIu64, bufferId); | 
| Alec Mouri | 539319f | 2018-12-19 17:56:23 -0800 | [diff] [blame] | 741 | } | 
|  | 742 |  | 
| Alec Mouri | 7c94edb | 2018-12-03 21:23:26 -0800 | [diff] [blame] | 743 | FloatRect GLESRenderEngine::setupLayerCropping(const LayerSettings& layer, Mesh& mesh) { | 
|  | 744 | // Translate win by the rounded corners rect coordinates, to have all values in | 
|  | 745 | // layer coordinate space. | 
|  | 746 | FloatRect cropWin = layer.geometry.boundaries; | 
|  | 747 | const FloatRect& roundedCornersCrop = layer.geometry.roundedCornersCrop; | 
|  | 748 | cropWin.left -= roundedCornersCrop.left; | 
|  | 749 | cropWin.right -= roundedCornersCrop.left; | 
|  | 750 | cropWin.top -= roundedCornersCrop.top; | 
|  | 751 | cropWin.bottom -= roundedCornersCrop.top; | 
|  | 752 | Mesh::VertexArray<vec2> cropCoords(mesh.getCropCoordArray<vec2>()); | 
|  | 753 | cropCoords[0] = vec2(cropWin.left, cropWin.top); | 
|  | 754 | cropCoords[1] = vec2(cropWin.left, cropWin.top + cropWin.getHeight()); | 
|  | 755 | cropCoords[2] = vec2(cropWin.right, cropWin.top + cropWin.getHeight()); | 
|  | 756 | cropCoords[3] = vec2(cropWin.right, cropWin.top); | 
|  | 757 |  | 
|  | 758 | setupCornerRadiusCropSize(roundedCornersCrop.getWidth(), roundedCornersCrop.getHeight()); | 
|  | 759 | return cropWin; | 
|  | 760 | } | 
|  | 761 |  | 
| Peiyong Lin | 1c09761 | 2019-03-22 16:21:46 -0700 | [diff] [blame] | 762 | void GLESRenderEngine::handleRoundedCorners(const DisplaySettings& display, | 
|  | 763 | const LayerSettings& layer, const Mesh& mesh) { | 
|  | 764 | // We separate the layer into 3 parts essentially, such that we only turn on blending for the | 
|  | 765 | // top rectangle and the bottom rectangle, and turn off blending for the middle rectangle. | 
|  | 766 | FloatRect bounds = layer.geometry.roundedCornersCrop; | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 767 |  | 
| Alec Mouri | d4bf795 | 2020-04-06 20:28:16 -0700 | [diff] [blame] | 768 | // Explicitly compute the transform from the clip rectangle to the physical | 
|  | 769 | // display. Normally, this is done in glViewport but we explicitly compute | 
|  | 770 | // it here so that we can get the scissor bounds correct. | 
|  | 771 | const Rect& source = display.clip; | 
|  | 772 | const Rect& destination = display.physicalDisplay; | 
|  | 773 | // Here we compute the following transform: | 
|  | 774 | // 1. Translate the top left corner of the source clip to (0, 0) | 
|  | 775 | // 2. Rotate the clip rectangle about the origin in accordance with the | 
|  | 776 | // orientation flag | 
|  | 777 | // 3. Translate the top left corner back to the origin. | 
|  | 778 | // 4. Scale the clip rectangle to the destination rectangle dimensions | 
|  | 779 | // 5. Translate the top left corner to the destination rectangle's top left | 
|  | 780 | // corner. | 
|  | 781 | const mat4 translateSource = mat4::translate(vec4(-source.left, -source.top, 0, 1)); | 
|  | 782 | mat4 rotation; | 
|  | 783 | int displacementX = 0; | 
|  | 784 | int displacementY = 0; | 
|  | 785 | float destinationWidth = static_cast<float>(destination.getWidth()); | 
|  | 786 | float destinationHeight = static_cast<float>(destination.getHeight()); | 
|  | 787 | float sourceWidth = static_cast<float>(source.getWidth()); | 
|  | 788 | float sourceHeight = static_cast<float>(source.getHeight()); | 
|  | 789 | const float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f; | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 790 | switch (display.orientation) { | 
|  | 791 | case ui::Transform::ROT_90: | 
| Alec Mouri | d4bf795 | 2020-04-06 20:28:16 -0700 | [diff] [blame] | 792 | rotation = mat4::rotate(rot90InRadians, vec3(0, 0, 1)); | 
|  | 793 | displacementX = source.getHeight(); | 
|  | 794 | std::swap(sourceHeight, sourceWidth); | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 795 | break; | 
|  | 796 | case ui::Transform::ROT_180: | 
| Alec Mouri | d4bf795 | 2020-04-06 20:28:16 -0700 | [diff] [blame] | 797 | rotation = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1)); | 
|  | 798 | displacementY = source.getHeight(); | 
|  | 799 | displacementX = source.getWidth(); | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 800 | break; | 
|  | 801 | case ui::Transform::ROT_270: | 
| Alec Mouri | d4bf795 | 2020-04-06 20:28:16 -0700 | [diff] [blame] | 802 | rotation = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1)); | 
|  | 803 | displacementY = source.getWidth(); | 
|  | 804 | std::swap(sourceHeight, sourceWidth); | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 805 | break; | 
|  | 806 | default: | 
|  | 807 | break; | 
|  | 808 | } | 
|  | 809 |  | 
| Alec Mouri | d4bf795 | 2020-04-06 20:28:16 -0700 | [diff] [blame] | 810 | const mat4 intermediateTranslation = mat4::translate(vec4(displacementX, displacementY, 0, 1)); | 
|  | 811 | const mat4 scale = mat4::scale( | 
|  | 812 | vec4(destinationWidth / sourceWidth, destinationHeight / sourceHeight, 1, 1)); | 
|  | 813 | const mat4 translateDestination = | 
|  | 814 | mat4::translate(vec4(destination.left, destination.top, 0, 1)); | 
|  | 815 | const mat4 globalTransform = | 
|  | 816 | translateDestination * scale * intermediateTranslation * rotation * translateSource; | 
|  | 817 |  | 
|  | 818 | const mat4 transformMatrix = globalTransform * layer.geometry.positionTransform; | 
|  | 819 | const vec4 leftTopCoordinate(bounds.left, bounds.top, 1.0, 1.0); | 
|  | 820 | const vec4 rightBottomCoordinate(bounds.right, bounds.bottom, 1.0, 1.0); | 
|  | 821 | const vec4 leftTopCoordinateInBuffer = transformMatrix * leftTopCoordinate; | 
|  | 822 | const vec4 rightBottomCoordinateInBuffer = transformMatrix * rightBottomCoordinate; | 
|  | 823 | bounds = FloatRect(std::min(leftTopCoordinateInBuffer[0], rightBottomCoordinateInBuffer[0]), | 
|  | 824 | std::min(leftTopCoordinateInBuffer[1], rightBottomCoordinateInBuffer[1]), | 
|  | 825 | std::max(leftTopCoordinateInBuffer[0], rightBottomCoordinateInBuffer[0]), | 
|  | 826 | std::max(leftTopCoordinateInBuffer[1], rightBottomCoordinateInBuffer[1])); | 
|  | 827 |  | 
| Peiyong Lin | b192596 | 2019-04-01 16:37:10 -0700 | [diff] [blame] | 828 | // Finally, we cut the layer into 3 parts, with top and bottom parts having rounded corners | 
|  | 829 | // and the middle part without rounded corners. | 
|  | 830 | const int32_t radius = ceil(layer.geometry.roundedCornersRadius); | 
| Peiyong Lin | 1c09761 | 2019-03-22 16:21:46 -0700 | [diff] [blame] | 831 | const Rect topRect(bounds.left, bounds.top, bounds.right, bounds.top + radius); | 
|  | 832 | setScissor(topRect); | 
|  | 833 | drawMesh(mesh); | 
|  | 834 | const Rect bottomRect(bounds.left, bounds.bottom - radius, bounds.right, bounds.bottom); | 
|  | 835 | setScissor(bottomRect); | 
|  | 836 | drawMesh(mesh); | 
|  | 837 |  | 
|  | 838 | // The middle part of the layer can turn off blending. | 
| Lucas Dupin | c9691d5 | 2019-09-26 13:46:04 -0700 | [diff] [blame] | 839 | if (topRect.bottom < bottomRect.top) { | 
|  | 840 | const Rect middleRect(bounds.left, bounds.top + radius, bounds.right, | 
|  | 841 | bounds.bottom - radius); | 
|  | 842 | setScissor(middleRect); | 
|  | 843 | mState.cornerRadius = 0.0; | 
|  | 844 | disableBlending(); | 
|  | 845 | drawMesh(mesh); | 
|  | 846 | } | 
| Peiyong Lin | 1c09761 | 2019-03-22 16:21:46 -0700 | [diff] [blame] | 847 | disableScissor(); | 
|  | 848 | } | 
|  | 849 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 850 | status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 851 | ATRACE_CALL(); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 852 | GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer); | 
|  | 853 | EGLImageKHR eglImage = glFramebuffer->getEGLImage(); | 
|  | 854 | uint32_t textureName = glFramebuffer->getTextureName(); | 
|  | 855 | uint32_t framebufferName = glFramebuffer->getFramebufferName(); | 
|  | 856 |  | 
|  | 857 | // Bind the texture and turn our EGLImage into a texture | 
|  | 858 | glBindTexture(GL_TEXTURE_2D, textureName); | 
|  | 859 | glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage); | 
|  | 860 |  | 
|  | 861 | // Bind the Framebuffer to render into | 
|  | 862 | glBindFramebuffer(GL_FRAMEBUFFER, framebufferName); | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 863 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 864 |  | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 865 | uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 866 | ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d", | 
|  | 867 | glStatus); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 868 |  | 
|  | 869 | return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE; | 
|  | 870 | } | 
|  | 871 |  | 
| Alec Mouri | 4dde178 | 2019-09-30 17:27:13 -0700 | [diff] [blame] | 872 | void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /*framebuffer*/) { | 
| Alec Mouri | 820c740 | 2019-01-23 13:02:39 -0800 | [diff] [blame] | 873 | ATRACE_CALL(); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 874 |  | 
|  | 875 | // back to main framebuffer | 
|  | 876 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | 
| Peiyong Lin | e5a9a7f | 2018-08-30 15:32:13 -0700 | [diff] [blame] | 877 | } | 
|  | 878 |  | 
| Alec Mouri | 4dde178 | 2019-09-30 17:27:13 -0700 | [diff] [blame] | 879 | bool GLESRenderEngine::cleanupPostRender() { | 
|  | 880 | ATRACE_CALL(); | 
|  | 881 |  | 
|  | 882 | if (mPriorResourcesCleaned || | 
|  | 883 | (mLastDrawFence != nullptr && mLastDrawFence->getStatus() != Fence::Status::Signaled)) { | 
|  | 884 | // If we don't have a prior frame needing cleanup, then don't do anything. | 
|  | 885 | return false; | 
|  | 886 | } | 
|  | 887 |  | 
|  | 888 | // Bind the texture to dummy data so that backing image data can be freed. | 
|  | 889 | GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(getFramebufferForDrawing()); | 
|  | 890 | glFramebuffer->allocateBuffers(1, 1, mPlaceholderDrawBuffer); | 
|  | 891 | // Release the cached fence here, so that we don't churn reallocations when | 
|  | 892 | // we could no-op repeated calls of this method instead. | 
|  | 893 | mLastDrawFence = nullptr; | 
|  | 894 | mPriorResourcesCleaned = true; | 
|  | 895 | return true; | 
|  | 896 | } | 
|  | 897 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 898 | void GLESRenderEngine::checkErrors() const { | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 899 | checkErrors(nullptr); | 
|  | 900 | } | 
|  | 901 |  | 
|  | 902 | void GLESRenderEngine::checkErrors(const char* tag) const { | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 903 | do { | 
|  | 904 | // there could be more than one error flag | 
|  | 905 | GLenum error = glGetError(); | 
|  | 906 | if (error == GL_NO_ERROR) break; | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 907 | if (tag == nullptr) { | 
|  | 908 | ALOGE("GL error 0x%04x", int(error)); | 
|  | 909 | } else { | 
|  | 910 | ALOGE("GL error: %s -> 0x%04x", tag, int(error)); | 
|  | 911 | } | 
| Peiyong Lin | 60bedb5 | 2018-09-05 10:47:31 -0700 | [diff] [blame] | 912 | } while (true); | 
|  | 913 | } | 
|  | 914 |  | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 915 | bool GLESRenderEngine::supportsProtectedContent() const { | 
|  | 916 | return mProtectedEGLContext != EGL_NO_CONTEXT; | 
|  | 917 | } | 
|  | 918 |  | 
|  | 919 | bool GLESRenderEngine::useProtectedContext(bool useProtectedContext) { | 
|  | 920 | if (useProtectedContext == mInProtectedContext) { | 
|  | 921 | return true; | 
|  | 922 | } | 
|  | 923 | if (useProtectedContext && mProtectedEGLContext == EGL_NO_CONTEXT) { | 
|  | 924 | return false; | 
|  | 925 | } | 
|  | 926 | const EGLSurface surface = useProtectedContext ? mProtectedDummySurface : mDummySurface; | 
|  | 927 | const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext; | 
|  | 928 | const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE; | 
|  | 929 | if (success) { | 
|  | 930 | mInProtectedContext = useProtectedContext; | 
|  | 931 | } | 
|  | 932 | return success; | 
|  | 933 | } | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 934 | EGLImageKHR GLESRenderEngine::createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer, | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 935 | bool isProtected, | 
|  | 936 | bool useFramebufferCache) { | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 937 | sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(nativeBuffer); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 938 | if (useFramebufferCache) { | 
| Alec Mouri | ef44c2d | 2019-07-15 15:27:22 -0700 | [diff] [blame] | 939 | std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 940 | for (const auto& image : mFramebufferImageCache) { | 
|  | 941 | if (image.first == graphicBuffer->getId()) { | 
|  | 942 | return image.second; | 
|  | 943 | } | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 944 | } | 
|  | 945 | } | 
|  | 946 | EGLint attributes[] = { | 
|  | 947 | isProtected ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE, | 
|  | 948 | isProtected ? EGL_TRUE : EGL_NONE, | 
|  | 949 | EGL_NONE, | 
|  | 950 | }; | 
|  | 951 | EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID, | 
|  | 952 | nativeBuffer, attributes); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 953 | if (useFramebufferCache) { | 
|  | 954 | if (image != EGL_NO_IMAGE_KHR) { | 
| Alec Mouri | ef44c2d | 2019-07-15 15:27:22 -0700 | [diff] [blame] | 955 | std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 956 | if (mFramebufferImageCache.size() >= mFramebufferImageCacheSize) { | 
|  | 957 | EGLImageKHR expired = mFramebufferImageCache.front().second; | 
|  | 958 | mFramebufferImageCache.pop_front(); | 
|  | 959 | eglDestroyImageKHR(mEGLDisplay, expired); | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 960 | DEBUG_EGL_IMAGE_TRACKER_DESTROY(); | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 961 | } | 
|  | 962 | mFramebufferImageCache.push_back({graphicBuffer->getId(), image}); | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 963 | } | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 964 | } | 
| Ady Abraham | a3b08ef | 2019-07-15 18:43:10 -0700 | [diff] [blame] | 965 |  | 
|  | 966 | if (image != EGL_NO_IMAGE_KHR) { | 
|  | 967 | DEBUG_EGL_IMAGE_TRACKER_CREATE(); | 
|  | 968 | } | 
| Alec Mouri | da4cf3b | 2019-02-12 15:33:01 -0800 | [diff] [blame] | 969 | return image; | 
|  | 970 | } | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 971 |  | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 972 | status_t GLESRenderEngine::drawLayers(const DisplaySettings& display, | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 973 | const std::vector<const LayerSettings*>& layers, | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 974 | ANativeWindowBuffer* const buffer, | 
| Alec Mouri | fe0d72b | 2019-03-21 14:05:56 -0700 | [diff] [blame] | 975 | const bool useFramebufferCache, base::unique_fd&& bufferFence, | 
|  | 976 | base::unique_fd* drawFence) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 977 | ATRACE_CALL(); | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 978 | if (layers.empty()) { | 
|  | 979 | ALOGV("Drawing empty layer stack"); | 
|  | 980 | return NO_ERROR; | 
|  | 981 | } | 
|  | 982 |  | 
| Alec Mouri | 8e82b8d | 2020-03-09 16:11:00 -0700 | [diff] [blame] | 983 | if (bufferFence.get() >= 0) { | 
|  | 984 | // Duplicate the fence for passing to waitFence. | 
|  | 985 | base::unique_fd bufferFenceDup(dup(bufferFence.get())); | 
|  | 986 | if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) { | 
|  | 987 | ATRACE_NAME("Waiting before draw"); | 
|  | 988 | sync_wait(bufferFence.get(), -1); | 
|  | 989 | } | 
| Alec Mouri | 6338c9d | 2019-02-07 16:57:51 -0800 | [diff] [blame] | 990 | } | 
|  | 991 |  | 
| Alec Mouri | d43ccab | 2019-03-13 12:23:45 -0700 | [diff] [blame] | 992 | if (buffer == nullptr) { | 
|  | 993 | ALOGE("No output buffer provided. Aborting GPU composition."); | 
|  | 994 | return BAD_VALUE; | 
|  | 995 | } | 
|  | 996 |  | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 997 | std::unique_ptr<BindNativeBufferAsFramebuffer> fbo; | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 998 | // Gathering layers that requested blur, we'll need them to decide when to render to an | 
|  | 999 | // offscreen buffer, and when to render to the native buffer. | 
|  | 1000 | std::deque<const LayerSettings*> blurLayers; | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1001 | if (CC_LIKELY(mBlurFilter != nullptr)) { | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1002 | for (auto layer : layers) { | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1003 | if (layer->backgroundBlurRadius > 0) { | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1004 | blurLayers.push_back(layer); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1005 | } | 
|  | 1006 | } | 
|  | 1007 | } | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1008 | const auto blurLayersSize = blurLayers.size(); | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 1009 |  | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1010 | if (blurLayersSize == 0) { | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1011 | fbo = std::make_unique<BindNativeBufferAsFramebuffer>(*this, buffer, useFramebufferCache); | 
|  | 1012 | if (fbo->getStatus() != NO_ERROR) { | 
|  | 1013 | ALOGE("Failed to bind framebuffer! Aborting GPU composition for buffer (%p).", | 
|  | 1014 | buffer->handle); | 
|  | 1015 | checkErrors(); | 
|  | 1016 | return fbo->getStatus(); | 
|  | 1017 | } | 
|  | 1018 | setViewportAndProjection(display.physicalDisplay, display.clip); | 
|  | 1019 | } else { | 
|  | 1020 | setViewportAndProjection(display.physicalDisplay, display.clip); | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1021 | auto status = | 
|  | 1022 | mBlurFilter->setAsDrawTarget(display, blurLayers.front()->backgroundBlurRadius); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1023 | if (status != NO_ERROR) { | 
|  | 1024 | ALOGE("Failed to prepare blur filter! Aborting GPU composition for buffer (%p).", | 
|  | 1025 | buffer->handle); | 
|  | 1026 | checkErrors(); | 
|  | 1027 | return status; | 
|  | 1028 | } | 
| Alec Mouri | f049727 | 2019-03-11 15:53:11 -0700 | [diff] [blame] | 1029 | } | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1030 |  | 
|  | 1031 | // clear the entire buffer, sometimes when we reuse buffers we'd persist | 
|  | 1032 | // ghost images otherwise. | 
|  | 1033 | // we also require a full transparent framebuffer for overlays. This is | 
|  | 1034 | // probably not quite efficient on all GPUs, since we could filter out | 
|  | 1035 | // opaque layers. | 
|  | 1036 | clearWithColor(0.0, 0.0, 0.0, 0.0); | 
|  | 1037 |  | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1038 | setOutputDataSpace(display.outputDataspace); | 
|  | 1039 | setDisplayMaxLuminance(display.maxLuminance); | 
|  | 1040 |  | 
| Alec Mouri | 5a6d857 | 2020-03-23 23:56:15 -0700 | [diff] [blame] | 1041 | const mat4 projectionMatrix = | 
|  | 1042 | ui::Transform(display.orientation).asMatrix4() * mState.projectionMatrix; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1043 | if (!display.clearRegion.isEmpty()) { | 
|  | 1044 | glDisable(GL_BLEND); | 
|  | 1045 | fillRegionWithColor(display.clearRegion, 0.0, 0.0, 0.0, 1.0); | 
|  | 1046 | } | 
|  | 1047 |  | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1048 | Mesh mesh = Mesh::Builder() | 
|  | 1049 | .setPrimitive(Mesh::TRIANGLE_FAN) | 
|  | 1050 | .setVertices(4 /* count */, 2 /* size */) | 
|  | 1051 | .setTexCoords(2 /* size */) | 
|  | 1052 | .setCropCoords(2 /* size */) | 
|  | 1053 | .build(); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1054 | for (auto const layer : layers) { | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1055 | if (blurLayers.size() > 0 && blurLayers.front() == layer) { | 
|  | 1056 | blurLayers.pop_front(); | 
|  | 1057 |  | 
| Lucas Dupin | f82ed8f | 2020-01-17 12:11:07 -0800 | [diff] [blame] | 1058 | auto status = mBlurFilter->prepare(); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1059 | if (status != NO_ERROR) { | 
|  | 1060 | ALOGE("Failed to render blur effect! Aborting GPU composition for buffer (%p).", | 
|  | 1061 | buffer->handle); | 
|  | 1062 | checkErrors("Can't render first blur pass"); | 
|  | 1063 | return status; | 
|  | 1064 | } | 
|  | 1065 |  | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1066 | if (blurLayers.size() == 0) { | 
|  | 1067 | // Done blurring, time to bind the native FBO and render our blur onto it. | 
|  | 1068 | fbo = std::make_unique<BindNativeBufferAsFramebuffer>(*this, buffer, | 
|  | 1069 | useFramebufferCache); | 
|  | 1070 | status = fbo->getStatus(); | 
|  | 1071 | setViewportAndProjection(display.physicalDisplay, display.clip); | 
|  | 1072 | } else { | 
|  | 1073 | // There's still something else to blur, so let's keep rendering to our FBO | 
|  | 1074 | // instead of to the display. | 
|  | 1075 | status = mBlurFilter->setAsDrawTarget(display, | 
|  | 1076 | blurLayers.front()->backgroundBlurRadius); | 
|  | 1077 | } | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1078 | if (status != NO_ERROR) { | 
|  | 1079 | ALOGE("Failed to bind framebuffer! Aborting GPU composition for buffer (%p).", | 
|  | 1080 | buffer->handle); | 
|  | 1081 | checkErrors("Can't bind native framebuffer"); | 
|  | 1082 | return status; | 
|  | 1083 | } | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1084 |  | 
| Lucas Dupin | a6f8e13 | 2020-02-06 18:37:10 -0800 | [diff] [blame] | 1085 | status = mBlurFilter->render(blurLayersSize > 1); | 
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 1086 | if (status != NO_ERROR) { | 
|  | 1087 | ALOGE("Failed to render blur effect! Aborting GPU composition for buffer (%p).", | 
|  | 1088 | buffer->handle); | 
|  | 1089 | checkErrors("Can't render blur filter"); | 
|  | 1090 | return status; | 
|  | 1091 | } | 
|  | 1092 | } | 
|  | 1093 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1094 | mState.maxMasteringLuminance = layer->source.buffer.maxMasteringLuminance; | 
|  | 1095 | mState.maxContentLuminance = layer->source.buffer.maxContentLuminance; | 
|  | 1096 | mState.projectionMatrix = projectionMatrix * layer->geometry.positionTransform; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1097 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1098 | const FloatRect bounds = layer->geometry.boundaries; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1099 | Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>()); | 
|  | 1100 | position[0] = vec2(bounds.left, bounds.top); | 
|  | 1101 | position[1] = vec2(bounds.left, bounds.bottom); | 
|  | 1102 | position[2] = vec2(bounds.right, bounds.bottom); | 
|  | 1103 | position[3] = vec2(bounds.right, bounds.top); | 
|  | 1104 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1105 | setupLayerCropping(*layer, mesh); | 
|  | 1106 | setColorTransform(display.colorTransform * layer->colorTransform); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1107 |  | 
|  | 1108 | bool usePremultipliedAlpha = true; | 
|  | 1109 | bool disableTexture = true; | 
|  | 1110 | bool isOpaque = false; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1111 | if (layer->source.buffer.buffer != nullptr) { | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1112 | disableTexture = false; | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1113 | isOpaque = layer->source.buffer.isOpaque; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1114 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1115 | sp<GraphicBuffer> gBuf = layer->source.buffer.buffer; | 
|  | 1116 | bindExternalTextureBuffer(layer->source.buffer.textureName, gBuf, | 
|  | 1117 | layer->source.buffer.fence); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1118 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1119 | usePremultipliedAlpha = layer->source.buffer.usePremultipliedAlpha; | 
|  | 1120 | Texture texture(Texture::TEXTURE_EXTERNAL, layer->source.buffer.textureName); | 
|  | 1121 | mat4 texMatrix = layer->source.buffer.textureTransform; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1122 |  | 
|  | 1123 | texture.setMatrix(texMatrix.asArray()); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1124 | texture.setFiltering(layer->source.buffer.useTextureFiltering); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1125 |  | 
|  | 1126 | texture.setDimensions(gBuf->getWidth(), gBuf->getHeight()); | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1127 | setSourceY410BT2020(layer->source.buffer.isY410BT2020); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1128 |  | 
|  | 1129 | renderengine::Mesh::VertexArray<vec2> texCoords(mesh.getTexCoordArray<vec2>()); | 
|  | 1130 | texCoords[0] = vec2(0.0, 0.0); | 
|  | 1131 | texCoords[1] = vec2(0.0, 1.0); | 
|  | 1132 | texCoords[2] = vec2(1.0, 1.0); | 
|  | 1133 | texCoords[3] = vec2(1.0, 0.0); | 
|  | 1134 | setupLayerTexturing(texture); | 
|  | 1135 | } | 
|  | 1136 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1137 | const half3 solidColor = layer->source.solidColor; | 
|  | 1138 | const half4 color = half4(solidColor.r, solidColor.g, solidColor.b, layer->alpha); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1139 | // Buffer sources will have a black solid color ignored in the shader, | 
|  | 1140 | // so in that scenario the solid color passed here is arbitrary. | 
|  | 1141 | setupLayerBlending(usePremultipliedAlpha, isOpaque, disableTexture, color, | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1142 | layer->geometry.roundedCornersRadius); | 
|  | 1143 | if (layer->disableBlending) { | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1144 | glDisable(GL_BLEND); | 
|  | 1145 | } | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1146 | setSourceDataSpace(layer->sourceDataspace); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1147 |  | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1148 | if (layer->shadow.length > 0.0f) { | 
|  | 1149 | handleShadow(layer->geometry.boundaries, layer->geometry.roundedCornersRadius, | 
|  | 1150 | layer->shadow); | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1151 | } | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1152 | // We only want to do a special handling for rounded corners when having rounded corners | 
|  | 1153 | // is the only reason it needs to turn on blending, otherwise, we handle it like the | 
|  | 1154 | // usual way since it needs to turn on blending anyway. | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1155 | else if (layer->geometry.roundedCornersRadius > 0.0 && color.a >= 1.0f && isOpaque) { | 
|  | 1156 | handleRoundedCorners(display, *layer, mesh); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1157 | } else { | 
|  | 1158 | drawMesh(mesh); | 
|  | 1159 | } | 
|  | 1160 |  | 
|  | 1161 | // Cleanup if there's a buffer source | 
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 1162 | if (layer->source.buffer.buffer != nullptr) { | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1163 | disableBlending(); | 
|  | 1164 | setSourceY410BT2020(false); | 
|  | 1165 | disableTexturing(); | 
|  | 1166 | } | 
|  | 1167 | } | 
|  | 1168 |  | 
|  | 1169 | if (drawFence != nullptr) { | 
|  | 1170 | *drawFence = flush(); | 
|  | 1171 | } | 
|  | 1172 | // If flush failed or we don't support native fences, we need to force the | 
|  | 1173 | // gl command stream to be executed. | 
|  | 1174 | if (drawFence == nullptr || drawFence->get() < 0) { | 
|  | 1175 | bool success = finish(); | 
|  | 1176 | if (!success) { | 
|  | 1177 | ALOGE("Failed to flush RenderEngine commands"); | 
|  | 1178 | checkErrors(); | 
|  | 1179 | // Chances are, something illegal happened (either the caller passed | 
|  | 1180 | // us bad parameters, or we messed up our shader generation). | 
|  | 1181 | return INVALID_OPERATION; | 
|  | 1182 | } | 
| Alec Mouri | 4dde178 | 2019-09-30 17:27:13 -0700 | [diff] [blame] | 1183 | mLastDrawFence = nullptr; | 
|  | 1184 | } else { | 
|  | 1185 | // The caller takes ownership of drawFence, so we need to duplicate the | 
|  | 1186 | // fd here. | 
|  | 1187 | mLastDrawFence = new Fence(dup(drawFence->get())); | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1188 | } | 
| Alec Mouri | 4dde178 | 2019-09-30 17:27:13 -0700 | [diff] [blame] | 1189 | mPriorResourcesCleaned = false; | 
| Alec Mouri | 3d7c561 | 2019-07-09 13:51:37 -0700 | [diff] [blame] | 1190 |  | 
|  | 1191 | checkErrors(); | 
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 1192 | return NO_ERROR; | 
|  | 1193 | } | 
|  | 1194 |  | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 1195 | void GLESRenderEngine::setViewportAndProjection(Rect viewport, Rect clip) { | 
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 1196 | ATRACE_CALL(); | 
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 1197 | mVpWidth = viewport.getWidth(); | 
|  | 1198 | mVpHeight = viewport.getHeight(); | 
|  | 1199 |  | 
|  | 1200 | // We pass the the top left corner instead of the bottom left corner, | 
|  | 1201 | // because since we're rendering off-screen first. | 
|  | 1202 | glViewport(viewport.left, viewport.top, mVpWidth, mVpHeight); | 
|  | 1203 |  | 
|  | 1204 | mState.projectionMatrix = mat4::ortho(clip.left, clip.right, clip.top, clip.bottom, 0, 1); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1205 | } | 
|  | 1206 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1207 | void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture, | 
|  | 1208 | const half4& color, float cornerRadius) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1209 | mState.isPremultipliedAlpha = premultipliedAlpha; | 
|  | 1210 | mState.isOpaque = opaque; | 
|  | 1211 | mState.color = color; | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 1212 | mState.cornerRadius = cornerRadius; | 
| Dan Stoza | 9e56aa0 | 2015-11-02 13:00:03 -0800 | [diff] [blame] | 1213 |  | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1214 | if (disableTexture) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1215 | mState.textureEnabled = false; | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 1216 | } | 
| Fabien Sanglard | 9d96de4 | 2016-10-11 00:15:18 +0000 | [diff] [blame] | 1217 |  | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 1218 | if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1219 | glEnable(GL_BLEND); | 
|  | 1220 | glBlendFunc(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 
|  | 1221 | } else { | 
|  | 1222 | glDisable(GL_BLEND); | 
|  | 1223 | } | 
|  | 1224 | } | 
|  | 1225 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1226 | void GLESRenderEngine::setSourceY410BT2020(bool enable) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1227 | mState.isY410BT2020 = enable; | 
| Chia-I Wu | 131d376 | 2018-01-11 14:35:27 -0800 | [diff] [blame] | 1228 | } | 
|  | 1229 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1230 | void GLESRenderEngine::setSourceDataSpace(Dataspace source) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 1231 | mDataSpace = source; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1232 | } | 
|  | 1233 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1234 | void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) { | 
| Chia-I Wu | 69bf10f | 2018-02-20 13:04:50 -0800 | [diff] [blame] | 1235 | mOutputDataSpace = dataspace; | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1236 | } | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1237 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1238 | void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1239 | mState.displayMaxLuminance = maxLuminance; | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 1240 | } | 
|  | 1241 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1242 | void GLESRenderEngine::setupLayerTexturing(const Texture& texture) { | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 1243 | GLuint target = texture.getTextureTarget(); | 
|  | 1244 | glBindTexture(target, texture.getTextureName()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1245 | GLenum filter = GL_NEAREST; | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 1246 | if (texture.getFiltering()) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1247 | filter = GL_LINEAR; | 
|  | 1248 | } | 
| Mathias Agopian | 49457ac | 2013-08-14 18:20:17 -0700 | [diff] [blame] | 1249 | glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 
|  | 1250 | glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 
|  | 1251 | glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter); | 
|  | 1252 | glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1253 |  | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1254 | mState.texture = texture; | 
|  | 1255 | mState.textureEnabled = true; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1256 | } | 
|  | 1257 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1258 | void GLESRenderEngine::setColorTransform(const mat4& colorTransform) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1259 | mState.colorMatrix = colorTransform; | 
| Dan Stoza | f008799 | 2014-10-20 15:46:09 -0700 | [diff] [blame] | 1260 | } | 
|  | 1261 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1262 | void GLESRenderEngine::disableTexturing() { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1263 | mState.textureEnabled = false; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1264 | } | 
|  | 1265 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1266 | void GLESRenderEngine::disableBlending() { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1267 | glDisable(GL_BLEND); | 
|  | 1268 | } | 
|  | 1269 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1270 | void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1271 | mState.isPremultipliedAlpha = true; | 
|  | 1272 | mState.isOpaque = false; | 
|  | 1273 | mState.color = half4(r, g, b, a); | 
|  | 1274 | mState.textureEnabled = false; | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1275 | glDisable(GL_BLEND); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1276 | } | 
|  | 1277 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1278 | void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) { | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 1279 | mState.cropSize = half2(width, height); | 
|  | 1280 | } | 
|  | 1281 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1282 | void GLESRenderEngine::drawMesh(const Mesh& mesh) { | 
| Dan Stoza | 2713c30 | 2018-03-28 17:07:36 -0700 | [diff] [blame] | 1283 | ATRACE_CALL(); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1284 | if (mesh.getTexCoordsSize()) { | 
|  | 1285 | glEnableVertexAttribArray(Program::texCoords); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 1286 | glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE, | 
|  | 1287 | mesh.getByteStride(), mesh.getTexCoords()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1288 | } | 
|  | 1289 |  | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 1290 | glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, | 
|  | 1291 | mesh.getByteStride(), mesh.getPositions()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1292 |  | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 1293 | if (mState.cornerRadius > 0.0f) { | 
|  | 1294 | glEnableVertexAttribArray(Program::cropCoords); | 
|  | 1295 | glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE, | 
|  | 1296 | mesh.getByteStride(), mesh.getCropCoords()); | 
|  | 1297 | } | 
|  | 1298 |  | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1299 | if (mState.drawShadows) { | 
|  | 1300 | glEnableVertexAttribArray(Program::shadowColor); | 
|  | 1301 | glVertexAttribPointer(Program::shadowColor, mesh.getShadowColorSize(), GL_FLOAT, GL_FALSE, | 
|  | 1302 | mesh.getByteStride(), mesh.getShadowColor()); | 
|  | 1303 |  | 
|  | 1304 | glEnableVertexAttribArray(Program::shadowParams); | 
|  | 1305 | glVertexAttribPointer(Program::shadowParams, mesh.getShadowParamsSize(), GL_FLOAT, GL_FALSE, | 
|  | 1306 | mesh.getByteStride(), mesh.getShadowParams()); | 
|  | 1307 | } | 
|  | 1308 |  | 
|  | 1309 | Description managedState = mState; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1310 | // By default, DISPLAY_P3 is the only supported wide color output. However, | 
|  | 1311 | // when HDR content is present, hardware composer may be able to handle | 
|  | 1312 | // BT2020 data space, in that case, the output data space is set to be | 
|  | 1313 | // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need | 
|  | 1314 | // to respect this and convert non-HDR content to HDR format. | 
| Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 1315 | if (mUseColorManagement) { | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1316 | Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK); | 
|  | 1317 | Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1318 | Dataspace outputStandard = | 
|  | 1319 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK); | 
|  | 1320 | Dataspace outputTransfer = | 
|  | 1321 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1322 | bool needsXYZConversion = needsXYZTransformMatrix(); | 
|  | 1323 |  | 
| Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 1324 | // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or | 
|  | 1325 | // STANDARD_BT2020, it will be  treated as STANDARD_BT709 | 
|  | 1326 | if (inputStandard != Dataspace::STANDARD_DCI_P3 && | 
|  | 1327 | inputStandard != Dataspace::STANDARD_BT2020) { | 
|  | 1328 | inputStandard = Dataspace::STANDARD_BT709; | 
|  | 1329 | } | 
|  | 1330 |  | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1331 | if (needsXYZConversion) { | 
|  | 1332 | // The supported input color spaces are standard RGB, Display P3 and BT2020. | 
|  | 1333 | switch (inputStandard) { | 
|  | 1334 | case Dataspace::STANDARD_DCI_P3: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1335 | managedState.inputTransformMatrix = mDisplayP3ToXyz; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1336 | break; | 
|  | 1337 | case Dataspace::STANDARD_BT2020: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1338 | managedState.inputTransformMatrix = mBt2020ToXyz; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1339 | break; | 
|  | 1340 | default: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1341 | managedState.inputTransformMatrix = mSrgbToXyz; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1342 | break; | 
|  | 1343 | } | 
|  | 1344 |  | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 1345 | // The supported output color spaces are BT2020, Display P3 and standard RGB. | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1346 | switch (outputStandard) { | 
|  | 1347 | case Dataspace::STANDARD_BT2020: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1348 | managedState.outputTransformMatrix = mXyzToBt2020; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1349 | break; | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 1350 | case Dataspace::STANDARD_DCI_P3: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1351 | managedState.outputTransformMatrix = mXyzToDisplayP3; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1352 | break; | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 1353 | default: | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1354 | managedState.outputTransformMatrix = mXyzToSrgb; | 
| Peiyong Lin | 9b03c73 | 2018-05-17 10:14:02 -0700 | [diff] [blame] | 1355 | break; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1356 | } | 
|  | 1357 | } else if (inputStandard != outputStandard) { | 
|  | 1358 | // At this point, the input data space and output data space could be both | 
|  | 1359 | // HDR data spaces, but they match each other, we do nothing in this case. | 
|  | 1360 | // In addition to the case above, the input data space could be | 
|  | 1361 | // - scRGB linear | 
|  | 1362 | // - scRGB non-linear | 
|  | 1363 | // - sRGB | 
|  | 1364 | // - Display P3 | 
| Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 1365 | // - BT2020 | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1366 | // The output data spaces could be | 
|  | 1367 | // - sRGB | 
|  | 1368 | // - Display P3 | 
| Valerie Hau | eb8e076 | 2018-11-06 10:10:42 -0800 | [diff] [blame] | 1369 | // - BT2020 | 
|  | 1370 | switch (outputStandard) { | 
|  | 1371 | case Dataspace::STANDARD_BT2020: | 
|  | 1372 | if (inputStandard == Dataspace::STANDARD_BT709) { | 
|  | 1373 | managedState.outputTransformMatrix = mSrgbToBt2020; | 
|  | 1374 | } else if (inputStandard == Dataspace::STANDARD_DCI_P3) { | 
|  | 1375 | managedState.outputTransformMatrix = mDisplayP3ToBt2020; | 
|  | 1376 | } | 
|  | 1377 | break; | 
|  | 1378 | case Dataspace::STANDARD_DCI_P3: | 
|  | 1379 | if (inputStandard == Dataspace::STANDARD_BT709) { | 
|  | 1380 | managedState.outputTransformMatrix = mSrgbToDisplayP3; | 
|  | 1381 | } else if (inputStandard == Dataspace::STANDARD_BT2020) { | 
|  | 1382 | managedState.outputTransformMatrix = mBt2020ToDisplayP3; | 
|  | 1383 | } | 
|  | 1384 | break; | 
|  | 1385 | default: | 
|  | 1386 | if (inputStandard == Dataspace::STANDARD_DCI_P3) { | 
|  | 1387 | managedState.outputTransformMatrix = mDisplayP3ToSrgb; | 
|  | 1388 | } else if (inputStandard == Dataspace::STANDARD_BT2020) { | 
|  | 1389 | managedState.outputTransformMatrix = mBt2020ToSrgb; | 
|  | 1390 | } | 
|  | 1391 | break; | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1392 | } | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1393 | } | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1394 |  | 
|  | 1395 | // we need to convert the RGB value to linear space and convert it back when: | 
|  | 1396 | // - there is a color matrix that is not an identity matrix, or | 
|  | 1397 | // - there is an output transform matrix that is not an identity matrix, or | 
|  | 1398 | // - the input transfer function doesn't match the output transfer function. | 
| Peiyong Lin | 13effd1 | 2018-07-24 17:01:47 -0700 | [diff] [blame] | 1399 | if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() || | 
| Chia-I Wu | d49d669 | 2018-06-27 07:17:41 +0800 | [diff] [blame] | 1400 | inputTransfer != outputTransfer) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1401 | managedState.inputTransferFunction = | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1402 | Description::dataSpaceToTransferFunction(inputTransfer); | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 1403 | managedState.outputTransferFunction = | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1404 | Description::dataSpaceToTransferFunction(outputTransfer); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1405 | } | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1406 | } | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1407 |  | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1408 | ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext : mEGLContext, | 
|  | 1409 | managedState); | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1410 |  | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1411 | if (mState.drawShadows) { | 
|  | 1412 | glDrawElements(mesh.getPrimitive(), mesh.getIndexCount(), GL_UNSIGNED_SHORT, | 
|  | 1413 | mesh.getIndices()); | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1414 | } else { | 
| Courtney Goeltzenleuchter | 5d94389 | 2017-03-22 13:46:46 -0600 | [diff] [blame] | 1415 | glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount()); | 
|  | 1416 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1417 |  | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1418 | if (mUseColorManagement && outputDebugPPMs) { | 
|  | 1419 | static uint64_t managedColorFrameCount = 0; | 
|  | 1420 | std::ostringstream out; | 
|  | 1421 | out << "/data/texture_out" << managedColorFrameCount++; | 
|  | 1422 | writePPM(out.str().c_str(), mVpWidth, mVpHeight); | 
|  | 1423 | } | 
|  | 1424 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1425 | if (mesh.getTexCoordsSize()) { | 
|  | 1426 | glDisableVertexAttribArray(Program::texCoords); | 
|  | 1427 | } | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 1428 |  | 
|  | 1429 | if (mState.cornerRadius > 0.0f) { | 
|  | 1430 | glDisableVertexAttribArray(Program::cropCoords); | 
|  | 1431 | } | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 1432 |  | 
|  | 1433 | if (mState.drawShadows) { | 
|  | 1434 | glDisableVertexAttribArray(Program::shadowColor); | 
|  | 1435 | glDisableVertexAttribArray(Program::shadowParams); | 
|  | 1436 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1437 | } | 
|  | 1438 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1439 | size_t GLESRenderEngine::getMaxTextureSize() const { | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 1440 | return mMaxTextureSize; | 
|  | 1441 | } | 
|  | 1442 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1443 | size_t GLESRenderEngine::getMaxViewportDims() const { | 
| Peiyong Lin | f1bada9 | 2018-08-29 09:39:31 -0700 | [diff] [blame] | 1444 | return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1]; | 
|  | 1445 | } | 
|  | 1446 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1447 | void GLESRenderEngine::dump(std::string& result) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 1448 | const GLExtensions& extensions = GLExtensions::getInstance(); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1449 | ProgramCache& cache = ProgramCache::getInstance(); | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 1450 |  | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1451 | StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion()); | 
|  | 1452 | StringAppendF(&result, "%s\n", extensions.getEGLExtensions()); | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1453 | StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), | 
|  | 1454 | extensions.getVersion()); | 
|  | 1455 | StringAppendF(&result, "%s\n", extensions.getExtensions()); | 
| Peiyong Lin | e62c5cb | 2019-04-30 13:59:59 -0700 | [diff] [blame] | 1456 | StringAppendF(&result, "RenderEngine supports protected context: %d\n", | 
|  | 1457 | supportsProtectedContent()); | 
|  | 1458 | StringAppendF(&result, "RenderEngine is in protected context: %d\n", mInProtectedContext); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1459 | StringAppendF(&result, "RenderEngine program cache size for unprotected context: %zu\n", | 
|  | 1460 | cache.getSize(mEGLContext)); | 
|  | 1461 | StringAppendF(&result, "RenderEngine program cache size for protected context: %zu\n", | 
|  | 1462 | cache.getSize(mProtectedEGLContext)); | 
| Yiwei Zhang | 5434a78 | 2018-12-05 18:06:32 -0800 | [diff] [blame] | 1463 | StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n", | 
|  | 1464 | dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(), | 
|  | 1465 | dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str()); | 
| Alec Mouri | ef44c2d | 2019-07-15 15:27:22 -0700 | [diff] [blame] | 1466 | { | 
|  | 1467 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 1468 | StringAppendF(&result, "RenderEngine image cache size: %zu\n", mImageCache.size()); | 
|  | 1469 | StringAppendF(&result, "Dumping buffer ids...\n"); | 
|  | 1470 | for (const auto& [id, unused] : mImageCache) { | 
|  | 1471 | StringAppendF(&result, "0x%" PRIx64 "\n", id); | 
|  | 1472 | } | 
|  | 1473 | } | 
|  | 1474 | { | 
|  | 1475 | std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex); | 
|  | 1476 | StringAppendF(&result, "RenderEngine framebuffer image cache size: %zu\n", | 
|  | 1477 | mFramebufferImageCache.size()); | 
|  | 1478 | StringAppendF(&result, "Dumping buffer ids...\n"); | 
|  | 1479 | for (const auto& [id, unused] : mFramebufferImageCache) { | 
|  | 1480 | StringAppendF(&result, "0x%" PRIx64 "\n", id); | 
|  | 1481 | } | 
|  | 1482 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1483 | } | 
|  | 1484 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1485 | GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) { | 
| Peiyong Lin | f11f39b | 2018-09-05 14:37:41 -0700 | [diff] [blame] | 1486 | int major, minor; | 
|  | 1487 | if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) { | 
|  | 1488 | if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) { | 
|  | 1489 | ALOGW("Unable to parse GL_VERSION string: \"%s\"", str); | 
|  | 1490 | return GLES_VERSION_1_0; | 
|  | 1491 | } | 
|  | 1492 | } | 
|  | 1493 |  | 
|  | 1494 | if (major == 1 && minor == 0) return GLES_VERSION_1_0; | 
|  | 1495 | if (major == 1 && minor >= 1) return GLES_VERSION_1_1; | 
|  | 1496 | if (major == 2 && minor >= 0) return GLES_VERSION_2_0; | 
|  | 1497 | if (major == 3 && minor >= 0) return GLES_VERSION_3_0; | 
|  | 1498 |  | 
|  | 1499 | ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor); | 
|  | 1500 | return GLES_VERSION_1_0; | 
|  | 1501 | } | 
|  | 1502 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1503 | EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config, | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1504 | EGLContext shareContext, bool useContextPriority, | 
|  | 1505 | Protection protection) { | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1506 | EGLint renderableType = 0; | 
|  | 1507 | if (config == EGL_NO_CONFIG) { | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1508 | renderableType = EGL_OPENGL_ES3_BIT; | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1509 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { | 
|  | 1510 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); | 
|  | 1511 | } | 
|  | 1512 | EGLint contextClientVersion = 0; | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1513 | if (renderableType & EGL_OPENGL_ES3_BIT) { | 
|  | 1514 | contextClientVersion = 3; | 
|  | 1515 | } else if (renderableType & EGL_OPENGL_ES2_BIT) { | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1516 | contextClientVersion = 2; | 
|  | 1517 | } else if (renderableType & EGL_OPENGL_ES_BIT) { | 
|  | 1518 | contextClientVersion = 1; | 
|  | 1519 | } else { | 
|  | 1520 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); | 
|  | 1521 | } | 
|  | 1522 |  | 
|  | 1523 | std::vector<EGLint> contextAttributes; | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1524 | contextAttributes.reserve(7); | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1525 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); | 
|  | 1526 | contextAttributes.push_back(contextClientVersion); | 
|  | 1527 | if (useContextPriority) { | 
|  | 1528 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); | 
|  | 1529 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); | 
|  | 1530 | } | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1531 | if (protection == Protection::PROTECTED) { | 
|  | 1532 | contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 1533 | contextAttributes.push_back(EGL_TRUE); | 
|  | 1534 | } | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1535 | contextAttributes.push_back(EGL_NONE); | 
|  | 1536 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1537 | EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 1538 |  | 
|  | 1539 | if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) { | 
|  | 1540 | // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus | 
|  | 1541 | // EGL_NO_CONTEXT so that we can abort. | 
|  | 1542 | if (config != EGL_NO_CONFIG) { | 
|  | 1543 | return context; | 
|  | 1544 | } | 
|  | 1545 | // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should | 
|  | 1546 | // try to fall back to GLES 2. | 
|  | 1547 | contextAttributes[1] = 2; | 
|  | 1548 | context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 1549 | } | 
|  | 1550 |  | 
|  | 1551 | return context; | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1552 | } | 
|  | 1553 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1554 | EGLSurface GLESRenderEngine::createDummyEglPbufferSurface(EGLDisplay display, EGLConfig config, | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1555 | int hwcFormat, Protection protection) { | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1556 | EGLConfig dummyConfig = config; | 
|  | 1557 | if (dummyConfig == EGL_NO_CONFIG) { | 
|  | 1558 | dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); | 
|  | 1559 | } | 
|  | 1560 | std::vector<EGLint> attributes; | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1561 | attributes.reserve(7); | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1562 | attributes.push_back(EGL_WIDTH); | 
|  | 1563 | attributes.push_back(1); | 
|  | 1564 | attributes.push_back(EGL_HEIGHT); | 
|  | 1565 | attributes.push_back(1); | 
| Peiyong Lin | fb530cf | 2018-12-15 05:07:38 +0000 | [diff] [blame] | 1566 | if (protection == Protection::PROTECTED) { | 
|  | 1567 | attributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 1568 | attributes.push_back(EGL_TRUE); | 
|  | 1569 | } | 
| Peiyong Lin | a5e9f1b | 2018-11-27 22:49:37 -0800 | [diff] [blame] | 1570 | attributes.push_back(EGL_NONE); | 
|  | 1571 |  | 
|  | 1572 | return eglCreatePbufferSurface(display, dummyConfig, attributes.data()); | 
|  | 1573 | } | 
|  | 1574 |  | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1575 | bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const { | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1576 | const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK); | 
|  | 1577 | const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK); | 
|  | 1578 | return standard == Dataspace::STANDARD_BT2020 && | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1579 | (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1580 | } | 
|  | 1581 |  | 
|  | 1582 | // For convenience, we want to convert the input color space to XYZ color space first, | 
|  | 1583 | // and then convert from XYZ color space to output color space when | 
|  | 1584 | // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or | 
|  | 1585 | //   HDR content will be tone-mapped to SDR; Or, | 
|  | 1586 | // - there are HDR PQ and HLG contents presented at the same time, where we want to convert | 
|  | 1587 | //   HLG content to PQ content. | 
|  | 1588 | // In either case above, we need to operate the Y value in XYZ color space. Thus, when either | 
|  | 1589 | // input data space or output data space is HDR data space, and the input transfer function | 
|  | 1590 | // doesn't match the output transfer function, we would enable an intermediate transfrom to | 
|  | 1591 | // XYZ color space. | 
| Peiyong Lin | 7e219eb | 2018-12-03 05:40:42 -0800 | [diff] [blame] | 1592 | bool GLESRenderEngine::needsXYZTransformMatrix() const { | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1593 | const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace); | 
|  | 1594 | const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace); | 
|  | 1595 | const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK); | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1596 | const Dataspace outputTransfer = | 
|  | 1597 | static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 1598 |  | 
|  | 1599 | return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer; | 
|  | 1600 | } | 
|  | 1601 |  | 
| Alec Mouri | d43ccab | 2019-03-13 12:23:45 -0700 | [diff] [blame] | 1602 | bool GLESRenderEngine::isImageCachedForTesting(uint64_t bufferId) { | 
|  | 1603 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 1604 | const auto& cachedImage = mImageCache.find(bufferId); | 
|  | 1605 | return cachedImage != mImageCache.end(); | 
|  | 1606 | } | 
|  | 1607 |  | 
|  | 1608 | bool GLESRenderEngine::isFramebufferImageCachedForTesting(uint64_t bufferId) { | 
| Alec Mouri | ef44c2d | 2019-07-15 15:27:22 -0700 | [diff] [blame] | 1609 | std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex); | 
| Alec Mouri | d43ccab | 2019-03-13 12:23:45 -0700 | [diff] [blame] | 1610 | return std::any_of(mFramebufferImageCache.cbegin(), mFramebufferImageCache.cend(), | 
|  | 1611 | [=](std::pair<uint64_t, EGLImageKHR> image) { | 
|  | 1612 | return image.first == bufferId; | 
|  | 1613 | }); | 
|  | 1614 | } | 
|  | 1615 |  | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 1616 | // FlushTracer implementation | 
|  | 1617 | GLESRenderEngine::FlushTracer::FlushTracer(GLESRenderEngine* engine) : mEngine(engine) { | 
|  | 1618 | mThread = std::thread(&GLESRenderEngine::FlushTracer::loop, this); | 
|  | 1619 | } | 
|  | 1620 |  | 
|  | 1621 | GLESRenderEngine::FlushTracer::~FlushTracer() { | 
|  | 1622 | { | 
|  | 1623 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 1624 | mRunning = false; | 
|  | 1625 | } | 
|  | 1626 | mCondition.notify_all(); | 
|  | 1627 | if (mThread.joinable()) { | 
|  | 1628 | mThread.join(); | 
|  | 1629 | } | 
|  | 1630 | } | 
|  | 1631 |  | 
|  | 1632 | void GLESRenderEngine::FlushTracer::queueSync(EGLSyncKHR sync) { | 
|  | 1633 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 1634 | char name[64]; | 
|  | 1635 | const uint64_t frameNum = mFramesQueued++; | 
|  | 1636 | snprintf(name, sizeof(name), "Queueing sync for frame: %lu", | 
|  | 1637 | static_cast<unsigned long>(frameNum)); | 
|  | 1638 | ATRACE_NAME(name); | 
|  | 1639 | mQueue.push({sync, frameNum}); | 
|  | 1640 | ATRACE_INT("GPU Frames Outstanding", mQueue.size()); | 
|  | 1641 | mCondition.notify_one(); | 
|  | 1642 | } | 
|  | 1643 |  | 
|  | 1644 | void GLESRenderEngine::FlushTracer::loop() { | 
|  | 1645 | while (mRunning) { | 
|  | 1646 | QueueEntry entry; | 
|  | 1647 | { | 
|  | 1648 | std::lock_guard<std::mutex> lock(mMutex); | 
|  | 1649 |  | 
|  | 1650 | mCondition.wait(mMutex, | 
| Alec Mouri | c15dd01 | 2019-01-15 15:30:38 -0800 | [diff] [blame] | 1651 | [&]() REQUIRES(mMutex) { return !mQueue.empty() || !mRunning; }); | 
| Alec Mouri | 554d06e | 2018-12-20 00:15:33 -0800 | [diff] [blame] | 1652 |  | 
|  | 1653 | if (!mRunning) { | 
|  | 1654 | // if mRunning is false, then FlushTracer is being destroyed, so | 
|  | 1655 | // bail out now. | 
|  | 1656 | break; | 
|  | 1657 | } | 
|  | 1658 | entry = mQueue.front(); | 
|  | 1659 | mQueue.pop(); | 
|  | 1660 | } | 
|  | 1661 | { | 
|  | 1662 | char name[64]; | 
|  | 1663 | snprintf(name, sizeof(name), "waiting for frame %lu", | 
|  | 1664 | static_cast<unsigned long>(entry.mFrameNum)); | 
|  | 1665 | ATRACE_NAME(name); | 
|  | 1666 | mEngine->waitSync(entry.mSync, 0); | 
|  | 1667 | } | 
|  | 1668 | } | 
|  | 1669 | } | 
|  | 1670 |  | 
| Vishnu Nair | 16efdbf | 2019-12-10 11:55:42 -0800 | [diff] [blame] | 1671 | void GLESRenderEngine::handleShadow(const FloatRect& casterRect, float casterCornerRadius, | 
|  | 1672 | const ShadowSettings& settings) { | 
|  | 1673 | ATRACE_CALL(); | 
|  | 1674 | const float casterZ = settings.length / 2.0f; | 
|  | 1675 | const GLShadowVertexGenerator shadows(casterRect, casterCornerRadius, casterZ, | 
|  | 1676 | settings.casterIsTranslucent, settings.ambientColor, | 
|  | 1677 | settings.spotColor, settings.lightPos, | 
|  | 1678 | settings.lightRadius); | 
|  | 1679 |  | 
|  | 1680 | // setup mesh for both shadows | 
|  | 1681 | Mesh mesh = Mesh::Builder() | 
|  | 1682 | .setPrimitive(Mesh::TRIANGLES) | 
|  | 1683 | .setVertices(shadows.getVertexCount(), 2 /* size */) | 
|  | 1684 | .setShadowAttrs() | 
|  | 1685 | .setIndices(shadows.getIndexCount()) | 
|  | 1686 | .build(); | 
|  | 1687 |  | 
|  | 1688 | Mesh::VertexArray<vec2> position = mesh.getPositionArray<vec2>(); | 
|  | 1689 | Mesh::VertexArray<vec4> shadowColor = mesh.getShadowColorArray<vec4>(); | 
|  | 1690 | Mesh::VertexArray<vec3> shadowParams = mesh.getShadowParamsArray<vec3>(); | 
|  | 1691 | shadows.fillVertices(position, shadowColor, shadowParams); | 
|  | 1692 | shadows.fillIndices(mesh.getIndicesArray()); | 
|  | 1693 |  | 
|  | 1694 | mState.cornerRadius = 0.0f; | 
|  | 1695 | mState.drawShadows = true; | 
| Vishnu Nair | f19544f | 2020-02-03 11:23:26 -0800 | [diff] [blame] | 1696 | setupLayerTexturing(mShadowTexture.getTexture()); | 
| Vishnu Nair | 16efdbf | 2019-12-10 11:55:42 -0800 | [diff] [blame] | 1697 | drawMesh(mesh); | 
|  | 1698 | mState.drawShadows = false; | 
|  | 1699 | } | 
|  | 1700 |  | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 1701 | } // namespace gl | 
|  | 1702 | } // namespace renderengine | 
|  | 1703 | } // namespace android |