Loader.cpp revision 2f5a6557ef6a7b9fd33077cfd8a037904d41e3bd
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 <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) {
107                //LOGD(">>> %u %u %s", dpy, impl, tag);
108                gConfig.add( entry_t(dpy, impl, tag) );
109            }
110        }
111        fclose(cfg);
112    }
113}
114
115Loader::~Loader()
116{
117    extern void StopDebugServer();
118    StopDebugServer();
119}
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
133void* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
134{
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;
141    int index = int(display);
142    driver_t* hnd = 0;
143
144    char const* tag = getTag(index, impl);
145    if (tag) {
146        dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
147        if (dso) {
148            hnd = new driver_t(dso);
149        } else {
150            // Always load EGL first
151            dso = load_driver("EGL", tag, cnx, EGL);
152            if (dso) {
153                hnd = new driver_t(dso);
154
155                // TODO: make this more automated
156                hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
157
158                hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
159            }
160        }
161    }
162
163    LOG_FATAL_IF(!index && !impl && !hnd,
164            "couldn't find the default OpenGL ES implementation "
165            "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{
182    char scrap[256];
183    while (*api) {
184        char const * name = *api;
185        __eglMustCastToProperFunctionPointerType f =
186            (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
187        if (f == NULL) {
188            // couldn't find the entry-point, use eglGetProcAddress()
189            f = getProcAddress(name);
190        }
191        if (f == NULL) {
192            // Try without the OES postfix
193            ssize_t index = ssize_t(strlen(name)) - 3;
194            if ((index>0 && (index<255)) && (!strcmp(name+index, "OES"))) {
195                strncpy(scrap, name, index);
196                scrap[index] = 0;
197                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
198                //LOGD_IF(f, "found <%s> instead", scrap);
199            }
200        }
201        if (f == NULL) {
202            // Try with the OES postfix
203            ssize_t index = ssize_t(strlen(name)) - 3;
204            if ((index>0 && (index<252)) && (strcmp(name+index, "OES"))) {
205                strncpy(scrap, name, index);
206                scrap[index] = 0;
207                strcat(scrap, "OES");
208                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
209                //LOGD_IF(f, "found <%s> instead", scrap);
210            }
211        }
212        if (f == NULL) {
213            //LOGD("%s", name);
214            f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
215        }
216        *curr++ = f;
217        api++;
218    }
219}
220
221void *Loader::load_driver(const char* kind, const char *tag,
222        egl_connection_t* cnx, uint32_t mask)
223{
224    char driver_absolute_path[PATH_MAX];
225    const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
226    const char* const search2 = "/system/lib/egl/lib%s_%s.so";
227
228    snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
229    if (access(driver_absolute_path, R_OK)) {
230        snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
231        if (access(driver_absolute_path, R_OK)) {
232            // this happens often, we don't want to log an error
233            return 0;
234        }
235    }
236
237    void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
238    if (dso == 0) {
239        const char* err = dlerror();
240        LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
241        return 0;
242    }
243
244    LOGD("loaded %s", driver_absolute_path);
245
246    if (mask & EGL) {
247        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
248
249        LOGE_IF(!getProcAddress,
250                "can't find eglGetProcAddress() in %s", driver_absolute_path);
251
252        egl_t* egl = &cnx->egl;
253        __eglMustCastToProperFunctionPointerType* curr =
254            (__eglMustCastToProperFunctionPointerType*)egl;
255        char const * const * api = egl_names;
256        while (*api) {
257            char const * name = *api;
258            __eglMustCastToProperFunctionPointerType f =
259                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
260            if (f == NULL) {
261                // couldn't find the entry-point, use eglGetProcAddress()
262                f = getProcAddress(name);
263                if (f == NULL) {
264                    f = (__eglMustCastToProperFunctionPointerType)0;
265                }
266            }
267            *curr++ = f;
268            api++;
269        }
270    }
271
272    if (mask & GLESv1_CM) {
273        init_api(dso, gl_names,
274            (__eglMustCastToProperFunctionPointerType*)
275                &cnx->hooks[GLESv1_INDEX]->gl,
276            getProcAddress);
277    }
278
279    if (mask & GLESv2) {
280      init_api(dso, gl_names,
281            (__eglMustCastToProperFunctionPointerType*)
282                &cnx->hooks[GLESv2_INDEX]->gl,
283            getProcAddress);
284    }
285
286    return dso;
287}
288
289// ----------------------------------------------------------------------------
290}; // namespace android
291// ----------------------------------------------------------------------------
292