blob: 5a3c2c85b01e46f25863006234202b131d9d1019 [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) {
30 mVertices = new float[1];
31 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);
Dan Stozaab79e332015-04-29 13:30:31 -070043 mVertices = new float[1];
44 mVertices[0] = 0.0f;
45 mVertexCount = 0;
46 mVertexSize = 0;
47 mTexCoordsSize = 0;
48 mStride = 0;
49 return;
50 }
51
52 mVertices = new float[stride * vertexCount];
53 mStride = stride;
Mathias Agopian3f844832013-08-07 21:24:32 -070054}
55
56Mesh::~Mesh() {
Chia-I Wub027f802017-11-29 14:00:52 -080057 delete[] mVertices;
Mathias Agopian3f844832013-08-07 21:24:32 -070058}
59
Mathias Agopian3f844832013-08-07 21:24:32 -070060Mesh::Primitive Mesh::getPrimitive() const {
61 return mPrimitive;
62}
63
Mathias Agopian5cdc8992013-08-13 20:51:23 -070064float const* Mesh::getPositions() const {
Mathias Agopian3f844832013-08-07 21:24:32 -070065 return mVertices;
66}
Mathias Agopian5cdc8992013-08-13 20:51:23 -070067float* Mesh::getPositions() {
Mathias Agopian3f844832013-08-07 21:24:32 -070068 return mVertices;
69}
70
71float const* Mesh::getTexCoords() const {
72 return mVertices + mVertexSize;
73}
74float* Mesh::getTexCoords() {
75 return mVertices + mVertexSize;
76}
77
Mathias Agopian3f844832013-08-07 21:24:32 -070078size_t Mesh::getVertexCount() const {
79 return mVertexCount;
80}
81
82size_t Mesh::getVertexSize() const {
83 return mVertexSize;
84}
85
86size_t Mesh::getTexCoordsSize() const {
87 return mTexCoordsSize;
88}
89
90size_t Mesh::getByteStride() const {
Chia-I Wub027f802017-11-29 14:00:52 -080091 return mStride * sizeof(float);
Mathias Agopian3f844832013-08-07 21:24:32 -070092}
93
94size_t Mesh::getStride() const {
95 return mStride;
96}
97
Peiyong Lin833074a2018-08-28 11:53:54 -070098} // namespace renderengine
99} // namespace android