| 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 |  | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 21 | #include <sys/prctl.h> | 
 | 22 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 23 | #include <mutex> | 
 | 24 |  | 
| Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 25 | #include <android/dlext.h> | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 26 | #include <cutils/properties.h> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 27 | #include <log/log.h> | 
 | 28 |  | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 29 | // TODO(b/37049319) Get this from a header once one exists | 
 | 30 | extern "C" { | 
 | 31 |   android_namespace_t* android_get_exported_namespace(const char*); | 
| Jiyong Park | 9b816a8 | 2018-01-02 17:37:37 +0900 | [diff] [blame] | 32 |   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 Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 43 | } | 
 | 44 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 45 | namespace android { | 
 | 46 |  | 
 | 47 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { | 
 | 48 |     static GraphicsEnv env; | 
 | 49 |     return env; | 
 | 50 | } | 
 | 51 |  | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 52 | int 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 Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 60 | void 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 Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 70 | void GraphicsEnv::setAngleInfo(const std::string path, const std::string appName, | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 71 |                                const std::string appPref, bool developerOptIn, | 
 | 72 |                                const int rulesFd, const long rulesOffset, | 
 | 73 |                                const long rulesLength) { | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 74 |     if (!mAnglePath.empty()) { | 
 | 75 |         ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(), | 
 | 76 |               path.c_str()); | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 77 |     } else { | 
 | 78 |         ALOGV("setting ANGLE path to '%s'", path.c_str()); | 
 | 79 |         mAnglePath = path; | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 80 |     } | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 81 |  | 
 | 82 |     if (!mAngleAppName.empty()) { | 
 | 83 |         ALOGV("ignoring attempt to change ANGLE app name from '%s' to '%s'", mAngleAppName.c_str(), | 
 | 84 |               appName.c_str()); | 
 | 85 |     } else { | 
 | 86 |         ALOGV("setting ANGLE app name to '%s'", appName.c_str()); | 
 | 87 |         mAngleAppName = appName; | 
 | 88 |     } | 
 | 89 |  | 
| Cody Northrop | 49d5199 | 2018-08-29 16:37:09 -0600 | [diff] [blame] | 90 |     if (!mAngleAppPref.empty()) { | 
 | 91 |         ALOGV("ignoring attempt to change ANGLE application opt-in from '%s' to '%s'", | 
 | 92 |               mAngleAppPref.c_str(), appPref.c_str()); | 
 | 93 |     } else { | 
 | 94 |         ALOGV("setting ANGLE application opt-in to '%s'", appPref.c_str()); | 
 | 95 |         mAngleAppPref = appPref; | 
 | 96 |     } | 
 | 97 |  | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 98 |     mAngleDeveloperOptIn = developerOptIn; | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 99 |  | 
 | 100 |     ALOGV("setting ANGLE rules file descriptor to '%i'", rulesFd); | 
 | 101 |     mAngleRulesFd = rulesFd; | 
 | 102 |     ALOGV("setting ANGLE rules offset to '%li'", rulesOffset); | 
 | 103 |     mAngleRulesOffset = rulesOffset; | 
 | 104 |     ALOGV("setting ANGLE rules length to '%li'", rulesLength); | 
 | 105 |     mAngleRulesLength = rulesLength; | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 106 | } | 
 | 107 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 108 | void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 109 |     if (mLayerPaths.empty()) { | 
 | 110 |         mLayerPaths = layerPaths; | 
 | 111 |         mAppNamespace = appNamespace; | 
 | 112 |     } else { | 
 | 113 |         ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'", | 
 | 114 |                 layerPaths.c_str(), appNamespace); | 
 | 115 |     } | 
 | 116 | } | 
 | 117 |  | 
| Victor Khimenko | 4819b52 | 2018-07-13 17:24:18 +0200 | [diff] [blame] | 118 | NativeLoaderNamespace* GraphicsEnv::getAppNamespace() { | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 119 |     return mAppNamespace; | 
 | 120 | } | 
 | 121 |  | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 122 | const char* GraphicsEnv::getAngleAppName() { | 
 | 123 |     if (mAngleAppName.empty()) return nullptr; | 
 | 124 |     return mAngleAppName.c_str(); | 
 | 125 | } | 
 | 126 |  | 
 | 127 | bool GraphicsEnv::getAngleDeveloperOptIn() { | 
 | 128 |     return mAngleDeveloperOptIn; | 
 | 129 | } | 
 | 130 |  | 
| Cody Northrop | 49d5199 | 2018-08-29 16:37:09 -0600 | [diff] [blame] | 131 | const char* GraphicsEnv::getAngleAppPref() { | 
 | 132 |     if (mAngleAppPref.empty()) return nullptr; | 
 | 133 |     return mAngleAppPref.c_str(); | 
 | 134 | } | 
 | 135 |  | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 136 | int GraphicsEnv::getAngleRulesFd() { | 
 | 137 |     return mAngleRulesFd; | 
 | 138 | } | 
 | 139 |  | 
 | 140 | long GraphicsEnv::getAngleRulesOffset() { | 
 | 141 |     return mAngleRulesOffset; | 
 | 142 | } | 
 | 143 |  | 
 | 144 | long GraphicsEnv::getAngleRulesLength() { | 
 | 145 |     return mAngleRulesLength; | 
 | 146 | } | 
 | 147 |  | 
