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 #include <log/log.h>
18 #include <ui/Rect.h>
19 #include <ui/Region.h>
20 
21 #include "GLES20RenderEngine.h"
22 #include "GLExtensions.h"
23 #include "Image.h"
24 #include "Mesh.h"
25 #include "RenderEngine.h"
26 
27 #include <SurfaceFlinger.h>
28 #include <vector>
29 
30 #include <android/hardware/configstore/1.0/ISurfaceFlingerConfigs.h>
31 #include <configstore/Utils.h>
32 
33 using namespace android::hardware::configstore;
34 using namespace android::hardware::configstore::V1_0;
35 
36 extern "C" EGLAPI const char* eglQueryStringImplementationANDROID(EGLDisplay dpy, EGLint name);
37 
38 // ---------------------------------------------------------------------------
39 namespace android {
40 namespace RE {
41 // ---------------------------------------------------------------------------
42 
43 RenderEngine::~RenderEngine() = default;
44 
45 namespace impl {
46 
create(int hwcFormat,uint32_t featureFlags)47 std::unique_ptr<RenderEngine> RenderEngine::create(int hwcFormat, uint32_t featureFlags) {
48     // initialize EGL for the default display
49     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);
50     if (!eglInitialize(display, nullptr, nullptr)) {
51         LOG_ALWAYS_FATAL("failed to initialize EGL");
52     }
53 
54     GLExtensions& extensions = GLExtensions::getInstance();
55     extensions.initWithEGLStrings(eglQueryStringImplementationANDROID(display, EGL_VERSION),
56                                   eglQueryStringImplementationANDROID(display, EGL_EXTENSIONS));
57 
58     // The code assumes that ES2 or later is available if this extension is
59     // supported.
60     EGLConfig config = EGL_NO_CONFIG;
61     if (!extensions.hasNoConfigContext()) {
62         config = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
63     }
64 
65     EGLint renderableType = 0;
66     if (config == EGL_NO_CONFIG) {
67         renderableType = EGL_OPENGL_ES2_BIT;
68     } else if (!eglGetConfigAttrib(display, config, EGL_RENDERABLE_TYPE, &renderableType)) {
69         LOG_ALWAYS_FATAL("can't query EGLConfig RENDERABLE_TYPE");
70     }
71     EGLint contextClientVersion = 0;
72     if (renderableType & EGL_OPENGL_ES2_BIT) {
73         contextClientVersion = 2;
74     } else if (renderableType & EGL_OPENGL_ES_BIT) {
75         contextClientVersion = 1;
76     } else {
77         LOG_ALWAYS_FATAL("no supported EGL_RENDERABLE_TYPEs");
78     }
79 
80     std::vector<EGLint> contextAttributes;
81     contextAttributes.reserve(6);
82     contextAttributes.push_back(EGL_CONTEXT_CLIENT_VERSION);
83     contextAttributes.push_back(contextClientVersion);
84     bool useContextPriority = overrideUseContextPriorityFromConfig(extensions.hasContextPriority());
85     if (useContextPriority) {
86         contextAttributes.push_back(EGL_CONTEXT_PRIORITY_LEVEL_IMG);
87         contextAttributes.push_back(EGL_CONTEXT_PRIORITY_HIGH_IMG);
88     }
89     contextAttributes.push_back(EGL_NONE);
90 
91     EGLContext ctxt = eglCreateContext(display, config, nullptr, contextAttributes.data());
92 
93     // if can't create a GL context, we can only abort.
94     LOG_ALWAYS_FATAL_IF(ctxt == EGL_NO_CONTEXT, "EGLContext creation failed");
95 
96     // now figure out what version of GL did we actually get
97     // NOTE: a dummy surface is not needed if KHR_create_context is supported
98 
99     EGLConfig dummyConfig = config;
100     if (dummyConfig == EGL_NO_CONFIG) {
101         dummyConfig = chooseEglConfig(display, hwcFormat, /*logConfig*/ true);
102     }
103     EGLint attribs[] = {EGL_WIDTH, 1, EGL_HEIGHT, 1, EGL_NONE, EGL_NONE};
104     EGLSurface dummy = eglCreatePbufferSurface(display, dummyConfig, attribs);
105     LOG_ALWAYS_FATAL_IF(dummy == EGL_NO_SURFACE, "can't create dummy pbuffer");
106     EGLBoolean success = eglMakeCurrent(display, dummy, dummy, ctxt);
107     LOG_ALWAYS_FATAL_IF(!success, "can't make dummy pbuffer current");
108 
109     extensions.initWithGLStrings(glGetString(GL_VENDOR), glGetString(GL_RENDERER),
110                                  glGetString(GL_VERSION), glGetString(GL_EXTENSIONS));
111 
112     GlesVersion version = parseGlesVersion(extensions.getVersion());
113 
114     // initialize the renderer while GL is current
115 
116     std::unique_ptr<RenderEngine> engine;
117     switch (version) {
118         case GLES_VERSION_1_0:
119         case GLES_VERSION_1_1:
120             LOG_ALWAYS_FATAL("SurfaceFlinger requires OpenGL ES 2.0 minimum to run.");
121             break;
122         case GLES_VERSION_2_0:
123         case GLES_VERSION_3_0:
124             engine = std::make_unique<GLES20RenderEngine>(featureFlags);
125             break;
126     }
127     engine->setEGLHandles(display, config, ctxt);
128 
129     ALOGI("OpenGL ES informations:");
130     ALOGI("vendor    : %s", extensions.getVendor());
131     ALOGI("renderer  : %s", extensions.getRenderer());
132     ALOGI("version   : %s", extensions.getVersion());
133     ALOGI("extensions: %s", extensions.getExtensions());
134     ALOGI("GL_MAX_TEXTURE_SIZE = %zu", engine->getMaxTextureSize());
135     ALOGI("GL_MAX_VIEWPORT_DIMS = %zu", engine->getMaxViewportDims());
136 
137     eglMakeCurrent(display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
138     eglDestroySurface(display, dummy);
139 
140     return engine;
141 }
142 
overrideUseContextPriorityFromConfig(bool useContextPriority)143 bool RenderEngine::overrideUseContextPriorityFromConfig(bool useContextPriority) {
144     OptionalBool ret;
145     ISurfaceFlingerConfigs::getService()->useContextPriority([&ret](OptionalBool b) { ret = b; });
146     if (ret.specified) {
147         return ret.value;
148     } else {
149         return useContextPriority;
150     }
151 }
152 
RenderEngine(uint32_t featureFlags)153 RenderEngine::RenderEngine(uint32_t featureFlags)
154       : mEGLDisplay(EGL_NO_DISPLAY),
155         mEGLConfig(nullptr),
156         mEGLContext(EGL_NO_CONTEXT),
157         mFeatureFlags(featureFlags) {}
158 
~RenderEngine()159 RenderEngine::~RenderEngine() {
160     eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
161     eglTerminate(mEGLDisplay);
162 }
163 
setEGLHandles(EGLDisplay display,EGLConfig config,EGLContext ctxt)164 void RenderEngine::setEGLHandles(EGLDisplay display, EGLConfig config, EGLContext ctxt) {
165     mEGLDisplay = display;
166     mEGLConfig = config;
167     mEGLContext = ctxt;
168 }
169 
getEGLDisplay() const170 EGLDisplay RenderEngine::getEGLDisplay() const {
171     return mEGLDisplay;
172 }
173 
getEGLConfig() const174 EGLConfig RenderEngine::getEGLConfig() const {
175     return mEGLConfig;
176 }
177 
supportsImageCrop() const178 bool RenderEngine::supportsImageCrop() const {
179     return GLExtensions::getInstance().hasImageCrop();
180 }
181 
isCurrent() const182 bool RenderEngine::isCurrent() const {
183     return mEGLDisplay == eglGetCurrentDisplay() && mEGLContext == eglGetCurrentContext();
184 }
185 
createSurface()186 std::unique_ptr<RE::Surface> RenderEngine::createSurface() {
187     return std::make_unique<Surface>(*this);
188 }
189 
createImage()190 std::unique_ptr<RE::Image> RenderEngine::createImage() {
191     return std::make_unique<Image>(*this);
192 }
193 
setCurrentSurface(const android::RE::Surface & surface)194 bool RenderEngine::setCurrentSurface(const android::RE::Surface& surface) {
195     // Note: RE::Surface is an abstract interface. This implementation only ever
196     // creates RE::impl::Surface's, so it is safe to just cast to the actual
197     // type.
198     return setCurrentSurface(static_cast<const android::RE::impl::Surface&>(surface));
199 }
200 
setCurrentSurface(const android::RE::impl::Surface & surface)201 bool RenderEngine::setCurrentSurface(const android::RE::impl::Surface& surface) {
202     bool success = true;
203     EGLSurface eglSurface = surface.getEGLSurface();
204     if (eglSurface != eglGetCurrentSurface(EGL_DRAW)) {
205         success = eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext) == EGL_TRUE;
206         if (success && surface.getAsync()) {
207             eglSwapInterval(mEGLDisplay, 0);
208         }
209     }
210 
211     return success;
212 }
213 
resetCurrentSurface()214 void RenderEngine::resetCurrentSurface() {
215     eglMakeCurrent(mEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
216 }
217 
flush()218 base::unique_fd RenderEngine::flush() {
219     if (!GLExtensions::getInstance().hasNativeFenceSync()) {
220         return base::unique_fd();
221     }
222 
223     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, nullptr);
224     if (sync == EGL_NO_SYNC_KHR) {
225         ALOGW("failed to create EGL native fence sync: %#x", eglGetError());
226         return base::unique_fd();
227     }
228 
229     // native fence fd will not be populated until flush() is done.
230     glFlush();
231 
232     // get the fence fd
233     base::unique_fd fenceFd(eglDupNativeFenceFDANDROID(mEGLDisplay, sync));
234     eglDestroySyncKHR(mEGLDisplay, sync);
235     if (fenceFd == EGL_NO_NATIVE_FENCE_FD_ANDROID) {
236         ALOGW("failed to dup EGL native fence sync: %#x", eglGetError());
237     }
238 
239     return fenceFd;
240 }
241 
finish()242 bool RenderEngine::finish() {
243     if (!GLExtensions::getInstance().hasFenceSync()) {
244         ALOGW("no synchronization support");
245         return false;
246     }
247 
248     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_FENCE_KHR, nullptr);
249     if (sync == EGL_NO_SYNC_KHR) {
250         ALOGW("failed to create EGL fence sync: %#x", eglGetError());
251         return false;
252     }
253 
254     EGLint result = eglClientWaitSyncKHR(mEGLDisplay, sync, EGL_SYNC_FLUSH_COMMANDS_BIT_KHR,
255                                          2000000000 /*2 sec*/);
256     EGLint error = eglGetError();
257     eglDestroySyncKHR(mEGLDisplay, sync);
258     if (result != EGL_CONDITION_SATISFIED_KHR) {
259         if (result == EGL_TIMEOUT_EXPIRED_KHR) {
260             ALOGW("fence wait timed out");
261         } else {
262             ALOGW("error waiting on EGL fence: %#x", error);
263         }
264         return false;
265     }
266 
267     return true;
268 }
269 
waitFence(base::unique_fd fenceFd)270 bool RenderEngine::waitFence(base::unique_fd fenceFd) {
271     if (!GLExtensions::getInstance().hasNativeFenceSync() ||
272         !GLExtensions::getInstance().hasWaitSync()) {
273         return false;
274     }
275 
276     EGLint attribs[] = {EGL_SYNC_NATIVE_FENCE_FD_ANDROID, fenceFd, EGL_NONE};
277     EGLSyncKHR sync = eglCreateSyncKHR(mEGLDisplay, EGL_SYNC_NATIVE_FENCE_ANDROID, attribs);
278     if (sync == EGL_NO_SYNC_KHR) {
279         ALOGE("failed to create EGL native fence sync: %#x", eglGetError());
280         return false;
281     }
282 
283     // fenceFd is now owned by EGLSync
284     (void)fenceFd.release();
285 
286     // XXX: The spec draft is inconsistent as to whether this should return an
287     // EGLint or void.  Ignore the return value for now, as it's not strictly
288     // needed.
289     eglWaitSyncKHR(mEGLDisplay, sync, 0);
290     EGLint error = eglGetError();
291     eglDestroySyncKHR(mEGLDisplay, sync);
292     if (error != EGL_SUCCESS) {
293         ALOGE("failed to wait for EGL native fence sync: %#x", error);
294         return false;
295     }
296 
297     return true;
298 }
299 
checkErrors() const300 void RenderEngine::checkErrors() const {
301     do {
302         // there could be more than one error flag
303         GLenum error = glGetError();
304         if (error == GL_NO_ERROR) break;
305         ALOGE("GL error 0x%04x", int(error));
306     } while (true);
307 }
308 
parseGlesVersion(const char * str)309 RenderEngine::GlesVersion RenderEngine::parseGlesVersion(const char* str) {
310     int major, minor;
311     if (sscanf(str, "OpenGL ES-CM %d.%d", &major, &minor) != 2) {
312         if (sscanf(str, "OpenGL ES %d.%d", &major, &minor) != 2) {
313             ALOGW("Unable to parse GL_VERSION string: \"%s\"", str);
314             return GLES_VERSION_1_0;
315         }
316     }
317 
318     if (major == 1 && minor == 0) return GLES_VERSION_1_0;
319     if (major == 1 && minor >= 1) return GLES_VERSION_1_1;
320     if (major == 2 && minor >= 0) return GLES_VERSION_2_0;
321     if (major == 3 && minor >= 0) return GLES_VERSION_3_0;
322 
323     ALOGW("Unrecognized OpenGL ES version: %d.%d", major, minor);
324     return GLES_VERSION_1_0;
325 }
326 
fillRegionWithColor(const Region & region,uint32_t height,float red,float green,float blue,float alpha)327 void RenderEngine::fillRegionWithColor(const Region& region, uint32_t height, float red,
328                                        float green, float blue, float alpha) {
329     size_t c;
330     Rect const* r = region.getArray(&c);
331     Mesh mesh(Mesh::TRIANGLES, c * 6, 2);
332     Mesh::VertexArray<vec2> position(mesh.getPositionArray<vec2>());
333     for (size_t i = 0; i < c; i++, r++) {
334         position[i * 6 + 0].x = r->left;
335         position[i * 6 + 0].y = height - r->top;
336         position[i * 6 + 1].x = r->left;
337         position[i * 6 + 1].y = height - r->bottom;
338         position[i * 6 + 2].x = r->right;
339         position[i * 6 + 2].y = height - r->bottom;
340         position[i * 6 + 3].x = r->left;
341         position[i * 6 + 3].y = height - r->top;
342         position[i * 6 + 4].x = r->right;
343         position[i * 6 + 4].y = height - r->bottom;
344         position[i * 6 + 5].x = r->right;
345         position[i * 6 + 5].y = height - r->top;
346     }
347     setupFillWithColor(red, green, blue, alpha);
348     drawMesh(mesh);
349 }
350 
clearWithColor(float red,float green,float blue,float alpha)351 void RenderEngine::clearWithColor(float red, float green, float blue, float alpha) {
352     glClearColor(red, green, blue, alpha);
353     glClear(GL_COLOR_BUFFER_BIT);
354 }
355 
setScissor(uint32_t left,uint32_t bottom,uint32_t right,uint32_t top)356 void RenderEngine::setScissor(uint32_t left, uint32_t bottom, uint32_t right, uint32_t top) {
357     glScissor(left, bottom, right, top);
358     glEnable(GL_SCISSOR_TEST);
359 }
360 
disableScissor()361 void RenderEngine::disableScissor() {
362     glDisable(GL_SCISSOR_TEST);
363 }
364 
genTextures(size_t count,uint32_t * names)365 void RenderEngine::genTextures(size_t count, uint32_t* names) {
366     glGenTextures(count, names);
367 }
368 
deleteTextures(size_t count,uint32_t const * names)369 void RenderEngine::deleteTextures(size_t count, uint32_t const* names) {
370     glDeleteTextures(count, names);
371 }
372 
bindExternalTextureImage(uint32_t texName,const android::RE::Image & image)373 void RenderEngine::bindExternalTextureImage(uint32_t texName, const android::RE::Image& image) {
374     // Note: RE::Image is an abstract interface. This implementation only ever
375     // creates RE::impl::Image's, so it is safe to just cast to the actual type.
376     return bindExternalTextureImage(texName, static_cast<const android::RE::impl::Image&>(image));
377 }
378 
bindExternalTextureImage(uint32_t texName,const android::RE::impl::Image & image)379 void RenderEngine::bindExternalTextureImage(uint32_t texName,
380                                             const android::RE::impl::Image& image) {
381     const GLenum target = GL_TEXTURE_EXTERNAL_OES;
382 
383     glBindTexture(target, texName);
384     if (image.getEGLImage() != EGL_NO_IMAGE_KHR) {
385         glEGLImageTargetTexture2DOES(target, static_cast<GLeglImageOES>(image.getEGLImage()));
386     }
387 }
388 
readPixels(size_t l,size_t b,size_t w,size_t h,uint32_t * pixels)389 void RenderEngine::readPixels(size_t l, size_t b, size_t w, size_t h, uint32_t* pixels) {
390     glReadPixels(l, b, w, h, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
391 }
392 
dump(String8 & result)393 void RenderEngine::dump(String8& result) {
394     const GLExtensions& extensions = GLExtensions::getInstance();
395 
396     result.appendFormat("EGL implementation : %s\n", extensions.getEGLVersion());
397     result.appendFormat("%s\n", extensions.getEGLExtensions());
398 
399     result.appendFormat("GLES: %s, %s, %s\n", extensions.getVendor(), extensions.getRenderer(),
400                         extensions.getVersion());
401     result.appendFormat("%s\n", extensions.getExtensions());
402 }
403 
404 // ---------------------------------------------------------------------------
405 
bindNativeBufferAsFrameBuffer(ANativeWindowBuffer * buffer,RE::BindNativeBufferAsFramebuffer * bindHelper)406 void RenderEngine::bindNativeBufferAsFrameBuffer(ANativeWindowBuffer* buffer,
407                                                  RE::BindNativeBufferAsFramebuffer* bindHelper) {
408     bindHelper->mImage = eglCreateImageKHR(mEGLDisplay, EGL_NO_CONTEXT, EGL_NATIVE_BUFFER_ANDROID,
409                                            buffer, nullptr);
410     if (bindHelper->mImage == EGL_NO_IMAGE_KHR) {
411         bindHelper->mStatus = NO_MEMORY;
412         return;
413     }
414 
415     uint32_t glStatus;
416     bindImageAsFramebuffer(bindHelper->mImage, &bindHelper->mTexName, &bindHelper->mFbName,
417                            &glStatus);
418 
419     ALOGE_IF(glStatus != GL_FRAMEBUFFER_COMPLETE_OES, "glCheckFramebufferStatusOES error %d",
420              glStatus);
421 
422     bindHelper->mStatus = glStatus == GL_FRAMEBUFFER_COMPLETE_OES ? NO_ERROR : BAD_VALUE;
423 }
424 
unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer * bindHelper)425 void RenderEngine::unbindNativeBufferAsFrameBuffer(RE::BindNativeBufferAsFramebuffer* bindHelper) {
426     if (bindHelper->mImage == EGL_NO_IMAGE_KHR) {
427         return;
428     }
429 
430     // back to main framebuffer
431     unbindFramebuffer(bindHelper->mTexName, bindHelper->mFbName);
432     eglDestroyImageKHR(mEGLDisplay, bindHelper->mImage);
433 
434     // Workaround for b/77935566 to force the EGL driver to release the
435     // screenshot buffer
436     setScissor(0, 0, 0, 0);
437     clearWithColor(0.0, 0.0, 0.0, 0.0);
438     disableScissor();
439 }
440 
441 // ---------------------------------------------------------------------------
442 
selectConfigForAttribute(EGLDisplay dpy,EGLint const * attrs,EGLint attribute,EGLint wanted,EGLConfig * outConfig)443 static status_t selectConfigForAttribute(EGLDisplay dpy, EGLint const* attrs, EGLint attribute,
444                                          EGLint wanted, EGLConfig* outConfig) {
445     EGLint numConfigs = -1, n = 0;
446     eglGetConfigs(dpy, nullptr, 0, &numConfigs);
447     EGLConfig* const configs = new EGLConfig[numConfigs];
448     eglChooseConfig(dpy, attrs, configs, numConfigs, &n);
449 
450     if (n) {
451         if (attribute != EGL_NONE) {
452             for (int i = 0; i < n; i++) {
453                 EGLint value = 0;
454                 eglGetConfigAttrib(dpy, configs[i], attribute, &value);
455                 if (wanted == value) {
456                     *outConfig = configs[i];
457                     delete[] configs;
458                     return NO_ERROR;
459                 }
460             }
461         } else {
462             // just pick the first one
463             *outConfig = configs[0];
464             delete[] configs;
465             return NO_ERROR;
466         }
467     }
468     delete[] configs;
469     return NAME_NOT_FOUND;
470 }
471 
472 class EGLAttributeVector {
473     struct Attribute;
474     class Adder;
475     friend class Adder;
476     KeyedVector<Attribute, EGLint> mList;
477     struct Attribute {
Attributeandroid::RE::impl::EGLAttributeVector::Attribute478         Attribute() : v(0){};
Attributeandroid::RE::impl::EGLAttributeVector::Attribute479         explicit Attribute(EGLint v) : v(v) {}
480         EGLint v;
operator <android::RE::impl::EGLAttributeVector::Attribute481         bool operator<(const Attribute& other) const {
482             // this places EGL_NONE at the end
483             EGLint lhs(v);
484             EGLint rhs(other.v);
485             if (lhs == EGL_NONE) lhs = 0x7FFFFFFF;
486             if (rhs == EGL_NONE) rhs = 0x7FFFFFFF;
487             return lhs < rhs;
488         }
489     };
490     class Adder {
491         friend class EGLAttributeVector;
492         EGLAttributeVector& v;
493         EGLint attribute;
Adder(EGLAttributeVector & v,EGLint attribute)494         Adder(EGLAttributeVector& v, EGLint attribute) : v(v), attribute(attribute) {}
495 
496     public:
operator =(EGLint value)497         void operator=(EGLint value) {
498             if (attribute != EGL_NONE) {
499                 v.mList.add(Attribute(attribute), value);
500             }
501         }
operator EGLint() const502         operator EGLint() const { return v.mList[attribute]; }
503     };
504 
505 public:
EGLAttributeVector()506     EGLAttributeVector() { mList.add(Attribute(EGL_NONE), EGL_NONE); }
remove(EGLint attribute)507     void remove(EGLint attribute) {
508         if (attribute != EGL_NONE) {
509             mList.removeItem(Attribute(attribute));
510         }
511     }
operator [](EGLint attribute)512     Adder operator[](EGLint attribute) { return Adder(*this, attribute); }
operator [](EGLint attribute) const513     EGLint operator[](EGLint attribute) const { return mList[attribute]; }
514     // cast-operator to (EGLint const*)
operator EGLint const*() const515     operator EGLint const*() const { return &mList.keyAt(0).v; }
516 };
517 
selectEGLConfig(EGLDisplay display,EGLint format,EGLint renderableType,EGLConfig * config)518 static status_t selectEGLConfig(EGLDisplay display, EGLint format, EGLint renderableType,
519                                 EGLConfig* config) {
520     // select our EGLConfig. It must support EGL_RECORDABLE_ANDROID if
521     // it is to be used with WIFI displays
522     status_t err;
523     EGLint wantedAttribute;
524     EGLint wantedAttributeValue;
525 
526     EGLAttributeVector attribs;
527     if (renderableType) {
528         attribs[EGL_RENDERABLE_TYPE] = renderableType;
529         attribs[EGL_RECORDABLE_ANDROID] = EGL_TRUE;
530         attribs[EGL_SURFACE_TYPE] = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
531         attribs[EGL_FRAMEBUFFER_TARGET_ANDROID] = EGL_TRUE;
532         attribs[EGL_RED_SIZE] = 8;
533         attribs[EGL_GREEN_SIZE] = 8;
534         attribs[EGL_BLUE_SIZE] = 8;
535         attribs[EGL_ALPHA_SIZE] = 8;
536         wantedAttribute = EGL_NONE;
537         wantedAttributeValue = EGL_NONE;
538     } else {
539         // if no renderable type specified, fallback to a simplified query
540         wantedAttribute = EGL_NATIVE_VISUAL_ID;
541         wantedAttributeValue = format;
542     }
543 
544     err = selectConfigForAttribute(display, attribs, wantedAttribute, wantedAttributeValue, config);
545     if (err == NO_ERROR) {
546         EGLint caveat;
547         if (eglGetConfigAttrib(display, *config, EGL_CONFIG_CAVEAT, &caveat))
548             ALOGW_IF(caveat == EGL_SLOW_CONFIG, "EGL_SLOW_CONFIG selected!");
549     }
550 
551     return err;
552 }
553 
chooseEglConfig(EGLDisplay display,int format,bool logConfig)554 EGLConfig RenderEngine::chooseEglConfig(EGLDisplay display, int format, bool logConfig) {
555     status_t err;
556     EGLConfig config;
557 
558     // First try to get an ES2 config
559     err = selectEGLConfig(display, format, EGL_OPENGL_ES2_BIT, &config);
560     if (err != NO_ERROR) {
561         // If ES2 fails, try ES1
562         err = selectEGLConfig(display, format, EGL_OPENGL_ES_BIT, &config);
563         if (err != NO_ERROR) {
564             // still didn't work, probably because we're on the emulator...
565             // try a simplified query
566             ALOGW("no suitable EGLConfig found, trying a simpler query");
567             err = selectEGLConfig(display, format, 0, &config);
568             if (err != NO_ERROR) {
569                 // this EGL is too lame for android
570                 LOG_ALWAYS_FATAL("no suitable EGLConfig found, giving up");
571             }
572         }
573     }
574 
575     if (logConfig) {
576         // print some debugging info
577         EGLint r, g, b, a;
578         eglGetConfigAttrib(display, config, EGL_RED_SIZE, &r);
579         eglGetConfigAttrib(display, config, EGL_GREEN_SIZE, &g);
580         eglGetConfigAttrib(display, config, EGL_BLUE_SIZE, &b);
581         eglGetConfigAttrib(display, config, EGL_ALPHA_SIZE, &a);
582         ALOGI("EGL information:");
583         ALOGI("vendor    : %s", eglQueryString(display, EGL_VENDOR));
584         ALOGI("version   : %s", eglQueryString(display, EGL_VERSION));
585         ALOGI("extensions: %s", eglQueryString(display, EGL_EXTENSIONS));
586         ALOGI("Client API: %s", eglQueryString(display, EGL_CLIENT_APIS) ?: "Not Supported");
587         ALOGI("EGLSurface: %d-%d-%d-%d, config=%p", r, g, b, a, config);
588     }
589 
590     return config;
591 }
592 
primeCache() const593 void RenderEngine::primeCache() const {
594     ProgramCache::getInstance().primeCache(mFeatureFlags & WIDE_COLOR_SUPPORT);
595 }
596 
597 // ---------------------------------------------------------------------------
598 
599 } // namespace impl
600 } // namespace RE
601 } // namespace android
602