meta-shaped-texture.c 32.6 KB
Newer Older
1 2
/*
 * Authored By Neil Roberts  <neil@linux.intel.com>
3
 * and Jasper St. Pierre <jstpierre@mecheye.net>
4 5
 *
 * Copyright (C) 2008 Intel Corporation
6
 * Copyright (C) 2012 Red Hat, Inc.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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 2 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
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

24 25 26 27 28 29
/**
 * SECTION:meta-shaped-texture
 * @title: MetaShapedTexture
 * @short_description: An actor to draw a masked texture.
 */

30 31
#include <config.h>

32
#include <meta/meta-shaped-texture.h>
33
#include <meta/util.h>
34
#include "clutter-utils.h"
35
#include "meta-texture-tower.h"
Thomas Wood's avatar
Thomas Wood committed
36

37
#include "meta-shaped-texture-private.h"
38 39 40
#include "meta-wayland-private.h"
#include <cogl/cogl-wayland-server.h>

Thomas Wood's avatar
Thomas Wood committed
41
#include <clutter/clutter.h>
42
#include <cogl/cogl.h>
43
#include <cogl/cogl-texture-pixmap-x11.h>
44
#include <gdk/gdk.h> /* for gdk_rectangle_intersect() */
45

46
static void meta_shaped_texture_dispose  (GObject    *object);
47

48 49 50
static void meta_shaped_texture_paint (ClutterActor       *actor);
static void meta_shaped_texture_pick  (ClutterActor       *actor,
				       const ClutterColor *color);
51

52 53 54 55 56 57 58 59 60
static void meta_shaped_texture_get_preferred_width (ClutterActor *self,
                                                     gfloat        for_height,
                                                     gfloat       *min_width_p,
                                                     gfloat       *natural_width_p);

static void meta_shaped_texture_get_preferred_height (ClutterActor *self,
                                                      gfloat        for_width,
                                                      gfloat       *min_height_p,
                                                      gfloat       *natural_height_p);
61

62 63
static gboolean meta_shaped_texture_get_paint_volume (ClutterActor *self, ClutterPaintVolume *volume);

64 65 66 67 68 69 70
typedef enum _MetaShapedTextureType
{
  META_SHAPED_TEXTURE_TYPE_X11_PIXMAP,
  META_SHAPED_TEXTURE_TYPE_WAYLAND_SURFACE,
} MetaShapedTextureType;


71
G_DEFINE_TYPE (MetaShapedTexture, meta_shaped_texture,
72
               CLUTTER_TYPE_ACTOR);
73

74 75 76
#define META_SHAPED_TEXTURE_GET_PRIVATE(obj) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((obj), META_TYPE_SHAPED_TEXTURE, \
                                MetaShapedTexturePrivate))
77

78
struct _MetaShapedTexturePrivate
79
{
80
  MetaTextureTower *paint_tower;
81 82 83 84 85 86 87 88 89 90 91 92 93

  MetaShapedTextureType type;
  union {
    struct {
      Pixmap pixmap;
    } x11;
    struct {
      MetaWaylandSurface *surface;
    } wayland;
  };

  CoglTexture *texture;

94
  CoglTexture *mask_texture;
95

Owen W. Taylor's avatar
Owen W. Taylor committed
96
  cairo_region_t *clip_region;
97
  cairo_region_t *input_shape_region;
98
  cairo_region_t *opaque_region;
99

100
  guint tex_width, tex_height;
101

102
  guint create_mipmaps : 1;
103 104 105
};

static void
106
meta_shaped_texture_class_init (MetaShapedTextureClass *klass)
107 108 109 110
{
  GObjectClass *gobject_class = (GObjectClass *) klass;
  ClutterActorClass *actor_class = (ClutterActorClass *) klass;

111
  gobject_class->dispose = meta_shaped_texture_dispose;
112

113 114
  actor_class->get_preferred_width = meta_shaped_texture_get_preferred_width;
  actor_class->get_preferred_height = meta_shaped_texture_get_preferred_height;
115 116
  actor_class->paint = meta_shaped_texture_paint;
  actor_class->pick = meta_shaped_texture_pick;
117
  actor_class->get_paint_volume = meta_shaped_texture_get_paint_volume;
118

119
  g_type_class_add_private (klass, sizeof (MetaShapedTexturePrivate));
120 121 122
}

static void
123
meta_shaped_texture_init (MetaShapedTexture *self)
124
{
125
  MetaShapedTexturePrivate *priv;
126

127
  priv = self->priv = META_SHAPED_TEXTURE_GET_PRIVATE (self);
128

129
  priv->paint_tower = meta_texture_tower_new ();
130 131

  priv->type = META_SHAPED_TEXTURE_TYPE_X11_PIXMAP;
132
  priv->texture = NULL;
133

134
  priv->mask_texture = NULL;
135
  priv->create_mipmaps = TRUE;
136 137 138
}

