blob: 71052fb56323e65bde097a8cb7fdb52a44f8d219 [file] [log] [blame]
Jesse Hallfc038bd2016-03-26 22:20:22 -07001/*
2 * Copyright 2016 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#include "GpuService.h"
18
Jesse Hallc0b15772016-12-20 14:47:45 -080019#include <binder/IResultReceiver.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070020#include <binder/Parcel.h>
Jesse Hall8b0d55e2016-03-31 19:29:36 -070021#include <utils/String8.h>
22#include <vkjson.h>
Jesse Hallfc038bd2016-03-26 22:20:22 -070023
24namespace android {
25
26// ----------------------------------------------------------------------------
27
28class BpGpuService : public BpInterface<IGpuService>
29{
30public:
Chih-Hung Hsiehc4067912016-05-03 14:03:27 -070031 explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
Jesse Hallfc038bd2016-03-26 22:20:22 -070032};
33
34IMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService");
35
36status_t BnGpuService::onTransact(uint32_t code, const Parcel& data,
37 Parcel* reply, uint32_t flags)
38{
Jesse Hallc0b15772016-12-20 14:47:45 -080039 status_t status;
Jesse Hallfc038bd2016-03-26 22:20:22 -070040 switch (code) {
41 case SHELL_COMMAND_TRANSACTION: {
42 int in = data.readFileDescriptor();
43 int out = data.readFileDescriptor();
44 int err = data.readFileDescriptor();
45 int argc = data.readInt32();
46 Vector<String16> args;
47 for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
48 args.add(data.readString16());
49 }
Jesse Hallc0b15772016-12-20 14:47:45 -080050 sp<IBinder> unusedCallback;
51 sp<IResultReceiver> resultReceiver;
52 if ((status = data.readNullableStrongBinder(&unusedCallback)) != OK)
53 return status;
54 if ((status = data.readNullableStrongBinder(&resultReceiver)) != OK)
55 return status;
56 status = shellCommand(in, out, err, args);
57 if (resultReceiver != nullptr)
58 resultReceiver->send(status);
59 return OK;
Jesse Hallfc038bd2016-03-26 22:20:22 -070060 }
61
62 default:
63 return BBinder::onTransact(code, data, reply, flags);
64 }
65}
66
67// ----------------------------------------------------------------------------
68
Jesse Hall8b0d55e2016-03-31 19:29:36 -070069namespace {
70 status_t cmd_help(int out);
71 status_t cmd_vkjson(int out, int err);
72}
73
Jesse Hallfc038bd2016-03-26 22:20:22 -070074const char* const GpuService::SERVICE_NAME = "gpu";
75
76GpuService::GpuService() {}
77
Jesse Hall8b0d55e2016-03-31 19:29:36 -070078status_t GpuService::shellCommand(int /*in*/, int out, int err,
79 Vector<String16>& args)
Jesse Hallfc038bd2016-03-26 22:20:22 -070080{
Jesse Hall8b0d55e2016-03-31 19:29:36 -070081 ALOGV("GpuService::shellCommand");
82 for (size_t i = 0, n = args.size(); i < n; i++)
83 ALOGV(" arg[%zu]: '%s'", i, String8(args[i]).string());
84
Jesse Hall3e26b132016-12-20 14:31:28 -080085 if (args.size() >= 1) {
86 if (args[0] == String16("vkjson"))
87 return cmd_vkjson(out, err);
88 if (args[0] == String16("help"))
89 return cmd_help(out);
90 }
91 // no command, or unrecognized command
92 cmd_help(err);
93 return BAD_VALUE;
Jesse Hallfc038bd2016-03-26 22:20:22 -070094}
95
Jesse Hall8b0d55e2016-03-31 19:29:36 -070096// ----------------------------------------------------------------------------
97
98namespace {
99
100status_t cmd_help(int out) {
101 FILE* outs = fdopen(out, "w");
102 if (!outs) {
103 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno),
104 errno);
105 return BAD_VALUE;
106 }
107 fprintf(outs,
108 "GPU Service commands:\n"
Jesse Hall3841bf42016-06-12 15:08:28 -0700109 " vkjson dump Vulkan properties as JSON\n");
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700110 fclose(outs);
111 return NO_ERROR;
112}
113
Jesse Hall3841bf42016-06-12 15:08:28 -0700114void vkjsonPrint(FILE* out) {
115 std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
116 fwrite(json.data(), 1, json.size(), out);
117 fputc('\n', out);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700118}
119
Jesse Hall3841bf42016-06-12 15:08:28 -0700120status_t cmd_vkjson(int out, int /*err*/) {
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700121 FILE* outs = fdopen(out, "w");
122 if (!outs) {
Jesse Hall3841bf42016-06-12 15:08:28 -0700123 int errnum = errno;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700124 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
125 return -errnum;
126 }
Jesse Hall3841bf42016-06-12 15:08:28 -0700127 vkjsonPrint(outs);
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700128 fclose(outs);
Jesse Hall3841bf42016-06-12 15:08:28 -0700129 return NO_ERROR;
Jesse Hall8b0d55e2016-03-31 19:29:36 -0700130}
131
132} // anonymous namespace
133
Jesse Hallfc038bd2016-03-26 22:20:22 -0700134} // namespace android