| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 1 | #ifndef ANDROID_DVR_SERVICES_DISPLAYD_ACQUIRED_BUFFER_H_ | 
|  | 2 | #define ANDROID_DVR_SERVICES_DISPLAYD_ACQUIRED_BUFFER_H_ | 
|  | 3 |  | 
|  | 4 | #include <pdx/file_handle.h> | 
| Tianyu Jiang | 4964232 | 2018-12-18 11:08:03 -0800 | [diff] [blame] | 5 | #include <private/dvr/consumer_buffer.h> | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 6 |  | 
|  | 7 | #include <memory> | 
|  | 8 |  | 
|  | 9 | namespace android { | 
|  | 10 | namespace dvr { | 
|  | 11 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 12 | // Manages the ACQUIRE/RELEASE ownership cycle of a ConsumerBuffer. | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 13 | class AcquiredBuffer { | 
|  | 14 | public: | 
|  | 15 | static constexpr int kEmptyFence = pdx::LocalHandle::kEmptyFileHandle; | 
|  | 16 |  | 
|  | 17 | AcquiredBuffer() : buffer_(nullptr), acquire_fence_(kEmptyFence) {} | 
|  | 18 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 19 | // Constructs an AcquiredBuffer from a ConsumerBuffer pointer and an acquire | 
|  | 20 | // fence. The ConsumerBuffer MUST be in the ACQUIRED state prior to calling | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 21 | // this constructor; the constructor does not attempt to ACQUIRE the buffer | 
|  | 22 | // itself. | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 23 | AcquiredBuffer(const std::shared_ptr<ConsumerBuffer>& buffer, | 
| Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 24 | pdx::LocalHandle acquire_fence, std::size_t slot = 0); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 25 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 26 | // Constructs an AcquiredBuffer from a ConsumerBuffer. The ConsumerBuffer MUST | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 27 | // be in the POSTED state prior to calling this constructor, as this | 
|  | 28 | // constructor attempts to ACQUIRE the buffer. If ACQUIRING the buffer fails | 
|  | 29 | // this instance is left in the empty state. An optional error code is | 
|  | 30 | // returned in |error|, which may be nullptr if not needed. | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 31 | AcquiredBuffer(const std::shared_ptr<ConsumerBuffer>& buffer, int* error); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 32 |  | 
|  | 33 | // Move constructor. Behaves similarly to the move assignment operator below. | 
| Chih-Hung Hsieh | 5bc849f | 2018-09-25 14:21:50 -0700 | [diff] [blame] | 34 | AcquiredBuffer(AcquiredBuffer&& other) noexcept; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | ~AcquiredBuffer(); | 
|  | 37 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 38 | // Move assignment operator. Moves the ConsumerBuffer and acquire fence from | 
|  | 39 | // |other| into this instance after RELEASING the current ConsumerBuffer and | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 40 | // closing the acquire fence. After the move |other| is left in the empty | 
|  | 41 | // state. | 
| Chih-Hung Hsieh | 5bc849f | 2018-09-25 14:21:50 -0700 | [diff] [blame] | 42 | AcquiredBuffer& operator=(AcquiredBuffer&& other) noexcept; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 43 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 44 | // Accessors for the underlying ConsumerBuffer, the acquire fence, and the | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 45 | // use-case specific sequence value from the acquisition (see | 
| Tianyu Jiang | 4964232 | 2018-12-18 11:08:03 -0800 | [diff] [blame] | 46 | // private/dvr/consumer_buffer.h). | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 47 | std::shared_ptr<ConsumerBuffer> buffer() const { return buffer_; } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 48 | int acquire_fence() const { return acquire_fence_.Get(); } | 
|  | 49 |  | 
|  | 50 | // When non-empty, returns true if the acquired fence was signaled (or if the | 
|  | 51 | // fence is empty). Returns false when empty or if the fence is not signaled. | 
|  | 52 | bool IsAvailable() const; | 
|  | 53 |  | 
|  | 54 | bool IsEmpty() const { return buffer_ == nullptr; } | 
|  | 55 |  | 
|  | 56 | // Returns the acquire fence, passing ownership to the caller. | 
|  | 57 | pdx::LocalHandle ClaimAcquireFence(); | 
|  | 58 |  | 
|  | 59 | // Returns the buffer, passing ownership to the caller. Caller is responsible | 
|  | 60 | // for calling Release on the returned buffer. | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 61 | std::shared_ptr<ConsumerBuffer> ClaimBuffer(); | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 62 |  | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 63 | // Releases the ConsumerBuffer, passing the release fence in |release_fence| | 
|  | 64 | // to the producer. On success, the ConsumerBuffer and acquire fence are set | 
|  | 65 | // to empty state; if release fails, the ConsumerBuffer and acquire fence are | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 66 | // left in place and a negative error code is returned. | 
| Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 67 | int Release(pdx::LocalHandle release_fence = {}); | 
|  | 68 |  | 
|  | 69 | // Returns the slot in the queue this buffer belongs to. Buffers that are not | 
|  | 70 | // part of a queue return 0. | 
|  | 71 | std::size_t slot() const { return slot_; } | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 72 |  | 
|  | 73 | private: | 
| Jiwen 'Steve' Cai | 1c73024 | 2018-12-31 18:40:02 -0800 | [diff] [blame] | 74 | std::shared_ptr<ConsumerBuffer> buffer_; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 75 | // Mutable so that the fence can be closed when it is determined to be | 
|  | 76 | // signaled during IsAvailable(). | 
|  | 77 | mutable pdx::LocalHandle acquire_fence_; | 
| Corey Tabaka | 0d07cdd | 2017-09-28 11:15:50 -0700 | [diff] [blame] | 78 | std::size_t slot_{0}; | 
| Corey Tabaka | 0b485c9 | 2017-05-19 12:02:58 -0700 | [diff] [blame] | 79 |  | 
|  | 80 | AcquiredBuffer(const AcquiredBuffer&) = delete; | 
|  | 81 | void operator=(const AcquiredBuffer&) = delete; | 
| Alex Vakulenko | a8a9278 | 2017-01-27 14:41:57 -0800 | [diff] [blame] | 82 | }; | 
|  | 83 |  | 
|  | 84 | }  // namespace dvr | 
|  | 85 | }  // namespace android | 
|  | 86 |  | 
|  | 87 | #endif  // ANDROID_DVR_SERVICES_DISPLAYD_ACQUIRED_BUFFER_H_ |