Loader.cpp revision 993814255fc248454368ed9fe34b4703a05eaf99
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>
24993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian#include <dirent.h>
25de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
26de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <cutils/log.h>
2780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner#include <cutils/properties.h>
28de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
29de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include <EGL/egl.h>
30de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
3139c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian#include "../glestrace.h"
3239c24a20bbc697630d2b92c251b70c04d6f9d00cMathias Agopian
331cadb25da1ed875bdd078270e642966724a0c39aMathias Agopian#include "egldefs.h"
34de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian#include "Loader.h"
35de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
36de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
37de58697644a52a614ad9498aa087e95d4a223673Mathias Agopiannamespace android {
38de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
39de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
40de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
41de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian/*
42993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * EGL userspace drivers must be provided either:
43993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * - as a single library:
44993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /vendor/lib/egl/libGLES.so
45993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *
46993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * - as separate libraries:
47993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /vendor/lib/egl/libEGL.so
48993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /vendor/lib/egl/libGLESv1_CM.so
49993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /vendor/lib/egl/libGLESv2.so
50993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *
51993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * The software renderer for the emulator must be provided as a single
52993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * library at:
53993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *
54993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /system/lib/egl/libGLES_android.so
55993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *
56993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *
57993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * For backward compatibility and to facilitate the transition to
58993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian * this new naming scheme, the loader will additionally look for:
59de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian *
60993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian *      /{vendor|system}/lib/egl/lib{GLES | [EGL|GLESv1_CM|GLESv2]}_*.so
61de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian *
62de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian */
63de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
64de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianANDROID_SINGLETON_STATIC_INSTANCE( Loader )
65de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
6680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner/* This function is called to check whether we run inside the emulator,
6780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner * and if this is the case whether GLES GPU emulation is supported.
6880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *
6980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner * Returned values are:
7080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *  -1   -> not running inside the emulator
7180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *   0   -> running inside the emulator, but GPU emulation not supported
7280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *   1   -> running inside the emulator, GPU emulation is supported
7380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner *          through the "emulation" config.
7480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner */
7580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turnerstatic int
7680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' TurnercheckGlesEmulationStatus(void)
7780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner{
7880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* We're going to check for the following kernel parameters:
7980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *
8080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *    qemu=1                      -> tells us that we run inside the emulator
8180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *    android.qemu.gles=<number>  -> tells us the GLES GPU emulation status
8280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     *
8380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     * Note that we will return <number> if we find it. This let us support
8480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     * more additionnal emulation modes in the future.
8580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner     */
8680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    char  prop[PROPERTY_VALUE_MAX];
8780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    int   result = -1;
8880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
8980b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* First, check for qemu=1 */
9080b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    property_get("ro.kernel.qemu",prop,"0");
9180b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    if (atoi(prop) != 1)
9280b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner        return -1;
9380b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
9480b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    /* We are in the emulator, get GPU status value */
9580b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    property_get("ro.kernel.qemu.gles",prop,"0");
9680b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner    return atoi(prop);
9780b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner}
9880b30c24ffc0f67b87d7a6b29f616d1c521d40aeDavid 'Digit' Turner
99de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
100de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
101d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopianstatic char const * getProcessCmdline() {
102d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    long pid = getpid();
103d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    char procPath[128];
104d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    snprintf(procPath, 128, "/proc/%ld/cmdline", pid);
105d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    FILE * file = fopen(procPath, "r");
106d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    if (file) {
107d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        static char cmdline[256];
108d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        char *str = fgets(cmdline, sizeof(cmdline) - 1, file);
109d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        fclose(file);
110d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        if (str) {
111d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            return cmdline;
112d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        }
113d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    }
114d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian    return NULL;
115d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian}
116d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
117d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian// ----------------------------------------------------------------------------
118d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
119de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::driver_t(void* gles)
120de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
121de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    dso[0] = gles;
122de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=1 ; i<NELEM(dso) ; i++)
123de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        dso[i] = 0;
124de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
125de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
126de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::driver_t::~driver_t()
127de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
128de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    for (size_t i=0 ; i<NELEM(dso) ; i++) {
129de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso[i]) {
130de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dlclose(dso[i]);
131de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[i] = 0;
132de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
133de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
134de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
135de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
136de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::driver_t::set(void* hnd, int32_t api)
137de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
138de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    switch (api) {
139de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case EGL:
140de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[0] = hnd;
141de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
142de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv1_CM:
143de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[1] = hnd;
144de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
145de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        case GLESv2:
146de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            dso[2] = hnd;
147de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            break;
148de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        default:
149de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            return BAD_INDEX;
150de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
151de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
152de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
153de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
154de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
155de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
156de58697644a52a614ad9498aa087e95d4a223673Mathias AgopianLoader::Loader()
157993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    : getProcAddress(NULL) {
158de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
159de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
160993814255fc248454368ed9fe34b4703a05eaf99Mathias AgopianLoader::~Loader() {
1610469dd6d55fa331bfd7de9431da98b6340d82271Siva Velusamy    GLTrace_stop();
162de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
163de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
164ada798b7ca7cabc255aa159964b64975e7fdb2dfMathias Agopianvoid* Loader::open(egl_connection_t* cnx)
165de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
166de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    void* dso;
167de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = 0;
168de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
169993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    dso = load_driver("GLES", cnx, EGL | GLESv1_CM | GLESv2);
170993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    if (dso) {
171993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        hnd = new driver_t(dso);
172993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    } else {
173993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        // Always load EGL first
174993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        dso = load_driver("EGL", cnx, EGL);
175de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (dso) {
176de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            hnd = new driver_t(dso);
177993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            hnd->set( load_driver("GLESv1_CM", cnx, GLESv1_CM), GLESv1_CM );
178993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            hnd->set( load_driver("GLESv2",    cnx, GLESv2),    GLESv2 );
179de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
180de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
181de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
182993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
183de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
184de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return (void*)hnd;
185de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
186de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
187de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianstatus_t Loader::close(void* driver)
188de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
189de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    driver_t* hnd = (driver_t*)driver;
190de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    delete hnd;
191de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return NO_ERROR;
192de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
193de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
194de58697644a52a614ad9498aa087e95d4a223673Mathias Agopianvoid Loader::init_api(void* dso,
195de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api,
196de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr,
197de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddressType getProcAddress)
198de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
1997773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian    const ssize_t SIZE = 256;
2000ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian    char scrap[SIZE];
201de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    while (*api) {
202de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * name = *api;
203de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType f =
204de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
205de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
206de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // couldn't find the entry-point, use eglGetProcAddress()
207de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = getProcAddress(name);
208de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
209de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
210de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try without the OES postfix
211de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2120ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if ((index>0 && (index<SIZE-1)) && (!strcmp(name+index, "OES"))) {
213de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                strncpy(scrap, name, index);
214de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                scrap[index] = 0;
215de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2169d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
217de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
218de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
219de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
220de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            // Try with the OES postfix
2210ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            ssize_t index = ssize_t(strlen(name)) - 3;
2220ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian            if (index>0 && strcmp(name+index, "OES")) {
2230ad71a97c6061e3b12d2308bd43e02dfeeb63db4Mathias Agopian                snprintf(scrap, SIZE, "%sOES", name);
224de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = (__eglMustCastToProperFunctionPointerType)dlsym(dso, scrap);
2259d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block                //ALOGD_IF(f, "found <%s> instead", scrap);
226de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
227de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
228de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        if (f == NULL) {
2299d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block            //ALOGD("%s", name);
230de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            f = (__eglMustCastToProperFunctionPointerType)gl_unimplemented;
23148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian
23248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            /*
23348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * GL_EXT_debug_label is special, we always report it as
23448d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * supported, it's handled by GLES_trace. If GLES_trace is not
23548d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             * enabled, then these are no-ops.
23648d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian             */
23748d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            if (!strcmp(name, "glInsertEventMarkerEXT")) {
23848d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
23948d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPushGroupMarkerEXT")) {
24048d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
24148d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            } else if (!strcmp(name, "glPopGroupMarkerEXT")) {
24248d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian                f = (__eglMustCastToProperFunctionPointerType)gl_noop;
24348d438d05f14c2f4bd83ae89f520368cd49122dfMathias Agopian            }
244de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
245de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        *curr++ = f;
246de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        api++;
247de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
248de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
249de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
250993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopianvoid *Loader::load_driver(const char* kind,
251618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_connection_t* cnx, uint32_t mask)
252de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian{
253993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    class MatchFile {
254993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    public:
255993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        static String8 find(const char* kind) {
256993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            String8 result;
257993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            String8 pattern;
258993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            pattern.appendFormat("lib%s", kind);
259993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            const char* const searchPaths[] = {
260993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    "/vendor/lib/egl",
261993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    "/system/lib/egl"
262993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            };
263993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
264993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // first, we search for the exact name of the GLES userspace
265993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // driver in both locations.
266993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // i.e.:
267993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            //      libGLES.so, or:
268993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            //      libEGL.so, libGLESv1_CM.so, libGLESv2.so
269993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
270993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
271993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                if (find(result, pattern, searchPaths[i], true)) {
272993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    return result;
273993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                }
274993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            }
275993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
276993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // for compatibility with the old "egl.cfg" naming convention
277993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // we look for files that match:
278993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            //      libGLES_*.so, or:
279993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            //      libEGL_*.so, libGLESv1_CM_*.so, libGLESv2_*.so
280993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
281993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            pattern.append("_");
282993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            for (size_t i=0 ; i<NELEM(searchPaths) ; i++) {
283993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                if (find(result, pattern, searchPaths[i], false)) {
284993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    return result;
285993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                }
286993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            }
287993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
288993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // we didn't find the driver. gah.
289993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            result.clear();
290993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            return result;
291993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        }
292993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
293993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    private:
294993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        static bool find(String8& result,
295993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                const String8& pattern, const char* const search, bool exact) {
296993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
297993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // in the emulator case, we just return the hardcoded name
298993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            // of the software renderer.
299993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            if (checkGlesEmulationStatus() == 0) {
300993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                ALOGD("Emulator without GPU support detected. "
301993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                      "Fallback to software renderer.");
302993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                result.setTo("/system/lib/egl/libGLES_android.so");
303993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                return true;
304993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            }
305993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
306993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            if (exact) {
307993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                String8 absolutePath;
308993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                absolutePath.appendFormat("%s/%s.so", search, pattern.string());
309993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                if (!access(absolutePath.string(), R_OK)) {
310993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    result = absolutePath;
311993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    return true;
312993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                }
313993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                return false;
314993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            }
315993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
316993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            DIR* d = opendir(search);
317993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            if (d != NULL) {
318993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                struct dirent cur;
319993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                struct dirent* e;
320993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                while (readdir_r(d, &cur, &e) == 0 && e) {
321993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    if (e->d_type == DT_DIR) {
322993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                        continue;
323993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    }
324993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    if (!strcmp(e->d_name, "libGLES_android.so")) {
325993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                        // always skip the software renderer
326993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                        continue;
327993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    }
328993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    if (strstr(e->d_name, pattern.string()) == e->d_name) {
329993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                        if (!strcmp(e->d_name + strlen(e->d_name) - 3, ".so")) {
330993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                            result.clear();
331993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                            result.appendFormat("%s/%s", search, e->d_name);
332993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                            closedir(d);
333993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                            return true;
334993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                        }
335993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                    }
336993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                }
337993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian                closedir(d);
338993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            }
339993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian            return false;
3402b9e4f6a2490864ead44e88a68da78be5cb3da22Brian Swetland        }
341993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    };
342993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
343993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian
344993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    String8 absolutePath = MatchFile::find(kind);
345993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    if (absolutePath.isEmpty()) {
346993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        // this happens often, we don't want to log an error
347993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian        return 0;
3488c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
349993814255fc248454368ed9fe34b4703a05eaf99Mathias Agopian    const char* const driver_absolute_path = absolutePath.string();
3508c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian
3518c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    void* dso = dlopen(driver_absolute_path, RTLD_NOW | RTLD_LOCAL);
3528c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    if (dso == 0) {
3538c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        const char* err = dlerror();
354e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE("load_driver(%s): %s", driver_absolute_path, err?err:"unknown");
3558c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian        return 0;
3568c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian    }
357de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
3589d4536835248525f32f1504a3d28d5bbfa0a2910Steve Block    ALOGD("loaded %s", driver_absolute_path);
359baca89c06a40c6c19ae2294fb4263d893126320cMathias Agopian
360de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & EGL) {
361de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        getProcAddress = (getProcAddressType)dlsym(dso, "eglGetProcAddress");
362de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
363e6f43ddce78d6846af12550ff9193c5c6fe5844bSteve Block        ALOGE_IF(!getProcAddress,
3648c17384a5edd027376926b857af1fb170dbe9b43Mathias Agopian                "can't find eglGetProcAddress() in %s", driver_absolute_path);
365de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
366d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#ifdef SYSTEMUI_PBSIZE_HACK
367d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#warning "SYSTEMUI_PBSIZE_HACK enabled"
368d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        /*
369d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * TODO: replace SYSTEMUI_PBSIZE_HACK by something less hackish
370d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         *
371d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * Here we adjust the PB size from its default value to 512KB which
372d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * is the minimum acceptable for the systemui process.
373d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * We do this on low-end devices only because it allows us to enable
374d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * h/w acceleration in the systemui process while keeping the
375d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * memory usage down.
376d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         *
377d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * Obviously, this is the wrong place and wrong way to make this
378d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * adjustment, but at the time of this writing this was the safest
379d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         * solution.
380d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian         */
381d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        const char *cmdline = getProcessCmdline();
382d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        if (strstr(cmdline, "systemui")) {
383d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            void *imgegl = dlopen("/vendor/lib/libIMGegl.so", RTLD_LAZY);
384d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            if (imgegl) {
385d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                unsigned int *PVRDefaultPBS =
386d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                        (unsigned int *)dlsym(imgegl, "PVRDefaultPBS");
387d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                if (PVRDefaultPBS) {
388d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                    ALOGD("setting default PBS to 512KB, was %d KB", *PVRDefaultPBS / 1024);
389d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                    *PVRDefaultPBS = 512*1024;
390d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian                }
391d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian            }
392d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian        }
393d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian#endif
394d75f84d6410da934d1b1760fdc0d05d4ba1e8f35Mathias Agopian
395618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        egl_t* egl = &cnx->egl;
396de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        __eglMustCastToProperFunctionPointerType* curr =
397de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            (__eglMustCastToProperFunctionPointerType*)egl;
398de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        char const * const * api = egl_names;
399de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        while (*api) {
400de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            char const * name = *api;
401de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            __eglMustCastToProperFunctionPointerType f =
402de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                (__eglMustCastToProperFunctionPointerType)dlsym(dso, name);
403de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            if (f == NULL) {
404de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                // couldn't find the entry-point, use eglGetProcAddress()
405de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                f = getProcAddress(name);
406de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                if (f == NULL) {
407de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                    f = (__eglMustCastToProperFunctionPointerType)0;
408de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian                }
409de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            }
410de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            *curr++ = f;
411de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            api++;
412de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian        }
413de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
414de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
415de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv1_CM) {
416618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian        init_api(dso, gl_names,
417618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
4187773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                &cnx->hooks[egl_connection_t::GLESv1_INDEX]->gl,
419618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            getProcAddress);
420de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
421de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
422de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    if (mask & GLESv2) {
423618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian      init_api(dso, gl_names,
424618fa10949c42eb83fa5fe105fe542bcff833ddaMathias Agopian            (__eglMustCastToProperFunctionPointerType*)
4257773c435bc5da8217433e1b242d3a6712a17b5f7Mathias Agopian                &cnx->hooks[egl_connection_t::GLESv2_INDEX]->gl,
426de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian            getProcAddress);
427de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    }
428de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
429de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian    return dso;
430de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}
431de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian
432de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
433de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian}; // namespace android
434de58697644a52a614ad9498aa087e95d4a223673Mathias Agopian// ----------------------------------------------------------------------------
435