OSDN Git Service

ab824d3466fb85067fa0a9014c6d9815d38e9245
[android-x86/frameworks-native.git] / libs / gui / GraphicsEnv.cpp
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
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
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