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