tessera_ui/renderer/drawer/
command.rs

1//! Draw command traits and barrier requirements.
2//!
3//! This module defines the core traits and types for graphics rendering commands
4//! in the unified command system.
5
6use std::any::Any;
7
8/// Specifies synchronization requirements for rendering operations.
9///
10/// Barrier requirements ensure proper ordering of rendering operations when
11/// commands need to sample from previously rendered content.
12pub enum BarrierRequirement {
13    /// Command needs to sample from the background (previously rendered content).
14    ///
15    /// This triggers a texture copy operation before the command is executed,
16    /// making the previous frame's content available for sampling in shaders.
17    SampleBackground,
18}
19
20/// Trait providing type erasure capabilities for command objects.
21///
22/// This trait allows commands to be stored and passed around as trait objects
23/// while still providing access to their concrete types when needed for
24/// pipeline dispatch.
25pub trait AsAny {
26    /// Returns a reference to the concrete type as `&dyn Any`.
27    fn as_any(&self) -> &dyn Any;
28}
29
30/// Blanket implementation of `AsAny` for all types that implement `Any`.
31impl<T: Any> AsAny for T {
32    fn as_any(&self) -> &dyn Any {
33        self
34    }
35}
36
37/// Trait for graphics rendering commands that can be processed by draw pipelines.
38///
39/// Implement this trait for structs that represent graphics operations such as
40/// shape drawing, text rendering, image display, or custom visual effects.
41///
42/// # Example
43///
44/// ```
45/// use tessera_ui::{BarrierRequirement, DrawCommand};
46///
47/// struct RectangleCommand {
48///     color: [f32; 4],
49///     corner_radius: f32,
50/// }
51///
52/// impl DrawCommand for RectangleCommand {
53///     // Most commands don't need barriers
54///     fn barrier(&self) -> Option<BarrierRequirement> {
55///         None
56///     }
57/// }
58/// ```
59pub trait DrawCommand: AsAny + Send + Sync {
60    /// Specifies barrier requirements for this draw operation.
61    ///
62    /// Return `Some(BarrierRequirement::SampleBackground)` if your command needs
63    /// to sample from previously rendered content (e.g., for blur effects or
64    /// other post-processing operations).
65    ///
66    /// # Returns
67    ///
68    /// - `None` for standard rendering operations (default)
69    /// - `Some(BarrierRequirement::SampleBackground)` for operations that sample previous content
70    fn barrier(&self) -> Option<BarrierRequirement> {
71        None
72    }
73}