Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | List of all members
skgpu::VulkanAMDMemoryAllocator Class Reference

#include <VulkanAMDMemoryAllocator.h>

Inheritance diagram for skgpu::VulkanAMDMemoryAllocator:
skgpu::VulkanMemoryAllocator SkRefCnt SkRefCntBase

Public Member Functions

 ~VulkanAMDMemoryAllocator () override
 
VkResult allocateImageMemory (VkImage image, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *) override
 
VkResult allocateBufferMemory (VkBuffer buffer, BufferUsage usage, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *) override
 
void freeMemory (const VulkanBackendMemory &) override
 
void getAllocInfo (const VulkanBackendMemory &, VulkanAlloc *) const override
 
VkResult mapMemory (const VulkanBackendMemory &, void **data) override
 
void unmapMemory (const VulkanBackendMemory &) override
 
VkResult flushMemory (const VulkanBackendMemory &, VkDeviceSize offset, VkDeviceSize size) override
 
VkResult invalidateMemory (const VulkanBackendMemory &, VkDeviceSize offset, VkDeviceSize size) override
 
std::pair< uint64_t, uint64_t > totalAllocatedAndUsedMemory () const override
 
- Public Member Functions inherited from skgpu::VulkanMemoryAllocator
virtual VkResult allocateImageMemory (VkImage image, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *memory)=0
 
virtual VkResult allocateBufferMemory (VkBuffer buffer, BufferUsage usage, uint32_t allocationPropertyFlags, skgpu::VulkanBackendMemory *memory)=0
 
virtual void getAllocInfo (const skgpu::VulkanBackendMemory &, skgpu::VulkanAlloc *) const =0
 
virtual void * mapMemory (const skgpu::VulkanBackendMemory &)
 
virtual VkResult mapMemory (const skgpu::VulkanBackendMemory &memory, void **data)
 
virtual void unmapMemory (const skgpu::VulkanBackendMemory &)=0
 
virtual void flushMappedMemory (const skgpu::VulkanBackendMemory &, VkDeviceSize, VkDeviceSize)
 
