blob: f5387f28ea7ba79b3f813ac45a7e5ffa7ac42d9b [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
Lucas Dupin1b6531c2018-07-05 17:18:21 -070036 const size_t CROP_COORD_SIZE = 2;
37 size_t stride = vertexSize + texCoordSize + CROP_COORD_SIZE;
Dan Stozaab79e332015-04-29 13:30:31 -070038 size_t remainder = (stride * vertexCount) / vertexCount;
39 // Since all of the input parameters are unsigned, if stride is less than
40 // either vertexSize or texCoordSize, it must have overflowed. remainder
41 // will be equal to stride as long as stride * vertexCount doesn't overflow.
42 if ((stride < vertexSize) || (remainder != stride)) {
Lucas Dupin1b6531c2018-07-05 17:18:21 -070043 ALOGE("Overflow in Mesh(..., %zu, %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize,
44 CROP_COORD_SIZE);
Chia-I Wud3b13cb2018-09-13 13:31:26 -070045 mVertices.resize(1);
Dan Stozaab79e332015-04-29 13:30:31 -070046 mVertices[0] = 0.0f;
47 mVertexCount = 0;
48 mVertexSize = 0;
49 mTexCoordsSize = 0;
50 mStride = 0;
51 return;
52 }
53
Chia-I Wud3b13cb2018-09-13 13:31:26 -070054 mVertices.resize(stride * vertexCount);
Dan Stozaab79e332015-04-29 13:30:31 -070055 mStride = stride;
Mathias Agopian3f844832013-08-07 21:24:32 -070056}
57
Mathias Agopian3f844832013-08-07 21:24:32 -070058Mesh::Primitive Mesh::getPrimitive() const {
59 return mPrimitive;
60}
61
Mathias Agopian5cdc8992013-08-13 20:51:23 -070062float const* Mesh::getPositions() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070063 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070064}
Mathias Agopian5cdc8992013-08-13 20:51:23 -070065float* Mesh::getPositions() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070066 return mVertices.data();
Mathias Agopian3f844832013-08-07 21:24:32 -070067}
68
69float const* Mesh::getTexCoords() const {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070070 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070071}
72float* Mesh::getTexCoords() {
Chia-I Wud3b13cb2018-09-13 13:31:26 -070073 return mVertices.data() + mVertexSize;
Mathias Agopian3f844832013-08-07 21:24:32 -070074}
75
Lucas Dupin1b6531c2018-07-05 17:18:21 -070076float const* Mesh::getCropCoords() const {
77 return mVertices.data() + mVertexSize + mTexCoordsSize;
78}
79float* Mesh::getCropCoords() {
80 return mVertices.data() + mVertexSize + mTexCoordsSize;
81}
82
Mathias Agopian3f844832013-08-07 21:24:32 -070083size_t Mesh::getVertexCount() const {
84 return mVertexCount;
85}
86
87size_t Mesh::getVertexSize() const {
88 return mVertexSize;
89}
90
91size_t Mesh::getTexCoordsSize() const {
92 return mTexCoordsSize;
93}
94
95size_t Mesh::getByteStride() const {
Chia-I Wub027f802017-11-29 14:00:52 -080096 return mStride * sizeof(float);
Mathias Agopian3f844832013-08-07 21:24:32 -070097}
98
99size_t Mesh::getStride() const {
100 return mStride;
101}
102
Peiyong Lin46080ef2018-10-26 18:43:14 -0700103} // namespace renderengine
104} // namespace android