OnScreenUI.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <map>
  5. #include <memory>
  6. #include <mutex>
  7. #include <span>
  8. #include <vector>
  9. #include "Common/CommonTypes.h"
  10. #include "VideoCommon/OnScreenUIKeyMap.h"
  11. class NativeVertexFormat;
  12. class AbstractTexture;
  13. class AbstractPipeline;
  14. namespace VideoCommon
  15. {
  16. // OnScreenUI handles all the ImGui rendering.
  17. class OnScreenUI
  18. {
  19. public:
  20. OnScreenUI() = default;
  21. ~OnScreenUI();
  22. // ImGui initialization depends on being able to create textures and pipelines, so do it last.
  23. bool Initialize(u32 width, u32 height, float scale);
  24. // Returns a lock for the ImGui mutex, enabling data structures to be modified from outside.
  25. // Use with care, only non-drawing functions should be called from outside the video thread,
  26. // as the drawing is tied to a "frame".
  27. std::unique_lock<std::mutex> GetImGuiLock();
  28. bool IsReady() { return m_ready; }
  29. // Sets up ImGui state for the next frame.
  30. // This function itself acquires the ImGui lock, so it should not be held.
  31. void BeginImGuiFrame(u32 width, u32 height);
  32. // Same as above but without locking the ImGui lock.
  33. void BeginImGuiFrameUnlocked(u32 width, u32 height);
  34. // Renders ImGui windows to the currently-bound framebuffer.
  35. // Should be called with the ImGui lock held.
  36. void DrawImGui();
  37. // Recompiles ImGui pipeline - call when stereo mode changes.
  38. bool RecompileImGuiPipeline();
  39. void SetScale(float backbuffer_scale);
  40. void Finalize();
  41. // Receive keyboard and mouse from QT
  42. void SetKeyMap(const DolphinKeyMap& key_map);
  43. void SetKey(u32 key, bool is_down, const char* chars);
  44. void SetMousePos(float x, float y);
  45. void SetMousePress(u32 button_mask);
  46. private:
  47. void DrawDebugText();
  48. void DrawChallengesAndLeaderboards();
  49. // ImGui resources.
  50. std::unique_ptr<NativeVertexFormat> m_imgui_vertex_format;
  51. std::vector<std::unique_ptr<AbstractTexture>> m_imgui_textures;
  52. std::unique_ptr<AbstractPipeline> m_imgui_pipeline;
  53. std::map<u32, int> m_dolphin_to_imgui_map;
  54. std::mutex m_imgui_mutex;
  55. u64 m_imgui_last_frame_time = 0;
  56. u32 m_backbuffer_width = 1;
  57. u32 m_backbuffer_height = 1;
  58. float m_backbuffer_scale = 1.0;
  59. #ifdef USE_RETRO_ACHIEVEMENTS
  60. std::map<int, std::unique_ptr<AbstractTexture>, std::less<>> m_challenge_texture_map;
  61. #endif // USE_RETRO_ACHIEVEMENTS
  62. bool m_ready = false;
  63. };
  64. } // namespace VideoCommon