blob: ce1942cc6d916d71ddcb21416b6f591a95d47d94 [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";
Cody Northrop7fd44212018-09-06 10:30:28 -060070 private static final String ANGLE_RULES_FILE = "a4a_rules.json";
Cody Northrop8d72a6b2018-11-01 08:20:26 -060071 private static final String ANGLE_TEMP_RULES = "debug.angle.rules";
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -070072 private static final String ACTION_ANGLE_FOR_ANDROID = "android.app.action.ANGLE_FOR_ANDROID";
Tim Van Pattenb225b442019-03-07 09:35:26 -070073 private static final String ACTION_ANGLE_FOR_ANDROID_TOAST_MESSAGE =
74 "android.app.action.ANGLE_FOR_ANDROID_TOAST_MESSAGE";
75 private static final String INTENT_KEY_A4A_TOAST_MESSAGE = "A4A Toast Message";
Peiyong Linb76bfe92019-02-13 14:46:54 -080076 private static final String GAME_DRIVER_WHITELIST_ALL = "*";
Yiwei Zhange3490fc2019-06-13 14:02:35 -070077 private static final String GAME_DRIVER_SPHAL_LIBRARIES_FILENAME = "sphal_libraries.txt";
Yiwei Zhang27a27432019-05-06 17:13:05 -070078 private static final int VULKAN_1_0 = 0x00400000;
79 private static final int VULKAN_1_1 = 0x00401000;
Jesse Hall79bf3922016-12-12 12:53:02 -080080
Peiyong Lin9eee3f92019-04-09 17:16:20 -070081 // GAME_DRIVER_ALL_APPS
82 // 0: Default (Invalid values fallback to default as well)
83 // 1: All apps use Game Driver
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -070084 // 2: All apps use Prerelease Driver
85 // 3: All apps use system graphics driver
Peiyong Lin9eee3f92019-04-09 17:16:20 -070086 private static final int GAME_DRIVER_GLOBAL_OPT_IN_DEFAULT = 0;
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -070087 private static final int GAME_DRIVER_GLOBAL_OPT_IN_GAME_DRIVER = 1;
88 private static final int GAME_DRIVER_GLOBAL_OPT_IN_PRERELEASE_DRIVER = 2;
89 private static final int GAME_DRIVER_GLOBAL_OPT_IN_OFF = 3;
Peiyong Lin9eee3f92019-04-09 17:16:20 -070090
Cody Northrop86cedcb2017-10-20 09:03:13 -060091 private ClassLoader mClassLoader;
92 private String mLayerPath;
93 private String mDebugLayerPath;
94
95 /**
96 * Set up GraphicsEnvironment
97 */
Cody Northropdeb43282018-10-04 16:04:05 -060098 public void setup(Context context, Bundle coreSettings) {
Yiwei Zhang33097bf2019-02-04 19:01:21 -080099 final PackageManager pm = context.getPackageManager();
100 final String packageName = context.getPackageName();
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700101 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupGpuLayers");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800102 setupGpuLayers(context, coreSettings, pm, packageName);
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700103 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
104 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "setupAngle");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800105 setupAngle(context, coreSettings, pm, packageName);
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700106 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
107 Trace.traceBegin(Trace.TRACE_TAG_GRAPHICS, "chooseDriver");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800108 if (!chooseDriver(context, coreSettings, pm, packageName)) {
109 setGpuStats(SYSTEM_DRIVER_NAME, SYSTEM_DRIVER_VERSION_NAME, SYSTEM_DRIVER_VERSION_CODE,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700110 SystemProperties.getLong(PROPERTY_GFX_DRIVER_BUILD_TIME, 0), packageName,
111 getVulkanVersion(pm));
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800112 }
Yiwei Zhange6bdc382019-03-18 22:16:42 -0700113 Trace.traceEnd(Trace.TRACE_TAG_GRAPHICS);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600114 }
115
116 /**
Yiwei Zhange54faf52019-05-08 18:43:11 -0700117 * Hint for GraphicsEnvironment that an activity is launching on the process.
118 * Then the app process is allowed to send stats to GpuStats module.
119 */
120 public static native void hintActivityLaunch();
121
122 /**
Cody Northrop12d320a2019-04-09 20:39:20 -0600123 * Query to determine if ANGLE should be used
124 */
125 public static boolean shouldUseAngle(Context context, Bundle coreSettings,
126 String packageName) {
127 if (packageName.isEmpty()) {
128 Log.v(TAG, "No package name available yet, ANGLE should not be used");
129 return false;
130 }
131
132 final String devOptIn = getDriverForPkg(context, coreSettings, packageName);
133 if (DEBUG) {
134 Log.v(TAG, "ANGLE Developer option for '" + packageName + "' "
135 + "set to: '" + devOptIn + "'");
136 }
137
138 // We only want to use ANGLE if the app is whitelisted or the developer has
139 // explicitly chosen something other than default driver.
140 // The whitelist will be generated by the ANGLE APK at both boot time and
141 // ANGLE update time. It will only include apps mentioned in the rules file.
142 final boolean whitelisted = checkAngleWhitelist(context, coreSettings, packageName);
143 final boolean requested = devOptIn.equals(sDriverMap.get(OpenGlDriverChoice.ANGLE));
144 final boolean useAngle = (whitelisted || requested);
145 if (!useAngle) {
146 return false;
147 }
148
149 if (whitelisted) {
150 Log.v(TAG, "ANGLE whitelist includes " + packageName);
151 }
152 if (requested) {
153 Log.v(TAG, "ANGLE developer option for " + packageName + ": " + devOptIn);
154 }
155
156 return true;
157 }
158
Yiwei Zhang27a27432019-05-06 17:13:05 -0700159 private static int getVulkanVersion(PackageManager pm) {
160 // PackageManager doesn't have an API to retrieve the version of a specific feature, and we
161 // need to avoid retrieving all system features here and looping through them.
162 if (pm.hasSystemFeature(PackageManager.FEATURE_VULKAN_HARDWARE_VERSION, VULKAN_1_1)) {
163 return VULKAN_1_1;
164 }
165
166 if (pm.hasSystemFeature(PackageManager.FEATURE_VULKAN_HARDWARE_VERSION, VULKAN_1_0)) {
167 return VULKAN_1_0;
168 }
169
170 return 0;
171 }
172
Cody Northrop12d320a2019-04-09 20:39:20 -0600173 /**
Cody Northrop86cedcb2017-10-20 09:03:13 -0600174 * Check whether application is debuggable
175 */
176 private static boolean isDebuggable(Context context) {
177 return (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) > 0;
178 }
179
180 /**
181 * Store the layer paths available to the loader.
182 */
183 public void setLayerPaths(ClassLoader classLoader,
184 String layerPath,
185 String debugLayerPath) {
186 // We have to store these in the class because they are set up before we
187 // have access to the Context to properly set up GraphicsEnvironment
188 mClassLoader = classLoader;
189 mLayerPath = layerPath;
190 mDebugLayerPath = debugLayerPath;
191 }
192
193 /**
Cody Northropebe6a562018-10-15 07:22:23 -0600194 * Return the debug layer app's on-disk and in-APK lib directories
195 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800196 private static String getDebugLayerAppPaths(PackageManager pm, String app) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800197 final ApplicationInfo appInfo;
Cody Northropebe6a562018-10-15 07:22:23 -0600198 try {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800199 appInfo = pm.getApplicationInfo(app, PackageManager.MATCH_ALL);
Cody Northropebe6a562018-10-15 07:22:23 -0600200 } catch (PackageManager.NameNotFoundException e) {
201 Log.w(TAG, "Debug layer app '" + app + "' not installed");
202
203 return null;
204 }
205
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800206 final String abi = chooseAbi(appInfo);
Cody Northropebe6a562018-10-15 07:22:23 -0600207
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800208 final StringBuilder sb = new StringBuilder();
Cody Northropebe6a562018-10-15 07:22:23 -0600209 sb.append(appInfo.nativeLibraryDir)
210 .append(File.pathSeparator);
211 sb.append(appInfo.sourceDir)
212 .append("!/lib/")
213 .append(abi);
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800214 final String paths = sb.toString();
Cody Northropebe6a562018-10-15 07:22:23 -0600215
216 if (DEBUG) Log.v(TAG, "Debug layer app libs: " + paths);
217
218 return paths;
219 }
220
221 /**
Cody Northrop86cedcb2017-10-20 09:03:13 -0600222 * Set up layer search paths for all apps
223 * If debuggable, check for additional debug settings
224 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800225 private void setupGpuLayers(
226 Context context, Bundle coreSettings, PackageManager pm, String packageName) {
Cody Northrop86cedcb2017-10-20 09:03:13 -0600227 String layerPaths = "";
228
229 // Only enable additional debug functionality if the following conditions are met:
Cody Northropc558c0c2019-05-28 11:56:37 -0600230 // 1. App is debuggable or device is rooted
Cody Northrop86cedcb2017-10-20 09:03:13 -0600231 // 2. ENABLE_GPU_DEBUG_LAYERS is true
232 // 3. Package name is equal to GPU_DEBUG_APP
233
Cody Northropc558c0c2019-05-28 11:56:37 -0600234 if (isDebuggable(context) || (getCanLoadSystemLibraries() == 1)) {
Cody Northrop86cedcb2017-10-20 09:03:13 -0600235
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800236 final int enable = coreSettings.getInt(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, 0);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600237
238 if (enable != 0) {
239
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800240 final String gpuDebugApp = coreSettings.getString(Settings.Global.GPU_DEBUG_APP);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600241
Cody Northrop86cedcb2017-10-20 09:03:13 -0600242 if ((gpuDebugApp != null && packageName != null)
243 && (!gpuDebugApp.isEmpty() && !packageName.isEmpty())
244 && gpuDebugApp.equals(packageName)) {
245 Log.i(TAG, "GPU debug layers enabled for " + packageName);
246
247 // Prepend the debug layer path as a searchable path.
248 // This will ensure debug layers added will take precedence over
249 // the layers specified by the app.
250 layerPaths = mDebugLayerPath + ":";
251
Cody Northropebe6a562018-10-15 07:22:23 -0600252 // If there is a debug layer app specified, add its path.
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800253 final String gpuDebugLayerApp =
Cody Northropebe6a562018-10-15 07:22:23 -0600254 coreSettings.getString(Settings.Global.GPU_DEBUG_LAYER_APP);
255
256 if (gpuDebugLayerApp != null && !gpuDebugLayerApp.isEmpty()) {
257 Log.i(TAG, "GPU debug layer app: " + gpuDebugLayerApp);
Cody Northropf959f6b2019-04-08 09:50:23 -0600258 // If a colon is present, treat this as multiple apps, so Vulkan and GLES
259 // layer apps can be provided at the same time.
260 String[] layerApps = gpuDebugLayerApp.split(":");
261 for (int i = 0; i < layerApps.length; i++) {
262 String paths = getDebugLayerAppPaths(pm, layerApps[i]);
263 if (paths != null) {
264 // Append the path so files placed in the app's base directory will
265 // override the external path
266 layerPaths += paths + ":";
267 }
Cody Northropebe6a562018-10-15 07:22:23 -0600268 }
269 }
270
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800271 final String layers = coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600272
Cody Northrop0fa1d222018-10-23 13:13:21 -0600273 Log.i(TAG, "Vulkan debug layer list: " + layers);
Cody Northrop86cedcb2017-10-20 09:03:13 -0600274 if (layers != null && !layers.isEmpty()) {
275 setDebugLayers(layers);
276 }
Cody Northrop0fa1d222018-10-23 13:13:21 -0600277
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800278 final String layersGLES =
Cody Northrop0fa1d222018-10-23 13:13:21 -0600279 coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS_GLES);
280
281 Log.i(TAG, "GLES debug layer list: " + layersGLES);
282 if (layersGLES != null && !layersGLES.isEmpty()) {
283 setDebugLayersGLES(layersGLES);
284 }
Cody Northrop86cedcb2017-10-20 09:03:13 -0600285 }
286 }
Cody Northrop86cedcb2017-10-20 09:03:13 -0600287 }
288
289 // Include the app's lib directory in all cases
290 layerPaths += mLayerPath;
291
292 setLayerPaths(mClassLoader, layerPaths);
293 }
294
Tim Van Patten3c612842018-11-09 16:48:24 -0700295 enum OpenGlDriverChoice {
296 DEFAULT,
297 NATIVE,
298 ANGLE
299 }
300
301 private static final Map<OpenGlDriverChoice, String> sDriverMap = buildMap();
302 private static Map<OpenGlDriverChoice, String> buildMap() {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800303 final Map<OpenGlDriverChoice, String> map = new HashMap<>();
Tim Van Patten3c612842018-11-09 16:48:24 -0700304 map.put(OpenGlDriverChoice.DEFAULT, "default");
305 map.put(OpenGlDriverChoice.ANGLE, "angle");
306 map.put(OpenGlDriverChoice.NATIVE, "native");
307
308 return map;
309 }
310
311
Tim Van Pattenddc43912018-12-18 17:47:52 -0700312 private static List<String> getGlobalSettingsString(ContentResolver contentResolver,
313 Bundle bundle,
314 String globalSetting) {
315 final List<String> valueList;
316 final String settingsValue;
317
318 if (bundle != null) {
319 settingsValue = bundle.getString(globalSetting);
320 } else {
321 settingsValue = Settings.Global.getString(contentResolver, globalSetting);
322 }
Tim Van Patten3c612842018-11-09 16:48:24 -0700323
324 if (settingsValue != null) {
325 valueList = new ArrayList<>(Arrays.asList(settingsValue.split(",")));
326 } else {
327 valueList = new ArrayList<>();
328 }
329
330 return valueList;
331 }
332
333 private static int getGlobalSettingsPkgIndex(String pkgName,
334 List<String> globalSettingsDriverPkgs) {
335 for (int pkgIndex = 0; pkgIndex < globalSettingsDriverPkgs.size(); pkgIndex++) {
336 if (globalSettingsDriverPkgs.get(pkgIndex).equals(pkgName)) {
337 return pkgIndex;
338 }
339 }
340
341 return -1;
342 }
343
Tim Van Pattenddc43912018-12-18 17:47:52 -0700344 private static String getDriverForPkg(Context context, Bundle bundle, String packageName) {
345 final String allUseAngle;
346 if (bundle != null) {
347 allUseAngle =
348 bundle.getString(Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE);
349 } else {
350 ContentResolver contentResolver = context.getContentResolver();
351 allUseAngle = Settings.Global.getString(contentResolver,
352 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE);
353 }
Tim Van Pattenff9476b2018-12-11 15:41:25 -0700354 if ((allUseAngle != null) && allUseAngle.equals("1")) {
355 return sDriverMap.get(OpenGlDriverChoice.ANGLE);
Tim Van Patten3c612842018-11-09 16:48:24 -0700356 }
357
Tim Van Pattenddc43912018-12-18 17:47:52 -0700358 final ContentResolver contentResolver = context.getContentResolver();
359 final List<String> globalSettingsDriverPkgs =
360 getGlobalSettingsString(contentResolver, bundle,
361 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS);
362 final List<String> globalSettingsDriverValues =
363 getGlobalSettingsString(contentResolver, bundle,
364 Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES);
Tim Van Patten3c612842018-11-09 16:48:24 -0700365
366 // Make sure we have a good package name
367 if ((packageName == null) || (packageName.isEmpty())) {
368 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
369 }
370 // Make sure we have good settings to use
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700371 if (globalSettingsDriverPkgs.size() != globalSettingsDriverValues.size()) {
Tim Van Patten3c612842018-11-09 16:48:24 -0700372 Log.w(TAG,
373 "Global.Settings values are invalid: "
374 + "globalSettingsDriverPkgs.size = "
375 + globalSettingsDriverPkgs.size() + ", "
376 + "globalSettingsDriverValues.size = "
377 + globalSettingsDriverValues.size());
378 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
379 }
380
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800381 final int pkgIndex = getGlobalSettingsPkgIndex(packageName, globalSettingsDriverPkgs);
Tim Van Patten3c612842018-11-09 16:48:24 -0700382
383 if (pkgIndex < 0) {
384 return sDriverMap.get(OpenGlDriverChoice.DEFAULT);
385 }
386
387 return globalSettingsDriverValues.get(pkgIndex);
388 }
389
Jesse Hallc37984f2017-05-23 16:55:08 -0700390 /**
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700391 * Get the ANGLE package name.
392 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800393 private String getAnglePackageName(PackageManager pm) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800394 final Intent intent = new Intent(ACTION_ANGLE_FOR_ANDROID);
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700395
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800396 final List<ResolveInfo> resolveInfos =
397 pm.queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY);
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700398 if (resolveInfos.size() != 1) {
399 Log.e(TAG, "Invalid number of ANGLE packages. Required: 1, Found: "
400 + resolveInfos.size());
401 for (ResolveInfo resolveInfo : resolveInfos) {
402 Log.e(TAG, "Found ANGLE package: " + resolveInfo.activityInfo.packageName);
403 }
404 return "";
405 }
406
407 // Must be exactly 1 ANGLE PKG found to get here.
408 return resolveInfos.get(0).activityInfo.packageName;
409 }
410
411 /**
Cody Northropdc2fd942019-03-28 19:27:19 -0600412 * Check for ANGLE debug package, but only for apps that can load them (dumpable)
413 */
414 private String getAngleDebugPackage(Context context, Bundle coreSettings) {
415 final boolean appIsDebuggable = isDebuggable(context);
Cody Northropdc2fd942019-03-28 19:27:19 -0600416 final boolean deviceIsDebuggable = getCanLoadSystemLibraries() == 1;
Cody Northrop32adba42019-05-28 12:18:44 -0600417 if (appIsDebuggable || deviceIsDebuggable) {
Tim Van Patten5973ec92019-04-08 17:30:17 -0600418 String debugPackage;
Cody Northropdc2fd942019-03-28 19:27:19 -0600419
Tim Van Patten5973ec92019-04-08 17:30:17 -0600420 if (coreSettings != null) {
421 debugPackage =
422 coreSettings.getString(Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE);
423 } else {
424 ContentResolver contentResolver = context.getContentResolver();
425 debugPackage = Settings.Global.getString(contentResolver,
426 Settings.Global.GLOBAL_SETTINGS_ANGLE_DEBUG_PACKAGE);
427 }
Cody Northropdc2fd942019-03-28 19:27:19 -0600428
429 if ((debugPackage != null) && (!debugPackage.isEmpty())) {
430 return debugPackage;
431 }
432 }
Tim Van Patten5973ec92019-04-08 17:30:17 -0600433
Cody Northropdc2fd942019-03-28 19:27:19 -0600434 return "";
435 }
436
437 /**
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700438 * Attempt to setup ANGLE with a temporary rules file.
439 * True: Temporary rules file was loaded.
440 * False: Temporary rules file was *not* loaded.
441 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700442 private static boolean setupAngleWithTempRulesFile(Context context,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700443 String packageName,
444 String paths,
445 String devOptIn) {
Tim Van Patten3df66432019-01-14 15:56:12 -0700446 /**
447 * We only want to load a temp rules file for:
448 * - apps that are marked 'debuggable' in their manifest
449 * - devices that are running a userdebug build (ro.debuggable) or can inject libraries for
450 * debugging (PR_SET_DUMPABLE).
451 */
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800452 final boolean appIsDebuggable = isDebuggable(context);
453 final boolean deviceIsDebuggable = getCanLoadSystemLibraries() == 1;
Tim Van Patten3df66432019-01-14 15:56:12 -0700454 if (!(appIsDebuggable || deviceIsDebuggable)) {
455 Log.v(TAG, "Skipping loading temporary rules file: "
456 + "appIsDebuggable = " + appIsDebuggable + ", "
457 + "adbRootEnabled = " + deviceIsDebuggable);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700458 return false;
459 }
460
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800461 final String angleTempRules = SystemProperties.get(ANGLE_TEMP_RULES);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700462
463 if ((angleTempRules == null) || angleTempRules.isEmpty()) {
464 Log.v(TAG, "System property '" + ANGLE_TEMP_RULES + "' is not set or is empty");
465 return false;
466 }
467
468 Log.i(TAG, "Detected system property " + ANGLE_TEMP_RULES + ": " + angleTempRules);
469
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800470 final File tempRulesFile = new File(angleTempRules);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700471 if (tempRulesFile.exists()) {
472 Log.i(TAG, angleTempRules + " exists, loading file.");
473 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800474 final FileInputStream stream = new FileInputStream(angleTempRules);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700475
476 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800477 final FileDescriptor rulesFd = stream.getFD();
478 final long rulesOffset = 0;
479 final long rulesLength = stream.getChannel().size();
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700480 Log.i(TAG, "Loaded temporary ANGLE rules from " + angleTempRules);
481
482 setAngleInfo(paths, packageName, devOptIn, rulesFd, rulesOffset, rulesLength);
483
484 stream.close();
485
486 // We successfully setup ANGLE, so return with good status
487 return true;
488 } catch (IOException e) {
489 Log.w(TAG, "Hit IOException thrown by FileInputStream: " + e);
490 }
491 } catch (FileNotFoundException e) {
492 Log.w(TAG, "Temp ANGLE rules file not found: " + e);
493 } catch (SecurityException e) {
494 Log.w(TAG, "Temp ANGLE rules file not accessible: " + e);
495 }
496 }
497
498 return false;
499 }
500
501 /**
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700502 * Attempt to setup ANGLE with a rules file loaded from the ANGLE APK.
503 * True: APK rules file was loaded.
504 * False: APK rules file was *not* loaded.
505 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700506 private static boolean setupAngleRulesApk(String anglePkgName,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700507 ApplicationInfo angleInfo,
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800508 PackageManager pm,
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700509 String packageName,
510 String paths,
511 String devOptIn) {
512 // Pass the rules file to loader for ANGLE decisions
513 try {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800514 final AssetManager angleAssets = pm.getResourcesForApplication(angleInfo).getAssets();
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700515
516 try {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800517 final AssetFileDescriptor assetsFd = angleAssets.openFd(ANGLE_RULES_FILE);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700518
519 setAngleInfo(paths, packageName, devOptIn, assetsFd.getFileDescriptor(),
520 assetsFd.getStartOffset(), assetsFd.getLength());
521
522 assetsFd.close();
523
524 return true;
525 } catch (IOException e) {
526 Log.w(TAG, "Failed to get AssetFileDescriptor for " + ANGLE_RULES_FILE
527 + " from '" + anglePkgName + "': " + e);
528 }
529 } catch (PackageManager.NameNotFoundException e) {
530 Log.w(TAG, "Failed to get AssetManager for '" + anglePkgName + "': " + e);
531 }
532
533 return false;
534 }
535
536 /**
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700537 * Pull ANGLE whitelist from GlobalSettings and compare against current package
538 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700539 private static boolean checkAngleWhitelist(Context context, Bundle bundle, String packageName) {
540 final ContentResolver contentResolver = context.getContentResolver();
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800541 final List<String> angleWhitelist =
Tim Van Pattenddc43912018-12-18 17:47:52 -0700542 getGlobalSettingsString(contentResolver, bundle,
543 Settings.Global.GLOBAL_SETTINGS_ANGLE_WHITELIST);
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700544
Cody Northrop12d320a2019-04-09 20:39:20 -0600545 if (DEBUG) Log.v(TAG, "ANGLE whitelist: " + angleWhitelist);
546
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700547 return angleWhitelist.contains(packageName);
548 }
549
550 /**
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600551 * Pass ANGLE details down to trigger enable logic
Tim Van Pattenddc43912018-12-18 17:47:52 -0700552 *
553 * @param context
554 * @param bundle
555 * @param packageName
556 * @return true: ANGLE setup successfully
557 * false: ANGLE not setup (not on whitelist, ANGLE not present, etc.)
Cody Northrop73c05042018-04-16 13:23:51 -0600558 */
Tim Van Pattenddc43912018-12-18 17:47:52 -0700559 public boolean setupAngle(Context context, Bundle bundle, PackageManager pm,
560 String packageName) {
Cody Northrop12d320a2019-04-09 20:39:20 -0600561
562 if (!shouldUseAngle(context, bundle, packageName)) {
Tim Van Pattenddc43912018-12-18 17:47:52 -0700563 return false;
Cody Northrop5ebb0db2019-01-15 14:06:36 -0700564 }
Cody Northrop73c05042018-04-16 13:23:51 -0600565
Cody Northrop41149c32019-05-06 11:36:40 -0600566 ApplicationInfo angleInfo = null;
Tim Van Patten5e3e2ae2018-12-13 17:43:37 -0700567
Cody Northrop41149c32019-05-06 11:36:40 -0600568 // If the developer has specified a debug package over ADB, attempt to find it
569 String anglePkgName = getAngleDebugPackage(context, bundle);
570 if (!anglePkgName.isEmpty()) {
571 Log.i(TAG, "ANGLE debug package enabled: " + anglePkgName);
Cody Northropdc2fd942019-03-28 19:27:19 -0600572 try {
573 // Note the debug package does not have to be pre-installed
Cody Northrop41149c32019-05-06 11:36:40 -0600574 angleInfo = pm.getApplicationInfo(anglePkgName, 0);
Cody Northropdc2fd942019-03-28 19:27:19 -0600575 } catch (PackageManager.NameNotFoundException e) {
Cody Northrop41149c32019-05-06 11:36:40 -0600576 Log.w(TAG, "ANGLE debug package '" + anglePkgName + "' not installed");
Cody Northropdc2fd942019-03-28 19:27:19 -0600577 return false;
578 }
Cody Northrop41149c32019-05-06 11:36:40 -0600579 }
580
581 // Otherwise, check to see if ANGLE is properly installed
582 if (angleInfo == null) {
583 anglePkgName = getAnglePackageName(pm);
584 if (!anglePkgName.isEmpty()) {
585 Log.i(TAG, "ANGLE package enabled: " + anglePkgName);
586 try {
587 // Production ANGLE libraries must be pre-installed as a system app
588 angleInfo = pm.getApplicationInfo(anglePkgName,
589 PackageManager.MATCH_SYSTEM_ONLY);
590 } catch (PackageManager.NameNotFoundException e) {
591 Log.w(TAG, "ANGLE package '" + anglePkgName + "' not installed");
592 return false;
593 }
594 } else {
595 Log.e(TAG, "Failed to find ANGLE package.");
Cody Northropdc2fd942019-03-28 19:27:19 -0600596 return false;
597 }
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600598 }
599
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800600 final String abi = chooseAbi(angleInfo);
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600601
602 // Build a path that includes installed native libs and APK
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800603 final String paths = angleInfo.nativeLibraryDir
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700604 + File.pathSeparator
605 + angleInfo.sourceDir
606 + "!/lib/"
607 + abi;
Cody Northropeb0ca8e2018-08-09 16:01:08 -0600608
609 if (DEBUG) Log.v(TAG, "ANGLE package libs: " + paths);
610
Cody Northrop12d320a2019-04-09 20:39:20 -0600611 // If the user has set the developer option to something other than default,
612 // we need to call setupAngleRulesApk() with the package name and the developer
613 // option value (native/angle/other). Then later when we are actually trying to
614 // load a driver, GraphicsEnv::getShouldUseAngle() has seen the package name before
615 // and can confidently answer yes/no based on the previously set developer
616 // option value.
617 final String devOptIn = getDriverForPkg(context, bundle, packageName);
618
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700619 if (setupAngleWithTempRulesFile(context, packageName, paths, devOptIn)) {
620 // We setup ANGLE with a temp rules file, so we're done here.
Tim Van Pattenddc43912018-12-18 17:47:52 -0700621 return true;
Cody Northrop8d72a6b2018-11-01 08:20:26 -0600622 }
623
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800624 if (setupAngleRulesApk(anglePkgName, angleInfo, pm, packageName, paths, devOptIn)) {
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700625 // We setup ANGLE with rules from the APK, so we're done here.
Tim Van Pattenddc43912018-12-18 17:47:52 -0700626 return true;
627 }
628
629 return false;
630 }
631
632 /**
633 * Determine if the "ANGLE In Use" dialog box should be shown.
634 */
635 private boolean shouldShowAngleInUseDialogBox(Context context) {
636 try {
637 ContentResolver contentResolver = context.getContentResolver();
638 final int showDialogBox = Settings.Global.getInt(contentResolver,
639 Settings.Global.GLOBAL_SETTINGS_SHOW_ANGLE_IN_USE_DIALOG_BOX);
640
641 return (showDialogBox == 1);
642 } catch (Settings.SettingNotFoundException | SecurityException e) {
643 // Do nothing and move on
644 }
645
646 // No setting, so assume false
647 return false;
648 }
649
650 /**
Cody Northrop12d320a2019-04-09 20:39:20 -0600651 * Determine if ANGLE will be used and setup the environment
Tim Van Pattenddc43912018-12-18 17:47:52 -0700652 */
Cody Northrop12d320a2019-04-09 20:39:20 -0600653 private boolean setupAndUseAngle(Context context, String packageName) {
Tim Van Pattenddc43912018-12-18 17:47:52 -0700654 // Need to make sure we are evaluating ANGLE usage for the correct circumstances
655 if (!setupAngle(context, null, context.getPackageManager(), packageName)) {
Cody Northrop41149c32019-05-06 11:36:40 -0600656 Log.v(TAG, "Package '" + packageName + "' should not use ANGLE");
Tim Van Pattenddc43912018-12-18 17:47:52 -0700657 return false;
658 }
659
660 final boolean useAngle = getShouldUseAngle(packageName);
661 Log.v(TAG, "Package '" + packageName + "' should use ANGLE = '" + useAngle + "'");
662
663 return useAngle;
664 }
665
666 /**
667 * Show the ANGLE in Use Dialog Box
668 * @param context
669 */
670 public void showAngleInUseDialogBox(Context context) {
671 final String packageName = context.getPackageName();
672
Cody Northrop12d320a2019-04-09 20:39:20 -0600673 if (shouldShowAngleInUseDialogBox(context) && setupAndUseAngle(context, packageName)) {
Tim Van Pattenb225b442019-03-07 09:35:26 -0700674 final Intent intent = new Intent(ACTION_ANGLE_FOR_ANDROID_TOAST_MESSAGE);
675 String anglePkg = getAnglePackageName(context.getPackageManager());
676 intent.setPackage(anglePkg);
677
678 context.sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
679 @Override
680 public void onReceive(Context context, Intent intent) {
681 Bundle results = getResultExtras(true);
682
683 String toastMsg = results.getString(INTENT_KEY_A4A_TOAST_MESSAGE);
684 final Toast toast = Toast.makeText(context, toastMsg, Toast.LENGTH_LONG);
685 toast.show();
686 }
687 }, null, Activity.RESULT_OK, null, null);
Tim Van Pattena7577bf2018-12-12 11:47:24 -0700688 }
Cody Northrop73c05042018-04-16 13:23:51 -0600689 }
690
691 /**
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700692 * Return the driver package name to use. Return null for system driver.
693 */
694 private static String chooseDriverInternal(Context context, Bundle coreSettings) {
695 final String gameDriver = SystemProperties.get(PROPERTY_GFX_DRIVER);
696 final boolean hasGameDriver = gameDriver != null && !gameDriver.isEmpty();
697
698 final String prereleaseDriver = SystemProperties.get(PROPERTY_GFX_DRIVER_PRERELEASE);
699 final boolean hasPrereleaseDriver = prereleaseDriver != null && !prereleaseDriver.isEmpty();
700
701 if (!hasGameDriver && !hasPrereleaseDriver) {
702 if (DEBUG) Log.v(TAG, "Neither Game Driver nor prerelease driver is supported.");
703 return null;
704 }
705
706 // To minimize risk of driver updates crippling the device beyond user repair, never use an
707 // updated driver for privileged or non-updated system apps. Presumably pre-installed apps
708 // were tested thoroughly with the pre-installed driver.
709 final ApplicationInfo ai = context.getApplicationInfo();
710 if (ai.isPrivilegedApp() || (ai.isSystemApp() && !ai.isUpdatedSystemApp())) {
711 if (DEBUG) Log.v(TAG, "Ignoring driver package for privileged/non-updated system app.");
712 return null;
713 }
714
715 // Priority for Game Driver settings global on confliction (Higher priority comes first):
716 // 1. GAME_DRIVER_ALL_APPS
717 // 2. GAME_DRIVER_OPT_OUT_APPS
718 // 3. GAME_DRIVER_PRERELEASE_OPT_IN_APPS
719 // 4. GAME_DRIVER_OPT_IN_APPS
720 // 5. GAME_DRIVER_BLACKLIST
721 // 6. GAME_DRIVER_WHITELIST
Yiwei Zhangff9a4ee2019-06-12 14:10:17 -0700722 switch (coreSettings.getInt(Settings.Global.GAME_DRIVER_ALL_APPS, 0)) {
723 case GAME_DRIVER_GLOBAL_OPT_IN_OFF:
724 if (DEBUG) Log.v(TAG, "Game Driver is turned off on this device.");
725 return null;
726 case GAME_DRIVER_GLOBAL_OPT_IN_GAME_DRIVER:
727 if (DEBUG) Log.v(TAG, "All apps opt in to use Game Driver.");
728 return hasGameDriver ? gameDriver : null;
729 case GAME_DRIVER_GLOBAL_OPT_IN_PRERELEASE_DRIVER:
730 if (DEBUG) Log.v(TAG, "All apps opt in to use prerelease driver.");
731 return hasPrereleaseDriver ? prereleaseDriver : null;
732 case GAME_DRIVER_GLOBAL_OPT_IN_DEFAULT:
733 default:
734 break;
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700735 }
736
737 final String appPackageName = ai.packageName;
738 if (getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_OPT_OUT_APPS)
739 .contains(appPackageName)) {
740 if (DEBUG) Log.v(TAG, "App opts out for Game Driver.");
741 return null;
742 }
743
744 if (getGlobalSettingsString(
745 null, coreSettings, Settings.Global.GAME_DRIVER_PRERELEASE_OPT_IN_APPS)
746 .contains(appPackageName)) {
747 if (DEBUG) Log.v(TAG, "App opts in for prerelease Game Driver.");
748 return hasPrereleaseDriver ? prereleaseDriver : null;
749 }
750
751 // Early return here since the rest logic is only for Game Driver.
752 if (!hasGameDriver) {
753 if (DEBUG) Log.v(TAG, "Game Driver is not supported on the device.");
754 return null;
755 }
756
757 final boolean isOptIn =
758 getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_OPT_IN_APPS)
759 .contains(appPackageName);
760 final List<String> whitelist =
761 getGlobalSettingsString(null, coreSettings, Settings.Global.GAME_DRIVER_WHITELIST);
762 if (!isOptIn && whitelist.indexOf(GAME_DRIVER_WHITELIST_ALL) != 0
763 && !whitelist.contains(appPackageName)) {
764 if (DEBUG) Log.v(TAG, "App is not on the whitelist for Game Driver.");
765 return null;
766 }
767
768 // If the application is not opted-in, then check whether it's on the blacklist,
769 // terminate early if it's on the blacklist and fallback to system driver.
770 if (!isOptIn
771 && getGlobalSettingsString(
772 null, coreSettings, Settings.Global.GAME_DRIVER_BLACKLIST)
773 .contains(appPackageName)) {
774 if (DEBUG) Log.v(TAG, "App is on the blacklist for Game Driver.");
775 return null;
776 }
777
778 return gameDriver;
779 }
780
781 /**
Jesse Hallc37984f2017-05-23 16:55:08 -0700782 * Choose whether the current process should use the builtin or an updated driver.
Jesse Hallc37984f2017-05-23 16:55:08 -0700783 */
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800784 private static boolean chooseDriver(
785 Context context, Bundle coreSettings, PackageManager pm, String packageName) {
Yiwei Zhang5d0d4c62019-06-09 23:29:59 -0700786 final String driverPackageName = chooseDriverInternal(context, coreSettings);
787 if (driverPackageName == null) {
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800788 return false;
Jesse Hall79bf3922016-12-12 12:53:02 -0800789 }
Peiyong Lin2d5a2bb2018-11-07 21:10:07 -0800790
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800791 final PackageInfo driverPackageInfo;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800792 try {
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800793 driverPackageInfo = pm.getPackageInfo(driverPackageName,
794 PackageManager.MATCH_SYSTEM_ONLY | PackageManager.GET_META_DATA);
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800795 } catch (PackageManager.NameNotFoundException e) {
796 Log.w(TAG, "driver package '" + driverPackageName + "' not installed");
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800797 return false;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800798 }
799
800 // O drivers are restricted to the sphal linker namespace, so don't try to use
801 // packages unless they declare they're compatible with that restriction.
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800802 final ApplicationInfo driverAppInfo = driverPackageInfo.applicationInfo;
803 if (driverAppInfo.targetSdkVersion < Build.VERSION_CODES.O) {
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800804 if (DEBUG) {
805 Log.w(TAG, "updated driver package is not known to be compatible with O");
806 }
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800807 return false;
Peiyong Lin9ca1dd82019-01-12 17:44:29 -0800808 }
809
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800810 final String abi = chooseAbi(driverAppInfo);
Jesse Hall79bf3922016-12-12 12:53:02 -0800811 if (abi == null) {
812 if (DEBUG) {
813 // This is the normal case for the pre-installed empty driver package, don't spam
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800814 if (driverAppInfo.isUpdatedSystemApp()) {
Jesse Hall79bf3922016-12-12 12:53:02 -0800815 Log.w(TAG, "updated driver package has no compatible native libraries");
816 }
817 }
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800818 return false;
Jesse Hall79bf3922016-12-12 12:53:02 -0800819 }
Jesse Hall79bf3922016-12-12 12:53:02 -0800820
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800821 final StringBuilder sb = new StringBuilder();
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800822 sb.append(driverAppInfo.nativeLibraryDir)
Jesse Hall79bf3922016-12-12 12:53:02 -0800823 .append(File.pathSeparator);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800824 sb.append(driverAppInfo.sourceDir)
Jesse Hall79bf3922016-12-12 12:53:02 -0800825 .append("!/lib/")
826 .append(abi);
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800827 final String paths = sb.toString();
Yiwei Zhange3490fc2019-06-13 14:02:35 -0700828 final String sphalLibraries = getSphalLibraries(context, driverPackageName);
Yiwei Zhang1c51f052019-02-14 12:05:47 -0800829 if (DEBUG) {
830 Log.v(TAG,
831 "gfx driver package search path: " + paths
832 + ", required sphal libraries: " + sphalLibraries);
833 }
834 setDriverPathAndSphalLibraries(paths, sphalLibraries);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800835
Yiwei Zhangf7b0a5f2019-02-19 16:04:10 -0800836 if (driverAppInfo.metaData == null) {
837 throw new NullPointerException("apk's meta-data cannot be null");
838 }
839
840 final String driverBuildTime = driverAppInfo.metaData.getString(METADATA_DRIVER_BUILD_TIME);
841 if (driverBuildTime == null || driverBuildTime.isEmpty()) {
Yiwei Zhang61f8d222019-03-21 16:14:54 -0700842 throw new IllegalArgumentException("com.android.gamedriver.build_time is not set");
Yiwei Zhangf7b0a5f2019-02-19 16:04:10 -0800843 }
844 // driver_build_time in the meta-data is in "L<Unix epoch timestamp>" format. e.g. L123456.
845 // Long.parseLong will throw if the meta-data "driver_build_time" is not set properly.
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800846 setGpuStats(driverPackageName, driverPackageInfo.versionName, driverAppInfo.longVersionCode,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700847 Long.parseLong(driverBuildTime.substring(1)), packageName, 0);
Yiwei Zhang31f5adb2019-02-15 16:15:17 -0800848
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800849 return true;
Jesse Hall79bf3922016-12-12 12:53:02 -0800850 }
851
Jesse Hall79bf3922016-12-12 12:53:02 -0800852 private static String chooseAbi(ApplicationInfo ai) {
Yiwei Zhanga0ee1f82019-02-04 17:53:57 -0800853 final String isa = VMRuntime.getCurrentInstructionSet();
Jesse Hall79bf3922016-12-12 12:53:02 -0800854 if (ai.primaryCpuAbi != null &&
855 isa.equals(VMRuntime.getInstructionSet(ai.primaryCpuAbi))) {
856 return ai.primaryCpuAbi;
857 }
858 if (ai.secondaryCpuAbi != null &&
859 isa.equals(VMRuntime.getInstructionSet(ai.secondaryCpuAbi))) {
860 return ai.secondaryCpuAbi;
861 }
862 return null;
863 }
864
Yiwei Zhange3490fc2019-06-13 14:02:35 -0700865 private static String getSphalLibraries(Context context, String driverPackageName) {
866 try {
867 final Context driverContext =
868 context.createPackageContext(driverPackageName, Context.CONTEXT_RESTRICTED);
869 final BufferedReader reader = new BufferedReader(new InputStreamReader(
870 driverContext.getAssets().open(GAME_DRIVER_SPHAL_LIBRARIES_FILENAME)));
871 final ArrayList<String> assetStrings = new ArrayList<>();
872 for (String assetString; (assetString = reader.readLine()) != null;) {
873 assetStrings.add(assetString);
874 }
875 return String.join(":", assetStrings);
876 } catch (PackageManager.NameNotFoundException e) {
877 if (DEBUG) {
878 Log.w(TAG, "Driver package '" + driverPackageName + "' not installed");
879 }
880 } catch (IOException e) {
881 if (DEBUG) {
882 Log.w(TAG, "Failed to load '" + GAME_DRIVER_SPHAL_LIBRARIES_FILENAME + "'");
883 }
884 }
885 return "";
886 }
887
Cody Northropebe6a562018-10-15 07:22:23 -0600888 private static native int getCanLoadSystemLibraries();
Cody Northrop86cedcb2017-10-20 09:03:13 -0600889 private static native void setLayerPaths(ClassLoader classLoader, String layerPaths);
890 private static native void setDebugLayers(String layers);
Cody Northrop0fa1d222018-10-23 13:13:21 -0600891 private static native void setDebugLayersGLES(String layers);
Yiwei Zhang1c51f052019-02-14 12:05:47 -0800892 private static native void setDriverPathAndSphalLibraries(String path, String sphalLibraries);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800893 private static native void setGpuStats(String driverPackageName, String driverVersionName,
Yiwei Zhang27a27432019-05-06 17:13:05 -0700894 long driverVersionCode, long driverBuildTime, String appPackageName, int vulkanVersion);
Yiwei Zhang33097bf2019-02-04 19:01:21 -0800895 private static native void setAngleInfo(String path, String appPackage, String devOptIn,
896 FileDescriptor rulesFd, long rulesOffset, long rulesLength);
Tim Van Pattenddc43912018-12-18 17:47:52 -0700897 private static native boolean getShouldUseAngle(String packageName);
Jesse Hall79bf3922016-12-12 12:53:02 -0800898}