RetroArch/tasks/task_screenshot.c

303 lines
7.8 KiB
C
Raw Normal View History

2012-04-21 23:13:50 +02:00
/* RetroArch - A frontend for libretro.
2014-01-01 01:50:59 +01:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2016-01-10 04:06:50 +01:00
* Copyright (C) 2011-2016 - Daniel De Matteis
*
2012-04-21 23:13:50 +02:00
* RetroArch is free software: you can redistribute it and/or modify it under the terms
2011-05-15 16:54:43 +02: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 23:13:50 +02:00
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
2011-05-15 16:54:43 +02: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 23:31:57 +02:00
* You should have received a copy of the GNU General Public License along with RetroArch.
2011-05-15 16:54:43 +02:00
* If not, see <http://www.gnu.org/licenses/>.
*/
/* TODO/FIXME - turn this into actual task */
2016-09-19 03:41:55 +02:00
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#ifdef _XBOX1
#include <xtl.h>
#include <xgraphics.h>
#endif
2011-05-15 16:54:43 +02:00
#include <stdio.h>
#include <stddef.h>
2011-05-15 16:54:43 +02:00
#include <time.h>
#include <boolean.h>
2011-05-15 16:54:43 +02:00
#include <stdint.h>
#include <string.h>
#include <file/file_path.h>
#include <compat/strl.h>
2016-05-17 15:02:25 +02:00
#include <string/stdstring.h>
#include <gfx/scaler/scaler.h>
2016-06-07 06:14:28 +02:00
#ifdef HAVE_RBMP
2015-09-19 04:40:30 +02:00
#include <formats/rbmp.h>
2016-06-07 06:14:28 +02:00
#endif
2015-09-19 04:40:30 +02:00
#ifdef HAVE_RPNG
#include <formats/rpng.h>
#define IMG_EXT "png"
#else
#define IMG_EXT "bmp"
#endif
#include "../defaults.h"
2016-09-01 05:36:52 +02:00
#include "../configuration.h"
#include "../runloop.h"
2016-09-17 19:41:16 +02:00
#include "../paths.h"
#include "../msg_hash.h"
#include "../gfx/video_driver.h"
#include "../gfx/video_frame.h"
2011-05-15 16:54:43 +02:00
#include "tasks_internal.h"
2015-09-27 02:32:30 +02:00
/* Take frame bottom-up. */
static bool screenshot_dump(
const char *name_base,
const char *folder,
const void *frame,
unsigned width,
unsigned height,
int pitch, bool bgr24)
2015-09-27 02:32:30 +02:00
{
char filename[PATH_MAX_LENGTH] = {0};
char shotname[256] = {0};
2016-05-15 10:19:07 +02:00
bool ret = false;
2016-05-04 16:37:58 +02:00
settings_t *settings = config_get_ptr();
#if defined(HAVE_RPNG)
2015-09-27 02:32:30 +02:00
uint8_t *out_buffer = NULL;
2016-01-25 01:01:57 +01:00
struct scaler_ctx scaler = {0};
#endif
2015-09-27 02:32:30 +02:00
if (settings->auto_screenshot_filename)
{
fill_str_dated_filename(shotname, path_basename(name_base),
IMG_EXT, sizeof(shotname));
}
else
{
snprintf(shotname, sizeof(shotname),"%s.png", path_basename(name_base));
}
fill_pathname_join(filename, folder, shotname, sizeof(filename));
2015-09-27 02:32:30 +02:00
#ifdef _XBOX1
2015-11-23 18:01:46 +01:00
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(true);
2015-09-27 02:32:30 +02:00
D3DSurface *surf = NULL;
2016-05-04 16:37:58 +02:00
2015-09-27 02:32:30 +02:00
d3d->dev->GetBackBuffer(-1, D3DBACKBUFFER_TYPE_MONO, &surf);
2016-05-15 10:19:07 +02:00
if (XGWriteSurfaceToFile(surf, filename) == S_OK)
2015-09-27 02:32:30 +02:00
ret = true;
2016-05-15 10:19:07 +02:00
surf->Release();
#elif defined(HAVE_RPNG)
2015-09-27 02:32:30 +02:00
out_buffer = (uint8_t*)malloc(width * height * 3);
if (!out_buffer)
return false;
video_frame_convert_to_bgr24(
2016-05-04 04:34:05 +02:00
&scaler,
out_buffer,
(const uint8_t*)frame + ((int)height - 1) * pitch,
width, height,
-pitch,
bgr24);
2015-09-27 02:32:30 +02:00
scaler_ctx_gen_reset(&scaler);
2016-03-20 05:12:53 +01:00
ret = rpng_save_image_bgr24(
filename,
out_buffer,
width,
height,
width * 3
);
2015-09-27 02:32:30 +02:00
free(out_buffer);
2016-06-07 06:14:28 +02:00
#elif defined(HAVE_RBMP)
2016-03-20 05:12:53 +01:00
enum rbmp_source_type bmp_type = RBMP_SOURCE_TYPE_DONT_CARE;
if (bgr24)
bmp_type = RBMP_SOURCE_TYPE_BGR24;
else if (video_driver_get_pixel_format() == RETRO_PIXEL_FORMAT_XRGB8888)
2016-03-20 15:07:49 +01:00
bmp_type = RBMP_SOURCE_TYPE_XRGB888;
2016-03-20 05:12:53 +01:00
ret = rbmp_save_image(filename,
frame,
width,
height,
pitch,
bmp_type);
2015-09-27 02:32:30 +02:00
#endif
#ifdef HAVE_IMAGEVIEWER
if (ret == true)
{
if (content_push_to_history_playlist(g_defaults.image_history, filename,
"imageviewer", "builtin"))
playlist_write_file(g_defaults.image_history);
}
#endif
2015-09-27 02:32:30 +02:00
return ret;
}
2016-08-24 14:04:22 +02:00
#if !defined(VITA)
static bool take_screenshot_viewport(const char *name_base)
{
2015-06-12 17:00:37 +02:00
char screenshot_path[PATH_MAX_LENGTH] = {0};
const char *screenshot_dir = NULL;
uint8_t *buffer = NULL;
bool retval = false;
struct video_viewport vp = {0};
settings_t *settings = config_get_ptr();
2016-05-08 14:00:51 +02:00
video_driver_get_viewport_info(&vp);
if (!vp.width || !vp.height)
return false;
2016-05-17 15:00:18 +02:00
buffer = (uint8_t*)malloc(vp.width * vp.height * 3);
if (!buffer)
return false;
2016-05-08 14:00:51 +02:00
if (!video_driver_read_viewport(buffer))
2015-03-22 21:57:17 +01:00
goto done;
2016-04-28 19:26:02 +02:00
screenshot_dir = settings->directory.screenshot;
2016-05-17 15:04:28 +02:00
if (string_is_empty(screenshot_dir))
{
fill_pathname_basedir(screenshot_path, name_base,
sizeof(screenshot_path));
screenshot_dir = screenshot_path;
}
/* Data read from viewport is in bottom-up order, suitable for BMP. */
if (!screenshot_dump(name_base, screenshot_dir, buffer, vp.width, vp.height,
vp.width * 3, true))
goto done;
retval = true;
done:
if (buffer)
free(buffer);
return retval;
}
2016-08-24 14:04:22 +02:00
#endif
static bool take_screenshot_raw(const char *name_base)
{
unsigned width, height;
size_t pitch;
2015-06-12 17:00:37 +02:00
char screenshot_path[PATH_MAX_LENGTH] = {0};
const void *data = NULL;
settings_t *settings = config_get_ptr();
2016-05-04 16:37:58 +02:00
const char *screenshot_dir = settings->directory.screenshot;
2015-06-17 09:29:09 -03:00
video_driver_cached_frame_get(&data, &width, &height, &pitch);
2016-05-17 15:03:17 +02:00
if (string_is_empty(settings->directory.screenshot))
{
fill_pathname_basedir(screenshot_path, name_base,
sizeof(screenshot_path));
screenshot_dir = screenshot_path;
}
/* Negative pitch is needed as screenshot takes bottom-up,
* but we use top-down.
*/
if (!screenshot_dump(name_base, screenshot_dir,
(const uint8_t*)data + (height - 1) * pitch,
width, height, -pitch, false))
return false;
return true;
}
static bool take_screenshot_choice(const char *name_base)
{
2016-01-30 03:08:33 +01:00
settings_t *settings = config_get_ptr();
/* No way to infer screenshot directory. */
if (string_is_empty(settings->directory.screenshot) && (!*name_base))
return false;
2016-05-08 14:00:51 +02:00
if (video_driver_supports_viewport_read())
{
/* Avoid taking screenshot of GUI overlays. */
video_driver_set_texture_enable(false, false);
2016-05-08 14:00:51 +02:00
video_driver_cached_frame_render();
#if defined(VITA)
return take_screenshot_raw(name_base);
#else
return take_screenshot_viewport(name_base);
#endif
}
2016-02-03 16:13:37 +01:00
2016-05-08 14:00:51 +02:00
if (!video_driver_cached_frame_has_valid_framebuffer())
return take_screenshot_raw(name_base);
2016-02-03 16:13:37 +01:00
2016-05-08 14:00:51 +02:00
if (video_driver_supports_read_frame_raw())
{
unsigned old_width, old_height;
size_t old_pitch;
2016-01-30 03:08:33 +01:00
bool ret = false;
2016-06-03 05:24:14 +02:00
void *frame_data = NULL;
const void* old_data = NULL;
2016-01-30 03:08:33 +01:00
2015-06-17 09:29:09 -03:00
video_driver_cached_frame_get(&old_data, &old_width, &old_height,
&old_pitch);
2016-01-30 03:08:33 +01:00
2015-06-29 21:39:00 +02:00
frame_data = video_driver_read_frame_raw(
&old_width, &old_height, &old_pitch);
video_driver_cached_frame_set(old_data, old_width, old_height,
old_pitch);
if (frame_data)
{
2016-05-08 14:00:51 +02:00
video_driver_set_cached_frame_ptr(frame_data);
if (take_screenshot_raw(name_base))
2016-01-30 03:08:33 +01:00
ret = true;
free(frame_data);
}
2016-01-30 03:08:33 +01:00
return ret;
}
2016-01-30 03:08:33 +01:00
return false;
}
/**
* take_screenshot:
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool take_screenshot(void)
{
2016-09-17 19:41:16 +02:00
char *name_base = strdup(path_get_basename());
bool is_paused = runloop_ctl(RUNLOOP_CTL_IS_PAUSED, NULL);
bool ret = take_screenshot_choice(name_base);
2016-05-04 16:36:10 +02:00
const char *msg_screenshot = ret
? msg_hash_to_str(MSG_TAKING_SCREENSHOT) :
msg_hash_to_str(MSG_FAILED_TO_TAKE_SCREENSHOT);
const char *msg = msg_screenshot;
free(name_base);
2015-12-07 15:32:14 +01:00
runloop_msg_queue_push(msg, 1, is_paused ? 1 : 180, true);
2015-09-26 13:04:07 +02:00
if (is_paused)
2016-05-08 14:00:51 +02:00
video_driver_cached_frame_render();
return ret;
}