• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
28 #if defined(VIRTIO_GPU) && !defined(HOST_BUILD)
29 struct StreamCreate {
30    int streamHandle;
31 };
32 
33 AddressSpaceStream* createVirtioGpuAddressSpaceStream(const struct StreamCreate &streamCreate);
34 #endif
35 
36 class AddressSpaceStream : public IOStream {
37 public:
38     explicit AddressSpaceStream(
39         address_space_handle_t handle,
40         uint32_t version,
41         struct asg_context context,
42         uint64_t ringOffset,
43         uint64_t writeBufferOffset,
44         bool virtioMode,
45         struct address_space_ops ops);
46     ~AddressSpaceStream();
47 
48     virtual size_t idealAllocSize(size_t len);
49     virtual void *allocBuffer(size_t minSize);
50     virtual int commitBuffer(size_t size);
51     virtual const unsigned char *readFully( void *buf, size_t len);
52     virtual const unsigned char *read( void *buf, size_t *inout_len);
53     virtual int writeFully(const void *buf, size_t len);
54     virtual int writeFullyAsync(const void *buf, size_t len);
55     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len);
56 
getRendernodeFd()57     int getRendernodeFd() const {
58 #if defined(__Fuchsia__)
59         return -1;
60 #else
61         if (!m_virtioMode) return -1;
62         return m_handle;
63 #endif
64     }
65 
66 private:
67     bool isInError() const;
68     ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize);
69     void notifyAvailable();
70     uint32_t getRelativeBufferPos(uint32_t pos);
71     void advanceWrite();
72     void ensureConsumerFinishing();
73     void ensureType1Finished();
74     void ensureType3Finished();
75     int type1Write(uint32_t offset, size_t size);
76 
77     void backoff();
78     void resetBackoff();
79 
80     bool m_virtioMode;
81     struct address_space_ops m_ops;
82 
83     unsigned char* m_tmpBuf;
84     size_t m_tmpBufSize;
85     size_t m_tmpBufXferSize;
86     bool m_usingTmpBuf;
87 
88     unsigned char* m_readBuf;
89     size_t m_read;
90     size_t m_readLeft;
91 
92     address_space_handle_t m_handle;
93     uint32_t m_version;
94     struct asg_context m_context;
95 
96     uint64_t m_ringOffset;
97     uint64_t m_writeBufferOffset;
98 
99     uint32_t m_writeBufferSize;
100     uint32_t m_writeBufferMask;
101     unsigned char* m_buf;
102     unsigned char* m_writeStart;
103     uint32_t m_writeStep;
104 
105     uint32_t m_notifs;
106     uint32_t m_written;
107 
108     uint64_t m_backoffIters;
109     uint64_t m_backoffFactor;
110 
111     size_t m_ringStorageSize;
112 };
113 
114 #endif
115