Use generic libpdx event bits facility in libvrflinger and bufferhubd.

Use the generic event bits abstraction to support UDS and ServiceFS
backends properly.

Bug: 34466748
Test: start/stop basicvr repeatedly; observe stable bufferhub counts.
Change-Id: I9f2ea67c25860ad7fb931938991a29a37f3391fc
diff --git a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_client.h b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_client.h
index b6ff5b6..cefde7b 100644
--- a/libs/vr/libbufferhub/include/private/dvr/buffer_hub_client.h
+++ b/libs/vr/libbufferhub/include/private/dvr/buffer_hub_client.h
@@ -71,6 +71,15 @@
   }
 
   using Client::event_fd;
+
+  Status<int> GetEventMask(int events) {
+    if (auto* client_channel = GetChannel()) {
+      return client_channel->GetEventMask(events);
+    } else {
+      return pdx::ErrorStatus(EINVAL);
+    }
+  }
+
   native_handle_t* native_handle() const {
     return const_cast<native_handle_t*>(slices_[0].handle());
   }
@@ -158,8 +167,9 @@
   int Post(const LocalHandle& ready_fence) {
     return Post(ready_fence, nullptr, 0);
   }
-  template <typename Meta, typename = typename std::enable_if<
-                               !std::is_void<Meta>::value>::type>
+  template <
+      typename Meta,
+      typename = typename std::enable_if<!std::is_void<Meta>::value>::type>
   int Post(const LocalHandle& ready_fence, const Meta& meta) {
     return Post(ready_fence, &meta, sizeof(meta));
   }
diff --git a/libs/vr/libvrflinger/display_surface.cpp b/libs/vr/libvrflinger/display_surface.cpp
index dff08b5..e1729f8 100644
--- a/libs/vr/libvrflinger/display_surface.cpp
+++ b/libs/vr/libvrflinger/display_surface.cpp
@@ -396,6 +396,16 @@
 
 void DisplaySurface::HandleConsumerEvents(
     const std::shared_ptr<BufferConsumer>& consumer, int events) {
+  auto status = consumer->GetEventMask(events);
+  if (!status) {
+    ALOGW(
+        "DisplaySurface::HandleConsumerEvents: Failed to get event mask for "
+        "consumer: %s",
+        status.GetErrorMessage().c_str());
+    return;
+  }
+
+  events = status.get();
   if (events & EPOLLHUP) {
     ALOGD_IF(TRACE,
              "DisplaySurface::HandleConsumerEvents: removing event handler for "
diff --git a/services/vr/bufferhubd/buffer_hub.cpp b/services/vr/bufferhubd/buffer_hub.cpp
index 12243dc..0906476 100644
--- a/services/vr/bufferhubd/buffer_hub.cpp
+++ b/services/vr/bufferhubd/buffer_hub.cpp
@@ -108,6 +108,7 @@
         // consumer_count is tracked by producer. When it's zero, producer must
         // have already hung up and the consumer is orphaned.
         stream << std::setw(14) << "Orphaned.";
+        stream << (" channel_id=" + std::to_string(channel->channel_id()));
         stream << std::endl;
         continue;
       }
@@ -433,6 +434,9 @@
 
 void BufferHubChannel::SignalAvailable() {
   ATRACE_NAME("BufferHubChannel::SignalAvailable");
+  ALOGD_IF(TRACE,
+           "BufferHubChannel::SignalAvailable: channel_id=%d buffer_id=%d",
+           channel_id(), buffer_id());
   if (!IsDetached()) {
     const int ret = service_->ModifyChannelEvents(channel_id_, 0, POLLIN);
     ALOGE_IF(ret < 0,
@@ -446,6 +450,9 @@
 
 void BufferHubChannel::ClearAvailable() {
   ATRACE_NAME("BufferHubChannel::ClearAvailable");
+  ALOGD_IF(TRACE,
+           "BufferHubChannel::ClearAvailable: channel_id=%d buffer_id=%d",
+           channel_id(), buffer_id());
   if (!IsDetached()) {
     const int ret = service_->ModifyChannelEvents(channel_id_, POLLIN, 0);
     ALOGE_IF(ret < 0,
@@ -459,6 +466,8 @@
 
 void BufferHubChannel::Hangup() {
   ATRACE_NAME("BufferHubChannel::Hangup");
+  ALOGD_IF(TRACE, "BufferHubChannel::Hangup: channel_id=%d buffer_id=%d",
+           channel_id(), buffer_id());
   if (!IsDetached()) {
     const int ret = service_->ModifyChannelEvents(channel_id_, 0, POLLHUP);
     ALOGE_IF(
diff --git a/services/vr/bufferhubd/consumer_channel.cpp b/services/vr/bufferhubd/consumer_channel.cpp
index 4f0ca4e..2264cef 100644
--- a/services/vr/bufferhubd/consumer_channel.cpp
+++ b/services/vr/bufferhubd/consumer_channel.cpp
@@ -27,8 +27,9 @@
 }
 
 ConsumerChannel::~ConsumerChannel() {
-  ALOGD_IF(TRACE, "ConsumerChannel::~ConsumerChannel: channel_id=%d",
-           channel_id());
+  ALOGD_IF(TRACE,
+           "ConsumerChannel::~ConsumerChannel: channel_id=%d buffer_id=%d",
+           channel_id(), buffer_id());
 
   if (auto producer = GetProducer()) {
     if (!handled_)  // Producer is waiting for our Release.
diff --git a/services/vr/bufferhubd/producer_channel.cpp b/services/vr/bufferhubd/producer_channel.cpp
index c1ef22c..98a419f 100644
--- a/services/vr/bufferhubd/producer_channel.cpp
+++ b/services/vr/bufferhubd/producer_channel.cpp
@@ -60,8 +60,9 @@
 }
 
 ProducerChannel::~ProducerChannel() {
-  ALOGD_IF(TRACE, "ProducerChannel::~ProducerChannel: channel_id=%d",
-           channel_id());
+  ALOGD_IF(TRACE,
+           "ProducerChannel::~ProducerChannel: channel_id=%d buffer_id=%d",
+           channel_id(), buffer_id());
   for (auto consumer : consumer_channels_)
     consumer->OnProducerClosed();
 }
@@ -274,8 +275,9 @@
   // Attempt to merge the fences if necessary.
   if (release_fence) {
     if (returned_fence_) {
-      LocalFence merged_fence(sync_merge(
-          "bufferhub_merged", returned_fence_.get_fd(), release_fence.get_fd()));
+      LocalFence merged_fence(sync_merge("bufferhub_merged",
+                                         returned_fence_.get_fd(),
+                                         release_fence.get_fd()));
       const int error = errno;
       if (!merged_fence) {
         ALOGE("ProducerChannel::OnConsumerRelease: Failed to merge fences: %s",
@@ -366,10 +368,9 @@
 bool ProducerChannel::CheckParameters(int width, int height, int format,
                                       int usage, size_t meta_size_bytes,
                                       size_t slice_count) {
-  return slices_.size() == slice_count &&
-         meta_size_bytes == meta_size_bytes_ && slices_[0].width() == width &&
-         slices_[0].height() == height && slices_[0].format() == format &&
-         slices_[0].usage() == usage;
+  return slices_.size() == slice_count && meta_size_bytes == meta_size_bytes_ &&
+         slices_[0].width() == width && slices_[0].height() == height &&
+         slices_[0].format() == format && slices_[0].usage() == usage;
 }
 
 }  // namespace dvr