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 | |
| 29 | /////////////////////////////////////////////////////////////////////////////// |
| 30 | // Constructors/destructor |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 33 | Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads): |
| 34 | mXCount(xCount), mYCount(yCount) { |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 35 | // 2 triangles per patch, 3 vertices per triangle |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 36 | verticesCount = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3; |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 37 | mVertices = new TextureVertex[verticesCount]; |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 38 | hasEmptyQuads = emptyQuads > 0; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 39 | mUploaded = false; |
| 40 | |
| 41 | mColorKey = 0; |
| 42 | mXDivs = new int32_t[mXCount]; |
| 43 | mYDivs = new int32_t[mYCount]; |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 44 | |
| 45 | glGenBuffers(1, &meshBuffer); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Patch::~Patch() { |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 49 | delete[] mVertices; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 50 | delete[] mXDivs; |
| 51 | delete[] mYDivs; |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 52 | glDeleteBuffers(1, &meshBuffer); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 56 | // Patch management |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
| 59 | void Patch::copy(const int32_t* xDivs, const int32_t* yDivs) { |
| 60 | memcpy(mXDivs, xDivs, mXCount * sizeof(int32_t)); |
| 61 | memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t)); |
| 62 | } |
| 63 | |
| 64 | void Patch::copy(const int32_t* yDivs) { |
| 65 | memcpy(mYDivs, yDivs, mYCount * sizeof(int32_t)); |
| 66 | } |
| 67 | |
| 68 | void Patch::updateColorKey(const uint32_t colorKey) { |
| 69 | mColorKey = colorKey; |
| 70 | } |
| 71 | |
| 72 | bool Patch::matches(const int32_t* xDivs, const int32_t* yDivs, const uint32_t colorKey) { |
| 73 | if (mColorKey != colorKey) { |
| 74 | updateColorKey(colorKey); |
| 75 | copy(xDivs, yDivs); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | for (uint32_t i = 0; i < mXCount; i++) { |
| 80 | if (mXDivs[i] != xDivs[i]) { |
| 81 | // The Y divs may or may not match, copy everything |
| 82 | copy(xDivs, yDivs); |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | for (uint32_t i = 0; i < mYCount; i++) { |
| 88 | if (mYDivs[i] != yDivs[i]) { |
| 89 | // We know all the X divs match, copy only Y divs |
| 90 | copy(yDivs); |
| 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | /////////////////////////////////////////////////////////////////////////////// |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 99 | // Vertices management |
| 100 | /////////////////////////////////////////////////////////////////////////////// |
| 101 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 102 | void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight, |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 103 | float left, float top, float right, float bottom) { |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 104 | if (hasEmptyQuads) quads.clear(); |
| 105 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 106 | const uint32_t xStretchCount = (mXCount + 1) >> 1; |
| 107 | const uint32_t yStretchCount = (mYCount + 1) >> 1; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 108 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 109 | float stretchX = 0.0f; |
| 110 | float stretchY = 0.0; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 111 | |
| 112 | const float meshWidth = right - left; |
| 113 | |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 114 | if (xStretchCount > 0) { |
| 115 | uint32_t stretchSize = 0; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 116 | for (uint32_t i = 1; i < mXCount; i += 2) { |
| 117 | stretchSize += mXDivs[i] - mXDivs[i - 1]; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 118 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 119 | const float xStretchTex = stretchSize; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 120 | const float fixed = bitmapWidth - stretchSize; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 121 | const float xStretch = right - left - fixed; |
| 122 | stretchX = xStretch / xStretchTex; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (yStretchCount > 0) { |
| 126 | uint32_t stretchSize = 0; |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 127 | for (uint32_t i = 1; i < mYCount; i += 2) { |
| 128 | stretchSize += mYDivs[i] - mYDivs[i - 1]; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 129 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 130 | const float yStretchTex = stretchSize; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 131 | const float fixed = bitmapHeight - stretchSize; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 132 | const float yStretch = bottom - top - fixed; |
| 133 | stretchY = yStretch / yStretchTex; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 136 | TextureVertex* vertex = mVertices; |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 137 | uint32_t quadCount = 0; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 138 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 139 | float previousStepY = 0.0f; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 140 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 141 | float y1 = 0.0f; |
| 142 | float v1 = 0.0f; |
| 143 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 144 | for (uint32_t i = 0; i < mYCount; i++) { |
| 145 | float stepY = mYDivs[i]; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 146 | |
| 147 | float y2 = 0.0f; |
| 148 | if (i & 1) { |
| 149 | const float segment = stepY - previousStepY; |
| 150 | y2 = y1 + segment * stretchY; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 151 | } else { |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 152 | y2 = y1 + stepY - previousStepY; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 153 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 154 | float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight; |
| 155 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 156 | generateRow(vertex, y1, y2, v1, v2, stretchX, right - left, bitmapWidth, quadCount); |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 157 | |
| 158 | y1 = y2; |
| 159 | v1 = (stepY + 0.5f) / bitmapHeight; |
| 160 | |
| 161 | previousStepY = stepY; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 164 | generateRow(vertex, y1, bottom - top, v1, 1.0f, stretchX, right - left, |
| 165 | bitmapWidth, quadCount); |
Romain Guy | 03750a0 | 2010-10-18 14:06:08 -0700 | [diff] [blame] | 166 | |
| 167 | Caches::getInstance().bindMeshBuffer(meshBuffer); |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 168 | if (!mUploaded) { |
| 169 | glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount, |
| 170 | mVertices, GL_DYNAMIC_DRAW); |
| 171 | mUploaded = true; |
| 172 | } else { |
| 173 | glBufferSubData(GL_ARRAY_BUFFER, 0, |
| 174 | sizeof(TextureVertex) * verticesCount, mVertices); |
| 175 | } |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 178 | void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2, |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 179 | float stretchX, float width, float bitmapWidth, uint32_t& quadCount) { |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 180 | float previousStepX = 0.0f; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 181 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 182 | float x1 = 0.0f; |
| 183 | float u1 = 0.0f; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 184 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 185 | // Generate the row quad by quad |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 186 | for (uint32_t i = 0; i < mXCount; i++) { |
| 187 | float stepX = mXDivs[i]; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 188 | |
| 189 | float x2 = 0.0f; |
| 190 | if (i & 1) { |
| 191 | const float segment = stepX - previousStepX; |
| 192 | x2 = x1 + segment * stretchX; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 193 | } else { |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 194 | x2 = x1 + stepX - previousStepX; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 195 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 196 | float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 197 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 198 | generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount); |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 199 | |
| 200 | x1 = x2; |
| 201 | u1 = (stepX + 0.5f) / bitmapWidth; |
| 202 | |
| 203 | previousStepX = stepX; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 206 | generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2, quadCount); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 209 | void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2, |
Romain Guy | 6f72beb | 2010-11-30 12:04:14 -0800 | [diff] [blame^] | 210 | float u1, float v1, float u2, float v2, uint32_t& quadCount) { |
| 211 | if (((mColorKey >> quadCount++) & 0x1) == 1) { |
Romain Guy | 4bb9420 | 2010-10-12 15:59:26 -0700 | [diff] [blame] | 212 | return; |
| 213 | } |
| 214 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 215 | if (hasEmptyQuads) { |
| 216 | Rect bounds(x1, y1, x2, y2); |
| 217 | quads.add(bounds); |
| 218 | } |
| 219 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 220 | // Left triangle |
| 221 | TextureVertex::set(vertex++, x1, y1, u1, v1); |
| 222 | TextureVertex::set(vertex++, x2, y1, u2, v1); |
| 223 | TextureVertex::set(vertex++, x1, y2, u1, v2); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 224 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 225 | // Right triangle |
| 226 | TextureVertex::set(vertex++, x1, y2, u1, v2); |
| 227 | TextureVertex::set(vertex++, x2, y1, u2, v1); |
| 228 | TextureVertex::set(vertex++, x2, y2, u2, v2); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | }; // namespace uirenderer |
| 232 | }; // namespace android |