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