Commit 0765d888 authored by Ell's avatar Ell

app: render drawable previews asynchronously

In GimpViewRendererDrawable, use
gimp_drawable_get_sub_preview_async(), added in the previous
commit, to render drawable previews asynchronously.  While the
preview is being rendered, either keep showing the previous
preview, or render a placeholder icon.

This commit also fixes an issue where, under certain conditions, a
drawable preview would be rendered, even when layer/channel
previews are disabled in the preferences.

(cherry picked from commit 8a81bfd2)
parent f993de75
......@@ -28,6 +28,11 @@
#include "widgets-types.h"
#include "config/gimpcoreconfig.h"
#include "core/gimp.h"
#include "core/gimpasync.h"
#include "core/gimpcancelable.h"
#include "core/gimpdrawable.h"
#include "core/gimpdrawable-preview.h"
#include "core/gimpimage.h"
......@@ -36,8 +41,28 @@
#include "gimpviewrendererdrawable.h"
static void gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
GtkWidget *widget);
struct _GimpViewRendererDrawablePrivate
{
GimpAsync *render_async;
GtkWidget *render_widget;
gint render_buf_x;
gint render_buf_y;
gboolean render_update;
gint prev_width;
gint prev_height;
};
/* local function prototypes */
static void gimp_view_renderer_drawable_dispose (GObject *object);
static void gimp_view_renderer_drawable_invalidate (GimpViewRenderer *renderer);
static void gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
GtkWidget *widget);
static void gimp_view_renderer_drawable_cancel_render (GimpViewRendererDrawable *renderdrawable);
G_DEFINE_TYPE (GimpViewRendererDrawable, gimp_view_renderer_drawable,
......@@ -46,40 +71,129 @@ G_DEFINE_TYPE (GimpViewRendererDrawable, gimp_view_renderer_drawable,
#define parent_class gimp_view_renderer_drawable_parent_class
/* private functions */
static void
gimp_view_renderer_drawable_class_init (GimpViewRendererDrawableClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpViewRendererClass *renderer_class = GIMP_VIEW_RENDERER_CLASS (klass);
renderer_class->render = gimp_view_renderer_drawable_render;
object_class->dispose = gimp_view_renderer_drawable_dispose;
renderer_class->invalidate = gimp_view_renderer_drawable_invalidate;
renderer_class->render = gimp_view_renderer_drawable_render;
g_type_class_add_private (klass, sizeof (GimpViewRendererDrawablePrivate));
}
static void
gimp_view_renderer_drawable_init (GimpViewRendererDrawable *renderdrawable)
{
renderdrawable->priv =
G_TYPE_INSTANCE_GET_PRIVATE (renderdrawable,
GIMP_TYPE_VIEW_RENDERER_DRAWABLE,
GimpViewRendererDrawablePrivate);
}
static void
gimp_view_renderer_drawable_dispose (GObject *object)
{
GimpViewRendererDrawable *renderdrawable = GIMP_VIEW_RENDERER_DRAWABLE (object);
gimp_view_renderer_drawable_cancel_render (renderdrawable);
G_OBJECT_CLASS (parent_class)->dispose (object);
}
static void
gimp_view_renderer_drawable_invalidate (GimpViewRenderer *renderer)
{
GimpViewRendererDrawable *renderdrawable = GIMP_VIEW_RENDERER_DRAWABLE (renderer);
gimp_view_renderer_drawable_cancel_render (renderdrawable);
GIMP_VIEW_RENDERER_CLASS (parent_class)->invalidate (renderer);
}
static void
gimp_view_renderer_drawable_init (GimpViewRendererDrawable *renderer)
gimp_view_renderer_drawable_render_async_callback (GimpAsync *async,
GimpViewRendererDrawable *renderdrawable)
{
GtkWidget *widget;
/* rendering was canceled, and the view renderer is potentially dead (see
* gimp_view_renderer_drawable_cancel_render()). bail.
*/
if (gimp_async_is_canceled (async))
return;
widget = renderdrawable->priv->render_widget;
renderdrawable->priv->render_async = NULL;
renderdrawable->priv->render_widget = NULL;
if (gimp_async_is_finished (async))
{
GimpViewRenderer *renderer = GIMP_VIEW_RENDERER (renderdrawable);
GimpTempBuf *render_buf = gimp_async_get_result (async);
gimp_view_renderer_render_temp_buf (
renderer,
widget,
render_buf,
renderdrawable->priv->render_buf_x,
renderdrawable->priv->render_buf_y,
-1,
GIMP_VIEW_BG_CHECKS,
GIMP_VIEW_BG_CHECKS);
if (renderdrawable->priv->render_update)
gimp_view_renderer_update (renderer);
}
g_object_unref (widget);
}
static void
gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
GtkWidget *widget)
{
GimpDrawable *drawable;
GimpItem *item;
GimpImage *image;
gint offset_x;
gint offset_y;
gint width;
gint height;
gint view_width;
gint view_height;
gdouble xres = 1.0;
gdouble yres = 1.0;
gboolean scaling_up;
GimpTempBuf *render_buf = NULL;
drawable = GIMP_DRAWABLE (renderer->viewable);
item = GIMP_ITEM (drawable);
image = gimp_item_get_image (item);
GimpViewRendererDrawable *renderdrawable = GIMP_VIEW_RENDERER_DRAWABLE (renderer);
GimpDrawable *drawable;
GimpItem *item;
GimpImage *image;
const gchar *icon_name;
GimpAsync *async;
gint offset_x;
gint offset_y;
gint width;
gint height;
gint view_width;
gint view_height;
gdouble xres = 1.0;
gdouble yres = 1.0;
gboolean scaling_up;
/* render is already in progress */
if (renderdrawable->priv->render_async)
return;
drawable = GIMP_DRAWABLE (renderer->viewable);
item = GIMP_ITEM (drawable);
image = gimp_item_get_image (item);
icon_name = gimp_viewable_get_icon_name (renderer->viewable);
if (image && ! image->gimp->config->layer_previews)
{
renderdrawable->priv->prev_width = 0;
renderdrawable->priv->prev_height = 0;
gimp_view_renderer_render_icon (renderer, widget, icon_name);
return;
}
gimp_item_get_offset (item, &offset_x, &offset_y);
......@@ -127,74 +241,63 @@ gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
(gimp_item_get_width (item) * gimp_item_get_height (item) * 4))
scaling_up = FALSE;
if (scaling_up)
if (scaling_up && image && ! renderer->is_popup)
{
if (image && ! renderer->is_popup)
gint src_x, src_y;
gint src_width, src_height;
if (gimp_rectangle_intersect (0, 0,
gimp_item_get_width (item),
gimp_item_get_height (item),
-offset_x, -offset_y,
gimp_image_get_width (image),
gimp_image_get_height (image),
&src_x, &src_y,
&src_width, &src_height))
{
gint src_x, src_y;
gint src_width, src_height;
if (gimp_rectangle_intersect (0, 0,
gimp_item_get_width (item),
gimp_item_get_height (item),
-offset_x, -offset_y,
gimp_image_get_width (image),
gimp_image_get_height (image),
&src_x, &src_y,
&src_width, &src_height))
{
gint dest_width;
gint dest_height;
dest_width = ROUND (((gdouble) renderer->width /
(gdouble) gimp_image_get_width (image)) *
(gdouble) src_width);
dest_height = ROUND (((gdouble) renderer->height /
(gdouble) gimp_image_get_height (image)) *
(gdouble) src_height);
if (dest_width < 1) dest_width = 1;
if (dest_height < 1) dest_height = 1;
render_buf = gimp_drawable_get_sub_preview (drawable,
src_x, src_y,
src_width, src_height,
dest_width, dest_height);
}
else
{
const Babl *format = gimp_drawable_get_preview_format (drawable);
gint dest_width;
gint dest_height;
render_buf = gimp_temp_buf_new (1, 1, format);
gimp_temp_buf_data_clear (render_buf);
}
dest_width = ROUND (((gdouble) renderer->width /
(gdouble) gimp_image_get_width (image)) *
(gdouble) src_width);
dest_height = ROUND (((gdouble) renderer->height /
(gdouble) gimp_image_get_height (image)) *
(gdouble) src_height);
if (dest_width < 1) dest_width = 1;
if (dest_height < 1) dest_height = 1;
async = gimp_drawable_get_sub_preview_async (drawable,
src_x, src_y,
src_width, src_height,
dest_width, dest_height);
}
else
{
GimpTempBuf *temp_buf;
const Babl *format = gimp_drawable_get_preview_format (drawable);
GimpTempBuf *render_buf;
temp_buf = gimp_viewable_get_new_preview (renderer->viewable,
renderer->context,
gimp_item_get_width (item),
gimp_item_get_height (item));
render_buf = gimp_temp_buf_new (1, 1, format);
gimp_temp_buf_data_clear (render_buf);
if (temp_buf)
{
render_buf = gimp_temp_buf_scale (temp_buf,
view_width, view_height);
gimp_temp_buf_unref (temp_buf);
}
async = gimp_async_new ();
gimp_async_finish_full (async,
render_buf,
(GDestroyNotify) gimp_temp_buf_unref);
}
}
else
{
render_buf = gimp_viewable_get_new_preview (renderer->viewable,
renderer->context,
view_width,
view_height);
async = gimp_drawable_get_sub_preview_async (drawable,
0, 0,
gimp_item_get_width (item),
gimp_item_get_height (item),
view_width, view_height);
}
if (render_buf)
if (async)
{
gint render_buf_x = 0;
gint render_buf_y = 0;
......@@ -228,19 +331,58 @@ gimp_view_renderer_drawable_render (GimpViewRenderer *renderer,
render_buf_y = (height - view_height) / 2;
}
gimp_view_renderer_render_temp_buf (renderer, widget, render_buf,
render_buf_x, render_buf_y,
-1,
GIMP_VIEW_BG_CHECKS,
GIMP_VIEW_BG_CHECKS);
gimp_temp_buf_unref (render_buf);
renderdrawable->priv->render_async = async;
renderdrawable->priv->render_widget = g_object_ref (widget);
renderdrawable->priv->render_buf_x = 0;
renderdrawable->priv->render_buf_y = 0;
renderdrawable->priv->render_update = FALSE;
gimp_async_add_callback (
async,
(GimpAsyncCallback) gimp_view_renderer_drawable_render_async_callback,
renderdrawable);
/* if rendering isn't done yet, update the render-view once it is, and
* either keep the old drawable preview for now, or, if size changed (or
* there's no old preview,) render an icon in the meantime.
*/
if (renderdrawable->priv->render_async)
{
renderdrawable->priv->render_update = TRUE;
if (view_width != renderdrawable->priv->prev_width ||
view_height != renderdrawable->priv->prev_height)
{
gimp_view_renderer_render_icon (renderer, widget, icon_name);
}
}
renderdrawable->priv->prev_width = view_width;
renderdrawable->priv->prev_height = view_height;
g_object_unref (async);
}
else
{
const gchar *icon_name;
icon_name = gimp_viewable_get_icon_name (renderer->viewable);
renderdrawable->priv->prev_width = 0;
renderdrawable->priv->prev_height = 0;
gimp_view_renderer_render_icon (renderer, widget, icon_name);
}
}
static void
gimp_view_renderer_drawable_cancel_render (GimpViewRendererDrawable *renderdrawable)
{
/* cancel the async render operation (if one is ongoing) without actually
* waiting for it. if the actual rendering hasn't started yet, it will be
* immediately aborted; otherwise, it can't really be interrupted, so we just
* let it go on without blocking the main thread.
* gimp_drawable_get_sub_preview_async() can continue rendering safely even
* after the drawable had died, and our completion callback is prepared to
* handle cancelation.
*/
g_clear_pointer (&renderdrawable->priv->render_async, gimp_cancelable_cancel);
g_clear_object (&renderdrawable->priv->render_widget);
}
......@@ -31,11 +31,14 @@
#define GIMP_VIEW_RENDERER_DRAWABLE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_VIEW_RENDERER_DRAWABLE, GimpViewRendererDrawableClass))
typedef struct _GimpViewRendererDrawableClass GimpViewRendererDrawableClass;
typedef struct _GimpViewRendererDrawablePrivate GimpViewRendererDrawablePrivate;
typedef struct _GimpViewRendererDrawableClass GimpViewRendererDrawableClass;
struct _GimpViewRendererDrawable
{
GimpViewRenderer parent_instance;
GimpViewRenderer parent_instance;
GimpViewRendererDrawablePrivate *priv;
};
struct _GimpViewRendererDrawableClass
......
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