blob: 45c619ee01d40d5fbac15b917db147f8d75aeb68 [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 Guy5341cea2013-01-09 14:15:58 -080040 mAllocatedVerticesCount = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -080041
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
Romain Guy6f72beb2010-11-30 12:04:14 -080071void Patch::updateColorKey(const uint32_t colorKey) {
72 mColorKey = colorKey;
73}
74
Romain Guy5341cea2013-01-09 14:15:58 -080075bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs,
76 const uint32_t colorKey, const int8_t emptyQuads) {
77
78 bool matches = true;
79
80 if (mEmptyQuads != emptyQuads) {
81 mEmptyQuads = emptyQuads;
82 hasEmptyQuads = emptyQuads > 0;
83 matches = false;
84 }
85
Romain Guy6f72beb2010-11-30 12:04:14 -080086 if (mColorKey != colorKey) {
87 updateColorKey(colorKey);
Romain Guy5341cea2013-01-09 14:15:58 -080088 matches = false;
Romain Guy6f72beb2010-11-30 12:04:14 -080089 }
90
Romain Guy5341cea2013-01-09 14:15:58 -080091 if (memcmp(mXDivs, xDivs, mXCount * sizeof(int32_t))) {
92 memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t));
93 matches = false;
Romain Guy6f72beb2010-11-30 12:04:14 -080094 }
95
Romain Guy5341cea2013-01-09 14:15:58 -080096 if (memcmp(mYDivs, yDivs, mYCount * sizeof(int32_t))) {
97 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t));
98 matches = false;
Romain Guy6f72beb2010-11-30 12:04:14 -080099 }
100
Romain Guy5341cea2013-01-09 14:15:58 -0800101 return matches;
Romain Guy6f72beb2010-11-30 12:04:14 -0800102}
103
104///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -0700105// Vertices management
106///////////////////////////////////////////////////////////////////////////////
107
Romain Guy759ea802010-09-16 20:49:46 -0700108void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
Romain Guy6f72beb2010-11-30 12:04:14 -0800109 float left, float top, float right, float bottom) {
Romain Guy5b3b3522010-10-27 18:57:51 -0700110 if (hasEmptyQuads) quads.clear();
Romain Guya5ef39a2010-12-03 16:48:20 -0800111
112 // Reset the vertices count here, we will count exactly how many
113 // vertices we actually need when generating the quads
114 verticesCount = 0;
Romain Guy5b3b3522010-10-27 18:57:51 -0700115
Romain Guy6f72beb2010-11-30 12:04:14 -0800116 const uint32_t xStretchCount = (mXCount + 1) >> 1;
117 const uint32_t yStretchCount = (mYCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700118
Romain Guy6820ac82010-09-15 18:11:50 -0700119 float stretchX = 0.0f;
Romain Guy41d35ae2012-10-10 16:06:04 -0700120 float stretchY = 0.0f;
121
122 float rescaleX = 1.0f;
123 float rescaleY = 1.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700124
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 Guy41d35ae2012-10-10 16:06:04 -0700132 const float xStretch = fmaxf(right - left - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -0700133 stretchX = xStretch / xStretchTex;
Romain Guyc37f3492012-10-10 17:13:36 -0700134 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700135 }
136
137 if (yStretchCount > 0) {
138 uint32_t stretchSize = 0;
Romain Guy6f72beb2010-11-30 12:04:14 -0800139 for (uint32_t i = 1; i < mYCount; i += 2) {
140 stretchSize += mYDivs[i] - mYDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700141 }
Romain Guy6820ac82010-09-15 18:11:50 -0700142 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700143 const float fixed = bitmapHeight - stretchSize;
Romain Guy41d35ae2012-10-10 16:06:04 -0700144 const float yStretch = fmaxf(bottom - top - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -0700145 stretchY = yStretch / yStretchTex;
Romain Guyc37f3492012-10-10 17:13:36 -0700146 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700147 }
148
Romain Guy03750a02010-10-18 14:06:08 -0700149 TextureVertex* vertex = mVertices;
Romain Guy4bb94202010-10-12 15:59:26 -0700150 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700151
Romain Guy6820ac82010-09-15 18:11:50 -0700152 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700153
Romain Guy6820ac82010-09-15 18:11:50 -0700154 float y1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700155 float y2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700156 float v1 = 0.0f;
157
Romain Guyeb6a4a12011-01-18 14:02:16 -0800158 for (uint32_t i = 0; i < mYCount; i++) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800159 float stepY = mYDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700160 const float segment = stepY - previousStepY;
Romain Guy6820ac82010-09-15 18:11:50 -0700161
Romain Guy6820ac82010-09-15 18:11:50 -0700162 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800163 y2 = y1 + floorf(segment * stretchY + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700164 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700165 y2 = y1 + segment * rescaleY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700166 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700167
168 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
169 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
170 v1 += vOffset / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700171
Romain Guyeb6a4a12011-01-18 14:02:16 -0800172 if (stepY > 0.0f) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700173#if DEBUG_EXPLODE_PATCHES
174 y1 += i * EXPLODE_GAP;
175 y2 += i * EXPLODE_GAP;
176#endif
Romain Guy41d35ae2012-10-10 16:06:04 -0700177 generateRow(vertex, y1, y2, v1, v2, stretchX, rescaleX, right - left,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800178 bitmapWidth, quadCount);
Romain Guyf504a2f2011-05-26 16:40:55 -0700179#if DEBUG_EXPLODE_PATCHES
180 y2 -= i * EXPLODE_GAP;
181#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800182 }
Romain Guy6820ac82010-09-15 18:11:50 -0700183
184 y1 = y2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700185 v1 = stepY / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700186
187 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700188 }
189
Romain Guyfdbec3e2011-01-19 10:37:35 -0800190 if (previousStepY != bitmapHeight) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700191 y2 = bottom - top;
192#if DEBUG_EXPLODE_PATCHES
193 y1 += mYCount * EXPLODE_GAP;
194 y2 += mYCount * EXPLODE_GAP;
195#endif
Romain Guy41d35ae2012-10-10 16:06:04 -0700196 generateRow(vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, right - left,
197 bitmapWidth, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800198 }
Romain Guy03750a02010-10-18 14:06:08 -0700199
Romain Guya5ef39a2010-12-03 16:48:20 -0800200 if (verticesCount > 0) {
Romain Guyf3a910b42011-12-12 20:35:21 -0800201 Caches& caches = Caches::getInstance();
202 caches.bindMeshBuffer(meshBuffer);
Romain Guy5341cea2013-01-09 14:15:58 -0800203 if (mAllocatedVerticesCount < verticesCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800204 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
205 mVertices, GL_DYNAMIC_DRAW);
Romain Guy5341cea2013-01-09 14:15:58 -0800206 mAllocatedVerticesCount = verticesCount;
Romain Guya5ef39a2010-12-03 16:48:20 -0800207 } else {
208 glBufferSubData(GL_ARRAY_BUFFER, 0,
209 sizeof(TextureVertex) * verticesCount, mVertices);
210 }
Romain Guyf3a910b42011-12-12 20:35:21 -0800211 caches.resetVertexPointers();
Romain Guy6f72beb2010-11-30 12:04:14 -0800212 }
Romain Guya5ef39a2010-12-03 16:48:20 -0800213
214 PATCH_LOGD(" patch: new vertices count = %d", verticesCount);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700215}
216
Romain Guy5b3b3522010-10-27 18:57:51 -0700217void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
Romain Guy41d35ae2012-10-10 16:06:04 -0700218 float stretchX, float rescaleX, float width, float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700219 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700220
Romain Guy6820ac82010-09-15 18:11:50 -0700221 float x1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700222 float x2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700223 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700224
Romain Guy6820ac82010-09-15 18:11:50 -0700225 // Generate the row quad by quad
Romain Guyeb6a4a12011-01-18 14:02:16 -0800226 for (uint32_t i = 0; i < mXCount; i++) {
Romain Guy6f72beb2010-11-30 12:04:14 -0800227 float stepX = mXDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700228 const float segment = stepX - previousStepX;
Romain Guy6820ac82010-09-15 18:11:50 -0700229
Romain Guy6820ac82010-09-15 18:11:50 -0700230 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800231 x2 = x1 + floorf(segment * stretchX + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700232 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700233 x2 = x1 + segment * rescaleX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700234 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700235
236 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
237 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
238 u1 += uOffset / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700239
Romain Guyeb6a4a12011-01-18 14:02:16 -0800240 if (stepX > 0.0f) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700241#if DEBUG_EXPLODE_PATCHES
242 x1 += i * EXPLODE_GAP;
243 x2 += i * EXPLODE_GAP;
244#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800245 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
Romain Guyf504a2f2011-05-26 16:40:55 -0700246#if DEBUG_EXPLODE_PATCHES
247 x2 -= i * EXPLODE_GAP;
248#endif
Romain Guyeb6a4a12011-01-18 14:02:16 -0800249 }
Romain Guy6820ac82010-09-15 18:11:50 -0700250
251 x1 = x2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700252 u1 = stepX / bitmapWidth;
Romain Guy6820ac82010-09-15 18:11:50 -0700253
254 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700255 }
256
Romain Guyfdbec3e2011-01-19 10:37:35 -0800257 if (previousStepX != bitmapWidth) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700258 x2 = width;
259#if DEBUG_EXPLODE_PATCHES
260 x1 += mXCount * EXPLODE_GAP;
261 x2 += mXCount * EXPLODE_GAP;
262#endif
263 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800264 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700265}
266
Romain Guy7444da52011-01-17 10:53:31 -0800267void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800268 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800269 const uint32_t oldQuadCount = quadCount;
Romain Guyeb6a4a12011-01-18 14:02:16 -0800270 quadCount++;
Romain Guybd41a112010-12-02 17:16:26 -0800271
Romain Guy70561df2012-09-10 17:40:18 -0700272 if (x1 < 0.0f) x1 = 0.0f;
273 if (x2 < 0.0f) x2 = 0.0f;
274 if (y1 < 0.0f) y1 = 0.0f;
275 if (y2 < 0.0f) y2 = 0.0f;
276
Romain Guya5ef39a2010-12-03 16:48:20 -0800277 // Skip degenerate and transparent (empty) quads
Romain Guy41d35ae2012-10-10 16:06:04 -0700278 if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800279#if DEBUG_PATCHES_EMPTY_VERTICES
280 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
Romain Guy5e7c4692011-10-20 20:31:50 -0700281 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
282 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
Romain Guyfb13abd2011-01-16 15:16:38 -0800283#endif
Romain Guy7444da52011-01-17 10:53:31 -0800284 return;
Romain Guy4bb94202010-10-12 15:59:26 -0700285 }
286
Romain Guya5ef39a2010-12-03 16:48:20 -0800287 // Record all non empty quads
Romain Guy5b3b3522010-10-27 18:57:51 -0700288 if (hasEmptyQuads) {
289 Rect bounds(x1, y1, x2, y2);
290 quads.add(bounds);
291 }
292
Romain Guy6820ac82010-09-15 18:11:50 -0700293 // Left triangle
294 TextureVertex::set(vertex++, x1, y1, u1, v1);
295 TextureVertex::set(vertex++, x2, y1, u2, v1);
296 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700297
Romain Guy6820ac82010-09-15 18:11:50 -0700298 // Right triangle
299 TextureVertex::set(vertex++, x1, y2, u1, v2);
300 TextureVertex::set(vertex++, x2, y1, u2, v1);
301 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800302
303 // A quad is made of 2 triangles, 6 vertices
304 verticesCount += 6;
305
306#if DEBUG_PATCHES_VERTICES
307 PATCH_LOGD(" quad %d", oldQuadCount);
Romain Guy5e7c4692011-10-20 20:31:50 -0700308 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1);
309 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800310#endif
Romain Guyfb5e23c2010-07-09 13:52:56 -0700311}
312
313}; // namespace uirenderer
314}; // namespace android