Flutter Engine
The Flutter Engine
VulkanMemory.cpp
Go to the documentation of this file.
1/*
2* Copyright 2015 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
13
14#include <cstdint>
15#include <cstring>
16
17namespace skgpu {
18
20
22 VkBuffer buffer,
24 bool shouldPersistentlyMapCpuToGpu,
25 const std::function<CheckResult>& checkResult,
26 VulkanAlloc* alloc) {
27 VulkanBackendMemory memory = 0;
28 uint32_t propFlags;
30 (usage == BufferUsage::kCpuWritesGpuReads && shouldPersistentlyMapCpuToGpu)) {
31 // In general it is always fine (and often better) to keep buffers always mapped that we are
32 // writing to on the cpu.
34 } else {
36 }
37
38 VkResult result = allocator->allocateBufferMemory(buffer, usage, propFlags, &memory);
39 if (!checkResult(result)) {
40 return false;
41 }
42 allocator->getAllocInfo(memory, alloc);
43 return true;
44}
45
48 allocator->freeMemory(alloc.fBackendMemory);
49}
50
52 VkImage image,
53 Protected isProtected,
54 bool forceDedicatedMemory,
55 bool useLazyAllocation,
56 const std::function<CheckResult>& checkResult,
57 VulkanAlloc* alloc) {
58 VulkanBackendMemory memory = 0;
59
60 uint32_t propFlags;
61 // If we ever find that our allocator is not aggressive enough in using dedicated image
62 // memory we can add a size check here to force the use of dedicate memory. However for now,
63 // we let the allocators decide. The allocator can query the GPU for each image to see if the
64 // GPU recommends or requires the use of dedicated memory.
65 if (forceDedicatedMemory) {
67 } else {
69 }
70
71 if (isProtected == Protected::kYes) {
73 }
74
75 if (useLazyAllocation) {
77 }
78
79 VkResult result = allocator->allocateImageMemory(image, propFlags, &memory);
80 if (!checkResult(result)) {
81 return false;
82 }
83
84 allocator->getAllocInfo(memory, alloc);
85 return true;
86}
87
89 const VulkanAlloc& alloc) {
91 allocator->freeMemory(alloc.fBackendMemory);
92}
93
95 const VulkanAlloc& alloc,
96 const std::function<CheckResult>& checkResult) {
99 void* mapPtr;
100 VkResult result = allocator->mapMemory(alloc.fBackendMemory, &mapPtr);
101 if (!checkResult(result)) {
102 return nullptr;
103 }
104 return mapPtr;
105}
106
108 const VulkanAlloc& alloc) {
110 allocator->unmapMemory(alloc.fBackendMemory);
111}
112
116 VkDeviceSize alignment,
117 VkMappedMemoryRange* range) {
119 offset = offset + alloc.fOffset;
120 VkDeviceSize offsetDiff = offset & (alignment -1);
121 offset = offset - offsetDiff;
122 size = (size + alignment - 1) & ~(alignment - 1);
123#ifdef SK_DEBUG
124 SkASSERT(offset >= alloc.fOffset);
125 SkASSERT(offset + size <= alloc.fOffset + alloc.fSize);
126 SkASSERT(0 == (offset & (alignment-1)));
127 SkASSERT(size > 0);
128 SkASSERT(0 == (size & (alignment-1)));
129#endif
130
131 std::memset(range, 0, sizeof(VkMappedMemoryRange));
133 range->memory = alloc.fMemory;
134 range->offset = offset;
135 range->size = size;
136}
137
139 const VulkanAlloc& alloc,
142 const std::function<CheckResult>& checkResult) {
144 SkASSERT(offset == 0);
145 SkASSERT(size <= alloc.fSize);
147 VkResult result = allocator->flushMemory(alloc.fBackendMemory, offset, size);
148 checkResult(result);
149 }
150}
151
153 const VulkanAlloc& alloc,
156 const std::function<CheckResult>& checkResult) {
158 SkASSERT(offset == 0);
159 SkASSERT(size <= alloc.fSize);
162 checkResult(result);
163 }
164}
165
166} // namespace skgpu
167
#define SkASSERT(cond)
Definition: SkAssert.h:116
virtual void unmapMemory(const skgpu::VulkanBackendMemory &)=0
virtual VkResult flushMemory(const skgpu::VulkanBackendMemory &memory, VkDeviceSize offset, VkDeviceSize size)
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)
GAsyncResult * result
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
void FreeBufferMemory(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
bool AllocBufferMemory(VulkanMemoryAllocator *, VkBuffer buffer, skgpu::VulkanMemoryAllocator::BufferUsage, bool shouldPersistentlyMapCpuToGpu, const std::function< CheckResult > &, VulkanAlloc *alloc)
void GetNonCoherentMappedMemoryRange(const VulkanAlloc &, VkDeviceSize offset, VkDeviceSize size, VkDeviceSize alignment, VkMappedMemoryRange *)
void FreeImageMemory(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
void * MapAlloc(VulkanMemoryAllocator *, const VulkanAlloc &, const std::function< CheckResult > &)
void FlushMappedAlloc(VulkanMemoryAllocator *, const skgpu::VulkanAlloc &, VkDeviceSize offset, VkDeviceSize size, const std::function< CheckResult > &)
bool AllocImageMemory(VulkanMemoryAllocator *, VkImage image, skgpu::Protected isProtected, bool forceDedicatedMemory, bool useLazyAllocation, const std::function< CheckResult > &, VulkanAlloc *alloc)
void UnmapAlloc(VulkanMemoryAllocator *, const VulkanAlloc &alloc)
void InvalidateMappedAlloc(VulkanMemoryAllocator *, const VulkanAlloc &alloc, VkDeviceSize offset, VkDeviceSize size, const std::function< CheckResult > &)
Definition: GpuTools.h:21
intptr_t VulkanBackendMemory
Definition: VulkanTypes.h:32
Protected
Definition: GpuTypes.h:61
static void usage(char *argv0)
SeparatedVector2 offset
VkDeviceSize offset
Definition: vulkan_core.h:3298
VkDeviceMemory memory
Definition: vulkan_core.h:3297
VkDeviceSize size
Definition: vulkan_core.h:3299
VkStructureType sType
Definition: vulkan_core.h:3295
VkDeviceSize fSize
Definition: VulkanTypes.h:41
VulkanBackendMemory fBackendMemory
Definition: VulkanTypes.h:44
VkDeviceMemory fMemory
Definition: VulkanTypes.h:39
VkDeviceSize fOffset
Definition: VulkanTypes.h:40
uint64_t VkDeviceSize
Definition: vulkan_core.h:96
VkResult
Definition: vulkan_core.h:140
@ VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE
Definition: vulkan_core.h:208