gskrendernodeimpl.c 132 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* GSK - The GTK Scene Kit
 *
 * Copyright 2016  Endless
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

Benjamin Otte's avatar
Benjamin Otte committed
19 20
#include "config.h"

21 22
#include "gskrendernodeprivate.h"

Benjamin Otte's avatar
Benjamin Otte committed
23
#include "gskcairoblurprivate.h"
24 25
#include "gskdebugprivate.h"
#include "gskrendererprivate.h"
26
#include "gskroundedrectprivate.h"
Benjamin Otte's avatar
Benjamin Otte committed
27
#include "gdk/gdktextureprivate.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static gboolean
check_variant_type (GVariant *variant,
                    const char *type_string,
                    GError     **error)
{
  if (!g_variant_is_of_type (variant, G_VARIANT_TYPE (type_string)))
    {
      g_set_error (error, GSK_SERIALIZATION_ERROR, GSK_SERIALIZATION_INVALID_DATA,
                   "Wrong variant type, got '%s' but needed '%s",
                   g_variant_get_type_string (variant), type_string);
      return FALSE;
    }

  return TRUE;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
/*** GSK_COLOR_NODE ***/

typedef struct _GskColorNode GskColorNode;

struct _GskColorNode
{
  GskRenderNode render_node;

  GdkRGBA color;
};

static void
gsk_color_node_finalize (GskRenderNode *node)
{
}

static void
gsk_color_node_draw (GskRenderNode *node,
                     cairo_t       *cr)
{
  GskColorNode *self = (GskColorNode *) node;

  gdk_cairo_set_source_rgba (cr, &self->color);

  cairo_rectangle (cr,
70 71
                   node->bounds.origin.x, node->bounds.origin.y,
                   node->bounds.size.width, node->bounds.size.height);
72 73 74
  cairo_fill (cr);
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
#define GSK_COLOR_NODE_VARIANT_TYPE "(dddddddd)"

static GVariant *
gsk_color_node_serialize (GskRenderNode *node)
{
  GskColorNode *self = (GskColorNode *) node;

  return g_variant_new (GSK_COLOR_NODE_VARIANT_TYPE,
                        self->color.red, self->color.green,
                        self->color.blue, self->color.alpha,
                        (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                        (double) node->bounds.size.width, (double) node->bounds.size.height);
}

static GskRenderNode *
90 91
gsk_color_node_deserialize (GVariant  *variant,
                            GError   **error)
92 93 94 95
{
  double x, y, w, h;
  GdkRGBA color;

96
  if (!check_variant_type (variant, GSK_COLOR_NODE_VARIANT_TYPE, error))
97 98 99 100 101 102 103 104 105
    return NULL;

  g_variant_get (variant, GSK_COLOR_NODE_VARIANT_TYPE,
                 &color.red, &color.green, &color.blue, &color.alpha,
                 &x, &y, &w, &h);

  return gsk_color_node_new (&color, &GRAPHENE_RECT_INIT (x, y, w, h));
}

106 107 108 109 110 111
static const GskRenderNodeClass GSK_COLOR_NODE_CLASS = {
  GSK_COLOR_NODE,
  sizeof (GskColorNode),
  "GskColorNode",
  gsk_color_node_finalize,
  gsk_color_node_draw,
112 113
  gsk_color_node_serialize,
  gsk_color_node_deserialize,
114 115 116 117 118 119 120 121 122 123 124 125
};

const GdkRGBA *
gsk_color_node_peek_color (GskRenderNode *node)
{
  GskColorNode *self = (GskColorNode *) node;

  return &self->color;
}

/**
 * gsk_color_node_new:
126
 * @rgba: a #GdkRGBA specifying a color
127 128
 * @bounds: the rectangle to render the color into
 *
129 130
 * Creates a #GskRenderNode that will render the color specified by @rgba into
 * the area given by @bounds.
131 132 133 134 135 136 137 138 139 140 141 142
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_color_node_new (const GdkRGBA         *rgba,
                    const graphene_rect_t *bounds)
{
  GskColorNode *self;

  g_return_val_if_fail (rgba != NULL, NULL);
  g_return_val_if_fail (bounds != NULL, NULL);

143
  self = (GskColorNode *) gsk_render_node_new (&GSK_COLOR_NODE_CLASS, 0);
144 145

  self->color = *rgba;
146
  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
147 148 149 150

  return &self->render_node;
}

151 152 153 154 155 156 157 158 159 160 161 162
/*** GSK_LINEAR_GRADIENT_NODE ***/

typedef struct _GskLinearGradientNode GskLinearGradientNode;

struct _GskLinearGradientNode
{
  GskRenderNode render_node;

  graphene_point_t start;
  graphene_point_t end;

  gsize n_stops;
163
  GskColorStop stops[];
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
};

static void
gsk_linear_gradient_node_finalize (GskRenderNode *node)
{
}

static void
gsk_linear_gradient_node_draw (GskRenderNode *node,
                               cairo_t       *cr)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;
  cairo_pattern_t *pattern;
  gsize i;

  pattern = cairo_pattern_create_linear (self->start.x, self->start.y,
                                         self->end.x, self->end.y);

  if (gsk_render_node_get_node_type (node) == GSK_REPEATING_LINEAR_GRADIENT_NODE)
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);

  for (i = 0; i < self->n_stops; i++)
    {
      cairo_pattern_add_color_stop_rgba (pattern,
                                         self->stops[i].offset,
                                         self->stops[i].color.red,
                                         self->stops[i].color.green,
                                         self->stops[i].color.blue,
                                         self->stops[i].color.alpha);
    }

  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);

  cairo_rectangle (cr,
199 200
                   node->bounds.origin.x, node->bounds.origin.y,
                   node->bounds.size.width, node->bounds.size.height);
201 202 203
  cairo_fill (cr);
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
#define GSK_LINEAR_GRADIENT_NODE_VARIANT_TYPE "(dddddddda(ddddd))"

static GVariant *
gsk_linear_gradient_node_serialize (GskRenderNode *node)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;
  GVariantBuilder builder;
  guint i;

  g_variant_builder_init (&builder, G_VARIANT_TYPE ("a(ddddd)"));
  for (i = 0; i < self->n_stops; i++)
    {
      g_variant_builder_add  (&builder, "(ddddd)",
                              (double) self->stops[i].offset,
                              self->stops[i].color.red, self->stops[i].color.green,
                              self->stops[i].color.blue, self->stops[i].color.alpha);
    }

  return g_variant_new (GSK_LINEAR_GRADIENT_NODE_VARIANT_TYPE,
                        (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                        (double) node->bounds.size.width, (double) node->bounds.size.height,
                        (double) self->start.x, (double) self->start.y,
                        (double) self->end.x, (double) self->end.y,
                        &builder);
}

static GskRenderNode *
231 232 233
gsk_linear_gradient_node_real_deserialize (GVariant  *variant,
                                           gboolean   repeating,
                                           GError   **error)
234 235 236 237 238
{
  GVariantIter *iter;
  double x, y, w, h, start_x, start_y, end_x, end_y;
  gsize i, n_stops;

239
  if (!check_variant_type (variant, GSK_LINEAR_GRADIENT_NODE_VARIANT_TYPE, error))
240 241 242 243 244 245 246 247
    return NULL;

  g_variant_get (variant, GSK_LINEAR_GRADIENT_NODE_VARIANT_TYPE,
                 &x, &y, &w, &h,
                 &start_x, &start_y, &end_x, &end_y,
                 &iter);

  n_stops = g_variant_iter_n_children (iter);
248
  GskColorStop *stops = g_newa (GskColorStop, n_stops);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  for (i = 0; i < n_stops; i++)
    {
      double offset;
      g_variant_iter_next (iter, "(ddddd)",
                           &offset,
                           &stops[i].color.red, &stops[i].color.green,
                           &stops[i].color.blue, &stops[i].color.alpha);
      stops[i].offset = offset;
    }
  g_variant_iter_free (iter);

  return (repeating ? gsk_repeating_linear_gradient_node_new : gsk_linear_gradient_node_new)
                      (&GRAPHENE_RECT_INIT (x, y, w, h),
                       &GRAPHENE_POINT_INIT (start_x, start_y),
                       &GRAPHENE_POINT_INIT (end_x, end_y),
                       stops,
                       n_stops);
}

static GskRenderNode *
269 270
gsk_linear_gradient_node_deserialize (GVariant  *variant,
                                      GError   **error)
271
{
272
  return gsk_linear_gradient_node_real_deserialize (variant, FALSE, error);
273 274 275
}

static GskRenderNode *
276 277
gsk_repeating_linear_gradient_node_deserialize (GVariant  *variant,
                                                GError   **error)
278
{
279
  return gsk_linear_gradient_node_real_deserialize (variant, TRUE, error);
280 281
}

282 283 284 285 286 287
static const GskRenderNodeClass GSK_LINEAR_GRADIENT_NODE_CLASS = {
  GSK_LINEAR_GRADIENT_NODE,
  sizeof (GskLinearGradientNode),
  "GskLinearGradientNode",
  gsk_linear_gradient_node_finalize,
  gsk_linear_gradient_node_draw,
288 289
  gsk_linear_gradient_node_serialize,
  gsk_linear_gradient_node_deserialize,
290 291 292 293 294
};

static const GskRenderNodeClass GSK_REPEATING_LINEAR_GRADIENT_NODE_CLASS = {
  GSK_REPEATING_LINEAR_GRADIENT_NODE,
  sizeof (GskLinearGradientNode),
Matthias Clasen's avatar
Matthias Clasen committed
295
  "GskRepeatingLinearGradientNode",
296 297
  gsk_linear_gradient_node_finalize,
  gsk_linear_gradient_node_draw,
298 299
  gsk_linear_gradient_node_serialize,
  gsk_repeating_linear_gradient_node_deserialize,
300 301 302 303
};

/**
 * gsk_linear_gradient_node_new:
304 305 306
 * @bounds: the rectangle to render the linear gradient into
 * @start: the point at which the linear gradient will begin
 * @end: the point at which the linear gradient will finish
307
 * @color_stops: (array length=n_color_stops): a pointer to an array of #GskColorStop defining the gradient
308
 * @n_color_stops: the number of elements in @color_stops
309
 *
310 311
 * Creates a #GskRenderNode that will create a linear gradient from the given
 * points and color stops, and render that into the area given by @bounds.
312 313 314 315 316 317 318 319 320 321 322
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_linear_gradient_node_new (const graphene_rect_t  *bounds,
                              const graphene_point_t *start,
                              const graphene_point_t *end,
                              const GskColorStop     *color_stops,
                              gsize                   n_color_stops)
{
  GskLinearGradientNode *self;
323
  gsize i;
324 325 326 327 328

  g_return_val_if_fail (bounds != NULL, NULL);
  g_return_val_if_fail (start != NULL, NULL);
  g_return_val_if_fail (end != NULL, NULL);
  g_return_val_if_fail (color_stops != NULL, NULL);
329 330 331
  g_return_val_if_fail (n_color_stops >= 2, NULL);
  g_return_val_if_fail (color_stops[0].offset >= 0, NULL);
  for (i = 1; i < n_color_stops; i++)
332
    g_return_val_if_fail (color_stops[i].offset >= color_stops[i-1].offset, NULL);
333
  g_return_val_if_fail (color_stops[n_color_stops - 1].offset <= 1, NULL);
334

335
  self = (GskLinearGradientNode *) gsk_render_node_new (&GSK_LINEAR_GRADIENT_NODE_CLASS, sizeof (GskColorStop) * n_color_stops);
336

337
  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
338 339 340
  graphene_point_init_from_point (&self->start, start);
  graphene_point_init_from_point (&self->end, end);

341
  memcpy (&self->stops, color_stops, sizeof (GskColorStop) * n_color_stops);
342 343 344 345 346
  self->n_stops = n_color_stops;

  return &self->render_node;
}

347 348 349 350 351
/**
 * gsk_repeating_linear_gradient_node_new:
 * @bounds: the rectangle to render the linear gradient into
 * @start: the point at which the linear gradient will begin
 * @end: the point at which the linear gradient will finish
352
 * @color_stops: (array length=n_color_stops): a pointer to an array of #GskColorStop defining the gradient
353 354 355 356 357 358 359 360
 * @n_color_stops: the number of elements in @color_stops
 *
 * Creates a #GskRenderNode that will create a repeating linear gradient
 * from the given points and color stops, and render that into the area
 * given by @bounds.
 *
 * Returns: A new #GskRenderNode
 */
361 362 363 364 365 366 367 368
GskRenderNode *
gsk_repeating_linear_gradient_node_new (const graphene_rect_t  *bounds,
                                        const graphene_point_t *start,
                                        const graphene_point_t *end,
                                        const GskColorStop     *color_stops,
                                        gsize                   n_color_stops)
{
  GskLinearGradientNode *self;
369
  gsize i;
370 371 372 373 374

  g_return_val_if_fail (bounds != NULL, NULL);
  g_return_val_if_fail (start != NULL, NULL);
  g_return_val_if_fail (end != NULL, NULL);
  g_return_val_if_fail (color_stops != NULL, NULL);
375 376 377
  g_return_val_if_fail (n_color_stops >= 2, NULL);
  g_return_val_if_fail (color_stops[0].offset >= 0, NULL);
  for (i = 1; i < n_color_stops; i++)
378
    g_return_val_if_fail (color_stops[i].offset >= color_stops[i-1].offset, NULL);
379
  g_return_val_if_fail (color_stops[n_color_stops - 1].offset <= 1, NULL);
380

381
  self = (GskLinearGradientNode *) gsk_render_node_new (&GSK_REPEATING_LINEAR_GRADIENT_NODE_CLASS, sizeof (GskColorStop) * n_color_stops);
382

383
  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
384 385 386
  graphene_point_init_from_point (&self->start, start);
  graphene_point_init_from_point (&self->end, end);

387
  memcpy (&self->stops, color_stops, sizeof (GskColorStop) * n_color_stops);
388 389 390 391 392
  self->n_stops = n_color_stops;

  return &self->render_node;
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
const graphene_point_t *
gsk_linear_gradient_node_peek_start (GskRenderNode *node)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;

  return &self->start;
}

const graphene_point_t *
gsk_linear_gradient_node_peek_end (GskRenderNode *node)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;

  return &self->end;
}

