forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenshot.cpp
451 lines (402 loc) · 12.5 KB
/
Screenshot.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// Copyright (c) 2012- PPSSPP Project.
// This program 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 Foundation, version 2.0 or later versions.
// This program 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 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "ppsspp_config.h"
#include <algorithm>
#include <png.h>
#include "ext/jpge/jpge.h"
#include "Common/Data/Convert/ColorConv.h"
#include "Common/File/FileUtil.h"
#include "Common/File/Path.h"
#include "Common/Log.h"
#include "Common/System/Display.h"
#include "Core/Config.h"
#include "Core/Screenshot.h"
#include "Core/Core.h"
#include "GPU/Common/GPUDebugInterface.h"
#include "GPU/Common/FramebufferManagerCommon.h"
#include "GPU/GPUInterface.h"
#include "GPU/GPUState.h"
// This is used to make non-ASCII paths work for filename.
// Technically only needed on Windows.
class JPEGFileStream : public jpge::output_stream
{
public:
JPEGFileStream(const Path &filename) {
fp_ = File::OpenCFile(filename, "wb");
}
~JPEGFileStream() {
if (fp_ ) {
fclose(fp_);
}
}
bool put_buf(const void *buf, int len) override
{
if (fp_) {
if (fwrite(buf, len, 1, fp_) != 1) {
fclose(fp_);
fp_ = nullptr;
}
}
return Valid();
}
bool Valid() {
return fp_ != nullptr;
}
private:
FILE *fp_;
};
static bool WriteScreenshotToJPEG(const Path &filename, int width, int height, int num_channels, const uint8_t *image_data, const jpge::params &comp_params) {
JPEGFileStream dst_stream(filename);
if (!dst_stream.Valid()) {
ERROR_LOG(Log::IO, "Unable to open screenshot file for writing.");
return false;
}
jpge::jpeg_encoder dst_image;
if (!dst_image.init(&dst_stream, width, height, num_channels, comp_params)) {
return false;
}
for (u32 pass_index = 0; pass_index < dst_image.get_total_passes(); pass_index++) {
for (int i = 0; i < height; i++) {
const uint8_t *buf = image_data + i * width * num_channels;
if (!dst_image.process_scanline(buf)) {
return false;
}
}
if (!dst_image.process_scanline(NULL)) {
return false;
}
}
if (!dst_stream.Valid()) {
ERROR_LOG(Log::System, "Screenshot file write failed.");
}
dst_image.deinit();
return dst_stream.Valid();
}
static bool WriteScreenshotToPNG(png_imagep image, const Path &filename, int convert_to_8bit, const void *buffer, png_int_32 row_stride, const void *colormap) {
FILE *fp = File::OpenCFile(filename, "wb");
if (!fp) {
ERROR_LOG(Log::IO, "Unable to open screenshot file for writing.");
return false;
}
if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer, row_stride, colormap)) {
fclose(fp);
return true;
} else {
ERROR_LOG(Log::IO, "Screenshot PNG encode failed.");
fclose(fp);
// Should we even do this?
File::Delete(filename);
return false;
}
}
static bool ConvertPixelTo8888RGBA(GPUDebugBufferFormat fmt, u8 &r, u8 &g, u8 &b, u8 &a, const void *buffer, int offset, bool rev) {
const u8 *buf8 = (const u8 *)buffer;
const u16 *buf16 = (const u16 *)buffer;
const u32 *buf32 = (const u32 *)buffer;
const float *fbuf = (const float *)buffer;
// NOTE: a and r might be the same channel. This is used for RGB.
u32 src;
double fsrc;
switch (fmt) {
case GPU_DBG_FORMAT_565:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
a = 255;
r = Convert5To8((src >> 0) & 0x1F);
g = Convert6To8((src >> 5) & 0x3F);
b = Convert5To8((src >> 11) & 0x1F);
break;
case GPU_DBG_FORMAT_5551:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
a = (src >> 15) ? 255 : 0;
r = Convert5To8((src >> 0) & 0x1F);
g = Convert5To8((src >> 5) & 0x1F);
b = Convert5To8((src >> 10) & 0x1F);
break;
case GPU_DBG_FORMAT_4444:
src = buf16[offset];
if (rev) {
src = bswap16(src);
}
a = Convert4To8((src >> 12) & 0xF);
r = Convert4To8((src >> 0) & 0xF);
g = Convert4To8((src >> 4) & 0xF);
b = Convert4To8((src >> 8) & 0xF);
break;
case GPU_DBG_FORMAT_8888:
src = buf32[offset];
if (rev) {
src = bswap32(src);
}
a = (src >> 24) & 0xFF;
r = (src >> 0) & 0xFF;
g = (src >> 8) & 0xFF;
b = (src >> 16) & 0xFF;
break;
case GPU_DBG_FORMAT_FLOAT:
fsrc = fbuf[offset];
r = 255;
g = 0;
b = 0;
a = fsrc >= 1.0 ? 255 : (fsrc < 0.0 ? 0 : (int)(fsrc * 255.0));
break;
case GPU_DBG_FORMAT_16BIT:
src = buf16[offset];
r = 255;
g = 0;
b = 0;
a = src >> 8;
break;
case GPU_DBG_FORMAT_8BIT:
src = buf8[offset];
r = 255;
g = 0;
b = 0;
a = src;
break;
case GPU_DBG_FORMAT_24BIT_8X:
src = buf32[offset];
r = 255;
g = 0;
b = 0;
a = (src >> 16) & 0xFF;
break;
case GPU_DBG_FORMAT_24X_8BIT:
src = buf32[offset];
r = 255;
g = 0;
b = 0;
a = (src >> 24) & 0xFF;
break;
case GPU_DBG_FORMAT_24BIT_8X_DIV_256:
src = buf32[offset]& 0x00FFFFFF;
src = src - 0x800000 + 0x8000;
r = 255;
g = 0;
b = 0;
a = (src >> 8) & 0xFF;
break;
case GPU_DBG_FORMAT_FLOAT_DIV_256:
fsrc = fbuf[offset];
src = (int)(fsrc * 16777215.0);
src = src - 0x800000 + 0x8000;
r = 255;
g = 0;
b = 0;
a = (src >> 8) & 0xFF;
break;
default:
_assert_msg_(false, "Unsupported framebuffer format for screenshot: %d", fmt);
return false;
}
return true;
}
const u8 *ConvertBufferToScreenshot(const GPUDebugBuffer &buf, bool alpha, u8 *&temp, u32 &w, u32 &h) {
size_t pixelSize = alpha ? 4 : 3;
GPUDebugBufferFormat nativeFmt = alpha ? GPU_DBG_FORMAT_8888 : GPU_DBG_FORMAT_888_RGB;
w = std::min(w, buf.GetStride());
h = std::min(h, buf.GetHeight());
// The temp buffer will be freed by the caller if set, and can be the return value.
temp = nullptr;
const u8 *buffer = buf.GetData();
if (buf.GetFlipped() && buf.GetFormat() == nativeFmt) {
temp = new u8[pixelSize * w * h];
// Silly OpenGL reads upside down, we flip to another buffer for simplicity.
for (u32 y = 0; y < h; y++) {
memcpy(temp + y * w * pixelSize, buffer + (buf.GetHeight() - y - 1) * buf.GetStride() * pixelSize, w * pixelSize);
}
} else if (buf.GetFormat() < GPU_DBG_FORMAT_FLOAT && buf.GetFormat() != nativeFmt) {
temp = new u8[pixelSize * w * h];
// Let's boil it down to how we need to interpret the bits.
int baseFmt = buf.GetFormat() & ~(GPU_DBG_FORMAT_REVERSE_FLAG | GPU_DBG_FORMAT_BRSWAP_FLAG);
bool rev = (buf.GetFormat() & GPU_DBG_FORMAT_REVERSE_FLAG) != 0;
bool brswap = (buf.GetFormat() & GPU_DBG_FORMAT_BRSWAP_FLAG) != 0;
bool flip = buf.GetFlipped();
// This is pretty inefficient.
for (u32 y = 0; y < h; y++) {
for (u32 x = 0; x < w; x++) {
u8 *dst;
if (flip) {
dst = &temp[(h - y - 1) * w * pixelSize + x * pixelSize];
} else {
dst = &temp[y * w * pixelSize + x * pixelSize];
}
u8 &r = brswap ? dst[2] : dst[0];
u8 &g = dst[1];
u8 &b = brswap ? dst[0] : dst[2];
u8 &a = alpha ? dst[3] : r;
if (!ConvertPixelTo8888RGBA(GPUDebugBufferFormat(baseFmt), r, g, b, a, buffer, y * buf.GetStride() + x, rev)) {
return nullptr;
}
}
}
} else if (buf.GetFormat() != nativeFmt) {
temp = new u8[pixelSize * w * h];
bool flip = buf.GetFlipped();
// This is pretty inefficient.
for (u32 y = 0; y < h; y++) {
for (u32 x = 0; x < w; x++) {
u8 *dst;
if (flip) {
dst = &temp[(h - y - 1) * w * pixelSize + x * pixelSize];
} else {
dst = &temp[y * w * pixelSize + x * pixelSize];
}
u8 &a = alpha ? dst[3] : dst[0];
if (!ConvertPixelTo8888RGBA(buf.GetFormat(), dst[0], dst[1], dst[2], a, buffer, y * buf.GetStride() + x, false)) {
return nullptr;
}
}
}
}
return temp ? temp : buffer;
}
static GPUDebugBuffer ApplyRotation(const GPUDebugBuffer &buf, DisplayRotation rotation) {
GPUDebugBuffer rotated;
// This is a simple but not terribly efficient rotation.
if (rotation == DisplayRotation::ROTATE_90) {
rotated.Allocate(buf.GetHeight(), buf.GetStride(), buf.GetFormat(), false);
for (u32 y = 0; y < buf.GetStride(); ++y) {
for (u32 x = 0; x < buf.GetHeight(); ++x) {
rotated.SetRawPixel(x, y, buf.GetRawPixel(buf.GetStride() - y - 1, x));
}
}
} else if (rotation == DisplayRotation::ROTATE_180) {
rotated.Allocate(buf.GetStride(), buf.GetHeight(), buf.GetFormat(), false);
for (u32 y = 0; y < buf.GetHeight(); ++y) {
for (u32 x = 0; x < buf.GetStride(); ++x) {
rotated.SetRawPixel(x, y, buf.GetRawPixel(buf.GetStride() - x - 1, buf.GetHeight() - y - 1));
}
}
} else {
rotated.Allocate(buf.GetHeight(), buf.GetStride(), buf.GetFormat(), false);
for (u32 y = 0; y < buf.GetStride(); ++y) {
for (u32 x = 0; x < buf.GetHeight(); ++x) {
rotated.SetRawPixel(x, y, buf.GetRawPixel(y, buf.GetHeight() - x - 1));
}
}
}
return rotated;
}
bool TakeGameScreenshot(Draw::DrawContext *draw, const Path &filename, ScreenshotFormat fmt, ScreenshotType type, int *width, int *height, int maxRes) {
GPUDebugBuffer buf;
bool success = false;
u32 w = (u32)-1;
u32 h = (u32)-1;
if (type == SCREENSHOT_DISPLAY || type == SCREENSHOT_RENDER) {
if (!gpuDebug) {
ERROR_LOG(Log::System, "Can't take screenshots when GPU not running");
return false;
}
success = gpuDebug->GetCurrentFramebuffer(buf, type == SCREENSHOT_RENDER ? GPU_DBG_FRAMEBUF_RENDER : GPU_DBG_FRAMEBUF_DISPLAY, maxRes);
w = maxRes > 0 ? 480 * maxRes : PSP_CoreParameter().renderWidth;
h = maxRes > 0 ? 272 * maxRes : PSP_CoreParameter().renderHeight;
} else if (g_display.rotation != DisplayRotation::ROTATE_0) {
_dbg_assert_(draw);
GPUDebugBuffer temp;
success = ::GetOutputFramebuffer(draw, buf);
if (success) {
buf = ApplyRotation(temp, g_display.rotation);
}
} else {
_dbg_assert_(draw);
success = ::GetOutputFramebuffer(draw, buf);
}
if (!success) {
ERROR_LOG(Log::G3D, "Failed to obtain screenshot data.");
return false;
}
if (success) {
u8 *flipbuffer = nullptr;
const u8 *buffer = ConvertBufferToScreenshot(buf, false, flipbuffer, w, h);
success = buffer != nullptr;
if (success) {
if (width)
*width = w;
if (height)
*height = h;
success = Save888RGBScreenshot(filename, fmt, buffer, w, h);
}
delete[] flipbuffer;
}
if (!success) {
ERROR_LOG(Log::IO, "Failed to write screenshot.");
}
return success;
}
bool Save888RGBScreenshot(const Path &filename, ScreenshotFormat fmt, const u8 *bufferRGB888, int w, int h) {
if (fmt == ScreenshotFormat::PNG) {
png_image png;
memset(&png, 0, sizeof(png));
png.version = PNG_IMAGE_VERSION;
png.format = PNG_FORMAT_RGB;
png.width = w;
png.height = h;
bool success = WriteScreenshotToPNG(&png, filename, 0, bufferRGB888, w * 3, nullptr);
png_image_free(&png);
if (png.warning_or_error >= 2) {
ERROR_LOG(Log::IO, "Saving screenshot to PNG produced errors.");
success = false;
}
return success;
} else if (fmt == ScreenshotFormat::JPG) {
jpge::params params;
params.m_quality = 90;
return WriteScreenshotToJPEG(filename, w, h, 3, bufferRGB888, params);
} else {
return false;
}
}
bool Save8888RGBAScreenshot(const Path &filename, const u8 *buffer, int w, int h) {
png_image png{};
png.version = PNG_IMAGE_VERSION;
png.format = PNG_FORMAT_RGBA;
png.width = w;
png.height = h;
bool success = WriteScreenshotToPNG(&png, filename, 0, buffer, w * 4, nullptr);
png_image_free(&png);
if (png.warning_or_error >= 2) {
ERROR_LOG(Log::IO, "Saving screenshot to PNG produced errors.");
success = false;
}
return success;
}
bool Save8888RGBAScreenshot(std::vector<uint8_t> &bufferPNG, const u8 *bufferRGBA8888, int w, int h) {
png_image png{};
png.version = PNG_IMAGE_VERSION;
png.format = PNG_FORMAT_RGBA;
png.width = w;
png.height = h;
png_alloc_size_t allocSize = bufferPNG.size();
int result = png_image_write_to_memory(&png, allocSize == 0 ? nullptr : bufferPNG.data(), &allocSize, 0, bufferRGBA8888, w * 4, nullptr);
bool success = result != 0 && png.warning_or_error <= 1;
if (!success && allocSize != bufferPNG.size()) {
bufferPNG.resize(allocSize);
png.warning_or_error = 0;
result = png_image_write_to_memory(&png, bufferPNG.data(), &allocSize, 0, bufferRGBA8888, w * 4, nullptr);
success = result != 0 && png.warning_or_error <= 1;
}
if (success)
bufferPNG.resize(allocSize);
png_image_free(&png);
if (!success) {
ERROR_LOG(Log::IO, "Buffering screenshot to PNG produced errors.");
bufferPNG.clear();
}
return success;
}