| Cody Northrop | d2aa3ab | 2017-10-20 09:01:53 -0600 | [diff] [blame] | 148 | const std::string GraphicsEnv::getLayerPaths(){ | 
 | 149 |     return mLayerPaths; | 
 | 150 | } | 
 | 151 |  | 
 | 152 | const std::string GraphicsEnv::getDebugLayers() { | 
 | 153 |     return mDebugLayers; | 
 | 154 | } | 
 | 155 |  | 
 | 156 | void GraphicsEnv::setDebugLayers(const std::string layers) { | 
 | 157 |     mDebugLayers = layers; | 
 | 158 | } | 
 | 159 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 160 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
 | 161 |     static std::once_flag once; | 
 | 162 |     std::call_once(once, [this]() { | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 163 |         if (mDriverPath.empty()) | 
 | 164 |             return; | 
| Jesse Hall | 57de0ff | 2017-05-05 16:41:35 -0700 | [diff] [blame] | 165 |         // If the sphal namespace isn't configured for a device, don't support updatable drivers. | 
 | 166 |         // We need a parent namespace to inherit the default search path from. | 
 | 167 |         auto sphalNamespace = android_get_exported_namespace("sphal"); | 
 | 168 |         if (!sphalNamespace) return; | 
 | 169 |         mDriverNamespace = android_create_namespace("gfx driver", | 
 | 170 |                                                     nullptr,             // ld_library_path | 
 | 171 |                                                     mDriverPath.c_str(), // default_library_path | 
 | 172 |                                                     ANDROID_NAMESPACE_TYPE_SHARED | | 
 | 173 |                                                             ANDROID_NAMESPACE_TYPE_ISOLATED, | 
 | 174 |                                                     nullptr, // permitted_when_isolated_path | 
 | 175 |                                                     sphalNamespace); | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 176 |     }); | 
 | 177 |     return mDriverNamespace; | 
 | 178 | } | 
 | 179 |  | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 180 | android_namespace_t* GraphicsEnv::getAngleNamespace() { | 
 | 181 |     static std::once_flag once; | 
 | 182 |     std::call_once(once, [this]() { | 
 | 183 |         if (mAnglePath.empty()) return; | 
 | 184 |  | 
 | 185 |         mAngleNamespace = android_create_namespace("ANGLE", | 
 | 186 |                                                    nullptr,            // ld_library_path | 
 | 187 |                                                    mAnglePath.c_str(), // default_library_path | 
 | 188 |                                                    ANDROID_NAMESPACE_TYPE_SHARED | | 
 | 189 |                                                            ANDROID_NAMESPACE_TYPE_ISOLATED, | 
 | 190 |                                                    nullptr, // permitted_when_isolated_path | 
 | 191 |                                                    nullptr); | 
 | 192 |         if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default"); | 
 | 193 |     }); | 
 | 194 |  | 
 | 195 |     return mAngleNamespace; | 
 | 196 | } | 
 | 197 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 198 | } // namespace android | 
| Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 199 |  | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 200 | extern "C" { | 
 | 201 | android_namespace_t* android_getDriverNamespace() { | 
| Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 202 |     return android::GraphicsEnv::getInstance().getDriverNamespace(); | 
 | 203 | } | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 204 | android_namespace_t* android_getAngleNamespace() { | 
 | 205 |     return android::GraphicsEnv::getInstance().getAngleNamespace(); | 
 | 206 | } | 
| Courtney Goeltzenleuchter | d41ef25 | 2018-09-26 14:37:42 -0600 | [diff] [blame] | 207 | const char* android_getAngleAppName() { | 
 | 208 |     return android::GraphicsEnv::getInstance().getAngleAppName(); | 
 | 209 | } | 
 | 210 | bool android_getAngleDeveloperOptIn() { | 
 | 211 |     return android::GraphicsEnv::getInstance().getAngleDeveloperOptIn(); | 
 | 212 | } | 
| Cody Northrop | 49d5199 | 2018-08-29 16:37:09 -0600 | [diff] [blame] | 213 | const char* android_getAngleAppPref() { | 
 | 214 |     return android::GraphicsEnv::getInstance().getAngleAppPref(); | 
 | 215 | } | 
| Cody Northrop | 04e7043 | 2018-09-06 10:34:58 -0600 | [diff] [blame] | 216 | int android_getAngleRulesFd() { | 
 | 217 |    return android::GraphicsEnv::getInstance().getAngleRulesFd(); | 
 | 218 | } | 
 | 219 | long android_getAngleRulesOffset() { | 
 | 220 |    return android::GraphicsEnv::getInstance().getAngleRulesOffset(); | 
 | 221 | } | 
 | 222 | long android_getAngleRulesLength() { | 
 | 223 |    return android::GraphicsEnv::getInstance().getAngleRulesLength(); | 
 | 224 | } | 
| Cody Northrop | 629ce4e | 2018-10-15 07:22:09 -0600 | [diff] [blame] | 225 | const char* android_getLayerPaths() { | 
 | 226 |     return android::GraphicsEnv::getInstance().getLayerPaths().c_str(); | 
 | 227 | } | 
 | 228 | const char* android_getDebugLayers() { | 
 | 229 |     return android::GraphicsEnv::getInstance().getDebugLayers().c_str(); | 
 | 230 | } | 
| Cody Northrop | 1f00e17 | 2018-04-02 11:23:31 -0600 | [diff] [blame] | 231 | } |