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 8 #ifndef skgpu_VulkanMemory_DEFINED 9 #define skgpu_VulkanMemory_DEFINED 10 11 #include "include/gpu/vk/VulkanMemoryAllocator.h" 12 #include "include/private/gpu/vk/SkiaVulkan.h" 13 #include "src/gpu/ganesh/vk/GrVkGpu.h" 14 15 #include <functional> 16 17 namespace skgpu { 18 enum class Protected : bool; 19 struct VulkanAlloc; 20 21 namespace VulkanMemory { 22 using CheckResult = bool(VkResult); 23 24 bool AllocBufferMemory(VulkanMemoryAllocator*, 25 VkBuffer buffer, 26 skgpu::Protected isProtected, 27 skgpu::VulkanMemoryAllocator::BufferUsage, 28 bool shouldPersistentlyMapCpuToGpu, 29 const std::function<CheckResult>&, 30 #ifdef SKIA_DFX_FOR_OHOS 31 VulkanAlloc* alloc, 32 size_t size); 33 #else 34 VulkanAlloc* alloc); 35 #endif 36 37 void FreeBufferMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc); 38 39 void FreeBufferMemory(const GrVkGpu* gpu, const VulkanAlloc& alloc); 40 41 bool AllocImageMemory(VulkanMemoryAllocator*, 42 VulkanMemoryAllocator*, 43 VkImage image, 44 skgpu::Protected isProtected, 45 bool forceDedicatedMemory, 46 bool useLazyAllocation, 47 const std::function<CheckResult>&, 48 VulkanAlloc* alloc, 49 int memorySize); 50 51 void FreeImageMemory(VulkanMemoryAllocator*, const VulkanAlloc& alloc); 52 53 bool ImportAndBindBufferMemory(GrVkGpu* gpu, 54 OH_NativeBuffer *nativeBuffer, 55 VkBuffer buffer, 56 VulkanAlloc* alloc); 57 58 // Maps the entire skgpu::VulkanAlloc and returns a pointer to the start of the allocation. 59 // Underneath the hood, we may map more than the range of the skgpu::VulkanAlloc (e.g. the 60 // entire VkDeviceMemory), but the pointer returned will always be to the start of the 61 // skgpu::VulkanAlloc. The caller should also never assume more than the skgpu::VulkanAlloc 62 // block has been mapped. 63 void* MapAlloc(VulkanMemoryAllocator*, 64 const VulkanAlloc&, 65 const std::function<CheckResult>&); 66 void UnmapAlloc(VulkanMemoryAllocator*, const VulkanAlloc& alloc); 67 68 // For the Flush and Invalidate calls, the offset should be relative to the skgpu::VulkanAlloc. 69 // Thus this will often be 0. The client does not need to make sure the offset and size are 70 // aligned to the nonCoherentAtomSize, the internal calls will handle that. 71 void FlushMappedAlloc(VulkanMemoryAllocator*, 72 const skgpu::VulkanAlloc&, 73 VkDeviceSize offset, 74 VkDeviceSize size, 75 const std::function<CheckResult>&); 76 void InvalidateMappedAlloc(VulkanMemoryAllocator*, 77 const VulkanAlloc& alloc, 78 VkDeviceSize offset, 79 VkDeviceSize size, 80 const std::function<CheckResult>&); 81 82 // Helper for aligning and setting VkMappedMemoryRange for flushing/invalidating noncoherent 83 // memory. 84 void GetNonCoherentMappedMemoryRange(const VulkanAlloc&, 85 VkDeviceSize offset, 86 VkDeviceSize size, 87 VkDeviceSize alignment, 88 VkMappedMemoryRange*); 89 } // namespace VulkanMemory 90 91 } // namespace skgpu 92 93 #endif 94