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