• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <optional>
21 
22 #include "VirtGpu.h"
23 
24 namespace gfxstream {
25 
26 // Emulates parts of the Linux Virtio GPU kernel module and parts of
27 // a virtual machine manager to allow speaking directly to the Gfxstream
28 // host server via rutabaga.
29 class EmulatedVirtioGpu {
30   public:
31     static EmulatedVirtioGpu& Get();
32     static void Reset();
33 
34     bool Init(bool withGl, bool withVk, bool withVkSnapshots);
35 
36     VirtGpuCaps GetCaps(VirtGpuCapset capset);
37 
38     std::optional<uint32_t> CreateContext();
39     void DestroyContext(uint32_t contextId);
40 
41     std::optional<uint32_t> CreateBlob(uint32_t contextId,
42                                        const struct VirtGpuCreateBlob& params);
43     std::optional<uint32_t> CreateVirglBlob(uint32_t contextId,
44                                             uint32_t width,
45                                             uint32_t height,
46                                             uint32_t virglFormat);
47 
48     void DestroyResource(uint32_t contextId,
49                          uint32_t resourceId);
50 
51     uint8_t* Map(uint32_t resourceId);
52     void Unmap(uint32_t resourceId);
53 
54     int ExecBuffer(uint32_t contextId,
55                    struct VirtGpuExecBuffer& execbuffer,
56                    std::optional<uint32_t> blobResourceId);
57 
58     int Wait(uint32_t resourceId);
59 
60     int TransferFromHost(uint32_t contextId,
61                          uint32_t resourceId,
62                          uint32_t transferOffset,
63                          uint32_t transferSize);
64     int TransferToHost(uint32_t contextId,
65                        uint32_t resourceId,
66                        uint32_t transferOffset,
67                        uint32_t transferSize);
68 
69     void SignalEmulatedFence(int fenceId);
70 
71     int WaitOnEmulatedFence(int fenceAsFileDescriptor, int timeoutMilliseconds);
72 
73   private:
74     EmulatedVirtioGpu();
75 
76     class EmulatedVirtioGpuImpl;
77     std::unique_ptr<EmulatedVirtioGpuImpl> mImpl;
78 };
79 
80 }  // namespace gfxstream
81