static void
139
meta_shaped_texture_dispose (GObject *object)
140
{
141 142
  MetaShapedTexture *self = (MetaShapedTexture *) object;
  MetaShapedTexturePrivate *priv = self->priv;
143

144
  if (priv->paint_tower)
145
    meta_texture_tower_free (priv->paint_tower);
146 147
  priv->paint_tower = NULL;

148
  g_clear_pointer (&priv->texture, cogl_object_unref);
149
  g_clear_pointer (&priv->opaque_region, cairo_region_destroy);
150

151
  meta_shaped_texture_set_mask_texture (self, NULL);
152
  meta_shaped_texture_set_clip_region (self, NULL);
153

154
  G_OBJECT_CLASS (meta_shaped_texture_parent_class)->dispose (object);
155 156
}

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
static CoglPipeline *
get_unmasked_pipeline (CoglContext *ctx)
{
  return cogl_pipeline_new (ctx);
}

static CoglPipeline *
get_masked_pipeline (CoglContext *ctx)
{
  static CoglPipeline *template = NULL;
  if (G_UNLIKELY (template == NULL))
    {
      template = cogl_pipeline_new (ctx);
      cogl_pipeline_set_layer_combine (template, 1,
                                       "RGBA = MODULATE (PREVIOUS, TEXTURE[A])",
                                       NULL);
    }

  return cogl_pipeline_copy (template);
}

178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
static CoglPipeline *
get_unblended_pipeline (CoglContext *ctx)
{
  static CoglPipeline *template = NULL;
  if (G_UNLIKELY (template == NULL))
    {
      CoglColor color;
      template = cogl_pipeline_new (ctx);
      cogl_color_init_from_4ub (&color, 255, 255, 255, 255);
      cogl_pipeline_set_blend (template,
                               "RGBA = ADD (SRC_COLOR, 0)",
                               NULL);
      cogl_pipeline_set_color (template, &color);
    }

  return cogl_pipeline_copy (template);
}

static void
paint_clipped_rectangle (CoglFramebuffer       *fb,
                         CoglPipeline          *pipeline,
                         cairo_rectangle_int_t *rect,
                         ClutterActorBox       *alloc)
{
  float coords[8];
  float x1, y1, x2, y2;

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

  coords[0] = rect->x / (alloc->x2 - alloc->x1);
  coords[1] = rect->y / (alloc->y2 - alloc->y1);
  coords[2] = (rect->x + rect->width) / (alloc->x2 - alloc->x1);
  coords[3] = (rect->y + rect->height) / (alloc->y2 - alloc->y1);

  coords[4] = coords[0];
  coords[5] = coords[1];
  coords[6] = coords[2];
  coords[7] = coords[3];

  cogl_framebuffer_draw_multitextured_rectangle (fb, pipeline,
                                                 x1, y1, x2, y2,
                                                 &coords[0], 8);

}


227 228 229 230 231 232 233 234 235 236 237 238 239 240
static void
set_cogl_texture (MetaShapedTexture *stex,
                  CoglTexture       *cogl_tex)
{
  MetaShapedTexturePrivate *priv;
  guint width, height;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));

  priv = stex->priv;

  if (priv->texture)
    cogl_object_unref (priv->texture);

241
  priv->texture = cogl_object_ref (cogl_tex);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270

  if (cogl_tex != NULL)
    {
      width = cogl_texture_get_width (COGL_TEXTURE (cogl_tex));
      height = cogl_texture_get_height (COGL_TEXTURE (cogl_tex));

      if (width != priv->tex_width ||
          height != priv->tex_height)
        {
          priv->tex_width = width;
          priv->tex_height = height;

          clutter_actor_queue_relayout (CLUTTER_ACTOR (stex));
        }
    }
  else
    {
      /* size changed to 0 going to an invalid handle */
      priv->tex_width = 0;
      priv->tex_height = 0;
      clutter_actor_queue_relayout (CLUTTER_ACTOR (stex));
    }

  /* NB: We don't queue a redraw of the actor here because we don't
   * know how much of the buffer has changed with respect to the
   * previous buffer. We only queue a redraw in response to surface
   * damage. */
}

