1cb93a386Sopenharmony_ci/*
2cb93a386Sopenharmony_ci * Copyright 2021 Google LLC
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 GrVkBuffer_DEFINED
9cb93a386Sopenharmony_ci#define GrVkBuffer_DEFINED
10cb93a386Sopenharmony_ci
11cb93a386Sopenharmony_ci#include "include/gpu/vk/GrVkTypes.h"
12cb93a386Sopenharmony_ci#include "src/gpu/GrGpuBuffer.h"
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciclass GrVkDescriptorSet;
15cb93a386Sopenharmony_ciclass GrVkGpu;
16cb93a386Sopenharmony_ci
17cb93a386Sopenharmony_ciclass GrVkBuffer : public GrGpuBuffer {
18cb93a386Sopenharmony_cipublic:
19cb93a386Sopenharmony_ci    static sk_sp<GrVkBuffer> Make(GrVkGpu* gpu,
20cb93a386Sopenharmony_ci                                   size_t size,
21cb93a386Sopenharmony_ci                                   GrGpuBufferType bufferType,
22cb93a386Sopenharmony_ci                                   GrAccessPattern accessPattern);
23cb93a386Sopenharmony_ci
24cb93a386Sopenharmony_ci    static sk_sp<GrVkBuffer> MakeFromOHNativeBuffer(GrVkGpu* gpu,
25cb93a386Sopenharmony_ci                                                    OH_NativeBuffer *nativeBuffer,
26cb93a386Sopenharmony_ci                                                    size_t bufferSize,
27cb93a386Sopenharmony_ci                                                    GrGpuBufferType bufferType,
28cb93a386Sopenharmony_ci                                                    GrAccessPattern accessPattern);
29cb93a386Sopenharmony_ci
30cb93a386Sopenharmony_ci    // OH ISSUE: Integrate Destroy and Free
31cb93a386Sopenharmony_ci    static void DestroyAndFreeBufferMemory(const GrVkGpu* gpu, const GrVkAlloc& alloc, const VkBuffer& buffer);
32cb93a386Sopenharmony_ci
33cb93a386Sopenharmony_ci    VkBuffer vkBuffer() const { return fBuffer; }
34cb93a386Sopenharmony_ci
35cb93a386Sopenharmony_ci    void addMemoryBarrier(VkAccessFlags srcAccessMask,
36cb93a386Sopenharmony_ci                          VkAccessFlags dstAccesMask,
37cb93a386Sopenharmony_ci                          VkPipelineStageFlags srcStageMask,
38cb93a386Sopenharmony_ci                          VkPipelineStageFlags dstStageMask,
39cb93a386Sopenharmony_ci                          bool byRegion) const;
40cb93a386Sopenharmony_ci
41cb93a386Sopenharmony_ci    // If the buffer is a uniform buffer, return the descriptor set for that buffer. It is not valid
42cb93a386Sopenharmony_ci    // to call this on non uniform buffers.
43cb93a386Sopenharmony_ci    const VkDescriptorSet* uniformDescriptorSet() const;
44cb93a386Sopenharmony_ci
45cb93a386Sopenharmony_ciprivate:
46cb93a386Sopenharmony_ci    GrVkBuffer(GrVkGpu* gpu,
47cb93a386Sopenharmony_ci                size_t sizeInBytes,
48cb93a386Sopenharmony_ci                GrGpuBufferType bufferType,
49cb93a386Sopenharmony_ci                GrAccessPattern accessPattern,
50cb93a386Sopenharmony_ci                VkBuffer buffer,
51cb93a386Sopenharmony_ci                const GrVkAlloc& alloc,
52cb93a386Sopenharmony_ci                const GrVkDescriptorSet* uniformDescriptorSet);
53cb93a386Sopenharmony_ci
54cb93a386Sopenharmony_ci    bool isVkMappable() const { return fAlloc.fFlags & GrVkAlloc::kMappable_Flag; }
55cb93a386Sopenharmony_ci
56cb93a386Sopenharmony_ci    bool vkIsMapped() const { return SkToBool(fMapPtr); }
57cb93a386Sopenharmony_ci    void vkMap(size_t size);
58cb93a386Sopenharmony_ci    void vkUnmap(size_t size);
59cb93a386Sopenharmony_ci    void copyCpuDataToGpuBuffer(const void* srcData, size_t size);
60cb93a386Sopenharmony_ci
61cb93a386Sopenharmony_ci
62cb93a386Sopenharmony_ci    void onMap() override;
63cb93a386Sopenharmony_ci    void onUnmap() override;
64cb93a386Sopenharmony_ci    bool onUpdateData(const void* src, size_t srcSizeInBytes) override;
65cb93a386Sopenharmony_ci
66cb93a386Sopenharmony_ci    void vkRelease();
67cb93a386Sopenharmony_ci
68cb93a386Sopenharmony_ci    void onAbandon() override;
69cb93a386Sopenharmony_ci    void onRelease() override;
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci    GrVkGpu* getVkGpu() const;
72cb93a386Sopenharmony_ci
73cb93a386Sopenharmony_ci    VkBuffer fBuffer;
74cb93a386Sopenharmony_ci    GrVkAlloc fAlloc;
75cb93a386Sopenharmony_ci
76cb93a386Sopenharmony_ci    const GrVkDescriptorSet* fUniformDescriptorSet;
77cb93a386Sopenharmony_ci
78cb93a386Sopenharmony_ci    using INHERITED = GrGpuBuffer;
79cb93a386Sopenharmony_ci};
80cb93a386Sopenharmony_ci
81cb93a386Sopenharmony_ci#endif
82