nvflinger.cpp 8.6 KB

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