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

Commit 5d832d29 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge changes from topic "GameDriver3"

* changes:
  Game Driver: process sphal libraries in GPU service
  Game Driver: Add a Settings.Global property for sphal libraries
  [Game Driver] Add global whitelist option.
  [GPU Service] Move blacklists process to GPU service.
  Split blacklist from versioned blacklists.
  Game Driver: clean up GraphicsEnvironment
  [GPU Service] Implement GPU service.
parents 0c9a514c b1eeb0cd
Loading
Loading
Loading
Loading
+37 −85
Original line number Diff line number Diff line
@@ -20,24 +20,16 @@ import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.gamedriver.GameDriverProto.Blacklist;
import android.gamedriver.GameDriverProto.Blacklists;
import android.opengl.EGL14;
import android.os.Build;
import android.os.SystemProperties;
import android.provider.Settings;
import android.util.Base64;
import android.util.Log;

import com.android.framework.protobuf.InvalidProtocolBufferException;

import dalvik.system.VMRuntime;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -57,9 +49,7 @@ public class GraphicsEnvironment {
    private static final boolean DEBUG = false;
    private static final String TAG = "GraphicsEnvironment";
    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_BLACKLIST_FLAG = "blacklist";
    private static final int BASE64_FLAGS = Base64.NO_PADDING | Base64.NO_WRAP;
    private static final String GAME_DRIVER_WHITELIST_ALL = "*";

    private ClassLoader mClassLoader;
    private String mLayerPath;
@@ -108,15 +98,15 @@ public class GraphicsEnvironment {

        if (isDebuggable(context)) {

            int enable = Settings.Global.getInt(context.getContentResolver(),
            final int enable = Settings.Global.getInt(context.getContentResolver(),
                                                      Settings.Global.ENABLE_GPU_DEBUG_LAYERS, 0);

            if (enable != 0) {

                String gpuDebugApp = Settings.Global.getString(context.getContentResolver(),
                final String gpuDebugApp = Settings.Global.getString(context.getContentResolver(),
                                                                     Settings.Global.GPU_DEBUG_APP);

                String packageName = context.getPackageName();
                final String packageName = context.getPackageName();

                if ((gpuDebugApp != null && packageName != null)
                        && (!gpuDebugApp.isEmpty() && !packageName.isEmpty())
@@ -148,7 +138,7 @@ public class GraphicsEnvironment {

    private static List<String> getGlobalSettingsString(Bundle bundle, String globalSetting) {
        List<String> valueList = null;
        String settingsValue = bundle.getString(globalSetting);
        final String settingsValue = bundle.getString(globalSetting);

        if (settingsValue != null) {
            valueList = new ArrayList<>(Arrays.asList(settingsValue.split(",")));
@@ -163,12 +153,12 @@ public class GraphicsEnvironment {
     * Choose whether the current process should use the builtin or an updated driver.
     */
    private static void chooseDriver(Context context, Bundle coreSettings) {
        String driverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER);
        final String driverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER);
        if (driverPackageName == null || driverPackageName.isEmpty()) {
            return;
        }

        ApplicationInfo driverInfo;
        final ApplicationInfo driverInfo;
        try {
            driverInfo = context.getPackageManager().getApplicationInfo(driverPackageName,
                    PackageManager.MATCH_SYSTEM_ONLY);
@@ -189,7 +179,7 @@ public class GraphicsEnvironment {
        // To minimize risk of driver updates crippling the device beyond user repair, never use an
        // updated driver for privileged or non-updated system apps. Presumably pre-installed apps
        // were tested thoroughly with the pre-installed driver.
        ApplicationInfo ai = context.getApplicationInfo();
        final ApplicationInfo ai = context.getApplicationInfo();
        if (ai.isPrivilegedApp() || (ai.isSystemApp() && !ai.isUpdatedSystemApp())) {
            if (DEBUG) Log.v(TAG, "ignoring driver package for privileged/non-updated system app");
            return;
@@ -199,7 +189,7 @@ public class GraphicsEnvironment {
        // 0: Default (Invalid values fallback to default as well)
        // 1: All apps use Game Driver
        // 2: All apps use system graphics driver
        int gameDriverAllApps = coreSettings.getInt(Settings.Global.GAME_DRIVER_ALL_APPS, 0);
        final int gameDriverAllApps = coreSettings.getInt(Settings.Global.GAME_DRIVER_ALL_APPS, 0);
        if (gameDriverAllApps == 2) {
            if (DEBUG) {
                Log.w(TAG, "Game Driver is turned off on this device");
@@ -216,49 +206,29 @@ public class GraphicsEnvironment {
                }
                return;
            }
            boolean isOptIn =
            final boolean isOptIn =
                    getGlobalSettingsString(coreSettings, Settings.Global.GAME_DRIVER_OPT_IN_APPS)
                            .contains(ai.packageName);

            if (!isOptIn && !onWhitelist(context, driverPackageName, ai.packageName)) {
            final List<String> whitelist = getGlobalSettingsString(coreSettings,
                    Settings.Global.GAME_DRIVER_WHITELIST);
            if (!isOptIn && whitelist.indexOf(GAME_DRIVER_WHITELIST_ALL) != 0
                    && !whitelist.contains(ai.packageName)) {
                if (DEBUG) {
                    Log.w(TAG, ai.packageName + " is not on the whitelist.");
                }
                return;
            }

            if (!isOptIn) {
                // At this point, the application is on the whitelist only, check whether it's
                // on the blacklist, terminate early when it's on the blacklist.
                try {
                    // TODO(b/121350991) Switch to DeviceConfig with property listener.
                    String base64String =
                            coreSettings.getString(Settings.Global.GAME_DRIVER_BLACKLIST);
                    if (base64String != null && !base64String.isEmpty()) {
                        Blacklists blacklistsProto = Blacklists.parseFrom(
                                Base64.decode(base64String, BASE64_FLAGS));
                        List<Blacklist> blacklists = blacklistsProto.getBlacklistsList();
                        long driverVersionCode = driverInfo.longVersionCode;
                        for (Blacklist blacklist : blacklists) {
                            if (blacklist.getVersionCode() == driverVersionCode) {
                                for (String packageName : blacklist.getPackageNamesList()) {
                                    if (packageName == ai.packageName) {
            // If the application is not opted-in and check whether it's on the blacklist,
            // terminate early if it's on the blacklist and fallback to system driver.
            if (!isOptIn
                    && getGlobalSettingsString(coreSettings, Settings.Global.GAME_DRIVER_BLACKLIST)
                            .contains(ai.packageName)) {
                return;
            }
        }
                                break;
                            }
                        }
                    }
                } catch (InvalidProtocolBufferException e) {
                    if (DEBUG) {
                        Log.w(TAG, "Can't parse blacklist, skip and continue...");
                    }
                }
            }
        }

        String abi = chooseAbi(driverInfo);
        final String abi = chooseAbi(driverInfo);
        if (abi == null) {
            if (DEBUG) {
                // This is the normal case for the pre-installed empty driver package, don't spam
@@ -269,16 +239,23 @@ public class GraphicsEnvironment {
            return;
        }

        StringBuilder sb = new StringBuilder();
        final StringBuilder sb = new StringBuilder();
        sb.append(driverInfo.nativeLibraryDir)
          .append(File.pathSeparator);
        sb.append(driverInfo.sourceDir)
          .append("!/lib/")
          .append(abi);
        String paths = sb.toString();
        final String paths = sb.toString();

        final String sphalLibraries =
                coreSettings.getString(Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES);

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

    /**
@@ -292,7 +269,7 @@ public class GraphicsEnvironment {
     * Should only be called after chooseDriver().
     */
    public static void earlyInitEGL() {
        Thread eglInitThread = new Thread(
        final Thread eglInitThread = new Thread(
                () -> {
                    EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY);
                },
@@ -301,7 +278,7 @@ public class GraphicsEnvironment {
    }

    private static String chooseAbi(ApplicationInfo ai) {
        String isa = VMRuntime.getCurrentInstructionSet();
        final String isa = VMRuntime.getCurrentInstructionSet();
        if (ai.primaryCpuAbi != null &&
                isa.equals(VMRuntime.getInstructionSet(ai.primaryCpuAbi))) {
            return ai.primaryCpuAbi;
@@ -313,32 +290,7 @@ public class GraphicsEnvironment {
        return null;
    }

    private static boolean onWhitelist(Context context, String driverPackageName,
            String applicationPackageName) {
        try {
            Context driverContext = context.createPackageContext(driverPackageName,
                                                                 Context.CONTEXT_RESTRICTED);
            AssetManager assets = driverContext.getAssets();
            InputStream stream = assets.open(GAME_DRIVER_WHITELIST_FILENAME);
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            for (String packageName; (packageName = reader.readLine()) != null; ) {
                if (packageName.equals(applicationPackageName)) {
                    return true;
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                Log.w(TAG, "driver package '" + driverPackageName + "' not installed");
            }
        } catch (IOException e) {
            if (DEBUG) {
                Log.w(TAG, "Failed to load whitelist driver package, abort.");
            }
        }
        return false;
    }

    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);
}
+14 −0
Original line number Diff line number Diff line
@@ -11474,6 +11474,12 @@ public final class Settings {
         */
        public static final String GAME_DRIVER_BLACKLIST = "game_driver_blacklist";
        /**
         * List of blacklists, each blacklist is a blacklist for a specific version of Game Driver.
         * @hide
         */
        public static final String GAME_DRIVER_BLACKLISTS = "game_driver_blacklists";
        /**
         * Apps on the whitelist that are allowed to use Game Driver.
         * The string is a list of application package names, seperated by comma.
@@ -11482,6 +11488,14 @@ public final class Settings {
         */
        public static final String GAME_DRIVER_WHITELIST = "game_driver_whitelist";
        /**
         * List of libraries in sphal accessible by Game Driver
         * The string is a list of library names, separated by colon.
         * i.e. <lib1>:<lib2>:...:<libN>
         * @hide
         */
        public static final String GAME_DRIVER_SPHAL_LIBRARIES = "game_driver_sphal_libraries";
        /**
         * Ordered GPU debug layer list
         * i.e. <layer1>:<layer2>:...:<layerN>
+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) },
};
+5 −0
Original line number Diff line number Diff line
@@ -399,6 +399,11 @@ message GlobalSettingsProto {
        optional SettingProto game_driver_blacklist = 11;
        // Game Driver - List of Apps that are allowed to use Game Driver
        optional SettingProto game_driver_whitelist = 12;
        // Game Driver - List of blacklists, each blacklist is a blacklist for
        // a specific Game Driver version
        optional SettingProto game_driver_blacklists = 14;
        // Game Driver - List of libraries in sphal accessible by Game Driver
        optional SettingProto game_driver_sphal_libraries = 16;
    }
    optional Gpu gpu = 59;

+2 −0
Original line number Diff line number Diff line
@@ -447,8 +447,10 @@ public class SettingsBackupTest {
                    Settings.Global.GAME_DRIVER_ALL_APPS,
                    Settings.Global.GAME_DRIVER_OPT_IN_APPS,
                    Settings.Global.GAME_DRIVER_OPT_OUT_APPS,
                    Settings.Global.GAME_DRIVER_BLACKLISTS,
                    Settings.Global.GAME_DRIVER_BLACKLIST,
                    Settings.Global.GAME_DRIVER_WHITELIST,
                    Settings.Global.GAME_DRIVER_SPHAL_LIBRARIES,
                    Settings.Global.ENABLE_GNSS_RAW_MEAS_FULL_TRACKING,
                    Settings.Global.INSTALL_CARRIER_APP_NOTIFICATION_PERSISTENT,
                    Settings.Global.INSTALL_CARRIER_APP_NOTIFICATION_SLEEP_MILLIS,
Loading