blob: abc88fa3c05460285884cb144b2f8802529a46f5 [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 Guya5ef39a2010-12-03 16:48:20 -080025#include "Properties.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070026
27namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
31// Constructors/destructor
32///////////////////////////////////////////////////////////////////////////////
33
Romain Guy6f72beb2010-11-30 12:04:14 -080034Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads):
Romain Guya5ef39a2010-12-03 16:48:20 -080035 mXCount(xCount), mYCount(yCount), mEmptyQuads(emptyQuads) {
36 // Initialized with the maximum number of vertices we will need
Romain Guyfb5e23c2010-07-09 13:52:56 -070037 // 2 triangles per patch, 3 vertices per triangle
Romain Guy8ab40792010-12-07 13:30:10 -080038 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3;
Romain Guya5ef39a2010-12-03 16:48:20 -080039 mVertices = new TextureVertex[maxVertices];
Romain Guy6f72beb2010-11-30 12:04:14 -080040 mUploaded = false;
41
Romain Guya5ef39a2010-12-03 16:48:20 -080042 verticesCount = 0;
43 hasEmptyQuads = emptyQuads > 0;
44
Romain Guy6f72beb2010-11-30 12:04:14 -080045 mColorKey = 0;
46 mXDivs = new int32_t[mXCount];
47 mYDivs = new int32_t[mYCount];
Romain Guy03750a02010-10-18 14:06:08 -070048
Romain Guya5ef39a2010-12-03 16:48:20 -080049 PATCH_LOGD(" patch: xCount = %d, yCount = %d, emptyQuads = %d, max vertices = %d",
50 xCount, yCount, emptyQuads, maxVertices);
Romain Guybd41a112010-12-02 17:16:26 -080051
Romain Guy03750a02010-10-18 14:06:08 -070052 glGenBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070053}
54
55Patch::~Patch() {
Romain Guy03750a02010-10-18 14:06:08 -070056 delete[] mVertices;
Romain Guy6f72beb2010-11-30 12:04:14 -080057 delete[] mXDivs;
58 delete[] mYDivs;
Romain Guy03750a02010-10-18 14:06:08 -070059 glDeleteBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070060}
61
62///////////////////////////////////////////////////////////////////////////////
Romain Guy6f72beb2010-11-30 12:04:14 -080063// Patch management
64///////////////////////////////////////////////////////////////////////////////
65
66void Patch::copy(const int32_t* xDivs, const int32_t* yDivs) {
67 memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t));
68 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
69}
70
71void Patch::copy(const int32_t* yDivs) {
72 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
73}
74
75void Patch::updateColorKey(const uint32_t colorKey) {
76 mColorKey = colorKey;
77}
78
79bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs, const uint32_t colorKey) {
80 if (mColorKey != colorKey) {
81 updateColorKey(colorKey);
82 copy(xDivs, yDivs);
83 return false;
84 }
85
86 for (uint32_t i = 0; i < mXCount; i++) {
87 if (mXDivs[i] != xDivs[i]) {
88 // The Y divs may or may not match, copy everything
89 copy(xDivs, yDivs);
90 return false;
91 }
92 }
93
94 for (uint32_t i = 0; i < mYCount; i++) {
95 if (mYDivs[i] != yDivs[i]) {
96 // We know all the X divs match, copy only Y divs
97 copy(yDivs);
98 return false;
99 }
100 }
101
102 return true;
103}
104
105///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -0700106// Vertices management
107///////////////////////////////////////////////////////////////////////////////
108
Romain Guy759ea802010-09-16 20:49:46 -0700109void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
Romain Guy6f72beb2010-11-30 12:04:14 -0800110 float left, float top, float right, float bottom) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700111 if (hasEmptyQuads) quads.clear();
Romain Guya5ef39a2010-12-03 16:48:20 -0800112
113 // Reset the vertices count here, we will count exactly how many
114 // vertices we actually need when generating the quads
115 verticesCount = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -0700116
Romain Guy6f72beb2010-11-30 12:04:14 -0800117 const uint32_t xStretchCount = (mXCount + 1) >> 1;
118 const uint32_t yStretchCount = (mYCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700119
Romain Guy6820ac82010-09-15 18:11:50 -0700120 float stretchX = 0.0f;
121 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700122
123 const float meshWidth = right - left;
124
Romain Guyfb5e23c2010-07-09 13:52:56 -0700125 if (xStretchCount > 0) {
126 uint32_t stretchSize = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -0800127 for (uint32_t i = 1; i < mXCount; i += 2) {
128 stretchSize += mXDivs[i] - mXDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700129 }
Romain Guy6820ac82010-09-15 18:11:50 -0700130 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700131 const float fixed = bitmapWidth - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -0700132 const float xStretch = right - left - fixed;
133 stretchX = xStretch / xStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700134 }
135
136 if (yStretchCount > 0) {
137 uint32_t stretchSize = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -0800138 for (uint32_t i = 1; i < mYCount; i += 2) {
139 stretchSize += mYDivs[i] - mYDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700140 }
Romain Guy6820ac82010-09-15 18:11:50 -0700141 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700142 const float fixed = bitmapHeight - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -0700143 const float yStretch = bottom - top - fixed;
144 stretchY = yStretch / yStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700145 }
146
Romain Guy03750a02010-10-18 14:06:08 -0700147 TextureVertex* vertex = mVertices;
Romain Guy4bb94202010-10-12 15:59:26 -0700148 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700149
Romain Guy6820ac82010-09-15 18:11:50 -0700150 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700151
Romain Guy6820ac82010-09-15 18:11:50 -0700152 float y1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700153 float y2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700154 float v1 = 0.0f;
155
Romain Guyeb6a4a12011-01-18 14:02:16 -0800156 for (uint32_t i = 0; i < mYCount; i++) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800157 float stepY = mYDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700158 const float segment = stepY - previousStepY;
Romain Guy6820ac82010-09-15 18:11:50 -0700159
Romain Guy6820ac82010-09-15 18:11:50 -0700160 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800161 y2 = y1 + floorf(segment * stretchY + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700162 } else {
Romain Guy5e7c4692011-10-20 20:31:50 -0700163 y2 = y1 + segment;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700164 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700165
166 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
167 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
168 v1 += vOffset / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700169
Romain Guyeb6a4a12011-01-18 14:02:16 -0800170 if (stepY > 0.0f) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700171#if DEBUG_EXPLODE_PATCHES
172 y1 += i * EXPLODE_GAP;
173 y2 += i * EXPLODE_GAP;
174#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800175 generateRow(vertex, y1, y2, v1, v2, stretchX, right - left,
176 bitmapWidth, quadCount);
Romain Guyf504a2f2011-05-26 16:40:55 -0700177#if DEBUG_EXPLODE_PATCHES
178 y2 -= i * EXPLODE_GAP;
179#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800180 }
Romain Guy6820ac82010-09-15 18:11:50 -0700181
182 y1 = y2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700183 v1 = stepY / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700184
185 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700186 }
187
Romain Guyfdbec3e2011-01-19 10:37:35 -0800188 if (previousStepY != bitmapHeight) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700189 y2 = bottom - top;
190#if DEBUG_EXPLODE_PATCHES
191 y1 += mYCount * EXPLODE_GAP;
192 y2 += mYCount * EXPLODE_GAP;
193#endif
Romain Guy5e7c4692011-10-20 20:31:50 -0700194 generateRow(vertex, y1, y2, v1, 1.0f, stretchX, right - left, bitmapWidth, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800195 }
Romain Guy03750a02010-10-18 14:06:08 -0700196
Romain Guya5ef39a2010-12-03 16:48:20 -0800197 if (verticesCount > 0) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800198 Caches& caches = Caches::getInstance();
199 caches.bindMeshBuffer(meshBuffer);
Romain Guya5ef39a2010-12-03 16:48:20 -0800200 if (!mUploaded) {
201 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
202 mVertices, GL_DYNAMIC_DRAW);
203 mUploaded = true;
204 } else {
205 glBufferSubData(GL_ARRAY_BUFFER, 0,
206 sizeof(TextureVertex) * verticesCount, mVertices);
207 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800208 caches.resetVertexPointers();
Romain Guy6f72beb2010-11-30 12:04:14 -0800209 }
Romain Guya5ef39a2010-12-03 16:48:20 -0800210
211 PATCH_LOGD(" patch: new vertices count = %d", verticesCount);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700212}
213
Romain Guy5b3b3522010-10-27 18:57:51 -0700214void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800215 float stretchX, float width, float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700216 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700217
Romain Guy6820ac82010-09-15 18:11:50 -0700218 float x1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700219 float x2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700220 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700221
Romain Guy6820ac82010-09-15 18:11:50 -0700222 // Generate the row quad by quad
Romain Guyeb6a4a12011-01-18 14:02:16 -0800223 for (uint32_t i = 0; i < mXCount; i++) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800224 float stepX = mXDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700225 const float segment = stepX - previousStepX;
Romain Guy6820ac82010-09-15 18:11:50 -0700226
Romain Guy6820ac82010-09-15 18:11:50 -0700227 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800228 x2 = x1 + floorf(segment * stretchX + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700229 } else {
Romain Guy5e7c4692011-10-20 20:31:50 -0700230 x2 = x1 + segment;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700231 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700232
233 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
234 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
235 u1 += uOffset / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700236
Romain Guyeb6a4a12011-01-18 14:02:16 -0800237 if (stepX > 0.0f) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700238#if DEBUG_EXPLODE_PATCHES
239 x1 += i * EXPLODE_GAP;
240 x2 += i * EXPLODE_GAP;
241#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800242 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
Romain Guyf504a2f2011-05-26 16:40:55 -0700243#if DEBUG_EXPLODE_PATCHES
244 x2 -= i * EXPLODE_GAP;
245#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800246 }
Romain Guy6820ac82010-09-15 18:11:50 -0700247
248 x1 = x2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700249 u1 = stepX / bitmapWidth;
Romain Guy6820ac82010-09-15 18:11:50 -0700250
251 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700252 }
253
Romain Guyfdbec3e2011-01-19 10:37:35 -0800254 if (previousStepX != bitmapWidth) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700255 x2 = width;
256#if DEBUG_EXPLODE_PATCHES
257 x1 += mXCount * EXPLODE_GAP;
258 x2 += mXCount * EXPLODE_GAP;
259#endif
260 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800261 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700262}
263
Romain Guy7444da52011-01-17 10:53:31 -0800264void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800265 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800266 const uint32_t oldQuadCount = quadCount;
Romain Guyeb6a4a12011-01-18 14:02:16 -0800267 quadCount++;
Romain Guybd41a112010-12-02 17:16:26 -0800268
Romain Guy70561df2012-09-10 17:40:18 -0700269 if (x1 < 0.0f) x1 = 0.0f;
270 if (x2 < 0.0f) x2 = 0.0f;
271 if (y1 < 0.0f) y1 = 0.0f;
272 if (y2 < 0.0f) y2 = 0.0f;
273
Romain Guya5ef39a2010-12-03 16:48:20 -0800274 // Skip degenerate and transparent (empty) quads
Romain Guyeb6a4a12011-01-18 14:02:16 -0800275 if ((mColorKey >> oldQuadCount) & 0x1) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800276#if DEBUG_PATCHES_EMPTY_VERTICES
277 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
Romain Guy5e7c4692011-10-20 20:31:50 -0700278 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
279 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
Romain Guyfb13abd2011-01-16 15:16:38 -0800280#endif
Romain Guy7444da52011-01-17 10:53:31 -0800281 return;
Romain Guy4bb94202010-10-12 15:59:26 -0700282 }
283
Romain Guya5ef39a2010-12-03 16:48:20 -0800284 // Record all non empty quads
Romain Guy5b3b3522010-10-27 18:57:51 -0700285 if (hasEmptyQuads) {
286 Rect bounds(x1, y1, x2, y2);
287 quads.add(bounds);
288 }
289
Romain Guy6820ac82010-09-15 18:11:50 -0700290 // Left triangle
291 TextureVertex::set(vertex++, x1, y1, u1, v1);
292 TextureVertex::set(vertex++, x2, y1, u2, v1);
293 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700294
Romain Guy6820ac82010-09-15 18:11:50 -0700295 // Right triangle
296 TextureVertex::set(vertex++, x1, y2, u1, v2);
297 TextureVertex::set(vertex++, x2, y1, u2, v1);
298 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800299
300 // A quad is made of 2 triangles, 6 vertices
301 verticesCount += 6;
302
303#if DEBUG_PATCHES_VERTICES
304 PATCH_LOGD(" quad %d", oldQuadCount);
Romain Guy5e7c4692011-10-20 20:31:50 -0700305 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
306 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800307#endif
Romain Guyfb5e23c2010-07-09 13:52:56 -0700308}
309
310}; // namespace uirenderer
311}; // namespace android