File: TransactionTracingTest.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (285 lines) | stat: -rw-r--r-- 12,110 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright 2021 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 <gmock/gmock.h>
#include <gtest/gtest.h>

#include <gui/SurfaceComposerClient.h>

#include "Tracing/RingBuffer.h"
#include "Tracing/TransactionTracing.h"

using namespace android::surfaceflinger;

namespace android {

class TransactionTracingTest : public testing::Test {
protected:
    static constexpr size_t SMALL_BUFFER_SIZE = 1024;
    TransactionTracing mTracing;

    void flush(int64_t vsyncId) { mTracing.flush(vsyncId); }
    proto::TransactionTraceFile writeToProto() { return mTracing.writeToProto(); }

    proto::TransactionTraceEntry bufferFront() {
        std::scoped_lock<std::mutex> lock(mTracing.mTraceLock);
        proto::TransactionTraceEntry entry;
        entry.ParseFromString(mTracing.mBuffer.front());
        return entry;
    }

    void queueAndCommitTransaction(int64_t vsyncId) {
        TransactionState transaction;
        transaction.id = static_cast<uint64_t>(vsyncId * 3);
        transaction.originUid = 1;
        transaction.originPid = 2;
        mTracing.addQueuedTransaction(transaction);
        std::vector<TransactionState> transactions;
        transactions.emplace_back(transaction);
        mTracing.addCommittedTransactions(transactions, vsyncId);
        flush(vsyncId);
    }

