1 /*
2 * Copyright 2023 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 <log/log.h>
18
19 #include "RutabagaLayer.h"
20 #include "RutabagaVirtGpu.h"
21
22 namespace gfxstream {
23
RutabagaVirtGpuDevice(uint32_t contextId,VirtGpuCapset capset)24 RutabagaVirtGpuDevice::RutabagaVirtGpuDevice(uint32_t contextId, VirtGpuCapset capset)
25 : VirtGpuDevice(capset), mContextId(contextId), mCapset(capset) {}
26
~RutabagaVirtGpuDevice()27 RutabagaVirtGpuDevice::~RutabagaVirtGpuDevice() {
28 EmulatedVirtioGpu::Get().DestroyContext(mContextId);
29 EmulatedVirtioGpu::Reset();
30 }
31
getDeviceHandle()32 int64_t RutabagaVirtGpuDevice::getDeviceHandle() { return -1; }
33
getCaps()34 VirtGpuCaps RutabagaVirtGpuDevice::getCaps() { return EmulatedVirtioGpu::Get().GetCaps(mCapset); }
35
createBlob(const struct VirtGpuCreateBlob & blobCreate)36 VirtGpuBlobPtr RutabagaVirtGpuDevice::createBlob(const struct VirtGpuCreateBlob& blobCreate) {
37 const auto resourceIdOpt = EmulatedVirtioGpu::Get().CreateBlob(mContextId, blobCreate);
38 if (!resourceIdOpt) {
39 return nullptr;
40 }
41
42 return VirtGpuBlobPtr(new RutabagaVirtGpuResource(
43 *resourceIdOpt, RutabagaVirtGpuResource::ResourceType::kBlob, mContextId));
44 }
45
createVirglBlob(uint32_t width,uint32_t height,uint32_t virglFormat)46 VirtGpuBlobPtr RutabagaVirtGpuDevice::createVirglBlob(uint32_t width, uint32_t height,
47 uint32_t virglFormat) {
48 const auto resourceIdOpt =
49 EmulatedVirtioGpu::Get().CreateVirglBlob(mContextId, width, height, virglFormat);
50 if (!resourceIdOpt) {
51 return nullptr;
52 }
53
54 return VirtGpuBlobPtr(new RutabagaVirtGpuResource(
55 *resourceIdOpt, RutabagaVirtGpuResource::ResourceType::kPipe, mContextId));
56 }
57
execBuffer(struct VirtGpuExecBuffer & execbuffer,const VirtGpuBlob * blob)58 int RutabagaVirtGpuDevice::execBuffer(struct VirtGpuExecBuffer& execbuffer,
59 const VirtGpuBlob* blob) {
60 std::optional<uint32_t> blobResourceId;
61 if (blob) {
62 blobResourceId = blob->getResourceHandle();
63 }
64 return EmulatedVirtioGpu::Get().ExecBuffer(mContextId, execbuffer, blobResourceId);
65 }
66
importBlob(const struct VirtGpuExternalHandle &)67 VirtGpuBlobPtr RutabagaVirtGpuDevice::importBlob(const struct VirtGpuExternalHandle&) {
68 ALOGE("Unimplemented %s", __FUNCTION__);
69 return nullptr;
70 }
71
72 } // namespace gfxstream
73
createPlatformVirtGpuDevice(enum VirtGpuCapset capset,int)74 VirtGpuDevice* createPlatformVirtGpuDevice(enum VirtGpuCapset capset, int) {
75 const auto contextIdOp = gfxstream::EmulatedVirtioGpu::Get().CreateContext();
76 if (!contextIdOp) {
77 ALOGE("Failed to create RutabagaVirtGpuDevice: failed to create context.");
78 return nullptr;
79 }
80 return new gfxstream::RutabagaVirtGpuDevice(*contextIdOp, capset);
81 }
82