BufferObjectManager.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. ** Copyright 2008, 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 <stdint.h>
  17. #include <stddef.h>
  18. #include <sys/types.h>
  19. #include <utils/Atomic.h>
  20. #include <utils/RefBase.h>
  21. #include <utils/KeyedVector.h>
  22. #include <utils/Errors.h>
  23. #include <GLES/gl.h>
  24. #include "BufferObjectManager.h"
  25. namespace android {
  26. using namespace gl;
  27. // ----------------------------------------------------------------------------
  28. EGLBufferObjectManager::EGLBufferObjectManager()
  29. : TokenManager(), mCount(0)
  30. {
  31. }
  32. EGLBufferObjectManager::~EGLBufferObjectManager()
  33. {
  34. // destroy all the buffer objects and their storage
  35. GLsizei n = mBuffers.size();
  36. for (GLsizei i=0 ; i<n ; i++) {
  37. buffer_t* bo = mBuffers.valueAt(i);
  38. free(bo->data);
  39. delete bo;
  40. }
  41. }
  42. buffer_t const* EGLBufferObjectManager::bind(GLuint buffer)
  43. {
  44. Mutex::Autolock _l(mLock);
  45. int32_t i = mBuffers.indexOfKey(buffer);
  46. if (i >= 0) {
  47. return mBuffers.valueAt(i);
  48. }
  49. buffer_t* bo = new buffer_t;
  50. bo->data = 0;
  51. bo->usage = GL_STATIC_DRAW;
  52. bo->size = 0;
  53. bo->name = buffer;
  54. mBuffers.add(buffer, bo);
  55. return bo;
  56. }
  57. int EGLBufferObjectManager::allocateStore(buffer_t* bo,
  58. GLsizeiptr size, GLenum usage)
  59. {
  60. Mutex::Autolock _l(mLock);
  61. if (size != bo->size) {
  62. uint8_t* data = (uint8_t*)malloc(size);
  63. if (data == 0)
  64. return -1;
  65. free(bo->data);
  66. bo->data = data;
  67. bo->size = size;
  68. }
  69. bo->usage = usage;
  70. return 0;
  71. }
  72. void EGLBufferObjectManager::deleteBuffers(GLsizei n, const GLuint* buffers)
  73. {
  74. Mutex::Autolock _l(mLock);
  75. while (n--) {
  76. const GLuint t = *buffers++;
  77. if (t) {
  78. int32_t index = mBuffers.indexOfKey(t);
  79. if (index >= 0) {
  80. buffer_t* bo = mBuffers.valueAt(index);
  81. free(bo->data);
  82. mBuffers.removeItemsAt(index);
  83. delete bo;
  84. }
  85. }
  86. }
  87. }
  88. // ----------------------------------------------------------------------------
  89. }; // namespace android