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 
90 public:
GlopGlop91     Glop() {}
92     struct Mesh {
93         GLuint primitiveMode;  // GL_TRIANGLES and GL_TRIANGLE_STRIP supported
94 
95         // buffer object and void* are mutually exclusive.
96         // Only GL_UNSIGNED_SHORT supported.
97         struct Indices {
98             GLuint bufferObject;
99             const void* indices;
100         } indices;
101 
102         // buffer object and void*s are mutually exclusive.
103         // TODO: enforce mutual exclusion with restricted setters and/or unions
104         struct Vertices {
105             GLuint bufferObject;
106             int attribFlags;
107             const void* position;
108             const void* texCoord;
109             const void* color;
110             GLsizei stride;
111         } vertices;
112 
113         int elementCount;
114         int vertexCount;  // only used for meshes (for glDrawRangeElements)
115         TextureVertex mappedVertices[4];
116     } mesh;
117 
118     struct Fill {
119         Program* program;
120 
121         struct TextureData {
122             Texture* texture;
123             GLenum filter;
124             GLenum clamp;
125             Matrix4* textureTransform;
126         } texture;
127 
128         bool colorEnabled;
129         FloatColor color;
130 
131         ProgramDescription::ColorFilterMode filterMode;
132         union Filter {
133             struct Matrix {
134                 float matrix[16];
135                 float vector[4];
136             } matrix;
137             FloatColor color;
138         } filter;
139 
140         SkiaShaderData skiaShaderData;
141     } fill;
142 
143     struct Transform {
144         // modelView transform, accounting for delta between mesh transform and content of the mesh
145         // often represents x/y offsets within command, or scaling for mesh unit size
146         Matrix4 modelView;
147 
148         // Canvas transform of Glop - not necessarily applied to geometry (see flags)
149         Matrix4 canvas;
150         int transformFlags;
151 
meshTransformGlop::Transform152         const Matrix4& meshTransform() const {
153             return (transformFlags & TransformFlags::MeshIgnoresCanvasTransform)
154                            ? Matrix4::identity()
155                            : canvas;
156         }
157     } transform;
158 
159     const RoundRectClipState* roundRectClipState = nullptr;
160 
161     /**
162      * Blending to be used by this draw - both GL_NONE if blending is disabled.
163      *
164      * Defined by fill step, but can be force-enabled by presence of kAlpha_Attrib
165      */
166     struct Blend {
167         GLenum src;
168         GLenum dst;
169     } blend;
170 
171     /**
172      * Additional render state to enumerate:
173      * - scissor + (bits for whether each of LTRB needed?)
174      * - stencil mode (draw into, mask, count, etc)
175      */
176 };
177 
178 } /* namespace uirenderer */
179 } /* namespace android */
180