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 "IOStream.h"
20 
21 #include "address_space_graphics_types.h"
22 #include "goldfish_address_space.h"
23 
24 class AddressSpaceStream;
25 
26 AddressSpaceStream* createAddressSpaceStream(size_t bufSize);
27 AddressSpaceStream* createVirtioGpuAddressSpaceStream(size_t bufSize);
28 
29 class AddressSpaceStream : public IOStream {
30 public:
31     explicit AddressSpaceStream(
32         address_space_handle_t handle,
33         uint32_t version,
34         struct asg_context context,
35         uint64_t ringOffset,
36         uint64_t writeBufferOffset,
37         bool virtioMode,
38         struct address_space_ops ops);
39     ~AddressSpaceStream();
40 
41     virtual size_t idealAllocSize(size_t len);
42     virtual void *allocBuffer(size_t minSize);
43     virtual int commitBuffer(size_t size);
44     virtual const unsigned char *readFully( void *buf, size_t len);
45     virtual const unsigned char *read( void *buf, size_t *inout_len);
46     virtual int writeFully(const void *buf, size_t len);
47     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len);
48 
getRendernodeFd()49     int getRendernodeFd() const {
50 #if defined(__Fuchsia__)
51         return -1;
52 #else
53         if (!m_virtioMode) return -1;
54         return m_handle;
55 #endif
56     }
57 
58 private:
59     bool isInError() const;
60     ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize);
61     void notifyAvailable();
62     uint32_t getRelativeBufferPos(uint32_t pos);
63     void advanceWrite();
64     void ensureConsumerFinishing();
65     void ensureType1Finished();
66     void ensureType3Finished();
67     int type1Write(uint32_t offset, size_t size);
68 
69     bool m_virtioMode;
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 
98 #endif
99