Patch.cpp revision 759ea80dca64ad652110a129e0d8bf93fea79f61
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 "Patch.h" 22 23namespace android { 24namespace uirenderer { 25 26/////////////////////////////////////////////////////////////////////////////// 27// Constructors/destructor 28/////////////////////////////////////////////////////////////////////////////// 29 30Patch::Patch(const uint32_t xCount, const uint32_t yCount) { 31 // 2 triangles per patch, 3 vertices per triangle 32 verticesCount = (xCount + 1) * (yCount + 1) * 2 * 3; 33 vertices = new TextureVertex[verticesCount]; 34} 35 36Patch::~Patch() { 37 delete vertices; 38} 39 40/////////////////////////////////////////////////////////////////////////////// 41// Vertices management 42/////////////////////////////////////////////////////////////////////////////// 43 44void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight, 45 float left, float top, float right, float bottom, 46 const int32_t* xDivs, const int32_t* yDivs, const uint32_t width, const uint32_t height) { 47 const uint32_t xStretchCount = (width + 1) >> 1; 48 const uint32_t yStretchCount = (height + 1) >> 1; 49 50 float stretchX = 0.0f; 51 float stretchY = 0.0; 52 53 const float meshWidth = right - left; 54 55 if (xStretchCount > 0) { 56 uint32_t stretchSize = 0; 57 for (uint32_t i = 1; i < width; i += 2) { 58 stretchSize += xDivs[i] - xDivs[i - 1]; 59 } 60 const float xStretchTex = stretchSize; 61 const float fixed = bitmapWidth - stretchSize; 62 const float xStretch = right - left - fixed; 63 stretchX = xStretch / xStretchTex; 64 } 65 66 if (yStretchCount > 0) { 67 uint32_t stretchSize = 0; 68 for (uint32_t i = 1; i < height; i += 2) { 69 stretchSize += yDivs[i] - yDivs[i - 1]; 70 } 71 const float yStretchTex = stretchSize; 72 const float fixed = bitmapHeight - stretchSize; 73 const float yStretch = bottom - top - fixed; 74 stretchY = yStretch / yStretchTex; 75 } 76 77 TextureVertex* vertex = vertices; 78 79 float previousStepY = 0.0f; 80 81 float y1 = 0.0f; 82 float v1 = 0.0f; 83 84 for (uint32_t i = 0; i < height; i++) { 85 float stepY = yDivs[i]; 86 87 float y2 = 0.0f; 88 if (i & 1) { 89 const float segment = stepY - previousStepY; 90 y2 = y1 + segment * stretchY; 91 } else { 92 y2 = y1 + stepY - previousStepY; 93 } 94 float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight; 95 96 generateRow(vertex, y1, y2, v1, v2, xDivs, width, stretchX, 97 right - left, bitmapWidth); 98 99 y1 = y2; 100 v1 = (stepY + 0.5f) / bitmapHeight; 101 102 previousStepY = stepY; 103 } 104 105 generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX, 106 right - left, bitmapWidth); 107} 108 109inline void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2, 110 const int32_t xDivs[], uint32_t xCount, float stretchX, float width, float bitmapWidth) { 111 float previousStepX = 0.0f; 112 113 float x1 = 0.0f; 114 float u1 = 0.0f; 115 116 // Generate the row quad by quad 117 for (uint32_t i = 0; i < xCount; i++) { 118 float stepX = xDivs[i]; 119 120 float x2 = 0.0f; 121 if (i & 1) { 122 const float segment = stepX - previousStepX; 123 x2 = x1 + segment * stretchX; 124 } else { 125 x2 = x1 + stepX - previousStepX; 126 } 127 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth; 128 129 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2); 130 131 x1 = x2; 132 u1 = (stepX + 0.5f) / bitmapWidth; 133 134 previousStepX = stepX; 135 } 136 137 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2); 138} 139 140inline void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, 141 float u1, float v1, float u2, float v2) { 142 // Left triangle 143 TextureVertex::set(vertex++, x1, y1, u1, v1); 144 TextureVertex::set(vertex++, x2, y1, u2, v1); 145 TextureVertex::set(vertex++, x1, y2, u1, v2); 146 147 // Right triangle 148 TextureVertex::set(vertex++, x1, y2, u1, v2); 149 TextureVertex::set(vertex++, x2, y1, u2, v1); 150 TextureVertex::set(vertex++, x2, y2, u2, v2); 151} 152 153}; // namespace uirenderer 154}; // namespace android 155