1 /*
2  * Copyright (C) 2015 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 #pragma once
18 
19 #include "FloatColor.h"
20 #include "Matrix.h"
21 #include "Program.h"
22 #include "Rect.h"
23 #include "SkiaShader.h"
24 #include "utils/Macros.h"
25 
26 #include <GLES2/gl2.h>
27 #include <GLES2/gl2ext.h>
28 
29 namespace android {
30 namespace uirenderer {
31 
32 class Program;
33 class RoundRectClipState;
34 class Texture;
35 
36 /*
37  * Enumerates optional vertex attributes
38  *
39  * Position is always enabled by MeshState, these other attributes
40  * are enabled/disabled dynamically based on mesh content.
41  */
42 
43 namespace VertexAttribFlags {
44     enum {
45         // Mesh is pure x,y vertex pairs
46         None = 0,
47         // Mesh has texture coordinates embedded. Note that texture can exist without this flag
48         // being set, if coordinates passed to sampler are determined another way.
49         TextureCoord = 1 << 0,
50         // Mesh has color embedded (to export to varying)
51         Color = 1 << 1,
52         // Mesh has alpha embedded (to export to varying)
53         Alpha = 1 << 2,
54     };
55 };
56 
57 /*
58  * Enumerates transform features
59  */
60 namespace TransformFlags {
61     enum {
62         None = 0,
63 
64         // offset the eventual drawing matrix by a tiny amount to
65         // disambiguate sampling patterns with non-AA rendering
66         OffsetByFudgeFactor = 1 << 0,
67 
68         // Canvas transform isn't applied to the mesh at draw time,
69         // since it's already built in.
70         MeshIgnoresCanvasTransform = 1 << 1, // TODO: remove for HWUI_NEW_OPS
71     };
72 };
73 
74 /**
75  * Structure containing all data required to issue an OpenGL draw
76  *
77  * Includes all of the mesh, fill, and GL state required to perform
78  * the operation. Pieces of data are either directly copied into the
79  * structure, or stored as a pointer or GL object reference to data
80  * managed.
81  *
82  * Eventually, a Glop should be able to be drawn multiple times from
83  * a single construction, up until GL context destruction. Currently,
84  * vertex/index/Texture/RoundRectClipState pointers prevent this from
85  * being safe.
86  */
87 struct Glop {
88     PREVENT_COPY_AND_ASSIGN(Glop);
89 public:
GlopGlop90     Glop() { }
91     struct Mesh {
92         GLuint primitiveMode; // GL_TRIANGLES and GL_TRIANGLE_STRIP supported
93 
94         // buffer object and void* are mutually exclusive.
95         // Only GL_UNSIGNED_SHORT supported.
96         struct Indices {
97             GLuint bufferObject;
98             const void* indices;
99         } indices;
100 
101         // buffer object and void*s are mutually exclusive.
102         // TODO: enforce mutual exclusion with restricted setters and/or unions
103         struct Vertices {
104             GLuint bufferObject;
105             int attribFlags;
106             const void* position;
107             const void* texCoord;
108             const void* color;
109             GLsizei stride;
110         } vertices;
111 
112         int elementCount;
113         TextureVertex mappedVertices[4];
114     } mesh;
115 
116     struct Fill {
117         Program* program;
118 
119         struct TextureData {
120             Texture* texture;
121             GLenum filter;
122             GLenum clamp;
123             Matrix4* textureTransform;
124         } texture;
125 
126         bool colorEnabled;
127         FloatColor color;
128 
129         ProgramDescription::ColorFilterMode filterMode;
130         union Filter {
131             struct Matrix {
132                 float matrix[16];
133                 float vector[4];
134             } matrix;
135             FloatColor color;
136         } filter;
137 
138         SkiaShaderData skiaShaderData;
139     } fill;
140 
141     struct Transform {
142         // modelView transform, accounting for delta between mesh transform and content of the mesh
143         // often represents x/y offsets within command, or scaling for mesh unit size
144         Matrix4 modelView;
145 
146         // Canvas transform of Glop - not necessarily applied to geometry (see flags)
147         Matrix4 canvas;
148         int transformFlags;
149 
meshTransformGlop::Transform150        const Matrix4& meshTransform() const {
151            return (transformFlags & TransformFlags::MeshIgnoresCanvasTransform)
152                    ? Matrix4::identity() : canvas;
153        }
154     } transform;
155 
156     const RoundRectClipState* roundRectClipState = nullptr;
157 
158     /**
159      * Blending to be used by this draw - both GL_NONE if blending is disabled.
160      *
161      * Defined by fill step, but can be force-enabled by presence of kAlpha_Attrib
162      */
163     struct Blend {
164         GLenum src;
165         GLenum dst;
166     } blend;
167 
168     /**
169      * Additional render state to enumerate:
170      * - scissor + (bits for whether each of LTRB needed?)
171      * - stencil mode (draw into, mask, count, etc)
172      */
173 };
174 
175 } /* namespace uirenderer */
176 } /* namespace android */
177