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"
19 #include <gui/GraphicsEnv.h>
20
21 #include <mutex>
22
23 #include <log/log.h>
24 #include <nativeloader/dlext_namespaces.h>
25
26 namespace android {
27
getInstance()28 /*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
29 static GraphicsEnv env;
30 return env;
31 }
32
setDriverPath(const std::string path)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
getDriverNamespace()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
77 } // namespace android
78
android_getDriverNamespace()79 extern "C" android_namespace_t* android_getDriverNamespace() {
80 return android::GraphicsEnv::getInstance().getDriverNamespace();
81 }
82