virtual VkResult flushMemory (const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
 
virtual void invalidateMappedMemory (const skgpu::VulkanBackendMemory &, VkDeviceSize, VkDeviceSize)
 
virtual VkResult invalidateMemory (const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
 
virtual void freeMemory (const skgpu::VulkanBackendMemory &)=0
 
virtual std::pair< uint64_t, uint64_t > totalAllocatedAndUsedMemory () const =0
 
- Public Member Functions inherited from SkRefCntBase
 SkRefCntBase ()
 
virtual ~SkRefCntBase ()
 
bool unique () const
 
void ref () const
 
void unref () const
 

Static Public Member Functions

static sk_sp< VulkanMemoryAllocatorMake (VkInstance instance, VkPhysicalDevice physicalDevice, VkDevice device, uint32_t physicalDeviceVersion, const VulkanExtensions *extensions, const VulkanInterface *interface, ThreadSafe)
 

Additional Inherited Members

- Public Types inherited from skgpu::VulkanMemoryAllocator
enum  AllocationPropertyFlags {
  kNone_AllocationPropertyFlag = 0b0000 , kDedicatedAllocation_AllocationPropertyFlag = 0b0001 , kLazyAllocation_AllocationPropertyFlag = 0b0010 , kPersistentlyMapped_AllocationPropertyFlag = 0b0100 ,
  kProtected_AllocationPropertyFlag = 0b1000
}
 
enum class  BufferUsage { kGpuOnly , kCpuWritesGpuReads , kTransfersFromCpuToGpu , kTransfersFromGpuToCpu }
 

Detailed Description

Definition at line 30 of file VulkanAMDMemoryAllocator.h.

Constructor & Destructor Documentation

◆ ~VulkanAMDMemoryAllocator()

skgpu::VulkanAMDMemoryAllocator::~VulkanAMDMemoryAllocator ( )
override

Definition at line 100 of file VulkanAMDMemoryAllocator.cpp.

100 {
101 vmaDestroyAllocator(fAllocator);
102 fAllocator = VK_NULL_HANDLE;
103}
#define VK_NULL_HANDLE
Definition: vulkan_core.h:46

Member Function Documentation

◆ allocateBufferMemory()

VkResult skgpu::VulkanAMDMemoryAllocator::allocateBufferMemory ( VkBuffer  buffer,
BufferUsage  usage,
uint32_t  allocationPropertyFlags,
skgpu::VulkanBackendMemory backendMemory 
)
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 136 of file VulkanAMDMemoryAllocator.cpp.

139 {
140 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
141 VmaAllocationCreateInfo info;
142 info.flags = 0;
143 info.usage = VMA_MEMORY_USAGE_UNKNOWN;
144 info.memoryTypeBits = 0;
145 info.pool = VK_NULL_HANDLE;
146 info.pUserData = nullptr;
147
148 switch (usage) {
151 info.preferredFlags = 0;
152 break;
154 // When doing cpu writes and gpu reads the general rule of thumb is to use coherent
155 // memory. Though this depends on the fact that we are not doing any cpu reads and the
156 // cpu writes are sequential. For sparse writes we'd want cpu cached memory, however we
157 // don't do these types of writes in Skia.
158 //
159 // TODO: In the future there may be times where specific types of memory could benefit
160 // from a coherent and cached memory. Typically these allow for the gpu to read cpu
161 // writes from the cache without needing to flush the writes throughout the cache. The
162 // reverse is not true and GPU writes tend to invalidate the cache regardless. Also
163 // these gpu cache read access are typically lower bandwidth than non-cached memory.
164 // For now Skia doesn't really have a need or want of this type of memory. But if we
165 // ever do we could pass in an AllocationPropertyFlag that requests the cached property.
166 info.requiredFlags =
169 break;
171 info.requiredFlags =
173 info.preferredFlags = 0;
174 break;
178 break;
179 }
180
181 if (kDedicatedAllocation_AllocationPropertyFlag & allocationPropertyFlags) {
182 info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
183 }
184 if ((kLazyAllocation_AllocationPropertyFlag & allocationPropertyFlags) &&
187 }
188
189 if (kPersistentlyMapped_AllocationPropertyFlag & allocationPropertyFlags) {
191 info.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
192 }
193
194 VmaAllocation allocation;
195 VkResult result = vmaAllocateMemoryForBuffer(fAllocator, buffer, &info, &allocation, nullptr);
196 if (VK_SUCCESS == result) {
197 *backendMemory = (VulkanBackendMemory)allocation;
198 }
199
200 return result;
201}
static void info(const char *fmt,...) SK_PRINTF_LIKE(1
Definition: DM.cpp:213
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define TRACE_FUNC
Definition: SkTraceEvent.h:30
GAsyncResult * result
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
intptr_t VulkanBackendMemory
Definition: VulkanTypes.h:32
static void usage(char *argv0)
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:131
@ VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT
Definition: vulkan_core.h:2403
@ VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
Definition: vulkan_core.h:2401
@ VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
Definition: vulkan_core.h:2399
@ VK_MEMORY_PROPERTY_HOST_CACHED_BIT
Definition: vulkan_core.h:2402
@ VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
Definition: vulkan_core.h:2400
VkResult
Definition: vulkan_core.h:140
@ VK_SUCCESS
Definition: vulkan_core.h:141

◆ allocateImageMemory()

VkResult skgpu::VulkanAMDMemoryAllocator::allocateImageMemory ( VkImage  image,
uint32_t  allocationPropertyFlags,
skgpu::VulkanBackendMemory backendMemory 
)
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 105 of file VulkanAMDMemoryAllocator.cpp.

107 {
108 TRACE_EVENT0_ALWAYS("skia.gpu", TRACE_FUNC);
109 VmaAllocationCreateInfo info;
110 info.flags = 0;
111 info.usage = VMA_MEMORY_USAGE_UNKNOWN;
113 info.preferredFlags = 0;
114 info.memoryTypeBits = 0;
115 info.pool = VK_NULL_HANDLE;
116 info.pUserData = nullptr;
117
118 if (kDedicatedAllocation_AllocationPropertyFlag & allocationPropertyFlags) {
119 info.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
120 }
121 if (kLazyAllocation_AllocationPropertyFlag & allocationPropertyFlags) {
123 }
124 if (kProtected_AllocationPropertyFlag & allocationPropertyFlags) {
125 info.requiredFlags |= VK_MEMORY_PROPERTY_PROTECTED_BIT;
126 }
127
128 VmaAllocation allocation;
129 VkResult result = vmaAllocateMemoryForImage(fAllocator, image, &info, &allocation, nullptr);
130 if (VK_SUCCESS == result) {
131 *backendMemory = (VulkanBackendMemory)allocation;
132 }
133 return result;
134}
#define TRACE_EVENT0_ALWAYS(category_group, name)
sk_sp< const SkImage > image
Definition: SkRecords.h:269
@ VK_MEMORY_PROPERTY_PROTECTED_BIT
Definition: vulkan_core.h:2404

◆ flushMemory()

VkResult skgpu::VulkanAMDMemoryAllocator::flushMemory ( const VulkanBackendMemory memoryHandle,
VkDeviceSize  offset,
VkDeviceSize  size 
)
overridevirtual

Reimplemented from skgpu::VulkanMemoryAllocator.

Definition at line 249 of file VulkanAMDMemoryAllocator.cpp.

250 {
251 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
252 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
253 return vmaFlushAllocation(fAllocator, allocation, offset, size);
254}
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
SeparatedVector2 offset

◆ freeMemory()

void skgpu::VulkanAMDMemoryAllocator::freeMemory ( const VulkanBackendMemory memoryHandle)
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 203 of file VulkanAMDMemoryAllocator.cpp.

203 {
204 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
205 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
206 vmaFreeMemory(fAllocator, allocation);
207}

◆ getAllocInfo()

void skgpu::VulkanAMDMemoryAllocator::getAllocInfo ( const VulkanBackendMemory memoryHandle,
VulkanAlloc alloc 
) const
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 209 of file VulkanAMDMemoryAllocator.cpp.

210 {
211 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
212 VmaAllocationInfo vmaInfo;
213 vmaGetAllocationInfo(fAllocator, allocation, &vmaInfo);
214
215 VkMemoryPropertyFlags memFlags;
216 vmaGetMemoryTypeProperties(fAllocator, vmaInfo.memoryType, &memFlags);
217
218 uint32_t flags = 0;
221 }
224 }
227 }
228
229 alloc->fMemory = vmaInfo.deviceMemory;
230 alloc->fOffset = vmaInfo.offset;
231 alloc->fSize = vmaInfo.size;
232 alloc->fFlags = flags;
233 alloc->fBackendMemory = memoryHandle;
234}
static constexpr bool SkToBool(const T &x)
Definition: SkTo.h:35
FlutterSemanticsFlag flags
VkFlags VkMemoryPropertyFlags
Definition: vulkan_core.h:2410

