blob: ed2f45fdf5492b966ceea078450d0fcfcca55c3f [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*
2 * Copyright 2013 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
Peiyong Lincbc184f2018-08-22 13:24:10 -070017#include <renderengine/Mesh.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070018
Dan Stozaab79e332015-04-29 13:30:31 -070019#include <utils/Log.h>
20
Mathias Agopian3f844832013-08-07 21:24:32 -070021namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070022namespace renderengine {
Mathias Agopian3f844832013-08-07 21:24:32 -070023
Vishnu Nair440992f2019-12-09 19:53:19 -080024Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize,
25 size_t cropCoordsSize, size_t shadowColorSize, size_t shadowParamsSize,
26 size_t indexCount)
Chia-I Wub027f802017-11-29 14:00:52 -080027 : mVertexCount(vertexCount),
28 mVertexSize(vertexSize),
29 mTexCoordsSize(texCoordSize),
Vishnu Nair440992f2019-12-09 19:53:19 -080030 mCropCoordsSize(cropCoordsSize),
31 mShadowColorSize(shadowColorSize),
32 mShadowParamsSize(shadowParamsSize),
33 mPrimitive(primitive),
34 mIndexCount(indexCount) {
Dan Stozaab79e332015-04-29 13:30:31 -070035 if (vertexCount == 0) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070036 mVertices.resize(1);
Dan Stozaab79e332015-04-29 13:30:31 -070037 mVertices[0] = 0.0f;
38 mStride = 0;
39 return;
40 }
Vishnu Nair440992f2019-12-09 19:53:19 -080041 size_t stride = vertexSize + texCoordSize + cropCoordsSize + shadowColorSize + shadowParamsSize;
Dan Stozaab79e332015-04-29 13:30:31 -070042 size_t remainder = (stride * vertexCount) / vertexCount;
43 // Since all of the input parameters are unsigned, if stride is less than
44 // either vertexSize or texCoordSize, it must have overflowed. remainder
45 // will be equal to stride as long as stride * vertexCount doesn't overflow.
46 if ((stride < vertexSize) || (remainder != stride)) {
Vishnu Nair440992f2019-12-09 19:53:19 -080047 ALOGE("Overflow in Mesh(..., %zu, %zu, %zu, %zu, %zu, %zu)", vertexCount, vertexSize,
48 texCoordSize, cropCoordsSize, shadowColorSize, shadowParamsSize);
Chia-I Wud3b13cb2018-09-13 13:31:26 -070049 mVertices.resize(1);
Dan Stozaab79e332015-04-29 13:30:31 -070050 mVertices[0] = 0.0f;
51 mVertexCount = 0;
52 mVertexSize = 0;
53 mTexCoordsSize = 0;
Vishnu Nair440992f2019-12-09 19:53:19 -080054 mCropCoordsSize = 0;
55 mShadowColorSize = 0;
56 mShadowParamsSize = 0;
Dan Stozaab79e332015-04-29 13:30:31 -070057 mStride = 0;
58 return;
59 }
60
Chia-I Wud3b13cb2018-09-13 13:31:26 -070061 mVertices.resize(stride * vertexCount);
Dan Stozaab79e332015-04-29 13:30:31 -070062 mStride = stride;
Vishnu Nair440992f2019-12-09 19:53:19 -080063 mIndices.resize(indexCount);
Mathias Agopian3f844832013-08-07 21:24:32 -070064}
65
Mathias Agopian3f844832013-08-07 21:24:32 -070066Mesh::Primitive Mesh::getPrimitive() const {
67 return mPrimitive;
68}
69
Mathias Agopian5cdc8992013-08-13 20:51:23 -070070float const* Mesh::getPositions() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070071 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070072}
Mathias Agopian5cdc8992013-08-13 20:51:23 -070073float* Mesh::getPositions() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070074 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070075}
76
77float const* Mesh::getTexCoords() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070078 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070079}
80float* Mesh::getTexCoords() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070081 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070082}
83
Lucas Dupin1b6531c2018-07-05 17:18:21 -070084float const* Mesh::getCropCoords() const {
85 return mVertices.data() + mVertexSize + mTexCoordsSize;
86}
87float* Mesh::getCropCoords() {
88 return mVertices.data() + mVertexSize + mTexCoordsSize;
89}
90
Vishnu Nair440992f2019-12-09 19:53:19 -080091float const* Mesh::getShadowColor() const {
92 return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize;
93}
94float* Mesh::getShadowColor() {
95 return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize;
96}
97
98float const* Mesh::getShadowParams() const {
99 return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize + mShadowColorSize;
100}
101float* Mesh::getShadowParams() {
102 return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize + mShadowColorSize;
103}
104
105uint16_t const* Mesh::getIndices() const {
106 return mIndices.data();
107}
108
109uint16_t* Mesh::getIndices() {
110 return mIndices.data();
111}
112
Mathias Agopian3f844832013-08-07 21:24:32 -0700113size_t Mesh::getVertexCount() const {
114 return mVertexCount;
115}
116
117size_t Mesh::getVertexSize() const {
118 return mVertexSize;
119}
120
121size_t Mesh::getTexCoordsSize() const {
122 return mTexCoordsSize;
123}
124
Vishnu Nair440992f2019-12-09 19:53:19 -0800125size_t Mesh::getShadowColorSize() const {
126 return mShadowColorSize;
127}
128
129size_t Mesh::getShadowParamsSize() const {
130 return mShadowParamsSize;
131}
132
Mathias Agopian3f844832013-08-07 21:24:32 -0700133size_t Mesh::getByteStride() const {
Chia-I Wub027f802017-11-29 14:00:52 -0800134 return mStride * sizeof(float);
Mathias Agopian3f844832013-08-07 21:24:32 -0700135}
136
137size_t Mesh::getStride() const {
138 return mStride;
139}
140
Vishnu Nair440992f2019-12-09 19:53:19 -0800141size_t Mesh::getIndexCount() const {
142 return mIndexCount;
143}
144
Peiyong Lin46080ef2018-10-26 18:43:14 -0700145} // namespace renderengine
146} // namespace android