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
|
2016-01-10 03:06:50 +00:00
|
|
|
* Copyright (C) 2011-2016 - Daniel De Matteis
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2015-06-29 22:38:10 +00:00
|
|
|
#ifdef _XBOX1
|
|
|
|
#include <xtl.h>
|
|
|
|
#include <xgraphics.h>
|
|
|
|
#endif
|
|
|
|
|
2011-05-15 14:54:43 +00:00
|
|
|
#include <stdio.h>
|
2014-09-11 22:11:08 +00:00
|
|
|
#include <stddef.h>
|
2011-05-15 14:54:43 +00:00
|
|
|
#include <time.h>
|
2014-10-21 03:05:52 +00:00
|
|
|
#include <boolean.h>
|
2011-05-15 14:54:43 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2015-06-29 22:38:10 +00:00
|
|
|
|
|
|
|
#include <file/file_path.h>
|
|
|
|
#include <compat/strl.h>
|
|
|
|
|
2015-09-19 02:40:30 +00:00
|
|
|
#include <formats/rbmp.h>
|
|
|
|
|
2015-06-29 22:38:10 +00:00
|
|
|
#if defined(HAVE_ZLIB_DEFLATE) && defined(HAVE_RPNG)
|
|
|
|
#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
|
|
|
|
2011-05-15 14:54:43 +00:00
|
|
|
#include "general.h"
|
2015-07-02 16:10:52 +00:00
|
|
|
#include "msg_hash.h"
|
2013-01-19 01:13:02 +00:00
|
|
|
#include "gfx/scaler/scaler.h"
|
2015-02-10 19:58:33 +00:00
|
|
|
#include "retroarch.h"
|
|
|
|
#include "screenshot.h"
|
2015-11-23 11:03:38 +00:00
|
|
|
#include "verbosity.h"
|
2015-03-22 20:57:17 +00:00
|
|
|
#include "gfx/video_driver.h"
|
2011-05-15 14:54:43 +00:00
|
|
|
|
2012-06-17 11:11:12 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
2011-05-15 14:54:43 +00:00
|
|
|
|
2015-09-27 00:32:30 +00:00
|
|
|
/* Take frame bottom-up. */
|
|
|
|
static bool screenshot_dump(const char *folder, const void *frame,
|
|
|
|
unsigned width, unsigned height, int pitch, bool bgr24)
|
|
|
|
{
|
2016-03-10 22:36:46 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
global_t *global = global_get_ptr();
|
2015-09-28 16:12:02 +00:00
|
|
|
bool ret;
|
2016-01-25 00:01:57 +00:00
|
|
|
char filename[PATH_MAX_LENGTH];
|
|
|
|
char shotname[256];
|
|
|
|
#if defined(HAVE_ZLIB_DEFLATE) && defined(HAVE_RPNG)
|
2015-09-27 00:32:30 +00:00
|
|
|
uint8_t *out_buffer = NULL;
|
2016-01-25 00:01:57 +00:00
|
|
|
struct scaler_ctx scaler = {0};
|
|
|
|
#endif
|
2015-09-27 00:32:30 +00:00
|
|
|
|
2016-03-10 22:36:46 +00:00
|
|
|
if (settings->auto_screenshot_filename)
|
|
|
|
{
|
|
|
|
fill_dated_filename(shotname, IMG_EXT, sizeof(shotname));
|
|
|
|
fill_pathname_join(filename, folder, shotname, sizeof(filename));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
snprintf(shotname, sizeof(shotname),"%s.png", path_basename(global->name.base));
|
|
|
|
fill_pathname_join(filename, folder, shotname, sizeof(filename));
|
|
|
|
}
|
2015-09-27 00:32:30 +00:00
|
|
|
|
|
|
|
#ifdef _XBOX1
|
2015-11-23 17:01:46 +00:00
|
|
|
d3d_video_t *d3d = (d3d_video_t*)video_driver_get_ptr(true);
|
2015-09-27 00:32:30 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
|
|
|
|
|
|
|
D3DSurface *surf = NULL;
|
|
|
|
d3d->dev->GetBackBuffer(-1, D3DBACKBUFFER_TYPE_MONO, &surf);
|
|
|
|
ret = XGWriteSurfaceToFile(surf, filename);
|
|
|
|
surf->Release();
|
|
|
|
|
|
|
|
if(ret == S_OK)
|
|
|
|
ret = true;
|
|
|
|
else
|
|
|
|
ret = false;
|
|
|
|
#elif defined(HAVE_ZLIB_DEFLATE) && defined(HAVE_RPNG)
|
|
|
|
out_buffer = (uint8_t*)malloc(width * height * 3);
|
|
|
|
if (!out_buffer)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
scaler.in_width = width;
|
|
|
|
scaler.in_height = height;
|
|
|
|
scaler.out_width = width;
|
|
|
|
scaler.out_height = height;
|
|
|
|
scaler.in_stride = -pitch;
|
|
|
|
scaler.out_stride = width * 3;
|
|
|
|
scaler.out_fmt = SCALER_FMT_BGR24;
|
|
|
|
scaler.scaler_type = SCALER_TYPE_POINT;
|
|
|
|
|
|
|
|
if (bgr24)
|
|
|
|
scaler.in_fmt = SCALER_FMT_BGR24;
|
|
|
|
else if (video_driver_get_pixel_format() == RETRO_PIXEL_FORMAT_XRGB8888)
|
|
|
|
scaler.in_fmt = SCALER_FMT_ARGB8888;
|
|
|
|
else
|
|
|
|
scaler.in_fmt = SCALER_FMT_RGB565;
|
|
|
|
|
|
|
|
scaler_ctx_gen_filter(&scaler);
|
|
|
|
scaler_ctx_scale(&scaler, out_buffer,
|
|
|
|
(const uint8_t*)frame + ((int)height - 1) * pitch);
|
|
|
|
scaler_ctx_gen_reset(&scaler);
|
|
|
|
|
|
|
|
RARCH_LOG("Using RPNG for PNG screenshots.\n");
|
2016-03-20 04:12:53 +00:00
|
|
|
ret = rpng_save_image_bgr24(
|
|
|
|
filename,
|
|
|
|
out_buffer,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
width * 3
|
|
|
|
);
|
2015-09-27 00:32:30 +00:00
|
|
|
free(out_buffer);
|
|
|
|
#else
|
2016-03-20 04:12:53 +00: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 14:07:49 +00:00
|
|
|
bmp_type = RBMP_SOURCE_TYPE_XRGB888;
|
2016-03-20 04:12:53 +00:00
|
|
|
|
|
|
|
ret = rbmp_save_image(filename,
|
|
|
|
frame,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
pitch,
|
|
|
|
bmp_type);
|
2015-09-27 00:32:30 +00:00
|
|
|
#endif
|
|
|
|
if (!ret)
|
|
|
|
RARCH_ERR("Failed to take screenshot.\n");
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-02-10 19:58:33 +00:00
|
|
|
static bool take_screenshot_viewport(void)
|
|
|
|
{
|
2015-06-12 15:00:37 +00: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();
|
|
|
|
global_t *global = global_get_ptr();
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2016-02-14 04:42:27 +00:00
|
|
|
video_driver_ctl(RARCH_DISPLAY_CTL_VIEWPORT_INFO, &vp);
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
if (!vp.width || !vp.height)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!(buffer = (uint8_t*)malloc(vp.width * vp.height * 3)))
|
|
|
|
return false;
|
|
|
|
|
2015-11-20 14:41:20 +00:00
|
|
|
if (!video_driver_ctl(RARCH_DISPLAY_CTL_READ_VIEWPORT, buffer))
|
2015-03-22 20:57:17 +00:00
|
|
|
goto done;
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2015-03-20 19:43:22 +00:00
|
|
|
screenshot_dir = settings->screenshot_directory;
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2015-03-20 19:43:22 +00:00
|
|
|
if (!*settings->screenshot_directory)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2015-07-27 15:18:10 +00:00
|
|
|
fill_pathname_basedir(screenshot_path, global->name.base,
|
2015-02-10 19:58:33 +00:00
|
|
|
sizeof(screenshot_path));
|
|
|
|
screenshot_dir = screenshot_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Data read from viewport is in bottom-up order, suitable for BMP. */
|
|
|
|
if (!screenshot_dump(screenshot_dir, buffer, vp.width, vp.height,
|
|
|
|
vp.width * 3, true))
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
retval = true;
|
|
|
|
|
|
|
|
done:
|
|
|
|
if (buffer)
|
|
|
|
free(buffer);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool take_screenshot_raw(void)
|
|
|
|
{
|
2015-05-20 19:29:23 +00:00
|
|
|
unsigned width, height;
|
|
|
|
size_t pitch;
|
2015-06-12 15:00:37 +00:00
|
|
|
char screenshot_path[PATH_MAX_LENGTH] = {0};
|
|
|
|
const void *data = NULL;
|
|
|
|
const char *screenshot_dir = NULL;
|
|
|
|
global_t *global = global_get_ptr();
|
|
|
|
settings_t *settings = config_get_ptr();
|
2015-05-20 19:29:23 +00:00
|
|
|
|
2015-06-17 12:29:09 +00:00
|
|
|
video_driver_cached_frame_get(&data, &width, &height, &pitch);
|
2016-03-10 22:36:46 +00:00
|
|
|
|
2015-03-20 19:43:22 +00:00
|
|
|
screenshot_dir = settings->screenshot_directory;
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2015-03-20 19:43:22 +00:00
|
|
|
if (!*settings->screenshot_directory)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2015-07-27 15:18:10 +00:00
|
|
|
fill_pathname_basedir(screenshot_path, global->name.base,
|
2015-02-10 19:58:33 +00:00
|
|
|
sizeof(screenshot_path));
|
|
|
|
screenshot_dir = screenshot_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Negative pitch is needed as screenshot takes bottom-up,
|
|
|
|
* but we use top-down.
|
|
|
|
*/
|
|
|
|
return screenshot_dump(screenshot_dir,
|
|
|
|
(const uint8_t*)data + (height - 1) * pitch,
|
|
|
|
width, height, -pitch, false);
|
|
|
|
}
|
|
|
|
|
2016-01-30 02:08:33 +00:00
|
|
|
static bool take_screenshot_choice(void)
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
2015-03-21 03:43:18 +00:00
|
|
|
global_t *global = global_get_ptr();
|
2016-01-30 02:08:33 +00:00
|
|
|
settings_t *settings = config_get_ptr();
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
/* No way to infer screenshot directory. */
|
2015-07-27 15:18:10 +00:00
|
|
|
if ((!*settings->screenshot_directory) && (!*global->name.base))
|
2015-02-10 19:58:33 +00:00
|
|
|
return false;
|
|
|
|
|
2016-01-30 02:08:33 +00:00
|
|
|
if (video_driver_ctl(RARCH_DISPLAY_CTL_SUPPORTS_VIEWPORT_READ, NULL))
|
2015-02-10 19:58:33 +00:00
|
|
|
{
|
|
|
|
/* Avoid taking screenshot of GUI overlays. */
|
2015-03-22 17:59:16 +00:00
|
|
|
video_driver_set_texture_enable(false, false);
|
2015-11-23 17:01:46 +00:00
|
|
|
video_driver_ctl(RARCH_DISPLAY_CTL_CACHED_FRAME_RENDER, NULL);
|
2016-01-30 02:08:33 +00:00
|
|
|
return take_screenshot_viewport();
|
2015-02-10 19:58:33 +00:00
|
|
|
}
|
2016-02-03 15:13:37 +00:00
|
|
|
|
|
|
|
if (!video_driver_ctl(RARCH_DISPLAY_CTL_CACHED_FRAME_HAS_VALID_FB, NULL))
|
2016-01-30 02:08:33 +00:00
|
|
|
return take_screenshot_raw();
|
2016-02-03 15:13:37 +00:00
|
|
|
|
|
|
|
if (video_driver_ctl(RARCH_DISPLAY_CTL_SUPPORTS_READ_FRAME_RAW, NULL))
|
2015-03-16 12:41:26 +00:00
|
|
|
{
|
2015-05-20 19:29:23 +00:00
|
|
|
unsigned old_width, old_height;
|
|
|
|
size_t old_pitch;
|
2015-06-29 19:39:00 +00:00
|
|
|
void *frame_data;
|
2016-01-30 02:08:33 +00:00
|
|
|
bool ret = false;
|
2015-05-20 19:29:23 +00:00
|
|
|
const void* old_data = NULL;
|
2016-01-30 02:08:33 +00:00
|
|
|
|
2015-06-17 12:29:09 +00:00
|
|
|
video_driver_cached_frame_get(&old_data, &old_width, &old_height,
|
2015-05-20 19:29:23 +00:00
|
|
|
&old_pitch);
|
2016-01-30 02:08:33 +00:00
|
|
|
|
2015-06-29 19:39:00 +00:00
|
|
|
frame_data = video_driver_read_frame_raw(
|
|
|
|
&old_width, &old_height, &old_pitch);
|
2015-05-20 19:29:23 +00:00
|
|
|
|
|
|
|
video_driver_cached_frame_set(old_data, old_width, old_height,
|
|
|
|
old_pitch);
|
2015-03-16 12:41:26 +00:00
|
|
|
|
|
|
|
if (frame_data)
|
|
|
|
{
|
2015-12-09 09:13:48 +00:00
|
|
|
video_driver_ctl(RARCH_DISPLAY_CTL_CACHED_FRAME_SET_PTR, (void*)frame_data);
|
2016-01-30 02:08:33 +00:00
|
|
|
if (take_screenshot_raw())
|
|
|
|
ret = true;
|
2015-03-16 12:41:26 +00:00
|
|
|
free(frame_data);
|
|
|
|
}
|
2016-01-30 02:08:33 +00:00
|
|
|
|
|
|
|
return ret;
|
2015-03-16 14:32:18 +00:00
|
|
|
}
|
2015-03-16 12:41:26 +00:00
|
|
|
|
2016-01-30 02:08:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* take_screenshot:
|
|
|
|
*
|
|
|
|
* Returns: true (1) if successful, otherwise false (0).
|
|
|
|
**/
|
|
|
|
bool take_screenshot(void)
|
|
|
|
{
|
|
|
|
bool is_paused;
|
2016-01-30 02:10:41 +00:00
|
|
|
const char *msg_screenshot = NULL;
|
|
|
|
const char *msg = NULL;
|
2016-01-30 02:08:33 +00:00
|
|
|
bool ret = take_screenshot_choice();
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
if (ret)
|
2016-01-30 02:10:41 +00:00
|
|
|
msg_screenshot = msg_hash_to_str(MSG_TAKING_SCREENSHOT);
|
2015-02-10 19:58:33 +00:00
|
|
|
else
|
2016-01-30 02:10:41 +00:00
|
|
|
msg_screenshot = msg_hash_to_str(MSG_FAILED_TO_TAKE_SCREENSHOT);
|
|
|
|
|
|
|
|
msg = msg_screenshot;
|
|
|
|
|
2016-01-30 02:11:07 +00:00
|
|
|
RARCH_LOG("%s.\n", msg);
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2015-11-30 21:25:13 +00:00
|
|
|
is_paused = runloop_ctl(RUNLOOP_CTL_IS_PAUSED, NULL);
|
2015-02-10 19:58:33 +00:00
|
|
|
|
2015-12-07 14:32:14 +00:00
|
|
|
runloop_msg_queue_push(msg, 1, is_paused ? 1 : 180, true);
|
2015-09-26 11:04:07 +00:00
|
|
|
|
|
|
|
if (is_paused)
|
2015-11-20 14:34:10 +00:00
|
|
|
video_driver_ctl(RARCH_DISPLAY_CTL_CACHED_FRAME_RENDER, NULL);
|
2015-02-10 19:58:33 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|