• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 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 "render-utils/IOStream.h"
17 #include "render-utils/RenderChannel.h"
18 
19 #include "aemu/base/ring_buffer.h"
20 #include "host-common/address_space_graphics_types.h"
21 
22 #include <functional>
23 #include <vector>
24 
25 namespace gfxstream {
26 
27 // An IOStream instance that can be used by the host RenderThread to process
28 // messages from a pair of ring buffers (to host and from host).  It also takes
29 // a callback that does something when there are no available bytes to read in
30 // the "to host" ring buffer.
31 class RingStream final : public IOStream {
32 public:
33     using OnUnavailableReadCallback = std::function<int()>;
34     using GetPtrAndSizeCallback =
35         std::function<void(uint64_t, char**, size_t*)>;
36 
37     RingStream(
38         struct asg_context context,
39         android::emulation::asg::ConsumerCallbacks callbacks,
40         size_t bufsize);
41     ~RingStream();
42 
43     int getNeededFreeTailSize() const;
44 
45     int writeFully(const void* buf, size_t len) override;
46     const unsigned char *readFully( void *buf, size_t len) override;
47 
48     void printStats();
49 
pausePreSnapshot()50     void pausePreSnapshot() {
51         mInSnapshotOperation = true;
52     }
53 
resume()54     void resume() {
55         mInSnapshotOperation = false;
56     }
57 
inSnapshotOperation()58     bool inSnapshotOperation() const {
59         return mInSnapshotOperation;
60     }
61 
62 protected:
63     virtual void* allocBuffer(size_t minSize) override final;
64     virtual int commitBuffer(size_t size) override final;
65     virtual const unsigned char* readRaw(void* buf, size_t* inout_len) override final;
66     virtual void* getDmaForReading(uint64_t guest_paddr) override final;
67     virtual void unlockDma(uint64_t guest_paddr) override final;
68 
69     void onSave(android::base::Stream* stream) override;
70     unsigned char* onLoad(android::base::Stream* stream) override;
71 
72     void type1Read(uint32_t available, char* begin, size_t* count, char** current, const char* ptrEnd);
73     void type2Read(uint32_t available, size_t* count, char** current, const char* ptrEnd);
74     void type3Read(uint32_t available, size_t* count, char** current, const char* ptrEnd);
75 
76     struct asg_context mContext;
77     android::emulation::asg::ConsumerCallbacks mCallbacks;
78 
79     std::vector<asg_type1_xfer> mType1Xfers;
80     std::vector<asg_type2_xfer> mType2Xfers;
81 
82     RenderChannel::Buffer mReadBuffer;
83     RenderChannel::Buffer mWriteBuffer;
84     size_t mReadBufferLeft = 0;
85 
86     size_t mXmits = 0;
87     size_t mTotalRecv = 0;
88     bool mBenchmarkEnabled = false;
89     bool mShouldExit = false;
90     bool mShouldExitForSnapshot = false;
91     bool mInSnapshotOperation = false;
92 };
93 
94 }  // namespace gfxstream
95