2017-07-08 09:30:31 +03:00
|
|
|
//
|
|
|
|
// Copyright (C) 2014 Tom Beckmann
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
using Clutter;
|
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
namespace Gala.Plugins.PIP {
|
|
|
|
public class ShadowEffect : Effect {
|
|
|
|
private class Shadow {
|
2020-01-09 22:26:30 +03:00
|
|
|
public int users;
|
|
|
|
public Cogl.Texture texture;
|
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
public Shadow (Cogl.Texture _texture) {
|
2020-01-09 22:26:30 +03:00
|
|
|
texture = _texture;
|
|
|
|
users = 1;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
// 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.
|
|
|
|
static Gee.HashMap<string,Shadow> shadow_cache;
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
static construct {
|
2020-01-09 22:26:30 +03:00
|
|
|
shadow_cache = new Gee.HashMap<string,Shadow> ();
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
public int shadow_size { get; construct; }
|
|
|
|
public int shadow_spread { get; construct; }
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
public float scale_factor { get; set; default = 1; }
|
|
|
|
public uint8 shadow_opacity { get; set; default = 255; }
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-03-13 01:38:28 +03:00
|
|
|
Cogl.Pipeline pipeline;
|
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
string? current_key = null;
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
public ShadowEffect (int shadow_size, int shadow_spread) {
|
2020-01-09 22:26:30 +03:00
|
|
|
Object (shadow_size: shadow_size, shadow_spread: shadow_spread);
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
construct {
|
2020-03-13 01:38:28 +03:00
|
|
|
pipeline = new Cogl.Pipeline (Clutter.get_default_backend ().get_cogl_context ());
|
2020-01-09 22:26:30 +03:00
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
~ShadowEffect () {
|
2020-01-09 22:26:30 +03:00
|
|
|
if (current_key != null)
|
|
|
|
decrement_shadow_users (current_key);
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-03-13 01:38:28 +03:00
|
|
|
Cogl.Texture? get_shadow (Cogl.Context context, int width, int height, int shadow_size, int shadow_spread) {
|
2020-01-09 22:26:30 +03:00
|
|
|
var old_key = current_key;
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
current_key = "%ix%i:%i:%i".printf (width, height, shadow_size, shadow_spread);
|
|
|
|
if (old_key == current_key)
|
|
|
|
return null;
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
if (old_key != null)
|
|
|
|
decrement_shadow_users (old_key);
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
Shadow? shadow = null;
|
|
|
|
if ((shadow = shadow_cache.@get (current_key)) != null) {
|
|
|
|
shadow.users++;
|
|
|
|
return shadow.texture;
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
// fill a new texture for this size
|
|
|
|
var buffer = new Granite.Drawing.BufferSurface (width, height);
|
|
|
|
buffer.context.rectangle (shadow_size - shadow_spread, shadow_size - shadow_spread,
|
|
|
|
width - shadow_size * 2 + shadow_spread * 2, height - shadow_size * 2 + shadow_spread * 2);
|
|
|
|
buffer.context.set_source_rgba (0, 0, 0, 0.7);
|
|
|
|
buffer.context.fill ();
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
buffer.exponential_blur (shadow_size / 2);
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
var surface = new Cairo.ImageSurface (Cairo.Format.ARGB32, width, height);
|
|
|
|
var cr = new Cairo.Context (surface);
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
cr.set_source_surface (buffer.surface, 0, 0);
|
|
|
|
cr.paint ();
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-03-13 01:38:28 +03:00
|
|
|
var texture = new Cogl.Texture2D.from_data (context, width, height, Cogl.PixelFormat.BGRA_8888_PRE,
|
|
|
|
surface.get_stride (), surface.get_data ());
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
shadow_cache.@set (current_key, new Shadow (texture));
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
return texture;
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
void decrement_shadow_users (string key) {
|
2020-01-09 22:26:30 +03:00
|
|
|
var shadow = shadow_cache.@get (key);
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
if (shadow == null)
|
|
|
|
return;
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
if (--shadow.users == 0)
|
|
|
|
shadow_cache.unset (key);
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-03-13 01:38:28 +03:00
|
|
|
public override void paint (Clutter.PaintContext context, EffectPaintFlags flags) {
|
|
|
|
var bounding_box = get_bounding_box ();
|
|
|
|
|
|
|
|
var shadow = get_shadow (context.get_framebuffer ().get_context (), (int) (bounding_box.x2 - bounding_box.x1),
|
|
|
|
(int) (bounding_box.y2 - bounding_box.y1), shadow_size, shadow_spread);
|
|
|
|
|
|
|
|
if (shadow != null)
|
|
|
|
pipeline.set_layer_texture (0, shadow);
|
|
|
|
|
|
|
|
var opacity = actor.get_paint_opacity () * shadow_opacity / 255;
|
|
|
|
var alpha = Cogl.Color.from_4ub (255, 255, 255, opacity);
|
|
|
|
alpha.premultiply ();
|
|
|
|
|
|
|
|
pipeline.set_color (alpha);
|
|
|
|
|
|
|
|
context.get_framebuffer ().draw_rectangle (pipeline, bounding_box.x1, bounding_box.y1, bounding_box.x2, bounding_box.y2);
|
|
|
|
|
|
|
|
actor.continue_paint (context);
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-15 03:44:21 +03:00
|
|
|
public virtual ActorBox get_bounding_box () {
|
2020-01-09 22:26:30 +03:00
|
|
|
var size = shadow_size * scale_factor;
|
|
|
|
var bounding_box = ActorBox ();
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
bounding_box.set_origin (-size, -size);
|
|
|
|
bounding_box.set_size (actor.width + size * 2, actor.height + size * 2);
|
2017-07-08 09:30:31 +03:00
|
|
|
|
2020-01-09 22:26:30 +03:00
|
|
|
return bounding_box;
|
|
|
|
}
|
|
|
|
}
|
2017-07-08 09:30:31 +03:00
|
|
|
}
|