acquired_buffer.h 3.5 KB

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