Use VsyncEventData in DisplayEventReceiver::Event.

Clean-up that re-uses VsyncEventData so it's easier to maintain.
The ParcelableVsyncEventData is used in AIDL. It is separated from
VsyncEventData because of the union in
DisplayEventReceiver::Event and Parcelable has non-trivial
constructors.

Bug: 218563993
Test: atest ChoreographerNativeTest
Test: atest libsurfaceflinger_unittest
Test: atest libgui_test
Change-Id: I3ebeb1c7826300c27c4a12d4dba6fbd16305e9e1
diff --git a/libs/gui/DisplayEventDispatcher.cpp b/libs/gui/DisplayEventDispatcher.cpp
index 26db59b..39d380d 100644
--- a/libs/gui/DisplayEventDispatcher.cpp
+++ b/libs/gui/DisplayEventDispatcher.cpp
@@ -126,7 +126,7 @@
         ALOGV("dispatcher %p ~ Vsync pulse: timestamp=%" PRId64
               ", displayId=%s, count=%d, vsyncId=%" PRId64,
               this, ns2ms(vsyncTimestamp), to_string(vsyncDisplayId).c_str(), vsyncCount,
-              vsyncEventData.id);
+              vsyncEventData.preferredVsyncId());
         mWaitingForVsync = false;
         mLastVsyncCount = vsyncCount;
         dispatchVsync(vsyncTimestamp, vsyncDisplayId, vsyncCount, vsyncEventData);
@@ -146,18 +146,6 @@
     return 1; // keep the callback
 }
 
-void DisplayEventDispatcher::populateFrameTimelines(const DisplayEventReceiver::Event& event,
-                                                    VsyncEventData* outVsyncEventData) const {
-    for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
-        DisplayEventReceiver::Event::VSync::FrameTimeline receiverTimeline =
-                event.vsync.frameTimelines[i];
-        outVsyncEventData->frameTimelines[i] =
-                VsyncEventData::FrameTimeline(receiverTimeline.vsyncId,
-                                              receiverTimeline.deadlineTimestamp,
-                                              receiverTimeline.expectedVSyncTimestamp);
-    }
-}
-
 bool DisplayEventDispatcher::processPendingEvents(nsecs_t* outTimestamp,
                                                   PhysicalDisplayId* outDisplayId,
                                                   uint32_t* outCount,
@@ -178,12 +166,7 @@
                     *outTimestamp = ev.header.timestamp;
                     *outDisplayId = ev.header.displayId;
                     *outCount = ev.vsync.count;
-                    outVsyncEventData->id = ev.vsync.vsyncId;
-                    outVsyncEventData->deadlineTimestamp = ev.vsync.deadlineTimestamp;
-                    outVsyncEventData->frameInterval = ev.vsync.frameInterval;
-                    outVsyncEventData->preferredFrameTimelineIndex =
-                            ev.vsync.preferredFrameTimelineIndex;
-                    populateFrameTimelines(ev, outVsyncEventData);
+                    *outVsyncEventData = ev.vsync.vsyncData;
                     break;
                 case DisplayEventReceiver::DISPLAY_EVENT_HOTPLUG:
                     dispatchHotplug(ev.header.timestamp, ev.header.displayId, ev.hotplug.connected);
diff --git a/libs/gui/DisplayEventReceiver.cpp b/libs/gui/DisplayEventReceiver.cpp
index 36e7d80..ec0be87 100644
--- a/libs/gui/DisplayEventReceiver.cpp
+++ b/libs/gui/DisplayEventReceiver.cpp
@@ -80,15 +80,14 @@
     return NO_INIT;
 }
 
