1ab265
From ac946bf95ce3e4dc900f72dcb4189dd49bdb3155 Mon Sep 17 00:00:00 2001
1ab265
From: Daniel van Vugt <daniel.van.vugt@canonical.com>
1ab265
Date: Thu, 18 Jul 2019 16:56:41 +0800
1ab265
Subject: [PATCH 1/4] clutter/point: Add ClutterPoint quarilateral testing API
1ab265
1ab265
Add a function to check whether a point is inside a quadrilateral
1ab265
by checking the cross product of vectors with the quadrilateral
1ab265
points, and the point being checked.
1ab265
1ab265
If the passed quadrilateral is zero-sized, no point is ever reported
1ab265
to be inside it.
1ab265
1ab265
This will be used by the next commit when comparing the transformed
1ab265
actor vertices.
1ab265
1ab265
[feaneron: add a commit message and remove unecessary code]
1ab265
1ab265
https://gitlab.gnome.org/GNOME/mutter/merge_requests/189
1ab265
---
1ab265
 clutter/clutter/clutter-base-types.c | 62 ++++++++++++++++++++++++++++
1ab265
 clutter/clutter/clutter-types.h      |  3 ++
1ab265
 2 files changed, 65 insertions(+)
1ab265
1ab265
diff --git a/clutter/clutter/clutter-base-types.c b/clutter/clutter/clutter-base-types.c
1ab265
index aeb25c90ef..c84f9aa64b 100644
1ab265
--- a/clutter/clutter/clutter-base-types.c
1ab265
+++ b/clutter/clutter/clutter-base-types.c
1ab265
@@ -570,6 +570,68 @@ G_DEFINE_BOXED_TYPE_WITH_CODE (ClutterPoint, clutter_point,
1ab265
                                clutter_point_free,
1ab265
                                CLUTTER_REGISTER_INTERVAL_PROGRESS (clutter_point_progress))
1ab265
 
1ab265
+static int
1ab265
+clutter_point_compare_line (const ClutterPoint *p,
1ab265
+                            const ClutterPoint *a,
1ab265
+                            const ClutterPoint *b)
1ab265
+{
1ab265
+  float x1 = b->x - a->x;
1ab265
+  float y1 = b->y - a->y;
1ab265
+  float x2 = p->x - a->x;
1ab265
+  float y2 = p->y - a->y;
1ab265
+  float cross_z = x1 * y2 - y1 * x2;
1ab265
+
1ab265
+  if (cross_z > 0.f)
1ab265
+    return 1;
1ab265
+  else if (cross_z < 0.f)
1ab265
+    return -1;
1ab265
+  else
1ab265
+    return 0;
1ab265
+}
1ab265
+
1ab265
+/**
1ab265
+ * clutter_point_inside_quadrilateral:
1ab265
+ * @point: a #ClutterPoint to test
1ab265
+ * @vertices: array of vertices of the quadrilateral, in clockwise order,
1ab265
+ *            from top-left to bottom-left
1ab265
+ *
1ab265
+ * Determines whether a point is inside the convex quadrilateral provided,
1ab265
+ * and not on any of its edges or vertices.
1ab265
+ *
1ab265
+ * Returns: %TRUE if @point is inside the quadrilateral
1ab265
+ */
1ab265
+gboolean
1ab265
+clutter_point_inside_quadrilateral (const ClutterPoint *point,
1ab265
+                                    const ClutterPoint *vertices)
1ab265
+{
1ab265
+  unsigned int i;
1ab265
+  int first_side;
1ab265
+
1ab265
+  first_side = 0;
1ab265
+
1ab265
+  for (i = 0; i < 4; i++)
1ab265
+    {
1ab265
+      int side;
1ab265
+
1ab265
+      side = clutter_point_compare_line (point,
1ab265
+                                         &vertices[i],
1ab265
+                                         &vertices[(i + 1) % 4]);
1ab265
+
1ab265
+      if (side)
1ab265
+        {
1ab265
+          if (first_side == 0)
1ab265
+            first_side = side;
1ab265
+          else if (side != first_side)
1ab265
+            return FALSE;
1ab265
+        }
1ab265
+    }
1ab265
+
1ab265
+  if (first_side == 0)
1ab265
+    return FALSE;
1ab265
+
1ab265
+  return TRUE;
1ab265
+}
1ab265
+
1ab265
 
1ab265
 
1ab265
 /*
1ab265
diff --git a/clutter/clutter/clutter-types.h b/clutter/clutter/clutter-types.h
1ab265
index 0f0fb1c2ac..0508028273 100644
1ab265
--- a/clutter/clutter/clutter-types.h
1ab265
+++ b/clutter/clutter/clutter-types.h
1ab265
@@ -200,6 +200,9 @@ float                   clutter_point_distance  (const ClutterPoint *a,
1ab265
                                                  const ClutterPoint *b,
1ab265
                                                  float              *x_distance,
1ab265
                                                  float              *y_distance);
1ab265
+CLUTTER_EXPORT
1ab265
+gboolean clutter_point_inside_quadrilateral     (const ClutterPoint *point,
1ab265
+                                                 const ClutterPoint *vertices);
1ab265
 
1ab265
 /**
1ab265
  * ClutterSize:
1ab265
-- 
1ab265
2.29.2
1ab265
1ab265
1ab265
From 8abac81711cfef8317bb675349d6b5b0a79eb05d Mon Sep 17 00:00:00 2001
1ab265
From: Daniel van Vugt <daniel.van.vugt@canonical.com>
1ab265
Date: Thu, 2 Aug 2018 19:03:30 +0800
1ab265
Subject: [PATCH 2/4] clutter: Introduce geometric picking
1ab265
1ab265
Currently, Clutter does picking by drawing with Cogl and reading
1ab265
the pixel that's beneath the given point. Since Cogl has a journal
1ab265
that records drawing operations, and has optimizations to read a
1ab265
single pixel from a list of rectangle, it would be expected that
1ab265
we would hit this fast path and not flush the journal while picking.
1ab265
1ab265
However, that's not the case: dithering, clipping with scissors, etc,
1ab265
can all flush the journal, issuing commands to the GPU and making
1ab265
picking slow. On NVidia-based systems, this glReadPixels() call is
1ab265
extremely costly.
1ab265
1ab265
Introduce geometric picking, and avoid using the Cogl journal entirely.
1ab265
Do this by introducing a stack of actors in ClutterStage. This stack
1ab265
is cached, but for now, don't use the cache as much as possible.
1ab265
1ab265
The picking routines are still tied to painting.
1ab265
1ab265
When projecting the actor vertexes, do it manually and take the modelview
1ab265
matrix of the framebuffer into account as well.
1ab265
1ab265
CPU usage on an Intel i7-7700, tested with two different GPUs/drivers:
1ab265
1ab265
  |         |     Intel | Nvidia |
1ab265
  | ------: | --------: | -----: |
1ab265
  | Moving the mouse:            |
1ab265
  | Before  |       10% |    10% |
1ab265
  | After   |        6% |     6% |
1ab265
  | Moving a window:             |
1ab265
  | Before  |       23% |    81% |
1ab265
  | After   |       19% |    40% |
1ab265
1ab265
Closes: https://gitlab.gnome.org/GNOME/mutter/issues/154,
1ab265
        https://gitlab.gnome.org/GNOME/mutter/issues/691
1ab265
1ab265
Helps significantly with: https://gitlab.gnome.org/GNOME/mutter/issues/283,
1ab265
                          https://gitlab.gnome.org/GNOME/mutter/issues/590,
1ab265
                          https://gitlab.gnome.org/GNOME/mutter/issues/700
1ab265
1ab265
v2: Fix code style issues
1ab265
    Simplify quadrilateral checks
1ab265
    Remove the 0.5f hack
1ab265
    Differentiate axis-aligned rectangles
1ab265
1ab265
https://gitlab.gnome.org/GNOME/mutter/merge_requests/189
1ab265
---
1ab265
 clutter/clutter/clutter-actor-private.h      |   2 -
1ab265
 clutter/clutter/clutter-actor.c              | 232 ++++++----
1ab265
 clutter/clutter/clutter-actor.h              |   4 +
1ab265
 clutter/clutter/clutter-debug.h              |   1 -
1ab265
 clutter/clutter/clutter-main.c               | 120 -----
1ab265
 clutter/clutter/clutter-private.h            |   5 -
1ab265
 clutter/clutter/clutter-stage-private.h      |  16 +-
1ab265
 clutter/clutter/clutter-stage-window.c       |  18 -
1ab265
 clutter/clutter/clutter-stage-window.h       |   8 -
1ab265
 clutter/clutter/clutter-stage.c              | 459 +++++++++++--------
1ab265
 clutter/clutter/cogl/clutter-stage-cogl.c    |  52 ---
1ab265
 clutter/clutter/deprecated/clutter-texture.c |  93 +---
1ab265
 clutter/tests/conform/actor-pick.c           |  99 +---
1ab265
 clutter/tests/conform/meson.build            |   1 -
1ab265
 clutter/tests/conform/texture.c              |  84 ----
1ab265
 src/compositor/meta-surface-actor.c          |  27 +-
1ab265
 16 files changed, 439 insertions(+), 782 deletions(-)
1ab265
 delete mode 100644 clutter/tests/conform/texture.c
1ab265
1ab265
diff --git a/clutter/clutter/clutter-actor-private.h b/clutter/clutter/clutter-actor-private.h
1ab265
index c44f6342fd..9bf1a30493 100644
1ab265
--- a/clutter/clutter/clutter-actor-private.h
1ab265
+++ b/clutter/clutter/clutter-actor-private.h
1ab265
@@ -297,8 +297,6 @@ const gchar *                   _clutter_actor_get_debug_name
1ab265
 void                            _clutter_actor_push_clone_paint                         (void);
1ab265
 void                            _clutter_actor_pop_clone_paint                          (void);
1ab265
 
1ab265
-guint32                         _clutter_actor_get_pick_id                              (ClutterActor *self);
1ab265
-
1ab265
 void                            _clutter_actor_shader_pre_paint                         (ClutterActor *actor,
1ab265
                                                                                          gboolean      repeat);
1ab265
 void                            _clutter_actor_shader_post_paint                        (ClutterActor *actor);
1ab265
diff --git a/clutter/clutter/clutter-actor.c b/clutter/clutter/clutter-actor.c
1ab265
index 43093fe79d..01ffa51caa 100644
1ab265
--- a/clutter/clutter/clutter-actor.c
1ab265
+++ b/clutter/clutter/clutter-actor.c
1ab265
@@ -730,8 +730,6 @@ struct _ClutterActorPrivate
1ab265
 
1ab265
   gchar *name; /* a non-unique name, used for debugging */
1ab265
 
1ab265
-  gint32 pick_id; /* per-stage unique id, used for picking */
1ab265
-
1ab265
   /* a back-pointer to the Pango context that we can use
1ab265
    * to create pre-configured PangoLayout
1ab265
    */
1ab265
@@ -1281,6 +1279,105 @@ clutter_actor_verify_map_state (ClutterActor *self)
1ab265
 
1ab265
 #endif /* CLUTTER_ENABLE_DEBUG */
1ab265
 
