From e638cada12ee42c3409cb5406960e8afea1b2e06 Mon Sep 17 00:00:00 2001 From: Adam Bodnar Date: Fri, 6 Sep 2019 14:31:14 -0700 Subject: [PATCH] Allow injecting layers by manifest metadata Bug: 138204026 Test: Add to the xml in the manifest of the RELEASE build of a GL/Vk app. Add layer(s) using the method described here: https://developer.android.com/ndk/guides/graphics/validation-layer Change-Id: If4809c139f4a667ddf125358274a6d1d030a57d8 --- core/java/android/os/GraphicsEnvironment.java | 56 ++++++++++++------- core/jni/android_os_GraphicsEnvironment.cpp | 5 ++ 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/core/java/android/os/GraphicsEnvironment.java b/core/java/android/os/GraphicsEnvironment.java index 7a70e93b69d54..947b0a1efebd1 100644 --- a/core/java/android/os/GraphicsEnvironment.java +++ b/core/java/android/os/GraphicsEnvironment.java @@ -69,6 +69,8 @@ public class GraphicsEnvironment { private static final String METADATA_DRIVER_BUILD_TIME = "com.android.gamedriver.build_time"; private static final String METADATA_DEVELOPER_DRIVER_ENABLE = "com.android.graphics.developerdriver.enable"; + private static final String METADATA_INJECT_LAYERS_ENABLE = + "com.android.graphics.injectLayers.enable"; private static final String ANGLE_RULES_FILE = "a4a_rules.json"; private static final String ANGLE_TEMP_RULES = "debug.angle.rules"; private static final String ACTION_ANGLE_FOR_ANDROID = "android.app.action.ANGLE_FOR_ANDROID"; @@ -100,14 +102,16 @@ public class GraphicsEnvironment { public void setup(Context context, Bundle coreSettings) { final PackageManager pm = context.getPackageManager(); final String packageName = context.getPackageName(); + final ApplicationInfo appInfoWithMetaData = + getAppInfoWithMetadata(context, pm, packageName); Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupGpuLayers"); - setupGpuLayers(context, coreSettings, pm, packageName); + setupGpuLayers(context, coreSettings, pm, packageName, appInfoWithMetaData); Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS); Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupAngle"); setupAngle(context, coreSettings, pm, packageName); Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS); Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "chooseDriver"); - if (!chooseDriver(context, coreSettings, pm, packageName)) { + if (!chooseDriver(context, coreSettings, pm, packageName, appInfoWithMetaData)) { setGpuStats(SYSTEM_DRIVER_NAME, SYSTEM_DRIVER_VERSION_NAME, SYSTEM_DRIVER_VERSION_CODE, SystemProperties.getLong(PROPERTY_GFX_DRIVER_BUILD_TIME, 0), packageName, getVulkanVersion(pm)); @@ -179,6 +183,14 @@ public class GraphicsEnvironment { return (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) > 0; } + /** + * Check whether application is has set the manifest metadata for layer injection. + */ + private static boolean canInjectLayers(ApplicationInfo ai) { + return (ai.metaData != null && ai.metaData.getBoolean(METADATA_INJECT_LAYERS_ENABLE) + && setInjectLayersPrSetDumpable()); + } + /** * Store the layer paths available to the loader. */ @@ -225,15 +237,16 @@ public class GraphicsEnvironment { * If debuggable, check for additional debug settings */ private void setupGpuLayers( - Context context, Bundle coreSettings, PackageManager pm, String packageName) { + Context context, Bundle coreSettings, PackageManager pm, String packageName, + ApplicationInfo ai) { String layerPaths = ""; // Only enable additional debug functionality if the following conditions are met: - // 1. App is debuggable or device is rooted + // 1. App is debuggable or device is rooted or layer injection metadata flag is true // 2. ENABLE_GPU_DEBUG_LAYERS is true // 3. Package name is equal to GPU_DEBUG_APP - if (isDebuggable(context) || (getCanLoadSystemLibraries() == 1)) { + if (isDebuggable(context) || (getCanLoadSystemLibraries() == 1) || canInjectLayers(ai)) { final int enable = coreSettings.getInt(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, 0); @@ -343,6 +356,20 @@ public class GraphicsEnvironment { return -1; } + private static ApplicationInfo getAppInfoWithMetadata(Context context, + PackageManager pm, String packageName) { + ApplicationInfo ai; + try { + // Get the ApplicationInfo from PackageManager so that metadata fields present. + ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); + } catch (PackageManager.NameNotFoundException e) { + // Unlikely to fail for applications, but in case of failure, fall back to use the + // ApplicationInfo from context directly. + ai = context.getApplicationInfo(); + } + return ai; + } + private static String getDriverForPkg(Context context, Bundle bundle, String packageName) { final String allUseAngle; if (bundle != null) { @@ -693,8 +720,7 @@ public class GraphicsEnvironment { /** * Return the driver package name to use. Return null for system driver. */ - private static String chooseDriverInternal( - Context context, Bundle coreSettings, PackageManager pm, String packageName) { + private static String chooseDriverInternal(Bundle coreSettings, ApplicationInfo ai) { final String gameDriver = SystemProperties.get(PROPERTY_GFX_DRIVER); final boolean hasGameDriver = gameDriver != null && !gameDriver.isEmpty(); @@ -709,15 +735,6 @@ 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; - try { - // Get the ApplicationInfo from PackageManager so that metadata fields present. - ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA); - } catch (PackageManager.NameNotFoundException e) { - // Unlikely to fail for applications, but in case of failure, fall back to use the - // ApplicationInfo from context directly. - 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 null; @@ -797,9 +814,9 @@ public class GraphicsEnvironment { * Choose whether the current process should use the builtin or an updated driver. */ private static boolean chooseDriver( - Context context, Bundle coreSettings, PackageManager pm, String packageName) { - final String driverPackageName = chooseDriverInternal(context, coreSettings, pm, - packageName); + Context context, Bundle coreSettings, PackageManager pm, String packageName, + ApplicationInfo ai) { + final String driverPackageName = chooseDriverInternal(coreSettings, ai); if (driverPackageName == null) { return false; } @@ -911,4 +928,5 @@ public class GraphicsEnvironment { private static native void setAngleInfo(String path, String appPackage, String devOptIn, FileDescriptor rulesFd, long rulesOffset, long rulesLength); private static native boolean getShouldUseAngle(String packageName); + private static native boolean setInjectLayersPrSetDumpable(); } diff --git a/core/jni/android_os_GraphicsEnvironment.cpp b/core/jni/android_os_GraphicsEnvironment.cpp index be9aee410d406..7582cae1a7611 100644 --- a/core/jni/android_os_GraphicsEnvironment.cpp +++ b/core/jni/android_os_GraphicsEnvironment.cpp @@ -85,6 +85,10 @@ void setDebugLayersGLES_native(JNIEnv* env, jobject clazz, jstring layers) { } } +bool setInjectLayersPrSetDumpable_native() { + return android::GraphicsEnv::getInstance().setInjectLayersPrSetDumpable(); +} + void hintActivityLaunch_native(JNIEnv* env, jobject clazz) { android::GraphicsEnv::getInstance().hintActivityLaunch(); } @@ -93,6 +97,7 @@ const JNINativeMethod g_methods[] = { { "getCanLoadSystemLibraries", "()I", reinterpret_cast(getCanLoadSystemLibraries_native) }, { "setDriverPathAndSphalLibraries", "(Ljava/lang/String;Ljava/lang/String;)V", reinterpret_cast(setDriverPathAndSphalLibraries_native) }, { "setGpuStats", "(Ljava/lang/String;Ljava/lang/String;JJLjava/lang/String;I)V", reinterpret_cast(setGpuStats_native) }, + { "setInjectLayersPrSetDumpable", "()Z", reinterpret_cast(setInjectLayersPrSetDumpable_native) }, { "setAngleInfo", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/io/FileDescriptor;JJ)V", reinterpret_cast(setAngleInfo_native) }, { "getShouldUseAngle", "(Ljava/lang/String;)Z", reinterpret_cast(shouldUseAngle_native) }, { "setLayerPaths", "(Ljava/lang/ClassLoader;Ljava/lang/String;)V", reinterpret_cast(setLayerPaths_native) },