blob: 4e9c5afce02658a7f7e7ac41d1c0a98bc1807108 [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 Mouri678245d2020-09-30 16:58:23 -070022#include <cstdint>
Alec Mouric7f6c8b2020-11-09 18:35:20 -080023#include <memory>
Alec Mouri029d1952020-10-12 10:37:08 -070024
25#include "SkImageInfo.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080026#include "log/log_main.h"
Alec Mouri029d1952020-10-12 10:37:08 -070027#include "system/graphics-base-v1.0.h"
John Reck67b1e2b2020-08-26 13:17:24 -070028
John Reck67b1e2b2020-08-26 13:17:24 -070029#include <EGL/egl.h>
30#include <EGL/eglext.h>
Lucas Dupinc3800b82020-10-02 16:24:48 -070031#include <sync/sync.h>
32#include <ui/BlurRegion.h>
33#include <ui/GraphicBuffer.h>
34#include <utils/Trace.h>
35#include "../gl/GLExtensions.h"
36#include "SkiaGLRenderEngine.h"
37#include "filters/BlurFilter.h"
Ana Krulec70d15b1b2020-12-01 10:05:15 -080038#include "filters/LinearEffect.h"
39#include "skia/debug/SkiaCapture.h"
Lucas Dupinc3800b82020-10-02 16:24:48 -070040
John Reck67b1e2b2020-08-26 13:17:24 -070041#include <GrContextOptions.h>
John Reck67b1e2b2020-08-26 13:17:24 -070042#include <SkCanvas.h>
Alec Mourib34f0b72020-10-02 13:18:34 -070043#include <SkColorFilter.h>
44#include <SkColorMatrix.h>
Alec Mourib5777452020-09-28 11:32:42 -070045#include <SkColorSpace.h>
John Reck67b1e2b2020-08-26 13:17:24 -070046#include <SkImage.h>
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070047#include <SkImageFilters.h>
Lucas Dupin3f11e922020-09-22 17:31:04 -070048#include <SkShadowUtils.h>
John Reck67b1e2b2020-08-26 13:17:24 -070049#include <SkSurface.h>
Alec Mourib5777452020-09-28 11:32:42 -070050#include <gl/GrGLInterface.h>
Alec Mourib5777452020-09-28 11:32:42 -070051
52#include <cmath>
53
John Reck67b1e2b2020-08-26 13:17:24 -070054bool checkGlError(const char* op, int lineNumber);
55
56namespace android {
57namespace renderengine {
58namespace skia {
59
60static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
61 EGLint wanted, EGLConfig* outConfig) {
62 EGLint numConfigs = -1, n = 0;
63 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
64 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
65 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
66 configs.resize(n);
67
68 if (!configs.empty()) {
69 if (attribute != EGL_NONE) {
70 for (EGLConfig config : configs) {
71 EGLint value = 0;
72 eglGetConfigAttrib(dpy, config, attribute, &value);
73 if (wanted == value) {
74 *outConfig = config;
75 return NO_ERROR;
76 }
77 }
78 } else {
79 // just pick the first one
80 *outConfig = configs[0];
81 return NO_ERROR;
82 }
83 }
84
85 return NAME_NOT_FOUND;
86}
87
88static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
89 EGLConfig* config) {
90 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
91 // it is to be used with WIFI displays
92 status_t err;
93 EGLint wantedAttribute;
94 EGLint wantedAttributeValue;
95
96 std::vector<EGLint> attribs;
97 if (renderableType) {
98 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
99 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
100
101 // Default to 8 bits per channel.
102 const EGLint tmpAttribs[] = {
103 EGL_RENDERABLE_TYPE,
104 renderableType,
105 EGL_RECORDABLE_ANDROID,
106 EGL_TRUE,
107 EGL_SURFACE_TYPE,
108 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
109 EGL_FRAMEBUFFER_TARGET_ANDROID,
110 EGL_TRUE,
111 EGL_RED_SIZE,
112 is1010102 ? 10 : 8,
113 EGL_GREEN_SIZE,
114 is1010102 ? 10 : 8,
115 EGL_BLUE_SIZE,
116 is1010102 ? 10 : 8,
117 EGL_ALPHA_SIZE,
118 is1010102 ? 2 : 8,
119 EGL_NONE,
120 };
121 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
122 std::back_inserter(attribs));
123 wantedAttribute = EGL_NONE;
124 wantedAttributeValue = EGL_NONE;
125 } else {
126 // if no renderable type specified, fallback to a simplified query
127 wantedAttribute = EGL_NATIVE_VISUAL_ID;
128 wantedAttributeValue = format;
129 }
130
131 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
132 config);
133 if (err == NO_ERROR) {
134 EGLint caveat;
135 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
136 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
137 }
138
139 return err;
140}
141
142std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
143 const RenderEngineCreationArgs& args) {
144 // initialize EGL for the default display
145 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
146 if (!eglInitialize(display, nullptr, nullptr)) {
147 LOG_ALWAYS_FATAL("failed to initialize EGL");
148 }
149
Yiwei Zhange2650962020-12-01 23:27:58 +0000150 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700151 if (!eglVersion) {
152 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000153 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700154 }
155
Yiwei Zhange2650962020-12-01 23:27:58 +0000156 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700157 if (!eglExtensions) {
158 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000159 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700160 }
161
162 auto& extensions = gl::GLExtensions::getInstance();
163 extensions.initWithEGLStrings(eglVersion, eglExtensions);
164
165 // The code assumes that ES2 or later is available if this extension is
166 // supported.
167 EGLConfig config = EGL_NO_CONFIG_KHR;
168 if (!extensions.hasNoConfigContext()) {
169 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
170 }
171
172 bool useContextPriority =
173 extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH;
174 EGLContext protectedContext = EGL_NO_CONTEXT;
175 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
176 protectedContext = createEglContext(display, config, nullptr, useContextPriority,
177 Protection::PROTECTED);
178 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
179 }
180
181 EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority,
182 Protection::UNPROTECTED);
183
184 // if can't create a GL context, we can only abort.
185 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
186
187 EGLSurface placeholder = EGL_NO_SURFACE;
188 if (!extensions.hasSurfacelessContext()) {
189 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
190 Protection::UNPROTECTED);
191 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
192 }
193 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
194 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
195 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
196 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
197
198 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
199 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
200 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
201 Protection::PROTECTED);
202 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
203 "can't create protected placeholder pbuffer");
204 }
205
206 // initialize the renderer while GL is current
207 std::unique_ptr<SkiaGLRenderEngine> engine =
Lucas Dupind508e472020-11-04 04:32:06 +0000208 std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext,
209 protectedPlaceholder);
John Reck67b1e2b2020-08-26 13:17:24 -0700210
211 ALOGI("OpenGL ES informations:");
212 ALOGI("vendor : %s", extensions.getVendor());
213 ALOGI("renderer : %s", extensions.getRenderer());
214 ALOGI("version : %s", extensions.getVersion());
215 ALOGI("extensions: %s", extensions.getExtensions());
216 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
217 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
218
219 return engine;
220}
221
222EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
223 status_t err;
224 EGLConfig config;
225
226 // First try to get an ES3 config
227 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
228 if (err != NO_ERROR) {
229 // If ES3 fails, try to get an ES2 config
230 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
231 if (err != NO_ERROR) {
232 // If ES2 still doesn't work, probably because we're on the emulator.
233 // try a simplified query
234 ALOGW("no suitable EGLConfig found, trying a simpler query");
235 err = selectEGLConfig(display, format, 0, &config);
236 if (err != NO_ERROR) {
237 // this EGL is too lame for android
238 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
239 }
240 }
241 }
242
243 if (logConfig) {
244 // print some debugging info
245 EGLint r, g, b, a;
246 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
247 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
248 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
249 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
250 ALOGI("EGL information:");
251 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
252 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
253 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
254 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
255 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
256 }
257
258 return config;
259}
260
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700261SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000262 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700263 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri081be4c2020-09-16 10:24:47 -0700264 : mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700265 mEGLContext(ctxt),
266 mPlaceholderSurface(placeholder),
267 mProtectedEGLContext(protectedContext),
Alec Mourib5777452020-09-28 11:32:42 -0700268 mProtectedPlaceholderSurface(protectedPlaceholder),
269 mUseColorManagement(args.useColorManagement) {
John Reck67b1e2b2020-08-26 13:17:24 -0700270 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
271 LOG_ALWAYS_FATAL_IF(!glInterface.get());
272
273 GrContextOptions options;
274 options.fPreferExternalImagesOverES3 = true;
275 options.fDisableDistanceFieldPaths = true;
Lucas Dupind508e472020-11-04 04:32:06 +0000276 mGrContext = GrDirectContext::MakeGL(glInterface, options);
277 if (useProtectedContext(true)) {
278 mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options);
279 useProtectedContext(false);
280 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700281
282 if (args.supportsBackgroundBlur) {
283 mBlurFilter = new BlurFilter();
284 }
John Reck67b1e2b2020-08-26 13:17:24 -0700285}
286
Lucas Dupind508e472020-11-04 04:32:06 +0000287bool SkiaGLRenderEngine::supportsProtectedContent() const {
288 return mProtectedEGLContext != EGL_NO_CONTEXT;
289}
290
291bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) {
292 if (useProtectedContext == mInProtectedContext) {
293 return true;
294 }
295 if (useProtectedContext && supportsProtectedContent()) {
296 return false;
297 }
298 const EGLSurface surface =
299 useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface;
300 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
301 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
302 if (success) {
303 mInProtectedContext = useProtectedContext;
304 }
305 return success;
306}
307
John Reck67b1e2b2020-08-26 13:17:24 -0700308base::unique_fd SkiaGLRenderEngine::flush() {
309 ATRACE_CALL();
310 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
311 return base::unique_fd();
312 }
313
314 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
315 if (sync == EGL_NO_SYNC_KHR) {
316 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
317 return base::unique_fd();
318 }
319
320 // native fence fd will not be populated until flush() is done.
321 glFlush();
322
323 // get the fence fd
324 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
325 eglDestroySyncKHR(mEGLDisplay, sync);
326 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
327 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
328 }
329
330 return fenceFd;
331}
332
333bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) {
334 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
335 !gl::GLExtensions::getInstance().hasWaitSync()) {
336 return false;
337 }
338
339 // release the fd and transfer the ownership to EGLSync
340 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
341 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
342 if (sync == EGL_NO_SYNC_KHR) {
343 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
344 return false;
345 }
346
347 // XXX: The spec draft is inconsistent as to whether this should return an
348 // EGLint or void. Ignore the return value for now, as it's not strictly
349 // needed.
350 eglWaitSyncKHR(mEGLDisplay, sync, 0);
351 EGLint error = eglGetError();
352 eglDestroySyncKHR(mEGLDisplay, sync);
353 if (error != EGL_SUCCESS) {
354 ALOGE("failed to wait for EGL native fence sync: %#x", error);
355 return false;
356 }
357
358 return true;
359}
360
361static bool hasUsage(const AHardwareBuffer_Desc& desc, uint64_t usage) {
362 return !!(desc.usage & usage);
363}
364
Alec Mouri678245d2020-09-30 16:58:23 -0700365static float toDegrees(uint32_t transform) {
366 switch (transform) {
367 case ui::Transform::ROT_90:
368 return 90.0;
369 case ui::Transform::ROT_180:
370 return 180.0;
371 case ui::Transform::ROT_270:
372 return 270.0;
373 default:
374 return 0.0;
375 }
376}
377
Alec Mourib34f0b72020-10-02 13:18:34 -0700378static SkColorMatrix toSkColorMatrix(const mat4& matrix) {
379 return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1],
380 matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2],
381 matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3],
382 matrix[3][3], 0);
383}
384
Alec Mouri029d1952020-10-12 10:37:08 -0700385static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) {
386 int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK;
387 int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK;
388
389 // Treat unsupported dataspaces as srgb
390 if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
391 destTransfer != HAL_DATASPACE_TRANSFER_HLG &&
392 destTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
393 destTransfer = HAL_DATASPACE_TRANSFER_SRGB;
394 }
395
396 if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
397 sourceTransfer != HAL_DATASPACE_TRANSFER_HLG &&
398 sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
399 sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB;
400 }
401
402 const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
403 const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB;
404 const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
405 const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB;
406
407 return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) &&
408 sourceTransfer != destTransfer;
409}
410
Alec Mouri1a4d0642020-11-13 17:42:01 -0800411static bool needsLinearEffect(const mat4& colorTransform, ui::Dataspace sourceDataspace,
412 ui::Dataspace destinationDataspace) {
413 return colorTransform != mat4() || needsToneMapping(sourceDataspace, destinationDataspace);
414}
415
John Reck67b1e2b2020-08-26 13:17:24 -0700416void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) {
417 std::lock_guard<std::mutex> lock(mRenderingMutex);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800418 mTextureCache.erase(bufferId);
419 mProtectedTextureCache.erase(bufferId);
John Reck67b1e2b2020-08-26 13:17:24 -0700420}
421
422status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
423 const std::vector<const LayerSettings*>& layers,
424 const sp<GraphicBuffer>& buffer,
425 const bool useFramebufferCache,
426 base::unique_fd&& bufferFence, base::unique_fd* drawFence) {
427 ATRACE_NAME("SkiaGL::drawLayers");
428 std::lock_guard<std::mutex> lock(mRenderingMutex);
429 if (layers.empty()) {
430 ALOGV("Drawing empty layer stack");
431 return NO_ERROR;
432 }
433
434 if (bufferFence.get() >= 0) {
435 // Duplicate the fence for passing to waitFence.
436 base::unique_fd bufferFenceDup(dup(bufferFence.get()));
437 if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
438 ATRACE_NAME("Waiting before draw");
439 sync_wait(bufferFence.get(), -1);
440 }
441 }
442 if (buffer == nullptr) {
443 ALOGE("No output buffer provided. Aborting GPU composition.");
444 return BAD_VALUE;
445 }
446
Lucas Dupind508e472020-11-04 04:32:06 +0000447 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800448 auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache;
John Reck67b1e2b2020-08-26 13:17:24 -0700449 AHardwareBuffer_Desc bufferDesc;
450 AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc);
John Reck67b1e2b2020-08-26 13:17:24 -0700451 LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE),
452 "missing usage");
453
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800454 std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700455 if (useFramebufferCache) {
Lucas Dupind508e472020-11-04 04:32:06 +0000456 auto iter = cache.find(buffer->getId());
457 if (iter != cache.end()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700458 ALOGV("Cache hit!");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800459 surfaceTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700460 }
461 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800462
463 if (surfaceTextureRef == nullptr || surfaceTextureRef->getTexture() == nullptr) {
464 surfaceTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
465 surfaceTextureRef->setTexture(
466 new AutoBackendTexture(mGrContext.get(), buffer->toAHardwareBuffer(), true));
467 if (useFramebufferCache) {
John Reck67b1e2b2020-08-26 13:17:24 -0700468 ALOGD("Adding to cache");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800469 cache.insert({buffer->getId(), surfaceTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700470 }
471 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800472
473 sk_sp<SkSurface> surface =
474 surfaceTextureRef->getTexture()->getOrCreateSurface(mUseColorManagement
475 ? display.outputDataspace
476 : ui::Dataspace::SRGB,
477 mGrContext.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700478
Ana Krulec70d15b1b2020-12-01 10:05:15 -0800479 SkCanvas* canvas = mCapture.tryCapture(surface.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700480 // Clear the entire canvas with a transparent black to prevent ghost images.
481 canvas->clear(SK_ColorTRANSPARENT);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700482 canvas->save();
John Reck67b1e2b2020-08-26 13:17:24 -0700483
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700484 // Before doing any drawing, let's make sure that we'll start at the origin of the display.
485 // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual
486 // displays might have different scaling when compared to the physical screen.
Alec Mouri678245d2020-09-30 16:58:23 -0700487
488 canvas->clipRect(getSkRect(display.physicalDisplay));
Galia Peychevaf7889b32020-11-25 22:22:40 +0100489 canvas->translate(display.physicalDisplay.left, display.physicalDisplay.top);
Alec Mouri678245d2020-09-30 16:58:23 -0700490
491 const auto clipWidth = display.clip.width();
492 const auto clipHeight = display.clip.height();
493 auto rotatedClipWidth = clipWidth;
494 auto rotatedClipHeight = clipHeight;
495 // Scale is contingent on the rotation result.
496 if (display.orientation & ui::Transform::ROT_90) {
497 std::swap(rotatedClipWidth, rotatedClipHeight);
498 }
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700499 const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700500 static_cast<SkScalar>(rotatedClipWidth);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700501 const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700502 static_cast<SkScalar>(rotatedClipHeight);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100503 canvas->scale(scaleX, scaleY);
Alec Mouri678245d2020-09-30 16:58:23 -0700504
505 // Canvas rotation is done by centering the clip window at the origin, rotating, translating
506 // back so that the top left corner of the clip is at (0, 0).
Galia Peychevaf7889b32020-11-25 22:22:40 +0100507 canvas->translate(rotatedClipWidth / 2, rotatedClipHeight / 2);
508 canvas->rotate(toDegrees(display.orientation));
509 canvas->translate(-clipWidth / 2, -clipHeight / 2);
510 canvas->translate(-display.clip.left, -display.clip.top);
John Reck67b1e2b2020-08-26 13:17:24 -0700511 for (const auto& layer : layers) {
Galia Peychevaf7889b32020-11-25 22:22:40 +0100512 canvas->save();
513
514 // Layers have a local transform that should be applied to them
515 canvas->concat(getSkM44(layer->geometry.positionTransform).asM33());
Galia Peycheva6c460652020-11-03 19:42:42 +0100516
Lucas Dupin21f348e2020-09-16 17:31:26 -0700517 SkPaint paint;
518 const auto& bounds = layer->geometry.boundaries;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700519 const auto dest = getSkRect(bounds);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100520 const auto layerRect = canvas->getTotalMatrix().mapRect(dest);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700521 std::unordered_map<uint32_t, sk_sp<SkSurface>> cachedBlurs;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700522 if (mBlurFilter) {
523 if (layer->backgroundBlurRadius > 0) {
524 ATRACE_NAME("BackgroundBlur");
Galia Peycheva80116e52020-11-06 11:57:25 +0100525 auto blurredSurface = mBlurFilter->generate(canvas, surface,
526 layer->backgroundBlurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700527 cachedBlurs[layer->backgroundBlurRadius] = blurredSurface;
Galia Peycheva80116e52020-11-06 11:57:25 +0100528
Galia Peychevaf7889b32020-11-25 22:22:40 +0100529 drawBlurRegion(canvas, getBlurRegion(layer), layerRect, blurredSurface);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700530 }
531 if (layer->blurRegions.size() > 0) {
532 for (auto region : layer->blurRegions) {
533 if (cachedBlurs[region.blurRadius]) {
534 continue;
535 }
536 ATRACE_NAME("BlurRegion");
Galia Peycheva6c460652020-11-03 19:42:42 +0100537 auto blurredSurface =
538 mBlurFilter->generate(canvas, surface, region.blurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700539 cachedBlurs[region.blurRadius] = blurredSurface;
540 }
541 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700542 }
543
John Reck67b1e2b2020-08-26 13:17:24 -0700544 if (layer->source.buffer.buffer) {
545 ATRACE_NAME("DrawImage");
546 const auto& item = layer->source.buffer;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800547 std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr;
548 auto iter = mTextureCache.find(item.buffer->getId());
549 if (iter != mTextureCache.end()) {
550 imageTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700551 } else {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800552 imageTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
553 imageTextureRef->setTexture(new AutoBackendTexture(mGrContext.get(),
554 item.buffer->toAHardwareBuffer(),
555 false));
556 mTextureCache.insert({buffer->getId(), imageTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700557 }
Alec Mouri1a4d0642020-11-13 17:42:01 -0800558
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800559 sk_sp<SkImage> image =
560 imageTextureRef->getTexture()
561 ->makeImage(mUseColorManagement
Alec Mouri1a4d0642020-11-13 17:42:01 -0800562 ? (needsLinearEffect(layer->colorTransform,
563 layer->sourceDataspace,
564 display.outputDataspace)
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800565 // If we need to map to linear space,
566 // then mark the source image with the
567 // same colorspace as the destination
568 // surface so that Skia's color
569 // management is a no-op.
570 ? display.outputDataspace
571 : layer->sourceDataspace)
572 : ui::Dataspace::SRGB,
573 item.isOpaque ? kOpaque_SkAlphaType
574 : (item.usePremultipliedAlpha
575 ? kPremul_SkAlphaType
576 : kUnpremul_SkAlphaType),
577 mGrContext.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700578
579 auto texMatrix = getSkM44(item.textureTransform).asM33();
580 // textureTansform was intended to be passed directly into a shader, so when
581 // building the total matrix with the textureTransform we need to first
582 // normalize it, then apply the textureTransform, then scale back up.
Huihong Luo3a3cf3c2020-12-07 17:05:41 -0800583 texMatrix.preScale(1.0f / bounds.getWidth(), 1.0f / bounds.getHeight());
584 texMatrix.postScale(image->width(), image->height());
Alec Mouri678245d2020-09-30 16:58:23 -0700585
Huihong Luo3a3cf3c2020-12-07 17:05:41 -0800586 SkMatrix matrix;
587 if (!texMatrix.invert(&matrix)) {
588 matrix = texMatrix;
Alec Mouri678245d2020-09-30 16:58:23 -0700589 }
590
Ana Krulecb7b28b22020-11-23 14:48:58 -0800591 sk_sp<SkShader> shader;
592
593 if (layer->source.buffer.useTextureFiltering) {
594 shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
595 SkSamplingOptions(
596 {SkFilterMode::kLinear, SkMipmapMode::kNone}),
597 &matrix);
598 } else {
599 shader = image->makeShader(matrix);
600 }
Alec Mouri029d1952020-10-12 10:37:08 -0700601
602 if (mUseColorManagement &&
Alec Mouri1a4d0642020-11-13 17:42:01 -0800603 needsLinearEffect(layer->colorTransform, layer->sourceDataspace,
604 display.outputDataspace)) {
Alec Mouri029d1952020-10-12 10:37:08 -0700605 LinearEffect effect = LinearEffect{.inputDataspace = layer->sourceDataspace,
606 .outputDataspace = display.outputDataspace,
607 .undoPremultipliedAlpha = !item.isOpaque &&
608 item.usePremultipliedAlpha};
Alec Mouric16a77d2020-11-13 13:20:11 -0800609
610 auto effectIter = mRuntimeEffects.find(effect);
611 sk_sp<SkRuntimeEffect> runtimeEffect = nullptr;
612 if (effectIter == mRuntimeEffects.end()) {
613 runtimeEffect = buildRuntimeEffect(effect);
614 mRuntimeEffects.insert({effect, runtimeEffect});
615 } else {
616 runtimeEffect = effectIter->second;
617 }
618
Alec Mouri029d1952020-10-12 10:37:08 -0700619 paint.setShader(createLinearEffectShader(shader, effect, runtimeEffect,
Alec Mouri1a4d0642020-11-13 17:42:01 -0800620 layer->colorTransform,
Alec Mouri029d1952020-10-12 10:37:08 -0700621 display.maxLuminance,
622 layer->source.buffer.maxMasteringLuminance,
623 layer->source.buffer.maxContentLuminance));
624 } else {
625 paint.setShader(shader);
626 }
Ana Krulec1768bd22020-11-23 14:51:31 -0800627 // Make sure to take into the account the alpha set on the layer.
628 paint.setAlphaf(layer->alpha);
John Reck67b1e2b2020-08-26 13:17:24 -0700629 } else {
630 ATRACE_NAME("DrawColor");
John Reck67b1e2b2020-08-26 13:17:24 -0700631 const auto color = layer->source.solidColor;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700632 paint.setShader(SkShaders::Color(SkColor4f{.fR = color.r,
633 .fG = color.g,
634 .fB = color.b,
635 layer->alpha},
636 nullptr));
John Reck67b1e2b2020-08-26 13:17:24 -0700637 }
Lucas Dupin21f348e2020-09-16 17:31:26 -0700638
Alec Mourib34f0b72020-10-02 13:18:34 -0700639 paint.setColorFilter(SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform)));
640
Lucas Dupinc3800b82020-10-02 16:24:48 -0700641 for (const auto effectRegion : layer->blurRegions) {
Galia Peychevaf7889b32020-11-25 22:22:40 +0100642 drawBlurRegion(canvas, effectRegion, layerRect, cachedBlurs[effectRegion.blurRadius]);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700643 }
644
Lucas Dupin3f11e922020-09-22 17:31:04 -0700645 if (layer->shadow.length > 0) {
646 const auto rect = layer->geometry.roundedCornersRadius > 0
647 ? getSkRect(layer->geometry.roundedCornersCrop)
648 : dest;
649 drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow);
650 }
651
Lucas Dupin21f348e2020-09-16 17:31:26 -0700652 if (layer->geometry.roundedCornersRadius > 0) {
653 canvas->drawRRect(getRoundedRect(layer), paint);
654 } else {
655 canvas->drawRect(dest, paint);
656 }
Lucas Dupinc3800b82020-10-02 16:24:48 -0700657
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700658 canvas->restore();
John Reck67b1e2b2020-08-26 13:17:24 -0700659 }
Ana Krulec70d15b1b2020-12-01 10:05:15 -0800660 canvas->restore();
661 mCapture.endCapture();
John Reck67b1e2b2020-08-26 13:17:24 -0700662 {
663 ATRACE_NAME("flush surface");
664 surface->flush();
665 }
666
667 if (drawFence != nullptr) {
668 *drawFence = flush();
669 }
670
671 // If flush failed or we don't support native fences, we need to force the
672 // gl command stream to be executed.
673 bool requireSync = drawFence == nullptr || drawFence->get() < 0;
674 if (requireSync) {
675 ATRACE_BEGIN("Submit(sync=true)");
676 } else {
677 ATRACE_BEGIN("Submit(sync=false)");
678 }
Lucas Dupind508e472020-11-04 04:32:06 +0000679 bool success = grContext->submit(requireSync);
John Reck67b1e2b2020-08-26 13:17:24 -0700680 ATRACE_END();
681 if (!success) {
682 ALOGE("Failed to flush RenderEngine commands");
683 // Chances are, something illegal happened (either the caller passed
684 // us bad parameters, or we messed up our shader generation).
685 return INVALID_OPERATION;
686 }
687
688 // checkErrors();
689 return NO_ERROR;
690}
691
Lucas Dupin3f11e922020-09-22 17:31:04 -0700692inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) {
693 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
694}
695
696inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) {
697 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
698}
699
Lucas Dupin21f348e2020-09-16 17:31:26 -0700700inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) {
Ana Krulec89717602020-12-04 12:48:05 -0800701 const auto rect = getSkRect(layer->geometry.boundaries);
Lucas Dupin21f348e2020-09-16 17:31:26 -0700702 const auto cornerRadius = layer->geometry.roundedCornersRadius;
703 return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius);
704}
705
Galia Peycheva80116e52020-11-06 11:57:25 +0100706inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) {
707 const auto rect = getSkRect(layer->geometry.boundaries);
708 const auto cornersRadius = layer->geometry.roundedCornersRadius;
709 return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius),
710 .cornerRadiusTL = cornersRadius,
711 .cornerRadiusTR = cornersRadius,
712 .cornerRadiusBL = cornersRadius,
713 .cornerRadiusBR = cornersRadius,
714 .alpha = 1,
715 .left = static_cast<int>(rect.fLeft),
716 .top = static_cast<int>(rect.fTop),
717 .right = static_cast<int>(rect.fRight),
718 .bottom = static_cast<int>(rect.fBottom)};
719}
720
Lucas Dupin3f11e922020-09-22 17:31:04 -0700721inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) {
722 return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255);
723}
724
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700725inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) {
726 return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
727 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
728 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
729 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
730}
731
Lucas Dupin3f11e922020-09-22 17:31:04 -0700732inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) {
733 return SkPoint3::Make(vector.x, vector.y, vector.z);
734}
735
John Reck67b1e2b2020-08-26 13:17:24 -0700736size_t SkiaGLRenderEngine::getMaxTextureSize() const {
737 return mGrContext->maxTextureSize();
738}
739
740size_t SkiaGLRenderEngine::getMaxViewportDims() const {
741 return mGrContext->maxRenderTargetSize();
742}
743
Lucas Dupin3f11e922020-09-22 17:31:04 -0700744void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius,
745 const ShadowSettings& settings) {
746 ATRACE_CALL();
747 const float casterZ = settings.length / 2.0f;
748 const auto shadowShape = cornerRadius > 0
749 ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius))
750 : SkPath::Rect(casterRect);
751 const auto flags =
752 settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag;
753
754 SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ),
755 getSkPoint3(settings.lightPos), settings.lightRadius,
756 getSkColor(settings.ambientColor), getSkColor(settings.spotColor),
757 flags);
758}
759
Lucas Dupinc3800b82020-10-02 16:24:48 -0700760void SkiaGLRenderEngine::drawBlurRegion(SkCanvas* canvas, const BlurRegion& effectRegion,
Galia Peychevaf7889b32020-11-25 22:22:40 +0100761 const SkRect& layerRect, sk_sp<SkSurface> blurredSurface) {
Lucas Dupinc3800b82020-10-02 16:24:48 -0700762 ATRACE_CALL();
Galia Peycheva80116e52020-11-06 11:57:25 +0100763
Lucas Dupinc3800b82020-10-02 16:24:48 -0700764 SkPaint paint;
Lyn Han1bdedb32020-11-23 20:33:57 +0000765 paint.setAlpha(static_cast<int>(effectRegion.alpha * 255));
Robin Leef180f412020-12-07 02:51:41 +0100766 const auto matrix = getBlurShaderTransform(canvas, layerRect);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100767 paint.setShader(blurredSurface->makeImageSnapshot()->makeShader(
Robin Leef180f412020-12-07 02:51:41 +0100768 SkTileMode::kClamp,
769 SkTileMode::kClamp,
770 SkSamplingOptions({SkFilterMode::kLinear, SkMipmapMode::kNone}),
771 &matrix));
Lucas Dupinc3800b82020-10-02 16:24:48 -0700772
Galia Peycheva80116e52020-11-06 11:57:25 +0100773 auto rect = SkRect::MakeLTRB(effectRegion.left, effectRegion.top, effectRegion.right,
774 effectRegion.bottom);
Galia Peycheva80116e52020-11-06 11:57:25 +0100775
Lucas Dupinc3800b82020-10-02 16:24:48 -0700776 if (effectRegion.cornerRadiusTL > 0 || effectRegion.cornerRadiusTR > 0 ||
777 effectRegion.cornerRadiusBL > 0 || effectRegion.cornerRadiusBR > 0) {
778 const SkVector radii[4] =
779 {SkVector::Make(effectRegion.cornerRadiusTL, effectRegion.cornerRadiusTL),
780 SkVector::Make(effectRegion.cornerRadiusTR, effectRegion.cornerRadiusTR),
781 SkVector::Make(effectRegion.cornerRadiusBL, effectRegion.cornerRadiusBL),
782 SkVector::Make(effectRegion.cornerRadiusBR, effectRegion.cornerRadiusBR)};
783 SkRRect roundedRect;
784 roundedRect.setRectRadii(rect, radii);
785 canvas->drawRRect(roundedRect, paint);
786 } else {
787 canvas->drawRect(rect, paint);
788 }
789}
790
Galia Peychevaf7889b32020-11-25 22:22:40 +0100791SkMatrix SkiaGLRenderEngine::getBlurShaderTransform(const SkCanvas* canvas,
792 const SkRect& layerRect) {
793 // 1. Apply the blur shader matrix, which scales up the blured surface to its real size
794 auto matrix = mBlurFilter->getShaderMatrix();
795 // 2. Since the blurred surface has the size of the layer, we align it with the
796 // top left corner of the layer position.
797 matrix.postConcat(SkMatrix::Translate(layerRect.fLeft, layerRect.fTop));
798 // 3. Finally, apply the inverse canvas matrix. The snapshot made in the BlurFilter is in the
799 // original surface orientation. The inverse matrix has to be applied to align the blur
800 // surface with the current orientation/position of the canvas.
801 SkMatrix drawInverse;
802 if (canvas->getTotalMatrix().invert(&drawInverse)) {
803 matrix.postConcat(drawInverse);
804 }
805
806 return matrix;
807}
808
John Reck67b1e2b2020-08-26 13:17:24 -0700809EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
810 EGLContext shareContext, bool useContextPriority,
811 Protection protection) {
812 EGLint renderableType = 0;
813 if (config == EGL_NO_CONFIG_KHR) {
814 renderableType = EGL_OPENGL_ES3_BIT;
815 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
816 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
817 }
818 EGLint contextClientVersion = 0;
819 if (renderableType & EGL_OPENGL_ES3_BIT) {
820 contextClientVersion = 3;
821 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
822 contextClientVersion = 2;
823 } else if (renderableType & EGL_OPENGL_ES_BIT) {
824 contextClientVersion = 1;
825 } else {
826 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
827 }
828
829 std::vector<EGLint> contextAttributes;
830 contextAttributes.reserve(7);
831 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
832 contextAttributes.push_back(contextClientVersion);
833 if (useContextPriority) {
834 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
835 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
836 }
837 if (protection == Protection::PROTECTED) {
838 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
839 contextAttributes.push_back(EGL_TRUE);
840 }
841 contextAttributes.push_back(EGL_NONE);
842
843 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
844
845 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
846 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
847 // EGL_NO_CONTEXT so that we can abort.
848 if (config != EGL_NO_CONFIG_KHR) {
849 return context;
850 }
851 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
852 // should try to fall back to GLES 2.
853 contextAttributes[1] = 2;
854 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
855 }
856
857 return context;
858}
859
860EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
861 EGLConfig config, int hwcFormat,
862 Protection protection) {
863 EGLConfig placeholderConfig = config;
864 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
865 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
866 }
867 std::vector<EGLint> attributes;
868 attributes.reserve(7);
869 attributes.push_back(EGL_WIDTH);
870 attributes.push_back(1);
871 attributes.push_back(EGL_HEIGHT);
872 attributes.push_back(1);
873 if (protection == Protection::PROTECTED) {
874 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
875 attributes.push_back(EGL_TRUE);
876 }
877 attributes.push_back(EGL_NONE);
878
879 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
880}
881
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800882void SkiaGLRenderEngine::cleanFramebufferCache() {}
John Reck67b1e2b2020-08-26 13:17:24 -0700883
884} // namespace skia
885} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100886} // namespace android