-status_t DisplayEventReceiver::getLatestVsyncEventData(VsyncEventData* outVsyncEventData) const {
+status_t DisplayEventReceiver::getLatestVsyncEventData(
+        ParcelableVsyncEventData* outVsyncEventData) const {
     if (mEventConnection != nullptr) {
-        VsyncEventData vsyncEventData;
-        auto status = mEventConnection->getLatestVsyncEventData(&vsyncEventData);
+        auto status = mEventConnection->getLatestVsyncEventData(outVsyncEventData);
         if (!status.isOk()) {
             ALOGE("Failed to get latest vsync event data: %s", status.exceptionMessage().c_str());
             return status.transactionError();
         }
-        *outVsyncEventData = vsyncEventData;
         return NO_ERROR;
     }
     return NO_INIT;
diff --git a/libs/gui/VsyncEventData.cpp b/libs/gui/VsyncEventData.cpp
index aad81d0..23f0921 100644
--- a/libs/gui/VsyncEventData.cpp
+++ b/libs/gui/VsyncEventData.cpp
@@ -23,52 +23,46 @@
 
 namespace android::gui {
 
-status_t VsyncEventData::readFromParcel(const Parcel* parcel) {
+int64_t VsyncEventData::preferredVsyncId() const {
+    return frameTimelines[preferredFrameTimelineIndex].vsyncId;
+}
+
+int64_t VsyncEventData::preferredDeadlineTimestamp() const {
+    return frameTimelines[preferredFrameTimelineIndex].deadlineTimestamp;
+}
+
+int64_t VsyncEventData::preferredExpectedPresentationTime() const {
+    return frameTimelines[preferredFrameTimelineIndex].expectedPresentationTime;
+}
+
+status_t ParcelableVsyncEventData::readFromParcel(const Parcel* parcel) {
     if (parcel == nullptr) {
         ALOGE("%s: Null parcel", __func__);
         return BAD_VALUE;
     }
 
-    SAFE_PARCEL(parcel->readInt64, &id)
-    SAFE_PARCEL(parcel->readInt64, &deadlineTimestamp);
-    SAFE_PARCEL(parcel->readInt64, &frameInterval);
+    SAFE_PARCEL(parcel->readInt64, &vsync.frameInterval);
 
     uint64_t uintPreferredFrameTimelineIndex;
     SAFE_PARCEL(parcel->readUint64, &uintPreferredFrameTimelineIndex);
-    preferredFrameTimelineIndex = static_cast<size_t>(uintPreferredFrameTimelineIndex);
+    vsync.preferredFrameTimelineIndex = static_cast<size_t>(uintPreferredFrameTimelineIndex);
 
-    std::vector<FrameTimeline> timelines;
-    SAFE_PARCEL(parcel->readParcelableVector, &timelines);
-    std::copy_n(timelines.begin(), timelines.size(), frameTimelines.begin());
-
-    return OK;
-}
-status_t VsyncEventData::writeToParcel(Parcel* parcel) const {
-    SAFE_PARCEL(parcel->writeInt64, id)
-    SAFE_PARCEL(parcel->writeInt64, deadlineTimestamp);
-    SAFE_PARCEL(parcel->writeInt64, frameInterval);
-    SAFE_PARCEL(parcel->writeUint64, preferredFrameTimelineIndex);
-    SAFE_PARCEL(parcel->writeParcelableVector,
-                std::vector(frameTimelines.begin(), frameTimelines.end()));
-
-    return OK;
-}
-status_t VsyncEventData::FrameTimeline::readFromParcel(const Parcel* parcel) {
-    if (parcel == nullptr) {
-        ALOGE("%s: Null parcel", __func__);
-        return BAD_VALUE;
+    for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
+        SAFE_PARCEL(parcel->readInt64, &vsync.frameTimelines[i].vsyncId);
+        SAFE_PARCEL(parcel->readInt64, &vsync.frameTimelines[i].deadlineTimestamp);
+        SAFE_PARCEL(parcel->readInt64, &vsync.frameTimelines[i].expectedPresentationTime);
     }
 
-    SAFE_PARCEL(parcel->readInt64, &id)
-    SAFE_PARCEL(parcel->readInt64, &deadlineTimestamp);
-    SAFE_PARCEL(parcel->readInt64, &expectedPresentTime);
-
     return OK;
 }
-status_t VsyncEventData::FrameTimeline::writeToParcel(Parcel* parcel) const {
-    SAFE_PARCEL(parcel->writeInt64, id);
-    SAFE_PARCEL(parcel->writeInt64, deadlineTimestamp);
-    SAFE_PARCEL(parcel->writeInt64, expectedPresentTime);
+status_t ParcelableVsyncEventData::writeToParcel(Parcel* parcel) const {
+    SAFE_PARCEL(parcel->writeInt64, vsync.frameInterval);
+    SAFE_PARCEL(parcel->writeUint64, vsync.preferredFrameTimelineIndex);
+    for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
+        SAFE_PARCEL(parcel->writeInt64, vsync.frameTimelines[i].vsyncId);
+        SAFE_PARCEL(parcel->writeInt64, vsync.frameTimelines[i].deadlineTimestamp);
+        SAFE_PARCEL(parcel->writeInt64, vsync.frameTimelines[i].expectedPresentationTime);
+    }
 
     return OK;
 }
diff --git a/libs/gui/aidl/android/gui/IDisplayEventConnection.aidl b/libs/gui/aidl/android/gui/IDisplayEventConnection.aidl
index e9a6a0c..9781ca9 100644
--- a/libs/gui/aidl/android/gui/IDisplayEventConnection.aidl
+++ b/libs/gui/aidl/android/gui/IDisplayEventConnection.aidl
@@ -17,7 +17,7 @@
 package android.gui;
 
 import android.gui.BitTube;
-import android.gui.VsyncEventData;
+import android.gui.ParcelableVsyncEventData;
 
 /** @hide */
 interface IDisplayEventConnection {
@@ -43,5 +43,5 @@
     /*
      * getLatestVsyncEventData() gets the latest vsync event data.
      */
-    VsyncEventData getLatestVsyncEventData();
+    ParcelableVsyncEventData getLatestVsyncEventData();
 }
diff --git a/libs/gui/aidl/android/gui/VsyncEventData.aidl b/libs/gui/aidl/android/gui/ParcelableVsyncEventData.aidl
similarity index 89%
rename from libs/gui/aidl/android/gui/VsyncEventData.aidl
rename to libs/gui/aidl/android/gui/ParcelableVsyncEventData.aidl
index 7343515..ba76671 100644
--- a/libs/gui/aidl/android/gui/VsyncEventData.aidl
+++ b/libs/gui/aidl/android/gui/ParcelableVsyncEventData.aidl
@@ -16,4 +16,4 @@
 
 package android.gui;
 
-parcelable VsyncEventData cpp_header "gui/VsyncEventData.h";
+parcelable ParcelableVsyncEventData cpp_header "gui/VsyncEventData.h";
diff --git a/libs/gui/include/gui/DisplayEventReceiver.h b/libs/gui/include/gui/DisplayEventReceiver.h
index 4e04db3..ddc109e 100644
--- a/libs/gui/include/gui/DisplayEventReceiver.h
+++ b/libs/gui/include/gui/DisplayEventReceiver.h
@@ -35,6 +35,7 @@
 // ----------------------------------------------------------------------------
 
 using gui::IDisplayEventConnection;
+using gui::ParcelableVsyncEventData;
 using gui::VsyncEventData;
 
 namespace gui {
@@ -76,16 +77,7 @@
 
         struct VSync {
             uint32_t count;
-            nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
-            nsecs_t deadlineTimestamp __attribute__((aligned(8)));
-            nsecs_t frameInterval __attribute__((aligned(8)));
-            int64_t vsyncId;
-            size_t preferredFrameTimelineIndex __attribute__((aligned(8)));
-            struct FrameTimeline {
-                nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
-                nsecs_t deadlineTimestamp __attribute__((aligned(8)));
-                int64_t vsyncId;
-            } frameTimelines[VsyncEventData::kFrameTimelinesLength];
+            VsyncEventData vsyncData;
         };
 
         struct Hotplug {
@@ -175,7 +167,7 @@
     /**
      * getLatestVsyncEventData() gets the latest vsync event data.
      */
-    status_t getLatestVsyncEventData(VsyncEventData* outVsyncEventData) const;
+    status_t getLatestVsyncEventData(ParcelableVsyncEventData* outVsyncEventData) const;
 
 private:
     sp<IDisplayEventConnection> mEventConnection;
diff --git a/libs/gui/include/gui/VsyncEventData.h b/libs/gui/include/gui/VsyncEventData.h
index abac61c..8e99539 100644
--- a/libs/gui/include/gui/VsyncEventData.h
+++ b/libs/gui/include/gui/VsyncEventData.h
@@ -21,52 +21,44 @@
 #include <array>
 
 namespace android::gui {
-struct VsyncEventData : public Parcelable {
+// Plain Old Data (POD) vsync data structure. For example, it can be easily used in the
+// DisplayEventReceiver::Event union.
+struct VsyncEventData {
     // Max amount of frame timelines is arbitrarily set to be reasonable.
     static constexpr int64_t kFrameTimelinesLength = 7;
 
-    // The Vsync Id corresponsing to this vsync event. This will be used to
-    // populate ISurfaceComposer::setFrameTimelineVsync and
-    // SurfaceComposerClient::setFrameTimelineVsync
-    // TODO(b/198191703): Remove when JNI DisplayEventReceiver uses frameTimelines array.
-    int64_t id = FrameTimelineInfo::INVALID_VSYNC_ID;
-
-    // The deadline in CLOCK_MONOTONIC that the app needs to complete its
-    // frame by (both on the CPU and the GPU)
-    // TODO(b/198191703): Remove when JNI DisplayEventReceiver uses frameTimelines array.
-    int64_t deadlineTimestamp = std::numeric_limits<int64_t>::max();
-
     // The current frame interval in ns when this frame was scheduled.
-    int64_t frameInterval = 0;
+    int64_t frameInterval;
 
-    struct FrameTimeline : public Parcelable {
-        FrameTimeline() = default;
-        FrameTimeline(int64_t id, int64_t deadlineTimestamp, int64_t expectedPresentTime)
-              : id(id),
-                deadlineTimestamp(deadlineTimestamp),
-                expectedPresentTime(expectedPresentTime) {}
+    // Index into the frameTimelines that represents the platform's preferred frame timeline.
+    uint32_t preferredFrameTimelineIndex;
 
+    struct alignas(8) FrameTimeline {
         // The Vsync Id corresponsing to this vsync event. This will be used to
         // populate ISurfaceComposer::setFrameTimelineVsync and
         // SurfaceComposerClient::setFrameTimelineVsync
-        int64_t id = FrameTimelineInfo::INVALID_VSYNC_ID;
+        int64_t vsyncId;
 
-        // The deadline in CLOCK_MONOTONIC that the app needs to complete its
-        // frame by (both on the CPU and the GPU)
-        int64_t deadlineTimestamp = std::numeric_limits<int64_t>::max();
+        // The deadline in CLOCK_MONOTONIC in nanos that the app needs to complete its
+        // frame by (both on the CPU and the GPU).
+        int64_t deadlineTimestamp;
 
-        // The anticipated Vsync present time.
-        int64_t expectedPresentTime = 0;
+        // The anticipated Vsync presentation time in nanos.
+        int64_t expectedPresentationTime;
+    } frameTimelines[kFrameTimelinesLength]; // Sorted possible frame timelines.
 
-        status_t readFromParcel(const Parcel*) override;
-        status_t writeToParcel(Parcel*) const override;
-    };
+    // Gets the preferred frame timeline's vsync ID.
+    int64_t preferredVsyncId() const;
 
-    // Sorted possible frame timelines.
-    std::array<FrameTimeline, kFrameTimelinesLength> frameTimelines;
+    // Gets the preferred frame timeline's deadline timestamp.
+    int64_t preferredDeadlineTimestamp() const;
 
-    // Index into the frameTimelines that represents the platform's preferred frame timeline.
-    size_t preferredFrameTimelineIndex = std::numeric_limits<size_t>::max();
+    // Gets the preferred frame timeline's expected vsync timestamp.
+    int64_t preferredExpectedPresentationTime() const;
+};
+
+struct ParcelableVsyncEventData : public Parcelable {
+    VsyncEventData vsync;
 
     status_t readFromParcel(const Parcel*) override;
     status_t writeToParcel(Parcel*) const override;
diff --git a/libs/gui/tests/DisplayEventStructLayout_test.cpp b/libs/gui/tests/DisplayEventStructLayout_test.cpp
index bcd39db..da88463 100644
--- a/libs/gui/tests/DisplayEventStructLayout_test.cpp
+++ b/libs/gui/tests/DisplayEventStructLayout_test.cpp
@@ -20,9 +20,10 @@
 namespace android::test {
 
 #define CHECK_OFFSET(type, member, expected_offset) \
-    static_assert((offsetof(type, member) == (expected_offset)), "")
+    static_assert((offsetof(type, member) == (expected_offset)))
 
 TEST(DisplayEventStructLayoutTest, TestEventAlignment) {
+    static_assert(std::is_pod<DisplayEventReceiver::Event::VSync>::value);
     CHECK_OFFSET(DisplayEventReceiver::Event, vsync, 24);
     CHECK_OFFSET(DisplayEventReceiver::Event, hotplug, 24);
     CHECK_OFFSET(DisplayEventReceiver::Event, modeChange, 24);
@@ -32,10 +33,29 @@
     CHECK_OFFSET(DisplayEventReceiver::Event::Header, timestamp, 16);
 
     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, count, 0);
-    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, expectedVSyncTimestamp, 8);
-    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, deadlineTimestamp, 16);
-    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, frameInterval, 24);
-    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncId, 32);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameInterval, 8);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.preferredFrameTimelineIndex, 16);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines, 24);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines[0].vsyncId, 24);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines[0].deadlineTimestamp,
+                 32);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
+                 vsyncData.frameTimelines[0].expectedPresentationTime, 40);
+    // Also test the offsets of the last frame timeline. A loop is not used because the non-const
+    // index cannot be used in static_assert.
+    const int lastFrameTimelineOffset = /* Start of array */ 24 +
+            (VsyncEventData::kFrameTimelinesLength - 1) * /* Size of FrameTimeline */ 24;
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
+                 vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesLength - 1].vsyncId,
+                 lastFrameTimelineOffset);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
+                 vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesLength - 1]
+                         .deadlineTimestamp,
+                 lastFrameTimelineOffset + 8);
+    CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
+                 vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesLength - 1]
+                         .expectedPresentationTime,
+                 lastFrameTimelineOffset + 16);
 
     CHECK_OFFSET(DisplayEventReceiver::Event::Hotplug, connected, 0);
 
