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