• 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 "buffer_hub.h"
5 
6 #include <functional>
7 #include <memory>
8 #include <vector>
9 
10 #include <pdx/channel_handle.h>
11 #include <pdx/file_handle.h>
12 #include <pdx/rpc/buffer_wrapper.h>
13 #include <private/dvr/bufferhub_rpc.h>
14 #include <private/dvr/ion_buffer.h>
15 
16 namespace android {
17 namespace dvr {
18 
19 // The buffer changes ownership according to the following sequence:
20 // POST -> ACQUIRE/RELEASE (all consumers) -> GAIN (producer acquires) -> POST
21 
22 // The producer channel is owned by a single app that writes into buffers and
23 // calls POST when drawing is complete. This channel has a set of consumer
24 // channels associated with it that are waiting for notifications.
25 class ProducerChannel : public BufferHubChannel {
26  public:
27   using Message = pdx::Message;
28   using BorrowedHandle = pdx::BorrowedHandle;
29   using RemoteChannelHandle = pdx::RemoteChannelHandle;
30   template <typename T>
31   using BufferWrapper = pdx::rpc::BufferWrapper<T>;
32 
33   static pdx::Status<std::shared_ptr<ProducerChannel>> Create(
34       BufferHubService* service, int channel_id, uint32_t width,
35       uint32_t height, uint32_t layer_count, uint32_t format, uint64_t usage,
36       size_t user_metadata_size);
37 
38   ~ProducerChannel() override;
39 
40   bool HandleMessage(Message& message) override;
41   void HandleImpulse(Message& message) override;
42 
43   BufferInfo GetBufferInfo() const override;
44 
45   BufferDescription<BorrowedHandle> GetBuffer(uint64_t buffer_state_bit);
46 
47   pdx::Status<RemoteChannelHandle> CreateConsumer(Message& message);
48   pdx::Status<RemoteChannelHandle> OnNewConsumer(Message& message);
49 
50   pdx::Status<LocalFence> OnConsumerAcquire(Message& message);
51   pdx::Status<void> OnConsumerRelease(Message& message,
52                                       LocalFence release_fence);
53 
54   void OnConsumerIgnored();
55   void OnConsumerOrphaned(ConsumerChannel* channel);
56 
57   void AddConsumer(ConsumerChannel* channel);
58   void RemoveConsumer(ConsumerChannel* channel);
59 
60   bool CheckAccess(int euid, int egid);
61   bool CheckParameters(uint32_t width, uint32_t height, uint32_t layer_count,
62                        uint32_t format, uint64_t usage,
63                        size_t user_metadata_size);
64 
65   pdx::Status<void> OnProducerMakePersistent(Message& message,
66                                              const std::string& name,
67                                              int user_id, int group_id);
68   pdx::Status<void> OnRemovePersistence(Message& message);
69 
70  private:
71   std::vector<ConsumerChannel*> consumer_channels_;
72   // This counts the number of consumers left to process this buffer. If this is
73   // zero then the producer can re-acquire ownership.
74   int pending_consumers_;
75 
76   IonBuffer buffer_;
77 
78   // IonBuffer that is shared between bufferhubd, producer, and consumers.
79   IonBuffer metadata_buffer_;
80   BufferHubDefs::MetadataHeader* metadata_header_ = nullptr;
81   std::atomic<uint64_t>* buffer_state_ = nullptr;
82   std::atomic<uint64_t>* fence_state_ = nullptr;
83 
84   // All active consumer bits. Valid bits are the lower 63 bits, while the
85   // highest bit is reserved for the producer and should not be set.
86   uint64_t active_consumer_bit_mask_{0ULL};
87   // All orphaned consumer bits. Valid bits are the lower 63 bits, while the
88   // highest bit is reserved for the producer and should not be set.
89   uint64_t orphaned_consumer_bit_mask_{0ULL};
90 
91   bool producer_owns_;
92   LocalFence post_fence_;
93   LocalFence returned_fence_;
94   size_t user_metadata_size_;  // size of user requested buffer buffer size.
95   size_t metadata_buf_size_;  // size of the ion buffer that holds metadata.
96 
97   pdx::LocalHandle acquire_fence_fd_;
98   pdx::LocalHandle release_fence_fd_;
99   pdx::LocalHandle dummy_fence_fd_;
100 
101   static constexpr int kNoCheckId = -1;
102   static constexpr int kUseCallerId = 0;
103   static constexpr int kRootId = 0;
104 
105   // User and group id to check when obtaining a persistent buffer.
106   int owner_user_id_ = kNoCheckId;
107   int owner_group_id_ = kNoCheckId;
108 
109   std::string name_;
110 
111   ProducerChannel(BufferHubService* service, int channel, uint32_t width,
112                   uint32_t height, uint32_t layer_count, uint32_t format,
113                   uint64_t usage, size_t user_metadata_size, int* error);
114 
115   pdx::Status<BufferDescription<BorrowedHandle>> OnGetBuffer(Message& message);
116   pdx::Status<void> OnProducerPost(Message& message, LocalFence acquire_fence);
117   pdx::Status<LocalFence> OnProducerGain(Message& message);
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