2012-04-21 21:13:50 +00:00
|
|
|
/* RetroArch - A frontend for libretro.
|
2014-01-01 00:50:59 +00:00
|
|
|
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
|
2017-01-22 12:40:32 +00:00
|
|
|
* Copyright (C) 2011-2017 - Daniel De Matteis
|
2019-02-22 21:31:54 +00:00
|
|
|
* Copyright (C) 2016-2019 - Brad Parker
|
2016-03-10 22:36:46 +00:00
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
2011-05-15 14:54:43 +00:00
|
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
2012-04-21 21:13:50 +00:00
|
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
2011-05-15 14:54:43 +00:00
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
|
|
*
|
2012-04-21 21:31:57 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
2011-05-15 14:54:43 +00:00
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-01-21 21:25:38 +00:00
|
|
|
#include <stdint.h>
|
2014-09-11 22:11:08 +00:00
|
|
|
#include <stddef.h>
|
2023-01-21 21:25:38 +00:00
|
|
|
#include <string.h>
|
2011-05-15 14:54:43 +00:00
|
|
|
#include <time.h>
|
2023-01-21 21:25:38 +00:00
|
|
|
|
2014-10-21 03:05:52 +00:00
|
|
|
#include <boolean.h>
|
2023-01-21 21:25:38 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "../config.h"
|
|
|
|
#endif
|
2015-06-29 22:38:10 +00:00
|
|
|
|
|
|
|
#include <file/file_path.h>
|
|
|
|
#include <compat/strl.h>
|
2016-05-17 13:02:25 +00:00
|
|
|
#include <string/stdstring.h>
|
2016-11-08 14:35:24 +00:00
|
|
|
#include <gfx/video_frame.h>
|
2015-06-29 22:38:10 +00:00
|
|
|
|
2016-06-07 04:14:28 +00:00
|
|
|
#ifdef HAVE_RBMP
|
2015-09-19 02:40:30 +00:00
|
|
|
#include <formats/rbmp.h>
|
2016-06-07 04:14:28 +00:00
|
|
|
#endif
|
2015-09-19 02:40:30 +00:00
|
|
|
|
2016-08-02 13:34:48 +00:00
|
|
|
#ifdef HAVE_RPNG
|
2015-06-29 22:38:10 +00:00
|
|
|
#include <formats/rpng.h>
|
2015-09-19 00:52:23 +00:00
|
|
|
#define IMG_EXT "png"
|
|
|
|
#else
|
|
|
|
#define IMG_EXT "bmp"
|
|
|
|
#endif
|
2015-06-29 22:38:10 +00:00
|
|
|
|
2020-02-17 20:28:42 +00:00
|
|
|
#if defined(HAVE_GFX_WIDGETS)
|
2020-02-17 00:54:07 +00:00
|
|
|
#include "../gfx/gfx_widgets.h"
|
2019-04-08 14:39:50 +00:00
|
|
|
#endif
|
|
|
|
|
2016-07-30 23:20:00 +00:00
|
|
|
#include "../defaults.h"
|
2017-11-25 22:06:25 +00:00
|
|
|
#include "../command.h"
|
2016-09-01 03:36:52 +00:00
|
|
|
#include "../configuration.h"
|
2023-02-19 09:21:37 +00:00
|
|
|
#include "../core.h"
|
|
|
|
#include "../gfx/video_driver.h"
|
2016-09-17 17:41:16 +00:00
|
|
|
#include "../paths.h"
|
2016-05-17 12:31:33 +00:00
|
|
|
#include "../msg_hash.h"
|
2022-12-05 14:46:44 +00:00
|
|
|
#include "../runloop.h"
|
2016-05-04 14:34:13 +00:00
|
|
|
|
2016-07-30 23:20:00 +00:00
|
|
|
#include "tasks_internal.h"
|
|
|
|
|
2018-08-19 03:08:55 +00:00
|
|
|
static bool screenshot_dump_direct(screenshot_task_state_t *state)
|
2016-10-03 02:41:37 +00:00
|
|
|
{
|
2021-10-02 02:19:46 +00:00
|
|
|
struct scaler_ctx *scaler = (struct scaler_ctx*)&state->scaler;
|
|
|
|
bool ret = false;
|
2016-05-04 14:37:58 +00:00
|
|
|
|
2018-02-04 23:29:05 +00:00
|
|
|
#if defined(HAVE_RPNG)
|
2022-10-06 18:57:07 +00:00
|
|
|
if (state->flags & SS_TASK_FLAG_BGR24)
|
2021-10-02 02:19:46 +00:00
|
|
|
scaler->in_fmt = SCALER_FMT_BGR24;
|
2017-01-24 00:58:36 +00:00
|
|
|
else if (state->pixel_format_type == RETRO_PIXEL_FORMAT_XRGB8888)
|
2021-10-02 02:19:46 +00:00
|
|
|
scaler->in_fmt = SCALER_FMT_ARGB8888;
|
2017-01-24 00:58:36 +00:00
|
|
|
else
|
2021-10-02 02:19:46 +00:00
|
|
|
scaler->in_fmt = SCALER_FMT_RGB565;
|
2016-11-08 14:26:15 +00:00
|
|
|
|
2017-01-24 00:58:36 +00:00
|
|
|
video_frame_convert_to_bgr24(
|
2021-10-02 02:19:46 +00:00
|
|
|
scaler,
|
2017-01-24 00:58:36 +00:00
|
|
|
state->out_buffer,
|
2017-12-12 07:55:31 +00:00
|
|
|
(const uint8_t*)state->frame + ((int)state->height - 1)
|
2017-01-24 00:58:36 +00:00
|
|
|
* state->pitch,
|
|
|
|
state->width, state->height,
|
|
|
|
-state->pitch);
|
2015-09-27 00:32:30 +00:00
|
|
|
|
2021-10-02 02:19:46 +00:00
|
|
|
scaler_ctx_gen_reset(&state->scaler);
|
2015-09-27 00:32:30 +00:00
|
|
|
|
2016-03-20 04:12:53 +00:00
|
|
|
ret = rpng_save_image_bgr24(
|
2016-10-03 02:41:37 +00:00
|
|
|
state->filename,
|
|
|
|
state->out_buffer,
|
|
|
|
state->width,
|
|
|
|
state->height,
|
|
|
|
state->width * 3
|
2016-03-20 04:12:53 +00:00
|
|
|
);
|
2016-10-03 02:41:37 +00:00
|
|
|
|
|
|
|
free(state->out_buffer);
|
2016-06-07 04:14:28 +00:00
|
|
|
#elif defined(HAVE_RBMP)
|
2019-04-13 02:56:11 +00:00
|
|
|
{
|
|
|
|
enum rbmp_source_type bmp_type = RBMP_SOURCE_TYPE_DONT_CARE;
|
|
|
|
if (state->bgr24)
|
|
|
|
bmp_type = RBMP_SOURCE_TYPE_BGR24;
|
|
|
|
else if (state->pixel_format_type == RETRO_PIXEL_FORMAT_XRGB8888)
|
|
|
|
bmp_type = RBMP_SOURCE_TYPE_XRGB888;
|
|
|
|
|
|
|
|
ret = rbmp_save_image(state->filename,
|
|
|
|
state->frame,
|
|
|
|
state->width,
|
|
|
|
state->height,
|
|
|
|
state->pitch,
|
|
|
|
bmp_type);
|
|
|
|
}
|
2015-09-27 00:32:30 +00:00
|
|
|
#endif
|
|
|
|
|
2018-08-19 03:08:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* task_screenshot_handler:
|
|
|
|
* @task : the task being worked on
|
|
|
|
*
|
|
|
|
* Saves a screenshot to disk.
|
|
|
|
**/
|
|
|
|
static void task_screenshot_handler(retro_task_t *task)
|
|
|
|
{
|
2020-04-30 10:25:53 +00:00
|
|
|
screenshot_task_state_t *state = NULL;
|
2018-08-19 03:08:55 +00:00
|
|
|
bool ret = false;
|
|
|
|
|
2020-04-30 10:25:53 +00:00
|
|
|
if (!task)
|
|
|
|
return;
|
2018-08-19 03:08:55 +00:00
|
|
|
|
2022-07-05 16:15:12 +00:00
|
|
|
if (!(state = (screenshot_task_state_t*)task->state))
|
2020-04-30 10:25:53 +00:00
|
|
|
goto task_finished;
|
|
|
|
if (task_get_cancelled(task))
|
|
|
|
goto task_finished;
|
|
|
|
if (task_get_progress(task) == 100)
|
|
|
|
goto task_finished;
|
2018-08-19 03:08:55 +00:00
|
|
|
|
2020-04-30 10:25:53 +00:00
|
|
|
/* Take screenshot */
|
2018-08-19 03:08:55 +00:00
|
|
|
ret = screenshot_dump_direct(state);
|
|
|
|
|
2020-04-30 10:25:53 +00:00
|
|
|
/* Push screenshot to image history playlist */
|
2016-07-31 12:22:43 +00:00
|
|
|
#ifdef HAVE_IMAGEVIEWER
|
2022-10-06 18:57:07 +00:00
|
|
|
if ( ret
|
|
|
|
&& !(state->flags & SS_TASK_FLAG_SILENCE)
|
|
|
|
&& (state->flags & SS_TASK_FLAG_HISTORY_LIST_ENABLE)
|
2017-11-25 21:33:43 +00:00
|
|
|
)
|
2019-04-12 16:50:27 +00:00
|
|
|
{
|
|
|
|
struct playlist_entry entry = {0};
|
|
|
|
|
|
|
|
/* the push function reads our entry as const, so these casts are safe */
|
2019-04-13 02:56:11 +00:00
|
|
|
entry.path = state->filename;
|
|
|
|
entry.core_path = (char*)"builtin";
|
|
|
|
entry.core_name = (char*)"imageviewer";
|
2019-04-12 16:50:27 +00:00
|
|
|
|
2020-06-26 14:39:03 +00:00
|
|
|
command_playlist_push_write(g_defaults.image_history, &entry);
|
2019-04-12 16:50:27 +00:00
|
|
|
}
|
2016-10-03 02:41:37 +00:00
|
|
|
#endif
|
|
|
|
|
2016-12-29 05:50:18 +00:00
|
|
|
task_set_progress(task, 100);
|
2016-10-03 02:41:37 +00:00
|
|
|
|
2020-04-30 10:25:53 +00:00
|
|
|
/* Report any errors */
|
2016-10-03 02:41:37 +00:00
|
|
|
if (!ret)
|
|
|
|
{
|
2016-12-06 05:56:07 +00:00
|
|
|
char *msg = strdup(msg_hash_to_str(MSG_FAILED_TO_TAKE_SCREENSHOT));
|
2022-10-06 18:57:07 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, (state->flags & SS_TASK_FLAG_IS_PAUSED) ? 1 : 180, true, NULL, MESSAGE_QUEUE_ICON_DEFAULT, MESSAGE_QUEUE_CATEGORY_INFO);
|
2016-12-06 05:56:07 +00:00
|
|
|
free(msg);
|
2016-07-30 23:20:00 +00:00
|
|
|
}
|
2019-03-01 17:05:14 +00:00
|
|
|
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
2020-04-30 10:25:53 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
task_finished:
|
|
|
|
|
|
|
|
task_set_finished(task, true);
|
|
|
|
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
|
|
|
|
|
|
|
if (state && state->userbuf)
|
|
|
|
free(state->userbuf);
|
|
|
|
|
|
|
|
#if defined(HAVE_GFX_WIDGETS)
|
|
|
|
/* If display widgets are enabled, state is freed
|
|
|
|
in the callback after the notification
|
|
|
|
is displayed */
|
2022-10-06 18:57:07 +00:00
|
|
|
if (state && !(state->flags & SS_TASK_FLAG_WIDGETS_READY))
|
2020-04-30 10:25:53 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
free(state);
|
|
|
|
/* Must explicitly set task->state to NULL here,
|
|
|
|
* to avoid potential heap-use-after-free errors */
|
|
|
|
state = NULL;
|
|
|
|
task->state = NULL;
|
|
|
|
}
|
2016-10-03 02:41:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 20:28:42 +00:00
|
|
|
#if defined(HAVE_GFX_WIDGETS)
|
2022-07-19 08:01:33 +00:00
|
|
|
static void task_screenshot_callback(retro_task_t *task,
|
2019-05-01 13:46:57 +00:00
|
|
|
void *task_data,
|
2022-07-19 08:01:33 +00:00
|
|
|
void *user_data, const char *error)
|
|
|
|
{
|
|
|
|
screenshot_task_state_t *state = NULL;
|
|
|
|
|
|
|
|
if (!task)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(state = (screenshot_task_state_t*)task->state))
|
|
|
|
return;
|
|
|
|
|
2022-10-06 18:57:07 +00:00
|
|
|
if ( !(state->flags & SS_TASK_FLAG_SILENCE)
|
|
|
|
&& (state->flags & SS_TASK_FLAG_WIDGETS_READY))
|
2022-07-19 08:01:33 +00:00
|
|
|
gfx_widget_screenshot_taken(dispwidget_get_ptr(),
|
|
|
|
state->shotname, state->filename);
|
|
|
|
|
|
|
|
free(state);
|
|
|
|
/* Must explicitly set task->state to NULL here,
|
|
|
|
* to avoid potential heap-use-after-free errors */
|
|
|
|
state = NULL;
|
|
|
|
task->state = NULL;
|
|
|
|
}
|
2019-05-01 13:46:57 +00:00
|
|
|
#endif
|
|
|
|
|
2016-10-03 02:41:37 +00:00
|
|
|
/* Take frame bottom-up. */
|
|
|
|
static bool screenshot_dump(
|
2019-05-22 03:54:35 +00:00
|
|
|
const char *screenshot_dir,
|
2016-10-03 02:41:37 +00:00
|
|
|
const char *name_base,
|
|
|
|
const void *frame,
|
|
|
|
unsigned width,
|
|
|
|
unsigned height,
|
2019-05-22 03:54:35 +00:00
|
|
|
int pitch,
|
|
|
|
bool bgr24,
|
|
|
|
void *userbuf,
|
|
|
|
bool savestate,
|
2022-10-30 17:41:41 +00:00
|
|
|
uint32_t runloop_flags,
|
2018-08-19 03:08:55 +00:00
|
|
|
bool fullpath,
|
2019-05-22 03:54:35 +00:00
|
|
|
bool use_thread,
|
|
|
|
unsigned pixel_format_type)
|
2016-10-03 02:41:37 +00:00
|
|
|
{
|
2019-04-13 02:56:11 +00:00
|
|
|
struct retro_system_info system_info;
|
2017-01-24 00:58:36 +00:00
|
|
|
uint8_t *buf = NULL;
|
2016-10-03 02:41:37 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2022-10-06 18:57:07 +00:00
|
|
|
bool history_list_enable = settings->bools.history_list_enable;
|
2020-06-07 14:15:35 +00:00
|
|
|
screenshot_task_state_t *state = (screenshot_task_state_t*)
|
2020-08-13 02:24:54 +00:00
|
|
|
calloc(1, sizeof(*state));
|
2017-01-03 03:30:02 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
/* If fullpath is true, name_base already contains a
|
|
|
|
* static path + filename to save the screenshot to. */
|
2018-08-19 03:08:55 +00:00
|
|
|
if (fullpath)
|
|
|
|
strlcpy(state->filename, name_base, sizeof(state->filename));
|
2016-10-03 02:41:37 +00:00
|
|
|
|
2022-10-30 17:41:41 +00:00
|
|
|
if (runloop_flags & RUNLOOP_FLAG_IDLE)
|
2022-10-06 18:57:07 +00:00
|
|
|
state->flags |= SS_TASK_FLAG_IS_IDLE;
|
2022-10-30 17:41:41 +00:00
|
|
|
if (runloop_flags & RUNLOOP_FLAG_PAUSED)
|
2022-10-06 18:57:07 +00:00
|
|
|
state->flags |= SS_TASK_FLAG_IS_PAUSED;
|
|
|
|
if (bgr24)
|
|
|
|
state->flags |= SS_TASK_FLAG_BGR24;
|
2020-01-04 11:04:00 +00:00
|
|
|
state->height = height;
|
|
|
|
state->width = width;
|
|
|
|
state->pitch = pitch;
|
|
|
|
state->frame = frame;
|
|
|
|
state->userbuf = userbuf;
|
2020-02-17 20:28:42 +00:00
|
|
|
#if defined(HAVE_GFX_WIDGETS)
|
2022-10-06 18:57:07 +00:00
|
|
|
if (gfx_widgets_ready())
|
|
|
|
state->flags |= SS_TASK_FLAG_WIDGETS_READY;
|
2019-08-15 11:12:15 +00:00
|
|
|
#endif
|
2022-10-06 18:57:07 +00:00
|
|
|
if (savestate)
|
|
|
|
state->flags |= SS_TASK_FLAG_SILENCE;
|
|
|
|
|
|
|
|
if (history_list_enable)
|
|
|
|
state->flags |= SS_TASK_FLAG_HISTORY_LIST_ENABLE;
|
2020-01-04 11:04:00 +00:00
|
|
|
state->pixel_format_type = pixel_format_type;
|
2016-10-03 02:41:37 +00:00
|
|
|
|
2018-08-19 03:08:55 +00:00
|
|
|
if (!fullpath)
|
2016-12-01 22:24:16 +00:00
|
|
|
{
|
2018-08-19 03:08:55 +00:00
|
|
|
if (savestate)
|
2019-09-18 00:31:02 +00:00
|
|
|
{
|
2022-08-04 01:44:19 +00:00
|
|
|
size_t len = strlcpy(state->filename,
|
2019-09-18 00:31:02 +00:00
|
|
|
name_base, sizeof(state->filename));
|
2022-08-04 01:44:19 +00:00
|
|
|
state->filename[len ] = '.';
|
|
|
|
state->filename[len+1] = 'p';
|
|
|
|
state->filename[len+2] = 'n';
|
|
|
|
state->filename[len+3] = 'g';
|
|
|
|
state->filename[len+4] = '\0';
|
2019-09-18 00:31:02 +00:00
|
|
|
}
|
2016-12-01 22:24:16 +00:00
|
|
|
else
|
2018-08-19 03:08:55 +00:00
|
|
|
{
|
2020-08-13 02:24:54 +00:00
|
|
|
char new_screenshot_dir[PATH_MAX_LENGTH];
|
|
|
|
|
|
|
|
if (!string_is_empty(screenshot_dir))
|
|
|
|
{
|
|
|
|
const char *content_dir = path_get(RARCH_PATH_BASENAME);
|
|
|
|
|
|
|
|
/* Append content directory name to screenshot
|
|
|
|
* path, if required */
|
|
|
|
if (settings->bools.sort_screenshots_by_content_enable &&
|
|
|
|
!string_is_empty(content_dir))
|
|
|
|
{
|
|
|
|
char content_dir_name[PATH_MAX_LENGTH];
|
|
|
|
fill_pathname_parent_dir_name(content_dir_name,
|
|
|
|
content_dir, sizeof(content_dir_name));
|
2022-08-04 12:18:11 +00:00
|
|
|
fill_pathname_join_special(
|
2020-08-13 02:24:54 +00:00
|
|
|
new_screenshot_dir,
|
|
|
|
screenshot_dir,
|
|
|
|
content_dir_name,
|
|
|
|
sizeof(new_screenshot_dir));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
strlcpy(new_screenshot_dir, screenshot_dir,
|
|
|
|
sizeof(new_screenshot_dir));
|
|
|
|
}
|
|
|
|
|
2018-08-19 03:08:55 +00:00
|
|
|
if (settings->bools.auto_screenshot_filename)
|
2018-12-27 20:57:55 +00:00
|
|
|
{
|
2019-01-16 04:33:48 +00:00
|
|
|
const char *screenshot_name = NULL;
|
|
|
|
|
2018-12-27 20:57:55 +00:00
|
|
|
if (path_is_empty(RARCH_PATH_CONTENT))
|
2019-01-16 04:33:48 +00:00
|
|
|
{
|
2019-04-13 02:56:11 +00:00
|
|
|
if (!core_get_system_info(&system_info))
|
2020-06-07 14:15:35 +00:00
|
|
|
{
|
|
|
|
free(state);
|
2019-04-13 02:56:11 +00:00
|
|
|
return false;
|
2020-06-07 14:15:35 +00:00
|
|
|
}
|
2019-04-13 02:56:11 +00:00
|
|
|
|
2019-01-16 04:33:48 +00:00
|
|
|
if (string_is_empty(system_info.library_name))
|
|
|
|
screenshot_name = "RetroArch";
|
|
|
|
else
|
|
|
|
screenshot_name = system_info.library_name;
|
|
|
|
}
|
2018-12-27 20:57:55 +00:00
|
|
|
else
|
2022-07-12 17:14:12 +00:00
|
|
|
screenshot_name = path_basename_nocompression(name_base);
|
2019-01-16 04:33:48 +00:00
|
|
|
|
2018-11-22 14:45:52 +00:00
|
|
|
fill_str_dated_filename(state->shotname, screenshot_name,
|
|
|
|
IMG_EXT, sizeof(state->shotname));
|
2018-12-27 20:57:55 +00:00
|
|
|
}
|
2018-08-19 03:08:55 +00:00
|
|
|
else
|
2019-09-18 00:31:02 +00:00
|
|
|
{
|
2022-08-04 01:44:19 +00:00
|
|
|
size_t len = strlcpy(
|
|
|
|
state->shotname, path_basename_nocompression(name_base),
|
2019-09-18 00:31:02 +00:00
|
|
|
sizeof(state->shotname));
|
2022-08-04 01:44:19 +00:00
|
|
|
state->shotname[len ] = '.';
|
|
|
|
state->shotname[len+1] = 'p';
|
|
|
|
state->shotname[len+2] = 'n';
|
|
|
|
state->shotname[len+3] = 'g';
|
|
|
|
state->shotname[len+4] = '\0';
|
2019-09-18 00:31:02 +00:00
|
|
|
}
|
2018-08-19 03:08:55 +00:00
|
|
|
|
2020-08-13 02:24:54 +00:00
|
|
|
if ( string_is_empty(new_screenshot_dir) ||
|
2019-05-22 05:40:23 +00:00
|
|
|
settings->bools.screenshots_in_content_dir)
|
2020-08-13 02:24:54 +00:00
|
|
|
fill_pathname_basedir(new_screenshot_dir, name_base,
|
|
|
|
sizeof(new_screenshot_dir));
|
2022-07-19 18:43:12 +00:00
|
|
|
|
2022-08-04 12:18:11 +00:00
|
|
|
fill_pathname_join_special(state->filename, new_screenshot_dir,
|
2022-07-19 18:43:12 +00:00
|
|
|
state->shotname, sizeof(state->filename));
|
2020-08-13 02:24:54 +00:00
|
|
|
|
|
|
|
/* Create screenshot directory, if required */
|
|
|
|
if (!path_is_directory(new_screenshot_dir))
|
|
|
|
if (!path_mkdir(new_screenshot_dir))
|
|
|
|
return false;
|
2018-08-19 03:08:55 +00:00
|
|
|
}
|
2016-12-01 22:24:16 +00:00
|
|
|
}
|
2016-10-03 02:41:37 +00:00
|
|
|
|
2018-02-04 23:29:05 +00:00
|
|
|
#if defined(HAVE_RPNG)
|
2022-07-19 08:01:33 +00:00
|
|
|
if (!(buf = (uint8_t*)malloc(width * height * 3)))
|
2016-10-04 09:02:01 +00:00
|
|
|
{
|
2016-10-06 03:17:22 +00:00
|
|
|
free(state);
|
2016-10-03 02:41:37 +00:00
|
|
|
return false;
|
2016-10-04 09:02:01 +00:00
|
|
|
}
|
2022-10-06 18:57:07 +00:00
|
|
|
state->out_buffer = buf;
|
2016-07-31 12:22:43 +00:00
|
|
|
#endif
|
2016-07-30 23:20:00 +00:00
|
|
|
|
2018-08-19 03:08:55 +00:00
|
|
|
if (use_thread)
|
|
|
|
{
|
2019-07-08 19:32:12 +00:00
|
|
|
retro_task_t *task = task_init();
|
2019-05-22 03:23:19 +00:00
|
|
|
|
2022-10-06 18:57:07 +00:00
|
|
|
task->type = TASK_TYPE_BLOCKING;
|
|
|
|
task->state = state;
|
|
|
|
task->handler = task_screenshot_handler;
|
|
|
|
task->mute = savestate;
|
2020-02-17 20:28:42 +00:00
|
|
|
#if defined(HAVE_GFX_WIDGETS)
|
2020-04-30 10:25:53 +00:00
|
|
|
/* This callback is only required when
|
|
|
|
* widgets are enabled */
|
2022-10-06 18:57:07 +00:00
|
|
|
if (state->flags & SS_TASK_FLAG_WIDGETS_READY)
|
|
|
|
task->callback = task_screenshot_callback;
|
|
|
|
|
|
|
|
if ((state->flags & SS_TASK_FLAG_WIDGETS_READY) && !savestate)
|
2019-04-08 14:39:50 +00:00
|
|
|
task_free_title(task);
|
|
|
|
else
|
|
|
|
#endif
|
2019-05-06 19:10:58 +00:00
|
|
|
{
|
2020-07-19 21:42:16 +00:00
|
|
|
if (!savestate & settings->bools.notification_show_screenshot)
|
2019-04-13 02:56:11 +00:00
|
|
|
task->title = strdup(msg_hash_to_str(MSG_TAKING_SCREENSHOT));
|
2019-05-06 19:10:58 +00:00
|
|
|
}
|
2017-01-03 03:37:52 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
if (task_queue_push(task))
|
|
|
|
return true;
|
2019-03-01 17:05:14 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
/* There is already a blocking task going on */
|
|
|
|
if (task->title)
|
|
|
|
task_free_title(task);
|
2019-03-01 17:05:14 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
free(task);
|
2019-03-01 17:05:14 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
if (state->out_buffer)
|
|
|
|
free(state->out_buffer);
|
2019-03-01 17:05:14 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
free(state);
|
|
|
|
|
|
|
|
return false;
|
2019-03-01 17:05:14 +00:00
|
|
|
}
|
2016-10-03 02:41:37 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
return screenshot_dump_direct(state);
|
2015-09-27 00:32:30 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 03:54:35 +00:00
|
|
|
static bool take_screenshot_viewport(
|
|
|
|
const char *screenshot_dir,
|
|
|
|
const char *name_base,
|
|
|
|
bool savestate,
|
2022-10-30 17:41:41 +00:00
|
|
|
uint32_t runloop_flags,
|
2019-05-22 03:54:35 +00:00
|
|
|
bool fullpath,
|
|
|
|
bool use_thread,
|
|
|
|
unsigned pixel_format_type)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2016-12-19 18:04:52 +00:00
|
|
|
struct video_viewport vp;
|
2023-05-30 10:19:53 +00:00
|
|
|
video_driver_state_t *video_st = video_state_get_ptr();
|
2015-06-12 15:00:37 +00:00
|
|
|
uint8_t *buffer = NULL;
|
2016-10-09 08:43:16 +00:00
|
|
|
|
2016-12-19 18:04:52 +00:00
|
|
|
vp.x = 0;
|
|
|
|
vp.y = 0;
|
|
|
|
vp.width = 0;
|
|
|
|
vp.height = 0;
|
|
|
|
vp.full_width = 0;
|
|
|
|
vp.full_height = 0;
|
|
|
|
|
2016-05-08 12:00:51 +00:00
|
|
|
video_driver_get_viewport_info(&vp);
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
if (!vp.width || !vp.height)
|
|
|
|
return false;
|
2022-07-05 16:15:12 +00:00
|
|
|
if (!(buffer = (uint8_t*)malloc(vp.width * vp.height * 3)))
|
2015-02-10 19:58:33 +00:00
|
|
|
return false;
|
|
|
|
|
2023-05-30 10:19:53 +00:00
|
|
|
if (!( video_st->current_video->read_viewport
|
|
|
|
&& video_st->current_video->read_viewport(
|
|
|
|
video_st->data, buffer, runloop_flags & RUNLOOP_FLAG_IDLE)))
|
2022-07-05 16:15:12 +00:00
|
|
|
goto error;
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
/* Data read from viewport is in bottom-up order, suitable for BMP. */
|
2019-05-22 03:54:35 +00:00
|
|
|
if (!screenshot_dump(screenshot_dir,
|
|
|
|
name_base,
|
2016-12-04 18:08:24 +00:00
|
|
|
buffer, vp.width, vp.height,
|
2019-08-15 11:12:15 +00:00
|
|
|
vp.width * 3, true, buffer,
|
2022-10-30 17:41:41 +00:00
|
|
|
savestate, runloop_flags, fullpath, use_thread,
|
2019-05-22 03:54:35 +00:00
|
|
|
pixel_format_type))
|
2022-07-05 16:15:12 +00:00
|
|
|
goto error;
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2016-10-03 02:41:37 +00:00
|
|
|
return true;
|
2022-07-05 16:15:12 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
free(buffer);
|
|
|
|
return false;
|
2015-02-10 19:58:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 23:09:38 +00:00
|
|
|
static bool take_screenshot_raw(
|
|
|
|
video_driver_state_t *video_st,
|
|
|
|
const char *screenshot_dir,
|
2019-05-22 03:54:35 +00:00
|
|
|
const char *name_base, void *userbuf,
|
2022-10-30 17:41:41 +00:00
|
|
|
bool savestate, uint32_t runloop_flags, bool fullpath, bool use_thread,
|
2019-05-22 03:54:35 +00:00
|
|
|
unsigned pixel_format_type)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2023-05-30 23:09:38 +00:00
|
|
|
const void *data = video_st->frame_cache_data;
|
|
|
|
unsigned width = video_st->frame_cache_width;
|
|
|
|
unsigned height = video_st->frame_cache_height;
|
|
|
|
size_t pitch = video_st->frame_cache_pitch;
|
2015-02-10 19:58:33 +00:00
|
|
|
/* Negative pitch is needed as screenshot takes bottom-up,
|
|
|
|
* but we use top-down.
|
|
|
|
*/
|
2022-07-05 16:15:12 +00:00
|
|
|
return screenshot_dump(screenshot_dir,
|
2019-05-22 03:54:35 +00:00
|
|
|
name_base,
|
|
|
|
(const uint8_t*)data + (height - 1) * pitch,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
(int)(-pitch),
|
|
|
|
false,
|
|
|
|
userbuf,
|
|
|
|
savestate,
|
2022-10-30 17:41:41 +00:00
|
|
|
runloop_flags,
|
2019-05-22 03:54:35 +00:00
|
|
|
fullpath,
|
|
|
|
use_thread,
|
2022-07-05 16:15:12 +00:00
|
|
|
pixel_format_type);
|
2015-02-10 19:58:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 03:54:35 +00:00
|
|
|
static bool take_screenshot_choice(
|
2023-05-30 20:10:03 +00:00
|
|
|
video_driver_state_t *video_st,
|
2019-05-22 03:54:35 +00:00
|
|
|
const char *screenshot_dir,
|
|
|
|
const char *name_base,
|
|
|
|
bool savestate,
|
2022-10-30 17:41:41 +00:00
|
|
|
uint32_t runloop_flags,
|
2019-05-22 03:54:35 +00:00
|
|
|
bool has_valid_framebuffer,
|
|
|
|
bool fullpath,
|
|
|
|
bool use_thread,
|
|
|
|
bool supports_viewport_read,
|
|
|
|
bool supports_read_frame_raw,
|
2019-05-22 05:07:33 +00:00
|
|
|
unsigned pixel_format_type
|
|
|
|
)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2019-05-22 03:54:35 +00:00
|
|
|
if (supports_viewport_read)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
|
|
|
/* Avoid taking screenshot of GUI overlays. */
|
2023-05-30 23:09:38 +00:00
|
|
|
if (video_st->poke && video_st->poke->set_texture_enable)
|
|
|
|
video_st->poke->set_texture_enable(video_st->data,
|
|
|
|
false, false);
|
2022-10-30 17:41:41 +00:00
|
|
|
if (!(runloop_flags & RUNLOOP_FLAG_IDLE))
|
2016-10-24 22:42:14 +00:00
|
|
|
video_driver_cached_frame();
|
2019-05-22 03:54:35 +00:00
|
|
|
return take_screenshot_viewport(screenshot_dir,
|
2022-10-30 17:41:41 +00:00
|
|
|
name_base, savestate, runloop_flags, fullpath, use_thread,
|
2019-05-22 03:54:35 +00:00
|
|
|
pixel_format_type);
|
2015-02-10 19:58:33 +00:00
|
|
|
}
|
2016-02-03 15:13:37 +00:00
|
|
|
|
2017-05-13 17:37:16 +00:00
|
|
|
if (!has_valid_framebuffer)
|
2023-05-30 23:09:38 +00:00
|
|
|
return take_screenshot_raw(video_st, screenshot_dir,
|
2022-10-30 17:41:41 +00:00
|
|
|
name_base, NULL, savestate, runloop_flags, fullpath, use_thread,
|
2019-05-22 03:54:35 +00:00
|
|
|
pixel_format_type);
|
2016-02-03 15:13:37 +00:00
|
|
|
|
2022-07-05 16:15:12 +00:00
|
|
|
if (supports_read_frame_raw)
|
|
|
|
{
|
2023-05-30 23:09:38 +00:00
|
|
|
const void *old_data = video_st->frame_cache_data;
|
|
|
|
unsigned old_width = video_st->frame_cache_width;
|
|
|
|
unsigned old_height = video_st->frame_cache_height;
|
|
|
|
size_t old_pitch = video_st->frame_cache_pitch;
|
|
|
|
void *frame_data = video_driver_read_frame_raw(
|
2022-07-05 16:15:12 +00:00
|
|
|
&old_width, &old_height, &old_pitch);
|
2015-03-16 12:41:26 +00:00
|
|
|
|
2023-05-30 20:10:03 +00:00
|
|
|
video_st->frame_cache_data = old_data;
|
|
|
|
video_st->frame_cache_width = old_width;
|
|
|
|
video_st->frame_cache_height = old_height;
|
|
|
|
video_st->frame_cache_pitch = old_pitch;
|
2016-01-30 02:08:33 +00:00
|
|
|
|
2022-07-05 16:15:12 +00:00
|
|
|
if (frame_data)
|
|
|
|
{
|
2023-05-30 20:10:03 +00:00
|
|
|
video_st->frame_cache_data = frame_data;
|
2023-05-30 23:09:38 +00:00
|
|
|
return take_screenshot_raw(video_st, screenshot_dir,
|
2022-10-30 17:41:41 +00:00
|
|
|
name_base, frame_data, savestate, runloop_flags, fullpath, use_thread,
|
2022-07-05 16:15:12 +00:00
|
|
|
pixel_format_type);
|
|
|
|
}
|
2015-03-16 14:32:18 +00:00
|
|
|
}
|
2015-03-16 12:41:26 +00:00
|
|
|
|
2019-04-13 02:56:11 +00:00
|
|
|
return false;
|
2016-01-30 02:08:33 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 03:54:35 +00:00
|
|
|
bool take_screenshot(
|
|
|
|
const char *screenshot_dir,
|
|
|
|
const char *name_base,
|
2022-10-03 13:13:01 +00:00
|
|
|
bool savestate, bool has_valid_framebuffer,
|
2019-04-13 02:56:11 +00:00
|
|
|
bool fullpath, bool use_thread)
|
2016-11-28 00:57:48 +00:00
|
|
|
{
|
2023-05-30 10:19:53 +00:00
|
|
|
bool ret = false;
|
|
|
|
uint32_t runloop_flags = runloop_get_flags();
|
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
video_driver_state_t *video_st = video_state_get_ptr();
|
|
|
|
bool video_gpu_screenshot = settings->bools.video_gpu_screenshot;
|
|
|
|
bool supports_viewport_read = video_st->current_video->read_viewport
|
|
|
|
&& (video_st->current_video->viewport_info);
|
2023-05-30 10:26:52 +00:00
|
|
|
bool prefer_viewport_read = false;
|
|
|
|
if (supports_viewport_read)
|
|
|
|
{
|
|
|
|
/* Use VP read screenshots if it's a HW context core
|
|
|
|
* and read_frame_raw is not implemented */
|
|
|
|
if ( video_driver_is_hw_context()
|
|
|
|
&& !video_st->current_video->read_frame_raw)
|
|
|
|
prefer_viewport_read = true;
|
|
|
|
/* Avoid GPU screenshots with savestates */
|
|
|
|
if (video_gpu_screenshot && !savestate)
|
|
|
|
prefer_viewport_read = true;
|
|
|
|
}
|
2017-01-23 14:13:31 +00:00
|
|
|
|
2019-05-22 03:54:35 +00:00
|
|
|
/* No way to infer screenshot directory. */
|
|
|
|
if ( string_is_empty(screenshot_dir)
|
|
|
|
&& string_is_empty(name_base))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ret = take_screenshot_choice(
|
2023-05-30 20:10:03 +00:00
|
|
|
video_st,
|
2019-05-22 03:54:35 +00:00
|
|
|
screenshot_dir,
|
2022-10-30 17:41:41 +00:00
|
|
|
name_base, savestate, runloop_flags,
|
2019-05-22 03:54:35 +00:00
|
|
|
has_valid_framebuffer, fullpath, use_thread,
|
2022-10-03 13:13:01 +00:00
|
|
|
prefer_viewport_read,
|
2023-05-30 10:19:53 +00:00
|
|
|
(video_st->current_video->read_frame_raw),
|
|
|
|
video_st->pix_fmt
|
2019-05-22 03:54:35 +00:00
|
|
|
);
|
2016-11-28 00:57:48 +00:00
|
|
|
|
2022-10-30 17:41:41 +00:00
|
|
|
if ( (runloop_flags & RUNLOOP_FLAG_PAUSED)
|
|
|
|
&& (!(runloop_flags & RUNLOOP_FLAG_IDLE)))
|
2016-11-28 00:57:48 +00:00
|
|
|
video_driver_cached_frame();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|