Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b9307823 authored by Rob Tsuk's avatar Rob Tsuk Committed by Android (Google) Code Review
Browse files

Merge "Ensure compatible layout for vertices"

parents daa880d9 a9fc9b20
Loading
Loading
Loading
Loading
+27 −16
Original line number Diff line number Diff line
@@ -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 {

}; // struct Vertex

REQUIRE_COMPATIBLE_LAYOUT(Vertex);

/**
 * Simple structure to describe a vertex with a position and texture UV.
 */
@@ -61,10 +69,7 @@ struct TextureVertex {
    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 {
    }
}; // 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 : Vertex {
    }
}; // struct AlphaVertex

REQUIRE_COMPATIBLE_LAYOUT(AlphaVertex);

}; // namespace uirenderer
}; // namespace android