blob: 6d0293695412c51e8c63921a58013253d67b7153 [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
Chris Craik96a5c4c2015-01-27 15:46:35 -080020namespace android {
21namespace uirenderer {
22
23MeshState::MeshState()
Chris Craik117bdbc2015-02-05 10:12:38 -080024 : mCurrentIndicesBuffer(0)
25 , mCurrentPixelBuffer(0)
26 , mCurrentPositionPointer(this)
Chris Craik96a5c4c2015-01-27 15:46:35 -080027 , mCurrentPositionStride(0)
28 , mCurrentTexCoordsPointer(this)
29 , mCurrentTexCoordsStride(0)
Chris Craik117bdbc2015-02-05 10:12:38 -080030 , mTexCoordsArrayEnabled(false)
31 , mQuadListIndices(0) {
Chris Craik03188872015-02-02 18:39:33 -080032 glGenBuffers(1, &mUnitQuadBuffer);
33 glBindBuffer(GL_ARRAY_BUFFER, mUnitQuadBuffer);
34 glBufferData(GL_ARRAY_BUFFER, sizeof(kUnitQuadVertices), kUnitQuadVertices, GL_STATIC_DRAW);
Chris Craik03188872015-02-02 18:39:33 -080035 mCurrentBuffer = mUnitQuadBuffer;
Chris Craik6c15ffa2015-02-02 13:50:55 -080036
Chris Craik182952f2015-03-09 14:17:29 -070037 uint16_t regionIndices[kMaxNumberOfQuads * 6];
Chris Craik2ab95d72015-02-06 15:25:51 -080038 for (uint32_t i = 0; i < kMaxNumberOfQuads; i++) {
39 uint16_t quad = i * 4;
40 int index = i * 6;
41 regionIndices[index ] = quad; // top-left
42 regionIndices[index + 1] = quad + 1; // top-right
43 regionIndices[index + 2] = quad + 2; // bottom-left
44 regionIndices[index + 3] = quad + 2; // bottom-left
45 regionIndices[index + 4] = quad + 1; // top-right
46 regionIndices[index + 5] = quad + 3; // bottom-right
47 }
48 glGenBuffers(1, &mQuadListIndices);
49 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mQuadListIndices);
Chris Craik182952f2015-03-09 14:17:29 -070050 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(regionIndices), regionIndices, GL_STATIC_DRAW);
Chris Craik2ab95d72015-02-06 15:25:51 -080051 mCurrentIndicesBuffer = mQuadListIndices;
52
Chris Craik6c15ffa2015-02-02 13:50:55 -080053 // position attribute always enabled
54 glEnableVertexAttribArray(Program::kBindingPosition);
Chris Craik96a5c4c2015-01-27 15:46:35 -080055}
56
57MeshState::~MeshState() {
Chris Craik03188872015-02-02 18:39:33 -080058 glDeleteBuffers(1, &mUnitQuadBuffer);
Chris Craik96a5c4c2015-01-27 15:46:35 -080059 mCurrentBuffer = 0;
60
61 glDeleteBuffers(1, &mQuadListIndices);
62 mQuadListIndices = 0;
Chris Craik117bdbc2015-02-05 10:12:38 -080063}
Chris Craik96a5c4c2015-01-27 15:46:35 -080064
Chris Craik117bdbc2015-02-05 10:12:38 -080065void MeshState::dump() {
Chris Craik08fa43f2015-02-09 18:58:32 -080066 ALOGD("MeshState VBOs: unitQuad %d, current %d", mUnitQuadBuffer, mCurrentBuffer);
Chris Craikf27133d2015-02-19 09:51:53 -080067 ALOGD("MeshState IBOs: quadList %d, current %d", mQuadListIndices, mCurrentIndicesBuffer);
Chris Craik08fa43f2015-02-09 18:58:32 -080068 ALOGD("MeshState vertices: vertex data %p, stride %d",
69 mCurrentPositionPointer, mCurrentPositionStride);
70 ALOGD("MeshState texCoord: data %p, stride %d",
71 mCurrentTexCoordsPointer, mCurrentTexCoordsStride);
Chris Craik96a5c4c2015-01-27 15:46:35 -080072}
73
74///////////////////////////////////////////////////////////////////////////////
75// Buffer Objects
76///////////////////////////////////////////////////////////////////////////////
77
Chris Craik1b7db402016-02-24 18:25:32 -080078void MeshState::bindMeshBuffer(GLuint buffer) {
Chris Craik117bdbc2015-02-05 10:12:38 -080079 if (mCurrentBuffer != buffer) {
80 glBindBuffer(GL_ARRAY_BUFFER, buffer);
81 mCurrentBuffer = buffer;
Chris Craik1b7db402016-02-24 18:25:32 -080082
83 // buffer has changed, so invalidate cached vertex pos/texcoord pointers
84 resetVertexPointers();
Chris Craik96a5c4c2015-01-27 15:46:35 -080085 }
Chris Craik1b7db402016-02-24 18:25:32 -080086}
87
88void MeshState::unbindMeshBuffer() {
89 return bindMeshBuffer(0);
Chris Craik96a5c4c2015-01-27 15:46:35 -080090}
91
Chris Craik8d2cf942015-11-02 14:52:21 -080092void MeshState::genOrUpdateMeshBuffer(GLuint* buffer, GLsizeiptr size,
93 const void* data, GLenum usage) {
94 if (!*buffer) {
95 glGenBuffers(1, buffer);
96 }
97 bindMeshBuffer(*buffer);
98 glBufferData(GL_ARRAY_BUFFER, size, data, usage);
99}
100
sergeyvfd3744b2016-05-11 16:52:33 -0700101void MeshState::updateMeshBufferSubData(GLuint buffer, GLintptr offset,
102 GLsizeiptr size, const void* data) {
103 bindMeshBuffer(buffer);
104 glBufferSubData(GL_ARRAY_BUFFER, offset, size, data);
105}
106
Chris Craik8d2cf942015-11-02 14:52:21 -0800107void MeshState::deleteMeshBuffer(GLuint buffer) {
108 if (buffer == mCurrentBuffer) {
109 // GL defines that deleting the currently bound VBO rebinds to 0 (no VBO).
110 // Reflect this in our cached value.
111 mCurrentBuffer = 0;
112 }
113 glDeleteBuffers(1, &buffer);
114}
115
Chris Craik96a5c4c2015-01-27 15:46:35 -0800116///////////////////////////////////////////////////////////////////////////////
117// Vertices
118///////////////////////////////////////////////////////////////////////////////
119
Chris Craik1b7db402016-02-24 18:25:32 -0800120void MeshState::bindPositionVertexPointer(const GLvoid* vertices, GLsizei stride) {
121 // update pos coords if !current vbo, since vertices may point into mutable memory (e.g. stack)
122 if (mCurrentBuffer == 0
123 || vertices != mCurrentPositionPointer
124 || stride != mCurrentPositionStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800125 glVertexAttribPointer(Program::kBindingPosition, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800126 mCurrentPositionPointer = vertices;
127 mCurrentPositionStride = stride;
128 }
129}
130
Chris Craik1b7db402016-02-24 18:25:32 -0800131void MeshState::bindTexCoordsVertexPointer(const GLvoid* vertices, GLsizei stride) {
132 // update tex coords if !current vbo, since vertices may point into mutable memory (e.g. stack)
133 if (mCurrentBuffer == 0
134 || vertices != mCurrentTexCoordsPointer
135 || stride != mCurrentTexCoordsStride) {
Chris Craik6c15ffa2015-02-02 13:50:55 -0800136 glVertexAttribPointer(Program::kBindingTexCoords, 2, GL_FLOAT, GL_FALSE, stride, vertices);
Chris Craik96a5c4c2015-01-27 15:46:35 -0800137 mCurrentTexCoordsPointer = vertices;
138 mCurrentTexCoordsStride = stride;
139 }
140}
141
142void MeshState::resetVertexPointers() {
143 mCurrentPositionPointer = this;
144 mCurrentTexCoordsPointer = this;
145}
146
Chris Craik96a5c4c2015-01-27 15:46:35 -0800147void MeshState::enableTexCoordsVertexArray() {
148 if (!mTexCoordsArrayEnabled) {
149 glEnableVertexAttribArray(Program::kBindingTexCoords);
150 mCurrentTexCoordsPointer = this;
151 mTexCoordsArrayEnabled = true;
152 }
153}
154
155void MeshState::disableTexCoordsVertexArray() {
156 if (mTexCoordsArrayEnabled) {
157 glDisableVertexAttribArray(Program::kBindingTexCoords);
158 mTexCoordsArrayEnabled = false;
159 }
160}
161
162///////////////////////////////////////////////////////////////////////////////
163// Indices
164///////////////////////////////////////////////////////////////////////////////
165
Chris Craik1b7db402016-02-24 18:25:32 -0800166void MeshState::bindIndicesBuffer(const GLuint buffer) {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800167 if (mCurrentIndicesBuffer != buffer) {
168 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer);
169 mCurrentIndicesBuffer = buffer;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800170 }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800171}
172
Chris Craik1b7db402016-02-24 18:25:32 -0800173void MeshState::unbindIndicesBuffer() {
Chris Craik96a5c4c2015-01-27 15:46:35 -0800174 if (mCurrentIndicesBuffer) {
175 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
176 mCurrentIndicesBuffer = 0;
Chris Craik96a5c4c2015-01-27 15:46:35 -0800177 }
Chris Craik96a5c4c2015-01-27 15:46:35 -0800178}
179
180} /* namespace uirenderer */
181} /* namespace android */
182