• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 GrBuffer_DEFINED
9 #define GrBuffer_DEFINED
10 
11 #include "GrGpuResource.h"
12 
13 class GrGpu;
14 
15 class GrBuffer : public GrGpuResource {
16 public:
17     /**
18      * Creates a client-side buffer.
19      */
20     static SK_WARN_UNUSED_RESULT sk_sp<GrBuffer> MakeCPUBacked(GrGpu*, size_t sizeInBytes,
21                                                                GrBufferType,
22                                                                const void* data = nullptr);
23 
24     /**
25      * Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with
26      * "static" and "stream" patterns are disqualified by nature from being cached and reused.)
27      */
28     static void ComputeScratchKeyForDynamicVBO(size_t size, GrBufferType, GrScratchKey*);
29 
accessPattern()30     GrAccessPattern accessPattern() const { return fAccessPattern; }
sizeInBytes()31     size_t sizeInBytes() const { return fSizeInBytes; }
32 
33     /**
34      * Returns true if the buffer is a wrapper around a CPU array. If true it
35      * indicates that map will always succeed and will be free.
36      */
isCPUBacked()37     bool isCPUBacked() const { return SkToBool(fCPUData); }
baseOffset()38     size_t baseOffset() const { return reinterpret_cast<size_t>(fCPUData); }
39 
40     /**
41      * Maps the buffer to be written by the CPU.
42      *
43      * The previous content of the buffer is invalidated. It is an error
44      * to draw from the buffer while it is mapped. It may fail if the backend
45      * doesn't support mapping the buffer. If the buffer is CPU backed then
46      * it will always succeed and is a free operation. Once a buffer is mapped,
47      * subsequent calls to map() are ignored.
48      *
49      * Note that buffer mapping does not go through GrContext and therefore is
50      * not serialized with other operations.
51      *
52      * @return a pointer to the data or nullptr if the map fails.
53      */
map()54      void* map() {
55          if (!fMapPtr) {
56              this->onMap();
57          }
58          return fMapPtr;
59      }
60 
61     /**
62      * Unmaps the buffer.
63      *
64      * The pointer returned by the previous map call will no longer be valid.
65      */
unmap()66      void unmap() {
67          SkASSERT(fMapPtr);
68          this->onUnmap();
69          fMapPtr = nullptr;
70      }
71 
72     /**
73      Queries whether the buffer has been mapped.
74 
75      @return true if the buffer is mapped, false otherwise.
76      */
isMapped()77      bool isMapped() const { return SkToBool(fMapPtr); }
78 
79     /**
80      * Updates the buffer data.
81      *
82      * The size of the buffer will be preserved. The src data will be
83      * placed at the beginning of the buffer and any remaining contents will
84      * be undefined. srcSizeInBytes must be <= to the buffer size.
85      *
86      * The buffer must not be mapped.
87      *
88      * Note that buffer updates do not go through GrContext and therefore are
89      * not serialized with other operations.
90      *
91      * @return returns true if the update succeeds, false otherwise.
92      */
updateData(const void * src,size_t srcSizeInBytes)93     bool updateData(const void* src, size_t srcSizeInBytes) {
94         SkASSERT(!this->isMapped());
95         SkASSERT(srcSizeInBytes <= fSizeInBytes);
96         return this->onUpdateData(src, srcSizeInBytes);
97     }
98 
~GrBuffer()99     ~GrBuffer() override {
100         sk_free(fCPUData);
101     }
102 
103 protected:
104     GrBuffer(GrGpu*, size_t sizeInBytes, GrBufferType, GrAccessPattern);
105 
106     void* fMapPtr;
107 
108 private:
109     /**
110      * Internal constructor to make a CPU-backed buffer.
111      */
112     GrBuffer(GrGpu*, size_t sizeInBytes, GrBufferType, void* cpuData);
113 
onMap()114     virtual void onMap() { SkASSERT(this->isCPUBacked()); fMapPtr = fCPUData; }
onUnmap()115     virtual void onUnmap() { SkASSERT(this->isCPUBacked()); }
116     virtual bool onUpdateData(const void* src, size_t srcSizeInBytes);
117 
onGpuMemorySize()118     size_t onGpuMemorySize() const override { return fSizeInBytes; } // TODO: zero for cpu backed?
getResourceType()119     const char* getResourceType() const override { return "Buffer Object"; }
120     void computeScratchKey(GrScratchKey* key) const override;
121 
122     size_t            fSizeInBytes;
123     GrAccessPattern   fAccessPattern;
124     void*             fCPUData;
125     GrBufferType      fIntendedType;
126 
127     typedef GrGpuResource INHERITED;
128 };
129 
130 #endif
131