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