gimpgrouplayer.c 77.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * GimpGroupLayer
 * Copyright (C) 2009  Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22 23 24
 */

#include "config.h"

#include <string.h>

25
#include <gdk-pixbuf/gdk-pixbuf.h>
26 27
#include <gegl.h>

28
#include "libgimpbase/gimpbase.h"
29
#include "libgimpmath/gimpmath.h"
30

31 32
#include "core-types.h"

33
#include "gegl/gimp-babl.h"
34
#include "gegl/gimp-gegl-loops.h"
35

36
#include "gimpgrouplayer.h"
37
#include "gimpgrouplayerundo.h"
38
#include "gimpimage.h"
39
#include "gimpimage-undo.h"
40
#include "gimpimage-undo-push.h"
Ell's avatar
Ell committed
41
#include "gimplayerstack.h"
42
#include "gimpobjectqueue.h"
43
#include "gimppickable.h"
44
#include "gimpprogress.h"
45 46
#include "gimpprojectable.h"
#include "gimpprojection.h"
47 48 49 50

#include "gimp-intl.h"


51 52 53 54 55 56
typedef struct _GimpGroupLayerPrivate GimpGroupLayerPrivate;

struct _GimpGroupLayerPrivate
{
  GimpContainer  *children;
  GimpProjection *projection;
57 58
  GeglNode       *source_node;
  GeglNode       *parent_source_node;
59 60 61
  GeglNode       *graph;
  GeglNode       *offset_node;
  gint            suspend_resize;
62 63 64
  gint            suspend_mask;
  GeglBuffer     *suspended_mask_buffer;
  GeglRectangle   suspended_mask_bounds;
65
  gint            direct_update;
66
  gint            transforming;
67
  gboolean        expanded;
68
  gboolean        pass_through;
69 70

  /*  hackish temp states to make the projection/tiles stuff work  */
71
  const Babl     *convert_format;
72 73 74
  gboolean        reallocate_projection;
  gint            reallocate_width;
  gint            reallocate_height;
75 76
};

77
#define GET_PRIVATE(item) ((GimpGroupLayerPrivate *) gimp_group_layer_get_instance_private ((GimpGroupLayer *) (item)))
78 79


80
static void            gimp_projectable_iface_init   (GimpProjectableInterface  *iface);
81
static void            gimp_pickable_iface_init      (GimpPickableInterface     *iface);
82

83
static void            gimp_group_layer_finalize     (GObject         *object);
84 85 86 87 88 89 90 91 92 93 94 95
static void            gimp_group_layer_set_property (GObject         *object,
                                                      guint            property_id,
                                                      const GValue    *value,
                                                      GParamSpec      *pspec);
static void            gimp_group_layer_get_property (GObject         *object,
                                                      guint            property_id,
                                                      GValue          *value,
                                                      GParamSpec      *pspec);

static gint64          gimp_group_layer_get_memsize  (GimpObject      *object,
                                                      gint64          *gui_size);

96
static void        gimp_group_layer_ancestry_changed (GimpViewable    *viewable);
97 98 99
static gboolean        gimp_group_layer_get_size     (GimpViewable    *viewable,
                                                      gint            *width,
                                                      gint            *height);
100
static GimpContainer * gimp_group_layer_get_children (GimpViewable    *viewable);
101 102 103
static gboolean        gimp_group_layer_get_expanded (GimpViewable    *viewable);
static void            gimp_group_layer_set_expanded (GimpViewable    *viewable,
                                                      gboolean         expanded);
104

105
static gboolean  gimp_group_layer_is_position_locked (GimpItem        *item);
106 107
static GimpItem      * gimp_group_layer_duplicate    (GimpItem        *item,
                                                      GType            new_type);
108
static void            gimp_group_layer_convert      (GimpItem        *item,
109 110
                                                      GimpImage       *dest_image,
                                                      GType            old_type);
111
static void         gimp_group_layer_start_transform (GimpItem        *item,
112
                                                      gboolean         push_undo);
113
static void           gimp_group_layer_end_transform (GimpItem        *item,
114
                                                      gboolean         push_undo);
115 116 117 118 119 120 121
static void            gimp_group_layer_resize       (GimpItem        *item,
                                                      GimpContext     *context,
                                                      GimpFillType     fill_type,
                                                      gint             new_width,
                                                      gint             new_height,
                                                      gint             offset_x,
                                                      gint             offset_y);
122 123 124 125 126 127 128

static gint64      gimp_group_layer_estimate_memsize (GimpDrawable      *drawable,
                                                      GimpComponentType  component_type,
                                                      gint               width,
                                                      gint               height);

static void            gimp_group_layer_translate    (GimpLayer       *layer,
129
                                                      gint             offset_x,
130 131
                                                      gint             offset_y);
static void            gimp_group_layer_scale        (GimpLayer       *layer,
132 133 134 135 136 137
                                                      gint             new_width,
                                                      gint             new_height,
                                                      gint             new_offset_x,
                                                      gint             new_offset_y,
                                                      GimpInterpolationType  interp_type,
                                                      GimpProgress    *progress);
138
static void            gimp_group_layer_flip         (GimpLayer       *layer,
139 140 141 142
                                                      GimpContext     *context,
                                                      GimpOrientationType flip_type,
                                                      gdouble          axis,
                                                      gboolean         clip_result);
143
static void            gimp_group_layer_rotate       (GimpLayer       *layer,
144 145 146 147 148
                                                      GimpContext     *context,
                                                      GimpRotationType rotate_type,
                                                      gdouble          center_x,
                                                      gdouble          center_y,
                                                      gboolean         clip_result);
149
static void            gimp_group_layer_transform    (GimpLayer       *layer,
150 151 152 153 154
                                                      GimpContext     *context,
                                                      const GimpMatrix3 *matrix,
                                                      GimpTransformDirection direction,
                                                      GimpInterpolationType  interpolation_type,
                                                      GimpTransformResize clip_result,
155 156
                                                      GimpProgress      *progress);
static void            gimp_group_layer_convert_type (GimpLayer         *layer,
157
                                                      GimpImage         *dest_image,
158
                                                      const Babl        *new_format,
159
                                                      GimpColorProfile  *src_profile,
160
                                                      GimpColorProfile  *dest_profile,
161 162
                                                      GeglDitherMethod   layer_dither_type,
                                                      GeglDitherMethod   mask_dither_type,
163 164
                                                      gboolean           push_undo,
                                                      GimpProgress      *progress);
165 166
static GeglNode   * gimp_group_layer_get_source_node (GimpDrawable      *drawable);

