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 | /** |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 18 | * Creates a client-side buffer. |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 19 | */ |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 20 | static SK_WARN_UNUSED_RESULT sk_sp<GrBuffer> MakeCPUBacked(GrGpu*, size_t sizeInBytes, |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 21 | GrGpuBufferType, |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 22 | const void* data = nullptr); |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Computes a scratch key for a GPU-side buffer with a "dynamic" access pattern. (Buffers with |
| 26 | * "static" and "stream" patterns are disqualified by nature from being cached and reused.) |
| 27 | */ |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 28 | static void ComputeScratchKeyForDynamicVBO(size_t size, GrGpuBufferType, GrScratchKey*); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 29 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 30 | GrAccessPattern accessPattern() const { return fAccessPattern; } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 31 | size_t sizeInBytes() const { return fSizeInBytes; } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Returns true if the buffer is a wrapper around a CPU array. If true it |
| 35 | * indicates that map will always succeed and will be free. |
| 36 | */ |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 37 | bool isCPUBacked() const { return SkToBool(fCPUData); } |
| 38 | size_t baseOffset() const { return reinterpret_cast<size_t>(fCPUData); } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Maps the buffer to be written by the CPU. |
| 42 | * |
| 43 | * The previous content of the buffer is invalidated. It is an error |
| 44 | * to draw from the buffer while it is mapped. It may fail if the backend |
| 45 | * doesn't support mapping the buffer. If the buffer is CPU backed then |
| 46 | * it will always succeed and is a free operation. Once a buffer is mapped, |
| 47 | * subsequent calls to map() are ignored. |
| 48 | * |
| 49 | * Note that buffer mapping does not go through GrContext and therefore is |
| 50 | * not serialized with other operations. |
| 51 | * |
| 52 | * @return a pointer to the data or nullptr if the map fails. |
| 53 | */ |
| 54 | void* map() { |
| 55 | if (!fMapPtr) { |
| 56 | this->onMap(); |
| 57 | } |
| 58 | return fMapPtr; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Unmaps the buffer. |
| 63 | * |
| 64 | * The pointer returned by the previous map call will no longer be valid. |
| 65 | */ |
| 66 | void unmap() { |
| 67 | SkASSERT(fMapPtr); |
| 68 | this->onUnmap(); |
| 69 | fMapPtr = nullptr; |
| 70 | } |
| 71 | |
| 72 | /** |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 73 | Queries whether the buffer has been mapped. |
| 74 | |
| 75 | @return true if the buffer is mapped, false otherwise. |
| 76 | */ |
| 77 | bool isMapped() const { return SkToBool(fMapPtr); } |
| 78 | |
| 79 | /** |
| 80 | * Updates the buffer data. |
| 81 | * |
| 82 | * The size of the buffer will be preserved. The src data will be |
| 83 | * placed at the beginning of the buffer and any remaining contents will |
| 84 | * be undefined. srcSizeInBytes must be <= to the buffer size. |
| 85 | * |
| 86 | * The buffer must not be mapped. |
| 87 | * |
| 88 | * Note that buffer updates do not go through GrContext and therefore are |
| 89 | * not serialized with other operations. |
| 90 | * |
| 91 | * @return returns true if the update succeeds, false otherwise. |
| 92 | */ |
| 93 | bool updateData(const void* src, size_t srcSizeInBytes) { |
| 94 | SkASSERT(!this->isMapped()); |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 95 | SkASSERT(srcSizeInBytes <= fSizeInBytes); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 96 | return this->onUpdateData(src, srcSizeInBytes); |
| 97 | } |
| 98 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 99 | ~GrBuffer() override { |
| 100 | sk_free(fCPUData); |
kkinnunen | 2e6055b | 2016-04-22 01:48:29 -0700 | [diff] [blame] | 101 | } |
| 102 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 103 | protected: |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 104 | GrBuffer(GrGpu*, size_t sizeInBytes, GrGpuBufferType, GrAccessPattern); |
Brian Salomon | 95548f6 | 2019-02-05 16:07:56 -0500 | [diff] [blame] | 105 | GrGpuBufferType intendedType() const { return fIntendedType; } |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 106 | |
| 107 | void* fMapPtr; |
| 108 | |
| 109 | private: |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 110 | /** |
| 111 | * Internal constructor to make a CPU-backed buffer. |
| 112 | */ |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 113 | GrBuffer(GrGpu*, size_t sizeInBytes, GrGpuBufferType, void* cpuData); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 114 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 115 | virtual void onMap() { SkASSERT(this->isCPUBacked()); fMapPtr = fCPUData; } |
| 116 | virtual void onUnmap() { SkASSERT(this->isCPUBacked()); } |
| 117 | virtual bool onUpdateData(const void* src, size_t srcSizeInBytes); |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 118 | |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 119 | size_t onGpuMemorySize() const override { return fSizeInBytes; } // TODO: zero for cpu backed? |
Derek Sollenberger | cf6da8c | 2018-03-29 13:40:02 -0400 | [diff] [blame] | 120 | const char* getResourceType() const override { return "Buffer Object"; } |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 121 | void computeScratchKey(GrScratchKey* key) const override; |
| 122 | |
| 123 | size_t fSizeInBytes; |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 124 | GrAccessPattern fAccessPattern; |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 125 | void* fCPUData; |
Brian Salomon | ae64c19 | 2019-02-05 09:41:37 -0500 | [diff] [blame] | 126 | GrGpuBufferType fIntendedType; |
csmartdalton | 485a120 | 2016-07-13 10:16:32 -0700 | [diff] [blame] | 127 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 128 | typedef GrGpuResource INHERITED; |
| 129 | }; |
| 130 | |
| 131 | #endif |