◆ invalidateMemory()

VkResult skgpu::VulkanAMDMemoryAllocator::invalidateMemory ( const VulkanBackendMemory memoryHandle,
VkDeviceSize  offset,
VkDeviceSize  size 
)
overridevirtual

Reimplemented from skgpu::VulkanMemoryAllocator.

Definition at line 256 of file VulkanAMDMemoryAllocator.cpp.

257 {
258 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
259 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
260 return vmaInvalidateAllocation(fAllocator, allocation, offset, size);
261}

◆ Make()

sk_sp< VulkanMemoryAllocator > skgpu::VulkanAMDMemoryAllocator::Make ( VkInstance  instance,
VkPhysicalDevice  physicalDevice,
VkDevice  device,
uint32_t  physicalDeviceVersion,
const VulkanExtensions extensions,
const VulkanInterface interface,
ThreadSafe  threadSafe 
)
static

Definition at line 21 of file VulkanAMDMemoryAllocator.cpp.

27 {
28#define SKGPU_COPY_FUNCTION(NAME) functions.vk##NAME = interface->fFunctions.f##NAME
29#define SKGPU_COPY_FUNCTION_KHR(NAME) functions.vk##NAME##KHR = interface->fFunctions.f##NAME
30
31 VmaVulkanFunctions functions;
32 // We should be setting all the required functions (at least through vulkan 1.1), but this is
33 // just extra belt and suspenders to make sure there isn't unitialized values here.
34 std::memset(&functions, 0, sizeof(VmaVulkanFunctions));
35
36 // We don't use dynamic function getting in the allocator so we set the getProc functions to
37 // null.
38 functions.vkGetInstanceProcAddr = nullptr;
39 functions.vkGetDeviceProcAddr = nullptr;
40 SKGPU_COPY_FUNCTION(GetPhysicalDeviceProperties);
41 SKGPU_COPY_FUNCTION(GetPhysicalDeviceMemoryProperties);
42 SKGPU_COPY_FUNCTION(AllocateMemory);
43 SKGPU_COPY_FUNCTION(FreeMemory);
44 SKGPU_COPY_FUNCTION(MapMemory);
45 SKGPU_COPY_FUNCTION(UnmapMemory);
46 SKGPU_COPY_FUNCTION(FlushMappedMemoryRanges);
47 SKGPU_COPY_FUNCTION(InvalidateMappedMemoryRanges);
48 SKGPU_COPY_FUNCTION(BindBufferMemory);
49 SKGPU_COPY_FUNCTION(BindImageMemory);
50 SKGPU_COPY_FUNCTION(GetBufferMemoryRequirements);
51 SKGPU_COPY_FUNCTION(GetImageMemoryRequirements);
52 SKGPU_COPY_FUNCTION(CreateBuffer);
53 SKGPU_COPY_FUNCTION(DestroyBuffer);
54 SKGPU_COPY_FUNCTION(CreateImage);
55 SKGPU_COPY_FUNCTION(DestroyImage);
56 SKGPU_COPY_FUNCTION(CmdCopyBuffer);
57 SKGPU_COPY_FUNCTION_KHR(GetBufferMemoryRequirements2);
58 SKGPU_COPY_FUNCTION_KHR(GetImageMemoryRequirements2);
59 SKGPU_COPY_FUNCTION_KHR(BindBufferMemory2);
60 SKGPU_COPY_FUNCTION_KHR(BindImageMemory2);
61 SKGPU_COPY_FUNCTION_KHR(GetPhysicalDeviceMemoryProperties2);
62
63 VmaAllocatorCreateInfo info;
64 info.flags = 0;
65 if (threadSafe == ThreadSafe::kNo) {
66 info.flags |= VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT;
67 }
68 if (physicalDeviceVersion >= VK_MAKE_VERSION(1, 1, 0) ||
71 info.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
72 }
73
74 info.physicalDevice = physicalDevice;
75 info.device = device;
76 // 4MB was picked for the size here by looking at memory usage of Android apps and runs of DM.
77 // It seems to be a good compromise of not wasting unused allocated space and not making too
78 // many small allocations. The AMD allocator will start making blocks at 1/8 the max size and
79 // builds up block size as needed before capping at the max set here.
80 info.preferredLargeHeapBlockSize = 4*1024*1024;
81 info.pAllocationCallbacks = nullptr;
82 info.pDeviceMemoryCallbacks = nullptr;
83 info.pHeapSizeLimit = nullptr;
84 info.pVulkanFunctions = &functions;
85 info.instance = instance;
86 // TODO: Update our interface and headers to support vulkan 1.3 and add in the new required
87 // functions for 1.3 that the allocator needs. Until then we just clamp the version to 1.1.
88 info.vulkanApiVersion = std::min(physicalDeviceVersion, VK_MAKE_VERSION(1, 1, 0));
89 info.pTypeExternalMemoryHandleTypes = nullptr;
90
91 VmaAllocator allocator;
92 vmaCreateAllocator(&info, &allocator);
93
94 return sk_sp<VulkanAMDMemoryAllocator>(new VulkanAMDMemoryAllocator(allocator));
95}
#define SKGPU_COPY_FUNCTION_KHR(NAME)
#define SKGPU_COPY_FUNCTION(NAME)
VkDevice device
Definition: main.cc:53
VkInstance instance
Definition: main.cc:48
static float min(float r, float g, float b)
Definition: hsl.cpp:48
#define VK_MAKE_VERSION(major, minor, patch)
Definition: vulkan_core.h:78
#define VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME
Definition: vulkan_core.h:9396
#define VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME
Definition: vulkan_core.h:9374

