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

Commit 8d72a6b0 authored by Cody Northrop's avatar Cody Northrop
Browse files

Add temp ANGLE rules support

If root is available or the app is debuggable,
check for the following property and use it to load
a rules file instead of the one that comes with ANGLE:

  debug.angle.rules

For example:

  adb shell setprop debug.angle.rules /data/local/tmp/a4a_rules.json

Bug: 80239516
Test: Manual build, ensure rules behave as expected
Test: cts-tradefed run singleCommand cts -m CtsAngleIntegrationHostTestCases
Change-Id: Ie43c67c078ad962ba12f1046a878f79216660755
parent 6d8d9680
Loading
Loading
Loading
Loading
+67 −27
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ import dalvik.system.VMRuntime;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -56,6 +58,7 @@ public class GraphicsEnvironment {
    private static final String PROPERTY_GFX_DRIVER_WHITELIST = "ro.gfx.driver.whitelist.0";
    private static final String ANGLE_PACKAGE_NAME = "com.android.angle";
    private static final String ANGLE_RULES_FILE = "a4a_rules.json";
    private static final String ANGLE_TEMP_RULES = "debug.angle.rules";

    private ClassLoader mClassLoader;
    private String mLayerPath;
@@ -206,7 +209,7 @@ public class GraphicsEnvironment {
                && (!angleEnabledApp.isEmpty() && !packageName.isEmpty())
                && angleEnabledApp.equals(packageName)) {

            if (DEBUG) Log.v(TAG, packageName + " opted in for ANGLE via Developer Setting");
            Log.i(TAG, packageName + " opted in for ANGLE via Developer Setting");

            devOptIn = true;
        }
@@ -233,6 +236,43 @@ public class GraphicsEnvironment {

        if (DEBUG) Log.v(TAG, "ANGLE package libs: " + paths);

        // Look up rules file to pass to ANGLE
        FileDescriptor rulesFd = null;
        long rulesOffset = 0;
        long rulesLength = 0;

        // Check for temporary rules if debuggable or root
        if (isDebuggable(context) || (getCanLoadSystemLibraries() == 1)) {
            String angleTempRules = SystemProperties.get(ANGLE_TEMP_RULES);
            if (angleTempRules != null && !angleTempRules.isEmpty()) {
                Log.i(TAG, "Detected system property " + ANGLE_TEMP_RULES + ": " + angleTempRules);
                File tempRulesFile = new File(angleTempRules);
                if (tempRulesFile.exists()) {
                    Log.i(TAG, angleTempRules + " exists, loading file.");
                    FileInputStream stream = null;
                    try {
                        stream = new FileInputStream(angleTempRules);
                    } catch (FileNotFoundException e) {
                        Log.w(TAG, "Unable to create stream for temp ANGLE rules");
                    }

                    if (stream != null) {
                        try {
                            rulesFd = stream.getFD();
                            rulesOffset = 0;
                            rulesLength = stream.getChannel().size();
                            Log.i(TAG, "Loaded temporary ANGLE rules from " + angleTempRules);
                        } catch (IOException e) {
                            Log.w(TAG, "Failed to get input stream for " + angleTempRules);
                        }
                    }
                }
            }
        }

        // If no temp rules, load the real ones from the APK
        if (rulesFd == null) {

            // Pass the rules file to loader for ANGLE decisions
            AssetManager angleAssets = null;
            try {
@@ -252,9 +292,6 @@ public class GraphicsEnvironment {
                return;
            }

        FileDescriptor rulesFd = null;
        long rulesOffset = 0;
        long rulesLength = 0;
            if (assetsFd != null) {
                rulesFd = assetsFd.getFileDescriptor();
                rulesOffset = assetsFd.getStartOffset();
@@ -263,8 +300,11 @@ public class GraphicsEnvironment {
                Log.w(TAG, "Failed to get file descriptor for " + ANGLE_RULES_FILE);
                return;
            }
        }

        // Further opt-in logic is handled in native, so pass relevant info down
        // TODO: Move the ANGLE selection logic earlier so we don't need to keep these
        //       file descriptors open.
        setAngleInfo(paths, packageName, devOptIn,
                     rulesFd, rulesOffset, rulesLength);
    }