blob: 5cb2bec08f865d9404ecdd36ec2435aa2f50a5e8 [file] [log] [blame]
Jesse Hall90b25ed2016-12-12 12:56:46 -08001/*
2 * Copyright 2017 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
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080017#define ATRACE_TAG ATRACE_TAG_GRAPHICS
18
Jesse Hall90b25ed2016-12-12 12:56:46 -080019//#define LOG_NDEBUG 1
20#define LOG_TAG "GraphicsEnv"
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080021
Jiyong Park27c39e12017-05-08 13:00:02 +090022#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080023
Yiwei Zhang64d89212018-11-27 19:58:29 -080024#include <dlfcn.h>
Tim Van Patten5f744f12018-12-12 11:46:21 -070025#include <unistd.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080026
27#include <android-base/file.h>
28#include <android-base/properties.h>
29#include <android-base/strings.h>
30#include <android/dlext.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080031#include <binder/IServiceManager.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080032#include <cutils/properties.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080033#include <graphicsenv/IGpuService.h>
Yiwei Zhang64d89212018-11-27 19:58:29 -080034#include <log/log.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060035#include <sys/prctl.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080036#include <utils/Trace.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060037
Tim Van Patten5f744f12018-12-12 11:46:21 -070038#include <memory>
Tim Van Patten5f744f12018-12-12 11:46:21 -070039#include <string>
Yiwei Zhang3c74da92019-06-28 10:16:49 -070040#include <thread>
Tim Van Patten5f744f12018-12-12 11:46:21 -070041
Jesse Hall57de0ff2017-05-05 16:41:35 -070042// TODO(b/37049319) Get this from a header once one exists
43extern "C" {
Yiwei Zhang64d89212018-11-27 19:58:29 -080044android_namespace_t* android_get_exported_namespace(const char*);
45android_namespace_t* android_create_namespace(const char* name, const char* ld_library_path,
46 const char* default_library_path, uint64_t type,
47 const char* permitted_when_isolated_path,
48 android_namespace_t* parent);
49bool android_link_namespaces(android_namespace_t* from, android_namespace_t* to,
50 const char* shared_libs_sonames);
Jiyong Park9b816a82018-01-02 17:37:37 +090051
Yiwei Zhang64d89212018-11-27 19:58:29 -080052enum {
53 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
54 ANDROID_NAMESPACE_TYPE_SHARED = 2,
55};
Jesse Hall57de0ff2017-05-05 16:41:35 -070056}
57
Tim Van Patten5f744f12018-12-12 11:46:21 -070058// TODO(ianelliott@): Get the following from an ANGLE header:
59#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
60// Version-2 API:
61typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
62typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
63 int* rulesVersion);
64typedef bool (*fpANGLEGetSystemInfo)(void** handle);
65typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
66 void* handle);
67typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
68 void* systemInfoHandle, const char* appName);
69typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
70typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
71
Jesse Hall90b25ed2016-12-12 12:56:46 -080072namespace android {
73
Yiwei Zhang64d89212018-11-27 19:58:29 -080074enum NativeLibrary {
75 LLNDK = 0,
76 VNDKSP = 1,
77};
78
Jooyung Han78396802020-02-23 03:02:43 +090079static constexpr const char* kNativeLibrariesSystemConfigPath[] =
80 {"/apex/com.android.vndk.v{}/etc/llndk.libraries.{}.txt",
81 "/apex/com.android.vndk.v{}/etc/vndksp.libraries.{}.txt"};
Yiwei Zhang64d89212018-11-27 19:58:29 -080082
83static std::string vndkVersionStr() {
84#ifdef __BIONIC__
Jooyung Han78396802020-02-23 03:02:43 +090085 return android::base::GetProperty("ro.vndk.version", "");
Yiwei Zhang64d89212018-11-27 19:58:29 -080086#endif
87 return "";
88}
89
90static void insertVndkVersionStr(std::string* fileName) {
91 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
Jooyung Han78396802020-02-23 03:02:43 +090092 std::string version = vndkVersionStr();
93 size_t pos = fileName->find("{}");
94 while (pos != std::string::npos) {
95 fileName->replace(pos, 2, version);
96 pos = fileName->find("{}", pos + version.size());
Yiwei Zhang64d89212018-11-27 19:58:29 -080097 }
Yiwei Zhang64d89212018-11-27 19:58:29 -080098}
99
100static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
101 // Read list of public native libraries from the config file.
102 std::string fileContent;
103 if (!base::ReadFileToString(configFile, &fileContent)) {
104 return false;
105 }
106
107 std::vector<std::string> lines = base::Split(fileContent, "\n");
108
109 for (auto& line : lines) {
110 auto trimmedLine = base::Trim(line);
111 if (!trimmedLine.empty()) {
112 soNames->push_back(trimmedLine);
113 }
114 }
115
116 return true;
117}
118
119static const std::string getSystemNativeLibraries(NativeLibrary type) {
Jooyung Han78396802020-02-23 03:02:43 +0900120 std::string nativeLibrariesSystemConfig = kNativeLibrariesSystemConfigPath[type];
Yiwei Zhang64d89212018-11-27 19:58:29 -0800121 insertVndkVersionStr(&nativeLibrariesSystemConfig);
122
123 std::vector<std::string> soNames;
124 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
125 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
126 return "";
127 }
128
129 return base::Join(soNames, ':');
130}
131
Jesse Hall90b25ed2016-12-12 12:56:46 -0800132/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
133 static GraphicsEnv env;
134 return env;
135}
136
Cody Northrop629ce4e2018-10-15 07:22:09 -0600137int GraphicsEnv::getCanLoadSystemLibraries() {
138 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
139 // Return an integer value since this crosses library boundaries
140 return 1;
141 }
142 return 0;
143}
144
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800145void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
146 const std::string sphalLibraries) {
147 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
148 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
149 "from '%s' to '%s'",
150 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800151 return;
152 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800153 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
154 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800155 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800156 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800157}
158
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700159void GraphicsEnv::hintActivityLaunch() {
160 ATRACE_CALL();
161
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700162 std::thread trySendGpuStatsThread([this]() {
163 // If there's already graphics driver preloaded in the process, just send
164 // the stats info to GpuStats directly through async binder.
165 std::lock_guard<std::mutex> lock(mStatsLock);
166 if (mGpuStats.glDriverToSend) {
167 mGpuStats.glDriverToSend = false;
168 sendGpuStatsLocked(GraphicsEnv::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
169 }
170 if (mGpuStats.vkDriverToSend) {
171 mGpuStats.vkDriverToSend = false;
172 sendGpuStatsLocked(GraphicsEnv::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
173 }
174 });
175 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700176}
177
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800178void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800179 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700180 int64_t driverBuildTime, const std::string& appPackageName,
181 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800182 ATRACE_CALL();
183
Yiwei Zhangd9861812019-02-13 11:51:55 -0800184 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800185 ALOGV("setGpuStats:\n"
186 "\tdriverPackageName[%s]\n"
187 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800188 "\tdriverVersionCode[%" PRIu64 "]\n"
189 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700190 "\tappPackageName[%s]\n"
191 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800192 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700193 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800194
Yiwei Zhangd9861812019-02-13 11:51:55 -0800195 mGpuStats.driverPackageName = driverPackageName;
196 mGpuStats.driverVersionName = driverVersionName;
197 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800198 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800199 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700200 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800201}
202
Yiwei Zhangd9861812019-02-13 11:51:55 -0800203void GraphicsEnv::setDriverToLoad(GraphicsEnv::Driver driver) {
204 ATRACE_CALL();
205
206 std::lock_guard<std::mutex> lock(mStatsLock);
207 switch (driver) {
208 case GraphicsEnv::Driver::GL:
209 case GraphicsEnv::Driver::GL_UPDATED:
210 case GraphicsEnv::Driver::ANGLE: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700211 if (mGpuStats.glDriverToLoad == GraphicsEnv::Driver::NONE ||
212 mGpuStats.glDriverToLoad == GraphicsEnv::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800213 mGpuStats.glDriverToLoad = driver;
214 break;
215 }
216
217 if (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE) {
218 mGpuStats.glDriverFallback = driver;
219 }
220 break;
221 }
222 case Driver::VULKAN:
223 case Driver::VULKAN_UPDATED: {
Yiwei Zhang8238fa02019-08-05 17:57:41 -0700224 if (mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::NONE ||
225 mGpuStats.vkDriverToLoad == GraphicsEnv::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800226 mGpuStats.vkDriverToLoad = driver;
227 break;
228 }
229
230 if (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE) {
231 mGpuStats.vkDriverFallback = driver;
232 }
233 break;
234 }
235 default:
236 break;
237 }
238}
239
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700240void GraphicsEnv::setDriverLoaded(GraphicsEnv::Api api, bool isDriverLoaded,
241 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800242 ATRACE_CALL();
243
244 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700245 const bool doNotSend = mGpuStats.appPackageName.empty();
Yiwei Zhangd9861812019-02-13 11:51:55 -0800246 if (api == GraphicsEnv::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700247 if (doNotSend) mGpuStats.glDriverToSend = true;
248 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800249 } else {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700250 if (doNotSend) mGpuStats.vkDriverToSend = true;
251 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800252 }
253
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700254 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800255}
256
Yiwei Zhangd9861812019-02-13 11:51:55 -0800257static sp<IGpuService> getGpuService() {
258 const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
259 if (!binder) {
260 ALOGE("Failed to get gpu service");
261 return nullptr;
262 }
263
264 return interface_cast<IGpuService>(binder);
265}
266
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700267void GraphicsEnv::setTargetStats(const Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700268 ATRACE_CALL();
269
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700270 std::lock_guard<std::mutex> lock(mStatsLock);
271 const sp<IGpuService> gpuService = getGpuService();
272 if (gpuService) {
Yiwei Zhangb1c1a372019-07-03 13:39:32 -0700273 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
274 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700275 }
276}
277
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700278void GraphicsEnv::sendGpuStatsLocked(GraphicsEnv::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800279 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800280 ATRACE_CALL();
281
282 // Do not sendGpuStats for those skipping the GraphicsEnvironment setup
283 if (mGpuStats.appPackageName.empty()) return;
284
285 ALOGV("sendGpuStats:\n"
286 "\tdriverPackageName[%s]\n"
287 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800288 "\tdriverVersionCode[%" PRIu64 "]\n"
289 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800290 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700291 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700292 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800293 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800294 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800295 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800296 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700297 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
298
299 GraphicsEnv::Driver driver = GraphicsEnv::Driver::NONE;
300 bool isIntendedDriverLoaded = false;
301 if (api == GraphicsEnv::Api::API_GL) {
302 driver = mGpuStats.glDriverToLoad;
303 isIntendedDriverLoaded =
304 isDriverLoaded && (mGpuStats.glDriverFallback == GraphicsEnv::Driver::NONE);
305 } else {
306 driver = mGpuStats.vkDriverToLoad;
307 isIntendedDriverLoaded =
308 isDriverLoaded && (mGpuStats.vkDriverFallback == GraphicsEnv::Driver::NONE);
309 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800310
Yiwei Zhangd9861812019-02-13 11:51:55 -0800311 const sp<IGpuService> gpuService = getGpuService();
312 if (gpuService) {
313 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800314 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700315 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700316 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800317 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800318}
319
Tim Van Patten5f744f12018-12-12 11:46:21 -0700320void* GraphicsEnv::loadLibrary(std::string name) {
321 const android_dlextinfo dlextinfo = {
322 .flags = ANDROID_DLEXT_USE_NAMESPACE,
323 .library_namespace = getAngleNamespace(),
324 };
325
326 std::string libName = std::string("lib") + name + "_angle.so";
327
328 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
329
330 if (so) {
331 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
332 return so;
333 } else {
334 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
335 }
336
337 return nullptr;
338}
339
340bool GraphicsEnv::checkAngleRules(void* so) {
341 char manufacturer[PROPERTY_VALUE_MAX];
342 char model[PROPERTY_VALUE_MAX];
343 property_get("ro.product.manufacturer", manufacturer, "UNSET");
344 property_get("ro.product.model", model, "UNSET");
345
346 auto ANGLEGetFeatureSupportUtilAPIVersion =
347 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
348 "ANGLEGetFeatureSupportUtilAPIVersion");
349
350 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
351 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
352 return false;
353 }
354
355 // Negotiate the interface version by requesting most recent known to the platform
356 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
357 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
358 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
359 "requested version %u",
360 versionToUse);
361 return false;
362 }
363
364 // Add and remove versions below as needed
365 bool useAngle = false;
366 switch (versionToUse) {
367 case 2: {
368 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
369 void* rulesHandle = nullptr;
370 int rulesVersion = 0;
371 void* systemInfoHandle = nullptr;
372
373 // Get the symbols for the feature-support-utility library:
374#define GET_SYMBOL(symbol) \
375 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
376 if (!symbol) { \
377 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
378 break; \
379 }
380 GET_SYMBOL(ANGLEAndroidParseRulesString);
381 GET_SYMBOL(ANGLEGetSystemInfo);
382 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
383 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
384 GET_SYMBOL(ANGLEFreeRulesHandle);
385 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
386
387 // Parse the rules, obtain the SystemInfo, and evaluate the
388 // application against the rules:
389 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
390 ALOGW("ANGLE feature-support library cannot parse rules file");
391 break;
392 }
393 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
394 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
395 break;
396 }
397 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
398 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
399 break;
400 }
401 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
402 systemInfoHandle, mAngleAppName.c_str());
403 (ANGLEFreeRulesHandle)(rulesHandle);
404 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
405 } break;
406
407 default:
408 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
409 }
410
411 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
412 return useAngle;
413}
414
415bool GraphicsEnv::shouldUseAngle(std::string appName) {
416 if (appName != mAngleAppName) {
417 // Make sure we are checking the app we were init'ed for
418 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
419 appName.c_str());
420 return false;
421 }
422
423 return shouldUseAngle();
424}
425
426bool GraphicsEnv::shouldUseAngle() {
427 // Make sure we are init'ed
428 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700429 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700430 return false;
431 }
432
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700433 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700434}
435
436void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700437 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700438
439 const char* ANGLE_PREFER_ANGLE = "angle";
440 const char* ANGLE_PREFER_NATIVE = "native";
441
442 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
443 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700444 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700445 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
446 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700447 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700448 } else {
449 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
450 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700451 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700452 if (featureSo) {
453 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700454 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700455 dlclose(featureSo);
456 featureSo = nullptr;
457 } else {
458 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
459 }
460 }
461}
462
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600463void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700464 const std::string developerOptIn, const int rulesFd,
465 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700466 if (mUseAngle != UNKNOWN) {
467 // We've already figured out an answer for this app, so just return.
468 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
469 (mUseAngle == YES) ? "true" : "false");
470 return;
471 }
472
Tim Van Patten5f744f12018-12-12 11:46:21 -0700473 ALOGV("setting ANGLE path to '%s'", path.c_str());
474 mAnglePath = path;
475 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
476 mAngleAppName = appName;
477 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
478 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600479
Tim Van Patten5f744f12018-12-12 11:46:21 -0700480 lseek(rulesFd, rulesOffset, SEEK_SET);
481 mRulesBuffer = std::vector<char>(rulesLength + 1);
482 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
483 if (numBytesRead < 0) {
484 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
485 numBytesRead = 0;
486 } else if (numBytesRead == 0) {
487 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600488 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700489 if (numBytesRead != rulesLength) {
490 ALOGW("Did not read all of the necessary bytes from the rules file."
491 "expected: %ld, got: %zd",
492 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700493 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700494 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600495
Tim Van Patten5f744f12018-12-12 11:46:21 -0700496 // Update the current status of whether we should use ANGLE or not
497 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600498}
499
Victor Khimenko4819b522018-07-13 17:24:18 +0200500void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600501 if (mLayerPaths.empty()) {
502 mLayerPaths = layerPaths;
503 mAppNamespace = appNamespace;
504 } else {
505 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800506 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600507 }
508}
509
Victor Khimenko4819b522018-07-13 17:24:18 +0200510NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600511 return mAppNamespace;
512}
513
Tim Van Patten5f744f12018-12-12 11:46:21 -0700514std::string& GraphicsEnv::getAngleAppName() {
515 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600516}
517
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600518const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600519 return mLayerPaths;
520}
521
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600522const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600523 return mDebugLayers;
524}
525
Cody Northropb9b01b62018-10-23 13:13:10 -0600526const std::string& GraphicsEnv::getDebugLayersGLES() {
527 return mDebugLayersGLES;
528}
529
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600530void GraphicsEnv::setDebugLayers(const std::string layers) {
531 mDebugLayers = layers;
532}
533
Cody Northropb9b01b62018-10-23 13:13:10 -0600534void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
535 mDebugLayersGLES = layers;
536}
537
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700538// Return true if all the required libraries from vndk and sphal namespace are
539// linked to the Game Driver namespace correctly.
540bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
541 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
542 if (llndkLibraries.empty()) {
543 return false;
544 }
545 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
546 ALOGE("Failed to link default namespace[%s]", dlerror());
547 return false;
548 }
549
550 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
551 if (vndkspLibraries.empty()) {
552 return false;
553 }
554 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
555 ALOGE("Failed to link vndk namespace[%s]", dlerror());
556 return false;
557 }
558
559 if (mSphalLibraries.empty()) {
560 return true;
561 }
562
563 // Make additional libraries in sphal to be accessible
564 auto sphalNamespace = android_get_exported_namespace("sphal");
565 if (!sphalNamespace) {
566 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
567 mSphalLibraries.c_str());
568 return false;
569 }
570
571 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
572 ALOGE("Failed to link sphal namespace[%s]", dlerror());
573 return false;
574 }
575
576 return true;
577}
578
Jesse Hall53457db2016-12-14 16:54:06 -0800579android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700580 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800581
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700582 if (mDriverNamespace) {
583 return mDriverNamespace;
584 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800585
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700586 if (mDriverPath.empty()) {
587 return nullptr;
588 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800589
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700590 auto vndkNamespace = android_get_exported_namespace("vndk");
591 if (!vndkNamespace) {
592 return nullptr;
593 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800594
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700595 mDriverNamespace = android_create_namespace("gfx driver",
596 mDriverPath.c_str(), // ld_library_path
597 mDriverPath.c_str(), // default_library_path
598 ANDROID_NAMESPACE_TYPE_ISOLATED,
599 nullptr, // permitted_when_isolated_path
600 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800601
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700602 if (!linkDriverNamespaceLocked(vndkNamespace)) {
603 mDriverNamespace = nullptr;
604 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800605
Jesse Hall53457db2016-12-14 16:54:06 -0800606 return mDriverNamespace;
607}
608
Cody Northrop1f00e172018-04-02 11:23:31 -0600609android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700610 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600611
Cody Northrop3892cfa2019-01-30 10:03:12 -0700612 if (mAngleNamespace) {
613 return mAngleNamespace;
614 }
615
616 if (mAnglePath.empty()) {
617 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
618 return nullptr;
619 }
620
621 mAngleNamespace = android_create_namespace("ANGLE",
622 nullptr, // ld_library_path
623 mAnglePath.c_str(), // default_library_path
624 ANDROID_NAMESPACE_TYPE_SHARED |
625 ANDROID_NAMESPACE_TYPE_ISOLATED,
626 nullptr, // permitted_when_isolated_path
627 nullptr);
628
629 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600630
631 return mAngleNamespace;
632}
633
Jesse Hall90b25ed2016-12-12 12:56:46 -0800634} // namespace android