1 /*
2  * Copyright (C) 2018 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 com.android.game.qualification.test;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 import static com.google.common.truth.Truth.assertWithMessage;
20 import static com.google.common.truth.Truth.assert_;
21 
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
25 
26 import com.google.gson.Gson;
27 import com.google.gson.JsonSyntaxException;
28 
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 
38 @RunWith(DeviceJUnit4ClassRunner.class)
39 public class VkJsonTests extends BaseHostJUnit4Test {
40     // *** BEGIN CLASSES FOR GSON ***
41     // Classes to be used with GSON.  The structure follows the output of 'cmd gpu vkjson', which is
42     // format is defined in /frameworks/native/vulkan/vkjson/vkjson.cc
43     private static class VkJson {
44         List<VkJsonDevice> devices;
45     }
46 
47     private static class VkJsonDevice {
48         VkJsonDeviceProperties properties;
49         VkJsonExtDriverProperties VK_KHR_driver_properties;
50         List<VkExtension> extensions;
51     }
52 
53     private static class VkJsonDeviceProperties {
54         Long apiVersion;
55     }
56 
57     private static class VkJsonExtDriverProperties {
58         VkPhysicalDeviceDriverPropertiesKHR driverPropertiesKHR;
59     }
60 
61     private static class VkPhysicalDeviceDriverPropertiesKHR {
62         Long driverID;
63         String driverName;
64         String driverInfo;
65         VkConformanceVersionKHR conformanceVersion;
66     }
67 
68     private static class VkConformanceVersionKHR {
69         Short major;
70         Short minor;
71         Short subminor;
72         Short patch;
73     }
74 
75     private static class VkExtension {
76         String extensionName;
77         Integer specVersion;
78     }
79     // *** END CLASSES FOR GSON ***
80 
81     private VkJson mVkJson;
82 
83     @Before
setUp()84     public void setUp() throws DeviceNotAvailableException {
85         TestUtils.assumeGameCoreCertified(getDevice());
86 
87         String cmdString = getDevice().executeShellCommand("cmd gpu vkjson");
88         Gson gson = new Gson();
89         try {
90             mVkJson = gson.fromJson(cmdString, VkJson.class);
91         } catch (JsonSyntaxException e) {
92             assert_().fail(
93                     "Error parsing JSON from 'cmd gpu vkjson': %s\nresult: %s",
94                     e.getMessage(),
95                     cmdString);
96         }
97 
98         assertThat(mVkJson.devices).isNotNull();
99         assertThat(mVkJson.devices).isNotEmpty();
100     }
101 
102     /**
103      * Vulkan version must be at least 1.1.
104      */
105     @Test
checkRequiredVersion()106     public void checkRequiredVersion() {
107         final long apiVersion = mVkJson.devices.get(0).properties.apiVersion;
108         final long vulkan11Version = 0x401000;
109         assertWithMessage("Supported Vulkan version must be at least 1.1")
110             .that(apiVersion)
111             .named("supported vulkan version")
112             .isAtLeast(vulkan11Version);
113     }
114 
115     /**
116      * The following Vulkan extensions are required:
117      *     VK_GOOGLE_display_timing
118      *     VK_KHR_driver_properties
119      */
120     @Test
checkRequiredExtensions()121     public void checkRequiredExtensions() {
122         final Collection<String> REQUIRED_EXTENSIONS = Arrays.asList(
123                 "VK_GOOGLE_display_timing",
124                 "VK_KHR_driver_properties");
125 
126         List<String> extensions = mVkJson.devices.get(0).extensions.stream()
127                 .map(it -> it.extensionName)
128                 .collect(Collectors.toList());
129         assertWithMessage("Required Vulkan extensions are not supported")
130                 .that(extensions)
131                 .named("supported extensions")
132                 .containsAllIn(REQUIRED_EXTENSIONS);
133     }
134 
135     /**
136      * Vulkan driver conformance must be at least 1.1.2.
137      */
138     @Test
checkKHRDriverProperties()139     public void checkKHRDriverProperties() {
140         // Check driver conformance version is at least 1.1.2.
141         final short MAJOR = 1;
142         final short MINOR = 1;
143         final short SUBMINOR = 2;
144         final String DRIVER_CONFORMANCE_VERSION = MAJOR + "." + MINOR + "." + SUBMINOR;
145 
146         assertWithMessage("VK_KHR_driver_properties is not supported")
147                 .that(mVkJson.devices.get(0).VK_KHR_driver_properties)
148                 .named("VK_KHR_driver_properties")
149                 .isNotNull();
150 
151         VkPhysicalDeviceDriverPropertiesKHR properties =
152                 mVkJson.devices.get(0).VK_KHR_driver_properties.driverPropertiesKHR;
153         assertWithMessage("VK_KHR_driver_properties is not supported")
154                 .that(properties).named("driverPropertiesKHR").isNotNull();
155 
156         VkConformanceVersionKHR version = properties.conformanceVersion;
157         assertThat(version).named("driverPropertiesKHR.conformanceVersion").isNotNull();
158 
159         String msg = "Driver conformance version must be at least " + DRIVER_CONFORMANCE_VERSION;
160         assertWithMessage(msg).that(version.major).named("major version").isAtLeast(MAJOR);
161         if (version.major == MAJOR) {
162             assertWithMessage(msg).that(version.minor).named("minor version").isAtLeast(MINOR);
163             if (version.minor == MINOR) {
164                 assertWithMessage(msg).that(version.subminor).named("subminor version")
165                         .isAtLeast(SUBMINOR);
166             }
167         }
168     }
169 }
170