app: port GimpImageMap to use the new drawable filters

which makes live update more responsive and removes tons of code, but
adds a delay with progress when finally committing the effect.
This commit is contained in:
Michael Natterer 2013-04-12 14:35:27 +02:00
parent 670fa2d670
commit 1e17f0aed1
5 changed files with 102 additions and 418 deletions

View File

@ -33,21 +33,23 @@
#include "config.h"
#include <glib-object.h>
#include <gegl.h>
#include "core-types.h"
#include "gegl/gimp-gegl-nodes.h"
#include "gegl/gimp-gegl-utils.h"
#include "gimpdrawable.h"
#include "gimpdrawable-shadow.h"
#include "gimpdrawable-filter.h"
#include "gimpfilter.h"
#include "gimpimage.h"
#include "gimpimagemap.h"
#include "gimpmarshal.h"
#include "gimppickable.h"
#include "gimpviewable.h"
#include "gimpchannel.h"
#include "gimpprogress.h"
enum
@ -64,21 +66,10 @@ struct _GimpImageMap
GimpDrawable *drawable;
gchar *undo_desc;
GeglBuffer *undo_buffer;
gint undo_offset_x;
gint undo_offset_y;
GimpFilter *filter;
GeglNode *gegl;
GeglNode *input;
GeglNode *translate;
GeglNode *operation;
GeglNode *output;
GeglProcessor *processor;
guint idle_id;
GTimer *timer;
guint64 pixel_count;
GeglNode *translate;
};
@ -98,15 +89,6 @@ static gboolean gimp_image_map_get_pixel_at (GimpPickable *pick
const Babl *format,
gpointer pixel);
static void gimp_image_map_update_undo_buffer
(GimpImageMap *image_map,
const GeglRectangle *rect);
static gboolean gimp_image_map_do (GimpImageMap *image_map);
static void gimp_image_map_data_written (GObject *operation,
const GeglRectangle *extent,
GimpImageMap *image_map);
static void gimp_image_map_stop_idle (GimpImageMap *image_map);
G_DEFINE_TYPE_WITH_CODE (GimpImageMap, gimp_image_map, GIMP_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
@ -148,23 +130,6 @@ gimp_image_map_pickable_iface_init (GimpPickableInterface *iface)
static void
gimp_image_map_init (GimpImageMap *image_map)
{
image_map->drawable = NULL;
image_map->undo_desc = NULL;
image_map->undo_buffer = NULL;
image_map->undo_offset_x = 0;
image_map->undo_offset_y = 0;
image_map->idle_id = 0;
#ifdef GIMP_UNSTABLE
image_map->timer = g_timer_new ();
#else
image_map->timer = NULL;
#endif
image_map->pixel_count = 0;
if (image_map->timer)
g_timer_stop (image_map->timer);
}
static void
@ -189,41 +154,22 @@ gimp_image_map_finalize (GObject *object)
image_map->undo_desc = NULL;
}
if (image_map->undo_buffer)
{
g_object_unref (image_map->undo_buffer);
image_map->undo_buffer = NULL;
}
gimp_image_map_stop_idle (image_map);
if (image_map->gegl)
{
g_object_unref (image_map->gegl);
image_map->gegl = NULL;
image_map->input = NULL;
image_map->translate = NULL;
image_map->output = NULL;
}
if (image_map->operation)
{
g_object_unref (image_map->operation);
image_map->operation = NULL;
}
if (image_map->drawable)
if (image_map->filter)
{
gimp_drawable_free_shadow_buffer (image_map->drawable);
g_object_unref (image_map->drawable);
image_map->drawable = NULL;
g_object_unref (image_map->filter);
image_map->filter = NULL;
}
if (image_map->timer)
if (image_map->drawable)
{
g_timer_destroy (image_map->timer);
image_map->timer = NULL;
g_object_unref (image_map->drawable);
image_map->drawable = NULL;
}
G_OBJECT_CLASS (parent_class)->finalize (object);
@ -258,9 +204,6 @@ gimp_image_map_get_buffer (GimpPickable *pickable)
{
GimpImageMap *image_map = GIMP_IMAGE_MAP (pickable);
if (image_map->undo_buffer)
return image_map->undo_buffer;
return gimp_pickable_get_buffer (GIMP_PICKABLE (image_map->drawable));
}
@ -272,37 +215,9 @@ gimp_image_map_get_pixel_at (GimpPickable *pickable,
gpointer pixel)
{
GimpImageMap *image_map = GIMP_IMAGE_MAP (pickable);
GimpItem *item = GIMP_ITEM (image_map->drawable);
if (x >= 0 && x < gimp_item_get_width (item) &&
y >= 0 && y < gimp_item_get_height (item))
{
/* Check if done damage to original image */
if (image_map->undo_buffer)
{
gint offset_x = image_map->undo_offset_x;
gint offset_y = image_map->undo_offset_y;
gint width = gegl_buffer_get_width (image_map->undo_buffer);
gint height = gegl_buffer_get_height (image_map->undo_buffer);
if (x >= offset_x && x < offset_x + width &&
y >= offset_y && y < offset_y + height)
{
gegl_buffer_sample (image_map->undo_buffer,
x - offset_x, y - offset_y,
NULL, pixel, format,
GEGL_SAMPLER_NEAREST,
GEGL_ABYSS_NONE);
return TRUE;
}
}
return gimp_pickable_get_pixel_at (GIMP_PICKABLE (image_map->drawable),
x, y, format, pixel);
}
return FALSE;
return gimp_pickable_get_pixel_at (GIMP_PICKABLE (image_map->drawable),
x, y, format, pixel);
}
GimpImageMap *
@ -321,8 +236,7 @@ gimp_image_map_new (GimpDrawable *drawable,
image_map->drawable = g_object_ref (drawable);
image_map->undo_desc = g_strdup (undo_desc);
if (operation)
image_map->operation = g_object_ref (operation);
image_map->operation = g_object_ref (operation);
gimp_viewable_preview_freeze (GIMP_VIEWABLE (drawable));
@ -333,15 +247,10 @@ void
gimp_image_map_apply (GimpImageMap *image_map,
const GeglRectangle *visible)
{
GeglBuffer *input_buffer;
GeglBuffer *output_buffer;
GeglRectangle rect;
GeglRectangle rect;
g_return_if_fail (GIMP_IS_IMAGE_MAP (image_map));
/* If we're still working, remove the timer */
gimp_image_map_stop_idle (image_map);
/* Make sure the drawable is still valid */
if (! gimp_item_is_attached (GIMP_ITEM (image_map->drawable)))
return;
@ -352,40 +261,32 @@ gimp_image_map_apply (GimpImageMap *image_map,
&rect.width, &rect.height))
return;
/* If undo buffer don't exist, or change size, (re)allocate */
gimp_image_map_update_undo_buffer (image_map, &rect);
input_buffer = image_map->undo_buffer;
output_buffer = gimp_drawable_get_shadow_buffer (image_map->drawable);
if (! image_map->gegl)
if (! image_map->filter)
{
image_map->gegl = gegl_node_new ();
GimpImage *image = gimp_item_get_image (GIMP_ITEM (image_map->drawable));
GimpChannel *mask = gimp_image_get_mask (image);
GeglNode *filter_node;
GeglNode *filter_output;
GeglNode *input;
GeglNode *output;
GeglNode *apply;
gint offset_x, offset_y;
g_object_set (image_map->gegl,
"dont-cache", TRUE,
NULL);
gimp_item_get_offset (GIMP_ITEM (image_map->drawable),
&offset_x, &offset_y);
image_map->input =
gegl_node_new_child (image_map->gegl,
"operation", "gegl:buffer-source",
NULL);
image_map->filter = gimp_filter_new ("Image Map");
image_map->translate =
gegl_node_new_child (image_map->gegl,
"operation", "gegl:translate",
NULL);
filter_node = gimp_filter_get_node (image_map->filter);
gegl_node_add_child (image_map->gegl, image_map->operation);
gegl_node_add_child (filter_node, image_map->operation);
image_map->output =
gegl_node_new_child (image_map->gegl,
"operation", "gegl:write-buffer",
NULL);
image_map->translate = gegl_node_new_child (filter_node,
"operation", "gegl:translate",
NULL);
g_signal_connect (image_map->output, "computed",
G_CALLBACK (gimp_image_map_data_written),
image_map);
input = gegl_node_get_input_proxy (filter_node, "input");
output = gegl_node_get_output_proxy (filter_node, "output");
if (gegl_node_has_pad (image_map->operation, "input") &&
gegl_node_has_pad (image_map->operation, "output"))
@ -393,11 +294,12 @@ gimp_image_map_apply (GimpImageMap *image_map,
/* if there are input and output pads we probably have a
* filter OP, connect it on both ends.
*/
gegl_node_link_many (image_map->input,
gegl_node_link_many (input,
image_map->translate,
image_map->operation,
image_map->output,
NULL);
filter_output = image_map->operation;
}
else if (gegl_node_has_pad (image_map->operation, "output"))
{
@ -405,128 +307,78 @@ gimp_image_map_apply (GimpImageMap *image_map,
* source OP, blend its result on top of the original
* pixels.
*/
GeglNode *over = gegl_node_new_child (image_map->gegl,
GeglNode *over = gegl_node_new_child (filter_node,
"operation", "gegl:over",
NULL);
gegl_node_link_many (image_map->input,
gegl_node_link_many (input,
image_map->translate,
over,
image_map->output,
NULL);
gegl_node_connect_to (image_map->operation, "output",
over, "aux");
over, "aux");
filter_output = over;
}
else
{
/* otherwise we just construct a silly nop pipleline
*/
gegl_node_link_many (image_map->input,
gegl_node_link_many (input,
image_map->translate,
image_map->output,
NULL);
filter_output = image_map->translate;
}
apply = gimp_gegl_create_apply_node (filter_output,
rect.x,
rect.y,
0, 0, 0, 0,
gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)),
-offset_x, -offset_y,
GIMP_OPACITY_OPAQUE,
GIMP_REPLACE_MODE,
gimp_drawable_get_active_mask (image_map->drawable));
gegl_node_add_child (filter_node, apply);
g_object_unref (apply);
gegl_node_link_many (input, apply, output, NULL);
}
gegl_node_set (image_map->input,
"buffer", input_buffer,
NULL);
if (! gimp_drawable_has_filter (image_map->drawable, image_map->filter))
gimp_drawable_add_filter (image_map->drawable, image_map->filter);
gegl_node_set (image_map->translate,
"x", (gdouble) rect.x,
"y", (gdouble) rect.y,
"x", (gdouble) -rect.x,
"y", (gdouble) -rect.y,
NULL);
gegl_node_set (image_map->output,
"buffer", output_buffer,
NULL);
gimp_drawable_update (image_map->drawable,
rect.x, rect.y,
rect.width, rect.height);
image_map->processor = gegl_node_new_processor (image_map->output, &rect);
if (image_map->timer)
{
image_map->pixel_count = 0;
g_timer_start (image_map->timer);
g_timer_stop (image_map->timer);
}
/* Start the intermittant work procedure */
image_map->idle_id = g_idle_add ((GSourceFunc) gimp_image_map_do, image_map);
g_signal_emit (image_map, image_map_signals[FLUSH], 0);
}
void
gimp_image_map_commit (GimpImageMap *image_map)
gimp_image_map_commit (GimpImageMap *image_map,
GimpProgress *progress)
{
g_return_if_fail (GIMP_IS_IMAGE_MAP (image_map));
g_return_if_fail (progress == NULL || GIMP_IS_PROGRESS (progress));
if (image_map->idle_id)
if (gimp_drawable_has_filter (image_map->drawable, image_map->filter))
{
g_source_remove (image_map->idle_id);
image_map->idle_id = 0;
gimp_drawable_remove_filter (image_map->drawable, image_map->filter);
/* Finish the changes */
while (gimp_image_map_do (image_map));
}
gimp_drawable_merge_filter (image_map->drawable, image_map->filter,
progress,
image_map->undo_desc);
/* Make sure the drawable is still valid */
if (! gimp_item_is_attached (GIMP_ITEM (image_map->drawable)))
return;
/* Register an undo step */
if (image_map->undo_buffer)
{
gint x = image_map->undo_offset_x;
gint y = image_map->undo_offset_y;
gint width = gegl_buffer_get_width (image_map->undo_buffer);
gint height = gegl_buffer_get_height (image_map->undo_buffer);
gimp_drawable_push_undo (image_map->drawable,
image_map->undo_desc,
image_map->undo_buffer,
x, y, width, height);
g_object_unref (image_map->undo_buffer);
image_map->undo_buffer = NULL;
}
}
void
gimp_image_map_clear (GimpImageMap *image_map)
{
g_return_if_fail (GIMP_IS_IMAGE_MAP (image_map));
gimp_image_map_stop_idle (image_map);
/* Make sure the drawable is still valid */
if (! gimp_item_is_attached (GIMP_ITEM (image_map->drawable)))
return;
/* restore the original image */
if (image_map->undo_buffer)
{
if (gegl_buffer_get_format (image_map->undo_buffer) !=
gimp_drawable_get_format (image_map->drawable))
{
g_message ("image depth change, unable to restore original image");
}
else
{
gint x = image_map->undo_offset_x;
gint y = image_map->undo_offset_y;
gint width = gegl_buffer_get_width (image_map->undo_buffer);
gint height = gegl_buffer_get_height (image_map->undo_buffer);
gegl_buffer_copy (image_map->undo_buffer,
GEGL_RECTANGLE (0, 0, width, height),
gimp_drawable_get_buffer (image_map->drawable),
GEGL_RECTANGLE (x, y, width, height));
gimp_drawable_update (image_map->drawable, x, y, width, height);
}
g_object_unref (image_map->undo_buffer);
image_map->undo_buffer = NULL;
g_signal_emit (image_map, image_map_signals[FLUSH], 0);
}
}
@ -535,173 +387,24 @@ gimp_image_map_abort (GimpImageMap *image_map)
{
g_return_if_fail (GIMP_IS_IMAGE_MAP (image_map));
gimp_image_map_stop_idle (image_map);
if (! gimp_item_is_attached (GIMP_ITEM (image_map->drawable)))
return;
gimp_image_map_clear (image_map);
}
/* private functions */
static void
gimp_image_map_update_undo_buffer (GimpImageMap *image_map,
const GeglRectangle *rect)
{
gint undo_offset_x;
gint undo_offset_y;
gint undo_width;
gint undo_height;
if (image_map->undo_buffer)
if (gimp_drawable_has_filter (image_map->drawable, image_map->filter))
{
undo_offset_x = image_map->undo_offset_x;
undo_offset_y = image_map->undo_offset_y;
undo_width = gegl_buffer_get_width (image_map->undo_buffer);
undo_height = gegl_buffer_get_height (image_map->undo_buffer);
}
else
{
undo_offset_x = 0;
undo_offset_y = 0;
undo_width = 0;
undo_height = 0;
}
GeglRectangle rect;
if (! image_map->undo_buffer ||
undo_offset_x != rect->x ||
undo_offset_y != rect->y ||
undo_width != rect->width ||
undo_height != rect->height)
{
/* If either the extents changed or the buffer don't exist,
* allocate new
*/
if (! image_map->undo_buffer ||
undo_width != rect->width ||
undo_height != rect->height)
gimp_drawable_remove_filter (image_map->drawable, image_map->filter);
if (gimp_item_mask_intersect (GIMP_ITEM (image_map->drawable),
&rect.x, &rect.y,
&rect.width, &rect.height))
{
if (image_map->undo_buffer)
g_object_unref (image_map->undo_buffer);
gimp_drawable_update (image_map->drawable,
rect.x, rect.y,
rect.width, rect.height);
image_map->undo_buffer =
gegl_buffer_new (GEGL_RECTANGLE (0, 0,
rect->width, rect->height),
gimp_drawable_get_format (image_map->drawable));
}
/* Copy from the image to the new tiles */
gegl_buffer_copy (gimp_drawable_get_buffer (image_map->drawable),
rect,
image_map->undo_buffer,
GEGL_RECTANGLE (0, 0, 0, 0));
/* Set the offsets */
image_map->undo_offset_x = rect->x;
image_map->undo_offset_y = rect->y;
}
}
static gboolean
gimp_image_map_do (GimpImageMap *image_map)
{
gboolean pending;
if (! gimp_item_is_attached (GIMP_ITEM (image_map->drawable)))
{
image_map->idle_id = 0;
if (image_map->processor)
{
g_object_unref (image_map->processor);
image_map->processor = NULL;
}
return FALSE;
}
if (image_map->timer)
g_timer_continue (image_map->timer);
pending = gegl_processor_work (image_map->processor, NULL);
if (image_map->timer)
g_timer_stop (image_map->timer);
if (! pending)
{
if (image_map->timer)
g_printerr ("%s: %g MPixels/sec\n",
image_map->undo_desc,
(gdouble) image_map->pixel_count /
(1000000.0 *
g_timer_elapsed (image_map->timer, NULL)));
g_object_unref (image_map->processor);
image_map->processor = NULL;
image_map->idle_id = 0;
g_signal_emit (image_map, image_map_signals[FLUSH], 0);
return FALSE;
}
g_signal_emit (image_map, image_map_signals[FLUSH], 0);
return TRUE;
}
static void
gimp_image_map_data_written (GObject *operation,
const GeglRectangle *extent,
GimpImageMap *image_map)
{
GimpImage *image = gimp_item_get_image (GIMP_ITEM (image_map->drawable));
if (! gimp_channel_is_empty (gimp_image_get_mask (image)))
{
/* Reset to initial drawable conditions. */
gegl_buffer_copy (image_map->undo_buffer,
GEGL_RECTANGLE (extent->x - image_map->undo_offset_x,
extent->y - image_map->undo_offset_y,
extent->width, extent->height),
gimp_drawable_get_buffer (image_map->drawable),
GEGL_RECTANGLE (extent->x, extent->y, 0, 0));
}
/* Apply the result of the gegl graph. */
gimp_drawable_apply_buffer (image_map->drawable,
gimp_drawable_get_shadow_buffer (image_map->drawable),
GEGL_RECTANGLE (extent->x, extent->y,
extent->width, extent->height),
FALSE, NULL,
GIMP_OPACITY_OPAQUE, GIMP_REPLACE_MODE,
NULL, extent->x, extent->y);
gimp_drawable_update (image_map->drawable,
extent->x, extent->y,
extent->width, extent->height);
if (image_map->timer)
image_map->pixel_count += extent->width * extent->height;
}
static void
gimp_image_map_stop_idle (GimpImageMap *image_map)
{
if (image_map->idle_id)
{
g_source_remove (image_map->idle_id);
image_map->idle_id = 0;
if (image_map->processor)
{
g_object_unref (image_map->processor);
image_map->processor = NULL;
g_signal_emit (image_map, image_map_signals[FLUSH], 0);
}
}
}

View File

@ -55,8 +55,8 @@ GimpImageMap * gimp_image_map_new (GimpDrawable *drawable,
void gimp_image_map_apply (GimpImageMap *image_map,
const GeglRectangle *visible);
void gimp_image_map_commit (GimpImageMap *image_map);
void gimp_image_map_clear (GimpImageMap *image_map);
void gimp_image_map_commit (GimpImageMap *image_map,
GimpProgress *progress);
void gimp_image_map_abort (GimpImageMap *image_map);

View File

@ -376,8 +376,7 @@ gimp_cage_tool_options_notify (GimpTool *tool,
else
{
/* switch to edit mode */
gimp_image_map_clear (ct->image_map);
gimp_cage_tool_image_map_flush (ct->image_map, tool);
gimp_image_map_abort (ct->image_map);
gimp_tool_pop_status (tool, tool->display);
ct->tool_state = CAGE_STATE_WAIT;
@ -429,7 +428,8 @@ gimp_cage_tool_key_press (GimpTool *tool,
{
gimp_tool_control_push_preserve (tool->control, TRUE);
gimp_image_map_commit (ct->image_map);
gimp_image_map_commit (ct->image_map,
GIMP_PROGRESS (tool));
g_object_unref (ct->image_map);
ct->image_map = NULL;
@ -1255,8 +1255,7 @@ gimp_cage_tool_image_map_flush (GimpImageMap *image_map,
{
GimpImage *image = gimp_display_get_image (tool->display);
gimp_projection_flush_now (gimp_image_get_projection (image));
gimp_display_flush_now (tool->display);
gimp_projection_flush (gimp_image_get_projection (image));
}
static void

View File

@ -47,6 +47,7 @@
#include "core/gimpimagemapconfig.h"
#include "core/gimplist.h"
#include "core/gimppickable.h"
#include "core/gimpprogress.h"
#include "core/gimpprojection.h"
#include "core/gimptoolinfo.h"
@ -460,8 +461,6 @@ gimp_image_map_tool_control (GimpTool *tool,
if (image_map_tool->image_map)
{
GimpImage *image;
gimp_tool_control_push_preserve (tool->control, TRUE);
gimp_image_map_abort (image_map_tool->image_map);
@ -469,20 +468,6 @@ gimp_image_map_tool_control (GimpTool *tool,
image_map_tool->image_map = NULL;
gimp_tool_control_pop_preserve (tool->control);
/* don't call gimp_image_flush() here, because the tool
* might be cancelled from some other place opening an undo
* group, so flushing the image would update menus and
* whatnot while that other operation is running, with
* unforeseeable side effects. Also, flusing the image here
* is not needed because we didn't change anything in the
* image. Instead, make sure manually that the display is
* updated correctly after restoring GimpImageMapTool's
* temporary editing.
*/
image = gimp_display_get_image (tool->display);
gimp_projection_flush_now (gimp_image_get_projection (image));
gimp_display_flush_now (tool->display);
}
tool->drawable = NULL;
@ -552,12 +537,9 @@ gimp_image_map_tool_options_notify (GimpTool *tool,
{
gimp_tool_control_push_preserve (tool->control, TRUE);
gimp_image_map_clear (image_map_tool->image_map);
gimp_image_map_abort (image_map_tool->image_map);
gimp_tool_control_pop_preserve (tool->control);
gimp_image_map_tool_flush (image_map_tool->image_map,
image_map_tool);
}
}
}
@ -685,7 +667,7 @@ gimp_image_map_tool_create_map (GimpImageMapTool *tool)
if (tool->image_map)
{
gimp_image_map_clear (tool->image_map);
gimp_image_map_abort (tool->image_map);
g_object_unref (tool->image_map);
}
@ -707,8 +689,7 @@ gimp_image_map_tool_flush (GimpImageMap *image_map,
GimpTool *tool = GIMP_TOOL (image_map_tool);
GimpImage *image = gimp_display_get_image (tool->display);
gimp_projection_flush_now (gimp_image_get_projection (image));
gimp_display_flush_now (tool->display);
gimp_projection_flush (gimp_image_get_projection (image));
}
static void
@ -737,7 +718,8 @@ gimp_image_map_tool_response (GtkWidget *widget,
if (! options->preview)
gimp_image_map_tool_map (image_map_tool);
gimp_image_map_commit (image_map_tool->image_map);
gimp_image_map_commit (image_map_tool->image_map,
GIMP_PROGRESS (tool));
g_object_unref (image_map_tool->image_map);
image_map_tool->image_map = NULL;

View File

@ -333,7 +333,7 @@ gimp_operation_tool_set_operation (GimpOperationTool *tool,
if (GIMP_IMAGE_MAP_TOOL (tool)->image_map)
{
gimp_image_map_clear (GIMP_IMAGE_MAP_TOOL (tool)->image_map);
gimp_image_map_abort (GIMP_IMAGE_MAP_TOOL (tool)->image_map);
g_object_unref (GIMP_IMAGE_MAP_TOOL (tool)->image_map);
GIMP_IMAGE_MAP_TOOL (tool)->image_map = NULL;
}