blob: a22a638fd32ed56d691f02836480ecaca146d7fe [file] [log] [blame]
Riddle Hsu54a86c62019-07-10 21:37:08 +08001/*
2 * Copyright (C) 2019 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
17package android.wm;
18
19import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
20import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
21
22import android.graphics.Rect;
23import android.os.RemoteException;
24import android.os.SystemClock;
25import android.perftests.utils.ManualBenchmarkState;
26import android.perftests.utils.ManualBenchmarkState.ManualBenchmarkTest;
27import android.perftests.utils.PerfManualStatusReporter;
28import android.view.Display;
29import android.view.DisplayCutout;
30import android.view.IWindowSession;
31import android.view.InputChannel;
32import android.view.InsetsState;
33import android.view.View;
34import android.view.WindowManager;
35import android.view.WindowManagerGlobal;
36
37import androidx.test.filters.LargeTest;
38
39import com.android.internal.view.BaseIWindow;
40
41import org.junit.AfterClass;
42import org.junit.BeforeClass;
43import org.junit.Rule;
44import org.junit.Test;
45
46@LargeTest
Riddle Hsu4a725002019-11-11 20:50:13 +080047public class WindowAddRemovePerfTest extends WindowManagerPerfTestBase
48 implements ManualBenchmarkState.CustomizedIterationListener {
49
50 private static final int PROFILED_ITERATIONS = 2;
51
Riddle Hsu54a86c62019-07-10 21:37:08 +080052 @Rule
53 public final PerfManualStatusReporter mPerfStatusReporter = new PerfManualStatusReporter();
54
55 @BeforeClass
56 public static void setUpClass() {
57 // Get the permission to use most window types.
58 sUiAutomation.adoptShellPermissionIdentity();
59 }
60
61 @AfterClass
62 public static void tearDownClass() {
63 sUiAutomation.dropShellPermissionIdentity();
64 }
65
Riddle Hsu4a725002019-11-11 20:50:13 +080066 /** The last {@link #PROFILED_ITERATIONS} will provide the information of method profiling. */
67 @Override
68 public void onStart(int iteration) {
69 startProfiling(WindowAddRemovePerfTest.class.getSimpleName()
70 + "_MethodTracing_" + iteration + ".trace");
71 }
72
73 @Override
74 public void onFinished(int iteration) {
75 stopProfiling();
76 }
77
Riddle Hsu54a86c62019-07-10 21:37:08 +080078 @Test
Riddle Hsu5ef56dd62019-07-26 21:28:51 -060079 @ManualBenchmarkTest(warmupDurationNs = TIME_1_S_IN_NS, targetTestDurationNs = TIME_5_S_IN_NS)
Riddle Hsu54a86c62019-07-10 21:37:08 +080080 public void testAddRemoveWindow() throws Throwable {
Riddle Hsu4a725002019-11-11 20:50:13 +080081 final ManualBenchmarkState state = mPerfStatusReporter.getBenchmarkState();
82 state.setCustomizedIterations(PROFILED_ITERATIONS, this);
83 new TestWindow().runBenchmark(state);
Riddle Hsu54a86c62019-07-10 21:37:08 +080084 }
85
86 private static class TestWindow extends BaseIWindow {
87 final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
88 final Rect mOutFrame = new Rect();
89 final Rect mOutContentInsets = new Rect();
90 final Rect mOutStableInsets = new Rect();
91 final Rect mOutOutsets = new Rect();
92 final DisplayCutout.ParcelableWrapper mOutDisplayCutout =
93 new DisplayCutout.ParcelableWrapper();
94 final InsetsState mOutInsetsState = new InsetsState();
95
96 TestWindow() {
97 mLayoutParams.setTitle(TestWindow.class.getName());
98 mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
99 // Simulate as common phone window.
100 mLayoutParams.flags = FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
101 }
102
103 void runBenchmark(ManualBenchmarkState state) throws RemoteException {
104 final IWindowSession session = WindowManagerGlobal.getWindowSession();
105 long elapsedTimeNs = 0;
106 while (state.keepRunning(elapsedTimeNs)) {
107 // InputChannel cannot be reused.
108 final InputChannel inputChannel = new InputChannel();
109
110 long startTime = SystemClock.elapsedRealtimeNanos();
111 session.addToDisplay(this, mSeq, mLayoutParams, View.VISIBLE,
112 Display.DEFAULT_DISPLAY, mOutFrame, mOutContentInsets, mOutStableInsets,
113 mOutOutsets, mOutDisplayCutout, inputChannel, mOutInsetsState);
114 final long elapsedTimeNsOfAdd = SystemClock.elapsedRealtimeNanos() - startTime;
115 state.addExtraResult("add", elapsedTimeNsOfAdd);
116
117 startTime = SystemClock.elapsedRealtimeNanos();
118 session.remove(this);
119 final long elapsedTimeNsOfRemove = SystemClock.elapsedRealtimeNanos() - startTime;
120 state.addExtraResult("remove", elapsedTimeNsOfRemove);
121
122 elapsedTimeNs = elapsedTimeNsOfAdd + elapsedTimeNsOfRemove;
Riddle Hsu4a725002019-11-11 20:50:13 +0800123 inputChannel.dispose();
Riddle Hsu54a86c62019-07-10 21:37:08 +0800124 }
125 }
126 }
127}