[mutter] clutter/actor: Use stage-views list for resource scales



commit 4a72ac05160b2af22bcfce40b716c9c30c534906
Author: Jonas Dreßler <verdre v0yd nl>
Date:   Fri Apr 10 01:53:38 2020 +0200

    clutter/actor: Use stage-views list for resource scales
    
    Now that ClutterActor has a convenient API for getting the stage views
    an actor is presented on, we can remove a large part of the code for
    resource-scale calculation and instead rely on the stage-views list.
    
    The way this works is a bit different from the old resource scales:
    clutter_actor_get_resource_scale() always returns a scale, but this
    value is only guaranteed to be correct when called from a vfunc_paint()
    implementation, in all other cases the value is guessed using the scale
    of the parent actor or the last valid scale. Now in case the value
    previously reported by clutter_actor_get_resource_scale() turns out to
    be wrong, "resource-scale-changed" will be emitted before the next paint
    and the actor has a chance to update its resources.
    
    The general idea behind this new implementation is for actors which only
    need the scale during painting to continue using
    clutter_actor_get_resource_scale() as they do right now, and for actors
    which need the resource scale on other occasions, like during size
    negotiation, to use the scale reported by
    clutter_actor_get_resource_scale() but also listen to the
    "resource-scale-changed" signal to eventually redo the work using the
    correct scale.
    
    The "guessing" of the scale is done with the intention of always giving
    actors a scale to work with so they don't have to fall back to a scale
    value the actor itself has to define, and also with the intention of
    emitting the "resource-scale-changed" signal as rarely as possible, so
    that when an actor is newly created, it won't have to load its resources
    multiple times.
    
    The big advantage this has over the old resource scales is that it's now
    safe to call clutter_actor_get_resource_scale() from everywhere (before,
    calling it from size negotiation functions would usually fail). It will
    also make it a lot easier to use the resource scale for complex cases
    like ClutterText without risking to get into relayout loops.
    
    https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1276

 clutter/clutter/clutter-actor.c | 264 ++++++++++++++++------------------------
 1 file changed, 103 insertions(+), 161 deletions(-)
---
diff --git a/clutter/clutter/clutter-actor.c b/clutter/clutter/clutter-actor.c
index 591b9db771..a31b48fa13 100644
--- a/clutter/clutter/clutter-actor.c
+++ b/clutter/clutter/clutter-actor.c
@@ -854,7 +854,6 @@ struct _ClutterActorPrivate
   guint needs_y_expand              : 1;
   guint needs_paint_volume_update   : 1;
   guint had_effects_on_last_paint_volume_update : 1;
-  guint needs_compute_resource_scale : 1;
   guint absolute_origin_changed     : 1;
   guint needs_update_stage_views    : 1;
 };
@@ -1075,8 +1074,6 @@ static void clutter_actor_set_child_transform_internal (ClutterActor        *sel
 
 static void     clutter_actor_realize_internal          (ClutterActor *self);
 static void     clutter_actor_unrealize_internal        (ClutterActor *self);
-static gboolean clutter_actor_update_resource_scale     (ClutterActor *self);
-static void     clutter_actor_ensure_resource_scale     (ClutterActor *self);
 
 static void clutter_actor_push_in_cloned_branch (ClutterActor *self,
                                                  gulong        count);
@@ -1618,8 +1615,6 @@ clutter_actor_real_map (ClutterActor *self)
       queue_update_stage_views (self);
     }
 
-  clutter_actor_ensure_resource_scale (self);
-
   /* notify on parent mapped before potentially mapping
    * children, so apps see a top-down notification.
    */