1ab265
+static gboolean
1ab265
+_clutter_actor_transform_local_box_to_stage (ClutterActor          *self,
1ab265
+                                             ClutterStage          *stage,
1ab265
+                                             const ClutterActorBox *box,
1ab265
+                                             ClutterPoint           vertices[4])
1ab265
+{
1ab265
+  CoglFramebuffer *fb = cogl_get_draw_framebuffer ();
1ab265
+  CoglMatrix stage_transform, inv_stage_transform;
1ab265
+  CoglMatrix modelview, transform_to_stage;
1ab265
+  int v;
1ab265
+
1ab265
+  clutter_actor_get_transform (CLUTTER_ACTOR (stage), &stage_transform);
1ab265
+  if (!cogl_matrix_get_inverse (&stage_transform, &inv_stage_transform))
1ab265
+    return FALSE;
1ab265
+  cogl_framebuffer_get_modelview_matrix (fb, &modelview);
1ab265
+  cogl_matrix_multiply (&transform_to_stage, &inv_stage_transform, &modelview);
1ab265
+
1ab265
+  vertices[0].x = box->x1;
1ab265
+  vertices[0].y = box->y1;
1ab265
+
1ab265
+  vertices[1].x = box->x2;
1ab265
+  vertices[1].y = box->y1;
1ab265
+
1ab265
+  vertices[2].x = box->x2;
1ab265
+  vertices[2].y = box->y2;
1ab265
+
1ab265
+  vertices[3].x = box->x1;
1ab265
+  vertices[3].y = box->y2;
1ab265
+
1ab265
+  for (v = 0; v < 4; v++)
1ab265
+    {
1ab265
+      float z = 0.f;
1ab265
+      float w = 1.f;
1ab265
+
1ab265
+      cogl_matrix_transform_point (&transform_to_stage,
1ab265
+                                   &vertices[v].x,
1ab265
+                                   &vertices[v].y,
1ab265
+                                   &z,
1ab265
+                                   &w);
1ab265
+    }
1ab265
+
1ab265
+  return TRUE;
1ab265
+}
1ab265
+
1ab265
+/**
1ab265
+ * clutter_actor_pick_box:
1ab265
+ * @self: The #ClutterActor being "pick" painted.
1ab265
+ * @box: A rectangle in the actor's own local coordinates.
1ab265
+ *
1ab265
+ * Logs (does a virtual paint of) a rectangle for picking. Note that @box is
1ab265
+ * in the actor's own local coordinates, so is usually {0,0,width,height}
1ab265
+ * to include the whole actor. That is unless the actor has a shaped input
1ab265
+ * region in which case you may wish to log the (multiple) smaller rectangles
1ab265
+ * that make up the input region.
1ab265
+ */
1ab265
+void
1ab265
+clutter_actor_pick_box (ClutterActor          *self,
1ab265
+                        const ClutterActorBox *box)
1ab265
+{
1ab265
+  ClutterStage *stage;
1ab265
+  ClutterPoint vertices[4];
1ab265
+
1ab265
+  g_return_if_fail (CLUTTER_IS_ACTOR (self));
1ab265
+  g_return_if_fail (box != NULL);
1ab265
+
1ab265
+  if (box->x1 >= box->x2 || box->y1 >= box->y2)
1ab265
+    return;
1ab265
+
1ab265
+  stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
1ab265
+
1ab265
+  if (_clutter_actor_transform_local_box_to_stage (self, stage, box, vertices))
1ab265
+    clutter_stage_log_pick (stage, vertices, self);
1ab265
+}
1ab265
+
1ab265
+static gboolean
1ab265
+_clutter_actor_push_pick_clip (ClutterActor          *self,
1ab265
+                               const ClutterActorBox *clip)
1ab265
+{
1ab265
+  ClutterStage *stage;
1ab265
+  ClutterPoint vertices[4];
1ab265
+
1ab265
+  stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
1ab265
+
1ab265
+  if (!_clutter_actor_transform_local_box_to_stage (self, stage, clip, vertices))
1ab265
+    return FALSE;
1ab265
+
1ab265
+  clutter_stage_push_pick_clip (stage, vertices);
1ab265
+  return TRUE;
1ab265
+}
1ab265
+
1ab265
+static void
1ab265
+_clutter_actor_pop_pick_clip (ClutterActor *self)
1ab265
+{
1ab265
+  ClutterActor *stage;
1ab265
+
1ab265
+  stage = _clutter_actor_get_stage_internal (self);
1ab265
+  clutter_stage_pop_pick_clip (CLUTTER_STAGE (stage));
1ab265
+}
1ab265
+
1ab265
 static void
1ab265
 clutter_actor_set_mapped (ClutterActor *self,
1ab265
                           gboolean      mapped)
