blob: a95186916cea41672880856d630637cc35f65976 [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
47public class WindowAddRemovePerfTest extends WindowManagerPerfTestBase {
48 @Rule
49 public final PerfManualStatusReporter mPerfStatusReporter = new PerfManualStatusReporter();
50
51 @BeforeClass
52 public static void setUpClass() {
53 // Get the permission to use most window types.
54 sUiAutomation.adoptShellPermissionIdentity();
55 }
56
57 @AfterClass
58 public static void tearDownClass() {
59 sUiAutomation.dropShellPermissionIdentity();
60 }
61
62 @Test
63 @ManualBenchmarkTest(warmupDurationNs = WARMUP_DURATION, targetTestDurationNs = TEST_DURATION)
64 public void testAddRemoveWindow() throws Throwable {
65 new TestWindow().runBenchmark(mPerfStatusReporter.getBenchmarkState());
66 }
67
68 private static class TestWindow extends BaseIWindow {
69 final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
70 final Rect mOutFrame = new Rect();
71 final Rect mOutContentInsets = new Rect();
72 final Rect mOutStableInsets = new Rect();
73 final Rect mOutOutsets = new Rect();
74 final DisplayCutout.ParcelableWrapper mOutDisplayCutout =
75 new DisplayCutout.ParcelableWrapper();
76 final InsetsState mOutInsetsState = new InsetsState();
77
78 TestWindow() {
79 mLayoutParams.setTitle(TestWindow.class.getName());
80 mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
81 // Simulate as common phone window.
82 mLayoutParams.flags = FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
83 }
84
85 void runBenchmark(ManualBenchmarkState state) throws RemoteException {
86 final IWindowSession session = WindowManagerGlobal.getWindowSession();
87 long elapsedTimeNs = 0;
88 while (state.keepRunning(elapsedTimeNs)) {
89 // InputChannel cannot be reused.
90 final InputChannel inputChannel = new InputChannel();
91
92 long startTime = SystemClock.elapsedRealtimeNanos();
93 session.addToDisplay(this, mSeq, mLayoutParams, View.VISIBLE,
94 Display.DEFAULT_DISPLAY, mOutFrame, mOutContentInsets, mOutStableInsets,
95 mOutOutsets, mOutDisplayCutout, inputChannel, mOutInsetsState);
96 final long elapsedTimeNsOfAdd = SystemClock.elapsedRealtimeNanos() - startTime;
97 state.addExtraResult("add", elapsedTimeNsOfAdd);
98
99 startTime = SystemClock.elapsedRealtimeNanos();
100 session.remove(this);
101 final long elapsedTimeNsOfRemove = SystemClock.elapsedRealtimeNanos() - startTime;
102 state.addExtraResult("remove", elapsedTimeNsOfRemove);
103
104 elapsedTimeNs = elapsedTimeNsOfAdd + elapsedTimeNsOfRemove;
105 }
106 }
107 }
108}