Home | History | Annotate | Download | only in hwui
      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 #include "FloatColor.h"
     23 #include "utils/Macros.h"
     24 
     25 namespace android {
     26 namespace uirenderer {
     27 
     28 /**
     29  * Simple structure to describe a vertex with a position and a texture.
     30  */
     31 struct Vertex {
     32     /**
     33      * Fudge-factor used to disambiguate geometry pixel positioning.
     34      *
     35      * Used to offset lines and points to avoid ambiguous intersection with pixel centers (see
     36      * Program::set()), and used to make geometry damage rect calculation conservative (see
     37      * Rect::snapGeometryToPixelBoundaries())
     38      */
     39     static float GeometryFudgeFactor() { return 0.0656f; }
     40 
     41     float x, y;
     42 
     43     static inline void set(Vertex* vertex, float x, float y) {
     44         vertex->x = x;
     45         vertex->y = y;
     46     }
     47 
     48     static inline void set(Vertex* vertex, Vector2 val) {
     49         set(vertex, val.x, val.y);
     50     }
     51 
     52     static inline void copyWithOffset(Vertex* vertex, const Vertex& src, float x, float y) {
     53         set(vertex, src.x + x, src.y + y);
     54     }
     55 
     56 }; // struct Vertex
     57 
     58 REQUIRE_COMPATIBLE_LAYOUT(Vertex);
     59 
     60 /**
     61  * Simple structure to describe a vertex with a position and texture UV.
     62  */
     63 struct TextureVertex {
     64     float x, y;
     65     float u, v;
     66 
     67     static inline void set(TextureVertex* vertex, float x, float y, float u, float v) {
     68         *vertex = { x, y, u, v };
     69     }
     70 
     71     static inline void setUV(TextureVertex* vertex, float u, float v) {
     72         vertex[0].u = u;
     73         vertex[0].v = v;
     74     }
     75 }; // struct TextureVertex
     76 
     77 REQUIRE_COMPATIBLE_LAYOUT(TextureVertex);
     78 
     79 /**
     80  * Simple structure to describe a vertex with a position, texture UV and an
     81  * sRGB color with alpha. The color is stored pre-multiplied in linear space.
     82  */
     83 struct ColorTextureVertex {
     84     float x, y;
     85     float u, v;
     86     float r, g, b, a; // pre-multiplied linear
     87 
     88     static inline void set(ColorTextureVertex* vertex, float x, float y,
     89             float u, float v, uint32_t color) {
     90         FloatColor c;
     91         c.set(color);
     92         *vertex = { x, y, u, v, c.r, c.g, c.b, c.a };
     93     }
     94 }; // struct ColorTextureVertex
     95 
     96 REQUIRE_COMPATIBLE_LAYOUT(ColorTextureVertex);
     97 
     98 /**
     99  * Simple structure to describe a vertex with a position and an alpha value.
    100  */
    101 struct AlphaVertex {
    102     float x, y;
    103     float alpha;
    104 
    105     static inline void set(AlphaVertex* vertex, float x, float y, float alpha) {
    106         *vertex = { x, y, alpha };
    107     }
    108 
    109     static inline void copyWithOffset(AlphaVertex* vertex, const AlphaVertex& src,
    110             float x, float y) {
    111         AlphaVertex::set(vertex, src.x + x, src.y + y, src.alpha);
    112     }
    113 
    114     static inline void setColor(AlphaVertex* vertex, float alpha) {
    115         vertex[0].alpha = alpha;
    116     }
    117 }; // struct AlphaVertex
    118 
    119 REQUIRE_COMPATIBLE_LAYOUT(AlphaVertex);
    120 
    121 }; // namespace uirenderer
    122 }; // namespace android
    123 
    124 #endif // ANDROID_HWUI_VERTEX_H
    125