Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 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 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 33 | static pdx::Status<std::shared_ptr<ProducerChannel>> Create( |
| 34 | BufferHubService* service, int channel_id, uint32_t width, |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame^] | 35 | uint32_t height, uint32_t format, uint64_t usage, size_t meta_size_bytes); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 36 | |
| 37 | ~ProducerChannel() override; |
| 38 | |
| 39 | bool HandleMessage(Message& message) override; |
| 40 | void HandleImpulse(Message& message) override; |
| 41 | |
| 42 | BufferInfo GetBufferInfo() const override; |
| 43 | |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame^] | 44 | pdx::Status<NativeBufferHandle<BorrowedHandle>> OnGetBuffer(Message& message); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 45 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 46 | pdx::Status<RemoteChannelHandle> CreateConsumer(Message& message); |
| 47 | pdx::Status<RemoteChannelHandle> OnNewConsumer(Message& message); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 48 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 49 | pdx::Status<std::pair<BorrowedFence, BufferWrapper<std::uint8_t*>>> |
| 50 | OnConsumerAcquire(Message& message, std::size_t metadata_size); |
| 51 | pdx::Status<void> OnConsumerRelease(Message& message, |
| 52 | LocalFence release_fence); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 53 | |
| 54 | void OnConsumerIgnored(); |
| 55 | |
| 56 | void AddConsumer(ConsumerChannel* channel); |
| 57 | void RemoveConsumer(ConsumerChannel* channel); |
| 58 | |
| 59 | bool CheckAccess(int euid, int egid); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 60 | bool CheckParameters(uint32_t width, uint32_t height, uint32_t format, |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame^] | 61 | uint64_t usage, size_t meta_size_bytes); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 62 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 63 | pdx::Status<void> OnProducerMakePersistent(Message& message, |
| 64 | const std::string& name, |
| 65 | int user_id, int group_id); |
| 66 | pdx::Status<void> OnRemovePersistence(Message& message); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 67 | |
| 68 | private: |
| 69 | std::vector<ConsumerChannel*> consumer_channels_; |
| 70 | // This counts the number of consumers left to process this buffer. If this is |
| 71 | // zero then the producer can re-acquire ownership. |
| 72 | int pending_consumers_; |
| 73 | |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame^] | 74 | IonBuffer buffer_; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 75 | |
| 76 | bool producer_owns_; |
| 77 | LocalFence post_fence_; |
| 78 | LocalFence returned_fence_; |
| 79 | size_t meta_size_bytes_; |
| 80 | std::unique_ptr<uint8_t[]> meta_; |
| 81 | |
| 82 | static constexpr int kNoCheckId = -1; |
| 83 | static constexpr int kUseCallerId = 0; |
| 84 | static constexpr int kRootId = 0; |
| 85 | |
| 86 | // User and group id to check when obtaining a persistent buffer. |
| 87 | int owner_user_id_ = kNoCheckId; |
| 88 | int owner_group_id_ = kNoCheckId; |
| 89 | |
| 90 | std::string name_; |
| 91 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 92 | ProducerChannel(BufferHubService* service, int channel, uint32_t width, |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 93 | uint32_t height, uint32_t format, uint64_t usage, |
Hendrik Wagenaar | 4d3590f | 2017-05-06 22:36:04 -0700 | [diff] [blame^] | 94 | size_t meta_size_bytes, int* error); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 95 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 96 | pdx::Status<void> OnProducerPost( |
| 97 | Message& message, LocalFence acquire_fence, |
| 98 | BufferWrapper<std::vector<std::uint8_t>> metadata); |
| 99 | pdx::Status<LocalFence> OnProducerGain(Message& message); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 100 | |
| 101 | ProducerChannel(const ProducerChannel&) = delete; |
| 102 | void operator=(const ProducerChannel&) = delete; |
| 103 | }; |
| 104 | |
| 105 | } // namespace dvr |
| 106 | } // namespace android |
| 107 | |
| 108 | #endif // ANDROID_DVR_BUFFERHUBD_PRODUCER_CHANNEL_H_ |