409
gsize
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
gsk_linear_gradient_node_get_n_color_stops (GskRenderNode *node)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;

  return self->n_stops;
}

const GskColorStop *
gsk_linear_gradient_node_peek_color_stops (GskRenderNode *node)
{
  GskLinearGradientNode *self = (GskLinearGradientNode *) node;

  return self->stops;
}

Benjamin Otte's avatar
Benjamin Otte committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
/*** GSK_BORDER_NODE ***/

typedef struct _GskBorderNode GskBorderNode;

struct _GskBorderNode
{
  GskRenderNode render_node;

  GskRoundedRect outline;
  float border_width[4];
  GdkRGBA border_color[4];
};

static void
gsk_border_node_finalize (GskRenderNode *node)
{
}

static void
gsk_border_node_draw (GskRenderNode *node,
                       cairo_t       *cr)
{
  GskBorderNode *self = (GskBorderNode *) node;
  GskRoundedRect inside;

  cairo_save (cr);

  gsk_rounded_rect_init_copy (&inside, &self->outline);
  gsk_rounded_rect_shrink (&inside,
                           self->border_width[0], self->border_width[1],
                           self->border_width[2], self->border_width[3]);

  cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
  gsk_rounded_rect_path (&self->outline, cr);
  gsk_rounded_rect_path (&inside, cr);

  if (gdk_rgba_equal (&self->border_color[0], &self->border_color[1]) &&
      gdk_rgba_equal (&self->border_color[0], &self->border_color[2]) &&
      gdk_rgba_equal (&self->border_color[0], &self->border_color[3]))
    {
      gdk_cairo_set_source_rgba (cr, &self->border_color[0]);
      cairo_fill (cr);
    }
  else
    {
      const graphene_rect_t *bounds = &self->outline.bounds;

      cairo_clip (cr);

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
      /* Top */
      if (self->border_width[0] > 0)
        {
          cairo_move_to (cr, bounds->origin.x, bounds->origin.y);
          cairo_rel_line_to (cr, self->border_width[3], self->border_width[0]);
          cairo_rel_line_to (cr, bounds->size.width - self->border_width[3] - self->border_width[1], 0);
          cairo_rel_line_to (cr, self->border_width[1], - self->border_width[0]);
          gdk_cairo_set_source_rgba (cr, &self->border_color[0]);
          cairo_fill (cr);
        }

      /* Right */
      if (self->border_width[1] > 0)
        {
          cairo_move_to (cr, bounds->origin.x + bounds->size.width, bounds->origin.y);
          cairo_rel_line_to (cr, - self->border_width[1], self->border_width[0]);
          cairo_rel_line_to (cr, 0, bounds->size.height - self->border_width[0] - self->border_width[2]);
          cairo_rel_line_to (cr, self->border_width[1], self->border_width[2]);
          gdk_cairo_set_source_rgba (cr, &self->border_color[1]);
          cairo_fill (cr);
        }

      /* Bottom */
      if (self->border_width[2] > 0)
        {
          cairo_move_to (cr, bounds->origin.x, bounds->origin.y + bounds->size.height);
          cairo_rel_line_to (cr, self->border_width[3], - self->border_width[2]);
          cairo_rel_line_to (cr, bounds->size.width - self->border_width[3] - self->border_width[1], 0);
          cairo_rel_line_to (cr, self->border_width[1], self->border_width[2]);
          gdk_cairo_set_source_rgba (cr, &self->border_color[2]);
          cairo_fill (cr);
        }

      /* Left */
      if (self->border_width[3] > 0)
        {
          cairo_move_to (cr, bounds->origin.x, bounds->origin.y);
          cairo_rel_line_to (cr, self->border_width[3], self->border_width[0]);
          cairo_rel_line_to (cr, 0, bounds->size.height - self->border_width[0] - self->border_width[2]);
          cairo_rel_line_to (cr, - self->border_width[3], self->border_width[2]);
          gdk_cairo_set_source_rgba (cr, &self->border_color[3]);
          cairo_fill (cr);
        }
Benjamin Otte's avatar
Benjamin Otte committed
517 518 519 520 521
    }

  cairo_restore (cr);
}

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
#define GSK_BORDER_NODE_VARIANT_TYPE "(dddddddddddddddddddddddddddddddd)"