diff --git a/libs/gui/tests/VsyncEventData_test.cpp b/libs/gui/tests/VsyncEventData_test.cpp
index a670d42..f114522 100644
--- a/libs/gui/tests/VsyncEventData_test.cpp
+++ b/libs/gui/tests/VsyncEventData_test.cpp
@@ -22,54 +22,37 @@
 
 namespace android {
 
+using gui::ParcelableVsyncEventData;
 using gui::VsyncEventData;
 using FrameTimeline = gui::VsyncEventData::FrameTimeline;
 
 namespace test {
 
-TEST(VsyncEventData, Parcelling) {
-    VsyncEventData data;
-    data.id = 123;
-    data.deadlineTimestamp = 456;
-    data.frameInterval = 789;
-    data.preferredFrameTimelineIndex = 1;
-    FrameTimeline timeline0 = FrameTimeline(1, 2, 3);
-    FrameTimeline timeline1 = FrameTimeline(4, 5, 6);
-    data.frameTimelines[0] = timeline0;
-    data.frameTimelines[1] = timeline1;
+TEST(ParcelableVsyncEventData, Parcelling) {
+    ParcelableVsyncEventData data;
+    data.vsync.frameInterval = 789;
+    data.vsync.preferredFrameTimelineIndex = 1;
+    FrameTimeline timeline0 = FrameTimeline{1, 2, 3};
+    FrameTimeline timeline1 = FrameTimeline{4, 5, 6};
+    data.vsync.frameTimelines[0] = timeline0;
+    data.vsync.frameTimelines[1] = timeline1;
 
     Parcel p;
     data.writeToParcel(&p);
     p.setDataPosition(0);
 
-    VsyncEventData data2;
+    ParcelableVsyncEventData data2;
     data2.readFromParcel(&p);
-    ASSERT_EQ(data.id, data2.id);
-    ASSERT_EQ(data.deadlineTimestamp, data2.deadlineTimestamp);
-    ASSERT_EQ(data.frameInterval, data2.frameInterval);
-    ASSERT_EQ(data.preferredFrameTimelineIndex, data2.preferredFrameTimelineIndex);
-    for (int i = 0; i < data.frameTimelines.size(); i++) {
-        ASSERT_EQ(data.frameTimelines[i].id, data2.frameTimelines[i].id);
-        ASSERT_EQ(data.frameTimelines[i].deadlineTimestamp,
-                  data2.frameTimelines[i].deadlineTimestamp);
-        ASSERT_EQ(data.frameTimelines[i].expectedPresentTime,
-                  data2.frameTimelines[i].expectedPresentTime);
+    ASSERT_EQ(data.vsync.frameInterval, data2.vsync.frameInterval);
+    ASSERT_EQ(data.vsync.preferredFrameTimelineIndex, data2.vsync.preferredFrameTimelineIndex);
+    for (int i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
+        ASSERT_EQ(data.vsync.frameTimelines[i].vsyncId, data2.vsync.frameTimelines[i].vsyncId);
+        ASSERT_EQ(data.vsync.frameTimelines[i].deadlineTimestamp,
+                  data2.vsync.frameTimelines[i].deadlineTimestamp);
+        ASSERT_EQ(data.vsync.frameTimelines[i].expectedPresentationTime,
+                  data2.vsync.frameTimelines[i].expectedPresentationTime);
     }
 }
 
-TEST(FrameTimeline, Parcelling) {
-    FrameTimeline timeline = FrameTimeline(1, 2, 3);
-
-    Parcel p;
-    timeline.writeToParcel(&p);
-    p.setDataPosition(0);
-
-    FrameTimeline timeline2;
-    timeline2.readFromParcel(&p);
-    ASSERT_EQ(timeline.id, timeline2.id);
-    ASSERT_EQ(timeline.deadlineTimestamp, timeline2.deadlineTimestamp);
-    ASSERT_EQ(timeline.expectedPresentTime, timeline2.expectedPresentTime);
-}
-
 } // namespace test
 } // namespace android