forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscreenshot.c
431 lines (361 loc) · 11.1 KB
/
screenshot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* 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.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <compat/strl.h>
#include <stdio.h>
#include <stddef.h>
#include <time.h>
#include <boolean.h>
#include <stdint.h>
#include <string.h>
#include "general.h"
#include "intl/intl.h"
#include <file/file_path.h>
#include "gfx/scaler/scaler.h"
#include "retroarch.h"
#include "runloop.h"
#include "retroarch_logger.h"
#include "screenshot.h"
#include "gfx/video_driver.h"
#include "gfx/video_viewport.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef _XBOX1
#include <xtl.h>
#include <xgraphics.h>
#endif
#if defined(HAVE_ZLIB_DEFLATE) && defined(HAVE_RPNG)
#include <formats/rpng.h>
#define IMG_EXT "png"
#else
#define IMG_EXT "bmp"
static bool write_header_bmp(FILE *file, unsigned width, unsigned height)
{
unsigned line_size = (width * 3 + 3) & ~3;
unsigned size = line_size * height + 54;
unsigned size_array = line_size * height;
/* Generic BMP stuff. */
const uint8_t header[] = {
'B', 'M',
(uint8_t)(size >> 0), (uint8_t)(size >> 8),
(uint8_t)(size >> 16), (uint8_t)(size >> 24),
0, 0, 0, 0,
54, 0, 0, 0,
40, 0, 0, 0,
(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),
1, 0,
24, 0,
0, 0, 0, 0,
(uint8_t)(size_array >> 0), (uint8_t)(size_array >> 8),
(uint8_t)(size_array >> 16), (uint8_t)(size_array >> 24),
19, 11, 0, 0,
19, 11, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
};
return fwrite(header, 1, sizeof(header), file) == sizeof(header);
}
static void dump_lines_file(FILE *file, uint8_t **lines,
size_t line_size, unsigned height)
{
unsigned i;
for (i = 0; i < height; i++)
fwrite(lines[i], 1, line_size, file);
}
static void dump_line_bgr(uint8_t *line, const uint8_t *src, unsigned width)
{
memcpy(line, src, width * 3);
}
static void dump_line_16(uint8_t *line, const uint16_t *src, unsigned width)
{
unsigned i;
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;
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)
{
size_t line_size;
int i, j;
union
{
const uint8_t *u8;
const uint16_t *u16;
const uint32_t *u32;
} u;
uint8_t **lines = (uint8_t**)calloc(height, sizeof(uint8_t*));
if (!lines)
return;
u.u8 = (const uint8_t*)frame;
line_size = (width * 3 + 3) & ~3;
for (i = 0; i < height; i++)
{
lines[i] = (uint8_t*)calloc(1, line_size);
if (!lines[i])
goto end;
}
if (bgr24) /* BGR24 byte order. Can directly copy. */
{
for (j = 0; j < height; j++, u.u8 += pitch)
dump_line_bgr(lines[j], u.u8, width);
}
else if (video_driver_get_pixel_format() == 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);
}
dump_lines_file(file, lines, line_size, height);
end:
for (i = 0; i < height; i++)
free(lines[i]);
free(lines);
}
#endif
static bool take_screenshot_viewport(void)
{
char screenshot_path[PATH_MAX_LENGTH];
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();
video_driver_viewport_info(&vp);
if (!vp.width || !vp.height)
return false;
if (!(buffer = (uint8_t*)malloc(vp.width * vp.height * 3)))
return false;
if (!video_driver_read_viewport(buffer))
goto done;
screenshot_dir = settings->screenshot_directory;
if (!*settings->screenshot_directory)
{
fill_pathname_basedir(screenshot_path, global->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)
{
unsigned width, height;
size_t pitch;
char screenshot_path[PATH_MAX_LENGTH];
global_t *global = global_get_ptr();
const void *data = NULL;
settings_t *settings = config_get_ptr();
const char *screenshot_dir = NULL;
video_driver_cached_frame_get(data, &width, &height, &pitch);
screenshot_dir = settings->screenshot_directory;
if (!*settings->screenshot_directory)
{
fill_pathname_basedir(screenshot_path, global->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;
runloop_t *runloop = rarch_main_get_ptr();
driver_t *driver = driver_get_ptr();
settings_t *settings = config_get_ptr();
global_t *global = global_get_ptr();
const struct retro_hw_render_callback *hw_render =
(const struct retro_hw_render_callback*)video_driver_callback();
/* No way to infer screenshot directory. */
if ((!*settings->screenshot_directory) && (!*global->basename))
return false;
viewport_read = (settings->video.gpu_screenshot ||
((hw_render->context_type
!= RETRO_HW_CONTEXT_NONE) && !driver->video->read_frame_raw))
&& driver->video->read_viewport && driver->video->viewport_info;
if (viewport_read)
{
/* Avoid taking screenshot of GUI overlays. */
video_driver_set_texture_enable(false, false);
if (driver->video)
video_driver_cached_frame();
}
if (viewport_read)
ret = take_screenshot_viewport();
else if (!video_driver_cached_frame_has_valid_fb())
ret = take_screenshot_raw();
else if (driver->video->read_frame_raw)
{
unsigned old_width, old_height;
size_t old_pitch;
const void* old_data = NULL;
video_driver_cached_frame_get(old_data, &old_width, &old_height,
&old_pitch);
void* 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)
{
video_driver_cached_frame_set_ptr(frame_data);
ret = take_screenshot_raw();
free(frame_data);
}
else
ret = false;
}
else
{
RARCH_ERR(RETRO_LOG_TAKE_SCREENSHOT_ERROR);
ret = false;
}
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, runloop->is_paused ? 1 : 180, true);
if (runloop->is_paused)
video_driver_cached_frame();
return ret;
}
/* 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];
struct scaler_ctx scaler = {0};
FILE *file = NULL;
uint8_t *out_buffer = NULL;
bool ret = false;
driver_t *driver = driver_get_ptr();
(void)file;
(void)out_buffer;
(void)scaler;
(void)driver;
fill_dated_filename(shotname, IMG_EXT, sizeof(shotname));
fill_pathname_join(filename, folder, shotname, sizeof(filename));
#ifdef _XBOX1
d3d_video_t *d3d = (d3d_video_t*)driver->video_data;
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)
{
RARCH_LOG("Screenshot saved: %s.\n", filename);
rarch_main_msg_queue_push("Screenshot saved.", 1, 30, false);
return true;
}
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");
ret = rpng_save_image_bgr24(filename,
out_buffer, width, height, width * 3);
if (!ret)
RARCH_ERR("Failed to take screenshot.\n");
free(out_buffer);
#else
file = fopen(filename, "wb");
if (!file)
{
RARCH_ERR("Failed to open file \"%s\" for screenshot.\n", filename);
return false;
}
ret = write_header_bmp(file, width, height);
if (ret)
dump_content(file, frame, width, height, pitch, bgr24);
else
RARCH_ERR("Failed to write image header.\n");
fclose(file);
#endif
return ret;
}