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 | |
| 19 | #include <binder/Parcel.h> |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 20 | #include <utils/String8.h> |
| 21 | #include <vkjson.h> |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // ---------------------------------------------------------------------------- |
| 26 | |
| 27 | class BpGpuService : public BpInterface<IGpuService> |
| 28 | { |
| 29 | public: |
Chih-Hung Hsieh | c406791 | 2016-05-03 14:03:27 -0700 | [diff] [blame^] | 30 | explicit BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {} |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | IMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService"); |
| 34 | |
| 35 | status_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 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 58 | namespace { |
| 59 | status_t cmd_help(int out); |
| 60 | status_t cmd_vkjson(int out, int err); |
| 61 | } |
| 62 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 63 | const char* const GpuService::SERVICE_NAME = "gpu"; |
| 64 | |
| 65 | GpuService::GpuService() {} |
| 66 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 67 | status_t GpuService::shellCommand(int /*in*/, int out, int err, |
| 68 | Vector<String16>& args) |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 69 | { |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 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[0] == String16("vkjson")) |
| 75 | return cmd_vkjson(out, err); |
| 76 | else if (args[0] == String16("help")) |
| 77 | return cmd_help(out); |
| 78 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 79 | return NO_ERROR; |
| 80 | } |
| 81 | |
Jesse Hall | 8b0d55e | 2016-03-31 19:29:36 -0700 | [diff] [blame] | 82 | // ---------------------------------------------------------------------------- |
| 83 | |
| 84 | namespace { |
| 85 | |
| 86 | status_t cmd_help(int out) { |
| 87 | FILE* outs = fdopen(out, "w"); |
| 88 | if (!outs) { |
| 89 | ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno), |
| 90 | errno); |
| 91 | return BAD_VALUE; |
| 92 | } |
| 93 | fprintf(outs, |
| 94 | "GPU Service commands:\n" |
| 95 | " vkjson dump Vulkan device capabilities as JSON\n"); |
| 96 | fclose(outs); |
| 97 | return NO_ERROR; |
| 98 | } |
| 99 | |
| 100 | VkResult vkjsonPrint(FILE* out, FILE* err) { |
| 101 | VkResult result; |
| 102 | |
| 103 | const VkApplicationInfo app_info = { |
| 104 | VK_STRUCTURE_TYPE_APPLICATION_INFO, nullptr, |
| 105 | "vkjson", 1, /* app name, version */ |
| 106 | "", 0, /* engine name, version */ |
| 107 | VK_API_VERSION |
| 108 | }; |
| 109 | const VkInstanceCreateInfo instance_info = { |
| 110 | VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, nullptr, |
| 111 | 0, /* flags */ |
| 112 | &app_info, |
| 113 | 0, nullptr, /* layers */ |
| 114 | 0, nullptr, /* extensions */ |
| 115 | }; |
| 116 | VkInstance instance; |
| 117 | result = vkCreateInstance(&instance_info, nullptr, &instance); |
| 118 | if (result != VK_SUCCESS) { |
| 119 | fprintf(err, "vkCreateInstance failed: %d\n", result); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | uint32_t ngpu = 0; |
| 124 | result = vkEnumeratePhysicalDevices(instance, &ngpu, nullptr); |
| 125 | if (result != VK_SUCCESS) { |
| 126 | fprintf(err, "vkEnumeratePhysicalDevices failed: %d\n", result); |
| 127 | return result; |
| 128 | } |
| 129 | std::vector<VkPhysicalDevice> gpus(ngpu, VK_NULL_HANDLE); |
| 130 | result = vkEnumeratePhysicalDevices(instance, &ngpu, gpus.data()); |
| 131 | if (result != VK_SUCCESS) { |
| 132 | fprintf(err, "vkEnumeratePhysicalDevices failed: %d\n", result); |
| 133 | return result; |
| 134 | } |
| 135 | |
| 136 | for (size_t i = 0, n = gpus.size(); i < n; i++) { |
| 137 | auto props = VkJsonGetAllProperties(gpus[i]); |
| 138 | std::string json = VkJsonAllPropertiesToJson(props); |
| 139 | fwrite(json.data(), 1, json.size(), out); |
| 140 | if (i < n - 1) |
| 141 | fputc(',', out); |
| 142 | fputc('\n', out); |
| 143 | } |
| 144 | |
| 145 | vkDestroyInstance(instance, nullptr); |
| 146 | |
| 147 | return VK_SUCCESS; |
| 148 | } |
| 149 | |
| 150 | status_t cmd_vkjson(int out, int err) { |
| 151 | int errnum; |
| 152 | FILE* outs = fdopen(out, "w"); |
| 153 | if (!outs) { |
| 154 | errnum = errno; |
| 155 | ALOGE("vkjson: failed to create output stream: %s", strerror(errnum)); |
| 156 | return -errnum; |
| 157 | } |
| 158 | FILE* errs = fdopen(err, "w"); |
| 159 | if (!errs) { |
| 160 | errnum = errno; |
| 161 | ALOGE("vkjson: failed to create error stream: %s", strerror(errnum)); |
| 162 | fclose(outs); |
| 163 | return -errnum; |
| 164 | } |
| 165 | fprintf(outs, "[\n"); |
| 166 | VkResult result = vkjsonPrint(outs, errs); |
| 167 | fprintf(outs, "]\n"); |
| 168 | fclose(errs); |
| 169 | fclose(outs); |
| 170 | return result >= 0 ? NO_ERROR : UNKNOWN_ERROR; |
| 171 | } |
| 172 | |
| 173 | } // anonymous namespace |
| 174 | |
Jesse Hall | fc038bd | 2016-03-26 22:20:22 -0700 | [diff] [blame] | 175 | } // namespace android |