nvflinger.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <list>
  7. #include <memory>
  8. #include <mutex>
  9. #include <optional>
  10. #include <string>
  11. #include <string_view>
  12. #include <thread>
  13. #include <vector>
  14. #include "common/common_types.h"
  15. namespace Common {
  16. class Event;
  17. } // namespace Common
  18. namespace Core::Timing {
  19. class CoreTiming;
  20. struct EventType;
  21. } // namespace Core::Timing
  22. namespace Kernel {
  23. class KReadableEvent;
  24. class KWritableEvent;
  25. } // namespace Kernel
  26. namespace Service::Nvidia {
  27. class Module;
  28. } // namespace Service::Nvidia
  29. namespace Service::VI {
  30. class Display;
  31. class Layer;
  32. } // namespace Service::VI
  33. namespace Service::NVFlinger {
  34. class BufferQueue;
  35. class NVFlinger final {
  36. public:
  37. explicit NVFlinger(Core::System& system_);
  38. ~NVFlinger();
  39. /// Sets the NVDrv module instance to use to send buffers to the GPU.
  40. void SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance);
  41. /// Opens the specified display and returns the ID.
  42. ///
  43. /// If an invalid display name is provided, then an empty optional is returned.
  44. [[nodiscard]] std::optional<u64> OpenDisplay(std::string_view name);
  45. /// Creates a layer on the specified display and returns the layer ID.
  46. ///
  47. /// If an invalid display ID is specified, then an empty optional is returned.
  48. [[nodiscard]] std::optional<u64> CreateLayer(u64 display_id);
  49. /// Closes a layer on all displays for the given layer ID.
  50. void CloseLayer(u64 layer_id);
  51. /// Finds the buffer queue ID of the specified layer in the specified display.
  52. ///
  53. /// If an invalid display ID or layer ID is provided, then an empty optional is returned.
  54. [[nodiscard]] std::optional<u32> FindBufferQueueId(u64 display_id, u64 layer_id);
  55. /// Gets the vsync event for the specified display.
  56. ///
  57. /// If an invalid display ID is provided, then nullptr is returned.
  58. [[nodiscard]] Kernel::KReadableEvent* FindVsyncEvent(u64 display_id);
  59. /// Obtains a buffer queue identified by the ID.
  60. [[nodiscard]] BufferQueue* FindBufferQueue(u32 id);
  61. /// Performs a composition request to the emulated nvidia GPU and triggers the vsync events when
  62. /// finished.
  63. void Compose();
  64. [[nodiscard]] s64 GetNextTicks() const;
  65. private:
  66. [[nodiscard]] std::unique_lock<std::mutex> Lock() const {
  67. return std::unique_lock{*guard};
  68. }
  69. /// Finds the display identified by the specified ID.
  70. [[nodiscard]] VI::Display* FindDisplay(u64 display_id);
  71. /// Finds the display identified by the specified ID.
  72. [[nodiscard]] const VI::Display* FindDisplay(u64 display_id) const;
  73. /// Finds the layer identified by the specified ID in the desired display.
  74. [[nodiscard]] VI::Layer* FindLayer(u64 display_id, u64 layer_id);
  75. /// Finds the layer identified by the specified ID in the desired display.
  76. [[nodiscard]] const VI::Layer* FindLayer(u64 display_id, u64 layer_id) const;
  77. /// Finds the layer identified by the specified ID in the desired display,
  78. /// or creates the layer if it is not found.
  79. /// To be used when the system expects the specified ID to already exist.
  80. [[nodiscard]] VI::Layer* FindOrCreateLayer(u64 display_id, u64 layer_id);
  81. /// Creates a layer with the specified layer ID in the desired display.
  82. void CreateLayerAtId(VI::Display& display, u64 layer_id);
  83. static void VSyncThread(NVFlinger& nv_flinger);
  84. void SplitVSync();
  85. std::shared_ptr<Nvidia::Module> nvdrv;
  86. std::list<VI::Display> displays;
  87. std::vector<std::unique_ptr<BufferQueue>> buffer_queues;
  88. /// Id to use for the next layer that is created, this counter is shared among all displays.
  89. u64 next_layer_id = 1;
  90. /// Id to use for the next buffer queue that is created, this counter is shared among all
  91. /// layers.
  92. u32 next_buffer_queue_id = 1;
  93. u32 swap_interval = 1;
  94. /// Event that handles screen composition.
  95. std::shared_ptr<Core::Timing::EventType> composition_event;
  96. std::shared_ptr<std::mutex> guard;
  97. Core::System& system;
  98. std::unique_ptr<std::thread> vsync_thread;
  99. std::unique_ptr<Common::Event> wait_event;
  100. std::atomic<bool> is_running{};
  101. };
  102. } // namespace Service::NVFlinger