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 "Patch.h" 24#include "Caches.h" 25#include "Properties.h" 26 27namespace android { 28namespace uirenderer { 29 30/////////////////////////////////////////////////////////////////////////////// 31// Constructors/destructor 32/////////////////////////////////////////////////////////////////////////////// 33 34Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads): 35 mXCount(xCount), mYCount(yCount), mEmptyQuads(emptyQuads) { 36 // Initialized with the maximum number of vertices we will need 37 // 2 triangles per patch, 3 vertices per triangle 38 uint32_t maxVertices = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3; 39 mVertices = new TextureVertex[maxVertices]; 40 mUploaded = false; 41 42 verticesCount = 0; 43 hasEmptyQuads = emptyQuads > 0; 44 45 mColorKey = 0; 46 mXDivs = new int32_t[mXCount]; 47 mYDivs = new int32_t[mYCount]; 48 49 PATCH_LOGD(" patch: xCount = %d, yCount = %d, emptyQuads = %d, max vertices = %d", 50 xCount, yCount, emptyQuads, maxVertices); 51 52 glGenBuffers(1, &meshBuffer); 53} 54 55Patch::~Patch() { 56 delete[] mVertices; 57 delete[] mXDivs; 58 delete[] mYDivs; 59 glDeleteBuffers(1, &meshBuffer); 60} 61 62/////////////////////////////////////////////////////////////////////////////// 63// Patch management 64/////////////////////////////////////////////////////////////////////////////// 65 66void Patch::copy(const int32_t* xDivs, const int32_t* yDivs) { 67 memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t)); 68 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t)); 69} 70 71void Patch::copy(const int32_t* yDivs) { 72 memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t)); 73} 74 75void Patch::updateColorKey(const uint32_t colorKey) { 76 mColorKey = colorKey; 77} 78 79bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs, const uint32_t colorKey) { 80 if (mColorKey != colorKey) { 81 updateColorKey(colorKey); 82 copy(xDivs, yDivs); 83 return false; 84 } 85 86 for (uint32_t i = 0; i < mXCount; i++) { 87 if (mXDivs[i] != xDivs[i]) { 88 // The Y divs may or may not match, copy everything 89 copy(xDivs, yDivs); 90 return false; 91 } 92 } 93 94 for (uint32_t i = 0; i < mYCount; i++) { 95 if (mYDivs[i] != yDivs[i]) { 96 // We know all the X divs match, copy only Y divs 97 copy(yDivs); 98 return false; 99 } 100 } 101 102 return true; 103} 104 105/////////////////////////////////////////////////////////////////////////////// 106// Vertices management 107/////////////////////////////////////////////////////////////////////////////// 108 109void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight, 110 float left, float top, float right, float bottom) { 111 if (hasEmptyQuads) quads.clear(); 112 113 // Reset the vertices count here, we will count exactly how many 114 // vertices we actually need when generating the quads 115 verticesCount = 0; 116 117 const uint32_t xStretchCount = (mXCount + 1) >> 1; 118 const uint32_t yStretchCount = (mYCount + 1) >> 1; 119 120 float stretchX = 0.0f; 121 float stretchY = 0.0f; 122 123 float rescaleX = 1.0f; 124 float rescaleY = 1.0f; 125 126 const float meshWidth = right - left; 127 128 if (xStretchCount > 0) { 129 uint32_t stretchSize = 0; 130 for (uint32_t i = 1; i < mXCount; i += 2) { 131 stretchSize += mXDivs[i] - mXDivs[i - 1]; 132 } 133 const float xStretchTex = stretchSize; 134 const float fixed = bitmapWidth - stretchSize; 135 const float xStretch = fmaxf(right - left - fixed, 0.0f); 136 stretchX = xStretch / xStretchTex; 137 rescaleX = fixed == 0.0f ? 0.0f : fminf(fmaxf(right - left, 0.0f) / fixed, 1.0f); 138 } 139 140 if (yStretchCount > 0) { 141 uint32_t stretchSize = 0; 142 for (uint32_t i = 1; i < mYCount; i += 2) { 143 stretchSize += mYDivs[i] - mYDivs[i - 1]; 144 } 145 const float yStretchTex = stretchSize; 146 const float fixed = bitmapHeight - stretchSize; 147 const float yStretch = fmaxf(bottom - top - fixed, 0.0f); 148 stretchY = yStretch / yStretchTex; 149 rescaleY = fixed == 0.0f ? 0.0f : fminf(fmaxf(bottom - top, 0.0f) / fixed, 1.0f); 150 } 151 152 TextureVertex* vertex = mVertices; 153 uint32_t quadCount = 0; 154 155 float previousStepY = 0.0f; 156 157 float y1 = 0.0f; 158 float y2 = 0.0f; 159 float v1 = 0.0f; 160 161 for (uint32_t i = 0; i < mYCount; i++) { 162 float stepY = mYDivs[i]; 163 const float segment = stepY - previousStepY; 164 165 if (i & 1) { 166 y2 = y1 + floorf(segment * stretchY + 0.5f); 167 } else { 168 y2 = y1 + segment * rescaleY; 169 } 170 171 float vOffset = y1 == y2 ? 0.0f : 0.5 - (0.5 * segment / (y2 - y1)); 172 float v2 = fmax(0.0f, stepY - vOffset) / bitmapHeight; 173 v1 += vOffset / bitmapHeight; 174 175 if (stepY > 0.0f) { 176#if DEBUG_EXPLODE_PATCHES 177 y1 += i * EXPLODE_GAP; 178 y2 += i * EXPLODE_GAP; 179#endif 180 generateRow(vertex, y1, y2, v1, v2, stretchX, rescaleX, right - left, 181 bitmapWidth, quadCount); 182#if DEBUG_EXPLODE_PATCHES 183 y2 -= i * EXPLODE_GAP; 184#endif 185 } 186 187 y1 = y2; 188 v1 = stepY / bitmapHeight; 189 190 previousStepY = stepY; 191 } 192 193 if (previousStepY != bitmapHeight) { 194 y2 = bottom - top; 195#if DEBUG_EXPLODE_PATCHES 196 y1 += mYCount * EXPLODE_GAP; 197 y2 += mYCount * EXPLODE_GAP; 198#endif 199 generateRow(vertex, y1, y2, v1, 1.0f, stretchX, rescaleX, right - left, 200 bitmapWidth, quadCount); 201 } 202 203 if (verticesCount > 0) { 204 Caches& caches = Caches::getInstance(); 205 caches.bindMeshBuffer(meshBuffer); 206 if (!mUploaded) { 207 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount, 208 mVertices, GL_DYNAMIC_DRAW); 209 mUploaded = true; 210 } else { 211 glBufferSubData(GL_ARRAY_BUFFER, 0, 212 sizeof(TextureVertex) * verticesCount, mVertices); 213 } 214 caches.resetVertexPointers(); 215 } 216 217 PATCH_LOGD(" patch: new vertices count = %d", verticesCount); 218} 219 220void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2, 221 float stretchX, float rescaleX, float width, float bitmapWidth, uint32_t& quadCount) { 222 float previousStepX = 0.0f; 223 224 float x1 = 0.0f; 225 float x2 = 0.0f; 226 float u1 = 0.0f; 227 228 // Generate the row quad by quad 229 for (uint32_t i = 0; i < mXCount; i++) { 230 float stepX = mXDivs[i]; 231 const float segment = stepX - previousStepX; 232 233 if (i & 1) { 234 x2 = x1 + floorf(segment * stretchX + 0.5f); 235 } else { 236 x2 = x1 + segment * rescaleX; 237 } 238 239 float uOffset = x1 == x2 ? 0.0f : 0.5 - (0.5 * segment / (x2 - x1)); 240 float u2 = fmax(0.0f, stepX - uOffset) / bitmapWidth; 241 u1 += uOffset / bitmapWidth; 242 243 if (stepX > 0.0f) { 244#if DEBUG_EXPLODE_PATCHES 245 x1 += i * EXPLODE_GAP; 246 x2 += i * EXPLODE_GAP; 247#endif 248 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount); 249#if DEBUG_EXPLODE_PATCHES 250 x2 -= i * EXPLODE_GAP; 251#endif 252 } 253 254 x1 = x2; 255 u1 = stepX / bitmapWidth; 256 257 previousStepX = stepX; 258 } 259 260 if (previousStepX != bitmapWidth) { 261 x2 = width; 262#if DEBUG_EXPLODE_PATCHES 263 x1 += mXCount * EXPLODE_GAP; 264 x2 += mXCount * EXPLODE_GAP; 265#endif 266 generateQuad(vertex, x1, y1, x2, y2, u1, v1, 1.0f, v2, quadCount); 267 } 268} 269 270void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, 271 float u1, float v1, float u2, float v2, uint32_t& quadCount) { 272 const uint32_t oldQuadCount = quadCount; 273 quadCount++; 274 275 if (x1 < 0.0f) x1 = 0.0f; 276 if (x2 < 0.0f) x2 = 0.0f; 277 if (y1 < 0.0f) y1 = 0.0f; 278 if (y2 < 0.0f) y2 = 0.0f; 279 280 // Skip degenerate and transparent (empty) quads 281 if (((mColorKey >> oldQuadCount) & 0x1) || x1 >= x2 || y1 >= y2) { 282#if DEBUG_PATCHES_EMPTY_VERTICES 283 PATCH_LOGD(" quad %d (empty)", oldQuadCount); 284 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1); 285 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2); 286#endif 287 return; 288 } 289 290 // Record all non empty quads 291 if (hasEmptyQuads) { 292 Rect bounds(x1, y1, x2, y2); 293 quads.add(bounds); 294 } 295 296 // Left triangle 297 TextureVertex::set(vertex++, x1, y1, u1, v1); 298 TextureVertex::set(vertex++, x2, y1, u2, v1); 299 TextureVertex::set(vertex++, x1, y2, u1, v2); 300 301 // Right triangle 302 TextureVertex::set(vertex++, x1, y2, u1, v2); 303 TextureVertex::set(vertex++, x2, y1, u2, v1); 304 TextureVertex::set(vertex++, x2, y2, u2, v2); 305 306 // A quad is made of 2 triangles, 6 vertices 307 verticesCount += 6; 308 309#if DEBUG_PATCHES_VERTICES 310 PATCH_LOGD(" quad %d", oldQuadCount); 311 PATCH_LOGD(" left, top = %.2f, %.2f\t\tu1, v1 = %.4f, %.4f", x1, y1, u1, v1); 312 PATCH_LOGD(" right, bottom = %.2f, %.2f\t\tu2, v2 = %.4f, %.4f", x2, y2, u2, v2); 313#endif 314} 315 316}; // namespace uirenderer 317}; // namespace android 318