1fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall/*
2fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * Copyright 2016 The Android Open Source Project
3fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall *
4fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * Licensed under the Apache License, Version 2.0 (the "License");
5fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * you may not use this file except in compliance with the License.
6fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * You may obtain a copy of the License at
7fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall *
8fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall *      http://www.apache.org/licenses/LICENSE-2.0
9fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall *
10fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * Unless required by applicable law or agreed to in writing, software
11fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * distributed under the License is distributed on an "AS IS" BASIS,
12fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * See the License for the specific language governing permissions and
14fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall * limitations under the License.
15fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall */
16fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
17fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall#include "GpuService.h"
18fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
19c0b1577538390f75de137e030bc22ff415e07683Jesse Hall#include <binder/IResultReceiver.h>
20fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall#include <binder/Parcel.h>
218b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall#include <utils/String8.h>
228b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall#include <vkjson.h>
23fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
24fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hallnamespace android {
25fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
26fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall// ----------------------------------------------------------------------------
27fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
28fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hallclass BpGpuService : public BpInterface<IGpuService>
29fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall{
30fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hallpublic:
31c406791ead6d864ec693ad01a80c5f471bdc2a7aChih-Hung Hsieh    explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
32fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall};
33fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
34fc038bd8fc77998a436d43027919f4500c4291e6Jesse HallIMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService");
35fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
36fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hallstatus_t BnGpuService::onTransact(uint32_t code, const Parcel& data,
37fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        Parcel* reply, uint32_t flags)
38fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall{
39c0b1577538390f75de137e030bc22ff415e07683Jesse Hall    status_t status;
40fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    switch (code) {
41fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    case SHELL_COMMAND_TRANSACTION: {
42fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        int in = data.readFileDescriptor();
43fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        int out = data.readFileDescriptor();
44fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        int err = data.readFileDescriptor();
45fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        int argc = data.readInt32();
46fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        Vector<String16> args;
47fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        for (int i = 0; i < argc && data.dataAvail() > 0; i++) {
48fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall           args.add(data.readString16());
49fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        }
50c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        sp<IBinder> unusedCallback;
51c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        sp<IResultReceiver> resultReceiver;
52c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        if ((status = data.readNullableStrongBinder(&unusedCallback)) != OK)
53c0b1577538390f75de137e030bc22ff415e07683Jesse Hall            return status;
54c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        if ((status = data.readNullableStrongBinder(&resultReceiver)) != OK)
55c0b1577538390f75de137e030bc22ff415e07683Jesse Hall            return status;
56c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        status = shellCommand(in, out, err, args);
57c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        if (resultReceiver != nullptr)
58c0b1577538390f75de137e030bc22ff415e07683Jesse Hall            resultReceiver->send(status);
59c0b1577538390f75de137e030bc22ff415e07683Jesse Hall        return OK;
60fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    }
61fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
62fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    default:
63fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall        return BBinder::onTransact(code, data, reply, flags);
64fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    }
65fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall}
66fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
67fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall// ----------------------------------------------------------------------------
68fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
698b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hallnamespace {
708b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    status_t cmd_help(int out);
718b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    status_t cmd_vkjson(int out, int err);
728b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall}
738b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
74fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hallconst char* const GpuService::SERVICE_NAME = "gpu";
75fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
76fc038bd8fc77998a436d43027919f4500c4291e6Jesse HallGpuService::GpuService() {}
77fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
788b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hallstatus_t GpuService::shellCommand(int /*in*/, int out, int err,
798b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        Vector<String16>& args)
80fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall{
818b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    ALOGV("GpuService::shellCommand");
828b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    for (size_t i = 0, n = args.size(); i < n; i++)
838b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        ALOGV("  arg[%zu]: '%s'", i, String8(args[i]).string());
848b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
853e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall    if (args.size() >= 1) {
863e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall        if (args[0] == String16("vkjson"))
873e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall            return cmd_vkjson(out, err);
883e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall        if (args[0] == String16("help"))
893e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall            return cmd_help(out);
903e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall    }
913e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall    // no command, or unrecognized command
923e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall    cmd_help(err);
933e26b1363ff8b39a472fc2d4fe9ee59ee818b977Jesse Hall    return BAD_VALUE;
948b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall}
958b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
968b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall// ----------------------------------------------------------------------------
978b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
988b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hallnamespace {
998b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
1008b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hallstatus_t cmd_help(int out) {
1018b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    FILE* outs = fdopen(out, "w");
1028b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    if (!outs) {
1038b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno),
1048b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall            errno);
1058b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        return BAD_VALUE;
1068b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    }
1078b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    fprintf(outs,
1088b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        "GPU Service commands:\n"
1093841bf4482d33285f343162639df316237a38b1dJesse Hall        "  vkjson   dump Vulkan properties as JSON\n");
1108b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    fclose(outs);
111fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall    return NO_ERROR;
112fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall}
113fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall
1143841bf4482d33285f343162639df316237a38b1dJesse Hallvoid vkjsonPrint(FILE* out) {
1153841bf4482d33285f343162639df316237a38b1dJesse Hall    std::string json = VkJsonInstanceToJson(VkJsonGetInstance());
1163841bf4482d33285f343162639df316237a38b1dJesse Hall    fwrite(json.data(), 1, json.size(), out);
1173841bf4482d33285f343162639df316237a38b1dJesse Hall    fputc('\n', out);
1188b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall}
1198b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
1203841bf4482d33285f343162639df316237a38b1dJesse Hallstatus_t cmd_vkjson(int out, int /*err*/) {
1218b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    FILE* outs = fdopen(out, "w");
1228b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    if (!outs) {
1233841bf4482d33285f343162639df316237a38b1dJesse Hall        int errnum = errno;
1248b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
1258b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall        return -errnum;
1268b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    }
1273841bf4482d33285f343162639df316237a38b1dJesse Hall    vkjsonPrint(outs);
1288b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall    fclose(outs);
1293841bf4482d33285f343162639df316237a38b1dJesse Hall    return NO_ERROR;
1308b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall}
1318b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
1328b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall} // anonymous namespace
1338b0d55e3652e68e9e2b0f4314c1eaeadc49cc2d0Jesse Hall
134fc038bd8fc77998a436d43027919f4500c4291e6Jesse Hall} // namespace android
135