blob: 64bdd6afb0606bc68b14f68e436381c2281459d6 [file] [log] [blame]
Romain Guy759ea802010-09-16 20:49:46 -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_LINE_H
18#define ANDROID_UI_LINE_H
19
20#include <GLES2/gl2.h>
21
22#include <cmath>
23
24#include <sys/types.h>
25
26#include "Patch.h"
27
28namespace android {
29namespace uirenderer {
30
31///////////////////////////////////////////////////////////////////////////////
32// Globals
33///////////////////////////////////////////////////////////////////////////////
34
35// Alpha8 texture used to perform texture anti-aliasing
36static const uint8_t gLineTexture[] = {
37 0, 0, 0, 0, 0,
38 0, 255, 255, 255, 0,
39 0, 255, 255, 255, 0,
40 0, 255, 255, 255, 0,
41 0, 0, 0, 0, 0
42};
43static const GLsizei gLineTextureWidth = 5;
44static const GLsizei gLineTextureHeight = 5;
45static const float gLineAABias = 1.0f;
46
47///////////////////////////////////////////////////////////////////////////////
48// Line
49///////////////////////////////////////////////////////////////////////////////
50
51class Line {
52public:
53 Line(): mXDivsCount(2), mYDivsCount(2) {
54 mPatch = new Patch(mXDivsCount, mYDivsCount);
55 mXDivs = new int32_t[mXDivsCount];
56 mYDivs = new int32_t[mYDivsCount];
57
58 mXDivs[0] = mYDivs[0] = 2;
59 mXDivs[1] = mYDivs[1] = 3;
60
61 glGenTextures(1, &mTexture);
62 glBindTexture(GL_TEXTURE_2D, mTexture);
63
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
65 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
66
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
68 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
69
70 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
71 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gLineTextureWidth, gLineTextureHeight, 0,
72 GL_ALPHA, GL_UNSIGNED_BYTE, gLineTexture);
73 }
74
75 ~Line() {
76 delete mPatch;
Romain Guy31529ff2010-09-17 10:26:31 -070077 delete[] mXDivs;
78 delete[] mYDivs;
Romain Guy759ea802010-09-16 20:49:46 -070079
80 glDeleteTextures(1, &mTexture);
81 }
82
Romain Guyc95c8d62010-09-17 15:31:32 -070083 inline float getLength(float x1, float y1, float x2, float y2) {
84 return sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
85 }
86
Romain Guy759ea802010-09-16 20:49:46 -070087 void update(float x1, float y1, float x2, float y2, float lineWidth, float& tx, float& ty) {
Romain Guyc95c8d62010-09-17 15:31:32 -070088 const float length = getLength(x1, y1, x2, y2);
Romain Guy759ea802010-09-16 20:49:46 -070089 const float half = lineWidth * 0.5f;
90
91 mPatch->updateVertices(gLineTextureWidth, gLineTextureHeight,
92 -gLineAABias, -half - gLineAABias, length + gLineAABias, half + gLineAABias,
93 mXDivs, mYDivs, mXDivsCount, mYDivsCount);
94
95 tx = -gLineAABias;
Romain Guyb5ab4172010-09-17 15:36:56 -070096 ty = lineWidth <= 1.0f ? -gLineAABias : -half - gLineAABias;
Romain Guy759ea802010-09-16 20:49:46 -070097 }
98
99 inline GLvoid* getVertices() const {
100 return &mPatch->vertices[0].position[0];
101 }
102
103 inline GLvoid* getTexCoords() const {
104 return &mPatch->vertices[0].texture[0];
105 }
106
107 inline GLsizei getElementsCount() const {
108 return mPatch->verticesCount;
109 }
110
111 inline GLuint getTexture() const {
112 return mTexture;
113 }
114
115private:
116 uint32_t mXDivsCount;
117 uint32_t mYDivsCount;
118
119 int32_t* mXDivs;
120 int32_t* mYDivs;
121
122 Patch* mPatch;
123
124 GLuint mTexture;
125}; // class Line
126
127}; // namespace uirenderer
128}; // namespace android
129
130#endif // ANDROID_UI_LINE_H