|
26 | 26 | #include "Common/File/FileUtil.h" |
27 | 27 | #include "Common/File/Path.h" |
28 | 28 | #include "Common/Log.h" |
| 29 | +#include "Common/System/System.h" |
29 | 30 | #include "Common/System/Display.h" |
| 31 | +#include "Common/System/NativeApp.h" |
| 32 | +#include "Common/Thread/Promise.h" |
30 | 33 | #include "Core/Screenshot.h" |
31 | 34 | #include "Core/System.h" |
32 | 35 | #include "GPU/Common/GPUDebugInterface.h" |
@@ -328,56 +331,54 @@ static GPUDebugBuffer ApplyRotation(const GPUDebugBuffer &buf, DisplayRotation r |
328 | 331 | return rotated; |
329 | 332 | } |
330 | 333 |
|
331 | | -bool TakeGameScreenshot(Draw::DrawContext *draw, const Path &filename, ScreenshotFormat fmt, ScreenshotType type, int *width, int *height, int maxRes) { |
| 334 | +ScreenshotResult TakeGameScreenshot(Draw::DrawContext *draw, const Path &filename, ScreenshotFormat fmt, ScreenshotType type, int maxRes, std::function<void(bool success)> callback) { |
332 | 335 | GPUDebugBuffer buf; |
333 | | - bool success = false; |
334 | 336 | u32 w = (u32)-1; |
335 | 337 | u32 h = (u32)-1; |
336 | 338 |
|
337 | 339 | if (type == SCREENSHOT_DISPLAY || type == SCREENSHOT_RENDER) { |
338 | 340 | if (!gpuDebug) { |
339 | 341 | ERROR_LOG(Log::System, "Can't take screenshots when GPU not running"); |
340 | | - return false; |
| 342 | + return ScreenshotResult::ScreenshotNotPossible; |
| 343 | + } |
| 344 | + if (!gpuDebug->GetCurrentFramebuffer(buf, type == SCREENSHOT_RENDER ? GPU_DBG_FRAMEBUF_RENDER : GPU_DBG_FRAMEBUF_DISPLAY, maxRes)) { |
| 345 | + return ScreenshotResult::ScreenshotNotPossible; |
341 | 346 | } |
342 | | - success = gpuDebug->GetCurrentFramebuffer(buf, type == SCREENSHOT_RENDER ? GPU_DBG_FRAMEBUF_RENDER : GPU_DBG_FRAMEBUF_DISPLAY, maxRes); |
343 | 347 | w = maxRes > 0 ? 480 * maxRes : PSP_CoreParameter().renderWidth; |
344 | 348 | h = maxRes > 0 ? 272 * maxRes : PSP_CoreParameter().renderHeight; |
345 | 349 | } else if (g_display.rotation != DisplayRotation::ROTATE_0) { |
346 | 350 | _dbg_assert_(draw); |
347 | 351 | GPUDebugBuffer temp; |
348 | | - success = ::GetOutputFramebuffer(draw, temp); |
349 | | - if (success) { |
350 | | - buf = ApplyRotation(temp, g_display.rotation); |
| 352 | + if (!::GetOutputFramebuffer(draw, temp)) { |
| 353 | + return ScreenshotResult::ScreenshotNotPossible; |
351 | 354 | } |
| 355 | + buf = ApplyRotation(temp, g_display.rotation); |
352 | 356 | } else { |
353 | 357 | _dbg_assert_(draw); |
354 | | - success = ::GetOutputFramebuffer(draw, buf); |
355 | | - } |
356 | | - |
357 | | - if (!success) { |
358 | | - return false; |
359 | | - } |
360 | | - |
361 | | - if (success) { |
362 | | - u8 *flipbuffer = nullptr; |
363 | | - const u8 *buffer = ConvertBufferToScreenshot(buf, false, flipbuffer, w, h); |
364 | | - success = buffer != nullptr; |
365 | | - if (success) { |
366 | | - if (width) |
367 | | - *width = w; |
368 | | - if (height) |
369 | | - *height = h; |
370 | | - |
371 | | - success = Save888RGBScreenshot(filename, fmt, buffer, w, h); |
| 358 | + if (!GetOutputFramebuffer(draw, buf)) { |
| 359 | + return ScreenshotResult::ScreenshotNotPossible; |
372 | 360 | } |
373 | | - delete[] flipbuffer; |
374 | 361 | } |
375 | 362 |
|
376 | | - if (!success) { |
377 | | - ERROR_LOG(Log::IO, "Failed to write screenshot."); |
| 363 | + if (callback) { |
| 364 | + g_threadManager.EnqueueTask(new IndependentTask(TaskType::CPU_COMPUTE, TaskPriority::LOW, |
| 365 | + [buf = std::move(buf), callback = std::move(callback), filename, fmt, w, h]() { |
| 366 | + u8 *flipbuffer = nullptr; |
| 367 | + u32 width = w, height = h; |
| 368 | + const u8 *buffer = ConvertBufferToScreenshot(buf, false, flipbuffer, width, height); |
| 369 | + bool success = Save888RGBScreenshot(filename, fmt, buffer, width, height); |
| 370 | + delete[] flipbuffer; |
| 371 | + System_RunOnMainThread([success, callback = std::move(callback)]() { |
| 372 | + callback(success); |
| 373 | + }); |
| 374 | + })); |
| 375 | + return ScreenshotResult::DelayedResult; |
378 | 376 | } |
379 | | - |
380 | | - return success; |
| 377 | + u8 *flipbuffer = nullptr; |
| 378 | + const u8 *buffer = ConvertBufferToScreenshot(buf, false, flipbuffer, w, h); |
| 379 | + bool success = Save888RGBScreenshot(filename, fmt, buffer, w, h); |
| 380 | + delete[] flipbuffer; |
| 381 | + return success ? ScreenshotResult::Success : ScreenshotResult::FailedToWriteFile; |
381 | 382 | } |
382 | 383 |
|
383 | 384 | bool Save888RGBScreenshot(const Path &filename, ScreenshotFormat fmt, const u8 *bufferRGB888, int w, int h) { |
|
0 commit comments