Files
spice2x-r3d/src/spice2x/hooks/graphics/jpeg_encoder.cpp
T
bicarusandGitHub 8b2f38307b graphics: rewrite screenshot and api capture image processing (#870)
## Link to GitHub Issue or related Pull Request, if one exists
#0

## Description of change
Significantly speeds up API screen capture and D3D9 screenshots saving.
Two reasons for doing this:

1. We now have a 4K game (GITADORA) and existing capture code was taking
multiple seconds.
2. Renewed user interest on streaming as we have a couple more companion
apps in active development.

**API screen capture (streaming), 1280x720:** 14.3ms -> 6.3ms per frame.
Back buffer copies go to pooled `D3DPOOL_SYSTEMMEM` surfaces via
`GetRenderTargetData` instead of allocating a lockable render target
every frame, and TooJpeg is replaced with libjpeg-turbo (encode 9.8ms ->
3.0ms). MSAA remains unsupported

**Screenshots for GITADORA arena model, across 4 screens with one of
them 4K**: 4068ms -> 124ms. `D3DXSaveSurfaceToFileA` is replaced with
fpng (encode 4043ms -> 76ms) and the screens encode in parallel.
Dropping D3DX also removes the `d3dx9_43.dll` ... `d3dx9_24.dll` probing
loop, so screenshots no longer fail outright on machines with no D3DX9
runtime installed.

Screenshot surfaces are read on the present thread, so no D3D call
reaches another thread for screenshots. This fixes a hang in DDR X2
introduced earlier in the branch: its device has no internal locking,
and reading the surface on a pool thread while the present thread sat
inside `GetRenderTargetData` left the game's own render thread
deadlocked.


## Testing

- **GITADORA** (arena model, D3D9Ex, 4K main plus three subscreens,
windowed) with
`-screenshotsub`: three sets of four screenshots, images verified
correct. Completion
order differs between sets, so the screens really are encoding in
parallel.
- **LovePlus** (KLP, plain D3D9, 768x1360): covers the inline path used
by games whose
  image processing must not leave the present thread. 
- **API screen capture** through a companion app: live video correct
throughout.
- **Print Screen** bound as the screenshot key: the clipboard copy
succeeded on every shot.
- Quitting the game after capturing leaves no `IDirect3DDevice9`
reference count warning,
  so the pooled readback surfaces are released along with the device.
2026-08-18 00:22:45 -07:00

141 lines
3.8 KiB
C++

#include "jpeg_encoder.h"
#include <csetjmp>
#include <cstdio>
#include <jpeglib.h>
namespace jpeg_encoder {
namespace {
constexpr size_t CHUNK_SIZE = 16 * 1024;
// libjpeg writes through a destination manager; this one appends straight into
// the caller's vector so the encoded frame is never copied
struct VectorDestination {
jpeg_destination_mgr mgr {};
std::vector<uint8_t> *out = nullptr;
uint8_t chunk[CHUNK_SIZE] {};
};
void dest_init(j_compress_ptr cinfo) {
auto dest = reinterpret_cast<VectorDestination *>(cinfo->dest);
dest->mgr.next_output_byte = dest->chunk;
dest->mgr.free_in_buffer = CHUNK_SIZE;
}
// libjpeg cannot unwind a C++ exception out of its own frames, so growing the
// output has to fail by value and be turned into an error_exit by the caller
bool dest_append(VectorDestination *dest, size_t size) {
try {
dest->out->insert(dest->out->end(), dest->chunk, dest->chunk + size);
return true;
} catch (...) {
return false;
}
}
boolean dest_empty(j_compress_ptr cinfo) {
auto dest = reinterpret_cast<VectorDestination *>(cinfo->dest);
// returning FALSE would mean suspension to libjpeg, not failure
if (!dest_append(dest, CHUNK_SIZE)) {
(*cinfo->err->error_exit)(reinterpret_cast<j_common_ptr>(cinfo));
}
dest->mgr.next_output_byte = dest->chunk;
dest->mgr.free_in_buffer = CHUNK_SIZE;
return TRUE;
}
void dest_term(j_compress_ptr cinfo) {
auto dest = reinterpret_cast<VectorDestination *>(cinfo->dest);
if (!dest_append(dest, CHUNK_SIZE - dest->mgr.free_in_buffer)) {
(*cinfo->err->error_exit)(reinterpret_cast<j_common_ptr>(cinfo));
}
}
// the default handler calls exit(), which is not an option inside a game process
struct ErrorManager {
jpeg_error_mgr mgr {};
jmp_buf escape {};
};
void on_error(j_common_ptr cinfo) {
longjmp(reinterpret_cast<ErrorManager *>(cinfo->err)->escape, 1);
}
void on_message(j_common_ptr) {
}
} // namespace
bool encode(
std::vector<uint8_t> &out,
const uint8_t *pixels,
int width,
int height,
int quality) {
if (!pixels || width <= 0 || height <= 0) {
return false;
}
if (quality < 1) {
quality = 1;
} else if (quality > 100) {
quality = 100;
}
// zero init is load bearing: the trap below is armed before the struct is
// created, and jpeg_destroy_compress only tolerates that on a zeroed struct
jpeg_compress_struct cinfo {};
ErrorManager err;
VectorDestination dest;
cinfo.err = jpeg_std_error(&err.mgr);
err.mgr.error_exit = on_error;
err.mgr.output_message = on_message;
if (setjmp(err.escape)) {
jpeg_destroy_compress(&cinfo);
return false;
}
jpeg_create_compress(&cinfo);
dest.out = &out;
dest.mgr.init_destination = dest_init;
dest.mgr.empty_output_buffer = dest_empty;
dest.mgr.term_destination = dest_term;
cinfo.dest = &dest.mgr;
cinfo.image_width = static_cast<JDIMENSION>(width);
cinfo.image_height = static_cast<JDIMENSION>(height);
cinfo.in_color_space = JCS_RGB;
cinfo.input_components = 3;
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE);
// 4:2:0, matching what the capture path asked the previous encoder for
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
jpeg_start_compress(&cinfo, TRUE);
const size_t pitch = static_cast<size_t>(width) * 3;
while (cinfo.next_scanline < cinfo.image_height) {
auto row = const_cast<uint8_t *>(pixels + cinfo.next_scanline * pitch);
JSAMPROW rows[1] = { row };
jpeg_write_scanlines(&cinfo, rows, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return true;
}
}