RetroArch/screenshot.c

397 lines
10 KiB
C
Raw Normal View History

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
2015-01-07 16:46:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
2011-05-15 14:54:43 +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/>.
*/
2014-10-21 05:58:58 +00:00
#include <compat/strl.h>
2011-05-15 14:54:43 +00:00
#include <stdio.h>
#include <stddef.h>
2011-05-15 14:54:43 +00:00
#include <time.h>
#include <boolean.h>
2011-05-15 14:54:43 +00:00
#include <stdint.h>
#include <string.h>
#include "general.h"
#include "intl/intl.h"
2014-10-21 22:23:06 +00:00
#include <file/file_path.h>
2013-01-19 01:13:02 +00:00
#include "gfx/scaler/scaler.h"
#include "retroarch.h"
#include "runloop.h"
#include "retroarch_logger.h"
#include "screenshot.h"
2015-02-14 04:52:05 +00:00
#include "gfx/video_viewport.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
2013-01-19 01:13:02 +00:00
#ifdef HAVE_ZLIB_DEFLATE
2015-01-16 06:03:26 +00:00
2015-02-11 01:30:56 +00:00
#include <formats/rpng.h>
2015-01-16 06:03:26 +00:00
#define IMG_EXT "png"
2012-06-17 11:11:12 +00:00
#else
2015-01-16 06:03:26 +00:00
#define IMG_EXT "bmp"
2012-06-17 11:11:12 +00:00
static bool write_header_bmp(FILE *file, unsigned width, unsigned height)
2011-05-15 14:54:43 +00:00
{
2015-01-16 06:03:26 +00:00
unsigned line_size = (width * 3 + 3) & ~3;
unsigned size = line_size * height + 54;
2011-05-15 14:54:43 +00:00
unsigned size_array = line_size * height;
/* Generic BMP stuff. */
2011-05-15 14:54:43 +00:00
const uint8_t header[] = {
'B', 'M',
2014-09-02 14:13:42 +00:00
(uint8_t)(size >> 0), (uint8_t)(size >> 8),
(uint8_t)(size >> 16), (uint8_t)(size >> 24),
2011-05-15 14:54:43 +00:00
0, 0, 0, 0,
54, 0, 0, 0,
40, 0, 0, 0,
2014-09-02 14:13:42 +00:00
(uint8_t)(width >> 0), (uint8_t)(width >> 8),
(uint8_t)(width >> 16), (uint8_t)(width >> 24),
(uint8_t)(height >> 0), (uint8_t)(height >> 8),
(uint8_t)(height >> 16), (uint8_t)(height >> 24),
2011-05-15 14:54:43 +00:00
1, 0,
24, 0,
0, 0, 0, 0,
2014-09-02 14:13:42 +00:00
(uint8_t)(size_array >> 0), (uint8_t)(size_array >> 8),
(uint8_t)(size_array >> 16), (uint8_t)(size_array >> 24),
2011-05-15 14:54:43 +00:00
19, 11, 0, 0,
19, 11, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
2012-06-17 11:11:12 +00:00
return fwrite(header, 1, sizeof(header), file) == sizeof(header);
2011-05-15 14:54:43 +00:00
}
2014-09-02 14:13:42 +00:00
static void dump_lines_file(FILE *file, uint8_t **lines,
size_t line_size, unsigned height)
2012-06-17 11:11:12 +00:00
{
unsigned i;
2015-01-16 06:03:26 +00:00
for (i = 0; i < height; i++)
2012-06-17 11:11:12 +00:00
fwrite(lines[i], 1, line_size, file);
}
static void dump_line_bgr(uint8_t *line, const uint8_t *src, unsigned width)
2011-05-15 14:54:43 +00:00
{
memcpy(line, src, width * 3);
}
static void dump_line_16(uint8_t *line, const uint16_t *src, unsigned width)
{
unsigned i;
2015-01-16 06:03:26 +00:00
for (i = 0; i < width; i++)
{
uint16_t pixel = *src++;
uint8_t b = (pixel >> 0) & 0x1f;
uint8_t g = (pixel >> 5) & 0x3f;
uint8_t r = (pixel >> 11) & 0x1f;
*line++ = (b << 3) | (b >> 2);
*line++ = (g << 2) | (g >> 4);
*line++ = (r << 3) | (r >> 2);
}
}
static void dump_line_32(uint8_t *line, const uint32_t *src, unsigned width)
{
unsigned i;
2015-01-16 06:03:26 +00:00
for (i = 0; i < width; i++)
{
uint32_t pixel = *src++;
*line++ = (pixel >> 0) & 0xff;
*line++ = (pixel >> 8) & 0xff;
*line++ = (pixel >> 16) & 0xff;
}
}
static void dump_content(FILE *file, const void *frame,
int width, int height, int pitch, bool bgr24)
{
2015-01-16 06:03:26 +00:00
size_t line_size;
int i, j;
2015-01-16 06:03:26 +00:00
uint8_t **lines;
union
{
const uint8_t *u8;
const uint16_t *u16;
const uint32_t *u32;
} u;
u.u8 = (const uint8_t*)frame;
2015-01-16 06:03:26 +00:00
lines = (uint8_t**)calloc(height, sizeof(uint8_t*));
2012-06-17 11:11:12 +00:00
if (!lines)
2011-12-24 12:46:12 +00:00
return;
2011-05-15 14:54:43 +00:00
2015-01-16 06:03:26 +00:00
line_size = (width * 3 + 3) & ~3;
2012-06-17 11:11:12 +00:00
for (i = 0; i < height; i++)
2012-06-17 11:11:12 +00:00
{
lines[i] = (uint8_t*)calloc(1, line_size);
if (!lines[i])
goto end;
}
if (bgr24) /* BGR24 byte order. Can directly copy. */
2011-05-15 14:54:43 +00:00
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_bgr(lines[j], u.u8, width);
}
else if (g_extern.system.pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888)
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_32(lines[j], u.u32, width);
}
else /* RGB565 */
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_16(lines[j], u.u16, width);
2011-05-15 14:54:43 +00:00
}
2011-12-24 12:46:12 +00:00
2012-06-17 11:11:12 +00:00
dump_lines_file(file, lines, line_size, height);
end:
for (i = 0; i < height; i++)
2012-06-17 11:11:12 +00:00
free(lines[i]);
free(lines);
2011-05-15 14:54:43 +00:00
}
2013-01-19 01:13:02 +00:00
#endif
2011-05-15 14:54:43 +00:00
static bool take_screenshot_viewport(void)
{
char screenshot_path[PATH_MAX_LENGTH];
const char *screenshot_dir = NULL;
uint8_t *buffer = NULL;
bool retval = false;
2015-02-14 04:52:05 +00:00
struct video_viewport vp = {0};
if (driver.video && driver.video->viewport_info)
driver.video->viewport_info(driver.video_data, &vp);
if (!vp.width || !vp.height)
return false;
if (!(buffer = (uint8_t*)malloc(vp.width * vp.height * 3)))
return false;
if (driver.video && driver.video->read_viewport)
if (!driver.video->read_viewport(driver.video_data, buffer))
goto done;
screenshot_dir = g_settings.screenshot_directory;
if (!*g_settings.screenshot_directory)
{
fill_pathname_basedir(screenshot_path, g_extern.basename,
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)
{
char screenshot_path[PATH_MAX_LENGTH];
const void *data = g_extern.frame_cache.data;
unsigned width = g_extern.frame_cache.width;
unsigned height = g_extern.frame_cache.height;
int pitch = g_extern.frame_cache.pitch;
const char *screenshot_dir = g_settings.screenshot_directory;
if (!*g_settings.screenshot_directory)
{
fill_pathname_basedir(screenshot_path, g_extern.basename,
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);
}
/**
* take_screenshot:
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool take_screenshot(void)
{
bool viewport_read = false;
bool ret = true;
const char *msg = NULL;
/* No way to infer screenshot directory. */
if ((!*g_settings.screenshot_directory) && (!*g_extern.basename))
return false;
viewport_read = (g_settings.video.gpu_screenshot ||
((g_extern.system.hw_render_callback.context_type
!= RETRO_HW_CONTEXT_NONE) && !driver.video->read_frame_raw))
&& driver.video->read_viewport && driver.video->viewport_info;
if (viewport_read)
{
#ifdef HAVE_MENU
/* Avoid taking screenshot of GUI overlays. */
if (driver.video_poke && driver.video_poke->set_texture_enable)
driver.video_poke->set_texture_enable(driver.video_data,
false, false);
#endif
if (driver.video)
rarch_render_cached_frame();
}
if (viewport_read)
ret = take_screenshot_viewport();
else if (g_extern.frame_cache.data &&
(g_extern.frame_cache.data != RETRO_HW_FRAME_BUFFER_VALID))
ret = take_screenshot_raw();
else if (driver.video->read_frame_raw)
{
const void* old_data = g_extern.frame_cache.data;
unsigned old_width = g_extern.frame_cache.width;
unsigned old_height = g_extern.frame_cache.height;
size_t old_pitch = g_extern.frame_cache.pitch;
void* frame_data = driver.video->read_frame_raw
(driver.video_data, &g_extern.frame_cache.width,
&g_extern.frame_cache.height, &g_extern.frame_cache.pitch);
if (frame_data)
{
g_extern.frame_cache.data = frame_data;
ret = take_screenshot_raw();
free(frame_data);
}
else
RARCH_ERR(RETRO_LOG_TAKE_SCREENSHOT_ERROR);
g_extern.frame_cache.data = old_data;
g_extern.frame_cache.width = old_width;
g_extern.frame_cache.height = old_height;
g_extern.frame_cache.pitch = old_pitch;
}
if (ret)
{
RARCH_LOG(RETRO_LOG_TAKE_SCREENSHOT);
msg = RETRO_MSG_TAKE_SCREENSHOT;
}
else
{
RARCH_WARN(RETRO_LOG_TAKE_SCREENSHOT_FAILED);
msg = RETRO_MSG_TAKE_SCREENSHOT_FAILED;
}
rarch_main_msg_queue_push(msg, 1, g_runloop.is_paused ? 1 : 180, true);
if (g_runloop.is_paused)
rarch_render_cached_frame();
return ret;
}
2015-01-16 06:03:26 +00:00
/* Take frame bottom-up. */
bool screenshot_dump(const char *folder, const void *frame,
unsigned width, unsigned height, int pitch, bool bgr24)
{
char filename[PATH_MAX_LENGTH];
char shotname[PATH_MAX_LENGTH];
2015-01-16 06:03:26 +00:00
struct scaler_ctx scaler = {0};
FILE *file = NULL;
uint8_t *out_buffer = NULL;
bool ret = false;
2015-01-16 06:03:26 +00:00
(void)file;
(void)out_buffer;
(void)scaler;
fill_dated_filename(shotname, IMG_EXT, sizeof(shotname));
2012-11-25 15:03:16 +00:00
fill_pathname_join(filename, folder, shotname, sizeof(filename));
2011-05-15 14:54:43 +00:00
2013-01-19 01:13:02 +00:00
#ifdef HAVE_ZLIB_DEFLATE
2015-01-16 06:03:26 +00:00
out_buffer = (uint8_t*)malloc(width * height * 3);
2013-01-19 01:13:02 +00:00
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 (g_extern.system.pix_fmt == RETRO_PIXEL_FORMAT_XRGB8888)
scaler.in_fmt = SCALER_FMT_ARGB8888;
else
scaler.in_fmt = SCALER_FMT_RGB565;
scaler_ctx_gen_filter(&scaler);
2014-09-02 14:13:42 +00:00
scaler_ctx_scale(&scaler, out_buffer,
(const uint8_t*)frame + ((int)height - 1) * pitch);
2013-01-19 01:13:02 +00:00
scaler_ctx_gen_reset(&scaler);
RARCH_LOG("Using RPNG for PNG screenshots.\n");
2015-01-16 06:03:26 +00:00
ret = rpng_save_image_bgr24(filename,
2014-09-02 14:13:42 +00:00
out_buffer, width, height, width * 3);
2013-01-19 01:13:02 +00:00
if (!ret)
RARCH_ERR("Failed to take screenshot.\n");
free(out_buffer);
#else
2015-01-16 06:03:26 +00:00
file = fopen(filename, "wb");
2011-05-15 14:54:43 +00:00
if (!file)
{
2012-04-21 21:25:32 +00:00
RARCH_ERR("Failed to open file \"%s\" for screenshot.\n", filename);
2011-05-15 14:54:43 +00:00
return false;
}
2015-01-16 06:03:26 +00:00
ret = write_header_bmp(file, width, height);
2011-05-15 14:54:43 +00:00
2012-06-17 11:11:12 +00:00
if (ret)
dump_content(file, frame, width, height, pitch, bgr24);
else
RARCH_ERR("Failed to write image header.\n");
2011-05-15 14:54:43 +00:00
2012-06-17 11:11:12 +00:00
fclose(file);
2013-01-19 01:13:02 +00:00
#endif
2015-01-16 06:03:26 +00:00
return ret;
2011-05-15 14:54:43 +00:00
}