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 #ifndef RENDERSTATE_MESHSTATE_H
17 #define RENDERSTATE_MESHSTATE_H
18 
19 #include "Vertex.h"
20 
21 #include <GLES2/gl2.h>
22 #include <GLES2/gl2ext.h>
23 #include <memory>
24 
25 namespace android {
26 namespace uirenderer {
27 
28 class Program;
29 
30 // Maximum number of quads that pre-allocated meshes can draw
31 const uint32_t kMaxNumberOfQuads = 2048;
32 
33 // This array is never used directly but used as a memcpy source in the
34 // OpenGLRenderer constructor
35 const TextureVertex kUnitQuadVertices[] = {
36         {0, 0, 0, 0}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 1, 1, 1},
37 };
38 
39 const GLsizei kVertexStride = sizeof(Vertex);
40 const GLsizei kAlphaVertexStride = sizeof(AlphaVertex);
41 const GLsizei kTextureVertexStride = sizeof(TextureVertex);
42 const GLsizei kColorTextureVertexStride = sizeof(ColorTextureVertex);
43 
44 const GLsizei kMeshTextureOffset = 2 * sizeof(float);
45 const GLsizei kVertexAlphaOffset = 2 * sizeof(float);
46 const GLsizei kVertexAAWidthOffset = 2 * sizeof(float);
47 const GLsizei kVertexAALengthOffset = 3 * sizeof(float);
48 const GLsizei kUnitQuadCount = 4;
49 
50 class MeshState {
51 private:
52     friend class RenderState;
53 
54 public:
55     ~MeshState();
56     void dump();
57     ///////////////////////////////////////////////////////////////////////////////
58     // Buffer objects
59     ///////////////////////////////////////////////////////////////////////////////
60 
61     /**
62      * Binds the specified VBO if needed. If buffer == 0, binds default simple textured quad.
63      */
64     void bindMeshBuffer(GLuint buffer);
65 
66     /**
67      * Unbinds the current VBO if active.
68      */
69     void unbindMeshBuffer();
70 
71     void genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size, const void* data, GLenum usage);
72     void updateMeshBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size, const void* data);
73     void deleteMeshBuffer(GLuint);
74 
75     ///////////////////////////////////////////////////////////////////////////////
76     // Vertices
77     ///////////////////////////////////////////////////////////////////////////////
78     /**
79      * Binds an attrib to the specified float vertex pointer.
80      * Assumes a stride of gTextureVertexStride and a size of 2.
81      */
82     void bindPositionVertexPointer(const GLvoid* vertices, GLsizei stride = kTextureVertexStride);
83 
84     /**
85      * Binds an attrib to the specified float vertex pointer.
86      * Assumes a stride of gTextureVertexStride and a size of 2.
87      */
88     void bindTexCoordsVertexPointer(const GLvoid* vertices, GLsizei stride = kTextureVertexStride);
89 
90     /**
91      * Resets the vertex pointers.
92      */
93     void resetVertexPointers();
94 
95     void enableTexCoordsVertexArray();
96     void disableTexCoordsVertexArray();
97 
98     ///////////////////////////////////////////////////////////////////////////////
99     // Indices
100     ///////////////////////////////////////////////////////////////////////////////
101     void bindIndicesBuffer(const GLuint buffer);
102     void unbindIndicesBuffer();
103 
104     ///////////////////////////////////////////////////////////////////////////////
105     // Getters - for use in Glop building
106     ///////////////////////////////////////////////////////////////////////////////
getUnitQuadVBO()107     GLuint getUnitQuadVBO() { return mUnitQuadBuffer; }
getQuadListIBO()108     GLuint getQuadListIBO() { return mQuadListIndices; }
109 
110 private:
111     MeshState();
112 
113     GLuint mUnitQuadBuffer;
114 
115     GLuint mCurrentBuffer;
116     GLuint mCurrentIndicesBuffer;
117     GLuint mCurrentPixelBuffer;
118 
119     const void* mCurrentPositionPointer;
120     GLsizei mCurrentPositionStride;
121     const void* mCurrentTexCoordsPointer;
122     GLsizei mCurrentTexCoordsStride;
123 
124     bool mTexCoordsArrayEnabled;
125 
126     // Global index buffer
127     GLuint mQuadListIndices;
128 };
129 
130 } /* namespace uirenderer */
131 } /* namespace android */
132 
133 #endif  // RENDERSTATE_MESHSTATE_H
134