1 /*
2 * Copyright (C) 2011 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 #ifndef __ADDRESS_SPACE_STREAM_H
17 #define __ADDRESS_SPACE_STREAM_H
18 
19 #include "VirtGpu.h"
20 #include "address_space.h"
21 #include "address_space_graphics_types.h"
22 #include "aemu/base/AndroidHealthMonitor.h"
23 #include "gfxstream/guest/IOStream.h"
24 
25 using gfxstream::guest::HealthMonitor;
26 using gfxstream::guest::IOStream;
27 
28 class AddressSpaceStream : public IOStream {
29 public:
30     explicit AddressSpaceStream(
31         address_space_handle_t handle,
32         uint32_t version,
33         struct asg_context context,
34         uint64_t ringOffset,
35         uint64_t writeBufferOffset,
36         struct address_space_ops ops,
37         HealthMonitor<>* healthMonitor);
38     ~AddressSpaceStream();
39 
40     virtual size_t idealAllocSize(size_t len);
41     virtual void *allocBuffer(size_t minSize);
42     virtual int commitBuffer(size_t size);
43     virtual const unsigned char *readFully( void *buf, size_t len);
44     virtual const unsigned char *read( void *buf, size_t *inout_len);
45     virtual int writeFully(const void *buf, size_t len);
46     virtual int writeFullyAsync(const void *buf, size_t len);
47     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len);
48 
setMapping(VirtGpuResourceMappingPtr mapping)49     void setMapping(VirtGpuResourceMappingPtr mapping) { m_mapping = mapping; }
50 
setResourceId(uint32_t id)51     void setResourceId(uint32_t id) {
52         m_resourceId = id;
53     }
54 
55 private:
56     bool isInError() const;
57     ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize);
58     void notifyAvailable();
59     uint32_t getRelativeBufferPos(uint32_t pos);
60     void advanceWrite();
61     void ensureConsumerFinishing();
62     void ensureType1Finished();
63     void ensureType3Finished();
64     int type1Write(uint32_t offset, size_t size);
65 
66     void backoff();
67     void resetBackoff();
68 
69     VirtGpuResourceMappingPtr m_mapping = nullptr;
70     struct address_space_ops m_ops;
71 
72     unsigned char* m_tmpBuf;
73     size_t m_tmpBufSize;
74     size_t m_tmpBufXferSize;
75     bool m_usingTmpBuf;
76 
77     unsigned char* m_readBuf;
78     size_t m_read;
79     size_t m_readLeft;
80 
81     address_space_handle_t m_handle;
82     uint32_t m_version;
83     struct asg_context m_context;
84 
85     uint64_t m_ringOffset;
86     uint64_t m_writeBufferOffset;
87 
88     uint32_t m_writeBufferSize;
89     uint32_t m_writeBufferMask;
90     unsigned char* m_buf;
91     unsigned char* m_writeStart;
92     uint32_t m_writeStep;
93 
94     uint32_t m_notifs;
95     uint32_t m_written;
96 
97     uint64_t m_backoffIters;
98     uint64_t m_backoffFactor;
99 
100     size_t m_ringStorageSize;
101     uint32_t m_resourceId = 0;
102 
103     HealthMonitor<>* m_healthMonitor;
104 };
105 
106 #endif
107