Split VsyncEventData from DisplayEventDispatcher.

Bug: 205721584
Test: atest libsurfaceflinger_unittest
Change-Id: I51b18ed356ae7a29f8a88634346c0025321dbe08
diff --git a/libs/gui/Android.bp b/libs/gui/Android.bp
index ec3587b..e17b2c8 100644
--- a/libs/gui/Android.bp
+++ b/libs/gui/Android.bp
@@ -198,6 +198,7 @@
         "SurfaceComposerClient.cpp",
         "SyncFeatures.cpp",
         "TransactionTracing.cpp",
+        "VsyncEventData.cpp",
         "view/Surface.cpp",
         "WindowInfosListenerReporter.cpp",
         "bufferqueue/1.0/B2HProducerListener.cpp",
diff --git a/libs/gui/DisplayEventDispatcher.cpp b/libs/gui/DisplayEventDispatcher.cpp
index ee80082..26db59b 100644
--- a/libs/gui/DisplayEventDispatcher.cpp
+++ b/libs/gui/DisplayEventDispatcher.cpp
@@ -148,14 +148,13 @@
 
 void DisplayEventDispatcher::populateFrameTimelines(const DisplayEventReceiver::Event& event,
                                                     VsyncEventData* outVsyncEventData) const {
-    for (size_t i = 0; i < DisplayEventReceiver::kFrameTimelinesLength; i++) {
+    for (size_t i = 0; i < VsyncEventData::kFrameTimelinesLength; i++) {
         DisplayEventReceiver::Event::VSync::FrameTimeline receiverTimeline =
                 event.vsync.frameTimelines[i];
-        outVsyncEventData->frameTimelines[i] = {.id = receiverTimeline.vsyncId,
-                                                .deadlineTimestamp =
-                                                        receiverTimeline.deadlineTimestamp,
-                                                .expectedPresentTime =
-                                                        receiverTimeline.expectedVSyncTimestamp};
+        outVsyncEventData->frameTimelines[i] =
+                VsyncEventData::FrameTimeline(receiverTimeline.vsyncId,
+                                              receiverTimeline.deadlineTimestamp,
+                                              receiverTimeline.expectedVSyncTimestamp);
     }
 }
 
diff --git a/libs/gui/VsyncEventData.cpp b/libs/gui/VsyncEventData.cpp
new file mode 100644
index 0000000..aad81d0
--- /dev/null
+++ b/libs/gui/VsyncEventData.cpp
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "gui/VsyncEventData.h"
+#include <gui/DisplayEventReceiver.h>
+#include <private/gui/ParcelUtils.h>
+#include <utils/Log.h>
+#include <utils/Looper.h>
+#include <cstdint>
+
+namespace android::gui {
+
+status_t VsyncEventData::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);
+
+    uint64_t uintPreferredFrameTimelineIndex;
+    SAFE_PARCEL(parcel->readUint64, &uintPreferredFrameTimelineIndex);
+    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;
+    }
+
+    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);
+
+    return OK;
+}
+
+}; // namespace android::gui
diff --git a/libs/gui/include/gui/DisplayEventDispatcher.h b/libs/gui/include/gui/DisplayEventDispatcher.h
index 40621dd..71968fa 100644
--- a/libs/gui/include/gui/DisplayEventDispatcher.h
+++ b/libs/gui/include/gui/DisplayEventDispatcher.h
@@ -17,45 +17,10 @@
 #include <gui/DisplayEventReceiver.h>
 #include <utils/Log.h>
 #include <utils/Looper.h>
