1 /*
2  * Copyright 2018 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 GrVkAMDMemoryAllocator_DEFINED
9 #define GrVkAMDMemoryAllocator_DEFINED
10 
11 #include "include/gpu/vk/GrVkMemoryAllocator.h"
12 #include <mutex>
13 
14 class GrVkCaps;
15 class GrVkExtensions;
16 struct GrVkInterface;
17 
18 #ifndef SK_USE_VMA
19 class GrVkAMDMemoryAllocator {
20 public:
21     static sk_sp<GrVkMemoryAllocator> Make(VkInstance instance,
22                                            VkPhysicalDevice physicalDevice,
23                                            VkDevice device,
24                                            uint32_t physicalDeviceVersion,
25                                            const GrVkExtensions* extensions,
26                                            sk_sp<const GrVkInterface> interface,
27                                            const GrVkCaps* caps,
28                                            bool cacheFlag = false,
29                                            size_t maxBlockCount = SIZE_MAX);
30 };
31 
32 #else
33 
34 #include "GrVulkanMemoryAllocator.h"
35 
36 class GrVkAMDMemoryAllocator : public GrVkMemoryAllocator {
37 public:
38     static sk_sp<GrVkMemoryAllocator> Make(VkInstance instance,
39                                            VkPhysicalDevice physicalDevice,
40                                            VkDevice device,
41                                            uint32_t physicalDeviceVersion,
42                                            const GrVkExtensions* extensions,
43                                            sk_sp<const GrVkInterface> interface,
44                                            const GrVkCaps* caps,
45                                            bool cacheFlag = false,
46                                            size_t maxBlockCount = SIZE_MAX);
47 
48     ~GrVkAMDMemoryAllocator() override;
49 
50     VkResult allocateImageMemory(VkImage image, AllocationPropertyFlags flags,
51                                  GrVkBackendMemory*) override;
52 
53     VkResult allocateBufferMemory(VkBuffer buffer, BufferUsage usage,
54                                   AllocationPropertyFlags flags, GrVkBackendMemory*) override;
55 
56     void freeMemory(const GrVkBackendMemory&) override;
57 
58     void getAllocInfo(const GrVkBackendMemory&, GrVkAlloc*) const override;
59 
60     VkResult mapMemory(const GrVkBackendMemory&, void** data) override;
61     void unmapMemory(const GrVkBackendMemory&) override;
62 
63     VkResult flushMemory(const GrVkBackendMemory&, VkDeviceSize offset, VkDeviceSize size) override;
64     VkResult invalidateMemory(const GrVkBackendMemory&, VkDeviceSize offset,
65                               VkDeviceSize size) override;
66 
67     uint64_t totalUsedMemory() const override;
68     uint64_t totalAllocatedMemory() const override;
69 
70     void vmaDefragment() override;
71     void dumpVmaStats(SkString *out, const char *sep = ", ") const override;
72 
73 private:
74     GrVkAMDMemoryAllocator(VmaAllocator allocator, sk_sp<const GrVkInterface> interface,
75 #ifdef NOT_USE_PRE_ALLOC
76                            bool mustUseCoherentHostVisibleMemory);
77 #else
78                            bool mustUseCoherentHostVisibleMemory, bool cacheFlag);
79 #endif
80 
81     VmaAllocator fAllocator;
82 
83     // If a future version of the AMD allocator has helper functions for flushing and invalidating
84     // memory, then we won't need to save the GrVkInterface here since we won't need to make direct
85     // vulkan calls.
86     sk_sp<const GrVkInterface> fInterface;
87 
88     // For host visible allocations do we require they are coherent or not. All devices are required
89     // to support a host visible and coherent memory type. This is used to work around bugs for
90     // devices that don't handle non coherent memory correctly.
91     bool fMustUseCoherentHostVisibleMemory;
92     // Protect preAlloc block
93     // 1. main thread cannot be allocated and released the preAlloc block at the same time
94     // 2. main thread and sub-thread cannot operate the preAlloc block at the same time
95     std::mutex mPreAllocMutex;
96 
97     bool fCacheFlag = false;
98 
99     using INHERITED = GrVkMemoryAllocator;
100 };
101 
102 #endif // SK_USE_VMA
103 
104 #endif
105