blob: 6c0fb78cae17b5fdfac54843f790055bb7cd0e68 [file] [log] [blame]
Chris Craik96a5c4c2015-01-27 15:46:35 -08001/*
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
Chris Craik03188872015-02-02 18:39:33 -080035const TextureVertex kUnitQuadVertices[] = {
Chris Craik96a5c4c2015-01-27 15:46:35 -080036 { 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);
Chris Craikef250742015-02-25 17:16:16 -080045const GLsizei kColorTextureVertexStride = sizeof(ColorTextureVertex);
Chris Craik96a5c4c2015-01-27 15:46:35 -080046
47const GLsizei kMeshTextureOffset = 2 * sizeof(float);
48const GLsizei kVertexAlphaOffset = 2 * sizeof(float);
49const GLsizei kVertexAAWidthOffset = 2 * sizeof(float);
50const GLsizei kVertexAALengthOffset = 3 * sizeof(float);
Chris Craik117bdbc2015-02-05 10:12:38 -080051const GLsizei kUnitQuadCount = 4;
Chris Craik96a5c4c2015-01-27 15:46:35 -080052
53class MeshState {
54private:
55 friend class RenderState;
56
57public:
58 ~MeshState();
Chris Craik117bdbc2015-02-05 10:12:38 -080059 void dump();
Chris Craik96a5c4c2015-01-27 15:46:35 -080060 ///////////////////////////////////////////////////////////////////////////////
61 // Buffer objects
62 ///////////////////////////////////////////////////////////////////////////////
63 /**
64 * Binds the VBO used to render simple textured quads.
65 */
66 bool bindMeshBuffer();
67
68 /**
69 * Binds the specified VBO if needed. If buffer == 0, binds default simple textured quad.
70 */
71 bool bindMeshBuffer(GLuint buffer);
72
73 /**
74 * Unbinds the VBO used to render simple textured quads.
75 */
76 bool unbindMeshBuffer();
77
Chris Craik8d2cf942015-11-02 14:52:21 -080078 void genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size, const void* data, GLenum usage);
79 void deleteMeshBuffer(GLuint);
80
Chris Craik96a5c4c2015-01-27 15:46:35 -080081 ///////////////////////////////////////////////////////////////////////////////
82 // Vertices
83 ///////////////////////////////////////////////////////////////////////////////
84 /**
85 * Binds an attrib to the specified float vertex pointer.
86 * Assumes a stride of gTextureVertexStride and a size of 2.
87 */
Chris Craik6c15ffa2015-02-02 13:50:55 -080088 void bindPositionVertexPointer(bool force, const GLvoid* vertices,
89 GLsizei stride = kTextureVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080090
91 /**
92 * Binds an attrib to the specified float vertex pointer.
93 * Assumes a stride of gTextureVertexStride and a size of 2.
94 */
Chris Craik6c15ffa2015-02-02 13:50:55 -080095 void bindTexCoordsVertexPointer(bool force, const GLvoid* vertices,
96 GLsizei stride = kTextureVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080097
98 /**
99 * Resets the vertex pointers.
100 */
101 void resetVertexPointers();
102 void resetTexCoordsVertexPointer();
103
104 void enableTexCoordsVertexArray();
105 void disableTexCoordsVertexArray();
106
107 ///////////////////////////////////////////////////////////////////////////////
108 // Indices
109 ///////////////////////////////////////////////////////////////////////////////
110 /**
111 * Binds a global indices buffer that can draw up to
112 * gMaxNumberOfQuads quads.
113 */
114 bool bindQuadIndicesBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800115 bool unbindIndicesBuffer();
116
Chris Craik03188872015-02-02 18:39:33 -0800117 ///////////////////////////////////////////////////////////////////////////////
118 // Getters - for use in Glop building
119 ///////////////////////////////////////////////////////////////////////////////
120 GLuint getUnitQuadVBO() { return mUnitQuadBuffer; }
Chris Craik2ab95d72015-02-06 15:25:51 -0800121 GLuint getQuadListIBO() { return mQuadListIndices; }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800122private:
123 MeshState();
Chris Craik117bdbc2015-02-05 10:12:38 -0800124 bool bindMeshBufferInternal(const GLuint buffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800125 bool bindIndicesBufferInternal(const GLuint buffer);
126
Chris Craik03188872015-02-02 18:39:33 -0800127 GLuint mUnitQuadBuffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800128
129 GLuint mCurrentBuffer;
130 GLuint mCurrentIndicesBuffer;
131 GLuint mCurrentPixelBuffer;
132
133 const void* mCurrentPositionPointer;
134 GLsizei mCurrentPositionStride;
135 const void* mCurrentTexCoordsPointer;
136 GLsizei mCurrentTexCoordsStride;
137
138 bool mTexCoordsArrayEnabled;
139
140 // Global index buffer
141 GLuint mQuadListIndices;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800142};
143
144} /* namespace uirenderer */
145} /* namespace android */
146
147#endif // RENDERSTATE_MESHSTATE_H