1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci* Copyright 2015 Google Inc.
3cb93a386Sopenharmony_ci*
4cb93a386Sopenharmony_ci* Use of this source code is governed by a BSD-style license that can be
5cb93a386Sopenharmony_ci* found in the LICENSE file.
6cb93a386Sopenharmony_ci*/
7cb93a386Sopenharmony_ci
8cb93a386Sopenharmony_ci#ifndef GrVkMemory_DEFINED
9cb93a386Sopenharmony_ci#define GrVkMemory_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include <map>
12cb93a386Sopenharmony_ci#include <mutex>
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkMemoryAllocator.h"
15cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
16cb93a386Sopenharmony_ci#include "include/private/GrTypesPriv.h"
17cb93a386Sopenharmony_ci#include "include/private/SkTArray.h"
18cb93a386Sopenharmony_ci
19cb93a386Sopenharmony_ciclass GrVkGpu;
20cb93a386Sopenharmony_ci
21cb93a386Sopenharmony_cinamespace GrVkMemory {
22cb93a386Sopenharmony_ci    /**
23cb93a386Sopenharmony_ci    * Allocates vulkan device memory and binds it to the gpu's device for the given object.
24cb93a386Sopenharmony_ci    * Returns true if allocation succeeded.
25cb93a386Sopenharmony_ci    */
26cb93a386Sopenharmony_ci    bool AllocAndBindBufferMemory(GrVkGpu* gpu,
27cb93a386Sopenharmony_ci                                  VkBuffer buffer,
28cb93a386Sopenharmony_ci                                  GrVkMemoryAllocator::BufferUsage,
29cb93a386Sopenharmony_ci#ifdef SKIA_DFX_FOR_OHOS
30cb93a386Sopenharmony_ci                                  GrVkAlloc* alloc,
31cb93a386Sopenharmony_ci                                  size_t size);
32cb93a386Sopenharmony_ci#else
33cb93a386Sopenharmony_ci                                  GrVkAlloc* alloc);
34cb93a386Sopenharmony_ci#endif
35cb93a386Sopenharmony_ci
36cb93a386Sopenharmony_ci    bool ImportAndBindBufferMemory(GrVkGpu* gpu,
37cb93a386Sopenharmony_ci                                   OH_NativeBuffer *nativeBuffer,
38cb93a386Sopenharmony_ci                                   VkBuffer buffer,
39cb93a386Sopenharmony_ci                                   GrVkAlloc* alloc);
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    void FreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc);
42cb93a386Sopenharmony_ci
43cb93a386Sopenharmony_ci    bool AllocAndBindImageMemory(GrVkGpu* gpu,
44cb93a386Sopenharmony_ci                                 VkImage image,
45cb93a386Sopenharmony_ci                                 GrMemoryless,
46cb93a386Sopenharmony_ci                                 GrVkAlloc* alloc,
47cb93a386Sopenharmony_ci                                 int memorySize);
48cb93a386Sopenharmony_ci    void FreeImageMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc);
49cb93a386Sopenharmony_ci
50cb93a386Sopenharmony_ci    // Maps the entire GrVkAlloc and returns a pointer to the start of the allocation. Underneath
51cb93a386Sopenharmony_ci    // the hood, we may map more than the range of the GrVkAlloc (e.g. the entire VkDeviceMemory),
52cb93a386Sopenharmony_ci    // but the pointer returned will always be to the start of the GrVkAlloc. The caller should also
53cb93a386Sopenharmony_ci    // never assume more than the GrVkAlloc block has been mapped.
54cb93a386Sopenharmony_ci    void* MapAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc);
55cb93a386Sopenharmony_ci    void UnmapAlloc(const GrVkGpu* gpu, const GrVkAlloc& alloc);
56cb93a386Sopenharmony_ci
57cb93a386Sopenharmony_ci    // For the Flush and Invalidate calls, the offset should be relative to the GrVkAlloc. Thus this
58cb93a386Sopenharmony_ci    // will often be 0. The client does not need to make sure the offset and size are aligned to the
59cb93a386Sopenharmony_ci    // nonCoherentAtomSize, the internal calls will handle that.
60cb93a386Sopenharmony_ci    void FlushMappedAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc, VkDeviceSize offset,
61cb93a386Sopenharmony_ci                          VkDeviceSize size);
62cb93a386Sopenharmony_ci    void InvalidateMappedAlloc(GrVkGpu* gpu, const GrVkAlloc& alloc, VkDeviceSize offset,
63cb93a386Sopenharmony_ci                               VkDeviceSize size);
64cb93a386Sopenharmony_ci
65cb93a386Sopenharmony_ci    // Helper for aligning and setting VkMappedMemoryRange for flushing/invalidating noncoherent
66cb93a386Sopenharmony_ci    // memory.
67cb93a386Sopenharmony_ci    void GetNonCoherentMappedMemoryRange(const GrVkAlloc&, VkDeviceSize offset, VkDeviceSize size,
68cb93a386Sopenharmony_ci                                         VkDeviceSize alignment, VkMappedMemoryRange*);
69cb93a386Sopenharmony_ci}  // namespace GrVkMemory
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci#endif
72