2021-04-09 12:38:02 +00:00
|
|
|
//
|
|
|
|
// Copyright (C) 2021 Bilal Elmoussaoui <bil.elmoussaoui@gmail.com>
|
|
|
|
// Copyright (C) 2021 Jordan Petridis <jordan@centricular.com>
|
|
|
|
// Copyright (C) 2021 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public License, v2.0.
|
|
|
|
// If a copy of the MPL was not distributed with this file, You can obtain one at
|
|
|
|
// <https://mozilla.org/MPL/2.0/>.
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::subclass::prelude::*;
|
2023-08-24 10:05:15 +00:00
|
|
|
use gtk::{gdk, glib, graphene, gsk};
|
2021-04-09 12:38:02 +00:00
|
|
|
|
2021-10-17 17:23:43 +00:00
|
|
|
use crate::sink::frame::{Frame, Texture};
|
2021-04-09 12:38:02 +00:00
|
|
|
|
2024-02-07 17:30:12 +00:00
|
|
|
use std::cell::{Cell, RefCell};
|
2021-10-17 17:23:43 +00:00
|
|
|
use std::collections::HashMap;
|
2021-04-09 12:38:02 +00:00
|
|
|
|
2024-01-31 15:07:56 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2021-04-09 12:38:02 +00:00
|
|
|
|
2021-10-19 06:45:07 +00:00
|
|
|
static CAT: Lazy<gst::DebugCategory> = Lazy::new(|| {
|
2021-04-09 12:38:02 +00:00
|
|
|
gst::DebugCategory::new(
|
2022-12-28 18:18:28 +00:00
|
|
|
"gtk4paintable",
|
2021-04-09 12:38:02 +00:00
|
|
|
gst::DebugColorFlags::empty(),
|
|
|
|
Some("GTK4 Paintable Sink Paintable"),
|
|
|
|
)
|
|
|
|
});
|
|
|
|
|
2023-08-24 10:05:15 +00:00
|
|
|
#[derive(Debug)]
|
2022-10-27 17:50:40 +00:00
|
|
|
pub struct Paintable {
|
2021-10-17 17:23:43 +00:00
|
|
|
paintables: RefCell<Vec<Texture>>,
|
|
|
|
cached_textures: RefCell<HashMap<usize, gdk::Texture>>,
|
2021-10-19 06:45:07 +00:00
|
|
|
gl_context: RefCell<Option<gdk::GLContext>>,
|
2024-02-07 17:30:12 +00:00
|
|
|
background_color: Cell<gdk::RGBA>,
|
2023-08-22 08:44:35 +00:00
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
scaling_filter: Cell<gsk::ScalingFilter>,
|
|
|
|
use_scaling_filter: Cell<bool>,
|
2024-02-07 16:12:29 +00:00
|
|
|
#[cfg(not(feature = "gtk_v4_10"))]
|
2023-08-24 10:05:15 +00:00
|
|
|
premult_shader: gsk::GLShader,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Paintable {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
paintables: Default::default(),
|
|
|
|
cached_textures: Default::default(),
|
|
|
|
gl_context: Default::default(),
|
2024-02-07 17:30:12 +00:00
|
|
|
background_color: Cell::new(gdk::RGBA::BLACK),
|
2023-08-22 08:44:35 +00:00
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
scaling_filter: Cell::new(gsk::ScalingFilter::Linear),
|
|
|
|
use_scaling_filter: Cell::new(false),
|
2024-02-07 16:12:29 +00:00
|
|
|
#[cfg(not(feature = "gtk_v4_10"))]
|
2023-08-24 10:05:15 +00:00
|
|
|
premult_shader: gsk::GLShader::from_bytes(&glib::Bytes::from_static(include_bytes!(
|
|
|
|
"premult.glsl"
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 12:38:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[glib::object_subclass]
|
2022-10-27 17:50:40 +00:00
|
|
|
impl ObjectSubclass for Paintable {
|
|
|
|
const NAME: &'static str = "GstGtk4Paintable";
|
|
|
|
type Type = super::Paintable;
|
2021-04-09 12:38:02 +00:00
|
|
|
type Interfaces = (gdk::Paintable,);
|
|
|
|
}
|
|
|
|
|
2021-10-19 06:45:07 +00:00
|
|
|
impl ObjectImpl for Paintable {
|
|
|
|
fn properties() -> &'static [glib::ParamSpec] {
|
|
|
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
|
|
|
vec![
|
|
|
|
glib::ParamSpecObject::builder::<gdk::GLContext>("gl-context")
|
|
|
|
.nick("GL Context")
|
|
|
|
.blurb("GL context to use for rendering")
|
|
|
|
.construct_only()
|
|
|
|
.build(),
|
2024-02-07 17:30:12 +00:00
|
|
|
glib::ParamSpecUInt::builder("background-color")
|
|
|
|
.nick("Background Color")
|
|
|
|
.blurb("Background color to render behind the video frame and in the borders")
|
|
|
|
.default_value(0)
|
|
|
|
.build(),
|
2023-08-22 08:44:35 +00:00
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
glib::ParamSpecEnum::builder_with_default::<gsk::ScalingFilter>(
|
|
|
|
"scaling-filter",
|
|
|
|
gsk::ScalingFilter::Linear,
|
|
|
|
)
|
|
|
|
.nick("Scaling Filter")
|
|
|
|
.blurb("Scaling filter to use for rendering")
|
|
|
|
.build(),
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
glib::ParamSpecBoolean::builder("use-scaling-filter")
|
|
|
|
.nick("Use Scaling Filter")
|
|
|
|
.blurb("Use selected scaling filter or GTK default for rendering")
|
|
|
|
.default_value(false)
|
|
|
|
.build(),
|
2021-10-19 06:45:07 +00:00
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
PROPERTIES.as_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
|
|
|
match pspec.name() {
|
|
|
|
"gl-context" => self.gl_context.borrow().to_value(),
|
2024-02-07 17:30:12 +00:00
|
|
|
"background-color" => {
|
|
|
|
let color = self.background_color.get();
|
|
|
|
|
|
|
|
let v = (f32::clamp(color.red() * 255.0, 0.0, 255.0) as u32) << 24
|
|
|
|
| (f32::clamp(color.green() * 255.0, 0.0, 255.0) as u32) << 16
|
|
|
|
| (f32::clamp(color.blue() * 255.0, 0.0, 255.0) as u32) << 8
|
|
|
|
| (f32::clamp(color.alpha() * 255.0, 0.0, 255.0) as u32);
|
|
|
|
|
|
|
|
v.to_value()
|
|
|
|
}
|
2023-08-22 08:44:35 +00:00
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
"scaling-filter" => self.scaling_filter.get().to_value(),
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
"use-scaling-filter" => self.use_scaling_filter.get().to_value(),
|
2021-10-19 06:45:07 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
|
|
|
match pspec.name() {
|
|
|
|
"gl-context" => {
|
|
|
|
*self.gl_context.borrow_mut() = value.get::<Option<gtk::gdk::GLContext>>().unwrap();
|
|
|
|
}
|
2024-02-07 17:30:12 +00:00
|
|
|
"background-color" => {
|
|
|
|
let v = value.get::<u32>().unwrap();
|
|
|
|
let red = ((v & 0xff_00_00_00) >> 24) as f32 / 255.0;
|
|
|
|
let green = ((v & 0x00_ff_00_00) >> 16) as f32 / 255.0;
|
|
|
|
let blue = ((v & 0x00_00_ff_00) >> 8) as f32 / 255.0;
|
|
|
|
let alpha = (v & 0x00_00_00_ff) as f32 / 255.0;
|
|
|
|
self.background_color
|
|
|
|
.set(gdk::RGBA::new(red, green, blue, alpha))
|
|
|
|
}
|
2023-08-22 08:44:35 +00:00
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
"scaling-filter" => self.scaling_filter.set(value.get().unwrap()),
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
"use-scaling-filter" => self.use_scaling_filter.set(value.get().unwrap()),
|
2021-10-19 06:45:07 +00:00
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-09 12:38:02 +00:00
|
|
|
|
2022-10-27 17:50:40 +00:00
|
|
|
impl PaintableImpl for Paintable {
|
2022-10-09 13:06:59 +00:00
|
|
|
fn intrinsic_height(&self) -> i32 {
|
2021-10-17 17:23:43 +00:00
|
|
|
if let Some(paintable) = self.paintables.borrow().first() {
|
|
|
|
f32::round(paintable.height) as i32
|
2021-04-09 12:38:02 +00:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn intrinsic_width(&self) -> i32 {
|
2021-10-17 17:23:43 +00:00
|
|
|
if let Some(paintable) = self.paintables.borrow().first() {
|
|
|
|
f32::round(paintable.width) as i32
|
2021-04-09 12:38:02 +00:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn intrinsic_aspect_ratio(&self) -> f64 {
|
2021-10-17 17:23:43 +00:00
|
|
|
if let Some(paintable) = self.paintables.borrow().first() {
|
|
|
|
paintable.width as f64 / paintable.height as f64
|
2021-04-09 12:38:02 +00:00
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 13:06:59 +00:00
|
|
|
fn snapshot(&self, snapshot: &gdk::Snapshot, width: f64, height: f64) {
|
2021-10-17 17:23:43 +00:00
|
|
|
let snapshot = snapshot.downcast_ref::<gtk::Snapshot>().unwrap();
|
|
|
|
|
2024-02-07 17:30:12 +00:00
|
|
|
let background_color = self.background_color.get();
|
2021-10-17 17:23:43 +00:00
|
|
|
let paintables = self.paintables.borrow();
|
|
|
|
|
|
|
|
if !paintables.is_empty() {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::trace!(CAT, imp: self, "Snapshotting frame");
|
2021-10-17 17:23:43 +00:00
|
|
|
|
|
|
|
let (frame_width, frame_height) =
|
|
|
|
paintables.first().map(|p| (p.width, p.height)).unwrap();
|
|
|
|
|
|
|
|
let mut scale_x = width / frame_width as f64;
|
|
|
|
let mut scale_y = height / frame_height as f64;
|
|
|
|
let mut trans_x = 0.0;
|
|
|
|
let mut trans_y = 0.0;
|
|
|
|
|
|
|
|
// TODO: Property for keeping aspect ratio or not
|
|
|
|
if (scale_x - scale_y).abs() > f64::EPSILON {
|
|
|
|
if scale_x > scale_y {
|
|
|
|
trans_x =
|
|
|
|
((frame_width as f64 * scale_x) - (frame_width as f64 * scale_y)) / 2.0;
|
|
|
|
scale_x = scale_y;
|
|
|
|
} else {
|
|
|
|
trans_y =
|
|
|
|
((frame_height as f64 * scale_y) - (frame_height as f64 * scale_x)) / 2.0;
|
|
|
|
scale_y = scale_x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-07 17:09:36 +00:00
|
|
|
snapshot.append_color(
|
2024-02-07 17:30:12 +00:00
|
|
|
&background_color,
|
2024-02-07 17:09:36 +00:00
|
|
|
&graphene::Rect::new(0f32, 0f32, width as f32, height as f32),
|
|
|
|
);
|
2021-10-17 17:23:43 +00:00
|
|
|
|
|
|
|
snapshot.translate(&graphene::Point::new(trans_x as f32, trans_y as f32));
|
|
|
|
|
|
|
|
for Texture {
|
|
|
|
texture,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
width: paintable_width,
|
|
|
|
height: paintable_height,
|
|
|
|
global_alpha,
|
2023-08-24 10:05:15 +00:00
|
|
|
has_alpha,
|
2021-10-17 17:23:43 +00:00
|
|
|
} in &*paintables
|
|
|
|
{
|
|
|
|
snapshot.push_opacity(*global_alpha as f64);
|
2023-08-24 10:05:15 +00:00
|
|
|
|
2023-08-22 06:20:22 +00:00
|
|
|
let texture_width = *paintable_width * scale_x as f32;
|
|
|
|
let texture_height = *paintable_height * scale_y as f32;
|
2024-03-15 11:59:52 +00:00
|
|
|
let x = *x * scale_x as f32;
|
|
|
|
let y = *y * scale_y as f32;
|
|
|
|
let bounds = graphene::Rect::new(x, y, texture_width, texture_height);
|
2023-08-24 10:05:15 +00:00
|
|
|
|
|
|
|
// Only premultiply GL textures that expect to be in premultiplied RGBA format.
|
2024-02-07 16:12:29 +00:00
|
|
|
//
|
|
|
|
// For GTK 4.14 or newer we use the correct format directly when building the
|
|
|
|
// texture, but only if a GLES3+ context is used. In that case the NGL renderer is
|
|
|
|
// used by GTK, which supports non-premultiplied formats correctly and fast.
|
|
|
|
//
|
|
|
|
// For GTK 4.10-4.12, or 4.14 and newer if a GLES2 context is used, we use a
|
|
|
|
// self-mask to pre-multiply the alpha.
|
|
|
|
//
|
|
|
|
// For GTK before 4.10, we use a GL shader and hope that it works.
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
{
|
|
|
|
let context_requires_premult = {
|
|
|
|
#[cfg(feature = "gtk_v4_14")]
|
|
|
|
{
|
|
|
|
self.gl_context.borrow().as_ref().map_or(false, |context| {
|
|
|
|
context.api() != gdk::GLAPI::GLES || context.version().0 < 3
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "gtk_v4_14"))]
|
|
|
|
{
|
|
|
|
true
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let do_premult =
|
|
|
|
context_requires_premult && texture.is::<gdk::GLTexture>() && *has_alpha;
|
|
|
|
if do_premult {
|
|
|
|
snapshot.push_mask(gsk::MaskMode::Alpha);
|
2023-08-22 08:44:35 +00:00
|
|
|
if self.use_scaling_filter.get() {
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
snapshot.append_scaled_texture(
|
|
|
|
texture,
|
|
|
|
self.scaling_filter.get(),
|
|
|
|
&bounds,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
snapshot.append_texture(texture, &bounds);
|
|
|
|
}
|
2024-02-07 16:12:29 +00:00
|
|
|
snapshot.pop(); // pop mask
|
|
|
|
|
|
|
|
// color matrix to set alpha of the source to 1.0 as it was
|
|
|
|
// already applied via the mask just above.
|
|
|
|
snapshot.push_color_matrix(
|
|
|
|
&graphene::Matrix::from_float({
|
|
|
|
[
|
|
|
|
1.0, 0.0, 0.0, 0.0, //
|
|
|
|
0.0, 1.0, 0.0, 0.0, //
|
|
|
|
0.0, 0.0, 1.0, 0.0, //
|
|
|
|
0.0, 0.0, 0.0, 0.0,
|
|
|
|
]
|
|
|
|
}),
|
|
|
|
&graphene::Vec4::new(0.0, 0.0, 0.0, 1.0),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-22 08:44:35 +00:00
|
|
|
if self.use_scaling_filter.get() {
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
snapshot.append_scaled_texture(texture, self.scaling_filter.get(), &bounds);
|
|
|
|
} else {
|
|
|
|
snapshot.append_texture(texture, &bounds);
|
|
|
|
}
|
2024-02-07 16:12:29 +00:00
|
|
|
|
|
|
|
if do_premult {
|
|
|
|
snapshot.pop(); // pop color matrix
|
|
|
|
snapshot.pop(); // pop mask 2
|
|
|
|
}
|
2023-08-24 10:05:15 +00:00
|
|
|
}
|
2024-02-07 16:12:29 +00:00
|
|
|
#[cfg(not(feature = "gtk_v4_10"))]
|
|
|
|
{
|
|
|
|
let do_premult =
|
|
|
|
texture.is::<gdk::GLTexture>() && *has_alpha && gtk::micro_version() < 13;
|
|
|
|
if do_premult {
|
|
|
|
snapshot.push_gl_shader(
|
|
|
|
&self.premult_shader,
|
|
|
|
&bounds,
|
|
|
|
gsk::ShaderArgsBuilder::new(&self.premult_shader, None).to_args(),
|
|
|
|
);
|
|
|
|
}
|
2023-08-24 10:05:15 +00:00
|
|
|
|
2023-08-22 08:44:35 +00:00
|
|
|
if self.use_scaling_filter.get() {
|
|
|
|
#[cfg(feature = "gtk_v4_10")]
|
|
|
|
snapshot.append_scaled_texture(texture, self.scaling_filter.get(), &bounds);
|
|
|
|
} else {
|
|
|
|
snapshot.append_texture(texture, &bounds);
|
|
|
|
}
|
2023-08-24 10:05:15 +00:00
|
|
|
|
2024-02-07 16:12:29 +00:00
|
|
|
if do_premult {
|
|
|
|
snapshot.gl_shader_pop_texture(); // pop texture appended above from the shader
|
|
|
|
snapshot.pop(); // pop shader
|
|
|
|
}
|
2023-08-24 10:05:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshot.pop(); // pop opacity
|
2021-10-17 17:23:43 +00:00
|
|
|
}
|
2021-04-09 12:38:02 +00:00
|
|
|
} else {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::trace!(CAT, imp: self, "Snapshotting black frame");
|
2021-04-09 12:38:02 +00:00
|
|
|
snapshot.append_color(
|
2024-02-07 17:30:12 +00:00
|
|
|
&background_color,
|
2021-04-09 12:38:02 +00:00
|
|
|
&graphene::Rect::new(0f32, 0f32, width as f32, height as f32),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-17 17:23:43 +00:00
|
|
|
|
2022-10-27 17:50:40 +00:00
|
|
|
impl Paintable {
|
2022-10-09 13:06:59 +00:00
|
|
|
pub(super) fn handle_frame_changed(&self, frame: Option<Frame>) {
|
2021-10-19 06:45:07 +00:00
|
|
|
let context = self.gl_context.borrow();
|
2021-10-17 17:23:43 +00:00
|
|
|
if let Some(frame) = frame {
|
2022-10-09 13:06:59 +00:00
|
|
|
gst::trace!(CAT, imp: self, "Received new frame");
|
2021-10-17 17:23:43 +00:00
|
|
|
|
2021-10-19 06:45:07 +00:00
|
|
|
let new_paintables =
|
|
|
|
frame.into_textures(context.as_ref(), &mut self.cached_textures.borrow_mut());
|
2021-10-17 17:23:43 +00:00
|
|
|
let new_size = new_paintables
|
|
|
|
.first()
|
|
|
|
.map(|p| (f32::round(p.width) as u32, f32::round(p.height) as u32))
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let old_paintables = self.paintables.replace(new_paintables);
|
|
|
|
let old_size = old_paintables
|
|
|
|
.first()
|
|
|
|
.map(|p| (f32::round(p.width) as u32, f32::round(p.height) as u32));
|
|
|
|
|
|
|
|
if Some(new_size) != old_size {
|
2022-02-21 17:43:46 +00:00
|
|
|
gst::debug!(
|
2021-10-17 17:23:43 +00:00
|
|
|
CAT,
|
2022-10-09 13:06:59 +00:00
|
|
|
imp: self,
|
2023-02-21 19:15:49 +00:00
|
|
|
"Size changed from {old_size:?} to {new_size:?}",
|
2021-10-17 17:23:43 +00:00
|
|
|
);
|
2022-10-23 20:03:22 +00:00
|
|
|
self.obj().invalidate_size();
|
2021-10-17 17:23:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-23 20:03:22 +00:00
|
|
|
self.obj().invalidate_contents();
|
2021-10-17 17:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-22 18:44:25 +00:00
|
|
|
|
|
|
|
pub(super) fn handle_flush_frames(&self) {
|
|
|
|
gst::debug!(CAT, imp: self, "Flushing frames");
|
|
|
|
self.paintables.borrow_mut().clear();
|
|
|
|
self.cached_textures.borrow_mut().clear();
|
|
|
|
self.obj().invalidate_size();
|
|
|
|
self.obj().invalidate_contents();
|
|
|
|
}
|
2021-10-17 17:23:43 +00:00
|
|
|
}
|