blob: 442e9ba616cf4114046f0aa337800d053992f35b [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 Guy03750a02010-10-18 14:06:08 -070023#include "Caches.h"
Romain Guy3b748a42013-04-17 18:54:38 -070024#include "Patch.h"
Romain Guya5ef39a2010-12-03 16:48:20 -080025#include "Properties.h"
Romain Guy3b748a42013-04-17 18:54:38 -070026#include "UvMapper.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070027
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Constructors/destructor
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guy03c00b52013-06-20 18:30:28 -070035Patch::Patch(): vertices(NULL), verticesCount(0), indexCount(0), hasEmptyQuads(false) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070036}
37
38Patch::~Patch() {
Jens Gulin6056e102014-02-04 17:38:02 +010039 delete[] vertices;
Romain Guy6f72beb2010-11-30 12:04:14 -080040}
41
42///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -070043// Vertices management
44///////////////////////////////////////////////////////////////////////////////
45
Romain Guy3b748a42013-04-17 18:54:38 -070046uint32_t Patch::getSize() const {
47 return verticesCount * sizeof(TextureVertex);
48}
Romain Guya5ef39a2010-12-03 16:48:20 -080049
Romain Guy3b748a42013-04-17 18:54:38 -070050TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
Romain Guy03c00b52013-06-20 18:30:28 -070051 float width, float height, const Res_png_9patch* patch) {
Romain Guy3b748a42013-04-17 18:54:38 -070052 UvMapper mapper;
Romain Guy03c00b52013-06-20 18:30:28 -070053 return createMesh(bitmapWidth, bitmapHeight, width, height, mapper, patch);
Romain Guy3b748a42013-04-17 18:54:38 -070054}
Romain Guy5b3b3522010-10-27 18:57:51 -070055
Romain Guy3b748a42013-04-17 18:54:38 -070056TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
Romain Guy03c00b52013-06-20 18:30:28 -070057 float width, float height, const UvMapper& mapper, const Res_png_9patch* patch) {
58 if (vertices) return vertices;
Romain Guy3b748a42013-04-17 18:54:38 -070059
Romain Guy3b748a42013-04-17 18:54:38 -070060 int8_t emptyQuads = 0;
Narayan Kamath6381dd42014-03-03 17:12:03 +000061 mColors = patch->getColors();
Romain Guy3b748a42013-04-17 18:54:38 -070062
Romain Guy6cad7572013-07-24 11:49:33 -070063 const int8_t numColors = patch->numColors;
Romain Guy3b748a42013-04-17 18:54:38 -070064 if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
65 for (int8_t i = 0; i < numColors; i++) {
Romain Guy6cad7572013-07-24 11:49:33 -070066 if (mColors[i] == 0x0) {
Romain Guy3b748a42013-04-17 18:54:38 -070067 emptyQuads++;
Romain Guy3b748a42013-04-17 18:54:38 -070068 }
69 }
70 }
71
72 hasEmptyQuads = emptyQuads > 0;
73
74 uint32_t xCount = patch->numXDivs;
75 uint32_t yCount = patch->numYDivs;
76
77 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
78 if (maxVertices == 0) return NULL;
79
Romain Guyf296dca2013-06-24 14:33:37 -070080 TextureVertex* tempVertices = new TextureVertex[maxVertices];
81 TextureVertex* vertex = tempVertices;
Romain Guy3b748a42013-04-17 18:54:38 -070082
Narayan Kamath6381dd42014-03-03 17:12:03 +000083 const int32_t* xDivs = patch->getXDivs();
84 const int32_t* yDivs = patch->getYDivs();
Romain Guy3b748a42013-04-17 18:54:38 -070085
86 const uint32_t xStretchCount = (xCount + 1) >> 1;
87 const uint32_t yStretchCount = (yCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -070088
Romain Guy6820ac82010-09-15 18:11:50 -070089 float stretchX = 0.0f;
Romain Guy41d35ae2012-10-10 16:06:04 -070090 float stretchY = 0.0f;
91
92 float rescaleX = 1.0f;
93 float rescaleY = 1.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070094
Romain Guyfb5e23c2010-07-09 13:52:56 -070095 if (xStretchCount > 0) {
96 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070097 for (uint32_t i = 1; i < xCount; i += 2) {
98 stretchSize += xDivs[i] - xDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -070099 }
Romain Guy6820ac82010-09-15 18:11:50 -0700100 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700101 const float fixed = bitmapWidth - stretchSize;
Romain Guy03c00b52013-06-20 18:30:28 -0700102 const float xStretch = fmaxf(width - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -0700103 stretchX = xStretch / xStretchTex;
Romain Guy03c00b52013-06-20 18:30:28 -0700104 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(width, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700105 }
106
107 if (yStretchCount > 0) {
108 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -0700109 for (uint32_t i = 1; i < yCount; i += 2) {
110 stretchSize += yDivs[i] - yDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700111 }
Romain Guy6820ac82010-09-15 18:11:50 -0700112 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700113 const float fixed = bitmapHeight - stretchSize;
Romain Guy03c00b52013-06-20 18:30:28 -0700114 const float yStretch = fmaxf(height - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -0700115 stretchY = yStretch / yStretchTex;
Romain Guy03c00b52013-06-20 18:30:28 -0700116 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(height, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700117 }
118
Romain Guy4bb94202010-10-12 15:59:26 -0700119 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700120
Romain Guy6820ac82010-09-15 18:11:50 -0700121 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700122
Romain Guy6820ac82010-09-15 18:11:50 -0700123 float y1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700124 float y2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700125 float v1 = 0.0f;
126
Romain Guy3b748a42013-04-17 18:54:38 -0700127 mUvMapper = mapper;
128
129 for (uint32_t i = 0; i < yCount; i++) {
130 float stepY = yDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700131 const float segment = stepY - previousStepY;
Romain Guy6820ac82010-09-15 18:11:50 -0700132
Romain Guy6820ac82010-09-15 18:11:50 -0700133 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800134 y2 = y1 + floorf(segment * stretchY + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700135 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700136 y2 = y1 + segment * rescaleY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700137 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700138
139 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
140 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
141 v1 += vOffset / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700142
Romain Guyeb6a4a12011-01-18 14:02:16 -0800143 if (stepY > 0.0f) {
Romain Guy3b748a42013-04-17 18:54:38 -0700144 generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX,
Romain Guy03c00b52013-06-20 18:30:28 -0700145 width, bitmapWidth, quadCount);
Romain Guyeb6a4a12011-01-18 14:02:16 -0800146 }
Romain Guy6820ac82010-09-15 18:11:50 -0700147
148 y1 = y2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700149 v1 = stepY / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700150
151 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700152 }
153
Romain Guyfdbec3e2011-01-19 10:37:35 -0800154 if (previousStepY != bitmapHeight) {
Romain Guy03c00b52013-06-20 18:30:28 -0700155 y2 = height;
Romain Guy3b748a42013-04-17 18:54:38 -0700156 generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX,
Romain Guy03c00b52013-06-20 18:30:28 -0700157 width, bitmapWidth, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800158 }
Romain Guy03750a02010-10-18 14:06:08 -0700159
Romain Guyf296dca2013-06-24 14:33:37 -0700160 if (verticesCount == maxVertices) {
161 vertices = tempVertices;
162 } else {
163 vertices = new TextureVertex[verticesCount];
164 memcpy(vertices, tempVertices, verticesCount * sizeof(TextureVertex));
165 delete[] tempVertices;
166 }
167
Romain Guy3b748a42013-04-17 18:54:38 -0700168 return vertices;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700169}
170
Romain Guy3b748a42013-04-17 18:54:38 -0700171void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex,
172 float y1, float y2, float v1, float v2, float stretchX, float rescaleX,
173 float width, float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700174 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700175
Romain Guy6820ac82010-09-15 18:11:50 -0700176 float x1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700177 float x2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700178 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700179
Romain Guy6820ac82010-09-15 18:11:50 -0700180 // Generate the row quad by quad
Romain Guy3b748a42013-04-17 18:54:38 -0700181 for (uint32_t i = 0; i < xCount; i++) {
182 float stepX = xDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700183 const float segment = stepX - previousStepX;
Romain Guy6820ac82010-09-15 18:11:50 -0700184
Romain Guy6820ac82010-09-15 18:11:50 -0700185 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800186 x2 = x1 + floorf(segment * stretchX + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700187 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700188 x2 = x1 + segment * rescaleX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700189 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700190
191 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
192 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
193 u1 += uOffset / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700194
Romain Guyeb6a4a12011-01-18 14:02:16 -0800195 if (stepX > 0.0f) {
196 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
197 }
Romain Guy6820ac82010-09-15 18:11:50 -0700198
199 x1 = x2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700200 u1 = stepX / bitmapWidth;
Romain Guy6820ac82010-09-15 18:11:50 -0700201
202 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700203 }
204
Romain Guyfdbec3e2011-01-19 10:37:35 -0800205 if (previousStepX != bitmapWidth) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700206 x2 = width;
Romain Guyf504a2f2011-05-26 16:40:55 -0700207 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800208 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700209}
210
Romain Guy7444da52011-01-17 10:53:31 -0800211void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800212 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800213 const uint32_t oldQuadCount = quadCount;
Romain Guyeb6a4a12011-01-18 14:02:16 -0800214 quadCount++;
Romain Guybd41a112010-12-02 17:16:26 -0800215
Romain Guy70561df2012-09-10 17:40:18 -0700216 if (x1 < 0.0f) x1 = 0.0f;
217 if (x2 < 0.0f) x2 = 0.0f;
218 if (y1 < 0.0f) y1 = 0.0f;
219 if (y2 < 0.0f) y2 = 0.0f;
220
Romain Guya5ef39a2010-12-03 16:48:20 -0800221 // Skip degenerate and transparent (empty) quads
Romain Guy6cad7572013-07-24 11:49:33 -0700222 if ((mColors[oldQuadCount] == 0) || x1 >= x2 || y1 >= y2) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800223#if DEBUG_PATCHES_EMPTY_VERTICES
224 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700225 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
226 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guyfb13abd2011-01-16 15:16:38 -0800227#endif
Romain Guy7444da52011-01-17 10:53:31 -0800228 return;
Romain Guy4bb94202010-10-12 15:59:26 -0700229 }
230
Romain Guya5ef39a2010-12-03 16:48:20 -0800231 // Record all non empty quads
Romain Guy5b3b3522010-10-27 18:57:51 -0700232 if (hasEmptyQuads) {
233 Rect bounds(x1, y1, x2, y2);
234 quads.add(bounds);
235 }
236
Romain Guy3b748a42013-04-17 18:54:38 -0700237 mUvMapper.map(u1, v1, u2, v2);
238
Romain Guy6820ac82010-09-15 18:11:50 -0700239 TextureVertex::set(vertex++, x1, y1, u1, v1);
240 TextureVertex::set(vertex++, x2, y1, u2, v1);
241 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guy6820ac82010-09-15 18:11:50 -0700242 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800243
Romain Guy3b748a42013-04-17 18:54:38 -0700244 verticesCount += 4;
245 indexCount += 6;
Romain Guya5ef39a2010-12-03 16:48:20 -0800246
247#if DEBUG_PATCHES_VERTICES
248 PATCH_LOGD(" quad %d", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700249 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
250 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800251#endif
Romain Guyfb5e23c2010-07-09 13:52:56 -0700252}
253
254}; // namespace uirenderer
255}; // namespace android