1 /*
2 * Copyright (C) 2011 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 #include "EglDisplay.h"
17
18 #include "aemu/base/containers/Lookup.h"
19 #include "aemu/base/files/StreamSerializing.h"
20 #include "EglConfig.h"
21 #include "EglGlobalInfo.h"
22 #include "EglOsApi.h"
23 #include <GLcommon/GLutils.h>
24
25 #include <algorithm>
26
EglDisplay(EGLNativeDisplayType dpy,EglOS::Display * idpy)27 EglDisplay::EglDisplay(EGLNativeDisplayType dpy,
28 EglOS::Display* idpy) :
29 m_dpy(dpy),
30 m_idpy(idpy)
31 {
32 m_manager[GLES_1_1] = new ObjectNameManager(&m_globalNameSpace);
33 m_manager[GLES_2_0] = new ObjectNameManager(&m_globalNameSpace);
34 m_manager[GLES_3_0] = m_manager[GLES_2_0];
35 m_manager[GLES_3_1] = m_manager[GLES_2_0];
36 };
37
~EglDisplay()38 EglDisplay::~EglDisplay() {
39 android::base::AutoLock mutex(m_lock);
40
41 m_configs.clear();
42
43 delete m_manager[GLES_1_1];
44 delete m_manager[GLES_2_0];
45
46 delete m_idpy;
47 }
48
initialize(int renderableType)49 void EglDisplay::initialize(int renderableType) {
50 android::base::AutoLock mutex(m_lock);
51 m_initialized = true;
52 initConfigurations(renderableType);
53 m_configInitialized = true;
54 }
55
isInitialize()56 bool EglDisplay::isInitialize() {
57 android::base::AutoLock mutex(m_lock);
58 return m_initialized;
59 }
60
terminate()61 void EglDisplay::terminate(){
62 android::base::AutoLock mutex(m_lock);
63 m_contexts.clear();
64 m_surfaces.clear();
65 m_initialized = false;
66 }
67
68 namespace CompareEglConfigs {
69
70 // Old compare function used to initialize to something decently sorted.
71 struct StaticCompare {
operator ()CompareEglConfigs::StaticCompare72 bool operator()(const std::unique_ptr<EglConfig>& first,
73 const std::unique_ptr<EglConfig>& second) const {
74 return *first < *second;
75 }
76 };
77
78 // In actual usage, we need to dynamically re-sort configs
79 // that are returned to the user.
80 struct DynamicCompare;
81 // This is because the sorting order of configs is affected
82 // based on dynamic properties.
83 //
84 // See https://www.khronos.org/registry/egl/sdk/docs/man/html/eglChooseConfig.xhtml
85 // and the section on config sorting.
86 //
87 // If the user requests an EGL config with a particular EGL_RED_SIZE,
88 // for example, we must sort configs based on that criteria, while if that
89 // was not specified, we would just skip right on to sorting by buffer size.
90 // Below is an implementation of EGL config sorting according
91 // to spec, that takes the dynamic properties into account.
ColorBufferTypeVal(EGLenum type)92 static int ColorBufferTypeVal(EGLenum type) {
93 switch (type) {
94 case EGL_RGB_BUFFER: return 0;
95 case EGL_LUMINANCE_BUFFER: return 1;
96 case EGL_YUV_BUFFER_EXT: return 2;
97 }
98 return 3;
99 }
100
nonTrivialAttribVal(EGLint val)101 static bool nonTrivialAttribVal(EGLint val) {
102 return val != 0 && val != EGL_DONT_CARE;
103 }
104
105 struct DynamicCompare {
106 public:
DynamicCompareCompareEglConfigs::DynamicCompare107 DynamicCompare(const EglConfig& wantedAttribs) {
108
109 EGLint wantedRVal = wantedAttribs.getConfAttrib(EGL_RED_SIZE);
110 EGLint wantedGVal = wantedAttribs.getConfAttrib(EGL_GREEN_SIZE);
111 EGLint wantedBVal = wantedAttribs.getConfAttrib(EGL_BLUE_SIZE);
112 EGLint wantedLVal = wantedAttribs.getConfAttrib(EGL_LUMINANCE_SIZE);
113 EGLint wantedAVal = wantedAttribs.getConfAttrib(EGL_ALPHA_SIZE);
114
115 wantedR = wantedAttribs.isWantedAttrib(EGL_RED_SIZE) && nonTrivialAttribVal(wantedRVal);
116 wantedG = wantedAttribs.isWantedAttrib(EGL_GREEN_SIZE) && nonTrivialAttribVal(wantedGVal);
117 wantedB = wantedAttribs.isWantedAttrib(EGL_BLUE_SIZE) && nonTrivialAttribVal(wantedBVal);
118 wantedL = wantedAttribs.isWantedAttrib(EGL_LUMINANCE_SIZE) && nonTrivialAttribVal(wantedLVal);
119 wantedA = wantedAttribs.isWantedAttrib(EGL_ALPHA_SIZE) && nonTrivialAttribVal(wantedAVal);
120 }
121
operator ()CompareEglConfigs::DynamicCompare122 bool operator()(EglConfig* a, EglConfig* b) const {
123 EGLint aConformant = a->getConfAttrib(EGL_CONFORMANT);
124 EGLint bConformant = b->getConfAttrib(EGL_CONFORMANT);
125
126 if (aConformant != bConformant) {
127 return aConformant != 0;
128 }
129
130 EGLint aCaveat = a->getConfAttrib(EGL_CONFIG_CAVEAT);
131 EGLint bCaveat = b->getConfAttrib(EGL_CONFIG_CAVEAT);
132 if (aCaveat != bCaveat) {
133 return aCaveat < bCaveat;
134 }
135
136 EGLint aCbType = a->getConfAttrib(EGL_COLOR_BUFFER_TYPE);
137 EGLint bCbType = b->getConfAttrib(EGL_COLOR_BUFFER_TYPE);
138 if (aCbType != bCbType) {
139 return ColorBufferTypeVal(aCbType) <
140 ColorBufferTypeVal(bCbType);
141 }
142
143 EGLint aCbSize = 0;
144 EGLint bCbSize = 0;
145
146 if (wantedR) {
147 aCbSize += a->getConfAttrib(EGL_RED_SIZE);
148 bCbSize += b->getConfAttrib(EGL_RED_SIZE);
149 }
150 if (wantedG) {
151 aCbSize += a->getConfAttrib(EGL_GREEN_SIZE);
152 bCbSize += b->getConfAttrib(EGL_GREEN_SIZE);
153 }
154 if (wantedB) {
155 aCbSize += a->getConfAttrib(EGL_BLUE_SIZE);
156 bCbSize += b->getConfAttrib(EGL_BLUE_SIZE);
157 }
158 if (wantedL) {
159 aCbSize += a->getConfAttrib(EGL_LUMINANCE_SIZE);
160 bCbSize += b->getConfAttrib(EGL_LUMINANCE_SIZE);
161 }
162 if (wantedA) {
163 aCbSize += a->getConfAttrib(EGL_ALPHA_SIZE);
164 bCbSize += b->getConfAttrib(EGL_ALPHA_SIZE);
165 }
166
167 if (aCbSize != bCbSize) {
168 return aCbSize > bCbSize;
169 }
170
171 EGLint aBufferSize = a->getConfAttrib(EGL_BUFFER_SIZE);
172 EGLint bBufferSize = b->getConfAttrib(EGL_BUFFER_SIZE);
173 if (aBufferSize != bBufferSize) {
174 return aBufferSize < bBufferSize;
175 }
176
177 EGLint aSampleBuffersNum = a->getConfAttrib(EGL_SAMPLE_BUFFERS);
178 EGLint bSampleBuffersNum = b->getConfAttrib(EGL_SAMPLE_BUFFERS);
179 if (aSampleBuffersNum != bSampleBuffersNum) {
180 return aSampleBuffersNum < bSampleBuffersNum;
181 }
182
183 EGLint aSPP = a->getConfAttrib(EGL_SAMPLES);
184 EGLint bSPP = b->getConfAttrib(EGL_SAMPLES);
185 if (aSPP != bSPP) {
186 return aSPP < bSPP;
187 }
188
189 EGLint aDepthSize = a->getConfAttrib(EGL_DEPTH_SIZE);
190 EGLint bDepthSize = b->getConfAttrib(EGL_DEPTH_SIZE);
191 if (aDepthSize != bDepthSize) {
192 return aDepthSize < bDepthSize;
193 }
194
195 EGLint aStencilSize = a->getConfAttrib(EGL_STENCIL_SIZE);
196 EGLint bStencilSize = b->getConfAttrib(EGL_STENCIL_SIZE);
197 if (aStencilSize != bStencilSize) {
198 return aStencilSize < bStencilSize;
199 }
200
201 return a->getConfAttrib(EGL_CONFIG_ID) < b->getConfAttrib(EGL_CONFIG_ID);
202 }
203
204 bool wantedR;
205 bool wantedG;
206 bool wantedB;
207 bool wantedL;
208 bool wantedA;
209 };
210
211 }
212
addSimplePixelFormat(int red_size,int green_size,int blue_size,int alpha_size,int sample_per_pixel)213 EglConfig* EglDisplay::addSimplePixelFormat(int red_size,
214 int green_size,
215 int blue_size,
216 int alpha_size,
217 int sample_per_pixel) {
218 std::sort(m_configs.begin(), m_configs.end(), CompareEglConfigs::StaticCompare());
219
220 EGLConfig match;
221
222 EglConfig dummy(red_size,
223 green_size,
224 blue_size,
225 alpha_size, // RGB_565
226 EGL_DONT_CARE,
227 16, // Depth
228 EGL_DONT_CARE,
229 EGL_DONT_CARE,
230 EGL_DONT_CARE,
231 EGL_DONT_CARE,
232 EGL_DONT_CARE,
233 EGL_DONT_CARE,
234 EGL_DONT_CARE,
235 EGL_DONT_CARE,
236 sample_per_pixel,
237 EGL_DONT_CARE,
238 EGL_DONT_CARE,
239 EGL_DONT_CARE,
240 EGL_DONT_CARE,
241 EGL_DONT_CARE,
242 EGL_DONT_CARE,
243 EGL_DONT_CARE,
244 NULL);
245
246 if(!doChooseConfigs(dummy, &match, 1))
247 {
248 return nullptr;
249 }
250
251 EglConfig* config = (EglConfig*)match;
252
253 int bSize;
254 config->getConfAttrib(EGL_BUFFER_SIZE,&bSize);
255
256 if(bSize == 16)
257 {
258 return config;
259 }
260
261 std::unique_ptr<EglConfig> newConfig(
262 new EglConfig(*config,
263 red_size, green_size, blue_size,
264 alpha_size));
265
266 if (m_uniqueConfigs.insert(*newConfig).second) {
267 config = newConfig.release();
268 m_configs.emplace_back(config);
269 }
270 return config;
271 }
272
273 // BUG: 246999412
274 // We might want to deprecate this list.
275 static const EGLint kCommonCfgs[][5] = {
276 {8, 8, 8, 0, EGL_DONT_CARE},
277 {8, 8, 8, 8, EGL_DONT_CARE},
278 // 565 fails with ANGLE on Mac
279 // {5, 6, 5, 0, EGL_DONT_CARE},
280 // The following are multi-sample configs. They have issues with CTS test:
281 // (API26) run cts -m CtsOpenGLTestCases -t
282 // android.opengl.cts.EglConfigTest#testEglConfigs
283 // We disable them until we figure out how to fix that test properly
284 // BUG: 69421199
285 // {5, 6, 5, 0, 2},
286 // {8, 8, 8, 0, 2},
287 // {8, 8, 8, 8, 2},
288 // {5, 6, 5, 0, 4},
289 // {8, 8, 8, 0, 4},
290 // {8, 8, 8, 8, 4},
291 };
292
293 static constexpr int kReservedIdNum = sizeof(kCommonCfgs) / 5 / sizeof(EGLint);
294
addReservedConfigs()295 void EglDisplay::addReservedConfigs() {
296 for (int i = 0; i < kReservedIdNum; i++) {
297 EglConfig* cfg = nullptr;
298 cfg = addSimplePixelFormat(kCommonCfgs[i][0],
299 kCommonCfgs[i][1],
300 kCommonCfgs[i][2],
301 kCommonCfgs[i][3],
302 kCommonCfgs[i][4]);
303 // ID starts with 1
304 if (cfg) {
305 cfg->setId(i + 1);
306 }
307 }
308 }
309
initConfigurations(int renderableType)310 void EglDisplay::initConfigurations(int renderableType) {
311 if (m_configInitialized) {
312 return;
313 }
314 m_idpy->queryConfigs(renderableType, addConfig, this);
315
316 for (size_t i = 0; i < m_configs.size(); i++) {
317 // ID starts with 1
318 m_configs[i]->setId(static_cast<EGLint>(i + 1 + kReservedIdNum));
319 }
320 addReservedConfigs();
321 // It is ok if config id is not continual.
322 std::sort(m_configs.begin(), m_configs.end(), CompareEglConfigs::StaticCompare());
323
324 #if EMUGL_DEBUG
325 for (ConfigsList::const_iterator it = m_configs.begin();
326 it != m_configs.end();
327 ++it) {
328 EglConfig* config = it->get();
329 EGLint red, green, blue, alpha, depth, stencil, renderable, surface;
330 config->getConfAttrib(EGL_RED_SIZE, &red);
331 config->getConfAttrib(EGL_GREEN_SIZE, &green);
332 config->getConfAttrib(EGL_BLUE_SIZE, &blue);
333 config->getConfAttrib(EGL_ALPHA_SIZE, &alpha);
334 config->getConfAttrib(EGL_DEPTH_SIZE, &depth);
335 config->getConfAttrib(EGL_STENCIL_SIZE, &stencil);
336 config->getConfAttrib(EGL_RENDERABLE_TYPE, &renderable);
337 config->getConfAttrib(EGL_SURFACE_TYPE, &surface);
338 }
339 #endif // EMUGL_DEBUG
340 }
341
getConfig(EGLConfig conf) const342 EglConfig* EglDisplay::getConfig(EGLConfig conf) const {
343 android::base::AutoLock mutex(m_lock);
344
345 for(ConfigsList::const_iterator it = m_configs.begin();
346 it != m_configs.end();
347 ++it) {
348 if(static_cast<EGLConfig>(it->get()) == conf) {
349 return it->get();
350 }
351 }
352 return NULL;
353 }
354
getSurface(EGLSurface surface) const355 SurfacePtr EglDisplay::getSurface(EGLSurface surface) const {
356 android::base::AutoLock mutex(m_lock);
357 /* surface is "key" in map<unsigned int, SurfacePtr>. */
358 unsigned int hndl = SafeUIntFromPointer(surface);
359 SurfacesHndlMap::const_iterator it = m_surfaces.find(hndl);
360 return it != m_surfaces.end() ?
361 (*it).second :
362 SurfacePtr();
363 }
364
getContext(EGLContext ctx) const365 ContextPtr EglDisplay::getContext(EGLContext ctx) const {
366 android::base::AutoLock mutex(m_lock);
367 /* ctx is "key" in map<unsigned int, ContextPtr>. */
368 unsigned int hndl = SafeUIntFromPointer(ctx);
369 ContextsHndlMap::const_iterator it = m_contexts.find(hndl);
370 return it != m_contexts.end() ?
371 (*it).second :
372 ContextPtr();
373 }
374
getLowLevelContext(EGLContext ctx) const375 void* EglDisplay::getLowLevelContext(EGLContext ctx) const {
376 auto lctx = getContext(ctx);
377 if (lctx) {
378 return lctx->nativeType()->lowLevelContext();
379 }
380 return nullptr;
381 }
382
removeSurface(EGLSurface s)383 bool EglDisplay::removeSurface(EGLSurface s) {
384 android::base::AutoLock mutex(m_lock);
385 /* s is "key" in map<unsigned int, SurfacePtr>. */
386 unsigned int hndl = SafeUIntFromPointer(s);
387 SurfacesHndlMap::iterator it = m_surfaces.find(hndl);
388 if(it != m_surfaces.end()) {
389 m_surfaces.erase(it);
390 return true;
391 }
392 return false;
393 }
394
removeContext(EGLContext ctx)395 bool EglDisplay::removeContext(EGLContext ctx) {
396 android::base::AutoLock mutex(m_lock);
397 /* ctx is "key" in map<unsigned int, ContextPtr>. */
398 unsigned int hndl = SafeUIntFromPointer(ctx);
399 ContextsHndlMap::iterator it = m_contexts.find(hndl);
400 if(it != m_contexts.end()) {
401 m_contexts.erase(it);
402 return true;
403 }
404 return false;
405 }
406
removeContext(ContextPtr ctx)407 bool EglDisplay::removeContext(ContextPtr ctx) {
408 android::base::AutoLock mutex(m_lock);
409
410 ContextsHndlMap::iterator it;
411 for(it = m_contexts.begin(); it != m_contexts.end();++it) {
412 if((*it).second.get() == ctx.get()){
413 break;
414 }
415 }
416 if(it != m_contexts.end()) {
417 m_contexts.erase(it);
418 return true;
419 }
420 return false;
421 }
422
getConfig(EGLint id) const423 EglConfig* EglDisplay::getConfig(EGLint id) const {
424 android::base::AutoLock mutex(m_lock);
425
426 for(ConfigsList::const_iterator it = m_configs.begin();
427 it != m_configs.end();
428 ++it) {
429 if((*it)->id() == id) {
430 return it->get();
431 }
432 }
433 return NULL;
434 }
435
getDefaultConfig() const436 EglConfig* EglDisplay::getDefaultConfig() const {
437 return getConfig(2); // rgba8888
438 }
439
getConfigs(EGLConfig * configs,int config_size) const440 int EglDisplay::getConfigs(EGLConfig* configs,int config_size) const {
441 android::base::AutoLock mutex(m_lock);
442 int i = 0;
443 for(ConfigsList::const_iterator it = m_configs.begin();
444 it != m_configs.end() && i < config_size;
445 i++, ++it) {
446 configs[i] = static_cast<EGLConfig>(it->get());
447 }
448 return i;
449 }
450
chooseConfigs(const EglConfig & dummy,EGLConfig * configs,int config_size) const451 int EglDisplay::chooseConfigs(const EglConfig& dummy,
452 EGLConfig* configs,
453 int config_size) const {
454 android::base::AutoLock mutex(m_lock);
455 return doChooseConfigs(dummy, configs, config_size);
456 }
457
doChooseConfigs(const EglConfig & dummy,EGLConfig * configs,int config_size) const458 int EglDisplay::doChooseConfigs(const EglConfig& dummy,
459 EGLConfig* configs,
460 int config_size) const {
461 int added = 0;
462
463 std::vector<EglConfig*> validConfigs;
464
465 CHOOSE_CONFIG_DLOG("returning configs. ids: {");
466 for(ConfigsList::const_iterator it = m_configs.begin();
467 it != m_configs.end() && (added < config_size || !configs);
468 ++it) {
469 if( (*it)->chosen(dummy)){
470 if(configs) {
471 CHOOSE_CONFIG_DLOG("valid config: id=0x%x", it->get()->id());
472 validConfigs.push_back(it->get());
473 }
474 added++;
475 }
476 }
477
478 CHOOSE_CONFIG_DLOG("sorting valid configs...");
479
480 std::sort(validConfigs.begin(),
481 validConfigs.end(),
482 CompareEglConfigs::DynamicCompare(dummy));
483
484 for (int i = 0; configs && i < added; i++) {
485 configs[i] = static_cast<EGLConfig>(validConfigs[i]);
486 }
487
488 CHOOSE_CONFIG_DLOG("returning configs. ids end }");
489 return added;
490 }
491
addSurface(SurfacePtr s)492 EGLSurface EglDisplay::addSurface(SurfacePtr s ) {
493 android::base::AutoLock mutex(m_lock);
494 unsigned int hndl = s.get()->getHndl();
495 EGLSurface ret =reinterpret_cast<EGLSurface> (hndl);
496
497 if(m_surfaces.find(hndl) != m_surfaces.end()) {
498 return ret;
499 }
500
501 m_surfaces[hndl] = s;
502 return ret;
503 }
504
addContext(ContextPtr ctx)505 EGLContext EglDisplay::addContext(ContextPtr ctx ) {
506 android::base::AutoLock mutex(m_lock);
507
508 unsigned int hndl = ctx.get()->getHndl();
509 EGLContext ret = reinterpret_cast<EGLContext> (hndl);
510
511 if(m_contexts.find(hndl) != m_contexts.end()) {
512 return ret;
513 }
514 m_contexts[hndl] = ctx;
515 return ret;
516 }
517
518
addImageKHR(ImagePtr img)519 EGLImageKHR EglDisplay::addImageKHR(ImagePtr img) {
520 android::base::AutoLock mutex(m_lock);
521 do {
522 ++m_nextEglImageId;
523 } while(m_nextEglImageId == 0
524 || android::base::contains(m_eglImages, m_nextEglImageId));
525 img->imageId = m_nextEglImageId;
526 m_eglImages[m_nextEglImageId] = img;
527 return reinterpret_cast<EGLImageKHR>(m_nextEglImageId);
528 }
529
touchEglImage(EglImage * eglImage,SaveableTexture::restorer_t restorer)530 static void touchEglImage(EglImage* eglImage,
531 SaveableTexture::restorer_t restorer) {
532 if (eglImage->needRestore) {
533 if (eglImage->saveableTexture.get()) {
534 restorer(eglImage->saveableTexture.get());
535 eglImage->saveableTexture->fillEglImage(eglImage);
536 }
537 eglImage->needRestore = false;
538 }
539 }
540
getImage(EGLImageKHR img,SaveableTexture::restorer_t restorer) const541 ImagePtr EglDisplay::getImage(EGLImageKHR img,
542 SaveableTexture::restorer_t restorer) const {
543 android::base::AutoLock mutex(m_lock);
544 /* img is "key" in map<unsigned int, ImagePtr>. */
545 unsigned int hndl = SafeUIntFromPointer(img);
546 ImagesHndlMap::const_iterator i( m_eglImages.find(hndl) );
547 if (i == m_eglImages.end()) {
548 return ImagePtr();
549 }
550 touchEglImage(i->second.get(), restorer);
551 return i->second;
552 }
553
destroyImageKHR(EGLImageKHR img)554 bool EglDisplay:: destroyImageKHR(EGLImageKHR img) {
555 android::base::AutoLock mutex(m_lock);
556 /* img is "key" in map<unsigned int, ImagePtr>. */
557 unsigned int hndl = SafeUIntFromPointer(img);
558 ImagesHndlMap::iterator i( m_eglImages.find(hndl) );
559 if (i != m_eglImages.end())
560 {
561 m_eglImages.erase(i);
562 return true;
563 }
564 return false;
565 }
566
getGlobalSharedContext() const567 EglOS::Context* EglDisplay::getGlobalSharedContext() const {
568 android::base::AutoLock mutex(m_lock);
569 #ifndef _WIN32
570 // find an existing OpenGL context to share with, if exist
571 EglOS::Context* ret =
572 (EglOS::Context*)m_manager[GLES_1_1]->getGlobalContext();
573 if (!ret)
574 ret = (EglOS::Context*)m_manager[GLES_2_0]->getGlobalContext();
575 return ret;
576 #else
577 if (!m_globalSharedContext) {
578 //
579 // On windows we create a dummy context to serve as the
580 // "global context" which all contexts share with.
581 // This is because on windows it is not possible to share
582 // with a context which is already current. This dummy context
583 // will never be current to any thread so it is safe to share with.
584 // Create that context using the first config
585 if (m_configs.empty()) {
586 // Should not happen! config list should be initialized at this point
587 return NULL;
588 }
589 EglConfig *cfg = m_configs.front().get();
590 m_globalSharedContext = m_idpy->createContext(
591 isCoreProfile() ? EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR : 0,
592 cfg->nativeFormat(), NULL);
593 }
594
595 return m_globalSharedContext.get();
596 #endif
597 }
598
599 // static
addConfig(void * opaque,const EglOS::ConfigInfo * info)600 void EglDisplay::addConfig(void* opaque, const EglOS::ConfigInfo* info) {
601 EglDisplay* display = static_cast<EglDisplay*>(opaque);
602
603 // Greater than 24 bits of color,
604 // or having no depth/stencil causes some
605 // unexpected behavior in real usage, such
606 // as frame corruption and wrong drawing order.
607 // Just don't use those configs.
608 if (info->red_size > 8 ||
609 info->green_size > 8 ||
610 info->blue_size > 8 ||
611 info->depth_size < 24 ||
612 info->stencil_size < 8 ||
613 info->samples_per_pixel > 2) {
614 return;
615 }
616
617 std::unique_ptr<EglConfig> config(new EglConfig(
618 info->red_size,
619 info->green_size,
620 info->blue_size,
621 info->alpha_size,
622 info->caveat,
623 info->depth_size,
624 info->frame_buffer_level,
625 info->max_pbuffer_width,
626 info->max_pbuffer_height,
627 info->max_pbuffer_size,
628 info->native_renderable,
629 info->renderable_type,
630 info->native_visual_id,
631 info->native_visual_type,
632 info->samples_per_pixel,
633 info->stencil_size,
634 info->surface_type,
635 info->transparent_type,
636 info->trans_red_val,
637 info->trans_green_val,
638 info->trans_blue_val,
639 info->recordable_android,
640 info->frmt));
641
642 if (display->m_uniqueConfigs.insert(*config).second) {
643 display->m_configs.emplace_back(config.release());
644 }
645 }
646
onSaveAllImages(android::base::Stream * stream,const android::snapshot::ITextureSaverPtr & textureSaver,SaveableTexture::saver_t saver,SaveableTexture::restorer_t restorer)647 void EglDisplay::onSaveAllImages(android::base::Stream* stream,
648 const android::snapshot::ITextureSaverPtr& textureSaver,
649 SaveableTexture::saver_t saver,
650 SaveableTexture::restorer_t restorer) {
651 // we could consider calling presave for all ShareGroups from here
652 // but it would introduce overheads because not all share groups need to be
653 // saved
654 android::base::AutoLock mutex(m_lock);
655 for (auto& image : m_eglImages) {
656 // In case we loaded textures from a previous snapshot and have not
657 // yet restore them to GPU, we do the restoration here.
658 // TODO: skip restoration and write saveableTexture directly to the
659 // new snapshot for better performance
660 touchEglImage(image.second.get(), restorer);
661 getGlobalNameSpace()->preSaveAddEglImage(image.second.get());
662 }
663 m_globalNameSpace.onSave(stream, textureSaver, saver);
664 saveCollection(stream, m_eglImages, [](
665 android::base::Stream* stream,
666 const ImagesHndlMap::value_type& img) {
667 stream->putBe32(img.first);
668 stream->putBe32(img.second->globalTexObj->getGlobalName());
669 // We do not need to save other fields in EglImage. We can load them
670 // from SaveableTexture.
671 });
672 }
673
onLoadAllImages(android::base::Stream * stream,const android::snapshot::ITextureLoaderPtr & textureLoader,SaveableTexture::creator_t creator)674 void EglDisplay::onLoadAllImages(android::base::Stream* stream,
675 const android::snapshot::ITextureLoaderPtr& textureLoader,
676 SaveableTexture::creator_t creator) {
677 if (!m_eglImages.empty()) {
678 // Could be triggered by this bug:
679 // b/36654917
680 fprintf(stderr, "Warning: unreleased EGL image handles\n");
681 }
682 m_eglImages.clear();
683 android::base::AutoLock mutex(m_lock);
684 m_globalNameSpace.setIfaces(
685 EglGlobalInfo::getInstance()->getEglIface(),
686 EglGlobalInfo::getInstance()->getIface(GLES_2_0));
687 m_globalNameSpace.onLoad(stream, textureLoader, creator);
688
689 loadCollection(stream, &m_eglImages, [this](
690 android::base::Stream* stream) {
691 unsigned int hndl = stream->getBe32();
692 unsigned int globalName = stream->getBe32();
693 ImagePtr eglImg(new EglImage);
694 eglImg->imageId = hndl;
695 eglImg->saveableTexture =
696 m_globalNameSpace.getSaveableTextureFromLoad(globalName);
697 eglImg->needRestore = true;
698 return std::make_pair(hndl, std::move(eglImg));
699 });
700 }
701
postLoadAllImages(android::base::Stream * stream)702 void EglDisplay::postLoadAllImages(android::base::Stream* stream) {
703 m_globalNameSpace.postLoad(stream);
704 }
705
nativeTextureDecompressionEnabled() const706 bool EglDisplay::nativeTextureDecompressionEnabled() const {
707 return m_nativeTextureDecompressionEnabled;
708 }
709
setNativeTextureDecompressionEnabled(bool enabled)710 void EglDisplay::setNativeTextureDecompressionEnabled(bool enabled) {
711 m_nativeTextureDecompressionEnabled = enabled;
712 }