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