blob: 264fd19c7543808763e050d6c82c0b02874b7556 [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
Romain Guy5b3b3522010-10-27 18:57:51 -070017#ifndef ANDROID_HWUI_LINE_H
18#define ANDROID_HWUI_LINE_H
Romain Guy759ea802010-09-16 20:49:46 -070019
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
Romain Guy6f72beb2010-11-30 12:04:14 -080061 mPatch->copy(mXDivs, mYDivs);
62
Romain Guy759ea802010-09-16 20:49:46 -070063 glGenTextures(1, &mTexture);
64 glBindTexture(GL_TEXTURE_2D, mTexture);
65
66 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
67 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
68
69 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
70 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
71
72 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
73 glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, gLineTextureWidth, gLineTextureHeight, 0,
74 GL_ALPHA, GL_UNSIGNED_BYTE, gLineTexture);
75 }
76
77 ~Line() {
78 delete mPatch;
Romain Guy31529ff2010-09-17 10:26:31 -070079 delete[] mXDivs;
80 delete[] mYDivs;
Romain Guy759ea802010-09-16 20:49:46 -070081
82 glDeleteTextures(1, &mTexture);
83 }
84
Romain Guyc95c8d62010-09-17 15:31:32 -070085 inline float getLength(float x1, float y1, float x2, float y2) {
86 return sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
87 }
88
Romain Guy759ea802010-09-16 20:49:46 -070089 void update(float x1, float y1, float x2, float y2, float lineWidth, float& tx, float& ty) {
Romain Guyc95c8d62010-09-17 15:31:32 -070090 const float length = getLength(x1, y1, x2, y2);
Romain Guy759ea802010-09-16 20:49:46 -070091 const float half = lineWidth * 0.5f;
92
93 mPatch->updateVertices(gLineTextureWidth, gLineTextureHeight,
Romain Guy6f72beb2010-11-30 12:04:14 -080094 -gLineAABias, -half - gLineAABias, length + gLineAABias, half + gLineAABias);
Romain Guy759ea802010-09-16 20:49:46 -070095
96 tx = -gLineAABias;
Romain Guyb5ab4172010-09-17 15:36:56 -070097 ty = lineWidth <= 1.0f ? -gLineAABias : -half - gLineAABias;
Romain Guy759ea802010-09-16 20:49:46 -070098 }
99
Romain Guy03750a02010-10-18 14:06:08 -0700100 inline GLuint getMeshBuffer() const {
101 return mPatch->meshBuffer;
Romain Guy759ea802010-09-16 20:49:46 -0700102 }
103
104 inline GLsizei getElementsCount() const {
105 return mPatch->verticesCount;
106 }
107
108 inline GLuint getTexture() const {
109 return mTexture;
110 }
111
112private:
113 uint32_t mXDivsCount;
114 uint32_t mYDivsCount;
115
116 int32_t* mXDivs;
117 int32_t* mYDivs;
118
119 Patch* mPatch;
120
121 GLuint mTexture;
122}; // class Line
123
124}; // namespace uirenderer
125}; // namespace android
126
Romain Guy5b3b3522010-10-27 18:57:51 -0700127#endif // ANDROID_HWUI_LINE_H