2 * Copyright 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "GpuService.h"
19 #include <binder/Parcel.h>
20 #include <utils/String8.h>
25 // ----------------------------------------------------------------------------
27 class BpGpuService : public BpInterface<IGpuService>
30 BpGpuService(const sp<IBinder>& impl) : BpInterface<IGpuService>(impl) {}
33 IMPLEMENT_META_INTERFACE(GpuService, "android.ui.IGpuService");
35 status_t BnGpuService::onTransact(uint32_t code, const Parcel& data,
36 Parcel* reply, uint32_t flags)
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());
48 return shellCommand(in, out, err, args);
52 return BBinder::onTransact(code, data, reply, flags);
56 // ----------------------------------------------------------------------------
59 status_t cmd_help(int out);
60 status_t cmd_vkjson(int out, int err);
63 const char* const GpuService::SERVICE_NAME = "gpu";
65 GpuService::GpuService() {}
67 status_t GpuService::shellCommand(int /*in*/, int out, int err,
68 Vector<String16>& args)
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());
74 if (args[0] == String16("vkjson"))
75 return cmd_vkjson(out, err);
76 else if (args[0] == String16("help"))
82 // ----------------------------------------------------------------------------
86 status_t cmd_help(int out) {
87 FILE* outs = fdopen(out, "w");
89 ALOGE("vkjson: failed to create out stream: %s (%d)", strerror(errno),
94 "GPU Service commands:\n"
95 " vkjson dump Vulkan device capabilities as JSON\n");
100 VkResult vkjsonPrint(FILE* out, FILE* err) {
103 const VkApplicationInfo app_info = {
104 VK_STRUCTURE_TYPE_APPLICATION_INFO, nullptr,
105 "vkjson", 1, /* app name, version */
106 "", 0, /* engine name, version */
109 const VkInstanceCreateInfo instance_info = {
110 VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, nullptr,
113 0, nullptr, /* layers */
114 0, nullptr, /* extensions */
117 result = vkCreateInstance(&instance_info, nullptr, &instance);
118 if (result != VK_SUCCESS) {
119 fprintf(err, "vkCreateInstance failed: %d\n", result);
124 result = vkEnumeratePhysicalDevices(instance, &ngpu, nullptr);
125 if (result != VK_SUCCESS) {
126 fprintf(err, "vkEnumeratePhysicalDevices failed: %d\n", result);
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);
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);
145 vkDestroyInstance(instance, nullptr);
150 status_t cmd_vkjson(int out, int err) {
152 FILE* outs = fdopen(out, "w");
155 ALOGE("vkjson: failed to create output stream: %s", strerror(errnum));
158 FILE* errs = fdopen(err, "w");
161 ALOGE("vkjson: failed to create error stream: %s", strerror(errnum));
165 fprintf(outs, "[\n");
166 VkResult result = vkjsonPrint(outs, errs);
167 fprintf(outs, "]\n");
170 return result >= 0 ? NO_ERROR : UNKNOWN_ERROR;
173 } // anonymous namespace
175 } // namespace android