blob: 5d6ac26a99b52fc5ac3c80a5d45c8e58cf8f44ea [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"
33
34// ----------------------------------------------------------------------------
35namespace android {
36// ----------------------------------------------------------------------------
37
38
39/*
40 * EGL drivers are called
41 *
42 * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
43 *
44 */
45
46ANDROID_SINGLETON_STATIC_INSTANCE( Loader )
47
48// ----------------------------------------------------------------------------
49
50Loader::driver_t::driver_t(void* gles)
51{
52 dso[0] = gles;
53 for (size_t i=1 ; i<NELEM(dso) ; i++)
54 dso[i] = 0;
55}
56
57Loader::driver_t::~driver_t()
58{
59 for (size_t i=0 ; i<NELEM(dso) ; i++) {
60 if (dso[i]) {
61 dlclose(dso[i]);
62 dso[i] = 0;
63 }
64 }
65}
66
67status_t Loader::driver_t::set(void* hnd, int32_t api)
68{
69 switch (api) {
70 case EGL:
71 dso[0] = hnd;
72 break;
73 case GLESv1_CM:
74 dso[1] = hnd;
75 break;
76 case GLESv2:
77 dso[2] = hnd;
78 break;
79 default:
80 return BAD_INDEX;
81 }
82 return NO_ERROR;
83}
84
85// ----------------------------------------------------------------------------
86
87Loader::entry_t::entry_t(int dpy, int impl, const char* tag)
88 : dpy(dpy), impl(impl), tag(tag) {
89}
90
91// ----------------------------------------------------------------------------
92
93Loader::Loader()
94{
95 char line[256];
96 char tag[256];
97 FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
98 if (cfg == NULL) {
99 // default config
100 LOGD("egl.cfg not found, using default config");
101 gConfig.add( entry_t(0, 0, "android") );
102 } else {
103 while (fgets(line, 256, cfg)) {
104 int dpy;
105 int impl;
106 if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
Mathias Agopianf909cb62009-06-03 18:30:22 -0700107 //LOGD(">>> %u %u %s", dpy, impl, tag);
Mathias Agopiande586972009-05-28 17:39:03 -0700108 gConfig.add( entry_t(dpy, impl, tag) );
109 }
110 }
111 fclose(cfg);
112 }
113}
114
115Loader::~Loader()
116{
117}
118
119const char* Loader::getTag(int dpy, int impl)
120{
121 const Vector<entry_t>& cfgs(gConfig);
122 const size_t c = cfgs.size();
123 for (size_t i=0 ; i<c ; i++) {
124 if (dpy == cfgs[i].dpy)
125 if (impl == cfgs[i].impl)
126 return cfgs[i].tag.string();
127 }
128 return 0;
129}
130
Mathias Agopian618fa102009-10-14 02:06:37 -0700131void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
Mathias Agopiande586972009-05-28 17:39:03 -0700132{
133 /*
134 * TODO: if we don't find display/0, then use 0/0
135 * (0/0 should always work)
136 */
137
138 void* dso;
139 char path[PATH_MAX];
140 int index = int(display);
141 driver_t* hnd = 0;
Marco Nelissenfc865652009-07-07 16:18:18 -0700142 const char* const format = "/system/lib/egl/lib%s_%s.so";
Mathias Agopiande586972009-05-28 17:39:03 -0700143
144 char const* tag = getTag(index, impl);
145 if (tag) {
146 snprintf(path, PATH_MAX, format, "GLES", tag);
Mathias Agopian618fa102009-10-14 02:06:37 -0700147 dso = load_driver(path, cnx, EGL | GLESv1_CM | GLESv2);
Mathias Agopiande586972009-05-28 17:39:03 -0700148 if (dso) {
149 hnd = new driver_t(dso);
150 } else {
151 // Always load EGL first
Mathias Agopian574c16f2009-07-29 11:10:29 -0700152 snprintf(path, PATH_MAX, format, "EGL", tag);
Mathias Agopian618fa102009-10-14 02:06:37 -0700153 dso = load_driver(path, cnx, EGL);
Mathias Agopiande586972009-05-28 17:39:03 -0700154 if (dso) {
155 hnd = new driver_t(dso);
156
157 // TODO: make this more automated
158 snprintf(path, PATH_MAX, format, "GLESv1_CM", tag);
Mathias Agopian618fa102009-10-14 02:06:37 -0700159 hnd->set( load_driver(path, cnx, GLESv1_CM), GLESv1_CM );
Mathias Agopiande586972009-05-28 17:39:03 -0700160
161 snprintf(path, PATH_MAX, format, "GLESv2", tag);
Mathias Agopian618fa102009-10-14 02:06:37 -0700162 hnd->set( load_driver(path, cnx, GLESv2), GLESv2 );
Mathias Agopiande586972009-05-28 17:39:03 -0700163 }
164 }
165 }
166
167 LOG_FATAL_IF(!index && !impl && !hnd,
Mathias Agopianacdebe32009-06-03 18:26:58 -0700168 "couldn't find the default OpenGL ES implementation "
Mathias Agopiande586972009-05-28 17:39:03 -0700169 "for default display");
170
171 return (void*)hnd;
172}
173
174status_t Loader::close(void* driver)
175{
176 driver_t* hnd = (driver_t*)driver;
177 delete hnd;
178 return NO_ERROR;
179}
180
181void Loader::init_api(void* dso,
182 char const * const * api,
183 __eglMustCastToProperFunctionPointerType* curr,
184 getProcAddressType getProcAddress)
185{
186 char scrap[256];
187 while (*api) {
188 char const * name = *api;
189 __eglMustCastToProperFunctionPointerType f =
190 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
191 if (f == NULL) {
192 // couldn't find the entry-point, use eglGetProcAddress()
193 f = getProcAddress(name);
194 }
195 if (f == NULL) {
196 // Try without the OES postfix
197 ssize_t index = ssize_t(strlen(name)) - 3;
198 if ((index>0 && (index<255)) && (!strcmp(name+index, "OES"))) {
199 strncpy(scrap, name, index);
200 scrap[index] = 0;
201 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
202 //LOGD_IF(f, "found <%s> instead", scrap);
203 }
204 }
205 if (f == NULL) {
206 // Try with the OES postfix
207 ssize_t index = ssize_t(strlen(name)) - 3;
208 if ((index>0 && (index<252)) && (strcmp(name+index, "OES"))) {
209 strncpy(scrap, name, index);
210 scrap[index] = 0;
211 strcat(scrap, "OES");
212 f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
213 //LOGD_IF(f, "found <%s> instead", scrap);
214 }
215 }
216 if (f == NULL) {
217 //LOGD("%s", name);
218 f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
219 }
220 *curr++ = f;
221 api++;
222 }
223}
224
Mathias Agopian8c173842009-09-20 16:01:02 -0700225void *Loader::load_driver(const char* driver_absolute_path,
Mathias Agopian618fa102009-10-14 02:06:37 -0700226 egl_connection_t* cnx, uint32_t mask)
Mathias Agopiande586972009-05-28 17:39:03 -0700227{
Mathias Agopian8c173842009-09-20 16:01:02 -0700228 if (access(driver_absolute_path, R_OK)) {
229 // this happens often, we don't want to log an error
Mathias Agopiande586972009-05-28 17:39:03 -0700230 return 0;
Mathias Agopian8c173842009-09-20 16:01:02 -0700231 }
Mathias Agopiande586972009-05-28 17:39:03 -0700232
Mathias Agopian8c173842009-09-20 16:01:02 -0700233 void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
234 if (dso == 0) {
235 const char* err = dlerror();
236 LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
237 return 0;
238 }
239
240 LOGD("loaded %s", driver_absolute_path);
Mathias Agopianbaca89c2009-08-20 19:09:34 -0700241
Mathias Agopiande586972009-05-28 17:39:03 -0700242 if (mask & EGL) {
243 getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
244
245 LOGE_IF(!getProcAddress,
Mathias Agopian8c173842009-09-20 16:01:02 -0700246 "can't find eglGetProcAddress() in %s", driver_absolute_path);
Mathias Agopiande586972009-05-28 17:39:03 -0700247
Mathias Agopian618fa102009-10-14 02:06:37 -0700248 egl_t* egl = &cnx->egl;
Mathias Agopiande586972009-05-28 17:39:03 -0700249 __eglMustCastToProperFunctionPointerType* curr =
250 (__eglMustCastToProperFunctionPointerType*)egl;
251 char const * const * api = egl_names;
252 while (*api) {
253 char const * name = *api;
254 __eglMustCastToProperFunctionPointerType f =
255 (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
256 if (f == NULL) {
257 // couldn't find the entry-point, use eglGetProcAddress()
258 f = getProcAddress(name);
259 if (f == NULL) {
260 f = (__eglMustCastToProperFunctionPointerType)0;
261 }
262 }
263 *curr++ = f;
264 api++;
265 }
266 }
267
268 if (mask & GLESv1_CM) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700269 init_api(dso, gl_names,
270 (__eglMustCastToProperFunctionPointerType*)
271 &cnx->hooks[GLESv1_INDEX]->gl,
272 getProcAddress);
Mathias Agopiande586972009-05-28 17:39:03 -0700273 }
274
275 if (mask & GLESv2) {
Mathias Agopian618fa102009-10-14 02:06:37 -0700276 init_api(dso, gl_names,
277 (__eglMustCastToProperFunctionPointerType*)
278 &cnx->hooks[GLESv2_INDEX]->gl,
Mathias Agopiande586972009-05-28 17:39:03 -0700279 getProcAddress);
280 }
281
282 return dso;
283}
284
285// ----------------------------------------------------------------------------
286}; // namespace android
287// ----------------------------------------------------------------------------