ltk/draw/
software.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// SPDX-License-Identifier: LGPL-2.1-only
// Copyright (C) 2026 Liberux Labs, S. L. <info@liberux.net>

//! Software (CPU + `wl_shm` pool) draw paths.
//!
//! Two functions, both symmetric with their GPU counterparts in
//! [`super::gles`]:
//!
//! * [`draw_surface_full`] — full redraw. Allocate a buffer, clear the
//!   canvas, run layout+draw, write pixels to the SHM buffer, damage
//!   the whole surface (or fine-grained rects from [`compute_damage`]),
//!   commit.
//! * [`draw_surface_partial`] — clip-masked repaint. The canvas
//!   pixmap from the previous frame is still valid; install a clip
//!   covering only the dirty rects, repaint the background + title bar
//!   + widget tree under the clip, and damage Wayland with exactly
//!   those rects.
//!
//! Both paths share the DrawCtx / `layout_and_draw` / `draw_titlebar`
//! / `apply_input_region` helpers with the GPU path.

use std::sync::Arc;

use smithay_client_toolkit::compositor::CompositorState;
use smithay_client_toolkit::reexports::client::protocol::{ wl_shm, wl_surface::WlSurface };

use crate::event_loop::SurfaceState;
use crate::render::Canvas;
use crate::types::{ Color, Rect };
use crate::widget::Element;

use super::{ DrawCtx, compute_damage, layout_and_draw };
use super::chrome::{ apply_input_region, draw_fallback_banner, draw_titlebar };

/// Full redraw path: clear the canvas, run layout+draw for every widget, then
/// emit either tight per-widget damage or a single full-surface damage rect
/// depending on what [`compute_damage`] derives.
pub( crate ) fn draw_surface_full<Msg: Clone>(
	ss:            &mut SurfaceState<Msg>,
	compositor:    &CompositorState,
	view:          &Element<Msg>,
	bg:            Color,
	input_region:  Option<&[Rect]>,
	debug_layout:  bool,
	shm_format:    wl_shm::Format,
	swap_rb:       bool,
	pw:            u32,
	ph:            u32,
	scale:         u32,
	request_frame: &dyn Fn( &WlSurface ),
)
{
	let Some( pool ) = ss.pool.as_mut() else { return };
	let stride = pw * 4;

	let ( buffer, canvas_buf ) = match pool.create_buffer(
		pw as i32, ph as i32, stride as i32, shm_format,
	)
	{
		Ok( r )  => r,
		Err( _ ) => return,
	};

	let canvas = ss.canvas.get_or_insert_with( || {
		let mut c = Canvas::new( pw, ph );
		c.set_dpi_scale( scale as f32 );
		if let Some( reg ) = crate::theme::build_font_registry()
		{
			c.set_font_registry( Arc::new( reg ) );
		}
		c
	} );
	if canvas.size() != ( pw, ph )
	{
		canvas.resize( pw, ph );
		canvas.set_dpi_scale( scale as f32 );
	}
	canvas.clear_clip();

	let sf          = scale as f32;
	let tb_h        = ss.titlebar_height * sf;
	let screen_rect = Rect { x: 0.0, y: tb_h, width: pw as f32, height: (ph as f32 - tb_h).max( 0.0 ) };

	if bg.a > 0.0 { canvas.fill( bg ); } else { canvas.clear(); }

	ss.titlebar_close_rect = draw_titlebar( canvas, &ss.titlebar_title, pw, tb_h, sf );

	let mut ctx: DrawCtx<Msg> = DrawCtx
	{
		focused_idx:     ss.focused_idx,
		hovered_idx:     ss.hovered_idx,
		pressed_idx:     ss.gesture.pressed_idx,
		cursor_state:    std::mem::take( &mut ss.cursor_state ),
		selection_anchor: std::mem::take( &mut ss.selection_anchor ),
		widget_rects:    Vec::new(),
		debug_layout,
		scroll_offsets:  std::mem::take( &mut ss.scroll_offsets ),
		scroll_rects:    Vec::new(),
		scroll_canvases: std::mem::take( &mut ss.scroll_canvases ),
		scroll_navigable_items: std::mem::take( &mut ss.scroll_navigable_items ),
		previous_widget_rects: ss.widget_rects.clone(),
		accessible_extras: Vec::new(),
		live_depth: 0,
	};
	layout_and_draw::<Msg>( view, canvas, screen_rect, &mut ctx, 0 );

	if ctx.debug_layout
	{
		for w in &ctx.widget_rects
		{
			canvas.stroke_rect( w.rect, Color::rgb( 1.0, 0.0, 0.0 ), 1.5, 0.0 );
		}
	}

	if let Some( ref menu ) = ss.context_menu
	{
		super::draw_context_menu( canvas, menu );
	}

	// Fallback-theme warning. Painted last so it sits on top of every
	// widget; no-op when the real theme is loaded.
	draw_fallback_banner( canvas, pw, sf );

	let damage_rects = if ss.content_dirty
	{
		Vec::new()
	} else {
		compute_damage(
			&ss.widget_rects,
			&ctx.widget_rects,
			ss.prev_focused,
			ss.prev_hovered,
			ss.prev_pressed,
			ss.focused_idx,
			ss.hovered_idx,
			ss.gesture.pressed_idx,
			pw, ph,
		)
	};

	ss.prev_focused = ss.focused_idx;
	ss.prev_hovered = ss.hovered_idx;
	ss.prev_pressed = ss.gesture.pressed_idx;

	ss.widget_rects    = ctx.widget_rects;
	ss.scroll_rects    = ctx.scroll_rects;
	ss.scroll_canvases = std::mem::take( &mut ctx.scroll_canvases );
	ss.cursor_state    = ctx.cursor_state;
	ss.selection_anchor = ctx.selection_anchor;
	ss.scroll_offsets  = ctx.scroll_offsets;
	ss.accessible_extras = ctx.accessible_extras;
	ss.scroll_navigable_items = ctx.scroll_navigable_items;
	ss.content_dirty   = false;

	canvas.write_to_wayland_buf( canvas_buf, swap_rb );

	let wl_surface = ss.surface.wl_surface();
	buffer.attach_to( wl_surface ).expect( "attach" );

	if damage_rects.is_empty()
	{
		wl_surface.damage_buffer( 0, 0, pw as i32, ph as i32 );
	} else {
		for r in &damage_rects
		{
			wl_surface.damage_buffer( r.x as i32, r.y as i32, r.width as i32, r.height as i32 );
		}
	}

	apply_input_region( wl_surface, compositor, input_region, scale );
	// Request a frame callback BEFORE commit so the compositor schedules it
	// against this exact frame; sets `frame_pending` so the run loop won't
	// try to draw this surface again until the callback fires.
	request_frame( wl_surface );
	wl_surface.commit();
	ss.frame_pending = true;
}

