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, |
| 26 | kProducerQueueType, |
| 27 | kConsumerQueueType, |
| 28 | }; |
| 29 | |
| 30 | enum : int { kDetachedId = -1 }; |
| 31 | |
| 32 | BufferHubChannel(BufferHubService* service, int buffer_id, int channel_id, |
| 33 | ChannelType channel_type) |
| 34 | : service_(service), |
| 35 | buffer_id_(buffer_id), |
| 36 | channel_id_(channel_id), |
| 37 | channel_type_(channel_type) {} |
| 38 | virtual ~BufferHubChannel() {} |
| 39 | |
| 40 | virtual bool HandleMessage(pdx::Message& message) = 0; |
| 41 | virtual void HandleImpulse(pdx::Message& message) = 0; |
| 42 | |
| 43 | // Captures buffer info for use by BufferHubService::DumpState(). |
| 44 | struct BufferInfo { |
| 45 | // Common data field shared by BufferProducer and ProducerQueue. |
| 46 | int id = -1; |
| 47 | int type = -1; |
| 48 | size_t consumer_count = 0; |
| 49 | |
| 50 | // Data field for buffer producer. |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 51 | uint32_t width = 0; |
| 52 | uint32_t height = 0; |
| 53 | uint32_t format = 0; |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame^] | 54 | uint64_t usage = 0; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 55 | size_t slice_count = 0; |
| 56 | std::string name; |
| 57 | |
| 58 | // Data filed for producer queue. |
| 59 | size_t capacity = 0; |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame^] | 60 | UsagePolicy usage_policy{0, 0, 0, 0}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 61 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 62 | BufferInfo(int id, size_t consumer_count, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame^] | 63 | uint32_t format, uint64_t usage, size_t slice_count, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 64 | const std::string& name) |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 65 | : id(id), |
| 66 | type(kProducerType), |
| 67 | consumer_count(consumer_count), |
| 68 | width(width), |
| 69 | height(height), |
| 70 | format(format), |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame^] | 71 | usage(usage), |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 72 | slice_count(slice_count), |
| 73 | name(name) {} |
| 74 | |
Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 75 | BufferInfo(int id, size_t consumer_count, size_t capacity, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 76 | const UsagePolicy& usage_policy) |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 77 | : id(id), |
| 78 | type(kProducerQueueType), |
| 79 | consumer_count(consumer_count), |
| 80 | capacity(capacity), |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 81 | usage_policy(usage_policy) {} |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 82 | |
| 83 | BufferInfo() {} |
| 84 | }; |
| 85 | |
| 86 | // Returns the buffer info for this buffer. |
| 87 | virtual BufferInfo GetBufferInfo() const = 0; |
| 88 | |
| 89 | // Signal the client fd that an ownership change occurred using POLLIN. |
| 90 | void SignalAvailable(); |
| 91 | |
| 92 | // Clear the ownership change event. |
| 93 | void ClearAvailable(); |
| 94 | |
| 95 | // Signal hangup event. |
| 96 | void Hangup(); |
| 97 | |
| 98 | BufferHubService* service() const { return service_; } |
| 99 | ChannelType channel_type() const { return channel_type_; } |
| 100 | int buffer_id() const { return buffer_id_; } |
| 101 | |
| 102 | int channel_id() const { return channel_id_; } |
| 103 | bool IsDetached() const { return channel_id_ == kDetachedId; } |
| 104 | |
| 105 | void Detach() { |
| 106 | if (channel_type_ == kProducerType) |
| 107 | channel_id_ = kDetachedId; |
| 108 | } |
| 109 | void Attach(int channel_id) { |
| 110 | if (channel_type_ == kProducerType && channel_id_ == kDetachedId) |
| 111 | channel_id_ = channel_id; |
| 112 | } |
| 113 | |
| 114 | private: |
| 115 | BufferHubService* service_; |
| 116 | |
| 117 | // Static id of the buffer for logging and informational purposes. This id |
| 118 | // does not change for the life of the buffer. |
| 119 | // TODO(eieio): Consider using an id allocator instead of the originating |
| 120 | // channel id; channel ids wrap after 2^31 ids, but this is not a problem in |
| 121 | // general because channel ids are not used for any lookup in this service. |
| 122 | int buffer_id_; |
| 123 | |
| 124 | // The channel id of the buffer. This may change for a persistent producer |
| 125 | // buffer if it is detached and re-attached to another channel. |
| 126 | int channel_id_; |
| 127 | |
| 128 | ChannelType channel_type_; |
| 129 | |
| 130 | BufferHubChannel(const BufferHubChannel&) = delete; |
| 131 | void operator=(const BufferHubChannel&) = delete; |
| 132 | }; |
| 133 | |
| 134 | class BufferHubService : public pdx::ServiceBase<BufferHubService> { |
| 135 | public: |
| 136 | BufferHubService(); |
| 137 | ~BufferHubService() override; |
| 138 | |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 139 | pdx::Status<void> HandleMessage(pdx::Message& message) override; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 140 | void HandleImpulse(pdx::Message& message) override; |
| 141 | |
| 142 | void OnChannelClose(pdx::Message& message, |
| 143 | const std::shared_ptr<pdx::Channel>& channel) override; |
| 144 | |
| 145 | bool IsInitialized() const override; |
| 146 | std::string DumpState(size_t max_length) override; |
| 147 | |
| 148 | bool AddNamedBuffer(const std::string& name, |
| 149 | const std::shared_ptr<ProducerChannel>& buffer); |
| 150 | std::shared_ptr<ProducerChannel> GetNamedBuffer(const std::string& name); |
| 151 | bool RemoveNamedBuffer(const ProducerChannel& buffer); |
| 152 | |
| 153 | private: |
| 154 | friend BASE; |
| 155 | |
| 156 | std::unordered_map<std::string, std::shared_ptr<ProducerChannel>> |
| 157 | named_buffers_; |
| 158 | |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame^] | 159 | pdx::Status<void> OnCreateBuffer(pdx::Message& message, uint32_t width, |
| 160 | uint32_t height, uint32_t format, |
| 161 | uint64_t usage, size_t meta_size_bytes, |
| 162 | size_t slice_count); |
| 163 | pdx::Status<void> OnCreatePersistentBuffer( |
| 164 | pdx::Message& message, const std::string& name, int user_id, int group_id, |
| 165 | uint32_t width, uint32_t height, uint32_t format, uint64_t usage, |
| 166 | size_t meta_size_bytes, size_t slice_count); |
| 167 | pdx::Status<void> OnGetPersistentBuffer(pdx::Message& message, |
| 168 | const std::string& name); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 169 | pdx::Status<QueueInfo> OnCreateProducerQueue(pdx::Message& message, |
| 170 | size_t meta_size_bytes, |
| 171 | const UsagePolicy& usage_policy); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 172 | |
| 173 | BufferHubService(const BufferHubService&) = delete; |
| 174 | void operator=(const BufferHubService&) = delete; |
| 175 | }; |
| 176 | |
| 177 | } // namespace dvr |
| 178 | } // namespace android |
| 179 | |
| 180 | #endif // ANDROID_DVR_BUFFERHUBD_BUFFER_HUB_H_ |