blob: 14e1aac7932982fdfe25fd0aa49d382f4e254670 [file] [log] [blame]
Vishnu Nair6b591152021-10-08 11:45:14 -07001/*
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
23#include "Tracing/TransactionProtoParser.h"
24
25using namespace android::surfaceflinger;
26
27namespace android {
28
29TEST(TransactionProtoParserTest, parse) {
Ady Abrahamd11bade2022-08-01 16:18:03 -070030 const sp<IBinder> layerHandle = sp<BBinder>::make();
31 const sp<IBinder> displayHandle = sp<BBinder>::make();
Vishnu Nair6b591152021-10-08 11:45:14 -070032 TransactionState t1;
33 t1.originPid = 1;
34 t1.originUid = 2;
35 t1.frameTimelineInfo.vsyncId = 3;
36 t1.frameTimelineInfo.inputEventId = 4;
37 t1.postTime = 5;
38
39 layer_state_t layer;
40 layer.layerId = 6;
41 layer.what = std::numeric_limits<uint64_t>::max();
Vishnu Nair9f0835e2022-01-07 09:33:19 -080042 layer.what &= ~static_cast<uint64_t>(layer_state_t::eBufferChanged);
Vishnu Nair6b591152021-10-08 11:45:14 -070043 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 ComposerState s;
50 if (i == 1) {
51 layer.parentSurfaceControlForChild =
Patrick Williamsa361de62022-10-06 20:34:10 +000052 sp<SurfaceControl>::make(SurfaceComposerClient::getDefault(), layerHandle, 42,
53 "#42");
Vishnu Nair6b591152021-10-08 11:45:14 -070054 }
55 s.state = layer;
56 t1.states.add(s);
57 }
58
59 size_t displayCount = 2;
60 t1.displays.reserve(displayCount);
61 for (uint32_t i = 0; i < displayCount; i++) {
62 DisplayState display;
63 display.what = std::numeric_limits<uint32_t>::max();
64 if (i == 0) {
65 display.token = displayHandle;
66 } else {
67 display.token = nullptr;
68 }
69 display.width = 85;
70 t1.displays.add(display);
71 }
72
Vishnu Nair685cfef2022-02-02 10:01:25 -080073 class TestMapper : public TransactionProtoParser::FlingerDataMapper {
74 public:
75 sp<IBinder> layerHandle;
76 sp<IBinder> displayHandle;
77
78 TestMapper(sp<IBinder> layerHandle, sp<IBinder> displayHandle)
79 : layerHandle(layerHandle), displayHandle(displayHandle) {}
80
81 sp<IBinder> getLayerHandle(int32_t id) const override {
82 return (id == 42) ? layerHandle : nullptr;
83 }
Robert Carra63d52a2022-03-03 08:03:37 -080084 int64_t getLayerId(const sp<IBinder>& handle) const override {
Vishnu Nair685cfef2022-02-02 10:01:25 -080085 return (handle == layerHandle) ? 42 : -1;
86 }
87 sp<IBinder> getDisplayHandle(int32_t id) const {
88 return (id == 43) ? displayHandle : nullptr;
89 }
90 int32_t getDisplayId(const sp<IBinder>& handle) const {
91 return (handle == displayHandle) ? 43 : -1;
92 }
Vishnu Nair6b591152021-10-08 11:45:14 -070093 };
94
Vishnu Nair685cfef2022-02-02 10:01:25 -080095 TransactionProtoParser parser(std::make_unique<TestMapper>(layerHandle, displayHandle));
96
97 proto::TransactionState proto = parser.toProto(t1);
98 TransactionState t2 = parser.fromProto(proto);
Vishnu Nair6b591152021-10-08 11:45:14 -070099
100 ASSERT_EQ(t1.originPid, t2.originPid);
101 ASSERT_EQ(t1.originUid, t2.originUid);
102 ASSERT_EQ(t1.frameTimelineInfo.vsyncId, t2.frameTimelineInfo.vsyncId);
103 ASSERT_EQ(t1.frameTimelineInfo.inputEventId, t2.frameTimelineInfo.inputEventId);
104 ASSERT_EQ(t1.postTime, t2.postTime);
105 ASSERT_EQ(t1.states.size(), t2.states.size());
106 ASSERT_EQ(t1.states[0].state.x, t2.states[0].state.x);
107 ASSERT_EQ(t1.states[0].state.matrix.dsdx, t2.states[0].state.matrix.dsdx);
108 ASSERT_EQ(t1.states[1].state.parentSurfaceControlForChild->getHandle(),
109 t2.states[1].state.parentSurfaceControlForChild->getHandle());
110
111 ASSERT_EQ(t1.displays.size(), t2.displays.size());
112 ASSERT_EQ(t1.displays[1].width, t2.displays[1].width);
113 ASSERT_EQ(t1.displays[0].token, t2.displays[0].token);
114}
115
116} // namespace android