ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ANDROID_HWUI_VERTEX_BUFFER_H |
| 18 | #define ANDROID_HWUI_VERTEX_BUFFER_H |
| 19 | |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 20 | #include "utils/MathUtils.h" |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
| 25 | class VertexBuffer { |
| 26 | public: |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 27 | enum Mode { |
| 28 | kStandard = 0, |
| 29 | kOnePolyRingShadow = 1, |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 30 | kTwoPolyRingShadow = 2, |
| 31 | kIndices = 3 |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 32 | }; |
| 33 | |
| 34 | VertexBuffer() |
| 35 | : mBuffer(0) |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 36 | , mIndices(0) |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 37 | , mVertexCount(0) |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 38 | , mIndexCount(0) |
| 39 | , mAllocatedVertexCount(0) |
| 40 | , mAllocatedIndexCount(0) |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 41 | , mByteCount(0) |
| 42 | , mMode(kStandard) |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 43 | , mReallocBuffer(0) |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 44 | , mCleanupMethod(NULL) |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 45 | , mCleanupIndexMethod(NULL) |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 46 | {} |
| 47 | |
| 48 | ~VertexBuffer() { |
| 49 | if (mCleanupMethod) mCleanupMethod(mBuffer); |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 50 | if (mCleanupIndexMethod) mCleanupIndexMethod(mIndices); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | /** |
| 54 | This should be the only method used by the Tessellator. Subsequent calls to |
| 55 | alloc will allocate space within the first allocation (useful if you want to |
| 56 | eventually allocate multiple regions within a single VertexBuffer, such as |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 57 | with PathTessellator::tessellateLines()) |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 58 | */ |
| 59 | template <class TYPE> |
| 60 | TYPE* alloc(int vertexCount) { |
| 61 | if (mVertexCount) { |
| 62 | TYPE* reallocBuffer = (TYPE*)mReallocBuffer; |
| 63 | // already have allocated the buffer, re-allocate space within |
| 64 | if (mReallocBuffer != mBuffer) { |
| 65 | // not first re-allocation, leave space for degenerate triangles to separate strips |
| 66 | reallocBuffer += 2; |
| 67 | } |
| 68 | mReallocBuffer = reallocBuffer + vertexCount; |
| 69 | return reallocBuffer; |
| 70 | } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 71 | mAllocatedVertexCount = vertexCount; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 72 | mVertexCount = vertexCount; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 73 | mByteCount = mVertexCount * sizeof(TYPE); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 74 | mReallocBuffer = mBuffer = (void*)new TYPE[vertexCount]; |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 75 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 76 | mCleanupMethod = &(cleanup<TYPE>); |
| 77 | |
| 78 | return (TYPE*)mBuffer; |
| 79 | } |
| 80 | |
| 81 | template <class TYPE> |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 82 | TYPE* allocIndices(int indexCount) { |
| 83 | mAllocatedIndexCount = indexCount; |
| 84 | mIndexCount = indexCount; |
| 85 | mIndices = (void*)new TYPE[indexCount]; |
| 86 | |
| 87 | mCleanupIndexMethod = &(cleanup<TYPE>); |
| 88 | |
| 89 | return (TYPE*)mIndices; |
| 90 | } |
| 91 | |
| 92 | template <class TYPE> |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 93 | void copyInto(const VertexBuffer& srcBuffer, float xOffset, float yOffset) { |
| 94 | int verticesToCopy = srcBuffer.getVertexCount(); |
| 95 | |
| 96 | TYPE* dst = alloc<TYPE>(verticesToCopy); |
| 97 | TYPE* src = (TYPE*)srcBuffer.getBuffer(); |
| 98 | |
| 99 | for (int i = 0; i < verticesToCopy; i++) { |
| 100 | TYPE::copyWithOffset(&dst[i], src[i], xOffset, yOffset); |
| 101 | } |
| 102 | } |
| 103 | |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Brute force bounds computation, used only if the producer of this |
| 106 | * vertex buffer can't determine bounds more simply/efficiently |
| 107 | */ |
| 108 | template <class TYPE> |
Chris Craik | 9a89bc6 | 2014-07-23 17:21:25 -0700 | [diff] [blame] | 109 | void computeBounds(int vertexCount = 0) { |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 110 | if (!mVertexCount) { |
| 111 | mBounds.setEmpty(); |
| 112 | return; |
| 113 | } |
Chris Craik | 9a89bc6 | 2014-07-23 17:21:25 -0700 | [diff] [blame] | 114 | |
| 115 | // default: compute over every vertex |
| 116 | if (vertexCount == 0) vertexCount = mVertexCount; |
| 117 | |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 118 | TYPE* current = (TYPE*)mBuffer; |
Chris Craik | 9a89bc6 | 2014-07-23 17:21:25 -0700 | [diff] [blame] | 119 | TYPE* end = current + vertexCount; |
Chris Craik | c93e45c | 2014-07-16 10:15:56 -0700 | [diff] [blame] | 120 | mBounds.set(current->x, current->y, current->x, current->y); |
| 121 | for (; current < end; current++) { |
| 122 | mBounds.expandToCoverVertex(current->x, current->y); |
| 123 | } |
| 124 | } |
| 125 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 126 | const void* getBuffer() const { return mBuffer; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 127 | const void* getIndices() const { return mIndices; } |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 128 | const Rect& getBounds() const { return mBounds; } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 129 | unsigned int getVertexCount() const { return mVertexCount; } |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 130 | unsigned int getSize() const { return mByteCount; } |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 131 | unsigned int getIndexCount() const { return mIndexCount; } |
| 132 | void updateIndexCount(unsigned int newCount) { |
| 133 | mIndexCount = MathUtils::min(newCount, mAllocatedIndexCount); |
| 134 | } |
| 135 | void updateVertexCount(unsigned int newCount) { |
ztenghui | 512e643 | 2014-09-10 13:08:20 -0700 | [diff] [blame] | 136 | mVertexCount = MathUtils::min(newCount, mAllocatedVertexCount); |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 137 | } |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 138 | Mode getMode() const { return mMode; } |
| 139 | |
| 140 | void setBounds(Rect bounds) { mBounds = bounds; } |
| 141 | void setMode(Mode mode) { mMode = mode; } |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 142 | |
| 143 | template <class TYPE> |
| 144 | void createDegenerateSeparators(int allocSize) { |
| 145 | TYPE* end = (TYPE*)mBuffer + mVertexCount; |
| 146 | for (TYPE* degen = (TYPE*)mBuffer + allocSize; degen < end; degen += 2 + allocSize) { |
| 147 | memcpy(degen, degen - 1, sizeof(TYPE)); |
| 148 | memcpy(degen + 1, degen + 2, sizeof(TYPE)); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | template <class TYPE> |
| 154 | static void cleanup(void* buffer) { |
| 155 | delete[] (TYPE*)buffer; |
| 156 | } |
| 157 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 158 | Rect mBounds; |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 159 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 160 | void* mBuffer; |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 161 | void* mIndices; |
| 162 | |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 163 | unsigned int mVertexCount; |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 164 | unsigned int mIndexCount; |
| 165 | unsigned int mAllocatedVertexCount; |
| 166 | unsigned int mAllocatedIndexCount; |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 167 | unsigned int mByteCount; |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 168 | |
Chris Craik | 05f3d6e | 2014-06-02 16:27:04 -0700 | [diff] [blame] | 169 | Mode mMode; |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 170 | |
| 171 | void* mReallocBuffer; // used for multi-allocation |
| 172 | |
| 173 | void (*mCleanupMethod)(void*); |
ztenghui | d5e8ade | 2014-08-13 15:48:02 -0700 | [diff] [blame] | 174 | void (*mCleanupIndexMethod)(void*); |
ztenghui | 55bfb4e | 2013-12-03 10:38:55 -0800 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | }; // namespace uirenderer |
| 178 | }; // namespace android |
| 179 | |
| 180 | #endif // ANDROID_HWUI_VERTEX_BUFFER_H |