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 <ui/GraphicsEnv.h>
20 
21 #include <mutex>
22 
23 #include <log/log.h>
24 #include <nativeloader/dlext_namespaces.h>
25 
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*);
29 }
30 
31 namespace android {
32 
getInstance()33 /*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
34     static GraphicsEnv env;
35     return env;
36 }
37 
setDriverPath(const std::string path)38 void GraphicsEnv::setDriverPath(const std::string path) {
39     if (!mDriverPath.empty()) {
40         ALOGV("ignoring attempt to change driver path from '%s' to '%s'",
41                 mDriverPath.c_str(), path.c_str());
42         return;
43     }
44     ALOGV("setting driver path to '%s'", path.c_str());
45     mDriverPath = path;
46 }
47 
getDriverNamespace()48 android_namespace_t* GraphicsEnv::getDriverNamespace() {
49     static std::once_flag once;
50     std::call_once(once, [this]() {
51         if (mDriverPath.empty())
52             return;
53         // If the sphal namespace isn't configured for a device, don't support updatable drivers.
54         // We need a parent namespace to inherit the default search path from.
55         auto sphalNamespace = android_get_exported_namespace("sphal");
56         if (!sphalNamespace) return;
57         mDriverNamespace = android_create_namespace("gfx driver",
58                                                     nullptr,             // ld_library_path
59                                                     mDriverPath.c_str(), // default_library_path
60                                                     ANDROID_NAMESPACE_TYPE_SHARED |
61                                                             ANDROID_NAMESPACE_TYPE_ISOLATED,
62                                                     nullptr, // permitted_when_isolated_path
63                                                     sphalNamespace);
64     });
65     return mDriverNamespace;
66 }
67 
68 } // namespace android
69 
android_getDriverNamespace()70 extern "C" android_namespace_t* android_getDriverNamespace() {
71     return android::GraphicsEnv::getInstance().getDriverNamespace();
72 }
73