1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <gtest/gtest.h>
18 #include <gui/DisplayEventReceiver.h>
19 
20 namespace android::test {
21 
22 #define CHECK_OFFSET(type, member, expected_offset) \
23     static_assert((offsetof(type, member) == (expected_offset)))
24 
TEST(DisplayEventStructLayoutTest,TestEventAlignment)25 TEST(DisplayEventStructLayoutTest, TestEventAlignment) {
26     static_assert(std::is_pod<DisplayEventReceiver::Event::VSync>::value);
27     CHECK_OFFSET(DisplayEventReceiver::Event, vsync, 24);
28     CHECK_OFFSET(DisplayEventReceiver::Event, hotplug, 24);
29     CHECK_OFFSET(DisplayEventReceiver::Event, modeChange, 24);
30 
31     CHECK_OFFSET(DisplayEventReceiver::Event::Header, type, 0);
32     CHECK_OFFSET(DisplayEventReceiver::Event::Header, displayId, 8);
33     CHECK_OFFSET(DisplayEventReceiver::Event::Header, timestamp, 16);
34 
35     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, count, 0);
36     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameInterval, 8);
37     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.preferredFrameTimelineIndex, 16);
38     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelinesLength, 20);
39     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines, 24);
40     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines[0].vsyncId, 24);
41     CHECK_OFFSET(DisplayEventReceiver::Event::VSync, vsyncData.frameTimelines[0].deadlineTimestamp,
42                  32);
43     CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
44                  vsyncData.frameTimelines[0].expectedPresentationTime, 40);
45     // Also test the offsets of the last frame timeline. A loop is not used because the non-const
46     // index cannot be used in static_assert.
47     const int lastFrameTimelineOffset = /* Start of array */ 24 +
48             (VsyncEventData::kFrameTimelinesCapacity - 1) * /* Size of FrameTimeline */ 24;
49     CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
50                  vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesCapacity - 1].vsyncId,
51                  lastFrameTimelineOffset);
52     CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
53                  vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesCapacity - 1]
54                          .deadlineTimestamp,
55                  lastFrameTimelineOffset + 8);
56     CHECK_OFFSET(DisplayEventReceiver::Event::VSync,
57                  vsyncData.frameTimelines[VsyncEventData::kFrameTimelinesCapacity - 1]
58                          .expectedPresentationTime,
59                  lastFrameTimelineOffset + 16);
60 
61     CHECK_OFFSET(DisplayEventReceiver::Event::Hotplug, connected, 0);
62     CHECK_OFFSET(DisplayEventReceiver::Event::Hotplug, connectionError, 4);
63 
64     CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, modeId, 0);
65     CHECK_OFFSET(DisplayEventReceiver::Event::ModeChange, vsyncPeriod, 8);
66 
67     CHECK_OFFSET(DisplayEventReceiver::Event::FrameRateOverride, uid, 0);
68     CHECK_OFFSET(DisplayEventReceiver::Event::FrameRateOverride, frameRateHz, 8);
69 }
70 
71 } // namespace android::test
72