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