1ab265
@@ -1509,8 +1606,7 @@ clutter_actor_update_map_state (ClutterActor  *self,
1ab265
 static void
1ab265
 clutter_actor_real_map (ClutterActor *self)
1ab265
 {
1ab265
-  ClutterActorPrivate *priv = self->priv;
1ab265
-  ClutterActor *stage, *iter;
1ab265
+  ClutterActor *iter;
1ab265
 
1ab265
   g_assert (!CLUTTER_ACTOR_IS_MAPPED (self));
1ab265
 
1ab265
@@ -1521,13 +1617,6 @@ clutter_actor_real_map (ClutterActor *self)
1ab265
 
1ab265
   self->priv->needs_paint_volume_update = TRUE;
1ab265
 
1ab265
-  stage = _clutter_actor_get_stage_internal (self);
1ab265
-  priv->pick_id = _clutter_stage_acquire_pick_id (CLUTTER_STAGE (stage), self);
1ab265
-
1ab265
-  CLUTTER_NOTE (ACTOR, "Pick id '%d' for actor '%s'",
1ab265
-                priv->pick_id,
1ab265
-                _clutter_actor_get_debug_name (self));
1ab265
-
1ab265
   clutter_actor_ensure_resource_scale (self);
1ab265
 
1ab265
   /* notify on parent mapped before potentially mapping
1ab265
@@ -1632,11 +1721,6 @@ clutter_actor_real_unmap (ClutterActor *self)
1ab265
 
1ab265
       stage = CLUTTER_STAGE (_clutter_actor_get_stage_internal (self));
1ab265
 
1ab265
-      if (stage != NULL)
1ab265
-        _clutter_stage_release_pick_id (stage, priv->pick_id);
1ab265
-
1ab265
-      priv->pick_id = -1;
1ab265
-
1ab265
       if (stage != NULL &&
1ab265
           clutter_stage_get_key_focus (stage) == self)
1ab265
         {
1ab265
@@ -2255,46 +2339,16 @@ static void
1ab265
 clutter_actor_real_pick (ClutterActor       *self,
1ab265
 			 const ClutterColor *color)
1ab265
 {
1ab265
-  CoglFramebuffer *framebuffer = cogl_get_draw_framebuffer ();
1ab265
-
1ab265
-  /* the default implementation is just to paint a rectangle
1ab265
-   * with the same size of the actor using the passed color
1ab265
-   */
1ab265
   if (clutter_actor_should_pick_paint (self))
1ab265
     {
1ab265
-      static CoglPipeline *default_pick_pipeline = NULL;
1ab265
-      ClutterActorBox box = { 0, };
1ab265
-      CoglPipeline *pick_pipeline;
1ab265
-      float width, height;
1ab265
-
1ab265
-      if (G_UNLIKELY (default_pick_pipeline == NULL))
1ab265
-        {
1ab265
-          CoglContext *ctx =
1ab265
-            clutter_backend_get_cogl_context (clutter_get_default_backend ());
1ab265
-
1ab265
-          default_pick_pipeline = cogl_pipeline_new (ctx);
1ab265
-        }
1ab265
-
1ab265
-      g_assert (default_pick_pipeline != NULL);
1ab265
-      pick_pipeline = cogl_pipeline_copy (default_pick_pipeline);
1ab265
+      ClutterActorBox box = {
1ab265
+        .x1 = 0,
1ab265
+        .y1 = 0,
1ab265
+        .x2 = clutter_actor_get_width (self),
1ab265
+        .y2 = clutter_actor_get_height (self),
1ab265
+      };
1ab265
 
1ab265
-      clutter_actor_get_allocation_box (self, &box);
1ab265
-
1ab265
-      width = box.x2 - box.x1;
1ab265
-      height = box.y2 - box.y1;
1ab265
-
1ab265
-      cogl_pipeline_set_color4ub (pick_pipeline,
1ab265
-                                  color->red,
1ab265
-                                  color->green,
1ab265
-                                  color->blue,
1ab265
-                                  color->alpha);
1ab265
-
1ab265
-      cogl_framebuffer_draw_rectangle (framebuffer,
1ab265
-                                       pick_pipeline,
1ab265
-                                       0, 0,
1ab265
-                                       width, height);
1ab265
-
1ab265
-      cogl_object_unref (pick_pipeline);
1ab265
+      clutter_actor_pick_box (self, &box);
1ab265
     }
1ab265
 
1ab265
   /* XXX - this thoroughly sucks, but we need to maintain compatibility
1ab265
@@ -3585,15 +3639,6 @@ _clutter_actor_update_last_paint_volume (ClutterActor *self)
1ab265
   priv->last_paint_volume_valid = TRUE;
1ab265
 }
1ab265
 
1ab265
-guint32
1ab265
-_clutter_actor_get_pick_id (ClutterActor *self)
1ab265
-{
1ab265
-  if (self->priv->pick_id < 0)
1ab265
-    return 0;
1ab265
-
1ab265
-  return self->priv->pick_id;
1ab265
-}
1ab265
-
1ab265
 /* This is the same as clutter_actor_add_effect except that it doesn't
1ab265
    queue a redraw and it doesn't notify on the effect property */
1ab265
 static void
1ab265
@@ -3826,6 +3871,7 @@ clutter_actor_paint (ClutterActor *self)
1ab265
   ClutterActorPrivate *priv;
1ab265
   ClutterPickMode pick_mode;
1ab265
   gboolean culling_inhibited;
1ab265
+  ClutterActorBox clip;
1ab265
   gboolean clip_set = FALSE;
1ab265
   ClutterStage *stage;
1ab265
 
1ab265
@@ -3919,24 +3965,38 @@ clutter_actor_paint (ClutterActor *self)
1ab265
 
1ab265
   if (priv->has_clip)
1ab265
     {
1ab265
-      CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
1ab265
-      cogl_framebuffer_push_rectangle_clip (fb,
1ab265
-                                            priv->clip.origin.x,
1ab265
-                                            priv->clip.origin.y,
1ab265
-                                            priv->clip.origin.x + priv->clip.size.width,
1ab265
-                                            priv->clip.origin.y + priv->clip.size.height);
1ab265
+      clip.x1 = priv->clip.origin.x;
1ab265
+      clip.y1 = priv->clip.origin.y;
1ab265
+      clip.x2 = priv->clip.origin.x + priv->clip.size.width;
1ab265
+      clip.y2 = priv->clip.origin.y + priv->clip.size.height;
1ab265
       clip_set = TRUE;
1ab265
     }
1ab265
   else if (priv->clip_to_allocation)
1ab265
     {
1ab265
-      CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
1ab265
-      gfloat width, height;
1ab265
+      clip.x1 = 0.f;
1ab265
+      clip.y1 = 0.f;
1ab265
+      clip.x2 = priv->allocation.x2 - priv->allocation.x1;
1ab265
+      clip.y2 = priv->allocation.y2 - priv->allocation.y1;
1ab265
+      clip_set = TRUE;
1ab265
+    }
1ab265
 
1ab265
-      width  = priv->allocation.x2 - priv->allocation.x1;
1ab265
-      height = priv->allocation.y2 - priv->allocation.y1;
1ab265
+  if (clip_set)
1ab265
+    {
1ab265
+      if (pick_mode == CLUTTER_PICK_NONE)
1ab265
+        {
1ab265
+          CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
1ab265
 
1ab265
-      cogl_framebuffer_push_rectangle_clip (fb, 0, 0, width, height);
1ab265
-      clip_set = TRUE;
1ab265
+          cogl_framebuffer_push_rectangle_clip (fb,
1ab265
+                                                clip.x1,
1ab265
+                                                clip.y1,
1ab265
+                                                clip.x2,
1ab265
+                                                clip.y2);
1ab265
+        }
1ab265
+      else
1ab265
+        {
1ab265
+          if (!_clutter_actor_push_pick_clip (self, &clip))
1ab265
+            clip_set = FALSE;
1ab265
+        }
1ab265
     }
1ab265
 
1ab265
   if (pick_mode == CLUTTER_PICK_NONE)
1ab265
@@ -4020,9 +4080,16 @@ clutter_actor_paint (ClutterActor *self)
1ab265
 done:
1ab265
   if (clip_set)
1ab265
     {
1ab265
-      CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
1ab265
+      if (pick_mode == CLUTTER_PICK_NONE)
1ab265
+        {
1ab265
+          CoglFramebuffer *fb = _clutter_stage_get_active_framebuffer (stage);
1ab265
 
1ab265
-      cogl_framebuffer_pop_clip (fb);
1ab265
+          cogl_framebuffer_pop_clip (fb);
1ab265
+        }
1ab265
+      else
1ab265
+        {
1ab265
+          _clutter_actor_pop_pick_clip (self);
1ab265
+        }
1ab265
     }
1ab265
 
1ab265
   cogl_pop_matrix ();
1ab265
@@ -4093,11 +4160,12 @@ clutter_actor_continue_paint (ClutterActor *self)
1ab265
         {
1ab265
           ClutterColor col = { 0, };
1ab265
 
1ab265
-          _clutter_id_to_color (_clutter_actor_get_pick_id (self), &col);
1ab265
-
1ab265
-          /* Actor will then paint silhouette of itself in supplied
1ab265
-           * color.  See clutter_stage_get_actor_at_pos() for where
1ab265
-           * picking is enabled.
1ab265
+          /* The actor will log a silhouette of itself to the stage pick log.
1ab265
+           * Note that the picking color is no longer used as the "log" instead
1ab265
+           * keeps a weak pointer to the actor itself. But we keep the color
1ab265
+           * parameter for now so as to maintain ABI compatibility. The color
1ab265
+           * parameter can be removed when someone feels like breaking the ABI
1ab265
+           * along with gnome-shell.
1ab265
            *
1ab265
            * XXX:2.0 - Call the pick() virtual directly
1ab265
            */
1ab265
@@ -8603,8 +8671,6 @@ clutter_actor_init (ClutterActor *self)
1ab265
 
1ab265
   self->priv = priv = clutter_actor_get_instance_private (self);
1ab265
 
1ab265
-  priv->pick_id = -1;
1ab265
-
1ab265
   priv->opacity = 0xff;
1ab265
   priv->show_on_set_parent = TRUE;
1ab265
   priv->resource_scale = -1.0f;
1ab265
diff --git a/clutter/clutter/clutter-actor.h b/clutter/clutter/clutter-actor.h
1ab265
index 3e7a59ac0c..16b438ba64 100644
1ab265
--- a/clutter/clutter/clutter-actor.h
1ab265
+++ b/clutter/clutter/clutter-actor.h
1ab265
@@ -910,6 +910,10 @@ void                            clutter_actor_bind_model_with_properties
1ab265
                                                                                  const char                 *first_model_property,
1ab265
                                                                                  ...);
1ab265
 
1ab265
+CLUTTER_EXPORT
1ab265
+void clutter_actor_pick_box (ClutterActor          *self,
1ab265
+                             const ClutterActorBox *box);
1ab265
+
1ab265
 G_END_DECLS
1ab265
 
1ab265
 #endif /* __CLUTTER_ACTOR_H__ */
1ab265
diff --git a/clutter/clutter/clutter-debug.h b/clutter/clutter/clutter-debug.h
1ab265
index 2462385f65..7d170d2d54 100644
1ab265
--- a/clutter/clutter/clutter-debug.h
1ab265
+++ b/clutter/clutter/clutter-debug.h
1ab265
@@ -30,7 +30,6 @@ typedef enum
1ab265
 typedef enum
1ab265
 {
1ab265
   CLUTTER_DEBUG_NOP_PICKING         = 1 << 0,
1ab265
-  CLUTTER_DEBUG_DUMP_PICK_BUFFERS   = 1 << 1
1ab265
 } ClutterPickDebugFlag;
1ab265
 
1ab265
 typedef enum
1ab265
diff --git a/clutter/clutter/clutter-main.c b/clutter/clutter/clutter-main.c
1ab265
index 645c8bceb6..11c221a65b 100644
1ab265
--- a/clutter/clutter/clutter-main.c
1ab265
+++ b/clutter/clutter/clutter-main.c
1ab265
@@ -131,7 +131,6 @@ static const GDebugKey clutter_debug_keys[] = {
1ab265
 
1ab265
 static const GDebugKey clutter_pick_debug_keys[] = {
1ab265
   { "nop-picking", CLUTTER_DEBUG_NOP_PICKING },
1ab265
-  { "dump-pick-buffers", CLUTTER_DEBUG_DUMP_PICK_BUFFERS },
1ab265
 };
1ab265
 
1ab265
 static const GDebugKey clutter_paint_debug_keys[] = {
1ab265
@@ -533,125 +532,6 @@ clutter_get_motion_events_enabled (void)
1ab265
   return _clutter_context_get_motion_events_enabled ();
1ab265
 }
1ab265
 
1ab265
-void
1ab265
-_clutter_id_to_color (guint         id_,
1ab265
-                      ClutterColor *col)
1ab265
-{
1ab265
-  ClutterMainContext *ctx;
1ab265
-  gint red, green, blue;
1ab265
-
1ab265
-  ctx = _clutter_context_get_default ();
1ab265
-
1ab265
-  if (ctx->fb_g_mask == 0)
1ab265
-    {
1ab265
-      /* Figure out framebuffer masks used for pick */
1ab265
-      cogl_get_bitmasks (&ctx->fb_r_mask,
1ab265
-			 &ctx->fb_g_mask,
1ab265
-			 &ctx->fb_b_mask, NULL);
1ab265
-
1ab265
-      ctx->fb_r_mask_used = ctx->fb_r_mask;
1ab265
-      ctx->fb_g_mask_used = ctx->fb_g_mask;
1ab265
-      ctx->fb_b_mask_used = ctx->fb_b_mask;
1ab265
-
1ab265
-      /* XXX - describe what "fuzzy picking" is */
1ab265
-      if (clutter_use_fuzzy_picking)
1ab265
-	{
1ab265
-	  ctx->fb_r_mask_used--;
1ab265
-	  ctx->fb_g_mask_used--;
1ab265
-	  ctx->fb_b_mask_used--;
1ab265
-	}
1ab265
-    }
1ab265
-
1ab265
-  /* compute the numbers we'll store in the components */
1ab265
-  red   = (id_ >> (ctx->fb_g_mask_used+ctx->fb_b_mask_used))
1ab265
-        & (0xff >> (8-ctx->fb_r_mask_used));
1ab265
-  green = (id_ >> ctx->fb_b_mask_used)
1ab265
-        & (0xff >> (8-ctx->fb_g_mask_used));
1ab265
-  blue  = (id_)
1ab265
-        & (0xff >> (8-ctx->fb_b_mask_used));
1ab265
-
1ab265
-  /* shift left bits a bit and add one, this circumvents
1ab265
-   * at least some potential rounding errors in GL/GLES
1ab265
-   * driver / hw implementation.
1ab265
-   */
1ab265
-  if (ctx->fb_r_mask_used != ctx->fb_r_mask)
1ab265
-    red = red * 2;
1ab265
-  if (ctx->fb_g_mask_used != ctx->fb_g_mask)
1ab265
-    green = green * 2;
1ab265
-  if (ctx->fb_b_mask_used != ctx->fb_b_mask)
1ab265
-    blue  = blue  * 2;
1ab265
-
1ab265
-  /* shift up to be full 8bit values */
1ab265
-  red   = (red   << (8 - ctx->fb_r_mask)) | (0x7f >> (ctx->fb_r_mask_used));
1ab265
-  green = (green << (8 - ctx->fb_g_mask)) | (0x7f >> (ctx->fb_g_mask_used));
1ab265
-  blue  = (blue  << (8 - ctx->fb_b_mask)) | (0x7f >> (ctx->fb_b_mask_used));
1ab265
-
1ab265
-  col->red   = red;
1ab265
-  col->green = green;
1ab265
-  col->blue  = blue;
1ab265
-  col->alpha = 0xff;
1ab265
-
1ab265
-  /* XXX: We rotate the nibbles of the colors here so that there is a
1ab265
-   * visible variation between colors of sequential actor identifiers;
1ab265
-   * otherwise pick buffers dumped to an image will pretty much just look
1ab265
-   * black.
1ab265
-   */
1ab265
-  if (G_UNLIKELY (clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
1ab265
-    {
1ab265
-      col->red   = (col->red << 4)   | (col->red >> 4);
1ab265
-      col->green = (col->green << 4) | (col->green >> 4);
1ab265
-      col->blue  = (col->blue << 4)  | (col->blue >> 4);
1ab265
-    }
1ab265
-}
1ab265
-
1ab265
-guint
1ab265
-_clutter_pixel_to_id (guchar pixel[4])
1ab265
-{
1ab265
-  ClutterMainContext *ctx;
1ab265
-  gint red, green, blue;
1ab265
-  guint retval;
1ab265
-
1ab265
-  ctx = _clutter_context_get_default ();
1ab265
-
1ab265
-  /* reduce the pixel components to the number of bits actually used of the
1ab265
-   * 8bits.
1ab265
-   */
1ab265
-  if (G_UNLIKELY (clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
1ab265
-    {
1ab265
-      guchar tmp;
1ab265
-
1ab265
-      /* XXX: In _clutter_id_to_color we rotated the nibbles of the colors so
1ab265
-       * that there is a visible variation between colors of sequential actor
1ab265
-       * identifiers (otherwise pick buffers dumped to an image will pretty
1ab265
-       * much just look black.) Here we reverse that rotation.
1ab265
-       */
1ab265
-      tmp = ((pixel[0] << 4) | (pixel[0] >> 4));
1ab265
-      red = tmp >> (8 - ctx->fb_r_mask);
1ab265
-      tmp = ((pixel[1] << 4) | (pixel[1] >> 4));
1ab265
-      green = tmp >> (8 - ctx->fb_g_mask);
1ab265
-      tmp = ((pixel[2] << 4) | (pixel[2] >> 4));
1ab265
-      blue = tmp >> (8 - ctx->fb_b_mask);
1ab265
-    }
1ab265
-  else
1ab265
-    {
1ab265
-      red   = pixel[0] >> (8 - ctx->fb_r_mask);
1ab265
-      green = pixel[1] >> (8 - ctx->fb_g_mask);
1ab265
-      blue  = pixel[2] >> (8 - ctx->fb_b_mask);
1ab265
-    }
1ab265
-
1ab265
-  /* divide potentially by two if 'fuzzy' */
1ab265
-  red   = red   >> (ctx->fb_r_mask - ctx->fb_r_mask_used);
1ab265
-  green = green >> (ctx->fb_g_mask - ctx->fb_g_mask_used);
1ab265
-  blue  = blue  >> (ctx->fb_b_mask - ctx->fb_b_mask_used);
1ab265
-
1ab265
-  /* combine the correct per component values into the final id */
1ab265
-  retval = blue
1ab265
-         + (green <<  ctx->fb_b_mask_used)
1ab265
-         + (red << (ctx->fb_b_mask_used + ctx->fb_g_mask_used));
1ab265
-
1ab265
-  return retval;
1ab265
-}
1ab265
-
1ab265
 static CoglPangoFontMap *
1ab265
 clutter_context_get_pango_fontmap (void)
1ab265
 {
1ab265
diff --git a/clutter/clutter/clutter-private.h b/clutter/clutter/clutter-private.h
1ab265
index 5a0fed85c9..f2f870b014 100644
1ab265
--- a/clutter/clutter/clutter-private.h
1ab265
+++ b/clutter/clutter/clutter-private.h
1ab265
@@ -210,11 +210,6 @@ gboolean      _clutter_feature_init (GError **error);
1ab265
 gboolean        _clutter_diagnostic_enabled     (void);
1ab265
 void            _clutter_diagnostic_message     (const char *fmt, ...) G_GNUC_PRINTF (1, 2);
1ab265
 
1ab265
-/* Picking code */
1ab265
-guint           _clutter_pixel_to_id            (guchar        pixel[4]);
1ab265
-void            _clutter_id_to_color            (guint         id,
1ab265
-                                                 ClutterColor *col);
1ab265
-
1ab265
 void            _clutter_set_sync_to_vblank     (gboolean      sync_to_vblank);
1ab265
 
1ab265
 /* use this function as the accumulator if you have a signal with
1ab265
diff --git a/clutter/clutter/clutter-stage-private.h b/clutter/clutter/clutter-stage-private.h
1ab265
index 42474687ad..51ae47af1d 100644
1ab265
--- a/clutter/clutter/clutter-stage-private.h
1ab265
+++ b/clutter/clutter/clutter-stage-private.h
1ab265
@@ -75,6 +75,15 @@ gint64    _clutter_stage_get_update_time                  (ClutterStage *stage);
1ab265
 void     _clutter_stage_clear_update_time                 (ClutterStage *stage);
1ab265
 gboolean _clutter_stage_has_full_redraw_queued            (ClutterStage *stage);
1ab265
 
1ab265
+void clutter_stage_log_pick (ClutterStage       *stage,
1ab265
+                             const ClutterPoint *vertices,
1ab265
+                             ClutterActor       *actor);
1ab265
+
1ab265
+void clutter_stage_push_pick_clip (ClutterStage       *stage,
1ab265
+                                   const ClutterPoint *vertices);
1ab265
+
1ab265
+void clutter_stage_pop_pick_clip (ClutterStage *stage);
1ab265
+
1ab265
 ClutterActor *_clutter_stage_do_pick (ClutterStage    *stage,
1ab265
                                       gint             x,
1ab265
                                       gint             y,
1ab265
@@ -93,13 +102,6 @@ void                          _clutter_stage_queue_redraw_entry_invalidate (Clut
1ab265
 
1ab265
 CoglFramebuffer *_clutter_stage_get_active_framebuffer (ClutterStage *stage);
1ab265
 
1ab265
-gint32          _clutter_stage_acquire_pick_id          (ClutterStage *stage,
1ab265
-                                                         ClutterActor *actor);
1ab265
-void            _clutter_stage_release_pick_id          (ClutterStage *stage,
1ab265
-                                                         gint32        pick_id);
1ab265
-ClutterActor *  _clutter_stage_get_actor_by_pick_id     (ClutterStage *stage,
1ab265
-                                                         gint32        pick_id);
1ab265
-
1ab265
 void            _clutter_stage_add_pointer_drag_actor    (ClutterStage       *stage,
1ab265
                                                           ClutterInputDevice *device,
1ab265
                                                           ClutterActor       *actor);
1ab265
diff --git a/clutter/clutter/clutter-stage-window.c b/clutter/clutter/clutter-stage-window.c
1ab265
index e8fa976a7d..4c4ef9d643 100644
1ab265
--- a/clutter/clutter/clutter-stage-window.c
1ab265
+++ b/clutter/clutter/clutter-stage-window.c
1ab265
@@ -293,24 +293,6 @@ _clutter_stage_window_redraw (ClutterStageWindow *window)
1ab265
     iface->redraw (window);
1ab265
 }
1ab265
 
1ab265
-
1ab265
-void
1ab265
-_clutter_stage_window_get_dirty_pixel (ClutterStageWindow *window,
1ab265
-                                       ClutterStageView   *view,
1ab265
-                                       int *x, int *y)
1ab265
-{
1ab265
-  ClutterStageWindowInterface *iface;
1ab265
-
1ab265
-  *x = 0;
1ab265
-  *y = 0;
1ab265
-
1ab265
-  g_return_if_fail (CLUTTER_IS_STAGE_WINDOW (window));
1ab265
-
1ab265
-  iface = CLUTTER_STAGE_WINDOW_GET_IFACE (window);
1ab265
-  if (iface->get_dirty_pixel)
1ab265
-    iface->get_dirty_pixel (window, view, x, y);
1ab265
-}
1ab265
-
1ab265
 gboolean
1ab265
 _clutter_stage_window_can_clip_redraws (ClutterStageWindow *window)
1ab265
 {
1ab265
diff --git a/clutter/clutter/clutter-stage-window.h b/clutter/clutter/clutter-stage-window.h
1ab265
index 6c3601745f..aa0c5f71cc 100644
1ab265
--- a/clutter/clutter/clutter-stage-window.h
1ab265
+++ b/clutter/clutter/clutter-stage-window.h
1ab265
@@ -68,10 +68,6 @@ struct _ClutterStageWindowInterface
1ab265
 
1ab265
   void              (* redraw)                  (ClutterStageWindow *stage_window);
1ab265
 
1ab265
-  void              (* get_dirty_pixel)         (ClutterStageWindow *stage_window,
1ab265
-                                                 ClutterStageView   *view,
1ab265
-                                                 int *x, int *y);
1ab265
-
1ab265
   gboolean          (* can_clip_redraws)        (ClutterStageWindow *stage_window);
1ab265
 
1ab265
   GList            *(* get_views)               (ClutterStageWindow *stage_window);
1ab265
@@ -119,10 +115,6 @@ void              _clutter_stage_window_set_accept_focus        (ClutterStageWin
1ab265
 
1ab265
 void              _clutter_stage_window_redraw                  (ClutterStageWindow *window);
1ab265
 
1ab265
-void              _clutter_stage_window_get_dirty_pixel         (ClutterStageWindow *window,
1ab265
-                                                                 ClutterStageView   *view,
1ab265
-                                                                 int *x, int *y);
1ab265
-
1ab265
 gboolean          _clutter_stage_window_can_clip_redraws        (ClutterStageWindow *window);
1ab265
 
1ab265
 GList *           _clutter_stage_window_get_views               (ClutterStageWindow *window);
1ab265
diff --git a/clutter/clutter/clutter-stage.c b/clutter/clutter/clutter-stage.c
1ab265
index aaa77d9ede..7d88d5752f 100644
1ab265
--- a/clutter/clutter/clutter-stage.c
1ab265
+++ b/clutter/clutter/clutter-stage.c
1ab265
@@ -97,6 +97,11 @@ typedef enum /*< prefix=CLUTTER_STAGE >*/
1ab265
 
1ab265
 #define STAGE_NO_CLEAR_ON_PAINT(s)      ((((ClutterStage *) (s))->priv->stage_hints & CLUTTER_STAGE_NO_CLEAR_ON_PAINT) != 0)
1ab265
 
1ab265
+#ifndef G_APPROX_VALUE
1ab265
+#define G_APPROX_VALUE(a, b, epsilon) \
1ab265
+  (((a) > (b) ? (a) - (b) : (b) - (a)) < (epsilon))
1ab265
+#endif
1ab265
+
1ab265
 struct _ClutterStageQueueRedrawEntry
1ab265
 {
1ab265
   ClutterActor *actor;
1ab265
@@ -104,6 +109,19 @@ struct _ClutterStageQueueRedrawEntry
1ab265
   ClutterPaintVolume clip;
1ab265
 };
1ab265
 
1ab265
+typedef struct _PickRecord
1ab265
+{
1ab265
+  ClutterPoint vertex[4];
1ab265
+  ClutterActor *actor;
1ab265
+  int clip_stack_top;
1ab265
+} PickRecord;
1ab265
+
1ab265
+typedef struct _PickClipRecord
1ab265
+{
1ab265
+  int prev;
1ab265
+  ClutterPoint vertex[4];
1ab265
+} PickClipRecord;
1ab265
+
1ab265
 struct _ClutterStagePrivate
1ab265
 {
1ab265
   /* the stage implementation */
1ab265
@@ -137,7 +155,11 @@ struct _ClutterStagePrivate
1ab265
   GTimer *fps_timer;
1ab265
   gint32 timer_n_frames;
1ab265
 
1ab265
-  ClutterIDPool *pick_id_pool;
1ab265
+  GArray *pick_stack;
1ab265
+  GArray *pick_clip_stack;
1ab265
+  int pick_clip_stack_top;
1ab265
+  gboolean pick_stack_frozen;
1ab265
+  ClutterPickMode cached_pick_mode;
1ab265
 
1ab265
 #ifdef CLUTTER_ENABLE_DEBUG
1ab265
   gulong redraw_count;
1ab265
@@ -326,6 +348,211 @@ clutter_stage_get_preferred_height (ClutterActor *self,
1ab265
     *natural_height_p = geom.height;
1ab265
 }
1ab265
 
1ab265
+static void
1ab265
+add_pick_stack_weak_refs (ClutterStage *stage)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv = stage->priv;
1ab265
+  int i;
1ab265
+
1ab265
+  if (priv->pick_stack_frozen)
1ab265
+    return;
1ab265
+
1ab265
+  for (i = 0; i < priv->pick_stack->len; i++)
1ab265
+    {
1ab265
+      PickRecord *rec = &g_array_index (priv->pick_stack, PickRecord, i);
1ab265
+
1ab265
+      if (rec->actor)
1ab265
+        g_object_add_weak_pointer (G_OBJECT (rec->actor),
1ab265
+                                   (gpointer) &rec->actor);
1ab265
+    }
1ab265
+
1ab265
+  priv->pick_stack_frozen = TRUE;
1ab265
+}
1ab265
+
1ab265
+static void
1ab265
+remove_pick_stack_weak_refs (ClutterStage *stage)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv = stage->priv;
1ab265
+  int i;
1ab265
+
1ab265
+  if (!priv->pick_stack_frozen)
1ab265
+    return;
1ab265
+
1ab265
+  for (i = 0; i < priv->pick_stack->len; i++)
1ab265
+    {
1ab265
+      PickRecord *rec = &g_array_index (priv->pick_stack, PickRecord, i);
1ab265
+
1ab265
+      if (rec->actor)
1ab265
+        g_object_remove_weak_pointer (G_OBJECT (rec->actor),
1ab265
+                                      (gpointer) &rec->actor);
1ab265
+    }
1ab265
+
1ab265
+  priv->pick_stack_frozen = FALSE;
1ab265
+}
1ab265
+
1ab265
+static void
1ab265
+_clutter_stage_clear_pick_stack (ClutterStage *stage)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv = stage->priv;
1ab265
+
1ab265
+  remove_pick_stack_weak_refs (stage);
1ab265
+  g_array_set_size (priv->pick_stack, 0);
1ab265
+  g_array_set_size (priv->pick_clip_stack, 0);
1ab265
+  priv->pick_clip_stack_top = -1;
1ab265
+  priv->cached_pick_mode = CLUTTER_PICK_NONE;
1ab265
+}
1ab265
+
1ab265
+void
1ab265
+clutter_stage_log_pick (ClutterStage       *stage,
1ab265
+                        const ClutterPoint *vertices,
1ab265
+                        ClutterActor       *actor)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv;
1ab265
+  PickRecord rec;
1ab265
+
1ab265
+  g_return_if_fail (CLUTTER_IS_STAGE (stage));
1ab265
+  g_return_if_fail (actor != NULL);
1ab265
+
1ab265
+  priv = stage->priv;
1ab265
+
1ab265
+  g_assert (!priv->pick_stack_frozen);
1ab265
+
1ab265
+  memcpy (rec.vertex, vertices, 4 * sizeof (ClutterPoint));
1ab265
+  rec.actor = actor;
1ab265
+  rec.clip_stack_top = priv->pick_clip_stack_top;
1ab265
+
1ab265
+  g_array_append_val (priv->pick_stack, rec);
1ab265
+}
1ab265
+
1ab265
+void
1ab265
+clutter_stage_push_pick_clip (ClutterStage       *stage,
1ab265
+                              const ClutterPoint *vertices)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv;
1ab265
+  PickClipRecord clip;
1ab265
+
1ab265
+  g_return_if_fail (CLUTTER_IS_STAGE (stage));
1ab265
+
1ab265
+  priv = stage->priv;
1ab265
+
1ab265
+  g_assert (!priv->pick_stack_frozen);
1ab265
+
1ab265
+  clip.prev = priv->pick_clip_stack_top;
1ab265
+  memcpy (clip.vertex, vertices, 4 * sizeof (ClutterPoint));
1ab265
+
1ab265
+  g_array_append_val (priv->pick_clip_stack, clip);
1ab265
+  priv->pick_clip_stack_top = priv->pick_clip_stack->len - 1;
1ab265
+}
1ab265
+
1ab265
+void
1ab265
+clutter_stage_pop_pick_clip (ClutterStage *stage)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv;
1ab265
+  const PickClipRecord *top;
1ab265
+
1ab265
+  g_return_if_fail (CLUTTER_IS_STAGE (stage));
1ab265
+
1ab265
+  priv = stage->priv;
1ab265
+
1ab265
+  g_assert (!priv->pick_stack_frozen);
1ab265
+  g_assert (priv->pick_clip_stack_top >= 0);
1ab265
+
1ab265
+  /* Individual elements of pick_clip_stack are not freed. This is so they
1ab265
+   * can be shared as part of a tree of different stacks used by different
1ab265
+   * actors in the pick_stack. The whole pick_clip_stack does however get
1ab265
+   * freed later in _clutter_stage_clear_pick_stack.
1ab265
+   */
1ab265
+
1ab265
+  top = &g_array_index (priv->pick_clip_stack,
1ab265
+                        PickClipRecord,
1ab265
+                        priv->pick_clip_stack_top);
1ab265
+
1ab265
+  priv->pick_clip_stack_top = top->prev;
1ab265
+}
1ab265
+
1ab265
+static gboolean
1ab265
+is_quadrilateral_axis_aligned_rectangle (const ClutterPoint *vertices)
1ab265
+{
1ab265
+  int i;
1ab265
+
1ab265
+  for (i = 0; i < 4; i++)
1ab265
+    {
1ab265
+      if (!G_APPROX_VALUE (vertices[i].x,
1ab265
+                           vertices[(i + 1) % 4].x,
1ab265
+                           FLT_EPSILON) &&
1ab265
+          !G_APPROX_VALUE (vertices[i].y,
1ab265
+                           vertices[(i + 1) % 4].y,
1ab265
+                           FLT_EPSILON))
1ab265
+        return FALSE;
1ab265
+    }
1ab265
+  return TRUE;
1ab265
+}
1ab265
+
1ab265
+static gboolean
1ab265
+is_inside_axis_aligned_rectangle (const ClutterPoint *point,
1ab265
+                                  const ClutterPoint *vertices)
1ab265
+{
1ab265
+  float min_x = FLT_MAX;
1ab265
+  float max_x = FLT_MIN;
1ab265
+  float min_y = FLT_MAX;
1ab265
+  float max_y = FLT_MIN;
1ab265
+  int i;
1ab265
+
1ab265
+  for (i = 0; i < 3; i++)
1ab265
+    {
1ab265
+      min_x = MIN (min_x, vertices[i].x);
1ab265
+      min_y = MIN (min_y, vertices[i].y);
1ab265
+      max_x = MAX (max_x, vertices[i].x);
1ab265
+      max_y = MAX (max_y, vertices[i].y);
1ab265
+    }
1ab265
+
1ab265
+  return (point->x >= min_x &&
1ab265
+          point->y >= min_y &&
1ab265
+          point->x < max_x &&
1ab265
+          point->y < max_y);
1ab265
+}
1ab265
+
1ab265
+static gboolean
1ab265
+is_inside_input_region (const ClutterPoint *point,
1ab265
+                        const ClutterPoint *vertices)
1ab265
+{
1ab265
+
1ab265
+  if (is_quadrilateral_axis_aligned_rectangle (vertices))
1ab265
+    return is_inside_axis_aligned_rectangle (point, vertices);
1ab265
+  else
1ab265
+    return clutter_point_inside_quadrilateral (point, vertices);
1ab265
+}
1ab265
+
1ab265
+static gboolean
1ab265
+pick_record_contains_pixel (ClutterStage     *stage,
1ab265
+                            const PickRecord *rec,
1ab265
+                            int               x,
1ab265
+                            int               y)
1ab265
+{
1ab265
+  const ClutterPoint point = CLUTTER_POINT_INIT (x, y);
1ab265
+  ClutterStagePrivate *priv;
1ab265
+  int clip_index;
1ab265
+
1ab265
+  if (!is_inside_input_region (&point, rec->vertex))
1ab265
+      return FALSE;
1ab265
+
1ab265
+  priv = stage->priv;
1ab265
+  clip_index = rec->clip_stack_top;
1ab265
+  while (clip_index >= 0)
1ab265
+    {
1ab265
+      const PickClipRecord *clip = &g_array_index (priv->pick_clip_stack,
1ab265
+                                                   PickClipRecord,
1ab265
+                                                   clip_index);
1ab265
+
1ab265
+      if (!is_inside_input_region (&point, clip->vertex))
1ab265
+        return FALSE;
1ab265
+
1ab265
+      clip_index = clip->prev;
1ab265
+    }
1ab265
+
1ab265
+  return TRUE;
1ab265
+}
1ab265
+
1ab265
 static inline void
1ab265
 queue_full_redraw (ClutterStage *stage)
1ab265
 {
1ab265
@@ -636,6 +863,12 @@ clutter_stage_do_paint_view (ClutterStage                *stage,
1ab265
   float viewport[4];
1ab265
   cairo_rectangle_int_t geom;
1ab265
 
1ab265
+  /* Any mode of painting/picking invalidates the pick cache, unless we're
1ab265
+   * in the middle of building it. So we reset the cached flag but don't
1ab265
+   * completely clear the pick stack.
1ab265
+   */
1ab265
+  priv->cached_pick_mode = CLUTTER_PICK_NONE;
1ab265
+
1ab265
   _clutter_stage_window_get_geometry (priv->impl, &geom);
1ab265
 
1ab265
   viewport[0] = priv->viewport[0];
1ab265
@@ -1414,40 +1647,6 @@ clutter_stage_get_redraw_clip_bounds (ClutterStage          *stage,
1ab265
     }
1ab265
 }
1ab265
 
1ab265
-static void
1ab265
-read_pixels_to_file (CoglFramebuffer *fb,
1ab265
-                     char            *filename_stem,
1ab265
-                     int              x,
1ab265
-                     int              y,
1ab265
-                     int              width,
1ab265
-                     int              height)
1ab265
-{
1ab265
-  guint8 *data;
1ab265
-  cairo_surface_t *surface;
1ab265
-  static int read_count = 0;
1ab265
-  char *filename = g_strdup_printf ("%s-%05d.png",
1ab265
-                                    filename_stem,
1ab265
-                                    read_count);
1ab265
-
1ab265
-  data = g_malloc (4 * width * height);
1ab265
-  cogl_framebuffer_read_pixels (fb,
1ab265
-                                x, y, width, height,
1ab265
-                                CLUTTER_CAIRO_FORMAT_ARGB32,
1ab265
-                                data);
1ab265
-
1ab265
-  surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_RGB24,
1ab265
-                                                 width, height,
1ab265
-                                                 width * 4);
1ab265
-
1ab265
-  cairo_surface_write_to_png (surface, filename);
1ab265
-  cairo_surface_destroy (surface);
1ab265
-
1ab265
-  g_free (data);
1ab265
-  g_free (filename);
1ab265
-
1ab265
-  read_count++;
1ab265
-}
1ab265
-
1ab265
 static ClutterActor *
1ab265
 _clutter_stage_do_pick_on_view (ClutterStage     *stage,
1ab265
                                 gint              x,
1ab265
@@ -1455,140 +1654,42 @@ _clutter_stage_do_pick_on_view (ClutterStage     *stage,
1ab265
                                 ClutterPickMode   mode,
1ab265
                                 ClutterStageView *view)
1ab265
 {
1ab265
-  ClutterActor *actor = CLUTTER_ACTOR (stage);
1ab265
+  ClutterMainContext *context = _clutter_context_get_default ();
1ab265
   ClutterStagePrivate *priv = stage->priv;
1ab265
   CoglFramebuffer *fb = clutter_stage_view_get_framebuffer (view);
1ab265
-  cairo_rectangle_int_t view_layout;
1ab265
-  ClutterMainContext *context;
1ab265
-  guchar pixel[4] = { 0xff, 0xff, 0xff, 0xff };
1ab265
-  CoglColor stage_pick_id;
1ab265
-  gboolean dither_enabled_save;
1ab265
-  ClutterActor *retval;
1ab265
-  gint dirty_x;
1ab265
-  gint dirty_y;
1ab265
-  gint read_x;
1ab265
-  gint read_y;
1ab265
-  float fb_width, fb_height;
1ab265
-  float fb_scale;
1ab265
-  float viewport_offset_x;
1ab265
-  float viewport_offset_y;
1ab265
-
1ab265
-  priv = stage->priv;
1ab265
-
1ab265
-  context = _clutter_context_get_default ();
1ab265
-  fb_scale = clutter_stage_view_get_scale (view);
1ab265
-  clutter_stage_view_get_layout (view, &view_layout);
1ab265
-
1ab265
-  fb_width = view_layout.width * fb_scale;
1ab265
-  fb_height = view_layout.height * fb_scale;
1ab265
-  cogl_push_framebuffer (fb);
1ab265
-
1ab265
-  /* needed for when a context switch happens */
1ab265
-  _clutter_stage_maybe_setup_viewport (stage, view);
1ab265
-
1ab265
-  /* FIXME: For some reason leaving the cogl clip stack empty causes the
1ab265
-   * picking to not work at all, so setting it the whole framebuffer content
1ab265
-   * for now. */
1ab265
-  cogl_framebuffer_push_scissor_clip (fb, 0, 0,
1ab265
-                                      view_layout.width * fb_scale,
1ab265
-                                      view_layout.height * fb_scale);
1ab265
-
1ab265
-  _clutter_stage_window_get_dirty_pixel (priv->impl, view, &dirty_x, &dirty_y);
1ab265
+  int i;
1ab265
 
1ab265
-  if (G_LIKELY (!(clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS)))
1ab265
-    {
1ab265
-      CLUTTER_NOTE (PICK, "Pushing pick scissor clip x: %d, y: %d, 1x1",
1ab265
-                    (int) (dirty_x * fb_scale),
1ab265
-                    (int) (dirty_y * fb_scale));
1ab265
-      cogl_framebuffer_push_scissor_clip (fb, dirty_x * fb_scale, dirty_y * fb_scale, 1, 1);
1ab265
-    }
1ab265
+  g_assert (context->pick_mode == CLUTTER_PICK_NONE);
1ab265
 
1ab265
-  viewport_offset_x = x * fb_scale - dirty_x * fb_scale;
1ab265
-  viewport_offset_y = y * fb_scale - dirty_y * fb_scale;
1ab265
-  CLUTTER_NOTE (PICK, "Setting viewport to %f, %f, %f, %f",
1ab265
-                priv->viewport[0] * fb_scale - viewport_offset_x,
1ab265
-                priv->viewport[1] * fb_scale - viewport_offset_y,
1ab265
-                priv->viewport[2] * fb_scale,
1ab265
-                priv->viewport[3] * fb_scale);
1ab265
-  cogl_framebuffer_set_viewport (fb,
1ab265
-                                 priv->viewport[0] * fb_scale - viewport_offset_x,
1ab265
-                                 priv->viewport[1] * fb_scale - viewport_offset_y,
1ab265
-                                 priv->viewport[2] * fb_scale,
1ab265
-                                 priv->viewport[3] * fb_scale);
1ab265
-
1ab265
-  read_x = dirty_x * fb_scale;
1ab265
-  read_y = dirty_y * fb_scale;
1ab265
-
1ab265
-  CLUTTER_NOTE (PICK, "Performing pick at %i,%i on view %dx%d+%d+%d s: %f",
1ab265
-                x, y,
1ab265
-                view_layout.width, view_layout.height,
1ab265
-                view_layout.x, view_layout.y, fb_scale);
1ab265
-
1ab265
-  cogl_color_init_from_4ub (&stage_pick_id, 255, 255, 255, 255);
1ab265
-  cogl_framebuffer_clear (fb, COGL_BUFFER_BIT_COLOR | COGL_BUFFER_BIT_DEPTH, &stage_pick_id);
1ab265
-
1ab265
-  /* Disable dithering (if any) when doing the painting in pick mode */
1ab265
-  dither_enabled_save = cogl_framebuffer_get_dither_enabled (fb);
1ab265
-  cogl_framebuffer_set_dither_enabled (fb, FALSE);
1ab265
-
1ab265
-  /* Render the entire scence in pick mode - just single colored silhouette's
1ab265
-   * are drawn offscreen (as we never swap buffers)
1ab265
-  */
1ab265
-  context->pick_mode = mode;
1ab265
-
1ab265
-  clutter_stage_do_paint_view (stage, view, NULL);
1ab265
-  context->pick_mode = CLUTTER_PICK_NONE;
1ab265
-
1ab265
-  /* Read the color of the screen co-ords pixel. RGBA_8888_PRE is used
1ab265
-     even though we don't care about the alpha component because under
1ab265
-     GLES this is the only format that is guaranteed to work so Cogl
1ab265
-     will end up having to do a conversion if any other format is
1ab265
-     used. The format is requested as pre-multiplied because Cogl
1ab265
-     assumes that all pixels in the framebuffer are premultiplied so
1ab265
-     it avoids a conversion. */
1ab265
-  cogl_framebuffer_read_pixels (fb,
1ab265
-                                read_x, read_y, 1, 1,
1ab265
-                                COGL_PIXEL_FORMAT_RGBA_8888_PRE,
1ab265
-                                pixel);
1ab265
-
1ab265
-  if (G_UNLIKELY (clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS))
1ab265
+  if (mode != priv->cached_pick_mode)
1ab265
     {
1ab265
-      char *file_name =
1ab265
-        g_strdup_printf ("pick-buffer-%s-view-x-%d",
1ab265
-                         _clutter_actor_get_debug_name (actor),
1ab265
-                         view_layout.x);
1ab265
+      _clutter_stage_clear_pick_stack (stage);
1ab265
 
1ab265
-      read_pixels_to_file (fb, file_name, 0, 0, fb_width, fb_height);
1ab265
+      cogl_push_framebuffer (fb);
1ab265
 
1ab265
-      g_free (file_name);
1ab265
-    }
1ab265
-
1ab265
-  /* Restore whether GL_DITHER was enabled */
1ab265
-  cogl_framebuffer_set_dither_enabled (fb, dither_enabled_save);
1ab265
-
1ab265
-  if (G_LIKELY (!(clutter_pick_debug_flags & CLUTTER_DEBUG_DUMP_PICK_BUFFERS)))
1ab265
-    cogl_framebuffer_pop_clip (fb);
1ab265
+      context->pick_mode = mode;
1ab265
+      clutter_stage_do_paint_view (stage, view, NULL);
1ab265
+      context->pick_mode = CLUTTER_PICK_NONE;
1ab265
+      priv->cached_pick_mode = mode;
1ab265
 
1ab265
-  cogl_framebuffer_pop_clip (fb);
1ab265
+      cogl_pop_framebuffer ();
1ab265
 
1ab265
-  _clutter_stage_dirty_viewport (stage);
1ab265
+      add_pick_stack_weak_refs (stage);
1ab265
+    }
1ab265
 
1ab265
-  if (pixel[0] == 0xff && pixel[1] == 0xff && pixel[2] == 0xff)
1ab265
-    retval = actor;
1ab265
-  else
1ab265
+  /* Search all "painted" pickable actors from front to back. A linear search
1ab265
+   * is required, and also performs fine since there is typically only
1ab265
+   * on the order of dozens of actors in the list (on screen) at a time.
1ab265
+   */
1ab265
+  for (i = priv->pick_stack->len - 1; i >= 0; i--)
1ab265
     {
1ab265
-      guint32 id_ = _clutter_pixel_to_id (pixel);
1ab265
+      const PickRecord *rec = &g_array_index (priv->pick_stack, PickRecord, i);
1ab265
 
1ab265
-      retval = _clutter_stage_get_actor_by_pick_id (stage, id_);
1ab265
-      CLUTTER_NOTE (PICK, "Picking actor %s with id %u (pixel: 0x%x%x%x%x",
1ab265
-                    G_OBJECT_TYPE_NAME (retval),
1ab265
-                    id_,
1ab265
-                    pixel[0], pixel[1], pixel[2], pixel[3]);
1ab265
+      if (rec->actor && pick_record_contains_pixel (stage, rec, x, y))
1ab265
+        return rec->actor;
1ab265
     }
1ab265
 
1ab265
-  cogl_pop_framebuffer ();
1ab265
-
1ab265
-  return retval;
1ab265
+  return CLUTTER_ACTOR (stage);
1ab265
 }
1ab265
 
1ab265
 static ClutterStageView *
1ab265
@@ -1901,7 +2002,9 @@ clutter_stage_finalize (GObject *object)
1ab265
 
1ab265
   g_array_free (priv->paint_volume_stack, TRUE);
1ab265
 
1ab265
-  _clutter_id_pool_free (priv->pick_id_pool);
1ab265
+  _clutter_stage_clear_pick_stack (stage);
1ab265
+  g_array_free (priv->pick_clip_stack, TRUE);
1ab265
+  g_array_free (priv->pick_stack, TRUE);
1ab265
 
1ab265
   if (priv->fps_timer != NULL)
1ab265
     g_timer_destroy (priv->fps_timer);
1ab265
@@ -2435,7 +2538,10 @@ clutter_stage_init (ClutterStage *self)
1ab265
   priv->paint_volume_stack =
1ab265
     g_array_new (FALSE, FALSE, sizeof (ClutterPaintVolume));
1ab265
 
1ab265
-  priv->pick_id_pool = _clutter_id_pool_new (256);
1ab265
+  priv->pick_stack = g_array_new (FALSE, FALSE, sizeof (PickRecord));
1ab265
+  priv->pick_clip_stack = g_array_new (FALSE, FALSE, sizeof (PickClipRecord));
1ab265
+  priv->pick_clip_stack_top = -1;
1ab265
+  priv->cached_pick_mode = CLUTTER_PICK_NONE;
1ab265
 }
1ab265
 
1ab265
 /**
1ab265
@@ -4253,6 +4359,12 @@ _clutter_stage_queue_actor_redraw (ClutterStage                 *stage,
1ab265
   CLUTTER_NOTE (CLIPPING, "stage_queue_actor_redraw (actor=%s, clip=%p): ",
1ab265
                 _clutter_actor_get_debug_name (actor), clip);
1ab265
 
1ab265
+  /* Queuing a redraw or clip change invalidates the pick cache, unless we're
1ab265
+   * in the middle of building it. So we reset the cached flag but don't
1ab265
+   * completely clear the pick stack...
1ab265
+   */
1ab265
+  priv->cached_pick_mode = CLUTTER_PICK_NONE;
1ab265
+
1ab265
   if (!priv->redraw_pending)
1ab265
     {
1ab265
       ClutterMasterClock *master_clock;
1ab265
@@ -4513,39 +4625,6 @@ _clutter_stage_get_active_framebuffer (ClutterStage *stage)
1ab265
   return stage->priv->active_framebuffer;
1ab265
 }
1ab265
 
1ab265
-gint32
1ab265
-_clutter_stage_acquire_pick_id (ClutterStage *stage,
1ab265
-                                ClutterActor *actor)
1ab265
-{
1ab265
-  ClutterStagePrivate *priv = stage->priv;
1ab265
-
1ab265
-  g_assert (priv->pick_id_pool != NULL);
1ab265
-
1ab265
-  return _clutter_id_pool_add (priv->pick_id_pool, actor);
1ab265
-}
1ab265
-
1ab265
-void
1ab265
-_clutter_stage_release_pick_id (ClutterStage *stage,
1ab265
-                                gint32        pick_id)
1ab265
-{
1ab265
-  ClutterStagePrivate *priv = stage->priv;
1ab265
-
1ab265
-  g_assert (priv->pick_id_pool != NULL);
1ab265
-
1ab265
-  _clutter_id_pool_remove (priv->pick_id_pool, pick_id);
1ab265
-}
1ab265
-
1ab265
-ClutterActor *
1ab265
-_clutter_stage_get_actor_by_pick_id (ClutterStage *stage,
1ab265
-                                     gint32        pick_id)
1ab265
-{
1ab265
-  ClutterStagePrivate *priv = stage->priv;
1ab265
-
1ab265
-  g_assert (priv->pick_id_pool != NULL);
1ab265
-
1ab265
-  return _clutter_id_pool_lookup (priv->pick_id_pool, pick_id);
1ab265
-}
1ab265
-
1ab265
 void
1ab265
 _clutter_stage_add_pointer_drag_actor (ClutterStage       *stage,
1ab265
                                        ClutterInputDevice *device,
1ab265
diff --git a/clutter/clutter/cogl/clutter-stage-cogl.c b/clutter/clutter/cogl/clutter-stage-cogl.c
1ab265
index 3f1f609c4e..effed79759 100644
1ab265
--- a/clutter/clutter/cogl/clutter-stage-cogl.c
1ab265
+++ b/clutter/clutter/cogl/clutter-stage-cogl.c
1ab265
@@ -926,57 +926,6 @@ clutter_stage_cogl_redraw (ClutterStageWindow *stage_window)
1ab265
   stage_cogl->frame_count++;
1ab265
 }
1ab265
 
1ab265
-static void
1ab265
-clutter_stage_cogl_get_dirty_pixel (ClutterStageWindow *stage_window,
1ab265
-                                    ClutterStageView   *view,
1ab265
-                                    int                *x,
1ab265
-                                    int                *y)
1ab265
-{
1ab265
-  CoglFramebuffer *onscreen = clutter_stage_view_get_onscreen (view);
1ab265
-  ClutterStageViewCogl *view_cogl = CLUTTER_STAGE_VIEW_COGL (view);
1ab265
-  ClutterStageViewCoglPrivate *view_priv =
1ab265
-    clutter_stage_view_cogl_get_instance_private (view_cogl);
1ab265
-  gboolean has_buffer_age =
1ab265
-    cogl_is_onscreen (onscreen) &&
1ab265
-    is_buffer_age_enabled ();
1ab265
-  float fb_scale;
1ab265
-  gboolean scale_is_fractional;
1ab265
-
1ab265
-  fb_scale = clutter_stage_view_get_scale (view);
1ab265
-  if (fb_scale != floorf (fb_scale))
1ab265
-    scale_is_fractional = TRUE;
1ab265
-  else
1ab265
-    scale_is_fractional = FALSE;
1ab265
-
1ab265
-  /*
1ab265
-   * Buffer damage is tracked in the framebuffer coordinate space
1ab265
-   * using the damage history. When fractional scaling is used, a
1ab265
-   * coordinate on the stage might not correspond to the exact position of any
1ab265
-   * physical pixel, which causes issues when painting using the pick mode.
1ab265
-   *
1ab265
-   * For now, always use the (0, 0) pixel for picking when using fractional
1ab265
-   * framebuffer scaling.
1ab265
-   */
1ab265
-  if (!has_buffer_age ||
1ab265
-      scale_is_fractional ||
1ab265
-      !clutter_damage_history_is_age_valid (view_priv->damage_history, 0))
1ab265
-    {
1ab265
-      *x = 0;
1ab265
-      *y = 0;
1ab265
-    }
1ab265
-  else
1ab265
-    {
1ab265
-      cairo_rectangle_int_t view_layout;
1ab265
-      const cairo_rectangle_int_t *fb_damage;
1ab265
-
1ab265
-      clutter_stage_view_get_layout (view, &view_layout);
1ab265
-
1ab265
-      fb_damage = clutter_damage_history_lookup (view_priv->damage_history, 0);
1ab265
-      *x = fb_damage->x / fb_scale;
1ab265
-      *y = fb_damage->y / fb_scale;
1ab265
-    }
1ab265
-}
1ab265
-
1ab265
 static void
1ab265
 clutter_stage_window_iface_init (ClutterStageWindowInterface *iface)
1ab265
 {
1ab265
@@ -994,7 +943,6 @@ clutter_stage_window_iface_init (ClutterStageWindowInterface *iface)
1ab265
   iface->ignoring_redraw_clips = clutter_stage_cogl_ignoring_redraw_clips;
1ab265
   iface->get_redraw_clip_bounds = clutter_stage_cogl_get_redraw_clip_bounds;
1ab265
   iface->redraw = clutter_stage_cogl_redraw;
1ab265
-  iface->get_dirty_pixel = clutter_stage_cogl_get_dirty_pixel;
1ab265
 }
1ab265
 
1ab265
 static void
1ab265
diff --git a/clutter/clutter/deprecated/clutter-texture.c b/clutter/clutter/deprecated/clutter-texture.c
1ab265
index bea239f454..2c677b8a44 100644
1ab265
--- a/clutter/clutter/deprecated/clutter-texture.c
1ab265
+++ b/clutter/clutter/deprecated/clutter-texture.c
1ab265
@@ -572,83 +572,6 @@ gen_texcoords_and_draw_cogl_rectangle (ClutterActor    *self,
1ab265
                                             0, 0, t_w, t_h);
1ab265
 }
1ab265
 
1ab265
-static CoglPipeline *
1ab265
-create_pick_pipeline (ClutterActor *self)
1ab265
-{
1ab265
-  ClutterTexture *texture = CLUTTER_TEXTURE (self);
1ab265
-  ClutterTexturePrivate *priv = texture->priv;
1ab265
-  CoglPipeline *pick_pipeline = cogl_pipeline_copy (texture_template_pipeline);
1ab265
-  GError *error = NULL;
1ab265
-
1ab265
-  if (!cogl_pipeline_set_layer_combine (pick_pipeline, 0,
1ab265
-                                        "RGBA = "
1ab265
-                                        "  MODULATE (CONSTANT, TEXTURE[A])",
1ab265
-                                        &error))
1ab265
-    {
1ab265
-      if (!priv->seen_create_pick_pipeline_warning)
1ab265
-        g_warning ("Error setting up texture combine for shaped "
1ab265
-                   "texture picking: %s", error->message);
1ab265
-      priv->seen_create_pick_pipeline_warning = TRUE;
1ab265
-      g_error_free (error);
1ab265
-      cogl_object_unref (pick_pipeline);
1ab265
-      return NULL;
1ab265
-    }
1ab265
-
1ab265
-  cogl_pipeline_set_blend (pick_pipeline,
1ab265
-                           "RGBA = ADD (SRC_COLOR[RGBA], 0)",
1ab265
-                           NULL);
1ab265
-
1ab265
-  cogl_pipeline_set_alpha_test_function (pick_pipeline,
1ab265
-                                         COGL_PIPELINE_ALPHA_FUNC_EQUAL,
1ab265
-                                         1.0);
1ab265
-
1ab265
-  return pick_pipeline;
1ab265
-}
1ab265
-
1ab265
-static void
1ab265
-clutter_texture_pick (ClutterActor       *self,
1ab265
-                      const ClutterColor *color)
1ab265
-{
1ab265
-  ClutterTexture *texture = CLUTTER_TEXTURE (self);
1ab265
-  ClutterTexturePrivate *priv = texture->priv;
1ab265
-  CoglFramebuffer *framebuffer = cogl_get_draw_framebuffer ();
1ab265
-
1ab265
-  if (!clutter_actor_should_pick_paint (self))
1ab265
-    return;
1ab265
-
1ab265
-  if (G_LIKELY (priv->pick_with_alpha_supported) && priv->pick_with_alpha)
1ab265
-    {
1ab265
-      CoglColor pick_color;
1ab265
-
1ab265
-      if (priv->pick_pipeline == NULL)
1ab265
-        priv->pick_pipeline = create_pick_pipeline (self);
1ab265
-
1ab265
-      if (priv->pick_pipeline == NULL)
1ab265
-        {
1ab265
-          priv->pick_with_alpha_supported = FALSE;
1ab265
-          CLUTTER_ACTOR_CLASS (clutter_texture_parent_class)->pick (self,
1ab265
-                                                                    color);
1ab265
-          return;
1ab265
-        }
1ab265
-
1ab265
-      if (priv->fbo_handle != NULL)
1ab265
-        update_fbo (self);
1ab265
-
1ab265
-      cogl_color_init_from_4ub (&pick_color,
1ab265
-                                color->red,
1ab265
-                                color->green,
1ab265
-                                color->blue,
1ab265
-                                0xff);
1ab265
-      cogl_pipeline_set_layer_combine_constant (priv->pick_pipeline,
1ab265
-                                                0, &pick_color);
1ab265
-      cogl_pipeline_set_layer_texture (priv->pick_pipeline, 0,
1ab265
-                                       clutter_texture_get_cogl_texture (texture));
1ab265
-      gen_texcoords_and_draw_cogl_rectangle (self, priv->pick_pipeline, framebuffer);
1ab265
-    }
1ab265
-  else
1ab265
-    CLUTTER_ACTOR_CLASS (clutter_texture_parent_class)->pick (self, color);
1ab265
-}
1ab265
-
1ab265
 static void
1ab265
 clutter_texture_paint (ClutterActor *self)
1ab265
 {
1ab265
@@ -767,12 +690,6 @@ clutter_texture_dispose (GObject *object)
1ab265
       priv->pipeline = NULL;
1ab265
     }
1ab265
 
1ab265
-  if (priv->pick_pipeline != NULL)
1ab265
-    {
1ab265
-      cogl_object_unref (priv->pick_pipeline);
1ab265
-      priv->pick_pipeline = NULL;
1ab265
-    }
1ab265
-
1ab265
   G_OBJECT_CLASS (clutter_texture_parent_class)->dispose (object);
1ab265
 }
