blob: ce51e0413cb63bd0d617b91678101f28bbf70297 [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///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -070032// Vertices management
33///////////////////////////////////////////////////////////////////////////////
34
Romain Guy3b748a42013-04-17 18:54:38 -070035uint32_t Patch::getSize() const {
36 return verticesCount * sizeof(TextureVertex);
37}
Romain Guya5ef39a2010-12-03 16:48:20 -080038
Romain Guy3b748a42013-04-17 18:54:38 -070039TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
Romain Guy03c00b52013-06-20 18:30:28 -070040 float width, float height, const Res_png_9patch* patch) {
Romain Guy3b748a42013-04-17 18:54:38 -070041 UvMapper mapper;
Romain Guy03c00b52013-06-20 18:30:28 -070042 return createMesh(bitmapWidth, bitmapHeight, width, height, mapper, patch);
Romain Guy3b748a42013-04-17 18:54:38 -070043}
Romain Guy5b3b3522010-10-27 18:57:51 -070044
Romain Guy3b748a42013-04-17 18:54:38 -070045TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight,
Romain Guy03c00b52013-06-20 18:30:28 -070046 float width, float height, const UvMapper& mapper, const Res_png_9patch* patch) {
Chris Craik51d6a3d2014-12-22 17:16:56 -080047 if (vertices) return vertices.get();
Romain Guy3b748a42013-04-17 18:54:38 -070048
Romain Guy3b748a42013-04-17 18:54:38 -070049 int8_t emptyQuads = 0;
Narayan Kamath6381dd42014-03-03 17:12:03 +000050 mColors = patch->getColors();
Romain Guy3b748a42013-04-17 18:54:38 -070051
Romain Guy6cad7572013-07-24 11:49:33 -070052 const int8_t numColors = patch->numColors;
Romain Guy3b748a42013-04-17 18:54:38 -070053 if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
54 for (int8_t i = 0; i < numColors; i++) {
Romain Guy6cad7572013-07-24 11:49:33 -070055 if (mColors[i] == 0x0) {
Romain Guy3b748a42013-04-17 18:54:38 -070056 emptyQuads++;
Romain Guy3b748a42013-04-17 18:54:38 -070057 }
58 }
59 }
60
61 hasEmptyQuads = emptyQuads > 0;
62
63 uint32_t xCount = patch->numXDivs;
64 uint32_t yCount = patch->numYDivs;
65
66 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
Chris Craikd41c4d82015-01-05 15:51:13 -080067 if (maxVertices == 0) return nullptr;
Romain Guy3b748a42013-04-17 18:54:38 -070068
Chris Craik51d6a3d2014-12-22 17:16:56 -080069 vertices.reset(new TextureVertex[maxVertices]);
70 TextureVertex* vertex = vertices.get();
Romain Guy3b748a42013-04-17 18:54:38 -070071
Narayan Kamath6381dd42014-03-03 17:12:03 +000072 const int32_t* xDivs = patch->getXDivs();
73 const int32_t* yDivs = patch->getYDivs();
Romain Guy3b748a42013-04-17 18:54:38 -070074
75 const uint32_t xStretchCount = (xCount + 1) >> 1;
76 const uint32_t yStretchCount = (yCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -070077
Romain Guy6820ac82010-09-15 18:11:50 -070078 float stretchX = 0.0f;
Romain Guy41d35ae2012-10-10 16:06:04 -070079 float stretchY = 0.0f;
80
81 float rescaleX = 1.0f;
82 float rescaleY = 1.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070083
Romain Guyfb5e23c2010-07-09 13:52:56 -070084 if (xStretchCount > 0) {
85 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070086 for (uint32_t i = 1; i < xCount; i += 2) {
87 stretchSize += xDivs[i] - xDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -070088 }
Romain Guy6820ac82010-09-15 18:11:50 -070089 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070090 const float fixed = bitmapWidth - stretchSize;
Romain Guy03c00b52013-06-20 18:30:28 -070091 const float xStretch = fmaxf(width - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -070092 stretchX = xStretch / xStretchTex;
Romain Guy03c00b52013-06-20 18:30:28 -070093 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(width, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -070094 }
95
96 if (yStretchCount > 0) {
97 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070098 for (uint32_t i = 1; i < yCount; i += 2) {
99 stretchSize += yDivs[i] - yDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -0700100 }
Romain Guy6820ac82010-09-15 18:11:50 -0700101 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700102 const float fixed = bitmapHeight - stretchSize;
Romain Guy03c00b52013-06-20 18:30:28 -0700103 const float yStretch = fmaxf(height - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -0700104 stretchY = yStretch / yStretchTex;
Romain Guy03c00b52013-06-20 18:30:28 -0700105 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(height, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700106 }
107
Romain Guy4bb94202010-10-12 15:59:26 -0700108 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700109
Romain Guy6820ac82010-09-15 18:11:50 -0700110 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700111
Romain Guy6820ac82010-09-15 18:11:50 -0700112 float y1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700113 float y2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700114 float v1 = 0.0f;
115
Romain Guy3b748a42013-04-17 18:54:38 -0700116 mUvMapper = mapper;
117
118 for (uint32_t i = 0; i < yCount; i++) {
119 float stepY = yDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700120 const float segment = stepY - previousStepY;
Romain Guy6820ac82010-09-15 18:11:50 -0700121
Romain Guy6820ac82010-09-15 18:11:50 -0700122 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800123 y2 = y1 + floorf(segment * stretchY + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700124 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700125 y2 = y1 + segment * rescaleY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700126 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700127
128 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
129 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight;
130 v1 += vOffset / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700131
Romain Guyeb6a4a12011-01-18 14:02:16 -0800132 if (stepY > 0.0f) {
Romain Guy3b748a42013-04-17 18:54:38 -0700133 generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX,
Romain Guy03c00b52013-06-20 18:30:28 -0700134 width, bitmapWidth, quadCount);
Romain Guyeb6a4a12011-01-18 14:02:16 -0800135 }
Romain Guy6820ac82010-09-15 18:11:50 -0700136
137 y1 = y2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700138 v1 = stepY / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700139
140 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700141 }
142
Romain Guyfdbec3e2011-01-19 10:37:35 -0800143 if (previousStepY != bitmapHeight) {
Romain Guy03c00b52013-06-20 18:30:28 -0700144 y2 = height;
Romain Guy3b748a42013-04-17 18:54:38 -0700145 generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX,
Romain Guy03c00b52013-06-20 18:30:28 -0700146 width, bitmapWidth, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800147 }
Romain Guy03750a02010-10-18 14:06:08 -0700148
Chris Craik51d6a3d2014-12-22 17:16:56 -0800149 if (verticesCount != maxVertices) {
150 std::unique_ptr<TextureVertex[]> reducedVertices(new TextureVertex[verticesCount]);
151 memcpy(reducedVertices.get(), vertices.get(), verticesCount * sizeof(TextureVertex));
152 vertices = std::move(reducedVertices);
Romain Guyf296dca2013-06-24 14:33:37 -0700153 }
154
Chris Craik51d6a3d2014-12-22 17:16:56 -0800155 return vertices.get();
Romain Guyfb5e23c2010-07-09 13:52:56 -0700156}
157
Romain Guy3b748a42013-04-17 18:54:38 -0700158void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex,
159 float y1, float y2, float v1, float v2, float stretchX, float rescaleX,
160 float width, float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700161 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700162
Romain Guy6820ac82010-09-15 18:11:50 -0700163 float x1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700164 float x2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700165 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700166
Romain Guy6820ac82010-09-15 18:11:50 -0700167 // Generate the row quad by quad
Romain Guy3b748a42013-04-17 18:54:38 -0700168 for (uint32_t i = 0; i < xCount; i++) {
169 float stepX = xDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700170 const float segment = stepX - previousStepX;
Romain Guy6820ac82010-09-15 18:11:50 -0700171
Romain Guy6820ac82010-09-15 18:11:50 -0700172 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800173 x2 = x1 + floorf(segment * stretchX + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700174 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700175 x2 = x1 + segment * rescaleX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700176 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700177
178 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
179 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth;
180 u1 += uOffset / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700181
Romain Guyeb6a4a12011-01-18 14:02:16 -0800182 if (stepX > 0.0f) {
183 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
184 }
Romain Guy6820ac82010-09-15 18:11:50 -0700185
186 x1 = x2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700187 u1 = stepX / bitmapWidth;
Romain Guy6820ac82010-09-15 18:11:50 -0700188
189 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700190 }
191
Romain Guyfdbec3e2011-01-19 10:37:35 -0800192 if (previousStepX != bitmapWidth) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700193 x2 = width;
Romain Guyf504a2f2011-05-26 16:40:55 -0700194 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800195 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700196}
197
Romain Guy7444da52011-01-17 10:53:31 -0800198void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guyeb6a4a12011-01-18 14:02:16 -0800199 float u1, float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800200 const uint32_t oldQuadCount = quadCount;
Romain Guyeb6a4a12011-01-18 14:02:16 -0800201 quadCount++;
Romain Guybd41a112010-12-02 17:16:26 -0800202
Romain Guy70561df2012-09-10 17:40:18 -0700203 if (x1 < 0.0f) x1 = 0.0f;
204 if (x2 < 0.0f) x2 = 0.0f;
205 if (y1 < 0.0f) y1 = 0.0f;
206 if (y2 < 0.0f) y2 = 0.0f;
207
Romain Guya5ef39a2010-12-03 16:48:20 -0800208 // Skip degenerate and transparent (empty) quads
Romain Guy6cad7572013-07-24 11:49:33 -0700209 if ((mColors[oldQuadCount] == 0) || x1 >= x2 || y1 >= y2) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800210#if DEBUG_PATCHES_EMPTY_VERTICES
211 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700212 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
213 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guyfb13abd2011-01-16 15:16:38 -0800214#endif
Romain Guy7444da52011-01-17 10:53:31 -0800215 return;
Romain Guy4bb94202010-10-12 15:59:26 -0700216 }
217
Romain Guya5ef39a2010-12-03 16:48:20 -0800218 // Record all non empty quads
Romain Guy5b3b3522010-10-27 18:57:51 -0700219 if (hasEmptyQuads) {
220 Rect bounds(x1, y1, x2, y2);
221 quads.add(bounds);
222 }
223
Romain Guy3b748a42013-04-17 18:54:38 -0700224 mUvMapper.map(u1, v1, u2, v2);
225
Romain Guy6820ac82010-09-15 18:11:50 -0700226 TextureVertex::set(vertex++, x1, y1, u1, v1);
227 TextureVertex::set(vertex++, x2, y1, u2, v1);
228 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guy6820ac82010-09-15 18:11:50 -0700229 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800230
Romain Guy3b748a42013-04-17 18:54:38 -0700231 verticesCount += 4;
232 indexCount += 6;
Romain Guya5ef39a2010-12-03 16:48:20 -0800233
234#if DEBUG_PATCHES_VERTICES
235 PATCH_LOGD(" quad %d", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700236 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
237 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800238#endif
Romain Guyfb5e23c2010-07-09 13:52:56 -0700239}
240
241}; // namespace uirenderer
242}; // namespace android