Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 1c3ded39 authored by Leon Scroggins III's avatar Leon Scroggins III
Browse files

Fixes for YuvToJpegEncoder::encode

Bug: 70969260
Test: Existing CTS tests

Call jpeg_destroy_compress after compression (and failure), eliminating
a memory leak.

In addition, use a custom method for handling errors. skjpeg_error_exit
was previously used, but that method expects a skjpeg_error_mgr, which
isn't used here. skjpeg_error_mgr is more complex than necessary; it
allows for multiple methods to set their own jmp_bufs, even if they call
each other. The entire compression here is contained in one method. The
code for handling this single jmp_buf is simple, so no need to share
code.

This is a follow-on to If9a33ed10ea60131906a632a7030e0b69a21f4ea, which
removed skjpeg_error_mgr, but incorrectly left the skjpeg_error_exit.

Change-Id: Ib76e07ae0d29b093d3709f60e427b18e0e32bd9d
parent 04fe7e60
Loading
Loading
Loading
Loading
+19 −5
Original line number Diff line number Diff line
@@ -23,16 +23,28 @@ YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) {
YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) {
}

struct ErrorMgr {
    struct jpeg_error_mgr pub;
    jmp_buf jmp;
};

void error_exit(j_common_ptr cinfo) {
    ErrorMgr* err = (ErrorMgr*) cinfo->err;
    (*cinfo->err->output_message) (cinfo);
    longjmp(err->jmp, 1);
}

bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width,
        int height, int* offsets, int jpegQuality) {
    jpeg_compress_struct    cinfo;
    jpeg_error_mgr          err;
    ErrorMgr                err;
    skjpeg_destination_mgr  sk_wstream(stream);

    cinfo.err = jpeg_std_error(&err);
    err.error_exit = skjpeg_error_exit;
    jmp_buf jmp;
    if (setjmp(jmp)) {
    cinfo.err = jpeg_std_error(&err.pub);
    err.pub.error_exit = error_exit;

    if (setjmp(err.jmp)) {
        jpeg_destroy_compress(&cinfo);
        return false;
    }
    jpeg_create_compress(&cinfo);
@@ -47,6 +59,8 @@ bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width,

    jpeg_finish_compress(&cinfo);

    jpeg_destroy_compress(&cinfo);

    return true;
}