1 /*
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_HWUI_VERTEX_H
18 #define ANDROID_HWUI_VERTEX_H
19 
20 #include "Vector.h"
21 
22 namespace android {
23 namespace uirenderer {
24 
25 /**
26  * Simple structure to describe a vertex with a position and a texture.
27  */
28 struct Vertex {
29     /**
30      * Fudge-factor used to disambiguate geometry pixel positioning.
31      *
32      * Used to offset lines and points to avoid ambiguous intersection with pixel centers (see
33      * Program::set()), and used to make geometry damage rect calculation conservative (see
34      * Rect::snapGeometryToPixelBoundaries())
35      */
GeometryFudgeFactorVertex36     static float GeometryFudgeFactor() { return 0.0656f; }
37 
38 
39     float x, y;
40 
setVertex41     static inline void set(Vertex* vertex, float x, float y) {
42         vertex[0].x = x;
43         vertex[0].y = y;
44     }
45 
setVertex46     static inline void set(Vertex* vertex, Vector2 val) {
47         set(vertex, val.x, val.y);
48     }
49 
copyWithOffsetVertex50     static inline void copyWithOffset(Vertex* vertex, const Vertex& src, float x, float y) {
51         set(vertex, src.x + x, src.y + y);
52     }
53 
54 }; // struct Vertex
55 
56 /**
57  * Simple structure to describe a vertex with a position and texture UV.
58  */
59 struct TextureVertex {
60     float x, y;
61     float u, v;
62 
setTextureVertex63     static inline void set(TextureVertex* vertex, float x, float y, float u, float v) {
64         vertex[0].x = x;
65         vertex[0].y = y;
66         vertex[0].u = u;
67         vertex[0].v = v;
68     }
69 
setUVTextureVertex70     static inline void setUV(TextureVertex* vertex, float u, float v) {
71         vertex[0].u = u;
72         vertex[0].v = v;
73     }
74 }; // struct TextureVertex
75 
76 /**
77  * Simple structure to describe a vertex with a position, texture UV and ARGB color.
78  */
79 struct ColorTextureVertex : TextureVertex {
80     float r, g, b, a;
81 
setColorTextureVertex82     static inline void set(ColorTextureVertex* vertex, float x, float y,
83             float u, float v, int color) {
84         TextureVertex::set(vertex, x, y, u, v);
85 
86         const float a = ((color >> 24) & 0xff) / 255.0f;
87         vertex[0].r = a * ((color >> 16) & 0xff) / 255.0f;
88         vertex[0].g = a * ((color >>  8) & 0xff) / 255.0f;
89         vertex[0].b = a * ((color      ) & 0xff) / 255.0f;
90         vertex[0].a = a;
91     }
92 }; // struct ColorTextureVertex
93 
94 /**
95  * Simple structure to describe a vertex with a position and an alpha value.
96  */
97 struct AlphaVertex : Vertex {
98     float alpha;
99 
setAlphaVertex100     static inline void set(AlphaVertex* vertex, float x, float y, float alpha) {
101         Vertex::set(vertex, x, y);
102         vertex[0].alpha = alpha;
103     }
104 
copyWithOffsetAlphaVertex105     static inline void copyWithOffset(AlphaVertex* vertex, const AlphaVertex& src,
106             float x, float y) {
107         Vertex::set(vertex, src.x + x, src.y + y);
108         vertex[0].alpha = src.alpha;
109     }
110 
setColorAlphaVertex111     static inline void setColor(AlphaVertex* vertex, float alpha) {
112         vertex[0].alpha = alpha;
113     }
114 }; // struct AlphaVertex
115 
116 }; // namespace uirenderer
117 }; // namespace android
118 
119 #endif // ANDROID_HWUI_VERTEX_H
120