• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
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 GrVkBuffer_DEFINED
9 #define GrVkBuffer_DEFINED
10 
11 #include "include/core/SkRefCnt.h"
12 #include "include/gpu/vk/VulkanTypes.h"
13 #include "include/private/base/SkTo.h"
14 #include "include/private/gpu/ganesh/GrTypesPriv.h"
15 #include "include/private/gpu/vk/SkiaVulkan.h"
16 #include "src/gpu/ganesh/GrGpuBuffer.h"
17 
18 #include <cstddef>
19 #include <string_view>
20 
21 class GrVkDescriptorSet;
22 class GrVkGpu;
23 
24 class GrVkBuffer : public GrGpuBuffer {
25 public:
26     static sk_sp<GrVkBuffer> Make(GrVkGpu* gpu,
27                                   size_t size,
28                                   GrGpuBufferType bufferType,
29                                   GrAccessPattern accessPattern);
30 
31     static sk_sp<GrVkBuffer> MakeFromOHNativeBuffer(GrVkGpu* gpu,
32                                                     OH_NativeBuffer *nativeBuffer,
33                                                     size_t bufferSize,
34                                                     GrGpuBufferType bufferType,
35                                                     GrAccessPattern accessPattern);
36 
vkBuffer()37     VkBuffer vkBuffer() const { return fBuffer; }
38 
39     void addMemoryBarrier(VkAccessFlags srcAccessMask,
40                           VkAccessFlags dstAccesMask,
41                           VkPipelineStageFlags srcStageMask,
42                           VkPipelineStageFlags dstStageMask,
43                           bool byRegion) const;
44 
45     // If the buffer is a uniform buffer, return the descriptor set for that buffer. It is not valid
46     // to call this on non uniform buffers.
47     const VkDescriptorSet* uniformDescriptorSet() const;
48 
49 private:
50     GrVkBuffer(GrVkGpu* gpu,
51                size_t sizeInBytes,
52                GrGpuBufferType bufferType,
53                GrAccessPattern accessPattern,
54                VkBuffer buffer,
55                const skgpu::VulkanAlloc& alloc,
56                const GrVkDescriptorSet* uniformDescriptorSet,
57                std::string_view label);
58 
isVkMappable()59     bool isVkMappable() const { return fAlloc.fFlags & skgpu::VulkanAlloc::kMappable_Flag; }
60 
vkIsMapped()61     bool vkIsMapped() const { return SkToBool(fMapPtr); }
62     void vkMap(size_t readOffset, size_t readSize);
63     void vkUnmap(size_t flushOffset, size_t flushSize);
64     void copyCpuDataToGpuBuffer(const void* srcData, size_t offset, size_t size);
65 
66     void onMap(MapType) override;
67     void onUnmap(MapType) override;
68     bool onClearToZero() override;
69     bool onUpdateData(const void* src, size_t offset, size_t size, bool preserve) override;
70 
71     void vkRelease();
72 
73     void onAbandon() override;
74     void onRelease() override;
75 
76     GrVkGpu* getVkGpu() const;
77 
78     VkBuffer fBuffer;
79     skgpu::VulkanAlloc fAlloc;
80 
81     const GrVkDescriptorSet* fUniformDescriptorSet;
82 
83     using INHERITED = GrGpuBuffer;
84 };
85 
86 #endif
87