static GVariant *
gsk_border_node_serialize (GskRenderNode *node)
{
  GskBorderNode *self = (GskBorderNode *) node;

  return g_variant_new (GSK_BORDER_NODE_VARIANT_TYPE,
                        (double) self->outline.bounds.origin.x, (double) self->outline.bounds.origin.y,
                        (double) self->outline.bounds.size.width, (double) self->outline.bounds.size.height,
                        (double) self->outline.corner[0].width, (double) self->outline.corner[0].height,
                        (double) self->outline.corner[1].width, (double) self->outline.corner[1].height,
                        (double) self->outline.corner[2].width, (double) self->outline.corner[2].height,
                        (double) self->outline.corner[3].width, (double) self->outline.corner[3].height,
                        (double) self->border_width[0], (double) self->border_width[1],
                        (double) self->border_width[2], (double) self->border_width[3],
                        self->border_color[0].red, self->border_color[0].green,
                        self->border_color[0].blue, self->border_color[0].alpha,
                        self->border_color[1].red, self->border_color[1].green,
                        self->border_color[1].blue, self->border_color[1].alpha,
                        self->border_color[2].red, self->border_color[2].green,
                        self->border_color[2].blue, self->border_color[2].alpha,
                        self->border_color[3].red, self->border_color[3].green,
                        self->border_color[3].blue, self->border_color[3].alpha);
}

static GskRenderNode *
549 550
gsk_border_node_deserialize (GVariant  *variant,
                             GError   **error)
551 552 553 554
{
  double doutline[12], dwidths[4];
  GdkRGBA colors[4];

555
  if (!check_variant_type (variant, GSK_BORDER_NODE_VARIANT_TYPE, error))
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
    return NULL;

  g_variant_get (variant, GSK_BORDER_NODE_VARIANT_TYPE,
                 &doutline[0], &doutline[1], &doutline[2], &doutline[3],
                 &doutline[4], &doutline[5], &doutline[6], &doutline[7],
                 &doutline[8], &doutline[9], &doutline[10], &doutline[11],
                 &dwidths[0], &dwidths[1], &dwidths[2], &dwidths[3],
                 &colors[0].red, &colors[0].green, &colors[0].blue, &colors[0].alpha,
                 &colors[1].red, &colors[1].green, &colors[1].blue, &colors[1].alpha,
                 &colors[2].red, &colors[2].green, &colors[2].blue, &colors[2].alpha,
                 &colors[3].red, &colors[3].green, &colors[3].blue, &colors[3].alpha);

  return gsk_border_node_new (&(GskRoundedRect) {
                                  .bounds = GRAPHENE_RECT_INIT(doutline[0], doutline[1], doutline[2], doutline[3]),
                                  .corner = {
                                      GRAPHENE_SIZE_INIT (doutline[4], doutline[5]),
                                      GRAPHENE_SIZE_INIT (doutline[6], doutline[7]),
                                      GRAPHENE_SIZE_INIT (doutline[8], doutline[9]),
                                      GRAPHENE_SIZE_INIT (doutline[10], doutline[11])
                                  }
                              },
                              (float[4]) { dwidths[0], dwidths[1], dwidths[2], dwidths[3] },
                              colors);
}

Benjamin Otte's avatar
Benjamin Otte committed
581 582 583 584 585 586
static const GskRenderNodeClass GSK_BORDER_NODE_CLASS = {
  GSK_BORDER_NODE,
  sizeof (GskBorderNode),
  "GskBorderNode",
  gsk_border_node_finalize,
  gsk_border_node_draw,
587 588
  gsk_border_node_serialize,
  gsk_border_node_deserialize
Benjamin Otte's avatar
Benjamin Otte committed
589 590 591 592 593 594 595 596 597 598
};

const GskRoundedRect *
gsk_border_node_peek_outline (GskRenderNode *node)
{
  GskBorderNode *self = (GskBorderNode *) node;

  return &self->outline;
}

599 600
const float *
gsk_border_node_peek_widths (GskRenderNode *node)
Benjamin Otte's avatar
Benjamin Otte committed
601 602 603
{
  GskBorderNode *self = (GskBorderNode *) node;

604
  return self->border_width;
Benjamin Otte's avatar
Benjamin Otte committed
605 606 607
}

const GdkRGBA *
608
gsk_border_node_peek_colors (GskRenderNode *node)
Benjamin Otte's avatar
Benjamin Otte committed
609 610 611
{
  GskBorderNode *self = (GskBorderNode *) node;

612
  return self->border_color;
Benjamin Otte's avatar
Benjamin Otte committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
}

/**
 * gsk_border_node_new:
 * @outline: a #GskRoundedRect describing the outline of the border
 * @border_width: the stroke width of the border on the top, right, bottom and
 *     left side respectively.
 * @border_color: the color used on the top, right, bottom and left side.
 *
 * Creates a #GskRenderNode that will stroke a border rectangle inside the
 * given @outline. The 4 sides of the border can have different widths and
 * colors.
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_border_node_new (const GskRoundedRect     *outline,
                     const float               border_width[4],
                     const GdkRGBA             border_color[4])
{
  GskBorderNode *self;

  g_return_val_if_fail (outline != NULL, NULL);
  g_return_val_if_fail (border_width != NULL, NULL);
  g_return_val_if_fail (border_color != NULL, NULL);

639
  self = (GskBorderNode *) gsk_render_node_new (&GSK_BORDER_NODE_CLASS, 0);
Benjamin Otte's avatar
Benjamin Otte committed
640 641 642 643 644

  gsk_rounded_rect_init_copy (&self->outline, outline);
  memcpy (self->border_width, border_width, sizeof (self->border_width));
  memcpy (self->border_color, border_color, sizeof (self->border_color));

645 646
  graphene_rect_init_from_rect (&self->render_node.bounds, &self->outline.bounds);

Benjamin Otte's avatar
Benjamin Otte committed
647 648 649
  return &self->render_node;
}

650
/*** GSK_TEXTURE_NODE ***/
651

652
typedef struct _GskTextureNode GskTextureNode;
653

654
struct _GskTextureNode
655 656 657
{
  GskRenderNode render_node;

Benjamin Otte's avatar
Benjamin Otte committed
658
  GdkTexture *texture;
659 660
};

661
static void
662
gsk_texture_node_finalize (GskRenderNode *node)
663
{
664
  GskTextureNode *self = (GskTextureNode *) node;
665

666
  g_object_unref (self->texture);
667 668
}

669
static void
670
gsk_texture_node_draw (GskRenderNode *node,
671 672
                       cairo_t       *cr)
{
673
  GskTextureNode *self = (GskTextureNode *) node;
674 675
  cairo_surface_t *surface;

Benjamin Otte's avatar
Benjamin Otte committed
676
  surface = gdk_texture_download_surface (self->texture);
677 678 679

  cairo_save (cr);

680
  cairo_translate (cr, node->bounds.origin.x, node->bounds.origin.y);
681
  cairo_scale (cr,
Benjamin Otte's avatar
Benjamin Otte committed
682 683
               node->bounds.size.width / gdk_texture_get_width (self->texture),
               node->bounds.size.height / gdk_texture_get_height (self->texture));
684 685 686 687 688 689 690 691 692

  cairo_set_source_surface (cr, surface, 0, 0);
  cairo_paint (cr);

  cairo_restore (cr);

  cairo_surface_destroy (surface);
}

693
#define GSK_TEXTURE_NODE_VARIANT_TYPE "(dddduuau)"
694 695

