blob: 034e6a7a06c4de822ff932a3c638ffa6c2de68a9 [file] [log] [blame]
Jesse Hall79bf3922016-12-12 12:53:02 -08001/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.os;
18
Tim Van Pattenb225b442019-03-07 09:35:26 -070019import android.app.Activity;
20import android.content.BroadcastReceiver;
Tim Van Pattenddc43912018-12-18 17:47:52 -070021import android.content.ContentResolver;
Jesse Hall79bf3922016-12-12 12:53:02 -080022import android.content.Context;
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -070023import android.content.Intent;
Jesse Hall79bf3922016-12-12 12:53:02 -080024import android.content.pm.ApplicationInfo;
Yiwei Zhang33097bf2019-02-04 19:01:21 -080025import android.content.pm.PackageInfo;
Jesse Hall79bf3922016-12-12 12:53:02 -080026import android.content.pm.PackageManager;
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -070027import android.content.pm.ResolveInfo;
Cody Northrop7fd44212018-09-06 10:30:28 -060028import android.content.res.AssetFileDescriptor;
Peiyong Lin88c38eb2018-10-12 15:34:15 -070029import android.content.res.AssetManager;
Cody Northrop86cedcb2017-10-20 09:03:13 -060030import android.provider.Settings;
Jesse Hall79bf3922016-12-12 12:53:02 -080031import android.util.Log;
Tim Van Pattenddc43912018-12-18 17:47:52 -070032import android.widget.Toast;
Jesse Hall79bf3922016-12-12 12:53:02 -080033
34import dalvik.system.VMRuntime;
35
Yiwei Zhange3490fc2019-06-13 14:02:35 -070036import java.io.BufferedReader;
Jesse Hall79bf3922016-12-12 12:53:02 -080037import java.io.File;
Cody Northrop7fd44212018-09-06 10:30:28 -060038import java.io.FileDescriptor;
Cody Northrop8d72a6b2018-11-01 08:20:26 -060039import java.io.FileInputStream;
40import java.io.FileNotFoundException;
Peiyong Lin88c38eb2018-10-12 15:34:15 -070041import java.io.IOException;
Yiwei Zhange3490fc2019-06-13 14:02:35 -070042import java.io.InputStreamReader;
Tim Van Patten3c612842018-11-09 16:48:24 -070043import java.util.ArrayList;
44import java.util.Arrays;
45import java.util.HashMap;
46import java.util.List;
47import java.util.Map;
Jesse Hall79bf3922016-12-12 12:53:02 -080048
49/** @hide */
Cody Northrop86cedcb2017-10-20 09:03:13 -060050public class GraphicsEnvironment {
51
52 private static final GraphicsEnvironment sInstance = new GraphicsEnvironment();
53
54 /**
55 * Returns the shared {@link GraphicsEnvironment} instance.
56 */
57 public static GraphicsEnvironment getInstance() {
58 return sInstance;
59 }
Jesse Hall79bf3922016-12-12 12:53:02 -080060
61 private static final boolean DEBUG = false;
62 private static final String TAG = "GraphicsEnvironment";
Yiwei Zhang33097bf2019-02-04 19:01:21 -080063 private static final String SYSTEM_DRIVER_NAME = "system";
64 private static final String SYSTEM_DRIVER_VERSION_NAME = "";
65 private static final long SYSTEM_DRIVER_VERSION_CODE = 0;
Jesse Hall79bf3922016-12-12 12:53:02 -080066 private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0";
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -070067 private static final String PROPERTY_GFX_DRIVER_PRERELEASE = "ro.gfx.driver.1";
Yiwei Zhangf7b0a5f2019-02-19 16:04:10 -080068 private static final String PROPERTY_GFX_DRIVER_BUILD_TIME = "ro.gfx.driver_build_time";
Yiwei Zhang61f8d222019-03-21 16:14:54 -070069 private static final String METADATA_DRIVER_BUILD_TIME = "com.android.gamedriver.build_time";
Peiyong Lin7797b4d2019-08-09 10:34:39 -070070 private static final String METADATA_DEVELOPER_DRIVER_ENABLE =
71 "com.android.graphics.developerdriver.enable";
Adam Bodnare638cad2019-09-06 14:31:14 -070072 private static final String METADATA_INJECT_LAYERS_ENABLE =
73 "com.android.graphics.injectLayers.enable";
Cody Northrop7fd44212018-09-06 10:30:28 -060074 private static final String ANGLE_RULES_FILE = "a4a_rules.json";
Cody Northrop8d72a6b2018-11-01 08:20:26 -060075 private static final String ANGLE_TEMP_RULES = "debug.angle.rules";
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -070076 private static final String ACTION_ANGLE_FOR_ANDROID = "android.app.action.ANGLE_FOR_ANDROID";
Tim Van Pattenb225b442019-03-07 09:35:26 -070077 private static final String ACTION_ANGLE_FOR_ANDROID_TOAST_MESSAGE =
78 "android.app.action.ANGLE_FOR_ANDROID_TOAST_MESSAGE";
79 private static final String INTENT_KEY_A4A_TOAST_MESSAGE = "A4A Toast Message";
Peiyong Linb76bfe92019-02-13 14:46:54 -080080 private static final String GAME_DRIVER_WHITELIST_ALL = "*";
Yiwei Zhange3490fc2019-06-13 14:02:35 -070081 private static final String GAME_DRIVER_SPHAL_LIBRARIES_FILENAME = "sphal_libraries.txt";
Yiwei Zhang27a27432019-05-06 17:13:05 -070082 private static final int VULKAN_1_0 = 0x00400000;
83 private static final int VULKAN_1_1 = 0x00401000;
Jesse Hall79bf3922016-12-12 12:53:02 -080084
Peiyong Lin9eee3f92019-04-09 17:16:20 -070085 // GAME_DRIVER_ALL_APPS
86 // 0: Default (Invalid values fallback to default as well)
87 // 1: All apps use Game Driver
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -070088 // 2: All apps use Prerelease Driver
89 // 3: All apps use system graphics driver
Peiyong Lin9eee3f92019-04-09 17:16:20 -070090 private static final int GAME_DRIVER_GLOBAL_OPT_IN_DEFAULT = 0;
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -070091 private static final int GAME_DRIVER_GLOBAL_OPT_IN_GAME_DRIVER = 1;
92 private static final int GAME_DRIVER_GLOBAL_OPT_IN_PRERELEASE_DRIVER = 2;
93 private static final int GAME_DRIVER_GLOBAL_OPT_IN_OFF = 3;
Peiyong Lin9eee3f92019-04-09 17:16:20 -070094
Cody Northrop86cedcb2017-10-20 09:03:13 -060095 private ClassLoader mClassLoader;
96 private String mLayerPath;
97 private String mDebugLayerPath;
98
99 /**
100 * Set up GraphicsEnvironment
101 */
Cody Northropdeb43282018-10-04 16:04:05 -0600102 public void setup(Context context, Bundle coreSettings) {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800103 final PackageManager pm = context.getPackageManager();
104 final String packageName = context.getPackageName();
Adam Bodnare638cad2019-09-06 14:31:14 -0700105 final ApplicationInfo appInfoWithMetaData =
106 getAppInfoWithMetadata(context, pm, packageName);
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700107 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupGpuLayers");
Adam Bodnare638cad2019-09-06 14:31:14 -0700108 setupGpuLayers(context, coreSettings, pm, packageName, appInfoWithMetaData);
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700109 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
110 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupAngle");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800111 setupAngle(context, coreSettings, pm, packageName);
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700112 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
113 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "chooseDriver");
Adam Bodnare638cad2019-09-06 14:31:14 -0700114 if (!chooseDriver(context, coreSettings, pm, packageName, appInfoWithMetaData)) {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800115 setGpuStats(SYSTEM_DRIVER_NAME, SYSTEM_DRIVER_VERSION_NAME, SYSTEM_DRIVER_VERSION_CODE,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700116 SystemProperties.getLong(PROPERTY_GFX_DRIVER_BUILD_TIME, 0), packageName,
117 getVulkanVersion(pm));
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800118 }
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700119 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600120 }
121
122 /**
Yiwei Zhange54faf52019-05-08 18:43:11 -0700123 * Hint for GraphicsEnvironment that an activity is launching on the process.
124 * Then the app process is allowed to send stats to GpuStats module.
125 */
126 public static native void hintActivityLaunch();
127
128 /**
Cody Northrop12d320a2019-04-09 20:39:20 -0600129 * Query to determine if ANGLE should be used
130 */
131 public static boolean shouldUseAngle(Context context, Bundle coreSettings,
132 String packageName) {
133 if (packageName.isEmpty()) {
134 Log.v(TAG, "No package name available yet, ANGLE should not be used");
135 return false;
136 }
137
138 final String devOptIn = getDriverForPkg(context, coreSettings, packageName);
139 if (DEBUG) {
140 Log.v(TAG, "ANGLE Developer option for '" + packageName + "' "
141 + "set to: '" + devOptIn + "'");
142 }
143
144 // We only want to use ANGLE if the app is whitelisted or the developer has
145 // explicitly chosen something other than default driver.
146 // The whitelist will be generated by the ANGLE APK at both boot time and
147 // ANGLE update time. It will only include apps mentioned in the rules file.
148 final boolean whitelisted = checkAngleWhitelist(context, coreSettings, packageName);
149 final boolean requested = devOptIn.equals(sDriverMap.get(OpenGlDriverChoice.ANGLE));
150 final boolean useAngle = (whitelisted || requested);
151 if (!useAngle) {
152 return false;
153 }
154
155 if (whitelisted) {
156 Log.v(TAG, "ANGLE whitelist includes " + packageName);
157 }
158 if (requested) {
159 Log.v(TAG, "ANGLE developer option for " + packageName + ": " + devOptIn);
160 }
161
162 return true;
163 }
164
Yiwei Zhang27a27432019-05-06 17:13:05 -0700165 private static int getVulkanVersion(PackageManager pm) {
166 // PackageManager doesn't have an API to retrieve the version of a specific feature, and we
167 // need to avoid retrieving all system features here and looping through them.
168 if (pm.hasSystemFeature(PackageManager.FEATURE_VULKAN_HARDWARE_VERSION, VULKAN_1_1)) {
169 return VULKAN_1_1;
170 }
171
172 if (pm.hasSystemFeature(PackageManager.FEATURE_VULKAN_HARDWARE_VERSION, VULKAN_1_0)) {
173 return VULKAN_1_0;
174 }
175
176 return 0;
177 }
178
Cody Northrop12d320a2019-04-09 20:39:20 -0600179 /**
Adam Bodnare638cad2019-09-06 14:31:14 -0700180 * Check whether application is has set the manifest metadata for layer injection.
181 */
182 private static boolean canInjectLayers(ApplicationInfo ai) {
183 return (ai.metaData != null && ai.metaData.getBoolean(METADATA_INJECT_LAYERS_ENABLE)
184 && setInjectLayersPrSetDumpable());
185 }
186
187 /**
Cody Northrop86cedcb2017-10-20 09:03:13 -0600188 * Store the layer paths available to the loader.
189 */
190 public void setLayerPaths(ClassLoader classLoader,
191 String layerPath,
192 String debugLayerPath) {
193 // We have to store these in the class because they are set up before we
194 // have access to the Context to properly set up GraphicsEnvironment
195 mClassLoader = classLoader;
196 mLayerPath = layerPath;
197 mDebugLayerPath = debugLayerPath;
198 }
199
200 /**
Cody Northropebe6a562018-10-15 07:22:23 -0600201 * Return the debug layer app's on-disk and in-APK lib directories
202 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800203 private static String getDebugLayerAppPaths(PackageManager pm, String app) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800204 final ApplicationInfo appInfo;
Cody Northropebe6a562018-10-15 07:22:23 -0600205 try {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800206 appInfo = pm.getApplicationInfo(app, PackageManager.MATCH_ALL);
Cody Northropebe6a562018-10-15 07:22:23 -0600207 } catch (PackageManager.NameNotFoundException e) {
208 Log.w(TAG, "Debug layer app '" + app + "' not installed");
209
210 return null;
211 }
212
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800213 final String abi = chooseAbi(appInfo);
Cody Northropebe6a562018-10-15 07:22:23 -0600214
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800215 final StringBuilder sb = new StringBuilder();
Cody Northropebe6a562018-10-15 07:22:23 -0600216 sb.append(appInfo.nativeLibraryDir)
217 .append(File.pathSeparator);
218 sb.append(appInfo.sourceDir)
219 .append("!/lib/")
220 .append(abi);
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800221 final String paths = sb.toString();
Cody Northropebe6a562018-10-15 07:22:23 -0600222
223 if (DEBUG) Log.v(TAG, "Debug layer app libs: " + paths);
224
225 return paths;
226 }
227
228 /**
Cody Northrop86cedcb2017-10-20 09:03:13 -0600229 * Set up layer search paths for all apps
230 * If debuggable, check for additional debug settings
231 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800232 private void setupGpuLayers(
Adam Bodnare638cad2019-09-06 14:31:14 -0700233 Context context, Bundle coreSettings, PackageManager pm, String packageName,
234 ApplicationInfo ai) {
Cody Northrop86cedcb2017-10-20 09:03:13 -0600235 String layerPaths = "";
236
237 // Only enable additional debug functionality if the following conditions are met:
Adam Bodnare638cad2019-09-06 14:31:14 -0700238 // 1. App is debuggable or device is rooted or layer injection metadata flag is true
Cody Northrop86cedcb2017-10-20 09:03:13 -0600239 // 2. ENABLE_GPU_DEBUG_LAYERS is true
240 // 3. Package name is equal to GPU_DEBUG_APP
241
Yiwei Zhang097a3062019-11-08 12:16:28 -0800242 if (isDebuggable() || canInjectLayers(ai)) {
Cody Northrop86cedcb2017-10-20 09:03:13 -0600243
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800244 final int enable = coreSettings.getInt(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, 0);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600245
246 if (enable != 0) {
247
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800248 final String gpuDebugApp = coreSettings.getString(Settings.Global.GPU_DEBUG_APP);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600249
Cody Northrop86cedcb2017-10-20 09:03:13 -0600250 if ((gpuDebugApp != null && packageName != null)
251 && (!gpuDebugApp.isEmpty() && !packageName.isEmpty())
252 && gpuDebugApp.equals(packageName)) {
253 Log.i(TAG, "GPU debug layers enabled for " + packageName);
254
255 // Prepend the debug layer path as a searchable path.
256 // This will ensure debug layers added will take precedence over
257 // the layers specified by the app.
258 layerPaths = mDebugLayerPath + ":";
259
Cody Northropebe6a562018-10-15 07:22:23 -0600260 // If there is a debug layer app specified, add its path.
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800261 final String gpuDebugLayerApp =
Cody Northropebe6a562018-10-15 07:22:23 -0600262 coreSettings.getString(Settings.Global.GPU_DEBUG_LAYER_APP);
263
264 if (gpuDebugLayerApp != null && !gpuDebugLayerApp.isEmpty()) {
265 Log.i(TAG, "GPU debug layer app: " + gpuDebugLayerApp);
Cody Northropf959f6b2019-04-08 09:50:23 -0600266 // If a colon is present, treat this as multiple apps, so Vulkan and GLES
267 // layer apps can be provided at the same time.
268 String[] layerApps = gpuDebugLayerApp.split(":");
269 for (int i = 0; i < layerApps.length; i++) {
270 String paths = getDebugLayerAppPaths(pm, layerApps[i]);
271 if (paths != null) {
272 // Append the path so files placed in the app's base directory will
273 // override the external path
274 layerPaths += paths + ":";
275 }
Cody Northropebe6a562018-10-15 07:22:23 -0600276 }
277 }
278
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800279 final String layers = coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600280
Cody Northrop0fa1d222018-10-23 13:13:21 -0600281 Log.i(TAG, "Vulkan debug layer list: " + layers);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600282 if (layers != null && !layers.isEmpty()) {
283 setDebugLayers(layers);
284 }
Cody Northrop0fa1d222018-10-23 13:13:21 -0600285
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800286 final String layersGLES =
Cody Northrop0fa1d222018-10-23 13:13:21 -0600287 coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS_GLES);
288
289 Log.i(TAG, "GLES debug layer list: " + layersGLES);
290 if (layersGLES != null && !layersGLES.isEmpty()) {
291 setDebugLayersGLES(layersGLES);
292 }
Cody Northrop86cedcb2017-10-20 09:03:13 -0600293 }
294 }
Cody Northrop86cedcb2017-10-20 09:03:13 -0600295 }
296
297 // Include the app's lib directory in all cases
298 layerPaths += mLayerPath;
299
300 setLayerPaths(mClassLoader, layerPaths);
301 }
302
Tim Van Patten3c612842018-11-09 16:48:24 -0700303 enum OpenGlDriverChoice {
304 DEFAULT,
305 NATIVE,
306 ANGLE
307 }
308
309 private static final Map<OpenGlDriverChoice, String> sDriverMap = buildMap();
310 private static Map<OpenGlDriverChoice, String> buildMap() {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800311 final Map<OpenGlDriverChoice, String> map = new HashMap<>();
Tim Van Patten3c612842018-11-09 16:48:24 -0700312 map.put(OpenGlDriverChoice.DEFAULT, "default");
313 map.put(OpenGlDriverChoice.ANGLE, "angle");
314 map.put(OpenGlDriverChoice.NATIVE, "native");
315
316 return map;
317 }
318
319
Tim Van Pattenddc43912018-12-18 17:47:52 -0700320 private static List<String> getGlobalSettingsString(ContentResolver contentResolver,
321 Bundle bundle,
322 String globalSetting) {
323 final List<String> valueList;
324 final String settingsValue;
325
326 if (bundle != null) {
327 settingsValue = bundle.getString(globalSetting);
328 } else {
329 settingsValue = Settings.Global.getString(contentResolver, globalSetting);
330 }
Tim Van Patten3c612842018-11-09 16:48:24 -0700331
332 if (settingsValue != null) {
333 valueList = new ArrayList<>(Arrays.asList(settingsValue.split(",")));
334 } else {
335 valueList = new ArrayList<>();
336 }
337
338 return valueList;
339 }
340
341 private static int getGlobalSettingsPkgIndex(String pkgName,
342 List<String> globalSettingsDriverPkgs) {
343 for (int pkgIndex = 0; pkgIndex < globalSettingsDriverPkgs.size(); pkgIndex++) {
344 if (globalSettingsDriverPkgs.get(pkgIndex).equals(pkgName)) {
345 return pkgIndex;
346 }
347 }
348
349 return -1;
350 }
351
Adam Bodnare638cad2019-09-06 14:31:14 -0700352 private static ApplicationInfo getAppInfoWithMetadata(Context context,
353 PackageManager pm, String packageName) {
354 ApplicationInfo ai;
355 try {
356 // Get the ApplicationInfo from PackageManager so that metadata fields present.
357 ai = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
358 } catch (PackageManager.NameNotFoundException e) {
359 // Unlikely to fail for applications, but in case of failure, fall back to use the
360 // ApplicationInfo from context directly.
361 ai = context.getApplicationInfo();
362 }
363 return ai;
364 }
365
Tim Van Pattenddc43912018-12-18 17:47:52 -0700366 private static String getDriverForPkg(Context context, Bundle bundle, String packageName) {
367 final String allUseAngle;
368 if (bundle != null) {
369 allUseAngle =
370 bundle.getString(Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE);
371 } else {
372 ContentResolver contentResolver = context.getContentResolver();
373 allUseAngle = Settings.Global.getString(contentResolver,
374 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE);
375 }
Tim Van Pattenff9476b2018-12-11 15:41:25 -0700376 if ((allUseAngle != null) && allUseAngle.equals("1")) {
377 return sDriverMap.get(OpenGlDriverChoice.ANGLE);
Tim Van Patten3c612842018-11-09 16:48:24 -0700378 }
379
Tim Van Pattenddc43912018-12-18 17:47:52 -0700380 final ContentResolver contentResolver = context.getContentResolver();
381 final List<String> globalSettingsDriverPkgs =
382 getGlobalSettingsString(contentResolver, bundle,
383 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS);
384 final List<String> globalSettingsDriverValues =
385 getGlobalSettingsString(contentResolver, bundle,
386 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES);
Tim Van Patten3c612842018-11-09 16:48:24 -0700387
388 // Make sure we have a good package name
389 if ((packageName == null) || (packageName.isEmpty())) {
390 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
391 }
392 // Make sure we have good settings to use
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700393 if (globalSettingsDriverPkgs.size() != globalSettingsDriverValues.size()) {
Tim Van Patten3c612842018-11-09 16:48:24 -0700394 Log.w(TAG,
395 "Global.Settings values are invalid: "
396 + "globalSettingsDriverPkgs.size = "
397 + globalSettingsDriverPkgs.size() + ", "
398 + "globalSettingsDriverValues.size = "
399 + globalSettingsDriverValues.size());
400 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
401 }
402
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800403 final int pkgIndex = getGlobalSettingsPkgIndex(packageName, globalSettingsDriverPkgs);
Tim Van Patten3c612842018-11-09 16:48:24 -0700404
405 if (pkgIndex < 0) {
406 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
407 }
408
409 return globalSettingsDriverValues.get(pkgIndex);
410 }
411
Jesse Hallc37984f2017-05-23 16:55:08 -0700412 /**
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700413 * Get the ANGLE package name.
414 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800415 private String getAnglePackageName(PackageManager pm) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800416 final Intent intent = new Intent(ACTION_ANGLE_FOR_ANDROID);
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700417
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800418 final List<ResolveInfo> resolveInfos =
419 pm.queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY);
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700420 if (resolveInfos.size() != 1) {
421 Log.e(TAG, "Invalid number of ANGLE packages. Required: 1, Found: "
422 + resolveInfos.size());
423 for (ResolveInfo resolveInfo : resolveInfos) {
424 Log.e(TAG, "Found ANGLE package: " + resolveInfo.activityInfo.packageName);
425 }
426 return "";
427 }
428
429 // Must be exactly 1 ANGLE PKG found to get here.
430 return resolveInfos.get(0).activityInfo.packageName;
431 }
432
433 /**
Cody Northropdc2fd942019-03-28 19:27:19 -0600434 * Check for ANGLE debug package, but only for apps that can load them (dumpable)
435 */
436 private String getAngleDebugPackage(Context context, Bundle coreSettings) {
Yiwei Zhang097a3062019-11-08 12:16:28 -0800437 if (isDebuggable()) {
Tim Van Patten5973ec92019-04-08 17:30:17 -0600438 String debugPackage;
Cody Northropdc2fd942019-03-28 19:27:19 -0600439
Tim Van Patten5973ec92019-04-08 17:30:17 -0600440 if (coreSettings != null) {
441 debugPackage =
442 coreSettings.getString(Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE);
443 } else {
444 ContentResolver contentResolver = context.getContentResolver();
445 debugPackage = Settings.Global.getString(contentResolver,
446 Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE);
447 }
Cody Northropdc2fd942019-03-28 19:27:19 -0600448
449 if ((debugPackage != null) && (!debugPackage.isEmpty())) {
450 return debugPackage;
451 }
452 }
Tim Van Patten5973ec92019-04-08 17:30:17 -0600453
Cody Northropdc2fd942019-03-28 19:27:19 -0600454 return "";
455 }
456
457 /**
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700458 * Attempt to setup ANGLE with a temporary rules file.
459 * True: Temporary rules file was loaded.
460 * False: Temporary rules file was *not* loaded.
461 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700462 private static boolean setupAngleWithTempRulesFile(Context context,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700463 String packageName,
464 String paths,
465 String devOptIn) {
Tim Van Patten3df66432019-01-14 15:56:12 -0700466 /**
467 * We only want to load a temp rules file for:
468 * - apps that are marked 'debuggable' in their manifest
469 * - devices that are running a userdebug build (ro.debuggable) or can inject libraries for
470 * debugging (PR_SET_DUMPABLE).
471 */
Yiwei Zhang097a3062019-11-08 12:16:28 -0800472 if (!isDebuggable()) {
473 Log.v(TAG, "Skipping loading temporary rules file");
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700474 return false;
475 }
476
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800477 final String angleTempRules = SystemProperties.get(ANGLE_TEMP_RULES);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700478
479 if ((angleTempRules == null) || angleTempRules.isEmpty()) {
480 Log.v(TAG, "System property '" + ANGLE_TEMP_RULES + "' is not set or is empty");
481 return false;
482 }
483
484 Log.i(TAG, "Detected system property " + ANGLE_TEMP_RULES + ": " + angleTempRules);
485
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800486 final File tempRulesFile = new File(angleTempRules);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700487 if (tempRulesFile.exists()) {
488 Log.i(TAG, angleTempRules + " exists, loading file.");
489 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800490 final FileInputStream stream = new FileInputStream(angleTempRules);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700491
492 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800493 final FileDescriptor rulesFd = stream.getFD();
494 final long rulesOffset = 0;
495 final long rulesLength = stream.getChannel().size();
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700496 Log.i(TAG, "Loaded temporary ANGLE rules from " + angleTempRules);
497
498 setAngleInfo(paths, packageName, devOptIn, rulesFd, rulesOffset, rulesLength);
499
500 stream.close();
501
502 // We successfully setup ANGLE, so return with good status
503 return true;
504 } catch (IOException e) {
505 Log.w(TAG, "Hit IOException thrown by FileInputStream: " + e);
506 }
507 } catch (FileNotFoundException e) {
508 Log.w(TAG, "Temp ANGLE rules file not found: " + e);
509 } catch (SecurityException e) {
510 Log.w(TAG, "Temp ANGLE rules file not accessible: " + e);
511 }
512 }
513
514 return false;
515 }
516
517 /**
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700518 * Attempt to setup ANGLE with a rules file loaded from the ANGLE APK.
519 * True: APK rules file was loaded.
520 * False: APK rules file was *not* loaded.
521 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700522 private static boolean setupAngleRulesApk(String anglePkgName,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700523 ApplicationInfo angleInfo,
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800524 PackageManager pm,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700525 String packageName,
526 String paths,
527 String devOptIn) {
528 // Pass the rules file to loader for ANGLE decisions
529 try {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800530 final AssetManager angleAssets = pm.getResourcesForApplication(angleInfo).getAssets();
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700531
532 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800533 final AssetFileDescriptor assetsFd = angleAssets.openFd(ANGLE_RULES_FILE);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700534
535 setAngleInfo(paths, packageName, devOptIn, assetsFd.getFileDescriptor(),
536 assetsFd.getStartOffset(), assetsFd.getLength());
537
538 assetsFd.close();
539
540 return true;
541 } catch (IOException e) {
542 Log.w(TAG, "Failed to get AssetFileDescriptor for " + ANGLE_RULES_FILE
543 + " from '" + anglePkgName + "': " + e);
544 }
545 } catch (PackageManager.NameNotFoundException e) {
546 Log.w(TAG, "Failed to get AssetManager for '" + anglePkgName + "': " + e);
547 }
548
549 return false;
550 }
551
552 /**
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700553 * Pull ANGLE whitelist from GlobalSettings and compare against current package
554 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700555 private static boolean checkAngleWhitelist(Context context, Bundle bundle, String packageName) {
556 final ContentResolver contentResolver = context.getContentResolver();
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800557 final List<String> angleWhitelist =
Tim Van Pattenddc43912018-12-18 17:47:52 -0700558 getGlobalSettingsString(contentResolver, bundle,
559 Settings.Global.GLOBAL_SETTINGS_ANGLE_WHITELIST);
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700560
Cody Northrop12d320a2019-04-09 20:39:20 -0600561 if (DEBUG) Log.v(TAG, "ANGLE whitelist: " + angleWhitelist);
562
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700563 return angleWhitelist.contains(packageName);
564 }
565
566 /**
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600567 * Pass ANGLE details down to trigger enable logic
Tim Van Pattenddc43912018-12-18 17:47:52 -0700568 *
569 * @param context
570 * @param bundle
571 * @param packageName
572 * @return true: ANGLE setup successfully
573 * false: ANGLE not setup (not on whitelist, ANGLE not present, etc.)
Cody Northrop73c05042018-04-16 13:23:51 -0600574 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700575 public boolean setupAngle(Context context, Bundle bundle, PackageManager pm,
576 String packageName) {
Cody Northrop12d320a2019-04-09 20:39:20 -0600577
578 if (!shouldUseAngle(context, bundle, packageName)) {
Tim Van Pattenddc43912018-12-18 17:47:52 -0700579 return false;
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700580 }
Cody Northrop73c05042018-04-16 13:23:51 -0600581
Cody Northrop41149c32019-05-06 11:36:40 -0600582 ApplicationInfo angleInfo = null;
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700583
Cody Northrop41149c32019-05-06 11:36:40 -0600584 // If the developer has specified a debug package over ADB, attempt to find it
585 String anglePkgName = getAngleDebugPackage(context, bundle);
586 if (!anglePkgName.isEmpty()) {
587 Log.i(TAG, "ANGLE debug package enabled: " + anglePkgName);
Cody Northropdc2fd942019-03-28 19:27:19 -0600588 try {
589 // Note the debug package does not have to be pre-installed
Cody Northrop41149c32019-05-06 11:36:40 -0600590 angleInfo = pm.getApplicationInfo(anglePkgName, 0);
Cody Northropdc2fd942019-03-28 19:27:19 -0600591 } catch (PackageManager.NameNotFoundException e) {
Cody Northrop41149c32019-05-06 11:36:40 -0600592 Log.w(TAG, "ANGLE debug package '" + anglePkgName + "' not installed");
Cody Northropdc2fd942019-03-28 19:27:19 -0600593 return false;
594 }
Cody Northrop41149c32019-05-06 11:36:40 -0600595 }
596
597 // Otherwise, check to see if ANGLE is properly installed
598 if (angleInfo == null) {
599 anglePkgName = getAnglePackageName(pm);
600 if (!anglePkgName.isEmpty()) {
601 Log.i(TAG, "ANGLE package enabled: " + anglePkgName);
602 try {
603 // Production ANGLE libraries must be pre-installed as a system app
604 angleInfo = pm.getApplicationInfo(anglePkgName,
605 PackageManager.MATCH_SYSTEM_ONLY);
606 } catch (PackageManager.NameNotFoundException e) {
607 Log.w(TAG, "ANGLE package '" + anglePkgName + "' not installed");
608 return false;
609 }
610 } else {
611 Log.e(TAG, "Failed to find ANGLE package.");
Cody Northropdc2fd942019-03-28 19:27:19 -0600612 return false;
613 }
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600614 }
615
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800616 final String abi = chooseAbi(angleInfo);
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600617
618 // Build a path that includes installed native libs and APK
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800619 final String paths = angleInfo.nativeLibraryDir
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700620 + File.pathSeparator
621 + angleInfo.sourceDir
622 + "!/lib/"
623 + abi;
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600624
625 if (DEBUG) Log.v(TAG, "ANGLE package libs: " + paths);
626
Cody Northrop12d320a2019-04-09 20:39:20 -0600627 // If the user has set the developer option to something other than default,
628 // we need to call setupAngleRulesApk() with the package name and the developer
629 // option value (native/angle/other). Then later when we are actually trying to
630 // load a driver, GraphicsEnv::getShouldUseAngle() has seen the package name before
631 // and can confidently answer yes/no based on the previously set developer
632 // option value.
633 final String devOptIn = getDriverForPkg(context, bundle, packageName);
634
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700635 if (setupAngleWithTempRulesFile(context, packageName, paths, devOptIn)) {
636 // We setup ANGLE with a temp rules file, so we're done here.
Tim Van Pattenddc43912018-12-18 17:47:52 -0700637 return true;
Cody Northrop8d72a6b2018-11-01 08:20:26 -0600638 }
639
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800640 if (setupAngleRulesApk(anglePkgName, angleInfo, pm, packageName, paths, devOptIn)) {
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700641 // We setup ANGLE with rules from the APK, so we're done here.
Tim Van Pattenddc43912018-12-18 17:47:52 -0700642 return true;
643 }
644
645 return false;
646 }
647
648 /**
649 * Determine if the "ANGLE In Use" dialog box should be shown.
650 */
651 private boolean shouldShowAngleInUseDialogBox(Context context) {
652 try {
653 ContentResolver contentResolver = context.getContentResolver();
654 final int showDialogBox = Settings.Global.getInt(contentResolver,
655 Settings.Global.GLOBAL_SETTINGS_SHOW_ANGLE_IN_USE_DIALOG_BOX);
656
657 return (showDialogBox == 1);
658 } catch (Settings.SettingNotFoundException | SecurityException e) {
659 // Do nothing and move on
660 }
661
662 // No setting, so assume false
663 return false;
664 }
665
666 /**
Cody Northrop12d320a2019-04-09 20:39:20 -0600667 * Determine if ANGLE will be used and setup the environment
Tim Van Pattenddc43912018-12-18 17:47:52 -0700668 */
Cody Northrop12d320a2019-04-09 20:39:20 -0600669 private boolean setupAndUseAngle(Context context, String packageName) {
Tim Van Pattenddc43912018-12-18 17:47:52 -0700670 // Need to make sure we are evaluating ANGLE usage for the correct circumstances
671 if (!setupAngle(context, null, context.getPackageManager(), packageName)) {
Cody Northrop41149c32019-05-06 11:36:40 -0600672 Log.v(TAG, "Package '" + packageName + "' should not use ANGLE");
Tim Van Pattenddc43912018-12-18 17:47:52 -0700673 return false;
674 }
675
676 final boolean useAngle = getShouldUseAngle(packageName);
677 Log.v(TAG, "Package '" + packageName + "' should use ANGLE = '" + useAngle + "'");
678
679 return useAngle;
680 }
681
682 /**
683 * Show the ANGLE in Use Dialog Box
684 * @param context
685 */
686 public void showAngleInUseDialogBox(Context context) {
687 final String packageName = context.getPackageName();
688
Cody Northrop12d320a2019-04-09 20:39:20 -0600689 if (shouldShowAngleInUseDialogBox(context) && setupAndUseAngle(context, packageName)) {
Tim Van Pattenb225b442019-03-07 09:35:26 -0700690 final Intent intent = new Intent(ACTION_ANGLE_FOR_ANDROID_TOAST_MESSAGE);
691 String anglePkg = getAnglePackageName(context.getPackageManager());
692 intent.setPackage(anglePkg);
693
694 context.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
695 @Override
696 public void onReceive(Context context, Intent intent) {
697 Bundle results = getResultExtras(true);
698
699 String toastMsg = results.getString(INTENT_KEY_A4A_TOAST_MESSAGE);
700 final Toast toast = Toast.makeText(context, toastMsg, Toast.LENGTH_LONG);
701 toast.show();
702 }
703 }, null, Activity.RESULT_OK, null, null);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700704 }
Cody Northrop73c05042018-04-16 13:23:51 -0600705 }
706
707 /**
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700708 * Return the driver package name to use. Return null for system driver.
709 */
Adam Bodnare638cad2019-09-06 14:31:14 -0700710 private static String chooseDriverInternal(Bundle coreSettings, ApplicationInfo ai) {
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700711 final String gameDriver = SystemProperties.get(PROPERTY_GFX_DRIVER);
712 final boolean hasGameDriver = gameDriver != null && !gameDriver.isEmpty();
713
714 final String prereleaseDriver = SystemProperties.get(PROPERTY_GFX_DRIVER_PRERELEASE);
715 final boolean hasPrereleaseDriver = prereleaseDriver != null && !prereleaseDriver.isEmpty();
716
717 if (!hasGameDriver && !hasPrereleaseDriver) {
718 if (DEBUG) Log.v(TAG, "Neither Game Driver nor prerelease driver is supported.");
719 return null;
720 }
721
722 // To minimize risk of driver updates crippling the device beyond user repair, never use an
723 // updated driver for privileged or non-updated system apps. Presumably pre-installed apps
724 // were tested thoroughly with the pre-installed driver.
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700725 if (ai.isPrivilegedApp() || (ai.isSystemApp() && !ai.isUpdatedSystemApp())) {
726 if (DEBUG) Log.v(TAG, "Ignoring driver package for privileged/non-updated system app.");
727 return null;
728 }
729
Peiyong Lin7797b4d2019-08-09 10:34:39 -0700730 final boolean enablePrereleaseDriver =
731 (ai.metaData != null && ai.metaData.getBoolean(METADATA_DEVELOPER_DRIVER_ENABLE))
Yiwei Zhang097a3062019-11-08 12:16:28 -0800732 || isDebuggable();
Peiyong Lin7797b4d2019-08-09 10:34:39 -0700733
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700734 // Priority for Game Driver settings global on confliction (Higher priority comes first):
735 // 1. GAME_DRIVER_ALL_APPS
736 // 2. GAME_DRIVER_OPT_OUT_APPS
737 // 3. GAME_DRIVER_PRERELEASE_OPT_IN_APPS
738 // 4. GAME_DRIVER_OPT_IN_APPS
739 // 5. GAME_DRIVER_BLACKLIST
740 // 6. GAME_DRIVER_WHITELIST
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -0700741 switch (coreSettings.getInt(Settings.Global.GAME_DRIVER_ALL_APPS, 0)) {
742 case GAME_DRIVER_GLOBAL_OPT_IN_OFF:
743 if (DEBUG) Log.v(TAG, "Game Driver is turned off on this device.");
744 return null;
745 case GAME_DRIVER_GLOBAL_OPT_IN_GAME_DRIVER:
746 if (DEBUG) Log.v(TAG, "All apps opt in to use Game Driver.");
747 return hasGameDriver ? gameDriver : null;
748 case GAME_DRIVER_GLOBAL_OPT_IN_PRERELEASE_DRIVER:
749 if (DEBUG) Log.v(TAG, "All apps opt in to use prerelease driver.");
Peiyong Lin7797b4d2019-08-09 10:34:39 -0700750 return hasPrereleaseDriver && enablePrereleaseDriver ? prereleaseDriver : null;
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -0700751 case GAME_DRIVER_GLOBAL_OPT_IN_DEFAULT:
752 default:
753 break;
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700754 }
755
756 final String appPackageName = ai.packageName;
757 if (getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_OPT_OUT_APPS)
758 .contains(appPackageName)) {
759 if (DEBUG) Log.v(TAG, "App opts out for Game Driver.");
760 return null;
761 }
762
763 if (getGlobalSettingsString(
764 null, coreSettings, Settings.Global.GAME_DRIVER_PRERELEASE_OPT_IN_APPS)
765 .contains(appPackageName)) {
766 if (DEBUG) Log.v(TAG, "App opts in for prerelease Game Driver.");
Peiyong Lin7797b4d2019-08-09 10:34:39 -0700767 return hasPrereleaseDriver && enablePrereleaseDriver ? prereleaseDriver : null;
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700768 }
769
770 // Early return here since the rest logic is only for Game Driver.
771 if (!hasGameDriver) {
772 if (DEBUG) Log.v(TAG, "Game Driver is not supported on the device.");
773 return null;
774 }
775
776 final boolean isOptIn =
777 getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_OPT_IN_APPS)
778 .contains(appPackageName);
779 final List<String> whitelist =
780 getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_WHITELIST);
781 if (!isOptIn && whitelist.indexOf(GAME_DRIVER_WHITELIST_ALL) != 0
782 && !whitelist.contains(appPackageName)) {
783 if (DEBUG) Log.v(TAG, "App is not on the whitelist for Game Driver.");
784 return null;
785 }
786
787 // If the application is not opted-in, then check whether it's on the blacklist,
788 // terminate early if it's on the blacklist and fallback to system driver.
789 if (!isOptIn
790 && getGlobalSettingsString(
791 null, coreSettings, Settings.Global.GAME_DRIVER_BLACKLIST)
792 .contains(appPackageName)) {
793 if (DEBUG) Log.v(TAG, "App is on the blacklist for Game Driver.");
794 return null;
795 }
796
797 return gameDriver;
798 }
799
800 /**
Jesse Hallc37984f2017-05-23 16:55:08 -0700801 * Choose whether the current process should use the builtin or an updated driver.
Jesse Hallc37984f2017-05-23 16:55:08 -0700802 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800803 private static boolean chooseDriver(
Adam Bodnare638cad2019-09-06 14:31:14 -0700804 Context context, Bundle coreSettings, PackageManager pm, String packageName,
805 ApplicationInfo ai) {
806 final String driverPackageName = chooseDriverInternal(coreSettings, ai);
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700807 if (driverPackageName == null) {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800808 return false;
Jesse Hall79bf3922016-12-12 12:53:02 -0800809 }
Peiyong Lin2d5a2bb2018-11-07 21:10:07 -0800810
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800811 final PackageInfo driverPackageInfo;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800812 try {
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800813 driverPackageInfo = pm.getPackageInfo(driverPackageName,
814 PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_META_DATA);
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800815 } catch (PackageManager.NameNotFoundException e) {
816 Log.w(TAG, "driver package '" + driverPackageName + "' not installed");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800817 return false;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800818 }
819
820 // O drivers are restricted to the sphal linker namespace, so don't try to use
821 // packages unless they declare they're compatible with that restriction.
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800822 final ApplicationInfo driverAppInfo = driverPackageInfo.applicationInfo;
823 if (driverAppInfo.targetSdkVersion < Build.VERSION_CODES.O) {
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800824 if (DEBUG) {
825 Log.w(TAG, "updated driver package is not known to be compatible with O");
826 }
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800827 return false;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800828 }
829
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800830 final String abi = chooseAbi(driverAppInfo);
Jesse Hall79bf3922016-12-12 12:53:02 -0800831 if (abi == null) {
832 if (DEBUG) {
833 // This is the normal case for the pre-installed empty driver package, don't spam
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800834 if (driverAppInfo.isUpdatedSystemApp()) {
Jesse Hall79bf3922016-12-12 12:53:02 -0800835 Log.w(TAG, "updated driver package has no compatible native libraries");
836 }
837 }
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800838 return false;
Jesse Hall79bf3922016-12-12 12:53:02 -0800839 }
Jesse Hall79bf3922016-12-12 12:53:02 -0800840
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800841 final StringBuilder sb = new StringBuilder();
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800842 sb.append(driverAppInfo.nativeLibraryDir)
Jesse Hall79bf3922016-12-12 12:53:02 -0800843 .append(File.pathSeparator);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800844 sb.append(driverAppInfo.sourceDir)
Jesse Hall79bf3922016-12-12 12:53:02 -0800845 .append("!/lib/")
846 .append(abi);
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800847 final String paths = sb.toString();
Yiwei Zhange3490fc2019-06-13 14:02:35 -0700848 final String sphalLibraries = getSphalLibraries(context, driverPackageName);
Yiwei Zhang1c51f052019-02-14 12:05:47 -0800849 if (DEBUG) {
850 Log.v(TAG,
851 "gfx driver package search path: " + paths
852 + ", required sphal libraries: " + sphalLibraries);
853 }
854 setDriverPathAndSphalLibraries(paths, sphalLibraries);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800855
Yiwei Zhangf7b0a5f2019-02-19 16:04:10 -0800856 if (driverAppInfo.metaData == null) {
857 throw new NullPointerException("apk's meta-data cannot be null");
858 }
859
860 final String driverBuildTime = driverAppInfo.metaData.getString(METADATA_DRIVER_BUILD_TIME);
861 if (driverBuildTime == null || driverBuildTime.isEmpty()) {
Yiwei Zhang61f8d222019-03-21 16:14:54 -0700862 throw new IllegalArgumentException("com.android.gamedriver.build_time is not set");
Yiwei Zhangf7b0a5f2019-02-19 16:04:10 -0800863 }
864 // driver_build_time in the meta-data is in "L<Unix epoch timestamp>" format. e.g. L123456.
865 // Long.parseLong will throw if the meta-data "driver_build_time" is not set properly.
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800866 setGpuStats(driverPackageName, driverPackageInfo.versionName, driverAppInfo.longVersionCode,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700867 Long.parseLong(driverBuildTime.substring(1)), packageName, 0);
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800868
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800869 return true;
Jesse Hall79bf3922016-12-12 12:53:02 -0800870 }
871
Jesse Hall79bf3922016-12-12 12:53:02 -0800872 private static String chooseAbi(ApplicationInfo ai) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800873 final String isa = VMRuntime.getCurrentInstructionSet();
Jesse Hall79bf3922016-12-12 12:53:02 -0800874 if (ai.primaryCpuAbi != null &&
875 isa.equals(VMRuntime.getInstructionSet(ai.primaryCpuAbi))) {
876 return ai.primaryCpuAbi;
877 }
878 if (ai.secondaryCpuAbi != null &&
879 isa.equals(VMRuntime.getInstructionSet(ai.secondaryCpuAbi))) {
880 return ai.secondaryCpuAbi;
881 }
882 return null;
883 }
884
Yiwei Zhange3490fc2019-06-13 14:02:35 -0700885 private static String getSphalLibraries(Context context, String driverPackageName) {
886 try {
887 final Context driverContext =
888 context.createPackageContext(driverPackageName, Context.CONTEXT_RESTRICTED);
889 final BufferedReader reader = new BufferedReader(new InputStreamReader(
890 driverContext.getAssets().open(GAME_DRIVER_SPHAL_LIBRARIES_FILENAME)));
891 final ArrayList<String> assetStrings = new ArrayList<>();
892 for (String assetString; (assetString = reader.readLine()) != null;) {
893 assetStrings.add(assetString);
894 }
895 return String.join(":", assetStrings);
896 } catch (PackageManager.NameNotFoundException e) {
897 if (DEBUG) {
898 Log.w(TAG, "Driver package '" + driverPackageName + "' not installed");
899 }
900 } catch (IOException e) {
901 if (DEBUG) {
902 Log.w(TAG, "Failed to load '" + GAME_DRIVER_SPHAL_LIBRARIES_FILENAME + "'");
903 }
904 }
905 return "";
906 }
907
Yiwei Zhang097a3062019-11-08 12:16:28 -0800908 private static native boolean isDebuggable();
Cody Northrop86cedcb2017-10-20 09:03:13 -0600909 private static native void setLayerPaths(ClassLoader classLoader, String layerPaths);
910 private static native void setDebugLayers(String layers);
Cody Northrop0fa1d222018-10-23 13:13:21 -0600911 private static native void setDebugLayersGLES(String layers);
Yiwei Zhang1c51f052019-02-14 12:05:47 -0800912 private static native void setDriverPathAndSphalLibraries(String path, String sphalLibraries);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800913 private static native void setGpuStats(String driverPackageName, String driverVersionName,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700914 long driverVersionCode, long driverBuildTime, String appPackageName, int vulkanVersion);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800915 private static native void setAngleInfo(String path, String appPackage, String devOptIn,
916 FileDescriptor rulesFd, long rulesOffset, long rulesLength);
Tim Van Pattenddc43912018-12-18 17:47:52 -0700917 private static native boolean getShouldUseAngle(String packageName);
Adam Bodnare638cad2019-09-06 14:31:14 -0700918 private static native boolean setInjectLayersPrSetDumpable();
Jesse Hall79bf3922016-12-12 12:53:02 -0800919}