FrameDumper.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2023 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include "Common/CommonTypes.h"
  5. #include "Common/Event.h"
  6. #include "Common/Flag.h"
  7. #include "Common/MathUtil.h"
  8. #include "Common/Thread.h"
  9. #include "VideoCommon/FrameDumpFFMpeg.h"
  10. #include "VideoCommon/VideoEvents.h"
  11. class AbstractStagingTexture;
  12. class AbstractTexture;
  13. class AbstractFramebuffer;
  14. class FrameDumper
  15. {
  16. public:
  17. FrameDumper();
  18. ~FrameDumper();
  19. // Ensures all rendered frames are queued for encoding.
  20. void FlushFrameDump();
  21. // Fills the frame dump staging texture with the current XFB texture.
  22. void DumpCurrentFrame(const AbstractTexture* src_texture,
  23. const MathUtil::Rectangle<int>& src_rect,
  24. const MathUtil::Rectangle<int>& target_rect, u64 ticks, int frame_number);
  25. void SaveScreenshot(std::string filename);
  26. bool IsFrameDumping() const;
  27. int GetRequiredResolutionLeastCommonMultiple() const;
  28. void DoState(PointerWrap& p);
  29. private:
  30. // NOTE: The methods below are called on the framedumping thread.
  31. void FrameDumpThreadFunc();
  32. bool StartFrameDumpToFFMPEG(const FrameData&);
  33. void DumpFrameToFFMPEG(const FrameData&);
  34. void StopFrameDumpToFFMPEG();
  35. std::string GetFrameDumpNextImageFileName() const;
  36. bool StartFrameDumpToImage(const FrameData&);
  37. void DumpFrameToImage(const FrameData&);
  38. void ShutdownFrameDumping();
  39. // Checks that the frame dump render texture exists and is the correct size.
  40. bool CheckFrameDumpRenderTexture(u32 target_width, u32 target_height);
  41. // Checks that the frame dump readback texture exists and is the correct size.
  42. bool CheckFrameDumpReadbackTexture(u32 target_width, u32 target_height);
  43. // Asynchronously encodes the specified pointer of frame data to the frame dump.
  44. void DumpFrameData(const u8* data, int w, int h, int stride);
  45. // Ensures all encoded frames have been written to the output file.
  46. void FinishFrameData();
  47. std::thread m_frame_dump_thread;
  48. Common::Flag m_frame_dump_thread_running;
  49. // Used to kick frame dump thread.
  50. Common::Event m_frame_dump_start;
  51. // Set by frame dump thread on frame completion.
  52. Common::Event m_frame_dump_done;
  53. // Holds emulation state during the last swap when dumping.
  54. FrameState m_last_frame_state;
  55. // Communication of frame between video and dump threads.
  56. FrameData m_frame_dump_data;
  57. // Texture used for screenshot/frame dumping
  58. std::unique_ptr<AbstractTexture> m_frame_dump_render_texture;
  59. std::unique_ptr<AbstractFramebuffer> m_frame_dump_render_framebuffer;
  60. // Double buffer:
  61. std::unique_ptr<AbstractStagingTexture> m_frame_dump_readback_texture;
  62. std::unique_ptr<AbstractStagingTexture> m_frame_dump_output_texture;
  63. // Set when readback texture holds a frame that needs to be dumped.
  64. bool m_frame_dump_needs_flush = false;
  65. // Set when thread is processing output texture.
  66. bool m_frame_dump_frame_running = false;
  67. // Used to generate screenshot names.
  68. u32 m_frame_dump_image_counter = 0;
  69. FFMpegFrameDump m_ffmpeg_dump;
  70. // Screenshots
  71. Common::Flag m_screenshot_request;
  72. Common::Event m_screenshot_completed;
  73. std::mutex m_screenshot_lock;
  74. std::string m_screenshot_name;
  75. Common::EventHook m_frame_end_handle;
  76. };
  77. extern std::unique_ptr<FrameDumper> g_frame_dumper;