blob: da2622965b98062909bb141b1185fae93fbb72db [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>
26
27#include <EGL/egl.h>
28
29#include "hooks.h"
30#include "egl_impl.h"
31
32#include "Loader.h"
David Li65948aa2011-03-10 16:40:37 -080033#include "glesv2dbg.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
49// ----------------------------------------------------------------------------
50
51Loader::driver_t::driver_t(void* gles)
52{
53 dso[0] = gles;
54 for (size_t i=1 ; i<NELEM(dso) ; i++)
55 dso[i] = 0;
56}
57
58Loader::driver_t::~driver_t()
59{
60 for (size_t i=0 ; i<NELEM(dso) ; i++) {
61 if (dso[i]) {
62 dlclose(dso[i]);
63 dso[i] = 0;
64 }
65 }
66}
67
68status_t Loader::driver_t::set(void* hnd, int32_t api)
69{
70 switch (api) {
71 case EGL:
72 dso[0] = hnd;
73 break;
74 case GLESv1_CM:
75 dso[1] = hnd;
76 break;
77 case GLESv2:
78 dso[2] = hnd;
79 break;
80 default:
81 return BAD_INDEX;
82 }
83 return NO_ERROR;
84}
85
86// ----------------------------------------------------------------------------
87
88Loader::entry_t::entry_t(int dpy, int impl, const char* tag)
89 : dpy(dpy), impl(impl), tag(tag) {
90}
91
92// ----------------------------------------------------------------------------
93
94Loader::Loader()
95{
96 char line[256];
97 char tag[256];
98 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
99 if (cfg == NULL) {
100 // default config
101 LOGD("egl.cfg not found, using default config");
102 gConfig.add( entry_t(0, 0, "android") );
103 } else {
104 while (fgets(line, 256, cfg)) {
105 int dpy;
106 int impl;
107 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
Mathias Agopianf909cb62009-06-03 18:30:22 -0700108 //LOGD(">>> %u %u %s", dpy, impl, tag);
Mathias Agopiande586972009-05-28 17:39:03 -0700109 gConfig.add( entry_t(dpy, impl, tag) );
110 }
111 }
112 fclose(cfg);
113 }
114}
115
116Loader::~Loader()
117{
David Li2f5a6552011-03-01 16:08:10 -0800118 StopDebugServer();
Mathias Agopiande586972009-05-28 17:39:03 -0700119}
120
121const char* Loader::getTag(int dpy, int impl)
122{
123 const Vector<entry_t>& cfgs(gConfig);
124 const size_t c = cfgs.size();
125 for (size_t i=0 ; i<c ; i++) {
126 if (dpy == cfgs[i].dpy)
127 if (impl == cfgs[i].impl)
128 return cfgs[i].tag.string();
129 }
130 return 0;
131}
132
Mathias Agopian618fa102009-10-14 02:06:37 -0700133void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700134{
135 /*
136 * TODO: if we don't find display/0, then use 0/0
137 * (0/0 should always work)
138 */
139
140 void* dso;
Mathias Agopiande586972009-05-28 17:39:03 -0700141 int index = int(display);
142 driver_t* hnd = 0;
Mathias Agopiande586972009-05-28 17:39:03 -0700143
144 char const* tag = getTag(index, impl);
145 if (tag) {
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700146 dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopiande586972009-05-28 17:39:03 -0700147 if (dso) {
148 hnd = new driver_t(dso);
149 } else {
150 // Always load EGL first
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700151 dso = load_driver("EGL", tag, cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700152 if (dso) {
153 hnd = new driver_t(dso);
154
155 // TODO: make this more automated
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700156 hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
Mathias Agopiande586972009-05-28 17:39:03 -0700157
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700158 hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700159 }
160 }
161 }
162
163 LOG_FATAL_IF(!index && !impl && !hnd,
Mathias Agopianacdebe32009-06-03 18:26:58 -0700164 "couldn't find the default OpenGL ES implementation "
Mathias Agopiande586972009-05-28 17:39:03 -0700165 "for default display");
166
167 return (void*)hnd;
168}
169
170status_t Loader::close(void* driver)
171{
172 driver_t* hnd = (driver_t*)driver;
173 delete hnd;
174 return NO_ERROR;
175}
176
177void Loader::init_api(void* dso,
178 char const * const * api,
179 __eglMustCastToProperFunctionPointerType* curr,
180 getProcAddressType getProcAddress)
181{
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700182 const size_t SIZE = 256;
183 char scrap[SIZE];
Mathias Agopiande586972009-05-28 17:39:03 -0700184 while (*api) {
185 char const * name = *api;
186 __eglMustCastToProperFunctionPointerType f =
187 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
188 if (f == NULL) {
189 // couldn't find the entry-point, use eglGetProcAddress()
190 f = getProcAddress(name);
191 }
192 if (f == NULL) {
193 // Try without the OES postfix
194 ssize_t index = ssize_t(strlen(name)) - 3;
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700195 if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
Mathias Agopiande586972009-05-28 17:39:03 -0700196 strncpy(scrap, name, index);
197 scrap[index] = 0;
198 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
199 //LOGD_IF(f, "found <%s> instead", scrap);
200 }
201 }
202 if (f == NULL) {
203 // Try with the OES postfix
Mathias Agopian0ad71a92011-05-11 20:37:47 -0700204 ssize_t index = ssize_t(strlen(name)) - 3;
205 if (index>0 && strcmp(name+index, "OES")) {
206 snprintf(scrap, SIZE, "%sOES", name);
Mathias Agopiande586972009-05-28 17:39:03 -0700207 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
208 //LOGD_IF(f, "found <%s> instead", scrap);
209 }
210 }
211 if (f == NULL) {
212 //LOGD("%s", name);
213 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
214 }
215 *curr++ = f;
216 api++;
217 }
218}
219
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700220void *Loader::load_driver(const char* kind, const char *tag,
Mathias Agopian618fa102009-10-14 02:06:37 -0700221 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700222{
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700223 char driver_absolute_path[PATH_MAX];
224 const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
225 const char* const search2 = "/system/lib/egl/lib%s_%s.so";
226
227 snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
Mathias Agopian8c173842009-09-20 16:01:02 -0700228 if (access(driver_absolute_path, R_OK)) {
Brian Swetland2b9e4f62010-09-20 12:58:15 -0700229 snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
230 if (access(driver_absolute_path, R_OK)) {
231 // this happens often, we don't want to log an error
232 return 0;
233 }
Mathias Agopian8c173842009-09-20 16:01:02 -0700234 }
Mathias Agopiande586972009-05-28 17:39:03 -0700235
Mathias Agopian8c173842009-09-20 16:01:02 -0700236 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
237 if (dso == 0) {
238 const char* err = dlerror();
239 LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
240 return 0;
241 }
242
243 LOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700244
Mathias Agopiande586972009-05-28 17:39:03 -0700245 if (mask & EGL) {
246 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
247
248 LOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700249 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700250
Mathias Agopian618fa102009-10-14 02:06:37 -0700251 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700252 __eglMustCastToProperFunctionPointerType* curr =
253 (__eglMustCastToProperFunctionPointerType*)egl;
254 char const * const * api = egl_names;
255 while (*api) {
256 char const * name = *api;
257 __eglMustCastToProperFunctionPointerType f =
258 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
259 if (f == NULL) {
260 // couldn't find the entry-point, use eglGetProcAddress()
261 f = getProcAddress(name);
262 if (f == NULL) {
263 f = (__eglMustCastToProperFunctionPointerType)0;
264 }
265 }
266 *curr++ = f;
267 api++;
268 }
269 }
270
271 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700272 init_api(dso, gl_names,
273 (__eglMustCastToProperFunctionPointerType*)
274 &cnx->hooks[GLESv1_INDEX]->gl,
275 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700276 }
277
278 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700279 init_api(dso, gl_names,
280 (__eglMustCastToProperFunctionPointerType*)
281 &cnx->hooks[GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700282 getProcAddress);
283 }
284
285 return dso;
286}
287
288// ----------------------------------------------------------------------------
289}; // namespace android
290// ----------------------------------------------------------------------------