dvr_buffer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "include/dvr/dvr_buffer.h"
  2. #include <android/hardware_buffer.h>
  3. #include <dvr/dvr_shared_buffers.h>
  4. #include <private/dvr/consumer_buffer.h>
  5. #include <private/dvr/producer_buffer.h>
  6. #include <ui/GraphicBuffer.h>
  7. #include "dvr_internal.h"
  8. using namespace android;
  9. namespace android {
  10. namespace dvr {
  11. DvrBuffer* CreateDvrBufferFromIonBuffer(
  12. const std::shared_ptr<IonBuffer>& ion_buffer) {
  13. if (!ion_buffer)
  14. return nullptr;
  15. return new DvrBuffer{std::move(ion_buffer)};
  16. }
  17. } // namespace dvr
  18. } // namespace android
  19. namespace {
  20. int ConvertToAHardwareBuffer(GraphicBuffer* graphic_buffer,
  21. AHardwareBuffer** hardware_buffer) {
  22. if (!hardware_buffer || !graphic_buffer) {
  23. return -EINVAL;
  24. }
  25. *hardware_buffer = reinterpret_cast<AHardwareBuffer*>(graphic_buffer);
  26. AHardwareBuffer_acquire(*hardware_buffer);
  27. return 0;
  28. }
  29. } // anonymous namespace
  30. extern "C" {
  31. void dvrWriteBufferDestroy(DvrWriteBuffer* write_buffer) {
  32. if (write_buffer != nullptr) {
  33. ALOGW_IF(
  34. write_buffer->slot != -1,
  35. "dvrWriteBufferDestroy: Destroying a buffer associated with a valid "
  36. "buffer queue slot. This may indicate possible leaks, buffer_id=%d.",
  37. dvrWriteBufferGetId(write_buffer));
  38. delete write_buffer;
  39. }
  40. }
  41. int dvrWriteBufferIsValid(DvrWriteBuffer* write_buffer) {
  42. return write_buffer && write_buffer->write_buffer;
  43. }
  44. int dvrWriteBufferGetId(DvrWriteBuffer* write_buffer) {
  45. if (!write_buffer || !write_buffer->write_buffer)
  46. return -EINVAL;
  47. return write_buffer->write_buffer->id();
  48. }
  49. int dvrWriteBufferGetAHardwareBuffer(DvrWriteBuffer* write_buffer,
  50. AHardwareBuffer** hardware_buffer) {
  51. if (!write_buffer || !write_buffer->write_buffer)
  52. return -EINVAL;
  53. return ConvertToAHardwareBuffer(
  54. write_buffer->write_buffer->buffer()->buffer().get(), hardware_buffer);
  55. }
  56. void dvrReadBufferDestroy(DvrReadBuffer* read_buffer) {
  57. if (read_buffer != nullptr) {
  58. ALOGW_IF(
  59. read_buffer->slot != -1,
  60. "dvrReadBufferDestroy: Destroying a buffer associated with a valid "
  61. "buffer queue slot. This may indicate possible leaks, buffer_id=%d.",
  62. dvrReadBufferGetId(read_buffer));
  63. delete read_buffer;
  64. }
  65. }
  66. int dvrReadBufferIsValid(DvrReadBuffer* read_buffer) {
  67. return read_buffer && read_buffer->read_buffer;
  68. }
  69. int dvrReadBufferGetId(DvrReadBuffer* read_buffer) {
  70. if (!read_buffer || !read_buffer->read_buffer)
  71. return -EINVAL;
  72. return read_buffer->read_buffer->id();
  73. }
  74. int dvrReadBufferGetAHardwareBuffer(DvrReadBuffer* read_buffer,
  75. AHardwareBuffer** hardware_buffer) {
  76. if (!read_buffer || !read_buffer->read_buffer)
  77. return -EINVAL;
  78. return ConvertToAHardwareBuffer(
  79. read_buffer->read_buffer->buffer()->buffer().get(), hardware_buffer);
  80. }
  81. void dvrBufferDestroy(DvrBuffer* buffer) { delete buffer; }
  82. int dvrBufferGetAHardwareBuffer(DvrBuffer* buffer,
  83. AHardwareBuffer** hardware_buffer) {
  84. if (!buffer || !buffer->buffer || !hardware_buffer) {
  85. return -EINVAL;
  86. }
  87. return ConvertToAHardwareBuffer(buffer->buffer->buffer().get(),
  88. hardware_buffer);
  89. }
  90. // Retrieve the shared buffer layout version defined in dvr_shared_buffers.h.
  91. int dvrBufferGlobalLayoutVersionGet() {
  92. return android::dvr::kSharedBufferLayoutVersion;
  93. }
  94. } // extern "C"