blob: 0798562aec52956c70b0a21182e71289b939d309 [file] [log] [blame]
John Reck67b1e2b2020-08-26 13:17:24 -07001/*
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 Krulec70d15b1b2020-12-01 10:05:15 -080018#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
Alec Mouri4ce5ec02021-01-07 17:33:21 -080022#include "SkiaGLRenderEngine.h"
John Reck67b1e2b2020-08-26 13:17:24 -070023
John Reck67b1e2b2020-08-26 13:17:24 -070024#include <EGL/egl.h>
25#include <EGL/eglext.h>
John Reck67b1e2b2020-08-26 13:17:24 -070026#include <GrContextOptions.h>
John Reck67b1e2b2020-08-26 13:17:24 -070027#include <SkCanvas.h>
Alec Mourib34f0b72020-10-02 13:18:34 -070028#include <SkColorFilter.h>
29#include <SkColorMatrix.h>
Alec Mourib5777452020-09-28 11:32:42 -070030#include <SkColorSpace.h>
John Reck67b1e2b2020-08-26 13:17:24 -070031#include <SkImage.h>
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070032#include <SkImageFilters.h>
Alec Mouric0aae732021-01-12 13:32:18 -080033#include <SkRegion.h>
Lucas Dupin3f11e922020-09-22 17:31:04 -070034#include <SkShadowUtils.h>
John Reck67b1e2b2020-08-26 13:17:24 -070035#include <SkSurface.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080036#include <android-base/stringprintf.h>
Alec Mourib5777452020-09-28 11:32:42 -070037#include <gl/GrGLInterface.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080038#include <sync/sync.h>
39#include <ui/BlurRegion.h>
Ana Krulec1d12b3b2021-01-27 16:49:51 -080040#include <ui/DebugUtils.h>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080041#include <ui/GraphicBuffer.h>
42#include <utils/Trace.h>
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050043#include "Cache.h"
Alec Mourib5777452020-09-28 11:32:42 -070044
45#include <cmath>
Alec Mouri4ce5ec02021-01-07 17:33:21 -080046#include <cstdint>
47#include <memory>
48
49#include "../gl/GLExtensions.h"
Alec Mouric0aae732021-01-12 13:32:18 -080050#include "ColorSpaces.h"
Alec Mouri4ce5ec02021-01-07 17:33:21 -080051#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 Mourib5777452020-09-28 11:32:42 -070058
John Reck67b1e2b2020-08-26 13:17:24 -070059bool checkGlError(const char* op, int lineNumber);
60
61namespace android {
62namespace renderengine {
63namespace skia {
64
Ana Krulec1d12b3b2021-01-27 16:49:51 -080065using base::StringAppendF;
66
John Reck67b1e2b2020-08-26 13:17:24 -070067static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
68 EGLint wanted, EGLConfig* outConfig) {
69 EGLint numConfigs = -1, n = 0;
70 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
71 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
72 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
73 configs.resize(n);
74
75 if (!configs.empty()) {
76 if (attribute != EGL_NONE) {
77 for (EGLConfig config : configs) {
78 EGLint value = 0;
79 eglGetConfigAttrib(dpy, config, attribute, &value);
80 if (wanted == value) {
81 *outConfig = config;
82 return NO_ERROR;
83 }
84 }
85 } else {
86 // just pick the first one
87 *outConfig = configs[0];
88 return NO_ERROR;
89 }
90 }
91
92 return NAME_NOT_FOUND;
93}
94
95static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
96 EGLConfig* config) {
97 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
98 // it is to be used with WIFI displays
99 status_t err;
100 EGLint wantedAttribute;
101 EGLint wantedAttributeValue;
102
103 std::vector<EGLint> attribs;
104 if (renderableType) {
105 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
106 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
107
108 // Default to 8 bits per channel.
109 const EGLint tmpAttribs[] = {
110 EGL_RENDERABLE_TYPE,
111 renderableType,
112 EGL_RECORDABLE_ANDROID,
113 EGL_TRUE,
114 EGL_SURFACE_TYPE,
115 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
116 EGL_FRAMEBUFFER_TARGET_ANDROID,
117 EGL_TRUE,
118 EGL_RED_SIZE,
119 is1010102 ? 10 : 8,
120 EGL_GREEN_SIZE,
121 is1010102 ? 10 : 8,
122 EGL_BLUE_SIZE,
123 is1010102 ? 10 : 8,
124 EGL_ALPHA_SIZE,
125 is1010102 ? 2 : 8,
126 EGL_NONE,
127 };
128 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
129 std::back_inserter(attribs));
130 wantedAttribute = EGL_NONE;
131 wantedAttributeValue = EGL_NONE;
132 } else {
133 // if no renderable type specified, fallback to a simplified query
134 wantedAttribute = EGL_NATIVE_VISUAL_ID;
135 wantedAttributeValue = format;
136 }
137
138 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
139 config);
140 if (err == NO_ERROR) {
141 EGLint caveat;
142 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
143 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
144 }
145
146 return err;
147}
148
149std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
150 const RenderEngineCreationArgs& args) {
151 // initialize EGL for the default display
152 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
153 if (!eglInitialize(display, nullptr, nullptr)) {
154 LOG_ALWAYS_FATAL("failed to initialize EGL");
155 }
156
Yiwei Zhange2650962020-12-01 23:27:58 +0000157 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700158 if (!eglVersion) {
159 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000160 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700161 }
162
Yiwei Zhange2650962020-12-01 23:27:58 +0000163 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700164 if (!eglExtensions) {
165 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000166 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700167 }
168
169 auto& extensions = gl::GLExtensions::getInstance();
170 extensions.initWithEGLStrings(eglVersion, eglExtensions);
171
172 // The code assumes that ES2 or later is available if this extension is
173 // supported.
174 EGLConfig config = EGL_NO_CONFIG_KHR;
175 if (!extensions.hasNoConfigContext()) {
176 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
177 }
178
John Reck67b1e2b2020-08-26 13:17:24 -0700179 EGLContext protectedContext = EGL_NO_CONTEXT;
Alec Mourid6f09462020-12-07 11:18:17 -0800180 const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
John Reck67b1e2b2020-08-26 13:17:24 -0700181 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
Alec Mourid6f09462020-12-07 11:18:17 -0800182 protectedContext =
183 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700184 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
185 }
186
Alec Mourid6f09462020-12-07 11:18:17 -0800187 EGLContext ctxt =
188 createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
John Reck67b1e2b2020-08-26 13:17:24 -0700189
190 // if can't create a GL context, we can only abort.
191 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
192
193 EGLSurface placeholder = EGL_NO_SURFACE;
194 if (!extensions.hasSurfacelessContext()) {
195 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
196 Protection::UNPROTECTED);
197 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
198 }
199 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
200 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
201 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
202 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
203
204 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
205 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
206 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
207 Protection::PROTECTED);
208 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
209 "can't create protected placeholder pbuffer");
210 }
211
212 // initialize the renderer while GL is current
213 std::unique_ptr<SkiaGLRenderEngine> engine =
Lucas Dupind508e472020-11-04 04:32:06 +0000214 std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext,
215 protectedPlaceholder);
John Reck67b1e2b2020-08-26 13:17:24 -0700216
217 ALOGI("OpenGL ES informations:");
218 ALOGI("vendor : %s", extensions.getVendor());
219 ALOGI("renderer : %s", extensions.getRenderer());
220 ALOGI("version : %s", extensions.getVersion());
221 ALOGI("extensions: %s", extensions.getExtensions());
222 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
223 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
224
225 return engine;
226}
227
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500228void SkiaGLRenderEngine::primeCache() {
229 Cache::primeShaderCache(this);
230}
231
John Reck67b1e2b2020-08-26 13:17:24 -0700232EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
233 status_t err;
234 EGLConfig config;
235
236 // First try to get an ES3 config
237 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
238 if (err != NO_ERROR) {
239 // If ES3 fails, try to get an ES2 config
240 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
241 if (err != NO_ERROR) {
242 // If ES2 still doesn't work, probably because we're on the emulator.
243 // try a simplified query
244 ALOGW("no suitable EGLConfig found, trying a simpler query");
245 err = selectEGLConfig(display, format, 0, &config);
246 if (err != NO_ERROR) {
247 // this EGL is too lame for android
248 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
249 }
250 }
251 }
252
253 if (logConfig) {
254 // print some debugging info
255 EGLint r, g, b, a;
256 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
257 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
258 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
259 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
260 ALOGI("EGL information:");
261 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
262 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
263 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
264 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
265 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
266 }
267
268 return config;
269}
270
Leon Scroggins III9f3072c2021-03-22 10:42:47 -0400271sk_sp<SkData> SkiaGLRenderEngine::SkSLCacheMonitor::load(const SkData& key) {
272 // This "cache" does not actually cache anything. It just allows us to
273 // monitor Skia's internal cache. So this method always returns null.
274 return nullptr;
275}
276
277void SkiaGLRenderEngine::SkSLCacheMonitor::store(const SkData& key, const SkData& data,
278 const SkString& description) {
279 mShadersCachedSinceLastCall++;
280}
281
282void SkiaGLRenderEngine::assertShadersCompiled(int numShaders) {
283 const int cached = mSkSLCacheMonitor.shadersCachedSinceLastCall();
284 LOG_ALWAYS_FATAL_IF(cached != numShaders, "Attempted to cache %i shaders; cached %i",
285 numShaders, cached);
286}
287
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700288SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000289 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700290 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri0d995102021-02-24 16:53:38 -0800291 : SkiaRenderEngine(args.renderEngineType),
292 mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700293 mEGLContext(ctxt),
294 mPlaceholderSurface(placeholder),
295 mProtectedEGLContext(protectedContext),
Alec Mourib5777452020-09-28 11:32:42 -0700296 mProtectedPlaceholderSurface(protectedPlaceholder),
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400297 mDefaultPixelFormat(static_cast<PixelFormat>(args.pixelFormat)),
Alec Mouri0d995102021-02-24 16:53:38 -0800298 mUseColorManagement(args.useColorManagement) {
John Reck67b1e2b2020-08-26 13:17:24 -0700299 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
300 LOG_ALWAYS_FATAL_IF(!glInterface.get());
301
302 GrContextOptions options;
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -0400303 options.fDisableDriverCorrectnessWorkarounds = true;
John Reck67b1e2b2020-08-26 13:17:24 -0700304 options.fDisableDistanceFieldPaths = true;
Leon Scroggins III9f3072c2021-03-22 10:42:47 -0400305 options.fPersistentCache = &mSkSLCacheMonitor;
Lucas Dupind508e472020-11-04 04:32:06 +0000306 mGrContext = GrDirectContext::MakeGL(glInterface, options);
307 if (useProtectedContext(true)) {
308 mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options);
309 useProtectedContext(false);
310 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700311
312 if (args.supportsBackgroundBlur) {
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500313 ALOGD("Background Blurs Enabled");
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700314 mBlurFilter = new BlurFilter();
315 }
Alec Mouric0aae732021-01-12 13:32:18 -0800316 mCapture = std::make_unique<SkiaCapture>();
317}
318
319SkiaGLRenderEngine::~SkiaGLRenderEngine() {
Marin Shalamanovcea12ef2021-03-15 17:00:51 +0100320 cleanFramebufferCache();
Alec Mouric0aae732021-01-12 13:32:18 -0800321
Marin Shalamanovcea12ef2021-03-15 17:00:51 +0100322 std::lock_guard<std::mutex> lock(mRenderingMutex);
Alec Mouric0aae732021-01-12 13:32:18 -0800323 if (mBlurFilter) {
324 delete mBlurFilter;
325 }
326
327 mCapture = nullptr;
328
329 mGrContext->flushAndSubmit(true);
330 mGrContext->abandonContext();
331
332 if (mProtectedGrContext) {
333 mProtectedGrContext->flushAndSubmit(true);
334 mProtectedGrContext->abandonContext();
335 }
336
337 if (mPlaceholderSurface != EGL_NO_SURFACE) {
338 eglDestroySurface(mEGLDisplay, mPlaceholderSurface);
339 }
340 if (mProtectedPlaceholderSurface != EGL_NO_SURFACE) {
341 eglDestroySurface(mEGLDisplay, mProtectedPlaceholderSurface);
342 }
343 if (mEGLContext != EGL_NO_CONTEXT) {
344 eglDestroyContext(mEGLDisplay, mEGLContext);
345 }
346 if (mProtectedEGLContext != EGL_NO_CONTEXT) {
347 eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
348 }
349 eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
350 eglTerminate(mEGLDisplay);
351 eglReleaseThread();
John Reck67b1e2b2020-08-26 13:17:24 -0700352}
353
Lucas Dupind508e472020-11-04 04:32:06 +0000354bool SkiaGLRenderEngine::supportsProtectedContent() const {
355 return mProtectedEGLContext != EGL_NO_CONTEXT;
356}
357
358bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) {
359 if (useProtectedContext == mInProtectedContext) {
360 return true;
361 }
Alec Mourif6a07812021-02-11 21:07:55 -0800362 if (useProtectedContext && !supportsProtectedContent()) {
Lucas Dupind508e472020-11-04 04:32:06 +0000363 return false;
364 }
365 const EGLSurface surface =
366 useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface;
367 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
368 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
Alec Mouric0aae732021-01-12 13:32:18 -0800369
Lucas Dupind508e472020-11-04 04:32:06 +0000370 if (success) {
371 mInProtectedContext = useProtectedContext;
372 }
373 return success;
374}
375
John Reck67b1e2b2020-08-26 13:17:24 -0700376base::unique_fd SkiaGLRenderEngine::flush() {
377 ATRACE_CALL();
378 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
379 return base::unique_fd();
380 }
381
382 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
383 if (sync == EGL_NO_SYNC_KHR) {
384 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
385 return base::unique_fd();
386 }
387
388 // native fence fd will not be populated until flush() is done.
389 glFlush();
390
391 // get the fence fd
392 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
393 eglDestroySyncKHR(mEGLDisplay, sync);
394 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
395 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
396 }
397
398 return fenceFd;
399}
400
401bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) {
402 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
403 !gl::GLExtensions::getInstance().hasWaitSync()) {
404 return false;
405 }
406
407 // release the fd and transfer the ownership to EGLSync
408 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
409 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
410 if (sync == EGL_NO_SYNC_KHR) {
411 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
412 return false;
413 }
414
415 // XXX: The spec draft is inconsistent as to whether this should return an
416 // EGLint or void. Ignore the return value for now, as it's not strictly
417 // needed.
418 eglWaitSyncKHR(mEGLDisplay, sync, 0);
419 EGLint error = eglGetError();
420 eglDestroySyncKHR(mEGLDisplay, sync);
421 if (error != EGL_SUCCESS) {
422 ALOGE("failed to wait for EGL native fence sync: %#x", error);
423 return false;
424 }
425
426 return true;
427}
428
Alec Mouri678245d2020-09-30 16:58:23 -0700429static float toDegrees(uint32_t transform) {
430 switch (transform) {
431 case ui::Transform::ROT_90:
432 return 90.0;
433 case ui::Transform::ROT_180:
434 return 180.0;
435 case ui::Transform::ROT_270:
436 return 270.0;
437 default:
438 return 0.0;
439 }
440}
441
Alec Mourib34f0b72020-10-02 13:18:34 -0700442static SkColorMatrix toSkColorMatrix(const mat4& matrix) {
443 return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1],
444 matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2],
445 matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3],
446 matrix[3][3], 0);
447}
448
Alec Mouri029d1952020-10-12 10:37:08 -0700449static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) {
450 int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK;
451 int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK;
452
453 // Treat unsupported dataspaces as srgb
454 if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
455 destTransfer != HAL_DATASPACE_TRANSFER_HLG &&
456 destTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
457 destTransfer = HAL_DATASPACE_TRANSFER_SRGB;
458 }
459
460 if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
461 sourceTransfer != HAL_DATASPACE_TRANSFER_HLG &&
462 sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
463 sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB;
464 }
465
466 const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
467 const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB;
468 const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
469 const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB;
470
471 return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) &&
472 sourceTransfer != destTransfer;
473}
474
Ana Krulecdfec8f52021-01-13 12:51:47 -0800475void SkiaGLRenderEngine::cacheExternalTextureBuffer(const sp<GraphicBuffer>& buffer) {
476 // Only run this if RE is running on its own thread. This way the access to GL
477 // operations is guaranteed to be happening on the same thread.
478 if (mRenderEngineType != RenderEngineType::SKIA_GL_THREADED) {
479 return;
480 }
481 ATRACE_CALL();
482
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400483 // We need to switch the currently bound context if the buffer is protected but the current
484 // context is not. The current state must then be restored after the buffer is cached.
485 const bool protectedContextState = mInProtectedContext;
486 if (!useProtectedContext(protectedContextState ||
487 (buffer->getUsage() & GRALLOC_USAGE_PROTECTED))) {
488 ALOGE("Attempting to cache a buffer into a different context than what is currently bound");
Ana Krulecdfec8f52021-01-13 12:51:47 -0800489 return;
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400490 }
491
492 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
493 auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache;
494
495 std::lock_guard<std::mutex> lock(mRenderingMutex);
496 auto iter = cache.find(buffer->getId());
497 if (iter != cache.end()) {
498 ALOGV("Texture already exists in cache.");
Ana Krulecdfec8f52021-01-13 12:51:47 -0800499 } else {
500 std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef =
501 std::make_shared<AutoBackendTexture::LocalRef>();
502 imageTextureRef->setTexture(
Derek Sollenberger957f7b32021-03-19 15:42:19 -0400503 new AutoBackendTexture(grContext.get(), buffer->toAHardwareBuffer()));
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400504 cache.insert({buffer->getId(), imageTextureRef});
Ana Krulecdfec8f52021-01-13 12:51:47 -0800505 }
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400506 // restore the original state of the protected context if necessary
507 useProtectedContext(protectedContextState);
Ana Krulecdfec8f52021-01-13 12:51:47 -0800508}
509
John Reck67b1e2b2020-08-26 13:17:24 -0700510void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800511 ATRACE_CALL();
John Reck67b1e2b2020-08-26 13:17:24 -0700512 std::lock_guard<std::mutex> lock(mRenderingMutex);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800513 mTextureCache.erase(bufferId);
514 mProtectedTextureCache.erase(bufferId);
John Reck67b1e2b2020-08-26 13:17:24 -0700515}
516
Ana Krulec47814212021-01-06 19:00:10 -0800517sk_sp<SkShader> SkiaGLRenderEngine::createRuntimeEffectShader(sk_sp<SkShader> shader,
518 const LayerSettings* layer,
519 const DisplaySettings& display,
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500520 bool undoPremultipliedAlpha,
521 bool requiresLinearEffect) {
John Reckcdb4ed72021-02-04 13:39:33 -0500522 if (layer->stretchEffect.hasEffect()) {
523 // TODO: Implement
524 }
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500525 if (requiresLinearEffect) {
526 const ui::Dataspace inputDataspace =
527 mUseColorManagement ? layer->sourceDataspace : ui::Dataspace::UNKNOWN;
528 const ui::Dataspace outputDataspace =
529 mUseColorManagement ? display.outputDataspace : ui::Dataspace::UNKNOWN;
530
531 LinearEffect effect = LinearEffect{.inputDataspace = inputDataspace,
532 .outputDataspace = outputDataspace,
Ana Krulec47814212021-01-06 19:00:10 -0800533 .undoPremultipliedAlpha = undoPremultipliedAlpha};
534
535 auto effectIter = mRuntimeEffects.find(effect);
536 sk_sp<SkRuntimeEffect> runtimeEffect = nullptr;
537 if (effectIter == mRuntimeEffects.end()) {
538 runtimeEffect = buildRuntimeEffect(effect);
539 mRuntimeEffects.insert({effect, runtimeEffect});
540 } else {
541 runtimeEffect = effectIter->second;
542 }
543 return createLinearEffectShader(shader, effect, runtimeEffect, layer->colorTransform,
544 display.maxLuminance,
545 layer->source.buffer.maxMasteringLuminance,
546 layer->source.buffer.maxContentLuminance);
547 }
548 return shader;
549}
550
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500551void SkiaGLRenderEngine::initCanvas(SkCanvas* canvas, const DisplaySettings& display) {
Derek Sollenberger76664d62021-02-04 11:13:09 -0500552 if (CC_UNLIKELY(mCapture->isCaptureRunning())) {
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500553 // Record display settings when capture is running.
554 std::stringstream displaySettings;
555 PrintTo(display, &displaySettings);
556 // Store the DisplaySettings in additional information.
557 canvas->drawAnnotation(SkRect::MakeEmpty(), "DisplaySettings",
558 SkData::MakeWithCString(displaySettings.str().c_str()));
559 }
560
561 // Before doing any drawing, let's make sure that we'll start at the origin of the display.
562 // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual
563 // displays might have different scaling when compared to the physical screen.
564
565 canvas->clipRect(getSkRect(display.physicalDisplay));
566 canvas->translate(display.physicalDisplay.left, display.physicalDisplay.top);
567
568 const auto clipWidth = display.clip.width();
569 const auto clipHeight = display.clip.height();
570 auto rotatedClipWidth = clipWidth;
571 auto rotatedClipHeight = clipHeight;
572 // Scale is contingent on the rotation result.
573 if (display.orientation & ui::Transform::ROT_90) {
574 std::swap(rotatedClipWidth, rotatedClipHeight);
575 }
576 const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) /
577 static_cast<SkScalar>(rotatedClipWidth);
578 const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) /
579 static_cast<SkScalar>(rotatedClipHeight);
580 canvas->scale(scaleX, scaleY);
581
582 // Canvas rotation is done by centering the clip window at the origin, rotating, translating
583 // back so that the top left corner of the clip is at (0, 0).
584 canvas->translate(rotatedClipWidth / 2, rotatedClipHeight / 2);
585 canvas->rotate(toDegrees(display.orientation));
586 canvas->translate(-clipWidth / 2, -clipHeight / 2);
587 canvas->translate(-display.clip.left, -display.clip.top);
588}
589
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500590class AutoSaveRestore {
591public:
592 AutoSaveRestore(SkCanvas* canvas) : mCanvas(canvas) { mSaveCount = canvas->save(); }
593 ~AutoSaveRestore() { restore(); }
594 void replace(SkCanvas* canvas) {
595 mCanvas = canvas;
596 mSaveCount = canvas->save();
597 }
598 void restore() {
599 if (mCanvas) {
600 mCanvas->restoreToCount(mSaveCount);
601 mCanvas = nullptr;
602 }
603 }
604
605private:
606 SkCanvas* mCanvas;
607 int mSaveCount;
608};
609
John Reck67b1e2b2020-08-26 13:17:24 -0700610status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
611 const std::vector<const LayerSettings*>& layers,
612 const sp<GraphicBuffer>& buffer,
613 const bool useFramebufferCache,
614 base::unique_fd&& bufferFence, base::unique_fd* drawFence) {
615 ATRACE_NAME("SkiaGL::drawLayers");
Alec Mouric0aae732021-01-12 13:32:18 -0800616
John Reck67b1e2b2020-08-26 13:17:24 -0700617 std::lock_guard<std::mutex> lock(mRenderingMutex);
618 if (layers.empty()) {
619 ALOGV("Drawing empty layer stack");
620 return NO_ERROR;
621 }
622
623 if (bufferFence.get() >= 0) {
624 // Duplicate the fence for passing to waitFence.
625 base::unique_fd bufferFenceDup(dup(bufferFence.get()));
626 if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
627 ATRACE_NAME("Waiting before draw");
628 sync_wait(bufferFence.get(), -1);
629 }
630 }
631 if (buffer == nullptr) {
632 ALOGE("No output buffer provided. Aborting GPU composition.");
633 return BAD_VALUE;
634 }
635
Ady Abraham193426d2021-02-18 14:01:53 -0800636 validateOutputBufferUsage(buffer);
637
Lucas Dupind508e472020-11-04 04:32:06 +0000638 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800639 auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache;
John Reck67b1e2b2020-08-26 13:17:24 -0700640 AHardwareBuffer_Desc bufferDesc;
641 AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc);
John Reck67b1e2b2020-08-26 13:17:24 -0700642
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800643 std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700644 if (useFramebufferCache) {
Lucas Dupind508e472020-11-04 04:32:06 +0000645 auto iter = cache.find(buffer->getId());
646 if (iter != cache.end()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700647 ALOGV("Cache hit!");
Ana Krulecdfec8f52021-01-13 12:51:47 -0800648 ATRACE_NAME("Cache hit");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800649 surfaceTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700650 }
651 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800652
653 if (surfaceTextureRef == nullptr || surfaceTextureRef->getTexture() == nullptr) {
Ana Krulecdfec8f52021-01-13 12:51:47 -0800654 ATRACE_NAME("Cache miss");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800655 surfaceTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
656 surfaceTextureRef->setTexture(
Derek Sollenberger957f7b32021-03-19 15:42:19 -0400657 new AutoBackendTexture(grContext.get(), buffer->toAHardwareBuffer()));
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800658 if (useFramebufferCache) {
John Reck67b1e2b2020-08-26 13:17:24 -0700659 ALOGD("Adding to cache");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800660 cache.insert({buffer->getId(), surfaceTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700661 }
662 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800663
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500664 const ui::Dataspace dstDataspace =
665 mUseColorManagement ? display.outputDataspace : ui::Dataspace::UNKNOWN;
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500666 sk_sp<SkSurface> dstSurface =
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500667 surfaceTextureRef->getTexture()->getOrCreateSurface(dstDataspace, grContext.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700668
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500669 SkCanvas* dstCanvas = mCapture->tryCapture(dstSurface.get());
670 if (dstCanvas == nullptr) {
Ana Krulec6eab17a2020-12-09 15:52:36 -0800671 ALOGE("Cannot acquire canvas from Skia.");
672 return BAD_VALUE;
673 }
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500674
675 // Find if any layers have requested blur, we'll use that info to decide when to render to an
676 // offscreen buffer and when to render to the native buffer.
677 sk_sp<SkSurface> activeSurface(dstSurface);
678 SkCanvas* canvas = dstCanvas;
Derek Sollenberger76664d62021-02-04 11:13:09 -0500679 SkiaCapture::OffscreenState offscreenCaptureState;
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500680 const LayerSettings* blurCompositionLayer = nullptr;
681 if (mBlurFilter) {
682 bool requiresCompositionLayer = false;
683 for (const auto& layer : layers) {
Derek Sollenberger3134ec32021-02-12 11:26:23 -0500684 if (layer->backgroundBlurRadius > 0 &&
685 layer->backgroundBlurRadius < BlurFilter::kMaxCrossFadeRadius) {
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500686 requiresCompositionLayer = true;
687 }
688 for (auto region : layer->blurRegions) {
689 if (region.blurRadius < BlurFilter::kMaxCrossFadeRadius) {
690 requiresCompositionLayer = true;
691 }
692 }
693 if (requiresCompositionLayer) {
694 activeSurface = dstSurface->makeSurface(dstSurface->imageInfo());
Derek Sollenberger76664d62021-02-04 11:13:09 -0500695 canvas = mCapture->tryOffscreenCapture(activeSurface.get(), &offscreenCaptureState);
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500696 blurCompositionLayer = layer;
697 break;
698 }
699 }
700 }
701
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500702 AutoSaveRestore surfaceAutoSaveRestore(canvas);
Alec Mouri678245d2020-09-30 16:58:23 -0700703 // Clear the entire canvas with a transparent black to prevent ghost images.
704 canvas->clear(SK_ColorTRANSPARENT);
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500705 initCanvas(canvas, display);
Alec Mouric0aae732021-01-12 13:32:18 -0800706
707 // TODO: clearRegion was required for SurfaceView when a buffer is not yet available but the
708 // view is still on-screen. The clear region could be re-specified as a black color layer,
709 // however.
710 if (!display.clearRegion.isEmpty()) {
Derek Sollenberger545ec442021-01-25 10:02:23 -0500711 ATRACE_NAME("ClearRegion");
Alec Mouric0aae732021-01-12 13:32:18 -0800712 size_t numRects = 0;
713 Rect const* rects = display.clearRegion.getArray(&numRects);
714 SkIRect skRects[numRects];
715 for (int i = 0; i < numRects; ++i) {
716 skRects[i] =
717 SkIRect::MakeLTRB(rects[i].left, rects[i].top, rects[i].right, rects[i].bottom);
718 }
719 SkRegion clearRegion;
720 SkPaint paint;
721 sk_sp<SkShader> shader =
722 SkShaders::Color(SkColor4f{.fR = 0., .fG = 0., .fB = 0., .fA = 1.0},
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500723 toSkColorSpace(dstDataspace));
Alec Mouric0aae732021-01-12 13:32:18 -0800724 paint.setShader(shader);
725 clearRegion.setRects(skRects, numRects);
726 canvas->drawRegion(clearRegion, paint);
727 }
728
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500729 // setup color filter if necessary
730 sk_sp<SkColorFilter> displayColorTransform;
731 if (display.colorTransform != mat4()) {
732 displayColorTransform = SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform));
733 }
734
John Reck67b1e2b2020-08-26 13:17:24 -0700735 for (const auto& layer : layers) {
Derek Sollenberger545ec442021-01-25 10:02:23 -0500736 ATRACE_NAME("DrawLayer");
Galia Peychevaf7889b32020-11-25 22:22:40 +0100737
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500738 sk_sp<SkImage> blurInput;
739 if (blurCompositionLayer == layer) {
740 LOG_ALWAYS_FATAL_IF(activeSurface == dstSurface);
741 LOG_ALWAYS_FATAL_IF(canvas == dstCanvas);
742
743 // save a snapshot of the activeSurface to use as input to the blur shaders
744 blurInput = activeSurface->makeImageSnapshot();
745
746 // TODO we could skip this step if we know the blur will cover the entire image
747 // blit the offscreen framebuffer into the destination AHB
748 SkPaint paint;
749 paint.setBlendMode(SkBlendMode::kSrc);
Derek Sollenberger76664d62021-02-04 11:13:09 -0500750 if (CC_UNLIKELY(mCapture->isCaptureRunning())) {
751 uint64_t id = mCapture->endOffscreenCapture(&offscreenCaptureState);
752 dstCanvas->drawAnnotation(SkRect::Make(dstCanvas->imageInfo().dimensions()),
753 String8::format("SurfaceID|%" PRId64, id).c_str(),
754 nullptr);
755 dstCanvas->drawImage(blurInput, 0, 0, SkSamplingOptions(), &paint);
756 } else {
757 activeSurface->draw(dstCanvas, 0, 0, SkSamplingOptions(), &paint);
758 }
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500759
760 // assign dstCanvas to canvas and ensure that the canvas state is up to date
761 canvas = dstCanvas;
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500762 surfaceAutoSaveRestore.replace(canvas);
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500763 initCanvas(canvas, display);
764
765 LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getSaveCount() !=
766 dstSurface->getCanvas()->getSaveCount());
767 LOG_ALWAYS_FATAL_IF(activeSurface->getCanvas()->getTotalMatrix() !=
768 dstSurface->getCanvas()->getTotalMatrix());
769
770 // assign dstSurface to activeSurface
771 activeSurface = dstSurface;
772 }
773
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500774 SkAutoCanvasRestore layerAutoSaveRestore(canvas, true);
Derek Sollenberger76664d62021-02-04 11:13:09 -0500775 if (CC_UNLIKELY(mCapture->isCaptureRunning())) {
Ana Krulec6eab17a2020-12-09 15:52:36 -0800776 // Record the name of the layer if the capture is running.
777 std::stringstream layerSettings;
778 PrintTo(*layer, &layerSettings);
779 // Store the LayerSettings in additional information.
780 canvas->drawAnnotation(SkRect::MakeEmpty(), layer->name.c_str(),
781 SkData::MakeWithCString(layerSettings.str().c_str()));
782 }
Galia Peychevaf7889b32020-11-25 22:22:40 +0100783 // Layers have a local transform that should be applied to them
784 canvas->concat(getSkM44(layer->geometry.positionTransform).asM33());
Galia Peycheva6c460652020-11-03 19:42:42 +0100785
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500786 const auto bounds = getSkRect(layer->geometry.boundaries);
787 if (mBlurFilter && layerHasBlur(layer)) {
788 std::unordered_map<uint32_t, sk_sp<SkImage>> cachedBlurs;
789
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500790 // if multiple layers have blur, then we need to take a snapshot now because
791 // only the lowest layer will have blurImage populated earlier
792 if (!blurInput) {
793 blurInput = activeSurface->makeImageSnapshot();
794 }
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500795 // rect to be blurred in the coordinate space of blurInput
796 const auto blurRect = canvas->getTotalMatrix().mapRect(bounds);
797
Lucas Dupinc3800b82020-10-02 16:24:48 -0700798 if (layer->backgroundBlurRadius > 0) {
799 ATRACE_NAME("BackgroundBlur");
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500800 auto blurredImage =
801 mBlurFilter->generate(grContext.get(), layer->backgroundBlurRadius,
802 blurInput, blurRect);
Galia Peycheva80116e52020-11-06 11:57:25 +0100803
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500804 cachedBlurs[layer->backgroundBlurRadius] = blurredImage;
805
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500806 mBlurFilter->drawBlurRegion(canvas, getBlurRegion(layer), blurRect, blurredImage,
807 blurInput);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700808 }
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500809 for (auto region : layer->blurRegions) {
Galia Peychevaa600b972021-02-19 15:50:12 +0100810 if (cachedBlurs[region.blurRadius] == nullptr) {
Lucas Dupinc3800b82020-10-02 16:24:48 -0700811 ATRACE_NAME("BlurRegion");
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500812 cachedBlurs[region.blurRadius] =
813 mBlurFilter->generate(grContext.get(), region.blurRadius, blurInput,
814 blurRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700815 }
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500816
817 mBlurFilter->drawBlurRegion(canvas, region, blurRect,
818 cachedBlurs[region.blurRadius], blurInput);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700819 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700820 }
821
Derek Sollenberger4c331c82021-02-23 13:09:50 -0500822 // Shadows are assumed to live only on their own layer - it's not valid
823 // to draw the boundary rectangles when there is already a caster shadow
824 // TODO(b/175915334): consider relaxing this restriction to enable more flexible
825 // composition - using a well-defined invalid color is long-term less error-prone.
826 if (layer->shadow.length > 0) {
827 const auto rect = layer->geometry.roundedCornersRadius > 0
828 ? getSkRect(layer->geometry.roundedCornersCrop)
829 : bounds;
Leon Scroggins IIIcf3d95c2021-03-19 13:06:32 -0400830 // This would require a new parameter/flag to SkShadowUtils::DrawShadow
831 LOG_ALWAYS_FATAL_IF(layer->disableBlending, "Cannot disableBlending with a shadow");
Derek Sollenberger4c331c82021-02-23 13:09:50 -0500832 drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow);
833 continue;
834 }
835
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500836 const bool requiresLinearEffect = layer->colorTransform != mat4() ||
837 (mUseColorManagement &&
838 needsToneMapping(layer->sourceDataspace, display.outputDataspace));
839
840 // quick abort from drawing the remaining portion of the layer
Leon Scroggins IIIcf3d95c2021-03-19 13:06:32 -0400841 if (layer->alpha == 0 && !requiresLinearEffect && !layer->disableBlending &&
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500842 (!displayColorTransform || displayColorTransform->isAlphaUnchanged())) {
843 continue;
844 }
845
846 // If we need to map to linear space or color management is disabled, then mark the source
847 // image with the same colorspace as the destination surface so that Skia's color
848 // management is a no-op.
849 const ui::Dataspace layerDataspace = (!mUseColorManagement || requiresLinearEffect)
850 ? dstDataspace
851 : layer->sourceDataspace;
Alec Mouric0aae732021-01-12 13:32:18 -0800852
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500853 SkPaint paint;
John Reck67b1e2b2020-08-26 13:17:24 -0700854 if (layer->source.buffer.buffer) {
855 ATRACE_NAME("DrawImage");
Ady Abraham193426d2021-02-18 14:01:53 -0800856 validateInputBufferUsage(layer->source.buffer.buffer);
John Reck67b1e2b2020-08-26 13:17:24 -0700857 const auto& item = layer->source.buffer;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800858 std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr;
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400859 auto iter = cache.find(item.buffer->getId());
860 if (iter != cache.end()) {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800861 imageTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700862 } else {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800863 imageTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
Derek Sollenberger957f7b32021-03-19 15:42:19 -0400864 imageTextureRef->setTexture(
865 new AutoBackendTexture(grContext.get(), item.buffer->toAHardwareBuffer()));
Derek Sollenbergereb904d42021-03-22 12:58:53 -0400866 cache.insert({item.buffer->getId(), imageTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700867 }
Alec Mouri1a4d0642020-11-13 17:42:01 -0800868
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800869 sk_sp<SkImage> image =
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500870 imageTextureRef->getTexture()->makeImage(layerDataspace,
Alec Mouric0aae732021-01-12 13:32:18 -0800871 item.usePremultipliedAlpha
872 ? kPremul_SkAlphaType
873 : kUnpremul_SkAlphaType,
874 grContext.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700875
876 auto texMatrix = getSkM44(item.textureTransform).asM33();
877 // textureTansform was intended to be passed directly into a shader, so when
878 // building the total matrix with the textureTransform we need to first
879 // normalize it, then apply the textureTransform, then scale back up.
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500880 texMatrix.preScale(1.0f / bounds.width(), 1.0f / bounds.height());
Huihong Luo3a3cf3c2020-12-07 17:05:41 -0800881 texMatrix.postScale(image->width(), image->height());
Alec Mouri678245d2020-09-30 16:58:23 -0700882
Huihong Luo3a3cf3c2020-12-07 17:05:41 -0800883 SkMatrix matrix;
884 if (!texMatrix.invert(&matrix)) {
885 matrix = texMatrix;
Alec Mouri678245d2020-09-30 16:58:23 -0700886 }
Ana Krulecf9a15d92020-12-11 08:35:00 -0800887 // The shader does not respect the translation, so we add it to the texture
888 // transform for the SkImage. This will make sure that the correct layer contents
889 // are drawn in the correct part of the screen.
890 matrix.postTranslate(layer->geometry.boundaries.left, layer->geometry.boundaries.top);
Alec Mouri678245d2020-09-30 16:58:23 -0700891
Ana Krulecb7b28b22020-11-23 14:48:58 -0800892 sk_sp<SkShader> shader;
893
894 if (layer->source.buffer.useTextureFiltering) {
895 shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
896 SkSamplingOptions(
897 {SkFilterMode::kLinear, SkMipmapMode::kNone}),
898 &matrix);
899 } else {
Mike Reed711e1f02020-12-11 13:06:19 -0500900 shader = image->makeShader(SkSamplingOptions(), matrix);
Ana Krulecb7b28b22020-11-23 14:48:58 -0800901 }
Alec Mouri029d1952020-10-12 10:37:08 -0700902
Alec Mouric0aae732021-01-12 13:32:18 -0800903 // Handle opaque images - it's a little nonstandard how we do this.
904 // Fundamentally we need to support SurfaceControl.Builder#setOpaque:
905 // https://developer.android.com/reference/android/view/SurfaceControl.Builder#setOpaque(boolean)
906 // The important language is that when isOpaque is set, opacity is not sampled from the
907 // alpha channel, but blending may still be supported on a transaction via setAlpha. So,
908 // here's the conundrum:
909 // 1. We can't force the SkImage alpha type to kOpaque_SkAlphaType, because it's treated
910 // as an internal hint - composition is undefined when there are alpha bits present.
911 // 2. We can try to lie about the pixel layout, but that only works for RGBA8888
912 // buffers, i.e., treating them as RGBx8888 instead. But we can't do the same for
913 // RGBA1010102 because RGBx1010102 is not supported as a pixel layout for SkImages. It's
914 // also not clear what to use for F16 either, and lying about the pixel layout is a bit
915 // of a hack anyways.
916 // 3. We can't change the blendmode to src, because while this satisfies the requirement
917 // for ignoring the alpha channel, it doesn't quite satisfy the blending requirement
918 // because src always clobbers the destination content.
919 //
920 // So, what we do here instead is an additive blend mode where we compose the input
921 // image with a solid black. This might need to be reassess if this does not support
922 // FP16 incredibly well, but FP16 end-to-end isn't well supported anyway at the moment.
923 if (item.isOpaque) {
924 shader = SkShaders::Blend(SkBlendMode::kPlus, shader,
925 SkShaders::Color(SkColors::kBlack,
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500926 toSkColorSpace(layerDataspace)));
Alec Mouric0aae732021-01-12 13:32:18 -0800927 }
928
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500929 paint.setShader(createRuntimeEffectShader(shader, layer, display,
930 !item.isOpaque && item.usePremultipliedAlpha,
931 requiresLinearEffect));
Ana Krulec1768bd22020-11-23 14:51:31 -0800932 paint.setAlphaf(layer->alpha);
John Reck67b1e2b2020-08-26 13:17:24 -0700933 } else {
934 ATRACE_NAME("DrawColor");
John Reck67b1e2b2020-08-26 13:17:24 -0700935 const auto color = layer->source.solidColor;
Ana Krulec47814212021-01-06 19:00:10 -0800936 sk_sp<SkShader> shader = SkShaders::Color(SkColor4f{.fR = color.r,
937 .fG = color.g,
938 .fB = color.b,
Alec Mouric0aae732021-01-12 13:32:18 -0800939 .fA = layer->alpha},
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500940 toSkColorSpace(layerDataspace));
Ana Krulec47814212021-01-06 19:00:10 -0800941 paint.setShader(createRuntimeEffectShader(shader, layer, display,
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500942 /* undoPremultipliedAlpha */ false,
943 requiresLinearEffect));
John Reck67b1e2b2020-08-26 13:17:24 -0700944 }
Lucas Dupin21f348e2020-09-16 17:31:26 -0700945
Leon Scroggins IIIcf3d95c2021-03-19 13:06:32 -0400946 if (layer->disableBlending) {
947 paint.setBlendMode(SkBlendMode::kSrc);
948 }
949
Derek Sollenbergere2fe78c2021-02-23 13:22:54 -0500950 paint.setColorFilter(displayColorTransform);
Alec Mourib34f0b72020-10-02 13:18:34 -0700951
Derek Sollenberger4c331c82021-02-23 13:09:50 -0500952 if (layer->geometry.roundedCornersRadius > 0) {
953 paint.setAntiAlias(true);
954 canvas->drawRRect(getRoundedRect(layer), paint);
Alec Mouribd17b3b2020-12-17 11:08:30 -0800955 } else {
Derek Sollenbergerecb21462021-01-29 16:53:49 -0500956 canvas->drawRect(bounds, paint);
Lucas Dupin3f11e922020-09-22 17:31:04 -0700957 }
John Reck67b1e2b2020-08-26 13:17:24 -0700958 }
Derek Sollenberger7c42bef2021-02-23 13:01:39 -0500959 surfaceAutoSaveRestore.restore();
Alec Mouric0aae732021-01-12 13:32:18 -0800960 mCapture->endCapture();
John Reck67b1e2b2020-08-26 13:17:24 -0700961 {
962 ATRACE_NAME("flush surface");
Derek Sollenberger3f77aa42021-02-04 11:06:34 -0500963 LOG_ALWAYS_FATAL_IF(activeSurface != dstSurface);
964 activeSurface->flush();
John Reck67b1e2b2020-08-26 13:17:24 -0700965 }
966
967 if (drawFence != nullptr) {
968 *drawFence = flush();
969 }
970
971 // If flush failed or we don't support native fences, we need to force the
972 // gl command stream to be executed.
973 bool requireSync = drawFence == nullptr || drawFence->get() < 0;
974 if (requireSync) {
975 ATRACE_BEGIN("Submit(sync=true)");
976 } else {
977 ATRACE_BEGIN("Submit(sync=false)");
978 }
Lucas Dupind508e472020-11-04 04:32:06 +0000979 bool success = grContext->submit(requireSync);
John Reck67b1e2b2020-08-26 13:17:24 -0700980 ATRACE_END();
981 if (!success) {
982 ALOGE("Failed to flush RenderEngine commands");
983 // Chances are, something illegal happened (either the caller passed
984 // us bad parameters, or we messed up our shader generation).
985 return INVALID_OPERATION;
986 }
987
988 // checkErrors();
989 return NO_ERROR;
990}
991
Lucas Dupin3f11e922020-09-22 17:31:04 -0700992inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) {
993 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
994}
995
996inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) {
997 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
998}
999
Lucas Dupin21f348e2020-09-16 17:31:26 -07001000inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) {
Ana Krulecf9a15d92020-12-11 08:35:00 -08001001 const auto rect = getSkRect(layer->geometry.roundedCornersCrop);
Lucas Dupin21f348e2020-09-16 17:31:26 -07001002 const auto cornerRadius = layer->geometry.roundedCornersRadius;
1003 return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius);
1004}
1005
Galia Peycheva80116e52020-11-06 11:57:25 +01001006inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) {
1007 const auto rect = getSkRect(layer->geometry.boundaries);
1008 const auto cornersRadius = layer->geometry.roundedCornersRadius;
1009 return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius),
1010 .cornerRadiusTL = cornersRadius,
1011 .cornerRadiusTR = cornersRadius,
1012 .cornerRadiusBL = cornersRadius,
1013 .cornerRadiusBR = cornersRadius,
1014 .alpha = 1,
1015 .left = static_cast<int>(rect.fLeft),
1016 .top = static_cast<int>(rect.fTop),
1017 .right = static_cast<int>(rect.fRight),
1018 .bottom = static_cast<int>(rect.fBottom)};
1019}
1020
Derek Sollenbergerecb21462021-01-29 16:53:49 -05001021inline bool SkiaGLRenderEngine::layerHasBlur(const LayerSettings* layer) {
1022 return layer->backgroundBlurRadius > 0 || layer->blurRegions.size();
1023}
1024
Lucas Dupin3f11e922020-09-22 17:31:04 -07001025inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) {
1026 return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255);
1027}
1028
Lucas Dupinbb1a1d42020-09-18 15:17:02 -07001029inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) {
1030 return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
1031 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
1032 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
1033 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
1034}
1035
Lucas Dupin3f11e922020-09-22 17:31:04 -07001036inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) {
1037 return SkPoint3::Make(vector.x, vector.y, vector.z);
1038}
1039
John Reck67b1e2b2020-08-26 13:17:24 -07001040size_t SkiaGLRenderEngine::getMaxTextureSize() const {
1041 return mGrContext->maxTextureSize();
1042}
1043
1044size_t SkiaGLRenderEngine::getMaxViewportDims() const {
1045 return mGrContext->maxRenderTargetSize();
1046}
1047
Lucas Dupin3f11e922020-09-22 17:31:04 -07001048void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius,
1049 const ShadowSettings& settings) {
1050 ATRACE_CALL();
1051 const float casterZ = settings.length / 2.0f;
1052 const auto shadowShape = cornerRadius > 0
1053 ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius))
1054 : SkPath::Rect(casterRect);
1055 const auto flags =
1056 settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag;
1057
1058 SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ),
1059 getSkPoint3(settings.lightPos), settings.lightRadius,
1060 getSkColor(settings.ambientColor), getSkColor(settings.spotColor),
1061 flags);
1062}
1063
John Reck67b1e2b2020-08-26 13:17:24 -07001064EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
Alec Mourid6f09462020-12-07 11:18:17 -08001065 EGLContext shareContext,
1066 std::optional<ContextPriority> contextPriority,
John Reck67b1e2b2020-08-26 13:17:24 -07001067 Protection protection) {
1068 EGLint renderableType = 0;
1069 if (config == EGL_NO_CONFIG_KHR) {
1070 renderableType = EGL_OPENGL_ES3_BIT;
1071 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
1072 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
1073 }
1074 EGLint contextClientVersion = 0;
1075 if (renderableType & EGL_OPENGL_ES3_BIT) {
1076 contextClientVersion = 3;
1077 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
1078 contextClientVersion = 2;
1079 } else if (renderableType & EGL_OPENGL_ES_BIT) {
1080 contextClientVersion = 1;
1081 } else {
1082 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
1083 }
1084
1085 std::vector<EGLint> contextAttributes;
1086 contextAttributes.reserve(7);
1087 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
1088 contextAttributes.push_back(contextClientVersion);
Alec Mourid6f09462020-12-07 11:18:17 -08001089 if (contextPriority) {
John Reck67b1e2b2020-08-26 13:17:24 -07001090 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
Alec Mourid6f09462020-12-07 11:18:17 -08001091 switch (*contextPriority) {
1092 case ContextPriority::REALTIME:
1093 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
1094 break;
1095 case ContextPriority::MEDIUM:
1096 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
1097 break;
1098 case ContextPriority::LOW:
1099 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
1100 break;
1101 case ContextPriority::HIGH:
1102 default:
1103 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
1104 break;
1105 }
John Reck67b1e2b2020-08-26 13:17:24 -07001106 }
1107 if (protection == Protection::PROTECTED) {
1108 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1109 contextAttributes.push_back(EGL_TRUE);
1110 }
1111 contextAttributes.push_back(EGL_NONE);
1112
1113 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
1114
1115 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
1116 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
1117 // EGL_NO_CONTEXT so that we can abort.
1118 if (config != EGL_NO_CONFIG_KHR) {
1119 return context;
1120 }
1121 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
1122 // should try to fall back to GLES 2.
1123 contextAttributes[1] = 2;
1124 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
1125 }
1126
1127 return context;
1128}
1129
Alec Mourid6f09462020-12-07 11:18:17 -08001130std::optional<RenderEngine::ContextPriority> SkiaGLRenderEngine::createContextPriority(
1131 const RenderEngineCreationArgs& args) {
1132 if (!gl::GLExtensions::getInstance().hasContextPriority()) {
1133 return std::nullopt;
1134 }
1135
1136 switch (args.contextPriority) {
1137 case RenderEngine::ContextPriority::REALTIME:
1138 if (gl::GLExtensions::getInstance().hasRealtimePriority()) {
1139 return RenderEngine::ContextPriority::REALTIME;
1140 } else {
1141 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
1142 return RenderEngine::ContextPriority::HIGH;
1143 }
1144 case RenderEngine::ContextPriority::HIGH:
1145 case RenderEngine::ContextPriority::MEDIUM:
1146 case RenderEngine::ContextPriority::LOW:
1147 return args.contextPriority;
1148 default:
1149 return std::nullopt;
1150 }
1151}
1152
John Reck67b1e2b2020-08-26 13:17:24 -07001153EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
1154 EGLConfig config, int hwcFormat,
1155 Protection protection) {
1156 EGLConfig placeholderConfig = config;
1157 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
1158 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
1159 }
1160 std::vector<EGLint> attributes;
1161 attributes.reserve(7);
1162 attributes.push_back(EGL_WIDTH);
1163 attributes.push_back(1);
1164 attributes.push_back(EGL_HEIGHT);
1165 attributes.push_back(1);
1166 if (protection == Protection::PROTECTED) {
1167 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1168 attributes.push_back(EGL_TRUE);
1169 }
1170 attributes.push_back(EGL_NONE);
1171
1172 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
1173}
1174
Marin Shalamanovcea12ef2021-03-15 17:00:51 +01001175void SkiaGLRenderEngine::cleanFramebufferCache() {
1176 // TODO(b/180767535) Remove this method and use b/180767535 instead, which would allow
1177 // SF to control texture lifecycle more tightly rather than through custom hooks into RE.
1178 std::lock_guard<std::mutex> lock(mRenderingMutex);
1179 mRuntimeEffects.clear();
1180 mProtectedTextureCache.clear();
1181 mTextureCache.clear();
1182}
John Reck67b1e2b2020-08-26 13:17:24 -07001183
Alec Mourid6f09462020-12-07 11:18:17 -08001184int SkiaGLRenderEngine::getContextPriority() {
1185 int value;
1186 eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
1187 return value;
1188}
1189
Derek Sollenbergerc4a05e12021-03-24 16:45:20 -04001190void SkiaGLRenderEngine::onPrimaryDisplaySizeChanged(ui::Size size) {
1191 // This cache multiplier was selected based on review of cache sizes relative
1192 // to the screen resolution. Looking at the worst case memory needed by blur (~1.5x),
1193 // shadows (~1x), and general data structures (e.g. vertex buffers) we selected this as a
1194 // conservative default based on that analysis.
1195 const float SURFACE_SIZE_MULTIPLIER = 3.5f * bytesPerPixel(mDefaultPixelFormat);
1196 const int maxResourceBytes = size.width * size.height * SURFACE_SIZE_MULTIPLIER;
1197
1198 // start by resizing the current context
1199 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
1200 grContext->setResourceCacheLimit(maxResourceBytes);
1201
1202 // if it is possible to switch contexts then we will resize the other context
1203 if (useProtectedContext(!mInProtectedContext)) {
1204 grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
1205 grContext->setResourceCacheLimit(maxResourceBytes);
1206 // reset back to the initial context that was active when this method was called
1207 useProtectedContext(!mInProtectedContext);
1208 }
1209}
1210
Ana Krulec1d12b3b2021-01-27 16:49:51 -08001211void SkiaGLRenderEngine::dump(std::string& result) {
1212 const gl::GLExtensions& extensions = gl::GLExtensions::getInstance();
1213
1214 StringAppendF(&result, "\n ------------RE-----------------\n");
1215 StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
1216 StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
1217 StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
1218 extensions.getVersion());
1219 StringAppendF(&result, "%s\n", extensions.getExtensions());
1220 StringAppendF(&result, "RenderEngine supports protected context: %d\n",
1221 supportsProtectedContent());
1222 StringAppendF(&result, "RenderEngine is in protected context: %d\n", mInProtectedContext);
Leon Scroggins III9f3072c2021-03-22 10:42:47 -04001223 StringAppendF(&result, "RenderEngine shaders cached since last dump/primeCache: %d\n",
1224 mSkSLCacheMonitor.shadersCachedSinceLastCall());
Ana Krulec1d12b3b2021-01-27 16:49:51 -08001225
1226 {
1227 std::lock_guard<std::mutex> lock(mRenderingMutex);
1228 StringAppendF(&result, "RenderEngine texture cache size: %zu\n", mTextureCache.size());
1229 StringAppendF(&result, "Dumping buffer ids...\n");
1230 // TODO(178539829): It would be nice to know which layer these are coming from and what
1231 // the texture sizes are.
1232 for (const auto& [id, unused] : mTextureCache) {
1233 StringAppendF(&result, "- 0x%" PRIx64 "\n", id);
1234 }
1235 StringAppendF(&result, "\n");
1236 StringAppendF(&result, "RenderEngine protected texture cache size: %zu\n",
1237 mProtectedTextureCache.size());
1238 StringAppendF(&result, "Dumping buffer ids...\n");
1239 for (const auto& [id, unused] : mProtectedTextureCache) {
1240 StringAppendF(&result, "- 0x%" PRIx64 "\n", id);
1241 }
1242 StringAppendF(&result, "\n");
1243 StringAppendF(&result, "RenderEngine runtime effects: %zu\n", mRuntimeEffects.size());
1244 for (const auto& [linearEffect, unused] : mRuntimeEffects) {
1245 StringAppendF(&result, "- inputDataspace: %s\n",
1246 dataspaceDetails(
1247 static_cast<android_dataspace>(linearEffect.inputDataspace))
1248 .c_str());
1249 StringAppendF(&result, "- outputDataspace: %s\n",
1250 dataspaceDetails(
1251 static_cast<android_dataspace>(linearEffect.outputDataspace))
1252 .c_str());
1253 StringAppendF(&result, "undoPremultipliedAlpha: %s\n",
1254 linearEffect.undoPremultipliedAlpha ? "true" : "false");
1255 }
1256 }
1257 StringAppendF(&result, "\n");
1258}
1259
John Reck67b1e2b2020-08-26 13:17:24 -07001260} // namespace skia
1261} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +01001262} // namespace android