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