1 // Copyright (C) 2018 The Android Open Source Project 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 #pragma once 15 16 #include "android/base/files/Stream.h" 17 #include "android/base/files/StreamSerializing.h" 18 19 #include "goldfish_vk_private_defs.h" 20 21 #include "VulkanHandleMapping.h" 22 23 #include "IOStream.h" 24 #include "ResourceTracker.h" 25 26 #include "android/base/BumpPool.h" 27 #include "android/base/Tracing.h" 28 29 #include <vector> 30 #include <memory> 31 32 #include <log/log.h> 33 #include <inttypes.h> 34 35 class IOStream; 36 37 namespace goldfish_vk { 38 39 class VulkanStreamGuest : public android::base::Stream { 40 public: 41 VulkanStreamGuest(IOStream* stream); 42 ~VulkanStreamGuest(); 43 44 // Returns whether the connection is valid. 45 bool valid(); 46 47 // General allocation function 48 void alloc(void** ptrAddr, size_t bytes); 49 50 // Utility functions to load strings or 51 // string arrays in place with allocation. 52 void loadStringInPlace(char** forOutput); 53 void loadStringArrayInPlace(char*** forOutput); 54 55 // When we load a string and are using a reserved pointer. 56 void loadStringInPlaceWithStreamPtr(char** forOutput, uint8_t** streamPtr); 57 void loadStringArrayInPlaceWithStreamPtr(char*** forOutput, uint8_t** streamPtr); 58 59 ssize_t read(void *buffer, size_t size) override; 60 ssize_t write(const void *buffer, size_t size) override; 61 62 void writeLarge(const void* buffer, size_t size); 63 64 // Frees everything that got alloc'ed. 65 void clearPool(); 66 67 void setHandleMapping(VulkanHandleMapping* mapping); 68 void unsetHandleMapping(); 69 VulkanHandleMapping* handleMapping() const; 70 71 void flush(); 72 73 uint32_t getFeatureBits() const; 74 75 void incStreamRef(); 76 bool decStreamRef(); 77 78 uint8_t* reserve(size_t size); 79 private: 80 android::base::BumpPool mPool; 81 std::vector<uint8_t> mWriteBuffer; 82 IOStream* mStream = nullptr; 83 DefaultHandleMapping mDefaultHandleMapping; 84 VulkanHandleMapping* mCurrentHandleMapping; 85 uint32_t mFeatureBits = 0; 86 }; 87 88 class VulkanCountingStream : public VulkanStreamGuest { 89 public: 90 VulkanCountingStream(); 91 ~VulkanCountingStream(); 92 93 ssize_t read(void *buffer, size_t size) override; 94 ssize_t write(const void *buffer, size_t size) override; 95 bytesWritten()96 size_t bytesWritten() const { return m_written; } bytesRead()97 size_t bytesRead() const { return m_read; } 98 99 void rewind(); 100 private: 101 size_t m_written = 0; 102 size_t m_read = 0; 103 }; 104 105 } // namespace goldfish_vk 106