blob: 2a7d76e856717df53b79eabb73c22a6d2c14b48d [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
Jesse Hall53457db2016-12-14 16:54:06 -080021#include <mutex>
22
Jiyong Park9b816a82018-01-02 17:37:37 +090023#include <android/dlext.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080024#include <log/log.h>
25
Jesse Hall57de0ff2017-05-05 16:41:35 -070026// TODO(b/37049319) Get this from a header once one exists
27extern "C" {
28 android_namespace_t* android_get_exported_namespace(const char*);
Jiyong Park9b816a82018-01-02 17:37:37 +090029 android_namespace_t* android_create_namespace(const char* name,
30 const char* ld_library_path,
31 const char* default_library_path,
32 uint64_t type,
33 const char* permitted_when_isolated_path,
34 android_namespace_t* parent);
35
36 enum {
37 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
38 ANDROID_NAMESPACE_TYPE_SHARED = 2,
39 };
Jesse Hall57de0ff2017-05-05 16:41:35 -070040}
41
Jesse Hall90b25ed2016-12-12 12:56:46 -080042namespace android {
43
44/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
45 static GraphicsEnv env;
46 return env;
47}
48
49void GraphicsEnv::setDriverPath(const std::string path) {
50 if (!mDriverPath.empty()) {
51 ALOGV("ignoring attempt to change driver path from '%s' to '%s'",
52 mDriverPath.c_str(), path.c_str());
53 return;
54 }
55 ALOGV("setting driver path to '%s'", path.c_str());
56 mDriverPath = path;
57}
58
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060059void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName,
Cody Northrop49d51992018-08-29 16:37:09 -060060 const std::string appPref, bool developerOptIn) {
Cody Northrop1f00e172018-04-02 11:23:31 -060061 if (!mAnglePath.empty()) {
62 ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(),
63 path.c_str());
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060064 } else {
65 ALOGV("setting ANGLE path to '%s'", path.c_str());
66 mAnglePath = path;
Cody Northrop1f00e172018-04-02 11:23:31 -060067 }
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060068
69 if (!mAngleAppName.empty()) {
70 ALOGV("ignoring attempt to change ANGLE app name from '%s' to '%s'", mAngleAppName.c_str(),
71 appName.c_str());
72 } else {
73 ALOGV("setting ANGLE app name to '%s'", appName.c_str());
74 mAngleAppName = appName;
75 }
76
Cody Northrop49d51992018-08-29 16:37:09 -060077 if (!mAngleAppPref.empty()) {
78 ALOGV("ignoring attempt to change ANGLE application opt-in from '%s' to '%s'",
79 mAngleAppPref.c_str(), appPref.c_str());
80 } else {
81 ALOGV("setting ANGLE application opt-in to '%s'", appPref.c_str());
82 mAngleAppPref = appPref;
83 }
84
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -060085 mAngleDeveloperOptIn = developerOptIn;
Cody Northrop1f00e172018-04-02 11:23:31 -060086}
87
Victor Khimenko4819b522018-07-13 17:24:18 +020088void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -060089 if (mLayerPaths.empty()) {
90 mLayerPaths = layerPaths;
91 mAppNamespace = appNamespace;
92 } else {
93 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
94 layerPaths.c_str(), appNamespace);
95 }
96}
97
Victor Khimenko4819b522018-07-13 17:24:18 +020098NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -060099 return mAppNamespace;
100}
101
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600102const char* GraphicsEnv::getAngleAppName() {
103 if (mAngleAppName.empty()) return nullptr;
104 return mAngleAppName.c_str();
105}
106
107bool GraphicsEnv::getAngleDeveloperOptIn() {
108 return mAngleDeveloperOptIn;
109}
110
Cody Northrop49d51992018-08-29 16:37:09 -0600111const char* GraphicsEnv::getAngleAppPref() {
112 if (mAngleAppPref.empty()) return nullptr;
113 return mAngleAppPref.c_str();
114}
115
Cody Northropd2aa3ab2017-10-20 09:01:53 -0600116const std::string GraphicsEnv::getLayerPaths(){
117 return mLayerPaths;
118}
119
120const std::string GraphicsEnv::getDebugLayers() {
121 return mDebugLayers;
122}
123
124void GraphicsEnv::setDebugLayers(const std::string layers) {
125 mDebugLayers = layers;
126}
127
Jesse Hall53457db2016-12-14 16:54:06 -0800128android_namespace_t* GraphicsEnv::getDriverNamespace() {
129 static std::once_flag once;
130 std::call_once(once, [this]() {
Jesse Hall53457db2016-12-14 16:54:06 -0800131 if (mDriverPath.empty())
132 return;
Jesse Hall57de0ff2017-05-05 16:41:35 -0700133 // If the sphal namespace isn't configured for a device, don't support updatable drivers.
134 // We need a parent namespace to inherit the default search path from.
135 auto sphalNamespace = android_get_exported_namespace("sphal");
136 if (!sphalNamespace) return;
137 mDriverNamespace = android_create_namespace("gfx driver",
138 nullptr, // ld_library_path
139 mDriverPath.c_str(), // default_library_path
140 ANDROID_NAMESPACE_TYPE_SHARED |
141 ANDROID_NAMESPACE_TYPE_ISOLATED,
142 nullptr, // permitted_when_isolated_path
143 sphalNamespace);
Jesse Hall53457db2016-12-14 16:54:06 -0800144 });
145 return mDriverNamespace;
146}
147
Cody Northrop1f00e172018-04-02 11:23:31 -0600148android_namespace_t* GraphicsEnv::getAngleNamespace() {
149 static std::once_flag once;
150 std::call_once(once, [this]() {
151 if (mAnglePath.empty()) return;
152
153 mAngleNamespace = android_create_namespace("ANGLE",
154 nullptr, // ld_library_path
155 mAnglePath.c_str(), // default_library_path
156 ANDROID_NAMESPACE_TYPE_SHARED |
157 ANDROID_NAMESPACE_TYPE_ISOLATED,
158 nullptr, // permitted_when_isolated_path
159 nullptr);
160 if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default");
161 });
162
163 return mAngleNamespace;
164}
165
Jesse Hall90b25ed2016-12-12 12:56:46 -0800166} // namespace android
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800167
Cody Northrop1f00e172018-04-02 11:23:31 -0600168extern "C" {
169android_namespace_t* android_getDriverNamespace() {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800170 return android::GraphicsEnv::getInstance().getDriverNamespace();
171}
Cody Northrop1f00e172018-04-02 11:23:31 -0600172android_namespace_t* android_getAngleNamespace() {
173 return android::GraphicsEnv::getInstance().getAngleNamespace();
174}
Courtney Goeltzenleuchterd41ef252018-09-26 14:37:42 -0600175const char* android_getAngleAppName() {
176 return android::GraphicsEnv::getInstance().getAngleAppName();
177}
178bool android_getAngleDeveloperOptIn() {
179 return android::GraphicsEnv::getInstance().getAngleDeveloperOptIn();
180}
Cody Northrop49d51992018-08-29 16:37:09 -0600181const char* android_getAngleAppPref() {
182 return android::GraphicsEnv::getInstance().getAngleAppPref();
183}
Cody Northrop1f00e172018-04-02 11:23:31 -0600184}