blob: 579260bba5445b898f0e2e737641de99974aed27 [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 Dupind508e472020-11-04 04:32:06 +0000255 std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext,
256 protectedPlaceholder);
John Reck67b1e2b2020-08-26 13:17:24 -0700257
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,
Lucas Dupind508e472020-11-04 04:32:06 +0000309 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700310 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri081be4c2020-09-16 10:24:47 -0700311 : mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700312 mEGLContext(ctxt),
313 mPlaceholderSurface(placeholder),
314 mProtectedEGLContext(protectedContext),
Alec Mourib5777452020-09-28 11:32:42 -0700315 mProtectedPlaceholderSurface(protectedPlaceholder),
316 mUseColorManagement(args.useColorManagement) {
John Reck67b1e2b2020-08-26 13:17:24 -0700317 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
318 LOG_ALWAYS_FATAL_IF(!glInterface.get());
319
320 GrContextOptions options;
321 options.fPreferExternalImagesOverES3 = true;
322 options.fDisableDistanceFieldPaths = true;
Lucas Dupind508e472020-11-04 04:32:06 +0000323 mGrContext = GrDirectContext::MakeGL(glInterface, options);
324 if (useProtectedContext(true)) {
325 mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options);
326 useProtectedContext(false);
327 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700328
329 if (args.supportsBackgroundBlur) {
330 mBlurFilter = new BlurFilter();
331 }
John Reck67b1e2b2020-08-26 13:17:24 -0700332}
333
Lucas Dupind508e472020-11-04 04:32:06 +0000334bool SkiaGLRenderEngine::supportsProtectedContent() const {
335 return mProtectedEGLContext != EGL_NO_CONTEXT;
336}
337
338bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) {
339 if (useProtectedContext == mInProtectedContext) {
340 return true;
341 }
342 if (useProtectedContext && supportsProtectedContent()) {
343 return false;
344 }
345 const EGLSurface surface =
346 useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface;
347 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
348 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
349 if (success) {
350 mInProtectedContext = useProtectedContext;
351 }
352 return success;
353}
354
John Reck67b1e2b2020-08-26 13:17:24 -0700355base::unique_fd SkiaGLRenderEngine::flush() {
356 ATRACE_CALL();
357 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
358 return base::unique_fd();
359 }
360
361 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
362 if (sync == EGL_NO_SYNC_KHR) {
363 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
364 return base::unique_fd();
365 }
366
367 // native fence fd will not be populated until flush() is done.
368 glFlush();
369
370 // get the fence fd
371 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
372 eglDestroySyncKHR(mEGLDisplay, sync);
373 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
374 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
375 }
376
377 return fenceFd;
378}
379
380bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) {
381 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
382 !gl::GLExtensions::getInstance().hasWaitSync()) {
383 return false;
384 }
385
386 // release the fd and transfer the ownership to EGLSync
387 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
388 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
389 if (sync == EGL_NO_SYNC_KHR) {
390 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
391 return false;
392 }
393
394 // XXX: The spec draft is inconsistent as to whether this should return an
395 // EGLint or void. Ignore the return value for now, as it's not strictly
396 // needed.
397 eglWaitSyncKHR(mEGLDisplay, sync, 0);
398 EGLint error = eglGetError();
399 eglDestroySyncKHR(mEGLDisplay, sync);
400 if (error != EGL_SUCCESS) {
401 ALOGE("failed to wait for EGL native fence sync: %#x", error);
402 return false;
403 }
404
405 return true;
406}
407
408static bool hasUsage(const AHardwareBuffer_Desc& desc, uint64_t usage) {
409 return !!(desc.usage & usage);
410}
411
Alec Mouri678245d2020-09-30 16:58:23 -0700412static float toDegrees(uint32_t transform) {
413 switch (transform) {
414 case ui::Transform::ROT_90:
415 return 90.0;
416 case ui::Transform::ROT_180:
417 return 180.0;
418 case ui::Transform::ROT_270:
419 return 270.0;
420 default:
421 return 0.0;
422 }
423}
424
Alec Mourib34f0b72020-10-02 13:18:34 -0700425static SkColorMatrix toSkColorMatrix(const mat4& matrix) {
426 return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1],
427 matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2],
428 matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3],
429 matrix[3][3], 0);
430}
431
Alec Mouri029d1952020-10-12 10:37:08 -0700432static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) {
433 int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK;
434 int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK;
435
436 // Treat unsupported dataspaces as srgb
437 if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
438 destTransfer != HAL_DATASPACE_TRANSFER_HLG &&
439 destTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
440 destTransfer = HAL_DATASPACE_TRANSFER_SRGB;
441 }
442
443 if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
444 sourceTransfer != HAL_DATASPACE_TRANSFER_HLG &&
445 sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
446 sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB;
447 }
448
449 const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
450 const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB;
451 const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
452 const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB;
453
454 return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) &&
455 sourceTransfer != destTransfer;
456}
457
John Reck67b1e2b2020-08-26 13:17:24 -0700458void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) {
459 std::lock_guard<std::mutex> lock(mRenderingMutex);
460 mImageCache.erase(bufferId);
461}
462
463status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
464 const std::vector<const LayerSettings*>& layers,
465 const sp<GraphicBuffer>& buffer,
466 const bool useFramebufferCache,
467 base::unique_fd&& bufferFence, base::unique_fd* drawFence) {
468 ATRACE_NAME("SkiaGL::drawLayers");
469 std::lock_guard<std::mutex> lock(mRenderingMutex);
470 if (layers.empty()) {
471 ALOGV("Drawing empty layer stack");
472 return NO_ERROR;
473 }
474
475 if (bufferFence.get() >= 0) {
476 // Duplicate the fence for passing to waitFence.
477 base::unique_fd bufferFenceDup(dup(bufferFence.get()));
478 if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
479 ATRACE_NAME("Waiting before draw");
480 sync_wait(bufferFence.get(), -1);
481 }
482 }
483 if (buffer == nullptr) {
484 ALOGE("No output buffer provided. Aborting GPU composition.");
485 return BAD_VALUE;
486 }
487
Lucas Dupind508e472020-11-04 04:32:06 +0000488 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
Derek Sollenberger4bf67f52020-11-19 16:41:23 -0500489 auto& cache = mInProtectedContext ? mProtectedSurfaceCache : mSurfaceCache;
John Reck67b1e2b2020-08-26 13:17:24 -0700490 AHardwareBuffer_Desc bufferDesc;
491 AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc);
John Reck67b1e2b2020-08-26 13:17:24 -0700492 LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE),
493 "missing usage");
494
495 sk_sp<SkSurface> surface;
496 if (useFramebufferCache) {
Lucas Dupind508e472020-11-04 04:32:06 +0000497 auto iter = cache.find(buffer->getId());
498 if (iter != cache.end()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700499 ALOGV("Cache hit!");
500 surface = iter->second;
501 }
502 }
503 if (!surface) {
Lucas Dupind508e472020-11-04 04:32:06 +0000504 surface = SkSurface::MakeFromAHardwareBuffer(grContext.get(), buffer->toAHardwareBuffer(),
John Reck67b1e2b2020-08-26 13:17:24 -0700505 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
Alec Mourib5777452020-09-28 11:32:42 -0700506 mUseColorManagement
507 ? toColorSpace(display.outputDataspace)
508 : SkColorSpace::MakeSRGB(),
509 nullptr);
John Reck67b1e2b2020-08-26 13:17:24 -0700510 if (useFramebufferCache && surface) {
511 ALOGD("Adding to cache");
Lucas Dupind508e472020-11-04 04:32:06 +0000512 cache.insert({buffer->getId(), surface});
John Reck67b1e2b2020-08-26 13:17:24 -0700513 }
514 }
515 if (!surface) {
516 ALOGE("Failed to make surface");
517 return BAD_VALUE;
518 }
Alec Mouri678245d2020-09-30 16:58:23 -0700519
John Reck67b1e2b2020-08-26 13:17:24 -0700520 auto canvas = surface->getCanvas();
Alec Mouri678245d2020-09-30 16:58:23 -0700521 // Clear the entire canvas with a transparent black to prevent ghost images.
522 canvas->clear(SK_ColorTRANSPARENT);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700523 canvas->save();
John Reck67b1e2b2020-08-26 13:17:24 -0700524
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700525 // Before doing any drawing, let's make sure that we'll start at the origin of the display.
526 // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual
527 // displays might have different scaling when compared to the physical screen.
Alec Mouri678245d2020-09-30 16:58:23 -0700528
529 canvas->clipRect(getSkRect(display.physicalDisplay));
Galia Peycheva6c460652020-11-03 19:42:42 +0100530 SkMatrix screenTransform;
531 screenTransform.setTranslate(display.physicalDisplay.left, display.physicalDisplay.top);
Alec Mouri678245d2020-09-30 16:58:23 -0700532
533 const auto clipWidth = display.clip.width();
534 const auto clipHeight = display.clip.height();
535 auto rotatedClipWidth = clipWidth;
536 auto rotatedClipHeight = clipHeight;
537 // Scale is contingent on the rotation result.
538 if (display.orientation & ui::Transform::ROT_90) {
539 std::swap(rotatedClipWidth, rotatedClipHeight);
540 }
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700541 const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700542 static_cast<SkScalar>(rotatedClipWidth);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700543 const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700544 static_cast<SkScalar>(rotatedClipHeight);
Galia Peycheva6c460652020-11-03 19:42:42 +0100545 screenTransform.preScale(scaleX, scaleY);
Alec Mouri678245d2020-09-30 16:58:23 -0700546
547 // Canvas rotation is done by centering the clip window at the origin, rotating, translating
548 // back so that the top left corner of the clip is at (0, 0).
Galia Peycheva6c460652020-11-03 19:42:42 +0100549 screenTransform.preTranslate(rotatedClipWidth / 2, rotatedClipHeight / 2);
550 screenTransform.preRotate(toDegrees(display.orientation));
551 screenTransform.preTranslate(-clipWidth / 2, -clipHeight / 2);
552 screenTransform.preTranslate(-display.clip.left, -display.clip.top);
John Reck67b1e2b2020-08-26 13:17:24 -0700553 for (const auto& layer : layers) {
Galia Peycheva6c460652020-11-03 19:42:42 +0100554 const SkMatrix drawTransform = getDrawTransform(layer, screenTransform);
555
Lucas Dupin21f348e2020-09-16 17:31:26 -0700556 SkPaint paint;
557 const auto& bounds = layer->geometry.boundaries;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700558 const auto dest = getSkRect(bounds);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700559 std::unordered_map<uint32_t, sk_sp<SkSurface>> cachedBlurs;
Lucas Dupin21f348e2020-09-16 17:31:26 -0700560
Lucas Dupinc3800b82020-10-02 16:24:48 -0700561 if (mBlurFilter) {
Galia Peycheva6c460652020-11-03 19:42:42 +0100562 const auto layerRect = drawTransform.mapRect(dest);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700563 if (layer->backgroundBlurRadius > 0) {
564 ATRACE_NAME("BackgroundBlur");
Galia Peycheva80116e52020-11-06 11:57:25 +0100565 auto blurredSurface = mBlurFilter->generate(canvas, surface,
566 layer->backgroundBlurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700567 cachedBlurs[layer->backgroundBlurRadius] = blurredSurface;
Galia Peycheva80116e52020-11-06 11:57:25 +0100568
569 drawBlurRegion(canvas, getBlurRegion(layer), drawTransform, blurredSurface);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700570 }
571 if (layer->blurRegions.size() > 0) {
572 for (auto region : layer->blurRegions) {
573 if (cachedBlurs[region.blurRadius]) {
574 continue;
575 }
576 ATRACE_NAME("BlurRegion");
Galia Peycheva6c460652020-11-03 19:42:42 +0100577 auto blurredSurface =
578 mBlurFilter->generate(canvas, surface, region.blurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700579 cachedBlurs[region.blurRadius] = blurredSurface;
580 }
581 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700582 }
583
John Reck67b1e2b2020-08-26 13:17:24 -0700584 if (layer->source.buffer.buffer) {
585 ATRACE_NAME("DrawImage");
586 const auto& item = layer->source.buffer;
Alec Mouri678245d2020-09-30 16:58:23 -0700587 const auto bufferWidth = item.buffer->getBounds().width();
588 const auto bufferHeight = item.buffer->getBounds().height();
John Reck67b1e2b2020-08-26 13:17:24 -0700589 sk_sp<SkImage> image;
590 auto iter = mImageCache.find(item.buffer->getId());
591 if (iter != mImageCache.end()) {
592 image = iter->second;
593 } else {
Alec Mouri029d1952020-10-12 10:37:08 -0700594 image = SkImage::MakeFromAHardwareBuffer(
595 item.buffer->toAHardwareBuffer(),
596 item.isOpaque ? kOpaque_SkAlphaType
597 : (item.usePremultipliedAlpha ? kPremul_SkAlphaType
598 : kUnpremul_SkAlphaType),
599 mUseColorManagement
600 ? (needsToneMapping(layer->sourceDataspace, display.outputDataspace)
601 // If we need to map to linear space, then
602 // mark the source image with the same
603 // colorspace as the destination surface so
604 // that Skia's color management is a no-op.
605 ? toColorSpace(display.outputDataspace)
606 : toColorSpace(layer->sourceDataspace))
607 : SkColorSpace::MakeSRGB());
John Reck67b1e2b2020-08-26 13:17:24 -0700608 mImageCache.insert({item.buffer->getId(), image});
609 }
Lucas Dupin21f348e2020-09-16 17:31:26 -0700610
611 SkMatrix matrix;
612 if (layer->geometry.roundedCornersRadius > 0) {
613 const auto roundedRect = getRoundedRect(layer);
614 matrix.setTranslate(roundedRect.getBounds().left() - dest.left(),
615 roundedRect.getBounds().top() - dest.top());
616 } else {
617 matrix.setIdentity();
618 }
Alec Mouri678245d2020-09-30 16:58:23 -0700619
620 auto texMatrix = getSkM44(item.textureTransform).asM33();
Huihong Luo42b86c12020-10-29 13:00:13 -0700621
622 // b/171404534, scale to fix the layer
623 matrix.postScale(bounds.getWidth() / bufferWidth, bounds.getHeight() / bufferHeight);
624
Alec Mouri678245d2020-09-30 16:58:23 -0700625 // textureTansform was intended to be passed directly into a shader, so when
626 // building the total matrix with the textureTransform we need to first
627 // normalize it, then apply the textureTransform, then scale back up.
628 matrix.postScale(1.0f / bufferWidth, 1.0f / bufferHeight);
629
630 auto rotatedBufferWidth = bufferWidth;
631 auto rotatedBufferHeight = bufferHeight;
632
633 // Swap the buffer width and height if we're rotating, so that we
634 // scale back up by the correct factors post-rotation.
635 if (texMatrix.getSkewX() <= -0.5f || texMatrix.getSkewX() >= 0.5f) {
636 std::swap(rotatedBufferWidth, rotatedBufferHeight);
637 // TODO: clean this up.
638 // GLESRenderEngine specifies its texture coordinates in
639 // CW orientation under OpenGL conventions, when they probably should have
640 // been CCW instead. The net result is that orientation
641 // transforms are applied in the reverse
642 // direction to render the correct result, because SurfaceFlinger uses the inverse
643 // of the display transform to correct for that. But this means that
644 // the tex transform passed by SkiaGLRenderEngine will rotate
645 // individual layers in the reverse orientation. Hack around it
646 // by injected a 180 degree rotation, but ultimately this is
647 // a bug in how SurfaceFlinger invokes the RenderEngine
648 // interface, so the proper fix should live there, and GLESRenderEngine
649 // should be fixed accordingly.
650 matrix.postRotate(180, 0.5, 0.5);
651 }
652
653 matrix.postConcat(texMatrix);
654 matrix.postScale(rotatedBufferWidth, rotatedBufferHeight);
Ana Krulecb7b28b22020-11-23 14:48:58 -0800655 sk_sp<SkShader> shader;
656
657 if (layer->source.buffer.useTextureFiltering) {
658 shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
659 SkSamplingOptions(
660 {SkFilterMode::kLinear, SkMipmapMode::kNone}),
661 &matrix);
662 } else {
663 shader = image->makeShader(matrix);
664 }
Alec Mouri029d1952020-10-12 10:37:08 -0700665
666 if (mUseColorManagement &&
667 needsToneMapping(layer->sourceDataspace, display.outputDataspace)) {
668 LinearEffect effect = LinearEffect{.inputDataspace = layer->sourceDataspace,
669 .outputDataspace = display.outputDataspace,
670 .undoPremultipliedAlpha = !item.isOpaque &&
671 item.usePremultipliedAlpha};
672 sk_sp<SkRuntimeEffect> runtimeEffect = buildRuntimeEffect(effect);
673 paint.setShader(createLinearEffectShader(shader, effect, runtimeEffect,
674 display.maxLuminance,
675 layer->source.buffer.maxMasteringLuminance,
676 layer->source.buffer.maxContentLuminance));
677 } else {
678 paint.setShader(shader);
679 }
Ana Krulec1768bd22020-11-23 14:51:31 -0800680 // Make sure to take into the account the alpha set on the layer.
681 paint.setAlphaf(layer->alpha);
John Reck67b1e2b2020-08-26 13:17:24 -0700682 } else {
683 ATRACE_NAME("DrawColor");
John Reck67b1e2b2020-08-26 13:17:24 -0700684 const auto color = layer->source.solidColor;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700685 paint.setShader(SkShaders::Color(SkColor4f{.fR = color.r,
686 .fG = color.g,
687 .fB = color.b,
688 layer->alpha},
689 nullptr));
John Reck67b1e2b2020-08-26 13:17:24 -0700690 }
Lucas Dupin21f348e2020-09-16 17:31:26 -0700691
Alec Mourib34f0b72020-10-02 13:18:34 -0700692 paint.setColorFilter(SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform)));
693
Lucas Dupinc3800b82020-10-02 16:24:48 -0700694 for (const auto effectRegion : layer->blurRegions) {
Galia Peycheva80116e52020-11-06 11:57:25 +0100695 drawBlurRegion(canvas, effectRegion, drawTransform,
696 cachedBlurs[effectRegion.blurRadius]);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700697 }
698
Galia Peycheva80116e52020-11-06 11:57:25 +0100699 canvas->save();
700 canvas->concat(drawTransform);
Lucas Dupin3f11e922020-09-22 17:31:04 -0700701 if (layer->shadow.length > 0) {
702 const auto rect = layer->geometry.roundedCornersRadius > 0
703 ? getSkRect(layer->geometry.roundedCornersCrop)
704 : dest;
705 drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow);
706 }
707
Lucas Dupin21f348e2020-09-16 17:31:26 -0700708 if (layer->geometry.roundedCornersRadius > 0) {
709 canvas->drawRRect(getRoundedRect(layer), paint);
710 } else {
711 canvas->drawRect(dest, paint);
712 }
Lucas Dupinc3800b82020-10-02 16:24:48 -0700713
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700714 canvas->restore();
John Reck67b1e2b2020-08-26 13:17:24 -0700715 }
716 {
717 ATRACE_NAME("flush surface");
718 surface->flush();
719 }
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700720 canvas->restore();
John Reck67b1e2b2020-08-26 13:17:24 -0700721
722 if (drawFence != nullptr) {
723 *drawFence = flush();
724 }
725
726 // If flush failed or we don't support native fences, we need to force the
727 // gl command stream to be executed.
728 bool requireSync = drawFence == nullptr || drawFence->get() < 0;
729 if (requireSync) {
730 ATRACE_BEGIN("Submit(sync=true)");
731 } else {
732 ATRACE_BEGIN("Submit(sync=false)");
733 }
Lucas Dupind508e472020-11-04 04:32:06 +0000734 bool success = grContext->submit(requireSync);
John Reck67b1e2b2020-08-26 13:17:24 -0700735 ATRACE_END();
736 if (!success) {
737 ALOGE("Failed to flush RenderEngine commands");
738 // Chances are, something illegal happened (either the caller passed
739 // us bad parameters, or we messed up our shader generation).
740 return INVALID_OPERATION;
741 }
742
743 // checkErrors();
744 return NO_ERROR;
745}
746
Lucas Dupin3f11e922020-09-22 17:31:04 -0700747inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) {
748 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
749}
750
751inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) {
752 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
753}
754
Lucas Dupin21f348e2020-09-16 17:31:26 -0700755inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) {
Lucas Dupin3f11e922020-09-22 17:31:04 -0700756 const auto rect = getSkRect(layer->geometry.roundedCornersCrop);
Lucas Dupin21f348e2020-09-16 17:31:26 -0700757 const auto cornerRadius = layer->geometry.roundedCornersRadius;
758 return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius);
759}
760
Galia Peycheva80116e52020-11-06 11:57:25 +0100761inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) {
762 const auto rect = getSkRect(layer->geometry.boundaries);
763 const auto cornersRadius = layer->geometry.roundedCornersRadius;
764 return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius),
765 .cornerRadiusTL = cornersRadius,
766 .cornerRadiusTR = cornersRadius,
767 .cornerRadiusBL = cornersRadius,
768 .cornerRadiusBR = cornersRadius,
769 .alpha = 1,
770 .left = static_cast<int>(rect.fLeft),
771 .top = static_cast<int>(rect.fTop),
772 .right = static_cast<int>(rect.fRight),
773 .bottom = static_cast<int>(rect.fBottom)};
774}
775
Lucas Dupin3f11e922020-09-22 17:31:04 -0700776inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) {
777 return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255);
778}
779
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700780inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) {
781 return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
782 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
783 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
784 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
785}
786
Galia Peycheva6c460652020-11-03 19:42:42 +0100787inline SkMatrix SkiaGLRenderEngine::getDrawTransform(const LayerSettings* layer,
788 const SkMatrix& screenTransform) {
789 // Layers have a local transform matrix that should be applied to them.
790 const auto layerTransform = getSkM44(layer->geometry.positionTransform).asM33();
791 return SkMatrix::Concat(screenTransform, layerTransform);
792}
793
Lucas Dupin3f11e922020-09-22 17:31:04 -0700794inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) {
795 return SkPoint3::Make(vector.x, vector.y, vector.z);
796}
797
John Reck67b1e2b2020-08-26 13:17:24 -0700798size_t SkiaGLRenderEngine::getMaxTextureSize() const {
799 return mGrContext->maxTextureSize();
800}
801
802size_t SkiaGLRenderEngine::getMaxViewportDims() const {
803 return mGrContext->maxRenderTargetSize();
804}
805
Lucas Dupin3f11e922020-09-22 17:31:04 -0700806void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius,
807 const ShadowSettings& settings) {
808 ATRACE_CALL();
809 const float casterZ = settings.length / 2.0f;
810 const auto shadowShape = cornerRadius > 0
811 ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius))
812 : SkPath::Rect(casterRect);
813 const auto flags =
814 settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag;
815
816 SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ),
817 getSkPoint3(settings.lightPos), settings.lightRadius,
818 getSkColor(settings.ambientColor), getSkColor(settings.spotColor),
819 flags);
820}
821
Lucas Dupinc3800b82020-10-02 16:24:48 -0700822void SkiaGLRenderEngine::drawBlurRegion(SkCanvas* canvas, const BlurRegion& effectRegion,
Galia Peycheva80116e52020-11-06 11:57:25 +0100823 const SkMatrix& drawTransform,
Lucas Dupinc3800b82020-10-02 16:24:48 -0700824 sk_sp<SkSurface> blurredSurface) {
825 ATRACE_CALL();
Galia Peycheva80116e52020-11-06 11:57:25 +0100826
Lucas Dupinc3800b82020-10-02 16:24:48 -0700827 SkPaint paint;
Lyn Han1bdedb32020-11-23 20:33:57 +0000828 paint.setAlpha(static_cast<int>(effectRegion.alpha * 255));
Galia Peycheva80116e52020-11-06 11:57:25 +0100829 const auto matrix = mBlurFilter->getShaderMatrix();
Lucas Dupinc3800b82020-10-02 16:24:48 -0700830 paint.setShader(blurredSurface->makeImageSnapshot()->makeShader(matrix));
831
Galia Peycheva80116e52020-11-06 11:57:25 +0100832 auto rect = SkRect::MakeLTRB(effectRegion.left, effectRegion.top, effectRegion.right,
833 effectRegion.bottom);
834 drawTransform.mapRect(&rect);
835
Lucas Dupinc3800b82020-10-02 16:24:48 -0700836 if (effectRegion.cornerRadiusTL > 0 || effectRegion.cornerRadiusTR > 0 ||
837 effectRegion.cornerRadiusBL > 0 || effectRegion.cornerRadiusBR > 0) {
838 const SkVector radii[4] =
839 {SkVector::Make(effectRegion.cornerRadiusTL, effectRegion.cornerRadiusTL),
840 SkVector::Make(effectRegion.cornerRadiusTR, effectRegion.cornerRadiusTR),
841 SkVector::Make(effectRegion.cornerRadiusBL, effectRegion.cornerRadiusBL),
842 SkVector::Make(effectRegion.cornerRadiusBR, effectRegion.cornerRadiusBR)};
843 SkRRect roundedRect;
844 roundedRect.setRectRadii(rect, radii);
845 canvas->drawRRect(roundedRect, paint);
846 } else {
847 canvas->drawRect(rect, paint);
848 }
849}
850
John Reck67b1e2b2020-08-26 13:17:24 -0700851EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
852 EGLContext shareContext, bool useContextPriority,
853 Protection protection) {
854 EGLint renderableType = 0;
855 if (config == EGL_NO_CONFIG_KHR) {
856 renderableType = EGL_OPENGL_ES3_BIT;
857 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
858 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
859 }
860 EGLint contextClientVersion = 0;
861 if (renderableType & EGL_OPENGL_ES3_BIT) {
862 contextClientVersion = 3;
863 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
864 contextClientVersion = 2;
865 } else if (renderableType & EGL_OPENGL_ES_BIT) {
866 contextClientVersion = 1;
867 } else {
868 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
869 }
870
871 std::vector<EGLint> contextAttributes;
872 contextAttributes.reserve(7);
873 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
874 contextAttributes.push_back(contextClientVersion);
875 if (useContextPriority) {
876 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
877 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
878 }
879 if (protection == Protection::PROTECTED) {
880 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
881 contextAttributes.push_back(EGL_TRUE);
882 }
883 contextAttributes.push_back(EGL_NONE);
884
885 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
886
887 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
888 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
889 // EGL_NO_CONTEXT so that we can abort.
890 if (config != EGL_NO_CONFIG_KHR) {
891 return context;
892 }
893 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
894 // should try to fall back to GLES 2.
895 contextAttributes[1] = 2;
896 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
897 }
898
899 return context;
900}
901
902EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
903 EGLConfig config, int hwcFormat,
904 Protection protection) {
905 EGLConfig placeholderConfig = config;
906 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
907 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
908 }
909 std::vector<EGLint> attributes;
910 attributes.reserve(7);
911 attributes.push_back(EGL_WIDTH);
912 attributes.push_back(1);
913 attributes.push_back(EGL_HEIGHT);
914 attributes.push_back(1);
915 if (protection == Protection::PROTECTED) {
916 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
917 attributes.push_back(EGL_TRUE);
918 }
919 attributes.push_back(EGL_NONE);
920
921 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
922}
923
924void SkiaGLRenderEngine::cleanFramebufferCache() {
925 mSurfaceCache.clear();
Lucas Dupind508e472020-11-04 04:32:06 +0000926 mProtectedSurfaceCache.clear();
John Reck67b1e2b2020-08-26 13:17:24 -0700927}
928
929} // namespace skia
930} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100931} // namespace android