1 /*
2  * Copyright 2023 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 package android.view.cts;
17 
18 import static android.server.wm.ActivityManagerTestBase.createFullscreenActivityScenarioRule;
19 
20 import static java.util.Objects.requireNonNull;
21 
22 import android.app.ActivityManager;
23 import android.app.ActivityManager.MemoryInfo;
24 import android.platform.test.annotations.RequiresFlagsEnabled;
25 import android.platform.test.flag.junit.CheckFlagsRule;
26 import android.platform.test.flag.junit.DeviceFlagsValueProvider;
27 import android.view.flags.Flags;
28 
29 import androidx.test.ext.junit.rules.ActivityScenarioRule;
30 
31 import org.junit.Before;
32 import org.junit.Rule;
33 import org.junit.Test;
34 
35 /**
36  * Tests covering Surface memory management. These tests can take a long time to run.
37  */
38 public class SurfaceOOMTest {
39     public static final int BIG_WIDTH = 1000;
40     public static final int BIG_HEIGHT = 1000;
41     public static final int BYTES_PER_PIXEL = 4;
42     public static final int MEMORY_MULTIPLIER = 2;
43 
44     @Rule
45     public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();
46 
47     @Rule
48     public ActivityScenarioRule<SurfaceOOMTestActivity> mActivityRule =
49             createFullscreenActivityScenarioRule(SurfaceOOMTestActivity.class);
50 
51     private SurfaceOOMTestActivity mActivity;
52 
53     @Before
setup()54     public void setup() {
55         mActivityRule.getScenario().onActivity(activity -> mActivity = activity);
56     }
57 
58     @Test(timeout = 5 * 60 * 1000)
59     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SURFACE_NATIVE_ALLOC_REGISTRATION_RO)
testSurfaceGarbageCollection()60     public void testSurfaceGarbageCollection() throws Throwable {
61         long imageSizeBytes = BIG_WIDTH * BIG_HEIGHT * BYTES_PER_PIXEL;
62         long numSurfacesToCreate = MEMORY_MULTIPLIER * (getMemoryInfo().totalMem / imageSizeBytes);
63 
64         mActivity.verifyCreatingManySurfaces(BIG_WIDTH, BIG_HEIGHT, numSurfacesToCreate);
65     }
66 
getMemoryInfo()67     private MemoryInfo getMemoryInfo() {
68         MemoryInfo memoryInfo = new MemoryInfo();
69         requireNonNull(mActivity.getSystemService(ActivityManager.class)).getMemoryInfo(memoryInfo);
70         return memoryInfo;
71     }
72 }
73