1 /*
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 
17 #include <renderengine/Mesh.h>
18 
19 #include <utils/Log.h>
20 
21 namespace android {
22 namespace renderengine {
23 
Mesh(Primitive primitive,size_t vertexCount,size_t vertexSize,size_t texCoordSize)24 Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
25       : mVertexCount(vertexCount),
26         mVertexSize(vertexSize),
27         mTexCoordsSize(texCoordSize),
28         mPrimitive(primitive) {
29     if (vertexCount == 0) {
30         mVertices.resize(1);
31         mVertices[0] = 0.0f;
32         mStride = 0;
33         return;
34     }
35 
36     const size_t CROP_COORD_SIZE = 2;
37     size_t stride = vertexSize + texCoordSize + CROP_COORD_SIZE;
38     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)) {
43         ALOGE("Overflow in Mesh(..., %zu, %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize,
44               CROP_COORD_SIZE);
45         mVertices.resize(1);
46         mVertices[0] = 0.0f;
47         mVertexCount = 0;
48         mVertexSize = 0;
49         mTexCoordsSize = 0;
50         mStride = 0;
51         return;
52     }
53 
54     mVertices.resize(stride * vertexCount);
55     mStride = stride;
56 }
57 
getPrimitive() const58 Mesh::Primitive Mesh::getPrimitive() const {
59     return mPrimitive;
60 }
61 
getPositions() const62 float const* Mesh::getPositions() const {
63     return mVertices.data();
64 }
getPositions()65 float* Mesh::getPositions() {
66     return mVertices.data();
67 }
68 
getTexCoords() const69 float const* Mesh::getTexCoords() const {
70     return mVertices.data() + mVertexSize;
71 }
getTexCoords()72 float* Mesh::getTexCoords() {
73     return mVertices.data() + mVertexSize;
74 }
75 
getCropCoords() const76 float const* Mesh::getCropCoords() const {
77     return mVertices.data() + mVertexSize + mTexCoordsSize;
78 }
getCropCoords()79 float* Mesh::getCropCoords() {
80     return mVertices.data() + mVertexSize + mTexCoordsSize;
81 }
82 
getVertexCount() const83 size_t Mesh::getVertexCount() const {
84     return mVertexCount;
85 }
86 
getVertexSize() const87 size_t Mesh::getVertexSize() const {
88     return mVertexSize;
89 }
90 
getTexCoordsSize() const91 size_t Mesh::getTexCoordsSize() const {
92     return mTexCoordsSize;
93 }
94 
getByteStride() const95 size_t Mesh::getByteStride() const {
96     return mStride * sizeof(float);
97 }
98 
getStride() const99 size_t Mesh::getStride() const {
100     return mStride;
101 }
102 
103 } // namespace renderengine
104 } // namespace android
105