1 /*
2  * Copyright 2017 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  */
17 
18 #define LOG_TAG "RootlessGpuDebug"
19 
20 #include <android/log.h>
21 #include <jni.h>
22 #include <string>
23 #include <vulkan/vulkan.h>
24 
25 #define ALOGI(msg, ...) \
26     __android_log_print(ANDROID_LOG_INFO, LOG_TAG, (msg), __VA_ARGS__)
27 #define ALOGE(msg, ...) \
28     __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, (msg), __VA_ARGS__)
29 
30 namespace {
31 
initVulkan()32 std::string initVulkan()
33 {
34     std::string result = "";
35 
36     const VkApplicationInfo app_info = {
37         VK_STRUCTURE_TYPE_APPLICATION_INFO,
38         nullptr,            // pNext
39         "RootlessGpuDebug", // app name
40         0,                  // app version
41         nullptr,            // engine name
42         0,                  // engine version
43         VK_API_VERSION_1_0,
44     };
45     const VkInstanceCreateInfo instance_info = {
46         VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO,
47         nullptr,   // pNext
48         0,         // flags
49         &app_info,
50         0,         // layer count
51         nullptr,   // layers
52         0,         // extension count
53         nullptr,   // extensions
54     };
55     VkInstance instance;
56     VkResult vkResult = vkCreateInstance(&instance_info, nullptr, &instance);
57     if (vkResult == VK_ERROR_INITIALIZATION_FAILED) {
58         result = "vkCreateInstance failed, meaning layers could not be chained.";
59     } else {
60         result = "vkCreateInstance succeeded.";
61     }
62 
63     return result;
64 }
65 
android_gputools_cts_RootlessGpuDebug_nativeInitVulkan(JNIEnv * env,jclass)66 jstring android_gputools_cts_RootlessGpuDebug_nativeInitVulkan(JNIEnv* env,
67     jclass /*clazz*/)
68 {
69     std::string result;
70 
71     result = initVulkan();
72 
73     return env->NewStringUTF(result.c_str());
74 }
75 
76 static JNINativeMethod gMethods[] = {
77     {    "nativeInitVulkan", "()Ljava/lang/String;",
78          (void*) android_gputools_cts_RootlessGpuDebug_nativeInitVulkan },
79 };
80 
81 } // anonymous namespace
82 
register_android_gputools_cts_RootlessGpuDebug(JNIEnv * env)83 int register_android_gputools_cts_RootlessGpuDebug(JNIEnv* env) {
84     jclass clazz = env->FindClass("android/rootlessgpudebug/app/RootlessGpuDebugDeviceActivity");
85     return env->RegisterNatives(clazz, gMethods,
86             sizeof(gMethods) / sizeof(JNINativeMethod));
87 }
88