blob: a92d22492ab4f397f7d05d3bc784d443b9103bce [file] [log] [blame]
Mathias Agopian518ec112011-05-13 16:21:08 -07001/*
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
Mathias Agopian4b9511c2011-11-13 23:52:47 -080017#include <string.h>
18
Jamie Gennisaca51c02011-11-03 17:42:43 -070019#include "egl_cache.h"
Mathias Agopian518ec112011-05-13 16:21:08 -070020#include "egl_display.h"
21#include "egl_object.h"
22#include "egl_tls.h"
23#include "egl_impl.h"
24#include "Loader.h"
25
26// ----------------------------------------------------------------------------
27namespace android {
28// ----------------------------------------------------------------------------
29
Mathias Agopian4b9511c2011-11-13 23:52:47 -080030static char const * const sVendorString = "Android";
31static char const * const sVersionString = "1.4 Android META-EGL";
32static char const * const sClientApiString = "OpenGL ES";
33
34// this is the list of EGL extensions that are exposed to applications
35// some of them are mandatory because used by the ANDROID system.
36//
37// mandatory extensions are required per the CDD and not explicitly
38// checked during EGL initialization. the system *assumes* these extensions
39// are present. the system may not function properly if some mandatory
40// extensions are missing.
41//
42// NOTE: sExtensionString MUST be have a single space as the last character.
43//
44static char const * const sExtensionString =
45 "EGL_KHR_image " // mandatory
46 "EGL_KHR_image_base " // mandatory
47 "EGL_KHR_image_pixmap "
48 "EGL_KHR_gl_texture_2D_image "
49 "EGL_KHR_gl_texture_cubemap_image "
50 "EGL_KHR_gl_renderbuffer_image "
51 "EGL_KHR_fence_sync "
52 "EGL_NV_system_time "
53 "EGL_ANDROID_image_native_buffer " // mandatory
54 ;
55
56// extensions not exposed to applications but used by the ANDROID system
57// "EGL_ANDROID_recordable " // mandatory
58// "EGL_ANDROID_blob_cache " // strongly recommended
59
Mathias Agopian518ec112011-05-13 16:21:08 -070060extern void initEglTraceLevel();
61extern void setGLHooksThreadSpecific(gl_hooks_t const *value);
62
63static int cmp_configs(const void* a, const void *b) {
64 const egl_config_t& c0 = *(egl_config_t const *)a;
65 const egl_config_t& c1 = *(egl_config_t const *)b;
66 return c0<c1 ? -1 : (c1<c0 ? 1 : 0);
67}
68
69// ----------------------------------------------------------------------------
70
71egl_display_t egl_display_t::sDisplay[NUM_DISPLAYS];
72
73egl_display_t::egl_display_t() :
74 magic('_dpy'), numTotalConfigs(0), configs(0), refs(0) {
75}
76
77egl_display_t::~egl_display_t() {
78 magic = 0;
Jamie Gennis76601082011-11-06 14:14:33 -080079 egl_cache_t::get()->terminate();
Mathias Agopian518ec112011-05-13 16:21:08 -070080}
81
82egl_display_t* egl_display_t::get(EGLDisplay dpy) {
83 uintptr_t index = uintptr_t(dpy)-1U;
84 return (index >= NUM_DISPLAYS) ? NULL : &sDisplay[index];
85}
86
87void egl_display_t::addObject(egl_object_t* object) {
88 Mutex::Autolock _l(lock);
89 objects.add(object);
90}
91
Mathias Agopian5b287a62011-05-16 18:58:55 -070092void egl_display_t::removeObject(egl_object_t* object) {
93 Mutex::Autolock _l(lock);
94 objects.remove(object);
95}
96
Mathias Agopianf0480de2011-11-13 20:50:07 -080097bool egl_display_t::getObject(egl_object_t* object) const {
Mathias Agopian518ec112011-05-13 16:21:08 -070098 Mutex::Autolock _l(lock);
99 if (objects.indexOf(object) >= 0) {
Mathias Agopianf0480de2011-11-13 20:50:07 -0800100 if (object->getDisplay() == this) {
101 object->incRef();
102 return true;
103 }
Mathias Agopian518ec112011-05-13 16:21:08 -0700104 }
105 return false;
106}
107
Mathias Agopian518ec112011-05-13 16:21:08 -0700108EGLDisplay egl_display_t::getFromNativeDisplay(EGLNativeDisplayType disp) {
109 if (uintptr_t(disp) >= NUM_DISPLAYS)
110 return NULL;
111
112 return sDisplay[uintptr_t(disp)].getDisplay(disp);
113}
114
115EGLDisplay egl_display_t::getDisplay(EGLNativeDisplayType display) {
116
117 Mutex::Autolock _l(lock);
118
119 // get our driver loader
120 Loader& loader(Loader::getInstance());
121
Mathias Agopianada798b2012-02-13 17:09:30 -0800122 egl_connection_t* const cnx = &gEGLImpl;
123 if (cnx->dso && disp.dpy == EGL_NO_DISPLAY) {
124 EGLDisplay dpy = cnx->egl.eglGetDisplay(display);
125 disp.dpy = dpy;
126 if (dpy == EGL_NO_DISPLAY) {
127 loader.close(cnx->dso);
128 cnx->dso = NULL;
Mathias Agopian518ec112011-05-13 16:21:08 -0700129 }
130 }
131
132 return EGLDisplay(uintptr_t(display) + 1U);
133}
134
135EGLBoolean egl_display_t::initialize(EGLint *major, EGLint *minor) {
136
137 Mutex::Autolock _l(lock);
138
139 if (refs > 0) {
140 if (major != NULL)
141 *major = VERSION_MAJOR;
142 if (minor != NULL)
143 *minor = VERSION_MINOR;
144 refs++;
145 return EGL_TRUE;
146 }
147
148#if EGL_TRACE
149
150 // Called both at early_init time and at this time. (Early_init is pre-zygote, so
151 // the information from that call may be stale.)
152 initEglTraceLevel();
153
154#endif
155
156 setGLHooksThreadSpecific(&gHooksNoContext);
157
158 // initialize each EGL and
159 // build our own extension string first, based on the extension we know
160 // and the extension supported by our client implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800161
162 egl_connection_t* const cnx = &gEGLImpl;
163 cnx->major = -1;
164 cnx->minor = -1;
165 if (cnx->dso) {
Mathias Agopian518ec112011-05-13 16:21:08 -0700166
167#if defined(ADRENO130)
168#warning "Adreno-130 eglInitialize() workaround"
169 /*
170 * The ADRENO 130 driver returns a different EGLDisplay each time
171 * eglGetDisplay() is called, but also makes the EGLDisplay invalid
172 * after eglTerminate() has been called, so that eglInitialize()
173 * cannot be called again. Therefore, we need to make sure to call
174 * eglGetDisplay() before calling eglInitialize();
175 */
176 if (i == IMPL_HARDWARE) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800177 disp[i].dpy = cnx->egl.eglGetDisplay(EGL_DEFAULT_DISPLAY);
Mathias Agopian518ec112011-05-13 16:21:08 -0700178 }
179#endif
180
Mathias Agopianada798b2012-02-13 17:09:30 -0800181 EGLDisplay idpy = disp.dpy;
Mathias Agopian518ec112011-05-13 16:21:08 -0700182 if (cnx->egl.eglInitialize(idpy, &cnx->major, &cnx->minor)) {
Mathias Agopianada798b2012-02-13 17:09:30 -0800183 //ALOGD("initialized dpy=%p, ver=%d.%d, cnx=%p",
184 // idpy, cnx->major, cnx->minor, cnx);
Mathias Agopian518ec112011-05-13 16:21:08 -0700185
186 // display is now initialized
Mathias Agopianada798b2012-02-13 17:09:30 -0800187 disp.state = egl_display_t::INITIALIZED;
Mathias Agopian518ec112011-05-13 16:21:08 -0700188
189 // get the query-strings for this display for each implementation
Mathias Agopianada798b2012-02-13 17:09:30 -0800190 disp.queryString.vendor = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700191 EGL_VENDOR);
Mathias Agopianada798b2012-02-13 17:09:30 -0800192 disp.queryString.version = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700193 EGL_VERSION);
Mathias Agopianada798b2012-02-13 17:09:30 -0800194 disp.queryString.extensions = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700195 EGL_EXTENSIONS);
Mathias Agopianada798b2012-02-13 17:09:30 -0800196 disp.queryString.clientApi = cnx->egl.eglQueryString(idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700197 EGL_CLIENT_APIS);
198
199 } else {
Mathias Agopianada798b2012-02-13 17:09:30 -0800200 ALOGW("eglInitialize(%p) failed (%s)", idpy,
Mathias Agopian518ec112011-05-13 16:21:08 -0700201 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
202 }
203 }
204
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800205 // the query strings are per-display
206 mVendorString.setTo(sVendorString);
207 mVersionString.setTo(sVersionString);
208 mClientApiString.setTo(sClientApiString);
209
210 // we only add extensions that exist in at least one implementation
211 char const* start = sExtensionString;
212 char const* end;
213 do {
214 // find the space separating this extension for the next one
215 end = strchr(start, ' ');
216 if (end) {
217 // length of the extension string
218 const size_t len = end - start;
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800219 if (len) {
220 // NOTE: we could avoid the copy if we had strnstr.
221 const String8 ext(start, len);
Mathias Agopianada798b2012-02-13 17:09:30 -0800222 // now look for this extension
223 if (disp.queryString.extensions) {
224 // if we find it, add this extension string to our list
225 // (and don't forget the space)
226 const char* match = strstr(disp.queryString.extensions, ext.string());
227 if (match && (match[len] == ' ' || match[len] == 0)) {
228 mExtensionString.append(start, len+1);
Mathias Agopianf3ae82d2011-11-16 16:49:25 -0800229 }
Mathias Agopian4b9511c2011-11-13 23:52:47 -0800230 }
231 }
232 // process the next extension string, and skip the space.
233 start = end + 1;
234 }
235 } while (end);
236
Jamie Gennisaca51c02011-11-03 17:42:43 -0700237 egl_cache_t::get()->initialize(this);
238
Mathias Agopian518ec112011-05-13 16:21:08 -0700239 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800240 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
241 EGLint n;
242 if (cnx->egl.eglGetConfigs(disp.dpy, 0, 0, &n)) {
243 disp.config = (EGLConfig*) malloc(sizeof(EGLConfig) * n);
244 if (disp.config) {
245 if (cnx->egl.eglGetConfigs(disp.dpy, disp.config, n,
246 &disp.numConfigs)) {
247 numTotalConfigs += n;
248 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700249 }
250 }
251 }
252 }
253
254 if (res == EGL_TRUE) {
255 configs = new egl_config_t[numTotalConfigs];
Mathias Agopianada798b2012-02-13 17:09:30 -0800256 int k = 0;
257 egl_connection_t* const cnx = &gEGLImpl;
258 if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) {
259 for (int j = 0; j < disp.numConfigs; j++) {
260 configs[k].config = disp.config[j];
261 configs[k].configId = k + 1; // CONFIG_ID start at 1
262 // store the implementation's CONFIG_ID
263 cnx->egl.eglGetConfigAttrib(disp.dpy, disp.config[j],
264 EGL_CONFIG_ID, &configs[k].implConfigId);
265 k++;
Mathias Agopian518ec112011-05-13 16:21:08 -0700266 }
267 }
268
269 // sort our configurations so we can do binary-searches
270 qsort(configs, numTotalConfigs, sizeof(egl_config_t), cmp_configs);
271
272 refs++;
273 if (major != NULL)
274 *major = VERSION_MAJOR;
275 if (minor != NULL)
276 *minor = VERSION_MINOR;
277 return EGL_TRUE;
278 }
279 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
280}
281
282EGLBoolean egl_display_t::terminate() {
283
284 Mutex::Autolock _l(lock);
285
286 if (refs == 0) {
287 return setError(EGL_NOT_INITIALIZED, EGL_FALSE);
288 }
289
290 // this is specific to Android, display termination is ref-counted.
291 if (refs > 1) {
292 refs--;
293 return EGL_TRUE;
294 }
295
296 EGLBoolean res = EGL_FALSE;
Mathias Agopianada798b2012-02-13 17:09:30 -0800297 egl_connection_t* const cnx = &gEGLImpl;
298 if (cnx->dso && disp.state == egl_display_t::INITIALIZED) {
299 if (cnx->egl.eglTerminate(disp.dpy) == EGL_FALSE) {
300 ALOGW("eglTerminate(%p) failed (%s)", disp.dpy,
301 egl_tls_t::egl_strerror(cnx->egl.eglGetError()));
Mathias Agopian518ec112011-05-13 16:21:08 -0700302 }
Mathias Agopianada798b2012-02-13 17:09:30 -0800303 // REVISIT: it's unclear what to do if eglTerminate() fails
304 free(disp.config);
305
306 disp.numConfigs = 0;
307 disp.config = 0;
308 disp.state = egl_display_t::TERMINATED;
309
310 res = EGL_TRUE;
Mathias Agopian518ec112011-05-13 16:21:08 -0700311 }
312
Mathias Agopian5b287a62011-05-16 18:58:55 -0700313 // Mark all objects remaining in the list as terminated, unless
314 // there are no reference to them, it which case, we're free to
315 // delete them.
316 size_t count = objects.size();
Steve Block32397c12012-01-05 23:22:43 +0000317 ALOGW_IF(count, "eglTerminate() called w/ %d objects remaining", count);
Mathias Agopian5b287a62011-05-16 18:58:55 -0700318 for (size_t i=0 ; i<count ; i++) {
319 egl_object_t* o = objects.itemAt(i);
320 o->destroy();
321 }
322
323 // this marks all object handles are "terminated"
324 objects.clear();
Mathias Agopian518ec112011-05-13 16:21:08 -0700325
326 refs--;
327 numTotalConfigs = 0;
328 delete[] configs;
329 return res;
330}
331
Mathias Agopianfb87e542012-01-30 18:20:52 -0800332void egl_display_t::loseCurrent(egl_context_t * cur_c)
333{
334 if (cur_c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800335 egl_display_t* display = cur_c->getDisplay();
336 if (display) {
337 display->loseCurrentImpl(cur_c);
338 }
339 }
340}
Mathias Agopianfb87e542012-01-30 18:20:52 -0800341
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800342void egl_display_t::loseCurrentImpl(egl_context_t * cur_c)
343{
344 // by construction, these are either 0 or valid (possibly terminated)
345 // it should be impossible for these to be invalid
346 ContextRef _cur_c(cur_c);
347 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
348 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800349
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800350 { // scope for the lock
351 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800352 cur_c->onLooseCurrent();
353
Mathias Agopianfb87e542012-01-30 18:20:52 -0800354 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800355
356 // This cannot be called with the lock held because it might end-up
357 // calling back into EGL (in particular when a surface is destroyed
358 // it calls ANativeWindow::disconnect
359 _cur_c.release();
360 _cur_r.release();
361 _cur_d.release();
Mathias Agopianfb87e542012-01-30 18:20:52 -0800362}
363
364EGLBoolean egl_display_t::makeCurrent(egl_context_t* c, egl_context_t* cur_c,
365 EGLSurface draw, EGLSurface read, EGLContext ctx,
366 EGLSurface impl_draw, EGLSurface impl_read, EGLContext impl_ctx)
367{
Mathias Agopianfb87e542012-01-30 18:20:52 -0800368 EGLBoolean result;
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800369
370 // by construction, these are either 0 or valid (possibly terminated)
371 // it should be impossible for these to be invalid
372 ContextRef _cur_c(cur_c);
373 SurfaceRef _cur_r(cur_c ? get_surface(cur_c->read) : NULL);
374 SurfaceRef _cur_d(cur_c ? get_surface(cur_c->draw) : NULL);
375
376 { // scope for the lock
377 Mutex::Autolock _l(lock);
Mathias Agopianfb87e542012-01-30 18:20:52 -0800378 if (c) {
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800379 result = c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800380 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800381 if (result == EGL_TRUE) {
382 c->onMakeCurrent(draw, read);
383 }
384 } else {
385 result = cur_c->cnx->egl.eglMakeCurrent(
Mathias Agopianada798b2012-02-13 17:09:30 -0800386 disp.dpy, impl_draw, impl_read, impl_ctx);
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800387 if (result == EGL_TRUE) {
388 cur_c->onLooseCurrent();
389 }
Mathias Agopianfb87e542012-01-30 18:20:52 -0800390 }
391 }
Mathias Agopiana4b2c042012-02-03 15:24:51 -0800392
393 if (result == EGL_TRUE) {
394 // This cannot be called with the lock held because it might end-up
395 // calling back into EGL (in particular when a surface is destroyed
396 // it calls ANativeWindow::disconnect
397 _cur_c.release();
398 _cur_r.release();
399 _cur_d.release();
400 }
401
Mathias Agopianfb87e542012-01-30 18:20:52 -0800402 return result;
403}
Mathias Agopian518ec112011-05-13 16:21:08 -0700404
405// ----------------------------------------------------------------------------
406}; // namespace android
407// ----------------------------------------------------------------------------