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 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     ~AddressSpaceStream();
37 
38     virtual size_t idealAllocSize(size_t len);
39     virtual void *allocBuffer(size_t minSize);
40     virtual int commitBuffer(size_t size);
41     virtual const unsigned char *readFully( void *buf, size_t len);
42     virtual const unsigned char *read( void *buf, size_t *inout_len);
43     virtual int writeFully(const void *buf, size_t len);
44     virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len);
45 
46 private:
47     bool isInError() const;
48     ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize);
49     void notifyAvailable();
50     uint32_t getRelativeBufferPos(uint32_t pos);
51     void advanceWrite();
52     void ensureConsumerFinishing();
53     void ensureType1Finished();
54     void ensureType3Finished();
55     int type1Write(uint32_t offset, size_t size);
56 
57     unsigned char* m_tmpBuf;
58     size_t m_tmpBufSize;
59     size_t m_tmpBufXferSize;
60     bool m_usingTmpBuf;
61 
62     unsigned char* m_readBuf;
63     size_t m_read;
64     size_t m_readLeft;
65 
66     address_space_handle_t m_handle;
67     uint32_t m_version;
68     struct asg_context m_context;
69 
70     uint64_t m_ringOffset;
71     uint64_t m_writeBufferOffset;
72 
73     uint32_t m_writeBufferSize;
74     uint32_t m_writeBufferMask;
75     unsigned char* m_buf;
76     unsigned char* m_writeStart;
77     uint32_t m_writeStep;
78 
79     uint32_t m_notifs;
80     uint32_t m_written;
81 };
82 
83 #endif
84