Commit f993de75 authored by Ell's avatar Ell

app: add gimp_drawable_get_sub_preview_async()

... which is an asynchronous version of
gimp_drawable_get_sub_preview().

We currently support async preview generation for drawables whose
buffer isn't backed by a GimpTileHandlerValidate tile handler
(i.e., anything other than group layers), since preview generation
fir such drawables may involve processing the corresponding graph,
which isn't thread-safe.

When the GIMP_NO_ASYNC_DRAWABLE_PREVIEWS environment variable is
defined, all drawable previews are synchronously generated.

(cherry picked from commit d79e3fbd)
parent fac91dda
......@@ -32,8 +32,11 @@
#include "gegl/gimp-babl.h"
#include "gegl/gimp-gegl-loops.h"
#include "gegl/gimptilehandlervalidate.h"
#include "gimp.h"
#include "gimp-parallel.h"
#include "gimpasync.h"
#include "gimpchannel.h"
#include "gimpimage.h"
#include "gimpimage-color-profile.h"
......@@ -43,8 +46,56 @@
#include "gimptempbuf.h"
typedef struct
{
const Babl *format;
GeglBuffer *buffer;
GeglRectangle rect;
gdouble scale;
} SubPreviewData;
/* local function prototypes */
static SubPreviewData * sub_preview_data_new (const Babl *format,
GeglBuffer *buffer,
const GeglRectangle *rect,
gdouble scale);
static void sub_preview_data_free (SubPreviewData *data);
/* private functions */
static SubPreviewData *
sub_preview_data_new (const Babl *format,
GeglBuffer *buffer,
const GeglRectangle *rect,
gdouble scale)
{
SubPreviewData *data = g_slice_new (SubPreviewData);
data->format = format;
data->buffer = g_object_ref (buffer);
data->rect = *rect;
data->scale = scale;
return data;
}
static void
sub_preview_data_free (SubPreviewData *data)
{
g_object_unref (data->buffer);
g_slice_free (SubPreviewData, data);
}
/* public functions */
GimpTempBuf *
gimp_drawable_get_new_preview (GimpViewable *viewable,
GimpContext *context,
......@@ -271,3 +322,102 @@ gimp_drawable_get_sub_pixbuf (GimpDrawable *drawable,
return pixbuf;
}
static void
gimp_drawable_get_sub_preview_async_func (GimpAsync *async,
SubPreviewData *data)
{
GimpTempBuf *preview;
preview = gimp_temp_buf_new (data->rect.width, data->rect.height,
data->format);
gegl_buffer_get (data->buffer, &data->rect, data->scale,
gimp_temp_buf_get_format (preview),
gimp_temp_buf_get_data (preview),
GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
sub_preview_data_free (data);
gimp_async_finish_full (async,
preview,
(GDestroyNotify) gimp_temp_buf_unref);
}
GimpAsync *
gimp_drawable_get_sub_preview_async (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height)
{
GimpItem *item;
GimpImage *image;
GeglBuffer *buffer;
gdouble scale;
gint scaled_x;
gint scaled_y;
static gint no_async_drawable_previews = -1;
g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
g_return_val_if_fail (src_x >= 0, NULL);
g_return_val_if_fail (src_y >= 0, NULL);
g_return_val_if_fail (src_width > 0, NULL);
g_return_val_if_fail (src_height > 0, NULL);
g_return_val_if_fail (dest_width > 0, NULL);
g_return_val_if_fail (dest_height > 0, NULL);
item = GIMP_ITEM (drawable);
g_return_val_if_fail ((src_x + src_width) <= gimp_item_get_width (item), NULL);
g_return_val_if_fail ((src_y + src_height) <= gimp_item_get_height (item), NULL);
image = gimp_item_get_image (item);
if (! image->gimp->config->layer_previews)
return NULL;
buffer = gimp_drawable_get_buffer (drawable);
if (no_async_drawable_previews < 0)
{
no_async_drawable_previews =
(g_getenv ("GIMP_NO_ASYNC_DRAWABLE_PREVIEWS") != NULL);
}
if (no_async_drawable_previews ||
gimp_tile_handler_validate_get_assigned (buffer))
{
GimpAsync *async = gimp_async_new ();
gimp_async_finish_full (async,
gimp_drawable_get_sub_preview (drawable,
src_x,
src_y,
src_width,
src_height,
dest_width,
dest_height),
(GDestroyNotify) gimp_temp_buf_unref);
return async;
}
scale = MIN ((gdouble) dest_width / (gdouble) src_width,
(gdouble) dest_height / (gdouble) src_height);
scaled_x = RINT ((gdouble) src_x * scale);
scaled_y = RINT ((gdouble) src_y * scale);
return gimp_parallel_run_async_full (
+1,
(GimpParallelRunAsyncFunc) gimp_drawable_get_sub_preview_async_func,
sub_preview_data_new (
gimp_drawable_get_preview_format (drawable),
buffer,
GEGL_RECTANGLE (scaled_x, scaled_y, dest_width, dest_height),
scale),
(GDestroyNotify) sub_preview_data_free);
}
......@@ -22,34 +22,42 @@
/*
* virtual functions of GimpDrawable -- don't call directly
*/
GimpTempBuf * gimp_drawable_get_new_preview (GimpViewable *viewable,
GimpContext *context,
gint width,
gint height);
GdkPixbuf * gimp_drawable_get_new_pixbuf (GimpViewable *viewable,
GimpContext *context,
gint width,
gint height);
GimpTempBuf * gimp_drawable_get_new_preview (GimpViewable *viewable,
GimpContext *context,
gint width,
gint height);
GdkPixbuf * gimp_drawable_get_new_pixbuf (GimpViewable *viewable,
GimpContext *context,
gint width,
gint height);
/*
* normal functions (no virtuals)
*/
const Babl * gimp_drawable_get_preview_format (GimpDrawable *drawable);
GimpTempBuf * gimp_drawable_get_sub_preview (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
GdkPixbuf * gimp_drawable_get_sub_pixbuf (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
const Babl * gimp_drawable_get_preview_format (GimpDrawable *drawable);
GimpTempBuf * gimp_drawable_get_sub_preview (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
GdkPixbuf * gimp_drawable_get_sub_pixbuf (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
GimpAsync * gimp_drawable_get_sub_preview_async (GimpDrawable *drawable,
gint src_x,
gint src_y,
gint src_width,
gint src_height,
gint dest_width,
gint dest_height);
#endif /* __GIMP_DRAWABLE__PREVIEW_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment