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