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