271
static void
272
meta_shaped_texture_paint (ClutterActor *actor)
273
{
274 275
  MetaShapedTexture *stex = (MetaShapedTexture *) actor;
  MetaShapedTexturePrivate *priv = stex->priv;
276
  guint tex_width, tex_height;
277
  guchar opacity;
278
  CoglContext *ctx;
279 280 281 282
  CoglFramebuffer *fb;
  CoglPipeline *pipeline = NULL;
  CoglTexture *paint_tex;
  ClutterActorBox alloc;
283
  cairo_region_t *blended_region = NULL;
284
  CoglPipelineFilter filter;
285

Owen W. Taylor's avatar
Owen W. Taylor committed
286
  if (priv->clip_region && cairo_region_is_empty (priv->clip_region))
287 288
    return;

289 290 291
  if (!CLUTTER_ACTOR_IS_REALIZED (CLUTTER_ACTOR (stex)))
    clutter_actor_realize (CLUTTER_ACTOR (stex));

292 293 294 295
  /* The GL EXT_texture_from_pixmap extension does allow for it to be
   * used together with SGIS_generate_mipmap, however this is very
   * rarely supported. Also, even when it is supported there
   * are distinct performance implications from:
296
   *
297 298
   *  - Updating mipmaps that we don't need
   *  - Having to reallocate pixmaps on the server into larger buffers
299
   *
300 301 302 303 304 305
   * So, we just unconditionally use our mipmap emulation code. If we
   * wanted to use SGIS_generate_mipmap, we'd have to  query COGL to
   * see if it was supported (no API currently), and then if and only
   * if that was the case, set the clutter texture quality to HIGH.
   * Setting the texture quality to high without SGIS_generate_mipmap
   * support for TFP textures will result in fallbacks to XGetImage.
306
   */
307
  if (priv->create_mipmaps)
308
    paint_tex = meta_texture_tower_get_paint_texture (priv->paint_tower);
309
  else
310
    paint_tex = COGL_TEXTURE (priv->texture);
311

312
  if (paint_tex == NULL)
313
    return;
314

315 316
  tex_width = priv->tex_width;
  tex_height = priv->tex_height;
317

318
  if (tex_width == 0 || tex_height == 0) /* no contents yet */
319 320
    return;

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  /* Use nearest-pixel interpolation if the texture is unscaled. This
   * improves performance, especially with software rendering.
   */

  filter = COGL_PIPELINE_FILTER_LINEAR;

  if (!clutter_actor_is_in_clone_paint (actor))
    {
      int x_origin, y_origin;

      if (meta_actor_is_untransformed (actor,
                                       &x_origin,
                                       &y_origin))
        filter = COGL_PIPELINE_FILTER_NEAREST;
    }

337
  ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
338
  fb = cogl_get_draw_framebuffer ();
339

340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
  opacity = clutter_actor_get_paint_opacity (actor);
  clutter_actor_get_allocation_box (actor, &alloc);

  if (priv->opaque_region != NULL && opacity == 255)
    {
      CoglPipeline *opaque_pipeline;
      cairo_region_t *region;
      int n_rects;
      int i;

      if (priv->clip_region != NULL)
        {
          region = cairo_region_copy (priv->clip_region);
          cairo_region_intersect (region, priv->opaque_region);
        }
      else
        {
          region = cairo_region_reference (priv->opaque_region);
        }

      if (cairo_region_is_empty (region))
        goto paint_blended;

      opaque_pipeline = get_unblended_pipeline (ctx);
      cogl_pipeline_set_layer_texture (opaque_pipeline, 0, paint_tex);
365
      cogl_pipeline_set_layer_filters (opaque_pipeline, 0, filter, filter);
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398

      n_rects = cairo_region_num_rectangles (region);
      for (i = 0; i < n_rects; i++)
        {
          cairo_rectangle_int_t rect;
          cairo_region_get_rectangle (region, i, &rect);
          paint_clipped_rectangle (fb, opaque_pipeline, &rect, &alloc);
        }

      cogl_object_unref (opaque_pipeline);

      if (priv->clip_region != NULL)
        {
          blended_region = cairo_region_copy (priv->clip_region);
        }
      else
        {
          cairo_rectangle_int_t rect = { 0, 0, tex_width, tex_height };
          blended_region = cairo_region_create_rectangle (&rect);
        }

      cairo_region_subtract (blended_region, priv->opaque_region);

    paint_blended:
      cairo_region_destroy (region);
    }

  if (blended_region == NULL && priv->clip_region != NULL)
    blended_region = cairo_region_reference (priv->clip_region);

  if (blended_region != NULL && cairo_region_is_empty (blended_region))
    goto out;

399
  if (priv->mask_texture == NULL)
400
    {
401
      pipeline = get_unmasked_pipeline (ctx);
402 403
    }
  else
404
    {
405
      pipeline = get_masked_pipeline (ctx);
406
      cogl_pipeline_set_layer_texture (pipeline, 1, priv->mask_texture);
407
      cogl_pipeline_set_layer_filters (pipeline, 1, filter, filter);
408 409
    }

410
  cogl_pipeline_set_layer_texture (pipeline, 0, paint_tex);
411
  cogl_pipeline_set_layer_filters (pipeline, 0, filter, filter);
412

413 414
  {
    CoglColor color;
415
    cogl_color_init_from_4ub (&color, opacity, opacity, opacity, opacity);
416
    cogl_pipeline_set_color (pipeline, &color);
417
  }
418

419
  if (blended_region != NULL)
420 421 422 423 424 425 426
    {
      int n_rects;

      /* Limit to how many separate rectangles we'll draw; beyond this just
       * fall back and draw the whole thing */
#     define MAX_RECTS 16

427
      n_rects = cairo_region_num_rectangles (blended_region);
Owen W. Taylor's avatar
Owen W. Taylor committed
428
      if (n_rects <= MAX_RECTS)
429
	{
430 431
          int i;
          cairo_rectangle_int_t tex_rect = { 0, 0, tex_width, tex_height };
432

433 434
	  for (i = 0; i < n_rects; i++)
	    {
Owen W. Taylor's avatar
Owen W. Taylor committed
435
	      cairo_rectangle_int_t rect;
436

437
	      cairo_region_get_rectangle (blended_region, i, &rect);
438

439 440 441
	      if (!gdk_rectangle_intersect (&tex_rect, &rect, &rect))
		continue;

442
              paint_clipped_rectangle (fb, pipeline, &rect, &alloc);
443
            }
444

445
          goto out;
446 447 448
	}
    }

449 450 451 452
  cogl_framebuffer_draw_rectangle (fb, pipeline,
                                   0, 0,
                                   alloc.x2 - alloc.x1,
                                   alloc.y2 - alloc.y1);
453 454

 out:
455 456
  if (pipeline != NULL)
    cogl_object_unref (pipeline);
457 458
  if (blended_region != NULL)
    cairo_region_destroy (blended_region);
459 460 461
}

