1 #ifndef ANDROID_DVR_BUFFER_QUEUE_INTERNAL_H_
2 #define ANDROID_DVR_BUFFER_QUEUE_INTERNAL_H_
3 
4 #include <gui/Surface.h>
5 #include <private/dvr/buffer_hub_queue_client.h>
6 #include <sys/cdefs.h>
7 
8 #include <array>
9 #include <memory>
10 
11 #include "dvr_internal.h"
12 
13 struct ANativeWindow;
14 
15 typedef struct DvrNativeBufferMetadata DvrNativeBufferMetadata;
16 typedef struct DvrReadBuffer DvrReadBuffer;
17 typedef struct DvrReadBufferQueue DvrReadBufferQueue;
18 typedef struct DvrWriteBuffer DvrWriteBuffer;
19 typedef void (*DvrReadBufferQueueBufferAvailableCallback)(void* context);
20 typedef void (*DvrReadBufferQueueBufferRemovedCallback)(DvrReadBuffer* buffer,
21                                                         void* context);
22 
23 struct DvrWriteBufferQueue {
24   using BufferHubQueue = android::dvr::BufferHubQueue;
25   using ProducerQueue = android::dvr::ProducerQueue;
26 
27   // Create a concrete object for DvrWriteBufferQueue.
28   //
29   // @param producer_queue The BufferHub's ProducerQueue that is used to back
30   //     this DvrWriteBufferQueue, must not be NULL.
31   explicit DvrWriteBufferQueue(
32       const std::shared_ptr<ProducerQueue>& producer_queue);
33 
idDvrWriteBufferQueue34   int id() const { return producer_queue_->id(); }
widthDvrWriteBufferQueue35   uint32_t width() const { return width_; };
heightDvrWriteBufferQueue36   uint32_t height() const { return height_; };
formatDvrWriteBufferQueue37   uint32_t format() const { return format_; };
capacityDvrWriteBufferQueue38   size_t capacity() const { return producer_queue_->capacity(); }
producer_queueDvrWriteBufferQueue39   const std::shared_ptr<ProducerQueue>& producer_queue() const {
40     return producer_queue_;
41   }
42 
43   int GetNativeWindow(ANativeWindow** out_window);
44   int CreateReadQueue(DvrReadBufferQueue** out_read_queue);
45   int Dequeue(int timeout, DvrWriteBuffer* write_buffer, int* out_fence_fd);
46   int GainBuffer(int timeout, DvrWriteBuffer** out_write_buffer,
47                  DvrNativeBufferMetadata* out_meta, int* out_fence_fd);
48   int PostBuffer(DvrWriteBuffer* write_buffer,
49                  const DvrNativeBufferMetadata* meta, int ready_fence_fd);
50   int ResizeBuffer(uint32_t width, uint32_t height);
51 
52  private:
53   std::shared_ptr<ProducerQueue> producer_queue_;
54   std::array<std::unique_ptr<DvrWriteBuffer>, BufferHubQueue::kMaxQueueCapacity>
55       write_buffers_;
56 
57   int64_t next_post_index_ = 0;
58   uint32_t width_;
59   uint32_t height_;
60   uint32_t format_;
61 
62   android::sp<android::Surface> native_window_;
63 };
64 
65 struct DvrReadBufferQueue {
66   using BufferHubQueue = android::dvr::BufferHubQueue;
67   using ConsumerQueue = android::dvr::ConsumerQueue;
68 
69   explicit DvrReadBufferQueue(
70       const std::shared_ptr<ConsumerQueue>& consumer_queue);
71 
idDvrReadBufferQueue72   int id() const { return consumer_queue_->id(); }
event_fdDvrReadBufferQueue73   int event_fd() const { return consumer_queue_->queue_fd(); }
capacityDvrReadBufferQueue74   size_t capacity() const { return consumer_queue_->capacity(); }
75 
76   int CreateReadQueue(DvrReadBufferQueue** out_read_queue);
77   int Dequeue(int timeout, DvrReadBuffer* read_buffer, int* out_fence_fd,
78               void* out_meta, size_t user_metadata_size);
79   int AcquireBuffer(int timeout, DvrReadBuffer** out_read_buffer,
80                     DvrNativeBufferMetadata* out_meta, int* out_fence_fd);
81   int ReleaseBuffer(DvrReadBuffer* read_buffer,
82                     const DvrNativeBufferMetadata* meta, int release_fence_fd);
83   void SetBufferAvailableCallback(
84       DvrReadBufferQueueBufferAvailableCallback callback, void* context);
85   void SetBufferRemovedCallback(
86       DvrReadBufferQueueBufferRemovedCallback callback, void* context);
87   int HandleEvents();
88 
89  private:
90   std::shared_ptr<ConsumerQueue> consumer_queue_;
91   std::array<std::unique_ptr<DvrReadBuffer>, BufferHubQueue::kMaxQueueCapacity>
92       read_buffers_;
93 };
94 
95 #endif  // ANDROID_DVR_BUFFER_QUEUE_INTERNAL_H_
96