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 class GraphicsEnv {
33 public:
34     static GraphicsEnv& getInstance();
35 
36     // Check if the process is debuggable. It returns false except in any of the
37     // following circumstances:
38     // 1. ro.debuggable=1 (global debuggable enabled).
39     // 2. android:debuggable="true" in the manifest for an individual app.
40     // 3. An app which explicitly calls prctl(PR_SET_DUMPABLE, 1).
41     // 4. GraphicsEnv calls prctl(PR_SET_DUMPABLE, 1) in the presence of
42     //    <meta-data android:name="com.android.graphics.injectLayers.enable"
43     //               android:value="true"/>
44     //    in the application manifest.
45     bool isDebuggable();
46 
47     /*
48      * Apis for updatable driver
49      */
50     // Set a search path for loading graphics drivers. The path is a list of
51     // directories separated by ':'. A directory can be contained in a zip file
52     // (drivers must be stored uncompressed and page aligned); such elements
53     // in the search path must have a '!' after the zip filename, e.g.
54     //     /data/app/com.example.driver/base.apk!/lib/arm64-v8a
55     // Also set additional required sphal libraries to the linker for loading
56     // graphics drivers. The string is a list of libraries separated by ':',
57     // which is required by android_link_namespaces.
58     void setDriverPathAndSphalLibraries(const std::string path, const std::string sphalLibraries);
59     // Get the updatable driver namespace.
60     android_namespace_t* getDriverNamespace();
61     std::string getDriverPath() const;
62 
63     /*
64      * Apis for GpuStats
65      */
66     // Hint there's real activity launching on the app process.
67     void hintActivityLaunch();
68     // Set the initial GpuStats.
69     void setGpuStats(const std::string& driverPackageName, const std::string& driverVersionName,
70                      uint64_t versionCode, int64_t driverBuildTime,
71                      const std::string& appPackageName, const int32_t vulkanVersion);
72     // Set stats for target GpuStatsInfo::Stats type.
73     void setTargetStats(const GpuStatsInfo::Stats stats, const uint64_t value = 0);
74     // Set which driver is intended to load.
75     void setDriverToLoad(GpuStatsInfo::Driver driver);
76     // Set which driver is actually loaded.
77     void setDriverLoaded(GpuStatsInfo::Api api, bool isDriverLoaded, int64_t driverLoadingTime);
78 
79     /*
80      * Api for Vk/GL layer injection.  Presently, drivers enable certain
81      * profiling features when prctl(PR_GET_DUMPABLE) returns true.
82      * Calling this when layer injection metadata is present allows the driver
83      * to enable profiling even when in a non-debuggable app
84      */
85     bool setInjectLayersPrSetDumpable();
86 
87     /*
88      * Apis for ANGLE
89      */
90     // Check if the requested app should use ANGLE.
91     bool shouldUseAngle(std::string appName);
92     // Check if this app process should use ANGLE.
93     bool shouldUseAngle();
94     // Set a search path for loading ANGLE libraries. The path is a list of
95     // directories separated by ':'. A directory can be contained in a zip file
96     // (libraries must be stored uncompressed and page aligned); such elements
97     // in the search path must have a '!' after the zip filename, e.g.
98     //     /system/app/ANGLEPrebuilt/ANGLEPrebuilt.apk!/lib/arm64-v8a
99     void setAngleInfo(const std::string path, const std::string appName, std::string devOptIn,
100                       const int rulesFd, const long rulesOffset, const long rulesLength);
101     // Get the ANGLE driver namespace.
102     android_namespace_t* getAngleNamespace();
103     // Get the app name for ANGLE debug message.
104     std::string& getAngleAppName();
105 
106     /*
107      * Apis for debug layer
108      */
109     // Set additional layer search paths.
110     void setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths);
111     // Get the app namespace for loading layers.
112     NativeLoaderNamespace* getAppNamespace();
113     // Get additional layer search paths.
114     const std::string& getLayerPaths();
115     // Set the Vulkan debug layers.
116     void setDebugLayers(const std::string layers);
117     // Set the GL debug layers.
118     void setDebugLayersGLES(const std::string layers);
119     // Get the debug layers to load.
120     const std::string& getDebugLayers();
121     // Get the debug layers to load.
122     const std::string& getDebugLayersGLES();
123 
124 private:
125     enum UseAngle { UNKNOWN, YES, NO };
126 
127     // Load requested ANGLE library.
128     void* loadLibrary(std::string name);
129     // Check ANGLE support with the rules.
130     bool checkAngleRules(void* so);
131     // Update whether ANGLE should be used.
132     void updateUseAngle();
133     // Link updatable driver namespace with llndk and vndk-sp libs.
134     bool linkDriverNamespaceLocked(android_namespace_t* vndkNamespace);
135     // Check whether this process is ready to send stats.
136     bool readyToSendGpuStatsLocked();
137     // Send the initial complete GpuStats to GpuService.
138     void sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, int64_t driverLoadingTime);
139 
140     GraphicsEnv() = default;
141     // Path to updatable driver libs.
142     std::string mDriverPath;
143     // Path to additional sphal libs linked to updatable driver namespace.
144     std::string mSphalLibraries;
145     // This mutex protects mGpuStats and get gpuservice call.
146     std::mutex mStatsLock;
147     // Cache the activity launch info
148     bool mActivityLaunched = false;
149     // Information bookkept for GpuStats.
150     GpuStatsInfo mGpuStats;
151     // Path to ANGLE libs.
152     std::string mAnglePath;
153     // This App's name.
154     std::string mAngleAppName;
155     // ANGLE developer opt in status.
156     std::string mAngleDeveloperOptIn;
157     // ANGLE rules.
158     std::vector<char> mRulesBuffer;
159     // Use ANGLE flag.
160     UseAngle mUseAngle = UNKNOWN;
161     // Vulkan debug layers libs.
162     std::string mDebugLayers;
163     // GL debug layers libs.
164     std::string mDebugLayersGLES;
165     // Additional debug layers search path.
166     std::string mLayerPaths;
167     // This mutex protects the namespace creation.
168     std::mutex mNamespaceMutex;
169     // Updatable driver namespace.
170     android_namespace_t* mDriverNamespace = nullptr;
171     // ANGLE namespace.
172     android_namespace_t* mAngleNamespace = nullptr;
173     // This App's namespace.
174     NativeLoaderNamespace* mAppNamespace = nullptr;
175 };
176 
177 } // namespace android
178 
179 #endif // ANDROID_UI_GRAPHICS_ENV_H
180