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 | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 21 | #include "Patch.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace uirenderer { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // Constructors/destructor |
| 28 | /////////////////////////////////////////////////////////////////////////////// |
| 29 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 30 | Patch::Patch(const uint32_t xCount, const uint32_t yCount) { |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 31 | // 2 triangles per patch, 3 vertices per triangle |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 32 | verticesCount = (xCount + 1) * (yCount + 1) * 2 * 3; |
| 33 | vertices = new TextureVertex[verticesCount]; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Patch::~Patch() { |
Romain Guy | 31529ff | 2010-09-17 10:26:31 -0700 | [diff] [blame^] | 37 | delete[] vertices; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | // Vertices management |
| 42 | /////////////////////////////////////////////////////////////////////////////// |
| 43 | |
Romain Guy | 759ea80 | 2010-09-16 20:49:46 -0700 | [diff] [blame] | 44 | void 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) { |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 47 | const uint32_t xStretchCount = (width + 1) >> 1; |
| 48 | const uint32_t yStretchCount = (height + 1) >> 1; |
| 49 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 50 | float stretchX = 0.0f; |
| 51 | float stretchY = 0.0; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 52 | |
| 53 | const float meshWidth = right - left; |
| 54 | |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 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 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 60 | const float xStretchTex = stretchSize; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 61 | const float fixed = bitmapWidth - stretchSize; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 62 | const float xStretch = right - left - fixed; |
| 63 | stretchX = xStretch / xStretchTex; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 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 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 71 | const float yStretchTex = stretchSize; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 72 | const float fixed = bitmapHeight - stretchSize; |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 73 | const float yStretch = bottom - top - fixed; |
| 74 | stretchY = yStretch / yStretchTex; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 77 | TextureVertex* vertex = vertices; |
| 78 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 79 | float previousStepY = 0.0f; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 80 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 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; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 91 | } else { |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 92 | y2 = y1 + stepY - previousStepY; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 93 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 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; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 105 | generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX, |
| 106 | right - left, bitmapWidth); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 109 | inline 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; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 112 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 113 | float x1 = 0.0f; |
| 114 | float u1 = 0.0f; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 115 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 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; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 124 | } else { |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 125 | x2 = x1 + stepX - previousStepX; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 126 | } |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 127 | float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 128 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 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; |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 137 | generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 140 | inline 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); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 146 | |
Romain Guy | 6820ac8 | 2010-09-15 18:11:50 -0700 | [diff] [blame] | 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); |
Romain Guy | fb5e23c | 2010-07-09 13:52:56 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | }; // namespace uirenderer |
| 154 | }; // namespace android |