1 /*
2  * Copyright 2019 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 #define LOG_TAG "GpuService"
18 
19 #include <graphicsenv/IGpuService.h>
20 
21 #include <binder/IResultReceiver.h>
22 #include <binder/Parcel.h>
23 
24 namespace android {
25 
26 class BpGpuService : public BpInterface<IGpuService> {
27 public:
BpGpuService(const sp<IBinder> & impl)28     explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
29 
setGpuStats(const std::string & driverPackageName,const std::string & driverVersionName,uint64_t driverVersionCode,int64_t driverBuildTime,const std::string & appPackageName,const int32_t vulkanVersion,GraphicsEnv::Driver driver,bool isDriverLoaded,int64_t driverLoadingTime)30     virtual void setGpuStats(const std::string& driverPackageName,
31                              const std::string& driverVersionName, uint64_t driverVersionCode,
32                              int64_t driverBuildTime, const std::string& appPackageName,
33                              const int32_t vulkanVersion, GraphicsEnv::Driver driver,
34                              bool isDriverLoaded, int64_t driverLoadingTime) {
35         Parcel data, reply;
36         data.writeInterfaceToken(IGpuService::getInterfaceDescriptor());
37 
38         data.writeUtf8AsUtf16(driverPackageName);
39         data.writeUtf8AsUtf16(driverVersionName);
40         data.writeUint64(driverVersionCode);
41         data.writeInt64(driverBuildTime);
42         data.writeUtf8AsUtf16(appPackageName);
43         data.writeInt32(vulkanVersion);
44         data.writeInt32(static_cast<int32_t>(driver));
45         data.writeBool(isDriverLoaded);
46         data.writeInt64(driverLoadingTime);
47 
48         remote()->transact(BnGpuService::SET_GPU_STATS, data, &reply, IBinder::FLAG_ONEWAY);
49     }
50 
getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo> * outStats) const51     virtual status_t getGpuStatsGlobalInfo(std::vector<GpuStatsGlobalInfo>* outStats) const {
52         if (!outStats) return UNEXPECTED_NULL;
53 
54         Parcel data, reply;
55         status_t status;
56 
57         if ((status = data.writeInterfaceToken(IGpuService::getInterfaceDescriptor())) != OK)
58             return status;
59 
60         if ((status = remote()->transact(BnGpuService::GET_GPU_STATS_GLOBAL_INFO, data, &reply)) !=
61             OK)
62             return status;
63 
64         int32_t result = 0;
65         if ((status = reply.readInt32(&result)) != OK) return status;
66         if (result != OK) return result;
67 
68         outStats->clear();
69         return reply.readParcelableVector(outStats);
70     }
71 
getGpuStatsAppInfo(std::vector<GpuStatsAppInfo> * outStats) const72     virtual status_t getGpuStatsAppInfo(std::vector<GpuStatsAppInfo>* outStats) const {
73         if (!outStats) return UNEXPECTED_NULL;
74 
75         Parcel data, reply;
76         status_t status;
77 
78         if ((status = data.writeInterfaceToken(IGpuService::getInterfaceDescriptor())) != OK) {
79             return status;
80         }
81 
82         if ((status = remote()->transact(BnGpuService::GET_GPU_STATS_APP_INFO, data, &reply)) !=
83             OK) {
84             return status;
85         }
86 
87         int32_t result = 0;
88         if ((status = reply.readInt32(&result)) != OK) return status;
89         if (result != OK) return result;
90 
91         outStats->clear();
92         return reply.readParcelableVector(outStats);
93     }
94 
setCpuVulkanInUse(const std::string & appPackageName,const uint64_t driverVersionCode)95     virtual void setCpuVulkanInUse(const std::string& appPackageName,
96                                    const uint64_t driverVersionCode) {
97         Parcel data, reply;
98         data.writeInterfaceToken(IGpuService::getInterfaceDescriptor());
99 
100         data.writeUtf8AsUtf16(appPackageName);
101         data.writeUint64(driverVersionCode);
102 
103         remote()->transact(BnGpuService::SET_CPU_VULKAN_IN_USE, data, &reply, IBinder::FLAG_ONEWAY);
104     }
105 };
106 
107 IMPLEMENT_META_INTERFACE(GpuService, "android.graphicsenv.IGpuService");
108 
onTransact(uint32_t code,const Parcel & data,Parcel * reply,uint32_t flags)109 status_t BnGpuService::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
110                                   uint32_t flags) {
111     ALOGV("onTransact code[0x%X]", code);
112 
113     status_t status;
114     switch (code) {
115         case SET_GPU_STATS: {
116             CHECK_INTERFACE(IGpuService, data, reply);
117 
118             std::string driverPackageName;
119             if ((status = data.readUtf8FromUtf16(&driverPackageName)) != OK) return status;
120 
121             std::string driverVersionName;
122             if ((status = data.readUtf8FromUtf16(&driverVersionName)) != OK) return status;
123 
124             uint64_t driverVersionCode;
125             if ((status = data.readUint64(&driverVersionCode)) != OK) return status;
126 
127             int64_t driverBuildTime;
128             if ((status = data.readInt64(&driverBuildTime)) != OK) return status;
129 
130             std::string appPackageName;
131             if ((status = data.readUtf8FromUtf16(&appPackageName)) != OK) return status;
132 
133             int32_t vulkanVersion;
134             if ((status = data.readInt32(&vulkanVersion)) != OK) return status;
135 
136             int32_t driver;
137             if ((status = data.readInt32(&driver)) != OK) return status;
138 
139             bool isDriverLoaded;
140             if ((status = data.readBool(&isDriverLoaded)) != OK) return status;
141 
142             int64_t driverLoadingTime;
143             if ((status = data.readInt64(&driverLoadingTime)) != OK) return status;
144 
145             setGpuStats(driverPackageName, driverVersionName, driverVersionCode, driverBuildTime,
146                         appPackageName, vulkanVersion, static_cast<GraphicsEnv::Driver>(driver),
147                         isDriverLoaded, driverLoadingTime);
148 
149             return OK;
150         }
151         case GET_GPU_STATS_GLOBAL_INFO: {
152             CHECK_INTERFACE(IGpuService, data, reply);
153 
154             std::vector<GpuStatsGlobalInfo> stats;
155             const status_t result = getGpuStatsGlobalInfo(&stats);
156 
157             if ((status = reply->writeInt32(result)) != OK) return status;
158             if (result != OK) return result;
159 
160             if ((status = reply->writeParcelableVector(stats)) != OK) return status;
161 
162             return OK;
163         }
164         case GET_GPU_STATS_APP_INFO: {
165             CHECK_INTERFACE(IGpuService, data, reply);
166 
167             std::vector<GpuStatsAppInfo> stats;
168             const status_t result = getGpuStatsAppInfo(&stats);
169 
170             if ((status = reply->writeInt32(result)) != OK) return status;
171             if (result != OK) return result;
172 
173             if ((status = reply->writeParcelableVector(stats)) != OK) return status;
174 
175             return OK;
176         }
177         case SET_CPU_VULKAN_IN_USE: {
178             CHECK_INTERFACE(IGpuService, data, reply);
179 
180             std::string appPackageName;
181             if ((status = data.readUtf8FromUtf16(&appPackageName)) != OK) return status;
182 
183             uint64_t driverVersionCode;
184             if ((status = data.readUint64(&driverVersionCode)) != OK) return status;
185 
186             setCpuVulkanInUse(appPackageName, driverVersionCode);
187 
188             return OK;
189         }
190         case SHELL_COMMAND_TRANSACTION: {
191             int in = data.readFileDescriptor();
192             int out = data.readFileDescriptor();
193             int err = data.readFileDescriptor();
194 
195             std::vector<String16> args;
196             data.readString16Vector(&args);
197 
198             sp<IBinder> unusedCallback;
199             if ((status = data.readNullableStrongBinder(&unusedCallback)) != OK) return status;
200 
201             sp<IResultReceiver> resultReceiver;
202             if ((status = data.readNullableStrongBinder(&resultReceiver)) != OK) return status;
203 
204             status = shellCommand(in, out, err, args);
205             if (resultReceiver != nullptr) resultReceiver->send(status);
206 
207             return OK;
208         }
209         default:
210             return BBinder::onTransact(code, data, reply, flags);
211     }
212 }
213 
214 } // namespace android
215