blob: 6543a2251f7ab6c5b114cc45a509a4433c7d04b5 [file] [log] [blame]
ztenghui55bfb4e2013-12-03 10:38:55 -08001/*
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
Chris Craik9db58c02015-08-19 15:19:18 -070020#include <algorithm>
ztenghui55bfb4e2013-12-03 10:38:55 -080021
22namespace android {
23namespace uirenderer {
24
25class VertexBuffer {
26public:
Chris Craik117bdbc2015-02-05 10:12:38 -080027 enum MeshFeatureFlags {
28 kNone = 0,
29 kAlpha = 1 << 0,
30 kIndices = 1 << 1,
Chris Craik05f3d6e2014-06-02 16:27:04 -070031 };
32
33 VertexBuffer()
Chris Craike84a2082014-12-22 14:28:49 -080034 : mBuffer(nullptr)
35 , mIndices(nullptr)
Chris Craik05f3d6e2014-06-02 16:27:04 -070036 , mVertexCount(0)
ztenghuid5e8ade2014-08-13 15:48:02 -070037 , mIndexCount(0)
38 , mAllocatedVertexCount(0)
39 , mAllocatedIndexCount(0)
Chris Craik05f3d6e2014-06-02 16:27:04 -070040 , mByteCount(0)
Chris Craik117bdbc2015-02-05 10:12:38 -080041 , mMeshFeatureFlags(kNone)
Chris Craike84a2082014-12-22 14:28:49 -080042 , mReallocBuffer(nullptr)
43 , mCleanupMethod(nullptr)
John Reck1bcacfd2017-11-03 10:12:19 -070044 , mCleanupIndexMethod(nullptr) {}
ztenghui55bfb4e2013-12-03 10:38:55 -080045
46 ~VertexBuffer() {
47 if (mCleanupMethod) mCleanupMethod(mBuffer);
ztenghuid5e8ade2014-08-13 15:48:02 -070048 if (mCleanupIndexMethod) mCleanupIndexMethod(mIndices);
ztenghui55bfb4e2013-12-03 10:38:55 -080049 }
50
51 /**
52 This should be the only method used by the Tessellator. Subsequent calls to
53 alloc will allocate space within the first allocation (useful if you want to
54 eventually allocate multiple regions within a single VertexBuffer, such as
Chris Craik05f3d6e2014-06-02 16:27:04 -070055 with PathTessellator::tessellateLines())
ztenghui55bfb4e2013-12-03 10:38:55 -080056 */
57 template <class TYPE>
58 TYPE* alloc(int vertexCount) {
59 if (mVertexCount) {
60 TYPE* reallocBuffer = (TYPE*)mReallocBuffer;
61 // already have allocated the buffer, re-allocate space within
62 if (mReallocBuffer != mBuffer) {
63 // not first re-allocation, leave space for degenerate triangles to separate strips
64 reallocBuffer += 2;
65 }
66 mReallocBuffer = reallocBuffer + vertexCount;
67 return reallocBuffer;
68 }
ztenghuid5e8ade2014-08-13 15:48:02 -070069 mAllocatedVertexCount = vertexCount;
ztenghui55bfb4e2013-12-03 10:38:55 -080070 mVertexCount = vertexCount;
Chris Craik05f3d6e2014-06-02 16:27:04 -070071 mByteCount = mVertexCount * sizeof(TYPE);
ztenghui55bfb4e2013-12-03 10:38:55 -080072 mReallocBuffer = mBuffer = (void*)new TYPE[vertexCount];
Chris Craikc93e45c2014-07-16 10:15:56 -070073
ztenghui55bfb4e2013-12-03 10:38:55 -080074 mCleanupMethod = &(cleanup<TYPE>);
75
76 return (TYPE*)mBuffer;
77 }
78
79 template <class TYPE>
ztenghuid5e8ade2014-08-13 15:48:02 -070080 TYPE* allocIndices(int indexCount) {
81 mAllocatedIndexCount = indexCount;
82 mIndexCount = indexCount;
83 mIndices = (void*)new TYPE[indexCount];
84
85 mCleanupIndexMethod = &(cleanup<TYPE>);
86
87 return (TYPE*)mIndices;
88 }
89
90 template <class TYPE>
ztenghui55bfb4e2013-12-03 10:38:55 -080091 void copyInto(const VertexBuffer& srcBuffer, float xOffset, float yOffset) {
92 int verticesToCopy = srcBuffer.getVertexCount();
93
94 TYPE* dst = alloc<TYPE>(verticesToCopy);
95 TYPE* src = (TYPE*)srcBuffer.getBuffer();
96
97 for (int i = 0; i < verticesToCopy; i++) {
98 TYPE::copyWithOffset(&dst[i], src[i], xOffset, yOffset);
99 }
100 }
101
Chris Craikc93e45c2014-07-16 10:15:56 -0700102 /**
103 * Brute force bounds computation, used only if the producer of this
104 * vertex buffer can't determine bounds more simply/efficiently
105 */
106 template <class TYPE>
Chris Craik9a89bc62014-07-23 17:21:25 -0700107 void computeBounds(int vertexCount = 0) {
Chris Craikc93e45c2014-07-16 10:15:56 -0700108 if (!mVertexCount) {
109 mBounds.setEmpty();
110 return;
111 }
Chris Craik9a89bc62014-07-23 17:21:25 -0700112
113 // default: compute over every vertex
114 if (vertexCount == 0) vertexCount = mVertexCount;
115
Chris Craikc93e45c2014-07-16 10:15:56 -0700116 TYPE* current = (TYPE*)mBuffer;
Chris Craik9a89bc62014-07-23 17:21:25 -0700117 TYPE* end = current + vertexCount;
Chris Craikc93e45c2014-07-16 10:15:56 -0700118 mBounds.set(current->x, current->y, current->x, current->y);
119 for (; current < end; current++) {
Chris Craik15c3f192015-12-03 12:16:56 -0800120 mBounds.expandToCover(current->x, current->y);
Chris Craikc93e45c2014-07-16 10:15:56 -0700121 }
122 }
123
ztenghui55bfb4e2013-12-03 10:38:55 -0800124 const void* getBuffer() const { return mBuffer; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700125 const void* getIndices() const { return mIndices; }
Chris Craik05f3d6e2014-06-02 16:27:04 -0700126 const Rect& getBounds() const { return mBounds; }
ztenghui55bfb4e2013-12-03 10:38:55 -0800127 unsigned int getVertexCount() const { return mVertexCount; }
Chris Craik05f3d6e2014-06-02 16:27:04 -0700128 unsigned int getSize() const { return mByteCount; }
ztenghuid5e8ade2014-08-13 15:48:02 -0700129 unsigned int getIndexCount() const { return mIndexCount; }
John Reck1bcacfd2017-11-03 10:12:19 -0700130 void updateIndexCount(unsigned int newCount) {
Chris Craik9db58c02015-08-19 15:19:18 -0700131 mIndexCount = std::min(newCount, mAllocatedIndexCount);
ztenghuid5e8ade2014-08-13 15:48:02 -0700132 }
John Reck1bcacfd2017-11-03 10:12:19 -0700133 void updateVertexCount(unsigned int newCount) {
Chris Craik9db58c02015-08-19 15:19:18 -0700134 mVertexCount = std::min(newCount, mAllocatedVertexCount);
ztenghuid5e8ade2014-08-13 15:48:02 -0700135 }
Chris Craik117bdbc2015-02-05 10:12:38 -0800136 MeshFeatureFlags getMeshFeatureFlags() const { return mMeshFeatureFlags; }
137 void setMeshFeatureFlags(int flags) {
138 mMeshFeatureFlags = static_cast<MeshFeatureFlags>(flags);
139 }
Chris Craik05f3d6e2014-06-02 16:27:04 -0700140
141 void setBounds(Rect bounds) { mBounds = bounds; }
ztenghui55bfb4e2013-12-03 10:38:55 -0800142
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
152private:
153 template <class TYPE>
154 static void cleanup(void* buffer) {
John Reck1bcacfd2017-11-03 10:12:19 -0700155 delete[](TYPE*) buffer;
ztenghui55bfb4e2013-12-03 10:38:55 -0800156 }
157
Chris Craik05f3d6e2014-06-02 16:27:04 -0700158 Rect mBounds;
ztenghuid5e8ade2014-08-13 15:48:02 -0700159
ztenghui55bfb4e2013-12-03 10:38:55 -0800160 void* mBuffer;
ztenghuid5e8ade2014-08-13 15:48:02 -0700161 void* mIndices;
162
ztenghui55bfb4e2013-12-03 10:38:55 -0800163 unsigned int mVertexCount;
ztenghuid5e8ade2014-08-13 15:48:02 -0700164 unsigned int mIndexCount;
165 unsigned int mAllocatedVertexCount;
166 unsigned int mAllocatedIndexCount;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700167 unsigned int mByteCount;
ztenghuid5e8ade2014-08-13 15:48:02 -0700168
Chris Craik117bdbc2015-02-05 10:12:38 -0800169 MeshFeatureFlags mMeshFeatureFlags;
ztenghui55bfb4e2013-12-03 10:38:55 -0800170
John Reck1bcacfd2017-11-03 10:12:19 -0700171 void* mReallocBuffer; // used for multi-allocation
ztenghui55bfb4e2013-12-03 10:38:55 -0800172
173 void (*mCleanupMethod)(void*);
ztenghuid5e8ade2014-08-13 15:48:02 -0700174 void (*mCleanupIndexMethod)(void*);
ztenghui55bfb4e2013-12-03 10:38:55 -0800175};
176
Chris Blume7b8a8082018-11-30 15:51:58 -0800177} // namespace uirenderer
178} // namespace android
ztenghui55bfb4e2013-12-03 10:38:55 -0800179
John Reck1bcacfd2017-11-03 10:12:19 -0700180#endif // ANDROID_HWUI_VERTEX_BUFFER_H