1 /*
2  * Copyright (C) 2023 Google LLC.
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 com.android.gpu.vts;
17 
18 import static org.junit.Assert.assertTrue;
19 import static org.junit.Assume.assumeFalse;
20 import static org.junit.Assume.assumeTrue;
21 
22 import com.android.compatibility.common.util.ApiLevelUtil;
23 import com.android.compatibility.common.util.GmsTest;
24 import com.android.compatibility.common.util.PropertyUtil;
25 import com.android.compatibility.common.util.VsrTest;
26 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
27 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
28 import org.json.JSONArray;
29 import org.json.JSONObject;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 
34 /**
35  * VTS test for GPU profiling requirements.
36  */
37 @RunWith(DeviceJUnit4ClassRunner.class)
38 public class GpuProfilingTest extends BaseHostJUnit4Test {
39     private static final int VK_PHYSICAL_DEVICE_TYPE_CPU = 4;
40     private static final int VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU = 3;
41 
42     private JSONObject[] mVulkanDevices;
43 
44     /**
45      * Test specific setup
46      */
47     @Before
setUp()48     public void setUp() throws Exception {
49         final String output = getDevice().executeShellCommand("cmd gpu vkjson");
50         final JSONArray vkjson;
51         // vkjson output in Android N is JSONArray.
52         if (ApiLevelUtil.isBefore(getDevice(), Build.OC)) {
53             vkjson = (new JSONArray(output));
54         } else {
55             vkjson = (new JSONObject(output)).getJSONArray("devices");
56         }
57         mVulkanDevices = new JSONObject[vkjson.length()];
58         for (int i = 0; i < vkjson.length(); i++) {
59             mVulkanDevices[i] = vkjson.getJSONObject(i);
60         }
61     }
62 
63     /**
64      * Devices launched in Android 12 and beyond must support GPU profiling.
65      */
66     @VsrTest(requirements = {"VSR-5.1-002"})
67     @Test
checkGpuProfilingRequirements()68     public void checkGpuProfilingRequirements() throws Exception {
69         final int apiLevel = Util.getVendorApiLevelOrFirstProductApiLevel(getDevice());
70 
71         // Only test for 64-bits devices launched in Android 12 and above.
72         assumeTrue("Test does not apply for API level lower than S", apiLevel >= Build.SC);
73         assumeTrue("Test does not apply for 32-bits devices",
74                 getDevice().getProperty("ro.product.cpu.abi").contains("64"));
75         assumeTrue("Test does not apply for non-handheld devices", Util.isHandheld(getDevice()));
76         assumeFalse(
77                 "Test does not apply for devices with only CPU Vulkan support", hasOnlyCpuDevice());
78         assumeFalse("Test does not apply for devices with only virtual GPU Vulkan support",
79                 hasOnlyVirtualGpuDevice());
80 
81         assertTrue("API level S must support GPU profiling",
82                 PropertyUtil.propertyEquals(getDevice(), "graphics.gpu.profiler.support", "true"));
83     }
84 
hasOnlyCpuDevice()85     private boolean hasOnlyCpuDevice() throws Exception {
86         for (JSONObject device : mVulkanDevices) {
87             if (device.getJSONObject("properties").getInt("deviceType")
88                     != VK_PHYSICAL_DEVICE_TYPE_CPU) {
89                 return false;
90             }
91         }
92         return true;
93     }
94 
hasOnlyVirtualGpuDevice()95     private boolean hasOnlyVirtualGpuDevice() throws Exception {
96         for (JSONObject device : mVulkanDevices) {
97             if (device.getJSONObject("properties").getInt("deviceType")
98                     != VK_PHYSICAL_DEVICE_TYPE_VIRTUAL_GPU) {
99                 return false;
100             }
101         }
102         return true;
103     }
104 }
105