167 168
static void         gimp_group_layer_opacity_changed (GimpLayer         *layer);
static void  gimp_group_layer_effective_mode_changed (GimpLayer         *layer);
169 170
static void
          gimp_group_layer_excludes_backdrop_changed (GimpLayer         *layer);
171 172 173 174 175 176
static void            gimp_group_layer_mask_changed (GimpLayer         *layer);
static void      gimp_group_layer_get_effective_mode (GimpLayer         *layer,
                                                      GimpLayerMode          *mode,
                                                      GimpLayerColorSpace    *blend_space,
                                                      GimpLayerColorSpace    *composite_space,
                                                      GimpLayerCompositeMode *composite_mode);
177 178
static gboolean
              gimp_group_layer_get_excludes_backdrop (GimpLayer         *layer);
179

180
static const Babl    * gimp_group_layer_get_format   (GimpProjectable *projectable);
181
static GeglNode      * gimp_group_layer_get_graph    (GimpProjectable *projectable);
182 183 184
static void            gimp_group_layer_begin_render (GimpProjectable *projectable);
static void            gimp_group_layer_end_render   (GimpProjectable *projectable);

185
static gdouble       gimp_group_layer_get_opacity_at (GimpPickable    *pickable,
186 187 188
                                                      gint             x,
                                                      gint             y);

189 190 191 192 193 194 195 196 197 198 199 200

static void            gimp_group_layer_child_add    (GimpContainer   *container,
                                                      GimpLayer       *child,
                                                      GimpGroupLayer  *group);
static void            gimp_group_layer_child_remove (GimpContainer   *container,
                                                      GimpLayer       *child,
                                                      GimpGroupLayer  *group);
static void            gimp_group_layer_child_move   (GimpLayer       *child,
                                                      GParamSpec      *pspec,
                                                      GimpGroupLayer  *group);
static void            gimp_group_layer_child_resize (GimpLayer       *child,
                                                      GimpGroupLayer  *group);
201
static void    gimp_group_layer_child_active_changed (GimpLayer       *child,
202
                                                      GimpGroupLayer  *group);
203 204 205
static void
       gimp_group_layer_child_effective_mode_changed (GimpLayer       *child,
                                                      GimpGroupLayer  *group);
206 207 208
static void
    gimp_group_layer_child_excludes_backdrop_changed (GimpLayer       *child,
                                                      GimpGroupLayer  *group);
209

210
static void            gimp_group_layer_flush        (GimpGroupLayer  *group);
211 212
static void            gimp_group_layer_update       (GimpGroupLayer  *group);
static void            gimp_group_layer_update_size  (GimpGroupLayer  *group);
213
static void        gimp_group_layer_update_mask_size (GimpGroupLayer  *group);
214
static void      gimp_group_layer_update_source_node (GimpGroupLayer  *group);
215
static void        gimp_group_layer_update_mode_node (GimpGroupLayer  *group);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

static void            gimp_group_layer_stack_update (GimpDrawableStack *stack,
                                                      gint               x,
                                                      gint               y,
                                                      gint               width,
                                                      gint               height,
                                                      GimpGroupLayer    *group);
static void            gimp_group_layer_proj_update  (GimpProjection    *proj,
                                                      gboolean           now,
                                                      gint               x,
                                                      gint               y,
                                                      gint               width,
                                                      gint               height,
                                                      GimpGroupLayer    *group);


G_DEFINE_TYPE_WITH_CODE (GimpGroupLayer, gimp_group_layer, GIMP_TYPE_LAYER,
233
                         G_ADD_PRIVATE (GimpGroupLayer)
234
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PROJECTABLE,
235 236 237 238
                                                gimp_projectable_iface_init)
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_PICKABLE,
                                                gimp_pickable_iface_init))

239 240 241 242

#define parent_class gimp_group_layer_parent_class


243 244 245 246 247 248
/* disable pass-through groups strength-reduction to normal groups.
 * see gimp_group_layer_get_effective_mode().
 */
static gboolean no_pass_through_strength_reduction = FALSE;


249 250 251 252 253 254 255
static void
gimp_group_layer_class_init (GimpGroupLayerClass *klass)
{
  GObjectClass      *object_class      = G_OBJECT_CLASS (klass);
  GimpObjectClass   *gimp_object_class = GIMP_OBJECT_CLASS (klass);
  GimpViewableClass *viewable_class    = GIMP_VIEWABLE_CLASS (klass);
  GimpItemClass     *item_class        = GIMP_ITEM_CLASS (klass);
256
  GimpDrawableClass *drawable_class    = GIMP_DRAWABLE_CLASS (klass);
257
  GimpLayerClass    *layer_class       = GIMP_LAYER_CLASS (klass);
258

259 260 261 262 263 264 265
  object_class->set_property             = gimp_group_layer_set_property;
  object_class->get_property             = gimp_group_layer_get_property;
  object_class->finalize                 = gimp_group_layer_finalize;

  gimp_object_class->get_memsize         = gimp_group_layer_get_memsize;

  viewable_class->default_icon_name      = "gimp-group-layer";
266
  viewable_class->ancestry_changed       = gimp_group_layer_ancestry_changed;
267 268 269 270 271 272 273 274
  viewable_class->get_size               = gimp_group_layer_get_size;
  viewable_class->get_children           = gimp_group_layer_get_children;
  viewable_class->set_expanded           = gimp_group_layer_set_expanded;
  viewable_class->get_expanded           = gimp_group_layer_get_expanded;

  item_class->is_position_locked         = gimp_group_layer_is_position_locked;
  item_class->duplicate                  = gimp_group_layer_duplicate;
  item_class->convert                    = gimp_group_layer_convert;
275 276
  item_class->start_transform            = gimp_group_layer_start_transform;
  item_class->end_transform              = gimp_group_layer_end_transform;
277
  item_class->resize                     = gimp_group_layer_resize;
278 279 280 281 282 283 284 285 286 287 288 289 290

  item_class->default_name               = _("Layer Group");
  item_class->rename_desc                = C_("undo-type", "Rename Layer Group");
  item_class->translate_desc             = C_("undo-type", "Move Layer Group");
  item_class->scale_desc                 = C_("undo-type", "Scale Layer Group");
  item_class->resize_desc                = C_("undo-type", "Resize Layer Group");
  item_class->flip_desc                  = C_("undo-type", "Flip Layer Group");
  item_class->rotate_desc                = C_("undo-type", "Rotate Layer Group");
  item_class->transform_desc             = C_("undo-type", "Transform Layer Group");

  drawable_class->estimate_memsize       = gimp_group_layer_estimate_memsize;
  drawable_class->get_source_node        = gimp_group_layer_get_source_node;

291 292
  layer_class->opacity_changed           = gimp_group_layer_opacity_changed;
  layer_class->effective_mode_changed    = gimp_group_layer_effective_mode_changed;
293
  layer_class->excludes_backdrop_changed = gimp_group_layer_excludes_backdrop_changed;
294
  layer_class->mask_changed              = gimp_group_layer_mask_changed;
295 296 297 298 299 300
  layer_class->translate                 = gimp_group_layer_translate;
  layer_class->scale                     = gimp_group_layer_scale;
  layer_class->flip                      = gimp_group_layer_flip;
  layer_class->rotate                    = gimp_group_layer_rotate;
  layer_class->transform                 = gimp_group_layer_transform;
  layer_class->convert_type              = gimp_group_layer_convert_type;
301
  layer_class->get_effective_mode        = gimp_group_layer_get_effective_mode;
302
  layer_class->get_excludes_backdrop     = gimp_group_layer_get_excludes_backdrop;
303

304 305
  if (g_getenv ("GIMP_NO_PASS_THROUGH_STRENGTH_REDUCTION"))
    no_pass_through_strength_reduction = TRUE;
306 307
}

