GraphicBufferAllocator.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. **
  3. ** Copyright 2009, The Android Open Source Project
  4. **
  5. ** Licensed under the Apache License, Version 2.0 (the "License");
  6. ** you may not use this file except in compliance with the License.
  7. ** You may obtain a copy of the License at
  8. **
  9. ** http://www.apache.org/licenses/LICENSE-2.0
  10. **
  11. ** Unless required by applicable law or agreed to in writing, software
  12. ** distributed under the License is distributed on an "AS IS" BASIS,
  13. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. ** See the License for the specific language governing permissions and
  15. ** limitations under the License.
  16. */
  17. #ifndef ANDROID_BUFFER_ALLOCATOR_H
  18. #define ANDROID_BUFFER_ALLOCATOR_H
  19. #include <stdint.h>
  20. #include <cutils/native_handle.h>
  21. #include <utils/Errors.h>
  22. #include <utils/KeyedVector.h>
  23. #include <utils/threads.h>
  24. #include <utils/Singleton.h>
  25. #include <ui/PixelFormat.h>
  26. #include <hardware/gralloc.h>
  27. namespace android {
  28. // ---------------------------------------------------------------------------
  29. class String8;
  30. class GraphicBufferAllocator : public Singleton<GraphicBufferAllocator>
  31. {
  32. public:
  33. enum {
  34. USAGE_SW_READ_NEVER = GRALLOC_USAGE_SW_READ_NEVER,
  35. USAGE_SW_READ_RARELY = GRALLOC_USAGE_SW_READ_RARELY,
  36. USAGE_SW_READ_OFTEN = GRALLOC_USAGE_SW_READ_OFTEN,
  37. USAGE_SW_READ_MASK = GRALLOC_USAGE_SW_READ_MASK,
  38. USAGE_SW_WRITE_NEVER = GRALLOC_USAGE_SW_WRITE_NEVER,
  39. USAGE_SW_WRITE_RARELY = GRALLOC_USAGE_SW_WRITE_RARELY,
  40. USAGE_SW_WRITE_OFTEN = GRALLOC_USAGE_SW_WRITE_OFTEN,
  41. USAGE_SW_WRITE_MASK = GRALLOC_USAGE_SW_WRITE_MASK,
  42. USAGE_SOFTWARE_MASK = USAGE_SW_READ_MASK|USAGE_SW_WRITE_MASK,
  43. USAGE_HW_TEXTURE = GRALLOC_USAGE_HW_TEXTURE,
  44. USAGE_HW_RENDER = GRALLOC_USAGE_HW_RENDER,
  45. USAGE_HW_2D = GRALLOC_USAGE_HW_2D,
  46. USAGE_HW_MASK = GRALLOC_USAGE_HW_MASK
  47. };
  48. static inline GraphicBufferAllocator& get() { return getInstance(); }
  49. status_t alloc(uint32_t w, uint32_t h, PixelFormat format, uint32_t usage,
  50. buffer_handle_t* handle, uint32_t* stride);
  51. status_t free(buffer_handle_t handle);
  52. void dump(String8& res) const;
  53. static void dumpToSystemLog();
  54. private:
  55. struct alloc_rec_t {
  56. uint32_t width;
  57. uint32_t height;
  58. uint32_t stride;
  59. PixelFormat format;
  60. uint32_t usage;
  61. size_t size;
  62. };
  63. static Mutex sLock;
  64. static KeyedVector<buffer_handle_t, alloc_rec_t> sAllocList;
  65. friend class Singleton<GraphicBufferAllocator>;
  66. GraphicBufferAllocator();
  67. ~GraphicBufferAllocator();
  68. alloc_device_t *mAllocDev;
  69. };
  70. // ---------------------------------------------------------------------------
  71. }; // namespace android
  72. #endif // ANDROID_BUFFER_ALLOCATOR_H