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