blob: a6607303010500b17d48f21395eb1b885ccbb307 [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
19#include <cstring>
Romain Guy6820ac82010-09-15 18:11:50 -070020#include <cmath>
Romain Guyfb5e23c2010-07-09 13:52:56 -070021
22#include <utils/Log.h>
23
24#include "Patch.h"
25
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy6820ac82010-09-15 18:11:50 -070033Patch::Patch(const uint32_t xCount, const uint32_t yCount) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070034 // 2 triangles per patch, 3 vertices per triangle
Romain Guy6820ac82010-09-15 18:11:50 -070035 verticesCount = (xCount + 1) * (yCount + 1) * 2 * 3;
36 vertices = new TextureVertex[verticesCount];
37 memset(vertices, 0, sizeof(TextureVertex) * verticesCount);
Romain Guyfb5e23c2010-07-09 13:52:56 -070038}
39
40Patch::~Patch() {
Romain Guyfb5e23c2010-07-09 13:52:56 -070041 delete vertices;
42}
43
44///////////////////////////////////////////////////////////////////////////////
45// Vertices management
46///////////////////////////////////////////////////////////////////////////////
47
48void Patch::updateVertices(const SkBitmap* bitmap, float left, float top, float right,
Romain Guy6820ac82010-09-15 18:11:50 -070049 float bottom, const int32_t* xDivs, const int32_t* yDivs, const uint32_t width,
Romain Guyfb5e23c2010-07-09 13:52:56 -070050 const uint32_t height) {
51 const uint32_t xStretchCount = (width + 1) >> 1;
52 const uint32_t yStretchCount = (height + 1) >> 1;
53
Romain Guy6820ac82010-09-15 18:11:50 -070054 float stretchX = 0.0f;
55 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070056
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 Guy6820ac82010-09-15 18:11:50 -070067 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070068 const float fixed = bitmapWidth - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070069 const float xStretch = right - left - fixed;
70 stretchX = xStretch / xStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070071 }
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 Guy6820ac82010-09-15 18:11:50 -070078 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070079 const float fixed = bitmapHeight - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070080 const float yStretch = bottom - top - fixed;
81 stretchY = yStretch / yStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070082 }
83
Romain Guyfb5e23c2010-07-09 13:52:56 -070084 TextureVertex* vertex = vertices;
85
Romain Guy6820ac82010-09-15 18:11:50 -070086 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070087
Romain Guy6820ac82010-09-15 18:11:50 -070088 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 Guyfb5e23c2010-07-09 13:52:56 -070098 } else {
Romain Guy6820ac82010-09-15 18:11:50 -070099 y2 = y1 + stepY - previousStepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700100 }
Romain Guy6820ac82010-09-15 18:11:50 -0700101 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 Guyfb5e23c2010-07-09 13:52:56 -0700110 }
111
Romain Guy6820ac82010-09-15 18:11:50 -0700112 generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX,
113 right - left, bitmapWidth);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700114}
115
Romain Guy6820ac82010-09-15 18:11:50 -0700116inline 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 Guyfb5e23c2010-07-09 13:52:56 -0700119
Romain Guy6820ac82010-09-15 18:11:50 -0700120 float x1 = 0.0f;
121 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700122
Romain Guy6820ac82010-09-15 18:11:50 -0700123 // 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 Guyfb5e23c2010-07-09 13:52:56 -0700131 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700132 x2 = x1 + stepX - previousStepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700133 }
Romain Guy6820ac82010-09-15 18:11:50 -0700134 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700135
Romain Guy6820ac82010-09-15 18:11:50 -0700136 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 Guyfb5e23c2010-07-09 13:52:56 -0700142 }
143
Romain Guy6820ac82010-09-15 18:11:50 -0700144 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700145}
146
Romain Guy6820ac82010-09-15 18:11:50 -0700147inline 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 Guyfb5e23c2010-07-09 13:52:56 -0700153
Romain Guy6820ac82010-09-15 18:11:50 -0700154 // 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 Guyfb5e23c2010-07-09 13:52:56 -0700158}
159
160}; // namespace uirenderer
161}; // namespace android