Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +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 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation; |
| 20 | |
| 21 | import android.app.Activity; |
| 22 | import android.content.Context; |
| 23 | import android.graphics.Rect; |
| 24 | import android.os.RemoteException; |
| 25 | import android.perftests.utils.BenchmarkState; |
| 26 | import android.perftests.utils.PerfStatusReporter; |
| 27 | import android.perftests.utils.StubActivity; |
| 28 | import android.util.MergedConfiguration; |
| 29 | import android.view.DisplayCutout; |
| 30 | import android.view.IWindow; |
| 31 | import android.view.IWindowSession; |
| 32 | import android.view.InsetsState; |
| 33 | import android.view.SurfaceControl; |
| 34 | import android.view.View; |
| 35 | import android.view.WindowManager; |
| 36 | import android.view.WindowManagerGlobal; |
| 37 | import android.widget.LinearLayout; |
| 38 | |
| 39 | import androidx.test.filters.LargeTest; |
| 40 | import androidx.test.rule.ActivityTestRule; |
| 41 | |
Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +0800 | [diff] [blame] | 42 | import org.junit.Rule; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | import org.junit.runners.Parameterized; |
| 46 | |
| 47 | import java.util.Arrays; |
| 48 | import java.util.Collection; |
| 49 | import java.util.function.IntSupplier; |
| 50 | |
| 51 | @RunWith(Parameterized.class) |
| 52 | @LargeTest |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame^] | 53 | public class RelayoutPerfTest extends WindowManagerPerfTestBase { |
Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +0800 | [diff] [blame] | 54 | private int mIteration; |
| 55 | |
| 56 | @Rule |
| 57 | public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 58 | |
| 59 | @Rule |
| 60 | public final ActivityTestRule<StubActivity> mActivityRule = |
| 61 | new ActivityTestRule<>(StubActivity.class); |
| 62 | |
| 63 | /** This is only a placement to match the input parameters from {@link #getParameters}. */ |
| 64 | @Parameterized.Parameter(0) |
| 65 | public String testName; |
| 66 | |
| 67 | /** The visibilities to loop for relayout. */ |
| 68 | @Parameterized.Parameter(1) |
| 69 | public int[] visibilities; |
| 70 | |
| 71 | /** |
| 72 | * Each row will be mapped into {@link #testName} and {@link #visibilities} of a new test |
| 73 | * instance according to the index of the parameter. |
| 74 | */ |
| 75 | @Parameterized.Parameters(name = "{0}") |
| 76 | public static Collection<Object[]> getParameters() { |
| 77 | return Arrays.asList(new Object[][] { |
| 78 | { "Visible", new int[] { View.VISIBLE } }, |
| 79 | { "Invisible~Visible", new int[] { View.INVISIBLE, View.VISIBLE } }, |
| 80 | { "Gone~Visible", new int[] { View.GONE, View.VISIBLE } }, |
| 81 | { "Gone~Invisible", new int[] { View.GONE, View.INVISIBLE } } |
| 82 | }); |
| 83 | } |
| 84 | |
Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +0800 | [diff] [blame] | 85 | @Test |
| 86 | public void testRelayout() throws Throwable { |
| 87 | final Activity activity = mActivityRule.getActivity(); |
| 88 | final ContentView contentView = new ContentView(activity); |
| 89 | mActivityRule.runOnUiThread(() -> { |
| 90 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); |
| 91 | activity.setContentView(contentView); |
| 92 | }); |
| 93 | getInstrumentation().waitForIdleSync(); |
| 94 | |
| 95 | final RelayoutRunner relayoutRunner = new RelayoutRunner(activity, contentView.getWindow(), |
| 96 | () -> visibilities[mIteration++ % visibilities.length]); |
| 97 | relayoutRunner.runBenchmark(mPerfStatusReporter.getBenchmarkState()); |
| 98 | } |
| 99 | |
| 100 | /** A dummy view to get IWindow. */ |
| 101 | private static class ContentView extends LinearLayout { |
| 102 | ContentView(Context context) { |
| 103 | super(context); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | protected IWindow getWindow() { |
| 108 | return super.getWindow(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | private static class RelayoutRunner { |
| 113 | final Rect mOutFrame = new Rect(); |
| 114 | final Rect mOutOverscanInsets = new Rect(); |
| 115 | final Rect mOutContentInsets = new Rect(); |
| 116 | final Rect mOutVisibleInsets = new Rect(); |
| 117 | final Rect mOutStableInsets = new Rect(); |
| 118 | final Rect mOutOutsets = new Rect(); |
| 119 | final Rect mOutBackDropFrame = new Rect(); |
| 120 | final DisplayCutout.ParcelableWrapper mOutDisplayCutout = |
| 121 | new DisplayCutout.ParcelableWrapper(DisplayCutout.NO_CUTOUT); |
| 122 | final MergedConfiguration mOutMergedConfiguration = new MergedConfiguration(); |
| 123 | final InsetsState mOutInsetsState = new InsetsState(); |
| 124 | final IWindow mWindow; |
| 125 | final View mView; |
| 126 | final WindowManager.LayoutParams mParams; |
| 127 | final int mWidth; |
| 128 | final int mHeight; |
| 129 | final SurfaceControl mOutSurfaceControl; |
| 130 | |
| 131 | final IntSupplier mViewVisibility; |
| 132 | |
| 133 | int mSeq; |
| 134 | int mFrameNumber; |
| 135 | int mFlags; |
| 136 | |
| 137 | RelayoutRunner(Activity activity, IWindow window, IntSupplier visibilitySupplier) { |
| 138 | mWindow = window; |
| 139 | mView = activity.getWindow().getDecorView(); |
| 140 | mParams = (WindowManager.LayoutParams) mView.getLayoutParams(); |
| 141 | mWidth = mView.getMeasuredWidth(); |
| 142 | mHeight = mView.getMeasuredHeight(); |
| 143 | mOutSurfaceControl = mView.getViewRootImpl().getSurfaceControl(); |
| 144 | mViewVisibility = visibilitySupplier; |
| 145 | } |
| 146 | |
| 147 | void runBenchmark(BenchmarkState state) throws RemoteException { |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame^] | 148 | final IWindowSession session = WindowManagerGlobal.getWindowSession(); |
Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +0800 | [diff] [blame] | 149 | while (state.keepRunning()) { |
Riddle Hsu | 54a86c6 | 2019-07-10 21:37:08 +0800 | [diff] [blame^] | 150 | session.relayout(mWindow, mSeq, mParams, mWidth, mHeight, |
Riddle Hsu | b8d89f7 | 2019-06-14 00:21:55 +0800 | [diff] [blame] | 151 | mViewVisibility.getAsInt(), mFlags, mFrameNumber, mOutFrame, |
| 152 | mOutOverscanInsets, mOutContentInsets, mOutVisibleInsets, mOutStableInsets, |
| 153 | mOutOutsets, mOutBackDropFrame, mOutDisplayCutout, mOutMergedConfiguration, |
| 154 | mOutSurfaceControl, mOutInsetsState); |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |