• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.graphics.Rect;
25 import android.os.RemoteException;
26 import android.perftests.utils.BenchmarkState;
27 import android.perftests.utils.PerfStatusReporter;
28 import android.perftests.utils.PerfTestActivity;
29 import android.util.MergedConfiguration;
30 import android.view.DisplayCutout;
31 import android.view.IWindow;
32 import android.view.IWindowSession;
33 import android.view.InsetsSourceControl;
34 import android.view.InsetsState;
35 import android.view.SurfaceControl;
36 import android.view.View;
37 import android.view.WindowManager;
38 import android.view.WindowManagerGlobal;
39 import android.widget.LinearLayout;
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 public class RelayoutPerfTest extends WindowManagerPerfTestBase {
56     private int mIteration;
57 
58     @Rule
59     public final PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter();
60 
61     @Rule
62     public final ActivityTestRule<PerfTestActivity> mActivityRule =
63             new ActivityTestRule<>(PerfTestActivity.class);
64 
65     /** This is only a placement to match the input parameters from {@link #getParameters}. */
66     @Parameterized.Parameter(0)
67     public String testName;
68 
69     /** The visibilities to loop for relayout. */
70     @Parameterized.Parameter(1)
71     public int[] visibilities;
72 
73     /**
74      * Each row will be mapped into {@link #testName} and {@link #visibilities} of a new test
75      * instance according to the index of the parameter.
76      */
77     @Parameterized.Parameters(name = "{0}")
78     public static Collection<Object[]> getParameters() {
79         return Arrays.asList(new Object[][] {
80                 { "Visible", new int[] { View.VISIBLE } },
81                 { "Invisible~Visible", new int[] { View.INVISIBLE, View.VISIBLE } },
82                 { "Gone~Visible", new int[] { View.GONE, View.VISIBLE } },
83                 { "Gone~Invisible", new int[] { View.GONE, View.INVISIBLE } }
84         });
85     }
86 
87     @Test
88     public void testRelayout() throws Throwable {
89         final Activity activity = mActivityRule.getActivity();
90         final ContentView contentView = new ContentView(activity);
91         mActivityRule.runOnUiThread(() -> activity.setContentView(contentView));
92         getInstrumentation().waitForIdleSync();
93 
94         final RelayoutRunner relayoutRunner = new RelayoutRunner(activity, contentView.getWindow(),
95                 () -> visibilities[mIteration++ % visibilities.length]);
96         relayoutRunner.runBenchmark(mPerfStatusReporter.getBenchmarkState());
97     }
98 
99     /** A dummy view to get IWindow. */
100     private static class ContentView extends LinearLayout {
101         ContentView(Context context) {
102             super(context);
103         }
104 
105         @Override
106         protected IWindow getWindow() {
107             return super.getWindow();
108         }
109     }
110 
111     private static class RelayoutRunner {
112         final Rect mOutFrame = new Rect();
113         final Rect mOutContentInsets = new Rect();
114         final Rect mOutVisibleInsets = new Rect();
115         final Rect mOutStableInsets = new Rect();
116         final Rect mOutBackDropFrame = new Rect();
117         final DisplayCutout.ParcelableWrapper mOutDisplayCutout =
118                 new DisplayCutout.ParcelableWrapper(DisplayCutout.NO_CUTOUT);
119         final MergedConfiguration mOutMergedConfiguration = new MergedConfiguration();
120         final InsetsState mOutInsetsState = new InsetsState();
121         final InsetsSourceControl[] mOutControls = new InsetsSourceControl[0];
122         final IWindow mWindow;
123         final View mView;
124         final WindowManager.LayoutParams mParams;
125         final int mWidth;
126         final int mHeight;
127         final Point mOutSurfaceSize = new Point();
128         final SurfaceControl mOutSurfaceControl;
129         final SurfaceControl mOutBlastSurfaceControl = new SurfaceControl();
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 {
148             final IWindowSession session = WindowManagerGlobal.getWindowSession();
149             while (state.keepRunning()) {
150                 session.relayout(mWindow, mSeq, mParams, mWidth, mHeight,
151                         mViewVisibility.getAsInt(), mFlags, mFrameNumber, mOutFrame,
152                         mOutContentInsets, mOutVisibleInsets, mOutStableInsets,
153                         mOutBackDropFrame, mOutDisplayCutout, mOutMergedConfiguration,
154                         mOutSurfaceControl, mOutInsetsState, mOutControls, mOutSurfaceSize,
155                         mOutBlastSurfaceControl);
156             }
157         }
158     }
159 }
160