308 309 310 311
static void
gimp_projectable_iface_init (GimpProjectableInterface *iface)
{
  iface->get_image          = (GimpImage * (*) (GimpProjectable *)) gimp_item_get_image;
312
  iface->get_format         = gimp_group_layer_get_format;
313 314 315
  iface->get_offset         = (void (*) (GimpProjectable*, gint*, gint*)) gimp_item_get_offset;
  iface->get_size           = (void (*) (GimpProjectable*, gint*, gint*)) gimp_viewable_get_size;
  iface->get_graph          = gimp_group_layer_get_graph;
316 317
  iface->begin_render       = gimp_group_layer_begin_render;
  iface->end_render         = gimp_group_layer_end_render;
318 319 320
  iface->invalidate_preview = (void (*) (GimpProjectable*)) gimp_viewable_invalidate_preview;
}

321 322 323 324 325 326
static void
gimp_pickable_iface_init (GimpPickableInterface *iface)
{
  iface->get_opacity_at = gimp_group_layer_get_opacity_at;
}

327
static void
328
gimp_group_layer_init (GimpGroupLayer *group)
329
{
330
  GimpGroupLayerPrivate *private = GET_PRIVATE (group);
331

Ell's avatar
Ell committed
332
  private->children = gimp_layer_stack_new (GIMP_TYPE_LAYER);
333
  private->expanded = TRUE;
334 335

  g_signal_connect (private->children, "add",
336
                    G_CALLBACK (gimp_group_layer_child_add),
337
                    group);
338
  g_signal_connect (private->children, "remove",
339
                    G_CALLBACK (gimp_group_layer_child_remove),
340
                    group);
341

342
  gimp_container_add_handler (private->children, "notify::offset-x",
343
                              G_CALLBACK (gimp_group_layer_child_move),
344
                              group);
345
  gimp_container_add_handler (private->children, "notify::offset-y",
346
                              G_CALLBACK (gimp_group_layer_child_move),
347
                              group);
348
  gimp_container_add_handler (private->children, "size-changed",
349
                              G_CALLBACK (gimp_group_layer_child_resize),
350
                              group);
351 352
  gimp_container_add_handler (private->children, "active-changed",
                              G_CALLBACK (gimp_group_layer_child_active_changed),
353
                              group);
354 355 356
  gimp_container_add_handler (private->children, "effective-mode-changed",
                              G_CALLBACK (gimp_group_layer_child_effective_mode_changed),
                              group);
357 358 359
  gimp_container_add_handler (private->children, "excludes-backdrop-changed",
                              G_CALLBACK (gimp_group_layer_child_excludes_backdrop_changed),
                              group);
360

361
  g_signal_connect (private->children, "update",
362 363 364
                    G_CALLBACK (gimp_group_layer_stack_update),
                    group);

365
  private->projection = gimp_projection_new (GIMP_PROJECTABLE (group));
366
  gimp_projection_set_priority (private->projection, 1);
367

368
  g_signal_connect (private->projection, "update",
369 370
                    G_CALLBACK (gimp_group_layer_proj_update),
                    group);
371 372
}

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
static void
gimp_group_layer_finalize (GObject *object)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (object);

  if (private->children)
    {
      g_signal_handlers_disconnect_by_func (private->children,
                                            gimp_group_layer_child_add,
                                            object);
      g_signal_handlers_disconnect_by_func (private->children,
                                            gimp_group_layer_child_remove,
                                            object);
      g_signal_handlers_disconnect_by_func (private->children,
                                            gimp_group_layer_stack_update,
                                            object);

390
      g_clear_object (&private->children);
391 392
    }

393
  g_clear_object (&private->projection);
394
  g_clear_object (&private->source_node);
395
  g_clear_object (&private->graph);
396 397 398 399

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
static void
gimp_group_layer_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_group_layer_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  switch (property_id)
    {
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

428 429 430 431
static gint64
gimp_group_layer_get_memsize (GimpObject *object,
                              gint64     *gui_size)
{
432 433
  GimpGroupLayerPrivate *private = GET_PRIVATE (object);
  gint64                 memsize = 0;
434

435 436
  memsize += gimp_object_get_memsize (GIMP_OBJECT (private->children), gui_size);
  memsize += gimp_object_get_memsize (GIMP_OBJECT (private->projection), gui_size);
437 438 439 440 441

  return memsize + GIMP_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
}

442 443 444 445 446 447 448 449 450 451 452
static void
gimp_group_layer_ancestry_changed (GimpViewable *viewable)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (viewable);

  gimp_projection_set_priority (private->projection,
                                gimp_viewable_get_depth (viewable) + 1);

  GIMP_VIEWABLE_CLASS (parent_class)->ancestry_changed (viewable);
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
static gboolean
gimp_group_layer_get_size (GimpViewable *viewable,
                           gint         *width,
                           gint         *height)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (viewable);

  if (private->reallocate_width  != 0 &&
      private->reallocate_height != 0)
    {
      *width  = private->reallocate_width;
      *height = private->reallocate_height;

      return TRUE;
    }

469 470 471 472 473 474 475
  /*  return the size only if there are children...  */
  if (gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children)))
    return GIMP_VIEWABLE_CLASS (parent_class)->get_size (viewable,
                                                         width, height);

  /*  ...otherwise return "no content"  */
  return FALSE;
476 477
}

