Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 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.graphics.Rect; |
| 23 | import android.os.RemoteException; |
| 24 | import android.os.SystemClock; |
| 25 | import android.perftests.utils.ManualBenchmarkState; |
| 26 | import android.perftests.utils.ManualBenchmarkState.ManualBenchmarkTest; |
| 27 | import android.perftests.utils.PerfManualStatusReporter; |
| 28 | import android.view.Display; |
| 29 | import android.view.DisplayCutout; |
| 30 | import android.view.IWindowSession; |
| 31 | import android.view.InputChannel; |
| 32 | import android.view.InsetsState; |
| 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 |
Riddle Hsu | 4a72500 | 2019-11-11 20:50:13 +0800 | [diff] [blame^] | 47 | public class WindowAddRemovePerfTest extends WindowManagerPerfTestBase |
| 48 | implements ManualBenchmarkState.CustomizedIterationListener { |
| 49 | |
| 50 | private static final int PROFILED_ITERATIONS = 2; |
| 51 | |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 52 | @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 Hsu | 4a72500 | 2019-11-11 20:50:13 +0800 | [diff] [blame^] | 66 | /** 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 Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 78 | @Test |
Riddle Hsu | 5ef56dd6 | 2019-07-26 21:28:51 -0600 | [diff] [blame] | 79 | @ManualBenchmarkTest(warmupDurationNs = TIME_1_S_IN_NS, targetTestDurationNs = TIME_5_S_IN_NS) |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 80 | public void testAddRemoveWindow() throws Throwable { |
Riddle Hsu | 4a72500 | 2019-11-11 20:50:13 +0800 | [diff] [blame^] | 81 | final ManualBenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 82 | state.setCustomizedIterations(PROFILED_ITERATIONS, this); |
| 83 | new TestWindow().runBenchmark(state); |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 84 | } |
| 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 Hsu | 4a72500 | 2019-11-11 20:50:13 +0800 | [diff] [blame^] | 123 | inputChannel.dispose(); |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |