1 /*
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
19 #include <cmath>
20
21 #include <utils/Log.h>
22
23 #include "Caches.h"
24 #include "Patch.h"
25 #include "Properties.h"
26 #include "UvMapper.h"
27
28 namespace android {
29 namespace uirenderer {
30
31 ///////////////////////////////////////////////////////////////////////////////
32 // Constructors/destructor
33 ///////////////////////////////////////////////////////////////////////////////
34
Patch()35 Patch::Patch(): vertices(NULL), verticesCount(0), indexCount(0), hasEmptyQuads(false) {
36 }
37
~Patch()38 Patch::~Patch() {
39 }
40
41 ///////////////////////////////////////////////////////////////////////////////
42 // Vertices management
43 ///////////////////////////////////////////////////////////////////////////////
44
getSize() const45 uint32_t Patch::getSize() const {
46 return verticesCount * sizeof(TextureVertex);
47 }
48
createMesh(const float bitmapWidth,const float bitmapHeight,float width,float height,const Res_png_9patch * patch)49 TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
50 float width, float height, const Res_png_9patch* patch) {
51 UvMapper mapper;
52 return createMesh(bitmapWidth, bitmapHeight, width, height, mapper, patch);
53 }
54
createMesh(const float bitmapWidth,const float bitmapHeight,float width,float height,const UvMapper & mapper,const Res_png_9patch * patch)55 TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
56 float width, float height, const UvMapper& mapper, const Res_png_9patch* patch) {
57 if (vertices) return vertices;
58
59 int8_t emptyQuads = 0;
60 mColors = patch->colors;
61
62 const int8_t numColors = patch->numColors;
63 if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
64 for (int8_t i = 0; i < numColors; i++) {
65 if (mColors[i] == 0x0) {
66 emptyQuads++;
67 }
68 }
69 }
70
71 hasEmptyQuads = emptyQuads > 0;
72
73 uint32_t xCount = patch->numXDivs;
74 uint32_t yCount = patch->numYDivs;
75
76 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
77 if (maxVertices == 0) return NULL;
78
79 TextureVertex* tempVertices = new TextureVertex[maxVertices];
80 TextureVertex* vertex = tempVertices;
81
82 const int32_t* xDivs = patch->xDivs;
83 const int32_t* yDivs = patch->yDivs;
84
85 const uint32_t xStretchCount = (xCount + 1) >> 1;
86 const uint32_t yStretchCount = (yCount + 1) >> 1;
87
88 float stretchX = 0.0f;
89 float stretchY = 0.0f;
90
91 float rescaleX = 1.0f;
92 float rescaleY = 1.0f;
93
94 if (xStretchCount > 0) {
95 uint32_t stretchSize = 0;
96 for (uint32_t i = 1; i < xCount; i += 2) {
97 stretchSize += xDivs[i] - xDivs[i - 1];
98 }
99 const float xStretchTex = stretchSize;
100 const float fixed = bitmapWidth - stretchSize;
101 const float xStretch = fmaxf(width - fixed, 0.0f);
102 stretchX = xStretch / xStretchTex;
103 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(width, 0.0f) / fixed, 1.0f);
104 }
105
106 if (yStretchCount > 0) {
107 uint32_t stretchSize = 0;
108 for (uint32_t i = 1; i < yCount; i += 2) {
109 stretchSize += yDivs[i] - yDivs[i - 1];
110 }
111 const float yStretchTex = stretchSize;
112 const float fixed = bitmapHeight - stretchSize;
113 const float yStretch = fmaxf(height - fixed, 0.0f);
114 stretchY = yStretch / yStretchTex;
115 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(height, 0.0f) / fixed, 1.0f);
116 }
117
118 uint32_t quadCount = 0;
119
120 float previousStepY = 0.0f;
121
122 float y1 = 0.0f;
123 float y2 = 0.0f;
124 float v1 = 0.0f;
125
126 mUvMapper = mapper;
127
128 for (uint32_t i = 0; i < yCount; i++) {
129 float stepY = yDivs[i];
130 const float segment = stepY - previousStepY;
131
132 if (i & 1) {
133 y2 = y1 + floorf(segment * stretchY + 0.5f);
134 } else {
135 y2 = y1 + segment * rescaleY;
136 }
137
138 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
139 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
140 v1 += vOffset / bitmapHeight;
141
142 if (stepY > 0.0f) {
143 generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX,
144 width, bitmapWidth, quadCount);
145 }
146
147 y1 = y2;
148 v1 = stepY / bitmapHeight;
149
150 previousStepY = stepY;
151 }
152
153 if (previousStepY != bitmapHeight) {
154 y2 = height;
155 generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX,
156 width, bitmapWidth, quadCount);
157 }
158
159 if (verticesCount == maxVertices) {
160 vertices = tempVertices;
161 } else {
162 vertices = new TextureVertex[verticesCount];
163 memcpy(vertices, tempVertices, verticesCount * sizeof(TextureVertex));
164 delete[] tempVertices;
165 }
166
167 return vertices;
168 }
169
generateRow(const int32_t * xDivs,uint32_t xCount,TextureVertex * & vertex,float y1,float y2,float v1,float v2,float stretchX,float rescaleX,float width,float bitmapWidth,uint32_t & quadCount)170 void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex,
171 float y1, float y2, float v1, float v2, float stretchX, float rescaleX,
172 float width, float bitmapWidth, uint32_t& quadCount) {
173 float previousStepX = 0.0f;
174
175 float x1 = 0.0f;
176 float x2 = 0.0f;
177 float u1 = 0.0f;
178
179 // Generate the row quad by quad
180 for (uint32_t i = 0; i < xCount; i++) {
181 float stepX = xDivs[i];
182 const float segment = stepX - previousStepX;
183
184 if (i & 1) {
185 x2 = x1 + floorf(segment * stretchX + 0.5f);
186 } else {
187 x2 = x1 + segment * rescaleX;
188 }
189
190 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
191 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
192 u1 += uOffset / bitmapWidth;
193
194 if (stepX > 0.0f) {
195 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
196 }
197
198 x1 = x2;
199 u1 = stepX / bitmapWidth;
200
201 previousStepX = stepX;
202 }
203
204 if (previousStepX != bitmapWidth) {
205 x2 = width;
206 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
207 }
208 }
209
generateQuad(TextureVertex * & vertex,float x1,float y1,float x2,float y2,float u1,float v1,float u2,float v2,uint32_t & quadCount)210 void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
211 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
212 const uint32_t oldQuadCount = quadCount;
213 quadCount++;
214
215 if (x1 < 0.0f) x1 = 0.0f;
216 if (x2 < 0.0f) x2 = 0.0f;
217 if (y1 < 0.0f) y1 = 0.0f;
218 if (y2 < 0.0f) y2 = 0.0f;
219
220 // Skip degenerate and transparent (empty) quads
221 if ((mColors[oldQuadCount] == 0) || x1 >= x2 || y1 >= y2) {
222 #if DEBUG_PATCHES_EMPTY_VERTICES
223 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
224 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
225 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
226 #endif
227 return;
228 }
229
230 // Record all non empty quads
231 if (hasEmptyQuads) {
232 Rect bounds(x1, y1, x2, y2);
233 quads.add(bounds);
234 }
235
236 mUvMapper.map(u1, v1, u2, v2);
237
238 TextureVertex::set(vertex++, x1, y1, u1, v1);
239 TextureVertex::set(vertex++, x2, y1, u2, v1);
240 TextureVertex::set(vertex++, x1, y2, u1, v2);
241 TextureVertex::set(vertex++, x2, y2, u2, v2);
242
243 verticesCount += 4;
244 indexCount += 6;
245
246 #if DEBUG_PATCHES_VERTICES
247 PATCH_LOGD(" quad %d", oldQuadCount);
248 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
249 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
250 #endif
251 }
252
253 }; // namespace uirenderer
254 }; // namespace android
255