1 #ifndef ANDROID_DVR_BUFFERHUBD_BUFFER_HUB_H_
2 #define ANDROID_DVR_BUFFERHUBD_BUFFER_HUB_H_
3 
4 #include <memory>
5 #include <string>
6 #include <unordered_map>
7 
8 #include <hardware/gralloc.h>
9 #include <pdx/service.h>
10 #include <private/dvr/bufferhub_rpc.h>
11 
12 namespace android {
13 namespace dvr {
14 
15 class BufferHubService;
16 class ConsumerChannel;
17 class ProducerChannel;
18 class ConsumerQueueChannel;
19 class ProducerQueueChannel;
20 
21 class BufferHubChannel : public pdx::Channel {
22  public:
23   enum ChannelType {
24     kProducerType,
25     kConsumerType,
26     kDetachedBufferType,
27     kProducerQueueType,
28     kConsumerQueueType,
29   };
30 
BufferHubChannel(BufferHubService * service,int buffer_id,int channel_id,ChannelType channel_type)31   BufferHubChannel(BufferHubService* service, int buffer_id, int channel_id,
32                    ChannelType channel_type)
33       : service_(service),
34         buffer_id_(buffer_id),
35         channel_id_(channel_id),
36         channel_type_(channel_type) {}
~BufferHubChannel()37   virtual ~BufferHubChannel() {}
38 
39   virtual bool HandleMessage(pdx::Message& message) = 0;
40   virtual void HandleImpulse(pdx::Message& message) = 0;
41 
42   // Captures buffer info for use by BufferHubService::DumpState().
43   struct BufferInfo {
44     // Common data field shared by ProducerBuffer and ProducerQueue.
45     int id = -1;
46     int type = -1;
47     size_t consumer_count = 0;
48 
49     // Data field for producer buffer.
50     uint32_t width = 0;
51     uint32_t height = 0;
52     uint32_t layer_count = 0;
53     uint32_t format = 0;
54     uint64_t usage = 0;
55     uint64_t state = 0;
56     uint64_t signaled_mask = 0;
57     uint64_t index = 0;
58 
59     // Data filed for producer queue.
60     size_t capacity = 0;
61     UsagePolicy usage_policy{0, 0, 0, 0};
62 
BufferInfoBufferInfo63     BufferInfo(int id, size_t consumer_count, uint32_t width, uint32_t height,
64                uint32_t layer_count, uint32_t format, uint64_t usage,
65                uint64_t state, uint64_t signaled_mask, uint64_t index)
66         : id(id),
67           type(kProducerType),
68           consumer_count(consumer_count),
69           width(width),
70           height(height),
71           layer_count(layer_count),
72           format(format),
73           usage(usage),
74           state(state),
75           signaled_mask(signaled_mask),
76           index(index) {}
77 
BufferInfoBufferInfo78     BufferInfo(int id, size_t consumer_count, size_t capacity,
79                const UsagePolicy& usage_policy)
80         : id(id),
81           type(kProducerQueueType),
82           consumer_count(consumer_count),
83           capacity(capacity),
84           usage_policy(usage_policy) {}
85 
BufferInfoBufferInfo86     BufferInfo() {}
87   };
88 
89   // Returns the buffer info for this buffer.
90   virtual BufferInfo GetBufferInfo() const = 0;
91 
92   // Signal the client fd that an ownership change occurred using POLLIN.
93   void SignalAvailable();
94 
95   // Clear the ownership change event.
96   void ClearAvailable();
97 
98   // Signal hangup event.
99   void Hangup();
100 
service()101   BufferHubService* service() const { return service_; }
channel_type()102   ChannelType channel_type() const { return channel_type_; }
buffer_id()103   int buffer_id() const { return buffer_id_; }
104 
channel_id()105   int channel_id() const { return channel_id_; }
106 
signaled()107   bool signaled() const { return signaled_; }
108 
109  private:
110   BufferHubService* service_;
111 
112   // Static id of the buffer for logging and informational purposes. This id
113   // does not change for the life of the buffer.
114   // TODO(eieio): Consider using an id allocator instead of the originating
115   // channel id; channel ids wrap after 2^31 ids, but this is not a problem in
116   // general because channel ids are not used for any lookup in this service.
117   int buffer_id_;
118 
119   // The channel id of the buffer.
120   int channel_id_;
121 
122   bool signaled_;
123 
124   ChannelType channel_type_;
125 
126   BufferHubChannel(const BufferHubChannel&) = delete;
127   void operator=(const BufferHubChannel&) = delete;
128 };
129 
130 class BufferHubService : public pdx::ServiceBase<BufferHubService> {
131  public:
132   BufferHubService();
133   ~BufferHubService() override;
134 
135   pdx::Status<void> HandleMessage(pdx::Message& message) override;
136   void HandleImpulse(pdx::Message& message) override;
137 
138   bool IsInitialized() const override;
139   std::string DumpState(size_t max_length) override;
140 
141  private:
142   friend BASE;
143 
144   pdx::Status<void> OnCreateBuffer(pdx::Message& message, uint32_t width,
145                                    uint32_t height, uint32_t format,
146                                    uint64_t usage, size_t meta_size_bytes);
147   pdx::Status<QueueInfo> OnCreateProducerQueue(
148       pdx::Message& message, const ProducerQueueConfig& producer_config,
149       const UsagePolicy& usage_policy);
150 
151   BufferHubService(const BufferHubService&) = delete;
152   void operator=(const BufferHubService&) = delete;
153 };
154 
155 }  // namespace dvr
156 }  // namespace android
157 
158 #endif  // ANDROID_DVR_BUFFERHUBD_BUFFER_HUB_H_
159