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 #ifndef ANDROID_UI_GRAPHICS_ENV_H
18 #define ANDROID_UI_GRAPHICS_ENV_H 1
19 
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 struct android_namespace_t;
25 
26 namespace android {
27 
28 struct NativeLoaderNamespace;
29 
30 class GraphicsEnv {
31 public:
32     enum Api {
33         API_GL = 0,
34         API_VK = 1,
35     };
36 
37     enum Driver {
38         NONE = 0,
39         GL = 1,
40         GL_UPDATED = 2,
41         VULKAN = 3,
42         VULKAN_UPDATED = 4,
43         ANGLE = 5,
44     };
45 
46 private:
47     struct GpuStats {
48         std::string driverPackageName;
49         std::string driverVersionName;
50         uint64_t driverVersionCode;
51         int64_t driverBuildTime;
52         std::string appPackageName;
53         int32_t vulkanVersion;
54         Driver glDriverToLoad;
55         Driver glDriverFallback;
56         Driver vkDriverToLoad;
57         Driver vkDriverFallback;
58         bool glDriverToSend;
59         bool vkDriverToSend;
60         int64_t glDriverLoadingTime;
61         int64_t vkDriverLoadingTime;
62 
GpuStatsGpuStats63         GpuStats()
64               : driverPackageName(""),
65                 driverVersionName(""),
66                 driverVersionCode(0),
67                 driverBuildTime(0),
68                 appPackageName(""),
69                 vulkanVersion(0),
70                 glDriverToLoad(Driver::NONE),
71                 glDriverFallback(Driver::NONE),
72                 vkDriverToLoad(Driver::NONE),
73                 vkDriverFallback(Driver::NONE),
74                 glDriverToSend(false),
75                 vkDriverToSend(false),
76                 glDriverLoadingTime(0),
77                 vkDriverLoadingTime(0) {}
78     };
79 
80 public:
81     static GraphicsEnv& getInstance();
82 
83     int getCanLoadSystemLibraries();
84 
85     // Set a search path for loading graphics drivers. The path is a list of
86     // directories separated by ':'. A directory can be contained in a zip file
87     // (drivers must be stored uncompressed and page aligned); such elements
88     // in the search path must have a '!' after the zip filename, e.g.
89     //     /data/app/com.example.driver/base.apk!/lib/arm64-v8a
90     // Also set additional required sphal libraries to the linker for loading
91     // graphics drivers. The string is a list of libraries separated by ':',
92     // which is required by android_link_namespaces.
93     void setDriverPathAndSphalLibraries(const std::string path, const std::string sphalLibraries);
94     android_namespace_t* getDriverNamespace();
95     void hintActivityLaunch();
96     void setGpuStats(const std::string& driverPackageName, const std::string& driverVersionName,
97                      uint64_t versionCode, int64_t driverBuildTime,
98                      const std::string& appPackageName, const int32_t vulkanVersion);
99     void setCpuVulkanInUse();
100     void setDriverToLoad(Driver driver);
101     void setDriverLoaded(Api api, bool isDriverLoaded, int64_t driverLoadingTime);
102     void sendGpuStatsLocked(Api api, bool isDriverLoaded, int64_t driverLoadingTime);
103 
104     bool shouldUseAngle(std::string appName);
105     bool shouldUseAngle();
106     // Set a search path for loading ANGLE libraries. The path is a list of
107     // directories separated by ':'. A directory can be contained in a zip file
108     // (libraries must be stored uncompressed and page aligned); such elements
109     // in the search path must have a '!' after the zip filename, e.g.
110     //     /system/app/ANGLEPrebuilt/ANGLEPrebuilt.apk!/lib/arm64-v8a
111     void setAngleInfo(const std::string path, const std::string appName, std::string devOptIn,
112                       const int rulesFd, const long rulesOffset, const long rulesLength);
113     android_namespace_t* getAngleNamespace();
114     std::string& getAngleAppName();
115 
116     void setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths);
117     NativeLoaderNamespace* getAppNamespace();
118 
119     const std::string& getLayerPaths();
120 
121     void setDebugLayers(const std::string layers);
122     void setDebugLayersGLES(const std::string layers);
123     const std::string& getDebugLayers();
124     const std::string& getDebugLayersGLES();
125 
126 private:
127     enum UseAngle { UNKNOWN, YES, NO };
128 
129     void* loadLibrary(std::string name);
130     bool checkAngleRules(void* so);
131     void updateUseAngle();
132     bool linkDriverNamespaceLocked(android_namespace_t* vndkNamespace);
133 
134     GraphicsEnv() = default;
135     std::string mDriverPath;
136     std::string mSphalLibraries;
137     std::mutex mStatsLock;
138     GpuStats mGpuStats;
139     std::string mAnglePath;
140     std::string mAngleAppName;
141     std::string mAngleDeveloperOptIn;
142     std::vector<char> mRulesBuffer;
143     UseAngle mUseAngle = UNKNOWN;
144     std::string mDebugLayers;
145     std::string mDebugLayersGLES;
146     std::string mLayerPaths;
147     std::mutex mNamespaceMutex;
148     android_namespace_t* mDriverNamespace = nullptr;
149     android_namespace_t* mAngleNamespace = nullptr;
150     NativeLoaderNamespace* mAppNamespace = nullptr;
151 };
152 
153 } // namespace android
154 
155 #endif // ANDROID_UI_GRAPHICS_ENV_H
156