| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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> | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 10 | #include <private/dvr/bufferhub_rpc.h> | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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, | 
| Jiwen 'Steve' Cai | 23c1a73 | 2018-03-12 12:16:47 -0700 | [diff] [blame] | 26 |     kDetachedBufferType, | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 27 |     kProducerQueueType, | 
 | 28 |     kConsumerQueueType, | 
 | 29 |   }; | 
 | 30 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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) {} | 
 | 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. | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 50 |     uint32_t width = 0; | 
 | 51 |     uint32_t height = 0; | 
| Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 52 |     uint32_t layer_count = 0; | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 53 |     uint32_t format = 0; | 
| Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 54 |     uint64_t usage = 0; | 
| Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 55 |     size_t pending_count = 0; | 
 | 56 |     uint64_t state = 0; | 
 | 57 |     uint64_t signaled_mask = 0; | 
 | 58 |     uint64_t index = 0; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 59 |  | 
 | 60 |     // Data filed for producer queue. | 
 | 61 |     size_t capacity = 0; | 
| Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 62 |     UsagePolicy usage_policy{0, 0, 0, 0}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 63 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 64 |     BufferInfo(int id, size_t consumer_count, uint32_t width, uint32_t height, | 
| Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 65 |                uint32_t layer_count, uint32_t format, uint64_t usage, | 
 | 66 |                size_t pending_count, uint64_t state, uint64_t signaled_mask, | 
| Jiwen 'Steve' Cai | 2f26033 | 2018-02-15 18:39:47 -0800 | [diff] [blame] | 67 |                uint64_t index) | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 68 |         : id(id), | 
 | 69 |           type(kProducerType), | 
 | 70 |           consumer_count(consumer_count), | 
 | 71 |           width(width), | 
 | 72 |           height(height), | 
| Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 73 |           layer_count(layer_count), | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 74 |           format(format), | 
| Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 75 |           usage(usage), | 
| Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 76 |           pending_count(pending_count), | 
 | 77 |           state(state), | 
 | 78 |           signaled_mask(signaled_mask), | 
| Jiwen 'Steve' Cai | 2f26033 | 2018-02-15 18:39:47 -0800 | [diff] [blame] | 79 |           index(index) {} | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 80 |  | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 81 |     BufferInfo(int id, size_t consumer_count, size_t capacity, | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 82 |                const UsagePolicy& usage_policy) | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 83 |         : id(id), | 
 | 84 |           type(kProducerQueueType), | 
 | 85 |           consumer_count(consumer_count), | 
 | 86 |           capacity(capacity), | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 87 |           usage_policy(usage_policy) {} | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 88 |  | 
 | 89 |     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 |  | 
 | 104 |   BufferHubService* service() const { return service_; } | 
 | 105 |   ChannelType channel_type() const { return channel_type_; } | 
 | 106 |   int buffer_id() const { return buffer_id_; } | 
 | 107 |  | 
 | 108 |   int channel_id() const { return channel_id_; } | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 109 |  | 
| Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 110 |   bool signaled() const { return signaled_; } | 
 | 111 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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 |  | 
| Jiwen 'Steve' Cai | 2f26033 | 2018-02-15 18:39:47 -0800 | [diff] [blame] | 122 |   // The channel id of the buffer. | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 123 |   int channel_id_; | 
 | 124 |  | 
| Corey Tabaka | 52ea25c | 2017-09-13 18:02:48 -0700 | [diff] [blame] | 125 |   bool signaled_; | 
 | 126 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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 |  | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 138 |   pdx::Status<void> HandleMessage(pdx::Message& message) override; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 139 |   void HandleImpulse(pdx::Message& message) override; | 
 | 140 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 141 |   bool IsInitialized() const override; | 
 | 142 |   std::string DumpState(size_t max_length) override; | 
 | 143 |  | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 144 |  private: | 
 | 145 |   friend BASE; | 
 | 146 |  | 
| Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 147 |   pdx::Status<void> OnCreateBuffer(pdx::Message& message, uint32_t width, | 
 | 148 |                                    uint32_t height, uint32_t format, | 
| Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame] | 149 |                                    uint64_t usage, size_t meta_size_bytes); | 
| Jiwen 'Steve' Cai | a8049a2 | 2018-03-28 15:14:02 -0700 | [diff] [blame] | 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); | 
| Jiwen 'Steve' Cai | 6bffc67 | 2017-05-18 23:05:05 -0700 | [diff] [blame] | 155 |   pdx::Status<QueueInfo> OnCreateProducerQueue( | 
 | 156 |       pdx::Message& message, const ProducerQueueConfig& producer_config, | 
 | 157 |       const UsagePolicy& usage_policy); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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_ |