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