use bitflags::bitflags;
use crate::Vector;
use crate::rendering::composition::ICompositionObject;
use crate::rendering::composition::ICompositionObjectHost;
use crate::rendering::composition::animations::AnimationInstanceFactory;
use crate::rendering::composition::animations::ICompositionAnimation;
use crate::rendering::composition::expressions::ExpressionVariant;
use crate::rendering::composition::server::AsServerRenderResource;
use crate::rendering::composition::server::CompositionProperty;
use crate::rendering::composition::server::CompositionPropertyOf;
use crate::rendering::composition::server::IServerAnimatedPropertyHost;
use crate::rendering::composition::server::IServerObject;
use crate::rendering::composition::server::ServerPropertyValue;
use crate::rendering::composition::server::ServerValueChange;
use crate::rendering::composition::server::props_of;
use crate::rendering::composition::server::read_server_object;
use crate::rendering::composition::transport::BatchStreamReader;
use crate::rendering::composition::transport::BatchStreamWriter;
use crate::rendering::composition::transport::BatchValue;
use crate::rendering::composition::transport::BatchValueReader;
use std::any::Any;
use std::any::TypeId;
use std::cell::Cell;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::OnceLock;
use std::time::Duration;
bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct CompositionVisualSurfaceChangedFields: u8 {
const SOURCE_VISUAL = 1;
const SOURCE_OFFSET = 2;
const SOURCE_OFFSET_ANIMATED = 4;
const SOURCE_SIZE = 8;
const SOURCE_SIZE_ANIMATED = 16;
}
}
impl BatchValue for CompositionVisualSurfaceChangedFields {
#[inline]
fn write_to(&self, out: &mut Vec<u8>) {
self.bits().write_to(out);
}
#[inline]
fn read_from(input: &mut BatchValueReader<'_>) -> Self {
CompositionVisualSurfaceChangedFields::from_bits_retain(u8::read_from(input))
}
}
pub trait CompositionVisualSurfaceHooks: ICompositionObjectHost {
fn initialize_defaults_extra(&self) {}
fn validate_source_visual_change(&self, _old_value: &Option<Rc<dyn ICompositionObject>>, _new_value: &Option<Rc<dyn ICompositionObject>>) {}
fn on_source_visual_changed(&self) {}
fn on_source_visual_changing(&self) {}
fn validate_source_offset_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_source_offset_changed(&self) {}
fn on_source_offset_changing(&self) {}
fn validate_source_size_change(&self, _old_value: Vector, _new_value: Vector) {}
fn on_source_size_changed(&self) {}
fn on_source_size_changing(&self) {}
}
pub struct CompositionVisualSurfaceProps {
changed_fields_of_composition_visual_surface: Cell<CompositionVisualSurfaceChangedFields>,
source_visual: RefCell<Option<Rc<dyn ICompositionObject>>>,
source_offset: Cell<Vector>,
source_size: Cell<Vector>,
}
impl Default for CompositionVisualSurfaceProps {
fn default() -> Self {
Self::new()
}
}
impl CompositionVisualSurfaceProps {
pub fn new() -> Self {
Self {
changed_fields_of_composition_visual_surface: Cell::new(CompositionVisualSurfaceChangedFields::empty()),
source_visual: RefCell::new(None),
source_offset: Cell::new(Default::default()),
source_size: Cell::new(Default::default()),
}
}
pub fn initialize_defaults(&self, host: &dyn CompositionVisualSurfaceHooks) {
self.initialize_own_defaults(host);
}
pub fn initialize_own_defaults(&self, host: &dyn CompositionVisualSurfaceHooks) {
CompositionVisualSurfaceHooks::initialize_defaults_extra(host);
}
pub fn source_visual(&self) -> Option<Rc<dyn ICompositionObject>> {
self.source_visual.borrow().clone()
}
pub fn set_source_visual(&self, host: &dyn CompositionVisualSurfaceHooks, value: Option<Rc<dyn ICompositionObject>>) {
let mut changed = false;
let old_value = self.source_visual.borrow().clone();
if old_value.differs(&value) {
CompositionVisualSurfaceHooks::validate_source_visual_change(host, &old_value, &value);
CompositionVisualSurfaceHooks::on_source_visual_changing(host);
changed = true;
*self.source_visual.borrow_mut() = value.clone();
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_VISUAL);
host.register_for_serialization();
}
*self.source_visual.borrow_mut() = value;
if changed {
CompositionVisualSurfaceHooks::on_source_visual_changed(host);
}
}
pub fn source_offset(&self) -> Vector {
self.source_offset.get()
}
pub fn set_source_offset(&self, host: &dyn CompositionVisualSurfaceHooks, value: Vector) {
let mut changed = false;
let old_value = self.source_offset.get();
if old_value.differs(&value) {
CompositionVisualSurfaceHooks::validate_source_offset_change(host, old_value, value);
CompositionVisualSurfaceHooks::on_source_offset_changing(host);
changed = true;
self.source_offset.set(value);
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_OFFSET);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionVisualSurfaceProps::id_of_source_offset_property().base());
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() & !CompositionVisualSurfaceChangedFields::SOURCE_OFFSET_ANIMATED);
if let Some(animation) = host.implicit_animation("SourceOffset") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_OFFSET_ANIMATED);
host.pending_animations().set(
ServerCompositionVisualSurfaceProps::id_of_source_offset_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "SourceOffset", ExpressionVariant::create(value));
}
}
self.source_offset.set(value);
if changed {
CompositionVisualSurfaceHooks::on_source_offset_changed(host);
}
}
pub fn source_size(&self) -> Vector {
self.source_size.get()
}
pub fn set_source_size(&self, host: &dyn CompositionVisualSurfaceHooks, value: Vector) {
let mut changed = false;
let old_value = self.source_size.get();
if old_value.differs(&value) {
CompositionVisualSurfaceHooks::validate_source_size_change(host, old_value, value);
CompositionVisualSurfaceHooks::on_source_size_changing(host);
changed = true;
self.source_size.set(value);
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_SIZE);
host.register_for_serialization();
host.pending_animations().remove(ServerCompositionVisualSurfaceProps::id_of_source_size_property().base());
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() & !CompositionVisualSurfaceChangedFields::SOURCE_SIZE_ANIMATED);
if let Some(animation) = host.implicit_animation("SourceSize") {
if let Some(a) = animation.as_composition_animation() {
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_SIZE_ANIMATED);
host.pending_animations().set(
ServerCompositionVisualSurfaceProps::id_of_source_size_property().base(),
a.create_instance(host.server(), Some(ExpressionVariant::create(value))),
);
}
host.start_animation_group(&animation, "SourceSize", ExpressionVariant::create(value));
}
}
self.source_size.set(value);
if changed {
CompositionVisualSurfaceHooks::on_source_size_changed(host);
}
}
pub fn start_animation(
&self,
host: &dyn CompositionVisualSurfaceHooks,
property_name: &str,
animation: &dyn ICompositionAnimation,
final_value: Option<ExpressionVariant>,
) -> bool {
if property_name == "SourceOffset" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionVisualSurfaceProps::id_of_source_offset_property().base(), server);
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_OFFSET_ANIMATED);
host.register_for_serialization();
return true;
}
if property_name == "SourceSize" {
let server = animation.create_instance(host.server(), final_value);
host.pending_animations().set(ServerCompositionVisualSurfaceProps::id_of_source_size_property().base(), server);
self.changed_fields_of_composition_visual_surface.set(self.changed_fields_of_composition_visual_surface.get() | CompositionVisualSurfaceChangedFields::SOURCE_SIZE_ANIMATED);
host.register_for_serialization();
return true;
}
false
}
pub fn serialize_changes_core(&self, host: &dyn CompositionVisualSurfaceHooks, writer: &mut BatchStreamWriter<'_>) {
let changed = self.changed_fields_of_composition_visual_surface.get();
writer.write(changed);
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_VISUAL) {
writer.write_server_object(self.source_visual.borrow().clone().map(|o| o.server()));
}
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_OFFSET_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionVisualSurfaceProps::id_of_source_offset_property().base()));
} else if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_OFFSET) {
writer.write(self.source_offset.get());
}
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_SIZE_ANIMATED) {
writer.write_value(host.pending_animations().get_and_remove(ServerCompositionVisualSurfaceProps::id_of_source_size_property().base()));
} else if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_SIZE) {
writer.write(self.source_size.get());
}
self.changed_fields_of_composition_visual_surface.set(CompositionVisualSurfaceChangedFields::empty());
}
}
pub trait ServerCompositionVisualSurfaceHooks: IServerAnimatedPropertyHost {
fn deserialize_changes_extra(&self, _reader: &mut BatchStreamReader<'_>) {}
fn on_fields_deserialized(&self, _changed: CompositionVisualSurfaceChangedFields) {}
fn on_source_visual_changed(&self) {}
fn on_source_visual_changing(&self) {}
}
pub struct ServerCompositionVisualSurfaceProps {
source_visual: RefCell<Option<Rc<dyn IServerObject>>>,
source_offset: Cell<Vector>,
source_size: Cell<Vector>,
}
impl Default for ServerCompositionVisualSurfaceProps {
fn default() -> Self {
Self::new()
}
}
impl ServerCompositionVisualSurfaceProps {
pub fn new() -> Self {
Self {
source_visual: RefCell::new(None),
source_offset: Cell::new(Default::default()),
source_size: Cell::new(Default::default()),
}
}
pub fn find_props(&self, type_id: TypeId) -> Option<&dyn Any> {
if type_id == TypeId::of::<Self>() {
return Some(self);
}
None
}
pub fn id_of_source_visual_property() -> &'static CompositionPropertyOf<Option<Rc<dyn IServerObject>>> {
static PROPERTY: OnceLock<CompositionPropertyOf<Option<Rc<dyn IServerObject>>>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionVisualSurfaceProps, Option<Rc<dyn IServerObject>>>(
"SourceVisual",
"ServerCompositionVisualSurface",
|obj| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_visual.borrow().clone(),
|obj, v| *props_of::<ServerCompositionVisualSurfaceProps>(obj).source_visual.borrow_mut() = v,
None,
None,
)
})
}
pub fn source_visual(&self) -> Option<Rc<dyn IServerObject>> {
self.source_visual.borrow().clone()
}
pub fn set_source_visual(&self, host: &dyn ServerCompositionVisualSurfaceHooks, value: Option<Rc<dyn IServerObject>>) {
let old_value = self.source_visual.borrow().clone();
let mut changed = false;
if old_value.differs(&value) {
ServerCompositionVisualSurfaceHooks::on_source_visual_changing(host);
changed = true;
}
host.set_value(
Self::id_of_source_visual_property().base(),
ServerValueChange {
equal: old_value.default_equals(&value),
old_resource: old_value.as_server_render_resource(),
new_resource: value.as_server_render_resource(),
assign: &mut || *self.source_visual.borrow_mut() = value.clone(),
},
);
if changed {
ServerCompositionVisualSurfaceHooks::on_source_visual_changed(host);
}
}
pub fn id_of_source_offset_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionVisualSurfaceProps, Vector>(
"SourceOffset",
"ServerCompositionVisualSurface",
|obj| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_offset.get(),
|obj, v| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_offset.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionVisualSurfaceProps>(obj).source_offset.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_offset.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn source_offset(&self) -> Vector {
self.source_offset.get()
}
pub fn set_source_offset(&self, host: &dyn ServerCompositionVisualSurfaceHooks, value: Vector) {
self.source_offset.set(value);
host.remove_animation_for_property(Self::id_of_source_offset_property().base());
}
pub fn id_of_source_size_property() -> &'static CompositionPropertyOf<Vector> {
static PROPERTY: OnceLock<CompositionPropertyOf<Vector>> = OnceLock::new();
PROPERTY.get_or_init(|| {
CompositionProperty::register::<ServerCompositionVisualSurfaceProps, Vector>(
"SourceSize",
"ServerCompositionVisualSurface",
|obj| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_size.get(),
|obj, v| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_size.set(v),
Some(|obj| ExpressionVariant::create(props_of::<ServerCompositionVisualSurfaceProps>(obj).source_size.get())),
Some(|obj, v: ExpressionVariant| props_of::<ServerCompositionVisualSurfaceProps>(obj).source_size.set(v.cast_or_default::<Vector>())),
)
})
}
pub fn source_size(&self) -> Vector {
self.source_size.get()
}
pub fn set_source_size(&self, host: &dyn ServerCompositionVisualSurfaceHooks, value: Vector) {
self.source_size.set(value);
host.remove_animation_for_property(Self::id_of_source_size_property().base());
}
pub fn deserialize_changes_core(
&self,
host: &dyn ServerCompositionVisualSurfaceHooks,
reader: &mut BatchStreamReader<'_>,
committed_at: Duration,
) {
let compositor = host.server_compositor();
ServerCompositionVisualSurfaceHooks::deserialize_changes_extra(host, reader);
let changed = reader.read::<CompositionVisualSurfaceChangedFields>();
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_VISUAL) {
let value = read_server_object(reader, compositor.as_ref());
self.set_source_visual(host, value);
}
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_OFFSET_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `SourceOffset` without an animation instance");
};
host.set_animated_value(
Self::id_of_source_offset_property().base(),
ExpressionVariant::create(self.source_offset.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_OFFSET) {
let value = reader.read::<Vector>();
self.set_source_offset(host, value);
}
if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_SIZE_ANIMATED) {
let Some(animation) = reader.read_value::<AnimationInstanceFactory>().map(AnimationInstanceFactory::create) else {
panic!("a batch starts an animation of `SourceSize` without an animation instance");
};
host.set_animated_value(
Self::id_of_source_size_property().base(),
ExpressionVariant::create(self.source_size.get()),
committed_at,
animation,
);
} else if changed.contains(CompositionVisualSurfaceChangedFields::SOURCE_SIZE) {
let value = reader.read::<Vector>();
self.set_source_size(host, value);
}
ServerCompositionVisualSurfaceHooks::on_fields_deserialized(host, changed);
}
pub fn get_composition_property(name: &str) -> Option<&'static CompositionProperty> {
if name == "SourceOffset" {
return Some(Self::id_of_source_offset_property().base());
}
if name == "SourceSize" {
return Some(Self::id_of_source_size_property().base());
}
None
}
}