[PATCH v3 9/23] rust: drm: kms: add plane damage-clip accessors

From: Mike Lothian

Date: Wed Aug 26 2026 - 12:42:12 EST


Add safe access to the FB_DAMAGE_CLIPS rectangles intersected with
the visible source area.

damage_merged() returns the bounding rectangle produced by
drm_atomic_helper_damage_merged(). for_each_damage_clip() wraps
the DRM damage iterator for drivers that can process the individual
rectangles without repainting their bounding box.

Assisted-by: Claude:claude-opus-5
Signed-off-by: Mike Lothian <mike@xxxxxxxxxxxxxx>
---
rust/bindings/bindings_helper.h | 1 +
rust/kernel/drm/kms/plane.rs | 105 ++++++++++++++++++++++++++++++++
2 files changed, 106 insertions(+)

diff --git a/rust/bindings/bindings_helper.h b/rust/bindings/bindings_helper.h
index 38ad80fae0ed..c4abdd887699 100644
--- a/rust/bindings/bindings_helper.h
+++ b/rust/bindings/bindings_helper.h
@@ -41,6 +41,7 @@
#include <drm/clients/drm_client_setup.h>
#include <drm/drm_connector.h>
#include <drm/drm_crtc.h>
+#include <drm/drm_damage_helper.h>
#include <drm/drm_device.h>
#include <drm/drm_drv.h>
#include <drm/drm_edid.h>
diff --git a/rust/kernel/drm/kms/plane.rs b/rust/kernel/drm/kms/plane.rs
index 8e3f711b0767..3bff4091abb2 100644
--- a/rust/kernel/drm/kms/plane.rs
+++ b/rust/kernel/drm/kms/plane.rs
@@ -693,6 +693,49 @@ pub trait FromRawPlaneState: AsRawPlaneState {
unsafe fn from_raw_mut<'a>(ptr: *mut bindings::drm_plane_state) -> &'a mut Self;
}

+/// A rectangle in a plane's source (pixel) space, as produced by
+/// [`RawPlaneState::damage_merged`].
+///
+/// The box is inclusive on the top-left and exclusive on the bottom-right (`[x1, x2)` by
+/// `[y1, y2)`), matching [`struct drm_rect`].
+///
+/// [`struct drm_rect`]: srctree/include/drm/drm_rect.h
+#[derive(Copy, Clone, Debug, PartialEq, Eq)]
+pub struct Rect {
+ /// Left edge, inclusive.
+ pub x1: i32,
+ /// Top edge, inclusive.
+ pub y1: i32,
+ /// Right edge, exclusive.
+ pub x2: i32,
+ /// Bottom edge, exclusive.
+ pub y2: i32,
+}
+
+impl Rect {
+ #[inline]
+ fn from_raw(r: &bindings::drm_rect) -> Self {
+ Self {
+ x1: r.x1,
+ y1: r.y1,
+ x2: r.x2,
+ y2: r.y2,
+ }
+ }
+
+ /// The width of the rectangle in pixels.
+ #[inline]
+ pub fn width(&self) -> i32 {
+ self.x2 - self.x1
+ }
+
+ /// The height of the rectangle in pixels.
+ #[inline]
+ pub fn height(&self) -> i32 {
+ self.y2 - self.y1
+ }
+}
+
/// Common methods available on any type which implements [`AsRawPlane`].
///
/// This is implemented internally by DRM, and provides many of the basic methods for working with
@@ -774,6 +817,68 @@ fn atomic_helper_check<S, D>(
})
}

+ /// Merge all frame-damage clips on this (new) plane state -- relative to `old` -- into a
+ /// single bounding rectangle, intersected with the plane's visible source area.
+ ///
+ /// Returns [`None`] when the plane is not visible or there is nothing to update. If the client
+ /// supplied no explicit damage clips, the full plane rectangle is returned, so a driver can
+ /// always treat [`Some`] as "repaint this rectangle" and fall back to a full-frame update.
+ /// Coordinates are integer pixels in the plane's source space.
+ ///
+ /// [`drm_atomic_helper_damage_merged`]: srctree/include/drm/drm_damage_helper.h
+ fn damage_merged(&self, old: &impl AsRawPlaneState) -> Option<Rect> {
+ let mut rect = bindings::drm_rect {
+ x1: 0,
+ y1: 0,
+ x2: 0,
+ y2: 0,
+ };
+
+ // SAFETY:
+ // - `old` and `self` are valid initialized `drm_plane_state`s via their type invariants.
+ // - `drm_atomic_helper_damage_merged` only reads the two states (to gather the damage
+ // clips and the source rectangle) and writes the merged result into `rect`; it does not
+ // mutate the plane state, so deriving a `*mut` from our shared reference is sound.
+ let visible = unsafe {
+ bindings::drm_atomic_helper_damage_merged(
+ core::ptr::from_ref(old.as_raw()),
+ core::ptr::from_ref(self.as_raw()).cast_mut(),
+ &mut rect,
+ )
+ };
+
+ visible.then(|| Rect::from_raw(&rect))
+ }
+
+ /// Invoke `f` once per frame-damage clip on this (new) plane state relative to `old`, each
+ /// intersected with the plane's visible source area -- i.e. the individual rectangles that
+ /// [`Self::damage_merged`] collapses into one. If the client supplied no explicit damage clips,
+ /// `f` is called once with the full plane rectangle. Coordinates are integer pixels in the
+ /// plane's source space.
+ ///
+ /// This lets a driver forward each changed region separately (e.g. to a remote display) instead
+ /// of the bounding box of them all.
+ ///
+ /// [`drm_atomic_helper_damage_iter`]: srctree/include/drm/drm_damage_helper.h
+ fn for_each_damage_clip(&self, old: &impl AsRawPlaneState, mut f: impl FnMut(Rect)) {
+ let mut iter = bindings::drm_atomic_helper_damage_iter::default();
+ let mut clip = bindings::drm_rect::default();
+ // SAFETY:
+ // - `old` and `self` are valid initialized `drm_plane_state`s via their type invariants.
+ // - `drm_atomic_helper_damage_iter_init` only reads the two states to set up `iter`, and
+ // `_next` only reads `iter` and writes `clip`; neither escapes a pointer.
+ unsafe {
+ bindings::drm_atomic_helper_damage_iter_init(
+ &mut iter,
+ core::ptr::from_ref(old.as_raw()),
+ core::ptr::from_ref(self.as_raw()),
+ );
+ while bindings::drm_atomic_helper_damage_iter_next(&mut iter, &mut clip) {
+ f(Rect::from_raw(&clip));
+ }
+ }
+ }
+
/// Return the framebuffer currently set for this plane state
#[inline]
fn framebuffer<D>(&self) -> Option<&Framebuffer<D>>