buffer_hub_queue_parcelable.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "include/private/dvr/buffer_hub_queue_parcelable.h"
  2. #include <binder/Parcel.h>
  3. #include <pdx/default_transport/channel_parcelable.h>
  4. namespace android {
  5. namespace dvr {
  6. template <BufferHubQueueParcelableMagic Magic>
  7. bool BufferHubQueueParcelable<Magic>::IsValid() const {
  8. return !!channel_parcelable_ && channel_parcelable_->IsValid();
  9. }
  10. template <BufferHubQueueParcelableMagic Magic>
  11. pdx::LocalChannelHandle BufferHubQueueParcelable<Magic>::TakeChannelHandle() {
  12. if (!IsValid()) {
  13. ALOGE(
  14. "BufferHubQueueParcelable::TakeChannelHandle: Invalid channel parcel.");
  15. return {}; // Returns an empty channel handle.
  16. }
  17. // Take channel handle out of the parcelable and reset the parcelable.
  18. pdx::LocalChannelHandle handle = channel_parcelable_->TakeChannelHandle();
  19. // Now channel_parcelable_ should already be invalid, but reset it to release
  20. // the invalid parcelable object from unique_ptr.
  21. channel_parcelable_ = nullptr;
  22. return handle;
  23. }
  24. template <BufferHubQueueParcelableMagic Magic>
  25. status_t BufferHubQueueParcelable<Magic>::writeToParcel(Parcel* parcel) const {
  26. if (!IsValid()) {
  27. ALOGE("BufferHubQueueParcelable::writeToParcel: Invalid channel.");
  28. return -EINVAL;
  29. }
  30. status_t res = parcel->writeUint32(Magic);
  31. if (res != OK) {
  32. ALOGE("BufferHubQueueParcelable::writeToParcel: Cannot write magic.");
  33. return res;
  34. }
  35. return channel_parcelable_->writeToParcel(parcel);
  36. }
  37. template <BufferHubQueueParcelableMagic Magic>
  38. status_t BufferHubQueueParcelable<Magic>::readFromParcel(const Parcel* parcel) {
  39. if (IsValid()) {
  40. ALOGE(
  41. "BufferHubQueueParcelable::readFromParcel: This parcelable object has "
  42. "been initialized already.");
  43. return -EINVAL;
  44. }
  45. uint32_t out_magic = 0;
  46. status_t res = OK;
  47. res = parcel->readUint32(&out_magic);
  48. if (res != OK)
  49. return res;
  50. if (out_magic != Magic) {
  51. ALOGE(
  52. "BufferHubQueueParcelable::readFromParcel: Unexpected magic: 0x%x, "
  53. "epxected: 0x%x",
  54. out_magic, Magic);
  55. return -EINVAL;
  56. }
  57. // (Re)Alocate channel parcelable object.
  58. channel_parcelable_ =
  59. std::make_unique<pdx::default_transport::ChannelParcelable>();
  60. return channel_parcelable_->readFromParcel(parcel);
  61. }
  62. template class BufferHubQueueParcelable<
  63. BufferHubQueueParcelableMagic::Producer>;
  64. template class BufferHubQueueParcelable<
  65. BufferHubQueueParcelableMagic::Consumer>;
  66. } // namespace dvr
  67. } // namespace android