blob: f0c474bfc5bb45a3ba6f009b9fd603ab422c5305 [file] [log] [blame]
Riddle Hsub8d89f72019-06-14 00:21:55 +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 androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20
21import android.app.Activity;
22import android.content.Context;
23import android.graphics.Rect;
24import android.os.RemoteException;
25import android.perftests.utils.BenchmarkState;
26import android.perftests.utils.PerfStatusReporter;
27import android.perftests.utils.StubActivity;
28import android.util.MergedConfiguration;
29import android.view.DisplayCutout;
30import android.view.IWindow;
31import android.view.IWindowSession;
32import android.view.InsetsState;
33import android.view.SurfaceControl;
34import android.view.View;
35import android.view.WindowManager;
36import android.view.WindowManagerGlobal;
37import android.widget.LinearLayout;
38
39import androidx.test.filters.LargeTest;
40import androidx.test.rule.ActivityTestRule;
41
Riddle Hsub8d89f72019-06-14 00:21:55 +080042import org.junit.Rule;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45import org.junit.runners.Parameterized;
46
47import java.util.Arrays;
48import java.util.Collection;
49import java.util.function.IntSupplier;
50
51@RunWith(Parameterized.class)
52@LargeTest
Riddle Hsu54a86c62019-07-10 21:37:08 +080053public class RelayoutPerfTest extends WindowManagerPerfTestBase {
Riddle Hsub8d89f72019-06-14 00:21:55 +080054 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 Hsub8d89f72019-06-14 00:21:55 +080085 @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 Hsu54a86c62019-07-10 21:37:08 +0800148 final IWindowSession session = WindowManagerGlobal.getWindowSession();
Riddle Hsub8d89f72019-06-14 00:21:55 +0800149 while (state.keepRunning()) {
Riddle Hsu54a86c62019-07-10 21:37:08 +0800150 session.relayout(mWindow, mSeq, mParams, mWidth, mHeight,
Riddle Hsub8d89f72019-06-14 00:21:55 +0800151 mViewVisibility.getAsInt(), mFlags, mFrameNumber, mOutFrame,
152 mOutOverscanInsets, mOutContentInsets, mOutVisibleInsets, mOutStableInsets,
153 mOutOutsets, mOutBackDropFrame, mOutDisplayCutout, mOutMergedConfiguration,
154 mOutSurfaceControl, mOutInsetsState);
155 }
156 }
157 }
158}