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