blob: 6a40c6c680a2887cce4e8d14308bb281c7878b2e [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
24Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
Chia-I Wub027f802017-11-29 14:00:52 -080025 : mVertexCount(vertexCount),
26 mVertexSize(vertexSize),
27 mTexCoordsSize(texCoordSize),
28 mPrimitive(primitive) {
Dan Stozaab79e332015-04-29 13:30:31 -070029 if (vertexCount == 0) {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070030 mVertices.resize(1);
Dan Stozaab79e332015-04-29 13:30:31 -070031 mVertices[0] = 0.0f;
32 mStride = 0;
33 return;
34 }
35
36 size_t stride = vertexSize + texCoordSize;
37 size_t remainder = (stride * vertexCount) / vertexCount;
38 // Since all of the input parameters are unsigned, if stride is less than
39 // either vertexSize or texCoordSize, it must have overflowed. remainder
40 // will be equal to stride as long as stride * vertexCount doesn't overflow.
41 if ((stride < vertexSize) || (remainder != stride)) {
Chia-I Wub027f802017-11-29 14:00:52 -080042 ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize);
Chia-I Wud3b13cb2018-09-13 13:31:26 -070043 mVertices.resize(1);
Dan Stozaab79e332015-04-29 13:30:31 -070044 mVertices[0] = 0.0f;
45 mVertexCount = 0;
46 mVertexSize = 0;
47 mTexCoordsSize = 0;
48 mStride = 0;
49 return;
50 }
51
Chia-I Wud3b13cb2018-09-13 13:31:26 -070052 mVertices.resize(stride * vertexCount);
Dan Stozaab79e332015-04-29 13:30:31 -070053 mStride = stride;
Mathias Agopian3f844832013-08-07 21:24:32 -070054}
55
Mathias Agopian3f844832013-08-07 21:24:32 -070056Mesh::Primitive Mesh::getPrimitive() const {
57 return mPrimitive;
58}
59
Mathias Agopian5cdc8992013-08-13 20:51:23 -070060float const* Mesh::getPositions() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070061 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070062}
Mathias Agopian5cdc8992013-08-13 20:51:23 -070063float* Mesh::getPositions() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070064 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070065}
66
67float const* Mesh::getTexCoords() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070068 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070069}
70float* Mesh::getTexCoords() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070071 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070072}
73
Mathias Agopian3f844832013-08-07 21:24:32 -070074size_t Mesh::getVertexCount() const {
75 return mVertexCount;
76}
77
78size_t Mesh::getVertexSize() const {
79 return mVertexSize;
80}
81
82size_t Mesh::getTexCoordsSize() const {
83 return mTexCoordsSize;
84}
85
86size_t Mesh::getByteStride() const {
Chia-I Wub027f802017-11-29 14:00:52 -080087 return mStride * sizeof(float);
Mathias Agopian3f844832013-08-07 21:24:32 -070088}
89
90size_t Mesh::getStride() const {
91 return mStride;
92}
93
Peiyong Lin46080ef2018-10-26 18:43:14 -070094} // namespace renderengine
95} // namespace android