blob: 95faf1ebfb02975a0d91c91e1cd4ccb11e96973c [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[] = {
John Reck1bcacfd2017-11-03 10:12:19 -070036 {0, 0, 0, 0}, {1, 0, 1, 0}, {0, 1, 0, 1}, {1, 1, 1, 1},
Chris Craik96a5c4c2015-01-27 15:46:35 -080037};
38
39const GLsizei kVertexStride = sizeof(Vertex);
40const GLsizei kAlphaVertexStride = sizeof(AlphaVertex);
41const GLsizei kTextureVertexStride = sizeof(TextureVertex);
Chris Craikef250742015-02-25 17:16:16 -080042const GLsizei kColorTextureVertexStride = sizeof(ColorTextureVertex);
Chris Craik96a5c4c2015-01-27 15:46:35 -080043
44const GLsizei kMeshTextureOffset = 2 * sizeof(float);
45const GLsizei kVertexAlphaOffset = 2 * sizeof(float);
46const GLsizei kVertexAAWidthOffset = 2 * sizeof(float);
47const GLsizei kVertexAALengthOffset = 3 * sizeof(float);
Chris Craik117bdbc2015-02-05 10:12:38 -080048const GLsizei kUnitQuadCount = 4;
Chris Craik96a5c4c2015-01-27 15:46:35 -080049
50class MeshState {
51private:
52 friend class RenderState;
53
54public:
55 ~MeshState();
Chris Craik117bdbc2015-02-05 10:12:38 -080056 void dump();
Chris Craik96a5c4c2015-01-27 15:46:35 -080057 ///////////////////////////////////////////////////////////////////////////////
58 // Buffer objects
59 ///////////////////////////////////////////////////////////////////////////////
Chris Craik96a5c4c2015-01-27 15:46:35 -080060
61 /**
62 * Binds the specified VBO if needed. If buffer == 0, binds default simple textured quad.
63 */
Chris Craik1b7db402016-02-24 18:25:32 -080064 void bindMeshBuffer(GLuint buffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080065
66 /**
Chris Craik1b7db402016-02-24 18:25:32 -080067 * Unbinds the current VBO if active.
Chris Craik96a5c4c2015-01-27 15:46:35 -080068 */
Chris Craik1b7db402016-02-24 18:25:32 -080069 void unbindMeshBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -080070
Chris Craik8d2cf942015-11-02 14:52:21 -080071 void genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size, const void* data, GLenum usage);
sergeyvfd3744b2016-05-11 16:52:33 -070072 void updateMeshBufferSubData(GLuint buffer, GLintptr offset, GLsizeiptr size, const void* data);
Chris Craik8d2cf942015-11-02 14:52:21 -080073 void deleteMeshBuffer(GLuint);
74
Chris Craik96a5c4c2015-01-27 15:46:35 -080075 ///////////////////////////////////////////////////////////////////////////////
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 */
John Reck1bcacfd2017-11-03 10:12:19 -070082 void bindPositionVertexPointer(const GLvoid* vertices, GLsizei stride = kTextureVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080083
84 /**
85 * Binds an attrib to the specified float vertex pointer.
86 * Assumes a stride of gTextureVertexStride and a size of 2.
87 */
John Reck1bcacfd2017-11-03 10:12:19 -070088 void bindTexCoordsVertexPointer(const GLvoid* vertices, GLsizei stride = kTextureVertexStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080089
90 /**
91 * Resets the vertex pointers.
92 */
93 void resetVertexPointers();
Chris Craik96a5c4c2015-01-27 15:46:35 -080094
95 void enableTexCoordsVertexArray();
96 void disableTexCoordsVertexArray();
97
98 ///////////////////////////////////////////////////////////////////////////////
99 // Indices
100 ///////////////////////////////////////////////////////////////////////////////
Chris Craik1b7db402016-02-24 18:25:32 -0800101 void bindIndicesBuffer(const GLuint buffer);
102 void unbindIndicesBuffer();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800103
Chris Craik03188872015-02-02 18:39:33 -0800104 ///////////////////////////////////////////////////////////////////////////////
105 // Getters - for use in Glop building
106 ///////////////////////////////////////////////////////////////////////////////
107 GLuint getUnitQuadVBO() { return mUnitQuadBuffer; }
Chris Craik2ab95d72015-02-06 15:25:51 -0800108 GLuint getQuadListIBO() { return mQuadListIndices; }
John Reck1bcacfd2017-11-03 10:12:19 -0700109
Chris Craik96a5c4c2015-01-27 15:46:35 -0800110private:
111 MeshState();
Chris Craik96a5c4c2015-01-27 15:46:35 -0800112
Chris Craik03188872015-02-02 18:39:33 -0800113 GLuint mUnitQuadBuffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800114
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;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800128};
129
130} /* namespace uirenderer */
131} /* namespace android */
132
John Reck1bcacfd2017-11-03 10:12:19 -0700133#endif // RENDERSTATE_MESHSTATE_H