Loader.cpp revision d75f84d6410da934d1b1760fdc0d05d4ba1e8f35
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
84d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopianstatic char const * getProcessCmdline() {
85d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    long pid = getpid();
86d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    char procPath[128];
87d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
88d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    FILE * file = fopen(procPath, "r");
89d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    if (file) {
90d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        static char cmdline[256];
91d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
92d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        fclose(file);
93d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        if (str) {
94d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            return cmdline;
95d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        }
96d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    }
97d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    return NULL;
98d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian}
99d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
100d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian// ----------------------------------------------------------------------------
101d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
102de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::driver_t(void* gles)
103de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
104de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    dso[0] = gles;
105de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=1 ; i<NELEM(dso) ; i++)
106de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        dso[i] = 0;
107de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
108de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
109de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::~driver_t()
110de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
111de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=0 ; i<NELEM(dso) ; i++) {
112de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso[i]) {
113de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dlclose(dso[i]);
114de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[i] = 0;
115de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
116de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
117de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
118de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
119de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::driver_t::set(void* hnd, int32_t api)
120de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
121de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    switch (api) {
122de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case EGL:
123de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[0] = hnd;
124de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
125de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv1_CM:
126de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[1] = hnd;
127de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
128de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv2:
129de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[2] = hnd;
130de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
131de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        default:
132de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            return BAD_INDEX;
133de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
134de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
135de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
136de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
137de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
138de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
139de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::Loader()
140de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
141de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char line[256];
142de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    char tag[256];
14380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
14480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Special case for GLES emulation */
14580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    if (checkGlesEmulationStatus() == 0) {
146ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        ALOGD("Emulator without GPU support detected. "
147ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian              "Fallback to software renderer.");
148ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        mDriverTag.setTo("android");
14980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        return;
15080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    }
15180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
15280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* Otherwise, use egl.cfg */
153de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    FILE* cfg = fopen("/system/lib/egl/egl.cfg", "r");
154de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (cfg == NULL) {
155de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        // default config
1569d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block        ALOGD("egl.cfg not found, using default config");
157ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian        mDriverTag.setTo("android");
158de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    } else {
159de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (fgets(line, 256, cfg)) {
160ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian            int dpy, impl;
161de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (sscanf(line, "%u %u %s", &dpy, &impl, tag) == 3) {
1629d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD(">>> %u %u %s", dpy, impl, tag);
163ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                // We only load the h/w accelerated implementation
164ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                if (tag != String8("android")) {
165ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                    mDriverTag = tag;
166ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                }
167de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
168de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
169de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        fclose(cfg);
170de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
171de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
172de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
173de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::~Loader()
174de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
1750469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    GLTrace_stop();
176de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
177de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
178ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopianvoid* Loader::open(egl_connection_t* cnx)
179de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
180de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    void* dso;
181de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = 0;
182de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
183ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    char const* tag = mDriverTag.string();
184de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (tag) {
1852b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        dso = load_driver("GLES", tag, cnx, EGL | GLESv1_CM | GLESv2);
186de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso) {
187de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            hnd = new driver_t(dso);
188de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        } else {
189de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Always load EGL first
1902b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            dso = load_driver("EGL", tag, cnx, EGL);
191de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (dso) {
192de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                hnd = new driver_t(dso);
193de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // TODO: make this more automated
1942b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland                hnd->set( load_driver("GLESv1_CM", tag, cnx, GLESv1_CM), GLESv1_CM );
195ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian                hnd->set( load_driver("GLESv2",    tag, cnx, GLESv2),    GLESv2 );
196de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
197de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
198de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
199de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
200ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopian    LOG_FATAL_IF(!index && !hnd,
201acdebe352dc7dadc1122a87d45137fd2441b329aMathias Agopian            "couldn't find the default OpenGL ES implementation "
202de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            "for default display");
203de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
204de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return (void*)hnd;
205de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
206de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
207de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::close(void* driver)
208de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
209de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = (driver_t*)driver;
210de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    delete hnd;
211de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
212de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
213de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
214de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianvoid Loader::init_api(void* dso,
215de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api,
216de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr,
217de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddressType getProcAddress)
218de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2197773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    const ssize_t SIZE = 256;
2200ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    char scrap[SIZE];
221de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    while (*api) {
222de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * name = *api;
223de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType f =
224de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
225de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
226de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // couldn't find the entry-point, use eglGetProcAddress()
227de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = getProcAddress(name);
228de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
229de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
230de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try without the OES postfix
231de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2320ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
233de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                strncpy(scrap, name, index);
234de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                scrap[index] = 0;
235de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2369d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
237de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
238de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
239de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
240de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try with the OES postfix
2410ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2420ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if (index>0 && strcmp(name+index, "OES")) {
2430ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian                snprintf(scrap, SIZE, "%sOES", name);
244de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2459d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
246de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
247de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
248de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
2499d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block            //ALOGD("%s", name);
250de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
25148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
25248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            /*
25348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * GL_EXT_debug_label is special, we always report it as
25448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * supported, it's handled by GLES_trace. If GLES_trace is not
25548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * enabled, then these are no-ops.
25648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             */
25748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            if (!strcmp(name, "glInsertEventMarkerEXT")) {
25848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
25948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
26048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
26148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
26248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
26348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            }
264de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
265de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        *curr++ = f;
266de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        api++;
267de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
268de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
269de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2702b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetlandvoid *Loader::load_driver(const char* kind, const char *tag,
271618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_connection_t* cnx, uint32_t mask)
272de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
2732b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    char driver_absolute_path[PATH_MAX];
2742b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search1 = "/vendor/lib/egl/lib%s_%s.so";
2752b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    const char* const search2 = "/system/lib/egl/lib%s_%s.so";
2762b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland
2772b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland    snprintf(driver_absolute_path, PATH_MAX, search1, kind, tag);
2788c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (access(driver_absolute_path, R_OK)) {
2792b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        snprintf(driver_absolute_path, PATH_MAX, search2, kind, tag);
2802b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        if (access(driver_absolute_path, R_OK)) {
2812b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            // this happens often, we don't want to log an error
2822b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland            return 0;
2832b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        }
2848c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
2858c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian
2868c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
2878c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (dso == 0) {
2888c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        const char* err = dlerror();
289e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
2908c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        return 0;
2918c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
292de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
2939d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block    ALOGD("loaded %s", driver_absolute_path);
294baca89c06a40c6c19ae2294fb4263d893126320cMathias Agopian
295de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & EGL) {
296de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
297de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
298e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(!getProcAddress,
2998c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian                "can't find eglGetProcAddress() in %s", driver_absolute_path);
300de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
301d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#ifdef SYSTEMUI_PBSIZE_HACK
302d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#warning "SYSTEMUI_PBSIZE_HACK enabled"
303d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        /*
304d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * TODO: replace SYSTEMUI_PBSIZE_HACK by something less hackish
305d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         *
306d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * Here we adjust the PB size from its default value to 512KB which
307d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * is the minimum acceptable for the systemui process.
308d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * We do this on low-end devices only because it allows us to enable
309d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * h/w acceleration in the systemui process while keeping the
310d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * memory usage down.
311d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         *
312d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * Obviously, this is the wrong place and wrong way to make this
313d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * adjustment, but at the time of this writing this was the safest
314d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * solution.
315d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         */
316d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        const char *cmdline = getProcessCmdline();
317d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        if (strstr(cmdline, "systemui")) {
318d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            void *imgegl = dlopen("/vendor/lib/libIMGegl.so", RTLD_LAZY);
319d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            if (imgegl) {
320d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                unsigned int *PVRDefaultPBS =
321d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                        (unsigned int *)dlsym(imgegl, "PVRDefaultPBS");
322d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                if (PVRDefaultPBS) {
323d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                    ALOGD("setting default PBS to 512KB, was %d KB", *PVRDefaultPBS / 1024);
324d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                    *PVRDefaultPBS = 512*1024;
325d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                }
326d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            }
327d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        }
328d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#endif
329d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
330618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_t* egl = &cnx->egl;
331de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr =
332de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType*)egl;
333de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api = egl_names;
334de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (*api) {
335de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            char const * name = *api;
336de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            __eglMustCastToProperFunctionPointerType f =
337de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
338de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (f == NULL) {
339de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // couldn't find the entry-point, use eglGetProcAddress()
340de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = getProcAddress(name);
341de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                if (f == NULL) {
342de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                    f = (__eglMustCastToProperFunctionPointerType)0;
343de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                }
344de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
345de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            *curr++ = f;
346de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            api++;
347de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
348de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
349de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
350de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv1_CM) {
351618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        init_api(dso, gl_names,
352618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
3537773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
354618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            getProcAddress);
355de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
356de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
357de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv2) {
358618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian      init_api(dso, gl_names,
359618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
3607773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
361de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            getProcAddress);
362de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
363de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
364de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return dso;
365de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
366de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
367de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
368de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}; // namespace android
369de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
370