478 479 480
static GimpContainer *
gimp_group_layer_get_children (GimpViewable *viewable)
{
481
  return GET_PRIVATE (viewable)->children;
482 483 484 485 486 487 488 489 490 491 492 493 494 495
}

static gboolean
gimp_group_layer_get_expanded (GimpViewable *viewable)
{
  GimpGroupLayer *group = GIMP_GROUP_LAYER (viewable);

  return GET_PRIVATE (group)->expanded;
}

static void
gimp_group_layer_set_expanded (GimpViewable *viewable,
                               gboolean      expanded)
{
496
  GimpGroupLayerPrivate *private = GET_PRIVATE (viewable);
497

498 499 500 501 502 503
  if (private->expanded != expanded)
    {
      private->expanded = expanded;

      gimp_viewable_expanded_changed (viewable);
    }
504 505
}

506
static gboolean
507
gimp_group_layer_is_position_locked (GimpItem *item)
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (item);
  GList                 *list;

  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;

      if (gimp_item_is_position_locked (child))
        return TRUE;
    }

  return GIMP_ITEM_CLASS (parent_class)->is_position_locked (item);
}

525 526 527 528 529 530
static GimpItem *
gimp_group_layer_duplicate (GimpItem *item,
                            GType     new_type)
{
  GimpItem *new_item;

531
  g_return_val_if_fail (g_type_is_a (new_type, GIMP_TYPE_DRAWABLE), NULL);
532 533 534 535 536

  new_item = GIMP_ITEM_CLASS (parent_class)->duplicate (item, new_type);

  if (GIMP_IS_GROUP_LAYER (new_item))
    {
537 538 539 540 541
      GimpGroupLayerPrivate *private     = GET_PRIVATE (item);
      GimpGroupLayer        *new_group   = GIMP_GROUP_LAYER (new_item);
      GimpGroupLayerPrivate *new_private = GET_PRIVATE (new_item);
      gint                   position    = 0;
      GList                 *list;
542

543 544
      gimp_group_layer_suspend_resize (new_group, FALSE);

545
      for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
546
           list;
547
           list = g_list_next (list))
548
        {
549 550 551
          GimpItem      *child = list->data;
          GimpItem      *new_child;
          GimpLayerMask *mask;
552 553 554

          new_child = gimp_item_duplicate (child, G_TYPE_FROM_INSTANCE (child));

555
          gimp_object_set_name (GIMP_OBJECT (new_child),
556
                                gimp_object_get_name (child));
557 558 559 560 561 562 563 564 565 566

          mask = gimp_layer_get_mask (GIMP_LAYER (child));

          if (mask)
            {
              GimpLayerMask *new_mask;

              new_mask = gimp_layer_get_mask (GIMP_LAYER (new_child));

              gimp_object_set_name (GIMP_OBJECT (new_mask),
567
                                    gimp_object_get_name (mask));
568 569
            }

570
          gimp_viewable_set_parent (GIMP_VIEWABLE (new_child),
571
                                    GIMP_VIEWABLE (new_group));
572

573
          gimp_container_insert (new_private->children,
574
                                 GIMP_OBJECT (new_child),
575
                                 position++);
576
        }
577

578 579
      /*  force the projection to reallocate itself  */
      GET_PRIVATE (new_group)->reallocate_projection = TRUE;
580 581

      gimp_group_layer_resume_resize (new_group, FALSE);
582 583 584 585 586
    }

  return new_item;
}

587 588
static void
gimp_group_layer_convert (GimpItem  *item,
589 590
                          GimpImage *dest_image,
                          GType      old_type)
591
{
592 593
  GimpGroupLayerPrivate *private = GET_PRIVATE (item);
  GList                 *list;
594

595
  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
596 597 598 599 600
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;

601 602
      GIMP_ITEM_GET_CLASS (child)->convert (child, dest_image,
                                            G_TYPE_FROM_INSTANCE (child));
603 604
    }

605
  GIMP_ITEM_CLASS (parent_class)->convert (item, dest_image, old_type);
606 607
}

608
static void
609
gimp_group_layer_start_transform (GimpItem *item,
610 611
                             gboolean  push_undo)
{
612
  _gimp_group_layer_start_transform (GIMP_GROUP_LAYER (item), push_undo);
613

614 615
  if (GIMP_ITEM_CLASS (parent_class)->start_transform)
    GIMP_ITEM_CLASS (parent_class)->start_transform (item, push_undo);
616 617 618
}

static void
619
gimp_group_layer_end_transform (GimpItem *item,
620 621
                           gboolean  push_undo)
{
622 623
  if (GIMP_ITEM_CLASS (parent_class)->end_transform)
    GIMP_ITEM_CLASS (parent_class)->end_transform (item, push_undo);
624

625
  _gimp_group_layer_end_transform (GIMP_GROUP_LAYER (item), push_undo);
626 627
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
static void
gimp_group_layer_resize (GimpItem     *item,
                         GimpContext  *context,
                         GimpFillType  fill_type,
                         gint          new_width,
                         gint          new_height,
                         gint          offset_x,
                         gint          offset_y)
{
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (item);
  GimpGroupLayerPrivate *private = GET_PRIVATE (item);
  GList                 *list;
  gint                   x, y;

  /* we implement GimpItem::resize(), instead of GimpLayer::resize(), so that
643 644 645
   * GimpLayer doesn't resize the mask.  note that gimp_item_resize() calls
   * gimp_item_{start,end}_move(), and not gimp_item_{start,end}_transform(),
   * so that mask resizing is handled by gimp_group_layer_update_size().
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
   */

  x = gimp_item_get_offset_x (item) - offset_x;
  y = gimp_item_get_offset_y (item) - offset_y;

  gimp_group_layer_suspend_resize (group, TRUE);

  list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));

  while (list)
    {
      GimpItem *child = list->data;
      gint      child_width;
      gint      child_height;
      gint      child_x;
      gint      child_y;

      list = g_list_next (list);

      if (gimp_rectangle_intersect (x,
                                    y,
                                    new_width,
                                    new_height,
                                    gimp_item_get_offset_x (child),
                                    gimp_item_get_offset_y (child),
                                    gimp_item_get_width  (child),
                                    gimp_item_get_height (child),
                                    &child_x,
                                    &child_y,
                                    &child_width,
                                    &child_height))
        {
          gint child_offset_x = gimp_item_get_offset_x (child) - child_x;
          gint child_offset_y = gimp_item_get_offset_y (child) - child_y;

          gimp_item_resize (child, context, fill_type,
                            child_width, child_height,
                            child_offset_x, child_offset_y);
        }
      else if (gimp_item_is_attached (item))
        {
          gimp_image_remove_layer (gimp_item_get_image (item),
                                   GIMP_LAYER (child),
                                   TRUE, NULL);
        }
      else
        {
          gimp_container_remove (private->children, GIMP_OBJECT (child));
        }
    }

  gimp_group_layer_resume_resize (group, TRUE);
}

