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