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