blob: c2ebee0405d42a11a9abbc059010de544e25bfcb [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>
Romain Guy03750a02010-10-18 14:06:08 -070022#include <utils/String8.h>
Romain Guy4bb94202010-10-12 15:59:26 -070023
Romain Guyfb5e23c2010-07-09 13:52:56 -070024#include "Patch.h"
Romain Guy03750a02010-10-18 14:06:08 -070025#include "Caches.h"
Romain Guyfb5e23c2010-07-09 13:52:56 -070026
27namespace android {
28namespace uirenderer {
29
Romain Guy03750a02010-10-18 14:06:08 -070030class Caches;
31
Romain Guyfb5e23c2010-07-09 13:52:56 -070032///////////////////////////////////////////////////////////////////////////////
33// Constructors/destructor
34///////////////////////////////////////////////////////////////////////////////
35
Romain Guy4bb94202010-10-12 15:59:26 -070036Patch::Patch(const uint32_t xCount, const uint32_t yCount, const int8_t emptyQuads) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070037 // 2 triangles per patch, 3 vertices per triangle
Romain Guy4bb94202010-10-12 15:59:26 -070038 verticesCount = ((xCount + 1) * (yCount + 1) - emptyQuads) * 2 * 3;
Romain Guy03750a02010-10-18 14:06:08 -070039 mVertices = new TextureVertex[verticesCount];
40
41 glGenBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070042}
43
44Patch::~Patch() {
Romain Guy03750a02010-10-18 14:06:08 -070045 delete[] mVertices;
46 glDeleteBuffers(1, &meshBuffer);
Romain Guyfb5e23c2010-07-09 13:52:56 -070047}
48
49///////////////////////////////////////////////////////////////////////////////
50// Vertices management
51///////////////////////////////////////////////////////////////////////////////
52
Romain Guy759ea802010-09-16 20:49:46 -070053void Patch::updateVertices(const float bitmapWidth, const float bitmapHeight,
54 float left, float top, float right, float bottom,
Romain Guy4bb94202010-10-12 15:59:26 -070055 const int32_t* xDivs, const int32_t* yDivs,
56 const uint32_t width, const uint32_t height, const uint32_t colorKey) {
Romain Guyfb5e23c2010-07-09 13:52:56 -070057 const uint32_t xStretchCount = (width + 1) >> 1;
58 const uint32_t yStretchCount = (height + 1) >> 1;
59
Romain Guy6820ac82010-09-15 18:11:50 -070060 float stretchX = 0.0f;
61 float stretchY = 0.0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070062
63 const float meshWidth = right - left;
64
Romain Guyfb5e23c2010-07-09 13:52:56 -070065 if (xStretchCount > 0) {
66 uint32_t stretchSize = 0;
67 for (uint32_t i = 1; i < width; i += 2) {
68 stretchSize += xDivs[i] - xDivs[i - 1];
69 }
Romain Guy6820ac82010-09-15 18:11:50 -070070 const float xStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070071 const float fixed = bitmapWidth - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070072 const float xStretch = right - left - fixed;
73 stretchX = xStretch / xStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070074 }
75
76 if (yStretchCount > 0) {
77 uint32_t stretchSize = 0;
78 for (uint32_t i = 1; i < height; i += 2) {
79 stretchSize += yDivs[i] - yDivs[i - 1];
80 }
Romain Guy6820ac82010-09-15 18:11:50 -070081 const float yStretchTex = stretchSize;
Romain Guyfb5e23c2010-07-09 13:52:56 -070082 const float fixed = bitmapHeight - stretchSize;
Romain Guy6820ac82010-09-15 18:11:50 -070083 const float yStretch = bottom - top - fixed;
84 stretchY = yStretch / yStretchTex;
Romain Guyfb5e23c2010-07-09 13:52:56 -070085 }
86
Romain Guy03750a02010-10-18 14:06:08 -070087 TextureVertex* vertex = mVertices;
Romain Guy4bb94202010-10-12 15:59:26 -070088 uint32_t quadCount = 0;
Romain Guyfb5e23c2010-07-09 13:52:56 -070089
Romain Guy6820ac82010-09-15 18:11:50 -070090 float previousStepY = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -070091
Romain Guy6820ac82010-09-15 18:11:50 -070092 float y1 = 0.0f;
93 float v1 = 0.0f;
94
95 for (uint32_t i = 0; i < height; i++) {
96 float stepY = yDivs[i];
97
98 float y2 = 0.0f;
99 if (i & 1) {
100 const float segment = stepY - previousStepY;
101 y2 = y1 + segment * stretchY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700102 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700103 y2 = y1 + stepY - previousStepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700104 }
Romain Guy6820ac82010-09-15 18:11:50 -0700105 float v2 = fmax(0.0f, stepY - 0.5f) / bitmapHeight;
106
107 generateRow(vertex, y1, y2, v1, v2, xDivs, width, stretchX,
Romain Guy4bb94202010-10-12 15:59:26 -0700108 right - left, bitmapWidth, quadCount, colorKey);
Romain Guy6820ac82010-09-15 18:11:50 -0700109
110 y1 = y2;
111 v1 = (stepY + 0.5f) / bitmapHeight;
112
113 previousStepY = stepY;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700114 }
115
Romain Guy6820ac82010-09-15 18:11:50 -0700116 generateRow(vertex, y1, bottom - top, v1, 1.0f, xDivs, width, stretchX,
Romain Guy4bb94202010-10-12 15:59:26 -0700117 right - left, bitmapWidth, quadCount, colorKey);
Romain Guy03750a02010-10-18 14:06:08 -0700118
119 Caches::getInstance().bindMeshBuffer(meshBuffer);
120 glBufferData(GL_ARRAY_BUFFER, sizeof(TextureVertex) * verticesCount,
121 mVertices, GL_STATIC_DRAW);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700122}
123
Romain Guy6820ac82010-09-15 18:11:50 -0700124inline void Patch::generateRow(TextureVertex*& vertex, float y1, float y2, float v1, float v2,
Romain Guy4bb94202010-10-12 15:59:26 -0700125 const int32_t xDivs[], uint32_t xCount, float stretchX, float width, float bitmapWidth,
126 uint32_t& quadCount, const uint32_t colorKey) {
Romain Guy6820ac82010-09-15 18:11:50 -0700127 float previousStepX = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700128
Romain Guy6820ac82010-09-15 18:11:50 -0700129 float x1 = 0.0f;
130 float u1 = 0.0f;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700131
Romain Guy6820ac82010-09-15 18:11:50 -0700132 // Generate the row quad by quad
133 for (uint32_t i = 0; i < xCount; i++) {
134 float stepX = xDivs[i];
135
136 float x2 = 0.0f;
137 if (i & 1) {
138 const float segment = stepX - previousStepX;
139 x2 = x1 + segment * stretchX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700140 } else {
Romain Guy6820ac82010-09-15 18:11:50 -0700141 x2 = x1 + stepX - previousStepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700142 }
Romain Guy6820ac82010-09-15 18:11:50 -0700143 float u2 = fmax(0.0f, stepX - 0.5f) / bitmapWidth;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700144
Romain Guy4bb94202010-10-12 15:59:26 -0700145 generateQuad(vertex, x1, y1, x2, y2, u1, v1, u2, v2, quadCount, colorKey);
Romain Guy6820ac82010-09-15 18:11:50 -0700146
147 x1 = x2;
148 u1 = (stepX + 0.5f) / bitmapWidth;
149
150 previousStepX = stepX;
Romain Guyfb5e23c2010-07-09 13:52:56 -0700151 }
152
Romain Guy4bb94202010-10-12 15:59:26 -0700153 generateQuad(vertex, x1, y1, width, y2, u1, v1, 1.0f, v2, quadCount, colorKey);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700154}
155
Romain Guy6820ac82010-09-15 18:11:50 -0700156inline void Patch::generateQuad(TextureVertex*& vertex, float x1, float y1, float x2, float y2,
Romain Guy4bb94202010-10-12 15:59:26 -0700157 float u1, float v1, float u2, float v2, uint32_t& quadCount, const uint32_t colorKey) {
158 if (((colorKey >> quadCount++) & 0x1) == 1) {
159 return;
160 }
161
Romain Guy6820ac82010-09-15 18:11:50 -0700162 // Left triangle
163 TextureVertex::set(vertex++, x1, y1, u1, v1);
164 TextureVertex::set(vertex++, x2, y1, u2, v1);
165 TextureVertex::set(vertex++, x1, y2, u1, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700166
Romain Guy6820ac82010-09-15 18:11:50 -0700167 // Right triangle
168 TextureVertex::set(vertex++, x1, y2, u1, v2);
169 TextureVertex::set(vertex++, x2, y1, u2, v1);
170 TextureVertex::set(vertex++, x2, y2, u2, v2);
Romain Guyfb5e23c2010-07-09 13:52:56 -0700171}
172
173}; // namespace uirenderer
174}; // namespace android