700 701 702 703 704
static gint64
gimp_group_layer_estimate_memsize (GimpDrawable      *drawable,
                                   GimpComponentType  component_type,
                                   gint               width,
                                   gint               height)
705
{
706
  GimpGroupLayerPrivate *private = GET_PRIVATE (drawable);
707
  GList                 *list;
708 709
  GimpImageBaseType      base_type;
  gint64                 memsize = 0;
710

711
  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
712 713 714
       list;
       list = g_list_next (list))
    {
715 716 717
      GimpDrawable *child = list->data;
      gint          child_width;
      gint          child_height;
718

719 720 721 722 723 724 725 726 727 728 729
      child_width  = (gimp_item_get_width (GIMP_ITEM (child)) *
                      width /
                      gimp_item_get_width (GIMP_ITEM (drawable)));
      child_height = (gimp_item_get_height (GIMP_ITEM (child)) *
                      height /
                      gimp_item_get_height (GIMP_ITEM (drawable)));

      memsize += gimp_drawable_estimate_memsize (child,
                                                 component_type,
                                                 child_width,
                                                 child_height);
730 731
    }

732
  base_type = gimp_drawable_get_base_type (drawable);
733

734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
  memsize += gimp_projection_estimate_memsize (base_type, component_type,
                                               width, height);

  return memsize +
         GIMP_DRAWABLE_CLASS (parent_class)->estimate_memsize (drawable,
                                                               component_type,
                                                               width, height);
}

static void
gimp_group_layer_translate (GimpLayer *layer,
                            gint       offset_x,
                            gint       offset_y)
{
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
750
  gint                   x, y;
751
  GList                 *list;
752

753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
  /*  don't use gimp_group_layer_suspend_resize(), but rather increment
   *  private->suspend_resize directly, since we're translating the group layer
   *  here, rather than relying on gimp_group_layer_update_size() to do it.
   */
  private->suspend_resize++;

  /*  redirect stack updates to the drawable, rather than to the projection  */
  private->direct_update++;

  gimp_item_get_offset (GIMP_ITEM (group), &x, &y);

  x += offset_x;
  y += offset_y;

  /*  update the offset node  */
  if (private->offset_node)
    gegl_node_set (private->offset_node,
                   "x", (gdouble) -x,
                   "y", (gdouble) -y,
                   NULL);

  /*  invalidate the selection boundary because of a layer modification  */
  gimp_drawable_invalidate_boundary (GIMP_DRAWABLE (layer));

  /*  update the group layer offset  */
  gimp_item_set_offset (GIMP_ITEM (group), x, y);
779 780 781 782 783 784

  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;
785

786 787
      /*  don't push an undo here because undo will call us again  */
      gimp_item_translate (child, offset_x, offset_y, FALSE);
788
    }
789

790 791 792 793 794 795 796 797
  /*  redirect stack updates back to the projection  */
  private->direct_update--;

  /*  don't use gimp_group_layer_resume_resize(), but rather decrement
   *  private->suspend_resize directly, so that gimp_group_layer_update_size()
   *  isn't called.
   */
  private->suspend_resize--;
798 799 800
}

static void
801
gimp_group_layer_scale (GimpLayer             *layer,
802 803 804 805 806 807 808
                        gint                   new_width,
                        gint                   new_height,
                        gint                   new_offset_x,
                        gint                   new_offset_y,
                        GimpInterpolationType  interpolation_type,
                        GimpProgress          *progress)
{
809 810 811
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
  GimpItem              *item    = GIMP_ITEM (layer);
812
  GimpObjectQueue       *queue   = NULL;
813 814 815 816 817
  GList                 *list;
  gdouble                width_factor;
  gdouble                height_factor;
  gint                   old_offset_x;
  gint                   old_offset_y;
818 819 820 821 822 823

  width_factor  = (gdouble) new_width  / (gdouble) gimp_item_get_width  (item);
  height_factor = (gdouble) new_height / (gdouble) gimp_item_get_height (item);

  old_offset_x = gimp_item_get_offset_x (item);
  old_offset_y = gimp_item_get_offset_y (item);
824

825 826 827 828 829 830 831 832
  if (progress)
    {
      queue    = gimp_object_queue_new (progress);
      progress = GIMP_PROGRESS (queue);

      gimp_object_queue_push_container (queue, private->children);
    }

833 834
  gimp_group_layer_suspend_resize (group, TRUE);

835
  list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
836 837

  while (list)
838 839
    {
      GimpItem *child = list->data;
840

841 842
      list = g_list_next (list);

843 844 845
      if (queue)
        gimp_object_queue_pop (queue);

846 847 848 849 850 851
      if (! gimp_item_scale_by_factors_with_origin (child,
                                                    width_factor, height_factor,
                                                    old_offset_x, old_offset_y,
                                                    new_offset_x, new_offset_y,
                                                    interpolation_type,
                                                    progress))
852
        {
853 854 855 856 857 858 859 860 861 862 863
          /* new width or height are 0; remove item */
          if (gimp_item_is_attached (item))
            {
              gimp_image_remove_layer (gimp_item_get_image (item),
                                       GIMP_LAYER (child),
                                       TRUE, NULL);
            }
          else
            {
              gimp_container_remove (private->children, GIMP_OBJECT (child));
            }
864
        }
865 866
    }

867
  gimp_group_layer_resume_resize (group, TRUE);
868 869

  g_clear_object (&queue);
870 871 872
}

static void
873
gimp_group_layer_flip (GimpLayer           *layer,
874 875 876 877 878
                       GimpContext         *context,
                       GimpOrientationType  flip_type,
                       gdouble              axis,
                       gboolean             clip_result)
{
879 880
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
881
  GList                 *list;
882

883 884
  gimp_group_layer_suspend_resize (group, TRUE);

885
  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
886 887 888 889 890 891 892 893 894
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;

      gimp_item_flip (child, context,
                      flip_type, axis, clip_result);
    }

895
  gimp_group_layer_resume_resize (group, TRUE);
896 897 898
}