/// Partial redraw: install a clip mask covering only `dirty_rects`, repaint
/// the background + title bar + widget tree under that mask (so unchanged
/// pixels from the previous frame stay), and damage Wayland with exactly
/// those rects.
pub( crate ) fn draw_surface_partial<Msg: Clone>(
	ss:            &mut SurfaceState<Msg>,
	compositor:    &CompositorState,
	view:          &Element<Msg>,
	bg:            Color,
	input_region:  Option<&[Rect]>,
	shm_format:    wl_shm::Format,
	swap_rb:       bool,
	dirty_rects:   Vec<Rect>,
	pw:            u32,
	ph:            u32,
	scale:         u32,
	request_frame: &dyn Fn( &WlSurface ),
)
{
	let Some( pool ) = ss.pool.as_mut() else { return };
	let stride = pw * 4;

	let ( buffer, canvas_buf ) = match pool.create_buffer(
		pw as i32, ph as i32, stride as i32, shm_format,
	)
	{
		Ok( r )  => r,
		Err( _ ) => return,
	};

	// Canvas pixels from the previous frame are still valid for the unclipped
	// region and serve as our background — do not call clear()/fill() here.
	let canvas = ss.canvas.as_mut().expect( "partial path requires existing canvas" );
	canvas.set_clip_rects( &dirty_rects );

	let sf          = scale as f32;
	let tb_h        = ss.titlebar_height * sf;
	let screen_rect = Rect { x: 0.0, y: tb_h, width: pw as f32, height: (ph as f32 - tb_h).max( 0.0 ) };

	// Repaint surface background under the clip so the dirty rects start from
	// a known state instead of leaking the previous widget's pixels.
	if bg.a > 0.0 { canvas.fill( bg ); }
	else
	{
		canvas.clear_rects_transparent( &dirty_rects );
	}

	ss.titlebar_close_rect = draw_titlebar( canvas, &ss.titlebar_title, pw, tb_h, sf );

	let mut ctx: DrawCtx<Msg> = DrawCtx
	{
		focused_idx:     ss.focused_idx,
		hovered_idx:     ss.hovered_idx,
		pressed_idx:     ss.gesture.pressed_idx,
		cursor_state:    std::mem::take( &mut ss.cursor_state ),
		selection_anchor: std::mem::take( &mut ss.selection_anchor ),
		widget_rects:    Vec::new(),
		debug_layout:    false,
		scroll_offsets:  std::mem::take( &mut ss.scroll_offsets ),
		scroll_rects:    Vec::new(),
		scroll_canvases: std::mem::take( &mut ss.scroll_canvases ),
		scroll_navigable_items: std::mem::take( &mut ss.scroll_navigable_items ),
		previous_widget_rects: ss.widget_rects.clone(),
		accessible_extras: Vec::new(),
		live_depth: 0,
	};
	layout_and_draw::<Msg>( view, canvas, screen_rect, &mut ctx, 0 );

	if let Some( ref menu ) = ss.context_menu
	{
		super::draw_context_menu( canvas, menu );
	}

	// Fallback-theme warning. Painted last so it sits on top of every
	// widget; no-op when the real theme is loaded. If the banner's row
	// happens to fall outside the partial-redraw clip, the previous
	// frame's banner is still on the canvas (pixmap preserved), which
	// is the correct behaviour.
	draw_fallback_banner( canvas, pw, sf );

	canvas.clear_clip();

	ss.prev_focused = ss.focused_idx;
	ss.prev_hovered = ss.hovered_idx;
	ss.prev_pressed = ss.gesture.pressed_idx;

	ss.widget_rects    = ctx.widget_rects;
	ss.scroll_rects    = ctx.scroll_rects;
	ss.scroll_canvases = std::mem::take( &mut ctx.scroll_canvases );
	ss.cursor_state    = ctx.cursor_state;
	ss.selection_anchor = ctx.selection_anchor;
	ss.scroll_offsets  = ctx.scroll_offsets;
	ss.accessible_extras = ctx.accessible_extras;
	ss.scroll_navigable_items = ctx.scroll_navigable_items;
	ss.content_dirty   = false;

	canvas.write_to_wayland_buf( canvas_buf, swap_rb );

	let wl_surface = ss.surface.wl_surface();
	buffer.attach_to( wl_surface ).expect( "attach" );

	for r in &dirty_rects
	{
		wl_surface.damage_buffer( r.x as i32, r.y as i32, r.width as i32, r.height as i32 );
	}

	apply_input_region( wl_surface, compositor, input_region, scale );
	request_frame( wl_surface );
	wl_surface.commit();
	ss.frame_pending = true;
}