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
use super::{
Capture, Damage, FrameDamages, FrameWindow, Held, PresentedImage, Since,
captures::{collect, widen},
};
use crate::frame_packet::HeldWindow;
/// The images of a swapchain the history follows.
const IMAGES: usize = 8;
/// The frames a swapchain's images hold: a frame drawn into an image that
/// holds an older frame draws the union of the damages since that frame.
#[derive(Default)]
pub(crate) struct SwapchainHistory {
surface_epoch: u64,
/// By image index, the frame the image holds.
held: [Option<Held>; IMAGES],
// By image index, a bit for each image the surface handed out.
seen: u8,
// How many images the surface hands out, and whether it handed one out
// a second time: until it handed out that many, or one twice, the next
// image may be one it never handed out, which holds no frame.
images: u32,
cycled: bool,
// The frames since the surface handed an image out a second time.
reused: u32,
/// The frame presented last, when it went into an image the history
/// follows and holds.
presented: Option<Held>,
// What the backdrops of the frame in progress read and write.
captures: Vec<Capture>,
}
impl SwapchainHistory {
/// Starts a frame on the surface of `surface_epoch`: a new surface's
/// images hold none of the frames before.
pub(crate) fn begin(&mut self, surface_epoch: u64) {
if surface_epoch != self.surface_epoch {
self.surface_epoch = surface_epoch;
self.forget();
}
}
// Forgets what the images hold.
pub(crate) fn forget(&mut self) {
self.held = [None; IMAGES];
self.seen = 0;
self.cycled = false;
self.reused = 0;
self.presented = None;
}
// Whether the surface may still hand out an image that holds no frame
// the history follows: one it never handed out, or one that holds none.
// A surface may hand an image out again before it handed out all of its
// images, as a Metal layer that makes its drawables as frames need them
// does: the history waits for the rest for as many frames as the
// surface holds images.
pub(crate) fn may_hand_out_unknown(&self) -> bool {
let images = self.images.min(u8::BITS);
let unseen = images == 0 || self.seen.count_ones() < images;
let unknown = self
.held
.iter()
.enumerate()
.any(|(index, held)| self.seen & (1 << index) != 0 && held.is_none());
(unseen || unknown) && self.reused < images.max(1)
}
// Whether `image` comes holding no frame the history follows.
pub(crate) fn holds_none(&self, image: PresentedImage) -> bool {
!image.kept
|| self
.held
.get(image.index as usize)
.copied()
.flatten()
.is_none()
}
// The oldest frame the images the surface handed out hold, and the
// parts of their frames drawn with stand-ins; `None` while one of them
// holds none known, or while the next image may be one the surface never
// handed out.
pub(crate) fn held(&self) -> Option<HeldWindow> {
if !self.cycled && self.seen.count_ones() < self.images.min(u8::BITS) {
return None;
}
let mut window: Option<HeldWindow> = None;
for (index, held) in self.held.iter().enumerate() {
if self.seen & (1 << index) == 0 {
continue;
}
let image = held.as_ref()?.window();
match &mut window {
Some(window) => {
window.oldest = window.oldest.min(image.oldest);
if !image.stand_ins.is_none() {
if window.stand_ins.is_none() {
window.landings = image.landings;
} else if window.landings != image.landings {
window.landings = None;
}
}
match (&mut window.stand_ins, image.stand_ins) {
(Damage::Part(parts), Damage::Part(more)) => parts.add_all(&more),
(stand_ins, _) => *stand_ins = Damage::Full,
}
}
None => window = Some(image),
}
}
window
}
fn see(&mut self, image: PresentedImage) {
self.images = image.images;
if let Some(bit) = 1u8.checked_shl(image.index) {
self.cycled |= self.seen & bit != 0;
self.seen |= bit;
}
if self.cycled {
self.reused = self.reused.saturating_add(1);
}
}
// Whether `frame` draws what the surface shows: the frame presented last
// on the frame's surface, into an image the history follows, with
// nothing changed since. Such a frame needs no image and no present.
pub(crate) fn shows(&self, frame: &FrameWindow<'_>, damages: &FrameDamages) -> bool {
frame.packet.surface_epoch == self.surface_epoch
&& matches!(
damages.since(self.presented, frame).damage,
Damage::Part(changed) if changed.is_empty()
)
}
/// What `frame` changes against the frame presented last.
pub(crate) fn presented_damage(
&self,
frame: &FrameWindow<'_>,
damages: &FrameDamages,
) -> Since {
damages.since(self.presented, frame)
}
/// What `frame` changes in `image`: the union of the damages since the
/// frame the image holds, grown by what the frame's backdrops read and
/// write, as a backdrop over a pixel an earlier frame changed changes
/// what it writes; with the parts of that frame whose stand-ins it keeps.
pub(crate) fn changed_since(
&mut self,
image: PresentedImage,
frame: &FrameWindow<'_>,
damages: &FrameDamages,
) -> Since {
let held = self.held.get(image.index as usize).copied().flatten();
let since = damages.since(held.filter(|_| image.kept), frame);
let Damage::Part(mut rects) = since.damage else {
return since;
};
if rects.is_empty() || !frame.packet.root.contains_backdrop() {
return since;
}
self.captures.clear();
let place = (frame.packet.root_scale, frame.viewport);
let damage = if collect(&frame.packet.root, place, &mut self.captures) {
widen(&self.captures, &mut rects);
let mut damage = Damage::Part(rects);
damage.settle(frame.viewport);
damage
} else {
Damage::Full
};
Since { damage, ..since }
}
/// Ends the frame drawn into `image`, which holds `held`: none when the
/// frame did not draw there whole.
pub(crate) fn record(&mut self, image: PresentedImage, held: Option<Held>) {
self.see(image);
if let Some(slot) = self.held.get_mut(image.index as usize) {
*slot = held;
}
self.presented = held;
}
}