Patch.cpp revision f296dca95f09be9832b5dcc79717986525d2b6cb
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 TextureVertex* tempVertices = new TextureVertex[maxVertices]; 83 TextureVertex* vertex = tempVertices; 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 if (verticesCount == maxVertices) { 163 vertices = tempVertices; 164 } else { 165 vertices = new TextureVertex[verticesCount]; 166 memcpy(vertices, tempVertices, verticesCount * sizeof(TextureVertex)); 167 delete[] tempVertices; 168 } 169 170 return vertices; 171} 172 173void Patch::generateRow(const int32_t* xDivs, uint32_t xCount, TextureVertex*& vertex, 174 float y1, float y2, float v1, float v2, float stretchX, float rescaleX, 175 float width, float bitmapWidth, uint32_t& quadCount) { 176 float previousStepX = 0.0f; 177 178 float x1 = 0.0f; 179 float x2 = 0.0f; 180 float u1 = 0.0f; 181 182 // Generate the row quad by quad 183 for (uint32_t i = 0; i < xCount; i++) { 184 float stepX = xDivs[i]; 185 const float segment = stepX - previousStepX; 186 187 if (i & 1) { 188 x2 = x1 + floorf(segment * stretchX + 0.5f); 189 } else { 190 x2 = x1 + segment * rescaleX; 191 } 192 193 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1)); 194 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth; 195 u1 += uOffset / bitmapWidth; 196 197 if (stepX > 0.0f) { 198 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount); 199 } 200 201 x1 = x2; 202 u1 = stepX / bitmapWidth; 203 204 previousStepX = stepX; 205 } 206 207 if (previousStepX != bitmapWidth) { 208 x2 = width; 209 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount); 210 } 211} 212 213void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, 214 float u1, float v1, float u2, float v2, uint32_t& quadCount) { 215 const uint32_t oldQuadCount = quadCount; 216 quadCount++; 217 218 if (x1 < 0.0f) x1 = 0.0f; 219 if (x2 < 0.0f) x2 = 0.0f; 220 if (y1 < 0.0f) y1 = 0.0f; 221 if (y2 < 0.0f) y2 = 0.0f; 222 223 // Skip degenerate and transparent (empty) quads 224 if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) { 225#if DEBUG_PATCHES_EMPTY_VERTICES 226 PATCH_LOGD(" quad %d (empty)", oldQuadCount); 227 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1); 228 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2); 229#endif 230 return; 231 } 232 233 // Record all non empty quads 234 if (hasEmptyQuads) { 235 Rect bounds(x1, y1, x2, y2); 236 quads.add(bounds); 237 } 238 239 mUvMapper.map(u1, v1, u2, v2); 240 241 TextureVertex::set(vertex++, x1, y1, u1, v1); 242 TextureVertex::set(vertex++, x2, y1, u2, v1); 243 TextureVertex::set(vertex++, x1, y2, u1, v2); 244 TextureVertex::set(vertex++, x2, y2, u2, v2); 245 246 verticesCount += 4; 247 indexCount += 6; 248 249#if DEBUG_PATCHES_VERTICES 250 PATCH_LOGD(" quad %d", oldQuadCount); 251 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1); 252 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2); 253#endif 254} 255 256}; // namespace uirenderer 257}; // namespace android 258