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 |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 18 | #include <cstdint> |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 19 | #include <memory> |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 20 | |
| 21 | #include "SkImageInfo.h" |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 22 | #include "log/log_main.h" |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 23 | #include "system/graphics-base-v1.0.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 24 | #undef LOG_TAG |
| 25 | #define LOG_TAG "RenderEngine" |
| 26 | #define ATRACE_TAG ATRACE_TAG_GRAPHICS |
| 27 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 28 | #include <EGL/egl.h> |
| 29 | #include <EGL/eglext.h> |
| 30 | #include <GLES2/gl2.h> |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 31 | #include <sync/sync.h> |
| 32 | #include <ui/BlurRegion.h> |
| 33 | #include <ui/GraphicBuffer.h> |
| 34 | #include <utils/Trace.h> |
| 35 | #include "../gl/GLExtensions.h" |
| 36 | #include "SkiaGLRenderEngine.h" |
| 37 | #include "filters/BlurFilter.h" |
| 38 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 39 | #include <GrContextOptions.h> |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 40 | #include <SkCanvas.h> |
Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 41 | #include <SkColorFilter.h> |
| 42 | #include <SkColorMatrix.h> |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 43 | #include <SkColorSpace.h> |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 44 | #include <SkImage.h> |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 45 | #include <SkImageFilters.h> |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 46 | #include <SkShadowUtils.h> |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 47 | #include <SkSurface.h> |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 48 | #include <gl/GrGLInterface.h> |
| 49 | #include <sync/sync.h> |
| 50 | #include <ui/GraphicBuffer.h> |
| 51 | #include <utils/Trace.h> |
| 52 | |
| 53 | #include <cmath> |
| 54 | |
| 55 | #include "../gl/GLExtensions.h" |
Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 56 | #include "SkiaGLRenderEngine.h" |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 57 | #include "filters/BlurFilter.h" |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 58 | #include "filters/LinearEffect.h" |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 59 | |
| 60 | extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name); |
| 61 | |
| 62 | bool checkGlError(const char* op, int lineNumber); |
| 63 | |
| 64 | namespace android { |
| 65 | namespace renderengine { |
| 66 | namespace skia { |
| 67 | |
| 68 | static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute, |
| 69 | EGLint wanted, EGLConfig* outConfig) { |
| 70 | EGLint numConfigs = -1, n = 0; |
| 71 | eglGetConfigs(dpy, nullptr, 0, &numConfigs); |
| 72 | std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR); |
| 73 | eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n); |
| 74 | configs.resize(n); |
| 75 | |
| 76 | if (!configs.empty()) { |
| 77 | if (attribute != EGL_NONE) { |
| 78 | for (EGLConfig config : configs) { |
| 79 | EGLint value = 0; |
| 80 | eglGetConfigAttrib(dpy, config, attribute, &value); |
| 81 | if (wanted == value) { |
| 82 | *outConfig = config; |
| 83 | return NO_ERROR; |
| 84 | } |
| 85 | } |
| 86 | } else { |
| 87 | // just pick the first one |
| 88 | *outConfig = configs[0]; |
| 89 | return NO_ERROR; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | return NAME_NOT_FOUND; |
| 94 | } |
| 95 | |
| 96 | static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType, |
| 97 | EGLConfig* config) { |
| 98 | // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if |
| 99 | // it is to be used with WIFI displays |
| 100 | status_t err; |
| 101 | EGLint wantedAttribute; |
| 102 | EGLint wantedAttributeValue; |
| 103 | |
| 104 | std::vector<EGLint> attribs; |
| 105 | if (renderableType) { |
| 106 | const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format); |
| 107 | const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102; |
| 108 | |
| 109 | // Default to 8 bits per channel. |
| 110 | const EGLint tmpAttribs[] = { |
| 111 | EGL_RENDERABLE_TYPE, |
| 112 | renderableType, |
| 113 | EGL_RECORDABLE_ANDROID, |
| 114 | EGL_TRUE, |
| 115 | EGL_SURFACE_TYPE, |
| 116 | EGL_WINDOW_BIT | EGL_PBUFFER_BIT, |
| 117 | EGL_FRAMEBUFFER_TARGET_ANDROID, |
| 118 | EGL_TRUE, |
| 119 | EGL_RED_SIZE, |
| 120 | is1010102 ? 10 : 8, |
| 121 | EGL_GREEN_SIZE, |
| 122 | is1010102 ? 10 : 8, |
| 123 | EGL_BLUE_SIZE, |
| 124 | is1010102 ? 10 : 8, |
| 125 | EGL_ALPHA_SIZE, |
| 126 | is1010102 ? 2 : 8, |
| 127 | EGL_NONE, |
| 128 | }; |
| 129 | std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)), |
| 130 | std::back_inserter(attribs)); |
| 131 | wantedAttribute = EGL_NONE; |
| 132 | wantedAttributeValue = EGL_NONE; |
| 133 | } else { |
| 134 | // if no renderable type specified, fallback to a simplified query |
| 135 | wantedAttribute = EGL_NATIVE_VISUAL_ID; |
| 136 | wantedAttributeValue = format; |
| 137 | } |
| 138 | |
| 139 | err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue, |
| 140 | config); |
| 141 | if (err == NO_ERROR) { |
| 142 | EGLint caveat; |
| 143 | if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat)) |
| 144 | ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!"); |
| 145 | } |
| 146 | |
| 147 | return err; |
| 148 | } |
| 149 | |
| 150 | std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create( |
| 151 | const RenderEngineCreationArgs& args) { |
| 152 | // initialize EGL for the default display |
| 153 | EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 154 | if (!eglInitialize(display, nullptr, nullptr)) { |
| 155 | LOG_ALWAYS_FATAL("failed to initialize EGL"); |
| 156 | } |
| 157 | |
| 158 | const auto eglVersion = eglQueryStringImplementationANDROID(display, EGL_VERSION); |
| 159 | if (!eglVersion) { |
| 160 | checkGlError(__FUNCTION__, __LINE__); |
| 161 | LOG_ALWAYS_FATAL("eglQueryStringImplementationANDROID(EGL_VERSION) failed"); |
| 162 | } |
| 163 | |
| 164 | const auto eglExtensions = eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS); |
| 165 | if (!eglExtensions) { |
| 166 | checkGlError(__FUNCTION__, __LINE__); |
| 167 | LOG_ALWAYS_FATAL("eglQueryStringImplementationANDROID(EGL_EXTENSIONS) failed"); |
| 168 | } |
| 169 | |
| 170 | auto& extensions = gl::GLExtensions::getInstance(); |
| 171 | extensions.initWithEGLStrings(eglVersion, eglExtensions); |
| 172 | |
| 173 | // The code assumes that ES2 or later is available if this extension is |
| 174 | // supported. |
| 175 | EGLConfig config = EGL_NO_CONFIG_KHR; |
| 176 | if (!extensions.hasNoConfigContext()) { |
| 177 | config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true); |
| 178 | } |
| 179 | |
| 180 | bool useContextPriority = |
| 181 | extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH; |
| 182 | EGLContext protectedContext = EGL_NO_CONTEXT; |
| 183 | if (args.enableProtectedContext && extensions.hasProtectedContent()) { |
| 184 | protectedContext = createEglContext(display, config, nullptr, useContextPriority, |
| 185 | Protection::PROTECTED); |
| 186 | ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context"); |
| 187 | } |
| 188 | |
| 189 | EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority, |
| 190 | Protection::UNPROTECTED); |
| 191 | |
| 192 | // if can't create a GL context, we can only abort. |
| 193 | LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed"); |
| 194 | |
| 195 | EGLSurface placeholder = EGL_NO_SURFACE; |
| 196 | if (!extensions.hasSurfacelessContext()) { |
| 197 | placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, |
| 198 | Protection::UNPROTECTED); |
| 199 | LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer"); |
| 200 | } |
| 201 | EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt); |
| 202 | LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current"); |
| 203 | extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER), |
| 204 | glGetString(GL_VERSION), glGetString(GL_EXTENSIONS)); |
| 205 | |
| 206 | EGLSurface protectedPlaceholder = EGL_NO_SURFACE; |
| 207 | if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) { |
| 208 | protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat, |
| 209 | Protection::PROTECTED); |
| 210 | ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE, |
| 211 | "can't create protected placeholder pbuffer"); |
| 212 | } |
| 213 | |
| 214 | // initialize the renderer while GL is current |
| 215 | std::unique_ptr<SkiaGLRenderEngine> engine = |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 216 | std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext, |
| 217 | protectedPlaceholder); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 218 | |
| 219 | ALOGI("OpenGL ES informations:"); |
| 220 | ALOGI("vendor : %s", extensions.getVendor()); |
| 221 | ALOGI("renderer : %s", extensions.getRenderer()); |
| 222 | ALOGI("version : %s", extensions.getVersion()); |
| 223 | ALOGI("extensions: %s", extensions.getExtensions()); |
| 224 | ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize()); |
| 225 | ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims()); |
| 226 | |
| 227 | return engine; |
| 228 | } |
| 229 | |
| 230 | EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) { |
| 231 | status_t err; |
| 232 | EGLConfig config; |
| 233 | |
| 234 | // First try to get an ES3 config |
| 235 | err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config); |
| 236 | if (err != NO_ERROR) { |
| 237 | // If ES3 fails, try to get an ES2 config |
| 238 | err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config); |
| 239 | if (err != NO_ERROR) { |
| 240 | // If ES2 still doesn't work, probably because we're on the emulator. |
| 241 | // try a simplified query |
| 242 | ALOGW("no suitable EGLConfig found, trying a simpler query"); |
| 243 | err = selectEGLConfig(display, format, 0, &config); |
| 244 | if (err != NO_ERROR) { |
| 245 | // this EGL is too lame for android |
| 246 | LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up"); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | if (logConfig) { |
| 252 | // print some debugging info |
| 253 | EGLint r, g, b, a; |
| 254 | eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r); |
| 255 | eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g); |
| 256 | eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b); |
| 257 | eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a); |
| 258 | ALOGI("EGL information:"); |
| 259 | ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR)); |
| 260 | ALOGI("version : %s", eglQueryString(display, EGL_VERSION)); |
| 261 | ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS)); |
| 262 | ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported"); |
| 263 | ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config); |
| 264 | } |
| 265 | |
| 266 | return config; |
| 267 | } |
| 268 | |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 269 | SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display, |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 270 | EGLContext ctxt, EGLSurface placeholder, |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 271 | EGLContext protectedContext, EGLSurface protectedPlaceholder) |
Alec Mouri | 081be4c | 2020-09-16 10:24:47 -0700 | [diff] [blame] | 272 | : mEGLDisplay(display), |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 273 | mEGLContext(ctxt), |
| 274 | mPlaceholderSurface(placeholder), |
| 275 | mProtectedEGLContext(protectedContext), |
Alec Mouri | b577745 | 2020-09-28 11:32:42 -0700 | [diff] [blame] | 276 | mProtectedPlaceholderSurface(protectedPlaceholder), |
| 277 | mUseColorManagement(args.useColorManagement) { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 278 | sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface()); |
| 279 | LOG_ALWAYS_FATAL_IF(!glInterface.get()); |
| 280 | |
| 281 | GrContextOptions options; |
| 282 | options.fPreferExternalImagesOverES3 = true; |
| 283 | options.fDisableDistanceFieldPaths = true; |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 284 | mGrContext = GrDirectContext::MakeGL(glInterface, options); |
| 285 | if (useProtectedContext(true)) { |
| 286 | mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options); |
| 287 | useProtectedContext(false); |
| 288 | } |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 289 | |
| 290 | if (args.supportsBackgroundBlur) { |
| 291 | mBlurFilter = new BlurFilter(); |
| 292 | } |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 293 | } |
| 294 | |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 295 | bool SkiaGLRenderEngine::supportsProtectedContent() const { |
| 296 | return mProtectedEGLContext != EGL_NO_CONTEXT; |
| 297 | } |
| 298 | |
| 299 | bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) { |
| 300 | if (useProtectedContext == mInProtectedContext) { |
| 301 | return true; |
| 302 | } |
| 303 | if (useProtectedContext && supportsProtectedContent()) { |
| 304 | return false; |
| 305 | } |
| 306 | const EGLSurface surface = |
| 307 | useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface; |
| 308 | const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext; |
| 309 | const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE; |
| 310 | if (success) { |
| 311 | mInProtectedContext = useProtectedContext; |
| 312 | } |
| 313 | return success; |
| 314 | } |
| 315 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 316 | base::unique_fd SkiaGLRenderEngine::flush() { |
| 317 | ATRACE_CALL(); |
| 318 | if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) { |
| 319 | return base::unique_fd(); |
| 320 | } |
| 321 | |
| 322 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr); |
| 323 | if (sync == EGL_NO_SYNC_KHR) { |
| 324 | ALOGW("failed to create EGL native fence sync: %#x", eglGetError()); |
| 325 | return base::unique_fd(); |
| 326 | } |
| 327 | |
| 328 | // native fence fd will not be populated until flush() is done. |
| 329 | glFlush(); |
| 330 | |
| 331 | // get the fence fd |
| 332 | base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync)); |
| 333 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 334 | if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) { |
| 335 | ALOGW("failed to dup EGL native fence sync: %#x", eglGetError()); |
| 336 | } |
| 337 | |
| 338 | return fenceFd; |
| 339 | } |
| 340 | |
| 341 | bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) { |
| 342 | if (!gl::GLExtensions::getInstance().hasNativeFenceSync() || |
| 343 | !gl::GLExtensions::getInstance().hasWaitSync()) { |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | // release the fd and transfer the ownership to EGLSync |
| 348 | EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE}; |
| 349 | EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs); |
| 350 | if (sync == EGL_NO_SYNC_KHR) { |
| 351 | ALOGE("failed to create EGL native fence sync: %#x", eglGetError()); |
| 352 | return false; |
| 353 | } |
| 354 | |
| 355 | // XXX: The spec draft is inconsistent as to whether this should return an |
| 356 | // EGLint or void. Ignore the return value for now, as it's not strictly |
| 357 | // needed. |
| 358 | eglWaitSyncKHR(mEGLDisplay, sync, 0); |
| 359 | EGLint error = eglGetError(); |
| 360 | eglDestroySyncKHR(mEGLDisplay, sync); |
| 361 | if (error != EGL_SUCCESS) { |
| 362 | ALOGE("failed to wait for EGL native fence sync: %#x", error); |
| 363 | return false; |
| 364 | } |
| 365 | |
| 366 | return true; |
| 367 | } |
| 368 | |
| 369 | static bool hasUsage(const AHardwareBuffer_Desc& desc, uint64_t usage) { |
| 370 | return !!(desc.usage & usage); |
| 371 | } |
| 372 | |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 373 | static float toDegrees(uint32_t transform) { |
| 374 | switch (transform) { |
| 375 | case ui::Transform::ROT_90: |
| 376 | return 90.0; |
| 377 | case ui::Transform::ROT_180: |
| 378 | return 180.0; |
| 379 | case ui::Transform::ROT_270: |
| 380 | return 270.0; |
| 381 | default: |
| 382 | return 0.0; |
| 383 | } |
| 384 | } |
| 385 | |
Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 386 | static SkColorMatrix toSkColorMatrix(const mat4& matrix) { |
| 387 | return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1], |
| 388 | matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2], |
| 389 | matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3], |
| 390 | matrix[3][3], 0); |
| 391 | } |
| 392 | |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 393 | static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) { |
| 394 | int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 395 | int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK; |
| 396 | |
| 397 | // Treat unsupported dataspaces as srgb |
| 398 | if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR && |
| 399 | destTransfer != HAL_DATASPACE_TRANSFER_HLG && |
| 400 | destTransfer != HAL_DATASPACE_TRANSFER_ST2084) { |
| 401 | destTransfer = HAL_DATASPACE_TRANSFER_SRGB; |
| 402 | } |
| 403 | |
| 404 | if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR && |
| 405 | sourceTransfer != HAL_DATASPACE_TRANSFER_HLG && |
| 406 | sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) { |
| 407 | sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB; |
| 408 | } |
| 409 | |
| 410 | const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR; |
| 411 | const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB; |
| 412 | const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR; |
| 413 | const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB; |
| 414 | |
| 415 | return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) && |
| 416 | sourceTransfer != destTransfer; |
| 417 | } |
| 418 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 419 | void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) { |
| 420 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 421 | mTextureCache.erase(bufferId); |
| 422 | mProtectedTextureCache.erase(bufferId); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display, |
| 426 | const std::vector<const LayerSettings*>& layers, |
| 427 | const sp<GraphicBuffer>& buffer, |
| 428 | const bool useFramebufferCache, |
| 429 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) { |
| 430 | ATRACE_NAME("SkiaGL::drawLayers"); |
| 431 | std::lock_guard<std::mutex> lock(mRenderingMutex); |
| 432 | if (layers.empty()) { |
| 433 | ALOGV("Drawing empty layer stack"); |
| 434 | return NO_ERROR; |
| 435 | } |
| 436 | |
| 437 | if (bufferFence.get() >= 0) { |
| 438 | // Duplicate the fence for passing to waitFence. |
| 439 | base::unique_fd bufferFenceDup(dup(bufferFence.get())); |
| 440 | if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) { |
| 441 | ATRACE_NAME("Waiting before draw"); |
| 442 | sync_wait(bufferFence.get(), -1); |
| 443 | } |
| 444 | } |
| 445 | if (buffer == nullptr) { |
| 446 | ALOGE("No output buffer provided. Aborting GPU composition."); |
| 447 | return BAD_VALUE; |
| 448 | } |
| 449 | |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 450 | auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext; |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 451 | auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 452 | AHardwareBuffer_Desc bufferDesc; |
| 453 | AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 454 | LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE), |
| 455 | "missing usage"); |
| 456 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 457 | std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef = nullptr; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 458 | if (useFramebufferCache) { |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 459 | auto iter = cache.find(buffer->getId()); |
| 460 | if (iter != cache.end()) { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 461 | ALOGV("Cache hit!"); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 462 | surfaceTextureRef = iter->second; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 465 | |
| 466 | if (surfaceTextureRef == nullptr || surfaceTextureRef->getTexture() == nullptr) { |
| 467 | surfaceTextureRef = std::make_shared<AutoBackendTexture::LocalRef>(); |
| 468 | surfaceTextureRef->setTexture( |
| 469 | new AutoBackendTexture(mGrContext.get(), buffer->toAHardwareBuffer(), true)); |
| 470 | if (useFramebufferCache) { |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 471 | ALOGD("Adding to cache"); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 472 | cache.insert({buffer->getId(), surfaceTextureRef}); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 473 | } |
| 474 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 475 | |
| 476 | sk_sp<SkSurface> surface = |
| 477 | surfaceTextureRef->getTexture()->getOrCreateSurface(mUseColorManagement |
| 478 | ? display.outputDataspace |
| 479 | : ui::Dataspace::SRGB, |
| 480 | mGrContext.get()); |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 481 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 482 | auto canvas = surface->getCanvas(); |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 483 | // Clear the entire canvas with a transparent black to prevent ghost images. |
| 484 | canvas->clear(SK_ColorTRANSPARENT); |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 485 | canvas->save(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 486 | |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 487 | // Before doing any drawing, let's make sure that we'll start at the origin of the display. |
| 488 | // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual |
| 489 | // displays might have different scaling when compared to the physical screen. |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 490 | |
| 491 | canvas->clipRect(getSkRect(display.physicalDisplay)); |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 492 | SkMatrix screenTransform; |
| 493 | screenTransform.setTranslate(display.physicalDisplay.left, display.physicalDisplay.top); |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 494 | |
| 495 | const auto clipWidth = display.clip.width(); |
| 496 | const auto clipHeight = display.clip.height(); |
| 497 | auto rotatedClipWidth = clipWidth; |
| 498 | auto rotatedClipHeight = clipHeight; |
| 499 | // Scale is contingent on the rotation result. |
| 500 | if (display.orientation & ui::Transform::ROT_90) { |
| 501 | std::swap(rotatedClipWidth, rotatedClipHeight); |
| 502 | } |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 503 | const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) / |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 504 | static_cast<SkScalar>(rotatedClipWidth); |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 505 | const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) / |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 506 | static_cast<SkScalar>(rotatedClipHeight); |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 507 | screenTransform.preScale(scaleX, scaleY); |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 508 | |
| 509 | // Canvas rotation is done by centering the clip window at the origin, rotating, translating |
| 510 | // back so that the top left corner of the clip is at (0, 0). |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 511 | screenTransform.preTranslate(rotatedClipWidth / 2, rotatedClipHeight / 2); |
| 512 | screenTransform.preRotate(toDegrees(display.orientation)); |
| 513 | screenTransform.preTranslate(-clipWidth / 2, -clipHeight / 2); |
| 514 | screenTransform.preTranslate(-display.clip.left, -display.clip.top); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 515 | for (const auto& layer : layers) { |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 516 | const SkMatrix drawTransform = getDrawTransform(layer, screenTransform); |
| 517 | |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 518 | SkPaint paint; |
| 519 | const auto& bounds = layer->geometry.boundaries; |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 520 | const auto dest = getSkRect(bounds); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 521 | std::unordered_map<uint32_t, sk_sp<SkSurface>> cachedBlurs; |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 522 | |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 523 | if (mBlurFilter) { |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 524 | const auto layerRect = drawTransform.mapRect(dest); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 525 | if (layer->backgroundBlurRadius > 0) { |
| 526 | ATRACE_NAME("BackgroundBlur"); |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 527 | auto blurredSurface = mBlurFilter->generate(canvas, surface, |
| 528 | layer->backgroundBlurRadius, layerRect); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 529 | cachedBlurs[layer->backgroundBlurRadius] = blurredSurface; |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 530 | |
| 531 | drawBlurRegion(canvas, getBlurRegion(layer), drawTransform, blurredSurface); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 532 | } |
| 533 | if (layer->blurRegions.size() > 0) { |
| 534 | for (auto region : layer->blurRegions) { |
| 535 | if (cachedBlurs[region.blurRadius]) { |
| 536 | continue; |
| 537 | } |
| 538 | ATRACE_NAME("BlurRegion"); |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 539 | auto blurredSurface = |
| 540 | mBlurFilter->generate(canvas, surface, region.blurRadius, layerRect); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 541 | cachedBlurs[region.blurRadius] = blurredSurface; |
| 542 | } |
| 543 | } |
Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 544 | } |
| 545 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 546 | if (layer->source.buffer.buffer) { |
| 547 | ATRACE_NAME("DrawImage"); |
| 548 | const auto& item = layer->source.buffer; |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 549 | const auto bufferWidth = item.buffer->getBounds().width(); |
| 550 | const auto bufferHeight = item.buffer->getBounds().height(); |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 551 | std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr; |
| 552 | auto iter = mTextureCache.find(item.buffer->getId()); |
| 553 | if (iter != mTextureCache.end()) { |
| 554 | imageTextureRef = iter->second; |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 555 | } else { |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 556 | imageTextureRef = std::make_shared<AutoBackendTexture::LocalRef>(); |
| 557 | imageTextureRef->setTexture(new AutoBackendTexture(mGrContext.get(), |
| 558 | item.buffer->toAHardwareBuffer(), |
| 559 | false)); |
| 560 | mTextureCache.insert({buffer->getId(), imageTextureRef}); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 561 | } |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 562 | sk_sp<SkImage> image = |
| 563 | imageTextureRef->getTexture() |
| 564 | ->makeImage(mUseColorManagement |
| 565 | ? (needsToneMapping(layer->sourceDataspace, |
| 566 | display.outputDataspace) |
| 567 | // If we need to map to linear space, |
| 568 | // then mark the source image with the |
| 569 | // same colorspace as the destination |
| 570 | // surface so that Skia's color |
| 571 | // management is a no-op. |
| 572 | ? display.outputDataspace |
| 573 | : layer->sourceDataspace) |
| 574 | : ui::Dataspace::SRGB, |
| 575 | item.isOpaque ? kOpaque_SkAlphaType |
| 576 | : (item.usePremultipliedAlpha |
| 577 | ? kPremul_SkAlphaType |
| 578 | : kUnpremul_SkAlphaType), |
| 579 | mGrContext.get()); |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 580 | SkMatrix matrix; |
| 581 | if (layer->geometry.roundedCornersRadius > 0) { |
| 582 | const auto roundedRect = getRoundedRect(layer); |
| 583 | matrix.setTranslate(roundedRect.getBounds().left() - dest.left(), |
| 584 | roundedRect.getBounds().top() - dest.top()); |
| 585 | } else { |
| 586 | matrix.setIdentity(); |
| 587 | } |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 588 | |
| 589 | auto texMatrix = getSkM44(item.textureTransform).asM33(); |
Huihong Luo | 42b86c1 | 2020-10-29 13:00:13 -0700 | [diff] [blame] | 590 | |
| 591 | // b/171404534, scale to fix the layer |
| 592 | matrix.postScale(bounds.getWidth() / bufferWidth, bounds.getHeight() / bufferHeight); |
| 593 | |
Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 594 | // textureTansform was intended to be passed directly into a shader, so when |
| 595 | // building the total matrix with the textureTransform we need to first |
| 596 | // normalize it, then apply the textureTransform, then scale back up. |
| 597 | matrix.postScale(1.0f / bufferWidth, 1.0f / bufferHeight); |
| 598 | |
| 599 | auto rotatedBufferWidth = bufferWidth; |
| 600 | auto rotatedBufferHeight = bufferHeight; |
| 601 | |
| 602 | // Swap the buffer width and height if we're rotating, so that we |
| 603 | // scale back up by the correct factors post-rotation. |
| 604 | if (texMatrix.getSkewX() <= -0.5f || texMatrix.getSkewX() >= 0.5f) { |
| 605 | std::swap(rotatedBufferWidth, rotatedBufferHeight); |
| 606 | // TODO: clean this up. |
| 607 | // GLESRenderEngine specifies its texture coordinates in |
| 608 | // CW orientation under OpenGL conventions, when they probably should have |
| 609 | // been CCW instead. The net result is that orientation |
| 610 | // transforms are applied in the reverse |
| 611 | // direction to render the correct result, because SurfaceFlinger uses the inverse |
| 612 | // of the display transform to correct for that. But this means that |
| 613 | // the tex transform passed by SkiaGLRenderEngine will rotate |
| 614 | // individual layers in the reverse orientation. Hack around it |
| 615 | // by injected a 180 degree rotation, but ultimately this is |
| 616 | // a bug in how SurfaceFlinger invokes the RenderEngine |
| 617 | // interface, so the proper fix should live there, and GLESRenderEngine |
| 618 | // should be fixed accordingly. |
| 619 | matrix.postRotate(180, 0.5, 0.5); |
| 620 | } |
| 621 | |
| 622 | matrix.postConcat(texMatrix); |
| 623 | matrix.postScale(rotatedBufferWidth, rotatedBufferHeight); |
Ana Krulec | b7b28b2 | 2020-11-23 14:48:58 -0800 | [diff] [blame] | 624 | sk_sp<SkShader> shader; |
| 625 | |
| 626 | if (layer->source.buffer.useTextureFiltering) { |
| 627 | shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, |
| 628 | SkSamplingOptions( |
| 629 | {SkFilterMode::kLinear, SkMipmapMode::kNone}), |
| 630 | &matrix); |
| 631 | } else { |
| 632 | shader = image->makeShader(matrix); |
| 633 | } |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 634 | |
| 635 | if (mUseColorManagement && |
| 636 | needsToneMapping(layer->sourceDataspace, display.outputDataspace)) { |
| 637 | LinearEffect effect = LinearEffect{.inputDataspace = layer->sourceDataspace, |
| 638 | .outputDataspace = display.outputDataspace, |
| 639 | .undoPremultipliedAlpha = !item.isOpaque && |
| 640 | item.usePremultipliedAlpha}; |
Alec Mouri | c16a77d | 2020-11-13 13:20:11 -0800 | [diff] [blame^] | 641 | |
| 642 | auto effectIter = mRuntimeEffects.find(effect); |
| 643 | sk_sp<SkRuntimeEffect> runtimeEffect = nullptr; |
| 644 | if (effectIter == mRuntimeEffects.end()) { |
| 645 | runtimeEffect = buildRuntimeEffect(effect); |
| 646 | mRuntimeEffects.insert({effect, runtimeEffect}); |
| 647 | } else { |
| 648 | runtimeEffect = effectIter->second; |
| 649 | } |
| 650 | |
Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 651 | paint.setShader(createLinearEffectShader(shader, effect, runtimeEffect, |
| 652 | display.maxLuminance, |
| 653 | layer->source.buffer.maxMasteringLuminance, |
| 654 | layer->source.buffer.maxContentLuminance)); |
| 655 | } else { |
| 656 | paint.setShader(shader); |
| 657 | } |
Ana Krulec | 1768bd2 | 2020-11-23 14:51:31 -0800 | [diff] [blame] | 658 | // Make sure to take into the account the alpha set on the layer. |
| 659 | paint.setAlphaf(layer->alpha); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 660 | } else { |
| 661 | ATRACE_NAME("DrawColor"); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 662 | const auto color = layer->source.solidColor; |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 663 | paint.setShader(SkShaders::Color(SkColor4f{.fR = color.r, |
| 664 | .fG = color.g, |
| 665 | .fB = color.b, |
| 666 | layer->alpha}, |
| 667 | nullptr)); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 668 | } |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 669 | |
Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 670 | paint.setColorFilter(SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform))); |
| 671 | |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 672 | for (const auto effectRegion : layer->blurRegions) { |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 673 | drawBlurRegion(canvas, effectRegion, drawTransform, |
| 674 | cachedBlurs[effectRegion.blurRadius]); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 677 | canvas->save(); |
| 678 | canvas->concat(drawTransform); |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 679 | if (layer->shadow.length > 0) { |
| 680 | const auto rect = layer->geometry.roundedCornersRadius > 0 |
| 681 | ? getSkRect(layer->geometry.roundedCornersCrop) |
| 682 | : dest; |
| 683 | drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow); |
| 684 | } |
| 685 | |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 686 | if (layer->geometry.roundedCornersRadius > 0) { |
| 687 | canvas->drawRRect(getRoundedRect(layer), paint); |
| 688 | } else { |
| 689 | canvas->drawRect(dest, paint); |
| 690 | } |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 691 | |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 692 | canvas->restore(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 693 | } |
| 694 | { |
| 695 | ATRACE_NAME("flush surface"); |
| 696 | surface->flush(); |
| 697 | } |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 698 | canvas->restore(); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 699 | |
| 700 | if (drawFence != nullptr) { |
| 701 | *drawFence = flush(); |
| 702 | } |
| 703 | |
| 704 | // If flush failed or we don't support native fences, we need to force the |
| 705 | // gl command stream to be executed. |
| 706 | bool requireSync = drawFence == nullptr || drawFence->get() < 0; |
| 707 | if (requireSync) { |
| 708 | ATRACE_BEGIN("Submit(sync=true)"); |
| 709 | } else { |
| 710 | ATRACE_BEGIN("Submit(sync=false)"); |
| 711 | } |
Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 712 | bool success = grContext->submit(requireSync); |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 713 | ATRACE_END(); |
| 714 | if (!success) { |
| 715 | ALOGE("Failed to flush RenderEngine commands"); |
| 716 | // Chances are, something illegal happened (either the caller passed |
| 717 | // us bad parameters, or we messed up our shader generation). |
| 718 | return INVALID_OPERATION; |
| 719 | } |
| 720 | |
| 721 | // checkErrors(); |
| 722 | return NO_ERROR; |
| 723 | } |
| 724 | |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 725 | inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) { |
| 726 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); |
| 727 | } |
| 728 | |
| 729 | inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) { |
| 730 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); |
| 731 | } |
| 732 | |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 733 | inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) { |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 734 | const auto rect = getSkRect(layer->geometry.roundedCornersCrop); |
Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 735 | const auto cornerRadius = layer->geometry.roundedCornersRadius; |
| 736 | return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius); |
| 737 | } |
| 738 | |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 739 | inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) { |
| 740 | const auto rect = getSkRect(layer->geometry.boundaries); |
| 741 | const auto cornersRadius = layer->geometry.roundedCornersRadius; |
| 742 | return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius), |
| 743 | .cornerRadiusTL = cornersRadius, |
| 744 | .cornerRadiusTR = cornersRadius, |
| 745 | .cornerRadiusBL = cornersRadius, |
| 746 | .cornerRadiusBR = cornersRadius, |
| 747 | .alpha = 1, |
| 748 | .left = static_cast<int>(rect.fLeft), |
| 749 | .top = static_cast<int>(rect.fTop), |
| 750 | .right = static_cast<int>(rect.fRight), |
| 751 | .bottom = static_cast<int>(rect.fBottom)}; |
| 752 | } |
| 753 | |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 754 | inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) { |
| 755 | return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255); |
| 756 | } |
| 757 | |
Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 758 | inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) { |
| 759 | return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], |
| 760 | matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1], |
| 761 | matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2], |
| 762 | matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]); |
| 763 | } |
| 764 | |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 765 | inline SkMatrix SkiaGLRenderEngine::getDrawTransform(const LayerSettings* layer, |
| 766 | const SkMatrix& screenTransform) { |
| 767 | // Layers have a local transform matrix that should be applied to them. |
| 768 | const auto layerTransform = getSkM44(layer->geometry.positionTransform).asM33(); |
| 769 | return SkMatrix::Concat(screenTransform, layerTransform); |
| 770 | } |
| 771 | |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 772 | inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) { |
| 773 | return SkPoint3::Make(vector.x, vector.y, vector.z); |
| 774 | } |
| 775 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 776 | size_t SkiaGLRenderEngine::getMaxTextureSize() const { |
| 777 | return mGrContext->maxTextureSize(); |
| 778 | } |
| 779 | |
| 780 | size_t SkiaGLRenderEngine::getMaxViewportDims() const { |
| 781 | return mGrContext->maxRenderTargetSize(); |
| 782 | } |
| 783 | |
Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 784 | void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius, |
| 785 | const ShadowSettings& settings) { |
| 786 | ATRACE_CALL(); |
| 787 | const float casterZ = settings.length / 2.0f; |
| 788 | const auto shadowShape = cornerRadius > 0 |
| 789 | ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius)) |
| 790 | : SkPath::Rect(casterRect); |
| 791 | const auto flags = |
| 792 | settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag; |
| 793 | |
| 794 | SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ), |
| 795 | getSkPoint3(settings.lightPos), settings.lightRadius, |
| 796 | getSkColor(settings.ambientColor), getSkColor(settings.spotColor), |
| 797 | flags); |
| 798 | } |
| 799 | |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 800 | void SkiaGLRenderEngine::drawBlurRegion(SkCanvas* canvas, const BlurRegion& effectRegion, |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 801 | const SkMatrix& drawTransform, |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 802 | sk_sp<SkSurface> blurredSurface) { |
| 803 | ATRACE_CALL(); |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 804 | |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 805 | SkPaint paint; |
Lyn Han | 1bdedb3 | 2020-11-23 20:33:57 +0000 | [diff] [blame] | 806 | paint.setAlpha(static_cast<int>(effectRegion.alpha * 255)); |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 807 | const auto matrix = mBlurFilter->getShaderMatrix(); |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 808 | paint.setShader(blurredSurface->makeImageSnapshot()->makeShader(matrix)); |
| 809 | |
Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 810 | auto rect = SkRect::MakeLTRB(effectRegion.left, effectRegion.top, effectRegion.right, |
| 811 | effectRegion.bottom); |
| 812 | drawTransform.mapRect(&rect); |
| 813 | |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 814 | if (effectRegion.cornerRadiusTL > 0 || effectRegion.cornerRadiusTR > 0 || |
| 815 | effectRegion.cornerRadiusBL > 0 || effectRegion.cornerRadiusBR > 0) { |
| 816 | const SkVector radii[4] = |
| 817 | {SkVector::Make(effectRegion.cornerRadiusTL, effectRegion.cornerRadiusTL), |
| 818 | SkVector::Make(effectRegion.cornerRadiusTR, effectRegion.cornerRadiusTR), |
| 819 | SkVector::Make(effectRegion.cornerRadiusBL, effectRegion.cornerRadiusBL), |
| 820 | SkVector::Make(effectRegion.cornerRadiusBR, effectRegion.cornerRadiusBR)}; |
| 821 | SkRRect roundedRect; |
| 822 | roundedRect.setRectRadii(rect, radii); |
| 823 | canvas->drawRRect(roundedRect, paint); |
| 824 | } else { |
| 825 | canvas->drawRect(rect, paint); |
| 826 | } |
| 827 | } |
| 828 | |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 829 | EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config, |
| 830 | EGLContext shareContext, bool useContextPriority, |
| 831 | Protection protection) { |
| 832 | EGLint renderableType = 0; |
| 833 | if (config == EGL_NO_CONFIG_KHR) { |
| 834 | renderableType = EGL_OPENGL_ES3_BIT; |
| 835 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { |
| 836 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); |
| 837 | } |
| 838 | EGLint contextClientVersion = 0; |
| 839 | if (renderableType & EGL_OPENGL_ES3_BIT) { |
| 840 | contextClientVersion = 3; |
| 841 | } else if (renderableType & EGL_OPENGL_ES2_BIT) { |
| 842 | contextClientVersion = 2; |
| 843 | } else if (renderableType & EGL_OPENGL_ES_BIT) { |
| 844 | contextClientVersion = 1; |
| 845 | } else { |
| 846 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); |
| 847 | } |
| 848 | |
| 849 | std::vector<EGLint> contextAttributes; |
| 850 | contextAttributes.reserve(7); |
| 851 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); |
| 852 | contextAttributes.push_back(contextClientVersion); |
| 853 | if (useContextPriority) { |
| 854 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); |
| 855 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); |
| 856 | } |
| 857 | if (protection == Protection::PROTECTED) { |
| 858 | contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 859 | contextAttributes.push_back(EGL_TRUE); |
| 860 | } |
| 861 | contextAttributes.push_back(EGL_NONE); |
| 862 | |
| 863 | EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data()); |
| 864 | |
| 865 | if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) { |
| 866 | // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus |
| 867 | // EGL_NO_CONTEXT so that we can abort. |
| 868 | if (config != EGL_NO_CONFIG_KHR) { |
| 869 | return context; |
| 870 | } |
| 871 | // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we |
| 872 | // should try to fall back to GLES 2. |
| 873 | contextAttributes[1] = 2; |
| 874 | context = eglCreateContext(display, config, shareContext, contextAttributes.data()); |
| 875 | } |
| 876 | |
| 877 | return context; |
| 878 | } |
| 879 | |
| 880 | EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display, |
| 881 | EGLConfig config, int hwcFormat, |
| 882 | Protection protection) { |
| 883 | EGLConfig placeholderConfig = config; |
| 884 | if (placeholderConfig == EGL_NO_CONFIG_KHR) { |
| 885 | placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); |
| 886 | } |
| 887 | std::vector<EGLint> attributes; |
| 888 | attributes.reserve(7); |
| 889 | attributes.push_back(EGL_WIDTH); |
| 890 | attributes.push_back(1); |
| 891 | attributes.push_back(EGL_HEIGHT); |
| 892 | attributes.push_back(1); |
| 893 | if (protection == Protection::PROTECTED) { |
| 894 | attributes.push_back(EGL_PROTECTED_CONTENT_EXT); |
| 895 | attributes.push_back(EGL_TRUE); |
| 896 | } |
| 897 | attributes.push_back(EGL_NONE); |
| 898 | |
| 899 | return eglCreatePbufferSurface(display, placeholderConfig, attributes.data()); |
| 900 | } |
| 901 | |
Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 902 | void SkiaGLRenderEngine::cleanFramebufferCache() {} |
John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 903 | |
| 904 | } // namespace skia |
| 905 | } // namespace renderengine |
Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 906 | } // namespace android |