blob: 3d21431bfd8d7877b5c9471a0c0fbf50b4d9475c [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 Guy4bb94202010-10-12 15:59:26 -070021#include <utils/Log.h>
22
Romain Guyfb5e23c2010-07-09 13:52:56 -070023#include "Patch.h"
Romain Guy03750a02010-10-18 14:06:08 -070024#include "Caches.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070025
26namespace android {
27namespace uirenderer {
28
29///////////////////////////////////////////////////////////////////////////////
30// Constructors/destructor
31///////////////////////////////////////////////////////////////////////////////
32
Romain Guy4bb94202010-10-12 15:59:26 -070033Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070034 // 2 triangles per patch, 3 vertices per triangle
Romain Guy4bb94202010-10-12 15:59:26 -070035 verticesCount = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3;
Romain Guy03750a02010-10-18 14:06:08 -070036 mVertices = new TextureVertex[verticesCount];
37
38 glGenBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070039}
40
41Patch::~Patch() {
Romain Guy03750a02010-10-18 14:06:08 -070042 delete[] mVertices;
43 glDeleteBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070044}
45
46///////////////////////////////////////////////////////////////////////////////
47// Vertices management
48///////////////////////////////////////////////////////////////////////////////
49
Romain Guy759ea802010-09-16 20:49:46 -070050void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
51 float left, float top, float right, float bottom,
Romain Guy4bb94202010-10-12 15:59:26 -070052 const int32_t* xDivs, const int32_t* yDivs,
53 const uint32_t width, const uint32_t height, const uint32_t colorKey) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070054 const uint32_t xStretchCount = (width + 1) >> 1;
55 const uint32_t yStretchCount = (height + 1) >> 1;
56
Romain Guy6820ac82010-09-15 18:11:50 -070057 float stretchX = 0.0f;
58 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070059
60 const float meshWidth = right - left;
61
Romain Guyfb5e23c2010-07-09 13:52:56 -070062 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 Guy03750a02010-10-18 14:06:08 -070084 TextureVertex* vertex = mVertices;
Romain Guy4bb94202010-10-12 15:59:26 -070085 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070086
Romain Guy6820ac82010-09-15 18:11:50 -070087 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070088
Romain Guy6820ac82010-09-15 18:11:50 -070089 float y1 = 0.0f;
90 float v1 = 0.0f;
91
92 for (uint32_t i = 0; i < height; i++) {
93 float stepY = yDivs[i];
94
95 float y2 = 0.0f;
96 if (i & 1) {
97 const float segment = stepY - previousStepY;
98 y2 = y1 + segment * stretchY;
Romain Guyfb5e23c2010-07-09 13:52:56 -070099 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700100 y2 = y1 + stepY - previousStepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700101 }
Romain Guy6820ac82010-09-15 18:11:50 -0700102 float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight;
103
104 generateRow(vertex, y1, y2, v1, v2, xDivs, width, stretchX,
Romain Guy4bb94202010-10-12 15:59:26 -0700105 right - left, bitmapWidth, quadCount, colorKey);
Romain Guy6820ac82010-09-15 18:11:50 -0700106
107 y1 = y2;
108 v1 = (stepY + 0.5f) / bitmapHeight;
109
110 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700111 }
112
Romain Guy6820ac82010-09-15 18:11:50 -0700113 generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX,
Romain Guy4bb94202010-10-12 15:59:26 -0700114 right - left, bitmapWidth, quadCount, colorKey);
Romain Guy03750a02010-10-18 14:06:08 -0700115
116 Caches::getInstance().bindMeshBuffer(meshBuffer);
117 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
118 mVertices, GL_STATIC_DRAW);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700119}
120
Romain Guy6820ac82010-09-15 18:11:50 -0700121inline void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
Romain Guy4bb94202010-10-12 15:59:26 -0700122 const int32_t xDivs[], uint32_t xCount, float stretchX, float width, float bitmapWidth,
123 uint32_t& quadCount, const uint32_t colorKey) {
Romain Guy6820ac82010-09-15 18:11:50 -0700124 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700125
Romain Guy6820ac82010-09-15 18:11:50 -0700126 float x1 = 0.0f;
127 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700128
Romain Guy6820ac82010-09-15 18:11:50 -0700129 // Generate the row quad by quad
130 for (uint32_t i = 0; i < xCount; i++) {
131 float stepX = xDivs[i];
132
133 float x2 = 0.0f;
134 if (i & 1) {
135 const float segment = stepX - previousStepX;
136 x2 = x1 + segment * stretchX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700137 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700138 x2 = x1 + stepX - previousStepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700139 }
Romain Guy6820ac82010-09-15 18:11:50 -0700140 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700141
Romain Guy4bb94202010-10-12 15:59:26 -0700142 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount, colorKey);
Romain Guy6820ac82010-09-15 18:11:50 -0700143
144 x1 = x2;
145 u1 = (stepX + 0.5f) / bitmapWidth;
146
147 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700148 }
149
Romain Guy4bb94202010-10-12 15:59:26 -0700150 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2, quadCount, colorKey);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700151}
152
Romain Guy6820ac82010-09-15 18:11:50 -0700153inline void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guy4bb94202010-10-12 15:59:26 -0700154 float u1, float v1, float u2, float v2, uint32_t& quadCount, const uint32_t colorKey) {
155 if (((colorKey >> quadCount++) & 0x1) == 1) {
156 return;
157 }
158
Romain Guy6820ac82010-09-15 18:11:50 -0700159 // Left triangle
160 TextureVertex::set(vertex++, x1, y1, u1, v1);
161 TextureVertex::set(vertex++, x2, y1, u2, v1);
162 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700163
Romain Guy6820ac82010-09-15 18:11:50 -0700164 // Right triangle
165 TextureVertex::set(vertex++, x1, y2, u1, v2);
166 TextureVertex::set(vertex++, x2, y1, u2, v1);
167 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700168}
169
170}; // namespace uirenderer
171}; // namespace android