| 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" | 
| Mathias Agopian | 991d254 | 2017-02-06 13:51:32 -0800 | [diff] [blame] | 19 | #include <ui/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 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 23 | #include <log/log.h> | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 24 | #include <nativeloader/dlext_namespaces.h> | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 |  | 
|  | 28 | /*static*/ GraphicsEnv& GraphicsEnv::getInstance() { | 
|  | 29 | static GraphicsEnv env; | 
|  | 30 | return env; | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | void GraphicsEnv::setDriverPath(const std::string path) { | 
|  | 34 | if (!mDriverPath.empty()) { | 
|  | 35 | ALOGV("ignoring attempt to change driver path from '%s' to '%s'", | 
|  | 36 | mDriverPath.c_str(), path.c_str()); | 
|  | 37 | return; | 
|  | 38 | } | 
|  | 39 | ALOGV("setting driver path to '%s'", path.c_str()); | 
|  | 40 | mDriverPath = path; | 
|  | 41 | } | 
|  | 42 |  | 
| Jesse Hall | 53457db | 2016-12-14 16:54:06 -0800 | [diff] [blame] | 43 | android_namespace_t* GraphicsEnv::getDriverNamespace() { | 
|  | 44 | static std::once_flag once; | 
|  | 45 | std::call_once(once, [this]() { | 
|  | 46 | // TODO; In the next version of Android, all graphics drivers will be | 
|  | 47 | // loaded into a custom namespace. To minimize risk for this release, | 
|  | 48 | // only updated drivers use a custom namespace. | 
|  | 49 | // | 
|  | 50 | // Additionally, the custom namespace will be | 
|  | 51 | // ANDROID_NAMESPACE_TYPE_ISOLATED, and will only have access to a | 
|  | 52 | // subset of the system. | 
|  | 53 | if (mDriverPath.empty()) | 
|  | 54 | return; | 
|  | 55 |  | 
|  | 56 | char defaultPath[PATH_MAX]; | 
|  | 57 | android_get_LD_LIBRARY_PATH(defaultPath, sizeof(defaultPath)); | 
|  | 58 | size_t defaultPathLen = strlen(defaultPath); | 
|  | 59 |  | 
|  | 60 | std::string path; | 
|  | 61 | path.reserve(mDriverPath.size() + 1 + defaultPathLen); | 
|  | 62 | path.append(mDriverPath); | 
|  | 63 | path.push_back(':'); | 
|  | 64 | path.append(defaultPath, defaultPathLen); | 
|  | 65 |  | 
|  | 66 | mDriverNamespace = android_create_namespace( | 
|  | 67 | "gfx driver", | 
|  | 68 | nullptr,                    // ld_library_path | 
|  | 69 | path.c_str(),               // default_library_path | 
|  | 70 | ANDROID_NAMESPACE_TYPE_SHARED, | 
|  | 71 | nullptr,                    // permitted_when_isolated_path | 
|  | 72 | nullptr);                   // parent | 
|  | 73 | }); | 
|  | 74 | return mDriverNamespace; | 
|  | 75 | } | 
|  | 76 |  | 
| Jesse Hall | 90b25ed | 2016-12-12 12:56:46 -0800 | [diff] [blame] | 77 | } // namespace android | 
| Jesse Hall | 7a8d83e | 2016-12-20 15:24:28 -0800 | [diff] [blame] | 78 |  | 
|  | 79 | extern "C" android_namespace_t* android_getDriverNamespace() { | 
|  | 80 | return android::GraphicsEnv::getInstance().getDriverNamespace(); | 
|  | 81 | } |