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