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