blob: ca0a0b1691de7926771835e6d275842a0f67ac2c [file] [log] [blame]
Romain Guyfb5e23c2010-07-09 13:52:56 -07001/*
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 Guy6820ac82010-09-15 18:11:50 -070019#include <cmath>
Romain Guyfb5e23c2010-07-09 13:52:56 -070020
Romain Guyfb5e23c2010-07-09 13:52:56 -070021#include "Patch.h"
22
23namespace android {
24namespace uirenderer {
25
26///////////////////////////////////////////////////////////////////////////////
27// Constructors/destructor
28///////////////////////////////////////////////////////////////////////////////
29
Romain Guy6820ac82010-09-15 18:11:50 -070030Patch::Patch(const uint32_t xCount, const uint32_t yCount) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070031 // 2 triangles per patch, 3 vertices per triangle
Romain Guy6820ac82010-09-15 18:11:50 -070032 verticesCount = (xCount + 1) * (yCount + 1) * 2 * 3;
33 vertices = new TextureVertex[verticesCount];
Romain Guyfb5e23c2010-07-09 13:52:56 -070034}
35
36Patch::~Patch() {
Romain Guy31529ff2010-09-17 10:26:31 -070037 delete[] vertices;
Romain Guyfb5e23c2010-07-09 13:52:56 -070038}
39
40///////////////////////////////////////////////////////////////////////////////
41// Vertices management
42///////////////////////////////////////////////////////////////////////////////
43
Romain Guy759ea802010-09-16 20:49:46 -070044void 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 Guyfb5e23c2010-07-09 13:52:56 -070047 const uint32_t xStretchCount = (width + 1) >> 1;
48 const uint32_t yStretchCount = (height + 1) >> 1;
49
Romain Guy6820ac82010-09-15 18:11:50 -070050 float stretchX = 0.0f;
51 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070052
53 const float meshWidth = right - left;
54
Romain Guyfb5e23c2010-07-09 13:52:56 -070055 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 Guy6820ac82010-09-15 18:11:50 -070060 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070061 const float fixed = bitmapWidth - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070062 const float xStretch = right - left - fixed;
63 stretchX = xStretch / xStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070064 }
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 Guy6820ac82010-09-15 18:11:50 -070071 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070072 const float fixed = bitmapHeight - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070073 const float yStretch = bottom - top - fixed;
74 stretchY = yStretch / yStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070075 }
76
Romain Guyfb5e23c2010-07-09 13:52:56 -070077 TextureVertex* vertex = vertices;
78
Romain Guy6820ac82010-09-15 18:11:50 -070079 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070080
Romain Guy6820ac82010-09-15 18:11:50 -070081 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 Guyfb5e23c2010-07-09 13:52:56 -070091 } else {
Romain Guy6820ac82010-09-15 18:11:50 -070092 y2 = y1 + stepY - previousStepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -070093 }
Romain Guy6820ac82010-09-15 18:11:50 -070094 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 Guyfb5e23c2010-07-09 13:52:56 -0700103 }
104
Romain Guy6820ac82010-09-15 18:11:50 -0700105 generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX,
106 right - left, bitmapWidth);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700107}
108
Romain Guy6820ac82010-09-15 18:11:50 -0700109inline 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 Guyfb5e23c2010-07-09 13:52:56 -0700112
Romain Guy6820ac82010-09-15 18:11:50 -0700113 float x1 = 0.0f;
114 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700115
Romain Guy6820ac82010-09-15 18:11:50 -0700116 // 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 Guyfb5e23c2010-07-09 13:52:56 -0700124 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700125 x2 = x1 + stepX - previousStepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700126 }
Romain Guy6820ac82010-09-15 18:11:50 -0700127 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700128
Romain Guy6820ac82010-09-15 18:11:50 -0700129 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 Guyfb5e23c2010-07-09 13:52:56 -0700135 }
136
Romain Guy6820ac82010-09-15 18:11:50 -0700137 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700138}
139
Romain Guy6820ac82010-09-15 18:11:50 -0700140inline 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 Guyfb5e23c2010-07-09 13:52:56 -0700146
Romain Guy6820ac82010-09-15 18:11:50 -0700147 // 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 Guyfb5e23c2010-07-09 13:52:56 -0700151}
152
153}; // namespace uirenderer
154}; // namespace android