static GVariant *
696
gsk_texture_node_serialize (GskRenderNode *node)
697
{
698
  GskTextureNode *self = (GskTextureNode *) node;
699 700 701
  cairo_surface_t *surface;
  GVariant *result;

Benjamin Otte's avatar
Benjamin Otte committed
702
  surface = gdk_texture_download_surface (self->texture);
703 704 705 706 707 708

  g_assert (cairo_image_surface_get_width (surface) * 4 == cairo_image_surface_get_stride (surface));

  result = g_variant_new ("(dddduu@au)",
                          (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                          (double) node->bounds.size.width, (double) node->bounds.size.height,
Benjamin Otte's avatar
Benjamin Otte committed
709 710
                          (guint32) gdk_texture_get_width (self->texture),
                          (guint32) gdk_texture_get_height (self->texture),
711 712
                          g_variant_new_fixed_array (G_VARIANT_TYPE ("u"),
                                                     cairo_image_surface_get_data (surface),
Benjamin Otte's avatar
Benjamin Otte committed
713 714
                                                     gdk_texture_get_width (self->texture)
                                                     * gdk_texture_get_height (self->texture),
715 716 717 718 719 720 721 722
                                                     sizeof (guint32)));

  cairo_surface_destroy (surface);

  return result;
}

static GskRenderNode *
723
gsk_texture_node_deserialize (GVariant  *variant,
724
                              GError   **error)
725 726
{
  GskRenderNode *node;
Benjamin Otte's avatar
Benjamin Otte committed
727
  GdkTexture *texture;
728 729 730 731 732
  double bounds[4];
  guint32 width, height;
  GVariant *pixel_variant;
  gsize n_pixels;

733
  if (!check_variant_type (variant, GSK_TEXTURE_NODE_VARIANT_TYPE, error))
734 735 736 737 738 739 740
    return NULL;

  g_variant_get (variant, "(dddduu@au)",
                 &bounds[0], &bounds[1], &bounds[2], &bounds[3],
                 &width, &height, &pixel_variant);

  /* XXX: Make this work without copying the data */
Benjamin Otte's avatar
Benjamin Otte committed
741
  texture = gdk_texture_new_for_data (g_variant_get_fixed_array (pixel_variant, &n_pixels, sizeof (guint32)),
742 743 744
                                      width, height, width * 4);
  g_variant_unref (pixel_variant);

745
  node = gsk_texture_node_new (texture, &GRAPHENE_RECT_INIT(bounds[0], bounds[1], bounds[2], bounds[3]));
746

747
  g_object_unref (texture);
748 749 750 751

  return node;
}

752 753 754 755 756 757 758 759
static const GskRenderNodeClass GSK_TEXTURE_NODE_CLASS = {
  GSK_TEXTURE_NODE,
  sizeof (GskTextureNode),
  "GskTextureNode",
  gsk_texture_node_finalize,
  gsk_texture_node_draw,
  gsk_texture_node_serialize,
  gsk_texture_node_deserialize
760 761
};

762 763 764 765 766 767
/**
 * gsk_texture_node_get_texture:
 * @node: a #GskRenderNode
 *
 * Returns: (transfer none): the #GdkTexture
 */
Benjamin Otte's avatar
Benjamin Otte committed
768
GdkTexture *
769
gsk_texture_node_get_texture (GskRenderNode *node)
770
{
771
  GskTextureNode *self = (GskTextureNode *) node;
772

773
  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_TEXTURE_NODE), 0);
774

775
  return self->texture;
776 777 778
}

/**
779
 * gsk_texture_node_new:
Benjamin Otte's avatar
Benjamin Otte committed
780
 * @texture: the #GdkTexture
781 782 783 784 785 786 787 788
 * @bounds: the rectangle to render the texture into
 *
 * Creates a #GskRenderNode that will render the given
 * @texture into the area given by @bounds.
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
789
gsk_texture_node_new (GdkTexture            *texture,
790 791
                      const graphene_rect_t *bounds)
{
792
  GskTextureNode *self;
793

Benjamin Otte's avatar
Benjamin Otte committed
794
  g_return_val_if_fail (GDK_IS_TEXTURE (texture), NULL);
795 796
  g_return_val_if_fail (bounds != NULL, NULL);

797
  self = (GskTextureNode *) gsk_render_node_new (&GSK_TEXTURE_NODE_CLASS, 0);
798

799
  self->texture = g_object_ref (texture);
800
  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
801

802
  return &self->render_node;
803 804
}

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
/*** GSK_INSET_SHADOW_NODE ***/

typedef struct _GskInsetShadowNode GskInsetShadowNode;

struct _GskInsetShadowNode
{
  GskRenderNode render_node;

  GskRoundedRect outline;
  GdkRGBA color;
  float dx;
  float dy;
  float spread;
  float blur_radius;
};

static void
gsk_inset_shadow_node_finalize (GskRenderNode *node)
{
}

static gboolean
has_empty_clip (cairo_t *cr)
{
  double x1, y1, x2, y2;

  cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
  return x1 == x2 && y1 == y2;
}

static void
draw_shadow (cairo_t             *cr,
             gboolean             inset,
838 839
             const GskRoundedRect*box,
             const GskRoundedRect*clip_box,
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
             float                radius,
             const GdkRGBA       *color,
	     GskBlurFlags         blur_flags)
{
  cairo_t *shadow_cr;
  gboolean do_blur;

  if (has_empty_clip (cr))
    return;

  gdk_cairo_set_source_rgba (cr, color);
  do_blur = (blur_flags & (GSK_BLUR_X | GSK_BLUR_Y)) != 0;
  if (do_blur)
    shadow_cr = gsk_cairo_blur_start_drawing (cr, radius, blur_flags);
  else
    shadow_cr = cr;

  cairo_set_fill_rule (shadow_cr, CAIRO_FILL_RULE_EVEN_ODD);
  gsk_rounded_rect_path (box, shadow_cr);
  if (inset)
860
    cairo_rectangle (shadow_cr,
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902
                     clip_box->bounds.origin.x, clip_box->bounds.origin.y,
                     clip_box->bounds.size.width, clip_box->bounds.size.height);

  cairo_fill (shadow_cr);

  if (do_blur)
    gsk_cairo_blur_finish_drawing (shadow_cr, radius, color, blur_flags);
}

typedef struct {
  float radius;
  graphene_size_t corner;
} CornerMask;

typedef enum {
  TOP,
  RIGHT,
  BOTTOM,
  LEFT
} Side;

static guint
corner_mask_hash (CornerMask *mask)
{
  return ((guint)mask->radius << 24) ^
    ((guint)(mask->corner.width*4)) << 12 ^
    ((guint)(mask->corner.height*4)) << 0;
}

static gboolean
corner_mask_equal (CornerMask *mask1,
                   CornerMask *mask2)
{
  return
    mask1->radius == mask2->radius &&
    mask1->corner.width == mask2->corner.width &&
    mask1->corner.height == mask2->corner.height;
}

