blob: ebffd34cf795eaa4ff31ce43209fcbae76bf886b [file] [log] [blame]
Romain Guyfb5e23c2010-07-09 13:52:56 -07001/*
2 * Copyright (C) 2010 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#define LOG_TAG "OpenGLRenderer"
18
Romain Guy6820ac82010-09-15 18:11:50 -070019#include <cmath>
Romain Guyfb5e23c2010-07-09 13:52:56 -070020
Romain Guy4bb94202010-10-12 15:59:26 -070021#include <utils/Log.h>
22
Romain Guyfb5e23c2010-07-09 13:52:56 -070023#include "Patch.h"
Romain Guy03750a02010-10-18 14:06:08 -070024#include "Caches.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy6f72beb2010-11-30 12:04:14 -080033Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads):
34 mXCount(xCount), mYCount(yCount) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070035 // 2 triangles per patch, 3 vertices per triangle
Romain Guy4bb94202010-10-12 15:59:26 -070036 verticesCount = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3;
Romain Guy03750a02010-10-18 14:06:08 -070037 mVertices = new TextureVertex[verticesCount];
Romain Guy5b3b3522010-10-27 18:57:51 -070038 hasEmptyQuads = emptyQuads > 0;
Romain Guy6f72beb2010-11-30 12:04:14 -080039 mUploaded = false;
40
41 mColorKey = 0;
42 mXDivs = new int32_t[mXCount];
43 mYDivs = new int32_t[mYCount];
Romain Guy03750a02010-10-18 14:06:08 -070044
Romain Guybd41a112010-12-02 17:16:26 -080045 PATCH_LOGD(" patch: xCount = %d, yCount = %d, emptyQuads = %d, vertices = %d",
46 xCount, yCount, emptyQuads, verticesCount);
47
Romain Guy03750a02010-10-18 14:06:08 -070048 glGenBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070049}
50
51Patch::~Patch() {
Romain Guy03750a02010-10-18 14:06:08 -070052 delete[] mVertices;
Romain Guy6f72beb2010-11-30 12:04:14 -080053 delete[] mXDivs;
54 delete[] mYDivs;
Romain Guy03750a02010-10-18 14:06:08 -070055 glDeleteBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070056}
57
58///////////////////////////////////////////////////////////////////////////////
Romain Guy6f72beb2010-11-30 12:04:14 -080059// Patch management
60///////////////////////////////////////////////////////////////////////////////
61
62void Patch::copy(const int32_t* xDivs, const int32_t* yDivs) {
63 memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t));
64 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
65}
66
67void Patch::copy(const int32_t* yDivs) {
68 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
69}
70
71void Patch::updateColorKey(const uint32_t colorKey) {
72 mColorKey = colorKey;
73}
74
75bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs, const uint32_t colorKey) {
76 if (mColorKey != colorKey) {
77 updateColorKey(colorKey);
78 copy(xDivs, yDivs);
79 return false;
80 }
81
82 for (uint32_t i = 0; i < mXCount; i++) {
83 if (mXDivs[i] != xDivs[i]) {
84 // The Y divs may or may not match, copy everything
85 copy(xDivs, yDivs);
86 return false;
87 }
88 }
89
90 for (uint32_t i = 0; i < mYCount; i++) {
91 if (mYDivs[i] != yDivs[i]) {
92 // We know all the X divs match, copy only Y divs
93 copy(yDivs);
94 return false;
95 }
96 }
97
98 return true;
99}
100
101///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -0700102// Vertices management
103///////////////////////////////////////////////////////////////////////////////
104
Romain Guy759ea802010-09-16 20:49:46 -0700105void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
Romain Guy6f72beb2010-11-30 12:04:14 -0800106 float left, float top, float right, float bottom) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700107 if (hasEmptyQuads) quads.clear();
108
Romain Guy6f72beb2010-11-30 12:04:14 -0800109 const uint32_t xStretchCount = (mXCount + 1) >> 1;
110 const uint32_t yStretchCount = (mYCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700111
Romain Guy6820ac82010-09-15 18:11:50 -0700112 float stretchX = 0.0f;
113 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700114
115 const float meshWidth = right - left;
116
Romain Guyfb5e23c2010-07-09 13:52:56 -0700117 if (xStretchCount > 0) {
118 uint32_t stretchSize = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -0800119 for (uint32_t i = 1; i < mXCount; i += 2) {
120 stretchSize += mXDivs[i] - mXDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700121 }
Romain Guy6820ac82010-09-15 18:11:50 -0700122 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700123 const float fixed = bitmapWidth - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -0700124 const float xStretch = right - left - fixed;
125 stretchX = xStretch / xStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700126 }
127
128 if (yStretchCount > 0) {
129 uint32_t stretchSize = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -0800130 for (uint32_t i = 1; i < mYCount; i += 2) {
131 stretchSize += mYDivs[i] - mYDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700132 }
Romain Guy6820ac82010-09-15 18:11:50 -0700133 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700134 const float fixed = bitmapHeight - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -0700135 const float yStretch = bottom - top - fixed;
136 stretchY = yStretch / yStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700137 }
138
Romain Guy03750a02010-10-18 14:06:08 -0700139 TextureVertex* vertex = mVertices;
Romain Guy4bb94202010-10-12 15:59:26 -0700140 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700141
Romain Guy6820ac82010-09-15 18:11:50 -0700142 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700143
Romain Guy6820ac82010-09-15 18:11:50 -0700144 float y1 = 0.0f;
145 float v1 = 0.0f;
146
Romain Guy6f72beb2010-11-30 12:04:14 -0800147 for (uint32_t i = 0; i < mYCount; i++) {
148 float stepY = mYDivs[i];
Romain Guy6820ac82010-09-15 18:11:50 -0700149
150 float y2 = 0.0f;
151 if (i & 1) {
152 const float segment = stepY - previousStepY;
153 y2 = y1 + segment * stretchY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700154 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700155 y2 = y1 + stepY - previousStepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700156 }
Romain Guy6820ac82010-09-15 18:11:50 -0700157 float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight;
158
Romain Guy6f72beb2010-11-30 12:04:14 -0800159 generateRow(vertex, y1, y2, v1, v2, stretchX, right - left, bitmapWidth, quadCount);
Romain Guy6820ac82010-09-15 18:11:50 -0700160
161 y1 = y2;
162 v1 = (stepY + 0.5f) / bitmapHeight;
163
164 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700165 }
166
Romain Guy6f72beb2010-11-30 12:04:14 -0800167 generateRow(vertex, y1, bottom - top, v1, 1.0f, stretchX, right - left,
168 bitmapWidth, quadCount);
Romain Guy03750a02010-10-18 14:06:08 -0700169
170 Caches::getInstance().bindMeshBuffer(meshBuffer);
Romain Guy6f72beb2010-11-30 12:04:14 -0800171 if (!mUploaded) {
172 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
173 mVertices, GL_DYNAMIC_DRAW);
174 mUploaded = true;
175 } else {
176 glBufferSubData(GL_ARRAY_BUFFER, 0,
177 sizeof(TextureVertex) * verticesCount, mVertices);
178 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700179}
180
Romain Guy5b3b3522010-10-27 18:57:51 -0700181void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
Romain Guy6f72beb2010-11-30 12:04:14 -0800182 float stretchX, float width, float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700183 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700184
Romain Guy6820ac82010-09-15 18:11:50 -0700185 float x1 = 0.0f;
186 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700187
Romain Guy6820ac82010-09-15 18:11:50 -0700188 // Generate the row quad by quad
Romain Guy6f72beb2010-11-30 12:04:14 -0800189 for (uint32_t i = 0; i < mXCount; i++) {
190 float stepX = mXDivs[i];
Romain Guy6820ac82010-09-15 18:11:50 -0700191
192 float x2 = 0.0f;
193 if (i & 1) {
194 const float segment = stepX - previousStepX;
195 x2 = x1 + segment * stretchX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700196 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700197 x2 = x1 + stepX - previousStepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700198 }
Romain Guy6820ac82010-09-15 18:11:50 -0700199 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700200
Romain Guy6f72beb2010-11-30 12:04:14 -0800201 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
Romain Guy6820ac82010-09-15 18:11:50 -0700202
203 x1 = x2;
204 u1 = (stepX + 0.5f) / bitmapWidth;
205
206 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700207 }
208
Romain Guy6f72beb2010-11-30 12:04:14 -0800209 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700210}
211
Romain Guy5b3b3522010-10-27 18:57:51 -0700212void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guy6f72beb2010-11-30 12:04:14 -0800213 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guybd41a112010-12-02 17:16:26 -0800214 uint32_t oldQuadCount = quadCount;
215
216 // Degenerate quads are an artifact of our implementation and should not
217 // be taken into account when checking for transparent quads
218 if (x2 - x1 > 0.999f && y2 - y1 > 0.999f) {
219 quadCount++;
220 }
221
222 if (((mColorKey >> oldQuadCount) & 0x1) == 1) {
Romain Guy4bb94202010-10-12 15:59:26 -0700223 return;
224 }
225
Romain Guy5b3b3522010-10-27 18:57:51 -0700226 if (hasEmptyQuads) {
227 Rect bounds(x1, y1, x2, y2);
228 quads.add(bounds);
229 }
230
Romain Guy6820ac82010-09-15 18:11:50 -0700231 // Left triangle
232 TextureVertex::set(vertex++, x1, y1, u1, v1);
233 TextureVertex::set(vertex++, x2, y1, u2, v1);
234 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700235
Romain Guy6820ac82010-09-15 18:11:50 -0700236 // Right triangle
237 TextureVertex::set(vertex++, x1, y2, u1, v2);
238 TextureVertex::set(vertex++, x2, y1, u2, v1);
239 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700240}
241
242}; // namespace uirenderer
243}; // namespace android