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