static void
draw_shadow_corner (cairo_t               *cr,
                    gboolean               inset,
903 904
                    const GskRoundedRect  *box,
                    const GskRoundedRect  *clip_box,
905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
                    float                  radius,
                    const GdkRGBA         *color,
                    GskCorner              corner,
                    cairo_rectangle_int_t *drawn_rect)
{
  float clip_radius;
  int x1, x2, x3, y1, y2, y3, x, y;
  GskRoundedRect corner_box;
  cairo_t *mask_cr;
  cairo_surface_t *mask;
  cairo_pattern_t *pattern;
  cairo_matrix_t matrix;
  float sx, sy;
  static GHashTable *corner_mask_cache = NULL;
  float max_other;
  CornerMask key;
  gboolean overlapped;

  clip_radius = gsk_cairo_blur_compute_pixels (radius);

  overlapped = FALSE;
  if (corner == GSK_CORNER_TOP_LEFT || corner == GSK_CORNER_BOTTOM_LEFT)
    {
      x1 = floor (box->bounds.origin.x - clip_radius);
      x2 = ceil (box->bounds.origin.x + box->corner[corner].width + clip_radius);
      x = x1;
      sx = 1;
      max_other = MAX(box->corner[GSK_CORNER_TOP_RIGHT].width, box->corner[GSK_CORNER_BOTTOM_RIGHT].width);
      x3 = floor (box->bounds.origin.x + box->bounds.size.width - max_other - clip_radius);
      if (x2 > x3)
        overlapped = TRUE;
    }
  else
    {
      x1 = floor (box->bounds.origin.x + box->bounds.size.width - box->corner[corner].width - clip_radius);
      x2 = ceil (box->bounds.origin.x + box->bounds.size.width + clip_radius);
      x = x2;
      sx = -1;
      max_other = MAX(box->corner[GSK_CORNER_TOP_LEFT].width, box->corner[GSK_CORNER_BOTTOM_LEFT].width);
      x3 = ceil (box->bounds.origin.x + max_other + clip_radius);
      if (x3 > x1)
        overlapped = TRUE;
    }

  if (corner == GSK_CORNER_TOP_LEFT || corner == GSK_CORNER_TOP_RIGHT)
    {
      y1 = floor (box->bounds.origin.y - clip_radius);
      y2 = ceil (box->bounds.origin.y + box->corner[corner].height + clip_radius);
      y = y1;
      sy = 1;
      max_other = MAX(box->corner[GSK_CORNER_BOTTOM_LEFT].height, box->corner[GSK_CORNER_BOTTOM_RIGHT].height);
      y3 = floor (box->bounds.origin.y + box->bounds.size.height - max_other - clip_radius);
      if (y2 > y3)
        overlapped = TRUE;
    }
  else
    {
      y1 = floor (box->bounds.origin.y + box->bounds.size.height - box->corner[corner].height - clip_radius);
      y2 = ceil (box->bounds.origin.y + box->bounds.size.height + clip_radius);
      y = y2;
      sy = -1;
      max_other = MAX(box->corner[GSK_CORNER_TOP_LEFT].height, box->corner[GSK_CORNER_TOP_RIGHT].height);
      y3 = ceil (box->bounds.origin.y + max_other + clip_radius);
      if (y3 > y1)
        overlapped = TRUE;
    }

  drawn_rect->x = x1;
  drawn_rect->y = y1;
  drawn_rect->width = x2 - x1;
  drawn_rect->height = y2 - y1;

  cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
  cairo_clip (cr);

  if (inset || overlapped)
    {
      /* Fall back to generic path if inset or if the corner radius
         runs into each other */
      draw_shadow (cr, inset, box, clip_box, radius, color, GSK_BLUR_X | GSK_BLUR_Y);
      return;
    }

  if (has_empty_clip (cr))
    return;

  /* At this point we're drawing a blurred outset corner. The only
   * things that affect the output of the blurred mask in this case
   * is:
   *
   * What corner this is, which defines the orientation (sx,sy)
   * and position (x,y)
   *
   * The blur radius (which also defines the clip_radius)
   *
   * The the horizontal and vertical corner radius
   *
   * We apply the first position and orientation when drawing the
   * mask, so we cache rendered masks based on the blur radius and the
   * corner radius.
   */
  if (corner_mask_cache == NULL)
    corner_mask_cache = g_hash_table_new_full ((GHashFunc)corner_mask_hash,
                                               (GEqualFunc)corner_mask_equal,
                                               g_free, (GDestroyNotify)cairo_surface_destroy);

  key.radius = radius;
  key.corner = box->corner[corner];

  mask = g_hash_table_lookup (corner_mask_cache, &key);
  if (mask == NULL)
    {
      mask = cairo_surface_create_similar_image (cairo_get_target (cr), CAIRO_FORMAT_A8,
                                                 drawn_rect->width + clip_radius,
                                                 drawn_rect->height + clip_radius);
      mask_cr = cairo_create (mask);
      gsk_rounded_rect_init_from_rect (&corner_box, &GRAPHENE_RECT_INIT (clip_radius, clip_radius, 2*drawn_rect->width, 2*drawn_rect->height), 0);
      corner_box.corner[0] = box->corner[corner];
      gsk_rounded_rect_path (&corner_box, mask_cr);
      cairo_fill (mask_cr);
      gsk_cairo_blur_surface (mask, radius, GSK_BLUR_X | GSK_BLUR_Y);
      cairo_destroy (mask_cr);
      g_hash_table_insert (corner_mask_cache, g_memdup (&key, sizeof (key)), mask);
    }

  gdk_cairo_set_source_rgba (cr, color);
  pattern = cairo_pattern_create_for_surface (mask);
  cairo_matrix_init_identity (&matrix);
  cairo_matrix_scale (&matrix, sx, sy);
  cairo_matrix_translate (&matrix, -x, -y);
  cairo_pattern_set_matrix (pattern, &matrix);
  cairo_mask (cr, pattern);
  cairo_pattern_destroy (pattern);
}

static void
draw_shadow_side (cairo_t               *cr,
                  gboolean               inset,
1043 1044
                  const GskRoundedRect  *box,
                  const GskRoundedRect  *clip_box,
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 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100
                  float                  radius,
                  const GdkRGBA         *color,
                  Side                   side,
                  cairo_rectangle_int_t *drawn_rect)
{
  GskBlurFlags blur_flags = GSK_BLUR_REPEAT;
  gdouble clip_radius;
  int x1, x2, y1, y2;

  clip_radius = gsk_cairo_blur_compute_pixels (radius);

  if (side == TOP || side == BOTTOM)
    {
      blur_flags |= GSK_BLUR_Y;
      x1 = floor (box->bounds.origin.x - clip_radius);
      x2 = ceil (box->bounds.origin.x + box->bounds.size.width + clip_radius);
    }
  else if (side == LEFT)
    {
      x1 = floor (box->bounds.origin.x -clip_radius);
      x2 = ceil (box->bounds.origin.x + clip_radius);
    }
  else
    {
      x1 = floor (box->bounds.origin.x + box->bounds.size.width -clip_radius);
      x2 = ceil (box->bounds.origin.x + box->bounds.size.width + clip_radius);
    }

  if (side == LEFT || side == RIGHT)
    {
      blur_flags |= GSK_BLUR_X;
      y1 = floor (box->bounds.origin.y - clip_radius);
      y2 = ceil (box->bounds.origin.y + box->bounds.size.height + clip_radius);
    }
  else if (side == TOP)
    {
      y1 = floor (box->bounds.origin.y -clip_radius);
      y2 = ceil (box->bounds.origin.y + clip_radius);
    }
  else
    {
      y1 = floor (box->bounds.origin.y + box->bounds.size.height -clip_radius);
      y2 = ceil (box->bounds.origin.y + box->bounds.size.height + clip_radius);
    }

  drawn_rect->x = x1;
  drawn_rect->y = y1;
  drawn_rect->width = x2 - x1;
  drawn_rect->height = y2 - y1;

  cairo_rectangle (cr, x1, y1, x2 - x1, y2 - y1);
  cairo_clip (cr);
  draw_shadow (cr, inset, box, clip_box, radius, color, blur_flags);
}

static gboolean
1101
needs_blur (double radius)
1102 1103 1104
{
  /* The code doesn't actually do any blurring for radius 1, as it
   * ends up with box filter size 1 */
1105
  if (radius <= 1.0)
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
    return FALSE;

  return TRUE;
}

static void
gsk_inset_shadow_node_draw (GskRenderNode *node,
                            cairo_t       *cr)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;
  GskRoundedRect box, clip_box;
  int clip_radius;
  double x1c, y1c, x2c, y2c;

  /* We don't need to draw invisible shadows */
  if (gdk_rgba_is_clear (&self->color))
    return;

  cairo_clip_extents (cr, &x1c, &y1c, &x2c, &y2c);
  if (!gsk_rounded_rect_intersects_rect (&self->outline, &GRAPHENE_RECT_INIT (x1c, y1c, x2c - x1c, y2c - y1c)))
    return;

  clip_radius = gsk_cairo_blur_compute_pixels (self->blur_radius);

  cairo_save (cr);

  gsk_rounded_rect_path (&self->outline, cr);
  cairo_clip (cr);

  gsk_rounded_rect_init_copy (&box, &self->outline);
  gsk_rounded_rect_offset (&box, self->dx, self->dy);
  gsk_rounded_rect_shrink (&box, self->spread, self->spread, self->spread, self->spread);

  gsk_rounded_rect_init_copy (&clip_box, &self->outline);
  gsk_rounded_rect_shrink (&clip_box, -clip_radius, -clip_radius, -clip_radius, -clip_radius);

