Loader.cpp revision 9d4536835248525f32f1504a3d28d5bbfa0a2910
1de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian/*
2de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** Copyright 2007, The Android Open Source Project
3de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian **
4de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** Licensed under the Apache License, Version 2.0 (the "License");
5de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** you may not use this file except in compliance with the License.
6de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** You may obtain a copy of the License at
7de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian **
8de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian **     http://www.apache.org/licenses/LICENSE-2.0
9de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian **
10de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** Unless required by applicable law or agreed to in writing, software
11de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** distributed under the License is distributed on an "AS IS" BASIS,
12de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** See the License for the specific language governing permissions and
14de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian ** limitations under the License.
15de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian */
16de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
17de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <ctype.h>
18de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <stdlib.h>
19de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <stdio.h>
20de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <string.h>
21de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <errno.h>
22de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <dlfcn.h>
23de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <limits.h>
24de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
25de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <cutils/log.h>
2680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner#include <cutils/properties.h>
27de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
28de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <EGL/egl.h>
29de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
301cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian#include "egldefs.h"
310469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy#include "glestrace.h"
32de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include "hooks.h"
33de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include "Loader.h"
34de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
35de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
36de58697644a52a614ad9498aa087e95d4a223673Mathias Agopiannamespace android {
37de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
38de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
39de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
40de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian/*
41de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian * EGL drivers are called
42de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian *
43de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian * /system/lib/egl/lib{[EGL|GLESv1_CM|GLESv2] | GLES}_$TAG.so
44de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian *
45de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian */
46de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
47de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianANDROID_SINGLETON_STATIC_INSTANCE( Loader )
48de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
4980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner/* This function is called to check whether we run inside the emulator,
5080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner * and if this is the case whether GLES GPU emulation is supported.
5180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *
5280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner * Returned values are:
5380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *  -1   -> not running inside the emulator
5480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *   0   -> running inside the emulator, but GPU emulation not supported
5580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *   1   -> running inside the emulator, GPU emulation is supported
5680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *          through the "emulation" config.
5780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner */
5880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turnerstatic int
5980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' TurnercheckGlesEmulationStatus(void)
6080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner{
6180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* We're going to check for the following kernel parameters:
6280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *
6380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *    qemu=1                      -> tells us that we run inside the emulator
6480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *    android.qemu.gles=<number>  -> tells us the GLES GPU emulation status
6580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *
6680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     * Note that we will return <number> if we find it. This let us support
6780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     * more additionnal emulation modes in the future.
6880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     */
6980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    char  prop[PROPERTY_VALUE_MAX];
7080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    int   result = -1;
7180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
7280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* First, check for qemu=1 */
7380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    property_get("ro.kernel.qemu",prop,"0");
7480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    if (atoi(prop) != 1)
7580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        return -1;
7680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
7780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* We are in the emulator, get GPU status value */
7880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    property_get("ro.kernel.qemu.gles",prop,"0");
7980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    return atoi(prop);
8080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner}
8180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
82de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
83de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
84de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::driver_t(void* gles)
85de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
86de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    dso[0] = gles;
87de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=1 ; i<NELEM(dso) ; i++)
88de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        dso[i] = 0;
89de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
90de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
91de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::~driver_t()
92de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
93de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=0 ; i<NELEM(dso) ; i++) {
94de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso[i]) {
95de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dlclose(dso[i]);
96de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[i] = 0;
97de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
98de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
99de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
100de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
101de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::driver_t::set(void* hnd, int32_t api)
102de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
103de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    switch (api) {
104de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case EGL:
105de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[0] = hnd;
106de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
107de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv1_CM:
108de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[1] = hnd;
109de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
110de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv2:
111de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[2] = hnd;
112de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
113de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        default:
114de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            return BAD_INDEX;
115de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
116de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
117de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
118de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
119de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
120de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
121de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::entry_t::entry_t(int dpy, int impl, const char* tag)
122de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    : dpy(dpy), impl(impl), tag(tag) {
123de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
124de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
125de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
126de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
127de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::Loader()
128de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
129de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char line[256];
130de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char tag[256];
13180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
13280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Special case for GLES emulation */
13380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    if (checkGlesEmulationStatus() == 0) {
1349d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD("Emulator without GPU support detected. Fallback to software renderer.");
13580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        gConfig.add( entry_t(0, 0, "android") );
13680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        return;
13780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    }
13880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
13980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Otherwise, use egl.cfg */
140de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
141de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (cfg == NULL) {
142de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        // default config
1439d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD("egl.cfg not found, using default config");
144de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        gConfig.add( entry_t(0, 0, "android") );
145de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    } else {
146de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (fgets(line, 256, cfg)) {
147de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            int dpy;
148de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            int impl;
149de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
1509d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD(">>> %u %u %s", dpy, impl, tag);
151de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                gConfig.add( entry_t(dpy, impl, tag) );
152de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
153de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
154de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        fclose(cfg);
155de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
156de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
157de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
158de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::~Loader()
159de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
1600469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    GLTrace_stop();
161de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
162de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
163de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianconst char* Loader::getTag(int dpy, int impl)
164de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
165de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    const Vector<entry_t>& cfgs(gConfig);
166de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    const size_t c = cfgs.size();
167de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=0 ; i<c ; i++) {
168de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dpy == cfgs[i].dpy)
169de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (impl == cfgs[i].impl)
170de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                return cfgs[i].tag.string();
171de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
172de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return 0;
173de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
174de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
175618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopianvoid* Loader::open(EGLNativeDisplayType display, int impl, egl_connection_t* cnx)
176de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
177de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    /*
178de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian     * TODO: if we don't find display/0, then use 0/0
179de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian     * (0/0 should always work)
180de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian     */
181de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
182de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    void* dso;
183de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    int index = int(display);
184de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = 0;
185de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
186de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char const* tag = getTag(index, impl);
187de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (tag) {
1882b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
189de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso) {
190de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            hnd = new driver_t(dso);
191de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        } else {
192de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Always load EGL first
1932b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            dso = load_driver("EGL", tag, cnx, EGL);
194de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (dso) {
195de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                hnd = new driver_t(dso);
196de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
197de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // TODO: make this more automated
1982b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland                hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
199de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2002b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland                hnd->set( load_driver("GLESv2", tag, cnx, GLESv2), GLESv2 );
201de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
202de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
203de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
204de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
205de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    LOG_FATAL_IF(!index && !impl && !hnd,
206acdebe352dc7dadc1122a87d45137fd2441b329aMathias Agopian            "couldn't find the default OpenGL ES implementation "
207de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            "for default display");
208de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
209de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return (void*)hnd;
210de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
211de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
212de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::close(void* driver)
213de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
214de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = (driver_t*)driver;
215de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    delete hnd;
216de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
217de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
218de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
219de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianvoid Loader::init_api(void* dso,
220de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api,
221de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr,
222de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddressType getProcAddress)
223de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2240ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    const size_t SIZE = 256;
2250ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    char scrap[SIZE];
226de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    while (*api) {
227de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * name = *api;
228de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType f =
229de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
230de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
231de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // couldn't find the entry-point, use eglGetProcAddress()
232de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = getProcAddress(name);
233de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
234de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
235de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try without the OES postfix
236de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2370ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
238de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                strncpy(scrap, name, index);
239de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                scrap[index] = 0;
240de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2419d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
242de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
243de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
244de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
245de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try with the OES postfix
2460ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2470ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if (index>0 && strcmp(name+index, "OES")) {
2480ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian                snprintf(scrap, SIZE, "%sOES", name);
249de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2509d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
251de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
252de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
253de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
2549d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block            //ALOGD("%s", name);
255de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
256de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
257de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        *curr++ = f;
258de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        api++;
259de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
260de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
261de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2622b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetlandvoid *Loader::load_driver(const char* kind, const char *tag,
263618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_connection_t* cnx, uint32_t mask)
264de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2652b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    char driver_absolute_path[PATH_MAX];
2662b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
2672b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search2 = "/system/lib/egl/lib%s_%s.so";
2682b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland
2692b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
2708c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (access(driver_absolute_path, R_OK)) {
2712b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
2722b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        if (access(driver_absolute_path, R_OK)) {
2732b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            // this happens often, we don't want to log an error
2742b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            return 0;
2752b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        }
2768c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
2778c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian
2788c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
2798c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (dso == 0) {
2808c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        const char* err = dlerror();
2818c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        LOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
2828c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        return 0;
2838c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
284de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2859d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block    ALOGD("loaded %s", driver_absolute_path);
286baca89c06a40c6c19ae2294fb4263d893126320cMathias Agopian
287de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & EGL) {
288de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
289de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
290de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        LOGE_IF(!getProcAddress,
2918c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian                "can't find eglGetProcAddress() in %s", driver_absolute_path);
292de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
293618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_t* egl = &cnx->egl;
294de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr =
295de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType*)egl;
296de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api = egl_names;
297de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (*api) {
298de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            char const * name = *api;
299de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            __eglMustCastToProperFunctionPointerType f =
300de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
301de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (f == NULL) {
302de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // couldn't find the entry-point, use eglGetProcAddress()
303de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = getProcAddress(name);
304de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                if (f == NULL) {
305de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                    f = (__eglMustCastToProperFunctionPointerType)0;
306de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                }
307de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
308de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            *curr++ = f;
309de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            api++;
310de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
311de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
312de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
313de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv1_CM) {
314618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        init_api(dso, gl_names,
315618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
316618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian                &cnx->hooks[GLESv1_INDEX]->gl,
317618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            getProcAddress);
318de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
319de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
320de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv2) {
321618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian      init_api(dso, gl_names,
322618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
323618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian                &cnx->hooks[GLESv2_INDEX]->gl,
324de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            getProcAddress);
325de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
326de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
327de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return dso;
328de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
329de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
330de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
331de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}; // namespace android
332de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
333