1 // Copyright 2019 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 #pragma once
15 
16 #include <functional>
17 #include <optional>
18 #include <vector>
19 
20 #include "AddressSpaceService.h"
21 #include "address_space_device.h"
22 #include "address_space_device.hpp"
23 #include "address_space_graphics_types.h"
24 #include "aemu/base/ring_buffer.h"
25 #include "aemu/base/synchronization/MessageChannel.h"
26 #include "aemu/base/threads/FunctorThread.h"
27 
28 namespace android {
29 namespace emulation {
30 namespace asg {
31 
32 struct Allocation {
33     char* buffer = nullptr;
34     size_t blockIndex = 0;
35     uint64_t offsetIntoPhys = 0;
36     uint64_t size = 0;
37     std::optional<uint32_t> dedicatedContextHandle;
38     uint64_t hostmemId = 0;
39     bool isView = false;
40 };
41 
42 class AddressSpaceGraphicsContext : public AddressSpaceDeviceContext {
43 public:
44  AddressSpaceGraphicsContext(const struct AddressSpaceCreateInfo& create);
45  ~AddressSpaceGraphicsContext();
46 
47  static void setConsumer(ConsumerInterface);
48  static void init(const address_space_device_control_ops* ops);
49  static void clear();
50 
51  void perform(AddressSpaceDevicePingInfo* info) override;
52  AddressSpaceDeviceType getDeviceType() const override;
53 
54  void save(base::Stream* stream) const override;
55  bool load(base::Stream* stream) override;
56 
57  void preSave() const override;
58  void postSave() const override;
59 
60  static void globalStatePreSave();
61  static void globalStateSave(base::Stream*);
62  static void globalStatePostSave();
63 
64  static bool globalStateLoad(base::Stream*,
65                              const std::optional<AddressSpaceDeviceLoadResources>& resources);
66 
67  enum AllocType {
68      AllocTypeRing,
69      AllocTypeBuffer,
70      AllocTypeCombined,
71  };
72 
73 private:
74 
75     void saveRingConfig(base::Stream* stream, const struct asg_ring_config& config) const;
76     void saveAllocation(base::Stream* stream, const Allocation& alloc) const;
77 
78     void loadRingConfig(base::Stream* stream, struct asg_ring_config& config);
79 
80     void loadAllocation(base::Stream* stream, Allocation& alloc);
81 
82     // For consumer communication
83     enum ConsumerCommand {
84         Wakeup = 0,
85         Sleep = 1,
86         Exit = 2,
87         PausePreSnapshot = 3,
88         ResumePostSnapshot = 4,
89     };
90 
91     // For ConsumerCallbacks
92     int onUnavailableRead();
93 
94     // Data layout
95     uint32_t mVersion = 1;
96     Allocation mRingAllocation;
97     Allocation mBufferAllocation;
98     Allocation mCombinedAllocation;
99     struct asg_context mHostContext = {};
100 
101     // Consumer storage
102     ConsumerCallbacks mConsumerCallbacks;
103     ConsumerInterface mConsumerInterface;
104     void* mCurrentConsumer = 0;
105 
106     // Communication with consumer
107     mutable base::MessageChannel<ConsumerCommand, 4> mConsumerMessages;
108     uint32_t mExiting = 0;
109     // For onUnavailableRead
110     uint32_t mUnavailableReadCount = 0;
111 
112     struct VirtioGpuInfo {
113         uint32_t contextId = 0;
114         uint32_t capsetId = 0;
115         std::optional<std::string> name;
116     };
117     std::optional<VirtioGpuInfo> mVirtioGpuInfo;
118     // To save the ring config if it is cleared on hostmem map
119     struct asg_ring_config mSavedConfig;
120 };
121 
122 }  // namespace asg
123 }  // namespace android
124 }  // namespace emulation
125