Loader.cpp revision acdebe352dc7dadc1122a87d45137fd2441b329a
1f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2robertphillips@google.com/*
2f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2robertphillips@google.com ** Copyright 2007, The Android Open Source Project
3f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2robertphillips@google.com **
4f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2robertphillips@google.com ** Licensed under the Apache License, Version 2.0 (the "License");
5f6747b0b90b3a270ec7b7bdfdc211cf5c19f28c2robertphillips@google.com ** you may not use this file except in compliance with the License.
6977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com ** You may obtain a copy of the License at
7977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com **
8977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com **     http://www.apache.org/licenses/LICENSE-2.0
9977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com **
10977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com ** Unless required by applicable law or agreed to in writing, software
11977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com ** distributed under the License is distributed on an "AS IS" BASIS,
12977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fbfcd5602128ec010c82cb733c9cdc0a3254f9f3rmistry@google.com ** See the License for the specific language governing permissions and
14977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com ** limitations under the License.
154d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com */
164d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com
174d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com#include <ctype.h>
184d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com#include <stdlib.h>
19977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com#include <stdio.h>
204e23068b374023d43c4c725138d523721d975892bsalomon@google.com#include <string.h>
21594dd3cd78e2f970d53bb0934fbbb63b41e1d40ccaryclark@google.com#include <errno.h>
224e23068b374023d43c4c725138d523721d975892bsalomon@google.com#include <dlfcn.h>
232ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org#include <limits.h>
242ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
252ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org#include <cutils/log.h>
262ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
272ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org#include <EGL/egl.h>
282ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
292ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org#include "hooks.h"
302ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org#include "egl_impl.h"
314d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com
3286b0de4745a8a8317b54f23878498633b9210a8fcommit-bot@chromium.org#include "Loader.h"
33b74af872cc1ce45768df3ae03fa86ad3ed76b582robertphillips@google.com
34b74af872cc1ce45768df3ae03fa86ad3ed76b582robertphillips@google.com// ----------------------------------------------------------------------------
3597fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.comnamespace android {
364d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com// ----------------------------------------------------------------------------
374d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com
38ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
394d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com/*
4097fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.com * EGL drivers are called
4197fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.com *
424d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
43ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org *
444d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com */
45ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
464d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.comANDROID_SINGLETON_STATIC_INSTANCE( Loader )
474d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com
484d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com// ----------------------------------------------------------------------------
492ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
501b81877880253c75f835eede9a8ee21b9e7b584amtkleinLoader::driver_t::driver_t(void* gles)
51ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org{
524d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    dso[0] = gles;
534d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    for (size_t i=1 ; i<NELEM(dso) ; i++)
541b81877880253c75f835eede9a8ee21b9e7b584amtklein        dso[i] = 0;
552ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org}
562ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
572ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.orgLoader::driver_t::~driver_t()
58e0e385c1d4171e065348ba17c546b3463a0bd651sugoi@google.com{
59ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    for (size_t i=0 ; i<NELEM(dso) ; i++) {
604d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com        if (dso[i]) {
614d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com            dlclose(dso[i]);
624d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com            dso[i] = 0;
63ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        }
64ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    }
65ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org}
66ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
67ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.orgstatus_t Loader::driver_t::set(void* hnd, int32_t api)
68ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org{
69ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    switch (api) {
70ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        case EGL:
712ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org            dso[0] = hnd;
722ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org            break;
73ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        case GLESv1_CM:
744d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com            dso[1] = hnd;
754d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com            break;
76d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman        case GLESv2:
77d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman            dso[2] = hnd;
78d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman            break;
792ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org        default:
80d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman            return BAD_INDEX;
81d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman    }
82d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman    return NO_ERROR;
83d6aeb6dc8fe21066f1a2c4813a4256a3acd3edf5bungeman}
842ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org
852ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org// ----------------------------------------------------------------------------
864d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com
874d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.comLoader::entry_t::entry_t(int dpy, int impl, const char* tag)
884da34e36cb7a07c3a28ae2a135b1837c26fc7aeabsalomon@google.com    : dpy(dpy), impl(impl), tag(tag) {
89ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org}
904da34e36cb7a07c3a28ae2a135b1837c26fc7aeabsalomon@google.com
914da34e36cb7a07c3a28ae2a135b1837c26fc7aeabsalomon@google.com// ----------------------------------------------------------------------------
9297fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.com
9323579275c7f544763d074d90563c4b1e567ab041robertphillips@google.comLoader::Loader()
9497fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.com{
9597fafe1b5ec06b470d36ea5cd98fe7bf2c143491robertphillips@google.com    char line[256];
9623579275c7f544763d074d90563c4b1e567ab041robertphillips@google.com    char tag[256];
97ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
9873e19fb1ba2562048c6987272c8d73eee6a44242reed@google.com    if (cfg == NULL) {
9915e9d3e66e161ce23df30bc13f8a0c87d196b463robertphillips@google.com        // default config
100ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        LOGD("egl.cfg not found, using default config");
1014d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com        gConfig.add( entry_t(0, 0, "android") );
102ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    } else {
103ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        while (fgets(line, 256, cfg)) {
10423579275c7f544763d074d90563c4b1e567ab041robertphillips@google.com            int dpy;
1052ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org            int impl;
106ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org            if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
107ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org                LOGD(">>> %u %u %s", dpy, impl, tag);
108ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org                gConfig.add( entry_t(dpy, impl, tag) );
10973e19fb1ba2562048c6987272c8d73eee6a44242reed@google.com            }
110ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org        }
1114d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com        fclose(cfg);
11273e19fb1ba2562048c6987272c8d73eee6a44242reed@google.com    }
113b74af872cc1ce45768df3ae03fa86ad3ed76b582robertphillips@google.com}
11473e19fb1ba2562048c6987272c8d73eee6a44242reed@google.com
11573e19fb1ba2562048c6987272c8d73eee6a44242reed@google.comLoader::~Loader()
11623579275c7f544763d074d90563c4b1e567ab041robertphillips@google.com{
117ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org}
118ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
11923579275c7f544763d074d90563c4b1e567ab041robertphillips@google.comconst char* Loader::getTag(int dpy, int impl)
120ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org{
1214d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    const Vector<entry_t>& cfgs(gConfig);
1224d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    const size_t c = cfgs.size();
123ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    for (size_t i=0 ; i<c ; i++) {
1242ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org        if (dpy == cfgs[i].dpy)
1252ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org            if (impl == cfgs[i].impl)
1262ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org                return cfgs[i].tag.string();
1272ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org    }
1282ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org    return 0;
1292ab1ba055536825552d6b49f0210c4e1531f02f0commit-bot@chromium.org}
130ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
1314d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.comvoid* Loader::open(EGLNativeDisplayType display, int impl, gl_hooks_t* hooks)
132977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com{
133977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com    /*
134977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com     * TODO: if we don't find display/0, then use 0/0
135ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org     * (0/0 should always work)
136ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org     */
137ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
138ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    void* dso;
139ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    char path[PATH_MAX];
140ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    int index = int(display);
141ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    driver_t* hnd = 0;
142ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org    const char* const format = "egl/lib%s_%s.so";
143ab1c13864df34aecfd4840ea7d1e4f8730b44f4ecommit-bot@chromium.org
1444d73ac22a1b99402fc8cff78a4eb4b27aa8fe019robertphillips@google.com    char const* tag = getTag(index, impl);
1454fa9c9f9b42c22bffff3b2eb3fcf7f16a2ab2948robertphillips@google.com    if (tag) {
146977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com        snprintf(path, PATH_MAX, format, "GLES", tag);
147977b9c8af3ef1b9a2fa2a0037cf3734cf2ba13d9robertphillips@google.com        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, "lib%s_%s.so", "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    //LOGD("%s", driver);
228    void* dso = dlopen(driver, RTLD_NOW | RTLD_LOCAL);
229    LOGE_IF(!dso, "%s", dlerror());
230    if (dso == 0)
231        return 0;
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