| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 2020 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 |  | 
|  | 17 | //#define LOG_NDEBUG 0 | 
| Ana Krulec | 70d15b1b | 2020-12-01 10:05:15 -0800 | [diff] [blame] | 18 | #undef LOG_TAG | 
|  | 19 | #define LOG_TAG "RenderEngine" | 
|  | 20 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS | 
|  | 21 |  | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 22 | #include "SkiaGLRenderEngine.h" | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 23 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 24 | #include <EGL/egl.h> | 
|  | 25 | #include <EGL/eglext.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 26 | #include <GrContextOptions.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 27 | #include <SkCanvas.h> | 
| Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 28 | #include <SkColorFilter.h> | 
|  | 29 | #include <SkColorMatrix.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 30 | #include <SkColorSpace.h> | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 31 | #include <SkGraphics.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 32 | #include <SkImage.h> | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 33 | #include <SkImageFilters.h> | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 34 | #include <SkRegion.h> | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 35 | #include <SkShadowUtils.h> | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 36 | #include <SkSurface.h> | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 37 | #include <android-base/stringprintf.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 38 | #include <gl/GrGLInterface.h> | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 39 | #include <sync/sync.h> | 
|  | 40 | #include <ui/BlurRegion.h> | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 41 | #include <ui/DebugUtils.h> | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 42 | #include <ui/GraphicBuffer.h> | 
|  | 43 | #include <utils/Trace.h> | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | #include <cmath> | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 46 | #include <cstdint> | 
|  | 47 | #include <memory> | 
|  | 48 |  | 
|  | 49 | #include "../gl/GLExtensions.h" | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 50 | #include "Cache.h" | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 51 | #include "ColorSpaces.h" | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 52 | #include "SkBlendMode.h" | 
|  | 53 | #include "SkImageInfo.h" | 
|  | 54 | #include "filters/BlurFilter.h" | 
|  | 55 | #include "filters/LinearEffect.h" | 
|  | 56 | #include "log/log_main.h" | 
|  | 57 | #include "skia/debug/SkiaCapture.h" | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 58 | #include "skia/debug/SkiaMemoryReporter.h" | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 59 | #include "skia/filters/StretchShaderFactory.h" | 
| Alec Mouri | 4ce5ec0 | 2021-01-07 17:33:21 -0800 | [diff] [blame] | 60 | #include "system/graphics-base-v1.0.h" | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 61 |  | 
| Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 62 | namespace { | 
|  | 63 | // Debugging settings | 
|  | 64 | static const bool kPrintLayerSettings = false; | 
|  | 65 | static const bool kFlushAfterEveryLayer = false; | 
|  | 66 | } // namespace | 
|  | 67 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 68 | bool checkGlError(const char* op, int lineNumber); | 
|  | 69 |  | 
|  | 70 | namespace android { | 
|  | 71 | namespace renderengine { | 
|  | 72 | namespace skia { | 
|  | 73 |  | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 74 | using base::StringAppendF; | 
|  | 75 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 76 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, | 
|  | 77 | EGLint wanted, EGLConfig* outConfig) { | 
|  | 78 | EGLint numConfigs = -1, n = 0; | 
|  | 79 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); | 
|  | 80 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); | 
|  | 81 | eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n); | 
|  | 82 | configs.resize(n); | 
|  | 83 |  | 
|  | 84 | if (!configs.empty()) { | 
|  | 85 | if (attribute != EGL_NONE) { | 
|  | 86 | for (EGLConfig config : configs) { | 
|  | 87 | EGLint value = 0; | 
|  | 88 | eglGetConfigAttrib(dpy, config, attribute, &value); | 
|  | 89 | if (wanted == value) { | 
|  | 90 | *outConfig = config; | 
|  | 91 | return NO_ERROR; | 
|  | 92 | } | 
|  | 93 | } | 
|  | 94 | } else { | 
|  | 95 | // just pick the first one | 
|  | 96 | *outConfig = configs[0]; | 
|  | 97 | return NO_ERROR; | 
|  | 98 | } | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | return NAME_NOT_FOUND; | 
|  | 102 | } | 
|  | 103 |  | 
|  | 104 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, | 
|  | 105 | EGLConfig* config) { | 
|  | 106 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if | 
|  | 107 | // it is to be used with WIFI displays | 
|  | 108 | status_t err; | 
|  | 109 | EGLint wantedAttribute; | 
|  | 110 | EGLint wantedAttributeValue; | 
|  | 111 |  | 
|  | 112 | std::vector<EGLint> attribs; | 
|  | 113 | if (renderableType) { | 
|  | 114 | const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format); | 
|  | 115 | const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102; | 
|  | 116 |  | 
|  | 117 | // Default to 8 bits per channel. | 
|  | 118 | const EGLint tmpAttribs[] = { | 
|  | 119 | EGL_RENDERABLE_TYPE, | 
|  | 120 | renderableType, | 
|  | 121 | EGL_RECORDABLE_ANDROID, | 
|  | 122 | EGL_TRUE, | 
|  | 123 | EGL_SURFACE_TYPE, | 
|  | 124 | EGL_WINDOW_BIT | EGL_PBUFFER_BIT, | 
|  | 125 | EGL_FRAMEBUFFER_TARGET_ANDROID, | 
|  | 126 | EGL_TRUE, | 
|  | 127 | EGL_RED_SIZE, | 
|  | 128 | is1010102 ? 10 : 8, | 
|  | 129 | EGL_GREEN_SIZE, | 
|  | 130 | is1010102 ? 10 : 8, | 
|  | 131 | EGL_BLUE_SIZE, | 
|  | 132 | is1010102 ? 10 : 8, | 
|  | 133 | EGL_ALPHA_SIZE, | 
|  | 134 | is1010102 ? 2 : 8, | 
|  | 135 | EGL_NONE, | 
|  | 136 | }; | 
|  | 137 | std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)), | 
|  | 138 | std::back_inserter(attribs)); | 
|  | 139 | wantedAttribute = EGL_NONE; | 
|  | 140 | wantedAttributeValue = EGL_NONE; | 
|  | 141 | } else { | 
|  | 142 | // if no renderable type specified, fallback to a simplified query | 
|  | 143 | wantedAttribute = EGL_NATIVE_VISUAL_ID; | 
|  | 144 | wantedAttributeValue = format; | 
|  | 145 | } | 
|  | 146 |  | 
|  | 147 | err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue, | 
|  | 148 | config); | 
|  | 149 | if (err == NO_ERROR) { | 
|  | 150 | EGLint caveat; | 
|  | 151 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) | 
|  | 152 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | return err; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create( | 
|  | 159 | const RenderEngineCreationArgs& args) { | 
|  | 160 | // initialize EGL for the default display | 
|  | 161 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); | 
|  | 162 | if (!eglInitialize(display, nullptr, nullptr)) { | 
|  | 163 | LOG_ALWAYS_FATAL("failed to initialize EGL"); | 
|  | 164 | } | 
|  | 165 |  | 
| Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 166 | const auto eglVersion = eglQueryString(display, EGL_VERSION); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 167 | if (!eglVersion) { | 
|  | 168 | checkGlError(__FUNCTION__, __LINE__); | 
| Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 169 | LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed"); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 172 | const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 173 | if (!eglExtensions) { | 
|  | 174 | checkGlError(__FUNCTION__, __LINE__); | 
| Yiwei Zhang | e265096 | 2020-12-01 23:27:58 +0000 | [diff] [blame] | 175 | LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed"); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 176 | } | 
|  | 177 |  | 
|  | 178 | auto& extensions = gl::GLExtensions::getInstance(); | 
|  | 179 | extensions.initWithEGLStrings(eglVersion, eglExtensions); | 
|  | 180 |  | 
|  | 181 | // The code assumes that ES2 or later is available if this extension is | 
|  | 182 | // supported. | 
|  | 183 | EGLConfig config = EGL_NO_CONFIG_KHR; | 
|  | 184 | if (!extensions.hasNoConfigContext()) { | 
|  | 185 | config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true); | 
|  | 186 | } | 
|  | 187 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 188 | EGLContext protectedContext = EGL_NO_CONTEXT; | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 189 | const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 190 | if (args.enableProtectedContext && extensions.hasProtectedContent()) { | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 191 | protectedContext = | 
|  | 192 | createEglContext(display, config, nullptr, priority, Protection::PROTECTED); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 193 | ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context"); | 
|  | 194 | } | 
|  | 195 |  | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 196 | EGLContext ctxt = | 
|  | 197 | createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 198 |  | 
|  | 199 | // if can't create a GL context, we can only abort. | 
|  | 200 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); | 
|  | 201 |  | 
|  | 202 | EGLSurface placeholder = EGL_NO_SURFACE; | 
|  | 203 | if (!extensions.hasSurfacelessContext()) { | 
|  | 204 | placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, | 
|  | 205 | Protection::UNPROTECTED); | 
|  | 206 | LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer"); | 
|  | 207 | } | 
|  | 208 | EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt); | 
|  | 209 | LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current"); | 
|  | 210 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), | 
|  | 211 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); | 
|  | 212 |  | 
|  | 213 | EGLSurface protectedPlaceholder = EGL_NO_SURFACE; | 
|  | 214 | if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) { | 
|  | 215 | protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, | 
|  | 216 | Protection::PROTECTED); | 
|  | 217 | ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE, | 
|  | 218 | "can't create protected placeholder pbuffer"); | 
|  | 219 | } | 
|  | 220 |  | 
|  | 221 | // initialize the renderer while GL is current | 
|  | 222 | std::unique_ptr<SkiaGLRenderEngine> engine = | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 223 | std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext, | 
|  | 224 | protectedPlaceholder); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 225 |  | 
|  | 226 | ALOGI("OpenGL ES informations:"); | 
|  | 227 | ALOGI("vendor    : %s", extensions.getVendor()); | 
|  | 228 | ALOGI("renderer  : %s", extensions.getRenderer()); | 
|  | 229 | ALOGI("version   : %s", extensions.getVersion()); | 
|  | 230 | ALOGI("extensions: %s", extensions.getExtensions()); | 
|  | 231 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); | 
|  | 232 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); | 
|  | 233 |  | 
|  | 234 | return engine; | 
|  | 235 | } | 
|  | 236 |  | 
| Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 237 | void SkiaGLRenderEngine::primeCache() { | 
|  | 238 | Cache::primeShaderCache(this); | 
|  | 239 | } | 
|  | 240 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 241 | EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { | 
|  | 242 | status_t err; | 
|  | 243 | EGLConfig config; | 
|  | 244 |  | 
|  | 245 | // First try to get an ES3 config | 
|  | 246 | err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config); | 
|  | 247 | if (err != NO_ERROR) { | 
|  | 248 | // If ES3 fails, try to get an ES2 config | 
|  | 249 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); | 
|  | 250 | if (err != NO_ERROR) { | 
|  | 251 | // If ES2 still doesn't work, probably because we're on the emulator. | 
|  | 252 | // try a simplified query | 
|  | 253 | ALOGW("no suitable EGLConfig found, trying a simpler query"); | 
|  | 254 | err = selectEGLConfig(display, format, 0, &config); | 
|  | 255 | if (err != NO_ERROR) { | 
|  | 256 | // this EGL is too lame for android | 
|  | 257 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); | 
|  | 258 | } | 
|  | 259 | } | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | if (logConfig) { | 
|  | 263 | // print some debugging info | 
|  | 264 | EGLint r, g, b, a; | 
|  | 265 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); | 
|  | 266 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); | 
|  | 267 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); | 
|  | 268 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); | 
|  | 269 | ALOGI("EGL information:"); | 
|  | 270 | ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR)); | 
|  | 271 | ALOGI("version   : %s", eglQueryString(display, EGL_VERSION)); | 
|  | 272 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); | 
|  | 273 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); | 
|  | 274 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | return config; | 
|  | 278 | } | 
|  | 279 |  | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 280 | sk_sp<SkData> SkiaGLRenderEngine::SkSLCacheMonitor::load(const SkData& key) { | 
|  | 281 | // This "cache" does not actually cache anything. It just allows us to | 
|  | 282 | // monitor Skia's internal cache. So this method always returns null. | 
|  | 283 | return nullptr; | 
|  | 284 | } | 
|  | 285 |  | 
|  | 286 | void SkiaGLRenderEngine::SkSLCacheMonitor::store(const SkData& key, const SkData& data, | 
|  | 287 | const SkString& description) { | 
|  | 288 | mShadersCachedSinceLastCall++; | 
|  | 289 | } | 
|  | 290 |  | 
|  | 291 | void SkiaGLRenderEngine::assertShadersCompiled(int numShaders) { | 
|  | 292 | const int cached = mSkSLCacheMonitor.shadersCachedSinceLastCall(); | 
|  | 293 | LOG_ALWAYS_FATAL_IF(cached != numShaders, "Attempted to cache %i shaders; cached %i", | 
|  | 294 | numShaders, cached); | 
|  | 295 | } | 
|  | 296 |  | 
| Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 297 | int SkiaGLRenderEngine::reportShadersCompiled() { | 
|  | 298 | return mSkSLCacheMonitor.shadersCachedSinceLastCall(); | 
|  | 299 | } | 
|  | 300 |  | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 301 | SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 302 | EGLContext ctxt, EGLSurface placeholder, | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 303 | EGLContext protectedContext, EGLSurface protectedPlaceholder) | 
| Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 304 | : SkiaRenderEngine(args.renderEngineType), | 
|  | 305 | mEGLDisplay(display), | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 306 | mEGLContext(ctxt), | 
|  | 307 | mPlaceholderSurface(placeholder), | 
|  | 308 | mProtectedEGLContext(protectedContext), | 
| Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 309 | mProtectedPlaceholderSurface(protectedPlaceholder), | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 310 | mDefaultPixelFormat(static_cast<PixelFormat>(args.pixelFormat)), | 
| Alec Mouri | 0d99510 | 2021-02-24 16:53:38 -0800 | [diff] [blame] | 311 | mUseColorManagement(args.useColorManagement) { | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 312 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); | 
|  | 313 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); | 
|  | 314 |  | 
|  | 315 | GrContextOptions options; | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 316 | options.fDisableDriverCorrectnessWorkarounds = true; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 317 | options.fDisableDistanceFieldPaths = true; | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 318 | options.fPersistentCache = &mSkSLCacheMonitor; | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 319 | mGrContext = GrDirectContext::MakeGL(glInterface, options); | 
|  | 320 | if (useProtectedContext(true)) { | 
|  | 321 | mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options); | 
|  | 322 | useProtectedContext(false); | 
|  | 323 | } | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 324 |  | 
|  | 325 | if (args.supportsBackgroundBlur) { | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 326 | ALOGD("Background Blurs Enabled"); | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 327 | mBlurFilter = new BlurFilter(); | 
|  | 328 | } | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 329 | mCapture = std::make_unique<SkiaCapture>(); | 
|  | 330 | } | 
|  | 331 |  | 
|  | 332 | SkiaGLRenderEngine::~SkiaGLRenderEngine() { | 
| Marin Shalamanov | cea12ef | 2021-03-15 17:00:51 +0100 | [diff] [blame] | 333 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 334 | if (mBlurFilter) { | 
|  | 335 | delete mBlurFilter; | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | mCapture = nullptr; | 
|  | 339 |  | 
|  | 340 | mGrContext->flushAndSubmit(true); | 
|  | 341 | mGrContext->abandonContext(); | 
|  | 342 |  | 
|  | 343 | if (mProtectedGrContext) { | 
|  | 344 | mProtectedGrContext->flushAndSubmit(true); | 
|  | 345 | mProtectedGrContext->abandonContext(); | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | if (mPlaceholderSurface != EGL_NO_SURFACE) { | 
|  | 349 | eglDestroySurface(mEGLDisplay, mPlaceholderSurface); | 
|  | 350 | } | 
|  | 351 | if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) { | 
|  | 352 | eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface); | 
|  | 353 | } | 
|  | 354 | if (mEGLContext != EGL_NO_CONTEXT) { | 
|  | 355 | eglDestroyContext(mEGLDisplay, mEGLContext); | 
|  | 356 | } | 
|  | 357 | if (mProtectedEGLContext != EGL_NO_CONTEXT) { | 
|  | 358 | eglDestroyContext(mEGLDisplay, mProtectedEGLContext); | 
|  | 359 | } | 
|  | 360 | eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | 
|  | 361 | eglTerminate(mEGLDisplay); | 
|  | 362 | eglReleaseThread(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 363 | } | 
|  | 364 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 365 | bool SkiaGLRenderEngine::supportsProtectedContent() const { | 
|  | 366 | return mProtectedEGLContext != EGL_NO_CONTEXT; | 
|  | 367 | } | 
|  | 368 |  | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 369 | GrDirectContext* SkiaGLRenderEngine::getActiveGrContext() const { | 
|  | 370 | return mInProtectedContext ? mProtectedGrContext.get() : mGrContext.get(); | 
|  | 371 | } | 
|  | 372 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 373 | bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) { | 
|  | 374 | if (useProtectedContext == mInProtectedContext) { | 
|  | 375 | return true; | 
|  | 376 | } | 
| Alec Mouri | f6a0781 | 2021-02-11 21:07:55 -0800 | [diff] [blame] | 377 | if (useProtectedContext && !supportsProtectedContent()) { | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 378 | return false; | 
|  | 379 | } | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 380 |  | 
|  | 381 | // release any scratch resources before switching into a new mode | 
|  | 382 | if (getActiveGrContext()) { | 
|  | 383 | getActiveGrContext()->purgeUnlockedResources(true); | 
|  | 384 | } | 
|  | 385 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 386 | const EGLSurface surface = | 
|  | 387 | useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface; | 
|  | 388 | const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext; | 
|  | 389 | const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE; | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 390 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 391 | if (success) { | 
|  | 392 | mInProtectedContext = useProtectedContext; | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 393 | // given that we are sharing the same thread between two GrContexts we need to | 
|  | 394 | // make sure that the thread state is reset when switching between the two. | 
|  | 395 | if (getActiveGrContext()) { | 
|  | 396 | getActiveGrContext()->resetContext(); | 
|  | 397 | } | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 398 | } | 
|  | 399 | return success; | 
|  | 400 | } | 
|  | 401 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 402 | base::unique_fd SkiaGLRenderEngine::flush() { | 
|  | 403 | ATRACE_CALL(); | 
|  | 404 | if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) { | 
|  | 405 | return base::unique_fd(); | 
|  | 406 | } | 
|  | 407 |  | 
|  | 408 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); | 
|  | 409 | if (sync == EGL_NO_SYNC_KHR) { | 
|  | 410 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); | 
|  | 411 | return base::unique_fd(); | 
|  | 412 | } | 
|  | 413 |  | 
|  | 414 | // native fence fd will not be populated until flush() is done. | 
|  | 415 | glFlush(); | 
|  | 416 |  | 
|  | 417 | // get the fence fd | 
|  | 418 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); | 
|  | 419 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 420 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { | 
|  | 421 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); | 
|  | 422 | } | 
|  | 423 |  | 
|  | 424 | return fenceFd; | 
|  | 425 | } | 
|  | 426 |  | 
|  | 427 | bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) { | 
|  | 428 | if (!gl::GLExtensions::getInstance().hasNativeFenceSync() || | 
|  | 429 | !gl::GLExtensions::getInstance().hasWaitSync()) { | 
|  | 430 | return false; | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | // release the fd and transfer the ownership to EGLSync | 
|  | 434 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE}; | 
|  | 435 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); | 
|  | 436 | if (sync == EGL_NO_SYNC_KHR) { | 
|  | 437 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); | 
|  | 438 | return false; | 
|  | 439 | } | 
|  | 440 |  | 
|  | 441 | // XXX: The spec draft is inconsistent as to whether this should return an | 
|  | 442 | // EGLint or void.  Ignore the return value for now, as it's not strictly | 
|  | 443 | // needed. | 
|  | 444 | eglWaitSyncKHR(mEGLDisplay, sync, 0); | 
|  | 445 | EGLint error = eglGetError(); | 
|  | 446 | eglDestroySyncKHR(mEGLDisplay, sync); | 
|  | 447 | if (error != EGL_SUCCESS) { | 
|  | 448 | ALOGE("failed to wait for EGL native fence sync: %#x", error); | 
|  | 449 | return false; | 
|  | 450 | } | 
|  | 451 |  | 
|  | 452 | return true; | 
|  | 453 | } | 
|  | 454 |  | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 455 | static float toDegrees(uint32_t transform) { | 
|  | 456 | switch (transform) { | 
|  | 457 | case ui::Transform::ROT_90: | 
|  | 458 | return 90.0; | 
|  | 459 | case ui::Transform::ROT_180: | 
|  | 460 | return 180.0; | 
|  | 461 | case ui::Transform::ROT_270: | 
|  | 462 | return 270.0; | 
|  | 463 | default: | 
|  | 464 | return 0.0; | 
|  | 465 | } | 
|  | 466 | } | 
|  | 467 |  | 
| Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 468 | static SkColorMatrix toSkColorMatrix(const mat4& matrix) { | 
|  | 469 | return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1], | 
|  | 470 | matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2], | 
|  | 471 | matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3], | 
|  | 472 | matrix[3][3], 0); | 
|  | 473 | } | 
|  | 474 |  | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 475 | static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) { | 
|  | 476 | int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK; | 
|  | 477 | int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK; | 
|  | 478 |  | 
|  | 479 | // Treat unsupported dataspaces as srgb | 
|  | 480 | if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR && | 
|  | 481 | destTransfer != HAL_DATASPACE_TRANSFER_HLG && | 
|  | 482 | destTransfer != HAL_DATASPACE_TRANSFER_ST2084) { | 
|  | 483 | destTransfer = HAL_DATASPACE_TRANSFER_SRGB; | 
|  | 484 | } | 
|  | 485 |  | 
|  | 486 | if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR && | 
|  | 487 | sourceTransfer != HAL_DATASPACE_TRANSFER_HLG && | 
|  | 488 | sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) { | 
|  | 489 | sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR; | 
|  | 493 | const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB; | 
|  | 494 | const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR; | 
|  | 495 | const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB; | 
|  | 496 |  | 
|  | 497 | return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) && | 
|  | 498 | sourceTransfer != destTransfer; | 
|  | 499 | } | 
|  | 500 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 501 | void SkiaGLRenderEngine::mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer, | 
|  | 502 | bool isRenderable) { | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 503 | // Only run this if RE is running on its own thread. This way the access to GL | 
|  | 504 | // operations is guaranteed to be happening on the same thread. | 
|  | 505 | if (mRenderEngineType != RenderEngineType::SKIA_GL_THREADED) { | 
|  | 506 | return; | 
|  | 507 | } | 
| Derek Sollenberger | bc14f3c | 2021-05-21 14:29:16 -0400 | [diff] [blame] | 508 | // We currently don't attempt to map a buffer if the buffer contains protected content | 
|  | 509 | // or we are using a protected context because GPU resources for protected buffers is | 
|  | 510 | // much more limited. | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 511 | const bool isProtectedBuffer = buffer->getUsage() & GRALLOC_USAGE_PROTECTED; | 
| Derek Sollenberger | bc14f3c | 2021-05-21 14:29:16 -0400 | [diff] [blame] | 512 | if (isProtectedBuffer || mInProtectedContext) { | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 513 | return; | 
|  | 514 | } | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 515 | ATRACE_CALL(); | 
|  | 516 |  | 
| Derek Sollenberger | 0ad873b | 2021-05-05 11:35:52 -0400 | [diff] [blame] | 517 | // If we were to support caching protected buffers then we will need to switch the currently | 
|  | 518 | // bound context if we are not already using the protected context (and subsequently switch | 
|  | 519 | // back after the buffer is cached). | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 520 | auto grContext = getActiveGrContext(); | 
| Derek Sollenberger | bc14f3c | 2021-05-21 14:29:16 -0400 | [diff] [blame] | 521 | auto& cache = mTextureCache; | 
| Derek Sollenberger | eb904d4 | 2021-03-22 12:58:53 -0400 | [diff] [blame] | 522 |  | 
|  | 523 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 524 | mGraphicBufferExternalRefs[buffer->getId()]++; | 
|  | 525 |  | 
|  | 526 | if (const auto& iter = cache.find(buffer->getId()); iter == cache.end()) { | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 527 | std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 528 | std::make_shared<AutoBackendTexture::LocalRef>(grContext, | 
| Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 529 | buffer->toAHardwareBuffer(), | 
|  | 530 | isRenderable); | 
| Derek Sollenberger | eb904d4 | 2021-03-22 12:58:53 -0400 | [diff] [blame] | 531 | cache.insert({buffer->getId(), imageTextureRef}); | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 532 | } | 
|  | 533 | } | 
|  | 534 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 535 | void SkiaGLRenderEngine::unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) { | 
| Ana Krulec | dfec8f5 | 2021-01-13 12:51:47 -0800 | [diff] [blame] | 536 | ATRACE_CALL(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 537 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 538 | if (const auto& iter = mGraphicBufferExternalRefs.find(buffer->getId()); | 
|  | 539 | iter != mGraphicBufferExternalRefs.end()) { | 
|  | 540 | if (iter->second == 0) { | 
|  | 541 | ALOGW("Attempted to unmap GraphicBuffer <id: %" PRId64 | 
|  | 542 | "> from RenderEngine texture, but the " | 
|  | 543 | "ref count was already zero!", | 
|  | 544 | buffer->getId()); | 
|  | 545 | mGraphicBufferExternalRefs.erase(buffer->getId()); | 
|  | 546 | return; | 
|  | 547 | } | 
|  | 548 |  | 
|  | 549 | iter->second--; | 
|  | 550 |  | 
|  | 551 | if (iter->second == 0) { | 
|  | 552 | mTextureCache.erase(buffer->getId()); | 
|  | 553 | mProtectedTextureCache.erase(buffer->getId()); | 
|  | 554 | mGraphicBufferExternalRefs.erase(buffer->getId()); | 
|  | 555 | } | 
|  | 556 | } | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 557 | } | 
|  | 558 |  | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 559 | sk_sp<SkShader> SkiaGLRenderEngine::createRuntimeEffectShader( | 
|  | 560 | sk_sp<SkShader> shader, | 
|  | 561 | const LayerSettings* layer, const DisplaySettings& display, bool undoPremultipliedAlpha, | 
|  | 562 | bool requiresLinearEffect) { | 
|  | 563 | const auto stretchEffect = layer->stretchEffect; | 
| Nader Jawad | 63644d3 | 2021-05-07 10:44:21 -0700 | [diff] [blame] | 564 | // The given surface will be stretched by HWUI via matrix transformation | 
|  | 565 | // which gets similar results for most surfaces | 
|  | 566 | // Determine later on if we need to leverage the stertch shader within | 
|  | 567 | // surface flinger | 
| Nader Jawad | c088bdc | 2021-05-10 13:24:46 -0700 | [diff] [blame] | 568 | if (stretchEffect.hasEffect()) { | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 569 | const auto targetBuffer = layer->source.buffer.buffer; | 
| Nader Jawad | c088bdc | 2021-05-10 13:24:46 -0700 | [diff] [blame] | 570 | const auto graphicBuffer = targetBuffer ? targetBuffer->getBuffer() : nullptr; | 
|  | 571 | if (graphicBuffer && shader) { | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 572 | shader = mStretchShaderFactory.createSkShader(shader, stretchEffect); | 
|  | 573 | } | 
| John Reck | cdb4ed7 | 2021-02-04 13:39:33 -0500 | [diff] [blame] | 574 | } | 
| Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 575 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 576 | if (requiresLinearEffect) { | 
|  | 577 | const ui::Dataspace inputDataspace = | 
|  | 578 | mUseColorManagement ? layer->sourceDataspace : ui::Dataspace::UNKNOWN; | 
|  | 579 | const ui::Dataspace outputDataspace = | 
|  | 580 | mUseColorManagement ? display.outputDataspace : ui::Dataspace::UNKNOWN; | 
|  | 581 |  | 
|  | 582 | LinearEffect effect = LinearEffect{.inputDataspace = inputDataspace, | 
|  | 583 | .outputDataspace = outputDataspace, | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 584 | .undoPremultipliedAlpha = undoPremultipliedAlpha}; | 
|  | 585 |  | 
|  | 586 | auto effectIter = mRuntimeEffects.find(effect); | 
|  | 587 | sk_sp<SkRuntimeEffect> runtimeEffect = nullptr; | 
|  | 588 | if (effectIter == mRuntimeEffects.end()) { | 
|  | 589 | runtimeEffect = buildRuntimeEffect(effect); | 
|  | 590 | mRuntimeEffects.insert({effect, runtimeEffect}); | 
|  | 591 | } else { | 
|  | 592 | runtimeEffect = effectIter->second; | 
|  | 593 | } | 
| John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 594 | float maxLuminance = layer->source.buffer.maxLuminanceNits; | 
|  | 595 | // If the buffer doesn't have a max luminance, treat it as SDR & use the display's SDR | 
|  | 596 | // white point | 
|  | 597 | if (maxLuminance <= 0.f) { | 
|  | 598 | maxLuminance = display.sdrWhitePointNits; | 
|  | 599 | } | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 600 | return createLinearEffectShader(shader, effect, runtimeEffect, layer->colorTransform, | 
| John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 601 | display.maxLuminance, maxLuminance); | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 602 | } | 
|  | 603 | return shader; | 
|  | 604 | } | 
|  | 605 |  | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 606 | void SkiaGLRenderEngine::initCanvas(SkCanvas* canvas, const DisplaySettings& display) { | 
| Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 607 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 608 | // Record display settings when capture is running. | 
|  | 609 | std::stringstream displaySettings; | 
|  | 610 | PrintTo(display, &displaySettings); | 
|  | 611 | // Store the DisplaySettings in additional information. | 
|  | 612 | canvas->drawAnnotation(SkRect::MakeEmpty(), "DisplaySettings", | 
|  | 613 | SkData::MakeWithCString(displaySettings.str().c_str())); | 
|  | 614 | } | 
|  | 615 |  | 
|  | 616 | // Before doing any drawing, let's make sure that we'll start at the origin of the display. | 
|  | 617 | // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual | 
|  | 618 | // displays might have different scaling when compared to the physical screen. | 
|  | 619 |  | 
|  | 620 | canvas->clipRect(getSkRect(display.physicalDisplay)); | 
|  | 621 | canvas->translate(display.physicalDisplay.left, display.physicalDisplay.top); | 
|  | 622 |  | 
|  | 623 | const auto clipWidth = display.clip.width(); | 
|  | 624 | const auto clipHeight = display.clip.height(); | 
|  | 625 | auto rotatedClipWidth = clipWidth; | 
|  | 626 | auto rotatedClipHeight = clipHeight; | 
|  | 627 | // Scale is contingent on the rotation result. | 
|  | 628 | if (display.orientation & ui::Transform::ROT_90) { | 
|  | 629 | std::swap(rotatedClipWidth, rotatedClipHeight); | 
|  | 630 | } | 
|  | 631 | const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) / | 
|  | 632 | static_cast<SkScalar>(rotatedClipWidth); | 
|  | 633 | const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) / | 
|  | 634 | static_cast<SkScalar>(rotatedClipHeight); | 
|  | 635 | canvas->scale(scaleX, scaleY); | 
|  | 636 |  | 
|  | 637 | // Canvas rotation is done by centering the clip window at the origin, rotating, translating | 
|  | 638 | // back so that the top left corner of the clip is at (0, 0). | 
|  | 639 | canvas->translate(rotatedClipWidth / 2, rotatedClipHeight / 2); | 
|  | 640 | canvas->rotate(toDegrees(display.orientation)); | 
|  | 641 | canvas->translate(-clipWidth / 2, -clipHeight / 2); | 
|  | 642 | canvas->translate(-display.clip.left, -display.clip.top); | 
|  | 643 | } | 
|  | 644 |  | 
| Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 645 | class AutoSaveRestore { | 
|  | 646 | public: | 
|  | 647 | AutoSaveRestore(SkCanvas* canvas) : mCanvas(canvas) { mSaveCount = canvas->save(); } | 
|  | 648 | ~AutoSaveRestore() { restore(); } | 
|  | 649 | void replace(SkCanvas* canvas) { | 
|  | 650 | mCanvas = canvas; | 
|  | 651 | mSaveCount = canvas->save(); | 
|  | 652 | } | 
|  | 653 | void restore() { | 
|  | 654 | if (mCanvas) { | 
|  | 655 | mCanvas->restoreToCount(mSaveCount); | 
|  | 656 | mCanvas = nullptr; | 
|  | 657 | } | 
|  | 658 | } | 
|  | 659 |  | 
|  | 660 | private: | 
|  | 661 | SkCanvas* mCanvas; | 
|  | 662 | int mSaveCount; | 
|  | 663 | }; | 
|  | 664 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 665 | static SkRRect getBlurRRect(const BlurRegion& region) { | 
|  | 666 | const auto rect = SkRect::MakeLTRB(region.left, region.top, region.right, region.bottom); | 
|  | 667 | const SkVector radii[4] = {SkVector::Make(region.cornerRadiusTL, region.cornerRadiusTL), | 
|  | 668 | SkVector::Make(region.cornerRadiusTR, region.cornerRadiusTR), | 
|  | 669 | SkVector::Make(region.cornerRadiusBR, region.cornerRadiusBR), | 
|  | 670 | SkVector::Make(region.cornerRadiusBL, region.cornerRadiusBL)}; | 
|  | 671 | SkRRect roundedRect; | 
|  | 672 | roundedRect.setRectRadii(rect, radii); | 
|  | 673 | return roundedRect; | 
|  | 674 | } | 
|  | 675 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 676 | status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display, | 
|  | 677 | const std::vector<const LayerSettings*>& layers, | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 678 | const std::shared_ptr<ExternalTexture>& buffer, | 
|  | 679 | const bool /*useFramebufferCache*/, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 680 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) { | 
|  | 681 | ATRACE_NAME("SkiaGL::drawLayers"); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 682 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 683 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 684 | if (layers.empty()) { | 
|  | 685 | ALOGV("Drawing empty layer stack"); | 
|  | 686 | return NO_ERROR; | 
|  | 687 | } | 
|  | 688 |  | 
|  | 689 | if (bufferFence.get() >= 0) { | 
|  | 690 | // Duplicate the fence for passing to waitFence. | 
|  | 691 | base::unique_fd bufferFenceDup(dup(bufferFence.get())); | 
|  | 692 | if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) { | 
|  | 693 | ATRACE_NAME("Waiting before draw"); | 
|  | 694 | sync_wait(bufferFence.get(), -1); | 
|  | 695 | } | 
|  | 696 | } | 
|  | 697 | if (buffer == nullptr) { | 
|  | 698 | ALOGE("No output buffer provided. Aborting GPU composition."); | 
|  | 699 | return BAD_VALUE; | 
|  | 700 | } | 
|  | 701 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 702 | validateOutputBufferUsage(buffer->getBuffer()); | 
| Ady Abraham | 193426d | 2021-02-18 14:01:53 -0800 | [diff] [blame] | 703 |  | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 704 | auto grContext = getActiveGrContext(); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 705 | auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 706 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 707 | std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef; | 
|  | 708 | if (const auto& it = cache.find(buffer->getBuffer()->getId()); it != cache.end()) { | 
|  | 709 | surfaceTextureRef = it->second; | 
|  | 710 | } else { | 
| Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 711 | surfaceTextureRef = | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 712 | std::make_shared<AutoBackendTexture::LocalRef>(grContext, | 
|  | 713 | buffer->getBuffer() | 
|  | 714 | ->toAHardwareBuffer(), | 
|  | 715 | true); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 716 | } | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 717 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 718 | const ui::Dataspace dstDataspace = | 
|  | 719 | mUseColorManagement ? display.outputDataspace : ui::Dataspace::UNKNOWN; | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 720 | sk_sp<SkSurface> dstSurface = surfaceTextureRef->getOrCreateSurface(dstDataspace, grContext); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 721 |  | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 722 | SkCanvas* dstCanvas = mCapture->tryCapture(dstSurface.get()); | 
|  | 723 | if (dstCanvas == nullptr) { | 
| Ana Krulec | 6eab17a | 2020-12-09 15:52:36 -0800 | [diff] [blame] | 724 | ALOGE("Cannot acquire canvas from Skia."); | 
|  | 725 | return BAD_VALUE; | 
|  | 726 | } | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 727 |  | 
|  | 728 | // Find if any layers have requested blur, we'll use that info to decide when to render to an | 
|  | 729 | // offscreen buffer and when to render to the native buffer. | 
|  | 730 | sk_sp<SkSurface> activeSurface(dstSurface); | 
|  | 731 | SkCanvas* canvas = dstCanvas; | 
| Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 732 | SkiaCapture::OffscreenState offscreenCaptureState; | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 733 | const LayerSettings* blurCompositionLayer = nullptr; | 
|  | 734 | if (mBlurFilter) { | 
|  | 735 | bool requiresCompositionLayer = false; | 
|  | 736 | for (const auto& layer : layers) { | 
| Derek Sollenberger | 3134ec3 | 2021-02-12 11:26:23 -0500 | [diff] [blame] | 737 | if (layer->backgroundBlurRadius > 0 && | 
|  | 738 | layer->backgroundBlurRadius < BlurFilter::kMaxCrossFadeRadius) { | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 739 | requiresCompositionLayer = true; | 
|  | 740 | } | 
|  | 741 | for (auto region : layer->blurRegions) { | 
|  | 742 | if (region.blurRadius < BlurFilter::kMaxCrossFadeRadius) { | 
|  | 743 | requiresCompositionLayer = true; | 
|  | 744 | } | 
|  | 745 | } | 
|  | 746 | if (requiresCompositionLayer) { | 
|  | 747 | activeSurface = dstSurface->makeSurface(dstSurface->imageInfo()); | 
| Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 748 | canvas = mCapture->tryOffscreenCapture(activeSurface.get(), &offscreenCaptureState); | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 749 | blurCompositionLayer = layer; | 
|  | 750 | break; | 
|  | 751 | } | 
|  | 752 | } | 
|  | 753 | } | 
|  | 754 |  | 
| Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 755 | AutoSaveRestore surfaceAutoSaveRestore(canvas); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 756 | // Clear the entire canvas with a transparent black to prevent ghost images. | 
|  | 757 | canvas->clear(SK_ColorTRANSPARENT); | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 758 | initCanvas(canvas, display); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 759 |  | 
|  | 760 | // TODO: clearRegion was required for SurfaceView when a buffer is not yet available but the | 
|  | 761 | // view is still on-screen. The clear region could be re-specified as a black color layer, | 
|  | 762 | // however. | 
|  | 763 | if (!display.clearRegion.isEmpty()) { | 
| Derek Sollenberger | 545ec44 | 2021-01-25 10:02:23 -0500 | [diff] [blame] | 764 | ATRACE_NAME("ClearRegion"); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 765 | size_t numRects = 0; | 
|  | 766 | Rect const* rects = display.clearRegion.getArray(&numRects); | 
|  | 767 | SkIRect skRects[numRects]; | 
|  | 768 | for (int i = 0; i < numRects; ++i) { | 
|  | 769 | skRects[i] = | 
|  | 770 | SkIRect::MakeLTRB(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom); | 
|  | 771 | } | 
|  | 772 | SkRegion clearRegion; | 
|  | 773 | SkPaint paint; | 
|  | 774 | sk_sp<SkShader> shader = | 
|  | 775 | SkShaders::Color(SkColor4f{.fR = 0., .fG = 0., .fB = 0., .fA = 1.0}, | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 776 | toSkColorSpace(dstDataspace)); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 777 | paint.setShader(shader); | 
|  | 778 | clearRegion.setRects(skRects, numRects); | 
|  | 779 | canvas->drawRegion(clearRegion, paint); | 
|  | 780 | } | 
|  | 781 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 782 | // setup color filter if necessary | 
|  | 783 | sk_sp<SkColorFilter> displayColorTransform; | 
|  | 784 | if (display.colorTransform != mat4()) { | 
|  | 785 | displayColorTransform = SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform)); | 
|  | 786 | } | 
|  | 787 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 788 | for (const auto& layer : layers) { | 
| Derek Sollenberger | 545ec44 | 2021-01-25 10:02:23 -0500 | [diff] [blame] | 789 | ATRACE_NAME("DrawLayer"); | 
| Galia Peycheva | f7889b3 | 2020-11-25 22:22:40 +0100 | [diff] [blame] | 790 |  | 
| Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 791 | if (kPrintLayerSettings) { | 
|  | 792 | std::stringstream ls; | 
|  | 793 | PrintTo(*layer, &ls); | 
|  | 794 | auto debugs = ls.str(); | 
|  | 795 | int pos = 0; | 
|  | 796 | while (pos < debugs.size()) { | 
|  | 797 | ALOGD("cache_debug %s", debugs.substr(pos, 1000).c_str()); | 
|  | 798 | pos += 1000; | 
|  | 799 | } | 
|  | 800 | } | 
|  | 801 |  | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 802 | sk_sp<SkImage> blurInput; | 
|  | 803 | if (blurCompositionLayer == layer) { | 
|  | 804 | LOG_ALWAYS_FATAL_IF(activeSurface == dstSurface); | 
|  | 805 | LOG_ALWAYS_FATAL_IF(canvas == dstCanvas); | 
|  | 806 |  | 
|  | 807 | // save a snapshot of the activeSurface to use as input to the blur shaders | 
|  | 808 | blurInput = activeSurface->makeImageSnapshot(); | 
|  | 809 |  | 
|  | 810 | // TODO we could skip this step if we know the blur will cover the entire image | 
|  | 811 | //  blit the offscreen framebuffer into the destination AHB | 
|  | 812 | SkPaint paint; | 
|  | 813 | paint.setBlendMode(SkBlendMode::kSrc); | 
| Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 814 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { | 
|  | 815 | uint64_t id = mCapture->endOffscreenCapture(&offscreenCaptureState); | 
|  | 816 | dstCanvas->drawAnnotation(SkRect::Make(dstCanvas->imageInfo().dimensions()), | 
|  | 817 | String8::format("SurfaceID|%" PRId64, id).c_str(), | 
|  | 818 | nullptr); | 
|  | 819 | dstCanvas->drawImage(blurInput, 0, 0, SkSamplingOptions(), &paint); | 
|  | 820 | } else { | 
|  | 821 | activeSurface->draw(dstCanvas, 0, 0, SkSamplingOptions(), &paint); | 
|  | 822 | } | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 823 |  | 
|  | 824 | // assign dstCanvas to canvas and ensure that the canvas state is up to date | 
|  | 825 | canvas = dstCanvas; | 
| Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 826 | surfaceAutoSaveRestore.replace(canvas); | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 827 | initCanvas(canvas, display); | 
|  | 828 |  | 
|  | 829 | LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getSaveCount() != | 
|  | 830 | dstSurface->getCanvas()->getSaveCount()); | 
|  | 831 | LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getTotalMatrix() != | 
|  | 832 | dstSurface->getCanvas()->getTotalMatrix()); | 
|  | 833 |  | 
|  | 834 | // assign dstSurface to activeSurface | 
|  | 835 | activeSurface = dstSurface; | 
|  | 836 | } | 
|  | 837 |  | 
| Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 838 | SkAutoCanvasRestore layerAutoSaveRestore(canvas, true); | 
| Derek Sollenberger | 76664d6 | 2021-02-04 11:13:09 -0500 | [diff] [blame] | 839 | if (CC_UNLIKELY(mCapture->isCaptureRunning())) { | 
| Ana Krulec | 6eab17a | 2020-12-09 15:52:36 -0800 | [diff] [blame] | 840 | // Record the name of the layer if the capture is running. | 
|  | 841 | std::stringstream layerSettings; | 
|  | 842 | PrintTo(*layer, &layerSettings); | 
|  | 843 | // Store the LayerSettings in additional information. | 
|  | 844 | canvas->drawAnnotation(SkRect::MakeEmpty(), layer->name.c_str(), | 
|  | 845 | SkData::MakeWithCString(layerSettings.str().c_str())); | 
|  | 846 | } | 
| Galia Peycheva | f7889b3 | 2020-11-25 22:22:40 +0100 | [diff] [blame] | 847 | // Layers have a local transform that should be applied to them | 
|  | 848 | canvas->concat(getSkM44(layer->geometry.positionTransform).asM33()); | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 849 |  | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 850 | const auto [bounds, roundRectClip] = | 
|  | 851 | getBoundsAndClip(layer->geometry.boundaries, layer->geometry.roundedCornersCrop, | 
|  | 852 | layer->geometry.roundedCornersRadius); | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 853 | if (mBlurFilter && layerHasBlur(layer)) { | 
|  | 854 | std::unordered_map<uint32_t, sk_sp<SkImage>> cachedBlurs; | 
|  | 855 |  | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 856 | // if multiple layers have blur, then we need to take a snapshot now because | 
|  | 857 | // only the lowest layer will have blurImage populated earlier | 
|  | 858 | if (!blurInput) { | 
|  | 859 | blurInput = activeSurface->makeImageSnapshot(); | 
|  | 860 | } | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 861 | // rect to be blurred in the coordinate space of blurInput | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 862 | const auto blurRect = canvas->getTotalMatrix().mapRect(bounds.rect()); | 
|  | 863 |  | 
|  | 864 | // if the clip needs to be applied then apply it now and make sure | 
|  | 865 | // it is restored before we attempt to draw any shadows. | 
|  | 866 | SkAutoCanvasRestore acr(canvas, true); | 
|  | 867 | if (!roundRectClip.isEmpty()) { | 
|  | 868 | canvas->clipRRect(roundRectClip, true); | 
|  | 869 | } | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 870 |  | 
| Galia Peycheva | e425ac8 | 2021-03-15 17:12:03 +0100 | [diff] [blame] | 871 | // TODO(b/182216890): Filter out empty layers earlier | 
|  | 872 | if (blurRect.width() > 0 && blurRect.height() > 0) { | 
|  | 873 | if (layer->backgroundBlurRadius > 0) { | 
|  | 874 | ATRACE_NAME("BackgroundBlur"); | 
|  | 875 | auto blurredImage = | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 876 | mBlurFilter->generate(grContext, layer->backgroundBlurRadius, blurInput, | 
|  | 877 | blurRect); | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 878 |  | 
| Galia Peycheva | e425ac8 | 2021-03-15 17:12:03 +0100 | [diff] [blame] | 879 | cachedBlurs[layer->backgroundBlurRadius] = blurredImage; | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 880 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 881 | mBlurFilter->drawBlurRegion(canvas, bounds, layer->backgroundBlurRadius, 1.0f, | 
|  | 882 | blurRect, blurredImage, blurInput); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 883 | } | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 884 |  | 
| Derek Sollenberger | 39feade | 2021-04-27 16:08:40 -0400 | [diff] [blame] | 885 | canvas->concat(getSkM44(layer->blurRegionTransform).asM33()); | 
| Galia Peycheva | e425ac8 | 2021-03-15 17:12:03 +0100 | [diff] [blame] | 886 | for (auto region : layer->blurRegions) { | 
|  | 887 | if (cachedBlurs[region.blurRadius] == nullptr) { | 
|  | 888 | ATRACE_NAME("BlurRegion"); | 
|  | 889 | cachedBlurs[region.blurRadius] = | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 890 | mBlurFilter->generate(grContext, region.blurRadius, blurInput, | 
| Galia Peycheva | e425ac8 | 2021-03-15 17:12:03 +0100 | [diff] [blame] | 891 | blurRect); | 
|  | 892 | } | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 893 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 894 | mBlurFilter->drawBlurRegion(canvas, getBlurRRect(region), region.blurRadius, | 
|  | 895 | region.alpha, blurRect, | 
| Galia Peycheva | e425ac8 | 2021-03-15 17:12:03 +0100 | [diff] [blame] | 896 | cachedBlurs[region.blurRadius], blurInput); | 
|  | 897 | } | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 898 | } | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 899 | } | 
|  | 900 |  | 
| Derek Sollenberger | 4c331c8 | 2021-02-23 13:09:50 -0500 | [diff] [blame] | 901 | if (layer->shadow.length > 0) { | 
| Leon Scroggins III | cf3d95c | 2021-03-19 13:06:32 -0400 | [diff] [blame] | 902 | // This would require a new parameter/flag to SkShadowUtils::DrawShadow | 
|  | 903 | LOG_ALWAYS_FATAL_IF(layer->disableBlending, "Cannot disableBlending with a shadow"); | 
| Leon Scroggins III | 63e8695 | 2021-05-12 10:45:08 -0400 | [diff] [blame] | 904 |  | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 905 | SkRRect shadowBounds, shadowClip; | 
|  | 906 | if (layer->geometry.boundaries == layer->shadow.boundaries) { | 
|  | 907 | shadowBounds = bounds; | 
|  | 908 | shadowClip = roundRectClip; | 
|  | 909 | } else { | 
|  | 910 | std::tie(shadowBounds, shadowClip) = | 
|  | 911 | getBoundsAndClip(layer->shadow.boundaries, | 
|  | 912 | layer->geometry.roundedCornersCrop, | 
|  | 913 | layer->geometry.roundedCornersRadius); | 
|  | 914 | } | 
|  | 915 |  | 
| Leon Scroggins III | 63e8695 | 2021-05-12 10:45:08 -0400 | [diff] [blame] | 916 | // Technically, if bounds is a rect and roundRectClip is not empty, | 
|  | 917 | // it means that the bounds and roundedCornersCrop were different | 
|  | 918 | // enough that we should intersect them to find the proper shadow. | 
|  | 919 | // In practice, this often happens when the two rectangles appear to | 
|  | 920 | // not match due to rounding errors. Draw the rounded version, which | 
|  | 921 | // looks more like the intent. | 
|  | 922 | const auto& rrect = | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 923 | shadowBounds.isRect() && !shadowClip.isEmpty() ? shadowClip : shadowBounds; | 
| Leon Scroggins III | 63e8695 | 2021-05-12 10:45:08 -0400 | [diff] [blame] | 924 | drawShadow(canvas, rrect, layer->shadow); | 
| Derek Sollenberger | 4c331c8 | 2021-02-23 13:09:50 -0500 | [diff] [blame] | 925 | } | 
|  | 926 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 927 | const bool requiresLinearEffect = layer->colorTransform != mat4() || | 
|  | 928 | (mUseColorManagement && | 
| John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 929 | needsToneMapping(layer->sourceDataspace, display.outputDataspace)) || | 
|  | 930 | (display.sdrWhitePointNits > 0.f && | 
|  | 931 | display.sdrWhitePointNits != display.maxLuminance); | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 932 |  | 
|  | 933 | // quick abort from drawing the remaining portion of the layer | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 934 | if (layer->skipContentDraw || | 
|  | 935 | (layer->alpha == 0 && !requiresLinearEffect && !layer->disableBlending && | 
|  | 936 | (!displayColorTransform || displayColorTransform->isAlphaUnchanged()))) { | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 937 | continue; | 
|  | 938 | } | 
|  | 939 |  | 
|  | 940 | // If we need to map to linear space or color management is disabled, then mark the source | 
|  | 941 | // image with the same colorspace as the destination surface so that Skia's color | 
|  | 942 | // management is a no-op. | 
|  | 943 | const ui::Dataspace layerDataspace = (!mUseColorManagement || requiresLinearEffect) | 
|  | 944 | ? dstDataspace | 
|  | 945 | : layer->sourceDataspace; | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 946 |  | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 947 | SkPaint paint; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 948 | if (layer->source.buffer.buffer) { | 
|  | 949 | ATRACE_NAME("DrawImage"); | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 950 | validateInputBufferUsage(layer->source.buffer.buffer->getBuffer()); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 951 | const auto& item = layer->source.buffer; | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 952 | std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr; | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 953 |  | 
|  | 954 | if (const auto& iter = cache.find(item.buffer->getBuffer()->getId()); | 
|  | 955 | iter != cache.end()) { | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 956 | imageTextureRef = iter->second; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 957 | } else { | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 958 | // If we didn't find the image in the cache, then create a local ref but don't cache | 
|  | 959 | // it. If we're using skia, we're guaranteed to run on a dedicated GPU thread so if | 
|  | 960 | // we didn't find anything in the cache then we intentionally did not cache this | 
|  | 961 | // buffer's resources. | 
| Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 962 | imageTextureRef = std::make_shared< | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 963 | AutoBackendTexture::LocalRef>(grContext, | 
| Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 964 | item.buffer->getBuffer()->toAHardwareBuffer(), | 
|  | 965 | false); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 966 | } | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 967 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 968 | sk_sp<SkImage> image = | 
| Derek Sollenberger | d8fdae3 | 2021-04-09 13:52:59 -0400 | [diff] [blame] | 969 | imageTextureRef->makeImage(layerDataspace, | 
|  | 970 | item.usePremultipliedAlpha ? kPremul_SkAlphaType | 
|  | 971 | : kUnpremul_SkAlphaType, | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 972 | grContext); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 973 |  | 
|  | 974 | auto texMatrix = getSkM44(item.textureTransform).asM33(); | 
|  | 975 | // textureTansform was intended to be passed directly into a shader, so when | 
|  | 976 | // building the total matrix with the textureTransform we need to first | 
|  | 977 | // normalize it, then apply the textureTransform, then scale back up. | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 978 | texMatrix.preScale(1.0f / bounds.width(), 1.0f / bounds.height()); | 
| Huihong Luo | 3a3cf3c | 2020-12-07 17:05:41 -0800 | [diff] [blame] | 979 | texMatrix.postScale(image->width(), image->height()); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 980 |  | 
| Huihong Luo | 3a3cf3c | 2020-12-07 17:05:41 -0800 | [diff] [blame] | 981 | SkMatrix matrix; | 
|  | 982 | if (!texMatrix.invert(&matrix)) { | 
|  | 983 | matrix = texMatrix; | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 984 | } | 
| Ana Krulec | f9a15d9 | 2020-12-11 08:35:00 -0800 | [diff] [blame] | 985 | // The shader does not respect the translation, so we add it to the texture | 
|  | 986 | // transform for the SkImage. This will make sure that the correct layer contents | 
|  | 987 | // are drawn in the correct part of the screen. | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 988 | matrix.postTranslate(bounds.rect().fLeft, bounds.rect().fTop); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 989 |  | 
| Ana Krulec | b7b28b2 | 2020-11-23 14:48:58 -0800 | [diff] [blame] | 990 | sk_sp<SkShader> shader; | 
|  | 991 |  | 
|  | 992 | if (layer->source.buffer.useTextureFiltering) { | 
|  | 993 | shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, | 
|  | 994 | SkSamplingOptions( | 
|  | 995 | {SkFilterMode::kLinear, SkMipmapMode::kNone}), | 
|  | 996 | &matrix); | 
|  | 997 | } else { | 
| Mike Reed | 711e1f0 | 2020-12-11 13:06:19 -0500 | [diff] [blame] | 998 | shader = image->makeShader(SkSamplingOptions(), matrix); | 
| Ana Krulec | b7b28b2 | 2020-11-23 14:48:58 -0800 | [diff] [blame] | 999 | } | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 1000 |  | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 1001 | // Handle opaque images - it's a little nonstandard how we do this. | 
|  | 1002 | // Fundamentally we need to support SurfaceControl.Builder#setOpaque: | 
|  | 1003 | // https://developer.android.com/reference/android/view/SurfaceControl.Builder#setOpaque(boolean) | 
|  | 1004 | // The important language is that when isOpaque is set, opacity is not sampled from the | 
|  | 1005 | // alpha channel, but blending may still be supported on a transaction via setAlpha. So, | 
|  | 1006 | // here's the conundrum: | 
|  | 1007 | // 1. We can't force the SkImage alpha type to kOpaque_SkAlphaType, because it's treated | 
|  | 1008 | // as an internal hint - composition is undefined when there are alpha bits present. | 
|  | 1009 | // 2. We can try to lie about the pixel layout, but that only works for RGBA8888 | 
|  | 1010 | // buffers, i.e., treating them as RGBx8888 instead. But we can't do the same for | 
|  | 1011 | // RGBA1010102 because RGBx1010102 is not supported as a pixel layout for SkImages. It's | 
|  | 1012 | // also not clear what to use for F16 either, and lying about the pixel layout is a bit | 
|  | 1013 | // of a hack anyways. | 
|  | 1014 | // 3. We can't change the blendmode to src, because while this satisfies the requirement | 
|  | 1015 | // for ignoring the alpha channel, it doesn't quite satisfy the blending requirement | 
|  | 1016 | // because src always clobbers the destination content. | 
|  | 1017 | // | 
|  | 1018 | // So, what we do here instead is an additive blend mode where we compose the input | 
|  | 1019 | // image with a solid black. This might need to be reassess if this does not support | 
|  | 1020 | // FP16 incredibly well, but FP16 end-to-end isn't well supported anyway at the moment. | 
|  | 1021 | if (item.isOpaque) { | 
|  | 1022 | shader = SkShaders::Blend(SkBlendMode::kPlus, shader, | 
|  | 1023 | SkShaders::Color(SkColors::kBlack, | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 1024 | toSkColorSpace(layerDataspace))); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 1025 | } | 
|  | 1026 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 1027 | paint.setShader(createRuntimeEffectShader(shader, layer, display, | 
|  | 1028 | !item.isOpaque && item.usePremultipliedAlpha, | 
|  | 1029 | requiresLinearEffect)); | 
| Ana Krulec | 1768bd2 | 2020-11-23 14:51:31 -0800 | [diff] [blame] | 1030 | paint.setAlphaf(layer->alpha); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1031 | } else { | 
|  | 1032 | ATRACE_NAME("DrawColor"); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1033 | const auto color = layer->source.solidColor; | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 1034 | sk_sp<SkShader> shader = SkShaders::Color(SkColor4f{.fR = color.r, | 
|  | 1035 | .fG = color.g, | 
|  | 1036 | .fB = color.b, | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 1037 | .fA = layer->alpha}, | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 1038 | toSkColorSpace(layerDataspace)); | 
| Ana Krulec | 4781421 | 2021-01-06 19:00:10 -0800 | [diff] [blame] | 1039 | paint.setShader(createRuntimeEffectShader(shader, layer, display, | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 1040 | /* undoPremultipliedAlpha */ false, | 
|  | 1041 | requiresLinearEffect)); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1042 | } | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 1043 |  | 
| Leon Scroggins III | cf3d95c | 2021-03-19 13:06:32 -0400 | [diff] [blame] | 1044 | if (layer->disableBlending) { | 
|  | 1045 | paint.setBlendMode(SkBlendMode::kSrc); | 
|  | 1046 | } | 
|  | 1047 |  | 
| Derek Sollenberger | e2fe78c | 2021-02-23 13:22:54 -0500 | [diff] [blame] | 1048 | paint.setColorFilter(displayColorTransform); | 
| Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 1049 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1050 | if (!roundRectClip.isEmpty()) { | 
|  | 1051 | canvas->clipRRect(roundRectClip, true); | 
|  | 1052 | } | 
|  | 1053 |  | 
|  | 1054 | if (!bounds.isRect()) { | 
| Derek Sollenberger | 4c331c8 | 2021-02-23 13:09:50 -0500 | [diff] [blame] | 1055 | paint.setAntiAlias(true); | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1056 | canvas->drawRRect(bounds, paint); | 
| Alec Mouri | bd17b3b | 2020-12-17 11:08:30 -0800 | [diff] [blame] | 1057 | } else { | 
| Nader Jawad | 63644d3 | 2021-05-07 10:44:21 -0700 | [diff] [blame] | 1058 | canvas->drawRect(bounds.rect(), paint); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1059 | } | 
| Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 1060 | if (kFlushAfterEveryLayer) { | 
|  | 1061 | ATRACE_NAME("flush surface"); | 
|  | 1062 | activeSurface->flush(); | 
|  | 1063 | } | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1064 | } | 
| Derek Sollenberger | 7c42bef | 2021-02-23 13:01:39 -0500 | [diff] [blame] | 1065 | surfaceAutoSaveRestore.restore(); | 
| Alec Mouri | c0aae73 | 2021-01-12 13:32:18 -0800 | [diff] [blame] | 1066 | mCapture->endCapture(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1067 | { | 
|  | 1068 | ATRACE_NAME("flush surface"); | 
| Derek Sollenberger | 3f77aa4 | 2021-02-04 11:06:34 -0500 | [diff] [blame] | 1069 | LOG_ALWAYS_FATAL_IF(activeSurface != dstSurface); | 
|  | 1070 | activeSurface->flush(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1071 | } | 
|  | 1072 |  | 
|  | 1073 | if (drawFence != nullptr) { | 
|  | 1074 | *drawFence = flush(); | 
|  | 1075 | } | 
|  | 1076 |  | 
|  | 1077 | // If flush failed or we don't support native fences, we need to force the | 
|  | 1078 | // gl command stream to be executed. | 
|  | 1079 | bool requireSync = drawFence == nullptr || drawFence->get() < 0; | 
|  | 1080 | if (requireSync) { | 
|  | 1081 | ATRACE_BEGIN("Submit(sync=true)"); | 
|  | 1082 | } else { | 
|  | 1083 | ATRACE_BEGIN("Submit(sync=false)"); | 
|  | 1084 | } | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 1085 | bool success = grContext->submit(requireSync); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1086 | ATRACE_END(); | 
|  | 1087 | if (!success) { | 
|  | 1088 | ALOGE("Failed to flush RenderEngine commands"); | 
|  | 1089 | // Chances are, something illegal happened (either the caller passed | 
|  | 1090 | // us bad parameters, or we messed up our shader generation). | 
|  | 1091 | return INVALID_OPERATION; | 
|  | 1092 | } | 
|  | 1093 |  | 
|  | 1094 | // checkErrors(); | 
|  | 1095 | return NO_ERROR; | 
|  | 1096 | } | 
|  | 1097 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1098 | inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) { | 
|  | 1099 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); | 
|  | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) { | 
|  | 1103 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); | 
|  | 1104 | } | 
|  | 1105 |  | 
| Derek Sollenberger | c31985e | 2021-05-18 16:38:17 -0400 | [diff] [blame] | 1106 | inline std::pair<SkRRect, SkRRect> SkiaGLRenderEngine::getBoundsAndClip(const FloatRect& boundsRect, | 
|  | 1107 | const FloatRect& cropRect, | 
|  | 1108 | const float cornerRadius) { | 
|  | 1109 | const SkRect bounds = getSkRect(boundsRect); | 
|  | 1110 | const SkRect crop = getSkRect(cropRect); | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 1111 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1112 | SkRRect clip; | 
|  | 1113 | if (cornerRadius > 0) { | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1114 | // it the crop and the bounds are equivalent or there is no crop then we don't need a clip | 
|  | 1115 | if (bounds == crop || crop.isEmpty()) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1116 | return {SkRRect::MakeRectXY(bounds, cornerRadius, cornerRadius), clip}; | 
|  | 1117 | } | 
|  | 1118 |  | 
|  | 1119 | // This makes an effort to speed up common, simple bounds + clip combinations by | 
|  | 1120 | // converting them to a single RRect draw. It is possible there are other cases | 
|  | 1121 | // that can be converted. | 
|  | 1122 | if (crop.contains(bounds)) { | 
|  | 1123 | bool intersectionIsRoundRect = true; | 
|  | 1124 | // check each cropped corner to ensure that it exactly matches the crop or is full | 
|  | 1125 | SkVector radii[4]; | 
|  | 1126 |  | 
|  | 1127 | const auto insetCrop = crop.makeInset(cornerRadius, cornerRadius); | 
|  | 1128 |  | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1129 | const bool leftEqual = bounds.fLeft == crop.fLeft; | 
|  | 1130 | const bool topEqual = bounds.fTop == crop.fTop; | 
|  | 1131 | const bool rightEqual = bounds.fRight == crop.fRight; | 
|  | 1132 | const bool bottomEqual = bounds.fBottom == crop.fBottom; | 
|  | 1133 |  | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1134 | // compute the UpperLeft corner radius | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1135 | if (leftEqual && topEqual) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1136 | radii[0].set(cornerRadius, cornerRadius); | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1137 | } else if ((leftEqual && bounds.fTop >= insetCrop.fTop) || | 
|  | 1138 | (topEqual && bounds.fLeft >= insetCrop.fLeft) || | 
|  | 1139 | insetCrop.contains(bounds.fLeft, bounds.fTop)) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1140 | radii[0].set(0, 0); | 
|  | 1141 | } else { | 
|  | 1142 | intersectionIsRoundRect = false; | 
|  | 1143 | } | 
|  | 1144 | // compute the UpperRight corner radius | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1145 | if (rightEqual && topEqual) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1146 | radii[1].set(cornerRadius, cornerRadius); | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1147 | } else if ((rightEqual && bounds.fTop >= insetCrop.fTop) || | 
|  | 1148 | (topEqual && bounds.fRight <= insetCrop.fRight) || | 
|  | 1149 | insetCrop.contains(bounds.fRight, bounds.fTop)) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1150 | radii[1].set(0, 0); | 
|  | 1151 | } else { | 
|  | 1152 | intersectionIsRoundRect = false; | 
|  | 1153 | } | 
|  | 1154 | // compute the BottomRight corner radius | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1155 | if (rightEqual && bottomEqual) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1156 | radii[2].set(cornerRadius, cornerRadius); | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1157 | } else if ((rightEqual && bounds.fBottom <= insetCrop.fBottom) || | 
|  | 1158 | (bottomEqual && bounds.fRight <= insetCrop.fRight) || | 
|  | 1159 | insetCrop.contains(bounds.fRight, bounds.fBottom)) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1160 | radii[2].set(0, 0); | 
|  | 1161 | } else { | 
|  | 1162 | intersectionIsRoundRect = false; | 
|  | 1163 | } | 
|  | 1164 | // compute the BottomLeft corner radius | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1165 | if (leftEqual && bottomEqual) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1166 | radii[3].set(cornerRadius, cornerRadius); | 
| Derek Sollenberger | 4f9959f | 2021-05-12 16:47:37 -0400 | [diff] [blame] | 1167 | } else if ((leftEqual && bounds.fBottom <= insetCrop.fBottom) || | 
|  | 1168 | (bottomEqual && bounds.fLeft >= insetCrop.fLeft) || | 
|  | 1169 | insetCrop.contains(bounds.fLeft, bounds.fBottom)) { | 
| Derek Sollenberger | 8e8b3bf | 2021-04-29 15:35:28 -0400 | [diff] [blame] | 1170 | radii[3].set(0, 0); | 
|  | 1171 | } else { | 
|  | 1172 | intersectionIsRoundRect = false; | 
|  | 1173 | } | 
|  | 1174 |  | 
|  | 1175 | if (intersectionIsRoundRect) { | 
|  | 1176 | SkRRect intersectionBounds; | 
|  | 1177 | intersectionBounds.setRectRadii(bounds, radii); | 
|  | 1178 | return {intersectionBounds, clip}; | 
|  | 1179 | } | 
|  | 1180 | } | 
|  | 1181 |  | 
|  | 1182 | // we didn't it any of our fast paths so set the clip to the cropRect | 
|  | 1183 | clip.setRectXY(crop, cornerRadius, cornerRadius); | 
|  | 1184 | } | 
|  | 1185 |  | 
|  | 1186 | // if we hit this point then we either don't have rounded corners or we are going to rely | 
|  | 1187 | // on the clip to round the corners for us | 
|  | 1188 | return {SkRRect::MakeRect(bounds), clip}; | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 1189 | } | 
|  | 1190 |  | 
| Derek Sollenberger | ecb2146 | 2021-01-29 16:53:49 -0500 | [diff] [blame] | 1191 | inline bool SkiaGLRenderEngine::layerHasBlur(const LayerSettings* layer) { | 
|  | 1192 | return layer->backgroundBlurRadius > 0 || layer->blurRegions.size(); | 
|  | 1193 | } | 
|  | 1194 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1195 | inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) { | 
|  | 1196 | return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255); | 
|  | 1197 | } | 
|  | 1198 |  | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 1199 | inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) { | 
|  | 1200 | return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], | 
|  | 1201 | matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1], | 
|  | 1202 | matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2], | 
|  | 1203 | matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]); | 
|  | 1204 | } | 
|  | 1205 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1206 | inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) { | 
|  | 1207 | return SkPoint3::Make(vector.x, vector.y, vector.z); | 
|  | 1208 | } | 
|  | 1209 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1210 | size_t SkiaGLRenderEngine::getMaxTextureSize() const { | 
|  | 1211 | return mGrContext->maxTextureSize(); | 
|  | 1212 | } | 
|  | 1213 |  | 
|  | 1214 | size_t SkiaGLRenderEngine::getMaxViewportDims() const { | 
|  | 1215 | return mGrContext->maxRenderTargetSize(); | 
|  | 1216 | } | 
|  | 1217 |  | 
| Derek Sollenberger | b0e764c | 2021-05-04 14:31:37 -0400 | [diff] [blame] | 1218 | void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRRect& casterRRect, | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1219 | const ShadowSettings& settings) { | 
|  | 1220 | ATRACE_CALL(); | 
|  | 1221 | const float casterZ = settings.length / 2.0f; | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1222 | const auto flags = | 
|  | 1223 | settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag; | 
|  | 1224 |  | 
| Derek Sollenberger | b0e764c | 2021-05-04 14:31:37 -0400 | [diff] [blame] | 1225 | SkShadowUtils::DrawShadow(canvas, SkPath::RRect(casterRRect), SkPoint3::Make(0, 0, casterZ), | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 1226 | getSkPoint3(settings.lightPos), settings.lightRadius, | 
|  | 1227 | getSkColor(settings.ambientColor), getSkColor(settings.spotColor), | 
|  | 1228 | flags); | 
|  | 1229 | } | 
|  | 1230 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1231 | EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config, | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 1232 | EGLContext shareContext, | 
|  | 1233 | std::optional<ContextPriority> contextPriority, | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1234 | Protection protection) { | 
|  | 1235 | EGLint renderableType = 0; | 
|  | 1236 | if (config == EGL_NO_CONFIG_KHR) { | 
|  | 1237 | renderableType = EGL_OPENGL_ES3_BIT; | 
|  | 1238 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { | 
|  | 1239 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); | 
|  | 1240 | } | 
|  | 1241 | EGLint contextClientVersion = 0; | 
|  | 1242 | if (renderableType & EGL_OPENGL_ES3_BIT) { | 
|  | 1243 | contextClientVersion = 3; | 
|  | 1244 | } else if (renderableType & EGL_OPENGL_ES2_BIT) { | 
|  | 1245 | contextClientVersion = 2; | 
|  | 1246 | } else if (renderableType & EGL_OPENGL_ES_BIT) { | 
|  | 1247 | contextClientVersion = 1; | 
|  | 1248 | } else { | 
|  | 1249 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); | 
|  | 1250 | } | 
|  | 1251 |  | 
|  | 1252 | std::vector<EGLint> contextAttributes; | 
|  | 1253 | contextAttributes.reserve(7); | 
|  | 1254 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); | 
|  | 1255 | contextAttributes.push_back(contextClientVersion); | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 1256 | if (contextPriority) { | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1257 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 1258 | switch (*contextPriority) { | 
|  | 1259 | case ContextPriority::REALTIME: | 
|  | 1260 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV); | 
|  | 1261 | break; | 
|  | 1262 | case ContextPriority::MEDIUM: | 
|  | 1263 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG); | 
|  | 1264 | break; | 
|  | 1265 | case ContextPriority::LOW: | 
|  | 1266 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG); | 
|  | 1267 | break; | 
|  | 1268 | case ContextPriority::HIGH: | 
|  | 1269 | default: | 
|  | 1270 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); | 
|  | 1271 | break; | 
|  | 1272 | } | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1273 | } | 
|  | 1274 | if (protection == Protection::PROTECTED) { | 
|  | 1275 | contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 1276 | contextAttributes.push_back(EGL_TRUE); | 
|  | 1277 | } | 
|  | 1278 | contextAttributes.push_back(EGL_NONE); | 
|  | 1279 |  | 
|  | 1280 | EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 1281 |  | 
|  | 1282 | if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) { | 
|  | 1283 | // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus | 
|  | 1284 | // EGL_NO_CONTEXT so that we can abort. | 
|  | 1285 | if (config != EGL_NO_CONFIG_KHR) { | 
|  | 1286 | return context; | 
|  | 1287 | } | 
|  | 1288 | // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we | 
|  | 1289 | // should try to fall back to GLES 2. | 
|  | 1290 | contextAttributes[1] = 2; | 
|  | 1291 | context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 1292 | } | 
|  | 1293 |  | 
|  | 1294 | return context; | 
|  | 1295 | } | 
|  | 1296 |  | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 1297 | std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority( | 
|  | 1298 | const RenderEngineCreationArgs& args) { | 
|  | 1299 | if (!gl::GLExtensions::getInstance().hasContextPriority()) { | 
|  | 1300 | return std::nullopt; | 
|  | 1301 | } | 
|  | 1302 |  | 
|  | 1303 | switch (args.contextPriority) { | 
|  | 1304 | case RenderEngine::ContextPriority::REALTIME: | 
|  | 1305 | if (gl::GLExtensions::getInstance().hasRealtimePriority()) { | 
|  | 1306 | return RenderEngine::ContextPriority::REALTIME; | 
|  | 1307 | } else { | 
|  | 1308 | ALOGI("Realtime priority unsupported, degrading gracefully to high priority"); | 
|  | 1309 | return RenderEngine::ContextPriority::HIGH; | 
|  | 1310 | } | 
|  | 1311 | case RenderEngine::ContextPriority::HIGH: | 
|  | 1312 | case RenderEngine::ContextPriority::MEDIUM: | 
|  | 1313 | case RenderEngine::ContextPriority::LOW: | 
|  | 1314 | return args.contextPriority; | 
|  | 1315 | default: | 
|  | 1316 | return std::nullopt; | 
|  | 1317 | } | 
|  | 1318 | } | 
|  | 1319 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1320 | EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display, | 
|  | 1321 | EGLConfig config, int hwcFormat, | 
|  | 1322 | Protection protection) { | 
|  | 1323 | EGLConfig placeholderConfig = config; | 
|  | 1324 | if (placeholderConfig == EGL_NO_CONFIG_KHR) { | 
|  | 1325 | placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); | 
|  | 1326 | } | 
|  | 1327 | std::vector<EGLint> attributes; | 
|  | 1328 | attributes.reserve(7); | 
|  | 1329 | attributes.push_back(EGL_WIDTH); | 
|  | 1330 | attributes.push_back(1); | 
|  | 1331 | attributes.push_back(EGL_HEIGHT); | 
|  | 1332 | attributes.push_back(1); | 
|  | 1333 | if (protection == Protection::PROTECTED) { | 
|  | 1334 | attributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 1335 | attributes.push_back(EGL_TRUE); | 
|  | 1336 | } | 
|  | 1337 | attributes.push_back(EGL_NONE); | 
|  | 1338 |  | 
|  | 1339 | return eglCreatePbufferSurface(display, placeholderConfig, attributes.data()); | 
|  | 1340 | } | 
|  | 1341 |  | 
| Alec Mouri | d6f0946 | 2020-12-07 11:18:17 -0800 | [diff] [blame] | 1342 | int SkiaGLRenderEngine::getContextPriority() { | 
|  | 1343 | int value; | 
|  | 1344 | eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value); | 
|  | 1345 | return value; | 
|  | 1346 | } | 
|  | 1347 |  | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 1348 | void SkiaGLRenderEngine::onPrimaryDisplaySizeChanged(ui::Size size) { | 
|  | 1349 | // This cache multiplier was selected based on review of cache sizes relative | 
|  | 1350 | // to the screen resolution. Looking at the worst case memory needed by blur (~1.5x), | 
|  | 1351 | // shadows (~1x), and general data structures (e.g. vertex buffers) we selected this as a | 
|  | 1352 | // conservative default based on that analysis. | 
|  | 1353 | const float SURFACE_SIZE_MULTIPLIER = 3.5f * bytesPerPixel(mDefaultPixelFormat); | 
|  | 1354 | const int maxResourceBytes = size.width * size.height * SURFACE_SIZE_MULTIPLIER; | 
|  | 1355 |  | 
|  | 1356 | // start by resizing the current context | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 1357 | getActiveGrContext()->setResourceCacheLimit(maxResourceBytes); | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 1358 |  | 
|  | 1359 | // if it is possible to switch contexts then we will resize the other context | 
|  | 1360 | if (useProtectedContext(!mInProtectedContext)) { | 
| Derek Sollenberger | b24258c | 2021-05-04 13:47:34 -0400 | [diff] [blame] | 1361 | getActiveGrContext()->setResourceCacheLimit(maxResourceBytes); | 
| Derek Sollenberger | c4a05e1 | 2021-03-24 16:45:20 -0400 | [diff] [blame] | 1362 | // reset back to the initial context that was active when this method was called | 
|  | 1363 | useProtectedContext(!mInProtectedContext); | 
|  | 1364 | } | 
|  | 1365 | } | 
|  | 1366 |  | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 1367 | void SkiaGLRenderEngine::dump(std::string& result) { | 
|  | 1368 | const gl::GLExtensions& extensions = gl::GLExtensions::getInstance(); | 
|  | 1369 |  | 
|  | 1370 | StringAppendF(&result, "\n ------------RE-----------------\n"); | 
|  | 1371 | StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion()); | 
|  | 1372 | StringAppendF(&result, "%s\n", extensions.getEGLExtensions()); | 
|  | 1373 | StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(), | 
|  | 1374 | extensions.getVersion()); | 
|  | 1375 | StringAppendF(&result, "%s\n", extensions.getExtensions()); | 
|  | 1376 | StringAppendF(&result, "RenderEngine supports protected context: %d\n", | 
|  | 1377 | supportsProtectedContent()); | 
|  | 1378 | StringAppendF(&result, "RenderEngine is in protected context: %d\n", mInProtectedContext); | 
| Leon Scroggins III | 9f3072c | 2021-03-22 10:42:47 -0400 | [diff] [blame] | 1379 | StringAppendF(&result, "RenderEngine shaders cached since last dump/primeCache: %d\n", | 
|  | 1380 | mSkSLCacheMonitor.shadersCachedSinceLastCall()); | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 1381 |  | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 1382 | std::vector<ResourcePair> cpuResourceMap = { | 
|  | 1383 | {"skia/sk_resource_cache/bitmap_", "Bitmaps"}, | 
|  | 1384 | {"skia/sk_resource_cache/rrect-blur_", "Masks"}, | 
|  | 1385 | {"skia/sk_resource_cache/rects-blur_", "Masks"}, | 
|  | 1386 | {"skia/sk_resource_cache/tessellated", "Shadows"}, | 
|  | 1387 | {"skia", "Other"}, | 
|  | 1388 | }; | 
|  | 1389 | SkiaMemoryReporter cpuReporter(cpuResourceMap, false); | 
|  | 1390 | SkGraphics::DumpMemoryStatistics(&cpuReporter); | 
|  | 1391 | StringAppendF(&result, "Skia CPU Caches: "); | 
|  | 1392 | cpuReporter.logTotals(result); | 
|  | 1393 | cpuReporter.logOutput(result); | 
|  | 1394 |  | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 1395 | { | 
|  | 1396 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 1397 |  | 
|  | 1398 | std::vector<ResourcePair> gpuResourceMap = { | 
|  | 1399 | {"texture_renderbuffer", "Texture/RenderBuffer"}, | 
|  | 1400 | {"texture", "Texture"}, | 
|  | 1401 | {"gr_text_blob_cache", "Text"}, | 
|  | 1402 | {"skia", "Other"}, | 
|  | 1403 | }; | 
|  | 1404 | SkiaMemoryReporter gpuReporter(gpuResourceMap, true); | 
|  | 1405 | mGrContext->dumpMemoryStatistics(&gpuReporter); | 
|  | 1406 | StringAppendF(&result, "Skia's GPU Caches: "); | 
|  | 1407 | gpuReporter.logTotals(result); | 
|  | 1408 | gpuReporter.logOutput(result); | 
|  | 1409 | StringAppendF(&result, "Skia's Wrapped Objects:\n"); | 
|  | 1410 | gpuReporter.logOutput(result, true); | 
|  | 1411 |  | 
| Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 1412 | StringAppendF(&result, "RenderEngine tracked buffers: %zu\n", | 
|  | 1413 | mGraphicBufferExternalRefs.size()); | 
|  | 1414 | StringAppendF(&result, "Dumping buffer ids...\n"); | 
|  | 1415 | for (const auto& [id, refCounts] : mGraphicBufferExternalRefs) { | 
|  | 1416 | StringAppendF(&result, "- 0x%" PRIx64 " - %d refs \n", id, refCounts); | 
|  | 1417 | } | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 1418 | StringAppendF(&result, "RenderEngine AHB/BackendTexture cache size: %zu\n", | 
|  | 1419 | mTextureCache.size()); | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 1420 | StringAppendF(&result, "Dumping buffer ids...\n"); | 
|  | 1421 | // TODO(178539829): It would be nice to know which layer these are coming from and what | 
|  | 1422 | // the texture sizes are. | 
|  | 1423 | for (const auto& [id, unused] : mTextureCache) { | 
|  | 1424 | StringAppendF(&result, "- 0x%" PRIx64 "\n", id); | 
|  | 1425 | } | 
|  | 1426 | StringAppendF(&result, "\n"); | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 1427 |  | 
|  | 1428 | SkiaMemoryReporter gpuProtectedReporter(gpuResourceMap, true); | 
| Derek Sollenberger | 80a7a76 | 2021-04-14 10:22:58 -0400 | [diff] [blame] | 1429 | if (mProtectedGrContext) { | 
|  | 1430 | mProtectedGrContext->dumpMemoryStatistics(&gpuProtectedReporter); | 
|  | 1431 | } | 
| Derek Sollenberger | 0e6d356 | 2021-04-07 19:34:39 -0400 | [diff] [blame] | 1432 | StringAppendF(&result, "Skia's GPU Protected Caches: "); | 
|  | 1433 | gpuProtectedReporter.logTotals(result); | 
|  | 1434 | gpuProtectedReporter.logOutput(result); | 
|  | 1435 | StringAppendF(&result, "Skia's Protected Wrapped Objects:\n"); | 
|  | 1436 | gpuProtectedReporter.logOutput(result, true); | 
|  | 1437 |  | 
|  | 1438 | StringAppendF(&result, "RenderEngine protected AHB/BackendTexture cache size: %zu\n", | 
| Ana Krulec | 1d12b3b | 2021-01-27 16:49:51 -0800 | [diff] [blame] | 1439 | mProtectedTextureCache.size()); | 
|  | 1440 | StringAppendF(&result, "Dumping buffer ids...\n"); | 
|  | 1441 | for (const auto& [id, unused] : mProtectedTextureCache) { | 
|  | 1442 | StringAppendF(&result, "- 0x%" PRIx64 "\n", id); | 
|  | 1443 | } | 
|  | 1444 | StringAppendF(&result, "\n"); | 
|  | 1445 | StringAppendF(&result, "RenderEngine runtime effects: %zu\n", mRuntimeEffects.size()); | 
|  | 1446 | for (const auto& [linearEffect, unused] : mRuntimeEffects) { | 
|  | 1447 | StringAppendF(&result, "- inputDataspace: %s\n", | 
|  | 1448 | dataspaceDetails( | 
|  | 1449 | static_cast<android_dataspace>(linearEffect.inputDataspace)) | 
|  | 1450 | .c_str()); | 
|  | 1451 | StringAppendF(&result, "- outputDataspace: %s\n", | 
|  | 1452 | dataspaceDetails( | 
|  | 1453 | static_cast<android_dataspace>(linearEffect.outputDataspace)) | 
|  | 1454 | .c_str()); | 
|  | 1455 | StringAppendF(&result, "undoPremultipliedAlpha: %s\n", | 
|  | 1456 | linearEffect.undoPremultipliedAlpha ? "true" : "false"); | 
|  | 1457 | } | 
|  | 1458 | } | 
|  | 1459 | StringAppendF(&result, "\n"); | 
|  | 1460 | } | 
|  | 1461 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 1462 | } // namespace skia | 
|  | 1463 | } // namespace renderengine | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 1464 | } // namespace android |