    void verifyEntry(const proto::TransactionTraceEntry& actualProto,
                     const std::vector<TransactionState>& expectedTransactions,
                     int64_t expectedVsyncId) {
        EXPECT_EQ(actualProto.vsync_id(), expectedVsyncId);
        EXPECT_EQ(actualProto.transactions().size(),
                  static_cast<int32_t>(expectedTransactions.size()));
        for (uint32_t i = 0; i < expectedTransactions.size(); i++) {
            EXPECT_EQ(actualProto.transactions(static_cast<int32_t>(i)).pid(),
                      expectedTransactions[i].originPid);
        }
    }
};

TEST_F(TransactionTracingTest, addTransactions) {
    std::vector<TransactionState> transactions;
    transactions.reserve(100);
    for (uint64_t i = 0; i < 100; i++) {
        TransactionState transaction;
        transaction.id = i;
        transaction.originPid = static_cast<int32_t>(i);
        transactions.emplace_back(transaction);
        mTracing.addQueuedTransaction(transaction);
    }

    // Split incoming transactions into two and commit them in reverse order to test out of order
    // commits.
    std::vector<TransactionState> firstTransactionSet =
            std::vector<TransactionState>(transactions.begin() + 50, transactions.end());
    int64_t firstTransactionSetVsyncId = 42;
    mTracing.addCommittedTransactions(firstTransactionSet, firstTransactionSetVsyncId);

    int64_t secondTransactionSetVsyncId = 43;
    std::vector<TransactionState> secondTransactionSet =
            std::vector<TransactionState>(transactions.begin(), transactions.begin() + 50);
    mTracing.addCommittedTransactions(secondTransactionSet, secondTransactionSetVsyncId);
    flush(secondTransactionSetVsyncId);

    proto::TransactionTraceFile proto = writeToProto();
    EXPECT_EQ(proto.entry().size(), 2);
    verifyEntry(proto.entry(0), firstTransactionSet, firstTransactionSetVsyncId);
    verifyEntry(proto.entry(1), secondTransactionSet, secondTransactionSetVsyncId);
}

class TransactionTracingLayerHandlingTest : public TransactionTracingTest {
protected:
    void SetUp() override {
        // add layers
        mTracing.setBufferSize(SMALL_BUFFER_SIZE);
        const sp<IBinder> fakeLayerHandle = new BBinder();
        mTracing.onLayerAdded(fakeLayerHandle->localBinder(), mParentLayerId, "parent",
                              123 /* flags */, -1 /* parentId */);
        const sp<IBinder> fakeChildLayerHandle = new BBinder();
        mTracing.onLayerAdded(fakeChildLayerHandle->localBinder(), mChildLayerId, "child",
                              456 /* flags */, mParentLayerId);

        // add some layer transaction
        {
            TransactionState transaction;
            transaction.id = 50;
            ComposerState layerState;
            layerState.state.surface = fakeLayerHandle;
            layerState.state.what = layer_state_t::eLayerChanged;
            layerState.state.z = 42;
            transaction.states.add(layerState);
            ComposerState childState;
            childState.state.surface = fakeChildLayerHandle;
            childState.state.what = layer_state_t::eLayerChanged;
            childState.state.z = 43;
            transaction.states.add(childState);
            mTracing.addQueuedTransaction(transaction);

            std::vector<TransactionState> transactions;
            transactions.emplace_back(transaction);
            VSYNC_ID_FIRST_LAYER_CHANGE = ++mVsyncId;
            mTracing.addCommittedTransactions(transactions, VSYNC_ID_FIRST_LAYER_CHANGE);
            flush(VSYNC_ID_FIRST_LAYER_CHANGE);
        }

        // add transactions that modify the layer state further so we can test that layer state
        // gets merged
        {
            TransactionState transaction;
            transaction.id = 51;
            ComposerState layerState;
            layerState.state.surface = fakeLayerHandle;
            layerState.state.what = layer_state_t::eLayerChanged | layer_state_t::ePositionChanged;
            layerState.state.z = 41;
            layerState.state.x = 22;
            transaction.states.add(layerState);
            mTracing.addQueuedTransaction(transaction);

            std::vector<TransactionState> transactions;
            transactions.emplace_back(transaction);
            VSYNC_ID_SECOND_LAYER_CHANGE = ++mVsyncId;
            mTracing.addCommittedTransactions(transactions, VSYNC_ID_SECOND_LAYER_CHANGE);
            flush(VSYNC_ID_SECOND_LAYER_CHANGE);
        }

        // remove child layer
        mTracing.onLayerRemoved(2);
        VSYNC_ID_CHILD_LAYER_REMOVED = ++mVsyncId;
        queueAndCommitTransaction(VSYNC_ID_CHILD_LAYER_REMOVED);

        // remove layer
        mTracing.onLayerRemoved(1);
        queueAndCommitTransaction(++mVsyncId);
    }

    int mParentLayerId = 1;
    int mChildLayerId = 2;
    int64_t mVsyncId = 0;
    int64_t VSYNC_ID_FIRST_LAYER_CHANGE;
    int64_t VSYNC_ID_SECOND_LAYER_CHANGE;
    int64_t VSYNC_ID_CHILD_LAYER_REMOVED;
};

TEST_F(TransactionTracingLayerHandlingTest, addStartingState) {
    // add transactions until we drop the transaction with the first layer change
    while (bufferFront().vsync_id() <= VSYNC_ID_FIRST_LAYER_CHANGE) {
        queueAndCommitTransaction(++mVsyncId);
    }
    proto::TransactionTraceFile proto = writeToProto();
    // verify we can still retrieve the layer change from the first entry containing starting
    // states.
    EXPECT_GT(proto.entry().size(), 0);
    EXPECT_EQ(proto.entry(0).transactions().size(), 1);
    EXPECT_EQ(proto.entry(0).added_layers().size(), 2);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 2);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).z(), 42);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(1).layer_id(), mChildLayerId);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(1).z(), 43);
}

TEST_F(TransactionTracingLayerHandlingTest, updateStartingState) {
    // add transactions until we drop the transaction with the second layer change
    while (bufferFront().vsync_id() <= VSYNC_ID_SECOND_LAYER_CHANGE) {
        queueAndCommitTransaction(++mVsyncId);
    }
    proto::TransactionTraceFile proto = writeToProto();
    // verify starting states are updated correctly
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).z(), 41);
}

TEST_F(TransactionTracingLayerHandlingTest, removeStartingState) {
    // add transactions until we drop the transaction which removes the child layer
    while (bufferFront().vsync_id() <= VSYNC_ID_CHILD_LAYER_REMOVED) {
        queueAndCommitTransaction(++mVsyncId);
    }
    proto::TransactionTraceFile proto = writeToProto();
    // verify the child layer has been removed from the trace
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 1);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId);
}

TEST_F(TransactionTracingLayerHandlingTest, startingStateSurvivesBufferFlush) {
    // add transactions until we drop the transaction with the second layer change
    while (bufferFront().vsync_id() <= VSYNC_ID_SECOND_LAYER_CHANGE) {
        queueAndCommitTransaction(++mVsyncId);
    }
    proto::TransactionTraceFile proto = writeToProto();
    // verify we have two starting states
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 2);

    // Continue adding transactions until child layer is removed
    while (bufferFront().vsync_id() <= VSYNC_ID_CHILD_LAYER_REMOVED) {
        queueAndCommitTransaction(++mVsyncId);
    }
    proto = writeToProto();
    // verify we still have the parent layer state
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 1);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(0).layer_id(), mParentLayerId);
}

class TransactionTracingMirrorLayerTest : public TransactionTracingTest {
protected:
    void SetUp() override {
        // add layers
        mTracing.setBufferSize(SMALL_BUFFER_SIZE);
        const sp<IBinder> fakeLayerHandle = new BBinder();
        mTracing.onLayerAdded(fakeLayerHandle->localBinder(), mLayerId, "Test Layer",
                              123 /* flags */, -1 /* parentId */);
        const sp<IBinder> fakeMirrorLayerHandle = new BBinder();
        mTracing.onMirrorLayerAdded(fakeMirrorLayerHandle->localBinder(), mMirrorLayerId, "Mirror",
                                    mLayerId);

        // add some layer transaction
        {
            TransactionState transaction;
            transaction.id = 50;
            ComposerState layerState;
            layerState.state.surface = fakeLayerHandle;
            layerState.state.what = layer_state_t::eLayerChanged;
            layerState.state.z = 42;
            transaction.states.add(layerState);
            ComposerState mirrorState;
            mirrorState.state.surface = fakeMirrorLayerHandle;
            mirrorState.state.what = layer_state_t::eLayerChanged;
            mirrorState.state.z = 43;
            transaction.states.add(mirrorState);
            mTracing.addQueuedTransaction(transaction);

            std::vector<TransactionState> transactions;
            transactions.emplace_back(transaction);
            mTracing.addCommittedTransactions(transactions, ++mVsyncId);
            flush(mVsyncId);
        }
    }

    int mLayerId = 5;
    int mMirrorLayerId = 55;
    int64_t mVsyncId = 0;
    int64_t VSYNC_ID_FIRST_LAYER_CHANGE;
    int64_t VSYNC_ID_SECOND_LAYER_CHANGE;
    int64_t VSYNC_ID_CHILD_LAYER_REMOVED;
};

TEST_F(TransactionTracingMirrorLayerTest, canAddMirrorLayers) {
    proto::TransactionTraceFile proto = writeToProto();
    // We don't have any starting states since no layer was removed from.
    EXPECT_EQ(proto.entry().size(), 1);

    // Verify the mirror layer was added
    EXPECT_EQ(proto.entry(0).transactions().size(), 1);
    EXPECT_EQ(proto.entry(0).added_layers().size(), 2);
    EXPECT_EQ(proto.entry(0).added_layers(1).layer_id(), mMirrorLayerId);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes().size(), 2);
    EXPECT_EQ(proto.entry(0).transactions(0).layer_changes(1).z(), 43);
}
} // namespace android