nvflinger.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-3.0-or-later
  3. #pragma once
  4. #include <list>
  5. #include <memory>
  6. #include <mutex>
  7. #include <optional>
  8. #include <thread>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "core/hle/result.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::android {
  32. class BufferQueueCore;
  33. class BufferQueueProducer;
  34. } // namespace Service::android
  35. namespace Service::NVFlinger {
  36. class NVFlinger final {
  37. public:
  38. explicit NVFlinger(Core::System& system_, HosBinderDriverServer& hos_binder_driver_server_);
  39. ~NVFlinger();
  40. /// Sets the NVDrv module instance to use to send buffers to the GPU.
  41. void SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance);
  42. /// Opens the specified display and returns the ID.
  43. ///
  44. /// If an invalid display name is provided, then an empty optional is returned.
  45. [[nodiscard]] std::optional<u64> OpenDisplay(std::string_view name);
  46. /// Creates a layer on the specified display and returns the layer ID.
  47. ///
  48. /// If an invalid display ID is specified, then an empty optional is returned.
  49. [[nodiscard]] std::optional<u64> CreateLayer(u64 display_id);
  50. /// Closes a layer on all displays for the given layer ID.
  51. void CloseLayer(u64 layer_id);
  52. /// Finds the buffer queue ID of the specified layer in the specified display.
  53. ///
  54. /// If an invalid display ID or layer ID is provided, then an empty optional is returned.
  55. [[nodiscard]] std::optional<u32> FindBufferQueueId(u64 display_id, u64 layer_id);
  56. /// Gets the vsync event for the specified display.
  57. ///
  58. /// If an invalid display ID is provided, then VI::ResultNotFound is returned.
  59. /// If the vsync event has already been retrieved, then VI::ResultPermissionDenied is returned.
  60. [[nodiscard]] ResultVal<Kernel::KReadableEvent*> FindVsyncEvent(u64 display_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. struct Layer {
  67. std::unique_ptr<android::BufferQueueCore> core;
  68. std::unique_ptr<android::BufferQueueProducer> producer;
  69. };
  70. private:
  71. [[nodiscard]] std::unique_lock<std::mutex> Lock() const {
  72. return std::unique_lock{*guard};
  73. }
  74. /// Finds the display identified by the specified ID.
  75. [[nodiscard]] VI::Display* FindDisplay(u64 display_id);
  76. /// Finds the display identified by the specified ID.
  77. [[nodiscard]] const VI::Display* FindDisplay(u64 display_id) const;
  78. /// Finds the layer identified by the specified ID in the desired display.
  79. [[nodiscard]] VI::Layer* FindLayer(u64 display_id, u64 layer_id);
  80. /// Finds the layer identified by the specified ID in the desired display.
  81. [[nodiscard]] const VI::Layer* FindLayer(u64 display_id, u64 layer_id) const;
  82. /// Finds the layer identified by the specified ID in the desired display,
  83. /// or creates the layer if it is not found.
  84. /// To be used when the system expects the specified ID to already exist.
  85. [[nodiscard]] VI::Layer* FindOrCreateLayer(u64 display_id, u64 layer_id);
  86. /// Creates a layer with the specified layer ID in the desired display.
  87. void CreateLayerAtId(VI::Display& display, u64 layer_id);
  88. void SplitVSync(std::stop_token stop_token);
  89. std::shared_ptr<Nvidia::Module> nvdrv;
  90. s32 disp_fd;
  91. std::list<VI::Display> displays;
  92. /// Id to use for the next layer that is created, this counter is shared among all displays.
  93. u64 next_layer_id = 1;
  94. /// Id to use for the next buffer queue that is created, this counter is shared among all
  95. /// layers.
  96. u32 next_buffer_queue_id = 1;
  97. u32 swap_interval = 1;
  98. /// Event that handles screen composition.
  99. std::shared_ptr<Core::Timing::EventType> multi_composition_event;
  100. std::shared_ptr<Core::Timing::EventType> single_composition_event;
  101. std::shared_ptr<std::mutex> guard;
  102. Core::System& system;
  103. std::atomic<bool> vsync_signal;
  104. std::jthread vsync_thread;
  105. KernelHelpers::ServiceContext service_context;
  106. HosBinderDriverServer& hos_binder_driver_server;
  107. };
  108. } // namespace Service::NVFlinger