blob: c6af18a474ac3d105756e9023df055966119549e [file] [log] [blame]
Romain Guyf7f93552010-07-08 19:17:03 -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#ifndef ANDROID_UI_PATCH_H
18#define ANDROID_UI_PATCH_H
19
20#include <sys/types.h>
21
22#include <cstring>
23
24#include "Vertex.h"
25
26namespace android {
27namespace uirenderer {
28
29/**
30 * Description of a patch.
31 */
32struct PatchDescription {
33 PatchDescription(): xCount(0), yCount(0) { }
34 PatchDescription(const uint32_t xCount, const uint32_t yCount):
35 xCount(xCount), yCount(yCount) { }
36 PatchDescription(const PatchDescription& description):
37 xCount(description.xCount), yCount(description.yCount) { }
38
39 uint32_t xCount;
40 uint32_t yCount;
41
42 bool operator<(const PatchDescription& rhs) const {
43 if (xCount == rhs.xCount) {
44 return yCount < rhs.yCount;
45 }
46 return xCount < rhs.xCount;
47 }
48
49 bool operator==(const PatchDescription& rhs) const {
50 return xCount == rhs.xCount && yCount == rhs.yCount;
51 }
52}; // struct PatchDescription
53
54/**
55 * An OpenGL patch. This contains an array of vertices and an array of
56 * indices to render the vertices.
57 */
58struct Patch {
59 Patch(const uint32_t xCount, const uint32_t yCount): xCount(xCount + 2), yCount(yCount + 2) {
60 verticesCount = (xCount + 2) * (yCount + 2);
61 vertices = new TextureVertex[verticesCount];
62
63 // 2 triangles per patch, 3 vertices per triangle
64 indicesCount = (xCount + 1) * (yCount + 1) * 2 * 3;
65 indices = new uint16_t[indicesCount];
66
67 const uint32_t xNum = xCount + 1;
68 const uint32_t yNum = yCount + 1;
69
70 uint16_t* startIndices = indices;
71 uint32_t n = 0;
72 for (uint32_t y = 0; y < yNum; y++) {
73 for (uint32_t x = 0; x < xNum; x++) {
74 *startIndices++ = n;
75 *startIndices++ = n + 1;
76 *startIndices++ = n + xNum + 2;
77
78 *startIndices++ = n;
79 *startIndices++ = n + xNum + 2;
80 *startIndices++ = n + xNum + 1;
81
82 n += 1;
83 }
84 n += 1;
85 }
86 }
87
88 ~Patch() {
89 delete indices;
90 delete vertices;
91 }
92
93 void dump() {
94 LOGD("Vertices [");
95 for (uint32_t y = 0; y < yCount; y++) {
96 char buffer[512];
97 buffer[0] = '\0';
98 uint32_t offset = 0;
99 for (uint32_t x = 0; x < xCount; x++) {
100 TextureVertex* v = &vertices[y * xCount + x];
101 offset += sprintf(&buffer[offset], " (%.4f,%.4f)-(%.4f,%.4f)",
102 v->position[0], v->position[1], v->texture[0], v->texture[1]);
103 }
104 LOGD(" [%s ]", buffer);
105 }
106 LOGD("]\nIndices [ ");
107 char buffer[4096];
108 buffer[0] = '\0';
109 uint32_t offset = 0;
110 for (uint32_t i = 0; i < indicesCount; i++) {
111 offset += sprintf(&buffer[offset], "%d ", indices[i]);
112 }
113 LOGD(" %s\n]", buffer);
114 }
115
116 uint32_t xCount;
117 uint32_t yCount;
118
119 uint16_t* indices;
120 uint32_t indicesCount;
121
122 TextureVertex* vertices;
123 uint32_t verticesCount;
124}; // struct Patch
125
126}; // namespace uirenderer
127}; // namespace android
128
129#endif // ANDROID_UI_PATCH_H