1ab265
 
1ab265
@@ -944,7 +861,6 @@ clutter_texture_class_init (ClutterTextureClass *klass)
1ab265
   GParamSpec *pspec;
1ab265
 
1ab265
   actor_class->paint            = clutter_texture_paint;
1ab265
-  actor_class->pick             = clutter_texture_pick;
1ab265
   actor_class->get_paint_volume = clutter_texture_get_paint_volume;
1ab265
   actor_class->realize          = clutter_texture_realize;
1ab265
   actor_class->unrealize        = clutter_texture_unrealize;
1ab265
@@ -1263,11 +1179,9 @@ clutter_texture_init (ClutterTexture *self)
1ab265
   priv->repeat_y          = FALSE;
1ab265
   priv->sync_actor_size   = TRUE;
1ab265
   priv->fbo_handle        = NULL;
1ab265
-  priv->pick_pipeline     = NULL;
1ab265
   priv->keep_aspect_ratio = FALSE;
1ab265
   priv->pick_with_alpha   = FALSE;
1ab265
   priv->pick_with_alpha_supported = TRUE;
1ab265
-  priv->seen_create_pick_pipeline_warning = FALSE;
1ab265
 
1ab265
   if (G_UNLIKELY (texture_template_pipeline == NULL))
1ab265
     {
1ab265
@@ -3052,13 +2966,8 @@ clutter_texture_set_pick_with_alpha (ClutterTexture *texture,
1ab265
   if (priv->pick_with_alpha == pick_with_alpha)
1ab265
     return;
1ab265
 
1ab265
-  if (!pick_with_alpha && priv->pick_pipeline != NULL)
1ab265
-    {
1ab265
-      cogl_object_unref (priv->pick_pipeline);
1ab265
-      priv->pick_pipeline = NULL;
1ab265
-    }
1ab265
+  g_assert (!pick_with_alpha);  /* No longer supported */
1ab265
 
1ab265
-  /* NB: the pick pipeline is created lazily when we first pick */
1ab265
   priv->pick_with_alpha = pick_with_alpha;
1ab265
 
1ab265
   /* NB: actors are expected to call clutter_actor_queue_redraw when
1ab265
diff --git a/clutter/tests/conform/actor-pick.c b/clutter/tests/conform/actor-pick.c
1ab265
index 969b4920ac..2bf5954c73 100644
1ab265
--- a/clutter/tests/conform/actor-pick.c
1ab265
+++ b/clutter/tests/conform/actor-pick.c
1ab265
@@ -5,7 +5,6 @@
1ab265
 #define STAGE_HEIGHT 480
1ab265
 #define ACTORS_X 12
1ab265
 #define ACTORS_Y 16
1ab265
-#define SHIFT_STEP STAGE_WIDTH / ACTORS_X
1ab265
 
1ab265
 typedef struct _State State;
1ab265
 
1ab265
@@ -20,84 +19,11 @@ struct _State
1ab265
   gboolean pass;
1ab265
 };
1ab265
 
1ab265
-struct _ShiftEffect
1ab265
-{
1ab265
-  ClutterShaderEffect parent_instance;
1ab265
-};
1ab265
-
1ab265
-struct _ShiftEffectClass
1ab265
-{
1ab265
-  ClutterShaderEffectClass parent_class;
1ab265
-};
1ab265
-
1ab265
-typedef struct _ShiftEffect       ShiftEffect;
1ab265
-typedef struct _ShiftEffectClass  ShiftEffectClass;
1ab265
-
1ab265
-#define TYPE_SHIFT_EFFECT        (shift_effect_get_type ())
1ab265
-
1ab265
-GType shift_effect_get_type (void);
1ab265
-
1ab265
-G_DEFINE_TYPE (ShiftEffect,
1ab265
-               shift_effect,
1ab265
-               CLUTTER_TYPE_SHADER_EFFECT);
1ab265
-
1ab265
-static void
1ab265
-shader_paint (ClutterEffect           *effect,
1ab265
-              ClutterEffectPaintFlags  flags)
1ab265
-{
1ab265
-  ClutterShaderEffect *shader = CLUTTER_SHADER_EFFECT (effect);
1ab265
-  float tex_width;
1ab265
-  ClutterActor *actor =
1ab265
-    clutter_actor_meta_get_actor (CLUTTER_ACTOR_META (effect));
1ab265
-
1ab265
-  if (g_test_verbose ())
1ab265
-    g_debug ("shader_paint");
1ab265
-
1ab265
-  clutter_shader_effect_set_shader_source (shader,
1ab265
-    "uniform sampler2D tex;\n"
1ab265
-    "uniform float step;\n"
1ab265
-    "void main (void)\n"
1ab265
-    "{\n"
1ab265
-    "  cogl_color_out = texture2D(tex, vec2 (cogl_tex_coord_in[0].s + step,\n"
1ab265
-    "                                        cogl_tex_coord_in[0].t));\n"
1ab265
-    "}\n");
1ab265
-
1ab265
-  tex_width = clutter_actor_get_width (actor);
1ab265
-
1ab265
-  clutter_shader_effect_set_uniform (shader, "tex", G_TYPE_INT, 1, 0);
1ab265
-  clutter_shader_effect_set_uniform (shader, "step", G_TYPE_FLOAT, 1,
1ab265
-                                     SHIFT_STEP / tex_width);
1ab265
-
1ab265
-  CLUTTER_EFFECT_CLASS (shift_effect_parent_class)->paint (effect, flags);
1ab265
-}
1ab265
-
1ab265
-static void
1ab265
-shader_pick (ClutterEffect           *effect,
1ab265
-             ClutterEffectPaintFlags  flags)
1ab265
-{
1ab265
-  shader_paint (effect, flags);
1ab265
-}
1ab265
-
1ab265
-static void
1ab265
-shift_effect_class_init (ShiftEffectClass *klass)
1ab265
-{
1ab265
-  ClutterEffectClass *shader_class = CLUTTER_EFFECT_CLASS (klass);
1ab265
-
1ab265
-  shader_class->paint = shader_paint;
1ab265
-  shader_class->pick = shader_pick;
1ab265
-}
1ab265
-
1ab265
-static void
1ab265
-shift_effect_init (ShiftEffect *self)
1ab265
-{
1ab265
-}
1ab265
-
1ab265
 static const char *test_passes[] = {
1ab265
   "No covering actor",
1ab265
   "Invisible covering actor",
1ab265
   "Clipped covering actor",
1ab265
   "Blur effect",
1ab265
-  "Shift effect",
1ab265
 };
1ab265
 
1ab265
 static gboolean
1ab265
@@ -165,30 +91,10 @@ on_timeout (gpointer data)
1ab265
           if (g_test_verbose ())
1ab265
             g_print ("With blur effect:\n");
1ab265
         }
1ab265
-      else if (test_num == 4)
1ab265
-        {
1ab265
-          if (!clutter_feature_available (CLUTTER_FEATURE_SHADERS_GLSL))
1ab265
-            continue;
1ab265
-
1ab265
-          clutter_actor_hide (over_actor);
1ab265
-          clutter_actor_remove_effect_by_name (CLUTTER_ACTOR (state->stage),
1ab265
-                                               "blur");
1ab265
-
1ab265
-          clutter_actor_add_effect_with_name (CLUTTER_ACTOR (state->stage),
1ab265
-                                              "shift",
1ab265
-                                              g_object_new (TYPE_SHIFT_EFFECT,
1ab265
-                                                            NULL));
1ab265
-
1ab265
-          if (g_test_verbose ())
1ab265
-            g_print ("With shift effect:\n");
1ab265
-        }
1ab265
 
1ab265
       for (y = 0; y < ACTORS_Y; y++)
1ab265
         {
1ab265
-          if (test_num == 4)
1ab265
-            x = 1;
1ab265
-          else
1ab265
-            x = 0;
1ab265
+          x = 0;
1ab265
 
1ab265
           for (; x < ACTORS_X; x++)
1ab265
             {
1ab265
@@ -198,9 +104,6 @@ on_timeout (gpointer data)
1ab265
 
1ab265
               pick_x = x * state->actor_width + state->actor_width / 2;
1ab265
 
1ab265
-              if (test_num == 4)
1ab265
-                pick_x -= SHIFT_STEP;
1ab265
-
1ab265
               actor =
1ab265
                 clutter_stage_get_actor_at_pos (CLUTTER_STAGE (state->stage),
1ab265
                                                 CLUTTER_PICK_ALL,
1ab265
diff --git a/clutter/tests/conform/meson.build b/clutter/tests/conform/meson.build
1ab265
index a9f2d7e20c..fffc9014c4 100644
1ab265
--- a/clutter/tests/conform/meson.build
1ab265
+++ b/clutter/tests/conform/meson.build
1ab265
@@ -42,7 +42,6 @@ clutter_conform_tests_deprecated_tests = [
1ab265
   'behaviours',
1ab265
   'group',
1ab265
   'rectangle',
1ab265
-  'texture',
1ab265
 ]
1ab265
 
1ab265
 clutter_conform_tests = []
1ab265
diff --git a/clutter/tests/conform/texture.c b/clutter/tests/conform/texture.c
1ab265
deleted file mode 100644
1ab265
index 392fd5c47e..0000000000
1ab265
--- a/clutter/tests/conform/texture.c
1ab265
+++ /dev/null
1ab265
@@ -1,84 +0,0 @@
1ab265
-#define CLUTTER_DISABLE_DEPRECATION_WARNINGS
1ab265
-#include <clutter/clutter.h>
1ab265
-#include <string.h>
1ab265
-
1ab265
-static CoglHandle
1ab265
-make_texture (void)
1ab265
-{
1ab265
-  guint32 *data = g_malloc (100 * 100 * 4);
1ab265
-  int x;
1ab265
-  int y;
1ab265
-
1ab265
-  for (y = 0; y < 100; y ++)
1ab265
-    for (x = 0; x < 100; x++)
1ab265
-      {
1ab265
-        if (x < 50 && y < 50)
1ab265
-          data[y * 100 + x] = 0xff00ff00;
1ab265
-        else
1ab265
-          data[y * 100 + x] = 0xff00ffff;
1ab265
-      }
1ab265
-  return cogl_texture_new_from_data (100,
1ab265
-                                     100,
1ab265
-                                     COGL_TEXTURE_NONE,
1ab265
-                                     COGL_PIXEL_FORMAT_ARGB_8888,
1ab265
-                                     COGL_PIXEL_FORMAT_ARGB_8888,
1ab265
-                                     400,
1ab265
-                                     (guchar *)data);
1ab265
-}
1ab265
-
1ab265
-static void
1ab265
-texture_pick_with_alpha (void)
1ab265
-{
1ab265
-  ClutterTexture *tex = CLUTTER_TEXTURE (clutter_texture_new ());
1ab265
-  ClutterStage *stage = CLUTTER_STAGE (clutter_test_get_stage ());
1ab265
-  ClutterActor *actor;
1ab265
-
1ab265
-  clutter_texture_set_cogl_texture (tex, make_texture ());
1ab265
-
1ab265
-  clutter_actor_add_child (CLUTTER_ACTOR (stage), CLUTTER_ACTOR (tex));
1ab265
-
1ab265
-  clutter_actor_show (CLUTTER_ACTOR (stage));
1ab265
-
1ab265
-  if (g_test_verbose ())
1ab265
-    {
1ab265
-      g_print ("\nstage = %p\n", stage);
1ab265
-      g_print ("texture = %p\n\n", tex);
1ab265
-    }
1ab265
-
1ab265
-  clutter_texture_set_pick_with_alpha (tex, TRUE);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("Testing with pick-with-alpha enabled:\n");
1ab265
-
1ab265
-  /* This should fall through and hit the stage: */
1ab265
-  actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, 10, 10);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("actor @ (10, 10) = %p\n", actor);
1ab265
-  g_assert (actor == CLUTTER_ACTOR (stage));
1ab265
-
1ab265
-  /* The rest should hit the texture */
1ab265
-  actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, 90, 10);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("actor @ (90, 10) = %p\n", actor);
1ab265
-  g_assert (actor == CLUTTER_ACTOR (tex));
1ab265
-  actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, 90, 90);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("actor @ (90, 90) = %p\n", actor);
1ab265
-  g_assert (actor == CLUTTER_ACTOR (tex));
1ab265
-  actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, 10, 90);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("actor @ (10, 90) = %p\n", actor);
1ab265
-  g_assert (actor == CLUTTER_ACTOR (tex));
1ab265
-
1ab265
-  clutter_texture_set_pick_with_alpha (tex, FALSE);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("Testing with pick-with-alpha disabled:\n");
1ab265
-
1ab265
-  actor = clutter_stage_get_actor_at_pos (stage, CLUTTER_PICK_ALL, 10, 10);
1ab265
-  if (g_test_verbose ())
1ab265
-    g_print ("actor @ (10, 10) = %p\n", actor);
1ab265
-  g_assert (actor == CLUTTER_ACTOR (tex));
1ab265
-}
1ab265
-
1ab265
-CLUTTER_TEST_SUITE (
1ab265
-  CLUTTER_TEST_UNIT ("/texture/pick-with-alpha", texture_pick_with_alpha)
1ab265
-)
1ab265
diff --git a/src/compositor/meta-surface-actor.c b/src/compositor/meta-surface-actor.c
1ab265
index ca4ca19a99..814199145a 100644
1ab265
--- a/src/compositor/meta-surface-actor.c
1ab265
+++ b/src/compositor/meta-surface-actor.c
1ab265
@@ -70,38 +70,23 @@ meta_surface_actor_pick (ClutterActor       *actor,
1ab265
   else
1ab265
     {
1ab265
       int n_rects;
1ab265
-      float *rectangles;
1ab265
       int i;
1ab265
-      CoglPipeline *pipeline;
1ab265
-      CoglContext *ctx;
1ab265
-      CoglFramebuffer *fb;
1ab265
-      CoglColor cogl_color;
1ab265
 
1ab265
       n_rects = cairo_region_num_rectangles (priv->input_region);
1ab265
-      rectangles = g_alloca (sizeof (float) * 4 * n_rects);
1ab265
 
1ab265
       for (i = 0; i < n_rects; i++)
1ab265
         {
1ab265
           cairo_rectangle_int_t rect;
1ab265
-          int pos = i * 4;
1ab265
+          ClutterActorBox box;
1ab265
 
1ab265
           cairo_region_get_rectangle (priv->input_region, i, &rect);
1ab265
 
1ab265
-          rectangles[pos + 0] = rect.x;
1ab265
-          rectangles[pos + 1] = rect.y;
1ab265
-          rectangles[pos + 2] = rect.x + rect.width;
1ab265
-          rectangles[pos + 3] = rect.y + rect.height;
1ab265
+          box.x1 = rect.x;
1ab265
+          box.y1 = rect.y;
1ab265
+          box.x2 = rect.x + rect.width;
1ab265
+          box.y2 = rect.y + rect.height;
1ab265
+          clutter_actor_pick_box (actor, &box);
1ab265
         }
1ab265
-
1ab265
-      ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
1ab265
-      fb = cogl_get_draw_framebuffer ();
1ab265
-
1ab265
-      cogl_color_init_from_4ub (&cogl_color, color->red, color->green, color->blue, color->alpha);
1ab265
-
1ab265
-      pipeline = cogl_pipeline_new (ctx);
1ab265
-      cogl_pipeline_set_color (pipeline, &cogl_color);
1ab265
-      cogl_framebuffer_draw_rectangles (fb, pipeline, rectangles, n_rects);
1ab265
-      cogl_object_unref (pipeline);
1ab265
     }
1ab265
 
1ab265
   clutter_actor_iter_init (&iter, actor);
1ab265
-- 
1ab265
2.29.2
1ab265
1ab265
1ab265
From 254e93de8d60393ca94fa430c0acc6f6a7b9516e Mon Sep 17 00:00:00 2001
1ab265
From: Iain Lane <laney@debian.org>
1ab265
Date: Mon, 9 Sep 2019 10:17:22 +0100
1ab265
Subject: [PATCH 3/4] build: Compile with `-ffloat-store` on x86 (32 bit)
1ab265
1ab265
GCC's manpage says that this flag does the following:
1ab265
1ab265
  Do not store floating-point variables in registers, and inhibit other
1ab265
  options that might change whether a floating-point value is taken from
1ab265
  a register or memory.
1ab265
1ab265
  This option prevents undesirable excess precision on machines such as
1ab265
  the 68000 where the floating registers (of the 68881) keep more
1ab265
  precision than a "double" is supposed to have.  Similarly for the x86
1ab265
  architecture.  For most programs, the excess precision does only good,
1ab265
  but a few programs rely on the precise definition of IEEE floating
1ab265
  point.
1ab265
1ab265
We rely on this behaviour in our fork of clutter. When performing
1ab265
floating point computations on x86, we are getting the wrong results
1ab265
because of this architecture's use of the CPU's extended (x87, non-IEEE
1ab265
confirming) precision by default. If we enable `-ffloat-store` here,
1ab265
then we'll get the same results everywhere by storing into variables
1ab265
instead of registers. This does not remove the need to be correct when
1ab265
handling floats, but it does mean we don't need to be more correct than
1ab265
the IEEE spec requires.
1ab265
1ab265
https://gitlab.gnome.org/GNOME/mutter/merge_requests/785
1ab265
---
1ab265
 meson.build | 3 +++
1ab265
 1 file changed, 3 insertions(+)
1ab265
1ab265
diff --git a/meson.build b/meson.build
1ab265
index 8ef592bc58..e1edb78ba7 100644
1ab265
--- a/meson.build
1ab265
+++ b/meson.build
1ab265
@@ -267,6 +267,9 @@ foreach function : required_functions
1ab265
   endif
1ab265
 endforeach
1ab265
 
1ab265
+if host_machine.cpu_family() == 'x86'
1ab265
+  add_project_arguments('-ffloat-store', language: 'c')
1ab265
+endif
1ab265
 add_project_arguments('-D_GNU_SOURCE', language: 'c')
1ab265
 
1ab265
 all_warnings = [
1ab265
-- 
1ab265
2.29.2
1ab265
1ab265
1ab265
From 2d42caef14772984344e62ce40957d3b40e1f2b6 Mon Sep 17 00:00:00 2001
1ab265
From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= <mail@3v1n0.net>
1ab265
Date: Thu, 19 Sep 2019 11:27:50 +0200
1ab265
Subject: [PATCH 4/4] stage: Compute view perspective when parameters changed
1ab265
1ab265
Clutter stage used to compute the initial projection using a fixed z
1ab265
translation which wasn't matching the one we computed in
1ab265
calculate_z_translation().
1ab265
This caused to have a wrong initial projection on startup which was then
1ab265
correctly recomputed only at the first paint.
1ab265
1ab265
However, since this calculation doesn't depend on view, but only on viewport
1ab265
size, perspective's fovy and z_near we can safely do this at startup and
1ab265
only when any of those parameters change.
1ab265
1ab265
Then we can move the computation out _clutter_stage_maybe_setup_viewport()
1ab265
since the cogl framebuffer viewport sizes aren't affecting this.
1ab265
1ab265
Fixes https://gitlab.gnome.org/GNOME/gnome-shell/issues/1639
1ab265
https://gitlab.gnome.org/GNOME/mutter/merge_requests/803
1ab265
---
1ab265
 clutter/clutter/clutter-stage.c | 104 +++++++++++++++-----------------
1ab265
 1 file changed, 47 insertions(+), 57 deletions(-)
1ab265
1ab265
diff --git a/clutter/clutter/clutter-stage.c b/clutter/clutter/clutter-stage.c
1ab265
index 7d88d5752f..0cfa87486e 100644
1ab265
--- a/clutter/clutter/clutter-stage.c
1ab265
+++ b/clutter/clutter/clutter-stage.c
1ab265
@@ -234,6 +234,7 @@ static void capture_view_into (ClutterStage          *stage,
1ab265
                                cairo_rectangle_int_t *rect,
1ab265
                                uint8_t               *data,
1ab265
                                int                    stride);
1ab265
+static void clutter_stage_update_view_perspective (ClutterStage *stage);
1ab265
 
1ab265
 static void clutter_container_iface_init (ClutterContainerIface *iface);
1ab265
 
1ab265
@@ -2492,29 +2493,6 @@ clutter_stage_init (ClutterStage *self)
1ab265
   clutter_actor_set_background_color (CLUTTER_ACTOR (self),
1ab265
                                       &default_stage_color);
1ab265
 
1ab265
-  priv->perspective.fovy   = 60.0; /* 60 Degrees */
1ab265
-  priv->perspective.aspect = (float) geom.width / (float) geom.height;
1ab265
-  priv->perspective.z_near = 0.1;
1ab265
-  priv->perspective.z_far  = 100.0;
1ab265
-
1ab265
-  cogl_matrix_init_identity (&priv->projection);
1ab265
-  cogl_matrix_perspective (&priv->projection,
1ab265
-                           priv->perspective.fovy,
1ab265
-                           priv->perspective.aspect,
1ab265
-                           priv->perspective.z_near,
1ab265
-                           priv->perspective.z_far);
1ab265
-  cogl_matrix_get_inverse (&priv->projection,
1ab265
-                           &priv->inverse_projection);
1ab265
-  cogl_matrix_init_identity (&priv->view);
1ab265
-  cogl_matrix_view_2d_in_perspective (&priv->view,
1ab265
-                                      priv->perspective.fovy,
1ab265
-                                      priv->perspective.aspect,
1ab265
-                                      priv->perspective.z_near,
1ab265
-                                      50, /* distance to 2d plane */
1ab265
-                                      geom.width,
1ab265
-                                      geom.height);
1ab265
-
1ab265
-
1ab265
   /* FIXME - remove for 2.0 */
1ab265
   priv->fog.z_near = 1.0;
1ab265
   priv->fog.z_far  = 2.0;
1ab265
@@ -2682,6 +2660,7 @@ clutter_stage_set_perspective (ClutterStage       *stage,
1ab265
   priv->has_custom_perspective = TRUE;
1ab265
 
1ab265
   clutter_stage_set_perspective_internal (stage, perspective);
1ab265
+  clutter_stage_update_view_perspective (stage);
1ab265
 }
1ab265
 
1ab265
 /**
1ab265
@@ -2808,6 +2787,7 @@ _clutter_stage_set_viewport (ClutterStage *stage,
1ab265
   priv->viewport[2] = width;
1ab265
   priv->viewport[3] = height;
1ab265
 
1ab265
+  clutter_stage_update_view_perspective (stage);
1ab265
   _clutter_stage_dirty_viewport (stage);
1ab265
 
1ab265
   queue_full_redraw (stage);
1ab265
@@ -3788,6 +3768,50 @@ calculate_z_translation (float z_near)
1ab265
        + z_near;
1ab265
 }
1ab265
 
1ab265
+static void
1ab265
+clutter_stage_update_view_perspective (ClutterStage *stage)
1ab265
+{
1ab265
+  ClutterStagePrivate *priv = stage->priv;
1ab265
+  ClutterPerspective perspective;
1ab265
+  float z_2d;
1ab265
+
1ab265
+  perspective = priv->perspective;
1ab265
+
1ab265
+  /* Ideally we want to regenerate the perspective matrix whenever
1ab265
+   * the size changes but if the user has provided a custom matrix
1ab265
+   * then we don't want to override it */
1ab265
+  if (!priv->has_custom_perspective)
1ab265
+    {
1ab265
+      perspective.fovy = 60.0; /* 60 Degrees */
1ab265
+      perspective.z_near = 0.1;
1ab265
+      perspective.aspect = priv->viewport[2] / priv->viewport[3];
1ab265
+      z_2d = calculate_z_translation (perspective.z_near);
1ab265
+
1ab265
+      /* NB: z_2d is only enough room for 85% of the stage_height between
1ab265
+       * the stage and the z_near plane. For behind the stage plane we
1ab265
+       * want a more consistent gap of 10 times the stage_height before
1ab265
+       * hitting the far plane so we calculate that relative to the final
1ab265
+       * height of the stage plane at the z_2d_distance we got... */
1ab265
+      perspective.z_far = z_2d +
1ab265
+        tanf (_DEG_TO_RAD (perspective.fovy / 2.0f)) * z_2d * 20.0f;
1ab265
+
1ab265
+      clutter_stage_set_perspective_internal (stage, &perspective);
1ab265
+    }
1ab265
+  else
1ab265
+    {
1ab265
+      z_2d = calculate_z_translation (perspective.z_near);
1ab265
+    }
1ab265
+
1ab265
+  cogl_matrix_init_identity (&priv->view);
1ab265
+  cogl_matrix_view_2d_in_perspective (&priv->view,
1ab265
+                                      perspective.fovy,
1ab265
+                                      perspective.aspect,
1ab265
+                                      perspective.z_near,
1ab265
+                                      z_2d,
1ab265
+                                      priv->viewport[2],
1ab265
+                                      priv->viewport[3]);
1ab265
+}
1ab265
+
1ab265
 void
1ab265
 _clutter_stage_maybe_setup_viewport (ClutterStage     *stage,
1ab265
                                      ClutterStageView *view)
1ab265
@@ -3797,7 +3821,6 @@ _clutter_stage_maybe_setup_viewport (ClutterStage     *stage,
1ab265
   if (clutter_stage_view_is_dirty_viewport (view))
1ab265
     {
1ab265
       cairo_rectangle_int_t view_layout;
1ab265
-      ClutterPerspective perspective;
1ab265
       float fb_scale;
1ab265
       float viewport_offset_x;
1ab265
       float viewport_offset_y;
1ab265
@@ -3805,7 +3828,6 @@ _clutter_stage_maybe_setup_viewport (ClutterStage     *stage,
1ab265
       float viewport_y;
1ab265
       float viewport_width;
1ab265
       float viewport_height;
1ab265
-      float z_2d;
1ab265
 
1ab265
       CLUTTER_NOTE (PAINT,
1ab265
                     "Setting up the viewport { w:%f, h:%f }",
1ab265
@@ -3825,38 +3847,6 @@ _clutter_stage_maybe_setup_viewport (ClutterStage     *stage,
1ab265
       clutter_stage_view_set_viewport (view,
1ab265
                                        viewport_x, viewport_y,
1ab265
                                        viewport_width, viewport_height);
1ab265
-
1ab265
-      perspective = priv->perspective;
1ab265
-
1ab265
-      /* Ideally we want to regenerate the perspective matrix whenever
1ab265
-       * the size changes but if the user has provided a custom matrix
1ab265
-       * then we don't want to override it */
1ab265
-      if (!priv->has_custom_perspective)
1ab265
-        {
1ab265
-          perspective.aspect = priv->viewport[2] / priv->viewport[3];
1ab265
-          z_2d = calculate_z_translation (perspective.z_near);
1ab265
-
1ab265
-          /* NB: z_2d is only enough room for 85% of the stage_height between
1ab265
-           * the stage and the z_near plane. For behind the stage plane we
1ab265
-           * want a more consistent gap of 10 times the stage_height before
1ab265
-           * hitting the far plane so we calculate that relative to the final
1ab265
-           * height of the stage plane at the z_2d_distance we got... */
1ab265
-          perspective.z_far = z_2d +
1ab265
-            tanf (_DEG_TO_RAD (perspective.fovy / 2.0f)) * z_2d * 20.0f;
1ab265
-
1ab265
-          clutter_stage_set_perspective_internal (stage, &perspective);
1ab265
-        }
1ab265
-      else
1ab265
-        z_2d = calculate_z_translation (perspective.z_near);
1ab265
-
1ab265
-      cogl_matrix_init_identity (&priv->view);
1ab265
-      cogl_matrix_view_2d_in_perspective (&priv->view,
1ab265
-                                          perspective.fovy,
1ab265
-                                          perspective.aspect,
1ab265
-                                          perspective.z_near,
1ab265
-                                          z_2d,
1ab265
-                                          priv->viewport[2],
1ab265
-                                          priv->viewport[3]);
1ab265
     }
1ab265
 
1ab265
   if (clutter_stage_view_is_dirty_projection (view))
1ab265
-- 
1ab265
2.29.2
1ab265