Skip to content

Commit

Permalink
ShadowEffect: Delay cache drop for switching shadow sizes performance…
Browse files Browse the repository at this point in the history
… improvements (#2061)

Co-authored-by: lenemter <[email protected]>
  • Loading branch information
leolost2605 and lenemter authored Nov 23, 2024
1 parent b9dd5cc commit 74abd1e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 20 deletions.
12 changes: 12 additions & 0 deletions data/gala.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@
<update_contact>contact_at_elementary.io</update_contact>

<releases>
<release version="8.0.4" date="2024-11-23" urgency="medium">
<description>
<p>Improvements:</p>
<ul>
<li>Updated translations</li>
<li>Improved shadows performance</li>
</ul>
</description>
<issues>
</issues>
</release>

<release version="8.0.3" date="2024-11-20" urgency="medium">
<description>
<p>Improvements:</p>
Expand Down
66 changes: 46 additions & 20 deletions lib/ShadowEffect.vala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ public class Gala.ShadowEffect : Clutter.Effect {

// the sizes of the textures often repeat, especially for the background actor
// so we keep a cache to avoid creating the same texture all over again.
private static Gee.HashMap<string,Shadow> shadow_cache;
// Delay the style context creation at render stage as Gtk need to access
// the current display.
private static Gee.HashMap<string, Shadow> shadow_cache;

// Sometimes we use a shadow in only one place and rapidly switch between two shadows
// In order to not drop them and create them all over again we wait 5 seconds before finally dropping a shadow.
private static Gee.HashMap<string, uint> shadows_marked_for_dropping;

static construct {
shadow_cache = new Gee.HashMap<string,Shadow> ();
shadow_cache = new Gee.HashMap<string, Shadow> ();
shadows_marked_for_dropping = new Gee.HashMap<string, uint> ();
}

private string _css_class;
Expand Down Expand Up @@ -80,9 +83,9 @@ public class Gala.ShadowEffect : Clutter.Effect {
decrement_shadow_users (old_key);
}

Shadow? shadow = null;
if ((shadow = shadow_cache.@get (current_key)) != null) {
shadow.users++;
var shadow = shadow_cache.@get (current_key);
if (shadow != null) {
increment_shadow_users (current_key);
return shadow.texture;
}

Expand Down Expand Up @@ -126,18 +129,6 @@ public class Gala.ShadowEffect : Clutter.Effect {
}
}

private void decrement_shadow_users (string key) {
var shadow = shadow_cache.@get (key);

if (shadow == null) {
return;
}

if (--shadow.users == 0) {
shadow_cache.unset (key);
}
}

public override void paint (Clutter.PaintNode node, Clutter.PaintContext context, Clutter.EffectPaintFlags flags) {
var bounding_box = get_bounding_box ();
var width = (int) (bounding_box.x2 - bounding_box.x1);
Expand All @@ -159,7 +150,7 @@ public class Gala.ShadowEffect : Clutter.Effect {
actor.continue_paint (context);
}

public virtual Clutter.ActorBox get_bounding_box () {
private Clutter.ActorBox get_bounding_box () {
var size = shadow_size * scale_factor;
var bounding_box = Clutter.ActorBox ();

Expand All @@ -184,4 +175,39 @@ public class Gala.ShadowEffect : Clutter.Effect {

return true;
}

private static void increment_shadow_users (string key) {
var shadow = shadow_cache.@get (key);

if (shadow == null) {
return;
}

shadow.users++;

uint timeout_id;
if (shadows_marked_for_dropping.unset (key, out timeout_id)) {
Source.remove (timeout_id);
}
}

private static void decrement_shadow_users (string key) {
var shadow = shadow_cache.@get (key);

if (shadow == null) {
return;
}

if (--shadow.users == 0) {
queue_shadow_drop (key);
}
}

private static void queue_shadow_drop (string key) {
shadows_marked_for_dropping[key] = Timeout.add_seconds (5, () => {
shadow_cache.unset (key);
shadows_marked_for_dropping.unset (key);
return Source.REMOVE;
});
}
}

0 comments on commit 74abd1e

Please sign in to comment.