◆ mapMemory()

VkResult skgpu::VulkanAMDMemoryAllocator::mapMemory ( const VulkanBackendMemory memoryHandle,
void **  data 
)
overridevirtual

Reimplemented from skgpu::VulkanMemoryAllocator.

Definition at line 236 of file VulkanAMDMemoryAllocator.cpp.

237 {
238 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
239 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
240 return vmaMapMemory(fAllocator, allocation, data);
241}
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63

◆ totalAllocatedAndUsedMemory()

std::pair< uint64_t, uint64_t > skgpu::VulkanAMDMemoryAllocator::totalAllocatedAndUsedMemory ( ) const
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 263 of file VulkanAMDMemoryAllocator.cpp.

263 {
264 VmaTotalStatistics stats;
265 vmaCalculateStatistics(fAllocator, &stats);
266 return {stats.total.statistics.blockBytes, stats.total.statistics.allocationBytes};
267}
dictionary stats
Definition: malisc.py:20

◆ unmapMemory()

void skgpu::VulkanAMDMemoryAllocator::unmapMemory ( const VulkanBackendMemory memoryHandle)
overridevirtual

Implements skgpu::VulkanMemoryAllocator.

Definition at line 243 of file VulkanAMDMemoryAllocator.cpp.

243 {
244 TRACE_EVENT0("skia.gpu", TRACE_FUNC);
245 const VmaAllocation allocation = (VmaAllocation)memoryHandle;
246 vmaUnmapMemory(fAllocator, allocation);
247}

The documentation for this class was generated from the following files: