• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
dvrWriteBufferCreateEmpty(DvrWriteBuffer ** write_buffer)41 void dvrWriteBufferCreateEmpty(DvrWriteBuffer** write_buffer) {
42   if (write_buffer)
43     *write_buffer = new DvrWriteBuffer;
44 }
45 
dvrWriteBufferDestroy(DvrWriteBuffer * write_buffer)46 void dvrWriteBufferDestroy(DvrWriteBuffer* write_buffer) {
47   if (write_buffer != nullptr) {
48     ALOGW_IF(
49         write_buffer->slot != -1,
50         "dvrWriteBufferDestroy: Destroying a buffer associated with a valid "
51         "buffer queue slot. This may indicate possible leaks.");
52     delete write_buffer;
53   }
54 }
55 
dvrWriteBufferIsValid(DvrWriteBuffer * write_buffer)56 int dvrWriteBufferIsValid(DvrWriteBuffer* write_buffer) {
57   return write_buffer && write_buffer->write_buffer;
58 }
59 
dvrWriteBufferClear(DvrWriteBuffer * write_buffer)60 int dvrWriteBufferClear(DvrWriteBuffer* write_buffer) {
61   if (!write_buffer)
62     return -EINVAL;
63 
64   write_buffer->write_buffer = nullptr;
65   return 0;
66 }
67 
dvrWriteBufferGetId(DvrWriteBuffer * write_buffer)68 int dvrWriteBufferGetId(DvrWriteBuffer* write_buffer) {
69   if (!write_buffer || !write_buffer->write_buffer)
70     return -EINVAL;
71 
72   return write_buffer->write_buffer->id();
73 }
74 
dvrWriteBufferGetAHardwareBuffer(DvrWriteBuffer * write_buffer,AHardwareBuffer ** hardware_buffer)75 int dvrWriteBufferGetAHardwareBuffer(DvrWriteBuffer* write_buffer,
76                                      AHardwareBuffer** hardware_buffer) {
77   if (!write_buffer || !write_buffer->write_buffer)
78     return -EINVAL;
79 
80   return ConvertToAHardwareBuffer(
81       write_buffer->write_buffer->buffer()->buffer().get(), hardware_buffer);
82 }
83 
dvrWriteBufferPost(DvrWriteBuffer * write_buffer,int ready_fence_fd,const void * meta,size_t meta_size_bytes)84 int dvrWriteBufferPost(DvrWriteBuffer* write_buffer, int ready_fence_fd,
85                        const void* meta, size_t meta_size_bytes) {
86   if (!write_buffer || !write_buffer->write_buffer)
87     return -EINVAL;
88 
89   pdx::LocalHandle fence(ready_fence_fd);
90   int result = write_buffer->write_buffer->Post(fence, meta, meta_size_bytes);
91   return result;
92 }
93 
dvrWriteBufferGain(DvrWriteBuffer * write_buffer,int * release_fence_fd)94 int dvrWriteBufferGain(DvrWriteBuffer* write_buffer, int* release_fence_fd) {
95   if (!write_buffer || !write_buffer->write_buffer || !release_fence_fd)
96     return -EINVAL;
97 
98   pdx::LocalHandle release_fence;
99   int result = write_buffer->write_buffer->Gain(&release_fence);
100   *release_fence_fd = release_fence.Release();
101   return result;
102 }
103 
dvrWriteBufferGainAsync(DvrWriteBuffer * write_buffer)104 int dvrWriteBufferGainAsync(DvrWriteBuffer* write_buffer) {
105   if (!write_buffer || !write_buffer->write_buffer)
106     return -EINVAL;
107 
108   return write_buffer->write_buffer->GainAsync();
109 }
110 
dvrReadBufferCreateEmpty(DvrReadBuffer ** read_buffer)111 void dvrReadBufferCreateEmpty(DvrReadBuffer** read_buffer) {
112   if (read_buffer)
113     *read_buffer = new DvrReadBuffer;
114 }
115 
dvrReadBufferDestroy(DvrReadBuffer * read_buffer)116 void dvrReadBufferDestroy(DvrReadBuffer* read_buffer) {
117   if (read_buffer != nullptr) {
118     ALOGW_IF(
119         read_buffer->slot != -1,
120         "dvrReadBufferDestroy: Destroying a buffer associated with a valid "
121         "buffer queue slot. This may indicate possible leaks.");
122     delete read_buffer;
123   }
124 }
125 
dvrReadBufferIsValid(DvrReadBuffer * read_buffer)126 int dvrReadBufferIsValid(DvrReadBuffer* read_buffer) {
127   return read_buffer && read_buffer->read_buffer;
128 }
129 
dvrReadBufferClear(DvrReadBuffer * read_buffer)130 int dvrReadBufferClear(DvrReadBuffer* read_buffer) {
131   if (!read_buffer)
132     return -EINVAL;
133 
134   read_buffer->read_buffer = nullptr;
135   return 0;
136 }
137 
dvrReadBufferGetId(DvrReadBuffer * read_buffer)138 int dvrReadBufferGetId(DvrReadBuffer* read_buffer) {
139   if (!read_buffer || !read_buffer->read_buffer)
140     return -EINVAL;
141 
142   return read_buffer->read_buffer->id();
143 }
144 
dvrReadBufferGetAHardwareBuffer(DvrReadBuffer * read_buffer,AHardwareBuffer ** hardware_buffer)145 int dvrReadBufferGetAHardwareBuffer(DvrReadBuffer* read_buffer,
146                                     AHardwareBuffer** hardware_buffer) {
147   if (!read_buffer || !read_buffer->read_buffer)
148     return -EINVAL;
149 
150   return ConvertToAHardwareBuffer(
151       read_buffer->read_buffer->buffer()->buffer().get(), hardware_buffer);
152 }
153 
dvrReadBufferAcquire(DvrReadBuffer * read_buffer,int * ready_fence_fd,void * meta,size_t meta_size_bytes)154 int dvrReadBufferAcquire(DvrReadBuffer* read_buffer, int* ready_fence_fd,
155                          void* meta, size_t meta_size_bytes) {
156   if (!read_buffer || !read_buffer->read_buffer)
157     return -EINVAL;
158 
159   pdx::LocalHandle ready_fence;
160   int result =
161       read_buffer->read_buffer->Acquire(&ready_fence, meta, meta_size_bytes);
162   *ready_fence_fd = ready_fence.Release();
163   return result;
164 }
165 
dvrReadBufferRelease(DvrReadBuffer * read_buffer,int release_fence_fd)166 int dvrReadBufferRelease(DvrReadBuffer* read_buffer, int release_fence_fd) {
167   if (!read_buffer || !read_buffer->read_buffer)
168     return -EINVAL;
169 
170   pdx::LocalHandle fence(release_fence_fd);
171   int result = read_buffer->read_buffer->Release(fence);
172   return result;
173 }
174 
dvrReadBufferReleaseAsync(DvrReadBuffer * read_buffer)175 int dvrReadBufferReleaseAsync(DvrReadBuffer* read_buffer) {
176   if (!read_buffer || !read_buffer->read_buffer)
177     return -EINVAL;
178 
179   return read_buffer->read_buffer->ReleaseAsync();
180 }
181 
dvrBufferDestroy(DvrBuffer * buffer)182 void dvrBufferDestroy(DvrBuffer* buffer) { delete buffer; }
183 
dvrBufferGetAHardwareBuffer(DvrBuffer * buffer,AHardwareBuffer ** hardware_buffer)184 int dvrBufferGetAHardwareBuffer(DvrBuffer* buffer,
185                                 AHardwareBuffer** hardware_buffer) {
186   if (!buffer || !buffer->buffer || !hardware_buffer) {
187     return -EINVAL;
188   }
189 
190   return ConvertToAHardwareBuffer(buffer->buffer->buffer().get(),
191                                   hardware_buffer);
192 }
193 
194 // Retrieve the shared buffer layout version defined in dvr_shared_buffers.h.
dvrBufferGlobalLayoutVersionGet()195 int dvrBufferGlobalLayoutVersionGet() {
196   return android::dvr::kSharedBufferLayoutVersion;
197 }
198 
dvrWriteBufferGetNativeHandle(DvrWriteBuffer * write_buffer)199 const struct native_handle* dvrWriteBufferGetNativeHandle(
200     DvrWriteBuffer* write_buffer) {
201   if (!write_buffer || !write_buffer->write_buffer)
202     return nullptr;
203 
204   return write_buffer->write_buffer->native_handle();
205 }
206 
dvrReadBufferGetNativeHandle(DvrReadBuffer * read_buffer)207 const struct native_handle* dvrReadBufferGetNativeHandle(
208     DvrReadBuffer* read_buffer) {
209   if (!read_buffer || !read_buffer->read_buffer)
210     return nullptr;
211 
212   return read_buffer->read_buffer->native_handle();
213 }
214 
dvrBufferGetNativeHandle(DvrBuffer * buffer)215 const struct native_handle* dvrBufferGetNativeHandle(DvrBuffer* buffer) {
216   if (!buffer || !buffer->buffer)
217     return nullptr;
218 
219   return buffer->buffer->handle();
220 }
221 
222 }  // extern "C"
223