1142
  if (!needs_blur (self->blur_radius))
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 1168 1169 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
    draw_shadow (cr, TRUE, &box, &clip_box, self->blur_radius, &self->color, GSK_BLUR_NONE);
  else
    {
      cairo_region_t *remaining;
      cairo_rectangle_int_t r;
      int i;

      /* For the blurred case we divide the rendering into 9 parts,
       * 4 of the corners, 4 for the horizonat/vertical lines and
       * one for the interior. We make the non-interior parts
       * large enought to fit the full radius of the blur, so that
       * the interior part can be drawn solidly.
       */

      /* In the inset case we want to paint the whole clip-box.
       * We could remove the part of "box" where the blur doesn't
       * reach, but computing that is a bit tricky since the
       * rounded corners are on the "inside" of it. */
      r.x = floor (clip_box.bounds.origin.x);
      r.y = floor (clip_box.bounds.origin.y);
      r.width = ceil (clip_box.bounds.origin.x + clip_box.bounds.size.width) - r.x;
      r.height = ceil (clip_box.bounds.origin.y + clip_box.bounds.size.height) - r.y;
      remaining = cairo_region_create_rectangle (&r);

      /* First do the corners of box */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_corner (cr, TRUE, &box, &clip_box, self->blur_radius, &self->color, i, &r);
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the sides */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_side (cr, TRUE, &box, &clip_box, self->blur_radius, &self->color, i, &r);
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the rest, which needs no blurring */

      cairo_save (cr);
      gdk_cairo_region (cr, remaining);
      cairo_clip (cr);
      draw_shadow (cr, TRUE, &box, &clip_box, self->blur_radius, &self->color, GSK_BLUR_NONE);
      cairo_restore (cr);

      cairo_region_destroy (remaining);
    }

  cairo_restore (cr);
}

1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
#define GSK_INSET_SHADOW_NODE_VARIANT_TYPE "(dddddddddddddddddddd)"

static GVariant *
gsk_inset_shadow_node_serialize (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  return g_variant_new (GSK_INSET_SHADOW_NODE_VARIANT_TYPE,
                        (double) self->outline.bounds.origin.x, (double) self->outline.bounds.origin.y,
                        (double) self->outline.bounds.size.width, (double) self->outline.bounds.size.height,
                        (double) self->outline.corner[0].width, (double) self->outline.corner[0].height,
                        (double) self->outline.corner[1].width, (double) self->outline.corner[1].height,
                        (double) self->outline.corner[2].width, (double) self->outline.corner[2].height,
                        (double) self->outline.corner[3].width, (double) self->outline.corner[3].height,
                        self->color.red, self->color.green,
                        self->color.blue, self->color.alpha,
                        (double) self->dx, (double) self->dy,
                        (double) self->spread, (double) self->blur_radius);
}

static GskRenderNode *
1230 1231
gsk_inset_shadow_node_deserialize (GVariant  *variant,
                                   GError   **error)
1232 1233 1234 1235
{
  double doutline[12], dx, dy, spread, radius;
  GdkRGBA color;

1236
  if (!check_variant_type (variant, GSK_INSET_SHADOW_NODE_VARIANT_TYPE, error))
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
    return NULL;

  g_variant_get (variant, GSK_INSET_SHADOW_NODE_VARIANT_TYPE,
                 &doutline[0], &doutline[1], &doutline[2], &doutline[3],
                 &doutline[4], &doutline[5], &doutline[6], &doutline[7],
                 &doutline[8], &doutline[9], &doutline[10], &doutline[11],
                 &color.red, &color.green, &color.blue, &color.alpha,
                 &dx, &dy, &spread, &radius);

  return gsk_inset_shadow_node_new (&(GskRoundedRect) {
                                        .bounds = GRAPHENE_RECT_INIT(doutline[0], doutline[1], doutline[2], doutline[3]),
                                        .corner = {
                                            GRAPHENE_SIZE_INIT (doutline[4], doutline[5]),
                                            GRAPHENE_SIZE_INIT (doutline[6], doutline[7]),
                                            GRAPHENE_SIZE_INIT (doutline[8], doutline[9]),
                                            GRAPHENE_SIZE_INIT (doutline[10], doutline[11])
                                        }
                                    },
                                    &color, dx, dy, spread, radius);
}

1258 1259 1260 1261 1262 1263
static const GskRenderNodeClass GSK_INSET_SHADOW_NODE_CLASS = {
  GSK_INSET_SHADOW_NODE,
  sizeof (GskInsetShadowNode),
  "GskInsetShadowNode",
  gsk_inset_shadow_node_finalize,
  gsk_inset_shadow_node_draw,
1264 1265
  gsk_inset_shadow_node_serialize,
  gsk_inset_shadow_node_deserialize
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
};

/**
 * gsk_inset_shadow_node_new:
 * @outline: outline of the region containing the shadow
 * @color: color of the shadow
 * @dx: horizontal offset of shadow
 * @dy: vertical offset of shadow
 * @spread: how far the shadow spreads towards the inside
 * @blur_radius: how much blur to apply to the shadow
 *
 * Creates a #GskRenderNode that will render an inset shadow
 * into the box given by @outline.
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_inset_shadow_node_new (const GskRoundedRect *outline,
                           const GdkRGBA        *color,
                           float                 dx,
                           float                 dy,
                           float                 spread,
                           float                 blur_radius)
{
  GskInsetShadowNode *self;

  g_return_val_if_fail (outline != NULL, NULL);
  g_return_val_if_fail (color != NULL, NULL);

1295
  self = (GskInsetShadowNode *) gsk_render_node_new (&GSK_INSET_SHADOW_NODE_CLASS, 0);
1296 1297 1298 1299 1300 1301 1302 1303

  gsk_rounded_rect_init_copy (&self->outline, outline);
  self->color = *color;
  self->dx = dx;
  self->dy = dy;
  self->spread = spread;
  self->blur_radius = blur_radius;

1304 1305
  graphene_rect_init_from_rect (&self->render_node.bounds, &self->outline.bounds);

1306 1307 1308
  return &self->render_node;
}

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
const GskRoundedRect *
gsk_inset_shadow_node_peek_outline (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), NULL);

  return &self->outline;
}

const GdkRGBA *
gsk_inset_shadow_node_peek_color (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), NULL);

  return &self->color;
}

float
gsk_inset_shadow_node_get_dx (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), 0.0f);

  return self->dx;
}

float
gsk_inset_shadow_node_get_dy (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), 0.0f);

  return self->dy;
}

float
gsk_inset_shadow_node_get_spread (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), 0.0f);

  return self->spread;
}

float
gsk_inset_shadow_node_get_blur_radius (GskRenderNode *node)
{
  GskInsetShadowNode *self = (GskInsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_INSET_SHADOW_NODE), 0.0f);

  return self->blur_radius;
}

1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 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 1503 1504 1505 1506 1507 1508 1509
/*** GSK_OUTSET_SHADOW_NODE ***/

typedef struct _GskOutsetShadowNode GskOutsetShadowNode;

struct _GskOutsetShadowNode
{
  GskRenderNode render_node;

  GskRoundedRect outline;
  GdkRGBA color;
  float dx;
  float dy;
  float spread;
  float blur_radius;
};

static void
gsk_outset_shadow_node_finalize (GskRenderNode *node)
{
}

static void
gsk_outset_shadow_get_extents (GskOutsetShadowNode *self,
                               float               *top,
                               float               *right,
                               float               *bottom,
                               float               *left)
{
  float clip_radius;

  clip_radius = gsk_cairo_blur_compute_pixels (self->blur_radius);
  *top = MAX (0, clip_radius + self->spread - self->dy);
  *right = MAX (0, ceil (clip_radius + self->spread + self->dx));
  *bottom = MAX (0, ceil (clip_radius + self->spread + self->dy));
  *left = MAX (0, ceil (clip_radius + self->spread - self->dx));
}

