• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/GpuTypes.h"
12 #include "include/gpu/vk/VulkanMemoryAllocator.h"
13 #include "include/gpu/vk/VulkanTypes.h"
14 #include "include/private/base/SkTArray.h"
15 
16 #include <functional>
17 
18 namespace skgpu {
19 
20 namespace VulkanMemory {
21     using CheckResult = bool(VkResult);
22 
23     bool AllocBufferMemory(VulkanMemoryAllocator*,
24                            VkBuffer buffer,
25                            skgpu::VulkanMemoryAllocator::BufferUsage,
26                            bool shouldPersistentlyMapCpuToGpu,
27                            const std::function<CheckResult>&,
28                            VulkanAlloc* alloc);
29 
30     void FreeBufferMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
31 
32     bool AllocImageMemory(VulkanMemoryAllocator*,
33                           VkImage image,
34                           skgpu::Protected isProtected,
35                           bool forceDedicatedMemory,
36                           bool useLazyAllocation,
37                           const std::function<CheckResult>&,
38                           VulkanAlloc* alloc);
39 
40     void FreeImageMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
41 
42     // Maps the entire skgpu::VulkanAlloc and returns a pointer to the start of the allocation.
43     // Underneath the hood, we may map more than the range of the skgpu::VulkanAlloc (e.g. the
44     // entire VkDeviceMemory), but the pointer returned will always be to the start of the
45     // skgpu::VulkanAlloc. The caller should also never assume more than the skgpu::VulkanAlloc
46     // block has been mapped.
47     void* MapAlloc(VulkanMemoryAllocator*,
48                    const VulkanAlloc&,
49                    const std::function<CheckResult>&);
50     void UnmapAlloc(VulkanMemoryAllocator*, const VulkanAlloc& alloc);
51 
52     // For the Flush and Invalidate calls, the offset should be relative to the skgpu::VulkanAlloc.
53     // Thus this will often be 0. The client does not need to make sure the offset and size are
54     // aligned to the nonCoherentAtomSize, the internal calls will handle that.
55     void FlushMappedAlloc(VulkanMemoryAllocator*,
56                           const skgpu::VulkanAlloc&,
57                           VkDeviceSize offset,
58                           VkDeviceSize size,
59                           const std::function<CheckResult>&);
60     void InvalidateMappedAlloc(VulkanMemoryAllocator*,
61                                const VulkanAlloc& alloc,
62                                VkDeviceSize offset,
63                                VkDeviceSize size,
64                                const std::function<CheckResult>&);
65 
66     // Helper for aligning and setting VkMappedMemoryRange for flushing/invalidating noncoherent
67     // memory.
68     void GetNonCoherentMappedMemoryRange(const VulkanAlloc&,
69                                          VkDeviceSize offset,
70                                          VkDeviceSize size,
71                                          VkDeviceSize alignment,
72                                          VkMappedMemoryRange*);
73 }  // namespace VulkanMemory
74 
75 }  // namespace skgpu
76 
77 #endif
78