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 <graphicsenv/GraphicsEnv.h>
20 
21 #include <mutex>
22 
23 #include <android/dlext.h>
24 #include <log/log.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   android_namespace_t* android_create_namespace(const char* name,
30                                                 const char* ld_library_path,
31                                                 const char* default_library_path,
32                                                 uint64_t type,
33                                                 const char* permitted_when_isolated_path,
34                                                 android_namespace_t* parent);
35 
36   enum {
37      ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
38      ANDROID_NAMESPACE_TYPE_SHARED = 2,
39   };
40 }
41 
42 namespace android {
43 
getInstance()44 /*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
45     static GraphicsEnv env;
46     return env;
47 }
48 
setDriverPath(const std::string path)49 void GraphicsEnv::setDriverPath(const std::string path) {
50     if (!mDriverPath.empty()) {
51         ALOGV("ignoring attempt to change driver path from '%s' to '%s'",
52                 mDriverPath.c_str(), path.c_str());
53         return;
54     }
55     ALOGV("setting driver path to '%s'", path.c_str());
56     mDriverPath = path;
57 }
58 
setLayerPaths(android_namespace_t * appNamespace,const std::string layerPaths)59 void GraphicsEnv::setLayerPaths(android_namespace_t* appNamespace, const std::string layerPaths) {
60     if (mLayerPaths.empty()) {
61         mLayerPaths = layerPaths;
62         mAppNamespace = appNamespace;
63     } else {
64         ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
65                 layerPaths.c_str(), appNamespace);
66     }
67 }
68 
getAppNamespace()69 android_namespace_t* GraphicsEnv::getAppNamespace() {
70     return mAppNamespace;
71 }
72 
getLayerPaths()73 const std::string GraphicsEnv::getLayerPaths(){
74     return mLayerPaths;
75 }
76 
getDebugLayers()77 const std::string GraphicsEnv::getDebugLayers() {
78     return mDebugLayers;
79 }
80 
setDebugLayers(const std::string layers)81 void GraphicsEnv::setDebugLayers(const std::string layers) {
82     mDebugLayers = layers;
83 }
84 
getDriverNamespace()85 android_namespace_t* GraphicsEnv::getDriverNamespace() {
86     static std::once_flag once;
87     std::call_once(once, [this]() {
88         if (mDriverPath.empty())
89             return;
90         // If the sphal namespace isn't configured for a device, don't support updatable drivers.
91         // We need a parent namespace to inherit the default search path from.
92         auto sphalNamespace = android_get_exported_namespace("sphal");
93         if (!sphalNamespace) return;
94         mDriverNamespace = android_create_namespace("gfx driver",
95                                                     nullptr,             // ld_library_path
96                                                     mDriverPath.c_str(), // default_library_path
97                                                     ANDROID_NAMESPACE_TYPE_SHARED |
98                                                             ANDROID_NAMESPACE_TYPE_ISOLATED,
99                                                     nullptr, // permitted_when_isolated_path
100                                                     sphalNamespace);
101     });
102     return mDriverNamespace;
103 }
104 
105 } // namespace android
106 
android_getDriverNamespace()107 extern "C" android_namespace_t* android_getDriverNamespace() {
108     return android::GraphicsEnv::getInstance().getDriverNamespace();
109 }
110