Skia
2DGraphicsLibrary
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GrBuffer.h
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:
20  static SK_WARN_UNUSED_RESULT GrBuffer* CreateCPUBacked(GrGpu*, size_t sizeInBytes, GrBufferType,
21  const void* data = nullptr);
22 
27  static void ComputeScratchKeyForDynamicVBO(size_t size, GrBufferType, GrScratchKey*);
28 
29  GrAccessPattern accessPattern() const { return fAccessPattern; }
30  size_t sizeInBytes() const { return fSizeInBytes; }
31 
36  bool isCPUBacked() const { return SkToBool(fCPUData); }
37  size_t baseOffset() const { return reinterpret_cast<size_t>(fCPUData); }
38 
53  void* map() {
54  if (!fMapPtr) {
55  this->onMap();
56  }
57  return fMapPtr;
58  }
59 
65  void unmap() {
66  SkASSERT(fMapPtr);
67  this->onUnmap();
68  fMapPtr = nullptr;
69  }
70 
77  void* mapPtr() const { return fMapPtr; }
78 
84  bool isMapped() const { return SkToBool(fMapPtr); }
85 
100  bool updateData(const void* src, size_t srcSizeInBytes) {
101  SkASSERT(!this->isMapped());
102  SkASSERT(srcSizeInBytes <= fSizeInBytes);
103  return this->onUpdateData(src, srcSizeInBytes);
104  }
105 
106  ~GrBuffer() override {
107  sk_free(fCPUData);
108  }
109 
110 protected:
111  GrBuffer(GrGpu*, size_t sizeInBytes, GrBufferType, GrAccessPattern);
112 
113  void* fMapPtr;
114 
115 private:
119  GrBuffer(GrGpu*, size_t sizeInBytes, GrBufferType, void* cpuData);
120 
121  virtual void onMap() { SkASSERT(this->isCPUBacked()); fMapPtr = fCPUData; }
122  virtual void onUnmap() { SkASSERT(this->isCPUBacked()); }
123  virtual bool onUpdateData(const void* src, size_t srcSizeInBytes);
124 
125  size_t onGpuMemorySize() const override { return fSizeInBytes; } // TODO: zero for cpu backed?
126  void computeScratchKey(GrScratchKey* key) const override;
127 
128  size_t fSizeInBytes;
129  GrAccessPattern fAccessPattern;
130  void* fCPUData;
131  GrBufferType fIntendedType;
132 
133  typedef GrGpuResource INHERITED;
134 };
135 
136 #endif
Base class for objects that can be kept in the GrResourceCache.
Definition: GrGpuResource.h:140
static SK_WARN_UNUSED_RESULT GrBuffer * CreateCPUBacked(GrGpu *, size_t sizeInBytes, GrBufferType, const void *data=nullptr)
Creates a client-side buffer.
SK_API void sk_free(void *)
Free memory returned by sk_malloc().
#define SkToBool(cond)
Returns 0 or 1 based on the condition.
Definition: SkTypes.h:287
bool updateData(const void *src, size_t srcSizeInBytes)
Updates the buffer data.
Definition: GrBuffer.h:100
void computeScratchKey(GrScratchKey *key) const override
Called by the registerWithCache if the resource is available to be used as scratch.
Definition: GrBuffer.h:15
void * mapPtr() const
Returns the same ptr that map() returned at time of map or nullptr if the is not mapped.
Definition: GrBuffer.h:77
void * map()
Maps the buffer to be written by the CPU.
Definition: GrBuffer.h:53
A key used for scratch resources.
Definition: GrResourceKey.h:166
bool isMapped() const
Queries whether the buffer has been mapped.
Definition: GrBuffer.h:84
void unmap()
Unmaps the buffer.
Definition: GrBuffer.h:65
bool isCPUBacked() const
Returns true if the buffer is a wrapper around a CPU array.
Definition: GrBuffer.h:36
static void ComputeScratchKeyForDynamicVBO(size_t size, GrBufferType, GrScratchKey *)
Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern.