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 #include <stdlib.h>
18 
19 #include <EGL/egl.h>
20 
21 #include <android-base/properties.h>
22 
23 #include <log/log.h>
24 
25 #include "../egl_impl.h"
26 
27 #include "egldefs.h"
28 #include "egl_tls.h"
29 #include "egl_display.h"
30 #include "egl_object.h"
31 #include "egl_layers.h"
32 #include "CallStack.h"
33 #include "Loader.h"
34 
35 // ----------------------------------------------------------------------------
36 namespace android {
37 // ----------------------------------------------------------------------------
38 
39 egl_connection_t gEGLImpl;
40 gl_hooks_t gHooks[2];
41 gl_hooks_t gHooksNoContext;
42 pthread_key_t gGLWrapperKey = -1;
43 
44 // ----------------------------------------------------------------------------
45 
setGLHooksThreadSpecific(gl_hooks_t const * value)46 void setGLHooksThreadSpecific(gl_hooks_t const *value) {
47     setGlThreadSpecific(value);
48 }
49 
50 /*****************************************************************************/
51 
gl_no_context()52 static int gl_no_context() {
53     if (egl_tls_t::logNoContextCall()) {
54         char const* const error = "call to OpenGL ES API with "
55                 "no current context (logged once per thread)";
56         if (LOG_NDEBUG) {
57             ALOGE(error);
58         } else {
59             LOG_ALWAYS_FATAL(error);
60         }
61         if (base::GetBoolProperty("debug.egl.callstack", false)) {
62             CallStack::log(LOG_TAG);
63         }
64     }
65     return 0;
66 }
67 
early_egl_init(void)68 static void early_egl_init(void)
69 {
70     int numHooks = sizeof(gHooksNoContext) / sizeof(EGLFuncPointer);
71     EGLFuncPointer *iter = reinterpret_cast<EGLFuncPointer*>(&gHooksNoContext);
72     for (int hook = 0; hook < numHooks; ++hook) {
73         *(iter++) = reinterpret_cast<EGLFuncPointer>(gl_no_context);
74     }
75 
76     setGLHooksThreadSpecific(&gHooksNoContext);
77 }
78 
79 static pthread_once_t once_control = PTHREAD_ONCE_INIT;
80 static int sEarlyInitState = pthread_once(&once_control, &early_egl_init);
81 
82 // ----------------------------------------------------------------------------
83 
validate_display(EGLDisplay dpy)84 egl_display_ptr validate_display(EGLDisplay dpy) {
85     egl_display_ptr dp = get_display(dpy);
86     if (!dp)
87         return setError(EGL_BAD_DISPLAY, egl_display_ptr(nullptr));
88     if (!dp->isReady())
89         return setError(EGL_NOT_INITIALIZED, egl_display_ptr(nullptr));
90 
91     return dp;
92 }
93 
validate_display_connection(EGLDisplay dpy,egl_connection_t * & cnx)94 egl_display_ptr validate_display_connection(EGLDisplay dpy,
95         egl_connection_t*& cnx) {
96     cnx = nullptr;
97     egl_display_ptr dp = validate_display(dpy);
98     if (!dp)
99         return dp;
100     cnx = &gEGLImpl;
101     if (cnx->dso == nullptr) {
102         return setError(EGL_BAD_CONFIG, egl_display_ptr(nullptr));
103     }
104     return dp;
105 }
106 
107 // ----------------------------------------------------------------------------
108 
egl_get_string_for_current_context(GLenum name)109 const GLubyte * egl_get_string_for_current_context(GLenum name) {
110     // NOTE: returning NULL here will fall-back to the default
111     // implementation.
112 
113     EGLContext context = egl_tls_t::getContext();
114     if (context == EGL_NO_CONTEXT)
115         return nullptr;
116 
117     egl_context_t const * const c = get_context(context);
118     if (c == nullptr) // this should never happen, by construction
119         return nullptr;
120 
121     if (name != GL_EXTENSIONS)
122         return nullptr;
123 
124     return (const GLubyte *)c->gl_extensions.c_str();
125 }
126 
egl_get_string_for_current_context(GLenum name,GLuint index)127 const GLubyte * egl_get_string_for_current_context(GLenum name, GLuint index) {
128     // NOTE: returning NULL here will fall-back to the default
129     // implementation.
130 
131     EGLContext context = egl_tls_t::getContext();
132     if (context == EGL_NO_CONTEXT)
133         return nullptr;
134 
135     egl_context_t const * const c = get_context(context);
136     if (c == nullptr) // this should never happen, by construction
137         return nullptr;
138 
139     if (name != GL_EXTENSIONS)
140         return nullptr;
141 
142     // if index is out of bounds, assume it will be in the default
143     // implementation too, so we don't have to generate a GL error here
144     if (index >= c->tokenized_gl_extensions.size())
145         return nullptr;
146 
147     return (const GLubyte *)c->tokenized_gl_extensions[index].c_str();
148 }
149 
egl_get_num_extensions_for_current_context()150 GLint egl_get_num_extensions_for_current_context() {
151     // NOTE: returning -1 here will fall-back to the default
152     // implementation.
153 
154     EGLContext context = egl_tls_t::getContext();
155     if (context == EGL_NO_CONTEXT)
156         return -1;
157 
158     egl_context_t const * const c = get_context(context);
159     if (c == nullptr) // this should never happen, by construction
160         return -1;
161 
162     return (GLint)c->tokenized_gl_extensions.size();
163 }
164 
egl_get_connection()165 egl_connection_t* egl_get_connection() {
166     return &gEGLImpl;
167 }
168 
169 // ----------------------------------------------------------------------------
170 
egl_init_drivers_locked()171 static EGLBoolean egl_init_drivers_locked() {
172     if (sEarlyInitState) {
173         // initialized by static ctor. should be set here.
174         return EGL_FALSE;
175     }
176 
177     // get our driver loader
178     Loader& loader(Loader::getInstance());
179 
180     // dynamically load our EGL implementation
181     egl_connection_t* cnx = &gEGLImpl;
182     cnx->hooks[egl_connection_t::GLESv1_INDEX] = &gHooks[egl_connection_t::GLESv1_INDEX];
183     cnx->hooks[egl_connection_t::GLESv2_INDEX] = &gHooks[egl_connection_t::GLESv2_INDEX];
184     cnx->dso = loader.open(cnx);
185 
186     // Check to see if any layers are enabled and route functions through them
187     if (cnx->dso) {
188         // Layers can be enabled long after the drivers have been loaded.
189         // They will only be initialized once.
190         LayerLoader& layer_loader(LayerLoader::getInstance());
191         layer_loader.InitLayers(cnx);
192     }
193 
194     return cnx->dso ? EGL_TRUE : EGL_FALSE;
195 }
196 
197 
198 // this mutex protects driver load logic as a critical section since it accesses to global variable
199 // like gEGLImpl
200 static pthread_mutex_t sInitDriverMutex = PTHREAD_MUTEX_INITIALIZER;
201 
egl_init_drivers()202 EGLBoolean egl_init_drivers() {
203     EGLBoolean res;
204     pthread_mutex_lock(&sInitDriverMutex);
205     res = egl_init_drivers_locked();
206     pthread_mutex_unlock(&sInitDriverMutex);
207     return res;
208 }
209 
210 static pthread_mutex_t sLogPrintMutex = PTHREAD_MUTEX_INITIALIZER;
211 static std::chrono::steady_clock::time_point sLogPrintTime;
212 static constexpr std::chrono::seconds DURATION(1);
213 
gl_unimplemented()214 void gl_unimplemented() {
215     bool printLog = false;
216     auto now = std::chrono::steady_clock::now();
217     pthread_mutex_lock(&sLogPrintMutex);
218     if ((now - sLogPrintTime) > DURATION) {
219         sLogPrintTime = now;
220         printLog = true;
221     }
222     pthread_mutex_unlock(&sLogPrintMutex);
223     if (printLog) {
224         ALOGE("called unimplemented OpenGL ES API");
225         if (base::GetBoolProperty("debug.egl.callstack", false)) {
226             CallStack::log(LOG_TAG);
227         }
228     }
229 }
230 
gl_noop()231 void gl_noop() {
232 }
233 
234 // ----------------------------------------------------------------------------
235 
setGlThreadSpecific(gl_hooks_t const * value)236 void setGlThreadSpecific(gl_hooks_t const *value) {
237     gl_hooks_t const * volatile * tls_hooks = get_tls_hooks();
238     tls_hooks[TLS_SLOT_OPENGL_API] = value;
239 }
240 
241 // ----------------------------------------------------------------------------
242 // GL / EGL hooks
243 // ----------------------------------------------------------------------------
244 
245 #undef GL_ENTRY
246 #undef EGL_ENTRY
247 #define GL_ENTRY(_r, _api, ...) #_api,
248 #define EGL_ENTRY(_r, _api, ...) #_api,
249 
250 char const * const gl_names[] = {
251     #include "../entries.in"
252     nullptr
253 };
254 
255 char const * const gl_names_1[] = {
256     #include "../entries_gles1.in"
257     nullptr
258 };
259 
260 char const * const egl_names[] = {
261     #include "egl_entries.in"
262     nullptr
263 };
264 
265 char const * const platform_names[] = {
266     #include "platform_entries.in"
267     nullptr
268 };
269 
270 #undef GL_ENTRY
271 #undef EGL_ENTRY
272 
273 
274 // ----------------------------------------------------------------------------
275 }; // namespace android
276 // ----------------------------------------------------------------------------
277 
278