• 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 <graphicsenv/GpuStatsInfo.h>
21 
22 #include <mutex>
23 #include <string>
24 #include <vector>
25 
26 struct android_namespace_t;
27 
28 namespace android {
29 
30 struct NativeLoaderNamespace;
31 
32 // The GraphicsEnv is a singleton per application process and is used to properly set up the
33 // graphics drivers for the application process during application starts. The architecture of
34 // the graphics driver loader does not support runtime switch and only supports switch to different
35 // graphics drivers when application process launches and hence the only way to switch to different
36 // graphics drivers is to completely kill the application process and relaunch the application.
37 class GraphicsEnv {
38 public:
39     static GraphicsEnv& getInstance();
40 
41     // Check if the process is debuggable. It returns false except in any of the
42     // following circumstances:
43     // 1. ANDROID_DEBUGGABLE is defined (global debuggable enabled).
44     // 2. android:debuggable="true" in the manifest for an individual app.
45     // 3. An app which explicitly calls prctl(PR_SET_DUMPABLE, 1).
46     // 4. GraphicsEnv calls prctl(PR_SET_DUMPABLE, 1) in the presence of
47     //    <meta-data android:name="com.android.graphics.injectLayers.enable"
48     //               android:value="true"/>
49     //    in the application manifest.
50     bool isDebuggable();
51 
52     /*
53      * Apis for updatable driver
54      */
55     // Set a search path for loading graphics drivers. The path is a list of
56     // directories separated by ':'. A directory can be contained in a zip file
57     // (drivers must be stored uncompressed and page aligned); such elements
58     // in the search path must have a '!' after the zip filename, e.g.
59     //     /data/app/com.example.driver/base.apk!/lib/arm64-v8a
60     // Also set additional required sphal libraries to the linker for loading
61     // graphics drivers. The string is a list of libraries separated by ':',
62     // which is required by android_link_namespaces.
63     void setDriverPathAndSphalLibraries(const std::string& path, const std::string& sphalLibraries);
64     // Get the updatable driver namespace.
65     android_namespace_t* getDriverNamespace();
66     std::string getDriverPath() const;
67 
68     /*
69      * Apis for GpuStats
70      */
71     // Hint there's real activity launching on the app process.
72     void hintActivityLaunch();
73     // Set the initial GpuStats.
74     void setGpuStats(const std::string& driverPackageName, const std::string& driverVersionName,
75                      uint64_t versionCode, int64_t driverBuildTime,
76                      const std::string& appPackageName, const int32_t vulkanVersion);
77     // Set stats for target GpuStatsInfo::Stats type.
78     void setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value = 0);
79     // Set array of stats for target GpuStatsInfo::Stats type.
80     void setTargetStatsArray(const GpuStatsInfo::Stats stats, const uint64_t* values,
81                              const uint32_t valueCount);
82     // Set which driver is intended to load.
83     void setDriverToLoad(GpuStatsInfo::Driver driver);
84     // Set which driver is actually loaded.
85     void setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, int64_t driverLoadingTime);
86     // Set which instance extensions are enabled for the app.
87     void setVulkanInstanceExtensions(uint32_t enabledExtensionCount,
88                                      const char* const* ppEnabledExtensionNames);
89     // Set which device extensions are enabled for the app.
90     void setVulkanDeviceExtensions(uint32_t enabledExtensionCount,
91                                    const char* const* ppEnabledExtensionNames);
92 
93     /*
94      * Api for Vk/GL layer injection.  Presently, drivers enable certain
95      * profiling features when prctl(PR_GET_DUMPABLE) returns true.
96      * Calling this when layer injection metadata is present allows the driver
97      * to enable profiling even when in a non-debuggable app
98      */
99     bool setInjectLayersPrSetDumpable();
100 
101     /*
102      * Apis for ANGLE
103      */
104     // Check if this app process should use ANGLE.
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     // If the search patch is "system", then it means the system ANGLE should be used.
112     // If shouldUseNativeDriver is true, it means native GLES drivers must be used for the process.
113     // If path is set to nonempty and shouldUseNativeDriver is true, ANGLE will be used regardless.
114     void setAngleInfo(const std::string& path, const bool shouldUseNativeDriver,
115                       const std::string& packageName, const std::vector<std::string> eglFeatures);
116     // Get the ANGLE driver namespace.
117     android_namespace_t* getAngleNamespace();
118     // Get the app package name.
119     std::string& getPackageName();
120     const std::vector<std::string>& getAngleEglFeatures();
121     // Set the persist.graphics.egl system property value.
122     void nativeToggleAngleAsSystemDriver(bool enabled);
123     bool shouldUseSystemAngle();
124     bool shouldUseNativeDriver();
125 
126     /*
127      * Apis for debug layer
128      */
129     // Set additional layer search paths.
130     void setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string& layerPaths);
131     // Get the app namespace for loading layers.
132     NativeLoaderNamespace* getAppNamespace();
133     // Get additional layer search paths.
134     const std::string& getLayerPaths();
135     // Set the Vulkan debug layers.
136     void setDebugLayers(const std::string& layers);
137     // Set the GL debug layers.
138     void setDebugLayersGLES(const std::string& layers);
139     // Get the debug layers to load.
140     const std::string& getDebugLayers();
141     // Get the debug layers to load.
142     const std::string& getDebugLayersGLES();
143 
144 private:
145     // Link updatable driver namespace with llndk and vndk-sp libs.
146     bool linkDriverNamespaceLocked(android_namespace_t* destNamespace,
147                                    android_namespace_t* vndkNamespace,
148                                    const std::string& sharedSphalLibraries);
149     // Check whether this process is ready to send stats.
150     bool readyToSendGpuStatsLocked();
151     // Send the initial complete GpuStats to GpuService.
152     void sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, int64_t driverLoadingTime);
153 
154     GraphicsEnv() = default;
155 
156     // This mutex protects the namespace creation.
157     std::mutex mNamespaceMutex;
158 
159     /**
160      * Updatable driver variables.
161      */
162     // Path to updatable driver libs.
163     std::string mDriverPath;
164     // Path to additional sphal libs linked to updatable driver namespace.
165     std::string mSphalLibraries;
166     // Updatable driver namespace.
167     android_namespace_t* mDriverNamespace = nullptr;
168 
169     /**
170      * ANGLE variables.
171      */
172     // Path to ANGLE libs.
173     std::string mAnglePath;
174     // App's package name.
175     std::string mPackageName;
176     // ANGLE EGL features;
177     std::vector<std::string> mAngleEglFeatures;
178     // Whether ANGLE should be used.
179     bool mShouldUseAngle = false;
180     // Whether loader should load system ANGLE.
181     bool mShouldUseSystemAngle = false;
182     // Whether loader should load native GLES driver.
183     bool mShouldUseNativeDriver = false;
184     // ANGLE namespace.
185     android_namespace_t* mAngleNamespace = nullptr;
186 
187     /**
188      * GPU metrics.
189      */
190     // This mutex protects mGpuStats and get gpuservice call.
191     std::mutex mStatsLock;
192     // Cache the activity launch info
193     bool mActivityLaunched = false;
194     // Information bookkept for GpuStats.
195     GpuStatsInfo mGpuStats;
196 
197     /**
198      * Debug layers.
199      */
200     // Vulkan debug layers libs.
201     std::string mDebugLayers;
202     // GL debug layers libs.
203     std::string mDebugLayersGLES;
204     // Additional debug layers search path.
205     std::string mLayerPaths;
206     // This App's namespace to open native libraries.
207     NativeLoaderNamespace* mAppNamespace = nullptr;
208 };
209 
210 } // namespace android
211 
212 #endif // ANDROID_UI_GRAPHICS_ENV_H
213