static void
462 463
meta_shaped_texture_pick (ClutterActor       *actor,
			  const ClutterColor *color)
464
{
465 466
  MetaShapedTexture *stex = (MetaShapedTexture *) actor;
  MetaShapedTexturePrivate *priv = stex->priv;
467

468 469 470 471
  if (!clutter_actor_should_pick_paint (actor) ||
      (priv->clip_region && cairo_region_is_empty (priv->clip_region)))
    return;

472
  /* If there is no region then use the regular pick */
473
  if (priv->input_shape_region == NULL)
474
    CLUTTER_ACTOR_CLASS (meta_shaped_texture_parent_class)->pick (actor, color);
475
  else
476
    {
477 478 479
      int n_rects;
      float *rectangles;
      int i;
480 481 482 483
      CoglPipeline *pipeline;
      CoglContext *ctx;
      CoglFramebuffer *fb;
      CoglColor cogl_color;
484

485 486 487 488 489 490 491 492 493 494
      /* Note: We don't bother trying to intersect the pick and clip regions
       * since needing to copy the region, do the intersection, and probably
       * increase the number of rectangles seems more likely to have a negative
       * effect.
       *
       * NB: Most of the time when just using rectangles for picking then
       * picking shouldn't involve any rendering, and minimizing the number of
       * rectangles has more benefit than reducing the area of the pick
       * region.
       */
495

496
      n_rects = cairo_region_num_rectangles (priv->input_shape_region);
497
      rectangles = g_alloca (sizeof (float) * 4 * n_rects);
498

499 500 501 502
      for (i = 0; i < n_rects; i++)
        {
          cairo_rectangle_int_t rect;
          int pos = i * 4;
503

504
          cairo_region_get_rectangle (priv->input_shape_region, i, &rect);
505

506 507 508 509 510
          rectangles[pos] = rect.x;
          rectangles[pos + 1] = rect.y;
          rectangles[pos + 2] = rect.x + rect.width;
          rectangles[pos + 3] = rect.y + rect.height;
        }
511

512 513 514 515 516 517 518
      ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
      fb = cogl_get_draw_framebuffer ();

      cogl_color_init_from_4ub (&cogl_color, color->red, color->green, color->blue, color->alpha);

      pipeline = cogl_pipeline_new (ctx);
      cogl_pipeline_set_color (pipeline, &cogl_color);
519

520 521 522
      cogl_framebuffer_draw_rectangles (fb, pipeline,
                                        rectangles, n_rects);
      cogl_object_unref (pipeline);
523 524 525
    }
}

526
static void
527 528 529 530
meta_shaped_texture_get_preferred_width (ClutterActor *self,
                                         gfloat        for_height,
                                         gfloat       *min_width_p,
                                         gfloat       *natural_width_p)
531
{
532
  MetaShapedTexturePrivate *priv;
533

534
  g_return_if_fail (META_IS_SHAPED_TEXTURE (self));
535

536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
  priv = META_SHAPED_TEXTURE (self)->priv;

  if (min_width_p)
    *min_width_p = 0;

  if (natural_width_p)
    *natural_width_p = priv->tex_width;
}

