1 /*
2  * Copyright 2013 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 #include "EGL/egl.h"
19 #undef LOG_TAG
20 #define LOG_TAG "RenderEngine"
21 #define ATRACE_TAG ATRACE_TAG_GRAPHICS
22 
23 #include <sched.h>
24 #include <cmath>
25 #include <fstream>
26 #include <sstream>
27 #include <unordered_set>
28 
29 #include <GLES2/gl2.h>
30 #include <GLES2/gl2ext.h>
31 #include <android-base/stringprintf.h>
32 #include <cutils/compiler.h>
33 #include <cutils/properties.h>
34 #include <gui/DebugEGLImageTracker.h>
35 #include <renderengine/Mesh.h>
36 #include <renderengine/Texture.h>
37 #include <renderengine/private/Description.h>
38 #include <sync/sync.h>
39 #include <ui/ColorSpace.h>
40 #include <ui/DebugUtils.h>
41 #include <ui/GraphicBuffer.h>
42 #include <ui/Rect.h>
43 #include <ui/Region.h>
44 #include <utils/KeyedVector.h>
45 #include <utils/Trace.h>
46 #include "GLESRenderEngine.h"
47 #include "GLExtensions.h"
48 #include "GLFramebuffer.h"
49 #include "GLImage.h"
50 #include "GLShadowVertexGenerator.h"
51 #include "Program.h"
52 #include "ProgramCache.h"
53 #include "filters/BlurFilter.h"
54 
checkGlError(const char * op,int lineNumber)55 bool checkGlError(const char* op, int lineNumber) {
56     bool errorFound = false;
57     GLint error = glGetError();
58     while (error != GL_NO_ERROR) {
59         errorFound = true;
60         error = glGetError();
61         ALOGV("after %s() (line # %d) glError (0x%x)\n", op, lineNumber, error);
62     }
63     return errorFound;
64 }
65 
66 static constexpr bool outputDebugPPMs = false;
67 
writePPM(const char * basename,GLuint width,GLuint height)68 void writePPM(const char* basename, GLuint width, GLuint height) {
69     ALOGV("writePPM #%s: %d x %d", basename, width, height);
70 
71     std::vector<GLubyte> pixels(width * height * 4);
72     std::vector<GLubyte> outBuffer(width * height * 3);
73 
74     // TODO(courtneygo): We can now have float formats, need
75     // to remove this code or update to support.
76     // Make returned pixels fit in uint32_t, one byte per component
77     glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
78     if (checkGlError(__FUNCTION__, __LINE__)) {
79         return;
80     }
81 
82     std::string filename(basename);
83     filename.append(".ppm");
84     std::ofstream file(filename.c_str(), std::ios::binary);
85     if (!file.is_open()) {
86         ALOGE("Unable to open file: %s", filename.c_str());
87         ALOGE("You may need to do: \"adb shell setenforce 0\" to enable "
88               "surfaceflinger to write debug images");
89         return;
90     }
91 
92     file << "P6\n";
93     file << width << "\n";
94     file << height << "\n";
95     file << 255 << "\n";
96 
97     auto ptr = reinterpret_cast<char*>(pixels.data());
98     auto outPtr = reinterpret_cast<char*>(outBuffer.data());
99     for (int y = height - 1; y >= 0; y--) {
100         char* data = ptr + y * width * sizeof(uint32_t);
101 
102         for (GLuint x = 0; x < width; x++) {
103             // Only copy R, G and B components
104             outPtr[0] = data[0];
105             outPtr[1] = data[1];
106             outPtr[2] = data[2];
107             data += sizeof(uint32_t);
108             outPtr += 3;
109         }
110     }
111     file.write(reinterpret_cast<char*>(outBuffer.data()), outBuffer.size());
112 }
113 
114 namespace android {
115 namespace renderengine {
116 namespace gl {
117 
118 class BindNativeBufferAsFramebuffer {
119 public:
BindNativeBufferAsFramebuffer(GLESRenderEngine & engine,ANativeWindowBuffer * buffer,const bool useFramebufferCache)120     BindNativeBufferAsFramebuffer(GLESRenderEngine& engine, ANativeWindowBuffer* buffer,
121                                   const bool useFramebufferCache)
122           : mEngine(engine), mFramebuffer(mEngine.getFramebufferForDrawing()), mStatus(NO_ERROR) {
123         mStatus = mFramebuffer->setNativeWindowBuffer(buffer, mEngine.isProtected(),
124                                                       useFramebufferCache)
125                 ? mEngine.bindFrameBuffer(mFramebuffer)
126                 : NO_MEMORY;
127     }
~BindNativeBufferAsFramebuffer()128     ~BindNativeBufferAsFramebuffer() {
129         mFramebuffer->setNativeWindowBuffer(nullptr, false, /*arbitrary*/ true);
130         mEngine.unbindFrameBuffer(mFramebuffer);
131     }
getStatus() const132     status_t getStatus() const { return mStatus; }
133 
134 private:
135     GLESRenderEngine& mEngine;
136     Framebuffer* mFramebuffer;
137     status_t mStatus;
138 };
139 
140 using base::StringAppendF;
141 using ui::Dataspace;
142 
selectConfigForAttribute(EGLDisplay dpy,EGLint const * attrs,EGLint attribute,EGLint wanted,EGLConfig * outConfig)143 static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
144                                          EGLint wanted, EGLConfig* outConfig) {
145     EGLint numConfigs = -1, n = 0;
146     eglGetConfigs(dpy, nullptr, 0, &numConfigs);
147     std::vector<EGLConfig> configs(numConfigs, EGL_NO_CONFIG_KHR);
148     eglChooseConfig(dpy, attrs, configs.data(), configs.size(), &n);
149     configs.resize(n);
150 
151     if (!configs.empty()) {
152         if (attribute != EGL_NONE) {
153             for (EGLConfig config : configs) {
154                 EGLint value = 0;
155                 eglGetConfigAttrib(dpy, config, attribute, &value);
156                 if (wanted == value) {
157                     *outConfig = config;
158                     return NO_ERROR;
159                 }
160             }
161         } else {
162             // just pick the first one
163             *outConfig = configs[0];
164             return NO_ERROR;
165         }
166     }
167 
168     return NAME_NOT_FOUND;
169 }
170 
selectEGLConfig(EGLDisplay display,EGLint format,EGLint renderableType,EGLConfig * config)171 static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
172                                 EGLConfig* config) {
173     // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
174     // it is to be used with WIFI displays
175     status_t err;
176     EGLint wantedAttribute;
177     EGLint wantedAttributeValue;
178 
179     std::vector<EGLint> attribs;
180     if (renderableType) {
181         const ui::PixelFormat pixelFormat = static_cast<ui::PixelFormat>(format);
182         const bool is1010102 = pixelFormat == ui::PixelFormat::RGBA_1010102;
183 
184         // Default to 8 bits per channel.
185         const EGLint tmpAttribs[] = {
186                 EGL_RENDERABLE_TYPE,
187                 renderableType,
188                 EGL_RECORDABLE_ANDROID,
189                 EGL_TRUE,
190                 EGL_SURFACE_TYPE,
191                 EGL_WINDOW_BIT | EGL_PBUFFER_BIT,
192                 EGL_FRAMEBUFFER_TARGET_ANDROID,
193                 EGL_TRUE,
194                 EGL_RED_SIZE,
195                 is1010102 ? 10 : 8,
196                 EGL_GREEN_SIZE,
197                 is1010102 ? 10 : 8,
198                 EGL_BLUE_SIZE,
199                 is1010102 ? 10 : 8,
200                 EGL_ALPHA_SIZE,
201                 is1010102 ? 2 : 8,
202                 EGL_NONE,
203         };
204         std::copy(tmpAttribs, tmpAttribs + (sizeof(tmpAttribs) / sizeof(EGLint)),
205                   std::back_inserter(attribs));
206         wantedAttribute = EGL_NONE;
207         wantedAttributeValue = EGL_NONE;
208     } else {
209         // if no renderable type specified, fallback to a simplified query
210         wantedAttribute = EGL_NATIVE_VISUAL_ID;
211         wantedAttributeValue = format;
212     }
213 
214     err = selectConfigForAttribute(display, attribs.data(), wantedAttribute, wantedAttributeValue,
215                                    config);
216     if (err == NO_ERROR) {
217         EGLint caveat;
218         if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
219             ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
220     }
221 
222     return err;
223 }
224 
createContextPriority(const RenderEngineCreationArgs & args)225 std::optional<RenderEngine::ContextPriority> GLESRenderEngine::createContextPriority(
226         const RenderEngineCreationArgs& args) {
227     if (!GLExtensions::getInstance().hasContextPriority()) {
228         return std::nullopt;
229     }
230 
231     switch (args.contextPriority) {
232         case RenderEngine::ContextPriority::REALTIME:
233             if (gl::GLExtensions::getInstance().hasRealtimePriority()) {
234                 return RenderEngine::ContextPriority::REALTIME;
235             } else {
236                 ALOGI("Realtime priority unsupported, degrading gracefully to high priority");
237                 return RenderEngine::ContextPriority::HIGH;
238             }
239         case RenderEngine::ContextPriority::HIGH:
240         case RenderEngine::ContextPriority::MEDIUM:
241         case RenderEngine::ContextPriority::LOW:
242             return args.contextPriority;
243         default:
244             return std::nullopt;
245     }
246 }
247 
create(const RenderEngineCreationArgs & args)248 std::unique_ptr<GLESRenderEngine> GLESRenderEngine::create(const RenderEngineCreationArgs& args) {
249     // initialize EGL for the default display
250     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
251     if (!eglInitialize(display, nullptr, nullptr)) {
252         LOG_ALWAYS_FATAL("failed to initialize EGL. EGL error=0x%x", eglGetError());
253     }
254 
255     const auto eglVersion = eglQueryString(display, EGL_VERSION);
256     if (!eglVersion) {
257         checkGlError(__FUNCTION__, __LINE__);
258         LOG_ALWAYS_FATAL("eglQueryString(EGL_VERSION) failed");
259     }
260 
261     // Use the Android impl to grab EGL_NV_context_priority_realtime
262     const auto eglExtensions = eglQueryString(display, EGL_EXTENSIONS);
263     if (!eglExtensions) {
264         checkGlError(__FUNCTION__, __LINE__);
265         LOG_ALWAYS_FATAL("eglQueryString(EGL_EXTENSIONS) failed");
266     }
267 
268     GLExtensions& extensions = GLExtensions::getInstance();
269     extensions.initWithEGLStrings(eglVersion, eglExtensions);
270 
271     // The code assumes that ES2 or later is available if this extension is
272     // supported.
273     EGLConfig config = EGL_NO_CONFIG;
274     if (!extensions.hasNoConfigContext()) {
275         config = chooseEglConfig(display, args.pixelFormat, /*logConfig*/ true);
276     }
277 
278     const std::optional<RenderEngine::ContextPriority> priority = createContextPriority(args);
279     EGLContext protectedContext = EGL_NO_CONTEXT;
280     if (args.enableProtectedContext && extensions.hasProtectedContent()) {
281         protectedContext =
282                 createEglContext(display, config, nullptr, priority, Protection::PROTECTED);
283         ALOGE_IF(protectedContext == EGL_NO_CONTEXT, "Can't create protected context");
284     }
285 
286     EGLContext ctxt =
287             createEglContext(display, config, protectedContext, priority, Protection::UNPROTECTED);
288 
289     // if can't create a GL context, we can only abort.
290     LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
291 
292     EGLSurface stub = EGL_NO_SURFACE;
293     if (!extensions.hasSurfacelessContext()) {
294         stub = createStubEglPbufferSurface(display, config, args.pixelFormat,
295                                            Protection::UNPROTECTED);
296         LOG_ALWAYS_FATAL_IF(stub == EGL_NO_SURFACE, "can't create stub pbuffer");
297     }
298     EGLBoolean success = eglMakeCurrent(display, stub, stub, ctxt);
299     LOG_ALWAYS_FATAL_IF(!success, "can't make stub pbuffer current");
300     extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
301                                  glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
302 
303     EGLSurface protectedStub = EGL_NO_SURFACE;
304     if (protectedContext != EGL_NO_CONTEXT && !extensions.hasSurfacelessContext()) {
305         protectedStub = createStubEglPbufferSurface(display, config, args.pixelFormat,
306                                                     Protection::PROTECTED);
307         ALOGE_IF(protectedStub == EGL_NO_SURFACE, "can't create protected stub pbuffer");
308     }
309 
310     // now figure out what version of GL did we actually get
311     GlesVersion version = parseGlesVersion(extensions.getVersion());
312 
313     LOG_ALWAYS_FATAL_IF(args.supportsBackgroundBlur && version < GLES_VERSION_3_0,
314         "Blurs require OpenGL ES 3.0. Please unset ro.surface_flinger.supports_background_blur");
315 
316     // initialize the renderer while GL is current
317     std::unique_ptr<GLESRenderEngine> engine;
318     switch (version) {
319         case GLES_VERSION_1_0:
320         case GLES_VERSION_1_1:
321             LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
322             break;
323         case GLES_VERSION_2_0:
324         case GLES_VERSION_3_0:
325             engine = std::make_unique<GLESRenderEngine>(args, display, config, ctxt, stub,
326                                                         protectedContext, protectedStub);
327             break;
328     }
329 
330     ALOGI("OpenGL ES informations:");
331     ALOGI("vendor    : %s", extensions.getVendor());
332     ALOGI("renderer  : %s", extensions.getRenderer());
333     ALOGI("version   : %s", extensions.getVersion());
334     ALOGI("extensions: %s", extensions.getExtensions());
335     ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
336     ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
337     return engine;
338 }
339 
chooseEglConfig(EGLDisplay display,int format,bool logConfig)340 EGLConfig GLESRenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
341     status_t err;
342     EGLConfig config;
343 
344     // First try to get an ES3 config
345     err = selectEGLConfig(display, format, EGL_OPENGL_ES3_BIT, &config);
346     if (err != NO_ERROR) {
347         // If ES3 fails, try to get an ES2 config
348         err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
349         if (err != NO_ERROR) {
350             // If ES2 still doesn't work, probably because we're on the emulator.
351             // try a simplified query
352             ALOGW("no suitable EGLConfig found, trying a simpler query");
353             err = selectEGLConfig(display, format, 0, &config);
354             if (err != NO_ERROR) {
355                 // this EGL is too lame for android
356                 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
357             }
358         }
359     }
360 
361     if (logConfig) {
362         // print some debugging info
363         EGLint r, g, b, a;
364         eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
365         eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
366         eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
367         eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
368         ALOGI("EGL information:");
369         ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR));
370         ALOGI("version   : %s", eglQueryString(display, EGL_VERSION));
371         ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
372         ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
373         ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
374     }
375 
376     return config;
377 }
378 
GLESRenderEngine(const RenderEngineCreationArgs & args,EGLDisplay display,EGLConfig config,EGLContext ctxt,EGLSurface stub,EGLContext protectedContext,EGLSurface protectedStub)379 GLESRenderEngine::GLESRenderEngine(const RenderEngineCreationArgs& args, EGLDisplay display,
380                                    EGLConfig config, EGLContext ctxt, EGLSurface stub,
381                                    EGLContext protectedContext, EGLSurface protectedStub)
382       : RenderEngine(args.renderEngineType),
383         mEGLDisplay(display),
384         mEGLConfig(config),
385         mEGLContext(ctxt),
386         mStubSurface(stub),
387         mProtectedEGLContext(protectedContext),
388         mProtectedStubSurface(protectedStub),
389         mVpWidth(0),
390         mVpHeight(0),
391         mFramebufferImageCacheSize(args.imageCacheSize),
392         mUseColorManagement(args.useColorManagement),
393         mPrecacheToneMapperShaderOnly(args.precacheToneMapperShaderOnly) {
394     glGetIntegerv(GL_MAX_TEXTURE_SIZE, &mMaxTextureSize);
395     glGetIntegerv(GL_MAX_VIEWPORT_DIMS, mMaxViewportDims);
396 
397     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
398     glPixelStorei(GL_PACK_ALIGNMENT, 4);
399 
400     // Initialize protected EGL Context.
401     if (mProtectedEGLContext != EGL_NO_CONTEXT) {
402         EGLBoolean success = eglMakeCurrent(display, mProtectedStubSurface, mProtectedStubSurface,
403                                             mProtectedEGLContext);
404         ALOGE_IF(!success, "can't make protected context current");
405         glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
406         glPixelStorei(GL_PACK_ALIGNMENT, 4);
407         success = eglMakeCurrent(display, mStubSurface, mStubSurface, mEGLContext);
408         LOG_ALWAYS_FATAL_IF(!success, "can't make default context current");
409     }
410 
411     // mColorBlindnessCorrection = M;
412 
413     if (mUseColorManagement) {
414         const ColorSpace srgb(ColorSpace::sRGB());
415         const ColorSpace displayP3(ColorSpace::DisplayP3());
416         const ColorSpace bt2020(ColorSpace::BT2020());
417 
418         // no chromatic adaptation needed since all color spaces use D65 for their white points.
419         mSrgbToXyz = mat4(srgb.getRGBtoXYZ());
420         mDisplayP3ToXyz = mat4(displayP3.getRGBtoXYZ());
421         mBt2020ToXyz = mat4(bt2020.getRGBtoXYZ());
422         mXyzToSrgb = mat4(srgb.getXYZtoRGB());
423         mXyzToDisplayP3 = mat4(displayP3.getXYZtoRGB());
424         mXyzToBt2020 = mat4(bt2020.getXYZtoRGB());
425 
426         // Compute sRGB to Display P3 and BT2020 transform matrix.
427         // NOTE: For now, we are limiting output wide color space support to
428         // Display-P3 and BT2020 only.
429         mSrgbToDisplayP3 = mXyzToDisplayP3 * mSrgbToXyz;
430         mSrgbToBt2020 = mXyzToBt2020 * mSrgbToXyz;
431 
432         // Compute Display P3 to sRGB and BT2020 transform matrix.
433         mDisplayP3ToSrgb = mXyzToSrgb * mDisplayP3ToXyz;
434         mDisplayP3ToBt2020 = mXyzToBt2020 * mDisplayP3ToXyz;
435 
436         // Compute BT2020 to sRGB and Display P3 transform matrix
437         mBt2020ToSrgb = mXyzToSrgb * mBt2020ToXyz;
438         mBt2020ToDisplayP3 = mXyzToDisplayP3 * mBt2020ToXyz;
439     }
440 
441     char value[PROPERTY_VALUE_MAX];
442     property_get("debug.egl.traceGpuCompletion", value, "0");
443     if (atoi(value)) {
444         mTraceGpuCompletion = true;
445         mFlushTracer = std::make_unique<FlushTracer>(this);
446     }
447 
448     if (args.supportsBackgroundBlur) {
449         mBlurFilter = new BlurFilter(*this);
450         checkErrors("BlurFilter creation");
451     }
452 
453     mImageManager = std::make_unique<ImageManager>(this);
454     mImageManager->initThread();
455     mDrawingBuffer = createFramebuffer();
456     sp<GraphicBuffer> buf =
457             new GraphicBuffer(1, 1, PIXEL_FORMAT_RGBA_8888, 1,
458                               GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE, "placeholder");
459 
460     const status_t err = buf->initCheck();
461     if (err != OK) {
462         ALOGE("Error allocating placeholder buffer: %d", err);
463         return;
464     }
465     mPlaceholderBuffer = buf.get();
466     EGLint attributes[] = {
467             EGL_NONE,
468     };
469     mPlaceholderImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
470                                           mPlaceholderBuffer, attributes);
471     ALOGE_IF(mPlaceholderImage == EGL_NO_IMAGE_KHR, "Failed to create placeholder image: %#x",
472              eglGetError());
473 
474     mShadowTexture = std::make_unique<GLShadowTexture>();
475 }
476 
~GLESRenderEngine()477 GLESRenderEngine::~GLESRenderEngine() {
478     // Destroy the image manager first.
479     mImageManager = nullptr;
480     mShadowTexture = nullptr;
481     cleanFramebufferCache();
482     ProgramCache::getInstance().purgeCaches();
483     std::lock_guard<std::mutex> lock(mRenderingMutex);
484     glDisableVertexAttribArray(Program::position);
485     unbindFrameBuffer(mDrawingBuffer.get());
486     mDrawingBuffer = nullptr;
487     eglDestroyImageKHR(mEGLDisplay, mPlaceholderImage);
488     mImageCache.clear();
489     if (mStubSurface != EGL_NO_SURFACE) {
490         eglDestroySurface(mEGLDisplay, mStubSurface);
491     }
492     if (mProtectedStubSurface != EGL_NO_SURFACE) {
493         eglDestroySurface(mEGLDisplay, mProtectedStubSurface);
494     }
495     if (mEGLContext != EGL_NO_CONTEXT) {
496         eglDestroyContext(mEGLDisplay, mEGLContext);
497     }
498     if (mProtectedEGLContext != EGL_NO_CONTEXT) {
499         eglDestroyContext(mEGLDisplay, mProtectedEGLContext);
500     }
501     eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
502     eglTerminate(mEGLDisplay);
503     eglReleaseThread();
504 }
505 
createFramebuffer()506 std::unique_ptr<Framebuffer> GLESRenderEngine::createFramebuffer() {
507     return std::make_unique<GLFramebuffer>(*this);
508 }
509 
createImage()510 std::unique_ptr<Image> GLESRenderEngine::createImage() {
511     return std::make_unique<GLImage>(*this);
512 }
513 
getFramebufferForDrawing()514 Framebuffer* GLESRenderEngine::getFramebufferForDrawing() {
515     return mDrawingBuffer.get();
516 }
517 
primeCache()518 std::future<void> GLESRenderEngine::primeCache() {
519     ProgramCache::getInstance().primeCache(mInProtectedContext ? mProtectedEGLContext : mEGLContext,
520                                            mUseColorManagement, mPrecacheToneMapperShaderOnly);
521     return {};
522 }
523 
flush()524 base::unique_fd GLESRenderEngine::flush() {
525     ATRACE_CALL();
526     if (!GLExtensions::getInstance().hasNativeFenceSync()) {
527         return base::unique_fd();
528     }
529 
530     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
531     if (sync == EGL_NO_SYNC_KHR) {
532         ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
533         return base::unique_fd();
534     }
535 
536     // native fence fd will not be populated until flush() is done.
537     glFlush();
538 
539     // get the fence fd
540     base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
541     eglDestroySyncKHR(mEGLDisplay, sync);
542     if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
543         ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
544     }
545 
546     // Only trace if we have a valid fence, as current usage falls back to
547     // calling finish() if the fence fd is invalid.
548     if (CC_UNLIKELY(mTraceGpuCompletion && mFlushTracer) && fenceFd.get() >= 0) {
549         mFlushTracer->queueSync(eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr));
550     }
551 
552     return fenceFd;
553 }
554 
finish()555 bool GLESRenderEngine::finish() {
556     ATRACE_CALL();
557     if (!GLExtensions::getInstance().hasFenceSync()) {
558         ALOGW("no synchronization support");
559         return false;
560     }
561 
562     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
563     if (sync == EGL_NO_SYNC_KHR) {
564         ALOGW("failed to create EGL fence sync: %#x", eglGetError());
565         return false;
566     }
567 
568     if (CC_UNLIKELY(mTraceGpuCompletion && mFlushTracer)) {
569         mFlushTracer->queueSync(eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr));
570     }
571 
572     return waitSync(sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR);
573 }
574 
waitSync(EGLSyncKHR sync,EGLint flags)575 bool GLESRenderEngine::waitSync(EGLSyncKHR sync, EGLint flags) {
576     EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, flags, 2000000000 /*2 sec*/);
577     EGLint error = eglGetError();
578     eglDestroySyncKHR(mEGLDisplay, sync);
579     if (result != EGL_CONDITION_SATISFIED_KHR) {
580         if (result == EGL_TIMEOUT_EXPIRED_KHR) {
581             ALOGW("fence wait timed out");
582         } else {
583             ALOGW("error waiting on EGL fence: %#x", error);
584         }
585         return false;
586     }
587 
588     return true;
589 }
590 
waitFence(base::unique_fd fenceFd)591 bool GLESRenderEngine::waitFence(base::unique_fd fenceFd) {
592     if (!GLExtensions::getInstance().hasNativeFenceSync() ||
593         !GLExtensions::getInstance().hasWaitSync()) {
594         return false;
595     }
596 
597     // release the fd and transfer the ownership to EGLSync
598     EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd.release(), EGL_NONE};
599     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
600     if (sync == EGL_NO_SYNC_KHR) {
601         ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
602         return false;
603     }
604 
605     // XXX: The spec draft is inconsistent as to whether this should return an
606     // EGLint or void.  Ignore the return value for now, as it's not strictly
607     // needed.
608     eglWaitSyncKHR(mEGLDisplay, sync, 0);
609     EGLint error = eglGetError();
610     eglDestroySyncKHR(mEGLDisplay, sync);
611     if (error != EGL_SUCCESS) {
612         ALOGE("failed to wait for EGL native fence sync: %#x", error);
613         return false;
614     }
615 
616     return true;
617 }
618 
clearWithColor(float red,float green,float blue,float alpha)619 void GLESRenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
620     ATRACE_CALL();
621     glDisable(GL_BLEND);
622     glClearColor(red, green, blue, alpha);
623     glClear(GL_COLOR_BUFFER_BIT);
624 }
625 
fillRegionWithColor(const Region & region,float red,float green,float blue,float alpha)626 void GLESRenderEngine::fillRegionWithColor(const Region& region, float red, float green, float blue,
627                                            float alpha) {
628     size_t c;
629     Rect const* r = region.getArray(&c);
630     Mesh mesh = Mesh::Builder()
631                         .setPrimitive(Mesh::TRIANGLES)
632                         .setVertices(c * 6 /* count */, 2 /* size */)
633                         .build();
634     Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
635     for (size_t i = 0; i < c; i++, r++) {
636         position[i * 6 + 0].x = r->left;
637         position[i * 6 + 0].y = r->top;
638         position[i * 6 + 1].x = r->left;
639         position[i * 6 + 1].y = r->bottom;
640         position[i * 6 + 2].x = r->right;
641         position[i * 6 + 2].y = r->bottom;
642         position[i * 6 + 3].x = r->left;
643         position[i * 6 + 3].y = r->top;
644         position[i * 6 + 4].x = r->right;
645         position[i * 6 + 4].y = r->bottom;
646         position[i * 6 + 5].x = r->right;
647         position[i * 6 + 5].y = r->top;
648     }
649     setupFillWithColor(red, green, blue, alpha);
650     drawMesh(mesh);
651 }
652 
setScissor(const Rect & region)653 void GLESRenderEngine::setScissor(const Rect& region) {
654     glScissor(region.left, region.top, region.getWidth(), region.getHeight());
655     glEnable(GL_SCISSOR_TEST);
656 }
657 
disableScissor()658 void GLESRenderEngine::disableScissor() {
659     glDisable(GL_SCISSOR_TEST);
660 }
661 
genTextures(size_t count,uint32_t * names)662 void GLESRenderEngine::genTextures(size_t count, uint32_t* names) {
663     glGenTextures(count, names);
664 }
665 
deleteTextures(size_t count,uint32_t const * names)666 void GLESRenderEngine::deleteTextures(size_t count, uint32_t const* names) {
667     for (int i = 0; i < count; ++i) {
668         mTextureView.erase(names[i]);
669     }
670     glDeleteTextures(count, names);
671 }
672 
bindExternalTextureImage(uint32_t texName,const Image & image)673 void GLESRenderEngine::bindExternalTextureImage(uint32_t texName, const Image& image) {
674     ATRACE_CALL();
675     const GLImage& glImage = static_cast<const GLImage&>(image);
676     const GLenum target = GL_TEXTURE_EXTERNAL_OES;
677 
678     glBindTexture(target, texName);
679     if (glImage.getEGLImage() != EGL_NO_IMAGE_KHR) {
680         glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(glImage.getEGLImage()));
681     }
682 }
683 
bindExternalTextureBuffer(uint32_t texName,const sp<GraphicBuffer> & buffer,const sp<Fence> & bufferFence)684 void GLESRenderEngine::bindExternalTextureBuffer(uint32_t texName, const sp<GraphicBuffer>& buffer,
685                                                  const sp<Fence>& bufferFence) {
686     ATRACE_CALL();
687 
688     bool found = false;
689     {
690         std::lock_guard<std::mutex> lock(mRenderingMutex);
691         auto cachedImage = mImageCache.find(buffer->getId());
692         found = (cachedImage != mImageCache.end());
693     }
694 
695     // If we couldn't find the image in the cache at this time, then either
696     // SurfaceFlinger messed up registering the buffer ahead of time or we got
697     // backed up creating other EGLImages.
698     if (!found) {
699         status_t cacheResult = mImageManager->cache(buffer);
700         if (cacheResult != NO_ERROR) {
701             ALOGE("Error with caching buffer: %d", cacheResult);
702             return;
703         }
704     }
705 
706     // Whether or not we needed to cache, re-check mImageCache to make sure that
707     // there's an EGLImage. The current threading model guarantees that we don't
708     // destroy a cached image until it's really not needed anymore (i.e. this
709     // function should not be called), so the only possibility is that something
710     // terrible went wrong and we should just bind something and move on.
711     {
712         std::lock_guard<std::mutex> lock(mRenderingMutex);
713         auto cachedImage = mImageCache.find(buffer->getId());
714 
715         if (cachedImage == mImageCache.end()) {
716             // We failed creating the image if we got here, so bail out.
717             ALOGE("Failed to create an EGLImage when rendering");
718             bindExternalTextureImage(texName, *createImage());
719             return;
720         }
721 
722         bindExternalTextureImage(texName, *cachedImage->second);
723         mTextureView.insert_or_assign(texName, buffer->getId());
724     }
725 
726     // Wait for the new buffer to be ready.
727     if (bufferFence != nullptr && bufferFence->isValid()) {
728         if (GLExtensions::getInstance().hasWaitSync()) {
729             base::unique_fd fenceFd(bufferFence->dup());
730             if (fenceFd == -1) {
731                 ALOGE("error dup'ing fence fd: %d", errno);
732                 return;
733             }
734             if (!waitFence(std::move(fenceFd))) {
735                 ALOGE("failed to wait on fence fd");
736                 return;
737             }
738         } else {
739             status_t err = bufferFence->waitForever("RenderEngine::bindExternalTextureBuffer");
740             if (err != NO_ERROR) {
741                 ALOGE("error waiting for fence: %d", err);
742                 return;
743             }
744         }
745     }
746 
747     return;
748 }
749 
mapExternalTextureBuffer(const sp<GraphicBuffer> & buffer,bool)750 void GLESRenderEngine::mapExternalTextureBuffer(const sp<GraphicBuffer>& buffer,
751                                                 bool /*isRenderable*/) {
752     ATRACE_CALL();
753     mImageManager->cacheAsync(buffer, nullptr);
754 }
755 
cacheExternalTextureBufferForTesting(const sp<GraphicBuffer> & buffer)756 std::shared_ptr<ImageManager::Barrier> GLESRenderEngine::cacheExternalTextureBufferForTesting(
757         const sp<GraphicBuffer>& buffer) {
758     auto barrier = std::make_shared<ImageManager::Barrier>();
759     mImageManager->cacheAsync(buffer, barrier);
760     return barrier;
761 }
762 
cacheExternalTextureBufferInternal(const sp<GraphicBuffer> & buffer)763 status_t GLESRenderEngine::cacheExternalTextureBufferInternal(const sp<GraphicBuffer>& buffer) {
764     if (buffer == nullptr) {
765         return BAD_VALUE;
766     }
767 
768     {
769         std::lock_guard<std::mutex> lock(mRenderingMutex);
770         if (mImageCache.count(buffer->getId()) > 0) {
771             // If there's already an image then fail fast here.
772             return NO_ERROR;
773         }
774     }
775     ATRACE_CALL();
776 
777     // Create the image without holding a lock so that we don't block anything.
778     std::unique_ptr<Image> newImage = createImage();
779 
780     bool created = newImage->setNativeWindowBuffer(buffer->getNativeBuffer(),
781                                                    buffer->getUsage() & GRALLOC_USAGE_PROTECTED);
782     if (!created) {
783         ALOGE("Failed to create image. id=%" PRIx64 " size=%ux%u st=%u usage=%#" PRIx64 " fmt=%d",
784               buffer->getId(), buffer->getWidth(), buffer->getHeight(), buffer->getStride(),
785               buffer->getUsage(), buffer->getPixelFormat());
786         return NO_INIT;
787     }
788 
789     {
790         std::lock_guard<std::mutex> lock(mRenderingMutex);
791         if (mImageCache.count(buffer->getId()) > 0) {
792             // In theory it's possible for another thread to recache the image,
793             // so bail out if another thread won.
794             return NO_ERROR;
795         }
796         mImageCache.insert(std::make_pair(buffer->getId(), std::move(newImage)));
797     }
798 
799     return NO_ERROR;
800 }
801 
unmapExternalTextureBuffer(const sp<GraphicBuffer> & buffer)802 void GLESRenderEngine::unmapExternalTextureBuffer(const sp<GraphicBuffer>& buffer) {
803     mImageManager->releaseAsync(buffer->getId(), nullptr);
804 }
805 
unbindExternalTextureBufferForTesting(uint64_t bufferId)806 std::shared_ptr<ImageManager::Barrier> GLESRenderEngine::unbindExternalTextureBufferForTesting(
807         uint64_t bufferId) {
808     auto barrier = std::make_shared<ImageManager::Barrier>();
809     mImageManager->releaseAsync(bufferId, barrier);
810     return barrier;
811 }
812 
unbindExternalTextureBufferInternal(uint64_t bufferId)813 void GLESRenderEngine::unbindExternalTextureBufferInternal(uint64_t bufferId) {
814     std::unique_ptr<Image> image;
815     {
816         std::lock_guard<std::mutex> lock(mRenderingMutex);
817         const auto& cachedImage = mImageCache.find(bufferId);
818 
819         if (cachedImage != mImageCache.end()) {
820             ALOGV("Destroying image for buffer: %" PRIu64, bufferId);
821             // Move the buffer out of cache first, so that we can destroy
822             // without holding the cache's lock.
823             image = std::move(cachedImage->second);
824             mImageCache.erase(bufferId);
825             return;
826         }
827     }
828     ALOGV("Failed to find image for buffer: %" PRIu64, bufferId);
829 }
830 
getContextPriority()831 int GLESRenderEngine::getContextPriority() {
832     int value;
833     eglQueryContext(mEGLDisplay, mEGLContext, EGL_CONTEXT_PRIORITY_LEVEL_IMG, &value);
834     return value;
835 }
836 
setupLayerCropping(const LayerSettings & layer,Mesh & mesh)837 FloatRect GLESRenderEngine::setupLayerCropping(const LayerSettings& layer, Mesh& mesh) {
838     // Translate win by the rounded corners rect coordinates, to have all values in
839     // layer coordinate space.
840     FloatRect cropWin = layer.geometry.boundaries;
841     const FloatRect& roundedCornersCrop = layer.geometry.roundedCornersCrop;
842     cropWin.left -= roundedCornersCrop.left;
843     cropWin.right -= roundedCornersCrop.left;
844     cropWin.top -= roundedCornersCrop.top;
845     cropWin.bottom -= roundedCornersCrop.top;
846     Mesh::VertexArray<vec2> cropCoords(mesh.getCropCoordArray<vec2>());
847     cropCoords[0] = vec2(cropWin.left, cropWin.top);
848     cropCoords[1] = vec2(cropWin.left, cropWin.top + cropWin.getHeight());
849     cropCoords[2] = vec2(cropWin.right, cropWin.top + cropWin.getHeight());
850     cropCoords[3] = vec2(cropWin.right, cropWin.top);
851 
852     setupCornerRadiusCropSize(roundedCornersCrop.getWidth(), roundedCornersCrop.getHeight());
853     return cropWin;
854 }
855 
handleRoundedCorners(const DisplaySettings & display,const LayerSettings & layer,const Mesh & mesh)856 void GLESRenderEngine::handleRoundedCorners(const DisplaySettings& display,
857                                             const LayerSettings& layer, const Mesh& mesh) {
858     // We separate the layer into 3 parts essentially, such that we only turn on blending for the
859     // top rectangle and the bottom rectangle, and turn off blending for the middle rectangle.
860     FloatRect bounds = layer.geometry.roundedCornersCrop;
861 
862     // Explicitly compute the transform from the clip rectangle to the physical
863     // display. Normally, this is done in glViewport but we explicitly compute
864     // it here so that we can get the scissor bounds correct.
865     const Rect& source = display.clip;
866     const Rect& destination = display.physicalDisplay;
867     // Here we compute the following transform:
868     // 1. Translate the top left corner of the source clip to (0, 0)
869     // 2. Rotate the clip rectangle about the origin in accordance with the
870     // orientation flag
871     // 3. Translate the top left corner back to the origin.
872     // 4. Scale the clip rectangle to the destination rectangle dimensions
873     // 5. Translate the top left corner to the destination rectangle's top left
874     // corner.
875     const mat4 translateSource = mat4::translate(vec4(-source.left, -source.top, 0, 1));
876     mat4 rotation;
877     int displacementX = 0;
878     int displacementY = 0;
879     float destinationWidth = static_cast<float>(destination.getWidth());
880     float destinationHeight = static_cast<float>(destination.getHeight());
881     float sourceWidth = static_cast<float>(source.getWidth());
882     float sourceHeight = static_cast<float>(source.getHeight());
883     const float rot90InRadians = 2.0f * static_cast<float>(M_PI) / 4.0f;
884     switch (display.orientation) {
885         case ui::Transform::ROT_90:
886             rotation = mat4::rotate(rot90InRadians, vec3(0, 0, 1));
887             displacementX = source.getHeight();
888             std::swap(sourceHeight, sourceWidth);
889             break;
890         case ui::Transform::ROT_180:
891             rotation = mat4::rotate(rot90InRadians * 2.0f, vec3(0, 0, 1));
892             displacementY = source.getHeight();
893             displacementX = source.getWidth();
894             break;
895         case ui::Transform::ROT_270:
896             rotation = mat4::rotate(rot90InRadians * 3.0f, vec3(0, 0, 1));
897             displacementY = source.getWidth();
898             std::swap(sourceHeight, sourceWidth);
899             break;
900         default:
901             break;
902     }
903 
904     const mat4 intermediateTranslation = mat4::translate(vec4(displacementX, displacementY, 0, 1));
905     const mat4 scale = mat4::scale(
906             vec4(destinationWidth / sourceWidth, destinationHeight / sourceHeight, 1, 1));
907     const mat4 translateDestination =
908             mat4::translate(vec4(destination.left, destination.top, 0, 1));
909     const mat4 globalTransform =
910             translateDestination * scale * intermediateTranslation * rotation * translateSource;
911 
912     const mat4 transformMatrix = globalTransform * layer.geometry.positionTransform;
913     const vec4 leftTopCoordinate(bounds.left, bounds.top, 1.0, 1.0);
914     const vec4 rightBottomCoordinate(bounds.right, bounds.bottom, 1.0, 1.0);
915     const vec4 leftTopCoordinateInBuffer = transformMatrix * leftTopCoordinate;
916     const vec4 rightBottomCoordinateInBuffer = transformMatrix * rightBottomCoordinate;
917     bounds = FloatRect(std::min(leftTopCoordinateInBuffer[0], rightBottomCoordinateInBuffer[0]),
918                        std::min(leftTopCoordinateInBuffer[1], rightBottomCoordinateInBuffer[1]),
919                        std::max(leftTopCoordinateInBuffer[0], rightBottomCoordinateInBuffer[0]),
920                        std::max(leftTopCoordinateInBuffer[1], rightBottomCoordinateInBuffer[1]));
921 
922     // Finally, we cut the layer into 3 parts, with top and bottom parts having rounded corners
923     // and the middle part without rounded corners.
924     const int32_t radius = ceil(layer.geometry.roundedCornersRadius);
925     const Rect topRect(bounds.left, bounds.top, bounds.right, bounds.top + radius);
926     setScissor(topRect);
927     drawMesh(mesh);
928     const Rect bottomRect(bounds.left, bounds.bottom - radius, bounds.right, bounds.bottom);
929     setScissor(bottomRect);
930     drawMesh(mesh);
931 
932     // The middle part of the layer can turn off blending.
933     if (topRect.bottom < bottomRect.top) {
934         const Rect middleRect(bounds.left, bounds.top + radius, bounds.right,
935                               bounds.bottom - radius);
936         setScissor(middleRect);
937         mState.cornerRadius = 0.0;
938         disableBlending();
939         drawMesh(mesh);
940     }
941     disableScissor();
942 }
943 
bindFrameBuffer(Framebuffer * framebuffer)944 status_t GLESRenderEngine::bindFrameBuffer(Framebuffer* framebuffer) {
945     ATRACE_CALL();
946     GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(framebuffer);
947     EGLImageKHR eglImage = glFramebuffer->getEGLImage();
948     uint32_t textureName = glFramebuffer->getTextureName();
949     uint32_t framebufferName = glFramebuffer->getFramebufferName();
950 
951     // Bind the texture and turn our EGLImage into a texture
952     glBindTexture(GL_TEXTURE_2D, textureName);
953     glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)eglImage);
954 
955     // Bind the Framebuffer to render into
956     glBindFramebuffer(GL_FRAMEBUFFER, framebufferName);
957     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureName, 0);
958 
959     uint32_t glStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
960     ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
961              glStatus);
962 
963     return glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
964 }
965 
unbindFrameBuffer(Framebuffer *)966 void GLESRenderEngine::unbindFrameBuffer(Framebuffer* /*framebuffer*/) {
967     ATRACE_CALL();
968 
969     // back to main framebuffer
970     glBindFramebuffer(GL_FRAMEBUFFER, 0);
971 }
972 
canSkipPostRenderCleanup() const973 bool GLESRenderEngine::canSkipPostRenderCleanup() const {
974     return mPriorResourcesCleaned ||
975             (mLastDrawFence != nullptr && mLastDrawFence->getStatus() != Fence::Status::Signaled);
976 }
977 
cleanupPostRender()978 void GLESRenderEngine::cleanupPostRender() {
979     ATRACE_CALL();
980 
981     if (canSkipPostRenderCleanup()) {
982         // If we don't have a prior frame needing cleanup, then don't do anything.
983         return;
984     }
985 
986     // Bind the texture to placeholder so that backing image data can be freed.
987     GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(getFramebufferForDrawing());
988     glFramebuffer->allocateBuffers(1, 1, mPlaceholderDrawBuffer);
989 
990     // Release the cached fence here, so that we don't churn reallocations when
991     // we could no-op repeated calls of this method instead.
992     mLastDrawFence = nullptr;
993     mPriorResourcesCleaned = true;
994 }
995 
cleanFramebufferCache()996 void GLESRenderEngine::cleanFramebufferCache() {
997     std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
998     // Bind the texture to placeholder so that backing image data can be freed.
999     GLFramebuffer* glFramebuffer = static_cast<GLFramebuffer*>(getFramebufferForDrawing());
1000     glFramebuffer->allocateBuffers(1, 1, mPlaceholderDrawBuffer);
1001 
1002     while (!mFramebufferImageCache.empty()) {
1003         EGLImageKHR expired = mFramebufferImageCache.front().second;
1004         mFramebufferImageCache.pop_front();
1005         eglDestroyImageKHR(mEGLDisplay, expired);
1006         DEBUG_EGL_IMAGE_TRACKER_DESTROY();
1007     }
1008 }
1009 
checkErrors() const1010 void GLESRenderEngine::checkErrors() const {
1011     checkErrors(nullptr);
1012 }
1013 
checkErrors(const char * tag) const1014 void GLESRenderEngine::checkErrors(const char* tag) const {
1015     do {
1016         // there could be more than one error flag
1017         GLenum error = glGetError();
1018         if (error == GL_NO_ERROR) break;
1019         if (tag == nullptr) {
1020             ALOGE("GL error 0x%04x", int(error));
1021         } else {
1022             ALOGE("GL error: %s -> 0x%04x", tag, int(error));
1023         }
1024     } while (true);
1025 }
1026 
supportsProtectedContent() const1027 bool GLESRenderEngine::supportsProtectedContent() const {
1028     return mProtectedEGLContext != EGL_NO_CONTEXT;
1029 }
1030 
useProtectedContext(bool useProtectedContext)1031 void GLESRenderEngine::useProtectedContext(bool useProtectedContext) {
1032     if (useProtectedContext == mInProtectedContext ||
1033         (useProtectedContext && !supportsProtectedContent())) {
1034         return;
1035     }
1036 
1037     const EGLSurface surface = useProtectedContext ? mProtectedStubSurface : mStubSurface;
1038     const EGLContext context = useProtectedContext ? mProtectedEGLContext : mEGLContext;
1039     if (eglMakeCurrent(mEGLDisplay, surface, surface, context) == EGL_TRUE) {
1040         mInProtectedContext = useProtectedContext;
1041     }
1042 }
createFramebufferImageIfNeeded(ANativeWindowBuffer * nativeBuffer,bool isProtected,bool useFramebufferCache)1043 EGLImageKHR GLESRenderEngine::createFramebufferImageIfNeeded(ANativeWindowBuffer* nativeBuffer,
1044                                                              bool isProtected,
1045                                                              bool useFramebufferCache) {
1046     sp<GraphicBuffer> graphicBuffer = GraphicBuffer::from(nativeBuffer);
1047     if (useFramebufferCache) {
1048         std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
1049         for (const auto& image : mFramebufferImageCache) {
1050             if (image.first == graphicBuffer->getId()) {
1051                 return image.second;
1052             }
1053         }
1054     }
1055     EGLint attributes[] = {
1056             isProtected ? EGL_PROTECTED_CONTENT_EXT : EGL_NONE,
1057             isProtected ? EGL_TRUE : EGL_NONE,
1058             EGL_NONE,
1059     };
1060     EGLImageKHR image = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
1061                                           nativeBuffer, attributes);
1062     if (useFramebufferCache) {
1063         if (image != EGL_NO_IMAGE_KHR) {
1064             std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
1065             if (mFramebufferImageCache.size() >= mFramebufferImageCacheSize) {
1066                 EGLImageKHR expired = mFramebufferImageCache.front().second;
1067                 mFramebufferImageCache.pop_front();
1068                 eglDestroyImageKHR(mEGLDisplay, expired);
1069                 DEBUG_EGL_IMAGE_TRACKER_DESTROY();
1070             }
1071             mFramebufferImageCache.push_back({graphicBuffer->getId(), image});
1072         }
1073     }
1074 
1075     if (image != EGL_NO_IMAGE_KHR) {
1076         DEBUG_EGL_IMAGE_TRACKER_CREATE();
1077     }
1078     return image;
1079 }
1080 
drawLayers(const DisplaySettings & display,const std::vector<const LayerSettings * > & layers,const std::shared_ptr<ExternalTexture> & buffer,const bool useFramebufferCache,base::unique_fd && bufferFence,base::unique_fd * drawFence)1081 status_t GLESRenderEngine::drawLayers(const DisplaySettings& display,
1082                                       const std::vector<const LayerSettings*>& layers,
1083                                       const std::shared_ptr<ExternalTexture>& buffer,
1084                                       const bool useFramebufferCache, base::unique_fd&& bufferFence,
1085                                       base::unique_fd* drawFence) {
1086     ATRACE_CALL();
1087     if (layers.empty()) {
1088         ALOGV("Drawing empty layer stack");
1089         return NO_ERROR;
1090     }
1091 
1092     if (bufferFence.get() >= 0) {
1093         // Duplicate the fence for passing to waitFence.
1094         base::unique_fd bufferFenceDup(dup(bufferFence.get()));
1095         if (bufferFenceDup < 0 || !waitFence(std::move(bufferFenceDup))) {
1096             ATRACE_NAME("Waiting before draw");
1097             sync_wait(bufferFence.get(), -1);
1098         }
1099     }
1100 
1101     if (buffer == nullptr) {
1102         ALOGE("No output buffer provided. Aborting GPU composition.");
1103         return BAD_VALUE;
1104     }
1105 
1106     validateOutputBufferUsage(buffer->getBuffer());
1107 
1108     std::unique_ptr<BindNativeBufferAsFramebuffer> fbo;
1109     // Gathering layers that requested blur, we'll need them to decide when to render to an
1110     // offscreen buffer, and when to render to the native buffer.
1111     std::deque<const LayerSettings*> blurLayers;
1112     if (CC_LIKELY(mBlurFilter != nullptr)) {
1113         for (auto layer : layers) {
1114             if (layer->backgroundBlurRadius > 0) {
1115                 blurLayers.push_back(layer);
1116             }
1117         }
1118     }
1119     const auto blurLayersSize = blurLayers.size();
1120 
1121     if (blurLayersSize == 0) {
1122         fbo = std::make_unique<BindNativeBufferAsFramebuffer>(*this,
1123                                                               buffer->getBuffer()
1124                                                                       .get()
1125                                                                       ->getNativeBuffer(),
1126                                                               useFramebufferCache);
1127         if (fbo->getStatus() != NO_ERROR) {
1128             ALOGE("Failed to bind framebuffer! Aborting GPU composition for buffer (%p).",
1129                   buffer->getBuffer()->handle);
1130             checkErrors();
1131             return fbo->getStatus();
1132         }
1133         setViewportAndProjection(display.physicalDisplay, display.clip);
1134     } else {
1135         setViewportAndProjection(display.physicalDisplay, display.clip);
1136         auto status =
1137                 mBlurFilter->setAsDrawTarget(display, blurLayers.front()->backgroundBlurRadius);
1138         if (status != NO_ERROR) {
1139             ALOGE("Failed to prepare blur filter! Aborting GPU composition for buffer (%p).",
1140                   buffer->getBuffer()->handle);
1141             checkErrors();
1142             return status;
1143         }
1144     }
1145 
1146     // clear the entire buffer, sometimes when we reuse buffers we'd persist
1147     // ghost images otherwise.
1148     // we also require a full transparent framebuffer for overlays. This is
1149     // probably not quite efficient on all GPUs, since we could filter out
1150     // opaque layers.
1151     clearWithColor(0.0, 0.0, 0.0, 0.0);
1152 
1153     setOutputDataSpace(display.outputDataspace);
1154     setDisplayMaxLuminance(display.maxLuminance);
1155     setDisplayColorTransform(display.colorTransform);
1156 
1157     const mat4 projectionMatrix =
1158             ui::Transform(display.orientation).asMatrix4() * mState.projectionMatrix;
1159     if (!display.clearRegion.isEmpty()) {
1160         glDisable(GL_BLEND);
1161         fillRegionWithColor(display.clearRegion, 0.0, 0.0, 0.0, 1.0);
1162     }
1163 
1164     Mesh mesh = Mesh::Builder()
1165                         .setPrimitive(Mesh::TRIANGLE_FAN)
1166                         .setVertices(4 /* count */, 2 /* size */)
1167                         .setTexCoords(2 /* size */)
1168                         .setCropCoords(2 /* size */)
1169                         .build();
1170     for (auto const layer : layers) {
1171         if (blurLayers.size() > 0 && blurLayers.front() == layer) {
1172             blurLayers.pop_front();
1173 
1174             auto status = mBlurFilter->prepare();
1175             if (status != NO_ERROR) {
1176                 ALOGE("Failed to render blur effect! Aborting GPU composition for buffer (%p).",
1177                       buffer->getBuffer()->handle);
1178                 checkErrors("Can't render first blur pass");
1179                 return status;
1180             }
1181 
1182             if (blurLayers.size() == 0) {
1183                 // Done blurring, time to bind the native FBO and render our blur onto it.
1184                 fbo = std::make_unique<BindNativeBufferAsFramebuffer>(*this,
1185                                                                       buffer.get()
1186                                                                               ->getBuffer()
1187                                                                               ->getNativeBuffer(),
1188                                                                       useFramebufferCache);
1189                 status = fbo->getStatus();
1190                 setViewportAndProjection(display.physicalDisplay, display.clip);
1191             } else {
1192                 // There's still something else to blur, so let's keep rendering to our FBO
1193                 // instead of to the display.
1194                 status = mBlurFilter->setAsDrawTarget(display,
1195                                                       blurLayers.front()->backgroundBlurRadius);
1196             }
1197             if (status != NO_ERROR) {
1198                 ALOGE("Failed to bind framebuffer! Aborting GPU composition for buffer (%p).",
1199                       buffer->getBuffer()->handle);
1200                 checkErrors("Can't bind native framebuffer");
1201                 return status;
1202             }
1203 
1204             status = mBlurFilter->render(blurLayersSize > 1);
1205             if (status != NO_ERROR) {
1206                 ALOGE("Failed to render blur effect! Aborting GPU composition for buffer (%p).",
1207                       buffer->getBuffer()->handle);
1208                 checkErrors("Can't render blur filter");
1209                 return status;
1210             }
1211         }
1212 
1213         // Ensure luminance is at least 100 nits to avoid div-by-zero
1214         const float maxLuminance = std::max(100.f, layer->source.buffer.maxLuminanceNits);
1215         mState.maxMasteringLuminance = maxLuminance;
1216         mState.maxContentLuminance = maxLuminance;
1217         mState.projectionMatrix = projectionMatrix * layer->geometry.positionTransform;
1218 
1219         const FloatRect bounds = layer->geometry.boundaries;
1220         Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
1221         position[0] = vec2(bounds.left, bounds.top);
1222         position[1] = vec2(bounds.left, bounds.bottom);
1223         position[2] = vec2(bounds.right, bounds.bottom);
1224         position[3] = vec2(bounds.right, bounds.top);
1225 
1226         setupLayerCropping(*layer, mesh);
1227         setColorTransform(layer->colorTransform);
1228 
1229         bool usePremultipliedAlpha = true;
1230         bool disableTexture = true;
1231         bool isOpaque = false;
1232         if (layer->source.buffer.buffer != nullptr) {
1233             disableTexture = false;
1234             isOpaque = layer->source.buffer.isOpaque;
1235 
1236             sp<GraphicBuffer> gBuf = layer->source.buffer.buffer->getBuffer();
1237             validateInputBufferUsage(gBuf);
1238             bindExternalTextureBuffer(layer->source.buffer.textureName, gBuf,
1239                                       layer->source.buffer.fence);
1240 
1241             usePremultipliedAlpha = layer->source.buffer.usePremultipliedAlpha;
1242             Texture texture(Texture::TEXTURE_EXTERNAL, layer->source.buffer.textureName);
1243             mat4 texMatrix = layer->source.buffer.textureTransform;
1244 
1245             texture.setMatrix(texMatrix.asArray());
1246             texture.setFiltering(layer->source.buffer.useTextureFiltering);
1247 
1248             texture.setDimensions(gBuf->getWidth(), gBuf->getHeight());
1249             setSourceY410BT2020(layer->source.buffer.isY410BT2020);
1250 
1251             renderengine::Mesh::VertexArray<vec2> texCoords(mesh.getTexCoordArray<vec2>());
1252             texCoords[0] = vec2(0.0, 0.0);
1253             texCoords[1] = vec2(0.0, 1.0);
1254             texCoords[2] = vec2(1.0, 1.0);
1255             texCoords[3] = vec2(1.0, 0.0);
1256             setupLayerTexturing(texture);
1257 
1258             // Do not cache protected EGLImage, protected memory is limited.
1259             if (gBuf->getUsage() & GRALLOC_USAGE_PROTECTED) {
1260                 unmapExternalTextureBuffer(gBuf);
1261             }
1262         }
1263 
1264         const half3 solidColor = layer->source.solidColor;
1265         const half4 color = half4(solidColor.r, solidColor.g, solidColor.b, layer->alpha);
1266         // Buffer sources will have a black solid color ignored in the shader,
1267         // so in that scenario the solid color passed here is arbitrary.
1268         setupLayerBlending(usePremultipliedAlpha, isOpaque, disableTexture, color,
1269                            layer->geometry.roundedCornersRadius);
1270         if (layer->disableBlending) {
1271             glDisable(GL_BLEND);
1272         }
1273         setSourceDataSpace(layer->sourceDataspace);
1274 
1275         if (layer->shadow.length > 0.0f) {
1276             handleShadow(layer->geometry.boundaries, layer->geometry.roundedCornersRadius,
1277                          layer->shadow);
1278         }
1279         // We only want to do a special handling for rounded corners when having rounded corners
1280         // is the only reason it needs to turn on blending, otherwise, we handle it like the
1281         // usual way since it needs to turn on blending anyway.
1282         else if (layer->geometry.roundedCornersRadius > 0.0 && color.a >= 1.0f && isOpaque) {
1283             handleRoundedCorners(display, *layer, mesh);
1284         } else {
1285             drawMesh(mesh);
1286         }
1287 
1288         // Cleanup if there's a buffer source
1289         if (layer->source.buffer.buffer != nullptr) {
1290             disableBlending();
1291             setSourceY410BT2020(false);
1292             disableTexturing();
1293         }
1294     }
1295 
1296     if (drawFence != nullptr) {
1297         *drawFence = flush();
1298     }
1299     // If flush failed or we don't support native fences, we need to force the
1300     // gl command stream to be executed.
1301     if (drawFence == nullptr || drawFence->get() < 0) {
1302         bool success = finish();
1303         if (!success) {
1304             ALOGE("Failed to flush RenderEngine commands");
1305             checkErrors();
1306             // Chances are, something illegal happened (either the caller passed
1307             // us bad parameters, or we messed up our shader generation).
1308             return INVALID_OPERATION;
1309         }
1310         mLastDrawFence = nullptr;
1311     } else {
1312         // The caller takes ownership of drawFence, so we need to duplicate the
1313         // fd here.
1314         mLastDrawFence = new Fence(dup(drawFence->get()));
1315     }
1316     mPriorResourcesCleaned = false;
1317 
1318     checkErrors();
1319     return NO_ERROR;
1320 }
1321 
setViewportAndProjection(Rect viewport,Rect clip)1322 void GLESRenderEngine::setViewportAndProjection(Rect viewport, Rect clip) {
1323     ATRACE_CALL();
1324     mVpWidth = viewport.getWidth();
1325     mVpHeight = viewport.getHeight();
1326 
1327     // We pass the the top left corner instead of the bottom left corner,
1328     // because since we're rendering off-screen first.
1329     glViewport(viewport.left, viewport.top, mVpWidth, mVpHeight);
1330 
1331     mState.projectionMatrix = mat4::ortho(clip.left, clip.right, clip.top, clip.bottom, 0, 1);
1332 }
1333 
setupLayerBlending(bool premultipliedAlpha,bool opaque,bool disableTexture,const half4 & color,float cornerRadius)1334 void GLESRenderEngine::setupLayerBlending(bool premultipliedAlpha, bool opaque, bool disableTexture,
1335                                           const half4& color, float cornerRadius) {
1336     mState.isPremultipliedAlpha = premultipliedAlpha;
1337     mState.isOpaque = opaque;
1338     mState.color = color;
1339     mState.cornerRadius = cornerRadius;
1340 
1341     if (disableTexture) {
1342         mState.textureEnabled = false;
1343     }
1344 
1345     if (color.a < 1.0f || !opaque || cornerRadius > 0.0f) {
1346         glEnable(GL_BLEND);
1347         glBlendFuncSeparate(premultipliedAlpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
1348                             GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
1349     } else {
1350         glDisable(GL_BLEND);
1351     }
1352 }
1353 
setSourceY410BT2020(bool enable)1354 void GLESRenderEngine::setSourceY410BT2020(bool enable) {
1355     mState.isY410BT2020 = enable;
1356 }
1357 
setSourceDataSpace(Dataspace source)1358 void GLESRenderEngine::setSourceDataSpace(Dataspace source) {
1359     mDataSpace = source;
1360 }
1361 
setOutputDataSpace(Dataspace dataspace)1362 void GLESRenderEngine::setOutputDataSpace(Dataspace dataspace) {
1363     mOutputDataSpace = dataspace;
1364 }
1365 
setDisplayMaxLuminance(const float maxLuminance)1366 void GLESRenderEngine::setDisplayMaxLuminance(const float maxLuminance) {
1367     mState.displayMaxLuminance = maxLuminance;
1368 }
1369 
setupLayerTexturing(const Texture & texture)1370 void GLESRenderEngine::setupLayerTexturing(const Texture& texture) {
1371     GLuint target = texture.getTextureTarget();
1372     glBindTexture(target, texture.getTextureName());
1373     GLenum filter = GL_NEAREST;
1374     if (texture.getFiltering()) {
1375         filter = GL_LINEAR;
1376     }
1377     glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
1378     glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
1379     glTexParameteri(target, GL_TEXTURE_MAG_FILTER, filter);
1380     glTexParameteri(target, GL_TEXTURE_MIN_FILTER, filter);
1381 
1382     mState.texture = texture;
1383     mState.textureEnabled = true;
1384 }
1385 
setColorTransform(const mat4 & colorTransform)1386 void GLESRenderEngine::setColorTransform(const mat4& colorTransform) {
1387     mState.colorMatrix = colorTransform;
1388 }
1389 
setDisplayColorTransform(const mat4 & colorTransform)1390 void GLESRenderEngine::setDisplayColorTransform(const mat4& colorTransform) {
1391     mState.displayColorMatrix = colorTransform;
1392 }
1393 
disableTexturing()1394 void GLESRenderEngine::disableTexturing() {
1395     mState.textureEnabled = false;
1396 }
1397 
disableBlending()1398 void GLESRenderEngine::disableBlending() {
1399     glDisable(GL_BLEND);
1400 }
1401 
setupFillWithColor(float r,float g,float b,float a)1402 void GLESRenderEngine::setupFillWithColor(float r, float g, float b, float a) {
1403     mState.isPremultipliedAlpha = true;
1404     mState.isOpaque = false;
1405     mState.color = half4(r, g, b, a);
1406     mState.textureEnabled = false;
1407     glDisable(GL_BLEND);
1408 }
1409 
setupCornerRadiusCropSize(float width,float height)1410 void GLESRenderEngine::setupCornerRadiusCropSize(float width, float height) {
1411     mState.cropSize = half2(width, height);
1412 }
1413 
drawMesh(const Mesh & mesh)1414 void GLESRenderEngine::drawMesh(const Mesh& mesh) {
1415     ATRACE_CALL();
1416     if (mesh.getTexCoordsSize()) {
1417         glEnableVertexAttribArray(Program::texCoords);
1418         glVertexAttribPointer(Program::texCoords, mesh.getTexCoordsSize(), GL_FLOAT, GL_FALSE,
1419                               mesh.getByteStride(), mesh.getTexCoords());
1420     }
1421 
1422     glVertexAttribPointer(Program::position, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
1423                           mesh.getByteStride(), mesh.getPositions());
1424 
1425     if (mState.cornerRadius > 0.0f) {
1426         glEnableVertexAttribArray(Program::cropCoords);
1427         glVertexAttribPointer(Program::cropCoords, mesh.getVertexSize(), GL_FLOAT, GL_FALSE,
1428                               mesh.getByteStride(), mesh.getCropCoords());
1429     }
1430 
1431     if (mState.drawShadows) {
1432         glEnableVertexAttribArray(Program::shadowColor);
1433         glVertexAttribPointer(Program::shadowColor, mesh.getShadowColorSize(), GL_FLOAT, GL_FALSE,
1434                               mesh.getByteStride(), mesh.getShadowColor());
1435 
1436         glEnableVertexAttribArray(Program::shadowParams);
1437         glVertexAttribPointer(Program::shadowParams, mesh.getShadowParamsSize(), GL_FLOAT, GL_FALSE,
1438                               mesh.getByteStride(), mesh.getShadowParams());
1439     }
1440 
1441     Description managedState = mState;
1442     // By default, DISPLAY_P3 is the only supported wide color output. However,
1443     // when HDR content is present, hardware composer may be able to handle
1444     // BT2020 data space, in that case, the output data space is set to be
1445     // BT2020_HLG or BT2020_PQ respectively. In GPU fall back we need
1446     // to respect this and convert non-HDR content to HDR format.
1447     if (mUseColorManagement) {
1448         Dataspace inputStandard = static_cast<Dataspace>(mDataSpace & Dataspace::STANDARD_MASK);
1449         Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
1450         Dataspace outputStandard =
1451                 static_cast<Dataspace>(mOutputDataSpace & Dataspace::STANDARD_MASK);
1452         Dataspace outputTransfer =
1453                 static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
1454         bool needsXYZConversion = needsXYZTransformMatrix();
1455 
1456         // NOTE: if the input standard of the input dataspace is not STANDARD_DCI_P3 or
1457         // STANDARD_BT2020, it will be  treated as STANDARD_BT709
1458         if (inputStandard != Dataspace::STANDARD_DCI_P3 &&
1459             inputStandard != Dataspace::STANDARD_BT2020) {
1460             inputStandard = Dataspace::STANDARD_BT709;
1461         }
1462 
1463         if (needsXYZConversion) {
1464             // The supported input color spaces are standard RGB, Display P3 and BT2020.
1465             switch (inputStandard) {
1466                 case Dataspace::STANDARD_DCI_P3:
1467                     managedState.inputTransformMatrix = mDisplayP3ToXyz;
1468                     break;
1469                 case Dataspace::STANDARD_BT2020:
1470                     managedState.inputTransformMatrix = mBt2020ToXyz;
1471                     break;
1472                 default:
1473                     managedState.inputTransformMatrix = mSrgbToXyz;
1474                     break;
1475             }
1476 
1477             // The supported output color spaces are BT2020, Display P3 and standard RGB.
1478             switch (outputStandard) {
1479                 case Dataspace::STANDARD_BT2020:
1480                     managedState.outputTransformMatrix = mXyzToBt2020;
1481                     break;
1482                 case Dataspace::STANDARD_DCI_P3:
1483                     managedState.outputTransformMatrix = mXyzToDisplayP3;
1484                     break;
1485                 default:
1486                     managedState.outputTransformMatrix = mXyzToSrgb;
1487                     break;
1488             }
1489         } else if (inputStandard != outputStandard) {
1490             // At this point, the input data space and output data space could be both
1491             // HDR data spaces, but they match each other, we do nothing in this case.
1492             // In addition to the case above, the input data space could be
1493             // - scRGB linear
1494             // - scRGB non-linear
1495             // - sRGB
1496             // - Display P3
1497             // - BT2020
1498             // The output data spaces could be
1499             // - sRGB
1500             // - Display P3
1501             // - BT2020
1502             switch (outputStandard) {
1503                 case Dataspace::STANDARD_BT2020:
1504                     if (inputStandard == Dataspace::STANDARD_BT709) {
1505                         managedState.outputTransformMatrix = mSrgbToBt2020;
1506                     } else if (inputStandard == Dataspace::STANDARD_DCI_P3) {
1507                         managedState.outputTransformMatrix = mDisplayP3ToBt2020;
1508                     }
1509                     break;
1510                 case Dataspace::STANDARD_DCI_P3:
1511                     if (inputStandard == Dataspace::STANDARD_BT709) {
1512                         managedState.outputTransformMatrix = mSrgbToDisplayP3;
1513                     } else if (inputStandard == Dataspace::STANDARD_BT2020) {
1514                         managedState.outputTransformMatrix = mBt2020ToDisplayP3;
1515                     }
1516                     break;
1517                 default:
1518                     if (inputStandard == Dataspace::STANDARD_DCI_P3) {
1519                         managedState.outputTransformMatrix = mDisplayP3ToSrgb;
1520                     } else if (inputStandard == Dataspace::STANDARD_BT2020) {
1521                         managedState.outputTransformMatrix = mBt2020ToSrgb;
1522                     }
1523                     break;
1524             }
1525         }
1526 
1527         // we need to convert the RGB value to linear space and convert it back when:
1528         // - there is a color matrix that is not an identity matrix, or
1529         // - there is an output transform matrix that is not an identity matrix, or
1530         // - the input transfer function doesn't match the output transfer function.
1531         if (managedState.hasColorMatrix() || managedState.hasOutputTransformMatrix() ||
1532             inputTransfer != outputTransfer) {
1533             managedState.inputTransferFunction =
1534                     Description::dataSpaceToTransferFunction(inputTransfer);
1535             managedState.outputTransferFunction =
1536                     Description::dataSpaceToTransferFunction(outputTransfer);
1537         }
1538     }
1539 
1540     ProgramCache::getInstance().useProgram(mInProtectedContext ? mProtectedEGLContext : mEGLContext,
1541                                            managedState);
1542 
1543     if (mState.drawShadows) {
1544         glDrawElements(mesh.getPrimitive(), mesh.getIndexCount(), GL_UNSIGNED_SHORT,
1545                        mesh.getIndices());
1546     } else {
1547         glDrawArrays(mesh.getPrimitive(), 0, mesh.getVertexCount());
1548     }
1549 
1550     if (mUseColorManagement && outputDebugPPMs) {
1551         static uint64_t managedColorFrameCount = 0;
1552         std::ostringstream out;
1553         out << "/data/texture_out" << managedColorFrameCount++;
1554         writePPM(out.str().c_str(), mVpWidth, mVpHeight);
1555     }
1556 
1557     if (mesh.getTexCoordsSize()) {
1558         glDisableVertexAttribArray(Program::texCoords);
1559     }
1560 
1561     if (mState.cornerRadius > 0.0f) {
1562         glDisableVertexAttribArray(Program::cropCoords);
1563     }
1564 
1565     if (mState.drawShadows) {
1566         glDisableVertexAttribArray(Program::shadowColor);
1567         glDisableVertexAttribArray(Program::shadowParams);
1568     }
1569 }
1570 
getMaxTextureSize() const1571 size_t GLESRenderEngine::getMaxTextureSize() const {
1572     return mMaxTextureSize;
1573 }
1574 
getMaxViewportDims() const1575 size_t GLESRenderEngine::getMaxViewportDims() const {
1576     return mMaxViewportDims[0] < mMaxViewportDims[1] ? mMaxViewportDims[0] : mMaxViewportDims[1];
1577 }
1578 
dump(std::string & result)1579 void GLESRenderEngine::dump(std::string& result) {
1580     const GLExtensions& extensions = GLExtensions::getInstance();
1581     ProgramCache& cache = ProgramCache::getInstance();
1582 
1583     StringAppendF(&result, "EGL implementation : %s\n", extensions.getEGLVersion());
1584     StringAppendF(&result, "%s\n", extensions.getEGLExtensions());
1585     StringAppendF(&result, "GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
1586                   extensions.getVersion());
1587     StringAppendF(&result, "%s\n", extensions.getExtensions());
1588     StringAppendF(&result, "RenderEngine supports protected context: %d\n",
1589                   supportsProtectedContent());
1590     StringAppendF(&result, "RenderEngine is in protected context: %d\n", mInProtectedContext);
1591     StringAppendF(&result, "RenderEngine program cache size for unprotected context: %zu\n",
1592                   cache.getSize(mEGLContext));
1593     StringAppendF(&result, "RenderEngine program cache size for protected context: %zu\n",
1594                   cache.getSize(mProtectedEGLContext));
1595     StringAppendF(&result, "RenderEngine last dataspace conversion: (%s) to (%s)\n",
1596                   dataspaceDetails(static_cast<android_dataspace>(mDataSpace)).c_str(),
1597                   dataspaceDetails(static_cast<android_dataspace>(mOutputDataSpace)).c_str());
1598     {
1599         std::lock_guard<std::mutex> lock(mRenderingMutex);
1600         StringAppendF(&result, "RenderEngine image cache size: %zu\n", mImageCache.size());
1601         StringAppendF(&result, "Dumping buffer ids...\n");
1602         for (const auto& [id, unused] : mImageCache) {
1603             StringAppendF(&result, "0x%" PRIx64 "\n", id);
1604         }
1605     }
1606     {
1607         std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
1608         StringAppendF(&result, "RenderEngine framebuffer image cache size: %zu\n",
1609                       mFramebufferImageCache.size());
1610         StringAppendF(&result, "Dumping buffer ids...\n");
1611         for (const auto& [id, unused] : mFramebufferImageCache) {
1612             StringAppendF(&result, "0x%" PRIx64 "\n", id);
1613         }
1614     }
1615 }
1616 
parseGlesVersion(const char * str)1617 GLESRenderEngine::GlesVersion GLESRenderEngine::parseGlesVersion(const char* str) {
1618     int major, minor;
1619     if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
1620         if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
1621             ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
1622             return GLES_VERSION_1_0;
1623         }
1624     }
1625 
1626     if (major == 1 && minor == 0) return GLES_VERSION_1_0;
1627     if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
1628     if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
1629     if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
1630 
1631     ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
1632     return GLES_VERSION_1_0;
1633 }
1634 
createEglContext(EGLDisplay display,EGLConfig config,EGLContext shareContext,std::optional<ContextPriority> contextPriority,Protection protection)1635 EGLContext GLESRenderEngine::createEglContext(EGLDisplay display, EGLConfig config,
1636                                               EGLContext shareContext,
1637                                               std::optional<ContextPriority> contextPriority,
1638                                               Protection protection) {
1639     EGLint renderableType = 0;
1640     if (config == EGL_NO_CONFIG) {
1641         renderableType = EGL_OPENGL_ES3_BIT;
1642     } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
1643         LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
1644     }
1645     EGLint contextClientVersion = 0;
1646     if (renderableType & EGL_OPENGL_ES3_BIT) {
1647         contextClientVersion = 3;
1648     } else if (renderableType & EGL_OPENGL_ES2_BIT) {
1649         contextClientVersion = 2;
1650     } else if (renderableType & EGL_OPENGL_ES_BIT) {
1651         contextClientVersion = 1;
1652     } else {
1653         LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
1654     }
1655 
1656     std::vector<EGLint> contextAttributes;
1657     contextAttributes.reserve(7);
1658     contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
1659     contextAttributes.push_back(contextClientVersion);
1660     if (contextPriority) {
1661         contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
1662         switch (*contextPriority) {
1663             case ContextPriority::REALTIME:
1664                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_REALTIME_NV);
1665                 break;
1666             case ContextPriority::MEDIUM:
1667                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_MEDIUM_IMG);
1668                 break;
1669             case ContextPriority::LOW:
1670                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LOW_IMG);
1671                 break;
1672             case ContextPriority::HIGH:
1673             default:
1674                 contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
1675                 break;
1676         }
1677     }
1678     if (protection == Protection::PROTECTED) {
1679         contextAttributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1680         contextAttributes.push_back(EGL_TRUE);
1681     }
1682     contextAttributes.push_back(EGL_NONE);
1683 
1684     EGLContext context = eglCreateContext(display, config, shareContext, contextAttributes.data());
1685 
1686     if (contextClientVersion == 3 && context == EGL_NO_CONTEXT) {
1687         // eglGetConfigAttrib indicated we can create GLES 3 context, but we failed, thus
1688         // EGL_NO_CONTEXT so that we can abort.
1689         if (config != EGL_NO_CONFIG) {
1690             return context;
1691         }
1692         // If |config| is EGL_NO_CONFIG, we speculatively try to create GLES 3 context, so we should
1693         // try to fall back to GLES 2.
1694         contextAttributes[1] = 2;
1695         context = eglCreateContext(display, config, shareContext, contextAttributes.data());
1696     }
1697 
1698     return context;
1699 }
1700 
createStubEglPbufferSurface(EGLDisplay display,EGLConfig config,int hwcFormat,Protection protection)1701 EGLSurface GLESRenderEngine::createStubEglPbufferSurface(EGLDisplay display, EGLConfig config,
1702                                                          int hwcFormat, Protection protection) {
1703     EGLConfig stubConfig = config;
1704     if (stubConfig == EGL_NO_CONFIG) {
1705         stubConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
1706     }
1707     std::vector<EGLint> attributes;
1708     attributes.reserve(7);
1709     attributes.push_back(EGL_WIDTH);
1710     attributes.push_back(1);
1711     attributes.push_back(EGL_HEIGHT);
1712     attributes.push_back(1);
1713     if (protection == Protection::PROTECTED) {
1714         attributes.push_back(EGL_PROTECTED_CONTENT_EXT);
1715         attributes.push_back(EGL_TRUE);
1716     }
1717     attributes.push_back(EGL_NONE);
1718 
1719     return eglCreatePbufferSurface(display, stubConfig, attributes.data());
1720 }
1721 
isHdrDataSpace(const Dataspace dataSpace) const1722 bool GLESRenderEngine::isHdrDataSpace(const Dataspace dataSpace) const {
1723     const Dataspace standard = static_cast<Dataspace>(dataSpace & Dataspace::STANDARD_MASK);
1724     const Dataspace transfer = static_cast<Dataspace>(dataSpace & Dataspace::TRANSFER_MASK);
1725     return standard == Dataspace::STANDARD_BT2020 &&
1726             (transfer == Dataspace::TRANSFER_ST2084 || transfer == Dataspace::TRANSFER_HLG);
1727 }
1728 
1729 // For convenience, we want to convert the input color space to XYZ color space first,
1730 // and then convert from XYZ color space to output color space when
1731 // - SDR and HDR contents are mixed, either SDR content will be converted to HDR or
1732 //   HDR content will be tone-mapped to SDR; Or,
1733 // - there are HDR PQ and HLG contents presented at the same time, where we want to convert
1734 //   HLG content to PQ content.
1735 // In either case above, we need to operate the Y value in XYZ color space. Thus, when either
1736 // input data space or output data space is HDR data space, and the input transfer function
1737 // doesn't match the output transfer function, we would enable an intermediate transfrom to
1738 // XYZ color space.
needsXYZTransformMatrix() const1739 bool GLESRenderEngine::needsXYZTransformMatrix() const {
1740     const bool isInputHdrDataSpace = isHdrDataSpace(mDataSpace);
1741     const bool isOutputHdrDataSpace = isHdrDataSpace(mOutputDataSpace);
1742     const Dataspace inputTransfer = static_cast<Dataspace>(mDataSpace & Dataspace::TRANSFER_MASK);
1743     const Dataspace outputTransfer =
1744             static_cast<Dataspace>(mOutputDataSpace & Dataspace::TRANSFER_MASK);
1745 
1746     return (isInputHdrDataSpace || isOutputHdrDataSpace) && inputTransfer != outputTransfer;
1747 }
1748 
isImageCachedForTesting(uint64_t bufferId)1749 bool GLESRenderEngine::isImageCachedForTesting(uint64_t bufferId) {
1750     std::lock_guard<std::mutex> lock(mRenderingMutex);
1751     const auto& cachedImage = mImageCache.find(bufferId);
1752     return cachedImage != mImageCache.end();
1753 }
1754 
isTextureNameKnownForTesting(uint32_t texName)1755 bool GLESRenderEngine::isTextureNameKnownForTesting(uint32_t texName) {
1756     const auto& entry = mTextureView.find(texName);
1757     return entry != mTextureView.end();
1758 }
1759 
getBufferIdForTextureNameForTesting(uint32_t texName)1760 std::optional<uint64_t> GLESRenderEngine::getBufferIdForTextureNameForTesting(uint32_t texName) {
1761     const auto& entry = mTextureView.find(texName);
1762     return entry != mTextureView.end() ? entry->second : std::nullopt;
1763 }
1764 
isFramebufferImageCachedForTesting(uint64_t bufferId)1765 bool GLESRenderEngine::isFramebufferImageCachedForTesting(uint64_t bufferId) {
1766     std::lock_guard<std::mutex> lock(mFramebufferImageCacheMutex);
1767     return std::any_of(mFramebufferImageCache.cbegin(), mFramebufferImageCache.cend(),
1768                        [=](std::pair<uint64_t, EGLImageKHR> image) {
1769                            return image.first == bufferId;
1770                        });
1771 }
1772 
1773 // FlushTracer implementation
FlushTracer(GLESRenderEngine * engine)1774 GLESRenderEngine::FlushTracer::FlushTracer(GLESRenderEngine* engine) : mEngine(engine) {
1775     mThread = std::thread(&GLESRenderEngine::FlushTracer::loop, this);
1776 }
1777 
~FlushTracer()1778 GLESRenderEngine::FlushTracer::~FlushTracer() {
1779     {
1780         std::lock_guard<std::mutex> lock(mMutex);
1781         mRunning = false;
1782     }
1783     mCondition.notify_all();
1784     if (mThread.joinable()) {
1785         mThread.join();
1786     }
1787 }
1788 
queueSync(EGLSyncKHR sync)1789 void GLESRenderEngine::FlushTracer::queueSync(EGLSyncKHR sync) {
1790     std::lock_guard<std::mutex> lock(mMutex);
1791     char name[64];
1792     const uint64_t frameNum = mFramesQueued++;
1793     snprintf(name, sizeof(name), "Queueing sync for frame: %lu",
1794              static_cast<unsigned long>(frameNum));
1795     ATRACE_NAME(name);
1796     mQueue.push({sync, frameNum});
1797     ATRACE_INT("GPU Frames Outstanding", mQueue.size());
1798     mCondition.notify_one();
1799 }
1800 
loop()1801 void GLESRenderEngine::FlushTracer::loop() {
1802     while (mRunning) {
1803         QueueEntry entry;
1804         {
1805             std::lock_guard<std::mutex> lock(mMutex);
1806 
1807             mCondition.wait(mMutex,
1808                             [&]() REQUIRES(mMutex) { return !mQueue.empty() || !mRunning; });
1809 
1810             if (!mRunning) {
1811                 // if mRunning is false, then FlushTracer is being destroyed, so
1812                 // bail out now.
1813                 break;
1814             }
1815             entry = mQueue.front();
1816             mQueue.pop();
1817         }
1818         {
1819             char name[64];
1820             snprintf(name, sizeof(name), "waiting for frame %lu",
1821                      static_cast<unsigned long>(entry.mFrameNum));
1822             ATRACE_NAME(name);
1823             mEngine->waitSync(entry.mSync, 0);
1824         }
1825     }
1826 }
1827 
handleShadow(const FloatRect & casterRect,float casterCornerRadius,const ShadowSettings & settings)1828 void GLESRenderEngine::handleShadow(const FloatRect& casterRect, float casterCornerRadius,
1829                                     const ShadowSettings& settings) {
1830     ATRACE_CALL();
1831     const float casterZ = settings.length / 2.0f;
1832     const GLShadowVertexGenerator shadows(casterRect, casterCornerRadius, casterZ,
1833                                           settings.casterIsTranslucent, settings.ambientColor,
1834                                           settings.spotColor, settings.lightPos,
1835                                           settings.lightRadius);
1836 
1837     // setup mesh for both shadows
1838     Mesh mesh = Mesh::Builder()
1839                         .setPrimitive(Mesh::TRIANGLES)
1840                         .setVertices(shadows.getVertexCount(), 2 /* size */)
1841                         .setShadowAttrs()
1842                         .setIndices(shadows.getIndexCount())
1843                         .build();
1844 
1845     Mesh::VertexArray<vec2> position = mesh.getPositionArray<vec2>();
1846     Mesh::VertexArray<vec4> shadowColor = mesh.getShadowColorArray<vec4>();
1847     Mesh::VertexArray<vec3> shadowParams = mesh.getShadowParamsArray<vec3>();
1848     shadows.fillVertices(position, shadowColor, shadowParams);
1849     shadows.fillIndices(mesh.getIndicesArray());
1850 
1851     mState.cornerRadius = 0.0f;
1852     mState.drawShadows = true;
1853     setupLayerTexturing(mShadowTexture->getTexture());
1854     drawMesh(mesh);
1855     mState.drawShadows = false;
1856 }
1857 
1858 } // namespace gl
1859 } // namespace renderengine
1860 } // namespace android
1861