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