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