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 "include/core/SkRefCnt.h" 12 #include "include/gpu/GrTypes.h" 13 #include "include/gpu/vk/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 // Allocation can only be accessed by the device using a protected context. 32 kProtected = 0x8, 33 }; 34 35 GR_DECL_BITFIELD_CLASS_OPS_FRIENDS(AllocationPropertyFlags); 36 37 enum class BufferUsage { 38 // Buffers that will only be accessed from the device (large const buffers). Will always be 39 // in device local memory. 40 kGpuOnly, 41 // Buffers that will be accessed on the host and copied to and from a GPU resource (transfer 42 // buffers). Will always be mappable and coherent memory. 43 kCpuOnly, 44 // Buffers that typically will be updated multiple times by the host and read on the gpu 45 // (e.g. uniform or vertex buffers). Will always be mappable memory, and will prefer to be 46 // in device local memory. 47 kCpuWritesGpuReads, 48 // Buffers which are typically writted to by the GPU and then read on the host. Will always 49 // be mappable memory, and will prefer coherent and cached memory. 50 kGpuWritesCpuReads, 51 }; 52 53 virtual bool allocateMemoryForImage(VkImage image, AllocationPropertyFlags flags, 54 GrVkBackendMemory*) = 0; 55 56 virtual bool allocateMemoryForBuffer(VkBuffer buffer, BufferUsage usage, 57 AllocationPropertyFlags flags, GrVkBackendMemory*) = 0; 58 59 // Fills out the passed in GrVkAlloc struct for the passed in GrVkBackendMemory. 60 virtual void getAllocInfo(const GrVkBackendMemory&, GrVkAlloc*) const = 0; 61 62 // Maps the entire allocation and returns a pointer to the start of the allocation. The 63 // implementation may map more memory than just the allocation, but the returned pointer must 64 // point at the start of the memory for the requested allocation. 65 virtual void* mapMemory(const GrVkBackendMemory&) = 0; 66 virtual void unmapMemory(const GrVkBackendMemory&) = 0; 67 68 // The following two calls are used for managing non-coherent memory. The offset is relative to 69 // the start of the allocation and not the underlying VkDeviceMemory. Additionaly the client 70 // must make sure that the offset + size passed in is less that or equal to the allocation size. 71 // It is the responsibility of the implementation to make sure all alignment requirements are 72 // followed. The client should not have to deal with any sort of alignment issues. 73 virtual void flushMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset, 74 VkDeviceSize size) = 0; 75 virtual void invalidateMappedMemory(const GrVkBackendMemory&, VkDeviceSize offset, 76 VkDeviceSize size)= 0; 77 78 virtual void freeMemory(const GrVkBackendMemory&) = 0; 79 80 // Returns the total amount of memory that is allocated and in use by an allocation for this 81 // allocator. 82 virtual uint64_t totalUsedMemory() const = 0; 83 84 // Returns the total amount of memory that is allocated by this allocator. 85 virtual uint64_t totalAllocatedMemory() const = 0; 86 }; 87 88 GR_MAKE_BITFIELD_CLASS_OPS(GrVkMemoryAllocator::AllocationPropertyFlags) 89 90 #endif 91