Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d986181d authored by Yiwei Zhang's avatar Yiwei Zhang
Browse files

Game Driver: plumb driver choice and loading time to GpuStats

This change plumb the below info from GL and Vulkan loader:
1. Intended driver to use
2. Whether intended driver is loaded
3. Total driver loading time

Bug: 123529932
Test: Build, flash and boot. Verify the GpuService receiver side.
Change-Id: I967d4361bf0e04c02390c7555617575c19ecadd4
parent 101843de
Loading
Loading
Loading
Loading
+96 −21
Original line number Original line Diff line number Diff line
@@ -157,10 +157,11 @@ void GraphicsEnv::setDriverPath(const std::string path) {
}
}


void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
                              const std::string& driverVersionName,
                              const std::string& driverVersionName, uint64_t driverVersionCode,
                              const uint64_t driverVersionCode, const std::string& appPackageName) {
                              const std::string& appPackageName) {
    ATRACE_CALL();
    ATRACE_CALL();


    std::lock_guard<std::mutex> lock(mStatsLock);
    ALOGV("setGpuStats:\n"
    ALOGV("setGpuStats:\n"
          "\tdriverPackageName[%s]\n"
          "\tdriverPackageName[%s]\n"
          "\tdriverVersionName[%s]\n"
          "\tdriverVersionName[%s]\n"
@@ -169,15 +170,89 @@ void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
          driverPackageName.c_str(), driverVersionName.c_str(),
          driverPackageName.c_str(), driverVersionName.c_str(),
          (unsigned long long)driverVersionCode, appPackageName.c_str());
          (unsigned long long)driverVersionCode, appPackageName.c_str());


    mGpuStats = {
    mGpuStats.driverPackageName = driverPackageName;
            .driverPackageName = driverPackageName,
    mGpuStats.driverVersionName = driverVersionName;
            .driverVersionName = driverVersionName,
    mGpuStats.driverVersionCode = driverVersionCode;
            .driverVersionCode = driverVersionCode,
    mGpuStats.appPackageName = appPackageName;
            .appPackageName = appPackageName,
}
    };

void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
    ATRACE_CALL();

    std::lock_guard<std::mutex> lock(mStatsLock);
    switch (driver) {
        case GraphicsEnv::Driver::GL:
        case GraphicsEnv::Driver::GL_UPDATED:
        case GraphicsEnv::Driver::ANGLE: {
            if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE) {
                mGpuStats.glDriverToLoad = driver;
                break;
            }

            if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
                mGpuStats.glDriverFallback = driver;
            }
            break;
        }
        case Driver::VULKAN:
        case Driver::VULKAN_UPDATED: {
            if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE) {
                mGpuStats.vkDriverToLoad = driver;
                break;
            }

            if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
                mGpuStats.vkDriverFallback = driver;
            }
            break;
        }
        default:
            break;
    }
}

void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isLoaded, int64_t driverLoadingTime) {
    ATRACE_CALL();

    std::lock_guard<std::mutex> lock(mStatsLock);
    GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
    bool isIntendedDriverLoaded = false;
    if (api == GraphicsEnv::Api::API_GL) {
        driver = mGpuStats.glDriverToLoad;
        isIntendedDriverLoaded = isLoaded &&
                ((mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) ||
                 (mGpuStats.glDriverToLoad == mGpuStats.glDriverFallback));
    } else {
        driver = mGpuStats.vkDriverToLoad;
        isIntendedDriverLoaded =
                isLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
    }

    sendGpuStatsLocked(driver, isIntendedDriverLoaded, driverLoadingTime);
}

void GraphicsEnv::clearDriverLoadingInfo(GraphicsEnv::Api api) {
    ATRACE_CALL();

    std::lock_guard<std::mutex> lock(mStatsLock);
    if (api == GraphicsEnv::Api::API_GL) {
        mGpuStats.glDriverToLoad = GraphicsEnv::Driver::NONE;
        mGpuStats.glDriverFallback = GraphicsEnv::Driver::NONE;
    }
}

static sp<IGpuService> getGpuService() {
    const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
    if (!binder) {
        ALOGE("Failed to get gpu service");
        return nullptr;
    }

    return interface_cast<IGpuService>(binder);
}
}


