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