• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef __COMMAND_BUFFER_STAGING_STREAM_H
17 #define __COMMAND_BUFFER_STAGING_STREAM_H
18 
19 #include <vulkan/vulkan_core.h>
20 
21 #include <functional>
22 
23 #include "gfxstream/guest/IOStream.h"
24 
25 namespace gfxstream {
26 namespace vk {
27 
28 class CommandBufferStagingStream : public gfxstream::guest::IOStream {
29    public:
30     // host will write kSyncDataReadComplete to the sync bytes to indicate memory is no longer being
31     // used by host. This is only used with custom allocators. The sync bytes are used to ensure
32     // that, during reallocations the guest does not free memory being read by the host. The guest
33     // ensures that the sync bytes are marked as read complete before releasing the memory.
34     static constexpr size_t kSyncDataSize = 8;
35     // indicates read is complete
36     static constexpr uint32_t kSyncDataReadComplete = 0X0;
37     // indicates read is pending
38     static constexpr uint32_t kSyncDataReadPending = 0X1;
39 
40     // \struct backing memory structure
41     struct Memory {
42         VkDeviceMemory deviceMemory =
43             VK_NULL_HANDLE;   // device memory associated with allocated memory
44         void* ptr = nullptr;  // pointer to allocated memory
45         bool operator==(const Memory& rhs) const {
46             return (deviceMemory == rhs.deviceMemory) && (ptr == rhs.ptr);
47         }
48     };
49 
50     // allocator
51     // param size to allocate
52     // return allocated memory
53     using Alloc = std::function<Memory(size_t)>;
54     // free function
55     // param memory to free
56     using Free = std::function<void(const Memory&)>;
57     // constructor
58     // \param allocFn is the allocation function provided.
59     // \param freeFn is the free function provided
60     explicit CommandBufferStagingStream(const Alloc& allocFn, const Free& freeFn);
61     // constructor
62     explicit CommandBufferStagingStream();
63     ~CommandBufferStagingStream();
64 
65     virtual size_t idealAllocSize(size_t len);
66     virtual void* allocBuffer(size_t minSize);
67     virtual int commitBuffer(size_t size);
68     virtual const unsigned char* readFully(void* buf, size_t len);
69     virtual const unsigned char* read(void* buf, size_t* inout_len);
70     virtual int writeFully(const void* buf, size_t len);
71     virtual const unsigned char* commitBufferAndReadFully(size_t size, void* buf, size_t len);
72 
73     void getWritten(unsigned char** bufOut, size_t* sizeOut);
74     void reset();
75 
76     // marks the command buffer stream as flushing. The owner of CommandBufferStagingStream
77     // should call markFlushing after finishing writing to the stream.
78     // This will mark the sync data to kSyncDataReadPending. This is only applicable when
79     // using custom allocators. markFlushing will be a no-op if called
80     // when not using custom allocators
81     void markFlushing();
82 
83     // gets the device memory associated with the stream. This is VK_NULL_HANDLE for default
84     // allocation \return device memory
85     VkDeviceMemory getDeviceMemory();
86 
87    private:
88     // underlying memory for data
89     Memory m_mem;
90     // size of portion of memory available for data.
91     // for custom allocation, this size excludes size of sync data.
92     size_t m_size;
93     // current write position in data buffer
94     uint32_t m_writePos;
95 
96     // alloc function
97     Alloc m_alloc;
98     // free function
99     Free m_free;
100 
101     // realloc function
102     // \param size of memory to be allocated
103     // \ param reference size to update with actual size allocated. This size can be < requested
104     // size for custom allocation to account for sync data
105     using Realloc = std::function<Memory(const Memory&, size_t)>;
106     Realloc m_realloc;
107 
108     // flag tracking use of custom allocation/free
109     bool m_usingCustomAlloc = false;
110 
111     // adjusted memory location to point to start of data after accounting for metadata
112     // \return pointer to data start
113     unsigned char* getDataPtr();
114 };
115 
116 }  // namespace vk
117 }  // namespace gfxstream
118 
119 #endif
120