void GraphicsEnv::sendGpuStats() {
void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Driver driver, bool isDriverLoaded,
                                     int64_t driverLoadingTime) {
    ATRACE_CALL();
    ATRACE_CALL();


    // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
    // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
@@ -187,20 +262,20 @@ void GraphicsEnv::sendGpuStats() {
          "\tdriverPackageName[%s]\n"
          "\tdriverPackageName[%s]\n"
          "\tdriverVersionName[%s]\n"
          "\tdriverVersionName[%s]\n"
          "\tdriverVersionCode[%llu]\n"
          "\tdriverVersionCode[%llu]\n"
          "\tappPackageName[%s]\n",
          "\tappPackageName[%s]\n"
          "\tdriver[%d]\n"
          "\tisDriverLoaded[%d]\n"
          "\tdriverLoadingTime[%lld]",
          mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
          mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
          (unsigned long long)mGpuStats.driverVersionCode, mGpuStats.appPackageName.c_str());
          (unsigned long long)mGpuStats.driverVersionCode, mGpuStats.appPackageName.c_str(),
          static_cast<int32_t>(driver), isDriverLoaded, (long long)driverLoadingTime);


    const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
    const sp<IGpuService> gpuService = getGpuService();
    if (!binder) {
    if (gpuService) {
        ALOGE("Failed to get gpu service for [%s]", mGpuStats.appPackageName.c_str());
        gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
        return;
                                mGpuStats.driverVersionCode, mGpuStats.appPackageName, driver,
                                isDriverLoaded, driverLoadingTime);
    }
    }

    interface_cast<IGpuService>(binder)->setGpuStats(mGpuStats.driverPackageName,
                                                     mGpuStats.driverVersionName,
                                                     mGpuStats.driverVersionCode,
                                                     mGpuStats.appPackageName);
}
}


void* GraphicsEnv::loadLibrary(std::string name) {
void* GraphicsEnv::loadLibrary(std::string name) {
+18 −3
Original line number Original line Diff line number Diff line
@@ -28,8 +28,9 @@ public:
    explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
    explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}


    virtual void setGpuStats(const std::string& driverPackageName,
    virtual void setGpuStats(const std::string& driverPackageName,
                             const std::string& driverVersionName, const uint64_t driverVersionCode,
                             const std::string& driverVersionName, uint64_t driverVersionCode,
                             const std::string& appPackageName) {
                             const std::string& appPackageName, GraphicsEnv::Driver driver,
                             bool isDriverLoaded, int64_t driverLoadingTime) {
        Parcel data, reply;
        Parcel data, reply;
        data.writeInterfaceToken(IGpuService::getInterfaceDescriptor());
        data.writeInterfaceToken(IGpuService::getInterfaceDescriptor());


@@ -37,6 +38,9 @@ public:
        data.writeUtf8AsUtf16(driverVersionName);
        data.writeUtf8AsUtf16(driverVersionName);
        data.writeUint64(driverVersionCode);
        data.writeUint64(driverVersionCode);
        data.writeUtf8AsUtf16(appPackageName);
        data.writeUtf8AsUtf16(appPackageName);
        data.writeInt32(static_cast<int32_t>(driver));
        data.writeBool(isDriverLoaded);
        data.writeInt64(driverLoadingTime);


        remote()->transact(BnGpuService::SET_GPU_STATS, data, &reply);
        remote()->transact(BnGpuService::SET_GPU_STATS, data, &reply);
    }
    }
@@ -65,7 +69,18 @@ status_t BnGpuService::onTransact(uint32_t code, const Parcel& data, Parcel* rep
            std::string appPackageName;
            std::string appPackageName;
            if ((status = data.readUtf8FromUtf16(&appPackageName)) != OK) return status;
            if ((status = data.readUtf8FromUtf16(&appPackageName)) != OK) return status;


            setGpuStats(driverPackageName, driverVersionName, driverVersionCode, appPackageName);
            int32_t driver;
            if ((status = data.readInt32(&driver)) != OK) return status;

            bool isDriverLoaded;
            if ((status = data.readBool(&isDriverLoaded)) != OK) return status;

            int64_t driverLoadingTime;
            if ((status = data.readInt64(&driverLoadingTime)) != OK) return status;

            setGpuStats(driverPackageName, driverVersionName, driverVersionCode, appPackageName,
                        static_cast<GraphicsEnv::Driver>(driver), isDriverLoaded,
                        driverLoadingTime);


            return OK;
            return OK;
        }
        }
