| 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 |  | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 419 | static bool needsLinearEffect(const mat4& colorTransform, ui::Dataspace sourceDataspace, | 
|  | 420 | ui::Dataspace destinationDataspace) { | 
|  | 421 | return colorTransform != mat4() || needsToneMapping(sourceDataspace, destinationDataspace); | 
|  | 422 | } | 
|  | 423 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 424 | void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) { | 
|  | 425 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 426 | mTextureCache.erase(bufferId); | 
|  | 427 | mProtectedTextureCache.erase(bufferId); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 428 | } | 
|  | 429 |  | 
|  | 430 | status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display, | 
|  | 431 | const std::vector<const LayerSettings*>& layers, | 
|  | 432 | const sp<GraphicBuffer>& buffer, | 
|  | 433 | const bool useFramebufferCache, | 
|  | 434 | base::unique_fd&& bufferFence, base::unique_fd* drawFence) { | 
|  | 435 | ATRACE_NAME("SkiaGL::drawLayers"); | 
|  | 436 | std::lock_guard<std::mutex> lock(mRenderingMutex); | 
|  | 437 | if (layers.empty()) { | 
|  | 438 | ALOGV("Drawing empty layer stack"); | 
|  | 439 | return NO_ERROR; | 
|  | 440 | } | 
|  | 441 |  | 
|  | 442 | if (bufferFence.get() >= 0) { | 
|  | 443 | // Duplicate the fence for passing to waitFence. | 
|  | 444 | base::unique_fd bufferFenceDup(dup(bufferFence.get())); | 
|  | 445 | if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) { | 
|  | 446 | ATRACE_NAME("Waiting before draw"); | 
|  | 447 | sync_wait(bufferFence.get(), -1); | 
|  | 448 | } | 
|  | 449 | } | 
|  | 450 | if (buffer == nullptr) { | 
|  | 451 | ALOGE("No output buffer provided. Aborting GPU composition."); | 
|  | 452 | return BAD_VALUE; | 
|  | 453 | } | 
|  | 454 |  | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 455 | auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext; | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 456 | auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 457 | AHardwareBuffer_Desc bufferDesc; | 
|  | 458 | AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 459 | LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE), | 
|  | 460 | "missing usage"); | 
|  | 461 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 462 | std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef = nullptr; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 463 | if (useFramebufferCache) { | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 464 | auto iter = cache.find(buffer->getId()); | 
|  | 465 | if (iter != cache.end()) { | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 466 | ALOGV("Cache hit!"); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 467 | surfaceTextureRef = iter->second; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 468 | } | 
|  | 469 | } | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 470 |  | 
|  | 471 | if (surfaceTextureRef == nullptr || surfaceTextureRef->getTexture() == nullptr) { | 
|  | 472 | surfaceTextureRef = std::make_shared<AutoBackendTexture::LocalRef>(); | 
|  | 473 | surfaceTextureRef->setTexture( | 
|  | 474 | new AutoBackendTexture(mGrContext.get(), buffer->toAHardwareBuffer(), true)); | 
|  | 475 | if (useFramebufferCache) { | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 476 | ALOGD("Adding to cache"); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 477 | cache.insert({buffer->getId(), surfaceTextureRef}); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 478 | } | 
|  | 479 | } | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 480 |  | 
|  | 481 | sk_sp<SkSurface> surface = | 
|  | 482 | surfaceTextureRef->getTexture()->getOrCreateSurface(mUseColorManagement | 
|  | 483 | ? display.outputDataspace | 
|  | 484 | : ui::Dataspace::SRGB, | 
|  | 485 | mGrContext.get()); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 486 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 487 | auto canvas = surface->getCanvas(); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 488 | // Clear the entire canvas with a transparent black to prevent ghost images. | 
|  | 489 | canvas->clear(SK_ColorTRANSPARENT); | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 490 | canvas->save(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 491 |  | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 492 | // Before doing any drawing, let's make sure that we'll start at the origin of the display. | 
|  | 493 | // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual | 
|  | 494 | // displays might have different scaling when compared to the physical screen. | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 495 |  | 
|  | 496 | canvas->clipRect(getSkRect(display.physicalDisplay)); | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 497 | SkMatrix screenTransform; | 
|  | 498 | screenTransform.setTranslate(display.physicalDisplay.left, display.physicalDisplay.top); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 499 |  | 
|  | 500 | const auto clipWidth = display.clip.width(); | 
|  | 501 | const auto clipHeight = display.clip.height(); | 
|  | 502 | auto rotatedClipWidth = clipWidth; | 
|  | 503 | auto rotatedClipHeight = clipHeight; | 
|  | 504 | // Scale is contingent on the rotation result. | 
|  | 505 | if (display.orientation & ui::Transform::ROT_90) { | 
|  | 506 | std::swap(rotatedClipWidth, rotatedClipHeight); | 
|  | 507 | } | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 508 | const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) / | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 509 | static_cast<SkScalar>(rotatedClipWidth); | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 510 | const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) / | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 511 | static_cast<SkScalar>(rotatedClipHeight); | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 512 | screenTransform.preScale(scaleX, scaleY); | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 513 |  | 
|  | 514 | // Canvas rotation is done by centering the clip window at the origin, rotating, translating | 
|  | 515 | // 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] | 516 | screenTransform.preTranslate(rotatedClipWidth / 2, rotatedClipHeight / 2); | 
|  | 517 | screenTransform.preRotate(toDegrees(display.orientation)); | 
|  | 518 | screenTransform.preTranslate(-clipWidth / 2, -clipHeight / 2); | 
|  | 519 | screenTransform.preTranslate(-display.clip.left, -display.clip.top); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 520 | for (const auto& layer : layers) { | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 521 | const SkMatrix drawTransform = getDrawTransform(layer, screenTransform); | 
|  | 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); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 526 | std::unordered_map<uint32_t, sk_sp<SkSurface>> cachedBlurs; | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 527 |  | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 528 | if (mBlurFilter) { | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 529 | const auto layerRect = drawTransform.mapRect(dest); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 530 | if (layer->backgroundBlurRadius > 0) { | 
|  | 531 | ATRACE_NAME("BackgroundBlur"); | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 532 | auto blurredSurface = mBlurFilter->generate(canvas, surface, | 
|  | 533 | layer->backgroundBlurRadius, layerRect); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 534 | cachedBlurs[layer->backgroundBlurRadius] = blurredSurface; | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 535 |  | 
|  | 536 | drawBlurRegion(canvas, getBlurRegion(layer), drawTransform, blurredSurface); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 537 | } | 
|  | 538 | if (layer->blurRegions.size() > 0) { | 
|  | 539 | for (auto region : layer->blurRegions) { | 
|  | 540 | if (cachedBlurs[region.blurRadius]) { | 
|  | 541 | continue; | 
|  | 542 | } | 
|  | 543 | ATRACE_NAME("BlurRegion"); | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 544 | auto blurredSurface = | 
|  | 545 | mBlurFilter->generate(canvas, surface, region.blurRadius, layerRect); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 546 | cachedBlurs[region.blurRadius] = blurredSurface; | 
|  | 547 | } | 
|  | 548 | } | 
| Lucas Dupin | f4cb4a0 | 2020-09-22 14:19:26 -0700 | [diff] [blame] | 549 | } | 
|  | 550 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 551 | if (layer->source.buffer.buffer) { | 
|  | 552 | ATRACE_NAME("DrawImage"); | 
|  | 553 | const auto& item = layer->source.buffer; | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 554 | const auto bufferWidth = item.buffer->getBounds().width(); | 
|  | 555 | const auto bufferHeight = item.buffer->getBounds().height(); | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 556 | std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr; | 
|  | 557 | auto iter = mTextureCache.find(item.buffer->getId()); | 
|  | 558 | if (iter != mTextureCache.end()) { | 
|  | 559 | imageTextureRef = iter->second; | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 560 | } else { | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 561 | imageTextureRef = std::make_shared<AutoBackendTexture::LocalRef>(); | 
|  | 562 | imageTextureRef->setTexture(new AutoBackendTexture(mGrContext.get(), | 
|  | 563 | item.buffer->toAHardwareBuffer(), | 
|  | 564 | false)); | 
|  | 565 | mTextureCache.insert({buffer->getId(), imageTextureRef}); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 566 | } | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 567 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 568 | sk_sp<SkImage> image = | 
|  | 569 | imageTextureRef->getTexture() | 
|  | 570 | ->makeImage(mUseColorManagement | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 571 | ? (needsLinearEffect(layer->colorTransform, | 
|  | 572 | layer->sourceDataspace, | 
|  | 573 | display.outputDataspace) | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 574 | // If we need to map to linear space, | 
|  | 575 | // then mark the source image with the | 
|  | 576 | // same colorspace as the destination | 
|  | 577 | // surface so that Skia's color | 
|  | 578 | // management is a no-op. | 
|  | 579 | ? display.outputDataspace | 
|  | 580 | : layer->sourceDataspace) | 
|  | 581 | : ui::Dataspace::SRGB, | 
|  | 582 | item.isOpaque ? kOpaque_SkAlphaType | 
|  | 583 | : (item.usePremultipliedAlpha | 
|  | 584 | ? kPremul_SkAlphaType | 
|  | 585 | : kUnpremul_SkAlphaType), | 
|  | 586 | mGrContext.get()); | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 587 | SkMatrix matrix; | 
|  | 588 | if (layer->geometry.roundedCornersRadius > 0) { | 
|  | 589 | const auto roundedRect = getRoundedRect(layer); | 
|  | 590 | matrix.setTranslate(roundedRect.getBounds().left() - dest.left(), | 
|  | 591 | roundedRect.getBounds().top() - dest.top()); | 
|  | 592 | } else { | 
|  | 593 | matrix.setIdentity(); | 
|  | 594 | } | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 595 |  | 
|  | 596 | auto texMatrix = getSkM44(item.textureTransform).asM33(); | 
| Huihong Luo | 42b86c1 | 2020-10-29 13:00:13 -0700 | [diff] [blame] | 597 |  | 
|  | 598 | // b/171404534, scale to fix the layer | 
|  | 599 | matrix.postScale(bounds.getWidth() / bufferWidth, bounds.getHeight() / bufferHeight); | 
|  | 600 |  | 
| Alec Mouri | 678245d | 2020-09-30 16:58:23 -0700 | [diff] [blame] | 601 | // textureTansform was intended to be passed directly into a shader, so when | 
|  | 602 | // building the total matrix with the textureTransform we need to first | 
|  | 603 | // normalize it, then apply the textureTransform, then scale back up. | 
|  | 604 | matrix.postScale(1.0f / bufferWidth, 1.0f / bufferHeight); | 
|  | 605 |  | 
|  | 606 | auto rotatedBufferWidth = bufferWidth; | 
|  | 607 | auto rotatedBufferHeight = bufferHeight; | 
|  | 608 |  | 
|  | 609 | // Swap the buffer width and height if we're rotating, so that we | 
|  | 610 | // scale back up by the correct factors post-rotation. | 
|  | 611 | if (texMatrix.getSkewX() <= -0.5f || texMatrix.getSkewX() >= 0.5f) { | 
|  | 612 | std::swap(rotatedBufferWidth, rotatedBufferHeight); | 
|  | 613 | // TODO: clean this up. | 
|  | 614 | // GLESRenderEngine specifies its texture coordinates in | 
|  | 615 | // CW orientation under OpenGL conventions, when they probably should have | 
|  | 616 | // been CCW instead. The net result is that orientation | 
|  | 617 | // transforms are applied in the reverse | 
|  | 618 | // direction to render the correct result, because SurfaceFlinger uses the inverse | 
|  | 619 | // of the display transform to correct for that. But this means that | 
|  | 620 | // the tex transform passed by SkiaGLRenderEngine will rotate | 
|  | 621 | // individual layers in the reverse orientation. Hack around it | 
|  | 622 | // by injected a 180 degree rotation, but ultimately this is | 
|  | 623 | // a bug in how SurfaceFlinger invokes the RenderEngine | 
|  | 624 | // interface, so the proper fix should live there, and GLESRenderEngine | 
|  | 625 | // should be fixed accordingly. | 
|  | 626 | matrix.postRotate(180, 0.5, 0.5); | 
|  | 627 | } | 
|  | 628 |  | 
|  | 629 | matrix.postConcat(texMatrix); | 
|  | 630 | matrix.postScale(rotatedBufferWidth, rotatedBufferHeight); | 
| Ana Krulec | b7b28b2 | 2020-11-23 14:48:58 -0800 | [diff] [blame] | 631 | sk_sp<SkShader> shader; | 
|  | 632 |  | 
|  | 633 | if (layer->source.buffer.useTextureFiltering) { | 
|  | 634 | shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp, | 
|  | 635 | SkSamplingOptions( | 
|  | 636 | {SkFilterMode::kLinear, SkMipmapMode::kNone}), | 
|  | 637 | &matrix); | 
|  | 638 | } else { | 
|  | 639 | shader = image->makeShader(matrix); | 
|  | 640 | } | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 641 |  | 
|  | 642 | if (mUseColorManagement && | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 643 | needsLinearEffect(layer->colorTransform, layer->sourceDataspace, | 
|  | 644 | display.outputDataspace)) { | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 645 | LinearEffect effect = LinearEffect{.inputDataspace = layer->sourceDataspace, | 
|  | 646 | .outputDataspace = display.outputDataspace, | 
|  | 647 | .undoPremultipliedAlpha = !item.isOpaque && | 
|  | 648 | item.usePremultipliedAlpha}; | 
| Alec Mouri | c16a77d | 2020-11-13 13:20:11 -0800 | [diff] [blame] | 649 |  | 
|  | 650 | auto effectIter = mRuntimeEffects.find(effect); | 
|  | 651 | sk_sp<SkRuntimeEffect> runtimeEffect = nullptr; | 
|  | 652 | if (effectIter == mRuntimeEffects.end()) { | 
|  | 653 | runtimeEffect = buildRuntimeEffect(effect); | 
|  | 654 | mRuntimeEffects.insert({effect, runtimeEffect}); | 
|  | 655 | } else { | 
|  | 656 | runtimeEffect = effectIter->second; | 
|  | 657 | } | 
|  | 658 |  | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 659 | paint.setShader(createLinearEffectShader(shader, effect, runtimeEffect, | 
| Alec Mouri | 1a4d064 | 2020-11-13 17:42:01 -0800 | [diff] [blame] | 660 | layer->colorTransform, | 
| Alec Mouri | 029d195 | 2020-10-12 10:37:08 -0700 | [diff] [blame] | 661 | display.maxLuminance, | 
|  | 662 | layer->source.buffer.maxMasteringLuminance, | 
|  | 663 | layer->source.buffer.maxContentLuminance)); | 
|  | 664 | } else { | 
|  | 665 | paint.setShader(shader); | 
|  | 666 | } | 
| Ana Krulec | 1768bd2 | 2020-11-23 14:51:31 -0800 | [diff] [blame] | 667 | // Make sure to take into the account the alpha set on the layer. | 
|  | 668 | paint.setAlphaf(layer->alpha); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 669 | } else { | 
|  | 670 | ATRACE_NAME("DrawColor"); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 671 | const auto color = layer->source.solidColor; | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 672 | paint.setShader(SkShaders::Color(SkColor4f{.fR = color.r, | 
|  | 673 | .fG = color.g, | 
|  | 674 | .fB = color.b, | 
|  | 675 | layer->alpha}, | 
|  | 676 | nullptr)); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 677 | } | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 678 |  | 
| Alec Mouri | b34f0b7 | 2020-10-02 13:18:34 -0700 | [diff] [blame] | 679 | paint.setColorFilter(SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform))); | 
|  | 680 |  | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 681 | for (const auto effectRegion : layer->blurRegions) { | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 682 | drawBlurRegion(canvas, effectRegion, drawTransform, | 
|  | 683 | cachedBlurs[effectRegion.blurRadius]); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 684 | } | 
|  | 685 |  | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 686 | canvas->save(); | 
|  | 687 | canvas->concat(drawTransform); | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 688 | if (layer->shadow.length > 0) { | 
|  | 689 | const auto rect = layer->geometry.roundedCornersRadius > 0 | 
|  | 690 | ? getSkRect(layer->geometry.roundedCornersCrop) | 
|  | 691 | : dest; | 
|  | 692 | drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow); | 
|  | 693 | } | 
|  | 694 |  | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 695 | if (layer->geometry.roundedCornersRadius > 0) { | 
|  | 696 | canvas->drawRRect(getRoundedRect(layer), paint); | 
|  | 697 | } else { | 
|  | 698 | canvas->drawRect(dest, paint); | 
|  | 699 | } | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 700 |  | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 701 | canvas->restore(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 702 | } | 
|  | 703 | { | 
|  | 704 | ATRACE_NAME("flush surface"); | 
|  | 705 | surface->flush(); | 
|  | 706 | } | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 707 | canvas->restore(); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 708 |  | 
|  | 709 | if (drawFence != nullptr) { | 
|  | 710 | *drawFence = flush(); | 
|  | 711 | } | 
|  | 712 |  | 
|  | 713 | // If flush failed or we don't support native fences, we need to force the | 
|  | 714 | // gl command stream to be executed. | 
|  | 715 | bool requireSync = drawFence == nullptr || drawFence->get() < 0; | 
|  | 716 | if (requireSync) { | 
|  | 717 | ATRACE_BEGIN("Submit(sync=true)"); | 
|  | 718 | } else { | 
|  | 719 | ATRACE_BEGIN("Submit(sync=false)"); | 
|  | 720 | } | 
| Lucas Dupin | d508e47 | 2020-11-04 04:32:06 +0000 | [diff] [blame] | 721 | bool success = grContext->submit(requireSync); | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 722 | ATRACE_END(); | 
|  | 723 | if (!success) { | 
|  | 724 | ALOGE("Failed to flush RenderEngine commands"); | 
|  | 725 | // Chances are, something illegal happened (either the caller passed | 
|  | 726 | // us bad parameters, or we messed up our shader generation). | 
|  | 727 | return INVALID_OPERATION; | 
|  | 728 | } | 
|  | 729 |  | 
|  | 730 | // checkErrors(); | 
|  | 731 | return NO_ERROR; | 
|  | 732 | } | 
|  | 733 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 734 | inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) { | 
|  | 735 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); | 
|  | 736 | } | 
|  | 737 |  | 
|  | 738 | inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) { | 
|  | 739 | return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom); | 
|  | 740 | } | 
|  | 741 |  | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 742 | inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) { | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 743 | const auto rect = getSkRect(layer->geometry.roundedCornersCrop); | 
| Lucas Dupin | 21f348e | 2020-09-16 17:31:26 -0700 | [diff] [blame] | 744 | const auto cornerRadius = layer->geometry.roundedCornersRadius; | 
|  | 745 | return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius); | 
|  | 746 | } | 
|  | 747 |  | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 748 | inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) { | 
|  | 749 | const auto rect = getSkRect(layer->geometry.boundaries); | 
|  | 750 | const auto cornersRadius = layer->geometry.roundedCornersRadius; | 
|  | 751 | return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius), | 
|  | 752 | .cornerRadiusTL = cornersRadius, | 
|  | 753 | .cornerRadiusTR = cornersRadius, | 
|  | 754 | .cornerRadiusBL = cornersRadius, | 
|  | 755 | .cornerRadiusBR = cornersRadius, | 
|  | 756 | .alpha = 1, | 
|  | 757 | .left = static_cast<int>(rect.fLeft), | 
|  | 758 | .top = static_cast<int>(rect.fTop), | 
|  | 759 | .right = static_cast<int>(rect.fRight), | 
|  | 760 | .bottom = static_cast<int>(rect.fBottom)}; | 
|  | 761 | } | 
|  | 762 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 763 | inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) { | 
|  | 764 | return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255); | 
|  | 765 | } | 
|  | 766 |  | 
| Lucas Dupin | bb1a1d4 | 2020-09-18 15:17:02 -0700 | [diff] [blame] | 767 | inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) { | 
|  | 768 | return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], | 
|  | 769 | matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1], | 
|  | 770 | matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2], | 
|  | 771 | matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]); | 
|  | 772 | } | 
|  | 773 |  | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 774 | inline SkMatrix SkiaGLRenderEngine::getDrawTransform(const LayerSettings* layer, | 
|  | 775 | const SkMatrix& screenTransform) { | 
|  | 776 | // Layers have a local transform matrix that should be applied to them. | 
|  | 777 | const auto layerTransform = getSkM44(layer->geometry.positionTransform).asM33(); | 
|  | 778 | return SkMatrix::Concat(screenTransform, layerTransform); | 
|  | 779 | } | 
|  | 780 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 781 | inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) { | 
|  | 782 | return SkPoint3::Make(vector.x, vector.y, vector.z); | 
|  | 783 | } | 
|  | 784 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 785 | size_t SkiaGLRenderEngine::getMaxTextureSize() const { | 
|  | 786 | return mGrContext->maxTextureSize(); | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | size_t SkiaGLRenderEngine::getMaxViewportDims() const { | 
|  | 790 | return mGrContext->maxRenderTargetSize(); | 
|  | 791 | } | 
|  | 792 |  | 
| Lucas Dupin | 3f11e92 | 2020-09-22 17:31:04 -0700 | [diff] [blame] | 793 | void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius, | 
|  | 794 | const ShadowSettings& settings) { | 
|  | 795 | ATRACE_CALL(); | 
|  | 796 | const float casterZ = settings.length / 2.0f; | 
|  | 797 | const auto shadowShape = cornerRadius > 0 | 
|  | 798 | ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius)) | 
|  | 799 | : SkPath::Rect(casterRect); | 
|  | 800 | const auto flags = | 
|  | 801 | settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag; | 
|  | 802 |  | 
|  | 803 | SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ), | 
|  | 804 | getSkPoint3(settings.lightPos), settings.lightRadius, | 
|  | 805 | getSkColor(settings.ambientColor), getSkColor(settings.spotColor), | 
|  | 806 | flags); | 
|  | 807 | } | 
|  | 808 |  | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 809 | void SkiaGLRenderEngine::drawBlurRegion(SkCanvas* canvas, const BlurRegion& effectRegion, | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 810 | const SkMatrix& drawTransform, | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 811 | sk_sp<SkSurface> blurredSurface) { | 
|  | 812 | ATRACE_CALL(); | 
| 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 | SkPaint paint; | 
| Lyn Han | 1bdedb3 | 2020-11-23 20:33:57 +0000 | [diff] [blame] | 815 | paint.setAlpha(static_cast<int>(effectRegion.alpha * 255)); | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 816 | const auto matrix = mBlurFilter->getShaderMatrix(); | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 817 | paint.setShader(blurredSurface->makeImageSnapshot()->makeShader(matrix)); | 
|  | 818 |  | 
| Galia Peycheva | 80116e5 | 2020-11-06 11:57:25 +0100 | [diff] [blame] | 819 | auto rect = SkRect::MakeLTRB(effectRegion.left, effectRegion.top, effectRegion.right, | 
|  | 820 | effectRegion.bottom); | 
|  | 821 | drawTransform.mapRect(&rect); | 
|  | 822 |  | 
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 823 | if (effectRegion.cornerRadiusTL > 0 || effectRegion.cornerRadiusTR > 0 || | 
|  | 824 | effectRegion.cornerRadiusBL > 0 || effectRegion.cornerRadiusBR > 0) { | 
|  | 825 | const SkVector radii[4] = | 
|  | 826 | {SkVector::Make(effectRegion.cornerRadiusTL, effectRegion.cornerRadiusTL), | 
|  | 827 | SkVector::Make(effectRegion.cornerRadiusTR, effectRegion.cornerRadiusTR), | 
|  | 828 | SkVector::Make(effectRegion.cornerRadiusBL, effectRegion.cornerRadiusBL), | 
|  | 829 | SkVector::Make(effectRegion.cornerRadiusBR, effectRegion.cornerRadiusBR)}; | 
|  | 830 | SkRRect roundedRect; | 
|  | 831 | roundedRect.setRectRadii(rect, radii); | 
|  | 832 | canvas->drawRRect(roundedRect, paint); | 
|  | 833 | } else { | 
|  | 834 | canvas->drawRect(rect, paint); | 
|  | 835 | } | 
|  | 836 | } | 
|  | 837 |  | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 838 | EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config, | 
|  | 839 | EGLContext shareContext, bool useContextPriority, | 
|  | 840 | Protection protection) { | 
|  | 841 | EGLint renderableType = 0; | 
|  | 842 | if (config == EGL_NO_CONFIG_KHR) { | 
|  | 843 | renderableType = EGL_OPENGL_ES3_BIT; | 
|  | 844 | } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) { | 
|  | 845 | LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE"); | 
|  | 846 | } | 
|  | 847 | EGLint contextClientVersion = 0; | 
|  | 848 | if (renderableType & EGL_OPENGL_ES3_BIT) { | 
|  | 849 | contextClientVersion = 3; | 
|  | 850 | } else if (renderableType & EGL_OPENGL_ES2_BIT) { | 
|  | 851 | contextClientVersion = 2; | 
|  | 852 | } else if (renderableType & EGL_OPENGL_ES_BIT) { | 
|  | 853 | contextClientVersion = 1; | 
|  | 854 | } else { | 
|  | 855 | LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs"); | 
|  | 856 | } | 
|  | 857 |  | 
|  | 858 | std::vector<EGLint> contextAttributes; | 
|  | 859 | contextAttributes.reserve(7); | 
|  | 860 | contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION); | 
|  | 861 | contextAttributes.push_back(contextClientVersion); | 
|  | 862 | if (useContextPriority) { | 
|  | 863 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG); | 
|  | 864 | contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG); | 
|  | 865 | } | 
|  | 866 | if (protection == Protection::PROTECTED) { | 
|  | 867 | contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 868 | contextAttributes.push_back(EGL_TRUE); | 
|  | 869 | } | 
|  | 870 | contextAttributes.push_back(EGL_NONE); | 
|  | 871 |  | 
|  | 872 | EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 873 |  | 
|  | 874 | if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) { | 
|  | 875 | // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus | 
|  | 876 | // EGL_NO_CONTEXT so that we can abort. | 
|  | 877 | if (config != EGL_NO_CONFIG_KHR) { | 
|  | 878 | return context; | 
|  | 879 | } | 
|  | 880 | // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we | 
|  | 881 | // should try to fall back to GLES 2. | 
|  | 882 | contextAttributes[1] = 2; | 
|  | 883 | context = eglCreateContext(display, config, shareContext, contextAttributes.data()); | 
|  | 884 | } | 
|  | 885 |  | 
|  | 886 | return context; | 
|  | 887 | } | 
|  | 888 |  | 
|  | 889 | EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display, | 
|  | 890 | EGLConfig config, int hwcFormat, | 
|  | 891 | Protection protection) { | 
|  | 892 | EGLConfig placeholderConfig = config; | 
|  | 893 | if (placeholderConfig == EGL_NO_CONFIG_KHR) { | 
|  | 894 | placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true); | 
|  | 895 | } | 
|  | 896 | std::vector<EGLint> attributes; | 
|  | 897 | attributes.reserve(7); | 
|  | 898 | attributes.push_back(EGL_WIDTH); | 
|  | 899 | attributes.push_back(1); | 
|  | 900 | attributes.push_back(EGL_HEIGHT); | 
|  | 901 | attributes.push_back(1); | 
|  | 902 | if (protection == Protection::PROTECTED) { | 
|  | 903 | attributes.push_back(EGL_PROTECTED_CONTENT_EXT); | 
|  | 904 | attributes.push_back(EGL_TRUE); | 
|  | 905 | } | 
|  | 906 | attributes.push_back(EGL_NONE); | 
|  | 907 |  | 
|  | 908 | return eglCreatePbufferSurface(display, placeholderConfig, attributes.data()); | 
|  | 909 | } | 
|  | 910 |  | 
| Alec Mouri | c7f6c8b | 2020-11-09 18:35:20 -0800 | [diff] [blame] | 911 | void SkiaGLRenderEngine::cleanFramebufferCache() {} | 
| John Reck | 67b1e2b | 2020-08-26 13:17:24 -0700 | [diff] [blame] | 912 |  | 
|  | 913 | } // namespace skia | 
|  | 914 | } // namespace renderengine | 
| Galia Peycheva | 6c46065 | 2020-11-03 19:42:42 +0100 | [diff] [blame] | 915 | } // namespace android |