-#include <array>
 
 namespace android {
 using FrameRateOverride = DisplayEventReceiver::Event::FrameRateOverride;
 
-struct VsyncEventData {
-    // 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;
-
-    // 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 current frame interval in ns when this frame was scheduled.
-    int64_t frameInterval = 0;
-
-    struct 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;
-
-        // 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 anticipated Vsync present time.
-        int64_t expectedPresentTime = 0;
-    };
-
-    // Sorted possible frame timelines.
-    std::array<FrameTimeline, DisplayEventReceiver::kFrameTimelinesLength> frameTimelines;
-
-    // Index into the frameTimelines that represents the platform's preferred frame timeline.
-    size_t preferredFrameTimelineIndex = std::numeric_limits<size_t>::max();
-};
-
 class DisplayEventDispatcher : public LooperCallback {
 public:
     explicit DisplayEventDispatcher(
diff --git a/libs/gui/include/gui/DisplayEventReceiver.h b/libs/gui/include/gui/DisplayEventReceiver.h
index 456bbfb..db41c32 100644
--- a/libs/gui/include/gui/DisplayEventReceiver.h
+++ b/libs/gui/include/gui/DisplayEventReceiver.h
@@ -26,6 +26,7 @@
 
 #include <binder/IInterface.h>
 #include <gui/ISurfaceComposer.h>
+#include <gui/VsyncEventData.h>
 
 // ----------------------------------------------------------------------------
 
@@ -34,6 +35,7 @@
 // ----------------------------------------------------------------------------
 
 using gui::IDisplayEventConnection;
+using gui::VsyncEventData;
 
 namespace gui {
 class BitTube;
@@ -49,8 +51,6 @@
 // ----------------------------------------------------------------------------
 class DisplayEventReceiver {
 public:
-    // Max amount of frame timelines is arbitrarily set to be reasonable.
-    static constexpr int64_t kFrameTimelinesLength = 7;
 
     enum {
         DISPLAY_EVENT_VSYNC = fourcc('v', 's', 'y', 'n'),
@@ -85,7 +85,7 @@
                 nsecs_t expectedVSyncTimestamp __attribute__((aligned(8)));
                 nsecs_t deadlineTimestamp __attribute__((aligned(8)));
                 int64_t vsyncId;
-            } frameTimelines[kFrameTimelinesLength];
+            } frameTimelines[VsyncEventData::kFrameTimelinesLength];
         };
 
         struct Hotplug {
diff --git a/libs/gui/include/gui/VsyncEventData.h b/libs/gui/include/gui/VsyncEventData.h
new file mode 100644
index 0000000..abac61c
--- /dev/null
+++ b/libs/gui/include/gui/VsyncEventData.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <gui/FrameTimelineInfo.h>
+
+#include <array>
+
+namespace android::gui {
+struct VsyncEventData : public Parcelable {
+    // 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;
+
+    struct FrameTimeline : public Parcelable {
+        FrameTimeline() = default;
+        FrameTimeline(int64_t id, int64_t deadlineTimestamp, int64_t expectedPresentTime)
+              : id(id),
+                deadlineTimestamp(deadlineTimestamp),
+                expectedPresentTime(expectedPresentTime) {}
+
+        // 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;
+
+        // 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 anticipated Vsync present time.
+        int64_t expectedPresentTime = 0;
+
+        status_t readFromParcel(const Parcel*) override;
+        status_t writeToParcel(Parcel*) const override;
+    };
+
+    // Sorted possible frame timelines.
+    std::array<FrameTimeline, kFrameTimelinesLength> frameTimelines;
+
+    // Index into the frameTimelines that represents the platform's preferred frame timeline.
+    size_t preferredFrameTimelineIndex = std::numeric_limits<size_t>::max();
+
+    status_t readFromParcel(const Parcel*) override;
+    status_t writeToParcel(Parcel*) const override;
+};
+} // namespace android::gui
diff --git a/libs/gui/tests/Android.bp b/libs/gui/tests/Android.bp
index 6dd1073..e58543a 100644
--- a/libs/gui/tests/Android.bp
+++ b/libs/gui/tests/Android.bp
@@ -44,6 +44,7 @@
         "SurfaceTextureMultiContextGL_test.cpp",
         "Surface_test.cpp",
         "TextureRenderer.cpp",
+        "VsyncEventData_test.cpp",
         "WindowInfo_test.cpp",
     ],
 
diff --git a/libs/gui/tests/VsyncEventData_test.cpp b/libs/gui/tests/VsyncEventData_test.cpp
new file mode 100644
index 0000000..a670d42
--- /dev/null
+++ b/libs/gui/tests/VsyncEventData_test.cpp
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <gtest/gtest.h>
+
+#include <binder/Parcel.h>
+
+#include <gui/VsyncEventData.h>
+
+namespace android {
+
+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;
+
+    Parcel p;
+    data.writeToParcel(&p);
+    p.setDataPosition(0);
+
+    VsyncEventData 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);
+    }
+}
+
+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