• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/Lookup.h"
19 #include "base/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 static const EGLint kCommonCfgs[][5] = {
274     {5, 6, 5, 0, EGL_DONT_CARE},
275     {8, 8, 8, 0, EGL_DONT_CARE},
276     {8, 8, 8, 8, EGL_DONT_CARE},
277     // The following are multi-sample configs. They have issues with CTS test:
278     // (API26) run cts -m CtsOpenGLTestCases -t
279     // android.opengl.cts.EglConfigTest#testEglConfigs
280     // We disable them until we figure out how to fix that test properly
281     // BUG: 69421199
282     // {5, 6, 5, 0, 2},
283     // {8, 8, 8, 0, 2},
284     // {8, 8, 8, 8, 2},
285     // {5, 6, 5, 0, 4},
286     // {8, 8, 8, 0, 4},
287     // {8, 8, 8, 8, 4},
288 };
289 
290 static constexpr int kReservedIdNum = sizeof(kCommonCfgs) / 5 / sizeof(EGLint);
291 
addReservedConfigs()292 void EglDisplay::addReservedConfigs() {
293     for (int i = 0; i < kReservedIdNum; i++) {
294         EglConfig* cfg = nullptr;
295         cfg = addSimplePixelFormat(kCommonCfgs[i][0],
296                 kCommonCfgs[i][1],
297                 kCommonCfgs[i][2],
298                 kCommonCfgs[i][3],
299                 kCommonCfgs[i][4]);
300         if (!cfg) { // if multi-sample fails, fall back to the basic ones
301             int fallbackCfg = 2;
302             do {
303                 cfg = addSimplePixelFormat(kCommonCfgs[fallbackCfg][0],
304                         kCommonCfgs[fallbackCfg][1],
305                         kCommonCfgs[fallbackCfg][2],
306                         kCommonCfgs[fallbackCfg][3],
307                         kCommonCfgs[fallbackCfg][4]);
308                 fallbackCfg --;
309             } while (!cfg && fallbackCfg >= 0);
310             if (cfg) {
311                 // Clone the basic cfg, and give it a different ID later
312                 cfg = new EglConfig(*cfg);
313                 m_configs.emplace_back(cfg);
314             }
315         }
316         // ID starts with 1
317         if (cfg) {
318             cfg->setId(i + 1);
319         }
320     }
321 }
322 
initConfigurations(int renderableType)323 void EglDisplay::initConfigurations(int renderableType) {
324     if (m_configInitialized) {
325         return;
326     }
327     m_idpy->queryConfigs(renderableType, addConfig, this);
328 
329     for (size_t i = 0; i < m_configs.size(); i++) {
330         // ID starts with 1
331         m_configs[i]->setId(static_cast<EGLint>(i + 1 + kReservedIdNum));
332     }
333     addReservedConfigs();
334     // It is ok if config id is not continual.
335     std::sort(m_configs.begin(), m_configs.end(), CompareEglConfigs::StaticCompare());
336 
337 #if EMUGL_DEBUG
338     for (ConfigsList::const_iterator it = m_configs.begin();
339          it != m_configs.end();
340          ++it) {
341         EglConfig* config = it->get();
342         EGLint red, green, blue, alpha, depth, stencil, renderable, surface;
343         config->getConfAttrib(EGL_RED_SIZE, &red);
344         config->getConfAttrib(EGL_GREEN_SIZE, &green);
345         config->getConfAttrib(EGL_BLUE_SIZE, &blue);
346         config->getConfAttrib(EGL_ALPHA_SIZE, &alpha);
347         config->getConfAttrib(EGL_DEPTH_SIZE, &depth);
348         config->getConfAttrib(EGL_STENCIL_SIZE, &stencil);
349         config->getConfAttrib(EGL_RENDERABLE_TYPE, &renderable);
350         config->getConfAttrib(EGL_SURFACE_TYPE, &surface);
351     }
352 #endif  // EMUGL_DEBUG
353 }
354 
getConfig(EGLConfig conf) const355 EglConfig* EglDisplay::getConfig(EGLConfig conf) const {
356     android::base::AutoLock mutex(m_lock);
357 
358     for(ConfigsList::const_iterator it = m_configs.begin();
359         it != m_configs.end();
360         ++it) {
361         if(static_cast<EGLConfig>(it->get()) == conf) {
362             return it->get();
363         }
364     }
365     return NULL;
366 }
367 
getSurface(EGLSurface surface) const368 SurfacePtr EglDisplay::getSurface(EGLSurface surface) const {
369     android::base::AutoLock mutex(m_lock);
370     /* surface is "key" in map<unsigned int, SurfacePtr>. */
371     unsigned int hndl = SafeUIntFromPointer(surface);
372     SurfacesHndlMap::const_iterator it = m_surfaces.find(hndl);
373     return it != m_surfaces.end() ?
374                                   (*it).second :
375                                    SurfacePtr();
376 }
377 
getContext(EGLContext ctx) const378 ContextPtr EglDisplay::getContext(EGLContext ctx) const {
379     android::base::AutoLock mutex(m_lock);
380     /* ctx is "key" in map<unsigned int, ContextPtr>. */
381     unsigned int hndl = SafeUIntFromPointer(ctx);
382     ContextsHndlMap::const_iterator it = m_contexts.find(hndl);
383     return it != m_contexts.end() ?
384                                   (*it).second :
385                                    ContextPtr();
386 }
387 
removeSurface(EGLSurface s)388 bool EglDisplay::removeSurface(EGLSurface s) {
389     android::base::AutoLock mutex(m_lock);
390     /* s is "key" in map<unsigned int, SurfacePtr>. */
391     unsigned int hndl = SafeUIntFromPointer(s);
392     SurfacesHndlMap::iterator it = m_surfaces.find(hndl);
393     if(it != m_surfaces.end()) {
394         m_surfaces.erase(it);
395         return true;
396     }
397     return false;
398 }
399 
removeContext(EGLContext ctx)400 bool EglDisplay::removeContext(EGLContext ctx) {
401     android::base::AutoLock mutex(m_lock);
402     /* ctx is "key" in map<unsigned int, ContextPtr>. */
403     unsigned int hndl = SafeUIntFromPointer(ctx);
404     ContextsHndlMap::iterator it = m_contexts.find(hndl);
405     if(it != m_contexts.end()) {
406         m_contexts.erase(it);
407         return true;
408     }
409     return false;
410 }
411 
removeContext(ContextPtr ctx)412 bool EglDisplay::removeContext(ContextPtr ctx) {
413     android::base::AutoLock mutex(m_lock);
414 
415     ContextsHndlMap::iterator it;
416     for(it = m_contexts.begin(); it != m_contexts.end();++it) {
417         if((*it).second.get() == ctx.get()){
418             break;
419         }
420     }
421     if(it != m_contexts.end()) {
422         m_contexts.erase(it);
423         return true;
424     }
425     return false;
426 }
427 
getConfig(EGLint id) const428 EglConfig* EglDisplay::getConfig(EGLint id) const {
429     android::base::AutoLock mutex(m_lock);
430 
431     for(ConfigsList::const_iterator it = m_configs.begin();
432         it != m_configs.end();
433         ++it) {
434         if((*it)->id() == id) {
435             return it->get();
436         }
437     }
438     return NULL;
439 }
440 
getDefaultConfig() const441 EglConfig* EglDisplay::getDefaultConfig() const {
442     return getConfig(2); // rgba8888
443 }
444 
getConfigs(EGLConfig * configs,int config_size) const445 int EglDisplay::getConfigs(EGLConfig* configs,int config_size) const {
446     android::base::AutoLock mutex(m_lock);
447     int i = 0;
448     for(ConfigsList::const_iterator it = m_configs.begin();
449         it != m_configs.end() && i < config_size;
450         i++, ++it) {
451         configs[i] = static_cast<EGLConfig>(it->get());
452     }
453     return i;
454 }
455 
chooseConfigs(const EglConfig & dummy,EGLConfig * configs,int config_size) const456 int EglDisplay::chooseConfigs(const EglConfig& dummy,
457                               EGLConfig* configs,
458                               int config_size) const {
459     android::base::AutoLock mutex(m_lock);
460     return doChooseConfigs(dummy, configs, config_size);
461 }
462 
doChooseConfigs(const EglConfig & dummy,EGLConfig * configs,int config_size) const463 int EglDisplay::doChooseConfigs(const EglConfig& dummy,
464                                 EGLConfig* configs,
465                                 int config_size) const {
466     int added = 0;
467 
468     std::vector<EglConfig*> validConfigs;
469 
470     CHOOSE_CONFIG_DLOG("returning configs. ids: {");
471     for(ConfigsList::const_iterator it = m_configs.begin();
472         it != m_configs.end() && (added < config_size || !configs);
473         ++it) {
474         if( (*it)->chosen(dummy)){
475             if(configs) {
476                 CHOOSE_CONFIG_DLOG("valid config: id=0x%x", it->get()->id());
477                 validConfigs.push_back(it->get());
478             }
479             added++;
480        }
481     }
482 
483     CHOOSE_CONFIG_DLOG("sorting valid configs...");
484 
485     std::sort(validConfigs.begin(),
486               validConfigs.end(),
487               CompareEglConfigs::DynamicCompare(dummy));
488 
489     for (int i = 0; configs && i < added; i++) {
490         configs[i] = static_cast<EGLConfig>(validConfigs[i]);
491     }
492 
493     CHOOSE_CONFIG_DLOG("returning configs. ids end }");
494     return added;
495 }
496 
addSurface(SurfacePtr s)497 EGLSurface EglDisplay::addSurface(SurfacePtr s ) {
498     android::base::AutoLock mutex(m_lock);
499    unsigned int hndl = s.get()->getHndl();
500    EGLSurface ret =reinterpret_cast<EGLSurface> (hndl);
501 
502    if(m_surfaces.find(hndl) != m_surfaces.end()) {
503        return ret;
504    }
505 
506    m_surfaces[hndl] = s;
507    return ret;
508 }
509 
addContext(ContextPtr ctx)510 EGLContext EglDisplay::addContext(ContextPtr ctx ) {
511     android::base::AutoLock mutex(m_lock);
512 
513    unsigned int hndl = ctx.get()->getHndl();
514    EGLContext ret    = reinterpret_cast<EGLContext> (hndl);
515 
516    if(m_contexts.find(hndl) != m_contexts.end()) {
517        return ret;
518    }
519    m_contexts[hndl] = ctx;
520    return ret;
521 }
522 
523 
addImageKHR(ImagePtr img)524 EGLImageKHR EglDisplay::addImageKHR(ImagePtr img) {
525     android::base::AutoLock mutex(m_lock);
526     do {
527         ++m_nextEglImageId;
528     } while(m_nextEglImageId == 0
529             || android::base::contains(m_eglImages, m_nextEglImageId));
530     img->imageId = m_nextEglImageId;
531     m_eglImages[m_nextEglImageId] = img;
532     return reinterpret_cast<EGLImageKHR>(m_nextEglImageId);
533 }
534 
touchEglImage(EglImage * eglImage,SaveableTexture::restorer_t restorer)535 static void touchEglImage(EglImage* eglImage,
536         SaveableTexture::restorer_t restorer) {
537     if (eglImage->needRestore) {
538         if (eglImage->saveableTexture.get()) {
539             restorer(eglImage->saveableTexture.get());
540             eglImage->saveableTexture->fillEglImage(eglImage);
541         }
542         eglImage->needRestore = false;
543     }
544 }
545 
getImage(EGLImageKHR img,SaveableTexture::restorer_t restorer) const546 ImagePtr EglDisplay::getImage(EGLImageKHR img,
547         SaveableTexture::restorer_t restorer) const {
548     android::base::AutoLock mutex(m_lock);
549     /* img is "key" in map<unsigned int, ImagePtr>. */
550     unsigned int hndl = SafeUIntFromPointer(img);
551     ImagesHndlMap::const_iterator i( m_eglImages.find(hndl) );
552     if (i == m_eglImages.end()) {
553         return ImagePtr();
554     }
555     touchEglImage(i->second.get(), restorer);
556     return i->second;
557 }
558 
destroyImageKHR(EGLImageKHR img)559 bool EglDisplay:: destroyImageKHR(EGLImageKHR img) {
560     android::base::AutoLock mutex(m_lock);
561     /* img is "key" in map<unsigned int, ImagePtr>. */
562     unsigned int hndl = SafeUIntFromPointer(img);
563     ImagesHndlMap::iterator i( m_eglImages.find(hndl) );
564     if (i != m_eglImages.end())
565     {
566         m_eglImages.erase(i);
567         return true;
568     }
569     return false;
570 }
571 
getGlobalSharedContext() const572 EglOS::Context* EglDisplay::getGlobalSharedContext() const {
573     android::base::AutoLock mutex(m_lock);
574 #ifndef _WIN32
575     // find an existing OpenGL context to share with, if exist
576     EglOS::Context* ret =
577         (EglOS::Context*)m_manager[GLES_1_1]->getGlobalContext();
578     if (!ret)
579         ret = (EglOS::Context*)m_manager[GLES_2_0]->getGlobalContext();
580     return ret;
581 #else
582     if (!m_globalSharedContext) {
583         //
584         // On windows we create a dummy context to serve as the
585         // "global context" which all contexts share with.
586         // This is because on windows it is not possible to share
587         // with a context which is already current. This dummy context
588         // will never be current to any thread so it is safe to share with.
589         // Create that context using the first config
590         if (m_configs.empty()) {
591             // Should not happen! config list should be initialized at this point
592             return NULL;
593         }
594         EglConfig *cfg = m_configs.front().get();
595         m_globalSharedContext = m_idpy->createContext(
596                 isCoreProfile() ? EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR : 0,
597                 cfg->nativeFormat(), NULL);
598     }
599 
600     return m_globalSharedContext.get();
601 #endif
602 }
603 
604 // static
addConfig(void * opaque,const EglOS::ConfigInfo * info)605 void EglDisplay::addConfig(void* opaque, const EglOS::ConfigInfo* info) {
606     EglDisplay* display = static_cast<EglDisplay*>(opaque);
607 
608     // Greater than 24 bits of color,
609     // or having no depth/stencil causes some
610     // unexpected behavior in real usage, such
611     // as frame corruption and wrong drawing order.
612     // Also, disallow high MSAA.
613     // Just don't use those configs.
614     if (info->red_size > 8 ||
615         info->green_size > 8 ||
616         info->blue_size > 8 ||
617         info->depth_size < 24 ||
618         info->stencil_size < 8 ||
619         info->samples_per_pixel > 0) {
620         return;
621     }
622 
623     std::unique_ptr<EglConfig> config(new EglConfig(
624          info->red_size,
625          info->green_size,
626          info->blue_size,
627          info->alpha_size,
628          info->caveat,
629          info->depth_size,
630          info->frame_buffer_level,
631          info->max_pbuffer_width,
632          info->max_pbuffer_height,
633          info->max_pbuffer_size,
634          info->native_renderable,
635          info->renderable_type,
636          info->native_visual_id,
637          info->native_visual_type,
638          info->samples_per_pixel,
639          info->stencil_size,
640          info->surface_type,
641          info->transparent_type,
642          info->trans_red_val,
643          info->trans_green_val,
644          info->trans_blue_val,
645          info->recordable_android,
646          info->frmt));
647 
648     if (display->m_uniqueConfigs.insert(*config).second) {
649         display->m_configs.emplace_back(config.release());
650     }
651 }
652 
onSaveAllImages(android::base::Stream * stream,const android::snapshot::ITextureSaverPtr & textureSaver,SaveableTexture::saver_t saver,SaveableTexture::restorer_t restorer)653 void EglDisplay::onSaveAllImages(android::base::Stream* stream,
654                                  const android::snapshot::ITextureSaverPtr& textureSaver,
655                                  SaveableTexture::saver_t saver,
656                                  SaveableTexture::restorer_t restorer) {
657     // we could consider calling presave for all ShareGroups from here
658     // but it would introduce overheads because not all share groups need to be
659     // saved
660     android::base::AutoLock mutex(m_lock);
661     for (auto& image : m_eglImages) {
662         // In case we loaded textures from a previous snapshot and have not
663         // yet restore them to GPU, we do the restoration here.
664         // TODO: skip restoration and write saveableTexture directly to the
665         // new snapshot for better performance
666         touchEglImage(image.second.get(), restorer);
667         getGlobalNameSpace()->preSaveAddEglImage(image.second.get());
668     }
669     m_globalNameSpace.onSave(stream, textureSaver, saver);
670     saveCollection(stream, m_eglImages, [](
671             android::base::Stream* stream,
672             const ImagesHndlMap::value_type& img) {
673         stream->putBe32(img.first);
674         stream->putBe32(img.second->globalTexObj->getGlobalName());
675         // We do not need to save other fields in EglImage. We can load them
676         // from SaveableTexture.
677     });
678 }
679 
onLoadAllImages(android::base::Stream * stream,const android::snapshot::ITextureLoaderPtr & textureLoader,SaveableTexture::creator_t creator)680 void EglDisplay::onLoadAllImages(android::base::Stream* stream,
681                                  const android::snapshot::ITextureLoaderPtr& textureLoader,
682                                  SaveableTexture::creator_t creator) {
683     if (!m_eglImages.empty()) {
684         // Could be triggered by this bug:
685         // b/36654917
686         fprintf(stderr, "Warning: unreleased EGL image handles\n");
687     }
688     m_eglImages.clear();
689     android::base::AutoLock mutex(m_lock);
690     m_globalNameSpace.setIfaces(
691         EglGlobalInfo::getInstance()->getEglIface(),
692         EglGlobalInfo::getInstance()->getIface(GLES_2_0));
693     m_globalNameSpace.onLoad(stream, textureLoader, creator);
694 
695     loadCollection(stream, &m_eglImages, [this](
696         android::base::Stream* stream) {
697         unsigned int hndl = stream->getBe32();
698         unsigned int globalName = stream->getBe32();
699         ImagePtr eglImg(new EglImage);
700         eglImg->imageId = hndl;
701         eglImg->saveableTexture =
702                 m_globalNameSpace.getSaveableTextureFromLoad(globalName);
703         eglImg->needRestore = true;
704         return std::make_pair(hndl, std::move(eglImg));
705     });
706 }
707 
postLoadAllImages(android::base::Stream * stream)708 void EglDisplay::postLoadAllImages(android::base::Stream* stream) {
709     m_globalNameSpace.postLoad(stream);
710 }
711