Flutter Engine
The Flutter Engine
VulkanMemoryAllocator.h
Go to the documentation of this file.
1/*
2 * Copyright 2022 Google LLC.
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 skgpu_VulkanMemoryAllocator_DEFINED
9#define skgpu_VulkanMemoryAllocator_DEFINED
10
14
15#include <cstdint>
16#include <utility>
17
18namespace skgpu {
19
21public:
24 // Allocation will be placed in its own VkDeviceMemory and not suballocated from some larger
25 // block.
27 // Says that the backing memory can only be accessed by the device. Additionally the device
28 // may lazily allocate the memory. This cannot be used with buffers that will be host
29 // visible. Setting this flag does not guarantee that we will allocate memory that respects
30 // it, but we will try to prefer memory that can respect it.
32 // The allocation will be mapped immediately and stay mapped until it is destroyed. This
33 // flag is only valid for buffers which are host visible (i.e. must have a usage other than
34 // BufferUsage::kGpuOnly).
36 // Allocation can only be accessed by the device using a protected context.
38 };
39
40 enum class BufferUsage {
41 // Buffers that will only be accessed from the device (large const buffers) will always be
42 // in device local memory.
43 kGpuOnly,
44 // Buffers that typically will be updated multiple times by the host and read on the gpu
45 // (e.g. uniform or vertex buffers). CPU writes will generally be sequential in the buffer
46 // and will try to take advantage of the write-combined nature of the gpu buffers. Thus this
47 // will always be mappable and coherent memory, and it will prefer to be in device local
48 // memory.
49 kCpuWritesGpuReads,
50 // Buffers that will be accessed on the host and copied to another GPU resource (transfer
51 // buffers). Will always be mappable and coherent memory.
52 kTransfersFromCpuToGpu,
53 // Buffers which are typically writted to by the GPU and then read on the host. Will always
54 // be mappable memory, and will prefer cached memory.
55 kTransfersFromGpuToCpu,
56 };
57
59 uint32_t allocationPropertyFlags,
60 skgpu::VulkanBackendMemory* memory) = 0;
61
64 uint32_t allocationPropertyFlags,
65 skgpu::VulkanBackendMemory* memory) = 0;
66
67 // Fills out the passed in skgpu::VulkanAlloc struct for the passed in
68 // skgpu::VulkanBackendMemory.
70
71 // Maps the entire allocation and returns a pointer to the start of the allocation. The
72 // implementation may map more memory than just the allocation, but the returned pointer must
73 // point at the start of the memory for the requested allocation.
74 virtual void* mapMemory(const skgpu::VulkanBackendMemory&) { return nullptr; }
75 virtual VkResult mapMemory(const skgpu::VulkanBackendMemory& memory, void** data) {
76 *data = this->mapMemory(memory);
77 // VK_ERROR_INITIALIZATION_FAILED is a bogus result to return from this function, but it is
78 // just something to return that is not VK_SUCCESS and can't be interpreted by a caller to
79 // mean something specific happened like device lost or oom. This will be removed once we
80 // update clients to implement this virtual.
82 }
83 virtual void unmapMemory(const skgpu::VulkanBackendMemory&) = 0;
84
85 // The following two calls are used for managing non-coherent memory. The offset is relative to
86 // the start of the allocation and not the underlying VkDeviceMemory. Additionaly the client
87 // must make sure that the offset + size passed in is less that or equal to the allocation size.
88 // It is the responsibility of the implementation to make sure all alignment requirements are
89 // followed. The client should not have to deal with any sort of alignment issues.
94 this->flushMappedMemory(memory, offset, size);
95 return VK_SUCCESS;
96 }
99 VkDeviceSize) {}
103 this->invalidateMappedMemory(memory, offset, size);
104 return VK_SUCCESS;
105 }
106
107 virtual void freeMemory(const skgpu::VulkanBackendMemory&) = 0;
108
109 // Returns the total amount of memory that is allocated as well as total
110 // amount of memory in use by an allocation from this allocator.
111 // Return 1st param is total allocated memory, 2nd is total used memory.
112 virtual std::pair<uint64_t, uint64_t> totalAllocatedAndUsedMemory() const = 0;
113};
114
115} // namespace skgpu
116
117#endif // skgpu_VulkanMemoryAllocator_DEFINED
virtual void unmapMemory(const skgpu::VulkanBackendMemory &)=0
virtual VkResult flushMemory(const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
virtual VkResult mapMemory(const skgpu::VulkanBackendMemory &memory, void **data)
virtual void invalidateMappedMemory(const skgpu::VulkanBackendMemory &, VkDeviceSize, VkDeviceSize)
virtual void flushMappedMemory(const skgpu::VulkanBackendMemory &, VkDeviceSize, VkDeviceSize)
virtual void * mapMemory(const skgpu::VulkanBackendMemory &)
virtual void freeMemory(const skgpu::VulkanBackendMemory &)=0
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 VkResult invalidateMemory(const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
virtual std::pair< uint64_t, uint64_t > totalAllocatedAndUsedMemory() const =0
sk_sp< const SkImage > image
Definition: SkRecords.h:269
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
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
Definition: GpuTools.h:21
intptr_t VulkanBackendMemory
Definition: VulkanTypes.h:32
static void usage(char *argv0)
SeparatedVector2 offset
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63
uint64_t VkDeviceSize
Definition: vulkan_core.h:96
VkResult
Definition: vulkan_core.h:140
@ VK_SUCCESS
Definition: vulkan_core.h:141
@ VK_ERROR_INITIALIZATION_FAILED
Definition: vulkan_core.h:149