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