xref: /aosp_15_r20/external/skia/src/gpu/vk/VulkanMemory.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7 
8 #ifndef skgpu_VulkanMemory_DEFINED
9 #define skgpu_VulkanMemory_DEFINED
10 
11 #include "include/gpu/vk/VulkanMemoryAllocator.h"
12 #include "include/private/gpu/vk/SkiaVulkan.h"
13 
14 #include <functional>
15 
16 namespace skgpu {
17 enum class Protected : bool;
18 struct VulkanAlloc;
19 
20 namespace VulkanMemory {
21     using CheckResult = bool(VkResult);
22 
23     bool AllocBufferMemory(VulkanMemoryAllocator*,
24                            VkBuffer buffer,
25                            skgpu::Protected isProtected,
26                            skgpu::VulkanMemoryAllocator::BufferUsage,
27                            bool shouldPersistentlyMapCpuToGpu,
28                            const std::function<CheckResult>&,
29                            VulkanAlloc* alloc);
30 
31     void FreeBufferMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
32 
33     bool AllocImageMemory(VulkanMemoryAllocator*,
34                           VkImage image,
35                           skgpu::Protected isProtected,
36                           bool forceDedicatedMemory,
37                           bool useLazyAllocation,
38                           const std::function<CheckResult>&,
39                           VulkanAlloc* alloc);
40 
41     void FreeImageMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
42 
43     // Maps the entire skgpu::VulkanAlloc and returns a pointer to the start of the allocation.
44     // Underneath the hood, we may map more than the range of the skgpu::VulkanAlloc (e.g. the
45     // entire VkDeviceMemory), but the pointer returned will always be to the start of the
46     // skgpu::VulkanAlloc. The caller should also never assume more than the skgpu::VulkanAlloc
47     // block has been mapped.
48     void* MapAlloc(VulkanMemoryAllocator*,
49                    const VulkanAlloc&,
50                    const std::function<CheckResult>&);
51     void UnmapAlloc(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
52 
53     // For the Flush and Invalidate calls, the offset should be relative to the skgpu::VulkanAlloc.
54     // Thus this will often be 0. The client does not need to make sure the offset and size are
55     // aligned to the nonCoherentAtomSize, the internal calls will handle that.
56     void FlushMappedAlloc(VulkanMemoryAllocator*,
57                           const skgpu::VulkanAlloc&,
58                           VkDeviceSize offset,
59                           VkDeviceSize size,
60                           const std::function<CheckResult>&);
61     void InvalidateMappedAlloc(VulkanMemoryAllocator*,
62                                const VulkanAlloc& alloc,
63                                VkDeviceSize offset,
64                                VkDeviceSize size,
65                                const std::function<CheckResult>&);
66 
67     // Helper for aligning and setting VkMappedMemoryRange for flushing/invalidating noncoherent
68     // memory.
69     void GetNonCoherentMappedMemoryRange(const VulkanAlloc&,
70                                          VkDeviceSize offset,
71                                          VkDeviceSize size,
72                                          VkDeviceSize alignment,
73                                          VkMappedMemoryRange*);
74 }  // namespace VulkanMemory
75 
76 }  // namespace skgpu
77 
78 #endif
79