blob: 585fb862b8fa313ca25b1cd4585fbc608003a2a4 [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);
71 ALOGD("MeshState vertices: vertex data %p, stride %d",
72 mCurrentPositionPointer, mCurrentPositionStride);
73 ALOGD("MeshState texCoord: data %p, stride %d",
74 mCurrentTexCoordsPointer, mCurrentTexCoordsStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080075}
76
77///////////////////////////////////////////////////////////////////////////////
78// Buffer Objects
79///////////////////////////////////////////////////////////////////////////////
80
81bool MeshState::bindMeshBuffer() {
Chris Craik03188872015-02-02 18:39:33 -080082 return bindMeshBuffer(mUnitQuadBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080083}
84
85bool MeshState::bindMeshBuffer(GLuint buffer) {
Chris Craik03188872015-02-02 18:39:33 -080086 if (!buffer) buffer = mUnitQuadBuffer;
Chris Craik117bdbc2015-02-05 10:12:38 -080087 return bindMeshBufferInternal(buffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080088}
89
90bool MeshState::unbindMeshBuffer() {
Chris Craik117bdbc2015-02-05 10:12:38 -080091 return bindMeshBufferInternal(0);
92}
93
94bool MeshState::bindMeshBufferInternal(GLuint buffer) {
95 if (mCurrentBuffer != buffer) {
96 glBindBuffer(GL_ARRAY_BUFFER, buffer);
97 mCurrentBuffer = buffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -080098 return true;
99 }
100 return false;
101}
102
103///////////////////////////////////////////////////////////////////////////////
104// Vertices
105///////////////////////////////////////////////////////////////////////////////
106
Chris Craik6c15ffa2015-02-02 13:50:55 -0800107void MeshState::bindPositionVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800108 if (force || vertices != mCurrentPositionPointer || stride != mCurrentPositionStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800109 glVertexAttribPointer(Program::kBindingPosition, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800110 mCurrentPositionPointer = vertices;
111 mCurrentPositionStride = stride;
112 }
113}
114
Chris Craik6c15ffa2015-02-02 13:50:55 -0800115void MeshState::bindTexCoordsVertexPointer(bool force, const GLvoid* vertices, GLsizei stride) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800116 if (force || vertices != mCurrentTexCoordsPointer || stride != mCurrentTexCoordsStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800117 glVertexAttribPointer(Program::kBindingTexCoords, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800118 mCurrentTexCoordsPointer = vertices;
119 mCurrentTexCoordsStride = stride;
120 }
121}
122
123void MeshState::resetVertexPointers() {
124 mCurrentPositionPointer = this;
125 mCurrentTexCoordsPointer = this;
126}
127
128void MeshState::resetTexCoordsVertexPointer() {
129 mCurrentTexCoordsPointer = this;
130}
131
132void MeshState::enableTexCoordsVertexArray() {
133 if (!mTexCoordsArrayEnabled) {
134 glEnableVertexAttribArray(Program::kBindingTexCoords);
135 mCurrentTexCoordsPointer = this;
136 mTexCoordsArrayEnabled = true;
137 }
138}
139
140void MeshState::disableTexCoordsVertexArray() {
141 if (mTexCoordsArrayEnabled) {
142 glDisableVertexAttribArray(Program::kBindingTexCoords);
143 mTexCoordsArrayEnabled = false;
144 }
145}
146
147///////////////////////////////////////////////////////////////////////////////
148// Indices
149///////////////////////////////////////////////////////////////////////////////
150
151bool MeshState::bindIndicesBufferInternal(const GLuint buffer) {
152 if (mCurrentIndicesBuffer != buffer) {
153 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
154 mCurrentIndicesBuffer = buffer;
155 return true;
156 }
157 return false;
158}
159
160bool MeshState::bindQuadIndicesBuffer() {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800161 return bindIndicesBufferInternal(mQuadListIndices);
162}
163
Chris Craik96a5c4c2015-01-27 15:46:35 -0800164bool MeshState::unbindIndicesBuffer() {
165 if (mCurrentIndicesBuffer) {
166 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
167 mCurrentIndicesBuffer = 0;
168 return true;
169 }
170 return false;
171}
172
173} /* namespace uirenderer */
174} /* namespace android */
175