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