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