static void
meta_shaped_texture_get_preferred_height (ClutterActor *self,
                                          gfloat        for_width,
                                          gfloat       *min_height_p,
                                          gfloat       *natural_height_p)
{
  MetaShapedTexturePrivate *priv;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (self));

  priv = META_SHAPED_TEXTURE (self)->priv;

  if (min_height_p)
    *min_height_p = 0;

  if (natural_height_p)
    *natural_height_p = priv->tex_height;
562 563
}

564 565 566 567 568 569 570
static gboolean
meta_shaped_texture_get_paint_volume (ClutterActor *self,
                                      ClutterPaintVolume *volume)
{
  return clutter_paint_volume_set_from_allocation (volume, self);
}

571
ClutterActor *
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
meta_shaped_texture_new_with_wayland_surface (MetaWaylandSurface *surface)
{
  ClutterActor *actor = g_object_new (META_TYPE_SHAPED_TEXTURE, NULL);
  MetaShapedTexturePrivate *priv = META_SHAPED_TEXTURE (actor)->priv;

  /* XXX: it could probably be better to have a "type" construct-only
   * property or create wayland/x11 subclasses */
  priv->type = META_SHAPED_TEXTURE_TYPE_WAYLAND_SURFACE;

  meta_shaped_texture_set_wayland_surface (META_SHAPED_TEXTURE (actor),
                                           surface);

  return actor;
}

void
meta_shaped_texture_set_wayland_surface (MetaShapedTexture *stex,
                                         MetaWaylandSurface *surface)
590
{
591 592 593
  MetaShapedTexturePrivate *priv = stex->priv;

  priv->wayland.surface = surface;
594

595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
  if (surface && surface->buffer_ref.buffer)
    meta_shaped_texture_attach_wayland_buffer (stex,
                                               surface->buffer_ref.buffer);
}

MetaWaylandSurface *
meta_shaped_texture_get_wayland_surface (MetaShapedTexture *stex)
{
  MetaShapedTexturePrivate *priv = stex->priv;
  return priv->wayland.surface;
}

ClutterActor *
meta_shaped_texture_new_with_xwindow (Window xwindow)
{
610
  return g_object_new (META_TYPE_SHAPED_TEXTURE, NULL);
611 612
}

613
void
614 615
meta_shaped_texture_set_create_mipmaps (MetaShapedTexture *stex,
					gboolean           create_mipmaps)
616
{
617
  MetaShapedTexturePrivate *priv;
618

619
  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));
620 621 622 623 624 625 626

  priv = stex->priv;

  create_mipmaps = create_mipmaps != FALSE;

  if (create_mipmaps != priv->create_mipmaps)
    {
627
      CoglTexture *base_texture;
628
      priv->create_mipmaps = create_mipmaps;
629
      base_texture = create_mipmaps ? priv->texture : NULL;
630
      meta_texture_tower_set_base_texture (priv->paint_tower, base_texture);
631 632 633
    }
}

634
void
635
meta_shaped_texture_set_mask_texture (MetaShapedTexture *stex,
636
                                      CoglTexture       *mask_texture)
637
{
638
  MetaShapedTexturePrivate *priv;
639

640
  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));
641 642 643

  priv = stex->priv;

644
  g_clear_pointer (&priv->mask_texture, cogl_object_unref);
645

646
  if (mask_texture != NULL)
647
    {
648
      priv->mask_texture = mask_texture;
649
      cogl_object_ref (priv->mask_texture);
650
    }
651

652
  clutter_actor_queue_redraw (CLUTTER_ACTOR (stex));
653 654
}

655 656 657 658 659 660
static void
wayland_surface_update_area (MetaShapedTexture *stex,
                             int                x,
                             int                y,
                             int                width,
                             int                height)
