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,size_t cropCoordsSize,size_t shadowColorSize,size_t shadowParamsSize,size_t indexCount)24 Mesh::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)
27       : mVertexCount(vertexCount),
28         mVertexSize(vertexSize),
29         mTexCoordsSize(texCoordSize),
30         mCropCoordsSize(cropCoordsSize),
31         mShadowColorSize(shadowColorSize),
32         mShadowParamsSize(shadowParamsSize),
33         mPrimitive(primitive),
34         mIndexCount(indexCount) {
35     if (vertexCount == 0) {
36         mVertices.resize(1);
37         mVertices[0] = 0.0f;
38         mStride = 0;
39         return;
40     }
41     size_t stride = vertexSize + texCoordSize + cropCoordsSize + shadowColorSize + shadowParamsSize;
42     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)) {
47         ALOGE("Overflow in Mesh(..., %zu, %zu, %zu, %zu, %zu, %zu)", vertexCount, vertexSize,
48               texCoordSize, cropCoordsSize, shadowColorSize, shadowParamsSize);
49         mVertices.resize(1);
50         mVertices[0] = 0.0f;
51         mVertexCount = 0;
52         mVertexSize = 0;
53         mTexCoordsSize = 0;
54         mCropCoordsSize = 0;
55         mShadowColorSize = 0;
56         mShadowParamsSize = 0;
57         mStride = 0;
58         return;
59     }
60 
61     mVertices.resize(stride * vertexCount);
62     mStride = stride;
63     mIndices.resize(indexCount);
64 }
65 
getPrimitive() const66 Mesh::Primitive Mesh::getPrimitive() const {
67     return mPrimitive;
68 }
69 
getPositions() const70 float const* Mesh::getPositions() const {
71     return mVertices.data();
72 }
getPositions()73 float* Mesh::getPositions() {
74     return mVertices.data();
75 }
76 
getTexCoords() const77 float const* Mesh::getTexCoords() const {
78     return mVertices.data() + mVertexSize;
79 }
getTexCoords()80 float* Mesh::getTexCoords() {
81     return mVertices.data() + mVertexSize;
82 }
83 
getCropCoords() const84 float const* Mesh::getCropCoords() const {
85     return mVertices.data() + mVertexSize + mTexCoordsSize;
86 }
getCropCoords()87 float* Mesh::getCropCoords() {
88     return mVertices.data() + mVertexSize + mTexCoordsSize;
89 }
90 
getShadowColor() const91 float const* Mesh::getShadowColor() const {
92     return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize;
93 }
getShadowColor()94 float* Mesh::getShadowColor() {
95     return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize;
96 }
97 
getShadowParams() const98 float const* Mesh::getShadowParams() const {
99     return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize + mShadowColorSize;
100 }
getShadowParams()101 float* Mesh::getShadowParams() {
102     return mVertices.data() + mVertexSize + mTexCoordsSize + mCropCoordsSize + mShadowColorSize;
103 }
104 
getIndices() const105 uint16_t const* Mesh::getIndices() const {
106     return mIndices.data();
107 }
108 
getIndices()109 uint16_t* Mesh::getIndices() {
110     return mIndices.data();
111 }
112 
getVertexCount() const113 size_t Mesh::getVertexCount() const {
114     return mVertexCount;
115 }
116 
getVertexSize() const117 size_t Mesh::getVertexSize() const {
118     return mVertexSize;
119 }
120 
getTexCoordsSize() const121 size_t Mesh::getTexCoordsSize() const {
122     return mTexCoordsSize;
123 }
124 
getShadowColorSize() const125 size_t Mesh::getShadowColorSize() const {
126     return mShadowColorSize;
127 }
128 
getShadowParamsSize() const129 size_t Mesh::getShadowParamsSize() const {
130     return mShadowParamsSize;
131 }
132 
getByteStride() const133 size_t Mesh::getByteStride() const {
134     return mStride * sizeof(float);
135 }
136 
getStride() const137 size_t Mesh::getStride() const {
138     return mStride;
139 }
140 
getIndexCount() const141 size_t Mesh::getIndexCount() const {
142     return mIndexCount;
143 }
144 
145 } // namespace renderengine
146 } // namespace android
147