blob: 2fc612522f7a7fa54550afa3eacdf09b28e7f8a8 [file] [log] [blame]
Mathias Agopiande586972009-05-28 17:39:03 -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
17#include <ctype.h>
18#include <stdlib.h>
19#include <stdio.h>
20#include <string.h>
21#include <errno.h>
22#include <dlfcn.h>
23#include <limits.h>
24
25#include <cutils/log.h>
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020026#include <cutils/properties.h>
Mathias Agopiande586972009-05-28 17:39:03 -070027
28#include <EGL/egl.h>
29
Mathias Agopian1cadb252011-05-23 17:26:14 -070030#include "egldefs.h"
Siva Velusamy0469dd62011-11-30 15:05:37 -080031#include "glestrace.h"
Mathias Agopian1cadb252011-05-23 17:26:14 -070032#include "hooks.h"
33#include "Loader.h"
Mathias Agopiande586972009-05-28 17:39:03 -070034
35// ----------------------------------------------------------------------------
36namespace android {
37// ----------------------------------------------------------------------------
38
39
40/*
41 * EGL drivers are called
42 *
43 * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
44 *
45 */
46
47ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
48
David 'Digit' Turner80b30c22011-08-26 17:38:47 +020049/* This function is called to check whether we run inside the emulator,
50 * and if this is the case whether GLES GPU emulation is supported.
51 *
52 * Returned values are:
53 * -1 -> not running inside the emulator
54 * 0 -> running inside the emulator, but GPU emulation not supported
55 * 1 -> running inside the emulator, GPU emulation is supported
56 * through the "emulation" config.
57 */
58static int
59checkGlesEmulationStatus(void)
60{
61 /* We're going to check for the following kernel parameters:
62 *
63 * qemu=1 -> tells us that we run inside the emulator
64 * android.qemu.gles=<number> -> tells us the GLES GPU emulation status
65 *
66 * Note that we will return <number> if we find it. This let us support
67 * more additionnal emulation modes in the future.
68 */
69 char prop[PROPERTY_VALUE_MAX];
70 int result = -1;
71
72 /* First, check for qemu=1 */
73 property_get("ro.kernel.qemu",prop,"0");
74 if (atoi(prop) != 1)
75 return -1;
76
77 /* We are in the emulator, get GPU status value */
78 property_get("ro.kernel.qemu.gles",prop,"0");
79 return atoi(prop);
80}
81
Mathias Agopiande586972009-05-28 17:39:03 -070082// ----------------------------------------------------------------------------
83
84Loader::driver_t::driver_t(void* gles)
85{
86 dso[0] = gles;
87 for (size_t i=1 ; i<NELEM(dso) ; i++)
88 dso[i] = 0;
89}
90
91Loader::driver_t::~driver_t()
92{
93 for (size_t i=0 ; i<NELEM(dso) ; i++) {
94 if (dso[i]) {
95 dlclose(dso[i]);
96 dso[i] = 0;
97 }
98 }
99}
100
101status_t Loader::driver_t::set(void* hnd, int32_t api)
102{
103 switch (api) {
104 case EGL:
105 dso[0] = hnd;
106 break;
107 case GLESv1_CM:
108 dso[1] = hnd;
109 break;
110 case GLESv2:
111 dso[2] = hnd;
112 break;
113 default:
114 return BAD_INDEX;
115 }
116 return NO_ERROR;
117}
118
119// ----------------------------------------------------------------------------
120
121Loader::entry_t::entry_t(int dpy, int impl, const char* tag)
122 : dpy(dpy), impl(impl), tag(tag) {
123}
124
125// ----------------------------------------------------------------------------
126
127Loader::Loader()
128{
129 char line[256];
130 char tag[256];
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200131
132 /* Special case for GLES emulation */
133 if (checkGlesEmulationStatus() == 0) {
Steve Block9d453682011-12-20 16:23:08 +0000134 ALOGD("Emulator without GPU support detected. Fallback to software renderer.");
David 'Digit' Turner80b30c22011-08-26 17:38:47 +0200135 gConfig.add( entry_t(0, 0, "android") );
136 return;
137 }
138
139 /* Otherwise, use egl.cfg */
Mathias Agopiande586972009-05-28 17:39:03 -0700140 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
141 if (cfg == NULL) {
142 // default config
Steve Block9d453682011-12-20 16:23:08 +0000143 ALOGD("egl.cfg not found, using default config");
Mathias Agopiande586972009-05-28 17:39:03 -0700144 gConfig.add( entry_t(0, 0, "android") );
145 } else {
146 while (fgets(line, 256, cfg)) {
147 int dpy;
148 int impl;
149 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
Steve Block9d453682011-12-20 16:23:08 +0000150 //ALOGD(">>> %u %u %s", dpy, impl, tag);
Mathias Agopiande586972009-05-28 17:39:03 -0700151 gConfig.add( entry_t(dpy, impl, tag) );
152 }
153 }
154 fclose(cfg);
155 }
156}
157
158Loader::~Loader()
159{
Siva Velusamy0469dd62011-11-30 15:05:37 -0800160 GLTrace_stop();
Mathias Agopiande586972009-05-28 17:39:03 -0700161}
162
163const char* Loader::getTag(int dpy, int impl)
164{
165 const Vector<entry_t>& cfgs(gConfig);
166 const size_t c = cfgs.size();
167 for (size_t i=0 ; i<c ; i++) {
168 if (dpy == cfgs[i].dpy)
169 if (impl == cfgs[i].impl)
170 return cfgs[i].tag.string();
171 }
172 return 0;
173}
174
Mathias Agopian618fa102009-10-14 02:06:37 -0700175void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700176{
177 /*
178 * TODO: if we don't find display/0, then use 0/0
179 * (0/0 should always work)
180 */
181
182 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700183 int index = int(display);
184 driver_t* hnd = 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700185
186 char const* tag = getTag(index, impl);
187 if (tag) {
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700188 dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopiande586972009-05-28 17:39:03 -0700189 if (dso) {
190 hnd = new driver_t(dso);
191 } else {
192 // Always load EGL first
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700193 dso = load_driver("EGL", tag, cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700194 if (dso) {
195 hnd = new driver_t(dso);
196
197 // TODO: make this more automated
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700198 hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
Mathias Agopiande586972009-05-28 17:39:03 -0700199
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700200 hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700201 }
202 }
203 }
204
205 LOG_FATAL_IF(!index && !impl && !hnd,
Mathias Agopianacdebe32009-06-03 18:26:58 -0700206 "couldn't find the default OpenGL ES implementation "
Mathias Agopiande586972009-05-28 17:39:03 -0700207 "for default display");
208
209 return (void*)hnd;
210}
211
212status_t Loader::close(void* driver)
213{
214 driver_t* hnd = (driver_t*)driver;
215 delete hnd;
216 return NO_ERROR;
217}
218
219void Loader::init_api(void* dso,
220 char const * const * api,
221 __eglMustCastToProperFunctionPointerType* curr,
222 getProcAddressType getProcAddress)
223{
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700224 const size_t SIZE = 256;
225 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700226 while (*api) {
227 char const * name = *api;
228 __eglMustCastToProperFunctionPointerType f =
229 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
230 if (f == NULL) {
231 // couldn't find the entry-point, use eglGetProcAddress()
232 f = getProcAddress(name);
233 }
234 if (f == NULL) {
235 // Try without the OES postfix
236 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700237 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700238 strncpy(scrap, name, index);
239 scrap[index] = 0;
240 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000241 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700242 }
243 }
244 if (f == NULL) {
245 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700246 ssize_t index = ssize_t(strlen(name)) - 3;
247 if (index>0 && strcmp(name+index, "OES")) {
248 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700249 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
Steve Block9d453682011-12-20 16:23:08 +0000250 //ALOGD_IF(f, "found <%s> instead", scrap);
Mathias Agopiande586972009-05-28 17:39:03 -0700251 }
252 }
253 if (f == NULL) {
Steve Block9d453682011-12-20 16:23:08 +0000254 //ALOGD("%s", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700255 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
256 }
257 *curr++ = f;
258 api++;
259 }
260}
261
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700262void *Loader::load_driver(const char* kind, const char *tag,
Mathias Agopian618fa102009-10-14 02:06:37 -0700263 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700264{
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700265 char driver_absolute_path[PATH_MAX];
266 const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
267 const char* const search2 = "/system/lib/egl/lib%s_%s.so";
268
269 snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
Mathias Agopian8c173842009-09-20 16:01:02 -0700270 if (access(driver_absolute_path, R_OK)) {
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700271 snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
272 if (access(driver_absolute_path, R_OK)) {
273 // this happens often, we don't want to log an error
274 return 0;
275 }
Mathias Agopian8c173842009-09-20 16:01:02 -0700276 }
Mathias Agopiande586972009-05-28 17:39:03 -0700277
Mathias Agopian8c173842009-09-20 16:01:02 -0700278 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
279 if (dso == 0) {
280 const char* err = dlerror();
Steve Blocke6f43dd2012-01-06 19:20:56 +0000281 ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
Mathias Agopian8c173842009-09-20 16:01:02 -0700282 return 0;
283 }
284
Steve Block9d453682011-12-20 16:23:08 +0000285 ALOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700286
Mathias Agopiande586972009-05-28 17:39:03 -0700287 if (mask & EGL) {
288 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
289
Steve Blocke6f43dd2012-01-06 19:20:56 +0000290 ALOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700291 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700292
Mathias Agopian618fa102009-10-14 02:06:37 -0700293 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700294 __eglMustCastToProperFunctionPointerType* curr =
295 (__eglMustCastToProperFunctionPointerType*)egl;
296 char const * const * api = egl_names;
297 while (*api) {
298 char const * name = *api;
299 __eglMustCastToProperFunctionPointerType f =
300 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
301 if (f == NULL) {
302 // couldn't find the entry-point, use eglGetProcAddress()
303 f = getProcAddress(name);
304 if (f == NULL) {
305 f = (__eglMustCastToProperFunctionPointerType)0;
306 }
307 }
308 *curr++ = f;
309 api++;
310 }
311 }
312
313 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700314 init_api(dso, gl_names,
315 (__eglMustCastToProperFunctionPointerType*)
316 &cnx->hooks[GLESv1_INDEX]->gl,
317 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700318 }
319
320 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700321 init_api(dso, gl_names,
322 (__eglMustCastToProperFunctionPointerType*)
323 &cnx->hooks[GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700324 getProcAddress);
325 }
326
327 return dso;
328}
329
330// ----------------------------------------------------------------------------
331}; // namespace android
332// ----------------------------------------------------------------------------