Home | History | Annotate | Download | only in dvr
      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, uint64_t usage,
     46                          uint64_t* outBufferAge,
     47                          FrameEventHistoryDelta* outTimestamps) override;
     48 
     49   // See |IGraphicBufferProducer::detachBuffer|
     50   status_t detachBuffer(int slot) override;
     51 
     52   // See |IGraphicBufferProducer::detachNextBuffer|
     53   status_t detachNextBuffer(sp<GraphicBuffer>* out_buffer,
     54                             sp<Fence>* out_fence) override;
     55 
     56   // See |IGraphicBufferProducer::attachBuffer|
     57   status_t attachBuffer(int* out_slot,
     58                         const sp<GraphicBuffer>& buffer) override;
     59 
     60   // See |IGraphicBufferProducer::queueBuffer|
     61   status_t queueBuffer(int slot, const QueueBufferInput& input,
     62                        QueueBufferOutput* output) override;
     63 
     64   // See |IGraphicBufferProducer::cancelBuffer|
     65   status_t cancelBuffer(int slot, const sp<Fence>& fence) override;
     66 
     67   // See |IGraphicBufferProducer::query|
     68   status_t query(int what, int* out_value) override;
     69 
     70   // See |IGraphicBufferProducer::connect|
     71   status_t connect(const sp<IProducerListener>& listener, int api,
     72                    bool producer_controlled_by_app,
     73                    QueueBufferOutput* output) override;
     74 
     75   // See |IGraphicBufferProducer::disconnect|
     76   status_t disconnect(int api,
     77                       DisconnectMode mode = DisconnectMode::Api) override;
     78 
     79   // See |IGraphicBufferProducer::setSidebandStream|
     80   status_t setSidebandStream(const sp<NativeHandle>& stream) override;
     81 
     82   // See |IGraphicBufferProducer::allocateBuffers|
     83   void allocateBuffers(uint32_t width, uint32_t height, PixelFormat format,
     84                        uint64_t usage) override;
     85 
     86   // See |IGraphicBufferProducer::allowAllocation|
     87   status_t allowAllocation(bool allow) override;
     88 
     89   // See |IGraphicBufferProducer::setGenerationNumber|
     90   status_t setGenerationNumber(uint32_t generation_number) override;
     91 
     92   // See |IGraphicBufferProducer::getConsumerName|
     93   String8 getConsumerName() const override;
     94 
     95   // See |IGraphicBufferProducer::setSharedBufferMode|
     96   status_t setSharedBufferMode(bool shared_buffer_mode) override;
     97 
     98   // See |IGraphicBufferProducer::setAutoRefresh|
     99   status_t setAutoRefresh(bool auto_refresh) override;
    100 
    101   // See |IGraphicBufferProducer::setDequeueTimeout|
    102   status_t setDequeueTimeout(nsecs_t timeout) override;
    103 
    104   // See |IGraphicBufferProducer::getLastQueuedBuffer|
    105   status_t getLastQueuedBuffer(sp<GraphicBuffer>* out_buffer,
    106                                sp<Fence>* out_fence,
    107                                float out_transform_matrix[16]) override;
    108 
    109   // See |IGraphicBufferProducer::getFrameTimestamps|
    110   void getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) override;
    111 
    112   // See |IGraphicBufferProducer::getUniqueId|
    113   status_t getUniqueId(uint64_t* out_id) const override;
    114 
    115   // See |IGraphicBufferProducer::getConsumerUsage|
    116   status_t getConsumerUsage(uint64_t* out_usage) const override;
    117 
    118  private:
    119   using LocalHandle = pdx::LocalHandle;
    120 
    121   // Private constructor to force use of |Create|.
    122   BufferHubQueueProducer() {}
    123 
    124   static uint64_t genUniqueId() {
    125     static std::atomic<uint32_t> counter{0};
    126     static uint64_t id = static_cast<uint64_t>(getpid()) << 32;
    127     return id | counter++;
    128   }
    129 
    130   // Allocate new buffer through BufferHub and add it into |queue_| for
    131   // bookkeeping.
    132   status_t AllocateBuffer(uint32_t width, uint32_t height, uint32_t layer_count,
    133                           PixelFormat format, uint64_t usage);
    134 
    135   // Remove a buffer via BufferHubRPC.
    136   status_t RemoveBuffer(size_t slot);
    137 
    138   // Free all buffers which are owned by the prodcuer. Note that if graphic
    139   // buffers are acquired by the consumer, we can't .
    140   status_t FreeAllBuffers();
    141 
    142   // Concreate implementation backed by BufferHubBuffer.
    143   std::shared_ptr<ProducerQueue> queue_;
    144 
    145   // Mutex for thread safety.
    146   std::mutex mutex_;
    147 
    148   // Connect client API, should be one of the NATIVE_WINDOW_API_* flags.
    149   int connected_api_{kNoConnectedApi};
    150 
    151   // |max_buffer_count_| sets the capacity of the underlying buffer queue.
    152   int32_t max_buffer_count_{BufferHubQueue::kMaxQueueCapacity};
    153 
    154   // |max_dequeued_buffer_count_| set the maximum number of buffers that can
    155   // be dequeued at the same momment.
    156   int32_t max_dequeued_buffer_count_{1};
    157 
    158   // Sets how long dequeueBuffer or attachBuffer will block if a buffer or
    159   // slot is not yet available. The timeout is stored in milliseconds.
    160   int dequeue_timeout_ms_{BufferHubQueue::kNoTimeOut};
    161 
    162   // |generation_number_| stores the current generation number of the attached
    163   // producer. Any attempt to attach a buffer with a different generation
    164   // number will fail.
    165   // TOOD(b/38137191) Currently not used as we don't support
    166   // IGraphicBufferProducer::detachBuffer.
    167   uint32_t generation_number_{0};
    168 
    169   // |buffers_| stores the buffers that have been dequeued from
    170   // |dvr::BufferHubQueue|, It is initialized to invalid buffers, and gets
    171   // filled in with the result of |Dequeue|.
    172   // TODO(jwcai) The buffer allocated to a slot will also be replaced if the
    173   // requested buffer usage or geometry differs from that of the buffer
    174   // allocated to a slot.
    175   struct BufferHubSlot : public BufferSlot {
    176     BufferHubSlot() : mBufferProducer(nullptr), mIsReallocating(false) {}
    177     // BufferSlot comes from android framework, using m prefix to comply with
    178     // the name convention with the reset of data fields from BufferSlot.
    179     std::shared_ptr<BufferProducer> mBufferProducer;
    180     bool mIsReallocating;
    181   };
    182   BufferHubSlot buffers_[BufferHubQueue::kMaxQueueCapacity];
    183 
    184   // A uniqueId used by IGraphicBufferProducer interface.
    185   const uint64_t unique_id_{genUniqueId()};
    186 };
    187 
    188 }  // namespace dvr
    189 }  // namespace android
    190 
    191 #endif  // ANDROID_DVR_BUFFER_HUB_QUEUE_PRODUCER_H_
    192