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 "base/BumpPool.h" 17 #include "base/Stream.h" 18 #include "base/StreamSerializing.h" 19 20 #include "VulkanHandleMapping.h" 21 #include "common/goldfish_vk_private_defs.h" 22 23 #include <memory> 24 #include <vector> 25 26 #include <inttypes.h> 27 28 #define E(fmt,...) fprintf(stderr, fmt "\n", ##__VA_ARGS__) 29 30 class IOStream; 31 32 namespace android { 33 namespace base { 34 class BumpPool; 35 } // namespace android 36 } // namespace base 37 38 namespace goldfish_vk { 39 40 class VulkanStream : public android::base::Stream { 41 public: 42 VulkanStream(IOStream* stream); 43 ~VulkanStream(); 44 45 void setStream(IOStream* stream); 46 47 // Returns whether the connection is valid. 48 bool valid(); 49 50 // General allocation function 51 void alloc(void** ptrAddr, size_t bytes); 52 53 // Utility functions to load strings or 54 // string arrays in place with allocation. 55 void loadStringInPlace(char** forOutput); 56 void loadStringArrayInPlace(char*** forOutput); 57 58 // When we load a string and are using a reserved pointer. 59 void loadStringInPlaceWithStreamPtr(char** forOutput, uint8_t** streamPtr); 60 void loadStringArrayInPlaceWithStreamPtr(char*** forOutput, uint8_t** streamPtr); 61 62 virtual ssize_t read(void *buffer, size_t size); 63 virtual ssize_t write(const void *buffer, size_t size); 64 65 void commitWrite(); 66 67 // Frees everything that got alloc'ed. 68 void clearPool(); 69 70 void setHandleMapping(VulkanHandleMapping* mapping); 71 void unsetHandleMapping(); 72 VulkanHandleMapping* handleMapping() const; 73 74 uint32_t getFeatureBits() const; 75 76 android::base::BumpPool* pool(); 77 78 private: 79 size_t remainingWriteBufferSize() const; 80 ssize_t bufferedWrite(const void *buffer, size_t size); 81 android::base::BumpPool mPool; 82 size_t mWritePos = 0; 83 std::vector<uint8_t> mWriteBuffer; 84 IOStream* mStream = nullptr; 85 DefaultHandleMapping mDefaultHandleMapping; 86 VulkanHandleMapping* mCurrentHandleMapping; 87 uint32_t mFeatureBits = 0; 88 }; 89 90 class VulkanMemReadingStream : public VulkanStream { 91 public: 92 VulkanMemReadingStream(uint8_t* start); 93 ~VulkanMemReadingStream(); 94 95 void setBuf(uint8_t* buf); 96 uint8_t* getBuf(); 97 void setReadPos(uintptr_t pos); 98 99 ssize_t read(void *buffer, size_t size) override; 100 ssize_t write(const void *buffer, size_t size) override; 101 102 uint8_t* beginTrace(); 103 size_t endTrace(); 104 105 private: 106 void resetTrace(); 107 108 uint8_t* mStart; 109 uint8_t* mTraceStart; 110 uintptr_t mReadPos = 0; 111 }; 112 113 } // namespace goldfish_vk 114