GpuDriver.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. // Based on: Skyline Emulator Project
  4. // SPDX-License-Identifier: MPL-2.0
  5. // Copyright © 2022 Skyline Team and Contributors (https://github.com/skyline-emu/)
  6. #include <jni.h>
  7. #include "Common/EnumUtils.h"
  8. #include "Common/IniFile.h"
  9. #include "jni/AndroidCommon/AndroidCommon.h"
  10. #include "jni/AndroidCommon/IDCache.h"
  11. #include <dlfcn.h>
  12. #include <fcntl.h>
  13. #include <jni.h>
  14. #include <unistd.h>
  15. #include "adrenotools/driver.h"
  16. #include "VideoBackends/Vulkan/VulkanContext.h"
  17. #include "VideoBackends/Vulkan/VulkanLoader.h"
  18. extern "C" {
  19. #if defined(_M_ARM_64)
  20. JNIEXPORT jobjectArray JNICALL
  21. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_getSystemDriverInfo(JNIEnv* env,
  22. jobject)
  23. {
  24. if (!Vulkan::LoadVulkanLibrary(true))
  25. {
  26. return nullptr;
  27. }
  28. u32 vk_api_version = 0;
  29. VkInstance instance = Vulkan::VulkanContext::CreateVulkanInstance(WindowSystemType::Headless,
  30. false, false, &vk_api_version);
  31. if (!instance)
  32. {
  33. return nullptr;
  34. }
  35. if (!Vulkan::LoadVulkanInstanceFunctions(instance))
  36. {
  37. vkDestroyInstance(instance, nullptr);
  38. return nullptr;
  39. }
  40. Vulkan::VulkanContext::GPUList gpu_list = Vulkan::VulkanContext::EnumerateGPUs(instance);
  41. if (gpu_list.empty())
  42. {
  43. vkDestroyInstance(instance, nullptr);
  44. Vulkan::UnloadVulkanLibrary();
  45. return nullptr;
  46. }
  47. VkPhysicalDeviceProperties properties;
  48. vkGetPhysicalDeviceProperties(gpu_list.front(), &properties);
  49. std::string driverId;
  50. if (vkGetPhysicalDeviceProperties2 && vk_api_version >= VK_VERSION_1_1)
  51. {
  52. VkPhysicalDeviceDriverProperties driverProperties;
  53. driverProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES;
  54. driverProperties.pNext = nullptr;
  55. VkPhysicalDeviceProperties2 properties2;
  56. properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2;
  57. properties2.pNext = &driverProperties;
  58. vkGetPhysicalDeviceProperties2(gpu_list.front(), &properties2);
  59. driverId = fmt::format("{}", Common::ToUnderlying(driverProperties.driverID));
  60. }
  61. else
  62. {
  63. driverId = "Unknown";
  64. }
  65. std::string driverVersion =
  66. fmt::format("{}.{}.{}", VK_API_VERSION_MAJOR(properties.driverVersion),
  67. VK_API_VERSION_MINOR(properties.driverVersion),
  68. VK_API_VERSION_PATCH(properties.driverVersion));
  69. vkDestroyInstance(instance, nullptr);
  70. Vulkan::UnloadVulkanLibrary();
  71. auto array = env->NewObjectArray(2, env->FindClass("java/lang/String"), nullptr);
  72. env->SetObjectArrayElement(array, 0, ToJString(env, driverId));
  73. env->SetObjectArrayElement(array, 1, ToJString(env, driverVersion));
  74. return array;
  75. }
  76. JNIEXPORT jboolean JNICALL
  77. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_supportsCustomDriverLoading(
  78. JNIEnv* env, jobject instance)
  79. {
  80. // If the KGSL device exists custom drivers can be loaded using adrenotools
  81. return Vulkan::SupportsCustomDriver();
  82. }
  83. JNIEXPORT jboolean JNICALL
  84. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_supportsForceMaxGpuClocks(
  85. JNIEnv* env, jobject instance)
  86. {
  87. // If the KGSL device exists adrenotools can be used to set GPU turbo mode
  88. return Vulkan::SupportsCustomDriver();
  89. }
  90. JNIEXPORT void JNICALL
  91. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_forceMaxGpuClocks(
  92. JNIEnv* env, jobject instance, jboolean enable)
  93. {
  94. adrenotools_set_turbo(enable);
  95. }
  96. #else
  97. JNIEXPORT jobjectArray JNICALL
  98. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_getSystemDriverInfo(
  99. JNIEnv* env, jobject instance)
  100. {
  101. auto array = env->NewObjectArray(0, env->FindClass("java/lang/String"), nullptr);
  102. return array;
  103. }
  104. JNIEXPORT jboolean JNICALL
  105. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_supportsCustomDriverLoading(
  106. JNIEnv* env, jobject instance)
  107. {
  108. return false;
  109. }
  110. JNIEXPORT jboolean JNICALL
  111. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_supportsForceMaxGpuClocks(
  112. JNIEnv* env, jobject instance)
  113. {
  114. return false;
  115. }
  116. JNIEXPORT void JNICALL
  117. Java_org_dolphinemu_dolphinemu_utils_GpuDriverHelper_00024Companion_forceMaxGpuClocks(
  118. JNIEnv* env, jobject instance, jboolean enable)
  119. {
  120. }
  121. #endif
  122. }