blob: 5875f25aa514d2385c9c6435bd7222c15b25cb9a [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
20
21namespace android {
22namespace uirenderer {
23
24class VertexBuffer {
25public:
Chris Craik05f3d6e2014-06-02 16:27:04 -070026 enum Mode {
27 kStandard = 0,
28 kOnePolyRingShadow = 1,
29 kTwoPolyRingShadow = 2
30 };
31
32 VertexBuffer()
33 : mBuffer(0)
34 , mVertexCount(0)
35 , mByteCount(0)
36 , mMode(kStandard)
37 , mCleanupMethod(NULL)
ztenghui55bfb4e2013-12-03 10:38:55 -080038 {}
39
40 ~VertexBuffer() {
41 if (mCleanupMethod) mCleanupMethod(mBuffer);
42 }
43
44 /**
45 This should be the only method used by the Tessellator. Subsequent calls to
46 alloc will allocate space within the first allocation (useful if you want to
47 eventually allocate multiple regions within a single VertexBuffer, such as
Chris Craik05f3d6e2014-06-02 16:27:04 -070048 with PathTessellator::tessellateLines())
ztenghui55bfb4e2013-12-03 10:38:55 -080049 */
50 template <class TYPE>
51 TYPE* alloc(int vertexCount) {
52 if (mVertexCount) {
53 TYPE* reallocBuffer = (TYPE*)mReallocBuffer;
54 // already have allocated the buffer, re-allocate space within
55 if (mReallocBuffer != mBuffer) {
56 // not first re-allocation, leave space for degenerate triangles to separate strips
57 reallocBuffer += 2;
58 }
59 mReallocBuffer = reallocBuffer + vertexCount;
60 return reallocBuffer;
61 }
62 mVertexCount = vertexCount;
Chris Craik05f3d6e2014-06-02 16:27:04 -070063 mByteCount = mVertexCount * sizeof(TYPE);
ztenghui55bfb4e2013-12-03 10:38:55 -080064 mReallocBuffer = mBuffer = (void*)new TYPE[vertexCount];
Chris Craikc93e45c2014-07-16 10:15:56 -070065 memset(mBuffer, 0, sizeof(TYPE) * vertexCount);
66
ztenghui55bfb4e2013-12-03 10:38:55 -080067 mCleanupMethod = &(cleanup<TYPE>);
68
69 return (TYPE*)mBuffer;
70 }
71
72 template <class TYPE>
73 void copyInto(const VertexBuffer& srcBuffer, float xOffset, float yOffset) {
74 int verticesToCopy = srcBuffer.getVertexCount();
75
76 TYPE* dst = alloc<TYPE>(verticesToCopy);
77 TYPE* src = (TYPE*)srcBuffer.getBuffer();
78
79 for (int i = 0; i < verticesToCopy; i++) {
80 TYPE::copyWithOffset(&dst[i], src[i], xOffset, yOffset);
81 }
82 }
83
Chris Craikc93e45c2014-07-16 10:15:56 -070084 /**
85 * Brute force bounds computation, used only if the producer of this
86 * vertex buffer can't determine bounds more simply/efficiently
87 */
88 template <class TYPE>
89 void computeBounds() {
90 if (!mVertexCount) {
91 mBounds.setEmpty();
92 return;
93 }
94 TYPE* current = (TYPE*)mBuffer;
95 TYPE* end = current + mVertexCount;
96 mBounds.set(current->x, current->y, current->x, current->y);
97 for (; current < end; current++) {
98 mBounds.expandToCoverVertex(current->x, current->y);
99 }
100 }
101
ztenghui55bfb4e2013-12-03 10:38:55 -0800102 const void* getBuffer() const { return mBuffer; }
Chris Craik05f3d6e2014-06-02 16:27:04 -0700103 const Rect& getBounds() const { return mBounds; }
ztenghui55bfb4e2013-12-03 10:38:55 -0800104 unsigned int getVertexCount() const { return mVertexCount; }
Chris Craik05f3d6e2014-06-02 16:27:04 -0700105 unsigned int getSize() const { return mByteCount; }
106 Mode getMode() const { return mMode; }
107
108 void setBounds(Rect bounds) { mBounds = bounds; }
109 void setMode(Mode mode) { mMode = mode; }
ztenghui55bfb4e2013-12-03 10:38:55 -0800110
111 template <class TYPE>
112 void createDegenerateSeparators(int allocSize) {
113 TYPE* end = (TYPE*)mBuffer + mVertexCount;
114 for (TYPE* degen = (TYPE*)mBuffer + allocSize; degen < end; degen += 2 + allocSize) {
115 memcpy(degen, degen - 1, sizeof(TYPE));
116 memcpy(degen + 1, degen + 2, sizeof(TYPE));
117 }
118 }
119
120private:
121 template <class TYPE>
122 static void cleanup(void* buffer) {
123 delete[] (TYPE*)buffer;
124 }
125
Chris Craik05f3d6e2014-06-02 16:27:04 -0700126 Rect mBounds;
ztenghui55bfb4e2013-12-03 10:38:55 -0800127 void* mBuffer;
128 unsigned int mVertexCount;
Chris Craik05f3d6e2014-06-02 16:27:04 -0700129 unsigned int mByteCount;
130 Mode mMode;
ztenghui55bfb4e2013-12-03 10:38:55 -0800131
132 void* mReallocBuffer; // used for multi-allocation
133
134 void (*mCleanupMethod)(void*);
135};
136
137}; // namespace uirenderer
138}; // namespace android
139
140#endif // ANDROID_HWUI_VERTEX_BUFFER_H