Patch.cpp revision d41c4d8c732095ae99c955b6b82f7306633004b1
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(), verticesCount(0), indexCount(0), hasEmptyQuads(false) { 36} 37 38/////////////////////////////////////////////////////////////////////////////// 39// Vertices management 40/////////////////////////////////////////////////////////////////////////////// 41 42uint32_t Patch::getSize() const { 43 return verticesCount * sizeof(TextureVertex); 44} 45 46TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight, 47 float width, float height, const Res_png_9patch* patch) { 48 UvMapper mapper; 49 return createMesh(bitmapWidth, bitmapHeight, width, height, mapper, patch); 50} 51 52TextureVertex* Patch::createMesh(const float bitmapWidth, const float bitmapHeight, 53 float width, float height, const UvMapper& mapper, const Res_png_9patch* patch) { 54 if (vertices) return vertices.get(); 55 56 int8_t emptyQuads = 0; 57 mColors = patch->getColors(); 58 59 const int8_t numColors = patch->numColors; 60 if (uint8_t(numColors) < sizeof(uint32_t) * 4) { 61 for (int8_t i = 0; i < numColors; i++) { 62 if (mColors[i] == 0x0) { 63 emptyQuads++; 64 } 65 } 66 } 67 68 hasEmptyQuads = emptyQuads > 0; 69 70 uint32_t xCount = patch->numXDivs; 71 uint32_t yCount = patch->numYDivs; 72 73 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 4; 74 if (maxVertices == 0) return nullptr; 75 76 vertices.reset(new TextureVertex[maxVertices]); 77 TextureVertex* vertex = vertices.get(); 78 79 const int32_t* xDivs = patch->getXDivs(); 80 const int32_t* yDivs = patch->getYDivs(); 81 82 const uint32_t xStretchCount = (xCount + 1) >> 1; 83 const uint32_t yStretchCount = (yCount + 1) >> 1; 84 85 float stretchX = 0.0f; 86 float stretchY = 0.0f; 87 88 float rescaleX = 1.0f; 89 float rescaleY = 1.0f; 90 91 if (xStretchCount > 0) { 92 uint32_t stretchSize = 0; 93 for (uint32_t i = 1; i < xCount; i += 2) { 94 stretchSize += xDivs[i] - xDivs[i - 1]; 95 } 96 const float xStretchTex = stretchSize; 97 const float fixed = bitmapWidth - stretchSize; 98 const float xStretch = fmaxf(width - fixed, 0.0f); 99 stretchX = xStretch / xStretchTex; 100 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(width, 0.0f) / fixed, 1.0f); 101 } 102 103 if (yStretchCount > 0) { 104 uint32_t stretchSize = 0; 105 for (uint32_t i = 1; i < yCount; i += 2) { 106 stretchSize += yDivs[i] - yDivs[i - 1]; 107 } 108 const float yStretchTex = stretchSize; 109 const float fixed = bitmapHeight - stretchSize; 110 const float yStretch = fmaxf(height - fixed, 0.0f); 111 stretchY = yStretch / yStretchTex; 112 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(height, 0.0f) / fixed, 1.0f); 113 } 114 115 uint32_t quadCount = 0; 116 117 float previousStepY = 0.0f; 118 119 float y1 = 0.0f; 120 float y2 = 0.0f; 121 float v1 = 0.0f; 122 123 mUvMapper = mapper; 124 125 for (uint32_t i = 0; i < yCount; i++) { 126 float stepY = yDivs[i]; 127 const float segment = stepY - previousStepY; 128 129 if (i & 1) { 130 y2 = y1 + floorf(segment * stretchY + 0.5f); 131 } else { 132 y2 = y1 + segment * rescaleY; 133 } 134 135 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1)); 136 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight; 137 v1 += vOffset / bitmapHeight; 138 139 if (stepY > 0.0f) { 140 generateRow(xDivs, xCount, vertex, y1, y2, v1, v2, stretchX, rescaleX, 141 width, bitmapWidth, quadCount); 142 } 143 144 y1 = y2; 145 v1 = stepY / bitmapHeight; 146 147 previousStepY = stepY; 148 } 149 150 if (previousStepY != bitmapHeight) { 151 y2 = height; 152 generateRow(xDivs, xCount, vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, 153 width, bitmapWidth, quadCount); 154 } 155 156 if (verticesCount != maxVertices) { 157 std::unique_ptr<TextureVertex[]> reducedVertices(new TextureVertex[verticesCount]); 158 memcpy(reducedVertices.get(), vertices.get(), verticesCount * sizeof(TextureVertex)); 159 vertices = std::move(reducedVertices); 160 } 161 162 return vertices.get(); 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 ((mColors[oldQuadCount] == 0) || 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 = %.8f, %.8f", x1, y1, u1, v1); 220 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", 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 = %.8f, %.8f", x1, y1, u1, v1); 244 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.8f, %.8f", x2, y2, u2, v2); 245#endif 246} 247 248}; // namespace uirenderer 249}; // namespace android 250