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