blob: f07c23132d08db5729180c34223575bfd3741c8b [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>
Yiwei Zhang49b9ac72019-08-05 16:57:17 -070035#include <nativeloader/dlext_namespaces.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060036#include <sys/prctl.h>
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -080037#include <utils/Trace.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060038
Tim Van Patten5f744f12018-12-12 11:46:21 -070039#include <memory>
Tim Van Patten5f744f12018-12-12 11:46:21 -070040#include <string>
Yiwei Zhang3c74da92019-06-28 10:16:49 -070041#include <thread>
Tim Van Patten5f744f12018-12-12 11:46:21 -070042
Tim Van Patten5f744f12018-12-12 11:46:21 -070043// TODO(ianelliott@): Get the following from an ANGLE header:
44#define CURRENT_ANGLE_API_VERSION 2 // Current API verion we are targetting
45// Version-2 API:
46typedef bool (*fpANGLEGetFeatureSupportUtilAPIVersion)(unsigned int* versionToUse);
47typedef bool (*fpANGLEAndroidParseRulesString)(const char* rulesString, void** rulesHandle,
48 int* rulesVersion);
49typedef bool (*fpANGLEGetSystemInfo)(void** handle);
50typedef bool (*fpANGLEAddDeviceInfoToSystemInfo)(const char* deviceMfr, const char* deviceModel,
51 void* handle);
52typedef bool (*fpANGLEShouldBeUsedForApplication)(void* rulesHandle, int rulesVersion,
53 void* systemInfoHandle, const char* appName);
54typedef bool (*fpANGLEFreeRulesHandle)(void* handle);
55typedef bool (*fpANGLEFreeSystemInfoHandle)(void* handle);
56
Jesse Hall90b25ed2016-12-12 12:56:46 -080057namespace android {
58
Yiwei Zhang64d89212018-11-27 19:58:29 -080059enum NativeLibrary {
60 LLNDK = 0,
61 VNDKSP = 1,
62};
63
64static constexpr const char* kNativeLibrariesSystemConfigPath[] = {"/etc/llndk.libraries.txt",
65 "/etc/vndksp.libraries.txt"};
66
67static std::string vndkVersionStr() {
68#ifdef __BIONIC__
69 std::string version = android::base::GetProperty("ro.vndk.version", "");
70 if (version != "" && version != "current") {
71 return "." + version;
72 }
73#endif
74 return "";
75}
76
77static void insertVndkVersionStr(std::string* fileName) {
78 LOG_ALWAYS_FATAL_IF(!fileName, "fileName should never be nullptr");
79 size_t insertPos = fileName->find_last_of(".");
80 if (insertPos == std::string::npos) {
81 insertPos = fileName->length();
82 }
83 fileName->insert(insertPos, vndkVersionStr());
84}
85
86static bool readConfig(const std::string& configFile, std::vector<std::string>* soNames) {
87 // Read list of public native libraries from the config file.
88 std::string fileContent;
89 if (!base::ReadFileToString(configFile, &fileContent)) {
90 return false;
91 }
92
93 std::vector<std::string> lines = base::Split(fileContent, "\n");
94
95 for (auto& line : lines) {
96 auto trimmedLine = base::Trim(line);
97 if (!trimmedLine.empty()) {
98 soNames->push_back(trimmedLine);
99 }
100 }
101
102 return true;
103}
104
105static const std::string getSystemNativeLibraries(NativeLibrary type) {
106 static const char* androidRootEnv = getenv("ANDROID_ROOT");
107 static const std::string rootDir = androidRootEnv != nullptr ? androidRootEnv : "/system";
108
109 std::string nativeLibrariesSystemConfig = rootDir + kNativeLibrariesSystemConfigPath[type];
110
111 insertVndkVersionStr(&nativeLibrariesSystemConfig);
112
113 std::vector<std::string> soNames;
114 if (!readConfig(nativeLibrariesSystemConfig, &soNames)) {
115 ALOGE("Failed to retrieve library names from %s", nativeLibrariesSystemConfig.c_str());
116 return "";
117 }
118
119 return base::Join(soNames, ':');
120}
121
Jesse Hall90b25ed2016-12-12 12:56:46 -0800122/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
123 static GraphicsEnv env;
124 return env;
125}
126
Yiwei Zhang6a674c92019-11-08 11:55:36 -0800127bool GraphicsEnv::isDebuggable() {
128 return prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) > 0;
Cody Northrop629ce4e2018-10-15 07:22:09 -0600129}
130
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800131void GraphicsEnv::setDriverPathAndSphalLibraries(const std::string path,
132 const std::string sphalLibraries) {
133 if (!mDriverPath.empty() || !mSphalLibraries.empty()) {
134 ALOGV("ignoring attempt to change driver path from '%s' to '%s' or change sphal libraries "
135 "from '%s' to '%s'",
136 mDriverPath.c_str(), path.c_str(), mSphalLibraries.c_str(), sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800137 return;
138 }
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800139 ALOGV("setting driver path to '%s' and sphal libraries to '%s'", path.c_str(),
140 sphalLibraries.c_str());
Jesse Hall90b25ed2016-12-12 12:56:46 -0800141 mDriverPath = path;
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800142 mSphalLibraries = sphalLibraries;
Jesse Hall90b25ed2016-12-12 12:56:46 -0800143}
144
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700145void GraphicsEnv::hintActivityLaunch() {
146 ATRACE_CALL();
147
Yiwei Zhangd3819382020-01-07 19:53:56 -0800148 {
149 std::lock_guard<std::mutex> lock(mStatsLock);
150 if (mActivityLaunched) return;
151 mActivityLaunched = true;
152 }
153
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700154 std::thread trySendGpuStatsThread([this]() {
155 // If there's already graphics driver preloaded in the process, just send
156 // the stats info to GpuStats directly through async binder.
157 std::lock_guard<std::mutex> lock(mStatsLock);
158 if (mGpuStats.glDriverToSend) {
159 mGpuStats.glDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700160 sendGpuStatsLocked(GpuStatsInfo::Api::API_GL, true, mGpuStats.glDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700161 }
162 if (mGpuStats.vkDriverToSend) {
163 mGpuStats.vkDriverToSend = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700164 sendGpuStatsLocked(GpuStatsInfo::Api::API_VK, true, mGpuStats.vkDriverLoadingTime);
Yiwei Zhang3c74da92019-06-28 10:16:49 -0700165 }
166 });
167 trySendGpuStatsThread.detach();
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700168}
169
Greg Kaiser210bb7e2019-02-12 12:40:05 -0800170void GraphicsEnv::setGpuStats(const std::string& driverPackageName,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800171 const std::string& driverVersionName, uint64_t driverVersionCode,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700172 int64_t driverBuildTime, const std::string& appPackageName,
173 const int vulkanVersion) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800174 ATRACE_CALL();
175
Yiwei Zhangd9861812019-02-13 11:51:55 -0800176 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800177 ALOGV("setGpuStats:\n"
178 "\tdriverPackageName[%s]\n"
179 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800180 "\tdriverVersionCode[%" PRIu64 "]\n"
181 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700182 "\tappPackageName[%s]\n"
183 "\tvulkanVersion[%d]\n",
Yiwei Zhang96c01712019-02-19 16:00:25 -0800184 driverPackageName.c_str(), driverVersionName.c_str(), driverVersionCode, driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700185 appPackageName.c_str(), vulkanVersion);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800186
Yiwei Zhangd9861812019-02-13 11:51:55 -0800187 mGpuStats.driverPackageName = driverPackageName;
188 mGpuStats.driverVersionName = driverVersionName;
189 mGpuStats.driverVersionCode = driverVersionCode;
Yiwei Zhang96c01712019-02-19 16:00:25 -0800190 mGpuStats.driverBuildTime = driverBuildTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800191 mGpuStats.appPackageName = appPackageName;
Yiwei Zhang794d2952019-05-06 17:43:59 -0700192 mGpuStats.vulkanVersion = vulkanVersion;
Yiwei Zhang8c8c1812019-02-04 18:56:38 -0800193}
194
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700195void GraphicsEnv::setDriverToLoad(GpuStatsInfo::Driver driver) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800196 ATRACE_CALL();
197
198 std::lock_guard<std::mutex> lock(mStatsLock);
199 switch (driver) {
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700200 case GpuStatsInfo::Driver::GL:
201 case GpuStatsInfo::Driver::GL_UPDATED:
202 case GpuStatsInfo::Driver::ANGLE: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700203 if (mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::NONE ||
204 mGpuStats.glDriverToLoad == GpuStatsInfo::Driver::GL) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800205 mGpuStats.glDriverToLoad = driver;
206 break;
207 }
208
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700209 if (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800210 mGpuStats.glDriverFallback = driver;
211 }
212 break;
213 }
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700214 case GpuStatsInfo::Driver::VULKAN:
215 case GpuStatsInfo::Driver::VULKAN_UPDATED: {
Yiwei Zhang472cab02019-08-05 17:57:41 -0700216 if (mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::NONE ||
217 mGpuStats.vkDriverToLoad == GpuStatsInfo::Driver::VULKAN) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800218 mGpuStats.vkDriverToLoad = driver;
219 break;
220 }
221
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700222 if (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800223 mGpuStats.vkDriverFallback = driver;
224 }
225 break;
226 }
227 default:
228 break;
229 }
230}
231
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700232void GraphicsEnv::setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700233 int64_t driverLoadingTime) {
Yiwei Zhangd9861812019-02-13 11:51:55 -0800234 ATRACE_CALL();
235
236 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700237 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800238 mGpuStats.glDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700239 mGpuStats.glDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800240 } else {
Yiwei Zhangd3819382020-01-07 19:53:56 -0800241 mGpuStats.vkDriverToSend = true;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700242 mGpuStats.vkDriverLoadingTime = driverLoadingTime;
Yiwei Zhangd9861812019-02-13 11:51:55 -0800243 }
244
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700245 sendGpuStatsLocked(api, isDriverLoaded, driverLoadingTime);
Yiwei Zhangd9861812019-02-13 11:51:55 -0800246}
247
Yiwei Zhangd9861812019-02-13 11:51:55 -0800248static sp<IGpuService> getGpuService() {
Yiwei Zhang8e097302019-07-08 16:11:12 -0700249 static const sp<IBinder> binder = defaultServiceManager()->checkService(String16("gpu"));
Yiwei Zhangd9861812019-02-13 11:51:55 -0800250 if (!binder) {
251 ALOGE("Failed to get gpu service");
252 return nullptr;
253 }
254
255 return interface_cast<IGpuService>(binder);
256}
257
Yiwei Zhangd3819382020-01-07 19:53:56 -0800258bool GraphicsEnv::readyToSendGpuStatsLocked() {
259 // Only send stats for processes having at least one activity launched and that process doesn't
260 // skip the GraphicsEnvironment setup.
261 return mActivityLaunched && !mGpuStats.appPackageName.empty();
262}
263
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700264void GraphicsEnv::setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value) {
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700265 ATRACE_CALL();
266
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700267 std::lock_guard<std::mutex> lock(mStatsLock);
Yiwei Zhangd3819382020-01-07 19:53:56 -0800268 if (!readyToSendGpuStatsLocked()) return;
269
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700270 const sp<IGpuService> gpuService = getGpuService();
271 if (gpuService) {
Yiwei Zhangbcba4112019-07-03 13:39:32 -0700272 gpuService->setTargetStats(mGpuStats.appPackageName, mGpuStats.driverVersionCode, stats,
273 value);
Yiwei Zhang8c5e3bd2019-05-09 14:34:19 -0700274 }
275}
276
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700277void GraphicsEnv::sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded,
Yiwei Zhangd9861812019-02-13 11:51:55 -0800278 int64_t driverLoadingTime) {
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800279 ATRACE_CALL();
280
Yiwei Zhangd3819382020-01-07 19:53:56 -0800281 if (!readyToSendGpuStatsLocked()) return;
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800282
283 ALOGV("sendGpuStats:\n"
284 "\tdriverPackageName[%s]\n"
285 "\tdriverVersionName[%s]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800286 "\tdriverVersionCode[%" PRIu64 "]\n"
287 "\tdriverBuildTime[%" PRId64 "]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800288 "\tappPackageName[%s]\n"
Yiwei Zhang794d2952019-05-06 17:43:59 -0700289 "\tvulkanVersion[%d]\n"
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700290 "\tapi[%d]\n"
Yiwei Zhangd9861812019-02-13 11:51:55 -0800291 "\tisDriverLoaded[%d]\n"
Yiwei Zhang96c01712019-02-19 16:00:25 -0800292 "\tdriverLoadingTime[%" PRId64 "]",
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800293 mGpuStats.driverPackageName.c_str(), mGpuStats.driverVersionName.c_str(),
Yiwei Zhang96c01712019-02-19 16:00:25 -0800294 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime, mGpuStats.appPackageName.c_str(),
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700295 mGpuStats.vulkanVersion, static_cast<int32_t>(api), isDriverLoaded, driverLoadingTime);
296
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700297 GpuStatsInfo::Driver driver = GpuStatsInfo::Driver::NONE;
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700298 bool isIntendedDriverLoaded = false;
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700299 if (api == GpuStatsInfo::Api::API_GL) {
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700300 driver = mGpuStats.glDriverToLoad;
301 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700302 isDriverLoaded && (mGpuStats.glDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700303 } else {
304 driver = mGpuStats.vkDriverToLoad;
305 isIntendedDriverLoaded =
Yiwei Zhang27ab3ac2019-07-02 18:10:55 -0700306 isDriverLoaded && (mGpuStats.vkDriverFallback == GpuStatsInfo::Driver::NONE);
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700307 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800308
Yiwei Zhangd9861812019-02-13 11:51:55 -0800309 const sp<IGpuService> gpuService = getGpuService();
310 if (gpuService) {
311 gpuService->setGpuStats(mGpuStats.driverPackageName, mGpuStats.driverVersionName,
Yiwei Zhang96c01712019-02-19 16:00:25 -0800312 mGpuStats.driverVersionCode, mGpuStats.driverBuildTime,
Yiwei Zhang794d2952019-05-06 17:43:59 -0700313 mGpuStats.appPackageName, mGpuStats.vulkanVersion, driver,
Yiwei Zhang5c640c12019-05-08 18:29:38 -0700314 isIntendedDriverLoaded, driverLoadingTime);
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800315 }
Yiwei Zhangcb9d4e42019-02-06 20:22:59 -0800316}
317
Adam Bodnar0afcca02019-09-17 13:23:17 -0700318bool GraphicsEnv::setInjectLayersPrSetDumpable() {
319 if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) == -1) {
320 return false;
321 }
322 return true;
323}
324
Tim Van Patten5f744f12018-12-12 11:46:21 -0700325void* GraphicsEnv::loadLibrary(std::string name) {
326 const android_dlextinfo dlextinfo = {
327 .flags = ANDROID_DLEXT_USE_NAMESPACE,
328 .library_namespace = getAngleNamespace(),
329 };
330
331 std::string libName = std::string("lib") + name + "_angle.so";
332
333 void* so = android_dlopen_ext(libName.c_str(), RTLD_LOCAL | RTLD_NOW, &dlextinfo);
334
335 if (so) {
336 ALOGD("dlopen_ext from APK (%s) success at %p", libName.c_str(), so);
337 return so;
338 } else {
339 ALOGE("dlopen_ext(\"%s\") failed: %s", libName.c_str(), dlerror());
340 }
341
342 return nullptr;
343}
344
345bool GraphicsEnv::checkAngleRules(void* so) {
346 char manufacturer[PROPERTY_VALUE_MAX];
347 char model[PROPERTY_VALUE_MAX];
348 property_get("ro.product.manufacturer", manufacturer, "UNSET");
349 property_get("ro.product.model", model, "UNSET");
350
351 auto ANGLEGetFeatureSupportUtilAPIVersion =
352 (fpANGLEGetFeatureSupportUtilAPIVersion)dlsym(so,
353 "ANGLEGetFeatureSupportUtilAPIVersion");
354
355 if (!ANGLEGetFeatureSupportUtilAPIVersion) {
356 ALOGW("Cannot find ANGLEGetFeatureSupportUtilAPIVersion function");
357 return false;
358 }
359
360 // Negotiate the interface version by requesting most recent known to the platform
361 unsigned int versionToUse = CURRENT_ANGLE_API_VERSION;
362 if (!(ANGLEGetFeatureSupportUtilAPIVersion)(&versionToUse)) {
363 ALOGW("Cannot use ANGLE feature-support library, it is older than supported by EGL, "
364 "requested version %u",
365 versionToUse);
366 return false;
367 }
368
369 // Add and remove versions below as needed
370 bool useAngle = false;
371 switch (versionToUse) {
372 case 2: {
373 ALOGV("Using version %d of ANGLE feature-support library", versionToUse);
374 void* rulesHandle = nullptr;
375 int rulesVersion = 0;
376 void* systemInfoHandle = nullptr;
377
378 // Get the symbols for the feature-support-utility library:
379#define GET_SYMBOL(symbol) \
380 fp##symbol symbol = (fp##symbol)dlsym(so, #symbol); \
381 if (!symbol) { \
382 ALOGW("Cannot find " #symbol " in ANGLE feature-support library"); \
383 break; \
384 }
385 GET_SYMBOL(ANGLEAndroidParseRulesString);
386 GET_SYMBOL(ANGLEGetSystemInfo);
387 GET_SYMBOL(ANGLEAddDeviceInfoToSystemInfo);
388 GET_SYMBOL(ANGLEShouldBeUsedForApplication);
389 GET_SYMBOL(ANGLEFreeRulesHandle);
390 GET_SYMBOL(ANGLEFreeSystemInfoHandle);
391
392 // Parse the rules, obtain the SystemInfo, and evaluate the
393 // application against the rules:
394 if (!(ANGLEAndroidParseRulesString)(mRulesBuffer.data(), &rulesHandle, &rulesVersion)) {
395 ALOGW("ANGLE feature-support library cannot parse rules file");
396 break;
397 }
398 if (!(ANGLEGetSystemInfo)(&systemInfoHandle)) {
399 ALOGW("ANGLE feature-support library cannot obtain SystemInfo");
400 break;
401 }
402 if (!(ANGLEAddDeviceInfoToSystemInfo)(manufacturer, model, systemInfoHandle)) {
403 ALOGW("ANGLE feature-support library cannot add device info to SystemInfo");
404 break;
405 }
406 useAngle = (ANGLEShouldBeUsedForApplication)(rulesHandle, rulesVersion,
407 systemInfoHandle, mAngleAppName.c_str());
408 (ANGLEFreeRulesHandle)(rulesHandle);
409 (ANGLEFreeSystemInfoHandle)(systemInfoHandle);
410 } break;
411
412 default:
413 ALOGW("Version %u of ANGLE feature-support library is NOT supported.", versionToUse);
414 }
415
416 ALOGV("Close temporarily-loaded ANGLE opt-in/out logic");
417 return useAngle;
418}
419
420bool GraphicsEnv::shouldUseAngle(std::string appName) {
421 if (appName != mAngleAppName) {
422 // Make sure we are checking the app we were init'ed for
423 ALOGE("App name does not match: expected '%s', got '%s'", mAngleAppName.c_str(),
424 appName.c_str());
425 return false;
426 }
427
428 return shouldUseAngle();
429}
430
431bool GraphicsEnv::shouldUseAngle() {
432 // Make sure we are init'ed
433 if (mAngleAppName.empty()) {
Cody Northrop2d7af742019-01-24 16:55:03 -0700434 ALOGV("App name is empty. setAngleInfo() has not been called to enable ANGLE.");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700435 return false;
436 }
437
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700438 return (mUseAngle == YES) ? true : false;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700439}
440
441void GraphicsEnv::updateUseAngle() {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700442 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700443
444 const char* ANGLE_PREFER_ANGLE = "angle";
445 const char* ANGLE_PREFER_NATIVE = "native";
446
447 if (mAngleDeveloperOptIn == ANGLE_PREFER_ANGLE) {
448 ALOGV("User set \"Developer Options\" to force the use of ANGLE");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700449 mUseAngle = YES;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700450 } else if (mAngleDeveloperOptIn == ANGLE_PREFER_NATIVE) {
451 ALOGV("User set \"Developer Options\" to force the use of Native");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700452 mUseAngle = NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700453 } else {
454 // The "Developer Options" value wasn't set to force the use of ANGLE. Need to temporarily
455 // load ANGLE and call the updatable opt-in/out logic:
Cody Northropc15d3822019-01-17 10:26:47 -0700456 void* featureSo = loadLibrary("feature_support");
Tim Van Patten5f744f12018-12-12 11:46:21 -0700457 if (featureSo) {
458 ALOGV("loaded ANGLE's opt-in/out logic from namespace");
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700459 mUseAngle = checkAngleRules(featureSo) ? YES : NO;
Tim Van Patten5f744f12018-12-12 11:46:21 -0700460 dlclose(featureSo);
461 featureSo = nullptr;
462 } else {
463 ALOGV("Could not load the ANGLE opt-in/out logic, cannot use ANGLE.");
464 }
465 }
466}
467
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600468void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700469 const std::string developerOptIn, const int rulesFd,
470 const long rulesOffset, const long rulesLength) {
Tim Van Patten8bd24e92019-02-08 10:16:40 -0700471 if (mUseAngle != UNKNOWN) {
472 // We've already figured out an answer for this app, so just return.
473 ALOGV("Already evaluated the rules file for '%s': use ANGLE = %s", appName.c_str(),
474 (mUseAngle == YES) ? "true" : "false");
475 return;
476 }
477
Tim Van Patten5f744f12018-12-12 11:46:21 -0700478 ALOGV("setting ANGLE path to '%s'", path.c_str());
479 mAnglePath = path;
480 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
481 mAngleAppName = appName;
482 ALOGV("setting ANGLE application opt-in to '%s'", developerOptIn.c_str());
483 mAngleDeveloperOptIn = developerOptIn;
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600484
Tim Van Patten5f744f12018-12-12 11:46:21 -0700485 lseek(rulesFd, rulesOffset, SEEK_SET);
486 mRulesBuffer = std::vector<char>(rulesLength + 1);
487 ssize_t numBytesRead = read(rulesFd, mRulesBuffer.data(), rulesLength);
488 if (numBytesRead < 0) {
489 ALOGE("Cannot read rules file: numBytesRead = %zd", numBytesRead);
490 numBytesRead = 0;
491 } else if (numBytesRead == 0) {
492 ALOGW("Empty rules file");
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600493 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700494 if (numBytesRead != rulesLength) {
495 ALOGW("Did not read all of the necessary bytes from the rules file."
496 "expected: %ld, got: %zd",
497 rulesLength, numBytesRead);
Tim Van Pattena2a60a02018-11-09 16:51:15 -0700498 }
Tim Van Patten5f744f12018-12-12 11:46:21 -0700499 mRulesBuffer[numBytesRead] = '\0';
Cody Northrop04e70432018-09-06 10:34:58 -0600500
Tim Van Patten5f744f12018-12-12 11:46:21 -0700501 // Update the current status of whether we should use ANGLE or not
502 updateUseAngle();
Cody Northrop1f00e172018-04-02 11:23:31 -0600503}
504
Victor Khimenko4819b522018-07-13 17:24:18 +0200505void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600506 if (mLayerPaths.empty()) {
507 mLayerPaths = layerPaths;
508 mAppNamespace = appNamespace;
509 } else {
510 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
Yiwei Zhang64d89212018-11-27 19:58:29 -0800511 layerPaths.c_str(), appNamespace);
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600512 }
513}
514
Victor Khimenko4819b522018-07-13 17:24:18 +0200515NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600516 return mAppNamespace;
517}
518
Tim Van Patten5f744f12018-12-12 11:46:21 -0700519std::string& GraphicsEnv::getAngleAppName() {
520 return mAngleAppName;
Cody Northrop04e70432018-09-06 10:34:58 -0600521}
522
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600523const std::string& GraphicsEnv::getLayerPaths() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600524 return mLayerPaths;
525}
526
Courtney Goeltzenleuchter30ad2ab2018-10-30 08:20:44 -0600527const std::string& GraphicsEnv::getDebugLayers() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600528 return mDebugLayers;
529}
530
Cody Northropb9b01b62018-10-23 13:13:10 -0600531const std::string& GraphicsEnv::getDebugLayersGLES() {
532 return mDebugLayersGLES;
533}
534
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600535void GraphicsEnv::setDebugLayers(const std::string layers) {
536 mDebugLayers = layers;
537}
538
Cody Northropb9b01b62018-10-23 13:13:10 -0600539void GraphicsEnv::setDebugLayersGLES(const std::string layers) {
540 mDebugLayersGLES = layers;
541}
542
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700543// Return true if all the required libraries from vndk and sphal namespace are
544// linked to the Game Driver namespace correctly.
545bool GraphicsEnv::linkDriverNamespaceLocked(android_namespace_t* vndkNamespace) {
546 const std::string llndkLibraries = getSystemNativeLibraries(NativeLibrary::LLNDK);
547 if (llndkLibraries.empty()) {
548 return false;
549 }
550 if (!android_link_namespaces(mDriverNamespace, nullptr, llndkLibraries.c_str())) {
551 ALOGE("Failed to link default namespace[%s]", dlerror());
552 return false;
553 }
554
555 const std::string vndkspLibraries = getSystemNativeLibraries(NativeLibrary::VNDKSP);
556 if (vndkspLibraries.empty()) {
557 return false;
558 }
559 if (!android_link_namespaces(mDriverNamespace, vndkNamespace, vndkspLibraries.c_str())) {
560 ALOGE("Failed to link vndk namespace[%s]", dlerror());
561 return false;
562 }
563
564 if (mSphalLibraries.empty()) {
565 return true;
566 }
567
568 // Make additional libraries in sphal to be accessible
569 auto sphalNamespace = android_get_exported_namespace("sphal");
570 if (!sphalNamespace) {
571 ALOGE("Depend on these libraries[%s] in sphal, but failed to get sphal namespace",
572 mSphalLibraries.c_str());
573 return false;
574 }
575
576 if (!android_link_namespaces(mDriverNamespace, sphalNamespace, mSphalLibraries.c_str())) {
577 ALOGE("Failed to link sphal namespace[%s]", dlerror());
578 return false;
579 }
580
581 return true;
582}
583
Jesse Hall53457db2016-12-14 16:54:06 -0800584android_namespace_t* GraphicsEnv::getDriverNamespace() {
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700585 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Yiwei Zhang64d89212018-11-27 19:58:29 -0800586
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700587 if (mDriverNamespace) {
588 return mDriverNamespace;
589 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800590
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700591 if (mDriverPath.empty()) {
592 return nullptr;
593 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800594
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700595 auto vndkNamespace = android_get_exported_namespace("vndk");
596 if (!vndkNamespace) {
597 return nullptr;
598 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800599
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700600 mDriverNamespace = android_create_namespace("gfx driver",
601 mDriverPath.c_str(), // ld_library_path
602 mDriverPath.c_str(), // default_library_path
603 ANDROID_NAMESPACE_TYPE_ISOLATED,
604 nullptr, // permitted_when_isolated_path
605 nullptr);
Yiwei Zhang6ef84942019-02-14 12:28:12 -0800606
Yiwei Zhang5e21eb32019-06-05 00:26:03 -0700607 if (!linkDriverNamespaceLocked(vndkNamespace)) {
608 mDriverNamespace = nullptr;
609 }
Yiwei Zhang64d89212018-11-27 19:58:29 -0800610
Jesse Hall53457db2016-12-14 16:54:06 -0800611 return mDriverNamespace;
612}
613
Cody Northrop1f00e172018-04-02 11:23:31 -0600614android_namespace_t* GraphicsEnv::getAngleNamespace() {
Cody Northrop3892cfa2019-01-30 10:03:12 -0700615 std::lock_guard<std::mutex> lock(mNamespaceMutex);
Cody Northrop1f00e172018-04-02 11:23:31 -0600616
Cody Northrop3892cfa2019-01-30 10:03:12 -0700617 if (mAngleNamespace) {
618 return mAngleNamespace;
619 }
620
621 if (mAnglePath.empty()) {
622 ALOGV("mAnglePath is empty, not creating ANGLE namespace");
623 return nullptr;
624 }
625
626 mAngleNamespace = android_create_namespace("ANGLE",
627 nullptr, // ld_library_path
628 mAnglePath.c_str(), // default_library_path
629 ANDROID_NAMESPACE_TYPE_SHARED |
630 ANDROID_NAMESPACE_TYPE_ISOLATED,
631 nullptr, // permitted_when_isolated_path
632 nullptr);
633
634 ALOGD_IF(!mAngleNamespace, "Could not create ANGLE namespace from default");
Cody Northrop1f00e172018-04-02 11:23:31 -0600635
636 return mAngleNamespace;
637}
638
Jesse Hall90b25ed2016-12-12 12:56:46 -0800639} // namespace android