nvnflinger.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "common/polyfill_thread.h"
  12. #include "common/thread.h"
  13. #include "core/hle/result.h"
  14. #include "core/hle/service/kernel_helpers.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. } // namespace Kernel
  25. namespace Service::Nvidia {
  26. class Module;
  27. } // namespace Service::Nvidia
  28. namespace Service::VI {
  29. class Display;
  30. class Layer;
  31. } // namespace Service::VI
  32. namespace Service::android {
  33. class BufferQueueCore;
  34. class BufferQueueProducer;
  35. } // namespace Service::android
  36. namespace Service::Nvnflinger {
  37. class FbShareBufferManager;
  38. class HardwareComposer;
  39. class HosBinderDriverServer;
  40. class Nvnflinger final {
  41. public:
  42. explicit Nvnflinger(Core::System& system_, HosBinderDriverServer& hos_binder_driver_server_);
  43. ~Nvnflinger();
  44. void ShutdownLayers();
  45. /// Sets the NVDrv module instance to use to send buffers to the GPU.
  46. void SetNVDrvInstance(std::shared_ptr<Nvidia::Module> instance);
  47. /// Opens the specified display and returns the ID.
  48. ///
  49. /// If an invalid display name is provided, then an empty optional is returned.
  50. [[nodiscard]] std::optional<u64> OpenDisplay(std::string_view name);
  51. /// Closes the specified display by its ID.
  52. ///
  53. /// Returns false if an invalid display ID is provided.
  54. [[nodiscard]] bool CloseDisplay(u64 display_id);
  55. /// Creates a layer on the specified display and returns the layer ID.
  56. ///
  57. /// If an invalid display ID is specified, then an empty optional is returned.
  58. [[nodiscard]] std::optional<u64> CreateLayer(u64 display_id);
  59. /// Opens a layer on all displays for the given layer ID.
  60. bool OpenLayer(u64 layer_id);
  61. /// Closes a layer on all displays for the given layer ID.
  62. bool CloseLayer(u64 layer_id);
  63. /// Makes a layer visible on all displays for the given layer ID.
  64. void SetLayerVisibility(u64 layer_id, bool visible);
  65. /// Destroys the given layer ID.
  66. void DestroyLayer(u64 layer_id);
  67. /// Finds the buffer queue ID of the specified layer in the specified display.
  68. ///
  69. /// If an invalid display ID or layer ID is provided, then an empty optional is returned.
  70. [[nodiscard]] std::optional<u32> FindBufferQueueId(u64 display_id, u64 layer_id);
  71. /// Gets the vsync event for the specified display.
  72. ///
  73. /// If an invalid display ID is provided, then VI::ResultNotFound is returned.
  74. /// If the vsync event has already been retrieved, then VI::ResultPermissionDenied is returned.
  75. [[nodiscard]] Result FindVsyncEvent(Kernel::KReadableEvent** out_vsync_event, u64 display_id);
  76. /// Performs a composition request to the emulated nvidia GPU and triggers the vsync events when
  77. /// finished.
  78. void Compose();
  79. [[nodiscard]] s64 GetNextTicks() const;
  80. FbShareBufferManager& GetSystemBufferManager();
  81. private:
  82. struct Layer {
  83. std::unique_ptr<android::BufferQueueCore> core;
  84. std::unique_ptr<android::BufferQueueProducer> producer;
  85. };
  86. friend class FbShareBufferManager;
  87. private:
  88. [[nodiscard]] std::unique_lock<std::mutex> Lock() const {
  89. return std::unique_lock{*guard};
  90. }
  91. /// Finds the display identified by the specified ID.
  92. [[nodiscard]] VI::Display* FindDisplay(u64 display_id);
  93. /// Finds the display identified by the specified ID.
  94. [[nodiscard]] const VI::Display* FindDisplay(u64 display_id) const;
  95. /// Finds the layer identified by the specified ID in the desired display.
  96. [[nodiscard]] VI::Layer* FindLayer(u64 display_id, u64 layer_id);
  97. /// Creates a layer with the specified layer ID in the desired display.
  98. void CreateLayerAtId(VI::Display& display, u64 layer_id);
  99. void SplitVSync(std::stop_token stop_token);
  100. std::shared_ptr<Nvidia::Module> nvdrv;
  101. s32 disp_fd;
  102. std::list<VI::Display> displays;
  103. /// Id to use for the next layer that is created, this counter is shared among all displays.
  104. u64 next_layer_id = 1;
  105. /// Id to use for the next buffer queue that is created, this counter is shared among all
  106. /// layers.
  107. u32 next_buffer_queue_id = 1;
  108. s32 swap_interval = 1;
  109. f32 compose_speed_scale = 1.0f;
  110. bool is_abandoned = false;
  111. /// Event that handles screen composition.
  112. std::shared_ptr<Core::Timing::EventType> multi_composition_event;
  113. std::shared_ptr<Core::Timing::EventType> single_composition_event;
  114. std::unique_ptr<FbShareBufferManager> system_buffer_manager;
  115. std::shared_ptr<std::mutex> guard;
  116. Core::System& system;
  117. Common::Event vsync_signal;
  118. std::jthread vsync_thread;
  119. KernelHelpers::ServiceContext service_context;
  120. HosBinderDriverServer& hos_binder_driver_server;
  121. };
  122. } // namespace Service::Nvnflinger