-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscale.rs
More file actions
423 lines (381 loc) · 13.9 KB
/
Copy pathscale.rs
File metadata and controls
423 lines (381 loc) · 13.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
use std::borrow::Cow;
use anyhow::{bail, Context, Result};
use ironrdp_server::PixelFormat;
use crate::display::geometry::{PresentationGeometry, Size};
use crate::input::OutputLayoutSnapshot;
pub(super) struct PreparedPresentationFrame<'a> {
pub(super) data: Cow<'a, [u8]>,
pub(super) width: u32,
pub(super) height: u32,
pub(super) stride: u32,
pub(super) damage_regions: Vec<(i32, i32, i32, i32)>,
pub(super) geometry_generation: u32,
}
pub(super) struct PresentationGenerationAction {
pub(super) refresh_processor: bool,
pub(super) next_generation: u32,
pub(super) damage_regions: Vec<(i32, i32, i32, i32)>,
}
pub(super) fn prepare_presentation_frame<'a>(
source_data: &'a [u8],
source_width: u32,
source_height: u32,
source_stride: u32,
pixel_format: PixelFormat,
source_damage_regions: &[(i32, i32, i32, i32)],
snapshot: &OutputLayoutSnapshot,
) -> Result<PreparedPresentationFrame<'a>> {
let source = Size::new(source_width, source_height).context("invalid source size")?;
let presentation = snapshot.presentation_geometry.presentation();
let geometry = if snapshot.presentation_geometry.source() == source {
snapshot.presentation_geometry
} else {
PresentationGeometry::new(source, presentation)
};
let damage_regions = geometry.map_source_damage(source_damage_regions);
if geometry.is_identity() {
let min_len = source_stride
.checked_mul(source_height)
.and_then(|len| usize::try_from(len).ok())
.context("source frame size overflow")?;
if source_data.len() < min_len {
bail!(
"source frame too small: got {} bytes, need {}",
source_data.len(),
min_len
);
}
return Ok(PreparedPresentationFrame {
data: Cow::Borrowed(source_data),
width: source_width,
height: source_height,
stride: source_stride,
damage_regions,
geometry_generation: snapshot.geometry_generation,
});
}
let stride = presentation
.width
.checked_mul(4)
.context("presentation stride overflow")?;
let len = stride
.checked_mul(presentation.height)
.and_then(|len| usize::try_from(len).ok())
.context("presentation frame size overflow")?;
let mut output = vec![0; len];
fill_black_bars(&mut output, pixel_format);
copy_scaled_visible_rect(source_data, source_stride, &mut output, stride, geometry)?;
Ok(PreparedPresentationFrame {
data: Cow::Owned(output),
width: presentation.width,
height: presentation.height,
stride,
damage_regions,
geometry_generation: snapshot.geometry_generation,
})
}
pub(super) fn dmabuf_zero_copy_allowed(snapshot: &OutputLayoutSnapshot) -> bool {
snapshot.presentation_geometry.is_identity()
}
pub(super) fn output_downscaling_generation_action(
current_generation: u32,
prepared: &PreparedPresentationFrame<'_>,
) -> PresentationGenerationAction {
if prepared.geometry_generation != current_generation {
PresentationGenerationAction {
refresh_processor: true,
next_generation: prepared.geometry_generation,
damage_regions: vec![(0, 0, prepared.width as i32, prepared.height as i32)],
}
} else {
PresentationGenerationAction {
refresh_processor: false,
next_generation: current_generation,
damage_regions: prepared.damage_regions.clone(),
}
}
}
pub(super) fn presentation_frame_shape(
source_width: u32,
source_height: u32,
source_stride: u32,
snapshot: &OutputLayoutSnapshot,
) -> Result<(u32, u32, u32)> {
let source = Size::new(source_width, source_height).context("invalid source size")?;
let presentation = snapshot.presentation_geometry.presentation();
let geometry = if snapshot.presentation_geometry.source() == source {
snapshot.presentation_geometry
} else {
PresentationGeometry::new(source, presentation)
};
if geometry.is_identity() {
Ok((source_width, source_height, source_stride))
} else {
let stride = presentation
.width
.checked_mul(4)
.context("presentation stride overflow")?;
Ok((presentation.width, presentation.height, stride))
}
}
fn fill_black_bars(output: &mut [u8], pixel_format: PixelFormat) {
for pixel in output.chunks_exact_mut(4) {
pixel[0] = 0;
pixel[1] = 0;
pixel[2] = 0;
pixel[3] = 0;
match pixel_format {
PixelFormat::ARgb32 | PixelFormat::ABgr32 => pixel[0] = 255,
PixelFormat::BgrA32 | PixelFormat::RgbA32 => pixel[3] = 255,
PixelFormat::XRgb32
| PixelFormat::XBgr32
| PixelFormat::BgrX32
| PixelFormat::RgbX32 => {}
}
}
}
fn copy_scaled_visible_rect(
source_data: &[u8],
source_stride: u32,
output: &mut [u8],
output_stride: u32,
geometry: PresentationGeometry,
) -> Result<()> {
let source = geometry.source();
let visible = geometry.visible_rect();
let source_stride = usize::try_from(source_stride).context("source stride out of range")?;
let output_stride = usize::try_from(output_stride).context("output stride out of range")?;
let source_required = source_stride
.checked_mul(source.height as usize)
.context("source frame size overflow")?;
if source_data.len() < source_required {
bail!(
"source frame too small: got {} bytes, need {}",
source_data.len(),
source_required
);
}
if source_stride < source.width as usize * 4 {
bail!(
"source stride {} too small for width {}",
source_stride,
source.width
);
}
let source_w = u64::from(source.width);
let source_h = u64::from(source.height);
let visible_w = u64::from(visible.width);
let visible_h = u64::from(visible.height);
// Area-average box filter: every output pixel averages the source region
// it covers, so downscaling anti-aliases instead of dropping pixels the
// way nearest sampling does (visible as jagged text on 4K -> window-size
// presentations). For upscaling the box degenerates to a single source
// pixel, matching the previous behavior.
for py in visible.y..visible.bottom() {
let rel_y = u64::from(py - visible.y);
let sy0 = (rel_y * source_h) / visible_h;
let sy1 = (((rel_y + 1) * source_h) / visible_h)
.max(sy0 + 1)
.min(source_h);
let (sy0, sy1) = (sy0 as usize, sy1 as usize);
for px in visible.x..visible.right() {
let rel_x = u64::from(px - visible.x);
let sx0 = (rel_x * source_w) / visible_w;
let sx1 = (((rel_x + 1) * source_w) / visible_w)
.max(sx0 + 1)
.min(source_w);
let (sx0, sx1) = (sx0 as usize, sx1 as usize);
let mut sum = [0u64; 4];
for sy in sy0..sy1 {
let row = sy * source_stride;
for sx in sx0..sx1 {
let s = row + sx * 4;
sum[0] += u64::from(source_data[s]);
sum[1] += u64::from(source_data[s + 1]);
sum[2] += u64::from(source_data[s + 2]);
sum[3] += u64::from(source_data[s + 3]);
}
}
let count = ((sy1 - sy0) * (sx1 - sx0)) as u64;
let output_offset = usize::try_from(py)
.ok()
.and_then(|row| row.checked_mul(output_stride))
.and_then(|row| row.checked_add(usize::try_from(px).ok()?.checked_mul(4)?))
.context("output pixel offset overflow")?;
let half = count / 2;
output[output_offset] = ((sum[0] + half) / count) as u8;
output[output_offset + 1] = ((sum[1] + half) / count) as u8;
output[output_offset + 2] = ((sum[2] + half) / count) as u8;
output[output_offset + 3] = ((sum[3] + half) / count) as u8;
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::display::geometry::PresentationGeometry;
fn snapshot(source: (u32, u32), presentation: (u32, u32)) -> OutputLayoutSnapshot {
let source_size = Size::new(source.0, source.1).unwrap();
let presentation_size = Size::new(presentation.0, presentation.1).unwrap();
OutputLayoutSnapshot {
output_name: "DP-1".into(),
output_w: source.0,
output_h: source.1,
layout_extent_w: source.0,
layout_extent_h: source.1,
output_offset_x: 0,
output_offset_y: 0,
presentation_geometry: PresentationGeometry::new(source_size, presentation_size),
geometry_generation: 7,
}
}
fn frame(width: usize, height: usize, stride: usize) -> Vec<u8> {
let mut data = vec![0; stride * height];
for y in 0..height {
for x in 0..width {
let offset = y * stride + x * 4;
data[offset] = x as u8;
data[offset + 1] = y as u8;
data[offset + 2] = 0x80;
data[offset + 3] = 0xff;
}
}
data
}
#[test]
fn capture_scale_keeps_identity_frame_borrowed_and_damage_identity() {
let source = frame(4, 2, 24);
let prepared = prepare_presentation_frame(
&source,
4,
2,
24,
PixelFormat::BgrA32,
&[(1, 0, 2, 1)],
&snapshot((4, 2), (4, 2)),
)
.expect("identity frame prepares");
assert!(matches!(prepared.data, Cow::Borrowed(_)));
assert_eq!(prepared.width, 4);
assert_eq!(prepared.height, 2);
assert_eq!(prepared.stride, 24);
assert_eq!(prepared.damage_regions, vec![(1, 0, 2, 1)]);
assert_eq!(prepared.geometry_generation, 7);
}
#[test]
fn capture_scale_maps_source_frame_to_presentation_with_fallback_bars() {
let source = frame(4, 2, 16);
let prepared = prepare_presentation_frame(
&source,
4,
2,
16,
PixelFormat::BgrA32,
&[(0, 0, 4, 2)],
&snapshot((4, 2), (4, 4)),
)
.expect("scaled frame prepares");
assert!(matches!(prepared.data, Cow::Owned(_)));
assert_eq!(prepared.width, 4);
assert_eq!(prepared.height, 4);
assert_eq!(prepared.stride, 16);
assert_eq!(&prepared.data[0..4], &[0, 0, 0, 255]);
assert_eq!(&prepared.data[16..20], &[0, 0, 0x80, 0xff]);
assert_eq!(&prepared.data[48..52], &[0, 0, 0, 255]);
}
#[test]
fn capture_scale_downscale_averages_covered_source_pixels() {
// 2x1 black+white halves -> 1x1: the area filter must yield mid-gray;
// nearest sampling would return one of the extremes.
let mut source = vec![0u8; 8];
source[0..4].copy_from_slice(&[0, 0, 0, 255]);
source[4..8].copy_from_slice(&[255, 255, 255, 255]);
let prepared = prepare_presentation_frame(
&source,
2,
1,
8,
PixelFormat::BgrA32,
&[(0, 0, 2, 1)],
&snapshot((2, 1), (1, 1)),
)
.expect("scaled frame prepares");
assert_eq!(&prepared.data[0..4], &[128, 128, 128, 255]);
}
#[test]
fn capture_scale_maps_damage_before_frame_processor() {
let source = frame(4, 2, 16);
let prepared = prepare_presentation_frame(
&source,
4,
2,
16,
PixelFormat::BgrX32,
&[(1, 0, 1, 1)],
&snapshot((4, 2), (8, 4)),
)
.expect("scaled frame prepares");
assert_eq!(prepared.damage_regions, vec![(2, 0, 2, 2)]);
}
#[test]
fn dmabuf_scaled_output_guard_rejects_non_identity_geometry() {
assert!(dmabuf_zero_copy_allowed(&snapshot(
(1920, 1080),
(1920, 1080)
)));
assert!(!dmabuf_zero_copy_allowed(&snapshot(
(3840, 2160),
(1920, 1080)
)));
assert!(!dmabuf_zero_copy_allowed(&snapshot(
(1920, 1080),
(1024, 768)
)));
}
#[test]
fn capture_scale_reports_presentation_frame_shape_before_frame_processor() {
assert_eq!(
presentation_frame_shape(3840, 2160, 3840 * 4, &snapshot((3840, 2160), (1920, 1080)))
.expect("shape"),
(1920, 1080, 1920 * 4)
);
}
#[test]
fn output_downscaling_generation_forces_full_redraw_and_discards_stale_mapped_damage() {
let source = frame(4, 2, 16);
let prepared = prepare_presentation_frame(
&source,
4,
2,
16,
PixelFormat::BgrX32,
&[(1, 0, 1, 1)],
&snapshot((4, 2), (8, 4)),
)
.expect("scaled frame prepares");
assert_eq!(prepared.damage_regions, vec![(2, 0, 2, 2)]);
let action = output_downscaling_generation_action(6, &prepared);
assert!(action.refresh_processor);
assert_eq!(action.next_generation, 7);
assert_eq!(action.damage_regions, vec![(0, 0, 8, 4)]);
}
#[test]
fn output_downscaling_generation_reuses_current_generation_damage_without_incrementing() {
let source = frame(4, 2, 16);
let prepared = prepare_presentation_frame(
&source,
4,
2,
16,
PixelFormat::BgrX32,
&[(1, 0, 1, 1)],
&snapshot((4, 2), (8, 4)),
)
.expect("scaled frame prepares");
let action = output_downscaling_generation_action(7, &prepared);
assert!(!action.refresh_processor);
assert_eq!(action.next_generation, 7);
assert_eq!(action.damage_regions, vec![(2, 0, 2, 2)]);
}
}