Loader.cpp revision ada798b7ca7cabc255aa159964b64975e7fdb2df
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::Loader()
122de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
123de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char line[256];
124de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char tag[256];
12580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
12680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Special case for GLES emulation */
12780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    if (checkGlesEmulationStatus() == 0) {
128ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        ALOGD("Emulator without GPU support detected. "
129ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian              "Fallback to software renderer.");
130ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        mDriverTag.setTo("android");
13180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        return;
13280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    }
13380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
13480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Otherwise, use egl.cfg */
135de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
136de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (cfg == NULL) {
137de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        // default config
1389d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD("egl.cfg not found, using default config");
139ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        mDriverTag.setTo("android");
140de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    } else {
141de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (fgets(line, 256, cfg)) {
142ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            int dpy, impl;
143de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
1449d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD(">>> %u %u %s", dpy, impl, tag);
145ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                // We only load the h/w accelerated implementation
146ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                if (tag != String8("android")) {
147ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                    mDriverTag = tag;
148ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                }
149de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
150de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
151de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        fclose(cfg);
152de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
153de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
154de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
155de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::~Loader()
156de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
1570469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    GLTrace_stop();
158de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
159de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
160ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopianvoid* Loader::open(egl_connection_t* cnx)
161de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
162de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    void* dso;
163de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = 0;
164de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
165ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    char const* tag = mDriverTag.string();
166de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (tag) {
1672b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
168de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso) {
169de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            hnd = new driver_t(dso);
170de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        } else {
171de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Always load EGL first
1722b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            dso = load_driver("EGL", tag, cnx, EGL);
173de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (dso) {
174de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                hnd = new driver_t(dso);
175de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // TODO: make this more automated
1762b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland                hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
177ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                hnd->set( load_driver("GLESv2",    tag, cnx, GLESv2),    GLESv2 );
178de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
179de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
180de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
181de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
182ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    LOG_FATAL_IF(!index && !hnd,
183acdebe352dc7dadc1122a87d45137fd2441b329aMathias Agopian            "couldn't find the default OpenGL ES implementation "
184de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            "for default display");
185de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
186de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return (void*)hnd;
187de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
188de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
189de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::close(void* driver)
190de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
191de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = (driver_t*)driver;
192de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    delete hnd;
193de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
194de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
195de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
196de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianvoid Loader::init_api(void* dso,
197de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api,
198de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr,
199de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddressType getProcAddress)
200de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2010ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    const size_t SIZE = 256;
2020ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    char scrap[SIZE];
203de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    while (*api) {
204de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * name = *api;
205de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType f =
206de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
207de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
208de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // couldn't find the entry-point, use eglGetProcAddress()
209de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = getProcAddress(name);
210de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
211de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
212de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try without the OES postfix
213de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2140ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
215de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                strncpy(scrap, name, index);
216de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                scrap[index] = 0;
217de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2189d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
219de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
220de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
221de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
222de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try with the OES postfix
2230ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2240ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if (index>0 && strcmp(name+index, "OES")) {
2250ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian                snprintf(scrap, SIZE, "%sOES", name);
226de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2279d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
228de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
229de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
230de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
2319d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block            //ALOGD("%s", name);
232de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
23348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
23448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            /*
23548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * GL_EXT_debug_label is special, we always report it as
23648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * supported, it's handled by GLES_trace. If GLES_trace is not
23748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * enabled, then these are no-ops.
23848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             */
23948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            if (!strcmp(name, "glInsertEventMarkerEXT")) {
24048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
24148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
24248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
24348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
24448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
24548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            }
246de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
247de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        *curr++ = f;
248de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        api++;
249de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
250de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
251de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2522b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetlandvoid *Loader::load_driver(const char* kind, const char *tag,
253618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_connection_t* cnx, uint32_t mask)
254de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2552b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    char driver_absolute_path[PATH_MAX];
2562b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
2572b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search2 = "/system/lib/egl/lib%s_%s.so";
2582b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland
2592b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
2608c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (access(driver_absolute_path, R_OK)) {
2612b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
2622b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        if (access(driver_absolute_path, R_OK)) {
2632b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            // this happens often, we don't want to log an error
2642b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            return 0;
2652b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        }
2668c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
2678c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian
2688c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
2698c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (dso == 0) {
2708c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        const char* err = dlerror();
271e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
2728c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        return 0;
2738c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
274de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2759d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block    ALOGD("loaded %s", driver_absolute_path);
276baca89c06a40c6c19ae2294fb4263d893126320cMathias Agopian
277de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & EGL) {
278de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
279de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
280e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(!getProcAddress,
2818c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian                "can't find eglGetProcAddress() in %s", driver_absolute_path);
282de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
283618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_t* egl = &cnx->egl;
284de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr =
285de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType*)egl;
286de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api = egl_names;
287de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (*api) {
288de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            char const * name = *api;
289de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            __eglMustCastToProperFunctionPointerType f =
290de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
291de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (f == NULL) {
292de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // couldn't find the entry-point, use eglGetProcAddress()
293de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = getProcAddress(name);
294de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                if (f == NULL) {
295de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                    f = (__eglMustCastToProperFunctionPointerType)0;
296de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                }
297de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
298de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            *curr++ = f;
299de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            api++;
300de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
301de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
302de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
303de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv1_CM) {
304618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        init_api(dso, gl_names,
305618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
306618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian                &cnx->hooks[GLESv1_INDEX]->gl,
307618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            getProcAddress);
308de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
309de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
310de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv2) {
311618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian      init_api(dso, gl_names,
312618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
313618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian                &cnx->hooks[GLESv2_INDEX]->gl,
314de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            getProcAddress);
315de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
316de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
317de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return dso;
318de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
319de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
320de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
321de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}; // namespace android
322de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
323