Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 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 | |
Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 17 | #include <renderengine/Mesh.h> |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 18 | |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 21 | namespace android { |
Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 22 | namespace renderengine { |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 23 | |
| 24 | Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize) |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 25 | : mVertexCount(vertexCount), |
| 26 | mVertexSize(vertexSize), |
| 27 | mTexCoordsSize(texCoordSize), |
| 28 | mPrimitive(primitive) { |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 29 | if (vertexCount == 0) { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 30 | mVertices.resize(1); |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 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 Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 42 | ALOGE("Overflow in Mesh(..., %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize); |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 43 | mVertices.resize(1); |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 44 | mVertices[0] = 0.0f; |
| 45 | mVertexCount = 0; |
| 46 | mVertexSize = 0; |
| 47 | mTexCoordsSize = 0; |
| 48 | mStride = 0; |
| 49 | return; |
| 50 | } |
| 51 | |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 52 | mVertices.resize(stride * vertexCount); |
Dan Stoza | ab79e33 | 2015-04-29 13:30:31 -0700 | [diff] [blame] | 53 | mStride = stride; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 56 | Mesh::Primitive Mesh::getPrimitive() const { |
| 57 | return mPrimitive; |
| 58 | } |
| 59 | |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 60 | float const* Mesh::getPositions() const { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 61 | return mVertices.data(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 62 | } |
Mathias Agopian | 5cdc899 | 2013-08-13 20:51:23 -0700 | [diff] [blame] | 63 | float* Mesh::getPositions() { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 64 | return mVertices.data(); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | float const* Mesh::getTexCoords() const { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 68 | return mVertices.data() + mVertexSize; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 69 | } |
| 70 | float* Mesh::getTexCoords() { |
Chia-I Wu | d3b13cb | 2018-09-13 13:31:26 -0700 | [diff] [blame] | 71 | return mVertices.data() + mVertexSize; |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 74 | size_t Mesh::getVertexCount() const { |
| 75 | return mVertexCount; |
| 76 | } |
| 77 | |
| 78 | size_t Mesh::getVertexSize() const { |
| 79 | return mVertexSize; |
| 80 | } |
| 81 | |
| 82 | size_t Mesh::getTexCoordsSize() const { |
| 83 | return mTexCoordsSize; |
| 84 | } |
| 85 | |
| 86 | size_t Mesh::getByteStride() const { |
Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 87 | return mStride * sizeof(float); |
Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | size_t Mesh::getStride() const { |
| 91 | return mStride; |
| 92 | } |
| 93 | |
Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame^] | 94 | } // namespace renderengine |
| 95 | } // namespace android |