Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 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 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_HWUI_VERTEX_H |
| 18 | #define ANDROID_HWUI_VERTEX_H |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 19 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 20 | #include "Vector.h" |
| 21 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 22 | #include <type_traits> |
| 23 | |
| 24 | #define REQUIRE_COMPATIBLE_LAYOUT(TARGET_TYPE) \ |
| 25 | static_assert(std::is_standard_layout<TARGET_TYPE>::value, \ |
| 26 | #TARGET_TYPE " must have standard layout") |
| 27 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 28 | namespace android { |
| 29 | namespace uirenderer { |
| 30 | |
| 31 | /** |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 32 | * Simple structure to describe a vertex with a position and a texture. |
| 33 | */ |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 34 | struct Vertex { |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Fudge-factor used to disambiguate geometry pixel positioning. |
| 37 | * |
| 38 | * Used to offset lines and points to avoid ambiguous intersection with pixel centers (see |
| 39 | * Program::set()), and used to make geometry damage rect calculation conservative (see |
| 40 | * Rect::snapGeometryToPixelBoundaries()) |
| 41 | */ |
Chris Craik | 564acf7 | 2014-01-02 16:46:18 -0800 | [diff] [blame] | 42 | static float GeometryFudgeFactor() { return 0.0656f; } |
| 43 | |
Chris Craik | 32f05e3 | 2013-09-17 16:20:29 -0700 | [diff] [blame] | 44 | |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 45 | float x, y; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 46 | |
| 47 | static inline void set(Vertex* vertex, float x, float y) { |
Chris Craik | 51d6a3d | 2014-12-22 17:16:56 -0800 | [diff] [blame] | 48 | vertex->x = x; |
| 49 | vertex->y = y; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 50 | } |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 51 | |
John Reck | 1aa5d2d | 2014-07-24 13:38:28 -0700 | [diff] [blame] | 52 | static inline void set(Vertex* vertex, Vector2 val) { |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 53 | set(vertex, val.x, val.y); |
| 54 | } |
| 55 | |
| 56 | static inline void copyWithOffset(Vertex* vertex, const Vertex& src, float x, float y) { |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 57 | set(vertex, src.x + x, src.y + y); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 60 | }; // struct Vertex |
| 61 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 62 | REQUIRE_COMPATIBLE_LAYOUT(Vertex); |
| 63 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 64 | /** |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 65 | * Simple structure to describe a vertex with a position and texture UV. |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 66 | */ |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 67 | struct TextureVertex { |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 68 | float x, y; |
| 69 | float u, v; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 70 | |
| 71 | static inline void set(TextureVertex* vertex, float x, float y, float u, float v) { |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 72 | *vertex = { x, y, u, v }; |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 73 | } |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 74 | |
| 75 | static inline void setUV(TextureVertex* vertex, float u, float v) { |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 76 | vertex[0].u = u; |
| 77 | vertex[0].v = v; |
Romain Guy | 82ba814 | 2010-07-09 13:25:56 -0700 | [diff] [blame] | 78 | } |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 79 | }; // struct TextureVertex |
| 80 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 81 | REQUIRE_COMPATIBLE_LAYOUT(TextureVertex); |
| 82 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 83 | /** |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 84 | * Simple structure to describe a vertex with a position, texture UV and ARGB color. |
| 85 | */ |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 86 | struct ColorTextureVertex { |
| 87 | float x, y; |
| 88 | float u, v; |
Romain Guy | 3380cfd | 2013-08-15 16:57:57 -0700 | [diff] [blame] | 89 | float r, g, b, a; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 90 | |
| 91 | static inline void set(ColorTextureVertex* vertex, float x, float y, |
| 92 | float u, float v, int color) { |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 93 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 94 | float a = ((color >> 24) & 0xff) / 255.0f; |
| 95 | float r = a * ((color >> 16) & 0xff) / 255.0f; |
| 96 | float g = a * ((color >> 8) & 0xff) / 255.0f; |
| 97 | float b = a * ((color) & 0xff) / 255.0f; |
| 98 | *vertex = { x, y, u, v, r, g, b, a }; |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 99 | } |
| 100 | }; // struct ColorTextureVertex |
| 101 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 102 | REQUIRE_COMPATIBLE_LAYOUT(ColorTextureVertex); |
| 103 | |
Romain Guy | ff316ec | 2013-02-13 18:39:43 -0800 | [diff] [blame] | 104 | /** |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 105 | * Simple structure to describe a vertex with a position and an alpha value. |
| 106 | */ |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 107 | struct AlphaVertex { |
| 108 | float x, y; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 109 | float alpha; |
| 110 | |
| 111 | static inline void set(AlphaVertex* vertex, float x, float y, float alpha) { |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 112 | *vertex = { x, y, alpha }; |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 115 | static inline void copyWithOffset(AlphaVertex* vertex, const AlphaVertex& src, |
| 116 | float x, float y) { |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 117 | AlphaVertex::set(vertex, src.x + x, src.y + y, src.alpha); |
Chris Craik | 6d29c8d | 2013-05-08 18:35:44 -0700 | [diff] [blame] | 118 | } |
| 119 | |
Chet Haase | 5b0200b | 2011-04-13 17:58:08 -0700 | [diff] [blame] | 120 | static inline void setColor(AlphaVertex* vertex, float alpha) { |
| 121 | vertex[0].alpha = alpha; |
| 122 | } |
| 123 | }; // struct AlphaVertex |
| 124 | |
Rob Tsuk | a9fc9b2 | 2015-01-22 12:46:31 -0800 | [diff] [blame^] | 125 | REQUIRE_COMPATIBLE_LAYOUT(AlphaVertex); |
| 126 | |
Romain Guy | f7f9355 | 2010-07-08 19:17:03 -0700 | [diff] [blame] | 127 | }; // namespace uirenderer |
| 128 | }; // namespace android |
| 129 | |
Romain Guy | 5b3b352 | 2010-10-27 18:57:51 -0700 | [diff] [blame] | 130 | #endif // ANDROID_HWUI_VERTEX_H |