OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * 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 |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef GrBuffer_DEFINED | 8 #ifndef GrBuffer_DEFINED |
9 #define GrBuffer_DEFINED | 9 #define GrBuffer_DEFINED |
10 | 10 |
11 #include "GrGpuResource.h" | 11 #include "GrGpuResource.h" |
12 | 12 |
13 class GrGpu; | 13 class GrGpu; |
14 | 14 |
15 class GrBuffer : public GrGpuResource { | 15 class GrBuffer : public GrGpuResource { |
16 public: | 16 public: |
17 /** | 17 /** |
18 * Computes a scratch key for a buffer with a "dynamic" access pattern. (Buf
fers with "static" | 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.) | 19 * and "stream" access patterns are disqualified by nature from being cached
and reused.) |
20 */ | 20 */ |
21 static void ComputeScratchKeyForDynamicBuffer(GrBufferType type, size_t size
, | 21 static void ComputeScratchKeyForDynamicBuffer(size_t size, GrBufferType inte
ndedType, |
22 GrScratchKey* key) { | 22 GrScratchKey* key) { |
23 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateRe
sourceType(); | 23 static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateRe
sourceType(); |
24 GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4); | 24 GrScratchKey::Builder builder(key, kType, 1 + (sizeof(size_t) + 3) / 4); |
25 // TODO: There's not always reason to cache a buffer by type. In some (a
ll?) APIs it's just | 25 // TODO: There's not always reason to cache a buffer by type. In some (a
ll?) APIs it's just |
26 // a chunk of memory we can use/reuse for any type of data. We really on
ly need to | 26 // a chunk of memory we can use/reuse for any type of data. We really on
ly need to |
27 // differentiate between the "read" types (e.g. kGpuToCpu_BufferType) an
d "draw" types. | 27 // differentiate between the "read" types (e.g. kGpuToCpu_BufferType) an
d "draw" types. |
28 builder[0] = type; | 28 builder[0] = intendedType; |
29 builder[1] = (uint32_t)size; | 29 builder[1] = (uint32_t)size; |
30 if (sizeof(size_t) > 4) { | 30 if (sizeof(size_t) > 4) { |
31 builder[2] = (uint32_t)((uint64_t)size >> 32); | 31 builder[2] = (uint32_t)((uint64_t)size >> 32); |
32 } | 32 } |
33 } | 33 } |
34 | 34 |
35 GrBufferType type() const { return fType; } | |
36 | |
37 GrAccessPattern accessPattern() const { return fAccessPattern; } | 35 GrAccessPattern accessPattern() const { return fAccessPattern; } |
38 | 36 |
39 /** | 37 /** |
40 * Returns true if the buffer is a wrapper around a CPU array. If true it | 38 * Returns true if the buffer is a wrapper around a CPU array. If true it |
41 * indicates that map will always succeed and will be free. | 39 * indicates that map will always succeed and will be free. |
42 */ | 40 */ |
43 bool isCPUBacked() const { return fCPUBacked; } | 41 bool isCPUBacked() const { return fCPUBacked; } |
44 | 42 |
45 /** | 43 /** |
46 * Maps the buffer to be written by the CPU. | 44 * Maps the buffer to be written by the CPU. |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 * | 101 * |
104 * @return returns true if the update succeeds, false otherwise. | 102 * @return returns true if the update succeeds, false otherwise. |
105 */ | 103 */ |
106 bool updateData(const void* src, size_t srcSizeInBytes) { | 104 bool updateData(const void* src, size_t srcSizeInBytes) { |
107 SkASSERT(!this->isMapped()); | 105 SkASSERT(!this->isMapped()); |
108 SkASSERT(srcSizeInBytes <= fGpuMemorySize); | 106 SkASSERT(srcSizeInBytes <= fGpuMemorySize); |
109 return this->onUpdateData(src, srcSizeInBytes); | 107 return this->onUpdateData(src, srcSizeInBytes); |
110 } | 108 } |
111 | 109 |
112 protected: | 110 protected: |
113 GrBuffer(GrGpu* gpu, GrBufferType type, size_t gpuMemorySize, GrAccessPatter
n accessPattern, | 111 GrBuffer(GrGpu* gpu, size_t gpuMemorySize, GrBufferType intendedType, |
114 bool cpuBacked) | 112 GrAccessPattern accessPattern, bool cpuBacked) |
115 : INHERITED(gpu, kCached_LifeCycle), | 113 : INHERITED(gpu, kCached_LifeCycle), |
116 fMapPtr(nullptr), | 114 fMapPtr(nullptr), |
117 fType(type), | |
118 fGpuMemorySize(gpuMemorySize), // TODO: Zero for cpu backed buffers? | 115 fGpuMemorySize(gpuMemorySize), // TODO: Zero for cpu backed buffers? |
119 fAccessPattern(accessPattern), | 116 fAccessPattern(accessPattern), |
120 fCPUBacked(cpuBacked) { | 117 fCPUBacked(cpuBacked) { |
121 if (!fCPUBacked && SkIsPow2(fGpuMemorySize) && kDynamic_GrAccessPattern
== fAccessPattern) { | 118 if (!fCPUBacked && SkIsPow2(fGpuMemorySize) && kDynamic_GrAccessPattern
== fAccessPattern) { |
122 GrScratchKey key; | 119 GrScratchKey key; |
123 ComputeScratchKeyForDynamicBuffer(fType, fGpuMemorySize, &key); | 120 ComputeScratchKeyForDynamicBuffer(fGpuMemorySize, intendedType, &key
); |
124 this->setScratchKey(key); | 121 this->setScratchKey(key); |
125 } | 122 } |
126 } | 123 } |
127 | 124 |
128 void* fMapPtr; | 125 void* fMapPtr; |
129 | 126 |
130 private: | 127 private: |
131 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } | 128 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } |
132 | 129 |
133 virtual void onMap() = 0; | 130 virtual void onMap() = 0; |
134 virtual void onUnmap() = 0; | 131 virtual void onUnmap() = 0; |
135 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; | 132 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; |
136 | 133 |
137 GrBufferType fType; | |
138 size_t fGpuMemorySize; | 134 size_t fGpuMemorySize; |
139 GrAccessPattern fAccessPattern; | 135 GrAccessPattern fAccessPattern; |
140 bool fCPUBacked; | 136 bool fCPUBacked; |
141 | 137 |
142 typedef GrGpuResource INHERITED; | 138 typedef GrGpuResource INHERITED; |
143 }; | 139 }; |
144 | 140 |
145 #endif | 141 #endif |
OLD | NEW |