static void
899
gimp_group_layer_rotate (GimpLayer        *layer,
900 901 902 903 904 905
                         GimpContext      *context,
                         GimpRotationType  rotate_type,
                         gdouble           center_x,
                         gdouble           center_y,
                         gboolean          clip_result)
{
906 907
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
908
  GList                 *list;
909

910 911
  gimp_group_layer_suspend_resize (group, TRUE);

912
  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
913 914 915 916 917 918 919 920 921
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;

      gimp_item_rotate (child, context,
                        rotate_type, center_x, center_y, clip_result);
    }

922
  gimp_group_layer_resume_resize (group, TRUE);
923 924 925
}

static void
926
gimp_group_layer_transform (GimpLayer              *layer,
927 928 929 930 931 932 933
                            GimpContext            *context,
                            const GimpMatrix3      *matrix,
                            GimpTransformDirection  direction,
                            GimpInterpolationType   interpolation_type,
                            GimpTransformResize     clip_result,
                            GimpProgress           *progress)
{
934 935
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
936
  GimpObjectQueue       *queue   = NULL;
937
  GList                 *list;
938

939 940 941 942 943 944 945 946
  if (progress)
    {
      queue    = gimp_object_queue_new (progress);
      progress = GIMP_PROGRESS (queue);

      gimp_object_queue_push_container (queue, private->children);
    }

947 948
  gimp_group_layer_suspend_resize (group, TRUE);

949
  for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
950 951 952 953 954
       list;
       list = g_list_next (list))
    {
      GimpItem *child = list->data;

955 956 957
      if (queue)
        gimp_object_queue_pop (queue);

958 959
      gimp_item_transform (child, context,
                           matrix, direction,
960
                           interpolation_type,
961 962 963
                           clip_result, progress);
    }

964
  gimp_group_layer_resume_resize (group, TRUE);
965 966

  g_clear_object (&queue);
967 968
}

969 970
static const Babl *
get_projection_format (GimpProjectable   *projectable,
971 972
                       GimpImageBaseType  base_type,
                       GimpPrecision      precision)
973
{
974 975
  GimpImage *image = gimp_item_get_image (GIMP_ITEM (projectable));

976 977 978 979
  switch (base_type)
    {
    case GIMP_RGB:
    case GIMP_INDEXED:
980 981
      return gimp_image_get_format (image, GIMP_RGB, precision, TRUE,
                                    gimp_image_get_layer_space (image));
982 983

    case GIMP_GRAY:
984 985
      return gimp_image_get_format (image, GIMP_GRAY, precision, TRUE,
                                    gimp_image_get_layer_space (image));
986 987
    }

988
  g_return_val_if_reached (NULL);
989 990
}

991
static void
992
gimp_group_layer_convert_type (GimpLayer        *layer,
993 994
                               GimpImage        *dest_image,
                               const Babl       *new_format,
995
                               GimpColorProfile *src_profile,
996 997 998 999 1000
                               GimpColorProfile *dest_profile,
                               GeglDitherMethod  layer_dither_type,
                               GeglDitherMethod  mask_dither_type,
                               gboolean          push_undo,
                               GimpProgress     *progress)
1001
{
1002 1003
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
1004
  GeglBuffer            *buffer;
1005

1006 1007 1008 1009 1010 1011 1012
  if (push_undo)
    {
      GimpImage *image = gimp_item_get_image (GIMP_ITEM (group));

      gimp_image_undo_push_group_layer_convert (image, NULL, group);
    }

1013 1014 1015 1016 1017 1018
  /*  Force allocation of a same-size projection, in case the group
   *  layer is empty
   */
  private->reallocate_width  = gimp_item_get_width  (GIMP_ITEM (group));
  private->reallocate_height = gimp_item_get_height (GIMP_ITEM (group));

1019 1020 1021
  /*  Need to temporarily set the projectable's format to the new
   *  values so the projection will create its tiles with the right
   *  depth
1022
   */
1023
  private->convert_format =
1024
    get_projection_format (GIMP_PROJECTABLE (group),
1025 1026
                           gimp_babl_format_get_base_type (new_format),
                           gimp_babl_format_get_precision (new_format));
1027
  gimp_projectable_structure_changed (GIMP_PROJECTABLE (group));
1028
  gimp_group_layer_flush (group);
1029

1030
  buffer = gimp_pickable_get_buffer (GIMP_PICKABLE (private->projection));
1031

1032
  gimp_drawable_set_buffer_full (GIMP_DRAWABLE (group),
1033
                                 FALSE, NULL,
1034
                                 buffer,
1035
                                 gimp_item_get_offset_x (GIMP_ITEM (group)),
1036 1037
                                 gimp_item_get_offset_y (GIMP_ITEM (group)),
                                 TRUE);
1038

1039 1040
  /*  reset, the actual format is right now  */
  private->convert_format = NULL;
1041 1042 1043

  private->reallocate_width  = 0;
  private->reallocate_height = 0;
1044 1045
}

1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
static GeglNode *
gimp_group_layer_get_source_node (GimpDrawable *drawable)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (drawable);
  GeglNode              *input;

  g_warn_if_fail (private->source_node == NULL);

  private->source_node = gegl_node_new ();

  input = gegl_node_get_input_proxy (private->source_node, "input");

  private->parent_source_node =
    GIMP_DRAWABLE_CLASS (parent_class)->get_source_node (drawable);

  gegl_node_add_child (private->source_node, private->parent_source_node);

  g_object_unref (private->parent_source_node);

  if (gegl_node_has_pad (private->parent_source_node, "input"))
    {
      gegl_node_connect_to (input,                       "output",
                            private->parent_source_node, "input");
    }

  /* make sure we have a graph */
  (void) gimp_group_layer_get_graph (GIMP_PROJECTABLE (drawable));

  gegl_node_add_child (private->source_node, private->graph);

  gimp_group_layer_update_source_node (GIMP_GROUP_LAYER (drawable));

  return g_object_ref (private->source_node);
}

static void
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
gimp_group_layer_opacity_changed (GimpLayer *layer)
{
  gimp_layer_update_effective_mode (layer);

  if (GIMP_LAYER_CLASS (parent_class)->opacity_changed)
    GIMP_LAYER_CLASS (parent_class)->opacity_changed (layer);
}

static void
gimp_group_layer_effective_mode_changed (GimpLayer *layer)
1092
{
1093 1094
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (layer);
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);
1095
  GimpLayerMode          mode;
1096 1097
  gboolean               pass_through;

