• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ANDROID_DVR_BUFFER_HUB_QUEUE_PRODUCER_H_
2 #define ANDROID_DVR_BUFFER_HUB_QUEUE_PRODUCER_H_
3 
4 #include <gui/IGraphicBufferProducer.h>
5 #include <private/dvr/buffer_hub_queue_client.h>
6 
7 namespace android {
8 namespace dvr {
9 
10 class BufferHubQueueProducer : public BnGraphicBufferProducer {
11  public:
12   static constexpr int kNoConnectedApi = -1;
13 
14   // TODO(b/36187402) The actual implementation of BufferHubQueue's consumer
15   // side logic doesn't limit the number of buffer it can acquire
16   // simultaneously. We need a way for consumer logic to configure and enforce
17   // that.
18   static constexpr int kDefaultUndequeuedBuffers = 1;
19 
20   // Create a BufferHubQueueProducer instance by creating a new producer queue.
21   static sp<BufferHubQueueProducer> Create();
22 
23   // Create a BufferHubQueueProducer instance by importing an existing prodcuer
24   // queue.
25   static sp<BufferHubQueueProducer> Create(
26       const std::shared_ptr<ProducerQueue>& producer);
27 
28   // See |IGraphicBufferProducer::requestBuffer|
29   status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) override;
30 
31   // For the BufferHub based implementation. All buffers in the queue are
32   // allowed to be dequeued from the consumer side. It call always returns
33   // 0 for |NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS| query. Thus setting
34   // |max_dequeued_buffers| here can be considered the same as setting queue
35   // capacity.
36   //
37   // See |IGraphicBufferProducer::setMaxDequeuedBufferCount| for more info
38   status_t setMaxDequeuedBufferCount(int max_dequeued_buffers) override;
39 
40   // See |IGraphicBufferProducer::setAsyncMode|
41   status_t setAsyncMode(bool async) override;
42 
43   // See |IGraphicBufferProducer::dequeueBuffer|
44   status_t dequeueBuffer(int* out_slot, sp<Fence>* out_fence, uint32_t width,
45                          uint32_t height, PixelFormat format, uint32_t usage,
46                          FrameEventHistoryDelta* outTimestamps) override;
47 
48   // See |IGraphicBufferProducer::detachBuffer|
49   status_t detachBuffer(int slot) override;
50 
51   // See |IGraphicBufferProducer::detachNextBuffer|
52   status_t detachNextBuffer(sp<GraphicBuffer>* out_buffer,
53                             sp<Fence>* out_fence) override;
54 
55   // See |IGraphicBufferProducer::attachBuffer|
56   status_t attachBuffer(int* out_slot,
57                         const sp<GraphicBuffer>& buffer) override;
58 
59   // See |IGraphicBufferProducer::queueBuffer|
60   status_t queueBuffer(int slot, const QueueBufferInput& input,
61                        QueueBufferOutput* output) override;
62 
63   // See |IGraphicBufferProducer::cancelBuffer|
64   status_t cancelBuffer(int slot, const sp<Fence>& fence) override;
65 
66   // See |IGraphicBufferProducer::query|
67   status_t query(int what, int* out_value) override;
68 
69   // See |IGraphicBufferProducer::connect|
70   status_t connect(const sp<IProducerListener>& listener, int api,
71                    bool producer_controlled_by_app,
72                    QueueBufferOutput* output) override;
73 
74   // See |IGraphicBufferProducer::disconnect|
75   status_t disconnect(int api,
76                       DisconnectMode mode = DisconnectMode::Api) override;
77 
78   // See |IGraphicBufferProducer::setSidebandStream|
79   status_t setSidebandStream(const sp<NativeHandle>& stream) override;
80 
81   // See |IGraphicBufferProducer::allocateBuffers|
82   void allocateBuffers(uint32_t width, uint32_t height, PixelFormat format,
83                        uint32_t usage) override;
84 
85   // See |IGraphicBufferProducer::allowAllocation|
86   status_t allowAllocation(bool allow) override;
87 
88   // See |IGraphicBufferProducer::setGenerationNumber|
89   status_t setGenerationNumber(uint32_t generation_number) override;
90 
91   // See |IGraphicBufferProducer::getConsumerName|
92   String8 getConsumerName() const override;
93 
94   // See |IGraphicBufferProducer::setSharedBufferMode|
95   status_t setSharedBufferMode(bool shared_buffer_mode) override;
96 
97   // See |IGraphicBufferProducer::setAutoRefresh|
98   status_t setAutoRefresh(bool auto_refresh) override;
99 
100   // See |IGraphicBufferProducer::setDequeueTimeout|
101   status_t setDequeueTimeout(nsecs_t timeout) override;
102 
103   // See |IGraphicBufferProducer::getLastQueuedBuffer|
104   status_t getLastQueuedBuffer(sp<GraphicBuffer>* out_buffer,
105                                sp<Fence>* out_fence,
106                                float out_transform_matrix[16]) override;
107 
108   // See |IGraphicBufferProducer::getFrameTimestamps|
109   void getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) override;
110 
111   // See |IGraphicBufferProducer::getUniqueId|
112   status_t getUniqueId(uint64_t* out_id) const override;
113 
114  private:
115   using LocalHandle = pdx::LocalHandle;
116 
117   // Private constructor to force use of |Create|.
BufferHubQueueProducer()118   BufferHubQueueProducer() {}
119 
genUniqueId()120   static uint64_t genUniqueId() {
121     static std::atomic<uint32_t> counter{0};
122     static uint64_t id = static_cast<uint64_t>(getpid()) << 32;
123     return id | counter++;
124   }
125 
126   // Allocate new buffer through BufferHub and add it into |queue_| for
127   // bookkeeping.
128   status_t AllocateBuffer(uint32_t width, uint32_t height, uint32_t layer_count,
129                           PixelFormat format, uint64_t usage);
130 
131   // Remove a buffer via BufferHubRPC.
132   status_t RemoveBuffer(size_t slot);
133 
134   // Concreate implementation backed by BufferHubBuffer.
135   std::shared_ptr<ProducerQueue> queue_;
136 
137   // Mutex for thread safety.
138   std::mutex mutex_;
139 
140   // Connect client API, should be one of the NATIVE_WINDOW_API_* flags.
141   int connected_api_{kNoConnectedApi};
142 
143   // |max_buffer_count_| sets the capacity of the underlying buffer queue.
144   int32_t max_buffer_count_{BufferHubQueue::kMaxQueueCapacity};
145 
146   // |max_dequeued_buffer_count_| set the maximum number of buffers that can
147   // be dequeued at the same momment.
148   int32_t max_dequeued_buffer_count_{1};
149 
150   // Sets how long dequeueBuffer or attachBuffer will block if a buffer or
151   // slot is not yet available. The timeout is stored in milliseconds.
152   int dequeue_timeout_ms_{BufferHubQueue::kNoTimeOut};
153 
154   // |generation_number_| stores the current generation number of the attached
155   // producer. Any attempt to attach a buffer with a different generation
156   // number will fail.
157   // TOOD(b/38137191) Currently not used as we don't support
158   // IGraphicBufferProducer::detachBuffer.
159   uint32_t generation_number_{0};
160 
161   // |buffers_| stores the buffers that have been dequeued from
162   // |dvr::BufferHubQueue|, It is initialized to invalid buffers, and gets
163   // filled in with the result of |Dequeue|.
164   // TODO(jwcai) The buffer allocated to a slot will also be replaced if the
165   // requested buffer usage or geometry differs from that of the buffer
166   // allocated to a slot.
167   struct BufferHubSlot : public BufferSlot {
BufferHubSlotBufferHubSlot168     BufferHubSlot() : mBufferProducer(nullptr), mIsReallocating(false) {}
169     // BufferSlot comes from android framework, using m prefix to comply with
170     // the name convention with the reset of data fields from BufferSlot.
171     std::shared_ptr<BufferProducer> mBufferProducer;
172     bool mIsReallocating;
173   };
174   BufferHubSlot buffers_[BufferHubQueue::kMaxQueueCapacity];
175 
176   // A uniqueId used by IGraphicBufferProducer interface.
177   const uint64_t unique_id_{genUniqueId()};
178 };
179 
180 }  // namespace dvr
181 }  // namespace android
182 
183 #endif  // ANDROID_DVR_BUFFER_HUB_QUEUE_PRODUCER_H_
184