661
{
662
  MetaShapedTexturePrivate *priv;
663
  MetaWaylandBuffer *buffer;
664 665 666

  priv = stex->priv;

667 668
  g_return_if_fail (priv->type == META_SHAPED_TEXTURE_TYPE_WAYLAND_SURFACE);
  g_return_if_fail (priv->texture != NULL);
669

670
  buffer = priv->wayland.surface->buffer_ref.buffer;
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 700 701
  if (buffer)
    {
      struct wl_resource *resource = buffer->resource;
      struct wl_shm_buffer *shm_buffer = wl_shm_buffer_get (resource);

      if (shm_buffer)
        {
          CoglPixelFormat format;

          switch (wl_shm_buffer_get_format (shm_buffer))
            {
#if G_BYTE_ORDER == G_BIG_ENDIAN
            case WL_SHM_FORMAT_ARGB8888:
              format = COGL_PIXEL_FORMAT_ARGB_8888_PRE;
              break;
            case WL_SHM_FORMAT_XRGB8888:
              format = COGL_PIXEL_FORMAT_ARGB_8888;
              break;
#elif G_BYTE_ORDER == G_LITTLE_ENDIAN
            case WL_SHM_FORMAT_ARGB8888:
              format = COGL_PIXEL_FORMAT_BGRA_8888_PRE;
              break;
            case WL_SHM_FORMAT_XRGB8888:
              format = COGL_PIXEL_FORMAT_BGRA_8888;
              break;
#endif
            default:
              g_warn_if_reached ();
              format = COGL_PIXEL_FORMAT_ARGB_8888;
            }
702

703 704 705 706 707 708 709 710 711 712
          cogl_texture_set_region (priv->texture,
                                   x, y,
                                   x, y,
                                   width, height,
                                   width, height,
                                   format,
                                   wl_shm_buffer_get_stride (shm_buffer),
                                   wl_shm_buffer_get_data (shm_buffer));
        }
    }
713 714
}

715
static gboolean
716 717 718 719 720 721
get_clip (MetaShapedTexture *stex,
          int x,
          int y,
          int width,
          int height,
          cairo_rectangle_int_t *clip)
722
{
723
  ClutterActor *self = CLUTTER_ACTOR (stex);
724
  MetaShapedTexturePrivate *priv;
725 726 727 728 729 730 731 732
  ClutterActorBox allocation;
  float scale_x;
  float scale_y;

  /* NB: clutter_actor_queue_redraw_with_clip expects a box in the actor's
   * coordinate space so we need to convert from surface coordinates to
   * actor coordinates...
   */
733

734 735 736 737 738 739 740
  /* Calling clutter_actor_get_allocation_box() is enormously expensive
   * if the actor has an out-of-date allocation, since it triggers
   * a full redraw. clutter_actor_queue_redraw_with_clip() would redraw
   * the whole stage anyways in that case, so just go ahead and do
   * it here.
   */
  if (!clutter_actor_has_allocation (self))
741
    return FALSE;
742 743 744

  priv = stex->priv;

745
  if (priv->tex_width == 0 || priv->tex_height == 0)
746
    return FALSE;
747

748
  clutter_actor_get_allocation_box (self, &allocation);
749

750 751
  scale_x = (allocation.x2 - allocation.x1) / priv->tex_width;
  scale_y = (allocation.y2 - allocation.y1) / priv->tex_height;
752

753 754 755 756
  clip->x = x * scale_x;
  clip->y = y * scale_y;
  clip->width = width * scale_x;
  clip->height = height * scale_y;
757 758

  return TRUE;
759
}
760

761 762 763 764 765 766 767 768 769 770 771 772
/**
 * meta_shaped_texture_update_area:
 * @stex: #MetaShapedTexture
 * @x: the x coordinate of the damaged area
 * @y: the y coordinate of the damaged area
 * @width: the width of the damaged area
 * @height: the height of the damaged area
 * @unobscured_region: The unobscured region of the window or %NULL if
 * there is no valid one (like when the actor is transformed or
 * has a mapped clone)
 *
 * Repairs the damaged area indicated by @x, @y, @width and @height
773 774
 * and queues a redraw for the intersection @unobscured_region and
 * the damage area. If @unobscured_region is %NULL a redraw will always
775 776 777 778 779
 * get queued.
 *
 * Return value: Whether a redraw have been queued or not
 */
gboolean
780
meta_shaped_texture_update_area (MetaShapedTexture *stex,
781 782 783 784 785
				 int                x,
				 int                y,
				 int                width,
				 int                height,
				 cairo_region_t    *unobscured_region)
786 787
{
  MetaShapedTexturePrivate *priv;
788
  cairo_rectangle_int_t clip;
789
  gboolean has_clip;
790

791
  priv = stex->priv;
792

793
  if (priv->texture == NULL)
794
    return FALSE;
795 796

  switch (priv->type)
797
    {
798 799 800 801 802 803 804
    case META_SHAPED_TEXTURE_TYPE_X11_PIXMAP:
      cogl_texture_pixmap_x11_update_area (COGL_TEXTURE_PIXMAP_X11 (priv->texture),
                                           x, y, width, height);
      break;
    case META_SHAPED_TEXTURE_TYPE_WAYLAND_SURFACE:
      wayland_surface_update_area (stex, x, y, width, height);
      break;
805
    }
806

807 808
  meta_texture_tower_update_area (priv->paint_tower, x, y, width, height);

809
  has_clip = get_clip (stex, x, y, width, height, &clip);
810

811 812 813 814 815 816 817 818
  if (unobscured_region)
    {
      cairo_region_t *intersection;

      if (cairo_region_is_empty (unobscured_region))
        return FALSE;

      intersection = cairo_region_copy (unobscured_region);
819 820
      if (has_clip)
	cairo_region_intersect_rectangle (intersection, &clip);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

      if (!cairo_region_is_empty (intersection))
        {
          cairo_rectangle_int_t damage_rect;
          cairo_region_get_extents (intersection, &damage_rect);
          clutter_actor_queue_redraw_with_clip (CLUTTER_ACTOR (stex), &damage_rect);
          cairo_region_destroy (intersection);

          return TRUE;
        }

      cairo_region_destroy (intersection);

      return FALSE;
    }

837 838 839 840
  if (has_clip)
    clutter_actor_queue_redraw_with_clip (CLUTTER_ACTOR (stex), &clip);
  else
    clutter_actor_queue_redraw (CLUTTER_ACTOR (stex));
841 842

  return TRUE;
843
}
844

