| 1 | // Copyright 2023 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "VideoCommon/FrameDumper.h" |
| 5 | |
| 6 | #include "Common/Assert.h" |
| 7 | #include "Common/FileUtil.h" |
| 8 | #include "Common/Image.h" |
| 9 | |
| 10 | #include "Core/Config/GraphicsSettings.h" |
| 11 | #include "Core/Config/MainSettings.h" |
| 12 | |
| 13 | #include "VideoCommon/AbstractFramebuffer.h" |
| 14 | #include "VideoCommon/AbstractGfx.h" |
| 15 | #include "VideoCommon/AbstractStagingTexture.h" |
| 16 | #include "VideoCommon/AbstractTexture.h" |
| 17 | #include "VideoCommon/OnScreenDisplay.h" |
| 18 | #include "VideoCommon/Present.h" |
| 19 | |
| 20 | // The video encoder needs the image to be a multiple of x samples. |
| 21 | static constexpr int VIDEO_ENCODER_LCM = 4; |
| 22 | |
| 23 | static bool DumpFrameToPNG(const FrameData& frame, const std::string& file_name) |
| 24 | { |
| 25 | return Common::ConvertRGBAToRGBAndSavePNG(file_name, frame.data, frame.width, frame.height, |
| 26 | frame.stride, |
| 27 | Config::Get(Config::GFX_PNG_COMPRESSION_LEVEL)); |
| 28 | } |
| 29 | |
| 30 | FrameDumper::FrameDumper() |
| 31 | { |
| 32 | m_frame_end_handle = |
| 33 | GetVideoEvents().after_frame_event.Register([this](Core::System&) { FlushFrameDump(); }); |
| 34 | } |
| 35 | |
| 36 | FrameDumper::~FrameDumper() |
| 37 | { |
| 38 | ShutdownFrameDumping(); |
| 39 | } |
| 40 | |
| 41 | void FrameDumper::DumpCurrentFrame(const AbstractTexture* src_texture, |
| 42 | const MathUtil::Rectangle<int>& src_rect, |
| 43 | const MathUtil::Rectangle<int>& target_rect, u64 ticks, |
| 44 | int frame_number) |
| 45 | { |
| 46 | int source_width = src_rect.GetWidth(); |
| 47 | int source_height = src_rect.GetHeight(); |
| 48 | int target_width = target_rect.GetWidth(); |
| 49 | int target_height = target_rect.GetHeight(); |
| 50 | |
| 51 | // We only need to render a copy if we need to stretch/scale the XFB copy. |
| 52 | MathUtil::Rectangle<int> copy_rect = src_rect; |
| 53 | if (source_width != target_width || source_height != target_height) |
| 54 | { |
| 55 | if (!CheckFrameDumpRenderTexture(target_width, target_height)) |
| 56 | return; |
| 57 | |
| 58 | g_gfx->ScaleTexture(m_frame_dump_render_framebuffer.get(), |
| 59 | m_frame_dump_render_framebuffer->GetRect(), src_texture, src_rect); |
| 60 | src_texture = m_frame_dump_render_texture.get(); |
| 61 | copy_rect = src_texture->GetRect(); |
| 62 | } |
| 63 | |
| 64 | if (!CheckFrameDumpReadbackTexture(target_width, target_height)) |
| 65 | return; |
| 66 | |
| 67 | m_frame_dump_readback_texture->CopyFromTexture(src_texture, copy_rect, 0, 0, |
| 68 | m_frame_dump_readback_texture->GetRect()); |
| 69 | m_last_frame_state = m_ffmpeg_dump.FetchState(ticks, frame_number); |
| 70 | m_frame_dump_needs_flush = true; |
| 71 | } |
| 72 | |
| 73 | bool FrameDumper::CheckFrameDumpRenderTexture(u32 target_width, u32 target_height) |
| 74 | { |
| 75 | // Ensure framebuffer exists (we lazily allocate it in case frame dumping isn't used). |
| 76 | // Or, resize texture if it isn't large enough to accommodate the current frame. |
| 77 | if (m_frame_dump_render_texture && m_frame_dump_render_texture->GetWidth() == target_width && |
| 78 | m_frame_dump_render_texture->GetHeight() == target_height) |
| 79 | { |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | // Recreate texture, but release before creating so we don't temporarily use twice the RAM. |
| 84 | m_frame_dump_render_framebuffer.reset(); |
| 85 | m_frame_dump_render_texture.reset(); |
| 86 | m_frame_dump_render_texture = g_gfx->CreateTexture( |
| 87 | TextureConfig(target_width, target_height, 1, 1, 1, AbstractTextureFormat::RGBA8, |
| 88 | AbstractTextureFlag_RenderTarget, AbstractTextureType::Texture_2DArray), |
| 89 | "Frame dump render texture"); |
| 90 | if (!m_frame_dump_render_texture) |
| 91 | { |
| 92 | PanicAlertFmt("Failed to allocate frame dump render texture"); |
| 93 | return false; |
| 94 | } |
| 95 | m_frame_dump_render_framebuffer = |
| 96 | g_gfx->CreateFramebuffer(m_frame_dump_render_texture.get(), nullptr); |
| 97 | ASSERT(m_frame_dump_render_framebuffer); |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | bool FrameDumper::CheckFrameDumpReadbackTexture(u32 target_width, u32 target_height) |
| 102 | { |
| 103 | std::unique_ptr<AbstractStagingTexture>& rbtex = m_frame_dump_readback_texture; |
| 104 | if (rbtex && rbtex->GetWidth() == target_width && rbtex->GetHeight() == target_height) |
| 105 | return true; |
| 106 | |
| 107 | rbtex.reset(); |
| 108 | rbtex = g_gfx->CreateStagingTexture(StagingTextureType::Readback, |
| 109 | TextureConfig(target_width, target_height, 1, 1, 1, |
| 110 | AbstractTextureFormat::RGBA8, 0, |
| 111 | AbstractTextureType::Texture_2DArray)); |
| 112 | if (!rbtex) |
| 113 | return false; |
| 114 | |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | void FrameDumper::FlushFrameDump() |
| 119 | { |
| 120 | if (!m_frame_dump_needs_flush) |
| 121 | return; |
| 122 | |
| 123 | // Ensure dumping thread is done with output texture before swapping. |
| 124 | FinishFrameData(); |
| 125 | |
| 126 | std::swap(m_frame_dump_output_texture, m_frame_dump_readback_texture); |
| 127 | |
| 128 | // Queue encoding of the last frame dumped. |
| 129 | auto& output = m_frame_dump_output_texture; |
| 130 | output->Flush(); |
| 131 | if (output->Map()) |
| 132 | { |
| 133 | DumpFrameData(reinterpret_cast<u8*>(output->GetMappedPointer()), output->GetConfig().width, |
| 134 | output->GetConfig().height, static_cast<int>(output->GetMappedStride())); |
| 135 | } |
| 136 | else |
| 137 | { |
| 138 | ERROR_LOG_FMT(VIDEO, "Failed to map texture for dumping."); |
| 139 | } |
| 140 | |
| 141 | m_frame_dump_needs_flush = false; |
| 142 | |
| 143 | // Shutdown frame dumping if it is no longer active. |
| 144 | if (!IsFrameDumping()) |
| 145 | ShutdownFrameDumping(); |
| 146 | } |
| 147 | |
| 148 | void FrameDumper::ShutdownFrameDumping() |
| 149 | { |
| 150 | // Ensure the last queued readback has been sent to the encoder. |
| 151 | FlushFrameDump(); |
| 152 | |
| 153 | if (!m_frame_dump_thread_running.IsSet()) |
| 154 | return; |
| 155 | |
| 156 | // Ensure previous frame has been encoded. |
| 157 | FinishFrameData(); |
| 158 | |
| 159 | // Wake thread up, and wait for it to exit. |
| 160 | m_frame_dump_thread_running.Clear(); |
| 161 | m_frame_dump_start.Set(); |
| 162 | if (m_frame_dump_thread.joinable()) |
| 163 | m_frame_dump_thread.join(); |
| 164 | m_frame_dump_render_framebuffer.reset(); |
| 165 | m_frame_dump_render_texture.reset(); |
| 166 | |
| 167 | m_frame_dump_readback_texture.reset(); |
| 168 | m_frame_dump_output_texture.reset(); |
| 169 | } |
| 170 | |
| 171 | void FrameDumper::DumpFrameData(const u8* data, int w, int h, int stride) |
| 172 | { |
| 173 | m_frame_dump_data = FrameData{data, w, h, stride, m_last_frame_state}; |
| 174 | |
| 175 | if (!m_frame_dump_thread_running.IsSet()) |
| 176 | { |
| 177 | if (m_frame_dump_thread.joinable()) |
| 178 | m_frame_dump_thread.join(); |
| 179 | m_frame_dump_thread_running.Set(); |
| 180 | m_frame_dump_thread = std::thread(&FrameDumper::FrameDumpThreadFunc, this); |
| 181 | } |
| 182 | |
| 183 | // Wake worker thread up. |
| 184 | m_frame_dump_start.Set(); |
| 185 | m_frame_dump_frame_running = true; |
| 186 | } |
| 187 | |
| 188 | void FrameDumper::FinishFrameData() |
| 189 | { |
| 190 | if (!m_frame_dump_frame_running) |
| 191 | return; |
| 192 | |
| 193 | m_frame_dump_done.Wait(); |
| 194 | m_frame_dump_frame_running = false; |
| 195 | |
| 196 | m_frame_dump_output_texture->Unmap(); |
| 197 | } |
| 198 | |
| 199 | void FrameDumper::FrameDumpThreadFunc() |
| 200 | { |
| 201 | Common::SetCurrentThreadName("FrameDumping"); |
| 202 | |
| 203 | bool dump_to_ffmpeg = !Config::Get(Config::GFX_DUMP_FRAMES_AS_IMAGES); |
| 204 | bool frame_dump_started = false; |
| 205 | |
| 206 | // If Dolphin was compiled without ffmpeg, we only support dumping to images. |
| 207 | #if !defined(HAVE_FFMPEG) |
| 208 | if (dump_to_ffmpeg) |
| 209 | { |
| 210 | WARN_LOG_FMT(VIDEO, "FrameDump: Dolphin was not compiled with FFmpeg, using fallback option. " |
| 211 | "Frames will be saved as PNG images instead."); |
| 212 | dump_to_ffmpeg = false; |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | while (true) |
| 217 | { |
| 218 | m_frame_dump_start.Wait(); |
| 219 | if (!m_frame_dump_thread_running.IsSet()) |
| 220 | break; |
| 221 | |
| 222 | auto frame = m_frame_dump_data; |
| 223 | |
| 224 | // Save screenshot |
| 225 | if (m_screenshot_request.TestAndClear()) |
| 226 | { |
| 227 | std::lock_guard<std::mutex> lk(m_screenshot_lock); |
| 228 | |
| 229 | if (DumpFrameToPNG(frame, m_screenshot_name)) |
| 230 | OSD::AddMessage("Screenshot saved to " + m_screenshot_name); |
| 231 | |
| 232 | // Reset settings |
| 233 | m_screenshot_name.clear(); |
| 234 | m_screenshot_completed.Set(); |
| 235 | } |
| 236 | |
| 237 | if (Config::Get(Config::MAIN_MOVIE_DUMP_FRAMES)) |
| 238 | { |
| 239 | if (!frame_dump_started) |
| 240 | { |
| 241 | if (dump_to_ffmpeg) |
| 242 | frame_dump_started = StartFrameDumpToFFMPEG(frame); |
| 243 | else |
| 244 | frame_dump_started = StartFrameDumpToImage(frame); |
| 245 | |
| 246 | // Stop frame dumping if we fail to start. |
| 247 | if (!frame_dump_started) |
| 248 | Config::SetCurrent(Config::MAIN_MOVIE_DUMP_FRAMES, false); |
| 249 | } |
| 250 | |
| 251 | // If we failed to start frame dumping, don't write a frame. |
| 252 | if (frame_dump_started) |
| 253 | { |
| 254 | if (dump_to_ffmpeg) |
| 255 | DumpFrameToFFMPEG(frame); |
| 256 | else |
| 257 | DumpFrameToImage(frame); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | m_frame_dump_done.Set(); |
| 262 | } |
| 263 | |
| 264 | if (frame_dump_started) |
| 265 | { |
| 266 | // No additional cleanup is needed when dumping to images. |
| 267 | if (dump_to_ffmpeg) |
| 268 | StopFrameDumpToFFMPEG(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | #if defined(HAVE_FFMPEG) |
| 273 | |
| 274 | bool FrameDumper::StartFrameDumpToFFMPEG(const FrameData& frame) |
| 275 | { |
| 276 | // If dumping started at boot, the start time must be set to the boot time to maintain audio sync. |
| 277 | // TODO: Perhaps we should care about this when starting dumping in the middle of emulation too, |
| 278 | // but it's less important there since the first frame to dump usually gets delivered quickly. |
| 279 | const u64 start_ticks = frame.state.frame_number == 0 ? 0 : frame.state.ticks; |
| 280 | return m_ffmpeg_dump.Start(frame.width, frame.height, start_ticks); |
| 281 | } |
| 282 | |
| 283 | void FrameDumper::DumpFrameToFFMPEG(const FrameData& frame) |
| 284 | { |
| 285 | m_ffmpeg_dump.AddFrame(frame); |
| 286 | } |
| 287 | |
| 288 | void FrameDumper::StopFrameDumpToFFMPEG() |
| 289 | { |
| 290 | m_ffmpeg_dump.Stop(); |
| 291 | } |
| 292 | |
| 293 | #else |
| 294 | |
| 295 | bool FrameDumper::StartFrameDumpToFFMPEG(const FrameData&) |
| 296 | { |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | void FrameDumper::DumpFrameToFFMPEG(const FrameData&) |
| 301 | { |
| 302 | } |
| 303 | |
| 304 | void FrameDumper::StopFrameDumpToFFMPEG() |
| 305 | { |
| 306 | } |
| 307 | |
| 308 | #endif // defined(HAVE_FFMPEG) |
| 309 | |
| 310 | std::string FrameDumper::GetFrameDumpNextImageFileName() const |
| 311 | { |
| 312 | return fmt::format("{}framedump_{}.png", File::GetUserPath(D_DUMPFRAMES_IDX), |
| 313 | m_frame_dump_image_counter); |
| 314 | } |
| 315 | |
| 316 | bool FrameDumper::StartFrameDumpToImage(const FrameData&) |
| 317 | { |
| 318 | m_frame_dump_image_counter = 1; |
| 319 | if (!Config::Get(Config::MAIN_MOVIE_DUMP_FRAMES_SILENT)) |
| 320 | { |
| 321 | // Only check for the presence of the first image to confirm overwriting. |
| 322 | // A previous run will always have at least one image, and it's safe to assume that if the user |
| 323 | // has allowed the first image to be overwritten, this will apply any remaining images as well. |
| 324 | std::string filename = GetFrameDumpNextImageFileName(); |
| 325 | if (File::Exists(filename)) |
| 326 | { |
| 327 | if (!AskYesNoFmtT("Frame dump image(s) '{0}' already exists. Overwrite?", filename)) |
| 328 | return false; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | return true; |
| 333 | } |
| 334 | |
| 335 | void FrameDumper::DumpFrameToImage(const FrameData& frame) |
| 336 | { |
| 337 | DumpFrameToPNG(frame, GetFrameDumpNextImageFileName()); |
| 338 | m_frame_dump_image_counter++; |
| 339 | } |
| 340 | |
| 341 | void FrameDumper::SaveScreenshot(std::string filename) |
| 342 | { |
| 343 | std::lock_guard<std::mutex> lk(m_screenshot_lock); |
| 344 | m_screenshot_name = std::move(filename); |
| 345 | m_screenshot_request.Set(); |
| 346 | } |
| 347 | |
| 348 | bool FrameDumper::IsFrameDumping() const |
| 349 | { |
| 350 | if (m_screenshot_request.IsSet()) |
| 351 | return true; |
| 352 | |
| 353 | if (Config::Get(Config::MAIN_MOVIE_DUMP_FRAMES)) |
| 354 | return true; |
| 355 | |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | int FrameDumper::GetRequiredResolutionLeastCommonMultiple() const |
| 360 | { |
| 361 | if (Config::Get(Config::MAIN_MOVIE_DUMP_FRAMES)) |
| 362 | return VIDEO_ENCODER_LCM; |
| 363 | return 1; |
| 364 | } |
| 365 | |
| 366 | void FrameDumper::DoState(PointerWrap& p) |
| 367 | { |
| 368 | #ifdef HAVE_FFMPEG |
| 369 | m_ffmpeg_dump.DoState(p); |
| 370 | #endif |
| 371 | } |
| 372 | std::unique_ptr<FrameDumper> g_frame_dumper; |