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