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