Reduce unnecessary object creation in relayout test

Also change to use STAY_ON_WHILE_PLUGGED_IN for keeping
device awake. That eliminates the toggle of wakelock by
showing and hiding a window with FLAG_KEEP_SCREEN_ON.

Test: atest RelayoutPerfTest InternalWindowOperationPerfTest
Bug: 131727899
Change-Id: Ib4608aa1a60c7560b39011cfd2e4740680b3c884
diff --git a/apct-tests/perftests/core/src/android/wm/RelayoutPerfTest.java b/apct-tests/perftests/core/src/android/wm/RelayoutPerfTest.java
index b6e39e1..8633c96 100644
--- a/apct-tests/perftests/core/src/android/wm/RelayoutPerfTest.java
+++ b/apct-tests/perftests/core/src/android/wm/RelayoutPerfTest.java
@@ -125,7 +125,9 @@
         final WindowManager.LayoutParams mParams;
         final int mWidth;
         final int mHeight;
+        final Point mOutSurfaceSize = new Point();
         final SurfaceControl mOutSurfaceControl;
+        final SurfaceControl mOutBlastSurfaceControl = new SurfaceControl();
 
         final IntSupplier mViewVisibility;
 
@@ -150,7 +152,8 @@
                         mViewVisibility.getAsInt(), mFlags, mFrameNumber, mOutFrame,
                         mOutContentInsets, mOutVisibleInsets, mOutStableInsets,
                         mOutBackDropFrame, mOutDisplayCutout, mOutMergedConfiguration,
-                        mOutSurfaceControl, mOutInsetsState, new Point(), new SurfaceControl());
+                        mOutSurfaceControl, mOutInsetsState, mOutSurfaceSize,
+                        mOutBlastSurfaceControl);
             }
         }
     }
diff --git a/apct-tests/perftests/core/src/android/wm/WindowManagerPerfTestBase.java b/apct-tests/perftests/core/src/android/wm/WindowManagerPerfTestBase.java
index 62e9ba8..9e17e94 100644
--- a/apct-tests/perftests/core/src/android/wm/WindowManagerPerfTestBase.java
+++ b/apct-tests/perftests/core/src/android/wm/WindowManagerPerfTestBase.java
@@ -20,15 +20,19 @@
 
 import android.app.Activity;
 import android.app.UiAutomation;
+import android.content.Context;
 import android.content.Intent;
+import android.os.BatteryManager;
 import android.os.ParcelFileDescriptor;
 import android.perftests.utils.PerfTestActivity;
+import android.provider.Settings;
 
 import androidx.test.rule.ActivityTestRule;
 import androidx.test.runner.lifecycle.ActivityLifecycleCallback;
 import androidx.test.runner.lifecycle.ActivityLifecycleMonitorRegistry;
 import androidx.test.runner.lifecycle.Stage;
 
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -52,18 +56,36 @@
      */
     static final File BASE_OUT_PATH = new File("/data/local/CorePerfTests");
 
+    private static int sOriginalStayOnWhilePluggedIn;
+
     @BeforeClass
     public static void setUpOnce() {
+        final Context context = getInstrumentation().getContext();
+        sOriginalStayOnWhilePluggedIn = Settings.Global.getInt(context.getContentResolver(),
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, 0);
+        // Keep the device awake during testing.
+        setStayOnWhilePluggedIn(BatteryManager.BATTERY_PLUGGED_USB);
+
         if (!BASE_OUT_PATH.exists()) {
             executeShellCommand("mkdir -p " + BASE_OUT_PATH);
         }
         // In order to be closer to the real use case.
         executeShellCommand("input keyevent KEYCODE_WAKEUP");
         executeShellCommand("wm dismiss-keyguard");
-        getInstrumentation().getContext().startActivity(new Intent(Intent.ACTION_MAIN)
+        context.startActivity(new Intent(Intent.ACTION_MAIN)
                 .addCategory(Intent.CATEGORY_HOME).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
     }
 
+    @AfterClass
+    public static void tearDownOnce() {
+        setStayOnWhilePluggedIn(sOriginalStayOnWhilePluggedIn);
+    }
+
+    private static void setStayOnWhilePluggedIn(int value) {
+        executeShellCommand(String.format("settings put global %s %d",
+                Settings.Global.STAY_ON_WHILE_PLUGGED_IN, value));
+    }
+
     /**
      * Executes shell command with reading the output. It may also used to block until the current
      * command is completed.
@@ -97,7 +119,7 @@
      */
     static class PerfTestActivityRule extends ActivityTestRule<PerfTestActivity> {
         private final Intent mStartIntent =
-                new Intent().putExtra(PerfTestActivity.INTENT_EXTRA_KEEP_SCREEN_ON, true);
+                new Intent(getInstrumentation().getTargetContext(), PerfTestActivity.class);
         private final LifecycleListener mLifecycleListener = new LifecycleListener();
 
         PerfTestActivityRule() {