Loader.cpp revision baca89c06a40c6c19ae2294fb4263d893126320c
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}
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
131void* Loader::open(EGLNativeDisplayType display, int impl, gl_hooks_t* hooks)
132{
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;
142    const char* const format = "/system/lib/egl/lib%s_%s.so";
143
144    char const* tag = getTag(index, impl);
145    if (tag) {
146        snprintf(path, PATH_MAX, format, "GLES", tag);
147        dso = load_driver(path, hooks, EGL | GLESv1_CM | GLESv2);
148        if (dso) {
149            hnd = new driver_t(dso);
150        } else {
151            // Always load EGL first
152            snprintf(path, PATH_MAX, format, "EGL", tag);
153            dso = load_driver(path, hooks, EGL);
154            if (dso) {
155                hnd = new driver_t(dso);
156
157                // TODO: make this more automated
158                snprintf(path, PATH_MAX, format, "GLESv1_CM", tag);
159                hnd->set( load_driver(path, hooks, GLESv1_CM), GLESv1_CM );
160
161                snprintf(path, PATH_MAX, format, "GLESv2", tag);
162                hnd->set( load_driver(path, hooks, GLESv2), GLESv2 );
163            }
164        }
165    }
166
167    LOG_FATAL_IF(!index && !impl && !hnd,
168            "couldn't find the default OpenGL ES implementation "
169            "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
225void *Loader::load_driver(const char* driver, gl_hooks_t* hooks, uint32_t mask)
226{
227    void* dso = dlopen(driver, RTLD_NOW | RTLD_LOCAL);
228    if (dso == 0)
229        return 0;
230
231    LOGD("loaded %s", driver);
232
233    if (mask & EGL) {
234        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
235
236        LOGE_IF(!getProcAddress,
237                "can't find eglGetProcAddress() in %s", driver);
238
239        gl_hooks_t::egl_t* egl = &hooks->egl;
240        __eglMustCastToProperFunctionPointerType* curr =
241            (__eglMustCastToProperFunctionPointerType*)egl;
242        char const * const * api = egl_names;
243        while (*api) {
244            char const * name = *api;
245            __eglMustCastToProperFunctionPointerType f =
246                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
247            if (f == NULL) {
248                // couldn't find the entry-point, use eglGetProcAddress()
249                f = getProcAddress(name);
250                if (f == NULL) {
251                    f = (__eglMustCastToProperFunctionPointerType)0;
252                }
253            }
254            *curr++ = f;
255            api++;
256        }
257    }
258
259    if (mask & GLESv1_CM) {
260        init_api(dso, gl_names,
261                (__eglMustCastToProperFunctionPointerType*)&hooks->gl,
262                getProcAddress);
263    }
264
265    if (mask & GLESv2) {
266      init_api(dso, gl2_names,
267            (__eglMustCastToProperFunctionPointerType*)&hooks->gl2,
268            getProcAddress);
269    }
270
271    return dso;
272}
273
274// ----------------------------------------------------------------------------
275}; // namespace android
276// ----------------------------------------------------------------------------
277