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 #include <log/log.h> 18 19 #include <memory> 20 #include <vector> 21 22 #include "gfxstream/guest/IOStream.h" 23 #include "ResourceTracker.h" 24 #include "VulkanHandleMapping.h" 25 #include "aemu/base/BumpPool.h" 26 #include "aemu/base/Tracing.h" 27 #include "aemu/base/files/Stream.h" 28 #include "aemu/base/files/StreamSerializing.h" 29 #include "goldfish_vk_private_defs.h" 30 31 namespace gfxstream { 32 namespace vk { 33 34 class VulkanStreamGuest : public gfxstream::guest::Stream { 35 public: 36 VulkanStreamGuest(gfxstream::guest::IOStream* stream); 37 ~VulkanStreamGuest(); 38 39 // Returns whether the connection is valid. 40 bool valid(); 41 42 // General allocation function 43 void alloc(void** ptrAddr, size_t bytes); 44 45 // Utility functions to load strings or 46 // string arrays in place with allocation. 47 void loadStringInPlace(char** forOutput); 48 void loadStringArrayInPlace(char*** forOutput); 49 50 // When we load a string and are using a reserved pointer. 51 void loadStringInPlaceWithStreamPtr(char** forOutput, uint8_t** streamPtr); 52 void loadStringArrayInPlaceWithStreamPtr(char*** forOutput, uint8_t** streamPtr); 53 54 ssize_t read(void* buffer, size_t size) override; 55 ssize_t write(const void* buffer, size_t size) override; 56 57 void writeLarge(const void* buffer, size_t size); 58 59 // Frees everything that got alloc'ed. 60 void clearPool(); 61 62 void setHandleMapping(VulkanHandleMapping* mapping); 63 void unsetHandleMapping(); 64 VulkanHandleMapping* handleMapping() const; 65 66 void flush(); 67 68 uint32_t getFeatureBits() const; 69 70 void incStreamRef(); 71 bool decStreamRef(); 72 73 uint8_t* reserve(size_t size); 74 75 private: 76 gfxstream::guest::BumpPool mPool; 77 std::vector<uint8_t> mWriteBuffer; 78 gfxstream::guest::IOStream* mStream = nullptr; 79 DefaultHandleMapping mDefaultHandleMapping; 80 VulkanHandleMapping* mCurrentHandleMapping; 81 uint32_t mFeatureBits = 0; 82 }; 83 84 class VulkanCountingStream : public VulkanStreamGuest { 85 public: 86 VulkanCountingStream(); 87 ~VulkanCountingStream(); 88 89 ssize_t read(void* buffer, size_t size) override; 90 ssize_t write(const void* buffer, size_t size) override; 91 bytesWritten()92 size_t bytesWritten() const { return m_written; } bytesRead()93 size_t bytesRead() const { return m_read; } 94 95 void rewind(); 96 97 private: 98 size_t m_written = 0; 99 size_t m_read = 0; 100 }; 101 102 } // namespace vk 103 } // namespace gfxstream 104