nvflinger.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <memory>
  7. #include <mutex>
  8. #include <optional>
  9. #include <string>
  10. #include <string_view>
  11. #include <thread>
  12. #include <vector>
  13. #include "common/common_types.h"
  14. #include "core/hle/kernel/object.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 ReadableEvent;
  24. class WritableEvent;
  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. 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. 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. std::optional<u32> FindBufferQueueId(u64 display_id, u64 layer_id) const;
  55. /// Gets the vsync event for the specified display.
  56. ///
  57. /// If an invalid display ID is provided, then nullptr is returned.
  58. std::shared_ptr<Kernel::ReadableEvent> FindVsyncEvent(u64 display_id) const;
  59. /// Obtains a buffer queue identified by the ID.
  60. BufferQueue& FindBufferQueue(u32 id);
  61. /// Obtains a buffer queue identified by the ID.
  62. const BufferQueue& FindBufferQueue(u32 id) const;
  63. /// Performs a composition request to the emulated nvidia GPU and triggers the vsync events when
  64. /// finished.
  65. void Compose();
  66. s64 GetNextTicks() const;
  67. std::unique_lock<std::mutex> Lock() {
  68. return std::unique_lock{*guard};
  69. }
  70. private:
  71. /// Finds the display identified by the specified ID.
  72. VI::Display* FindDisplay(u64 display_id);
  73. /// Finds the display identified by the specified ID.
  74. const VI::Display* FindDisplay(u64 display_id) const;
  75. /// Finds the layer identified by the specified ID in the desired display.
  76. VI::Layer* FindLayer(u64 display_id, u64 layer_id);
  77. /// Finds the layer identified by the specified ID in the desired display.
  78. const VI::Layer* FindLayer(u64 display_id, u64 layer_id) const;
  79. static void VSyncThread(NVFlinger& nv_flinger);
  80. void SplitVSync();
  81. std::shared_ptr<Nvidia::Module> nvdrv;
  82. std::vector<VI::Display> displays;
  83. std::vector<BufferQueue> buffer_queues;
  84. /// Id to use for the next layer that is created, this counter is shared among all displays.
  85. u64 next_layer_id = 1;
  86. /// Id to use for the next buffer queue that is created, this counter is shared among all
  87. /// layers.
  88. u32 next_buffer_queue_id = 1;
  89. u32 swap_interval = 1;
  90. /// Event that handles screen composition.
  91. std::shared_ptr<Core::Timing::EventType> composition_event;
  92. std::shared_ptr<std::mutex> guard;
  93. Core::System& system;
  94. std::unique_ptr<std::thread> vsync_thread;
  95. std::unique_ptr<Common::Event> wait_event;
  96. std::atomic<bool> is_running{};
  97. };
  98. } // namespace Service::NVFlinger