• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef ANDROID_DVR_BUFFERHUBD_PRODUCER_CHANNEL_H_
2 #define ANDROID_DVR_BUFFERHUBD_PRODUCER_CHANNEL_H_
3 
4 #include <functional>
5 #include <memory>
6 #include <vector>
7 
8 #include <pdx/channel_handle.h>
9 #include <pdx/file_handle.h>
10 #include <pdx/rpc/buffer_wrapper.h>
11 #include <private/dvr/buffer_hub.h>
12 #include <private/dvr/bufferhub_rpc.h>
13 #include <private/dvr/ion_buffer.h>
14 
15 namespace android {
16 namespace dvr {
17 
18 // The buffer changes ownership according to the following sequence:
19 // POST -> ACQUIRE/RELEASE (all consumers) -> GAIN (producer acquires) -> POST
20 
21 // The producer channel is owned by a single app that writes into buffers and
22 // calls POST when drawing is complete. This channel has a set of consumer
23 // channels associated with it that are waiting for notifications.
24 class ProducerChannel : public BufferHubChannel {
25  public:
26   using Message = pdx::Message;
27   using BorrowedHandle = pdx::BorrowedHandle;
28   using RemoteChannelHandle = pdx::RemoteChannelHandle;
29   template <typename T>
30   using BufferWrapper = pdx::rpc::BufferWrapper<T>;
31 
32   static std::unique_ptr<ProducerChannel> Create(BufferHubService* service,
33                                                  int buffer_id, int channel_id,
34                                                  IonBuffer buffer,
35                                                  IonBuffer metadata_buffer,
36                                                  size_t user_metadata_size);
37 
38   static pdx::Status<std::shared_ptr<ProducerChannel>> Create(
39       BufferHubService* service, int channel_id, uint32_t width,
40       uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage,
41       size_t user_metadata_size);
42 
43   ~ProducerChannel() override;
44 
buffer_state()45   uint32_t buffer_state() const {
46     return buffer_state_->load(std::memory_order_acquire);
47   }
48 
49   bool HandleMessage(Message& message) override;
50   void HandleImpulse(Message& message) override;
51 
52   BufferInfo GetBufferInfo() const override;
53 
54   BufferDescription<BorrowedHandle> GetBuffer(uint32_t client_state_mask);
55 
56   pdx::Status<RemoteChannelHandle> CreateConsumer(Message& message,
57                                                   uint32_t consumer_state_mask);
58   pdx::Status<uint32_t> CreateConsumerStateMask();
59   pdx::Status<RemoteChannelHandle> OnNewConsumer(Message& message);
60 
61   pdx::Status<LocalFence> OnConsumerAcquire(Message& message);
62   pdx::Status<void> OnConsumerRelease(Message& message,
63                                       LocalFence release_fence);
64 
65   void OnConsumerOrphaned(const uint32_t& consumer_state_mask);
66 
67   void AddConsumer(ConsumerChannel* channel);
68   void RemoveConsumer(ConsumerChannel* channel);
69 
70   bool CheckParameters(uint32_t width, uint32_t height, uint32_t layer_count,
71                        uint32_t format, uint64_t usage,
72                        size_t user_metadata_size) const;
73 
74  private:
75   std::vector<ConsumerChannel*> consumer_channels_;
76 
77   IonBuffer buffer_;
78 
79   // IonBuffer that is shared between bufferhubd, producer, and consumers.
80   IonBuffer metadata_buffer_;
81   BufferHubDefs::MetadataHeader* metadata_header_ = nullptr;
82   std::atomic<uint32_t>* buffer_state_ = nullptr;
83   std::atomic<uint32_t>* fence_state_ = nullptr;
84   std::atomic<uint32_t>* active_clients_bit_mask_ = nullptr;
85 
86   // All orphaned consumer bits. Valid bits are the lower 63 bits, while the
87   // highest bit is reserved for the producer and should not be set.
88   uint32_t orphaned_consumer_bit_mask_{0U};
89 
90   LocalFence post_fence_;
91   LocalFence returned_fence_;
92   size_t user_metadata_size_;  // size of user requested buffer buffer size.
93   size_t metadata_buf_size_;   // size of the ion buffer that holds metadata.
94 
95   pdx::LocalHandle acquire_fence_fd_;
96   pdx::LocalHandle release_fence_fd_;
97   pdx::LocalHandle dummy_fence_fd_;
98 
99   ProducerChannel(BufferHubService* service, int buffer_id, int channel_id,
100                   IonBuffer buffer, IonBuffer metadata_buffer,
101                   size_t user_metadata_size, int* error);
102   ProducerChannel(BufferHubService* service, int channel, uint32_t width,
103                   uint32_t height, uint32_t layer_count, uint32_t format,
104                   uint64_t usage, size_t user_metadata_size, int* error);
105 
106   int InitializeBuffer();
107   pdx::Status<BufferDescription<BorrowedHandle>> OnGetBuffer(Message& message);
108   pdx::Status<void> OnProducerPost(Message& message, LocalFence acquire_fence);
109   pdx::Status<LocalFence> OnProducerGain(Message& message);
110 
111   // Remove consumer from atomics in shared memory based on consumer_state_mask.
112   // This function is used for clean up for failures in CreateConsumer method.
113   void RemoveConsumerClientMask(uint32_t consumer_state_mask);
114 
115   // Checks whether the buffer is released by all active clients, excluding
116   // orphaned consumers.
117   bool IsBufferReleasedByAllActiveClientsExceptForOrphans() const;
118 
119   ProducerChannel(const ProducerChannel&) = delete;
120   void operator=(const ProducerChannel&) = delete;
121 };
122 
123 }  // namespace dvr
124 }  // namespace android
125 
126 #endif  // ANDROID_DVR_BUFFERHUBD_PRODUCER_CHANNEL_H_
127