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