blob: 48e18eb4a63b975c6da615dfe985ee7aca7ad87a [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;
77 delete mXDivs;
78 delete mYDivs;
79
80 glDeleteTextures(1, &mTexture);
81 }
82
83 void update(float x1, float y1, float x2, float y2, float lineWidth, float& tx, float& ty) {
84 const float length = sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
85 const float half = lineWidth * 0.5f;
86
87 mPatch->updateVertices(gLineTextureWidth, gLineTextureHeight,
88 -gLineAABias, -half - gLineAABias, length + gLineAABias, half + gLineAABias,
89 mXDivs, mYDivs, mXDivsCount, mYDivsCount);
90
91 tx = -gLineAABias;
92 ty = -half - gLineAABias;
93 }
94
95 inline GLvoid* getVertices() const {
96 return &mPatch->vertices[0].position[0];
97 }
98
99 inline GLvoid* getTexCoords() const {
100 return &mPatch->vertices[0].texture[0];
101 }
102
103 inline GLsizei getElementsCount() const {
104 return mPatch->verticesCount;
105 }
106
107 inline GLuint getTexture() const {
108 return mTexture;
109 }
110
111private:
112 uint32_t mXDivsCount;
113 uint32_t mYDivsCount;
114
115 int32_t* mXDivs;
116 int32_t* mYDivs;
117
118 Patch* mPatch;
119
120 GLuint mTexture;
121}; // class Line
122
123}; // namespace uirenderer
124}; // namespace android
125
126#endif // ANDROID_UI_LINE_H