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