1098 1099 1100
  gimp_layer_get_effective_mode (layer, &mode, NULL, NULL, NULL);

  pass_through = (mode == GIMP_LAYER_MODE_PASS_THROUGH);
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112

  if (private->pass_through && ! pass_through)
    {
      /* when switching from pass-through mode to a non-pass-through mode,
       * flush the pickable in order to make sure the projection's buffer
       * gets properly invalidated synchronously, so that it can be used
       * as a source for the rest of the composition.
       */
      gimp_pickable_flush (GIMP_PICKABLE (private->projection));
    }

  private->pass_through = pass_through;
1113 1114

  gimp_group_layer_update_source_node (group);
1115
  gimp_group_layer_update_mode_node (group);
1116

1117 1118
  if (GIMP_LAYER_CLASS (parent_class)->effective_mode_changed)
    GIMP_LAYER_CLASS (parent_class)->effective_mode_changed (layer);
1119 1120
}

1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132
static void
gimp_group_layer_excludes_backdrop_changed (GimpLayer *layer)
{
  GimpGroupLayer *group = GIMP_GROUP_LAYER (layer);

  gimp_group_layer_update_source_node (group);
  gimp_group_layer_update_mode_node (group);

  if (GIMP_LAYER_CLASS (parent_class)->excludes_backdrop_changed)
    GIMP_LAYER_CLASS (parent_class)->excludes_backdrop_changed (layer);
}

1133 1134 1135
static void
gimp_group_layer_mask_changed (GimpLayer *layer)
{
1136 1137
  g_warn_if_fail (GET_PRIVATE (layer)->suspend_mask == 0);

1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
  gimp_layer_update_effective_mode (layer);

  if (GIMP_LAYER_CLASS (parent_class)->mask_changed)
    GIMP_LAYER_CLASS (parent_class)->mask_changed (layer);
}

static void
gimp_group_layer_get_effective_mode (GimpLayer              *layer,
                                     GimpLayerMode          *mode,
                                     GimpLayerColorSpace    *blend_space,
                                     GimpLayerColorSpace    *composite_space,
                                     GimpLayerCompositeMode *composite_mode)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);

  /* try to strength-reduce pass-through groups to normal groups, which are
   * cheaper.
   */
  if (gimp_layer_get_mode (layer) == GIMP_LAYER_MODE_PASS_THROUGH &&
      ! no_pass_through_strength_reduction)
    {
      /* we perform the strength-reduction if:
       *
       *   - the group has no active children;
       *
       *   or,
       *
       *     - the group has a single active child; or,
       *
       *     - the effective mode of all the active children is normal, their
1168 1169
       *       effective composite mode is UNION, and their effective blend and
       *       composite spaces are equal;
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
       *
       *   - and,
       *
       *     - the group's opacity is 100%, and it has no mask; or,
       *
       *     - the group's composite space equals the active children's
       *       composite space.
       */

      GList    *list;
      gboolean  reduce = TRUE;
      gboolean  first  = TRUE;

      *mode            = GIMP_LAYER_MODE_NORMAL;
      *blend_space     = gimp_layer_get_real_blend_space (layer);
      *composite_space = gimp_layer_get_real_composite_space (layer);
      *composite_mode  = gimp_layer_get_real_composite_mode (layer);

      for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
           list;
           list = g_list_next (list))
        {
          GimpLayer *child = list->data;

          if (! gimp_filter_get_active (GIMP_FILTER (child)))
            continue;

          if (first)
            {
              gimp_layer_get_effective_mode (child,
                                             mode,
                                             blend_space,
                                             composite_space,
                                             composite_mode);

              if (*mode == GIMP_LAYER_MODE_NORMAL_LEGACY)
                *mode = GIMP_LAYER_MODE_NORMAL;

              first = FALSE;
            }
          else
            {
              GimpLayerMode          other_mode;
              GimpLayerColorSpace    other_blend_space;
              GimpLayerColorSpace    other_composite_space;
              GimpLayerCompositeMode other_composite_mode;

              if (*mode           != GIMP_LAYER_MODE_NORMAL ||
1218
                  *composite_mode != GIMP_LAYER_COMPOSITE_UNION)
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266
                {
                  reduce = FALSE;

                  break;
                }

              gimp_layer_get_effective_mode (child,
                                             &other_mode,
                                             &other_blend_space,
                                             &other_composite_space,
                                             &other_composite_mode);

              if (other_mode == GIMP_LAYER_MODE_NORMAL_LEGACY)
                other_mode = GIMP_LAYER_MODE_NORMAL;

              if (other_mode            != *mode            ||
                  other_blend_space     != *blend_space     ||
                  other_composite_space != *composite_space ||
                  other_composite_mode  != *composite_mode)
                {
                  reduce = FALSE;

                  break;
                }
            }
        }

      if (reduce)
        {
          if (first                                                  ||
              (gimp_layer_get_opacity (layer) == GIMP_OPACITY_OPAQUE &&
               ! gimp_layer_get_mask (layer))                        ||
              *composite_space == gimp_layer_get_real_composite_space (layer))
            {
              /* strength reduction succeeded! */
              return;
            }
        }
    }

  /* strength-reduction failed.  chain up. */
  GIMP_LAYER_CLASS (parent_class)->get_effective_mode (layer,
                                                       mode,
                                                       blend_space,
                                                       composite_space,
                                                       composite_mode);
}

1267 1268 1269
static gboolean
gimp_group_layer_get_excludes_backdrop (GimpLayer *layer)
{
1270 1271 1272
  GimpGroupLayerPrivate *private = GET_PRIVATE (layer);

  if (private->pass_through)
1273
    {
1274
      GList *list;
1275 1276 1277 1278 1279

      for (list = gimp_item_stack_get_item_iter (GIMP_ITEM_STACK (private->children));
           list;
           list = g_list_next (list))
        {
1280
          GimpFilter *child = list->data;
1281

1282
          if (gimp_filter_get_active (child) &&
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292
              gimp_layer_get_excludes_backdrop (GIMP_LAYER (child)))
            return TRUE;
        }

      return FALSE;
    }
  else
    return GIMP_LAYER_CLASS (parent_class)->get_excludes_backdrop (layer);
}

1293 1294 1295
static const Babl *
gimp_group_layer_get_format (GimpProjectable *projectable)
{
1296 1297
  GimpGroupLayerPrivate *private = GET_PRIVATE (projectable);
  GimpImageBaseType      base_type;
1298
  GimpPrecision          precision;
1299

1300 1301
  if (private->convert_format)
    return private->convert_format;
1302

1303
  base_type = gimp_drawable_get_base_type (GIMP_DRAWABLE (projectable));
1304
  precision = gimp_drawable_get_precision (GIMP_DRAWABLE (projectable));
1305

1306
  return get_projection_format (projectable, base_type, precision);
1307 1308
}

