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
// The window packets a producer built last: what each changed, the one the
// next changes against, and the oldest one the window's targets hold, which
// a window walk culls against with the parts they draw again.
use cranpose_render_common::scene_store::SceneVersion;
use crate::{
frame::{Damage, DamageRects, FrameDamages, PacketDamage},
frame_packet::HeldWindow,
};
// What a packet draws: two packets of the same draw the same frame.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) struct PacketDraws {
pub(crate) scene: SceneVersion,
pub(crate) overlay: SceneVersion,
pub(crate) viewport: (u32, u32),
pub(crate) root_scale: u32,
pub(crate) transparent: bool,
}
#[derive(Default)]
pub(crate) struct WindowPackets {
damages: FrameDamages,
// The window packet built last, which the next one changes against, and
// what it drew: a capture of the same draws the same frame.
last: Option<(u64, PacketDraws)>,
// The damage of the window packet built last when it went back unshown:
// the next window packet changes against what it changed against.
carried: Option<PacketDamage>,
held: Option<HeldWindow>,
}
impl WindowPackets {
pub(crate) fn last(&self) -> Option<u64> {
self.last.map(|(frame, _)| frame)
}
// The damages since the oldest packet the window's targets hold, the
// parts they draw again at `landings` and the rects of `begun`: a region
// a window walk may cull to. `None` when one of the damages is not a
// part of the window.
pub(crate) fn cull_region(
&self,
begun: [DamageRects; 2],
landings: Option<u64>,
) -> Option<DamageRects> {
let mut region = self.held_since(self.last()?, landings)?;
for rects in &begun {
region.add_all(rects);
}
Some(region)
}
// The damages from `at` back to the oldest packet the window's targets
// hold and, once the count of landings moved past `landings`, the parts
// they drew with stand-ins, which a frame draws again then; `None` when
// one of those is not a part of the window. The renderer refuses a
// culled packet when a landing it counted first makes it draw more.
fn held_since(&self, at: u64, landings: Option<u64>) -> Option<DamageRects> {
let held = self.held?;
let mut region = self.damages.union_back(at, held.oldest).ok()?;
if held.landings.is_none() || held.landings != landings {
let Damage::Part(stand_ins) = held.stand_ins else {
return None;
};
region.add_all(&stand_ins);
}
Some(region)
}
// The damage of the next window packet, whose frame changes `damage` in
// a window of `viewport` against `since`: what an unshown packet changed
// against, when one went back.
pub(crate) fn next_damage(
&self,
damage: Damage,
since: Option<u64>,
viewport: (u32, u32),
) -> PacketDamage {
match self.carried {
Some(carried) => PacketDamage {
since: carried.since,
damage: carried.damage.union(damage, viewport),
},
None => PacketDamage { since, damage },
}
}
// What an image that holds the oldest frame the window's targets hold
// repaints for `packet`, the next window packet `next`, at `landings`,
// merged as the present side merges it: an image that holds a later
// frame repaints a part of it. `None` when it is not a part of the
// window.
pub(crate) fn repaint(
&mut self,
next: u64,
packet: PacketDamage,
landings: Option<u64>,
) -> Option<DamageRects> {
self.damages.note(next, packet);
self.held_since(next, landings)
}
// Notes the window packet `frame` built with `damage`, drawing `draws`.
pub(crate) fn built(&mut self, frame: u64, damage: PacketDamage, draws: PacketDraws) {
self.carried = None;
self.damages.note(frame, damage);
self.last = Some((frame, draws));
}
// The damage of a capture that draws `draws`: none when the window
// packet built last draws the same.
pub(crate) fn capture_damage(&self, draws: PacketDraws) -> PacketDamage {
match self.last {
Some((frame, last)) if last == draws => PacketDamage {
since: Some(frame),
damage: Damage::Part(DamageRects::default()),
},
_ => PacketDamage::WHOLE,
}
}
// Notes that the packet `frame`, which changed `damage`, went back
// unshown.
pub(crate) fn went_back(&mut self, frame: u64, damage: PacketDamage) {
if self.last() == Some(frame) {
self.carried = Some(damage);
}
}
// Notes the oldest window packet the window's targets hold, and the
// parts they draw again; `None` when one holds none known.
pub(crate) fn hold(&mut self, held: Option<HeldWindow>) {
self.held = held;
}
}