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