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 "VulkanHandleMapping.h" 20 21 #include <memory> 22 23 class IOStream; 24 25 namespace goldfish_vk { 26 27 class VulkanStreamGuest : public android::base::Stream { 28 public: 29 VulkanStreamGuest(IOStream* stream); 30 ~VulkanStreamGuest(); 31 32 // Returns whether the connection is valid. 33 bool valid(); 34 35 // General allocation function 36 void alloc(void** ptrAddr, size_t bytes); 37 38 // Utility functions to load strings or 39 // string arrays in place with allocation. 40 void loadStringInPlace(char** forOutput); 41 void loadStringArrayInPlace(char*** forOutput); 42 43 ssize_t read(void *buffer, size_t size) override; 44 ssize_t write(const void *buffer, size_t size) override; 45 46 // Frees everything that got alloc'ed. 47 void clearPool(); 48 49 void setHandleMapping(VulkanHandleMapping* mapping); 50 void unsetHandleMapping(); 51 VulkanHandleMapping* handleMapping() const; 52 53 void flush(); 54 private: 55 class Impl; 56 std::unique_ptr<Impl> mImpl; 57 }; 58 59 class VulkanCountingStream : public VulkanStreamGuest { 60 public: 61 VulkanCountingStream(); 62 ~VulkanCountingStream(); 63 64 ssize_t read(void *buffer, size_t size) override; 65 ssize_t write(const void *buffer, size_t size) override; 66 bytesWritten()67 size_t bytesWritten() const { return m_written; } bytesRead()68 size_t bytesRead() const { return m_read; } 69 70 void rewind(); 71 private: 72 size_t m_written = 0; 73 size_t m_read = 0; 74 }; 75 76 } // namespace goldfish_vk 77