nvflinger.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <optional>
  6. #include "common/assert.h"
  7. #include "common/logging/log.h"
  8. #include "common/microprofile.h"
  9. #include "common/scope_exit.h"
  10. #include "common/settings.h"
  11. #include "common/thread.h"
  12. #include "core/core.h"
  13. #include "core/core_timing.h"
  14. #include "core/core_timing_util.h"
  15. #include "core/hardware_properties.h"
  16. #include "core/hle/kernel/k_readable_event.h"
  17. #include "core/hle/kernel/kernel.h"
  18. #include "core/hle/service/nvdrv/devices/nvdisp_disp0.h"
  19. #include "core/hle/service/nvdrv/nvdrv.h"
  20. #include "core/hle/service/nvflinger/buffer_queue.h"
  21. #include "core/hle/service/nvflinger/nvflinger.h"
  22. #include "core/hle/service/vi/display/vi_display.h"
  23. #include "core/hle/service/vi/layer/vi_layer.h"
  24. #include "core/perf_stats.h"
  25. #include "video_core/renderer_base.h"
  26. namespace Service::NVFlinger {
  27. constexpr auto frame_ns = std::chrono::nanoseconds{1000000000 / 60};
  28. void NVFlinger::VSyncThread(NVFlinger& nv_flinger) {
  29. nv_flinger.SplitVSync();
  30. }
  31. void NVFlinger::SplitVSync() {
  32. system.RegisterHostThread();
  33. std::string name = "yuzu:VSyncThread";
  34. MicroProfileOnThreadCreate(name.c_str());
  35. // Cleanup
  36. SCOPE_EXIT({ MicroProfileOnThreadExit(); });
  37. Common::SetCurrentThreadName(name.c_str());
  38. Common::SetCurrentThreadPriority(Common::ThreadPriority::High);
  39. s64 delay = 0;
  40. while (is_running) {
  41. guard->lock();
  42. const s64 time_start = system.CoreTiming().GetGlobalTimeNs().count();
  43. Compose();
  44. const auto ticks = GetNextTicks();
  45. const s64 time_end = system.CoreTiming().GetGlobalTimeNs().count();
  46. const s64 time_passed = time_end - time_start;
  47. const s64 next_time = std::max<s64>(0, ticks - time_passed - delay);
  48. guard->unlock();
  49. if (next_time > 0) {
  50. wait_event->WaitFor(std::chrono::nanoseconds{next_time});
  51. }
  52. delay = (system.CoreTiming().GetGlobalTimeNs().count() - time_end) - next_time;
  53. }
  54. }
  55. NVFlinger::NVFlinger(Core::System& system_) : system(system_) {
  56. displays.emplace_back(0, "Default", system);
  57. displays.emplace_back(1, "External", system);
  58. displays.emplace_back(2, "Edid", system);
  59. displays.emplace_back(3, "Internal", system);
  60. displays.emplace_back(4, "Null", system);
  61. guard = std::make_shared<std::mutex>();
  62. // Schedule the screen composition events
  63. composition_event = Core::Timing::CreateEvent(
  64. "ScreenComposition", [this](std::uintptr_t, std::chrono::nanoseconds ns_late) {
  65. const auto lock_guard = Lock();
  66. Compose();
  67. const auto ticks = std::chrono::nanoseconds{GetNextTicks()};
  68. const auto ticks_delta = ticks - ns_late;
  69. const auto future_ns = std::max(std::chrono::nanoseconds::zero(), ticks_delta);
  70. this->system.CoreTiming().ScheduleEvent(future_ns, composition_event);
  71. });
  72. if (system.IsMulticore()) {
  73. is_running = true;
  74. wait_event = std::make_unique<Common::Event>();
  75. vsync_thread = std::make_unique<std::thread>(VSyncThread, std::ref(*this));
  76. } else {
  77. system.CoreTiming().ScheduleEvent(frame_ns, composition_event);
  78. }
  79. }
  80. NVFlinger::~NVFlinger() {
  81. for (auto& buffer_queue : buffer_queues) {
  82. buffer_queue->Disconnect();
  83. }
  84. if (system.IsMulticore()) {
  85. is_running = false;
  86. wait_event->Set();
  87. vsync_thread->join();
  88. vsync_thread.reset();
  89. wait_event.reset();
  90. } else {
  91. system.CoreTiming().UnscheduleEvent(composition_event, 0);
  92. }
  93. }
  94. void NVFlinger::SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance) {
  95. nvdrv = std::move(instance);
  96. }
  97. std::optional<u64> NVFlinger::OpenDisplay(std::string_view name) {
  98. const auto lock_guard = Lock();
  99. LOG_DEBUG(Service, "Opening \"{}\" display", name);
  100. // TODO(Subv): Currently we only support the Default display.
  101. ASSERT(name == "Default");
  102. const auto itr =
  103. std::find_if(displays.begin(), displays.end(),
  104. [&](const VI::Display& display) { return display.GetName() == name; });
  105. if (itr == displays.end()) {
  106. return std::nullopt;
  107. }
  108. return itr->GetID();
  109. }
  110. std::optional<u64> NVFlinger::CreateLayer(u64 display_id) {
  111. const auto lock_guard = Lock();
  112. auto* const display = FindDisplay(display_id);
  113. if (display == nullptr) {
  114. return std::nullopt;
  115. }
  116. const u64 layer_id = next_layer_id++;
  117. CreateLayerAtId(*display, layer_id);
  118. return layer_id;
  119. }
  120. void NVFlinger::CreateLayerAtId(VI::Display& display, u64 layer_id) {
  121. const u32 buffer_queue_id = next_buffer_queue_id++;
  122. buffer_queues.emplace_back(
  123. std::make_unique<BufferQueue>(system.Kernel(), buffer_queue_id, layer_id));
  124. display.CreateLayer(layer_id, *buffer_queues.back());
  125. }
  126. void NVFlinger::CloseLayer(u64 layer_id) {
  127. const auto lock_guard = Lock();
  128. for (auto& display : displays) {
  129. display.CloseLayer(layer_id);
  130. }
  131. }
  132. std::optional<u32> NVFlinger::FindBufferQueueId(u64 display_id, u64 layer_id) {
  133. const auto lock_guard = Lock();
  134. const auto* const layer = FindOrCreateLayer(display_id, layer_id);
  135. if (layer == nullptr) {
  136. return std::nullopt;
  137. }
  138. return layer->GetBufferQueue().GetId();
  139. }
  140. Kernel::KReadableEvent* NVFlinger::FindVsyncEvent(u64 display_id) {
  141. const auto lock_guard = Lock();
  142. auto* const display = FindDisplay(display_id);
  143. if (display == nullptr) {
  144. return nullptr;
  145. }
  146. return &display->GetVSyncEvent();
  147. }
  148. BufferQueue* NVFlinger::FindBufferQueue(u32 id) {
  149. const auto lock_guard = Lock();
  150. const auto itr = std::find_if(buffer_queues.begin(), buffer_queues.end(),
  151. [id](const auto& queue) { return queue->GetId() == id; });
  152. if (itr == buffer_queues.end()) {
  153. return nullptr;
  154. }
  155. return itr->get();
  156. }
  157. VI::Display* NVFlinger::FindDisplay(u64 display_id) {
  158. const auto itr =
  159. std::find_if(displays.begin(), displays.end(),
  160. [&](const VI::Display& display) { return display.GetID() == display_id; });
  161. if (itr == displays.end()) {
  162. return nullptr;
  163. }
  164. return &*itr;
  165. }
  166. const VI::Display* NVFlinger::FindDisplay(u64 display_id) const {
  167. const auto itr =
  168. std::find_if(displays.begin(), displays.end(),
  169. [&](const VI::Display& display) { return display.GetID() == display_id; });
  170. if (itr == displays.end()) {
  171. return nullptr;
  172. }
  173. return &*itr;
  174. }
  175. VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) {
  176. auto* const display = FindDisplay(display_id);
  177. if (display == nullptr) {
  178. return nullptr;
  179. }
  180. return display->FindLayer(layer_id);
  181. }
  182. const VI::Layer* NVFlinger::FindLayer(u64 display_id, u64 layer_id) const {
  183. const auto* const display = FindDisplay(display_id);
  184. if (display == nullptr) {
  185. return nullptr;
  186. }
  187. return display->FindLayer(layer_id);
  188. }
  189. VI::Layer* NVFlinger::FindOrCreateLayer(u64 display_id, u64 layer_id) {
  190. auto* const display = FindDisplay(display_id);
  191. if (display == nullptr) {
  192. return nullptr;
  193. }
  194. auto* layer = display->FindLayer(layer_id);
  195. if (layer == nullptr) {
  196. LOG_DEBUG(Service, "Layer at id {} not found. Trying to create it.", layer_id);
  197. CreateLayerAtId(*display, layer_id);
  198. return display->FindLayer(layer_id);
  199. }
  200. return layer;
  201. }
  202. void NVFlinger::Compose() {
  203. for (auto& display : displays) {
  204. // Trigger vsync for this display at the end of drawing
  205. SCOPE_EXIT({ display.SignalVSyncEvent(); });
  206. // Don't do anything for displays without layers.
  207. if (!display.HasLayers())
  208. continue;
  209. // TODO(Subv): Support more than 1 layer.
  210. VI::Layer& layer = display.GetLayer(0);
  211. auto& buffer_queue = layer.GetBufferQueue();
  212. // Search for a queued buffer and acquire it
  213. auto buffer = buffer_queue.AcquireBuffer();
  214. if (!buffer) {
  215. continue;
  216. }
  217. const auto& igbp_buffer = buffer->get().igbp_buffer;
  218. if (!system.IsPoweredOn()) {
  219. return; // We are likely shutting down
  220. }
  221. auto& gpu = system.GPU();
  222. const auto& multi_fence = buffer->get().multi_fence;
  223. guard->unlock();
  224. for (u32 fence_id = 0; fence_id < multi_fence.num_fences; fence_id++) {
  225. const auto& fence = multi_fence.fences[fence_id];
  226. gpu.WaitFence(fence.id, fence.value);
  227. }
  228. guard->lock();
  229. MicroProfileFlip();
  230. // Now send the buffer to the GPU for drawing.
  231. // TODO(Subv): Support more than just disp0. The display device selection is probably based
  232. // on which display we're drawing (Default, Internal, External, etc)
  233. auto nvdisp = nvdrv->GetDevice<Nvidia::Devices::nvdisp_disp0>("/dev/nvdisp_disp0");
  234. ASSERT(nvdisp);
  235. nvdisp->flip(igbp_buffer.gpu_buffer_id, igbp_buffer.offset, igbp_buffer.format,
  236. igbp_buffer.width, igbp_buffer.height, igbp_buffer.stride,
  237. buffer->get().transform, buffer->get().crop_rect);
  238. swap_interval = buffer->get().swap_interval;
  239. buffer_queue.ReleaseBuffer(buffer->get().slot);
  240. }
  241. }
  242. s64 NVFlinger::GetNextTicks() const {
  243. constexpr s64 max_hertz = 120LL;
  244. return (1000000000 * (1LL << swap_interval)) / max_hertz;
  245. }
  246. } // namespace Service::NVFlinger