• 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 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 std::vector<std::string> eglFeatures, const int rulesFd,
101                       const long rulesOffset, const long rulesLength);
102     // Get the ANGLE driver namespace.
103     android_namespace_t* getAngleNamespace();
104     // Get the app name for ANGLE debug message.
105     std::string& getAngleAppName();
106 
107     const std::vector<std::string>& getAngleEglFeatures();
108 
109     /*
110      * Apis for debug layer
111      */
112     // Set additional layer search paths.
113     void setLayerPaths(NativeLoaderNamespace* appNamespace, const std::string layerPaths);
114     // Get the app namespace for loading layers.
115     NativeLoaderNamespace* getAppNamespace();
116     // Get additional layer search paths.
117     const std::string& getLayerPaths();
118     // Set the Vulkan debug layers.
119     void setDebugLayers(const std::string layers);
120     // Set the GL debug layers.
121     void setDebugLayersGLES(const std::string layers);
122     // Get the debug layers to load.
123     const std::string& getDebugLayers();
124     // Get the debug layers to load.
125     const std::string& getDebugLayersGLES();
126 
127 private:
128     enum UseAngle { UNKNOWN, YES, NO };
129 
130     // Load requested ANGLE library.
131     void* loadLibrary(std::string name);
132     // Check ANGLE support with the rules.
133     bool checkAngleRules(void* so);
134     // Update whether ANGLE should be used.
135     void updateUseAngle();
136     // Link updatable driver namespace with llndk and vndk-sp libs.
137     bool linkDriverNamespaceLocked(android_namespace_t* vndkNamespace);
138     // Check whether this process is ready to send stats.
139     bool readyToSendGpuStatsLocked();
140     // Send the initial complete GpuStats to GpuService.
141     void sendGpuStatsLocked(GpuStatsInfo::Api api, bool isDriverLoaded, int64_t driverLoadingTime);
142 
143     GraphicsEnv() = default;
144     // Path to updatable driver libs.
145     std::string mDriverPath;
146     // Path to additional sphal libs linked to updatable driver namespace.
147     std::string mSphalLibraries;
148     // This mutex protects mGpuStats and get gpuservice call.
149     std::mutex mStatsLock;
150     // Cache the activity launch info
151     bool mActivityLaunched = false;
152     // Information bookkept for GpuStats.
153     GpuStatsInfo mGpuStats;
154     // Path to ANGLE libs.
155     std::string mAnglePath;
156     // This App's name.
157     std::string mAngleAppName;
158     // ANGLE developer opt in status.
159     std::string mAngleDeveloperOptIn;
160     // ANGLE EGL features;
161     std::vector<std::string> mAngleEglFeatures;
162     // ANGLE rules.
163     std::vector<char> mRulesBuffer;
164     // Use ANGLE flag.
165     UseAngle mUseAngle = UNKNOWN;
166     // Vulkan debug layers libs.
167     std::string mDebugLayers;
168     // GL debug layers libs.
169     std::string mDebugLayersGLES;
170     // Additional debug layers search path.
171     std::string mLayerPaths;
172     // This mutex protects the namespace creation.
173     std::mutex mNamespaceMutex;
174     // Updatable driver namespace.
175     android_namespace_t* mDriverNamespace = nullptr;
176     // ANGLE namespace.
177     android_namespace_t* mAngleNamespace = nullptr;
178     // This App's namespace.
179     NativeLoaderNamespace* mAppNamespace = nullptr;
180 };
181 
182 } // namespace android
183 
184 #endif // ANDROID_UI_GRAPHICS_ENV_H
185