@@ -2530,7 +2525,6 @@ clutter_actor_notify_if_geometry_changed (ClutterActor          *self,
 static void
 absolute_allocation_changed (ClutterActor *actor)
 {
-  actor->priv->needs_compute_resource_scale = TRUE;
   queue_update_stage_views (actor);
 }
 
@@ -3731,8 +3725,6 @@ clutter_actor_paint (ClutterActor        *self,
   if (!CLUTTER_ACTOR_IS_MAPPED (self))
     return;
 
-  clutter_actor_ensure_resource_scale (self);
-
   actor_node = clutter_actor_node_new (self);
   root_node = clutter_paint_node_ref (actor_node);
 
@@ -4014,8 +4006,6 @@ clutter_actor_pick (ClutterActor       *actor,
   if (!CLUTTER_ACTOR_IS_MAPPED (actor))
     return;
 
-  clutter_actor_ensure_resource_scale (actor);
-
   /* mark that we are in the paint process */
   CLUTTER_SET_PRIVATE_FLAGS (actor, CLUTTER_IN_PICK);
 
@@ -4342,10 +4332,7 @@ clutter_actor_remove_child_internal (ClutterActor                 *self,
     clutter_actor_clear_stage_views_recursive (child);
 
   if (emit_parent_set && !CLUTTER_ACTOR_IN_DESTRUCTION (child))
-    {
-      child->priv->needs_compute_resource_scale = TRUE;
-      g_signal_emit (child, actor_signals[PARENT_SET], 0, self);
-    }
+    g_signal_emit (child, actor_signals[PARENT_SET], 0, self);
 
   /* if the child was mapped then we need to relayout ourselves to account
    * for the removed child
@@ -7994,7 +7981,6 @@ clutter_actor_init (ClutterActor *self)
   priv->needs_height_request = TRUE;
   priv->needs_allocation = TRUE;
   priv->needs_paint_volume_update = TRUE;
-  priv->needs_compute_resource_scale = TRUE;
   priv->needs_update_stage_views = TRUE;
 
   priv->cached_width_age = 1;
@@ -11895,10 +11881,7 @@ clutter_actor_add_child_internal (ClutterActor              *self,
     }
 
   if (emit_parent_set)
-    {
-      child->priv->needs_compute_resource_scale = TRUE;
-      g_signal_emit (child, actor_signals[PARENT_SET], 0, NULL);
-    }
+    g_signal_emit (child, actor_signals[PARENT_SET], 0, NULL);
 
   if (check_state)
     {
@@ -15958,107 +15941,6 @@ clutter_actor_get_paint_box (ClutterActor    *self,
   return TRUE;
 }
 
-static gboolean
-_clutter_actor_get_resource_scale_for_rect (ClutterActor    *self,
-                                            graphene_rect_t *bounding_rect,
-                                            float           *resource_scale)
-{
-  ClutterActor *stage;
-  g_autoptr (GList) views = NULL;
-  GList *l;
-  float max_scale = 0;
-
-  stage = _clutter_actor_get_stage_internal (self);
-  if (!stage)
-    return FALSE;
-
-  views = clutter_stage_get_views_for_rect (CLUTTER_STAGE (stage),
-                                            bounding_rect);
-
-  if (!views)
-    return FALSE;
-
-  for (l = views; l; l = l->next)
-    {
-      ClutterStageView *view = l->data;
-
-      max_scale = MAX (clutter_stage_view_get_scale (view), max_scale);
-    }
-
-  *resource_scale = max_scale;
-
-  return TRUE;
-}
-
-static gboolean
-_clutter_actor_compute_resource_scale (ClutterActor *self,
-                                       float        *resource_scale)
-{
-  graphene_rect_t bounding_rect;
-  ClutterActorPrivate *priv = self->priv;
-
-  if (CLUTTER_ACTOR_IN_DESTRUCTION (self) ||
-      CLUTTER_ACTOR_IN_PREF_SIZE (self) ||
-      !clutter_actor_is_mapped (self))
-    {
-      return FALSE;
-    }
-
-  clutter_actor_get_transformed_position (self,
-                                          &bounding_rect.origin.x,
-                                          &bounding_rect.origin.y);
-  clutter_actor_get_transformed_size (self,
-                                      &bounding_rect.size.width,
-                                      &bounding_rect.size.height);
-
-  if (bounding_rect.size.width == 0.0 ||
-      bounding_rect.size.height == 0.0 ||
-      !_clutter_actor_get_resource_scale_for_rect (self,
-                                                   &bounding_rect,
-                                                   resource_scale))
-    {
-      if (priv->parent)
-        {
-          gboolean in_clone_paint;
-          gboolean was_parent_in_clone_paint;
-          gboolean was_parent_unmapped;
-          gboolean was_parent_paint_unmapped;
-          gboolean ret;
-
-          in_clone_paint = clutter_actor_is_in_clone_paint (self);
-          was_parent_unmapped = !clutter_actor_is_mapped (priv->parent);
-          was_parent_in_clone_paint =
-            clutter_actor_is_in_clone_paint (priv->parent);
-          was_parent_paint_unmapped = priv->parent->priv->enable_paint_unmapped;
-
-          if (in_clone_paint && was_parent_unmapped)
-            {
-              _clutter_actor_set_in_clone_paint (priv->parent, TRUE);
-              _clutter_actor_set_enable_paint_unmapped (priv->parent, TRUE);
-            }
-
-          ret = _clutter_actor_compute_resource_scale (priv->parent,
-                                                       resource_scale);
-
-          if (in_clone_paint && was_parent_unmapped)
-            {
-              _clutter_actor_set_in_clone_paint (priv->parent,
-                                                 was_parent_in_clone_paint);
-              _clutter_actor_set_enable_paint_unmapped (priv->parent,
-                                                        was_parent_paint_unmapped);
-            }
-
-          return ret;
-        }
-      else
-        {
-          return FALSE;
-        }
-    }
-
-  return TRUE;
-}
-
 static ClutterActorTraverseVisitFlags
 clear_stage_views_cb (ClutterActor *actor,
                       int           depth,
@@ -16067,7 +15949,6 @@ clear_stage_views_cb (ClutterActor *actor,
   g_autoptr (GList) old_stage_views = NULL;
 
   actor->priv->needs_update_stage_views = TRUE;
-  actor->priv->needs_compute_resource_scale = TRUE;
 
   old_stage_views = g_steal_pointer (&actor->priv->stage_views);
 
@@ -16087,58 +15968,63 @@ clutter_actor_clear_stage_views_recursive (ClutterActor *self)
                            NULL);
 }
 
-static gboolean
-clutter_actor_update_resource_scale (ClutterActor *self)
+gboolean
+_clutter_actor_get_real_resource_scale (ClutterActor *self,
+                                        gfloat       *resource_scale)
 {
-  ClutterActorPrivate *priv;
-  float resource_scale;
-  float old_resource_scale;
-  priv = self->priv;
+  ClutterActorPrivate *priv = self->priv;
+  float guessed_scale;
 
-  g_return_val_if_fail (priv->needs_compute_resource_scale, FALSE);
+  if (priv->resource_scale != -1.f)
+    {
+      *resource_scale = priv->resource_scale;
+      return TRUE;
+    }
 
-  old_resource_scale = priv->resource_scale;
-  priv->resource_scale = -1.0f;
+  /* If the scale hasn't been computed yet, we return a best guess */
 
-  if (_clutter_actor_compute_resource_scale (self, &resource_scale))
+  if (priv->parent != NULL)
     {
-      priv->resource_scale = resource_scale;
-      priv->needs_compute_resource_scale = FALSE;
-
-      return fabsf (old_resource_scale - resource_scale) > FLT_EPSILON;
+      /* If the scale hasn't been calculated yet, assume this actor is located
+       * inside its parents box and go up the hierarchy.
+       */
+      _clutter_actor_get_real_resource_scale (priv->parent, &guessed_scale);
     }
+  else if (CLUTTER_ACTOR_IS_TOPLEVEL (self))
+    {
+      /* This must be the first allocation cycle and the resource scale of
+       * the stage has not been updated yet, so return it manually.
+       */
+      GList *l;
+      ClutterStage *stage = CLUTTER_STAGE (self);
+      float max_scale = -1.f;
 
-  return FALSE;
-}
-
-static void
-clutter_actor_ensure_resource_scale (ClutterActor *self)
-{
-  ClutterActorPrivate *priv = self->priv;
+      for (l = clutter_stage_peek_stage_views (stage); l; l = l->next)
+        {
+          ClutterStageView *view = l->data;
 
-  if (!priv->needs_compute_resource_scale)
-    return;
+          max_scale = MAX (clutter_stage_view_get_scale (view), max_scale);
+        }
 
-  if (clutter_actor_update_resource_scale (self))
-    g_signal_emit (self, actor_signals[RESOURCE_SCALE_CHANGED], 0);
-}
+      guessed_scale = max_scale;
+    }
+  else
+    {
+      ClutterBackend *backend = clutter_get_default_backend ();
 
-gboolean
-_clutter_actor_get_real_resource_scale (ClutterActor *self,
-                                        gfloat       *resource_scale)
-{
-  ClutterActorPrivate *priv = self->priv;
+      guessed_scale = clutter_backend_get_fallback_resource_scale (backend);
+    }
 
-  clutter_actor_ensure_resource_scale (self);
+  g_assert (guessed_scale >= 1.f);
 
-  if (!priv->needs_compute_resource_scale)
-    {
-      *resource_scale = priv->resource_scale;
-      return TRUE;
-    }
+  /* Always return this value until we compute the correct one later.
+   * If our guess turns out to be wrong, we'll emit "resource-scale-changed"
+   * and correct it before painting.
+   */
+  priv->resource_scale = guessed_scale;
 
-  *resource_scale = -1.0f;
-  return FALSE;
+  *resource_scale = priv->resource_scale;
+  return TRUE;
 }
 
 /**
@@ -16146,7 +16032,7 @@ _clutter_actor_get_real_resource_scale (ClutterActor *self,
  * @self: A #ClutterActor
  * @resource_scale: (out): return location for the resource scale
  *
- * Retrieves the resource scale for this actor, if available.
+ * Retrieves the resource scale for this actor.
  *
  * The resource scale refers to the scale the actor should use for its resources.
  * For example if an actor draws a a picture of size 100 x 100 in the stage
@@ -16156,6 +16042,24 @@ _clutter_actor_get_real_resource_scale (ClutterActor *self,
  * The resource scale is determined by calculating the highest #ClutterStageView
  * scale the actor will get painted on.
  *
+ * Note that the scale returned by this function is only guaranteed to be
+ * correct when queried during the paint cycle, in all other cases this
+ * function will only return a best guess. If your implementation really
+ * needs to get a resource scale outside of the paint cycle, make sure to
+ * subscribe to the "resource-scale-changed" signal to get notified about
+ * the new, correct resource scale before painting.
+ *
+ * Also avoid getting the resource scale for actors that are not attached
+ * to a stage. There's no sane way for Clutter to guess which #ClutterStageView
+ * the actor is going to be painted on, so you'll probably end up receiving
+ * the "resource-scale-changed" signal and having to rebuild your resources.
+ *
+ * The best guess this function may return is usually just the last resource
+ * scale the actor got painted with. If this resource scale couldn't be found
+ * because the actor was never painted so far or Clutter was unable to
+ * determine its position and size, this function will return the resource
+ * scale of a parent.
+ *
  * Returns: TRUE if resource scale is set for the actor, otherwise FALSE
  */
 gboolean
@@ -16240,6 +16144,43 @@ out:
     }
 }
 
+static void
+update_resource_scale (ClutterActor *self)
+{
+  ClutterActorPrivate *priv = self->priv;
+  GList *l;
+  float new_resource_scale = -1.f;
+  float old_resource_scale;
+
+  for (l = priv->stage_views; l; l = l->next)
+    {
+      ClutterStageView *view = l->data;
+
+      new_resource_scale = MAX (clutter_stage_view_get_scale (view),
+                                new_resource_scale);
+    }
+
+  if (priv->resource_scale == new_resource_scale)
+    return;
+
+  /* If the actor moved out of the stage, simply keep the last scale */
+  if (new_resource_scale == -1.f)
+    return;
+
+  old_resource_scale = priv->resource_scale;
+  priv->resource_scale = new_resource_scale;
+
+  /* Never notify the initial change, otherwise, to be consistent,
+   * we'd also have to notify if we guessed correctly in
+   * _clutter_actor_get_real_resource_scale().
+   */
+  if (old_resource_scale == -1.f)
+    return;
+
+  if (ceilf (old_resource_scale) != ceilf (priv->resource_scale))
+    g_signal_emit (self, actor_signals[RESOURCE_SCALE_CHANGED], 0);
+}
+
 void
 clutter_actor_update_stage_views (ClutterActor *self)
 {
@@ -16254,6 +16195,7 @@ clutter_actor_update_stage_views (ClutterActor *self)
     return;
 
   update_stage_views (self);
+  update_resource_scale (self);
 
   priv->needs_update_stage_views = FALSE;
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]