Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.os; |
| 18 | |
| 19 | import android.content.Context; |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 20 | import android.content.Intent; |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 21 | import android.content.pm.ApplicationInfo; |
| 22 | import android.content.pm.PackageManager; |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 23 | import android.content.pm.ResolveInfo; |
Cody Northrop | 7fd4421 | 2018-09-06 10:30:28 -0600 | [diff] [blame] | 24 | import android.content.res.AssetFileDescriptor; |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 25 | import android.content.res.AssetManager; |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 26 | import android.gamedriver.GameDriverProto.Blacklist; |
| 27 | import android.gamedriver.GameDriverProto.Blacklists; |
Jesse Hall | fd104e7 | 2017-01-19 17:59:08 -0800 | [diff] [blame] | 28 | import android.opengl.EGL14; |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 29 | import android.provider.Settings; |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 30 | import android.util.Base64; |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 31 | import android.util.Log; |
| 32 | |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 33 | import com.android.framework.protobuf.InvalidProtocolBufferException; |
| 34 | |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 35 | import dalvik.system.VMRuntime; |
| 36 | |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 37 | import java.io.BufferedReader; |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 38 | import java.io.File; |
Cody Northrop | 7fd4421 | 2018-09-06 10:30:28 -0600 | [diff] [blame] | 39 | import java.io.FileDescriptor; |
Cody Northrop | 8d72a6b | 2018-11-01 08:20:26 -0600 | [diff] [blame] | 40 | import java.io.FileInputStream; |
| 41 | import java.io.FileNotFoundException; |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 42 | import java.io.IOException; |
| 43 | import java.io.InputStream; |
| 44 | import java.io.InputStreamReader; |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 45 | import java.util.ArrayList; |
| 46 | import java.util.Arrays; |
| 47 | import java.util.HashMap; |
| 48 | import java.util.List; |
| 49 | import java.util.Map; |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 50 | |
| 51 | /** @hide */ |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 52 | public class GraphicsEnvironment { |
| 53 | |
| 54 | private static final GraphicsEnvironment sInstance = new GraphicsEnvironment(); |
| 55 | |
| 56 | /** |
| 57 | * Returns the shared {@link GraphicsEnvironment} instance. |
| 58 | */ |
| 59 | public static GraphicsEnvironment getInstance() { |
| 60 | return sInstance; |
| 61 | } |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 62 | |
| 63 | private static final boolean DEBUG = false; |
| 64 | private static final String TAG = "GraphicsEnvironment"; |
| 65 | private static final String PROPERTY_GFX_DRIVER = "ro.gfx.driver.0"; |
Peiyong Lin | 3a1be74 | 2019-01-02 17:46:55 -0800 | [diff] [blame] | 66 | private static final String GUP_WHITELIST_FILENAME = "whitelist.txt"; |
Cody Northrop | 7fd4421 | 2018-09-06 10:30:28 -0600 | [diff] [blame] | 67 | private static final String ANGLE_RULES_FILE = "a4a_rules.json"; |
Cody Northrop | 8d72a6b | 2018-11-01 08:20:26 -0600 | [diff] [blame] | 68 | private static final String ANGLE_TEMP_RULES = "debug.angle.rules"; |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 69 | private static final String ACTION_ANGLE_FOR_ANDROID = "android.app.action.ANGLE_FOR_ANDROID"; |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 70 | private static final String GAME_DRIVER_BLACKLIST_FLAG = "blacklist"; |
| 71 | private static final int BASE64_FLAGS = Base64.NO_PADDING | Base64.NO_WRAP; |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 72 | |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 73 | private ClassLoader mClassLoader; |
| 74 | private String mLayerPath; |
| 75 | private String mDebugLayerPath; |
| 76 | |
| 77 | /** |
| 78 | * Set up GraphicsEnvironment |
| 79 | */ |
Cody Northrop | deb4328 | 2018-10-04 16:04:05 -0600 | [diff] [blame] | 80 | public void setup(Context context, Bundle coreSettings) { |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 81 | setupGpuLayers(context, coreSettings); |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 82 | setupAngle(context, coreSettings, context.getPackageName()); |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 83 | chooseDriver(context, coreSettings); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Check whether application is debuggable |
| 88 | */ |
| 89 | private static boolean isDebuggable(Context context) { |
| 90 | return (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) > 0; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Store the layer paths available to the loader. |
| 95 | */ |
| 96 | public void setLayerPaths(ClassLoader classLoader, |
| 97 | String layerPath, |
| 98 | String debugLayerPath) { |
| 99 | // We have to store these in the class because they are set up before we |
| 100 | // have access to the Context to properly set up GraphicsEnvironment |
| 101 | mClassLoader = classLoader; |
| 102 | mLayerPath = layerPath; |
| 103 | mDebugLayerPath = debugLayerPath; |
| 104 | } |
| 105 | |
| 106 | /** |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 107 | * Return the debug layer app's on-disk and in-APK lib directories |
| 108 | */ |
| 109 | private static String getDebugLayerAppPaths(Context context, String app) { |
| 110 | ApplicationInfo appInfo; |
| 111 | try { |
| 112 | appInfo = context.getPackageManager().getApplicationInfo( |
| 113 | app, PackageManager.MATCH_ALL); |
| 114 | } catch (PackageManager.NameNotFoundException e) { |
| 115 | Log.w(TAG, "Debug layer app '" + app + "' not installed"); |
| 116 | |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | String abi = chooseAbi(appInfo); |
| 121 | |
| 122 | StringBuilder sb = new StringBuilder(); |
| 123 | sb.append(appInfo.nativeLibraryDir) |
| 124 | .append(File.pathSeparator); |
| 125 | sb.append(appInfo.sourceDir) |
| 126 | .append("!/lib/") |
| 127 | .append(abi); |
| 128 | String paths = sb.toString(); |
| 129 | |
| 130 | if (DEBUG) Log.v(TAG, "Debug layer app libs: " + paths); |
| 131 | |
| 132 | return paths; |
| 133 | } |
| 134 | |
| 135 | /** |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 136 | * Set up layer search paths for all apps |
| 137 | * If debuggable, check for additional debug settings |
| 138 | */ |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 139 | private void setupGpuLayers(Context context, Bundle coreSettings) { |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 140 | |
| 141 | String layerPaths = ""; |
| 142 | |
| 143 | // Only enable additional debug functionality if the following conditions are met: |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 144 | // 1. App is debuggable or device is rooted |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 145 | // 2. ENABLE_GPU_DEBUG_LAYERS is true |
| 146 | // 3. Package name is equal to GPU_DEBUG_APP |
| 147 | |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 148 | if (isDebuggable(context) || (getCanLoadSystemLibraries() == 1)) { |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 149 | |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 150 | int enable = coreSettings.getInt(Settings.Global.ENABLE_GPU_DEBUG_LAYERS, 0); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 151 | |
| 152 | if (enable != 0) { |
| 153 | |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 154 | String gpuDebugApp = coreSettings.getString(Settings.Global.GPU_DEBUG_APP); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 155 | |
| 156 | String packageName = context.getPackageName(); |
| 157 | |
| 158 | if ((gpuDebugApp != null && packageName != null) |
| 159 | && (!gpuDebugApp.isEmpty() && !packageName.isEmpty()) |
| 160 | && gpuDebugApp.equals(packageName)) { |
| 161 | Log.i(TAG, "GPU debug layers enabled for " + packageName); |
| 162 | |
| 163 | // Prepend the debug layer path as a searchable path. |
| 164 | // This will ensure debug layers added will take precedence over |
| 165 | // the layers specified by the app. |
| 166 | layerPaths = mDebugLayerPath + ":"; |
| 167 | |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 168 | |
| 169 | // If there is a debug layer app specified, add its path. |
| 170 | String gpuDebugLayerApp = |
| 171 | coreSettings.getString(Settings.Global.GPU_DEBUG_LAYER_APP); |
| 172 | |
| 173 | if (gpuDebugLayerApp != null && !gpuDebugLayerApp.isEmpty()) { |
| 174 | Log.i(TAG, "GPU debug layer app: " + gpuDebugLayerApp); |
| 175 | String paths = getDebugLayerAppPaths(context, gpuDebugLayerApp); |
| 176 | if (paths != null) { |
| 177 | // Append the path so files placed in the app's base directory will |
| 178 | // override the external path |
| 179 | layerPaths += paths + ":"; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | String layers = coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 184 | |
Cody Northrop | 0fa1d22 | 2018-10-23 13:13:21 -0600 | [diff] [blame] | 185 | Log.i(TAG, "Vulkan debug layer list: " + layers); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 186 | if (layers != null && !layers.isEmpty()) { |
| 187 | setDebugLayers(layers); |
| 188 | } |
Cody Northrop | 0fa1d22 | 2018-10-23 13:13:21 -0600 | [diff] [blame] | 189 | |
| 190 | String layersGLES = |
| 191 | coreSettings.getString(Settings.Global.GPU_DEBUG_LAYERS_GLES); |
| 192 | |
| 193 | Log.i(TAG, "GLES debug layer list: " + layersGLES); |
| 194 | if (layersGLES != null && !layersGLES.isEmpty()) { |
| 195 | setDebugLayersGLES(layersGLES); |
| 196 | } |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 197 | } |
| 198 | } |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Include the app's lib directory in all cases |
| 202 | layerPaths += mLayerPath; |
| 203 | |
| 204 | setLayerPaths(mClassLoader, layerPaths); |
| 205 | } |
| 206 | |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 207 | enum OpenGlDriverChoice { |
| 208 | DEFAULT, |
| 209 | NATIVE, |
| 210 | ANGLE |
| 211 | } |
| 212 | |
| 213 | private static final Map<OpenGlDriverChoice, String> sDriverMap = buildMap(); |
| 214 | private static Map<OpenGlDriverChoice, String> buildMap() { |
| 215 | Map<OpenGlDriverChoice, String> map = new HashMap<>(); |
| 216 | map.put(OpenGlDriverChoice.DEFAULT, "default"); |
| 217 | map.put(OpenGlDriverChoice.ANGLE, "angle"); |
| 218 | map.put(OpenGlDriverChoice.NATIVE, "native"); |
| 219 | |
| 220 | return map; |
| 221 | } |
| 222 | |
| 223 | |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 224 | private static List<String> getGlobalSettingsString(Bundle bundle, String globalSetting) { |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 225 | List<String> valueList = null; |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 226 | String settingsValue = bundle.getString(globalSetting); |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 227 | |
| 228 | if (settingsValue != null) { |
| 229 | valueList = new ArrayList<>(Arrays.asList(settingsValue.split(","))); |
| 230 | } else { |
| 231 | valueList = new ArrayList<>(); |
| 232 | } |
| 233 | |
| 234 | return valueList; |
| 235 | } |
| 236 | |
| 237 | private static int getGlobalSettingsPkgIndex(String pkgName, |
| 238 | List<String> globalSettingsDriverPkgs) { |
| 239 | for (int pkgIndex = 0; pkgIndex < globalSettingsDriverPkgs.size(); pkgIndex++) { |
| 240 | if (globalSettingsDriverPkgs.get(pkgIndex).equals(pkgName)) { |
| 241 | return pkgIndex; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return -1; |
| 246 | } |
| 247 | |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 248 | private static String getDriverForPkg(Bundle bundle, String packageName) { |
| 249 | String allUseAngle = |
| 250 | bundle.getString(Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_ALL_ANGLE); |
| 251 | if ((allUseAngle != null) && allUseAngle.equals("1")) { |
| 252 | return sDriverMap.get(OpenGlDriverChoice.ANGLE); |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | List<String> globalSettingsDriverPkgs = |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 256 | getGlobalSettingsString(bundle, |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 257 | Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_PKGS); |
| 258 | List<String> globalSettingsDriverValues = |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 259 | getGlobalSettingsString(bundle, |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 260 | Settings.Global.GLOBAL_SETTINGS_ANGLE_GL_DRIVER_SELECTION_VALUES); |
| 261 | |
| 262 | // Make sure we have a good package name |
| 263 | if ((packageName == null) || (packageName.isEmpty())) { |
| 264 | return sDriverMap.get(OpenGlDriverChoice.DEFAULT); |
| 265 | } |
| 266 | // Make sure we have good settings to use |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 267 | if (globalSettingsDriverPkgs.size() != globalSettingsDriverValues.size()) { |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 268 | Log.w(TAG, |
| 269 | "Global.Settings values are invalid: " |
| 270 | + "globalSettingsDriverPkgs.size = " |
| 271 | + globalSettingsDriverPkgs.size() + ", " |
| 272 | + "globalSettingsDriverValues.size = " |
| 273 | + globalSettingsDriverValues.size()); |
| 274 | return sDriverMap.get(OpenGlDriverChoice.DEFAULT); |
| 275 | } |
| 276 | |
| 277 | int pkgIndex = getGlobalSettingsPkgIndex(packageName, globalSettingsDriverPkgs); |
| 278 | |
| 279 | if (pkgIndex < 0) { |
| 280 | return sDriverMap.get(OpenGlDriverChoice.DEFAULT); |
| 281 | } |
| 282 | |
| 283 | return globalSettingsDriverValues.get(pkgIndex); |
| 284 | } |
| 285 | |
Jesse Hall | c37984f | 2017-05-23 16:55:08 -0700 | [diff] [blame] | 286 | /** |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 287 | * Get the ANGLE package name. |
| 288 | */ |
| 289 | private String getAnglePackageName(Context context) { |
| 290 | Intent intent = new Intent(ACTION_ANGLE_FOR_ANDROID); |
| 291 | |
| 292 | List<ResolveInfo> resolveInfos = context.getPackageManager() |
| 293 | .queryIntentActivities(intent, PackageManager.MATCH_SYSTEM_ONLY); |
| 294 | if (resolveInfos.size() != 1) { |
| 295 | Log.e(TAG, "Invalid number of ANGLE packages. Required: 1, Found: " |
| 296 | + resolveInfos.size()); |
| 297 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 298 | Log.e(TAG, "Found ANGLE package: " + resolveInfo.activityInfo.packageName); |
| 299 | } |
| 300 | return ""; |
| 301 | } |
| 302 | |
| 303 | // Must be exactly 1 ANGLE PKG found to get here. |
| 304 | return resolveInfos.get(0).activityInfo.packageName; |
| 305 | } |
| 306 | |
| 307 | /** |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 308 | * Attempt to setup ANGLE with a temporary rules file. |
| 309 | * True: Temporary rules file was loaded. |
| 310 | * False: Temporary rules file was *not* loaded. |
| 311 | */ |
| 312 | private boolean setupAngleWithTempRulesFile(Context context, |
| 313 | String packageName, |
| 314 | String paths, |
| 315 | String devOptIn) { |
Tim Van Patten | 3df6643 | 2019-01-14 15:56:12 -0700 | [diff] [blame] | 316 | /** |
| 317 | * We only want to load a temp rules file for: |
| 318 | * - apps that are marked 'debuggable' in their manifest |
| 319 | * - devices that are running a userdebug build (ro.debuggable) or can inject libraries for |
| 320 | * debugging (PR_SET_DUMPABLE). |
| 321 | */ |
| 322 | boolean appIsDebuggable = isDebuggable(context); |
| 323 | boolean deviceIsDebuggable = getCanLoadSystemLibraries() == 1; |
| 324 | if (!(appIsDebuggable || deviceIsDebuggable)) { |
| 325 | Log.v(TAG, "Skipping loading temporary rules file: " |
| 326 | + "appIsDebuggable = " + appIsDebuggable + ", " |
| 327 | + "adbRootEnabled = " + deviceIsDebuggable); |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 328 | return false; |
| 329 | } |
| 330 | |
| 331 | String angleTempRules = SystemProperties.get(ANGLE_TEMP_RULES); |
| 332 | |
| 333 | if ((angleTempRules == null) || angleTempRules.isEmpty()) { |
| 334 | Log.v(TAG, "System property '" + ANGLE_TEMP_RULES + "' is not set or is empty"); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | Log.i(TAG, "Detected system property " + ANGLE_TEMP_RULES + ": " + angleTempRules); |
| 339 | |
| 340 | File tempRulesFile = new File(angleTempRules); |
| 341 | if (tempRulesFile.exists()) { |
| 342 | Log.i(TAG, angleTempRules + " exists, loading file."); |
| 343 | try { |
| 344 | FileInputStream stream = new FileInputStream(angleTempRules); |
| 345 | |
| 346 | try { |
| 347 | FileDescriptor rulesFd = stream.getFD(); |
| 348 | long rulesOffset = 0; |
| 349 | long rulesLength = stream.getChannel().size(); |
| 350 | Log.i(TAG, "Loaded temporary ANGLE rules from " + angleTempRules); |
| 351 | |
| 352 | setAngleInfo(paths, packageName, devOptIn, rulesFd, rulesOffset, rulesLength); |
| 353 | |
| 354 | stream.close(); |
| 355 | |
| 356 | // We successfully setup ANGLE, so return with good status |
| 357 | return true; |
| 358 | } catch (IOException e) { |
| 359 | Log.w(TAG, "Hit IOException thrown by FileInputStream: " + e); |
| 360 | } |
| 361 | } catch (FileNotFoundException e) { |
| 362 | Log.w(TAG, "Temp ANGLE rules file not found: " + e); |
| 363 | } catch (SecurityException e) { |
| 364 | Log.w(TAG, "Temp ANGLE rules file not accessible: " + e); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | return false; |
| 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Attempt to setup ANGLE with a (temporary) default rules file: b/121153494 |
| 373 | * True: Rules file was loaded. |
| 374 | * False: Rules file was *not* loaded. |
| 375 | */ |
| 376 | private boolean setupAngleRulesDebug(String packageName, String paths, String devOptIn) { |
| 377 | // b/121153494 |
| 378 | // Skip APK rules file checking. |
| 379 | if (!DEBUG) { |
| 380 | Log.v(TAG, "Skipping loading the rules file."); |
| 381 | // Fill in some default values for now, so the loader can get an answer when it asks. |
| 382 | // Most importantly, we need to indicate which app we are init'ing and what the |
| 383 | // developer options for it are so we can turn on ANGLE if needed. |
| 384 | setAngleInfo(paths, packageName, devOptIn, null, 0, 0); |
| 385 | return true; |
| 386 | } |
| 387 | |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Attempt to setup ANGLE with a rules file loaded from the ANGLE APK. |
| 393 | * True: APK rules file was loaded. |
| 394 | * False: APK rules file was *not* loaded. |
| 395 | */ |
| 396 | private boolean setupAngleRulesApk(String anglePkgName, |
| 397 | ApplicationInfo angleInfo, |
| 398 | Context context, |
| 399 | String packageName, |
| 400 | String paths, |
| 401 | String devOptIn) { |
| 402 | // Pass the rules file to loader for ANGLE decisions |
| 403 | try { |
| 404 | AssetManager angleAssets = |
| 405 | context.getPackageManager().getResourcesForApplication(angleInfo).getAssets(); |
| 406 | |
| 407 | try { |
| 408 | AssetFileDescriptor assetsFd = angleAssets.openFd(ANGLE_RULES_FILE); |
| 409 | |
| 410 | setAngleInfo(paths, packageName, devOptIn, assetsFd.getFileDescriptor(), |
| 411 | assetsFd.getStartOffset(), assetsFd.getLength()); |
| 412 | |
| 413 | assetsFd.close(); |
| 414 | |
| 415 | return true; |
| 416 | } catch (IOException e) { |
| 417 | Log.w(TAG, "Failed to get AssetFileDescriptor for " + ANGLE_RULES_FILE |
| 418 | + " from '" + anglePkgName + "': " + e); |
| 419 | } |
| 420 | } catch (PackageManager.NameNotFoundException e) { |
| 421 | Log.w(TAG, "Failed to get AssetManager for '" + anglePkgName + "': " + e); |
| 422 | } |
| 423 | |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | /** |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 428 | * Pass ANGLE details down to trigger enable logic |
Cody Northrop | 73c0504 | 2018-04-16 13:23:51 -0600 | [diff] [blame] | 429 | */ |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 430 | public void setupAngle(Context context, Bundle bundle, String packageName) { |
Tim Van Patten | ff9476b | 2018-12-11 15:41:25 -0700 | [diff] [blame] | 431 | String devOptIn = getDriverForPkg(bundle, packageName); |
Cody Northrop | 73c0504 | 2018-04-16 13:23:51 -0600 | [diff] [blame] | 432 | |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 433 | if (DEBUG) { |
| 434 | Log.v(TAG, "ANGLE Developer option for '" + packageName + "' " |
| 435 | + "set to: '" + devOptIn + "'"); |
Cody Northrop | 841c3d9 | 2018-08-29 16:36:12 -0600 | [diff] [blame] | 436 | } |
| 437 | |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 438 | String anglePkgName = getAnglePackageName(context); |
| 439 | if (anglePkgName.isEmpty()) { |
| 440 | Log.e(TAG, "Failed to find ANGLE package."); |
| 441 | return; |
| 442 | } |
| 443 | |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 444 | ApplicationInfo angleInfo; |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 445 | try { |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 446 | angleInfo = context.getPackageManager().getApplicationInfo(anglePkgName, |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 447 | PackageManager.MATCH_SYSTEM_ONLY); |
| 448 | } catch (PackageManager.NameNotFoundException e) { |
Tim Van Patten | 5e3e2ae | 2018-12-13 17:43:37 -0700 | [diff] [blame] | 449 | Log.w(TAG, "ANGLE package '" + anglePkgName + "' not installed"); |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 450 | return; |
| 451 | } |
| 452 | |
| 453 | String abi = chooseAbi(angleInfo); |
| 454 | |
| 455 | // Build a path that includes installed native libs and APK |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 456 | String paths = angleInfo.nativeLibraryDir |
| 457 | + File.pathSeparator |
| 458 | + angleInfo.sourceDir |
| 459 | + "!/lib/" |
| 460 | + abi; |
Cody Northrop | eb0ca8e | 2018-08-09 16:01:08 -0600 | [diff] [blame] | 461 | |
| 462 | if (DEBUG) Log.v(TAG, "ANGLE package libs: " + paths); |
| 463 | |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 464 | if (setupAngleWithTempRulesFile(context, packageName, paths, devOptIn)) { |
| 465 | // We setup ANGLE with a temp rules file, so we're done here. |
| 466 | return; |
Cody Northrop | 8d72a6b | 2018-11-01 08:20:26 -0600 | [diff] [blame] | 467 | } |
| 468 | |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 469 | // b/121153494 |
| 470 | if (setupAngleRulesDebug(packageName, paths, devOptIn)) { |
| 471 | // We setup ANGLE with defaults, so we're done here. |
| 472 | return; |
Cody Northrop | 7fd4421 | 2018-09-06 10:30:28 -0600 | [diff] [blame] | 473 | } |
| 474 | |
Tim Van Patten | a7577bf | 2018-12-12 11:47:24 -0700 | [diff] [blame] | 475 | if (setupAngleRulesApk(anglePkgName, angleInfo, context, packageName, paths, devOptIn)) { |
| 476 | // We setup ANGLE with rules from the APK, so we're done here. |
| 477 | return; |
| 478 | } |
Cody Northrop | 73c0504 | 2018-04-16 13:23:51 -0600 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
Jesse Hall | c37984f | 2017-05-23 16:55:08 -0700 | [diff] [blame] | 482 | * Choose whether the current process should use the builtin or an updated driver. |
Jesse Hall | c37984f | 2017-05-23 16:55:08 -0700 | [diff] [blame] | 483 | */ |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 484 | private static void chooseDriver(Context context, Bundle coreSettings) { |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 485 | String driverPackageName = SystemProperties.get(PROPERTY_GFX_DRIVER); |
| 486 | if (driverPackageName == null || driverPackageName.isEmpty()) { |
| 487 | return; |
| 488 | } |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 489 | |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 490 | ApplicationInfo driverInfo; |
| 491 | try { |
| 492 | driverInfo = context.getPackageManager().getApplicationInfo(driverPackageName, |
| 493 | PackageManager.MATCH_SYSTEM_ONLY); |
| 494 | } catch (PackageManager.NameNotFoundException e) { |
| 495 | Log.w(TAG, "driver package '" + driverPackageName + "' not installed"); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | // O drivers are restricted to the sphal linker namespace, so don't try to use |
| 500 | // packages unless they declare they're compatible with that restriction. |
| 501 | if (driverInfo.targetSdkVersion < Build.VERSION_CODES.O) { |
| 502 | if (DEBUG) { |
| 503 | Log.w(TAG, "updated driver package is not known to be compatible with O"); |
| 504 | } |
| 505 | return; |
| 506 | } |
| 507 | |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 508 | // To minimize risk of driver updates crippling the device beyond user repair, never use an |
| 509 | // updated driver for privileged or non-updated system apps. Presumably pre-installed apps |
| 510 | // were tested thoroughly with the pre-installed driver. |
| 511 | ApplicationInfo ai = context.getApplicationInfo(); |
| 512 | if (ai.isPrivilegedApp() || (ai.isSystemApp() && !ai.isUpdatedSystemApp())) { |
| 513 | if (DEBUG) Log.v(TAG, "ignoring driver package for privileged/non-updated system app"); |
| 514 | return; |
| 515 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 516 | |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 517 | // GUP_DEV_ALL_APPS |
| 518 | // 0: Default (Invalid values fallback to default as well) |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 519 | // 1: All apps use Game Driver |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 520 | // 2: All apps use system graphics driver |
| 521 | int gupDevAllApps = coreSettings.getInt(Settings.Global.GUP_DEV_ALL_APPS, 0); |
| 522 | if (gupDevAllApps == 2) { |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 523 | if (DEBUG) { |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 524 | Log.w(TAG, "GUP is turned off on this device"); |
Yiwei Zhang | 51015a7 | 2018-12-29 03:47:56 +0800 | [diff] [blame] | 525 | } |
| 526 | return; |
| 527 | } |
| 528 | |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 529 | if (gupDevAllApps != 1) { |
| 530 | // GUP_DEV_OPT_OUT_APPS has higher priority than GUP_DEV_OPT_IN_APPS |
| 531 | if (getGlobalSettingsString(coreSettings, Settings.Global.GUP_DEV_OPT_OUT_APPS) |
| 532 | .contains(ai.packageName)) { |
| 533 | if (DEBUG) { |
| 534 | Log.w(TAG, ai.packageName + " opts out from GUP."); |
| 535 | } |
| 536 | return; |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 537 | } |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 538 | boolean isDevOptIn = getGlobalSettingsString(coreSettings, |
| 539 | Settings.Global.GUP_DEV_OPT_IN_APPS) |
| 540 | .contains(ai.packageName); |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 541 | |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 542 | if (!isDevOptIn && !onWhitelist(context, driverPackageName, ai.packageName)) { |
Yiwei Zhang | 4a626ac | 2019-01-10 10:23:09 +0800 | [diff] [blame] | 543 | if (DEBUG) { |
| 544 | Log.w(TAG, ai.packageName + " is not on the whitelist."); |
| 545 | } |
| 546 | return; |
| 547 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 548 | |
Peiyong Lin | 9ca1dd8 | 2019-01-12 17:44:29 -0800 | [diff] [blame] | 549 | if (!isDevOptIn) { |
| 550 | // At this point, the application is on the whitelist only, check whether it's |
| 551 | // on the blacklist, terminate early when it's on the blacklist. |
| 552 | try { |
| 553 | // TODO(b/121350991) Switch to DeviceConfig with property listener. |
| 554 | String base64String = coreSettings.getString(Settings.Global.GUP_BLACKLIST); |
| 555 | if (base64String != null && !base64String.isEmpty()) { |
| 556 | Blacklists blacklistsProto = Blacklists.parseFrom( |
| 557 | Base64.decode(base64String, BASE64_FLAGS)); |
| 558 | List<Blacklist> blacklists = blacklistsProto.getBlacklistsList(); |
| 559 | long driverVersionCode = driverInfo.longVersionCode; |
| 560 | for (Blacklist blacklist : blacklists) { |
| 561 | if (blacklist.getVersionCode() == driverVersionCode) { |
| 562 | for (String packageName : blacklist.getPackageNamesList()) { |
| 563 | if (packageName == ai.packageName) { |
| 564 | return; |
| 565 | } |
| 566 | } |
| 567 | break; |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | } catch (InvalidProtocolBufferException e) { |
| 572 | if (DEBUG) { |
| 573 | Log.w(TAG, "Can't parse blacklist, skip and continue..."); |
| 574 | } |
| 575 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 576 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 579 | String abi = chooseAbi(driverInfo); |
| 580 | if (abi == null) { |
| 581 | if (DEBUG) { |
| 582 | // This is the normal case for the pre-installed empty driver package, don't spam |
| 583 | if (driverInfo.isUpdatedSystemApp()) { |
| 584 | Log.w(TAG, "updated driver package has no compatible native libraries"); |
| 585 | } |
| 586 | } |
| 587 | return; |
| 588 | } |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 589 | |
| 590 | StringBuilder sb = new StringBuilder(); |
| 591 | sb.append(driverInfo.nativeLibraryDir) |
| 592 | .append(File.pathSeparator); |
| 593 | sb.append(driverInfo.sourceDir) |
| 594 | .append("!/lib/") |
| 595 | .append(abi); |
| 596 | String paths = sb.toString(); |
| 597 | |
| 598 | if (DEBUG) Log.v(TAG, "gfx driver package libs: " + paths); |
| 599 | setDriverPath(paths); |
| 600 | } |
| 601 | |
Jesse Hall | c37984f | 2017-05-23 16:55:08 -0700 | [diff] [blame] | 602 | /** |
| 603 | * Start a background thread to initialize EGL. |
| 604 | * |
| 605 | * Initializing EGL involves loading and initializing the graphics driver. Some drivers take |
| 606 | * several 10s of milliseconds to do this, so doing it on-demand when an app tries to render |
| 607 | * its first frame adds directly to user-visible app launch latency. By starting it earlier |
| 608 | * on a separate thread, it can usually be finished well before the UI is ready to be drawn. |
| 609 | * |
| 610 | * Should only be called after chooseDriver(). |
Jesse Hall | c37984f | 2017-05-23 16:55:08 -0700 | [diff] [blame] | 611 | */ |
| 612 | public static void earlyInitEGL() { |
Jesse Hall | 317fa5a | 2017-05-23 15:46:55 -0700 | [diff] [blame] | 613 | Thread eglInitThread = new Thread( |
| 614 | () -> { |
| 615 | EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); |
| 616 | }, |
| 617 | "EGL Init"); |
| 618 | eglInitThread.start(); |
| 619 | } |
| 620 | |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 621 | private static String chooseAbi(ApplicationInfo ai) { |
| 622 | String isa = VMRuntime.getCurrentInstructionSet(); |
| 623 | if (ai.primaryCpuAbi != null && |
| 624 | isa.equals(VMRuntime.getInstructionSet(ai.primaryCpuAbi))) { |
| 625 | return ai.primaryCpuAbi; |
| 626 | } |
| 627 | if (ai.secondaryCpuAbi != null && |
| 628 | isa.equals(VMRuntime.getInstructionSet(ai.secondaryCpuAbi))) { |
| 629 | return ai.secondaryCpuAbi; |
| 630 | } |
| 631 | return null; |
| 632 | } |
| 633 | |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 634 | private static boolean onWhitelist(Context context, String driverPackageName, |
| 635 | String applicationPackageName) { |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 636 | try { |
| 637 | Context driverContext = context.createPackageContext(driverPackageName, |
| 638 | Context.CONTEXT_RESTRICTED); |
| 639 | AssetManager assets = driverContext.getAssets(); |
Peiyong Lin | 3a1be74 | 2019-01-02 17:46:55 -0800 | [diff] [blame] | 640 | InputStream stream = assets.open(GUP_WHITELIST_FILENAME); |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 641 | BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 642 | for (String packageName; (packageName = reader.readLine()) != null; ) { |
| 643 | if (packageName.equals(applicationPackageName)) { |
| 644 | return true; |
| 645 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 646 | } |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 647 | } catch (PackageManager.NameNotFoundException e) { |
| 648 | if (DEBUG) { |
| 649 | Log.w(TAG, "driver package '" + driverPackageName + "' not installed"); |
| 650 | } |
| 651 | } catch (IOException e) { |
| 652 | if (DEBUG) { |
| 653 | Log.w(TAG, "Failed to load whitelist driver package, abort."); |
| 654 | } |
| 655 | } |
Peiyong Lin | 2d5a2bb | 2018-11-07 21:10:07 -0800 | [diff] [blame] | 656 | return false; |
Peiyong Lin | 88c38eb | 2018-10-12 15:34:15 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Cody Northrop | ebe6a56 | 2018-10-15 07:22:23 -0600 | [diff] [blame] | 659 | private static native int getCanLoadSystemLibraries(); |
Cody Northrop | 86cedcb | 2017-10-20 09:03:13 -0600 | [diff] [blame] | 660 | private static native void setLayerPaths(ClassLoader classLoader, String layerPaths); |
| 661 | private static native void setDebugLayers(String layers); |
Cody Northrop | 0fa1d22 | 2018-10-23 13:13:21 -0600 | [diff] [blame] | 662 | private static native void setDebugLayersGLES(String layers); |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 663 | private static native void setDriverPath(String path); |
Cody Northrop | 6d8d968 | 2018-10-29 10:59:49 -0600 | [diff] [blame] | 664 | private static native void setAngleInfo(String path, String appPackage, |
Tim Van Patten | 3c61284 | 2018-11-09 16:48:24 -0700 | [diff] [blame] | 665 | String devOptIn, FileDescriptor rulesFd, |
Cody Northrop | 7fd4421 | 2018-09-06 10:30:28 -0600 | [diff] [blame] | 666 | long rulesOffset, long rulesLength); |
Jesse Hall | 79bf392 | 2016-12-12 12:53:02 -0800 | [diff] [blame] | 667 | } |