blob: 6b0002034e3f904b7bde833cf9de518a3f4871d8 [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#include "renderstate/MeshState.h"
17
18#include "Program.h"
19
20#include "ShadowTessellator.h"
21
22namespace android {
23namespace uirenderer {
24
25MeshState::MeshState()
Chris Craik117bdbc2015-02-05 10:12:38 -080026 : mCurrentIndicesBuffer(0)
27 , mCurrentPixelBuffer(0)
28 , mCurrentPositionPointer(this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080029 , mCurrentPositionStride(0)
30 , mCurrentTexCoordsPointer(this)
31 , mCurrentTexCoordsStride(0)
Chris Craik117bdbc2015-02-05 10:12:38 -080032 , mTexCoordsArrayEnabled(false)
33 , mQuadListIndices(0) {
Chris Craik03188872015-02-02 18:39:33 -080034 glGenBuffers(1, &mUnitQuadBuffer);
35 glBindBuffer(GL_ARRAY_BUFFER, mUnitQuadBuffer);
36 glBufferData(GL_ARRAY_BUFFER, sizeof(kUnitQuadVertices), kUnitQuadVertices, GL_STATIC_DRAW);
Chris Craik96a5c4c2015-01-27 15:46:35 -080037
Chris Craik03188872015-02-02 18:39:33 -080038 mCurrentBuffer = mUnitQuadBuffer;
Chris Craik6c15ffa2015-02-02 13:50:55 -080039
Chris Craik2ab95d72015-02-06 15:25:51 -080040 std::unique_ptr<uint16_t[]> regionIndices(new uint16_t[kMaxNumberOfQuads * 6]);
41 for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) {
42 uint16_t quad = i * 4;
43 int index = i * 6;
44 regionIndices[index ] = quad; // top-left
45 regionIndices[index + 1] = quad + 1; // top-right
46 regionIndices[index + 2] = quad + 2; // bottom-left
47 regionIndices[index + 3] = quad + 2; // bottom-left
48 regionIndices[index + 4] = quad + 1; // top-right
49 regionIndices[index + 5] = quad + 3; // bottom-right
50 }
51 glGenBuffers(1, &mQuadListIndices);
52 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mQuadListIndices);
53 glBufferData(GL_ELEMENT_ARRAY_BUFFER, kMaxNumberOfQuads * 6 * sizeof(uint16_t),
54 regionIndices.get(), GL_STATIC_DRAW);
55 mCurrentIndicesBuffer = mQuadListIndices;
56
Chris Craik6c15ffa2015-02-02 13:50:55 -080057 // position attribute always enabled
58 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik96a5c4c2015-01-27 15:46:35 -080059}
60
61MeshState::~MeshState() {
Chris Craik03188872015-02-02 18:39:33 -080062 glDeleteBuffers(1, &mUnitQuadBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080063 mCurrentBuffer = 0;
64
65 glDeleteBuffers(1, &mQuadListIndices);
66 mQuadListIndices = 0;
Chris Craik117bdbc2015-02-05 10:12:38 -080067}
Chris Craik96a5c4c2015-01-27 15:46:35 -080068
Chris Craik117bdbc2015-02-05 10:12:38 -080069void MeshState::dump() {
Chris Craik08fa43f2015-02-09 18:58:32 -080070 ALOGD("MeshState VBOs: unitQuad %d, current %d", mUnitQuadBuffer, mCurrentBuffer);
Chris Craikf27133d2015-02-19 09:51:53 -080071 ALOGD("MeshState IBOs: quadList %d, current %d", mQuadListIndices, mCurrentIndicesBuffer);
Chris Craik08fa43f2015-02-09 18:58:32 -080072 ALOGD("MeshState vertices: vertex data %p, stride %d",
73 mCurrentPositionPointer, mCurrentPositionStride);
74 ALOGD("MeshState texCoord: data %p, stride %d",
75 mCurrentTexCoordsPointer, mCurrentTexCoordsStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080076}
77
78///////////////////////////////////////////////////////////////////////////////
79// Buffer Objects
80///////////////////////////////////////////////////////////////////////////////
81
82bool MeshState::bindMeshBuffer() {
Chris Craik03188872015-02-02 18:39:33 -080083 return bindMeshBuffer(mUnitQuadBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080084}
85
86bool MeshState::bindMeshBuffer(GLuint buffer) {
Chris Craik03188872015-02-02 18:39:33 -080087 if (!buffer) buffer = mUnitQuadBuffer;
Chris Craik117bdbc2015-02-05 10:12:38 -080088 return bindMeshBufferInternal(buffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080089}
90
91bool MeshState::unbindMeshBuffer() {
Chris Craik117bdbc2015-02-05 10:12:38 -080092 return bindMeshBufferInternal(0);
93}
94
95bool MeshState::bindMeshBufferInternal(GLuint buffer) {
96 if (mCurrentBuffer != buffer) {
97 glBindBuffer(GL_ARRAY_BUFFER, buffer);
98 mCurrentBuffer = buffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -080099 return true;
100 }
101 return false;
102}
103
104///////////////////////////////////////////////////////////////////////////////
105// Vertices
106///////////////////////////////////////////////////////////////////////////////
107
Chris Craik6c15ffa2015-02-02 13:50:55 -0800108void MeshState::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800109 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800110 glVertexAttribPointer(Program::kBindingPosition, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800111 mCurrentPositionPointer = vertices;
112 mCurrentPositionStride = stride;
113 }
114}
115
Chris Craik6c15ffa2015-02-02 13:50:55 -0800116void MeshState::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800117 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800118 glVertexAttribPointer(Program::kBindingTexCoords, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800119 mCurrentTexCoordsPointer = vertices;
120 mCurrentTexCoordsStride = stride;
121 }
122}
123
124void MeshState::resetVertexPointers() {
125 mCurrentPositionPointer = this;
126 mCurrentTexCoordsPointer = this;
127}
128
129void MeshState::resetTexCoordsVertexPointer() {
130 mCurrentTexCoordsPointer = this;
131}
132
133void MeshState::enableTexCoordsVertexArray() {
134 if (!mTexCoordsArrayEnabled) {
135 glEnableVertexAttribArray(Program::kBindingTexCoords);
136 mCurrentTexCoordsPointer = this;
137 mTexCoordsArrayEnabled = true;
138 }
139}
140
141void MeshState::disableTexCoordsVertexArray() {
142 if (mTexCoordsArrayEnabled) {
143 glDisableVertexAttribArray(Program::kBindingTexCoords);
144 mTexCoordsArrayEnabled = false;
145 }
146}
147
148///////////////////////////////////////////////////////////////////////////////
149// Indices
150///////////////////////////////////////////////////////////////////////////////
151
152bool MeshState::bindIndicesBufferInternal(const GLuint buffer) {
153 if (mCurrentIndicesBuffer != buffer) {
154 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
155 mCurrentIndicesBuffer = buffer;
156 return true;
157 }
158 return false;
159}
160
161bool MeshState::bindQuadIndicesBuffer() {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800162 return bindIndicesBufferInternal(mQuadListIndices);
163}
164
Chris Craik96a5c4c2015-01-27 15:46:35 -0800165bool MeshState::unbindIndicesBuffer() {
166 if (mCurrentIndicesBuffer) {
167 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
168 mCurrentIndicesBuffer = 0;
169 return true;
170 }
171 return false;
172}
173
174} /* namespace uirenderer */
175} /* namespace android */
176