1 /*
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 
17 package android.wm;
18 
19 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR;
20 import static android.view.WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
21 
22 import android.os.RemoteException;
23 import android.os.SystemClock;
24 import android.perftests.utils.ManualBenchmarkState;
25 import android.perftests.utils.ManualBenchmarkState.ManualBenchmarkTest;
26 import android.perftests.utils.PerfManualStatusReporter;
27 import android.view.Display;
28 import android.view.IWindowSession;
29 import android.view.InputChannel;
30 import android.view.InsetsSourceControl;
31 import android.view.InsetsState;
32 import android.view.InsetsVisibilities;
33 import android.view.View;
34 import android.view.WindowManager;
35 import android.view.WindowManagerGlobal;
36 
37 import androidx.test.filters.LargeTest;
38 
39 import com.android.internal.view.BaseIWindow;
40 
41 import org.junit.AfterClass;
42 import org.junit.BeforeClass;
43 import org.junit.Rule;
44 import org.junit.Test;
45 
46 @LargeTest
47 public class WindowAddRemovePerfTest extends WindowManagerPerfTestBase
48         implements ManualBenchmarkState.CustomizedIterationListener {
49 
50     @Rule
51     public final PerfManualStatusReporter mPerfStatusReporter = new PerfManualStatusReporter();
52 
53     @BeforeClass
setUpClass()54     public static void setUpClass() {
55         // Get the permission to use most window types.
56         getUiAutomation().adoptShellPermissionIdentity();
57     }
58 
59     @AfterClass
tearDownClass()60     public static void tearDownClass() {
61         getUiAutomation().dropShellPermissionIdentity();
62     }
63 
64     /** The last customized iterations will provide the information of method profiling. */
65     @Override
onStart(int iteration)66     public void onStart(int iteration) {
67         startProfiling(WindowAddRemovePerfTest.class.getSimpleName()
68                 + "_MethodTracing_" + iteration + ".trace");
69     }
70 
71     @Override
onFinished(int iteration)72     public void onFinished(int iteration) {
73         stopProfiling();
74     }
75 
76     @Test
77     @ManualBenchmarkTest(warmupDurationNs = TIME_1_S_IN_NS, targetTestDurationNs = TIME_5_S_IN_NS)
testAddRemoveWindow()78     public void testAddRemoveWindow() throws Throwable {
79         final ManualBenchmarkState state = mPerfStatusReporter.getBenchmarkState();
80         state.setCustomizedIterations(getProfilingIterations(), this);
81         new TestWindow().runBenchmark(state);
82     }
83 
84     private static class TestWindow extends BaseIWindow {
85         final WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams();
86         final InsetsVisibilities mRequestedVisibilities = new InsetsVisibilities();
87         final InsetsState mOutInsetsState = new InsetsState();
88         final InsetsSourceControl[] mOutControls = new InsetsSourceControl[0];
89 
TestWindow()90         TestWindow() {
91             mLayoutParams.setTitle(TestWindow.class.getName());
92             mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
93             // Simulate as common phone window.
94             mLayoutParams.flags = FLAG_LAYOUT_IN_SCREEN | FLAG_LAYOUT_INSET_DECOR;
95         }
96 
runBenchmark(ManualBenchmarkState state)97         void runBenchmark(ManualBenchmarkState state) throws RemoteException {
98             final IWindowSession session = WindowManagerGlobal.getWindowSession();
99             long elapsedTimeNs = 0;
100             while (state.keepRunning(elapsedTimeNs)) {
101                 // InputChannel cannot be reused.
102                 final InputChannel inputChannel = new InputChannel();
103 
104                 long startTime = SystemClock.elapsedRealtimeNanos();
105                 session.addToDisplay(this, mLayoutParams, View.VISIBLE,
106                         Display.DEFAULT_DISPLAY, mRequestedVisibilities, inputChannel,
107                         mOutInsetsState, mOutControls);
108                 final long elapsedTimeNsOfAdd = SystemClock.elapsedRealtimeNanos() - startTime;
109                 state.addExtraResult("add", elapsedTimeNsOfAdd);
110 
111                 startTime = SystemClock.elapsedRealtimeNanos();
112                 session.remove(this);
113                 final long elapsedTimeNsOfRemove = SystemClock.elapsedRealtimeNanos() - startTime;
114                 state.addExtraResult("remove", elapsedTimeNsOfRemove);
115 
116                 elapsedTimeNs = elapsedTimeNsOfAdd + elapsedTimeNsOfRemove;
117                 inputChannel.dispose();
118             }
119         }
120     }
121 }
122