1 /*
2  ** Copyright 2007, 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 #ifndef ANDROID_EGL_DISPLAY_H
18 #define ANDROID_EGL_DISPLAY_H
19 
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22 #include <stddef.h>
23 #include <stdint.h>
24 
25 #include <condition_variable>
26 #include <map>
27 #include <memory>
28 #include <mutex>
29 #include <string>
30 #include <unordered_set>
31 
32 #include "../hooks.h"
33 #include "egldefs.h"
34 
35 namespace android {
36 
37 class egl_object_t;
38 class egl_context_t;
39 struct egl_connection_t;
40 
41 bool findExtension(const char* exts, const char* name, size_t nameLen = 0);
42 bool needsAndroidPEglMitigation();
43 
44 class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
45     static std::map<EGLDisplay, std::unique_ptr<egl_display_t>> displayMap;
46     static std::mutex displayMapLock;
47     EGLDisplay getDisplay(EGLNativeDisplayType display);
48     static EGLDisplay getPlatformDisplay(EGLNativeDisplayType display,
49                                          const EGLAttrib* attrib_list);
50     void loseCurrentImpl(egl_context_t* cur_c);
51 
52 public:
53     enum {
54         NOT_INITIALIZED = 0,
55         INITIALIZED = 1,
56         TERMINATED = 2,
57     };
58 
59     egl_display_t();
60     ~egl_display_t();
61 
62     EGLBoolean initialize(EGLint* major, EGLint* minor);
63     EGLBoolean terminate();
64 
65     // add object to this display's list
66     void addObject(egl_object_t* object);
67     // remove object from this display's list
68     void removeObject(egl_object_t* object);
69     // add reference to this object. returns true if this is a valid object.
70     bool getObject(egl_object_t* object) const;
71 
72     static egl_display_t* get(EGLDisplay dpy);
73     static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp, const EGLAttrib* attrib_list);
74 
75     EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c, EGLSurface draw, EGLSurface read,
76                            EGLContext ctx, EGLSurface impl_draw, EGLSurface impl_read,
77                            EGLContext impl_ctx);
78     static void loseCurrent(egl_context_t* cur_c);
79 
isReady()80     inline bool isReady() const { return (refs > 0); }
isValid()81     inline bool isValid() const { return magic == '_dpy'; }
isAlive()82     inline bool isAlive() const { return isValid(); }
83 
getVendorString()84     char const* getVendorString() const { return mVendorString.c_str(); }
getVersionString()85     char const* getVersionString() const { return mVersionString.c_str(); }
getClientApiString()86     char const* getClientApiString() const { return mClientApiString.c_str(); }
getExtensionString()87     char const* getExtensionString() const { return mExtensionString.c_str(); }
88 
89     bool haveExtension(const char* name, size_t nameLen = 0) const;
90 
getRefsCount()91     inline uint32_t getRefsCount() const { return refs; }
92 
93     struct strings_t {
94         char const* vendor;
95         char const* version;
96         char const* clientApi;
97         char const* extensions;
98     };
99 
100     struct DisplayImpl {
DisplayImplDisplayImpl101         DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) {}
102         EGLDisplay dpy;
103         EGLint state;
104         strings_t queryString;
105     };
106 
107 private:
108     uint32_t magic;
109 
110 public:
111     DisplayImpl disp;
112     bool finishOnSwap;       // property: debug.egl.finish
113     bool traceGpuCompletion; // property: debug.egl.traceGpuCompletion
114     bool hasColorSpaceSupport;
115 
116 private:
117     uint32_t refs;
118     bool eglIsInitialized;
119     mutable std::mutex lock;
120     mutable std::mutex refLock;
121     mutable std::condition_variable refCond;
122     std::unordered_set<egl_object_t*> objects;
123     std::string mVendorString;
124     std::string mVersionString;
125     std::string mClientApiString;
126     std::string mExtensionString;
127 };
128 
get_display(EGLDisplay dpy)129 inline egl_display_t* get_display(EGLDisplay dpy) {
130     return egl_display_t::get(dpy);
131 }
132 
133 egl_display_t* validate_display(EGLDisplay dpy);
134 egl_display_t* validate_display_connection(EGLDisplay dpy, egl_connection_t** outCnx);
135 EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
136 EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
137 
138 }; // namespace android
139 
140 #endif // ANDROID_EGL_DISPLAY_H
141