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

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

Game Driver: process sphal libraries in GPU service

This change adds the sphal libraries text file parsing to the GPU
service. As the result, when the Game Driver apk is updated, the sphal
library list will be read out to the GAME_DRIVER_SPHAL_LIBRARIES
settings global property to be used in the graphics environment to
extend the current linker namespace.

Bug: 124448366
Test: Build, flash and boot. Install the apk to verify settings global.

Change-Id: Ifb4007a1fe7269e0a2857fe7badc8642342b1449
Merged-In: Ifb4007a1fe7269e0a2857fe7badc8642342b1449
parent eb8d7cc3
Loading
Loading
Loading
Loading
+10 −3
Original line number Diff line number Diff line
@@ -247,8 +247,15 @@ public class GraphicsEnvironment {
          .append(abi);
        final String paths = sb.toString();

        if (DEBUG) Log.v(TAG, "gfx driver package libs: " + paths);
        setDriverPath(paths);
        final String sphalLibraries =
                coreSettings.getString(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES);

        if (DEBUG) {
            Log.v(TAG,
                    "gfx driver package search path: " + paths
                            + ", required sphal libraries: " + sphalLibraries);
        }
        setDriverPathAndSphalLibraries(paths, sphalLibraries);
    }

    /**
@@ -285,5 +292,5 @@ public class GraphicsEnvironment {

    private static native void setLayerPaths(ClassLoader classLoader, String layerPaths);
    private static native void setDebugLayers(String layers);
    private static native void setDriverPath(String path);
    private static native void setDriverPathAndSphalLibraries(String path, String sphalLibraries);
}
+6 −3
Original line number Diff line number Diff line
@@ -23,9 +23,12 @@

namespace {

void setDriverPath(JNIEnv* env, jobject clazz, jstring path) {
void setDriverPathAndSphalLibraries_native(JNIEnv* env, jobject clazz, jstring path,
                                           jstring sphalLibraries) {
    ScopedUtfChars pathChars(env, path);
    android::GraphicsEnv::getInstance().setDriverPath(pathChars.c_str());
    ScopedUtfChars sphalLibrariesChars(env, sphalLibraries);
    android::GraphicsEnv::getInstance().setDriverPathAndSphalLibraries(pathChars.c_str(),
                                                                       sphalLibrariesChars.c_str());
}

void setLayerPaths_native(JNIEnv* env, jobject clazz, jobject classLoader, jstring layerPaths) {
@@ -43,7 +46,7 @@ void setDebugLayers_native(JNIEnv* env, jobject clazz, jstring layers) {
}

const JNINativeMethod g_methods[] = {
    { "setDriverPath", "(Ljava/lang/String;)V", reinterpret_cast<void*>(setDriverPath) },
    { "setDriverPathAndSphalLibraries", "(Ljava/lang/String;Ljava/lang/String;)V", reinterpret_cast<void*>(setDriverPathAndSphalLibraries_native) },
    { "setLayerPaths", "(Ljava/lang/ClassLoader;Ljava/lang/String;)V", reinterpret_cast<void*>(setLayerPaths_native) },
    { "setDebugLayers", "(Ljava/lang/String;)V", reinterpret_cast<void*>(setDebugLayers_native) },
};
+30 −14
Original line number Diff line number Diff line
@@ -62,6 +62,7 @@ public class GpuService extends SystemService {

    private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0";
    private static final String GAME_DRIVER_WHITELIST_FILENAME = "whitelist.txt";
    private static final String GAME_DRIVER_SPHAL_LIBRARIES_FILENAME = "sphal_libraries.txt";
    private static final int BASE64_FLAGS = Base64.NO_PADDING | Base64.NO_WRAP;

    private final Context mContext;
@@ -162,6 +163,25 @@ public class GpuService extends SystemService {
        }
    }

    private static void assetToSettingsGlobal(Context context, Context driverContext,
            String fileName, String settingsGlobal, CharSequence delimiter) {
        try {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(driverContext.getAssets().open(fileName)));
            final ArrayList<String> assetStrings = new ArrayList<>();
            for (String assetString; (assetString = reader.readLine()) != null; ) {
                assetStrings.add(assetString);
            }
            Settings.Global.putString(context.getContentResolver(),
                                      settingsGlobal,
                                      String.join(delimiter, assetStrings));
        } catch (IOException e) {
            if (DEBUG) {
                Slog.w(TAG, "Failed to load " + fileName + ", abort.");
            }
        }
    }

    private void fetchGameDriverPackageProperties() {
        final ApplicationInfo driverInfo;
        try {
@@ -186,29 +206,25 @@ public class GpuService extends SystemService {
        // Reset the whitelist.
        Settings.Global.putString(mContentResolver,
                                  Settings.Global.GAME_DRIVER_WHITELIST, "");
        // Reset the sphal libraries
        Settings.Global.putString(mContentResolver,
                                  Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, "");
        mGameDriverVersionCode = driverInfo.longVersionCode;

        try {
            final Context driverContext = mContext.createPackageContext(mDriverPackageName,
                                                                        Context.CONTEXT_RESTRICTED);
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(driverContext.getAssets()
                                              .open(GAME_DRIVER_WHITELIST_FILENAME)));
            final ArrayList<String> whitelistedPackageNames = new ArrayList<>();
            for (String packageName; (packageName = reader.readLine()) != null; ) {
                whitelistedPackageNames.add(packageName);
            }
            Settings.Global.putString(mContentResolver,
                                      Settings.Global.GAME_DRIVER_WHITELIST,
                                      String.join(",", whitelistedPackageNames));

            assetToSettingsGlobal(mContext, driverContext, GAME_DRIVER_WHITELIST_FILENAME,
                    Settings.Global.GAME_DRIVER_WHITELIST, ",");

            assetToSettingsGlobal(mContext, driverContext, GAME_DRIVER_SPHAL_LIBRARIES_FILENAME,
                    Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES, ":");

        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                Slog.w(TAG, "driver package '" + mDriverPackageName + "' not installed");
            }
        } catch (IOException e) {
            if (DEBUG) {
                Slog.w(TAG, "Failed to load whitelist driver package, abort.");
            }
        }
    }