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