+36 −2
Original line number Original line Diff line number Diff line
@@ -28,11 +28,41 @@ namespace android {
struct NativeLoaderNamespace;
struct NativeLoaderNamespace;


class GraphicsEnv {
class GraphicsEnv {
public:
    enum Api {
        API_GL = 0,
        API_VK = 1,
    };

    enum Driver {
        NONE = 0,
        GL = 1,
        GL_UPDATED = 2,
        VULKAN = 3,
        VULKAN_UPDATED = 4,
        ANGLE = 5,
    };

private:
    struct GpuStats {
    struct GpuStats {
        std::string driverPackageName;
        std::string driverPackageName;
        std::string driverVersionName;
        std::string driverVersionName;
        uint64_t driverVersionCode;
        uint64_t driverVersionCode;
        std::string appPackageName;
        std::string appPackageName;
        Driver glDriverToLoad;
        Driver glDriverFallback;
        Driver vkDriverToLoad;
        Driver vkDriverFallback;

        GpuStats()
              : driverPackageName(""),
                driverVersionName(""),
                driverVersionCode(0),
                appPackageName(""),
                glDriverToLoad(Driver::NONE),
                glDriverFallback(Driver::NONE),
                vkDriverToLoad(Driver::NONE),
                vkDriverFallback(Driver::NONE) {}
    };
    };


public:
public:
@@ -48,8 +78,11 @@ public:
    void setDriverPath(const std::string path);
    void setDriverPath(const std::string path);
    android_namespace_t* getDriverNamespace();
    android_namespace_t* getDriverNamespace();
    void setGpuStats(const std::string& driverPackageName, const std::string& driverVersionName,
    void setGpuStats(const std::string& driverPackageName, const std::string& driverVersionName,
                     const uint64_t versionCode, const std::string& appPackageName);
                     uint64_t versionCode, const std::string& appPackageName);
    void sendGpuStats();
    void setDriverToLoad(Driver driver);
    void setDriverLoaded(Api api, bool isDriverLoaded, int64_t driverLoadingTime);
    void clearDriverLoadingInfo(Api api);
    void sendGpuStatsLocked(Driver driver, bool isDriverLoaded, int64_t driverLoadingTime);


    bool shouldUseAngle(std::string appName);
    bool shouldUseAngle(std::string appName);
    bool shouldUseAngle();
    bool shouldUseAngle();
@@ -82,6 +115,7 @@ private:


    GraphicsEnv() = default;
    GraphicsEnv() = default;
    std::string mDriverPath;
    std::string mDriverPath;
    std::mutex mStatsLock;
    GpuStats mGpuStats;
    GpuStats mGpuStats;
    std::string mAnglePath;
    std::string mAnglePath;
    std::string mAngleAppName;
    std::string mAngleAppName;
+5 −3
Original line number Original line Diff line number Diff line
@@ -18,6 +18,7 @@


#include <binder/IInterface.h>
#include <binder/IInterface.h>
#include <cutils/compiler.h>
#include <cutils/compiler.h>
#include <graphicsenv/GraphicsEnv.h>


#include <vector>
#include <vector>


@@ -29,12 +30,13 @@ namespace android {
 */
 */
class IGpuService : public IInterface {
class IGpuService : public IInterface {
public:
public:
    DECLARE_META_INTERFACE(GpuService);
    DECLARE_META_INTERFACE(GpuService)


    // set GPU stats from GraphicsEnvironment.
    // set GPU stats from GraphicsEnvironment.
    virtual void setGpuStats(const std::string& driverPackageName,
    virtual void setGpuStats(const std::string& driverPackageName,
                             const std::string& driverVersionName, const uint64_t driverVersionCode,
                             const std::string& driverVersionName, uint64_t driverVersionCode,
                             const std::string& appPackageName) = 0;
                             const std::string& appPackageName, GraphicsEnv::Driver driver,
                             bool isDriverLoaded, int64_t driverLoadingTime) = 0;
};
};


class BnGpuService : public BnInterface<IGpuService> {
class BnGpuService : public BnInterface<IGpuService> {
+20 −1
Original line number Original line Diff line number Diff line
@@ -27,6 +27,7 @@
#include <android/dlext.h>
#include <android/dlext.h>
#include <cutils/properties.h>
#include <cutils/properties.h>
#include <log/log.h>
#include <log/log.h>
#include <utils/Timers.h>


#ifndef __ANDROID_VNDK__
#ifndef __ANDROID_VNDK__
#include <graphicsenv/GraphicsEnv.h>
#include <graphicsenv/GraphicsEnv.h>
@@ -217,6 +218,7 @@ static void setEmulatorGlesValue(void) {
void* Loader::open(egl_connection_t* cnx)
void* Loader::open(egl_connection_t* cnx)
{
{
    ATRACE_CALL();
    ATRACE_CALL();
    const nsecs_t openTime = systemTime();


    void* dso;
    void* dso;
    driver_t* hnd = nullptr;
    driver_t* hnd = nullptr;
@@ -234,6 +236,8 @@ void* Loader::open(egl_connection_t* cnx)
    if (dso) {
    if (dso) {
        hnd = new driver_t(dso);
        hnd = new driver_t(dso);
    } else {
    } else {
        android::GraphicsEnv::getInstance().clearDriverLoadingInfo(
                android::GraphicsEnv::Api::API_GL);
        // Always load EGL first
        // Always load EGL first
        dso = load_driver("EGL", cnx, EGL);
        dso = load_driver("EGL", cnx, EGL);
        if (dso) {
        if (dso) {
@@ -243,19 +247,30 @@ void* Loader::open(egl_connection_t* cnx)
        }
        }
    }
    }


    if (!hnd) {
        android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
                                                            false, systemTime() - openTime);
    }

    LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");
    LOG_ALWAYS_FATAL_IF(!hnd, "couldn't find an OpenGL ES implementation");


    cnx->libEgl   = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
    cnx->libEgl   = load_wrapper(EGL_WRAPPER_DIR "/libEGL.so");
    cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
    cnx->libGles2 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv2.so");
    cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");
    cnx->libGles1 = load_wrapper(EGL_WRAPPER_DIR "/libGLESv1_CM.so");


    if (!cnx->libEgl || !cnx->libGles2 || !cnx->libGles1) {
        android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL,
                                                            false, systemTime() - openTime);
    }

    LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
    LOG_ALWAYS_FATAL_IF(!cnx->libEgl,
            "couldn't load system EGL wrapper libraries");
            "couldn't load system EGL wrapper libraries");


    LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
    LOG_ALWAYS_FATAL_IF(!cnx->libGles2 || !cnx->libGles1,
            "couldn't load system OpenGL ES wrapper libraries");
            "couldn't load system OpenGL ES wrapper libraries");


    android::GraphicsEnv::getInstance().sendGpuStats();
    android::GraphicsEnv::getInstance().setDriverLoaded(android::GraphicsEnv::Api::API_GL, true,
                                                        systemTime() - openTime);


    return (void*)hnd;
    return (void*)hnd;
}
}
@@ -591,17 +606,21 @@ void *Loader::load_driver(const char* kind,
    void* dso = nullptr;
    void* dso = nullptr;
    android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
    android_namespace_t* ns = android::GraphicsEnv::getInstance().getAngleNamespace();
    if (ns) {
    if (ns) {
        android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::ANGLE);
        dso = load_angle(kind, ns, cnx);
        dso = load_angle(kind, ns, cnx);
    }
    }
#ifndef __ANDROID_VNDK__
#ifndef __ANDROID_VNDK__
    if (!dso) {
    if (!dso) {
        android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
        android_namespace_t* ns = android::GraphicsEnv::getInstance().getDriverNamespace();
        if (ns) {
        if (ns) {
            android::GraphicsEnv::getInstance().setDriverToLoad(
                    android::GraphicsEnv::Driver::GL_UPDATED);
            dso = load_updated_driver(kind, ns);
            dso = load_updated_driver(kind, ns);
        }
        }
    }
    }
#endif
#endif
    if (!dso) {
    if (!dso) {
        android::GraphicsEnv::getInstance().setDriverToLoad(android::GraphicsEnv::Driver::GL);
        dso = load_system_driver(kind);
        dso = load_system_driver(kind);
        if (!dso)
        if (!dso)
            return nullptr;
            return nullptr;
Loading