xref: /aosp_15_r20/external/skia/src/gpu/graphite/vk/VulkanBuffer.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 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 skgpu_graphite_VulkanBuffer_DEFINED
9 #define skgpu_graphite_VulkanBuffer_DEFINED
10 
11 #include "include/gpu/vk/VulkanMemoryAllocator.h"
12 #include "src/gpu/graphite/Buffer.h"
13 #include "src/gpu/graphite/vk/VulkanSharedContext.h"
14 
15 namespace skgpu::graphite {
16 
17 class VulkanCommandBuffer;
18 
19 class VulkanBuffer final : public Buffer {
20 public:
21     static sk_sp<Buffer> Make(const VulkanSharedContext*, size_t, BufferType, AccessPattern);
22     void freeGpuData() override;
vkBuffer()23     VkBuffer vkBuffer() const { return fBuffer; }
bufferUsageFlags()24     VkBufferUsageFlags bufferUsageFlags() const { return fBufferUsageFlags; }
25 
26     void setBufferAccess(VulkanCommandBuffer* buffer,
27                          VkAccessFlags dstAccessMask,
28                          VkPipelineStageFlags dstStageMask) const;
29 
30 private:
31     VulkanBuffer(const VulkanSharedContext*,
32                  size_t,
33                  BufferType,
34                  AccessPattern,
35                  VkBuffer,
36                  const skgpu::VulkanAlloc&,
37                  VkBufferUsageFlags,
38                  Protected isProtected);
39 
40     void onMap() override;
41     void onUnmap() override;
42 
43     void internalMap(size_t readOffset, size_t readSize);
44     void internalUnmap(size_t flushOffset, size_t flushSize);
45 
isMappable()46     bool isMappable() const { return fAlloc.fFlags & skgpu::VulkanAlloc::kMappable_Flag; }
47 
vulkanSharedContext()48     const VulkanSharedContext* vulkanSharedContext() const {
49         return static_cast<const VulkanSharedContext*>(this->sharedContext());
50     }
51 
52     static VkPipelineStageFlags AccessMaskToPipelineSrcStageFlags(const VkAccessFlags accessFlags);
53 
54     VkBuffer fBuffer;
55     skgpu::VulkanAlloc fAlloc;
56     const VkBufferUsageFlags fBufferUsageFlags;
57     mutable VkAccessFlags fCurrentAccessMask = 0;
58 
59     /**
60      * Buffers can either be mapped for:
61      * 1) Reading from the CPU (The effect of writing would be undefined)
62      * 2) Writing from the CPU (The existing contents are discarded. Even in the case where the
63      *    initial contents are overwritten, CPU reads should be avoided for performance reasons as
64      *    the memory may not be cached).
65      */
66     bool fBufferUsedForCPURead = false;
67 };
68 
69 } // namespace skgpu::graphite
70 
71 #endif // skgpu_graphite_VulkanBuffer_DEFINED
72 
73