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