xref: /aosp_15_r20/frameworks/native/vulkan/vkprofiles/vkprofiles.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker  * Copyright 2024 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker  *
4*38e8c45fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker  *
8*38e8c45fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker  *
10*38e8c45fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker  * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker  *
16*38e8c45fSAndroid Build Coastguard Worker  */
17*38e8c45fSAndroid Build Coastguard Worker 
18*38e8c45fSAndroid Build Coastguard Worker #define LOG_TAG "vkprofiles"
19*38e8c45fSAndroid Build Coastguard Worker 
20*38e8c45fSAndroid Build Coastguard Worker #ifndef VK_USE_PLATFORM_ANDROID_KHR
21*38e8c45fSAndroid Build Coastguard Worker #define VK_USE_PLATFORM_ANDROID_KHR
22*38e8c45fSAndroid Build Coastguard Worker #endif
23*38e8c45fSAndroid Build Coastguard Worker 
24*38e8c45fSAndroid Build Coastguard Worker #include <string>
25*38e8c45fSAndroid Build Coastguard Worker #include <vector>
26*38e8c45fSAndroid Build Coastguard Worker 
27*38e8c45fSAndroid Build Coastguard Worker #include <android/log.h>
28*38e8c45fSAndroid Build Coastguard Worker 
29*38e8c45fSAndroid Build Coastguard Worker #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
30*38e8c45fSAndroid Build Coastguard Worker #define ALOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
31*38e8c45fSAndroid Build Coastguard Worker 
32*38e8c45fSAndroid Build Coastguard Worker #include "generated/vulkan_profiles.h"
33*38e8c45fSAndroid Build Coastguard Worker #include "vkprofiles.h"
34*38e8c45fSAndroid Build Coastguard Worker 
35*38e8c45fSAndroid Build Coastguard Worker namespace android::vkprofiles {
36*38e8c45fSAndroid Build Coastguard Worker 
37*38e8c45fSAndroid Build Coastguard Worker /* Wrap vkProfileGetSupport in an anonymous namespace.
38*38e8c45fSAndroid Build Coastguard Worker  * vkProfileGetSupport only works for profiles that we explicitly add, we don't
39*38e8c45fSAndroid Build Coastguard Worker  * want a user of this library to mistakenly call with a profile that we haven't
40*38e8c45fSAndroid Build Coastguard Worker  * added.
41*38e8c45fSAndroid Build Coastguard Worker  */
42*38e8c45fSAndroid Build Coastguard Worker namespace {
43*38e8c45fSAndroid Build Coastguard Worker 
vkProfileGetSupport(const VpProfileProperties * pProfile,const uint32_t minApiVersion)44*38e8c45fSAndroid Build Coastguard Worker std::string vkProfileGetSupport(const VpProfileProperties* pProfile,
45*38e8c45fSAndroid Build Coastguard Worker                                 const uint32_t minApiVersion) {
46*38e8c45fSAndroid Build Coastguard Worker     VkResult result = VK_SUCCESS;
47*38e8c45fSAndroid Build Coastguard Worker     VkBool32 supported = VK_FALSE;
48*38e8c45fSAndroid Build Coastguard Worker 
49*38e8c45fSAndroid Build Coastguard Worker     result = vpGetInstanceProfileSupport(nullptr, pProfile, &supported);
50*38e8c45fSAndroid Build Coastguard Worker     if (result != VK_SUCCESS) {
51*38e8c45fSAndroid Build Coastguard Worker         std::string error(
52*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vpGetInstanceProfileSupport,"
53*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
54*38e8c45fSAndroid Build Coastguard Worker             " result = " +
55*38e8c45fSAndroid Build Coastguard Worker             std::to_string(result));
56*38e8c45fSAndroid Build Coastguard Worker         return error;
57*38e8c45fSAndroid Build Coastguard Worker     }
58*38e8c45fSAndroid Build Coastguard Worker     if (supported != VK_TRUE) {
59*38e8c45fSAndroid Build Coastguard Worker         std::string error(
60*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vpGetInstanceProfileSupport,"
61*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
62*38e8c45fSAndroid Build Coastguard Worker             " supported = " +
63*38e8c45fSAndroid Build Coastguard Worker             std::to_string(supported));
64*38e8c45fSAndroid Build Coastguard Worker         return error;
65*38e8c45fSAndroid Build Coastguard Worker     }
66*38e8c45fSAndroid Build Coastguard Worker 
67*38e8c45fSAndroid Build Coastguard Worker     const VkApplicationInfo appInfo = {
68*38e8c45fSAndroid Build Coastguard Worker         VK_STRUCTURE_TYPE_APPLICATION_INFO,
69*38e8c45fSAndroid Build Coastguard Worker         nullptr,
70*38e8c45fSAndroid Build Coastguard Worker         "vkprofiles",
71*38e8c45fSAndroid Build Coastguard Worker         0,
72*38e8c45fSAndroid Build Coastguard Worker         "",
73*38e8c45fSAndroid Build Coastguard Worker         0,
74*38e8c45fSAndroid Build Coastguard Worker         minApiVersion,
75*38e8c45fSAndroid Build Coastguard Worker     };
76*38e8c45fSAndroid Build Coastguard Worker     VkInstanceCreateInfo instanceCreateInfo = {
77*38e8c45fSAndroid Build Coastguard Worker         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
78*38e8c45fSAndroid Build Coastguard Worker         nullptr,
79*38e8c45fSAndroid Build Coastguard Worker         0,
80*38e8c45fSAndroid Build Coastguard Worker         &appInfo,
81*38e8c45fSAndroid Build Coastguard Worker         0,
82*38e8c45fSAndroid Build Coastguard Worker         nullptr,
83*38e8c45fSAndroid Build Coastguard Worker         0,
84*38e8c45fSAndroid Build Coastguard Worker         nullptr,
85*38e8c45fSAndroid Build Coastguard Worker     };
86*38e8c45fSAndroid Build Coastguard Worker 
87*38e8c45fSAndroid Build Coastguard Worker     VpInstanceCreateInfo vpInstanceCreateInfo{};
88*38e8c45fSAndroid Build Coastguard Worker     vpInstanceCreateInfo.pCreateInfo = &instanceCreateInfo;
89*38e8c45fSAndroid Build Coastguard Worker     vpInstanceCreateInfo.enabledFullProfileCount = 1;
90*38e8c45fSAndroid Build Coastguard Worker     vpInstanceCreateInfo.pEnabledFullProfiles = pProfile;
91*38e8c45fSAndroid Build Coastguard Worker 
92*38e8c45fSAndroid Build Coastguard Worker     VkInstance instance = VK_NULL_HANDLE;
93*38e8c45fSAndroid Build Coastguard Worker     result = vpCreateInstance(&vpInstanceCreateInfo, nullptr, &instance);
94*38e8c45fSAndroid Build Coastguard Worker     if (result != VK_SUCCESS) {
95*38e8c45fSAndroid Build Coastguard Worker         std::string error(
96*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vpCreateInstance,"
97*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
98*38e8c45fSAndroid Build Coastguard Worker             " result = " +
99*38e8c45fSAndroid Build Coastguard Worker             std::to_string(result));
100*38e8c45fSAndroid Build Coastguard Worker         return error;
101*38e8c45fSAndroid Build Coastguard Worker     }
102*38e8c45fSAndroid Build Coastguard Worker 
103*38e8c45fSAndroid Build Coastguard Worker     uint32_t count;
104*38e8c45fSAndroid Build Coastguard Worker     result = vkEnumeratePhysicalDevices(instance, &count, nullptr);
105*38e8c45fSAndroid Build Coastguard Worker     if (result != VK_SUCCESS) {
106*38e8c45fSAndroid Build Coastguard Worker         vkDestroyInstance(instance, nullptr);
107*38e8c45fSAndroid Build Coastguard Worker         std::string error(
108*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vkEnumeratePhysicalDevices,"
109*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
110*38e8c45fSAndroid Build Coastguard Worker             " result = " +
111*38e8c45fSAndroid Build Coastguard Worker             std::to_string(result));
112*38e8c45fSAndroid Build Coastguard Worker         return error;
113*38e8c45fSAndroid Build Coastguard Worker     }
114*38e8c45fSAndroid Build Coastguard Worker 
115*38e8c45fSAndroid Build Coastguard Worker     std::vector<VkPhysicalDevice> devices(count, VK_NULL_HANDLE);
116*38e8c45fSAndroid Build Coastguard Worker     result = vkEnumeratePhysicalDevices(instance, &count, devices.data());
117*38e8c45fSAndroid Build Coastguard Worker     if (result != VK_SUCCESS) {
118*38e8c45fSAndroid Build Coastguard Worker         vkDestroyInstance(instance, nullptr);
119*38e8c45fSAndroid Build Coastguard Worker         std::string error(
120*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vkEnumeratePhysicalDevices (2),"
121*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
122*38e8c45fSAndroid Build Coastguard Worker             " result = " +
123*38e8c45fSAndroid Build Coastguard Worker             std::to_string(result));
124*38e8c45fSAndroid Build Coastguard Worker         return error;
125*38e8c45fSAndroid Build Coastguard Worker     }
126*38e8c45fSAndroid Build Coastguard Worker 
127*38e8c45fSAndroid Build Coastguard Worker     bool onePhysicalDeviceSupports = false;
128*38e8c45fSAndroid Build Coastguard Worker     for (size_t i = 0; i < count; i++) {
129*38e8c45fSAndroid Build Coastguard Worker         result = vpGetPhysicalDeviceProfileSupport(instance, devices[i],
130*38e8c45fSAndroid Build Coastguard Worker                                                    pProfile, &supported);
131*38e8c45fSAndroid Build Coastguard Worker         if (result != VK_SUCCESS) {
132*38e8c45fSAndroid Build Coastguard Worker             ALOGD("vpGetPhysicalDeviceProfileSupport fail, result = %d",
133*38e8c45fSAndroid Build Coastguard Worker                   result);
134*38e8c45fSAndroid Build Coastguard Worker             continue;
135*38e8c45fSAndroid Build Coastguard Worker         } else if (supported != VK_TRUE) {
136*38e8c45fSAndroid Build Coastguard Worker             ALOGD("vpGetPhysicalDeviceProfileSupport fail, supported = %d",
137*38e8c45fSAndroid Build Coastguard Worker                   supported);
138*38e8c45fSAndroid Build Coastguard Worker             continue;
139*38e8c45fSAndroid Build Coastguard Worker         }
140*38e8c45fSAndroid Build Coastguard Worker 
141*38e8c45fSAndroid Build Coastguard Worker         onePhysicalDeviceSupports = true;
142*38e8c45fSAndroid Build Coastguard Worker     }
143*38e8c45fSAndroid Build Coastguard Worker 
144*38e8c45fSAndroid Build Coastguard Worker     if (!onePhysicalDeviceSupports) {
145*38e8c45fSAndroid Build Coastguard Worker         std::string error(
146*38e8c45fSAndroid Build Coastguard Worker             "There was a failure from vpGetPhysicalDeviceProfileSupport,"
147*38e8c45fSAndroid Build Coastguard Worker             " check `vkprofiles` in logcat."
148*38e8c45fSAndroid Build Coastguard Worker             " No VkPhysicalDevice supports the profile");
149*38e8c45fSAndroid Build Coastguard Worker         return error;
150*38e8c45fSAndroid Build Coastguard Worker     }
151*38e8c45fSAndroid Build Coastguard Worker 
152*38e8c45fSAndroid Build Coastguard Worker     return std::string("SUPPORTED");
153*38e8c45fSAndroid Build Coastguard Worker }
154*38e8c45fSAndroid Build Coastguard Worker 
155*38e8c45fSAndroid Build Coastguard Worker }  // anonymous namespace
156*38e8c45fSAndroid Build Coastguard Worker 
vkAbp2021GetSupport()157*38e8c45fSAndroid Build Coastguard Worker std::string vkAbp2021GetSupport() {
158*38e8c45fSAndroid Build Coastguard Worker     VpProfileProperties profile{VP_ANDROID_BASELINE_2021_NAME,
159*38e8c45fSAndroid Build Coastguard Worker                                 VP_ANDROID_BASELINE_2021_SPEC_VERSION};
160*38e8c45fSAndroid Build Coastguard Worker     return vkProfileGetSupport(&profile,
161*38e8c45fSAndroid Build Coastguard Worker                                VP_ANDROID_BASELINE_2021_MIN_API_VERSION);
162*38e8c45fSAndroid Build Coastguard Worker }
163*38e8c45fSAndroid Build Coastguard Worker 
vkAbp2021CpuOnlyGetSupport()164*38e8c45fSAndroid Build Coastguard Worker std::string vkAbp2021CpuOnlyGetSupport() {
165*38e8c45fSAndroid Build Coastguard Worker     VpProfileProperties profile{VP_ANDROID_BASELINE_2021_CPU_ONLY_NAME,
166*38e8c45fSAndroid Build Coastguard Worker                                 VP_ANDROID_BASELINE_2021_CPU_ONLY_SPEC_VERSION};
167*38e8c45fSAndroid Build Coastguard Worker     return vkProfileGetSupport(&profile,
168*38e8c45fSAndroid Build Coastguard Worker                                VP_ANDROID_BASELINE_2021_MIN_API_VERSION);
169*38e8c45fSAndroid Build Coastguard Worker }
170*38e8c45fSAndroid Build Coastguard Worker 
vkAbp2022GetSupport()171*38e8c45fSAndroid Build Coastguard Worker std::string vkAbp2022GetSupport() {
172*38e8c45fSAndroid Build Coastguard Worker     VpProfileProperties profile{VP_ANDROID_BASELINE_2022_NAME,
173*38e8c45fSAndroid Build Coastguard Worker                                 VP_ANDROID_BASELINE_2022_SPEC_VERSION};
174*38e8c45fSAndroid Build Coastguard Worker     return vkProfileGetSupport(&profile,
175*38e8c45fSAndroid Build Coastguard Worker                                VP_ANDROID_BASELINE_2022_MIN_API_VERSION);
176*38e8c45fSAndroid Build Coastguard Worker }
177*38e8c45fSAndroid Build Coastguard Worker 
vkVpa15GetSupport()178*38e8c45fSAndroid Build Coastguard Worker std::string vkVpa15GetSupport() {
179*38e8c45fSAndroid Build Coastguard Worker     VpProfileProperties profile{VP_ANDROID_15_MINIMUMS_NAME,
180*38e8c45fSAndroid Build Coastguard Worker                                 VP_ANDROID_15_MINIMUMS_SPEC_VERSION};
181*38e8c45fSAndroid Build Coastguard Worker     return vkProfileGetSupport(&profile,
182*38e8c45fSAndroid Build Coastguard Worker                                VP_ANDROID_15_MINIMUMS_MIN_API_VERSION);
183*38e8c45fSAndroid Build Coastguard Worker }
184*38e8c45fSAndroid Build Coastguard Worker 
vkProfiles()185*38e8c45fSAndroid Build Coastguard Worker std::string vkProfiles() {
186*38e8c45fSAndroid Build Coastguard Worker     return "{"
187*38e8c45fSAndroid Build Coastguard Worker            "\"" + std::string(VP_ANDROID_BASELINE_2021_NAME) + "\": "
188*38e8c45fSAndroid Build Coastguard Worker            "\"" + vkAbp2021GetSupport() + "\","
189*38e8c45fSAndroid Build Coastguard Worker            "\"" + std::string(VP_ANDROID_BASELINE_2021_CPU_ONLY_NAME) + "\": "
190*38e8c45fSAndroid Build Coastguard Worker            "\"" + vkAbp2021CpuOnlyGetSupport() + "\","
191*38e8c45fSAndroid Build Coastguard Worker            "\"" + std::string(VP_ANDROID_BASELINE_2022_NAME) + "\": "
192*38e8c45fSAndroid Build Coastguard Worker            "\"" + vkAbp2022GetSupport() + "\","
193*38e8c45fSAndroid Build Coastguard Worker            "\"" + std::string(VP_ANDROID_15_MINIMUMS_NAME) + "\": "
194*38e8c45fSAndroid Build Coastguard Worker            "\"" + vkVpa15GetSupport() + "\""
195*38e8c45fSAndroid Build Coastguard Worker            "}";
196*38e8c45fSAndroid Build Coastguard Worker }
197*38e8c45fSAndroid Build Coastguard Worker 
198*38e8c45fSAndroid Build Coastguard Worker }  // namespace android::vkprofiles
199