1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#pragma once
6
7#include "flutter/flutter_vma/flutter_vma.h"
8
9#include "flutter/fml/memory/ref_ptr.h"
10#include "flutter/vulkan/procs/vulkan_proc_table.h"
11#include "third_party/skia/include/gpu/vk/GrVkBackendContext.h"
12
13namespace flutter {
14
15class FlutterSkiaVulkanMemoryAllocator : public skgpu::VulkanMemoryAllocator {
16 public:
17 static sk_sp<VulkanMemoryAllocator> Make(
18 uint32_t vulkan_api_version,
19 VkInstance instance,
20 VkPhysicalDevice physicalDevice,
21 VkDevice device,
22 const fml::RefPtr<vulkan::VulkanProcTable>& vk,
23 bool mustUseCoherentHostVisibleMemory);
24
25 ~FlutterSkiaVulkanMemoryAllocator() override;
26
27 VkResult allocateImageMemory(VkImage image,
28 uint32_t allocationPropertyFlags,
29 skgpu::VulkanBackendMemory*) override;
30
31 VkResult allocateBufferMemory(VkBuffer buffer,
32 BufferUsage usage,
33 uint32_t allocationPropertyFlags,
34 skgpu::VulkanBackendMemory*) override;
35
36 void freeMemory(const skgpu::VulkanBackendMemory&) override;
37
38 void getAllocInfo(const skgpu::VulkanBackendMemory&,
39 skgpu::VulkanAlloc*) const override;
40
41 VkResult mapMemory(const skgpu::VulkanBackendMemory&, void** data) override;
42 void unmapMemory(const skgpu::VulkanBackendMemory&) override;
43
44 VkResult flushMemory(const skgpu::VulkanBackendMemory&,
45 VkDeviceSize offset,
46 VkDeviceSize size) override;
47 VkResult invalidateMemory(const skgpu::VulkanBackendMemory&,
48 VkDeviceSize offset,
49 VkDeviceSize size) override;
50
51 std::pair<uint64_t, uint64_t> totalAllocatedAndUsedMemory() const override;
52
53 private:
54 FlutterSkiaVulkanMemoryAllocator(
55 fml::RefPtr<vulkan::VulkanProcTable> vk_proc_table,
56 VmaAllocator allocator,
57 bool mustUseCoherentHostVisibleMemory);
58
59 fml::RefPtr<vulkan::VulkanProcTable> vk_proc_table_;
60 VmaAllocator allocator_;
61
62 // For host visible allocations do we require they are coherent or not. All
63 // devices are required to support a host visible and coherent memory type.
64 // This is used to work around bugs for devices that don't handle non coherent
65 // memory correctly.
66 bool must_use_coherent_host_visible_memory_;
67};
68
69} // namespace flutter
70

source code of flutter_engine/flutter/flutter_vma/flutter_skia_vma.h