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