blob: 80daeb9fbbc403b371700fb3a3e33bcc6bdc7c0c [file] [log] [blame]
Jesse Hall90b25ed2016-12-12 12:56:46 -08001/*
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"
Jiyong Park27c39e12017-05-08 13:00:02 +090019#include <graphicsenv/GraphicsEnv.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080020
Jesse Hall53457db2016-12-14 16:54:06 -080021#include <mutex>
22
Jiyong Park9b816a82018-01-02 17:37:37 +090023#include <android/dlext.h>
Jesse Hall90b25ed2016-12-12 12:56:46 -080024#include <log/log.h>
25
Jesse Hall57de0ff2017-05-05 16:41:35 -070026// TODO(b/37049319) Get this from a header once one exists
27extern "C" {
28 android_namespace_t* android_get_exported_namespace(const char*);
Jiyong Park9b816a82018-01-02 17:37:37 +090029 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 };
Jesse Hall57de0ff2017-05-05 16:41:35 -070040}
41
Jesse Hall90b25ed2016-12-12 12:56:46 -080042namespace android {
43
44/*static*/ GraphicsEnv& GraphicsEnv::getInstance() {
45 static GraphicsEnv env;
46 return env;
47}
48
49void 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
Cody Northrop1f00e172018-04-02 11:23:31 -060059void GraphicsEnv::setAnglePath(const std::string path) {
60 if (!mAnglePath.empty()) {
61 ALOGV("ignoring attempt to change ANGLE path from '%s' to '%s'", mAnglePath.c_str(),
62 path.c_str());
63 return;
64 }
65 ALOGV("setting ANGLE path to '%s'", path.c_str());
66 mAnglePath = path;
67}
68
Victor Khimenko4819b522018-07-13 17:24:18 +020069void GraphicsEnv::setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths) {
Cody Northropd2aa3ab2017-10-20 09:01:53 -060070 if (mLayerPaths.empty()) {
71 mLayerPaths = layerPaths;
72 mAppNamespace = appNamespace;
73 } else {
74 ALOGV("Vulkan layer search path already set, not clobbering with '%s' for namespace %p'",
75 layerPaths.c_str(), appNamespace);
76 }
77}
78
Victor Khimenko4819b522018-07-13 17:24:18 +020079NativeLoaderNamespace* GraphicsEnv::getAppNamespace() {
Cody Northropd2aa3ab2017-10-20 09:01:53 -060080 return mAppNamespace;
81}
82
83const std::string GraphicsEnv::getLayerPaths(){
84 return mLayerPaths;
85}
86
87const std::string GraphicsEnv::getDebugLayers() {
88 return mDebugLayers;
89}
90
91void GraphicsEnv::setDebugLayers(const std::string layers) {
92 mDebugLayers = layers;
93}
94
Jesse Hall53457db2016-12-14 16:54:06 -080095android_namespace_t* GraphicsEnv::getDriverNamespace() {
96 static std::once_flag once;
97 std::call_once(once, [this]() {
Jesse Hall53457db2016-12-14 16:54:06 -080098 if (mDriverPath.empty())
99 return;
Jesse Hall57de0ff2017-05-05 16:41:35 -0700100 // If the sphal namespace isn't configured for a device, don't support updatable drivers.
101 // We need a parent namespace to inherit the default search path from.
102 auto sphalNamespace = android_get_exported_namespace("sphal");
103 if (!sphalNamespace) return;
104 mDriverNamespace = android_create_namespace("gfx driver",
105 nullptr, // ld_library_path
106 mDriverPath.c_str(), // default_library_path
107 ANDROID_NAMESPACE_TYPE_SHARED |
108 ANDROID_NAMESPACE_TYPE_ISOLATED,
109 nullptr, // permitted_when_isolated_path
110 sphalNamespace);
Jesse Hall53457db2016-12-14 16:54:06 -0800111 });
112 return mDriverNamespace;
113}
114
Cody Northrop1f00e172018-04-02 11:23:31 -0600115android_namespace_t* GraphicsEnv::getAngleNamespace() {
116 static std::once_flag once;
117 std::call_once(once, [this]() {
118 if (mAnglePath.empty()) return;
119
120 mAngleNamespace = android_create_namespace("ANGLE",
121 nullptr, // ld_library_path
122 mAnglePath.c_str(), // default_library_path
123 ANDROID_NAMESPACE_TYPE_SHARED |
124 ANDROID_NAMESPACE_TYPE_ISOLATED,
125 nullptr, // permitted_when_isolated_path
126 nullptr);
127 if (!mAngleNamespace) ALOGD("Could not create ANGLE namespace from default");
128 });
129
130 return mAngleNamespace;
131}
132
Jesse Hall90b25ed2016-12-12 12:56:46 -0800133} // namespace android
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800134
Cody Northrop1f00e172018-04-02 11:23:31 -0600135extern "C" {
136android_namespace_t* android_getDriverNamespace() {
Jesse Hall7a8d83e2016-12-20 15:24:28 -0800137 return android::GraphicsEnv::getInstance().getDriverNamespace();
138}
Cody Northrop1f00e172018-04-02 11:23:31 -0600139android_namespace_t* android_getAngleNamespace() {
140 return android::GraphicsEnv::getInstance().getAngleNamespace();
141}
142}