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 #pragma once
18 
19 #include <memory>
20 
21 #include "VirtGpu.h"
22 
23 namespace gfxstream {
24 
25 // Virtio GPU abstraction that directly runs a host render server.
26 
27 class RutabagaVirtGpuDevice;
28 
29 class RutabagaVirtGpuResourceMapping : public VirtGpuResourceMapping {
30    public:
31     RutabagaVirtGpuResourceMapping(std::shared_ptr<EmulatedVirtioGpu> emulation,
32                                    VirtGpuResourcePtr blob, uint8_t* mapped);
33     ~RutabagaVirtGpuResourceMapping();
34 
35     uint8_t* asRawPtr(void) override;
36 
37    private:
38     const std::shared_ptr<EmulatedVirtioGpu> mEmulation;
39     const VirtGpuResourcePtr mBlob;
40     uint8_t* mMapped = nullptr;
41 };
42 
43 class RutabagaVirtGpuResource : public std::enable_shared_from_this<RutabagaVirtGpuResource>,
44                                 public VirtGpuResource {
45    public:
46     ~RutabagaVirtGpuResource();
47 
48     VirtGpuResourceMappingPtr createMapping(void) override;
49 
50     uint32_t getResourceHandle() const override;
51     uint32_t getBlobHandle() const override;
52 
53     int exportBlob(VirtGpuExternalHandle& handle) override;
54     int wait() override;
55 
56     int transferFromHost(uint32_t offset, uint32_t size) override;
57     int transferFromHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) override;
58 
59     int transferToHost(uint32_t offset, uint32_t size) override;
60     int transferToHost(uint32_t x, uint32_t y, uint32_t w, uint32_t h) override;
61 
62    private:
63     friend class RutabagaVirtGpuDevice;
64 
65     enum class ResourceType {
66         kBlob,
67         kPipe,
68     };
69 
70     RutabagaVirtGpuResource(std::shared_ptr<EmulatedVirtioGpu> emulation, uint32_t resourceId,
71                             ResourceType resourceType, uint32_t contextId);
72 
73     const std::shared_ptr<EmulatedVirtioGpu> mEmulation;
74     const uint32_t mContextId;
75     const uint32_t mResourceId;
76     const ResourceType mResourceType;
77 };
78 
79 class RutabagaVirtGpuDevice : public std::enable_shared_from_this<RutabagaVirtGpuDevice>, public VirtGpuDevice {
80   public:
81    RutabagaVirtGpuDevice(std::shared_ptr<EmulatedVirtioGpu> emulation, VirtGpuCapset capset);
82    ~RutabagaVirtGpuDevice();
83 
84    bool Init();
85 
86    int64_t getDeviceHandle() override;
87 
88    VirtGpuCaps getCaps() override;
89 
90    VirtGpuResourcePtr createBlob(const struct VirtGpuCreateBlob& blobCreate) override;
91 
92    VirtGpuResourcePtr createResource(uint32_t width, uint32_t height, uint32_t stride,
93                                      uint32_t virglFormat, uint32_t target, uint32_t bind) override;
94 
95    VirtGpuResourcePtr importBlob(const struct VirtGpuExternalHandle& handle) override;
96 
97    int execBuffer(struct VirtGpuExecBuffer& execbuffer, const VirtGpuResource* blob) override;
98 
99   private:
100    const std::shared_ptr<EmulatedVirtioGpu> mEmulation;
101    uint32_t mContextId;
102    VirtGpuCapset mCapset;
103    struct VirtGpuCaps mCaps;
104 
105    friend class RutabagaVirtGpuResource;
GetContextId()106    uint32_t GetContextId() const { return mContextId; }
107 };
108 
109 }  // namespace gfxstream
110