GpuService.cpp revision 3e26b1363ff8b39a472fc2d4fe9ee59ee818b977
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/Parcel.h>
20#include <utils/String8.h>
21#include <vkjson.h>
22
23namespace android {
24
25// ----------------------------------------------------------------------------
26
27class BpGpuService : public BpInterface<IGpuService>
28{
29public:
30    explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
31};
32
33IMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService");
34
35status_t BnGpuService::onTransact(uint32_t code, const Parcel& data,
36        Parcel* reply, uint32_t flags)
37{
38    switch (code) {
39    case SHELL_COMMAND_TRANSACTION: {
40        int in = data.readFileDescriptor();
41        int out = data.readFileDescriptor();
42        int err = data.readFileDescriptor();
43        int argc = data.readInt32();
44        Vector<String16> args;
45        for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
46           args.add(data.readString16());
47        }
48        return shellCommand(in, out, err, args);
49    }
50
51    default:
52        return BBinder::onTransact(code, data, reply, flags);
53    }
54}
55
56// ----------------------------------------------------------------------------
57
58namespace {
59    status_t cmd_help(int out);
60    status_t cmd_vkjson(int out, int err);
61}
62
63const char* const GpuService::SERVICE_NAME = "gpu";
64
65GpuService::GpuService() {}
66
67status_t GpuService::shellCommand(int /*in*/, int out, int err,
68        Vector<String16>& args)
69{
70    ALOGV("GpuService::shellCommand");
71    for (size_t i = 0, n = args.size(); i < n; i++)
72        ALOGV("  arg[%zu]: '%s'", i, String8(args[i]).string());
73
74    if (args.size() >= 1) {
75        if (args[0] == String16("vkjson"))
76            return cmd_vkjson(out, err);
77        if (args[0] == String16("help"))
78            return cmd_help(out);
79    }
80    // no command, or unrecognized command
81    cmd_help(err);
82    return BAD_VALUE;
83}
84
85// ----------------------------------------------------------------------------
86
87namespace {
88
89status_t cmd_help(int out) {
90    FILE* outs = fdopen(out, "w");
91    if (!outs) {
92        ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno),
93            errno);
94        return BAD_VALUE;
95    }
96    fprintf(outs,
97        "GPU Service commands:\n"
98        "  vkjson   dump Vulkan properties as JSON\n");
99    fclose(outs);
100    return NO_ERROR;
101}
102
103void vkjsonPrint(FILE* out) {
104    std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
105    fwrite(json.data(), 1, json.size(), out);
106    fputc('\n', out);
107}
108
109status_t cmd_vkjson(int out, int /*err*/) {
110    FILE* outs = fdopen(out, "w");
111    if (!outs) {
112        int errnum = errno;
113        ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
114        return -errnum;
115    }
116    vkjsonPrint(outs);
117    fclose(outs);
118    return NO_ERROR;
119}
120
121} // anonymous namespace
122
123} // namespace android
124