Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 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 | |
Jesse Hall | c0b1577 | 2016-12-20 14:47:45 -0800 | [diff] [blame^] | 19 | #include <binder/IResultReceiver.h> |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 20 | #include <binder/Parcel.h> |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> |
| 22 | #include <vkjson.h> |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | // ---------------------------------------------------------------------------- |
| 27 | |
| 28 | class BpGpuService : public BpInterface<IGpuService> |
| 29 | { |
| 30 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame] | 31 | explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {} |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | IMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService"); |
| 35 | |
| 36 | status_t BnGpuService::onTransact(uint32_t code, const Parcel& data, |
| 37 | Parcel* reply, uint32_t flags) |
| 38 | { |
Jesse Hall | c0b1577 | 2016-12-20 14:47:45 -0800 | [diff] [blame^] | 39 | status_t status; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 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 | } |
Jesse Hall | c0b1577 | 2016-12-20 14:47:45 -0800 | [diff] [blame^] | 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; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | default: |
| 63 | return BBinder::onTransact(code, data, reply, flags); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | // ---------------------------------------------------------------------------- |
| 68 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 69 | namespace { |
| 70 | status_t cmd_help(int out); |
| 71 | status_t cmd_vkjson(int out, int err); |
| 72 | } |
| 73 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 74 | const char* const GpuService::SERVICE_NAME = "gpu"; |
| 75 | |
| 76 | GpuService::GpuService() {} |
| 77 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 78 | status_t GpuService::shellCommand(int /*in*/, int out, int err, |
| 79 | Vector<String16>& args) |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 80 | { |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 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 | |
Jesse Hall | 3e26b13 | 2016-12-20 14:31:28 -0800 | [diff] [blame] | 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; |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 96 | // ---------------------------------------------------------------------------- |
| 97 | |
| 98 | namespace { |
| 99 | |
| 100 | status_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 Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 109 | " vkjson dump Vulkan properties as JSON\n"); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 110 | fclose(outs); |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 114 | void vkjsonPrint(FILE* out) { |
| 115 | std::string json = VkJsonInstanceToJson(VkJsonGetInstance()); |
| 116 | fwrite(json.data(), 1, json.size(), out); |
| 117 | fputc('\n', out); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 120 | status_t cmd_vkjson(int out, int /*err*/) { |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 121 | FILE* outs = fdopen(out, "w"); |
| 122 | if (!outs) { |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 123 | int errnum = errno; |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 124 | ALOGE("vkjson: failed to create output stream: %s", strerror(errnum)); |
| 125 | return -errnum; |
| 126 | } |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 127 | vkjsonPrint(outs); |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 128 | fclose(outs); |
Jesse Hall | 3841bf4 | 2016-06-12 15:08:28 -0700 | [diff] [blame] | 129 | return NO_ERROR; |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | } // anonymous namespace |
| 133 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 134 | } // namespace android |