Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 1 | /* |
| 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 Park | 27c39e1 | 2017-05-08 13:00:02 +0900 | [diff] [blame] | 19 | #include <graphicsenv/GraphicsEnv.h> |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 20 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 21 | #include <mutex> |
| 22 | |
Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 23 | #include <android/dlext.h> |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 24 | #include <log/log.h> |
| 25 | |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 26 | // TODO(b/37049319) Get this from a header once one exists |
| 27 | extern "C" { |
| 28 | android_namespace_t* android_get_exported_namespace(const char*); |
Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 29 | 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 Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 42 | namespace android { |
| 43 | |
| 44 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { |
| 45 | static GraphicsEnv env; |
| 46 | return env; |
| 47 | } |
| 48 | |
| 49 | void 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 Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame^] | 59 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, |
| 60 | bool developerOptIn) { |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 61 | if (!mAnglePath.empty()) { |
| 62 | ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(), |
| 63 | path.c_str()); |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame^] | 64 | } else { |
| 65 | ALOGV("setting ANGLE path to '%s'", path.c_str()); |
| 66 | mAnglePath = path; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 67 | } |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame^] | 68 | |
| 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 | |
| 77 | mAngleDeveloperOptIn = developerOptIn; |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 80 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 81 | if (mLayerPaths.empty()) { |
| 82 | mLayerPaths = layerPaths; |
| 83 | mAppNamespace = appNamespace; |
| 84 | } else { |
| 85 | ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", |
| 86 | layerPaths.c_str(), appNamespace); |
| 87 | } |
| 88 | } |
| 89 | |
Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 90 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 91 | return mAppNamespace; |
| 92 | } |
| 93 | |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame^] | 94 | const char* GraphicsEnv::getAngleAppName() { |
| 95 | if (mAngleAppName.empty()) return nullptr; |
| 96 | return mAngleAppName.c_str(); |
| 97 | } |
| 98 | |
| 99 | bool GraphicsEnv::getAngleDeveloperOptIn() { |
| 100 | return mAngleDeveloperOptIn; |
| 101 | } |
| 102 | |
Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 103 | const std::string GraphicsEnv::getLayerPaths(){ |
| 104 | return mLayerPaths; |
| 105 | } |
| 106 | |
| 107 | const std::string GraphicsEnv::getDebugLayers() { |
| 108 | return mDebugLayers; |
| 109 | } |
| 110 | |
| 111 | void GraphicsEnv::setDebugLayers(const std::string layers) { |
| 112 | mDebugLayers = layers; |
| 113 | } |
| 114 | |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 115 | android_namespace_t* GraphicsEnv::getDriverNamespace() { |
| 116 | static std::once_flag once; |
| 117 | std::call_once(once, [this]() { |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 118 | if (mDriverPath.empty()) |
| 119 | return; |
Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 120 | // If the sphal namespace isn't configured for a device, don't support updatable drivers. |
| 121 | // We need a parent namespace to inherit the default search path from. |
| 122 | auto sphalNamespace = android_get_exported_namespace("sphal"); |
| 123 | if (!sphalNamespace) return; |
| 124 | mDriverNamespace = android_create_namespace("gfx driver", |
| 125 | nullptr, // ld_library_path |
| 126 | mDriverPath.c_str(), // default_library_path |
| 127 | ANDROID_NAMESPACE_TYPE_SHARED | |
| 128 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 129 | nullptr, // permitted_when_isolated_path |
| 130 | sphalNamespace); |
Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 131 | }); |
| 132 | return mDriverNamespace; |
| 133 | } |
| 134 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 135 | android_namespace_t* GraphicsEnv::getAngleNamespace() { |
| 136 | static std::once_flag once; |
| 137 | std::call_once(once, [this]() { |
| 138 | if (mAnglePath.empty()) return; |
| 139 | |
| 140 | mAngleNamespace = android_create_namespace("ANGLE", |
| 141 | nullptr, // ld_library_path |
| 142 | mAnglePath.c_str(), // default_library_path |
| 143 | ANDROID_NAMESPACE_TYPE_SHARED | |
| 144 | ANDROID_NAMESPACE_TYPE_ISOLATED, |
| 145 | nullptr, // permitted_when_isolated_path |
| 146 | nullptr); |
| 147 | if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default"); |
| 148 | }); |
| 149 | |
| 150 | return mAngleNamespace; |
| 151 | } |
| 152 | |
Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 153 | } // namespace android |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 154 | |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 155 | extern "C" { |
| 156 | android_namespace_t* android_getDriverNamespace() { |
Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 157 | return android::GraphicsEnv::getInstance().getDriverNamespace(); |
| 158 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 159 | android_namespace_t* android_getAngleNamespace() { |
| 160 | return android::GraphicsEnv::getInstance().getAngleNamespace(); |
| 161 | } |
Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame^] | 162 | const char* android_getAngleAppName() { |
| 163 | return android::GraphicsEnv::getInstance().getAngleAppName(); |
| 164 | } |
| 165 | bool android_getAngleDeveloperOptIn() { |
| 166 | return android::GraphicsEnv::getInstance().getAngleDeveloperOptIn(); |
| 167 | } |
Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 168 | } |