blob: 94ba15392f9e471002fd25cd0088ce8e1c869224 [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
18#undef LOG_TAG
19#define LOG_TAG "RenderEngine"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21
22#include <cmath>
23
24#include <EGL/egl.h>
25#include <EGL/eglext.h>
26#include <GLES2/gl2.h>
27#include <sync/sync.h>
28#include <ui/GraphicBuffer.h>
29#include <utils/Trace.h>
30#include "../gl/GLExtensions.h"
31#include "SkiaGLRenderEngine.h"
32
33#include <GrContextOptions.h>
34#include <gl/GrGLInterface.h>
35
36#include <SkCanvas.h>
37#include <SkImage.h>
38#include <SkSurface.h>
39
40extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
41
42bool checkGlError(const char* op, int lineNumber);
43
44namespace android {
45namespace renderengine {
46namespace skia {
47
48static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
49 EGLint wanted, EGLConfig* outConfig) {
50 EGLint numConfigs = -1, n = 0;
51 eglGetConfigs(dpy, nullptr, 0, &numConfigs);
52 std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
53 eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
54 configs.resize(n);
55
56 if (!configs.empty()) {
57 if (attribute != EGL_NONE) {
58 for (EGLConfig config : configs) {
59 EGLint value = 0;
60 eglGetConfigAttrib(dpy, config, attribute, &value);
61 if (wanted == value) {
62 *outConfig = config;
63 return NO_ERROR;
64 }
65 }
66 } else {
67 // just pick the first one
68 *outConfig = configs[0];
69 return NO_ERROR;
70 }
71 }
72
73 return NAME_NOT_FOUND;
74}
75
76static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
77 EGLConfig* config) {
78 // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
79 // it is to be used with WIFI displays
80 status_t err;
81 EGLint wantedAttribute;
82 EGLint wantedAttributeValue;
83
84 std::vector<EGLint> attribs;
85 if (renderableType) {
86 const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
87 const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
88
89 // Default to 8 bits per channel.
90 const EGLint tmpAttribs[] = {
91 EGL_RENDERABLE_TYPE,
92 renderableType,
93 EGL_RECORDABLE_ANDROID,
94 EGL_TRUE,
95 EGL_SURFACE_TYPE,
96 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
97 EGL_FRAMEBUFFER_TARGET_ANDROID,
98 EGL_TRUE,
99 EGL_RED_SIZE,
100 is1010102 ? 10 : 8,
101 EGL_GREEN_SIZE,
102 is1010102 ? 10 : 8,
103 EGL_BLUE_SIZE,
104 is1010102 ? 10 : 8,
105 EGL_ALPHA_SIZE,
106 is1010102 ? 2 : 8,
107 EGL_NONE,
108 };
109 std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
110 std::back_inserter(attribs));
111 wantedAttribute = EGL_NONE;
112 wantedAttributeValue = EGL_NONE;
113 } else {
114 // if no renderable type specified, fallback to a simplified query
115 wantedAttribute = EGL_NATIVE_VISUAL_ID;
116 wantedAttributeValue = format;
117 }
118
119 err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
120 config);
121 if (err == NO_ERROR) {
122 EGLint caveat;
123 if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
124 ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
125 }
126
127 return err;
128}
129
130std::unique_ptr<SkiaGLRenderEngine> SkiaGLRenderEngine::create(
131 const RenderEngineCreationArgs& args) {
132 // initialize EGL for the default display
133 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
134 if (!eglInitialize(display, nullptr, nullptr)) {
135 LOG_ALWAYS_FATAL("failed to initialize EGL");
136 }
137
138 const auto eglVersion = eglQueryStringImplementationANDROID(display, EGL_VERSION);
139 if (!eglVersion) {
140 checkGlError(__FUNCTION__, __LINE__);
141 LOG_ALWAYS_FATAL("eglQueryStringImplementationANDROID(EGL_VERSION) failed");
142 }
143
144 const auto eglExtensions = eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS);
145 if (!eglExtensions) {
146 checkGlError(__FUNCTION__, __LINE__);
147 LOG_ALWAYS_FATAL("eglQueryStringImplementationANDROID(EGL_EXTENSIONS) failed");
148 }
149
150 auto& extensions = gl::GLExtensions::getInstance();
151 extensions.initWithEGLStrings(eglVersion, eglExtensions);
152
153 // The code assumes that ES2 or later is available if this extension is
154 // supported.
155 EGLConfig config = EGL_NO_CONFIG_KHR;
156 if (!extensions.hasNoConfigContext()) {
157 config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
158 }
159
160 bool useContextPriority =
161 extensions.hasContextPriority() && args.contextPriority == ContextPriority::HIGH;
162 EGLContext protectedContext = EGL_NO_CONTEXT;
163 if (args.enableProtectedContext && extensions.hasProtectedContent()) {
164 protectedContext = createEglContext(display, config, nullptr, useContextPriority,
165 Protection::PROTECTED);
166 ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
167 }
168
169 EGLContext ctxt = createEglContext(display, config, protectedContext, useContextPriority,
170 Protection::UNPROTECTED);
171
172 // if can't create a GL context, we can only abort.
173 LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
174
175 EGLSurface placeholder = EGL_NO_SURFACE;
176 if (!extensions.hasSurfacelessContext()) {
177 placeholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
178 Protection::UNPROTECTED);
179 LOG_ALWAYS_FATAL_IF(placeholder == EGL_NO_SURFACE, "can't create placeholder pbuffer");
180 }
181 EGLBoolean success = eglMakeCurrent(display, placeholder, placeholder, ctxt);
182 LOG_ALWAYS_FATAL_IF(!success, "can't make placeholder pbuffer current");
183 extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
184 glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
185
186 EGLSurface protectedPlaceholder = EGL_NO_SURFACE;
187 if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
188 protectedPlaceholder = createPlaceholderEglPbufferSurface(display, config, args.pixelFormat,
189 Protection::PROTECTED);
190 ALOGE_IF(protectedPlaceholder == EGL_NO_SURFACE,
191 "can't create protected placeholder pbuffer");
192 }
193
194 // initialize the renderer while GL is current
195 std::unique_ptr<SkiaGLRenderEngine> engine =
196 std::make_unique<SkiaGLRenderEngine>(args, display, config, ctxt, placeholder,
197 protectedContext, protectedPlaceholder);
198
199 ALOGI("OpenGL ES informations:");
200 ALOGI("vendor : %s", extensions.getVendor());
201 ALOGI("renderer : %s", extensions.getRenderer());
202 ALOGI("version : %s", extensions.getVersion());
203 ALOGI("extensions: %s", extensions.getExtensions());
204 ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
205 ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
206
207 return engine;
208}
209
210EGLConfig SkiaGLRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
211 status_t err;
212 EGLConfig config;
213
214 // First try to get an ES3 config
215 err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
216 if (err != NO_ERROR) {
217 // If ES3 fails, try to get an ES2 config
218 err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
219 if (err != NO_ERROR) {
220 // If ES2 still doesn't work, probably because we're on the emulator.
221 // try a simplified query
222 ALOGW("no suitable EGLConfig found, trying a simpler query");
223 err = selectEGLConfig(display, format, 0, &config);
224 if (err != NO_ERROR) {
225 // this EGL is too lame for android
226 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
227 }
228 }
229 }
230
231 if (logConfig) {
232 // print some debugging info
233 EGLint r, g, b, a;
234 eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
235 eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
236 eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
237 eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
238 ALOGI("EGL information:");
239 ALOGI("vendor : %s", eglQueryString(display, EGL_VENDOR));
240 ALOGI("version : %s", eglQueryString(display, EGL_VERSION));
241 ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
242 ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
243 ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
244 }
245
246 return config;
247}
248
249SkiaGLRenderEngine::SkiaGLRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
250 EGLConfig config, EGLContext ctxt, EGLSurface placeholder,
251 EGLContext protectedContext, EGLSurface protectedPlaceholder)
252 : renderengine::skia::SkiaRenderEngine(args),
253 mEGLDisplay(display),
254 mEGLConfig(config),
255 mEGLContext(ctxt),
256 mPlaceholderSurface(placeholder),
257 mProtectedEGLContext(protectedContext),
258 mProtectedPlaceholderSurface(protectedPlaceholder) {
259 // Suppress unused field warnings for things we definitely will need/use
260 // These EGL fields will all be needed for toggling between protected & unprotected contexts
261 // Or we need different RE instances for that
262 (void)mEGLDisplay;
263 (void)mEGLConfig;
264 (void)mEGLContext;
265 (void)mPlaceholderSurface;
266 (void)mProtectedEGLContext;
267 (void)mProtectedPlaceholderSurface;
268
269 sk_sp<const GrGLInterface> glInterface(GrGLCreateNativeInterface());
270 LOG_ALWAYS_FATAL_IF(!glInterface.get());
271
272 GrContextOptions options;
273 options.fPreferExternalImagesOverES3 = true;
274 options.fDisableDistanceFieldPaths = true;
275 mGrContext = GrDirectContext::MakeGL(std::move(glInterface), options);
276}
277
278base::unique_fd SkiaGLRenderEngine::flush() {
279 ATRACE_CALL();
280 if (!gl::GLExtensions::getInstance().hasNativeFenceSync()) {
281 return base::unique_fd();
282 }
283
284 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
285 if (sync == EGL_NO_SYNC_KHR) {
286 ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
287 return base::unique_fd();
288 }
289
290 // native fence fd will not be populated until flush() is done.
291 glFlush();
292
293 // get the fence fd
294 base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
295 eglDestroySyncKHR(mEGLDisplay, sync);
296 if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
297 ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
298 }
299
300 return fenceFd;
301}
302
303bool SkiaGLRenderEngine::waitFence(base::unique_fd fenceFd) {
304 if (!gl::GLExtensions::getInstance().hasNativeFenceSync() ||
305 !gl::GLExtensions::getInstance().hasWaitSync()) {
306 return false;
307 }
308
309 // release the fd and transfer the ownership to EGLSync
310 EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
311 EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
312 if (sync == EGL_NO_SYNC_KHR) {
313 ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
314 return false;
315 }
316
317 // XXX: The spec draft is inconsistent as to whether this should return an
318 // EGLint or void. Ignore the return value for now, as it's not strictly
319 // needed.
320 eglWaitSyncKHR(mEGLDisplay, sync, 0);
321 EGLint error = eglGetError();
322 eglDestroySyncKHR(mEGLDisplay, sync);
323 if (error != EGL_SUCCESS) {
324 ALOGE("failed to wait for EGL native fence sync: %#x", error);
325 return false;
326 }
327
328 return true;
329}
330
331static bool hasUsage(const AHardwareBuffer_Desc& desc, uint64_t usage) {
332 return !!(desc.usage & usage);
333}
334
335void SkiaGLRenderEngine::unbindExternalTextureBuffer(uint64_t bufferId) {
336 std::lock_guard<std::mutex> lock(mRenderingMutex);
337 mImageCache.erase(bufferId);
338}
339
340status_t SkiaGLRenderEngine::drawLayers(const DisplaySettings& display,
341 const std::vector<const LayerSettings*>& layers,
342 const sp<GraphicBuffer>& buffer,
343 const bool useFramebufferCache,
344 base::unique_fd&& bufferFence, base::unique_fd* drawFence) {
345 ATRACE_NAME("SkiaGL::drawLayers");
346 std::lock_guard<std::mutex> lock(mRenderingMutex);
347 if (layers.empty()) {
348 ALOGV("Drawing empty layer stack");
349 return NO_ERROR;
350 }
351
352 if (bufferFence.get() >= 0) {
353 // Duplicate the fence for passing to waitFence.
354 base::unique_fd bufferFenceDup(dup(bufferFence.get()));
355 if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
356 ATRACE_NAME("Waiting before draw");
357 sync_wait(bufferFence.get(), -1);
358 }
359 }
360 if (buffer == nullptr) {
361 ALOGE("No output buffer provided. Aborting GPU composition.");
362 return BAD_VALUE;
363 }
364
365 AHardwareBuffer_Desc bufferDesc;
366 AHardwareBuffer_describe(buffer->toAHardwareBuffer(), &bufferDesc);
367
368 LOG_ALWAYS_FATAL_IF(!hasUsage(bufferDesc, AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE),
369 "missing usage");
370
371 sk_sp<SkSurface> surface;
372 if (useFramebufferCache) {
373 auto iter = mSurfaceCache.find(buffer->getId());
374 if (iter != mSurfaceCache.end()) {
375 ALOGV("Cache hit!");
376 surface = iter->second;
377 }
378 }
379 if (!surface) {
380 surface = SkSurface::MakeFromAHardwareBuffer(mGrContext.get(), buffer->toAHardwareBuffer(),
381 GrSurfaceOrigin::kTopLeft_GrSurfaceOrigin,
382 SkColorSpace::MakeSRGB(), nullptr);
383 if (useFramebufferCache && surface) {
384 ALOGD("Adding to cache");
385 mSurfaceCache.insert({buffer->getId(), surface});
386 }
387 }
388 if (!surface) {
389 ALOGE("Failed to make surface");
390 return BAD_VALUE;
391 }
392 auto canvas = surface->getCanvas();
393
394 canvas->clipRect(SkRect::MakeLTRB(display.clip.left, display.clip.top, display.clip.right,
395 display.clip.bottom));
396 canvas->drawColor(0, SkBlendMode::kSrc);
397 for (const auto& layer : layers) {
398 if (layer->source.buffer.buffer) {
399 ATRACE_NAME("DrawImage");
400 const auto& item = layer->source.buffer;
401 sk_sp<SkImage> image;
402 auto iter = mImageCache.find(item.buffer->getId());
403 if (iter != mImageCache.end()) {
404 image = iter->second;
405 } else {
406 image = SkImage::MakeFromAHardwareBuffer(item.buffer->toAHardwareBuffer(),
407 item.usePremultipliedAlpha
408 ? kPremul_SkAlphaType
409 : kUnpremul_SkAlphaType);
410 mImageCache.insert({item.buffer->getId(), image});
411 }
412 const auto& bounds = layer->geometry.boundaries;
413 SkRect dest = SkRect::MakeLTRB(bounds.left, bounds.top, bounds.right, bounds.bottom);
414 canvas->drawImageRect(image, dest, nullptr);
415 } else {
416 ATRACE_NAME("DrawColor");
417 SkPaint paint;
418 const auto color = layer->source.solidColor;
419 paint.setColor(SkColor4f{.fR = color.r, .fG = color.g, .fB = color.b, layer->alpha});
420 }
421 }
422 {
423 ATRACE_NAME("flush surface");
424 surface->flush();
425 }
426
427 if (drawFence != nullptr) {
428 *drawFence = flush();
429 }
430
431 // If flush failed or we don't support native fences, we need to force the
432 // gl command stream to be executed.
433 bool requireSync = drawFence == nullptr || drawFence->get() < 0;
434 if (requireSync) {
435 ATRACE_BEGIN("Submit(sync=true)");
436 } else {
437 ATRACE_BEGIN("Submit(sync=false)");
438 }
439 bool success = mGrContext->submit(requireSync);
440 ATRACE_END();
441 if (!success) {
442 ALOGE("Failed to flush RenderEngine commands");
443 // Chances are, something illegal happened (either the caller passed
444 // us bad parameters, or we messed up our shader generation).
445 return INVALID_OPERATION;
446 }
447
448 // checkErrors();
449 return NO_ERROR;
450}
451
452size_t SkiaGLRenderEngine::getMaxTextureSize() const {
453 return mGrContext->maxTextureSize();
454}
455
456size_t SkiaGLRenderEngine::getMaxViewportDims() const {
457 return mGrContext->maxRenderTargetSize();
458}
459
460EGLContext SkiaGLRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
461 EGLContext shareContext, bool useContextPriority,
462 Protection protection) {
463 EGLint renderableType = 0;
464 if (config == EGL_NO_CONFIG_KHR) {
465 renderableType = EGL_OPENGL_ES3_BIT;
466 } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
467 LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
468 }
469 EGLint contextClientVersion = 0;
470 if (renderableType & EGL_OPENGL_ES3_BIT) {
471 contextClientVersion = 3;
472 } else if (renderableType & EGL_OPENGL_ES2_BIT) {
473 contextClientVersion = 2;
474 } else if (renderableType & EGL_OPENGL_ES_BIT) {
475 contextClientVersion = 1;
476 } else {
477 LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
478 }
479
480 std::vector<EGLint> contextAttributes;
481 contextAttributes.reserve(7);
482 contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
483 contextAttributes.push_back(contextClientVersion);
484 if (useContextPriority) {
485 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
486 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
487 }
488 if (protection == Protection::PROTECTED) {
489 contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
490 contextAttributes.push_back(EGL_TRUE);
491 }
492 contextAttributes.push_back(EGL_NONE);
493
494 EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
495
496 if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
497 // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
498 // EGL_NO_CONTEXT so that we can abort.
499 if (config != EGL_NO_CONFIG_KHR) {
500 return context;
501 }
502 // If |config| is EGL_NO_CONFIG_KHR, we speculatively try to create GLES 3 context, so we
503 // should try to fall back to GLES 2.
504 contextAttributes[1] = 2;
505 context = eglCreateContext(display, config, shareContext, contextAttributes.data());
506 }
507
508 return context;
509}
510
511EGLSurface SkiaGLRenderEngine::createPlaceholderEglPbufferSurface(EGLDisplay display,
512 EGLConfig config, int hwcFormat,
513 Protection protection) {
514 EGLConfig placeholderConfig = config;
515 if (placeholderConfig == EGL_NO_CONFIG_KHR) {
516 placeholderConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
517 }
518 std::vector<EGLint> attributes;
519 attributes.reserve(7);
520 attributes.push_back(EGL_WIDTH);
521 attributes.push_back(1);
522 attributes.push_back(EGL_HEIGHT);
523 attributes.push_back(1);
524 if (protection == Protection::PROTECTED) {
525 attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
526 attributes.push_back(EGL_TRUE);
527 }
528 attributes.push_back(EGL_NONE);
529
530 return eglCreatePbufferSurface(display, placeholderConfig, attributes.data());
531}
532
533void SkiaGLRenderEngine::cleanFramebufferCache() {
534 mSurfaceCache.clear();
535}
536
537} // namespace skia
538} // namespace renderengine
539} // namespace android