1 // Copyright (C) 2024 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <stdint.h>
18 
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 #include <unordered_set>
24 
25 #include "ExternalObjectManager.h"
26 #include "VirtioGpu.h"
27 #ifdef GFXSTREAM_BUILD_WITH_SNAPSHOT_FRONTEND_SUPPORT
28 // X11 defines status as a preprocessor define which messes up
29 // anyone with a `Status` type.
30 #undef Status
31 #include "VirtioGpuContextSnapshot.pb.h"
32 #endif
33 #include "VirtioGpuResource.h"
34 #include "gfxstream/virtio-gpu-gfxstream-renderer.h"
35 #include "host-common/address_space_device_control_ops.h"
36 extern "C" {
37 #include "host-common/goldfish_pipe.h"
38 }  // extern "C"
39 
40 namespace gfxstream {
41 namespace host {
42 
43 class VirtioGpuContext {
44    public:
45     VirtioGpuContext() = default;
46 
47     static std::optional<VirtioGpuContext> Create(const GoldfishPipeServiceOps* ops,
48                                                   VirtioGpuContextId contextId,
49                                                   const std::string& contextName,
50                                                   uint32_t capsetId);
51 
52     int Destroy(const GoldfishPipeServiceOps* pipeOps,
53                 const struct address_space_device_control_ops* asgOps);
54 
55     void AttachResource(VirtioGpuResource& resource);
56     void DetachResource(VirtioGpuResource& resource);
57 
58     const std::unordered_set<VirtioGpuResourceId>& GetAttachedResources() const;
59 
60     void SetHostPipe(GoldfishHostPipe* pipe);
61 
62     int AcquireSync(uint64_t syncId);
63     std::optional<SyncDescriptorInfo> TakeSync();
64 
65     int CreateAddressSpaceGraphicsInstance(const struct address_space_device_control_ops* asgOps,
66                                            VirtioGpuResource& resource);
67     const std::unordered_map<VirtioGpuResourceId, uint32_t>& AsgInstances() const;
68     std::optional<uint32_t> TakeAddressSpaceGraphicsHandle(VirtioGpuResourceId resourceId);
69 
70     int PingAddressSpaceGraphicsInstance(const struct address_space_device_control_ops* asgOps,
71                                          VirtioGpuResourceId resourceId);
72 
73     int AddPendingBlob(uint32_t blobId, struct stream_renderer_resource_create_args args);
74     std::optional<struct stream_renderer_resource_create_args> TakePendingBlob(uint32_t args);
75 
76 #ifdef GFXSTREAM_BUILD_WITH_SNAPSHOT_FRONTEND_SUPPORT
77     std::optional<gfxstream::host::snapshot::VirtioGpuContextSnapshot> Snapshot() const;
78 
79     static std::optional<VirtioGpuContext> Restore(
80         const gfxstream::host::snapshot::VirtioGpuContextSnapshot& snapshot);
81 #endif
82 
83    private:
84     // LINT.IfChange(virtio_gpu_context)
85     VirtioGpuContextId mId;
86     std::string mName;
87     uint32_t mCapsetId;
88     GoldfishHostPipe* mHostPipe;
89     std::unordered_set<VirtioGpuResourceId> mAttachedResources;
90     std::unordered_map<VirtioGpuResourceId, uint32_t> mAddressSpaceHandles;
91     std::unordered_map<uint32_t, struct stream_renderer_resource_create_args> mPendingBlobs;
92     std::optional<SyncDescriptorInfo> mLatestSync;
93     // LINT.ThenChange(VirtioGpuContextSnapshot.proto:virtio_gpu_context)
94 };
95 
96 }  // namespace host
97 }  // namespace gfxstream