1/*
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
19#include <binder/IResultReceiver.h>
20#include <binder/Parcel.h>
21#include <utils/String8.h>
22#include <vkjson.h>
23
24namespace android {
25
26// ----------------------------------------------------------------------------
27
28class BpGpuService : public BpInterface<IGpuService>
29{
30public:
31    explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
32};
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{
39    status_t status;
40    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        }
50        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;
60    }
61
62    default:
63        return BBinder::onTransact(code, data, reply, flags);
64    }
65}
66
67// ----------------------------------------------------------------------------
68
69namespace {
70    status_t cmd_help(int out);
71    status_t cmd_vkjson(int out, int err);
72}
73
74const char* const GpuService::SERVICE_NAME = "gpu";
75
76GpuService::GpuService() {}
77
78status_t GpuService::shellCommand(int /*in*/, int out, int err,
79        Vector<String16>& args)
80{
81    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
85    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;
94}
95
96// ----------------------------------------------------------------------------
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"
109        "  vkjson   dump Vulkan properties as JSON\n");
110    fclose(outs);
111    return NO_ERROR;
112}
113
114void vkjsonPrint(FILE* out) {
115    std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
116    fwrite(json.data(), 1, json.size(), out);
117    fputc('\n', out);
118}
119
120status_t cmd_vkjson(int out, int /*err*/) {
121    FILE* outs = fdopen(out, "w");
122    if (!outs) {
123        int errnum = errno;
124        ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
125        return -errnum;
126    }
127    vkjsonPrint(outs);
128    fclose(outs);
129    return NO_ERROR;
130}
131
132} // anonymous namespace
133
134} // namespace android
135