epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 10 | #ifndef GrGeometryBuffer_DEFINED |
| 11 | #define GrGeometryBuffer_DEFINED |
| 12 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 13 | #include "GrGpuResource.h" |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 14 | |
| 15 | class GrGpu; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 16 | |
| 17 | /** |
| 18 | * Parent class for vertex and index buffers |
| 19 | */ |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 20 | class GrGeometryBuffer : public GrGpuResource { |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 21 | public: |
mtklein | 2766c00 | 2015-06-26 11:45:03 -0700 | [diff] [blame] | 22 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 23 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 24 | /** |
| 25 | *Retrieves whether the buffer was created with the dynamic flag |
| 26 | * |
| 27 | * @return true if the buffer was created with the dynamic flag |
| 28 | */ |
| 29 | bool dynamic() const { return fDynamic; } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 30 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 31 | /** |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 32 | * Returns true if the buffer is a wrapper around a CPU array. If true it |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 33 | * indicates that map will always succeed and will be free. |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 34 | */ |
| 35 | bool isCPUBacked() const { return fCPUBacked; } |
| 36 | |
| 37 | /** |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 38 | * Maps the buffer to be written by the CPU. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 39 | * |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 40 | * The previous content of the buffer is invalidated. It is an error |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 41 | * 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 |
| 43 | * 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. |
| 45 | * Currently only one map at a time is supported (no nesting of |
| 46 | * map/unmap). |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 47 | * |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 48 | * Note that buffer mapping does not go through GrContext and therefore is |
| 49 | * not serialized with other operations. |
| 50 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | * @return a pointer to the data or nullptr if the map fails. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 52 | */ |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 53 | void* map() { return (fMapPtr = this->onMap()); } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 56 | * Unmaps the buffer. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 57 | * |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 58 | * The pointer returned by the previous map call will no longer be valid. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 59 | */ |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 60 | void unmap() { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 61 | SkASSERT(fMapPtr); |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 62 | this->onUnmap(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | fMapPtr = nullptr; |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | /** |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 67 | * Returns the same ptr that map() returned at time of map or nullptr if the |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 68 | * is not mapped. |
| 69 | * |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 70 | * @return ptr to mapped buffer data or nullptr if buffer is not mapped. |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 71 | */ |
| 72 | void* mapPtr() const { return fMapPtr; } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 73 | |
| 74 | /** |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 75 | Queries whether the buffer has been mapped. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 76 | |
commit-bot@chromium.org | 8341eb7 | 2014-05-07 20:51:05 +0000 | [diff] [blame] | 77 | @return true if the buffer is mapped, false otherwise. |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 78 | */ |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 79 | bool isMapped() const { return SkToBool(fMapPtr); } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 80 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 81 | /** |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 82 | * Updates the buffer data. |
| 83 | * |
| 84 | * The size of the buffer will be preserved. The src data will be |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 85 | * placed at the beginning of the buffer and any remaining contents will |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 86 | * be undefined. srcSizeInBytes must be <= to the buffer size. |
| 87 | * |
| 88 | * The buffer must not be mapped. |
| 89 | * |
| 90 | * Note that buffer updates do not go through GrContext and therefore are |
| 91 | * not serialized with other operations. |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 92 | * |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 93 | * @return returns true if the update succeeds, false otherwise. |
| 94 | */ |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 95 | bool updateData(const void* src, size_t srcSizeInBytes) { |
| 96 | SkASSERT(!this->isMapped()); |
| 97 | SkASSERT(srcSizeInBytes <= fGpuMemorySize); |
| 98 | return this->onUpdateData(src, srcSizeInBytes); |
| 99 | } |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 100 | |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 101 | protected: |
bsalomon | 5236cf4 | 2015-01-14 10:42:08 -0800 | [diff] [blame] | 102 | GrGeometryBuffer(GrGpu* gpu, size_t gpuMemorySize, bool dynamic, bool cpuBacked) |
| 103 | : INHERITED(gpu, kCached_LifeCycle) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | , fMapPtr(nullptr) |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 105 | , fGpuMemorySize(gpuMemorySize) |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 106 | , fDynamic(dynamic) |
| 107 | , fCPUBacked(cpuBacked) {} |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 108 | |
| 109 | private: |
bsalomon | 69ed47f | 2014-11-12 11:13:39 -0800 | [diff] [blame] | 110 | virtual size_t onGpuMemorySize() const { return fGpuMemorySize; } |
| 111 | |
commit-bot@chromium.org | e529a61 | 2014-05-08 18:13:23 +0000 | [diff] [blame] | 112 | virtual void* onMap() = 0; |
| 113 | virtual void onUnmap() = 0; |
| 114 | virtual bool onUpdateData(const void* src, size_t srcSizeInBytes) = 0; |
| 115 | |
| 116 | void* fMapPtr; |
commit-bot@chromium.org | 089a780 | 2014-05-02 21:38:22 +0000 | [diff] [blame] | 117 | size_t fGpuMemorySize; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 118 | bool fDynamic; |
bsalomon@google.com | ee3bc3b | 2013-02-21 14:33:46 +0000 | [diff] [blame] | 119 | bool fCPUBacked; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 120 | |
bsalomon | 6d3fe02 | 2014-07-25 08:35:45 -0700 | [diff] [blame] | 121 | typedef GrGpuResource INHERITED; |
bsalomon@google.com | 27847de | 2011-02-22 20:59:41 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | #endif |