nvflinger.h 4.1 KB

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