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