1 /*
2  * Copyright 2021 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 <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 #include <limits> // std::numeric_limits
20 
21 #include <gui/SurfaceComposerClient.h>
22 #include <ui/Rotation.h>
23 #include "LayerProtoHelper.h"
24 
25 #include "Tracing/TransactionProtoParser.h"
26 
27 using namespace android::surfaceflinger;
28 
29 namespace android {
30 
TEST(TransactionProtoParserTest,parse)31 TEST(TransactionProtoParserTest, parse) {
32     const sp<IBinder> displayHandle = sp<BBinder>::make();
33     TransactionState t1;
34     t1.originPid = 1;
35     t1.originUid = 2;
36     t1.frameTimelineInfo.vsyncId = 3;
37     t1.frameTimelineInfo.inputEventId = 4;
38     t1.postTime = 5;
39 
40     layer_state_t layer;
41     layer.what = std::numeric_limits<uint64_t>::max();
42     layer.what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
43     layer.x = 7;
44     layer.matrix.dsdx = 15;
45 
46     size_t layerCount = 2;
47     t1.states.reserve(layerCount);
48     for (uint32_t i = 0; i < layerCount; i++) {
49         ResolvedComposerState s;
50         if (i == 1) {
51             s.parentId = 42;
52         }
53         s.layerId = 6 + i;
54         s.state = layer;
55         t1.states.emplace_back(s);
56     }
57 
58     size_t displayCount = 2;
59     t1.displays.reserve(displayCount);
60     for (uint32_t i = 0; i < displayCount; i++) {
61         DisplayState display;
62         display.what = std::numeric_limits<uint32_t>::max();
63         if (i == 0) {
64             display.token = displayHandle;
65         } else {
66             display.token = nullptr;
67         }
68         display.width = 85;
69         t1.displays.add(display);
70     }
71 
72     class TestMapper : public TransactionProtoParser::FlingerDataMapper {
73     public:
74         sp<IBinder> displayHandle;
75 
76         TestMapper(sp<IBinder> displayHandle) : displayHandle(displayHandle) {}
77 
78         sp<IBinder> getDisplayHandle(int32_t id) const {
79             return (id == 43) ? displayHandle : nullptr;
80         }
81         int32_t getDisplayId(const sp<IBinder>& handle) const {
82             return (handle == displayHandle) ? 43 : -1;
83         }
84     };
85 
86     TransactionProtoParser parser(std::make_unique<TestMapper>(displayHandle));
87 
88     perfetto::protos::TransactionState proto = parser.toProto(t1);
89     TransactionState t2 = parser.fromProto(proto);
90 
91     ASSERT_EQ(t1.originPid, t2.originPid);
92     ASSERT_EQ(t1.originUid, t2.originUid);
93     ASSERT_EQ(t1.frameTimelineInfo.vsyncId, t2.frameTimelineInfo.vsyncId);
94     ASSERT_EQ(t1.frameTimelineInfo.inputEventId, t2.frameTimelineInfo.inputEventId);
95     ASSERT_EQ(t1.postTime, t2.postTime);
96     ASSERT_EQ(t1.states.size(), t2.states.size());
97     ASSERT_EQ(t1.states[0].state.x, t2.states[0].state.x);
98     ASSERT_EQ(t1.states[0].state.matrix.dsdx, t2.states[0].state.matrix.dsdx);
99     ASSERT_EQ(t1.states[1].layerId, t2.states[1].layerId);
100     ASSERT_EQ(t1.states[1].parentId, t2.states[1].parentId);
101 
102     ASSERT_EQ(t1.displays.size(), t2.displays.size());
103     ASSERT_EQ(t1.displays[1].width, t2.displays[1].width);
104     ASSERT_EQ(t1.displays[0].token, t2.displays[0].token);
105 }
106 
TEST(TransactionProtoParserTest,parseDisplayInfo)107 TEST(TransactionProtoParserTest, parseDisplayInfo) {
108     frontend::DisplayInfo d1;
109     d1.info.displayId = ui::LogicalDisplayId{42};
110     d1.info.logicalWidth = 43;
111     d1.info.logicalHeight = 44;
112     d1.info.transform.set(1, 2, 3, 4);
113     d1.transform = d1.info.transform.inverse();
114     d1.receivesInput = true;
115     d1.isSecure = false;
116     d1.isPrimary = true;
117     d1.isVirtual = false;
118     d1.rotationFlags = ui::Transform::ROT_180;
119     d1.transformHint = ui::Transform::ROT_90;
120 
121     const uint32_t layerStack = 2;
122     google::protobuf::RepeatedPtrField<perfetto::protos::DisplayInfo> displayProtos;
123     auto displayInfoProto = displayProtos.Add();
124     *displayInfoProto = TransactionProtoParser::toProto(d1, layerStack);
125     frontend::DisplayInfos displayInfos;
126     TransactionProtoParser::fromProto(displayProtos, displayInfos);
127 
128     ASSERT_TRUE(displayInfos.contains(ui::LayerStack::fromValue(layerStack)));
129     frontend::DisplayInfo d2 = displayInfos.get(ui::LayerStack::fromValue(layerStack))->get();
130     EXPECT_EQ(d1.info.displayId, d2.info.displayId);
131     EXPECT_EQ(d1.info.logicalWidth, d2.info.logicalWidth);
132     EXPECT_EQ(d1.info.logicalHeight, d2.info.logicalHeight);
133 
134     EXPECT_EQ(d1.info.transform.dsdx(), d2.info.transform.dsdx());
135     EXPECT_EQ(d1.info.transform.dsdy(), d2.info.transform.dsdy());
136     EXPECT_EQ(d1.info.transform.dtdx(), d2.info.transform.dtdx());
137     EXPECT_EQ(d1.info.transform.dtdy(), d2.info.transform.dtdy());
138 
139     EXPECT_EQ(d1.transform.dsdx(), d2.transform.dsdx());
140     EXPECT_EQ(d1.transform.dsdy(), d2.transform.dsdy());
141     EXPECT_EQ(d1.transform.dtdx(), d2.transform.dtdx());
142     EXPECT_EQ(d1.transform.dtdy(), d2.transform.dtdy());
143 
144     EXPECT_EQ(d1.receivesInput, d2.receivesInput);
145     EXPECT_EQ(d1.isSecure, d2.isSecure);
146     EXPECT_EQ(d1.isVirtual, d2.isVirtual);
147     EXPECT_EQ(d1.rotationFlags, d2.rotationFlags);
148     EXPECT_EQ(d1.transformHint, d2.transformHint);
149 }
150 
151 } // namespace android
152