OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef GrGeometryBuffer_DEFINED | 10 #ifndef GrBuffer_DEFINED |
11 #define GrGeometryBuffer_DEFINED | 11 #define GrBuffer_DEFINED |
12 | 12 |
13 #include "GrGpuResource.h" | 13 #include "GrGpuResource.h" |
14 | 14 |
15 class GrGpu; | 15 class GrGpu; |
16 | 16 |
17 /** | 17 class GrBuffer : public GrGpuResource { |
18 * Parent class for vertex and index buffers | |
19 */ | |
20 class GrGeometryBuffer : public GrGpuResource { | |
21 public: | 18 public: |
22 | 19 /** |
| 20 * Computes a scratch key for a buffer with a "dynamic" access pattern. (Buf
fers with "static" |
| 21 * and "stream" access patterns are disqualified by nature from being cached
and reused.) |
| 22 */ |
| 23 static void ComputeScratchKeyForDynamicBuffer(GrBufferType type, size_t size
, |
| 24 GrScratchKey* key) { |
| 25 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateRe
sourceType(); |
| 26 if (uint32_t hi = size >> 32) { |
| 27 GrScratchKey::Builder builder(key, kType, 3); |
| 28 builder[0] = type; |
| 29 builder[1] = size; |
| 30 builder[2] = hi; |
| 31 } else { |
| 32 GrScratchKey::Builder builder(key, kType, 2); |
| 33 // TODO: There's not always reason to cache a buffer by type. In som
e (all?) APIs it's |
| 34 // just a chunk of memory we can use/reuse for any type of data. We
really only need to |
| 35 // differentiate between the "read" types (e.g. kGpuToCpu_BufferType
) and "draw" types. |
| 36 builder[0] = type; |
| 37 builder[1] = size; |
| 38 } |
| 39 } |
23 | 40 |
24 /** | 41 GrBufferType type() const { return fType; } |
25 *Retrieves whether the buffer was created with the dynamic flag | 42 |
26 * | 43 GrAccessPattern accessPattern() const { return fAccessPattern; } |
27 * @return true if the buffer was created with the dynamic flag | |
28 */ | |
29 bool dynamic() const { return fDynamic; } | |
30 | 44 |
31 /** | 45 /** |
32 * Returns true if the buffer is a wrapper around a CPU array. If true it | 46 * Returns true if the buffer is a wrapper around a CPU array. If true it |
33 * indicates that map will always succeed and will be free. | 47 * indicates that map will always succeed and will be free. |
34 */ | 48 */ |
35 bool isCPUBacked() const { return fCPUBacked; } | 49 bool isCPUBacked() const { return fCPUBacked; } |
36 | 50 |
37 /** | 51 /** |
38 * Maps the buffer to be written by the CPU. | 52 * Maps the buffer to be written by the CPU. |
39 * | 53 * |
40 * The previous content of the buffer is invalidated. It is an error | 54 * The previous content of the buffer is invalidated. It is an error |
41 * to draw from the buffer while it is mapped. It is an error to call map | 55 * to draw from the buffer while it is mapped. It is an error to call map |
42 * on an already mapped buffer. It may fail if the backend doesn't support | 56 * on an already mapped buffer. It may fail if the backend doesn't support |
43 * mapping the buffer. If the buffer is CPU backed then it will always | 57 * mapping the buffer. If the buffer is CPU backed then it will always |
44 * succeed and is a free operation. Must be matched by an unmap() call. | 58 * succeed and is a free operation. Must be matched by an unmap() call. |
45 * Currently only one map at a time is supported (no nesting of | 59 * Currently only one map at a time is supported (no nesting of |
46 * map/unmap). | 60 * map/unmap). |
47 * | 61 * |
48 * Note that buffer mapping does not go through GrContext and therefore is | 62 * Note that buffer mapping does not go through GrContext and therefore is |
49 * not serialized with other operations. | 63 * not serialized with other operations. |
50 * | 64 * |
51 * @return a pointer to the data or nullptr if the map fails. | 65 * @return a pointer to the data or nullptr if the map fails. |
52 */ | 66 */ |
53 void* map() { return (fMapPtr = this->onMap()); } | 67 void* map() { |
| 68 this->onMap(); |
| 69 return fMapPtr; |
| 70 } |
54 | 71 |
55 /** | 72 /** |
56 * Unmaps the buffer. | 73 * Unmaps the buffer. |
57 * | 74 * |
58 * The pointer returned by the previous map call will no longer be valid. | 75 * The pointer returned by the previous map call will no longer be valid. |
59 */ | 76 */ |
60 void unmap() { | 77 void unmap() { |
61 SkASSERT(fMapPtr); | 78 SkASSERT(fMapPtr); |
62 this->onUnmap(); | 79 this->onUnmap(); |
63 fMapPtr = nullptr; | 80 fMapPtr = nullptr; |
(...skipping 28 matching lines...) Expand all Loading... |
92 * | 109 * |
93 * @return returns true if the update succeeds, false otherwise. | 110 * @return returns true if the update succeeds, false otherwise. |
94 */ | 111 */ |
95 bool updateData(const void* src, size_t srcSizeInBytes) { | 112 bool updateData(const void* src, size_t srcSizeInBytes) { |
96 SkASSERT(!this->isMapped()); | 113 SkASSERT(!this->isMapped()); |
97 SkASSERT(srcSizeInBytes <= fGpuMemorySize); | 114 SkASSERT(srcSizeInBytes <= fGpuMemorySize); |
98 return this->onUpdateData(src, srcSizeInBytes); | 115 return this->onUpdateData(src, srcSizeInBytes); |
99 } | 116 } |
100 | 117 |
101 protected: | 118 protected: |
102 GrGeometryBuffer(GrGpu* gpu, size_t gpuMemorySize, bool dynamic, bool cpuBac
ked) | 119 GrBuffer(GrGpu* gpu, GrBufferType type, size_t gpuMemorySize, GrAccessPatter
n accessPattern, |
103 : INHERITED(gpu, kCached_LifeCycle) | 120 bool cpuBacked) |
104 , fMapPtr(nullptr) | 121 : INHERITED(gpu, kCached_LifeCycle), |
105 , fGpuMemorySize(gpuMemorySize) | 122 fMapPtr(nullptr), |
106 , fDynamic(dynamic) | 123 fType(type), |
107 , fCPUBacked(cpuBacked) {} | 124 fGpuMemorySize(gpuMemorySize), // TODO: Zero for cpu backed buffers? |
| 125 fAccessPattern(accessPattern), |
| 126 fCPUBacked(cpuBacked) {} |
| 127 |
| 128 void* fMapPtr; |
108 | 129 |
109 private: | 130 private: |
110 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } | 131 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } |
111 | 132 |
112 virtual void* onMap() = 0; | 133 virtual void onMap() = 0; |
113 virtual void onUnmap() = 0; | 134 virtual void onUnmap() = 0; |
114 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; | 135 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; |
115 | 136 |
116 void* fMapPtr; | 137 GrBufferType fType; |
117 size_t fGpuMemorySize; | 138 size_t fGpuMemorySize; |
118 bool fDynamic; | 139 GrAccessPattern fAccessPattern; |
119 bool fCPUBacked; | 140 bool fCPUBacked; |
120 | 141 |
121 typedef GrGpuResource INHERITED; | 142 typedef GrGpuResource INHERITED; |
122 }; | 143 }; |
123 | 144 |
124 #endif | 145 #endif |
OLD | NEW |