Skip to content

Commit

Permalink
release python GIL during WEBP encode
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeev Tarantov committed Feb 17, 2020
1 parent 8ec548d commit 0d5e800
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/_webp.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@

#endif

void ImagingSectionEnter(ImagingSectionCookie* cookie)
{
*cookie = (PyThreadState *) PyEval_SaveThread();
}

void ImagingSectionLeave(ImagingSectionCookie* cookie)
{
PyEval_RestoreThread((PyThreadState*) *cookie);
}

/* -------------------------------------------------------------------- */
/* WebP Muxer Error Handling */
/* -------------------------------------------------------------------- */
Expand Down Expand Up @@ -555,6 +565,7 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
Py_ssize_t exif_size;
Py_ssize_t xmp_size;
size_t ret_size;
ImagingSectionCookie cookie;

if (!PyArg_ParseTuple(args, "y#iiifss#s#s#",
(char**)&rgb, &size, &width, &height, &lossless, &quality_factor, &mode,
Expand All @@ -567,23 +578,31 @@ PyObject* WebPEncode_wrapper(PyObject* self, PyObject* args)
}
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
if (lossless) {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeLosslessRGBA(rgb, width, height, 4 * width, &output);
ImagingSectionLeave(&cookie);
} else
#endif
{
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeRGBA(rgb, width, height, 4 * width, quality_factor, &output);
ImagingSectionLeave(&cookie);
}
} else if (strcmp(mode, "RGB")==0){
if (size < width * height * 3){
Py_RETURN_NONE;
}
#if WEBP_ENCODER_ABI_VERSION >= 0x0100
if (lossless) {
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeLosslessRGB(rgb, width, height, 3 * width, &output);
ImagingSectionLeave(&cookie);
} else
#endif
{
ImagingSectionEnter(&cookie);
ret_size = WebPEncodeRGB(rgb, width, height, 3 * width, quality_factor, &output);
ImagingSectionLeave(&cookie);
}
} else {
Py_RETURN_NONE;
Expand Down

0 comments on commit 0d5e800

Please sign in to comment.