Debian Codegraph @

src/gfxstream/guest/platform/kumquat/VirtGpuKumquatBlob.cpp

raw
1 /*
2 * Copyright 2022 Google
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include <cerrno>
10 #include <cstring>
11
12 #include "GfxStreamVulkanMapper.h"
13 #include "VirtGpuKumquat.h"
14 #include "util/log.h"
15
16 VirtGpuKumquatResource::VirtGpuKumquatResource(struct virtgpu_kumquat* virtGpu, uint32_t blobHandle,
17 uint32_t resourceHandle, uint64_t size)
18 : mVirtGpu(virtGpu), mBlobHandle(blobHandle), mResourceHandle(resourceHandle), mSize(size) {}
19
20 VirtGpuKumquatResource::~VirtGpuKumquatResource() {
21 struct drm_kumquat_resource_unref unref {
22 .bo_handle = mBlobHandle, .pad = 0,
23 };
24
25 int ret = virtgpu_kumquat_resource_unref(mVirtGpu, &unref);
26 if (ret) {
27 mesa_loge("Closed failed with : [%s, blobHandle %u, resourceHandle: %u]", strerror(errno),
28 mBlobHandle, mResourceHandle);
29 }
30 }
31
32 uint32_t VirtGpuKumquatResource::getBlobHandle() const { return mBlobHandle; }
33
34 uint32_t VirtGpuKumquatResource::getResourceHandle() const { return mResourceHandle; }
35
36 uint64_t VirtGpuKumquatResource::getSize() const { return mSize; }
37
38 VirtGpuResourceMappingPtr VirtGpuKumquatResource::createMapping() {
39 int ret;
40 struct drm_kumquat_resource_info info = {};
41
42 struct drm_kumquat_map map {
43 .bo_handle = mBlobHandle, .ptr = NULL, .size = mSize,
44 };
45
46 info.bo_handle = mBlobHandle;
47 ret = virtgpu_kumquat_resource_info(mVirtGpu, &info);
48
49 if (info.vulkan_info.device_id.device_uuid[0]) {
50 struct drm_kumquat_resource_export resource_export = {};
51 struct DeviceId deviceId = {};
52
53 resource_export.bo_handle = mBlobHandle;
54
55 ret = virtgpu_kumquat_resource_export(mVirtGpu, &resource_export);
56 if (ret) {
57 mesa_loge("External memory export from kumquat failed: %s", strerror(errno));
58 return nullptr;
59 }
60
61 memcpy(&deviceId, &info.vulkan_info.device_id, sizeof(struct DeviceId));
62 std::optional<DeviceId> deviceIdOpt{deviceId};
63 auto mapper = GfxStreamVulkanMapper::getInstance(deviceIdOpt);
64 struct VulkanMapperData mapData = {0};
65
66 mapData.handle = resource_export.os_handle;
67 mapData.handleType = resource_export.handle_type;
68 mapData.memoryIdx = info.vulkan_info.memory_idx;
69 mapData.size = mSize;
70
71 ret = mapper->map(&mapData);
72 if (ret < 0) {
73 mesa_loge("Mapping failed with %s for resource %u blob %u", strerror(errno),
74 mResourceHandle, mBlobHandle);
75 return nullptr;
76 }
77
78 return std::make_shared<VirtGpuKumquatResourceMapping>(shared_from_this(), mVirtGpu,
79 mapData, mSize);
80 } else {
81 ret = virtgpu_kumquat_resource_map(mVirtGpu, &map);
82 if (ret < 0) {
83 mesa_loge("Mapping failed with %s for resource %u blob %u", strerror(errno),
84 mResourceHandle, mBlobHandle);
85 return nullptr;
86 }
87
88 return std::make_shared<VirtGpuKumquatResourceMapping>(shared_from_this(), mVirtGpu,
89 (uint8_t*)map.ptr, mSize);
90 }
91 }
92
93 int VirtGpuKumquatResource::exportBlob(struct VirtGpuExternalHandle& handle) {
94 int ret;
95 struct drm_kumquat_resource_export exp = {0};
96
97 exp.bo_handle = mBlobHandle;
98
99 ret = virtgpu_kumquat_resource_export(mVirtGpu, &exp);
100 if (ret) {
101 mesa_loge("Failed to export blob with %s", strerror(errno));
102 return ret;
103 }
104
105 handle.osHandle = static_cast<int64_t>(exp.os_handle);
106 handle.type = static_cast<VirtGpuHandleType>(exp.handle_type);
107 return 0;
108 }
109
110 int VirtGpuKumquatResource::wait() {
111 int ret;
112 struct drm_kumquat_wait wait = {
113 .handle = mBlobHandle,
114 .flags = 0,
115 };
116
117 ret = virtgpu_kumquat_wait(mVirtGpu, &wait);
118 if (ret < 0) {
119 mesa_loge("Wait failed with %s", strerror(errno));
120 return ret;
121 }
122
123 return 0;
124 }
125
126 int VirtGpuKumquatResource::transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
127 int ret;
128 struct drm_kumquat_transfer_to_host xfer = {0};
129
130 xfer.box.x = x;
131 xfer.box.y = y;
132 xfer.box.w = w;
133 xfer.box.h = h;
134 xfer.box.d = 1;
135 xfer.bo_handle = mBlobHandle;
136
137 ret = virtgpu_kumquat_transfer_to_host(mVirtGpu, &xfer);
138 if (ret < 0) {
139 mesa_loge("Transfer to host failed with %s", strerror(errno));
140 return ret;
141 }
142
143 return 0;
144 }
145
146 int VirtGpuKumquatResource::transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
147 int ret;
148 struct drm_kumquat_transfer_from_host xfer = {0};
149
150 xfer.box.x = x;
151 xfer.box.y = y;
152 xfer.box.w = w;
153 xfer.box.h = h;
154 xfer.box.d = 1;
155 xfer.bo_handle = mBlobHandle;
156
157 ret = virtgpu_kumquat_transfer_from_host(mVirtGpu, &xfer);
158 if (ret < 0) {
159 mesa_loge("Transfer from host failed with %s", strerror(errno));
160 return ret;
161 }
162
163 return 0;
164 }