845 846 847 848 849 850 851 852 853 854 855 856 857 858 859
/**
 * meta_shaped_texture_set_pixmap:
 * @stex: The #MetaShapedTexture
 * @pixmap: The pixmap you want the stex to assume
 */
void
meta_shaped_texture_set_pixmap (MetaShapedTexture *stex,
                                Pixmap             pixmap)
{
  MetaShapedTexturePrivate *priv;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));

  priv = stex->priv;

860
  if (priv->x11.pixmap == pixmap)
861 862
    return;

863
  priv->x11.pixmap = pixmap;
864 865

  if (pixmap != None)
866 867 868
    {
      CoglContext *ctx =
        clutter_backend_get_cogl_context (clutter_get_default_backend ());
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
      CoglTexture *texture =
        COGL_TEXTURE (cogl_texture_pixmap_x11_new (ctx, pixmap, FALSE, NULL));
      set_cogl_texture (stex, texture);
    }
  else
    set_cogl_texture (stex, NULL);

  if (priv->create_mipmaps)
    meta_texture_tower_set_base_texture (priv->paint_tower,
                                         COGL_TEXTURE (priv->texture));
}

void
meta_shaped_texture_attach_wayland_buffer (MetaShapedTexture  *stex,
                                           MetaWaylandBuffer  *buffer)
{
  MetaShapedTexturePrivate *priv;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));

  priv = stex->priv;

  /* TODO: we should change this api to be something like
   * meta_shaped_texture_notify_buffer_attach() since we now maintain
   * a reference to the MetaWaylandSurface where we can access the
   * buffer without it being explicitly passed as an argument.
   */
  g_return_if_fail (priv->wayland.surface->buffer_ref.buffer == buffer);

  if (buffer)
899
    set_cogl_texture (stex, buffer->texture);
900
  else
901
    set_cogl_texture (stex, NULL);
902 903

  if (priv->create_mipmaps)
904 905
    meta_texture_tower_set_base_texture (priv->paint_tower,
                                         COGL_TEXTURE (priv->texture));
906 907 908 909 910 911 912 913
}

/**
 * meta_shaped_texture_get_texture:
 * @stex: The #MetaShapedTexture
 *
 * Returns: (transfer none): the unshaped texture
 */
914
CoglTexture *
915 916
meta_shaped_texture_get_texture (MetaShapedTexture *stex)
{
917 918
  g_return_val_if_fail (META_IS_SHAPED_TEXTURE (stex), NULL);
  return COGL_TEXTURE (stex->priv->texture);
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
/**
 * meta_shaped_texture_set_input_shape_region:
 * @stex: a #MetaShapedTexture
 * @shape_region: the region of the texture that should respond to
 *    input.
 *
 * Determines what region of the texture should accept input. For
 * X based windows this is defined by the ShapeInput region of the
 * window.
 */
void
meta_shaped_texture_set_input_shape_region (MetaShapedTexture *stex,
                                            cairo_region_t    *shape_region)
{
  MetaShapedTexturePrivate *priv;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));

  priv = stex->priv;

  if (priv->input_shape_region != NULL)
    {
      cairo_region_destroy (priv->input_shape_region);
      priv->input_shape_region = NULL;
    }

  if (shape_region != NULL)
    {
      cairo_region_reference (shape_region);
      priv->input_shape_region = shape_region;
    }

  clutter_actor_queue_redraw (CLUTTER_ACTOR (stex));
}

956
/**
957
 * meta_shaped_texture_set_clip_region:
958
 * @stex: a #MetaShapedTexture
959 960
 * @clip_region: the region of the texture that is visible and
 *   should be painted.
961 962 963 964 965 966 967 968 969 970
 *
 * Provides a hint to the texture about what areas of the texture
 * are not completely obscured and thus need to be painted. This
 * is an optimization and is not supposed to have any effect on
 * the output.
 *
 * Typically a parent container will set the clip region before
 * painting its children, and then unset it afterwards.
 */
