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 int writeFullyAsync(const void *buf, size_t len); 48 virtual const unsigned char *commitBufferAndReadFully(size_t size, void *buf, size_t len); 49 getRendernodeFd()50 int getRendernodeFd() const { 51 #if defined(__Fuchsia__) 52 return -1; 53 #else 54 if (!m_virtioMode) return -1; 55 return m_handle; 56 #endif 57 } 58 59 private: 60 bool isInError() const; 61 ssize_t speculativeRead(unsigned char* readBuffer, size_t trySize); 62 void notifyAvailable(); 63 uint32_t getRelativeBufferPos(uint32_t pos); 64 void advanceWrite(); 65 void ensureConsumerFinishing(); 66 void ensureType1Finished(); 67 void ensureType3Finished(); 68 int type1Write(uint32_t offset, size_t size); 69 70 void backoff(); 71 void resetBackoff(); 72 73 bool m_virtioMode; 74 struct address_space_ops m_ops; 75 76 unsigned char* m_tmpBuf; 77 size_t m_tmpBufSize; 78 size_t m_tmpBufXferSize; 79 bool m_usingTmpBuf; 80 81 unsigned char* m_readBuf; 82 size_t m_read; 83 size_t m_readLeft; 84 85 address_space_handle_t m_handle; 86 uint32_t m_version; 87 struct asg_context m_context; 88 89 uint64_t m_ringOffset; 90 uint64_t m_writeBufferOffset; 91 92 uint32_t m_writeBufferSize; 93 uint32_t m_writeBufferMask; 94 unsigned char* m_buf; 95 unsigned char* m_writeStart; 96 uint32_t m_writeStep; 97 98 uint32_t m_notifs; 99 uint32_t m_written; 100 101 uint64_t m_backoffIters; 102 uint64_t m_backoffFactor; 103 }; 104 105 #endif 106