cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 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. (Buffers with "static" |
| 19 | * and "stream" access patterns are disqualified by nature from being cached and reused.) |
| 20 | */ |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 21 | static void ComputeScratchKeyForDynamicBuffer(size_t size, GrBufferType intendedType, |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 22 | GrScratchKey* key) { |
| 23 | static const GrScratchKey::ResourceType kType = GrScratchKey::GenerateResourceType(); |
| 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 (all?) APIs it's just |
| 26 | // a chunk of memory we can use/reuse for any type of data. We really only need to |
| 27 | // differentiate between the "read" types (e.g. kGpuToCpu_BufferType) and "draw" types. |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 28 | builder[0] = intendedType; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 29 | builder[1] = (uint32_t)size; |
| 30 | if (sizeof(size_t) > 4) { |
| 31 | builder[2] = (uint32_t)((uint64_t)size >> 32); |
| 32 | } |
| 33 | } |
| 34 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 35 | GrAccessPattern accessPattern() const { return fAccessPattern; } |
| 36 | |
| 37 | /** |
| 38 | * Returns true if the buffer is a wrapper around a CPU array. If true it |
| 39 | * indicates that map will always succeed and will be free. |
| 40 | */ |
| 41 | bool isCPUBacked() const { return fCPUBacked; } |
| 42 | |
| 43 | /** |
| 44 | * Maps the buffer to be written by the CPU. |
| 45 | * |
| 46 | * The previous content of the buffer is invalidated. It is an error |
| 47 | * to draw from the buffer while it is mapped. It may fail if the backend |
| 48 | * doesn't support mapping the buffer. If the buffer is CPU backed then |
| 49 | * it will always succeed and is a free operation. Once a buffer is mapped, |
| 50 | * subsequent calls to map() are ignored. |
| 51 | * |
| 52 | * Note that buffer mapping does not go through GrContext and therefore is |
| 53 | * not serialized with other operations. |
| 54 | * |
| 55 | * @return a pointer to the data or nullptr if the map fails. |
| 56 | */ |
| 57 | void* map() { |
| 58 | if (!fMapPtr) { |
| 59 | this->onMap(); |
| 60 | } |
| 61 | return fMapPtr; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Unmaps the buffer. |
| 66 | * |
| 67 | * The pointer returned by the previous map call will no longer be valid. |
| 68 | */ |
| 69 | void unmap() { |
| 70 | SkASSERT(fMapPtr); |
| 71 | this->onUnmap(); |
| 72 | fMapPtr = nullptr; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns the same ptr that map() returned at time of map or nullptr if the |
| 77 | * is not mapped. |
| 78 | * |
| 79 | * @return ptr to mapped buffer data or nullptr if buffer is not mapped. |
| 80 | */ |
| 81 | void* mapPtr() const { return fMapPtr; } |
| 82 | |
| 83 | /** |
| 84 | Queries whether the buffer has been mapped. |
| 85 | |
| 86 | @return true if the buffer is mapped, false otherwise. |
| 87 | */ |
| 88 | bool isMapped() const { return SkToBool(fMapPtr); } |
| 89 | |
| 90 | /** |
| 91 | * Updates the buffer data. |
| 92 | * |
| 93 | * The size of the buffer will be preserved. The src data will be |
| 94 | * placed at the beginning of the buffer and any remaining contents will |
| 95 | * be undefined. srcSizeInBytes must be <= to the buffer size. |
| 96 | * |
| 97 | * The buffer must not be mapped. |
| 98 | * |
| 99 | * Note that buffer updates do not go through GrContext and therefore are |
| 100 | * not serialized with other operations. |
| 101 | * |
| 102 | * @return returns true if the update succeeds, false otherwise. |
| 103 | */ |
| 104 | bool updateData(const void* src, size_t srcSizeInBytes) { |
| 105 | SkASSERT(!this->isMapped()); |
| 106 | SkASSERT(srcSizeInBytes <= fGpuMemorySize); |
| 107 | return this->onUpdateData(src, srcSizeInBytes); |
| 108 | } |
| 109 | |
| 110 | protected: |
cdalton | e2e71c2 | 2016-04-07 18:13:29 -0700 | [diff] [blame] | 111 | GrBuffer(GrGpu* gpu, size_t gpuMemorySize, GrBufferType intendedType, |
| 112 | GrAccessPattern accessPattern, bool cpuBacked) |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 113 | : INHERITED(gpu), |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 114 | fMapPtr(nullptr), |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 115 | fGpuMemorySize(gpuMemorySize), // TODO: Zero for cpu backed buffers? |
| 116 | fAccessPattern(accessPattern), |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 117 | fCPUBacked(cpuBacked), |
| 118 | fIntendedType(intendedType) { |
| 119 | } |
| 120 | |
| 121 | void computeScratchKey(GrScratchKey* key) const override { |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 122 | if (!fCPUBacked && SkIsPow2(fGpuMemorySize) && kDynamic_GrAccessPattern == fAccessPattern) { |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 123 | ComputeScratchKeyForDynamicBuffer(fGpuMemorySize, fIntendedType, key); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
| 127 | void* fMapPtr; |
| 128 | |
| 129 | private: |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 130 | size_t onGpuMemorySize() const override { return fGpuMemorySize; } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 131 | |
| 132 | virtual void onMap() = 0; |
| 133 | virtual void onUnmap() = 0; |
| 134 | virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; |
| 135 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 136 | size_t fGpuMemorySize; |
| 137 | GrAccessPattern fAccessPattern; |
| 138 | bool fCPUBacked; |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 139 | GrBufferType fIntendedType; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 140 | typedef GrGpuResource INHERITED; |
| 141 | }; |
| 142 | |
| 143 | #endif |