xref: /aosp_15_r20/external/mesa3d/src/gfxstream/guest/vulkan_enc/HostVisibleMemoryVirtualization.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2018 Google
3  * SPDX-License-Identifier: MIT
4  */
5 #pragma once
6 
7 #include <vulkan/vulkan.h>
8 
9 #include "VirtGpu.h"
10 #include "goldfish_address_space.h"
11 #include "util/u_mm.h"
12 
13 constexpr uint64_t kMegaByte = 1048576;
14 
15 // This needs to be a power of 2 that is at least the min alignment needed
16 // in HostVisibleMemoryVirtualization.cpp.
17 // Some Windows drivers require a 64KB alignment for suballocated memory (b:152769369) for YUV
18 // images.
19 constexpr uint64_t kLargestPageSize = 65536;
20 
21 constexpr uint64_t kDefaultHostMemBlockSize = 16 * kMegaByte;  // 16 mb
22 constexpr uint64_t kHostVisibleHeapSize = 512 * kMegaByte;     // 512 mb
23 
24 namespace gfxstream {
25 namespace vk {
26 
27 using GoldfishAddressSpaceBlockPtr = std::shared_ptr<GoldfishAddressSpaceBlock>;
28 
29 class CoherentMemory {
30    public:
31     CoherentMemory(VirtGpuResourceMappingPtr blobMapping, uint64_t size, VkDevice device,
32                    VkDeviceMemory memory);
33 
34 #if defined(__ANDROID__)
35     CoherentMemory(GoldfishAddressSpaceBlockPtr block, uint64_t gpuAddr, uint64_t size,
36                    VkDevice device, VkDeviceMemory memory);
37 #endif  // defined(__ANDROID__)
38 
39     ~CoherentMemory();
40 
41     VkDeviceMemory getDeviceMemory() const;
42 
43     bool subAllocate(uint64_t size, uint8_t** ptr, uint64_t& offset);
44     bool release(uint8_t* ptr);
45 
46    private:
47     CoherentMemory(CoherentMemory const&);
48     void operator=(CoherentMemory const&);
49 
50     uint64_t mSize;
51     VirtGpuResourceMappingPtr mBlobMapping;
52     GoldfishAddressSpaceBlockPtr mBlock;
53     VkDevice mDevice;
54     VkDeviceMemory mMemory;
55 
56     uint8_t* mBaseAddr = nullptr;
57     struct mem_block* mHeap = nullptr;
58 };
59 
60 using CoherentMemoryPtr = std::shared_ptr<CoherentMemory>;
61 
62 }  // namespace vk
63 }  // namespace gfxstream
64