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 BufferProducer and ProducerQueue.
45     int id = -1;
46     int type = -1;
47     size_t consumer_count = 0;
48 
49     // Data field for buffer producer.
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     size_t pending_count = 0;
56     uint64_t state = 0;
57     uint64_t signaled_mask = 0;
58     uint64_t index = 0;
59 
60     // Data filed for producer queue.
61     size_t capacity = 0;
62     UsagePolicy usage_policy{0, 0, 0, 0};
63 
BufferInfoBufferInfo64     BufferInfo(int id, size_t consumer_count, uint32_t width, uint32_t height,
65                uint32_t layer_count, uint32_t format, uint64_t usage,
66                size_t pending_count, uint64_t state, uint64_t signaled_mask,
67                uint64_t index)
68         : id(id),
69           type(kProducerType),
70           consumer_count(consumer_count),
71           width(width),
72           height(height),
73           layer_count(layer_count),
74           format(format),
75           usage(usage),
76           pending_count(pending_count),
77           state(state),
78           signaled_mask(signaled_mask),
79           index(index) {}
80 
BufferInfoBufferInfo81     BufferInfo(int id, size_t consumer_count, size_t capacity,
82                const UsagePolicy& usage_policy)
83         : id(id),
84           type(kProducerQueueType),
85           consumer_count(consumer_count),
86           capacity(capacity),
87           usage_policy(usage_policy) {}
88 
BufferInfoBufferInfo89     BufferInfo() {}
90   };
91 
92   // Returns the buffer info for this buffer.
93   virtual BufferInfo GetBufferInfo() const = 0;
94 
95   // Signal the client fd that an ownership change occurred using POLLIN.
96   void SignalAvailable();
97 
98   // Clear the ownership change event.
99   void ClearAvailable();
100 
101   // Signal hangup event.
102   void Hangup();
103 
service()104   BufferHubService* service() const { return service_; }
channel_type()105   ChannelType channel_type() const { return channel_type_; }
buffer_id()106   int buffer_id() const { return buffer_id_; }
107 
channel_id()108   int channel_id() const { return channel_id_; }
109 
signaled()110   bool signaled() const { return signaled_; }
111 
112  private:
113   BufferHubService* service_;
114 
115   // Static id of the buffer for logging and informational purposes. This id
116   // does not change for the life of the buffer.
117   // TODO(eieio): Consider using an id allocator instead of the originating
118   // channel id; channel ids wrap after 2^31 ids, but this is not a problem in
119   // general because channel ids are not used for any lookup in this service.
120   int buffer_id_;
121 
122   // The channel id of the buffer.
123   int channel_id_;
124 
125   bool signaled_;
126 
127   ChannelType channel_type_;
128 
129   BufferHubChannel(const BufferHubChannel&) = delete;
130   void operator=(const BufferHubChannel&) = delete;
131 };
132 
133 class BufferHubService : public pdx::ServiceBase<BufferHubService> {
134  public:
135   BufferHubService();
136   ~BufferHubService() override;
137 
138   pdx::Status<void> HandleMessage(pdx::Message& message) override;
139   void HandleImpulse(pdx::Message& message) override;
140 
141   bool IsInitialized() const override;
142   std::string DumpState(size_t max_length) override;
143 
144  private:
145   friend BASE;
146 
147   pdx::Status<void> OnCreateBuffer(pdx::Message& message, uint32_t width,
148                                    uint32_t height, uint32_t format,
149                                    uint64_t usage, size_t meta_size_bytes);
150   pdx::Status<void> OnCreateDetachedBuffer(pdx::Message& message,
151                                            uint32_t width, uint32_t height,
152                                            uint32_t layer_count,
153                                            uint32_t format, uint64_t usage,
154                                            size_t user_metadata_size);
155   pdx::Status<QueueInfo> OnCreateProducerQueue(
156       pdx::Message& message, const ProducerQueueConfig& producer_config,
157       const UsagePolicy& usage_policy);
158 
159   BufferHubService(const BufferHubService&) = delete;
160   void operator=(const BufferHubService&) = delete;
161 };
162 
163 }  // namespace dvr
164 }  // namespace android
165 
166 #endif  // ANDROID_DVR_BUFFERHUBD_BUFFER_HUB_H_
167