• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrVkMemoryAllocator_DEFINED
9 #define GrVkMemoryAllocator_DEFINED
10 
11 #include "SkRefCnt.h"
12 #include "GrTypes.h"
13 #include "GrVkTypes.h"
14 
15 class GrVkMemoryAllocator : public SkRefCnt {
16 public:
17     enum class AllocationPropertyFlags {
18         kNone                = 0,
19         // Allocation will be placed in its own VkDeviceMemory and not suballocated from some larger
20         // block.
21         kDedicatedAllocation = 0x1,
22         // Says that the backing memory can only be accessed by the device. Additionally the device
23         // may lazily allocate the memory. This cannot be used with buffers that will be host
24         // visible. Setting this flag does not guarantee that we will allocate memory that respects
25         // it, but we will try to prefer memory that can respect it.
26         kLazyAllocation      = 0x2,
27         // The allocation will be mapped immediately and stay mapped until it is destroyed. This
28         // flag is only valid for buffers which are host visible (i.e. must have a usage other than
29         // BufferUsage::kGpuOnly).
30         kPersistentlyMapped  = 0x4,
31     };
32 
33     GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AllocationPropertyFlags);
34 
35     enum class BufferUsage {
36         // Buffers that will only be accessed from the device (large const buffers). Will always be
37         // in device local memory.
38         kGpuOnly,
39         // Buffers that will be accessed on the host and copied to and from a GPU resource (transfer
40         // buffers). Will always be mappable and coherent memory.
41         kCpuOnly,
42         // Buffers that typically will be updated multiple times by the host and read on the gpu
43         // (e.g. uniform or vertex buffers). Will always be mappable memory, and will prefer to be
44         // in device local memory.
45         kCpuWritesGpuReads,
46         // Buffers which are typically writted to by the GPU and then read on the host. Will always
47         // be mappable memory, and will prefer coherent and cached memory.
48         kGpuWritesCpuReads,
49     };
50 
51     virtual bool allocateMemoryForImage(VkImage image, AllocationPropertyFlags flags,
52                                         GrVkBackendMemory*) = 0;
53 
54     virtual bool allocateMemoryForBuffer(VkBuffer buffer, BufferUsage usage,
55                                          AllocationPropertyFlags flags, GrVkBackendMemory*) = 0;
56 
57     // Fills out the passed in GrVkAlloc struct for the passed in GrVkBackendMemory.
58     virtual void getAllocInfo(const GrVkBackendMemory&, GrVkAlloc*) const = 0;
59 
60     // Maps the entire allocation and returns a pointer to the start of the allocation. The
61     // implementation may map more memory than just the allocation, but the returned pointer must
62     // point at the start of the memory for the requested allocation.
63     virtual void* mapMemory(const GrVkBackendMemory&) = 0;
64     virtual void unmapMemory(const GrVkBackendMemory&) = 0;
65 
66     // The following two calls are used for managing non-coherent memory. The offset is relative to
67     // the start of the allocation and not the underlying VkDeviceMemory. Additionaly the client
68     // must make sure that the offset + size passed in is less that or equal to the allocation size.
69     // It is the responsibility of the implementation to make sure all alignment requirements are
70     // followed. The client should not have to deal with any sort of alignment issues.
71     virtual void flushMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset,
72                                    VkDeviceSize size) = 0;
73     virtual void invalidateMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset,
74                                         VkDeviceSize size)= 0;
75 
76     virtual void freeMemory(const GrVkBackendMemory&) = 0;
77 
78     // Returns the total amount of memory that is allocated and in use by an allocation for this
79     // allocator.
80     virtual uint64_t totalUsedMemory() const = 0;
81 
82     // Returns the total amount of memory that is allocated by this allocator.
83     virtual uint64_t totalAllocatedMemory() const = 0;
84 };
85 
86 GR_MAKE_BITFIELD_CLASS_OPS(GrVkMemoryAllocator::AllocationPropertyFlags)
87 
88 #endif
89