• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 GrVkBuffer_DEFINED
9 #define GrVkBuffer_DEFINED
10 
11 #include "GrVkResource.h"
12 #include "vk/GrVkDefines.h"
13 #include "vk/GrVkTypes.h"
14 
15 class GrVkGpu;
16 
17 /**
18  * This class serves as the base of GrVk*Buffer classes. It was written to avoid code
19  * duplication in those classes.
20  */
21 class GrVkBuffer : public SkNoncopyable {
22 public:
~GrVkBuffer()23     virtual ~GrVkBuffer() {
24         // either release or abandon should have been called by the owner of this object.
25         SkASSERT(!fResource);
26         delete [] (unsigned char*)fMapPtr;
27     }
28 
buffer()29     VkBuffer                    buffer() const { return fResource->fBuffer; }
alloc()30     const GrVkAlloc&            alloc() const { return fResource->fAlloc; }
resource()31     const GrVkRecycledResource* resource() const { return fResource; }
size()32     size_t                      size() const { return fDesc.fSizeInBytes; }
offset()33     VkDeviceSize                offset() const { return fOffset;  }
34 
35     void addMemoryBarrier(const GrVkGpu* gpu,
36                           VkAccessFlags srcAccessMask,
37                           VkAccessFlags dstAccessMask,
38                           VkPipelineStageFlags srcStageMask,
39                           VkPipelineStageFlags dstStageMask,
40                           bool byRegion) const;
41 
42     enum Type {
43         kVertex_Type,
44         kIndex_Type,
45         kUniform_Type,
46         kTexel_Type,
47         kCopyRead_Type,
48         kCopyWrite_Type,
49     };
50 
51 protected:
52     struct Desc {
53         size_t      fSizeInBytes;
54         Type        fType;         // vertex buffer, index buffer, etc.
55         bool        fDynamic;
56     };
57 
58     class Resource : public GrVkRecycledResource {
59     public:
Resource(VkBuffer buf,const GrVkAlloc & alloc,Type type)60         Resource(VkBuffer buf, const GrVkAlloc& alloc, Type type)
61             : INHERITED(), fBuffer(buf), fAlloc(alloc), fType(type) {}
62 
63 #ifdef SK_TRACE_VK_RESOURCES
dumpInfo()64         void dumpInfo() const override {
65             SkDebugf("GrVkBuffer: %d (%d refs)\n", fBuffer, this->getRefCnt());
66         }
67 #endif
68         VkBuffer           fBuffer;
69         GrVkAlloc          fAlloc;
70         Type               fType;
71 
72     private:
73         void freeGPUData(const GrVkGpu* gpu) const override;
74 
onRecycle(GrVkGpu * gpu)75         void onRecycle(GrVkGpu* gpu) const override { this->unref(gpu); }
76 
77         typedef GrVkRecycledResource INHERITED;
78     };
79 
80     // convenience routine for raw buffer creation
81     static const Resource* Create(const GrVkGpu* gpu,
82                                   const Desc& descriptor);
83 
GrVkBuffer(const Desc & desc,const GrVkBuffer::Resource * resource)84     GrVkBuffer(const Desc& desc, const GrVkBuffer::Resource* resource)
85         : fDesc(desc), fResource(resource), fOffset(0), fMapPtr(nullptr) {
86     }
87 
vkMap(GrVkGpu * gpu)88     void* vkMap(GrVkGpu* gpu) {
89         this->internalMap(gpu, fDesc.fSizeInBytes);
90         return fMapPtr;
91     }
vkUnmap(GrVkGpu * gpu)92     void vkUnmap(GrVkGpu* gpu) { this->internalUnmap(gpu, this->size()); }
93 
94     // If the caller passes in a non null createdNewBuffer, this function will set the bool to true
95     // if it creates a new VkBuffer to upload the data to.
96     bool vkUpdateData(GrVkGpu* gpu, const void* src, size_t srcSizeInBytes,
97                       bool* createdNewBuffer = nullptr);
98 
99     void vkAbandon();
100     void vkRelease(const GrVkGpu* gpu);
101 
102 private:
createResource(GrVkGpu * gpu,const Desc & descriptor)103     virtual const Resource* createResource(GrVkGpu* gpu,
104                                            const Desc& descriptor) {
105         return Create(gpu, descriptor);
106     }
107 
108     void internalMap(GrVkGpu* gpu, size_t size, bool* createdNewBuffer = nullptr);
109     void internalUnmap(GrVkGpu* gpu, size_t size);
110 
111     void validate() const;
112     bool vkIsMapped() const;
113 
114     Desc                    fDesc;
115     const Resource*         fResource;
116     VkDeviceSize            fOffset;
117     void*                   fMapPtr;
118 
119     typedef SkNoncopyable INHERITED;
120 };
121 
122 #endif
123