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 |
| 47 | public 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 | } |