static void
gsk_outset_shadow_node_draw (GskRenderNode *node,
                             cairo_t       *cr)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;
  GskRoundedRect box, clip_box;
  int clip_radius;
  double x1c, y1c, x2c, y2c;
  float top, right, bottom, left;

  /* We don't need to draw invisible shadows */
  if (gdk_rgba_is_clear (&self->color))
    return;

  cairo_clip_extents (cr, &x1c, &y1c, &x2c, &y2c);
  if (gsk_rounded_rect_contains_rect (&self->outline, &GRAPHENE_RECT_INIT (x1c, y1c, x2c - x1c, y2c - y1c)))
    return;

  clip_radius = gsk_cairo_blur_compute_pixels (self->blur_radius);

  cairo_save (cr);

  gsk_rounded_rect_init_copy (&clip_box, &self->outline);
  gsk_outset_shadow_get_extents (self, &top, &right, &bottom, &left);
  gsk_rounded_rect_shrink (&clip_box, -top, -right, -bottom, -left);

  cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
  gsk_rounded_rect_path (&self->outline, cr);
  cairo_rectangle (cr,
                   clip_box.bounds.origin.x, clip_box.bounds.origin.y,
                   clip_box.bounds.size.width, clip_box.bounds.size.height);

  cairo_clip (cr);

  gsk_rounded_rect_init_copy (&box, &self->outline);
  gsk_rounded_rect_offset (&box, self->dx, self->dy);
  gsk_rounded_rect_shrink (&box, -self->spread, -self->spread, -self->spread, -self->spread);

  if (!needs_blur (self->blur_radius))
    draw_shadow (cr, FALSE, &box, &clip_box, self->blur_radius, &self->color, GSK_BLUR_NONE);
  else
    {
      int i;
      cairo_region_t *remaining;
      cairo_rectangle_int_t r;

      /* For the blurred case we divide the rendering into 9 parts,
       * 4 of the corners, 4 for the horizonat/vertical lines and
       * one for the interior. We make the non-interior parts
       * large enought to fit the full radius of the blur, so that
       * the interior part can be drawn solidly.
       */

      /* In the outset case we want to paint the entire box, plus as far
       * as the radius reaches from it */
      r.x = floor (box.bounds.origin.x - clip_radius);
      r.y = floor (box.bounds.origin.y - clip_radius);
      r.width = ceil (box.bounds.origin.x + box.bounds.size.width + clip_radius) - r.x;
      r.height = ceil (box.bounds.origin.y + box.bounds.size.height + clip_radius) - r.y;

      remaining = cairo_region_create_rectangle (&r);

      /* First do the corners of box */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_corner (cr, FALSE, &box, &clip_box, self->blur_radius, &self->color, i, &r);
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the sides */
      for (i = 0; i < 4; i++)
	{
	  cairo_save (cr);
          /* Always clip with remaining to ensure we never draw any area twice */
          gdk_cairo_region (cr, remaining);
          cairo_clip (cr);
	  draw_shadow_side (cr, FALSE, &box, &clip_box, self->blur_radius, &self->color, i, &r);
	  cairo_restore (cr);

	  /* We drew the region, remove it from remaining */
	  cairo_region_subtract_rectangle (remaining, &r);
	}

      /* Then the rest, which needs no blurring */

      cairo_save (cr);
      gdk_cairo_region (cr, remaining);
      cairo_clip (cr);
      draw_shadow (cr, FALSE, &box, &clip_box, self->blur_radius, &self->color, GSK_BLUR_NONE);
      cairo_restore (cr);

      cairo_region_destroy (remaining);
    }

  cairo_restore (cr);
}

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530
#define GSK_OUTSET_SHADOW_NODE_VARIANT_TYPE "(dddddddddddddddddddd)"

static GVariant *
gsk_outset_shadow_node_serialize (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  return g_variant_new (GSK_OUTSET_SHADOW_NODE_VARIANT_TYPE,
                        (double) self->outline.bounds.origin.x, (double) self->outline.bounds.origin.y,
                        (double) self->outline.bounds.size.width, (double) self->outline.bounds.size.height,
                        (double) self->outline.corner[0].width, (double) self->outline.corner[0].height,
                        (double) self->outline.corner[1].width, (double) self->outline.corner[1].height,
                        (double) self->outline.corner[2].width, (double) self->outline.corner[2].height,
                        (double) self->outline.corner[3].width, (double) self->outline.corner[3].height,
                        self->color.red, self->color.green,
                        self->color.blue, self->color.alpha,
                        (double) self->dx, (double) self->dy,
                        (double) self->spread, (double) self->blur_radius);
}

static GskRenderNode *
1531 1532
gsk_outset_shadow_node_deserialize (GVariant  *variant,
                                    GError   **error)
1533 1534 1535 1536
{
  double doutline[12], dx, dy, spread, radius;
  GdkRGBA color;

1537
  if (!check_variant_type (variant, GSK_OUTSET_SHADOW_NODE_VARIANT_TYPE, error))
1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558
    return NULL;

  g_variant_get (variant, GSK_INSET_SHADOW_NODE_VARIANT_TYPE,
                 &doutline[0], &doutline[1], &doutline[2], &doutline[3],
                 &doutline[4], &doutline[5], &doutline[6], &doutline[7],
                 &doutline[8], &doutline[9], &doutline[10], &doutline[11],
                 &color.red, &color.green, &color.blue, &color.alpha,
                 &dx, &dy, &spread, &radius);

  return gsk_outset_shadow_node_new (&(GskRoundedRect) {
                                         .bounds = GRAPHENE_RECT_INIT(doutline[0], doutline[1], doutline[2], doutline[3]),
                                         .corner = {
                                             GRAPHENE_SIZE_INIT (doutline[4], doutline[5]),
                                             GRAPHENE_SIZE_INIT (doutline[6], doutline[7]),
                                             GRAPHENE_SIZE_INIT (doutline[8], doutline[9]),
                                             GRAPHENE_SIZE_INIT (doutline[10], doutline[11])
                                         }
                                     },
                                     &color, dx, dy, spread, radius);
}

1559 1560 1561 1562 1563 1564
static const GskRenderNodeClass GSK_OUTSET_SHADOW_NODE_CLASS = {
  GSK_OUTSET_SHADOW_NODE,
  sizeof (GskOutsetShadowNode),
  "GskOutsetShadowNode",
  gsk_outset_shadow_node_finalize,
  gsk_outset_shadow_node_draw,
1565 1566
  gsk_outset_shadow_node_serialize,
  gsk_outset_shadow_node_deserialize
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
};

/**
 * gsk_outset_shadow_node_new:
 * @outline: outline of the region surrounded by shadow
 * @color: color of the shadow
 * @dx: horizontal offset of shadow
 * @dy: vertical offset of shadow
 * @spread: how far the shadow spreads towards the inside
 * @blur_radius: how much blur to apply to the shadow
 *
 * Creates a #GskRenderNode that will render an outset shadow
 * around the box given by @outline.
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_outset_shadow_node_new (const GskRoundedRect *outline,
                            const GdkRGBA        *color,
                            float                 dx,
                            float                 dy,
                            float                 spread,
                            float                 blur_radius)
{
  GskOutsetShadowNode *self;
1592
  float top, right, bottom, left;
1593 1594 1595 1596

  g_return_val_if_fail (outline != NULL, NULL);
  g_return_val_if_fail (color != NULL, NULL);

1597
  self = (GskOutsetShadowNode *) gsk_render_node_new (&GSK_OUTSET_SHADOW_NODE_CLASS, 0);
1598 1599 1600 1601 1602 1603 1604 1605

  gsk_rounded_rect_init_copy (&self->outline, outline);
  self->color = *color;
  self->dx = dx;
  self->dy = dy;
  self->spread = spread;
  self->blur_radius = blur_radius;

1606 1607 1608 1609 1610 1611 1612 1613 1614
  gsk_outset_shadow_get_extents (self, &top, &right, &bottom, &left);

  graphene_rect_init_from_rect (&self->render_node.bounds, &self->outline.bounds);

  self->render_node.bounds.origin.x -= left;
  self->render_node.bounds.origin.y -= top;
  self->render_node.bounds.size.width += left + right;
  self->render_node.bounds.size.height += top + bottom;

1615 1616 1617
  return &self->render_node;
}

1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677
const GskRoundedRect *
gsk_outset_shadow_node_peek_outline (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), NULL);

  return &self->outline;
}

const GdkRGBA *
gsk_outset_shadow_node_peek_color (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), NULL);

  return &self->color;
}

float
gsk_outset_shadow_node_get_dx (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), 0.0f);

  return self->dx;
}

float
gsk_outset_shadow_node_get_dy (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), 0.0f);

  return self->dy;
}

float
gsk_outset_shadow_node_get_spread (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), 0.0f);

  return self->spread;
}

float
gsk_outset_shadow_node_get_blur_radius (GskRenderNode *node)
{
  GskOutsetShadowNode *self = (GskOutsetShadowNode *) node;

  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_OUTSET_SHADOW_NODE), 0.0f);

  return self->blur_radius;
}

1678 1679
/*** GSK_CAIRO_NODE ***/

1680 1681 1682 1683 1684 1685 1686 1687 1688
typedef struct _GskCairoNode GskCairoNode;

struct _GskCairoNode
{
  GskRenderNode render_node;

  cairo_surface_t *surface;
};

1689 1690 1691
static void
gsk_cairo_node_finalize (GskRenderNode *node)
{
1692 1693 1694 1695
  GskCairoNode *self = (GskCairoNode *) node;

  if (self->surface)
    cairo_surface_destroy (self->surface);
1696 1697
}

1698 1699 1700 1701 1702 1703 1704 1705 1706
static void
gsk_cairo_node_draw (GskRenderNode *node,
                     cairo_t       *cr)
{
  GskCairoNode *self = (GskCairoNode *) node;

  if (self->surface == NULL)
    return;

1707
  cairo_set_source_surface (cr, self->surface, node->bounds.origin.x, node->bounds.origin.y);
1708 1709 1710
  cairo_paint (cr);
}

1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
#define GSK_CAIRO_NODE_VARIANT_TYPE "(dddduuau)"

static GVariant *
gsk_cairo_node_serialize (GskRenderNode *node)
{
  GskCairoNode *self = (GskCairoNode *) node;

  if (self->surface == NULL)
    {
      return g_variant_new ("(dddduu@au)",
                            (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                            (double) node->bounds.size.width, (double) node->bounds.size.height,
                            (guint32) 0, (guint32) 0,
                            g_variant_new_array (G_VARIANT_TYPE ("u"), NULL, 0));
    }
  else if (cairo_image_surface_get_width (self->surface) * 4 == cairo_image_surface_get_stride (self->surface))
    {
      return g_variant_new ("(dddduu@au)",
                            (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                            (double) node->bounds.size.width, (double) node->bounds.size.height,
                            (guint32) cairo_image_surface_get_width (self->surface),
                            (guint32) cairo_image_surface_get_height (self->surface),
                            g_variant_new_fixed_array (G_VARIANT_TYPE ("u"),
                                                       cairo_image_surface_get_data (self->surface),
                                                       cairo_image_surface_get_width (self->surface)
                                                       * cairo_image_surface_get_height (self->surface),
                                                       sizeof (guint32)));
    }
  else
    {
1741 1742 1743
      int width, height;
      int stride, i;
      guchar *mem_surface, *data;
1744
      GVariant *result;
1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755

      width = cairo_image_surface_get_width (self->surface);
      height = cairo_image_surface_get_height (self->surface);
      stride = cairo_image_surface_get_stride (self->surface);
      data = cairo_image_surface_get_data (self->surface);

      mem_surface = (guchar *) g_malloc (width * height * 4);

      for (i = 0; i < height; i++)
        memcpy (mem_surface + i * width * 4, data + i * stride, width * 4);

1756 1757 1758 1759 1760 1761 1762 1763 1764
      result = g_variant_new ("(dddduu@au)",
                             (double) node->bounds.origin.x, (double) node->bounds.origin.y,
                             (double) node->bounds.size.width, (double) node->bounds.size.height,
                             (guint32) width,
                             (guint32) height,
                             g_variant_new_fixed_array (G_VARIANT_TYPE ("u"),
                                                        mem_surface,
                                                        width * height,
                                                        sizeof (guint32)));
1765
      g_free (mem_surface);
1766 1767

      return result;
1768 1769 1770 1771 1772 1773
    }
}

const cairo_user_data_key_t gsk_surface_variant_key;

static GskRenderNode *
1774 1775
gsk_cairo_node_deserialize (GVariant  *variant,
                            GError   **error)
1776 1777 1778 1779 1780 1781 1782 1783
{
  GskRenderNode *result;
  cairo_surface_t *surface;
  double x, y, width, height;
  guint32 surface_width, surface_height;
  GVariant *pixel_variant;
  gsize n_pixels;

1784
  if (!check_variant_type (variant, GSK_CAIRO_NODE_VARIANT_TYPE, error))
1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
    return NULL;

  g_variant_get (variant, "(dddduu@au)",
                 &x, &y, &width, &height,
                 &surface_width, &surface_height,
                 &pixel_variant);

  if (surface_width == 0 || surface_height == 0)
    {
      g_variant_unref (pixel_variant);
      return gsk_cairo_node_new (&GRAPHENE_RECT_INIT (x, y, width, height));
    }

  /* XXX: Make this work without copying the data */
  surface = cairo_image_surface_create_for_data ((guchar *) g_variant_get_fixed_array (pixel_variant, &n_pixels, sizeof (guint32)),
                                                 CAIRO_FORMAT_ARGB32,
                                                 surface_width, surface_height, surface_width * 4);
  cairo_surface_set_user_data (surface,
                               &gsk_surface_variant_key,
                               pixel_variant,
                               (cairo_destroy_func_t) g_variant_unref);

  result = gsk_cairo_node_new_for_surface (&GRAPHENE_RECT_INIT (x, y, width, height), surface);

  cairo_surface_destroy (surface);

  return result;
}

1814 1815
static const GskRenderNodeClass GSK_CAIRO_NODE_CLASS = {
  GSK_CAIRO_NODE,
1816
  sizeof (GskCairoNode),
1817
  "GskCairoNode",
1818
  gsk_cairo_node_finalize,
1819
  gsk_cairo_node_draw,
1820 1821
  gsk_cairo_node_serialize,
  gsk_cairo_node_deserialize
1822 1823
};

1824 1825
const cairo_surface_t *
gsk_cairo_node_peek_surface (GskRenderNode *node)
1826
{
1827 1828
  GskCairoNode *self = (GskCairoNode *) node;

1829
  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_CAIRO_NODE), NULL);
1830

1831
  return self->surface;
1832 1833
}

1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
GskRenderNode *
gsk_cairo_node_new_for_surface (const graphene_rect_t *bounds,
                                cairo_surface_t       *surface)
{
  GskCairoNode *self;

  g_return_val_if_fail (bounds != NULL, NULL);

  self = (GskCairoNode *) gsk_render_node_new (&GSK_CAIRO_NODE_CLASS, 0);

  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
  self->surface = cairo_surface_reference (surface);

  return &self->render_node;
}

1850 1851
/**
 * gsk_cairo_node_new:
1852
 * @bounds: the rectangle to render to
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
 *
 * Creates a #GskRenderNode that will render a cairo surface
 * into the area given by @bounds. You can draw to the cairo
 * surface using gsk_cairo_node_get_draw_context()
 *
 * Returns: A new #GskRenderNode
 */
GskRenderNode *
gsk_cairo_node_new (const graphene_rect_t *bounds)
{
1863
  GskCairoNode *self;
1864 1865 1866

  g_return_val_if_fail (bounds != NULL, NULL);

1867
  self = (GskCairoNode *) gsk_render_node_new (&GSK_CAIRO_NODE_CLASS, 0);
1868

1869
  graphene_rect_init_from_rect (&self->render_node.bounds, bounds);
1870

1871
  return &self->render_node;
1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890
}

/**
 * gsk_cairo_node_get_draw_context:
 * @node: a cairo #GskRenderNode
 * @renderer: (nullable): Renderer to optimize for or %NULL for any
 *
 * Creates a Cairo context for drawing using the surface associated
 * to the render node.
 * If no surface exists yet, a surface will be created optimized for
 * rendering to @renderer.
 *
 * Returns: (transfer full): a Cairo context used for drawing; use
 *   cairo_destroy() when done drawing
 */
cairo_t *
gsk_cairo_node_get_draw_context (GskRenderNode *node,
                                 GskRenderer   *renderer)
{
1891
  GskCairoNode *self = (GskCairoNode *) node;
1892 1893 1894
  int width, height;
  cairo_t *res;

1895
  g_return_val_if_fail (GSK_IS_RENDER_NODE_TYPE (node, GSK_CAIRO_NODE), NULL);
1896 1897
  g_return_val_if_fail (renderer == NULL || GSK_IS_RENDERER (renderer), NULL);

1898 1899
  width = ceilf (node->bounds.size.width);
  height = ceilf (node->bounds.size.height);
1900 1901 1902 1903 1904 1905 1906

  if (width <= 0 || height <= 0)
    {
      cairo_surface_t *surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 0, 0);
      res = cairo_create (surface);
      cairo_surface_destroy (surface);
    }
1907
  else if (self->surface == NULL)
1908 1909 1910
    {
      if (renderer)
        {
1911
          self->surface = gsk_renderer_create_cairo_surface (renderer,
1912
                                                             CAIRO_FORMAT_ARGB32,
1913 1914
                                                             ceilf (node->bounds.size.width),
                                                             ceilf (node->bounds.size.height));
1915 1916 1917
        }
      else
        {
1918
          self->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
1919 1920
                                                      ceilf (node->bounds.size.width),
                                                      ceilf (node->bounds.size.height));
1921
        }
1922
      res = cairo_create (self->surface);
1923 1924 1925
    }
  else
    {
1926
      res = cairo_create (self->surface);
1927 1928
    }

1929
  cairo_translate (res, -node->bounds.origin.x, -node->bounds.origin.y);
1930 1931

  cairo_rectangle (res,
1932 1933
                   node->bounds.origin.x, node->bounds.origin.y,
                   node->bounds.size.width, node->bounds.size.height);
1934 1935
  cairo_clip (res);

1936
  if (renderer && GSK_RENDERER_DEBUG_CHECK (renderer, SURFACE))
1937 1938 1939 1940 1941 1942 1943
    {
      const char *prefix;
      prefix = g_getenv ("GSK_DEBUG_PREFIX");
      if (!prefix || g_str_has_prefix (node->name, prefix))
        {
          cairo_save (res);
          cairo_rectangle (res,
1944 1945
                           node->bounds.origin.x + 1, node->bounds.origin.y + 1,
                           node->bounds.size.width - 2, node->bounds.size.height - 2);
1946 1947 1948 1949 1950 1951 1952