void
971 972
meta_shaped_texture_set_clip_region (MetaShapedTexture *stex,
				     cairo_region_t    *clip_region)
973
{
974
  MetaShapedTexturePrivate *priv;
975

976
  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));
977 978 979 980

  priv = stex->priv;

  if (priv->clip_region)
981
    cairo_region_destroy (priv->clip_region);
982

983 984 985 986
  if (clip_region)
    priv->clip_region = cairo_region_copy (clip_region);
  else
    priv->clip_region = NULL;
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
/**
 * meta_shaped_texture_set_opaque_region:
 * @stex: a #MetaShapedTexture
 * @opaque_region: (transfer full): the region of the texture that
 *   can have blending turned off.
 *
 * As most windows have a large portion that does not require blending,
 * we can easily turn off blending if we know the areas that do not
 * require blending. This sets the region where we will not blend for
 * optimization purposes.
 */
void
meta_shaped_texture_set_opaque_region (MetaShapedTexture *stex,
                                       cairo_region_t    *opaque_region)
{
  MetaShapedTexturePrivate *priv;

  g_return_if_fail (META_IS_SHAPED_TEXTURE (stex));

  priv = stex->priv;

  if (priv->opaque_region)
    cairo_region_destroy (priv->opaque_region);

  if (opaque_region)
    priv->opaque_region = cairo_region_reference (opaque_region);
  else
    priv->opaque_region = NULL;
}

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/**
 * meta_shaped_texture_get_image:
 * @stex: A #MetaShapedTexture
 * @clip: A clipping rectangle, to help prevent extra processing.
 * In the case that the clipping rectangle is partially or fully
 * outside the bounds of the texture, the rectangle will be clipped.
 *
 * Flattens the two layers of the shaped texture into one ARGB32
 * image by alpha blending the two images, and returns the flattened
 * image.
 *
 * Returns: (transfer full): a new cairo surface to be freed with
 * cairo_surface_destroy().
 */
cairo_surface_t *
meta_shaped_texture_get_image (MetaShapedTexture     *stex,
                               cairo_rectangle_int_t *clip)
{
1037
  CoglTexture *texture, *mask_texture;
1038 1039 1040 1041 1042
  cairo_rectangle_int_t texture_rect = { 0, 0, 0, 0 };
  cairo_surface_t *surface;

  g_return_val_if_fail (META_IS_SHAPED_TEXTURE (stex), NULL);

1043
  texture = COGL_TEXTURE (stex->priv->texture);
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

  if (texture == NULL)
    return NULL;

  texture_rect.width = cogl_texture_get_width (texture);
  texture_rect.height = cogl_texture_get_height (texture);

  if (clip != NULL)
    {
      /* GdkRectangle is just a typedef of cairo_rectangle_int_t,
       * so we can use the gdk_rectangle_* APIs on these. */
      if (!gdk_rectangle_intersect (&texture_rect, clip, clip))
        return NULL;
    }

  if (clip != NULL)
    texture = cogl_texture_new_from_sub_texture (texture,
                                                 clip->x,
                                                 clip->y,
                                                 clip->width,
                                                 clip->height);

  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
                                        cogl_texture_get_width (texture),
                                        cogl_texture_get_height (texture));

  cogl_texture_get_data (texture, CLUTTER_CAIRO_FORMAT_ARGB32,
                         cairo_image_surface_get_stride (surface),
                         cairo_image_surface_get_data (surface));

  cairo_surface_mark_dirty (surface);

  if (clip != NULL)
    cogl_object_unref (texture);

  mask_texture = stex->priv->mask_texture;
1080
  if (mask_texture != NULL)
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
    {
      cairo_t *cr;
      cairo_surface_t *mask_surface;

      if (clip != NULL)
        mask_texture = cogl_texture_new_from_sub_texture (mask_texture,
                                                          clip->x,
                                                          clip->y,
                                                          clip->width,
                                                          clip->height);

      mask_surface = cairo_image_surface_create (CAIRO_FORMAT_A8,
                                                 cogl_texture_get_width (mask_texture),
                                                 cogl_texture_get_height (mask_texture));

      cogl_texture_get_data (mask_texture, COGL_PIXEL_FORMAT_A_8,
                             cairo_image_surface_get_stride (mask_surface),
                             cairo_image_surface_get_data (mask_surface));

      cairo_surface_mark_dirty (mask_surface);

      cr = cairo_create (surface);
      cairo_set_source_surface (cr, mask_surface, 0, 0);
      cairo_set_operator (cr, CAIRO_OPERATOR_DEST_IN);
      cairo_paint (cr);
      cairo_destroy (cr);

      cairo_surface_destroy (mask_surface);

      if (clip != NULL)
        cogl_object_unref (mask_texture);
    }

  return surface;
}