1309 1310 1311
static GeglNode *
gimp_group_layer_get_graph (GimpProjectable *projectable)
{
1312 1313
  GimpGroupLayer        *group   = GIMP_GROUP_LAYER (projectable);
  GimpGroupLayerPrivate *private = GET_PRIVATE (projectable);
1314
  GeglNode              *input;
1315 1316 1317 1318
  GeglNode              *layers_node;
  GeglNode              *output;
  gint                   off_x;
  gint                   off_y;
1319

1320 1321
  if (private->graph)
    return private->graph;
1322

1323
  private->graph = gegl_node_new ();
1324

1325 1326
  input = gegl_node_get_input_proxy (private->graph, "input");

1327
  layers_node =
1328
    gimp_filter_stack_get_graph (GIMP_FILTER_STACK (private->children));
1329

1330
  gegl_node_add_child (private->graph, layers_node);
1331

1332 1333 1334
  gegl_node_connect_to (input,       "output",
                        layers_node, "input");

1335 1336
  gimp_item_get_offset (GIMP_ITEM (group), &off_x, &off_y);

1337 1338 1339 1340 1341
  private->offset_node = gegl_node_new_child (private->graph,
                                              "operation", "gegl:translate",
                                              "x",         (gdouble) -off_x,
                                              "y",         (gdouble) -off_y,
                                              NULL);
1342

1343 1344
  gegl_node_connect_to (layers_node,          "output",
                        private->offset_node, "input");
1345

1346
  output = gegl_node_get_output_proxy (private->graph, "output");
1347

1348 1349
  gegl_node_connect_to (private->offset_node, "output",
                        output,               "input");
1350

1351
  return private->graph;
1352 1353
}

1354 1355 1356 1357 1358 1359 1360 1361
static void
gimp_group_layer_begin_render (GimpProjectable *projectable)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (projectable);

  if (private->source_node == NULL)
    return;

1362 1363
  if (private->pass_through)
    gegl_node_disconnect (private->graph, "input");
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373
}

static void
gimp_group_layer_end_render (GimpProjectable *projectable)
{
  GimpGroupLayerPrivate *private = GET_PRIVATE (projectable);

  if (private->source_node == NULL)
    return;

1374
  if (private->pass_through)
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
    {
      GeglNode *input;

      input = gegl_node_get_input_proxy (private->source_node, "input");

      gegl_node_connect_to (input,          "output",
                            private->graph, "input");
    }
}

1385
static gdouble
1386 1387 1388 1389 1390
gimp_group_layer_get_opacity_at (GimpPickable *pickable,
                                 gint          x,
                                 gint          y)
{
  /* Only consider child layers as having content */
1391 1392

  return GIMP_OPACITY_TRANSPARENT;
1393 1394
}

1395 1396 1397

/*  public functions  */

1398 1399 1400
GimpLayer *
gimp_group_layer_new (GimpImage *image)
{
1401
  GimpGroupLayer *group;
1402
  const Babl     *format;
1403 1404 1405

  g_return_val_if_fail (GIMP_IS_IMAGE (image), NULL);

1406
  format = gimp_image_get_layer_format (image, TRUE);
1407

1408 1409 1410
  group = GIMP_GROUP_LAYER (gimp_drawable_new (GIMP_TYPE_GROUP_LAYER,
                                               image, NULL,
                                               0, 0, 1, 1,
1411
                                               format));
1412

1413 1414 1415 1416
  gimp_layer_set_mode (GIMP_LAYER (group),
                       gimp_image_get_default_new_layer_mode (image),
                       FALSE);

1417
  return GIMP_LAYER (group);
1418
}
1419

1420 1421 1422 1423 1424 1425 1426 1427
GimpProjection *
gimp_group_layer_get_projection (GimpGroupLayer *group)
{
  g_return_val_if_fail (GIMP_IS_GROUP_LAYER (group), NULL);

  return GET_PRIVATE (group)->projection;
}

1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
void
gimp_group_layer_suspend_resize (GimpGroupLayer *group,
                                 gboolean        push_undo)
{
  GimpItem *item;

  g_return_if_fail (GIMP_IS_GROUP_LAYER (group));

  item = GIMP_ITEM (group);

  if (! gimp_item_is_attached (item))
    push_undo = FALSE;

  if (push_undo)
1442 1443
    gimp_image_undo_push_group_layer_suspend_resize (gimp_item_get_image (item),
                                                     NULL, group);
1444

1445
  GET_PRIVATE (group)->suspend_resize++;
1446 1447 1448 1449 1450 1451
}

void
gimp_group_layer_resume_resize (GimpGroupLayer *group,
                                gboolean        push_undo)
{
1452 1453
  GimpGroupLayerPrivate *private;
  GimpItem              *item;
1454 1455 1456 1457
  GimpItem              *mask = NULL;
  GeglBuffer            *mask_buffer;
  GeglRectangle          mask_bounds;
  GimpUndo              *undo;
1458 1459

  g_return_if_fail (GIMP_IS_GROUP_LAYER (group));
1460 1461 1462 1463

  private = GET_PRIVATE (group);

  g_return_if_fail (private->suspend_resize > 0);
1464 1465 1466 1467 1468 1469 1470

  item = GIMP_ITEM (group);

  if (! gimp_item_is_attached (item))
    push_undo = FALSE;

  if (push_undo)
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
    {
      undo =
        gimp_image_undo_push_group_layer_resume_resize (gimp_item_get_image (item),
                                                        NULL, group);

      /* if there were any {suspend,resume}_mask() calls during the time the
       * group's size was suspended, the resume_mask() calls will not have seen
       * any changes to the mask, and will therefore won't restore the mask
       * during undo.  if the group's bounding box did change while resize was
       * suspended, and if there are no other {suspend,resume}_mask() blocks
       * that will see the resized mask, we have to restore the mask during the
       * resume_resize() undo.
       *
       * we ref the mask buffer here, and compare it to the mask buffer after
       * updating the size.
       */
      if (private->suspend_resize == 1 && private->suspend_mask == 0)
        {
          mask = GIMP_ITEM (gimp_layer_get_mask (GIMP_LAYER (group)));

          if (mask)
            {
              mask_buffer =
                g_object_ref (gimp_drawable_get_buffer (GIMP_DRAWABLE (mask)));

              mask_bounds.x      = gimp_item_get_offset_x (mask);
              mask_bounds.y      = gimp_item_get_offset_y (mask);
              mask_bounds.width  = gimp_item_get_width    (mask);
              mask_bounds.height = gimp_item_get_height   (mask);
            }
        }
    }
1503