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