Mesh.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #include <renderengine/Mesh.h>
  17. #include <utils/Log.h>
  18. namespace android {
  19. namespace renderengine {
  20. Mesh::Mesh(Primitive primitive, size_t vertexCount, size_t vertexSize, size_t texCoordSize)
  21. : mVertexCount(vertexCount),
  22. mVertexSize(vertexSize),
  23. mTexCoordsSize(texCoordSize),
  24. mPrimitive(primitive) {
  25. if (vertexCount == 0) {
  26. mVertices.resize(1);
  27. mVertices[0] = 0.0f;
  28. mStride = 0;
  29. return;
  30. }
  31. const size_t CROP_COORD_SIZE = 2;
  32. size_t stride = vertexSize + texCoordSize + CROP_COORD_SIZE;
  33. size_t remainder = (stride * vertexCount) / vertexCount;
  34. // Since all of the input parameters are unsigned, if stride is less than
  35. // either vertexSize or texCoordSize, it must have overflowed. remainder
  36. // will be equal to stride as long as stride * vertexCount doesn't overflow.
  37. if ((stride < vertexSize) || (remainder != stride)) {
  38. ALOGE("Overflow in Mesh(..., %zu, %zu, %zu, %zu)", vertexCount, vertexSize, texCoordSize,
  39. CROP_COORD_SIZE);
  40. mVertices.resize(1);
  41. mVertices[0] = 0.0f;
  42. mVertexCount = 0;
  43. mVertexSize = 0;
  44. mTexCoordsSize = 0;
  45. mStride = 0;
  46. return;
  47. }
  48. mVertices.resize(stride * vertexCount);
  49. mStride = stride;
  50. }
  51. Mesh::Primitive Mesh::getPrimitive() const {
  52. return mPrimitive;
  53. }
  54. float const* Mesh::getPositions() const {
  55. return mVertices.data();
  56. }
  57. float* Mesh::getPositions() {
  58. return mVertices.data();
  59. }
  60. float const* Mesh::getTexCoords() const {
  61. return mVertices.data() + mVertexSize;
  62. }
  63. float* Mesh::getTexCoords() {
  64. return mVertices.data() + mVertexSize;
  65. }
  66. float const* Mesh::getCropCoords() const {
  67. return mVertices.data() + mVertexSize + mTexCoordsSize;
  68. }
  69. float* Mesh::getCropCoords() {
  70. return mVertices.data() + mVertexSize + mTexCoordsSize;
  71. }
  72. size_t Mesh::getVertexCount() const {
  73. return mVertexCount;
  74. }
  75. size_t Mesh::getVertexSize() const {
  76. return mVertexSize;
  77. }
  78. size_t Mesh::getTexCoordsSize() const {
  79. return mTexCoordsSize;
  80. }
  81. size_t Mesh::getByteStride() const {
  82. return mStride * sizeof(float);
  83. }
  84. size_t Mesh::getStride() const {
  85. return mStride;
  86. }
  87. } // namespace renderengine
  88. } // namespace android