blob: c243dad01f041b2c0e3acf50fa0086a5b03c8175 [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
Chris Craik9db58c02015-08-19 15:19:18 -070017#include "Patch.h"
Romain Guy4bb94202010-10-12 15:59:26 -070018
Romain Guy03750a02010-10-18 14:06:08 -070019#include "Caches.h"
Romain Guya5ef39a2010-12-03 16:48:20 -080020#include "Properties.h"
Romain Guy3b748a42013-04-17 18:54:38 -070021#include "UvMapper.h"
Chris Craik8820fd12015-03-03 14:20:47 -080022#include "utils/MathUtils.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070023
Chris Craik9db58c02015-08-19 15:19:18 -070024#include <utils/Log.h>
John Reck1bcacfd2017-11-03 10:12:19 -070025#include <algorithm>
Chris Craik9db58c02015-08-19 15:19:18 -070026
Romain Guyfb5e23c2010-07-09 13:52:56 -070027namespace android {
28namespace uirenderer {
29
30///////////////////////////////////////////////////////////////////////////////
Romain Guyfb5e23c2010-07-09 13:52:56 -070031// Vertices management
32///////////////////////////////////////////////////////////////////////////////
33
Romain Guy3b748a42013-04-17 18:54:38 -070034uint32_t Patch::getSize() const {
35 return verticesCount * sizeof(TextureVertex);
36}
Romain Guya5ef39a2010-12-03 16:48:20 -080037
John Reck1bcacfd2017-11-03 10:12:19 -070038Patch::Patch(const float bitmapWidth, const float bitmapHeight, float width, float height,
39 const UvMapper& mapper, const Res_png_9patch* patch)
Chris Craik8820fd12015-03-03 14:20:47 -080040 : mColors(patch->getColors()) {
Romain Guy3b748a42013-04-17 18:54:38 -070041 int8_t emptyQuads = 0;
Romain Guy6cad7572013-07-24 11:49:33 -070042 const int8_t numColors = patch->numColors;
Romain Guy3b748a42013-04-17 18:54:38 -070043 if (uint8_t(numColors) < sizeof(uint32_t) * 4) {
44 for (int8_t i = 0; i < numColors; i++) {
Romain Guy6cad7572013-07-24 11:49:33 -070045 if (mColors[i] == 0x0) {
Romain Guy3b748a42013-04-17 18:54:38 -070046 emptyQuads++;
Romain Guy3b748a42013-04-17 18:54:38 -070047 }
48 }
49 }
50
51 hasEmptyQuads = emptyQuads > 0;
52
53 uint32_t xCount = patch->numXDivs;
54 uint32_t yCount = patch->numYDivs;
55
56 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4;
Chris Craik8820fd12015-03-03 14:20:47 -080057 if (maxVertices == 0) return;
Romain Guy3b748a42013-04-17 18:54:38 -070058
Chris Craik51d6a3d2014-12-22 17:16:56 -080059 vertices.reset(new TextureVertex[maxVertices]);
60 TextureVertex* vertex = vertices.get();
Romain Guy3b748a42013-04-17 18:54:38 -070061
Narayan Kamath6381dd42014-03-03 17:12:03 +000062 const int32_t* xDivs = patch->getXDivs();
63 const int32_t* yDivs = patch->getYDivs();
Romain Guy3b748a42013-04-17 18:54:38 -070064
65 const uint32_t xStretchCount = (xCount + 1) >> 1;
66 const uint32_t yStretchCount = (yCount + 1) >> 1;
Romain Guyfb5e23c2010-07-09 13:52:56 -070067
Romain Guy6820ac82010-09-15 18:11:50 -070068 float stretchX = 0.0f;
Romain Guy41d35ae2012-10-10 16:06:04 -070069 float stretchY = 0.0f;
70
71 float rescaleX = 1.0f;
72 float rescaleY = 1.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070073
Romain Guyfb5e23c2010-07-09 13:52:56 -070074 if (xStretchCount > 0) {
75 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070076 for (uint32_t i = 1; i < xCount; i += 2) {
77 stretchSize += xDivs[i] - xDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -070078 }
Romain Guy6820ac82010-09-15 18:11:50 -070079 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070080 const float fixed = bitmapWidth - stretchSize;
Chris Craikdf72b632015-06-30 17:56:13 -070081 const float xStretch = std::max(width - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -070082 stretchX = xStretch / xStretchTex;
Chris Craikdf72b632015-06-30 17:56:13 -070083 rescaleX = fixed == 0.0f ? 0.0f : std::min(std::max(width, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -070084 }
85
86 if (yStretchCount > 0) {
87 uint32_t stretchSize = 0;
Romain Guy3b748a42013-04-17 18:54:38 -070088 for (uint32_t i = 1; i < yCount; i += 2) {
89 stretchSize += yDivs[i] - yDivs[i - 1];
Romain Guyfb5e23c2010-07-09 13:52:56 -070090 }
Romain Guy6820ac82010-09-15 18:11:50 -070091 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070092 const float fixed = bitmapHeight - stretchSize;
Chris Craikdf72b632015-06-30 17:56:13 -070093 const float yStretch = std::max(height - fixed, 0.0f);
Romain Guy6820ac82010-09-15 18:11:50 -070094 stretchY = yStretch / yStretchTex;
Chris Craikdf72b632015-06-30 17:56:13 -070095 rescaleY = fixed == 0.0f ? 0.0f : std::min(std::max(height, 0.0f) / fixed, 1.0f);
Romain Guyfb5e23c2010-07-09 13:52:56 -070096 }
97
Romain Guy4bb94202010-10-12 15:59:26 -070098 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070099
Romain Guy6820ac82010-09-15 18:11:50 -0700100 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700101
Romain Guy6820ac82010-09-15 18:11:50 -0700102 float y1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700103 float y2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700104 float v1 = 0.0f;
105
Romain Guy3b748a42013-04-17 18:54:38 -0700106 mUvMapper = mapper;
107
108 for (uint32_t i = 0; i < yCount; i++) {
109 float stepY = yDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700110 const float segment = stepY - previousStepY;
Romain Guy6820ac82010-09-15 18:11:50 -0700111
Romain Guy6820ac82010-09-15 18:11:50 -0700112 if (i & 1) {
Romain Guy8ab40792010-12-07 13:30:10 -0800113 y2 = y1 + floorf(segment * stretchY + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700114 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700115 y2 = y1 + segment * rescaleY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700116 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700117
118 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1));
Chris Craike6a15ee2015-07-07 18:42:17 -0700119 float v2 = std::max(0.0f, stepY - vOffset) / bitmapHeight;
Romain Guy5e7c4692011-10-20 20:31:50 -0700120 v1 += vOffset / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700121
Romain Guyeb6a4a12011-01-18 14:02:16 -0800122 if (stepY > 0.0f) {
John Reck1bcacfd2017-11-03 10:12:19 -0700123 generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX, width,
124 bitmapWidth, quadCount);
Romain Guyeb6a4a12011-01-18 14:02:16 -0800125 }
Romain Guy6820ac82010-09-15 18:11:50 -0700126
127 y1 = y2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700128 v1 = stepY / bitmapHeight;
Romain Guy6820ac82010-09-15 18:11:50 -0700129
130 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700131 }
132
Romain Guyfdbec3e2011-01-19 10:37:35 -0800133 if (previousStepY != bitmapHeight) {
Romain Guy03c00b52013-06-20 18:30:28 -0700134 y2 = height;
John Reck1bcacfd2017-11-03 10:12:19 -0700135 generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, width, bitmapWidth,
136 quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800137 }
Romain Guy03750a02010-10-18 14:06:08 -0700138
Chris Craik51d6a3d2014-12-22 17:16:56 -0800139 if (verticesCount != maxVertices) {
140 std::unique_ptr<TextureVertex[]> reducedVertices(new TextureVertex[verticesCount]);
141 memcpy(reducedVertices.get(), vertices.get(), verticesCount * sizeof(TextureVertex));
142 vertices = std::move(reducedVertices);
Romain Guyf296dca2013-06-24 14:33:37 -0700143 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700144}
145
John Reck1bcacfd2017-11-03 10:12:19 -0700146void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex, float y1,
147 float y2, float v1, float v2, float stretchX, float rescaleX, float width,
148 float bitmapWidth, uint32_t& quadCount) {
Romain Guy6820ac82010-09-15 18:11:50 -0700149 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700150
Romain Guy6820ac82010-09-15 18:11:50 -0700151 float x1 = 0.0f;
Romain Guyf504a2f2011-05-26 16:40:55 -0700152 float x2 = 0.0f;
Romain Guy6820ac82010-09-15 18:11:50 -0700153 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700154
Romain Guy6820ac82010-09-15 18:11:50 -0700155 // Generate the row quad by quad
Romain Guy3b748a42013-04-17 18:54:38 -0700156 for (uint32_t i = 0; i < xCount; i++) {
157 float stepX = xDivs[i];
Romain Guy5e7c4692011-10-20 20:31:50 -0700158 const float segment = stepX - previousStepX;
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 x2 = x1 + floorf(segment * stretchX + 0.5f);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700162 } else {
Romain Guy41d35ae2012-10-10 16:06:04 -0700163 x2 = x1 + segment * rescaleX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700164 }
Romain Guy5e7c4692011-10-20 20:31:50 -0700165
166 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1));
Chris Craike6a15ee2015-07-07 18:42:17 -0700167 float u2 = std::max(0.0f, stepX - uOffset) / bitmapWidth;
Romain Guy5e7c4692011-10-20 20:31:50 -0700168 u1 += uOffset / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700169
Romain Guyeb6a4a12011-01-18 14:02:16 -0800170 if (stepX > 0.0f) {
171 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount);
172 }
Romain Guy6820ac82010-09-15 18:11:50 -0700173
174 x1 = x2;
Romain Guy5e7c4692011-10-20 20:31:50 -0700175 u1 = stepX / bitmapWidth;
Romain Guy6820ac82010-09-15 18:11:50 -0700176
177 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700178 }
179
Romain Guyfdbec3e2011-01-19 10:37:35 -0800180 if (previousStepX != bitmapWidth) {
Romain Guyf504a2f2011-05-26 16:40:55 -0700181 x2 = width;
Romain Guyf504a2f2011-05-26 16:40:55 -0700182 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount);
Romain Guyfdbec3e2011-01-19 10:37:35 -0800183 }
Romain Guyfb5e23c2010-07-09 13:52:56 -0700184}
185
John Reck1bcacfd2017-11-03 10:12:19 -0700186void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, float u1,
187 float v1, float u2, float v2, uint32_t& quadCount) {
Romain Guya5ef39a2010-12-03 16:48:20 -0800188 const uint32_t oldQuadCount = quadCount;
Romain Guyeb6a4a12011-01-18 14:02:16 -0800189 quadCount++;
Romain Guybd41a112010-12-02 17:16:26 -0800190
Chris Craik9db58c02015-08-19 15:19:18 -0700191 x1 = std::max(x1, 0.0f);
192 x2 = std::max(x2, 0.0f);
193 y1 = std::max(y1, 0.0f);
194 y2 = std::max(y2, 0.0f);
Romain Guy70561df2012-09-10 17:40:18 -0700195
Romain Guya5ef39a2010-12-03 16:48:20 -0800196 // Skip degenerate and transparent (empty) quads
Romain Guy6cad7572013-07-24 11:49:33 -0700197 if ((mColors[oldQuadCount] == 0) || x1 >= x2 || y1 >= y2) {
Romain Guyfb13abd2011-01-16 15:16:38 -0800198#if DEBUG_PATCHES_EMPTY_VERTICES
199 PATCH_LOGD(" quad %d (empty)", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700200 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
201 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guyfb13abd2011-01-16 15:16:38 -0800202#endif
Romain Guy7444da52011-01-17 10:53:31 -0800203 return;
Romain Guy4bb94202010-10-12 15:59:26 -0700204 }
205
Romain Guya5ef39a2010-12-03 16:48:20 -0800206 // Record all non empty quads
Romain Guy5b3b3522010-10-27 18:57:51 -0700207 if (hasEmptyQuads) {
John Reck272a6852015-07-29 16:48:58 -0700208 quads.emplace_back(x1, y1, x2, y2);
Romain Guy5b3b3522010-10-27 18:57:51 -0700209 }
210
Romain Guy3b748a42013-04-17 18:54:38 -0700211 mUvMapper.map(u1, v1, u2, v2);
212
Romain Guy6820ac82010-09-15 18:11:50 -0700213 TextureVertex::set(vertex++, x1, y1, u1, v1);
214 TextureVertex::set(vertex++, x2, y1, u2, v1);
215 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guy6820ac82010-09-15 18:11:50 -0700216 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800217
Romain Guy3b748a42013-04-17 18:54:38 -0700218 verticesCount += 4;
219 indexCount += 6;
Romain Guya5ef39a2010-12-03 16:48:20 -0800220
221#if DEBUG_PATCHES_VERTICES
222 PATCH_LOGD(" quad %d", oldQuadCount);
Romain Guy6cad7572013-07-24 11:49:33 -0700223 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.8f, %.8f", x1, y1, u1, v1);
224 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2);
Romain Guya5ef39a2010-12-03 16:48:20 -0800225#endif
Romain Guyfb5e23c2010-07-09 13:52:56 -0700226}
227
John Reck1bcacfd2017-11-03 10:12:19 -0700228}; // namespace uirenderer
229}; // namespace android