• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include <stdint.h>
22 #include <stddef.h>
23 
24 #include <condition_variable>
25 #include <mutex>
26 #include <string>
27 #include <unordered_set>
28 
29 #include <EGL/egl.h>
30 #include <EGL/eglext.h>
31 
32 #include <cutils/compiler.h>
33 
34 #include "egldefs.h"
35 #include "../hooks.h"
36 
37 // ----------------------------------------------------------------------------
38 namespace android {
39 // ----------------------------------------------------------------------------
40 
41 class egl_object_t;
42 class egl_context_t;
43 struct egl_connection_t;
44 
45 bool findExtension(const char* exts, const char* name, size_t nameLen = 0);
46 bool needsAndroidPEglMitigation();
47 
48 // ----------------------------------------------------------------------------
49 
50 class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
51     static egl_display_t sDisplay[NUM_DISPLAYS];
52     EGLDisplay getDisplay(EGLNativeDisplayType display);
53     EGLDisplay getPlatformDisplay(EGLNativeDisplayType display, const EGLAttrib* attrib_list);
54     void loseCurrentImpl(egl_context_t * cur_c);
55 
56 public:
57     enum {
58         NOT_INITIALIZED = 0,
59         INITIALIZED     = 1,
60         TERMINATED      = 2
61     };
62 
63     egl_display_t();
64     ~egl_display_t();
65 
66     EGLBoolean initialize(EGLint *major, EGLint *minor);
67     EGLBoolean terminate();
68 
69     // add object to this display's list
70     void addObject(egl_object_t* object);
71     // remove object from this display's list
72     void removeObject(egl_object_t* object);
73     // add reference to this object. returns true if this is a valid object.
74     bool getObject(egl_object_t* object) const;
75 
76     static egl_display_t* get(EGLDisplay dpy);
77     static EGLDisplay getFromNativeDisplay(EGLNativeDisplayType disp, const EGLAttrib* attrib_list);
78 
79     EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
80             EGLSurface draw, EGLSurface read, EGLContext ctx,
81             EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx);
82     static void loseCurrent(egl_context_t * cur_c);
83 
isReady()84     inline bool isReady() const { return (refs > 0); }
isValid()85     inline bool isValid() const { return magic == '_dpy'; }
isAlive()86     inline bool isAlive() const { return isValid(); }
87 
getVendorString()88     char const * getVendorString() const { return mVendorString.c_str(); }
getVersionString()89     char const * getVersionString() const { return mVersionString.c_str(); }
getClientApiString()90     char const * getClientApiString() const { return mClientApiString.c_str(); }
getExtensionString()91     char const * getExtensionString() const { return mExtensionString.c_str(); }
92 
93     bool haveExtension(const char* name, size_t nameLen = 0) const;
94 
getRefsCount()95     inline uint32_t getRefsCount() const { return refs; }
96 
97     struct strings_t {
98         char const * vendor;
99         char const * version;
100         char const * clientApi;
101         char const * extensions;
102     };
103 
104     struct DisplayImpl {
DisplayImplDisplayImpl105         DisplayImpl() : dpy(EGL_NO_DISPLAY), state(NOT_INITIALIZED) { }
106         EGLDisplay  dpy;
107         EGLint      state;
108         strings_t   queryString;
109     };
110 
111 private:
112     uint32_t        magic;
113 
114 public:
115     DisplayImpl     disp;
116     bool    finishOnSwap;       // property: debug.egl.finish
117     bool    traceGpuCompletion; // property: debug.egl.traceGpuCompletion
118     bool    hasColorSpaceSupport;
119 
120 private:
121     friend class egl_display_ptr;
122 
123             uint32_t                    refs;
124             bool                        eglIsInitialized;
125     mutable std::mutex                  lock;
126     mutable std::mutex                  refLock;
127     mutable std::condition_variable     refCond;
128             std::unordered_set<egl_object_t*> objects;
129             std::string mVendorString;
130             std::string mVersionString;
131             std::string mClientApiString;
132             std::string mExtensionString;
133 };
134 
135 // ----------------------------------------------------------------------------
136 
137 // An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
138 // It doesn't refcount the egl_display_t, but does ensure that the underlying
139 // EGL implementation is "awake" (not hibernating) and ready for use as long
140 // as the egl_display_ptr exists.
141 class egl_display_ptr {
142 public:
egl_display_ptr(egl_display_t * dpy)143     explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {}
144 
145     // We only really need a C++11 move constructor, not a copy constructor.
146     // A move constructor would save an enter()/leave() pair on every EGL API
147     // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
148     // can't use a move constructor until those are cleaned up.
149     //
150     // egl_display_ptr(egl_display_ptr&& other) {
151     //     mDpy = other.mDpy;
152     //     other.mDpy = NULL;
153     // }
154     //
egl_display_ptr(const egl_display_ptr & other)155     egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {}
156 
~egl_display_ptr()157     ~egl_display_ptr() {}
158 
159     const egl_display_t* operator->() const { return mDpy; }
160           egl_display_t* operator->()       { return mDpy; }
161 
get()162     const egl_display_t* get() const { return mDpy; }
get()163           egl_display_t* get()       { return mDpy; }
164 
165     operator bool() const { return mDpy != nullptr; }
166 
167 private:
168     egl_display_t* mDpy;
169 
170     // non-assignable
171     egl_display_ptr& operator=(const egl_display_ptr&);
172 };
173 
174 // ----------------------------------------------------------------------------
175 
get_display(EGLDisplay dpy)176 inline egl_display_ptr get_display(EGLDisplay dpy) {
177     return egl_display_ptr(egl_display_t::get(dpy));
178 }
179 
180 // Does not ensure EGL is unhibernated. Use with caution: calls into the
181 // underlying EGL implementation are not safe.
get_display_nowake(EGLDisplay dpy)182 inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
183     return egl_display_t::get(dpy);
184 }
185 
186 // ----------------------------------------------------------------------------
187 
188 egl_display_ptr validate_display(EGLDisplay dpy);
189 egl_display_ptr validate_display_connection(EGLDisplay dpy,
190         egl_connection_t*& cnx);
191 EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
192 EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
193 
194 // ----------------------------------------------------------------------------
195 }; // namespace android
196 // ----------------------------------------------------------------------------
197 
198 #endif // ANDROID_EGL_DISPLAY_H
199