blob: 8dc0c68df3b8a2160dc47fcd1c592cf033fe9a53 [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 Mouric7f6c8b2020-11-09 18:35:20 -080019#include <memory>
Alec Mouri029d1952020-10-12 10:37:08 -070020
21#include "SkImageInfo.h"
Alec Mouric7f6c8b2020-11-09 18:35:20 -080022#include "log/log_main.h"
Alec Mouri029d1952020-10-12 10:37:08 -070023#include "system/graphics-base-v1.0.h"
John Reck67b1e2b2020-08-26 13:17:24 -070024#undef LOG_TAG
25#define LOG_TAG "RenderEngine"
26#define ATRACE_TAG ATRACE_TAG_GRAPHICS
27
John Reck67b1e2b2020-08-26 13:17:24 -070028#include <EGL/egl.h>
29#include <EGL/eglext.h>
30#include <GLES2/gl2.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"
38
John Reck67b1e2b2020-08-26 13:17:24 -070039#include <GrContextOptions.h>
John Reck67b1e2b2020-08-26 13:17:24 -070040#include <SkCanvas.h>
Alec Mourib34f0b72020-10-02 13:18:34 -070041#include <SkColorFilter.h>
42#include <SkColorMatrix.h>
Alec Mourib5777452020-09-28 11:32:42 -070043#include <SkColorSpace.h>
John Reck67b1e2b2020-08-26 13:17:24 -070044#include <SkImage.h>
Lucas Dupinf4cb4a02020-09-22 14:19:26 -070045#include <SkImageFilters.h>
Lucas Dupin3f11e922020-09-22 17:31:04 -070046#include <SkShadowUtils.h>
John Reck67b1e2b2020-08-26 13:17:24 -070047#include <SkSurface.h>
Alec Mourib5777452020-09-28 11:32:42 -070048#include <gl/GrGLInterface.h>
49#include <sync/sync.h>
50#include <ui/GraphicBuffer.h>
51#include <utils/Trace.h>
52
53#include <cmath>
54
55#include "../gl/GLExtensions.h"
Alec Mourib34f0b72020-10-02 13:18:34 -070056#include "SkiaGLRenderEngine.h"
Alec Mourib5777452020-09-28 11:32:42 -070057#include "filters/BlurFilter.h"
Alec Mouri029d1952020-10-12 10:37:08 -070058#include "filters/LinearEffect.h"
John Reck67b1e2b2020-08-26 13:17:24 -070059
John Reck67b1e2b2020-08-26 13:17:24 -070060bool 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
148std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
149 const RenderEngineCreationArgs& args) {
150 // initialize EGL for the default display
151 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
152 if (!eglInitialize(display, nullptr, nullptr)) {
153 LOG_ALWAYS_FATAL("failed to initialize EGL");
154 }
155
Yiwei Zhange2650962020-12-01 23:27:58 +0000156 const auto eglVersion = eglQueryString(display, EGL_VERSION);
John Reck67b1e2b2020-08-26 13:17:24 -0700157 if (!eglVersion) {
158 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000159 LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700160 }
161
Yiwei Zhange2650962020-12-01 23:27:58 +0000162 const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
John Reck67b1e2b2020-08-26 13:17:24 -0700163 if (!eglExtensions) {
164 checkGlError(__FUNCTION__, __LINE__);
Yiwei Zhange2650962020-12-01 23:27:58 +0000165 LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
John Reck67b1e2b2020-08-26 13:17:24 -0700166 }
167
168 auto& extensions = gl::GLExtensions::getInstance();
169 extensions.initWithEGLStrings(eglVersion, eglExtensions);
170
171 // The code assumes that ES2 or later is available if this extension is
172 // supported.
173 EGLConfig config = EGL_NO_CONFIG_KHR;
174 if (!extensions.hasNoConfigContext()) {
175 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
176 }
177
178 bool useContextPriority =
179 extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH;
180 EGLContext protectedContext = EGL_NO_CONTEXT;
181 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
182 protectedContext = createEglContext(display, config, nullptr, useContextPriority,
183 Protection::PROTECTED);
184 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
185 }
186
187 EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority,
188 Protection::UNPROTECTED);
189
190 // if can't create a GL context, we can only abort.
191 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
192
193 EGLSurface placeholder = EGL_NO_SURFACE;
194 if (!extensions.hasSurfacelessContext()) {
195 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
196 Protection::UNPROTECTED);
197 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
198 }
199 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
200 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
201 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
202 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
203
204 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
205 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
206 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
207 Protection::PROTECTED);
208 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
209 "can't create protected placeholder pbuffer");
210 }
211
212 // initialize the renderer while GL is current
213 std::unique_ptr<SkiaGLRenderEngine> engine =
Lucas Dupind508e472020-11-04 04:32:06 +0000214 std::make_unique<SkiaGLRenderEngine>(args, display, ctxt, placeholder, protectedContext,
215 protectedPlaceholder);
John Reck67b1e2b2020-08-26 13:17:24 -0700216
217 ALOGI("OpenGL ES informations:");
218 ALOGI("vendor : %s", extensions.getVendor());
219 ALOGI("renderer : %s", extensions.getRenderer());
220 ALOGI("version : %s", extensions.getVersion());
221 ALOGI("extensions: %s", extensions.getExtensions());
222 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
223 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
224
225 return engine;
226}
227
228EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
229 status_t err;
230 EGLConfig config;
231
232 // First try to get an ES3 config
233 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
234 if (err != NO_ERROR) {
235 // If ES3 fails, try to get an ES2 config
236 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
237 if (err != NO_ERROR) {
238 // If ES2 still doesn't work, probably because we're on the emulator.
239 // try a simplified query
240 ALOGW("no suitable EGLConfig found, trying a simpler query");
241 err = selectEGLConfig(display, format, 0, &config);
242 if (err != NO_ERROR) {
243 // this EGL is too lame for android
244 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
245 }
246 }
247 }
248
249 if (logConfig) {
250 // print some debugging info
251 EGLint r, g, b, a;
252 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
253 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
254 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
255 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
256 ALOGI("EGL information:");
257 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
258 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
259 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
260 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
261 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
262 }
263
264 return config;
265}
266
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700267SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
Lucas Dupind508e472020-11-04 04:32:06 +0000268 EGLContext ctxt, EGLSurface placeholder,
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700269 EGLContext protectedContext, EGLSurface protectedPlaceholder)
Alec Mouri081be4c2020-09-16 10:24:47 -0700270 : mEGLDisplay(display),
John Reck67b1e2b2020-08-26 13:17:24 -0700271 mEGLContext(ctxt),
272 mPlaceholderSurface(placeholder),
273 mProtectedEGLContext(protectedContext),
Alec Mourib5777452020-09-28 11:32:42 -0700274 mProtectedPlaceholderSurface(protectedPlaceholder),
275 mUseColorManagement(args.useColorManagement) {
John Reck67b1e2b2020-08-26 13:17:24 -0700276 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
277 LOG_ALWAYS_FATAL_IF(!glInterface.get());
278
279 GrContextOptions options;
280 options.fPreferExternalImagesOverES3 = true;
281 options.fDisableDistanceFieldPaths = true;
Lucas Dupind508e472020-11-04 04:32:06 +0000282 mGrContext = GrDirectContext::MakeGL(glInterface, options);
283 if (useProtectedContext(true)) {
284 mProtectedGrContext = GrDirectContext::MakeGL(glInterface, options);
285 useProtectedContext(false);
286 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700287
288 if (args.supportsBackgroundBlur) {
289 mBlurFilter = new BlurFilter();
290 }
John Reck67b1e2b2020-08-26 13:17:24 -0700291}
292
Lucas Dupind508e472020-11-04 04:32:06 +0000293bool SkiaGLRenderEngine::supportsProtectedContent() const {
294 return mProtectedEGLContext != EGL_NO_CONTEXT;
295}
296
297bool SkiaGLRenderEngine::useProtectedContext(bool useProtectedContext) {
298 if (useProtectedContext == mInProtectedContext) {
299 return true;
300 }
301 if (useProtectedContext && supportsProtectedContent()) {
302 return false;
303 }
304 const EGLSurface surface =
305 useProtectedContext ? mProtectedPlaceholderSurface : mPlaceholderSurface;
306 const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
307 const bool success = eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE;
308 if (success) {
309 mInProtectedContext = useProtectedContext;
310 }
311 return success;
312}
313
John Reck67b1e2b2020-08-26 13:17:24 -0700314base::unique_fd SkiaGLRenderEngine::flush() {
315 ATRACE_CALL();
316 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
317 return base::unique_fd();
318 }
319
320 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
321 if (sync == EGL_NO_SYNC_KHR) {
322 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
323 return base::unique_fd();
324 }
325
326 // native fence fd will not be populated until flush() is done.
327 glFlush();
328
329 // get the fence fd
330 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
331 eglDestroySyncKHR(mEGLDisplay, sync);
332 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
333 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
334 }
335
336 return fenceFd;
337}
338
339bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) {
340 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
341 !gl::GLExtensions::getInstance().hasWaitSync()) {
342 return false;
343 }
344
345 // release the fd and transfer the ownership to EGLSync
346 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
347 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
348 if (sync == EGL_NO_SYNC_KHR) {
349 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
350 return false;
351 }
352
353 // XXX: The spec draft is inconsistent as to whether this should return an
354 // EGLint or void. Ignore the return value for now, as it's not strictly
355 // needed.
356 eglWaitSyncKHR(mEGLDisplay, sync, 0);
357 EGLint error = eglGetError();
358 eglDestroySyncKHR(mEGLDisplay, sync);
359 if (error != EGL_SUCCESS) {
360 ALOGE("failed to wait for EGL native fence sync: %#x", error);
361 return false;
362 }
363
364 return true;
365}
366
367static bool hasUsage(const AHardwareBuffer_Desc& desc, uint64_t usage) {
368 return !!(desc.usage & usage);
369}
370
Alec Mouri678245d2020-09-30 16:58:23 -0700371static float toDegrees(uint32_t transform) {
372 switch (transform) {
373 case ui::Transform::ROT_90:
374 return 90.0;
375 case ui::Transform::ROT_180:
376 return 180.0;
377 case ui::Transform::ROT_270:
378 return 270.0;
379 default:
380 return 0.0;
381 }
382}
383
Alec Mourib34f0b72020-10-02 13:18:34 -0700384static SkColorMatrix toSkColorMatrix(const mat4& matrix) {
385 return SkColorMatrix(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 0, matrix[0][1],
386 matrix[1][1], matrix[2][1], matrix[3][1], 0, matrix[0][2], matrix[1][2],
387 matrix[2][2], matrix[3][2], 0, matrix[0][3], matrix[1][3], matrix[2][3],
388 matrix[3][3], 0);
389}
390
Alec Mouri029d1952020-10-12 10:37:08 -0700391static bool needsToneMapping(ui::Dataspace sourceDataspace, ui::Dataspace destinationDataspace) {
392 int64_t sourceTransfer = sourceDataspace & HAL_DATASPACE_TRANSFER_MASK;
393 int64_t destTransfer = destinationDataspace & HAL_DATASPACE_TRANSFER_MASK;
394
395 // Treat unsupported dataspaces as srgb
396 if (destTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
397 destTransfer != HAL_DATASPACE_TRANSFER_HLG &&
398 destTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
399 destTransfer = HAL_DATASPACE_TRANSFER_SRGB;
400 }
401
402 if (sourceTransfer != HAL_DATASPACE_TRANSFER_LINEAR &&
403 sourceTransfer != HAL_DATASPACE_TRANSFER_HLG &&
404 sourceTransfer != HAL_DATASPACE_TRANSFER_ST2084) {
405 sourceTransfer = HAL_DATASPACE_TRANSFER_SRGB;
406 }
407
408 const bool isSourceLinear = sourceTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
409 const bool isSourceSRGB = sourceTransfer == HAL_DATASPACE_TRANSFER_SRGB;
410 const bool isDestLinear = destTransfer == HAL_DATASPACE_TRANSFER_LINEAR;
411 const bool isDestSRGB = destTransfer == HAL_DATASPACE_TRANSFER_SRGB;
412
413 return !(isSourceLinear && isDestSRGB) && !(isSourceSRGB && isDestLinear) &&
414 sourceTransfer != destTransfer;
415}
416
Alec Mouri1a4d0642020-11-13 17:42:01 -0800417static bool needsLinearEffect(const mat4& colorTransform, ui::Dataspace sourceDataspace,
418 ui::Dataspace destinationDataspace) {
419 return colorTransform != mat4() || needsToneMapping(sourceDataspace, destinationDataspace);
420}
421
John Reck67b1e2b2020-08-26 13:17:24 -0700422void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) {
423 std::lock_guard<std::mutex> lock(mRenderingMutex);
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800424 mTextureCache.erase(bufferId);
425 mProtectedTextureCache.erase(bufferId);
John Reck67b1e2b2020-08-26 13:17:24 -0700426}
427
428status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
429 const std::vector<const LayerSettings*>& layers,
430 const sp<GraphicBuffer>& buffer,
431 const bool useFramebufferCache,
432 base::unique_fd&& bufferFence, base::unique_fd* drawFence) {
433 ATRACE_NAME("SkiaGL::drawLayers");
434 std::lock_guard<std::mutex> lock(mRenderingMutex);
435 if (layers.empty()) {
436 ALOGV("Drawing empty layer stack");
437 return NO_ERROR;
438 }
439
440 if (bufferFence.get() >= 0) {
441 // Duplicate the fence for passing to waitFence.
442 base::unique_fd bufferFenceDup(dup(bufferFence.get()));
443 if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
444 ATRACE_NAME("Waiting before draw");
445 sync_wait(bufferFence.get(), -1);
446 }
447 }
448 if (buffer == nullptr) {
449 ALOGE("No output buffer provided. Aborting GPU composition.");
450 return BAD_VALUE;
451 }
452
Lucas Dupind508e472020-11-04 04:32:06 +0000453 auto grContext = mInProtectedContext ? mProtectedGrContext : mGrContext;
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800454 auto& cache = mInProtectedContext ? mProtectedTextureCache : mTextureCache;
John Reck67b1e2b2020-08-26 13:17:24 -0700455 AHardwareBuffer_Desc bufferDesc;
456 AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc);
John Reck67b1e2b2020-08-26 13:17:24 -0700457 LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE),
458 "missing usage");
459
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800460 std::shared_ptr<AutoBackendTexture::LocalRef> surfaceTextureRef = nullptr;
John Reck67b1e2b2020-08-26 13:17:24 -0700461 if (useFramebufferCache) {
Lucas Dupind508e472020-11-04 04:32:06 +0000462 auto iter = cache.find(buffer->getId());
463 if (iter != cache.end()) {
John Reck67b1e2b2020-08-26 13:17:24 -0700464 ALOGV("Cache hit!");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800465 surfaceTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700466 }
467 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800468
469 if (surfaceTextureRef == nullptr || surfaceTextureRef->getTexture() == nullptr) {
470 surfaceTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
471 surfaceTextureRef->setTexture(
472 new AutoBackendTexture(mGrContext.get(), buffer->toAHardwareBuffer(), true));
473 if (useFramebufferCache) {
John Reck67b1e2b2020-08-26 13:17:24 -0700474 ALOGD("Adding to cache");
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800475 cache.insert({buffer->getId(), surfaceTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700476 }
477 }
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800478
479 sk_sp<SkSurface> surface =
480 surfaceTextureRef->getTexture()->getOrCreateSurface(mUseColorManagement
481 ? display.outputDataspace
482 : ui::Dataspace::SRGB,
483 mGrContext.get());
Alec Mouri678245d2020-09-30 16:58:23 -0700484
John Reck67b1e2b2020-08-26 13:17:24 -0700485 auto canvas = surface->getCanvas();
Alec Mouri678245d2020-09-30 16:58:23 -0700486 // Clear the entire canvas with a transparent black to prevent ghost images.
487 canvas->clear(SK_ColorTRANSPARENT);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700488 canvas->save();
John Reck67b1e2b2020-08-26 13:17:24 -0700489
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700490 // Before doing any drawing, let's make sure that we'll start at the origin of the display.
491 // Some displays don't start at 0,0 for example when we're mirroring the screen. Also, virtual
492 // displays might have different scaling when compared to the physical screen.
Alec Mouri678245d2020-09-30 16:58:23 -0700493
494 canvas->clipRect(getSkRect(display.physicalDisplay));
Galia Peychevaf7889b32020-11-25 22:22:40 +0100495 canvas->translate(display.physicalDisplay.left, display.physicalDisplay.top);
Alec Mouri678245d2020-09-30 16:58:23 -0700496
497 const auto clipWidth = display.clip.width();
498 const auto clipHeight = display.clip.height();
499 auto rotatedClipWidth = clipWidth;
500 auto rotatedClipHeight = clipHeight;
501 // Scale is contingent on the rotation result.
502 if (display.orientation & ui::Transform::ROT_90) {
503 std::swap(rotatedClipWidth, rotatedClipHeight);
504 }
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700505 const auto scaleX = static_cast<SkScalar>(display.physicalDisplay.width()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700506 static_cast<SkScalar>(rotatedClipWidth);
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700507 const auto scaleY = static_cast<SkScalar>(display.physicalDisplay.height()) /
Alec Mouri678245d2020-09-30 16:58:23 -0700508 static_cast<SkScalar>(rotatedClipHeight);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100509 canvas->scale(scaleX, scaleY);
Alec Mouri678245d2020-09-30 16:58:23 -0700510
511 // Canvas rotation is done by centering the clip window at the origin, rotating, translating
512 // back so that the top left corner of the clip is at (0, 0).
Galia Peychevaf7889b32020-11-25 22:22:40 +0100513 canvas->translate(rotatedClipWidth / 2, rotatedClipHeight / 2);
514 canvas->rotate(toDegrees(display.orientation));
515 canvas->translate(-clipWidth / 2, -clipHeight / 2);
516 canvas->translate(-display.clip.left, -display.clip.top);
John Reck67b1e2b2020-08-26 13:17:24 -0700517 for (const auto& layer : layers) {
Galia Peychevaf7889b32020-11-25 22:22:40 +0100518 canvas->save();
519
520 // Layers have a local transform that should be applied to them
521 canvas->concat(getSkM44(layer->geometry.positionTransform).asM33());
Galia Peycheva6c460652020-11-03 19:42:42 +0100522
Lucas Dupin21f348e2020-09-16 17:31:26 -0700523 SkPaint paint;
524 const auto& bounds = layer->geometry.boundaries;
Lucas Dupin3f11e922020-09-22 17:31:04 -0700525 const auto dest = getSkRect(bounds);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100526 const auto layerRect = canvas->getTotalMatrix().mapRect(dest);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700527 std::unordered_map<uint32_t, sk_sp<SkSurface>> cachedBlurs;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700528 if (mBlurFilter) {
529 if (layer->backgroundBlurRadius > 0) {
530 ATRACE_NAME("BackgroundBlur");
Galia Peycheva80116e52020-11-06 11:57:25 +0100531 auto blurredSurface = mBlurFilter->generate(canvas, surface,
532 layer->backgroundBlurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700533 cachedBlurs[layer->backgroundBlurRadius] = blurredSurface;
Galia Peycheva80116e52020-11-06 11:57:25 +0100534
Galia Peychevaf7889b32020-11-25 22:22:40 +0100535 drawBlurRegion(canvas, getBlurRegion(layer), layerRect, blurredSurface);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700536 }
537 if (layer->blurRegions.size() > 0) {
538 for (auto region : layer->blurRegions) {
539 if (cachedBlurs[region.blurRadius]) {
540 continue;
541 }
542 ATRACE_NAME("BlurRegion");
Galia Peycheva6c460652020-11-03 19:42:42 +0100543 auto blurredSurface =
544 mBlurFilter->generate(canvas, surface, region.blurRadius, layerRect);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700545 cachedBlurs[region.blurRadius] = blurredSurface;
546 }
547 }
Lucas Dupinf4cb4a02020-09-22 14:19:26 -0700548 }
549
John Reck67b1e2b2020-08-26 13:17:24 -0700550 if (layer->source.buffer.buffer) {
551 ATRACE_NAME("DrawImage");
552 const auto& item = layer->source.buffer;
Alec Mouri678245d2020-09-30 16:58:23 -0700553 const auto bufferWidth = item.buffer->getBounds().width();
554 const auto bufferHeight = item.buffer->getBounds().height();
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800555 std::shared_ptr<AutoBackendTexture::LocalRef> imageTextureRef = nullptr;
556 auto iter = mTextureCache.find(item.buffer->getId());
557 if (iter != mTextureCache.end()) {
558 imageTextureRef = iter->second;
John Reck67b1e2b2020-08-26 13:17:24 -0700559 } else {
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800560 imageTextureRef = std::make_shared<AutoBackendTexture::LocalRef>();
561 imageTextureRef->setTexture(new AutoBackendTexture(mGrContext.get(),
562 item.buffer->toAHardwareBuffer(),
563 false));
564 mTextureCache.insert({buffer->getId(), imageTextureRef});
John Reck67b1e2b2020-08-26 13:17:24 -0700565 }
Alec Mouri1a4d0642020-11-13 17:42:01 -0800566
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800567 sk_sp<SkImage> image =
568 imageTextureRef->getTexture()
569 ->makeImage(mUseColorManagement
Alec Mouri1a4d0642020-11-13 17:42:01 -0800570 ? (needsLinearEffect(layer->colorTransform,
571 layer->sourceDataspace,
572 display.outputDataspace)
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800573 // If we need to map to linear space,
574 // then mark the source image with the
575 // same colorspace as the destination
576 // surface so that Skia's color
577 // management is a no-op.
578 ? display.outputDataspace
579 : layer->sourceDataspace)
580 : ui::Dataspace::SRGB,
581 item.isOpaque ? kOpaque_SkAlphaType
582 : (item.usePremultipliedAlpha
583 ? kPremul_SkAlphaType
584 : kUnpremul_SkAlphaType),
585 mGrContext.get());
Lucas Dupin21f348e2020-09-16 17:31:26 -0700586 SkMatrix matrix;
587 if (layer->geometry.roundedCornersRadius > 0) {
588 const auto roundedRect = getRoundedRect(layer);
589 matrix.setTranslate(roundedRect.getBounds().left() - dest.left(),
590 roundedRect.getBounds().top() - dest.top());
591 } else {
592 matrix.setIdentity();
593 }
Alec Mouri678245d2020-09-30 16:58:23 -0700594
595 auto texMatrix = getSkM44(item.textureTransform).asM33();
Huihong Luo42b86c12020-10-29 13:00:13 -0700596
597 // b/171404534, scale to fix the layer
598 matrix.postScale(bounds.getWidth() / bufferWidth, bounds.getHeight() / bufferHeight);
599
Alec Mouri678245d2020-09-30 16:58:23 -0700600 // textureTansform was intended to be passed directly into a shader, so when
601 // building the total matrix with the textureTransform we need to first
602 // normalize it, then apply the textureTransform, then scale back up.
603 matrix.postScale(1.0f / bufferWidth, 1.0f / bufferHeight);
604
605 auto rotatedBufferWidth = bufferWidth;
606 auto rotatedBufferHeight = bufferHeight;
607
608 // Swap the buffer width and height if we're rotating, so that we
609 // scale back up by the correct factors post-rotation.
610 if (texMatrix.getSkewX() <= -0.5f || texMatrix.getSkewX() >= 0.5f) {
611 std::swap(rotatedBufferWidth, rotatedBufferHeight);
612 // TODO: clean this up.
613 // GLESRenderEngine specifies its texture coordinates in
614 // CW orientation under OpenGL conventions, when they probably should have
615 // been CCW instead. The net result is that orientation
616 // transforms are applied in the reverse
617 // direction to render the correct result, because SurfaceFlinger uses the inverse
618 // of the display transform to correct for that. But this means that
619 // the tex transform passed by SkiaGLRenderEngine will rotate
620 // individual layers in the reverse orientation. Hack around it
621 // by injected a 180 degree rotation, but ultimately this is
622 // a bug in how SurfaceFlinger invokes the RenderEngine
623 // interface, so the proper fix should live there, and GLESRenderEngine
624 // should be fixed accordingly.
625 matrix.postRotate(180, 0.5, 0.5);
626 }
627
628 matrix.postConcat(texMatrix);
629 matrix.postScale(rotatedBufferWidth, rotatedBufferHeight);
Ana Krulecb7b28b22020-11-23 14:48:58 -0800630 sk_sp<SkShader> shader;
631
632 if (layer->source.buffer.useTextureFiltering) {
633 shader = image->makeShader(SkTileMode::kClamp, SkTileMode::kClamp,
634 SkSamplingOptions(
635 {SkFilterMode::kLinear, SkMipmapMode::kNone}),
636 &matrix);
637 } else {
638 shader = image->makeShader(matrix);
639 }
Alec Mouri029d1952020-10-12 10:37:08 -0700640
641 if (mUseColorManagement &&
Alec Mouri1a4d0642020-11-13 17:42:01 -0800642 needsLinearEffect(layer->colorTransform, layer->sourceDataspace,
643 display.outputDataspace)) {
Alec Mouri029d1952020-10-12 10:37:08 -0700644 LinearEffect effect = LinearEffect{.inputDataspace = layer->sourceDataspace,
645 .outputDataspace = display.outputDataspace,
646 .undoPremultipliedAlpha = !item.isOpaque &&
647 item.usePremultipliedAlpha};
Alec Mouric16a77d2020-11-13 13:20:11 -0800648
649 auto effectIter = mRuntimeEffects.find(effect);
650 sk_sp<SkRuntimeEffect> runtimeEffect = nullptr;
651 if (effectIter == mRuntimeEffects.end()) {
652 runtimeEffect = buildRuntimeEffect(effect);
653 mRuntimeEffects.insert({effect, runtimeEffect});
654 } else {
655 runtimeEffect = effectIter->second;
656 }
657
Alec Mouri029d1952020-10-12 10:37:08 -0700658 paint.setShader(createLinearEffectShader(shader, effect, runtimeEffect,
Alec Mouri1a4d0642020-11-13 17:42:01 -0800659 layer->colorTransform,
Alec Mouri029d1952020-10-12 10:37:08 -0700660 display.maxLuminance,
661 layer->source.buffer.maxMasteringLuminance,
662 layer->source.buffer.maxContentLuminance));
663 } else {
664 paint.setShader(shader);
665 }
Ana Krulec1768bd22020-11-23 14:51:31 -0800666 // Make sure to take into the account the alpha set on the layer.
667 paint.setAlphaf(layer->alpha);
John Reck67b1e2b2020-08-26 13:17:24 -0700668 } else {
669 ATRACE_NAME("DrawColor");
John Reck67b1e2b2020-08-26 13:17:24 -0700670 const auto color = layer->source.solidColor;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700671 paint.setShader(SkShaders::Color(SkColor4f{.fR = color.r,
672 .fG = color.g,
673 .fB = color.b,
674 layer->alpha},
675 nullptr));
John Reck67b1e2b2020-08-26 13:17:24 -0700676 }
Lucas Dupin21f348e2020-09-16 17:31:26 -0700677
Alec Mourib34f0b72020-10-02 13:18:34 -0700678 paint.setColorFilter(SkColorFilters::Matrix(toSkColorMatrix(display.colorTransform)));
679
Lucas Dupinc3800b82020-10-02 16:24:48 -0700680 for (const auto effectRegion : layer->blurRegions) {
Galia Peychevaf7889b32020-11-25 22:22:40 +0100681 drawBlurRegion(canvas, effectRegion, layerRect, cachedBlurs[effectRegion.blurRadius]);
Lucas Dupinc3800b82020-10-02 16:24:48 -0700682 }
683
Lucas Dupin3f11e922020-09-22 17:31:04 -0700684 if (layer->shadow.length > 0) {
685 const auto rect = layer->geometry.roundedCornersRadius > 0
686 ? getSkRect(layer->geometry.roundedCornersCrop)
687 : dest;
688 drawShadow(canvas, rect, layer->geometry.roundedCornersRadius, layer->shadow);
689 }
690
Lucas Dupin21f348e2020-09-16 17:31:26 -0700691 if (layer->geometry.roundedCornersRadius > 0) {
692 canvas->drawRRect(getRoundedRect(layer), paint);
693 } else {
694 canvas->drawRect(dest, paint);
695 }
Lucas Dupinc3800b82020-10-02 16:24:48 -0700696
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700697 canvas->restore();
John Reck67b1e2b2020-08-26 13:17:24 -0700698 }
699 {
700 ATRACE_NAME("flush surface");
701 surface->flush();
702 }
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700703 canvas->restore();
John Reck67b1e2b2020-08-26 13:17:24 -0700704
705 if (drawFence != nullptr) {
706 *drawFence = flush();
707 }
708
709 // If flush failed or we don't support native fences, we need to force the
710 // gl command stream to be executed.
711 bool requireSync = drawFence == nullptr || drawFence->get() < 0;
712 if (requireSync) {
713 ATRACE_BEGIN("Submit(sync=true)");
714 } else {
715 ATRACE_BEGIN("Submit(sync=false)");
716 }
Lucas Dupind508e472020-11-04 04:32:06 +0000717 bool success = grContext->submit(requireSync);
John Reck67b1e2b2020-08-26 13:17:24 -0700718 ATRACE_END();
719 if (!success) {
720 ALOGE("Failed to flush RenderEngine commands");
721 // Chances are, something illegal happened (either the caller passed
722 // us bad parameters, or we messed up our shader generation).
723 return INVALID_OPERATION;
724 }
725
726 // checkErrors();
727 return NO_ERROR;
728}
729
Lucas Dupin3f11e922020-09-22 17:31:04 -0700730inline SkRect SkiaGLRenderEngine::getSkRect(const FloatRect& rect) {
731 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
732}
733
734inline SkRect SkiaGLRenderEngine::getSkRect(const Rect& rect) {
735 return SkRect::MakeLTRB(rect.left, rect.top, rect.right, rect.bottom);
736}
737
Lucas Dupin21f348e2020-09-16 17:31:26 -0700738inline SkRRect SkiaGLRenderEngine::getRoundedRect(const LayerSettings* layer) {
Ana Krulec89717602020-12-04 12:48:05 -0800739 const auto rect = getSkRect(layer->geometry.boundaries);
Lucas Dupin21f348e2020-09-16 17:31:26 -0700740 const auto cornerRadius = layer->geometry.roundedCornersRadius;
741 return SkRRect::MakeRectXY(rect, cornerRadius, cornerRadius);
742}
743
Galia Peycheva80116e52020-11-06 11:57:25 +0100744inline BlurRegion SkiaGLRenderEngine::getBlurRegion(const LayerSettings* layer) {
745 const auto rect = getSkRect(layer->geometry.boundaries);
746 const auto cornersRadius = layer->geometry.roundedCornersRadius;
747 return BlurRegion{.blurRadius = static_cast<uint32_t>(layer->backgroundBlurRadius),
748 .cornerRadiusTL = cornersRadius,
749 .cornerRadiusTR = cornersRadius,
750 .cornerRadiusBL = cornersRadius,
751 .cornerRadiusBR = cornersRadius,
752 .alpha = 1,
753 .left = static_cast<int>(rect.fLeft),
754 .top = static_cast<int>(rect.fTop),
755 .right = static_cast<int>(rect.fRight),
756 .bottom = static_cast<int>(rect.fBottom)};
757}
758
Lucas Dupin3f11e922020-09-22 17:31:04 -0700759inline SkColor SkiaGLRenderEngine::getSkColor(const vec4& color) {
760 return SkColorSetARGB(color.a * 255, color.r * 255, color.g * 255, color.b * 255);
761}
762
Lucas Dupinbb1a1d42020-09-18 15:17:02 -0700763inline SkM44 SkiaGLRenderEngine::getSkM44(const mat4& matrix) {
764 return SkM44(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
765 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
766 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
767 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
768}
769
Lucas Dupin3f11e922020-09-22 17:31:04 -0700770inline SkPoint3 SkiaGLRenderEngine::getSkPoint3(const vec3& vector) {
771 return SkPoint3::Make(vector.x, vector.y, vector.z);
772}
773
John Reck67b1e2b2020-08-26 13:17:24 -0700774size_t SkiaGLRenderEngine::getMaxTextureSize() const {
775 return mGrContext->maxTextureSize();
776}
777
778size_t SkiaGLRenderEngine::getMaxViewportDims() const {
779 return mGrContext->maxRenderTargetSize();
780}
781
Lucas Dupin3f11e922020-09-22 17:31:04 -0700782void SkiaGLRenderEngine::drawShadow(SkCanvas* canvas, const SkRect& casterRect, float cornerRadius,
783 const ShadowSettings& settings) {
784 ATRACE_CALL();
785 const float casterZ = settings.length / 2.0f;
786 const auto shadowShape = cornerRadius > 0
787 ? SkPath::RRect(SkRRect::MakeRectXY(casterRect, cornerRadius, cornerRadius))
788 : SkPath::Rect(casterRect);
789 const auto flags =
790 settings.casterIsTranslucent ? kTransparentOccluder_ShadowFlag : kNone_ShadowFlag;
791
792 SkShadowUtils::DrawShadow(canvas, shadowShape, SkPoint3::Make(0, 0, casterZ),
793 getSkPoint3(settings.lightPos), settings.lightRadius,
794 getSkColor(settings.ambientColor), getSkColor(settings.spotColor),
795 flags);
796}
797
Lucas Dupinc3800b82020-10-02 16:24:48 -0700798void SkiaGLRenderEngine::drawBlurRegion(SkCanvas* canvas, const BlurRegion& effectRegion,
Galia Peychevaf7889b32020-11-25 22:22:40 +0100799 const SkRect& layerRect, sk_sp<SkSurface> blurredSurface) {
Lucas Dupinc3800b82020-10-02 16:24:48 -0700800 ATRACE_CALL();
Galia Peycheva80116e52020-11-06 11:57:25 +0100801
Lucas Dupinc3800b82020-10-02 16:24:48 -0700802 SkPaint paint;
Lyn Han1bdedb32020-11-23 20:33:57 +0000803 paint.setAlpha(static_cast<int>(effectRegion.alpha * 255));
Robin Leef180f412020-12-07 02:51:41 +0100804 const auto matrix = getBlurShaderTransform(canvas, layerRect);
Galia Peychevaf7889b32020-11-25 22:22:40 +0100805 paint.setShader(blurredSurface->makeImageSnapshot()->makeShader(
Robin Leef180f412020-12-07 02:51:41 +0100806 SkTileMode::kClamp,
807 SkTileMode::kClamp,
808 SkSamplingOptions({SkFilterMode::kLinear, SkMipmapMode::kNone}),
809 &matrix));
Lucas Dupinc3800b82020-10-02 16:24:48 -0700810
Galia Peycheva80116e52020-11-06 11:57:25 +0100811 auto rect = SkRect::MakeLTRB(effectRegion.left, effectRegion.top, effectRegion.right,
812 effectRegion.bottom);
Galia Peycheva80116e52020-11-06 11:57:25 +0100813
Lucas Dupinc3800b82020-10-02 16:24:48 -0700814 if (effectRegion.cornerRadiusTL > 0 || effectRegion.cornerRadiusTR > 0 ||
815 effectRegion.cornerRadiusBL > 0 || effectRegion.cornerRadiusBR > 0) {
816 const SkVector radii[4] =
817 {SkVector::Make(effectRegion.cornerRadiusTL, effectRegion.cornerRadiusTL),
818 SkVector::Make(effectRegion.cornerRadiusTR, effectRegion.cornerRadiusTR),
819 SkVector::Make(effectRegion.cornerRadiusBL, effectRegion.cornerRadiusBL),
820 SkVector::Make(effectRegion.cornerRadiusBR, effectRegion.cornerRadiusBR)};
821 SkRRect roundedRect;
822 roundedRect.setRectRadii(rect, radii);
823 canvas->drawRRect(roundedRect, paint);
824 } else {
825 canvas->drawRect(rect, paint);
826 }
827}
828
Galia Peychevaf7889b32020-11-25 22:22:40 +0100829SkMatrix SkiaGLRenderEngine::getBlurShaderTransform(const SkCanvas* canvas,
830 const SkRect& layerRect) {
831 // 1. Apply the blur shader matrix, which scales up the blured surface to its real size
832 auto matrix = mBlurFilter->getShaderMatrix();
833 // 2. Since the blurred surface has the size of the layer, we align it with the
834 // top left corner of the layer position.
835 matrix.postConcat(SkMatrix::Translate(layerRect.fLeft, layerRect.fTop));
836 // 3. Finally, apply the inverse canvas matrix. The snapshot made in the BlurFilter is in the
837 // original surface orientation. The inverse matrix has to be applied to align the blur
838 // surface with the current orientation/position of the canvas.
839 SkMatrix drawInverse;
840 if (canvas->getTotalMatrix().invert(&drawInverse)) {
841 matrix.postConcat(drawInverse);
842 }
843
844 return matrix;
845}
846
John Reck67b1e2b2020-08-26 13:17:24 -0700847EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
848 EGLContext shareContext, bool useContextPriority,
849 Protection protection) {
850 EGLint renderableType = 0;
851 if (config == EGL_NO_CONFIG_KHR) {
852 renderableType = EGL_OPENGL_ES3_BIT;
853 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
854 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
855 }
856 EGLint contextClientVersion = 0;
857 if (renderableType & EGL_OPENGL_ES3_BIT) {
858 contextClientVersion = 3;
859 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
860 contextClientVersion = 2;
861 } else if (renderableType & EGL_OPENGL_ES_BIT) {
862 contextClientVersion = 1;
863 } else {
864 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
865 }
866
867 std::vector<EGLint> contextAttributes;
868 contextAttributes.reserve(7);
869 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
870 contextAttributes.push_back(contextClientVersion);
871 if (useContextPriority) {
872 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
873 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
874 }
875 if (protection == Protection::PROTECTED) {
876 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
877 contextAttributes.push_back(EGL_TRUE);
878 }
879 contextAttributes.push_back(EGL_NONE);
880
881 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
882
883 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
884 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
885 // EGL_NO_CONTEXT so that we can abort.
886 if (config != EGL_NO_CONFIG_KHR) {
887 return context;
888 }
889 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
890 // should try to fall back to GLES 2.
891 contextAttributes[1] = 2;
892 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
893 }
894
895 return context;
896}
897
898EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
899 EGLConfig config, int hwcFormat,
900 Protection protection) {
901 EGLConfig placeholderConfig = config;
902 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
903 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
904 }
905 std::vector<EGLint> attributes;
906 attributes.reserve(7);
907 attributes.push_back(EGL_WIDTH);
908 attributes.push_back(1);
909 attributes.push_back(EGL_HEIGHT);
910 attributes.push_back(1);
911 if (protection == Protection::PROTECTED) {
912 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
913 attributes.push_back(EGL_TRUE);
914 }
915 attributes.push_back(EGL_NONE);
916
917 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
918}
919
Alec Mouric7f6c8b2020-11-09 18:35:20 -0800920void SkiaGLRenderEngine::cleanFramebufferCache() {}
John Reck67b1e2b2020-08-26 13:17:24 -0700921
922} // namespace skia
923} // namespace renderengine
Galia Peycheva6c460652020-11-03 19:42:42 +0100924} // namespace android