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