• 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 // ----------------------------------------------------------------------------
46 
47 class EGLAPI egl_display_t { // marked as EGLAPI for testing purposes
48     static egl_display_t sDisplay[NUM_DISPLAYS];
49     EGLDisplay getDisplay(EGLNativeDisplayType display);
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);
74 
75     EGLBoolean makeCurrent(egl_context_t* c, egl_context_t* cur_c,
76             EGLSurface draw, EGLSurface read, EGLContext ctx,
77             EGLSurface impl_draw, EGLSurface impl_read, 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 
115 private:
116     friend class egl_display_ptr;
117 
118             uint32_t                    refs;
119             bool                        eglIsInitialized;
120     mutable std::mutex                  lock;
121     mutable std::mutex                  refLock;
122     mutable std::condition_variable     refCond;
123             std::unordered_set<egl_object_t*> objects;
124             std::string mVendorString;
125             std::string mVersionString;
126             std::string mClientApiString;
127             std::string mExtensionString;
128 };
129 
130 // ----------------------------------------------------------------------------
131 
132 // An egl_display_ptr is a kind of smart pointer for egl_display_t objects.
133 // It doesn't refcount the egl_display_t, but does ensure that the underlying
134 // EGL implementation is "awake" (not hibernating) and ready for use as long
135 // as the egl_display_ptr exists.
136 class egl_display_ptr {
137 public:
egl_display_ptr(egl_display_t * dpy)138     explicit egl_display_ptr(egl_display_t* dpy): mDpy(dpy) {}
139 
140     // We only really need a C++11 move constructor, not a copy constructor.
141     // A move constructor would save an enter()/leave() pair on every EGL API
142     // call. But enabling -std=c++0x causes lots of errors elsewhere, so I
143     // can't use a move constructor until those are cleaned up.
144     //
145     // egl_display_ptr(egl_display_ptr&& other) {
146     //     mDpy = other.mDpy;
147     //     other.mDpy = NULL;
148     // }
149     //
egl_display_ptr(const egl_display_ptr & other)150     egl_display_ptr(const egl_display_ptr& other): mDpy(other.mDpy) {}
151 
~egl_display_ptr()152     ~egl_display_ptr() {}
153 
154     const egl_display_t* operator->() const { return mDpy; }
155           egl_display_t* operator->()       { return mDpy; }
156 
get()157     const egl_display_t* get() const { return mDpy; }
get()158           egl_display_t* get()       { return mDpy; }
159 
160     operator bool() const { return mDpy != NULL; }
161 
162 private:
163     egl_display_t* mDpy;
164 
165     // non-assignable
166     egl_display_ptr& operator=(const egl_display_ptr&);
167 };
168 
169 // ----------------------------------------------------------------------------
170 
get_display(EGLDisplay dpy)171 inline egl_display_ptr get_display(EGLDisplay dpy) {
172     return egl_display_ptr(egl_display_t::get(dpy));
173 }
174 
175 // Does not ensure EGL is unhibernated. Use with caution: calls into the
176 // underlying EGL implementation are not safe.
get_display_nowake(EGLDisplay dpy)177 inline egl_display_t* get_display_nowake(EGLDisplay dpy) {
178     return egl_display_t::get(dpy);
179 }
180 
181 // ----------------------------------------------------------------------------
182 
183 egl_display_ptr validate_display(EGLDisplay dpy);
184 egl_display_ptr validate_display_connection(EGLDisplay dpy,
185         egl_connection_t*& cnx);
186 EGLBoolean validate_display_context(EGLDisplay dpy, EGLContext ctx);
187 EGLBoolean validate_display_surface(EGLDisplay dpy, EGLSurface surface);
188 
189 // ----------------------------------------------------------------------------
190 }; // namespace android
191 // ----------------------------------------------------------------------------
192 
193 #endif // ANDROID_EGL_DISPLAY_H
194