blob: 3328ad729146a534e2bc975414df508bbdc5cc03 [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
17//#define LOG_NDEBUG 1
18#define LOG_TAG "GraphicsEnv"
Jiyong Park27c39e12017-05-08 13:00:02 +090019#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080020
Cody Northrop629ce4e2018-10-15 07:22:09 -060021#include <sys/prctl.h>
22
Jesse Hall53457db2016-12-14 16:54:06 -080023#include <mutex>
24
Jiyong Park9b816a82018-01-02 17:37:37 +090025#include <android/dlext.h>
Cody Northrop629ce4e2018-10-15 07:22:09 -060026#include <cutils/properties.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080027#include <log/log.h>
28
Jesse Hall57de0ff2017-05-05 16:41:35 -070029// TODO(b/37049319) Get this from a header once one exists
30extern "C" {
31 android_namespace_t* android_get_exported_namespace(const char*);
Jiyong Park9b816a82018-01-02 17:37:37 +090032 android_namespace_t* android_create_namespace(const char* name,
33 const char* ld_library_path,
34 const char* default_library_path,
35 uint64_t type,
36 const char* permitted_when_isolated_path,
37 android_namespace_t* parent);
38
39 enum {
40 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
41 ANDROID_NAMESPACE_TYPE_SHARED = 2,
42 };
Jesse Hall57de0ff2017-05-05 16:41:35 -070043}
44
Jesse Hall90b25ed2016-12-12 12:56:46 -080045namespace android {
46
47/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
48 static GraphicsEnv env;
49 return env;
50}
51
Cody Northrop629ce4e2018-10-15 07:22:09 -060052int GraphicsEnv::getCanLoadSystemLibraries() {
53 if (property_get_bool("ro.debuggable", false) && prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)) {
54 // Return an integer value since this crosses library boundaries
55 return 1;
56 }
57 return 0;
58}
59
Jesse Hall90b25ed2016-12-12 12:56:46 -080060void GraphicsEnv::setDriverPath(const std::string path) {
61 if (!mDriverPath.empty()) {
62 ALOGV("ignoring attempt to change driver path from '%s' to '%s'",
63 mDriverPath.c_str(), path.c_str());
64 return;
65 }
66 ALOGV("setting driver path to '%s'", path.c_str());
67 mDriverPath = path;
68}
69
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060070void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Cody Northrop49d51992018-08-29 16:37:09 -060071 const std::string appPref, bool developerOptIn) {
Cody Northrop1f00e172018-04-02 11:23:31 -060072 if (!mAnglePath.empty()) {
73 ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(),
74 path.c_str());
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060075 } else {
76 ALOGV("setting ANGLE path to '%s'", path.c_str());
77 mAnglePath = path;
Cody Northrop1f00e172018-04-02 11:23:31 -060078 }
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060079
80 if (!mAngleAppName.empty()) {
81 ALOGV("ignoring attempt to change ANGLE app name from '%s' to '%s'", mAngleAppName.c_str(),
82 appName.c_str());
83 } else {
84 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
85 mAngleAppName = appName;
86 }
87
Cody Northrop49d51992018-08-29 16:37:09 -060088 if (!mAngleAppPref.empty()) {
89 ALOGV("ignoring attempt to change ANGLE application opt-in from '%s' to '%s'",
90 mAngleAppPref.c_str(), appPref.c_str());
91 } else {
92 ALOGV("setting ANGLE application opt-in to '%s'", appPref.c_str());
93 mAngleAppPref = appPref;
94 }
95
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060096 mAngleDeveloperOptIn = developerOptIn;
Cody Northrop1f00e172018-04-02 11:23:31 -060097}
98
Victor Khimenko4819b522018-07-13 17:24:18 +020099void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600100 if (mLayerPaths.empty()) {
101 mLayerPaths = layerPaths;
102 mAppNamespace = appNamespace;
103 } else {
104 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
105 layerPaths.c_str(), appNamespace);
106 }
107}
108
Victor Khimenko4819b522018-07-13 17:24:18 +0200109NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600110 return mAppNamespace;
111}
112
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600113const char* GraphicsEnv::getAngleAppName() {
114 if (mAngleAppName.empty()) return nullptr;
115 return mAngleAppName.c_str();
116}
117
118bool GraphicsEnv::getAngleDeveloperOptIn() {
119 return mAngleDeveloperOptIn;
120}
121
Cody Northrop49d51992018-08-29 16:37:09 -0600122const char* GraphicsEnv::getAngleAppPref() {
123 if (mAngleAppPref.empty()) return nullptr;
124 return mAngleAppPref.c_str();
125}
126
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600127const std::string GraphicsEnv::getLayerPaths(){
128 return mLayerPaths;
129}
130
131const std::string GraphicsEnv::getDebugLayers() {
132 return mDebugLayers;
133}
134
135void GraphicsEnv::setDebugLayers(const std::string layers) {
136 mDebugLayers = layers;
137}
138
Jesse Hall53457db2016-12-14 16:54:06 -0800139android_namespace_t* GraphicsEnv::getDriverNamespace() {
140 static std::once_flag once;
141 std::call_once(once, [this]() {
Jesse Hall53457db2016-12-14 16:54:06 -0800142 if (mDriverPath.empty())
143 return;
Jesse Hall57de0ff2017-05-05 16:41:35 -0700144 // If the sphal namespace isn't configured for a device, don't support updatable drivers.
145 // We need a parent namespace to inherit the default search path from.
146 auto sphalNamespace = android_get_exported_namespace("sphal");
147 if (!sphalNamespace) return;
148 mDriverNamespace = android_create_namespace("gfx driver",
149 nullptr, // ld_library_path
150 mDriverPath.c_str(), // default_library_path
151 ANDROID_NAMESPACE_TYPE_SHARED |
152 ANDROID_NAMESPACE_TYPE_ISOLATED,
153 nullptr, // permitted_when_isolated_path
154 sphalNamespace);
Jesse Hall53457db2016-12-14 16:54:06 -0800155 });
156 return mDriverNamespace;
157}
158
Cody Northrop1f00e172018-04-02 11:23:31 -0600159android_namespace_t* GraphicsEnv::getAngleNamespace() {
160 static std::once_flag once;
161 std::call_once(once, [this]() {
162 if (mAnglePath.empty()) return;
163
164 mAngleNamespace = android_create_namespace("ANGLE",
165 nullptr, // ld_library_path
166 mAnglePath.c_str(), // default_library_path
167 ANDROID_NAMESPACE_TYPE_SHARED |
168 ANDROID_NAMESPACE_TYPE_ISOLATED,
169 nullptr, // permitted_when_isolated_path
170 nullptr);
171 if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default");
172 });
173
174 return mAngleNamespace;
175}
176
Jesse Hall90b25ed2016-12-12 12:56:46 -0800177} // namespace android
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800178
Cody Northrop1f00e172018-04-02 11:23:31 -0600179extern "C" {
180android_namespace_t* android_getDriverNamespace() {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800181 return android::GraphicsEnv::getInstance().getDriverNamespace();
182}
Cody Northrop1f00e172018-04-02 11:23:31 -0600183android_namespace_t* android_getAngleNamespace() {
184 return android::GraphicsEnv::getInstance().getAngleNamespace();
185}
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600186const char* android_getAngleAppName() {
187 return android::GraphicsEnv::getInstance().getAngleAppName();
188}
189bool android_getAngleDeveloperOptIn() {
190 return android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
191}
Cody Northrop49d51992018-08-29 16:37:09 -0600192const char* android_getAngleAppPref() {
193 return android::GraphicsEnv::getInstance().getAngleAppPref();
194}
Cody Northrop629ce4e2018-10-15 07:22:09 -0600195const char* android_getLayerPaths() {
196 return android::GraphicsEnv::getInstance().getLayerPaths().c_str();
197}
198const char* android_getDebugLayers() {
199 return android::GraphicsEnv::getInstance().getDebugLayers().c_str();
200}
Cody Northrop1f00e172018-04-02 11:23:31 -0600201}