Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 * 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.) | |
| 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; | |
|
jvanverth1
2016/03/24 19:21:01
needs a cast to uint32_t
Chris Dalton
2016/03/24 21:12:06
Done.
| |
| 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; | |
|
jvanverth1
2016/03/24 19:21:01
needs a cast to uint32_t
Chris Dalton
2016/03/24 21:12:06
Done.
| |
| 36 } | |
| 37 } | |
| 38 | |
| 39 GrBufferType type() const { return fType; } | |
| 40 | |
| 41 GrAccessPattern accessPattern() const { return fAccessPattern; } | |
| 42 | |
| 43 /** | |
| 44 * Returns true if the buffer is a wrapper around a CPU array. If true it | |
| 45 * indicates that map will always succeed and will be free. | |
| 46 */ | |
| 47 bool isCPUBacked() const { return fCPUBacked; } | |
| 48 | |
| 49 /** | |
| 50 * Maps the buffer to be written by the CPU. | |
| 51 * | |
| 52 * The previous content of the buffer is invalidated. It is an error | |
| 53 * to draw from the buffer while it is mapped. It may fail if the backend | |
| 54 * doesn't support mapping the buffer. If the buffer is CPU backed then | |
| 55 * it will always succeed and is a free operation. Once a buffer is mapped, | |
| 56 * subsequent calls to map() are ignored. | |
| 57 * | |
| 58 * Note that buffer mapping does not go through GrContext and therefore is | |
| 59 * not serialized with other operations. | |
| 60 * | |
| 61 * @return a pointer to the data or nullptr if the map fails. | |
| 62 */ | |
| 63 void* map() { | |
| 64 if (!fMapPtr) { | |
| 65 this->onMap(); | |
| 66 } | |
| 67 return fMapPtr; | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Unmaps the buffer. | |
| 72 * | |
| 73 * The pointer returned by the previous map call will no longer be valid. | |
| 74 */ | |
| 75 void unmap() { | |
| 76 SkASSERT(fMapPtr); | |
| 77 this->onUnmap(); | |
| 78 fMapPtr = nullptr; | |
| 79 } | |
| 80 | |
| 81 /** | |
| 82 * Returns the same ptr that map() returned at time of map or nullptr if the | |
| 83 * is not mapped. | |
| 84 * | |
| 85 * @return ptr to mapped buffer data or nullptr if buffer is not mapped. | |
| 86 */ | |
| 87 void* mapPtr() const { return fMapPtr; } | |
| 88 | |
| 89 /** | |
| 90 Queries whether the buffer has been mapped. | |
| 91 | |
| 92 @return true if the buffer is mapped, false otherwise. | |
| 93 */ | |
| 94 bool isMapped() const { return SkToBool(fMapPtr); } | |
| 95 | |
| 96 /** | |
| 97 * Updates the buffer data. | |
| 98 * | |
| 99 * The size of the buffer will be preserved. The src data will be | |
| 100 * placed at the beginning of the buffer and any remaining contents will | |
| 101 * be undefined. srcSizeInBytes must be <= to the buffer size. | |
| 102 * | |
| 103 * The buffer must not be mapped. | |
| 104 * | |
| 105 * Note that buffer updates do not go through GrContext and therefore are | |
| 106 * not serialized with other operations. | |
| 107 * | |
| 108 * @return returns true if the update succeeds, false otherwise. | |
| 109 */ | |
| 110 bool updateData(const void* src, size_t srcSizeInBytes) { | |
| 111 SkASSERT(!this->isMapped()); | |
| 112 SkASSERT(srcSizeInBytes <= fGpuMemorySize); | |
| 113 return this->onUpdateData(src, srcSizeInBytes); | |
| 114 } | |
| 115 | |
| 116 protected: | |
| 117 GrBuffer(GrGpu* gpu, GrBufferType type, size_t gpuMemorySize, GrAccessPatter n accessPattern, | |
| 118 bool cpuBacked) | |
| 119 : INHERITED(gpu, kCached_LifeCycle), | |
| 120 fMapPtr(nullptr), | |
| 121 fType(type), | |
| 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; | |
| 133 | |
| 134 private: | |
| 135 virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } | |
| 136 | |
| 137 virtual void onMap() = 0; | |
| 138 virtual void onUnmap() = 0; | |
| 139 virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; | |
| 140 | |
| 141 GrBufferType fType; | |
| 142 size_t fGpuMemorySize; | |
| 143 GrAccessPattern fAccessPattern; | |
| 144 bool fCPUBacked; | |
| 145 | |
| 146 typedef GrGpuResource INHERITED; | |
| 147 }; | |
| 148 | |
| 149 #endif | |
| OLD | NEW |