Ensure compatible layout for vertices
Require that these structs have standard layout, which is what we need
to pass them to OpenGL. Inheritence with data members violates the
rules for standard layout so I re-implemented them using templates to
share the code and putting all the data members in each struct.
Change-Id: I19cd0e5518728f2d3e0993b17d15fce7be7e0edb
diff --git a/libs/hwui/Vertex.h b/libs/hwui/Vertex.h
index fe8a752..5b80bbd 100644
--- a/libs/hwui/Vertex.h
+++ b/libs/hwui/Vertex.h
@@ -19,6 +19,12 @@
#include "Vector.h"
+#include <type_traits>
+
+#define REQUIRE_COMPATIBLE_LAYOUT(TARGET_TYPE) \
+ static_assert(std::is_standard_layout<TARGET_TYPE>::value, \
+ #TARGET_TYPE " must have standard layout")
+
namespace android {
namespace uirenderer {
@@ -53,6 +59,8 @@
}; // struct Vertex
+REQUIRE_COMPATIBLE_LAYOUT(Vertex);
+
/**
* Simple structure to describe a vertex with a position and texture UV.
*/
@@ -61,10 +69,7 @@
float u, v;
static inline void set(TextureVertex* vertex, float x, float y, float u, float v) {
- vertex[0].x = x;
- vertex[0].y = y;
- vertex[0].u = u;
- vertex[0].v = v;
+ *vertex = { x, y, u, v };
}
static inline void setUV(TextureVertex* vertex, float u, float v) {
@@ -73,39 +78,43 @@
}
}; // struct TextureVertex
+REQUIRE_COMPATIBLE_LAYOUT(TextureVertex);
+
/**
* Simple structure to describe a vertex with a position, texture UV and ARGB color.
*/
-struct ColorTextureVertex : TextureVertex {
+struct ColorTextureVertex {
+ float x, y;
+ float u, v;
float r, g, b, a;
static inline void set(ColorTextureVertex* vertex, float x, float y,
float u, float v, int color) {
- TextureVertex::set(vertex, x, y, u, v);
- const float a = ((color >> 24) & 0xff) / 255.0f;
- vertex[0].r = a * ((color >> 16) & 0xff) / 255.0f;
- vertex[0].g = a * ((color >> 8) & 0xff) / 255.0f;
- vertex[0].b = a * ((color ) & 0xff) / 255.0f;
- vertex[0].a = a;
+ float a = ((color >> 24) & 0xff) / 255.0f;
+ float r = a * ((color >> 16) & 0xff) / 255.0f;
+ float g = a * ((color >> 8) & 0xff) / 255.0f;
+ float b = a * ((color) & 0xff) / 255.0f;
+ *vertex = { x, y, u, v, r, g, b, a };
}
}; // struct ColorTextureVertex
+REQUIRE_COMPATIBLE_LAYOUT(ColorTextureVertex);
+
/**
* Simple structure to describe a vertex with a position and an alpha value.
*/
-struct AlphaVertex : Vertex {
+struct AlphaVertex {
+ float x, y;
float alpha;
static inline void set(AlphaVertex* vertex, float x, float y, float alpha) {
- Vertex::set(vertex, x, y);
- vertex[0].alpha = alpha;
+ *vertex = { x, y, alpha };
}
static inline void copyWithOffset(AlphaVertex* vertex, const AlphaVertex& src,
float x, float y) {
- Vertex::set(vertex, src.x + x, src.y + y);
- vertex[0].alpha = src.alpha;
+ AlphaVertex::set(vertex, src.x + x, src.y + y, src.alpha);
}
static inline void setColor(AlphaVertex* vertex, float alpha) {
@@ -113,6 +122,8 @@
}
}; // struct AlphaVertex
+REQUIRE_COMPATIBLE_LAYOUT(AlphaVertex);
+
}; // namespace uirenderer
}; // namespace android