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