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 meta_size_bytes);
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   pdx::Status<NativeBufferHandle<BorrowedHandle>> OnGetBuffer(Message& message);
46 
47   pdx::Status<RemoteChannelHandle> CreateConsumer(Message& message);
48   pdx::Status<RemoteChannelHandle> OnNewConsumer(Message& message);
49 
50   pdx::Status<std::pair<BorrowedFence, BufferWrapper<std::uint8_t*>>>
51   OnConsumerAcquire(Message& message, std::size_t metadata_size);
52   pdx::Status<void> OnConsumerRelease(Message& message,
53                                       LocalFence release_fence);
54 
55   void OnConsumerIgnored();
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, size_t meta_size_bytes);
63 
64   pdx::Status<void> OnProducerMakePersistent(Message& message,
65                                              const std::string& name,
66                                              int user_id, int group_id);
67   pdx::Status<void> OnRemovePersistence(Message& message);
68 
69  private:
70   std::vector<ConsumerChannel*> consumer_channels_;
71   // This counts the number of consumers left to process this buffer. If this is
72   // zero then the producer can re-acquire ownership.
73   int pending_consumers_;
74 
75   IonBuffer buffer_;
76 
77   bool producer_owns_;
78   LocalFence post_fence_;
79   LocalFence returned_fence_;
80   size_t meta_size_bytes_;
81   std::unique_ptr<uint8_t[]> meta_;
82 
83   static constexpr int kNoCheckId = -1;
84   static constexpr int kUseCallerId = 0;
85   static constexpr int kRootId = 0;
86 
87   // User and group id to check when obtaining a persistent buffer.
88   int owner_user_id_ = kNoCheckId;
89   int owner_group_id_ = kNoCheckId;
90 
91   std::string name_;
92 
93   ProducerChannel(BufferHubService* service, int channel, uint32_t width,
94                   uint32_t height, uint32_t layer_count, uint32_t format,
95                   uint64_t usage, size_t meta_size_bytes, int* error);
96 
97   pdx::Status<void> OnProducerPost(
98       Message& message, LocalFence acquire_fence,
99       BufferWrapper<std::vector<std::uint8_t>> metadata);
100   pdx::Status<LocalFence> OnProducerGain(Message& message);
101 
102   ProducerChannel(const ProducerChannel&) = delete;
103   void operator=(const ProducerChannel&) = delete;
104 };
105 
106 }  // namespace dvr
107 }  // namespace android
108 
109 #endif  // ANDROID_DVR_BUFFERHUBD_PRODUCER_CHANNEL_H_
110