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
// What a frame changes against the frame its target kept, and what it draws
// for that.
use crate::{
frame::{ClearedImages, Damage, Held, OwnSake, PresentedImage, Repaint, SwapchainHistory},
frame_packet::{FramePacket, HeldWindow},
gpu_stats::{FrameDamage, FrameStats},
target_state::TargetDrawn,
};
// The targets a window frame keeps its pixels in.
pub(super) struct Keepers<'a> {
// The frame the composition target holds, when the renderer has one.
pub(super) composition: Option<&'a mut Option<Held>>,
pub(super) swapchain: &'a mut SwapchainHistory,
pub(super) cleared: &'a mut ClearedImages,
}
/// The kept frame a frame's damage is measured against.
#[derive(Clone, Copy)]
pub(super) enum Keeper {
/// The renderer's composition target.
Composition,
/// A swapchain image.
Image(PresentedImage),
/// The frame drawn last into an image that comes cleared; `keeping`
/// when the composition target takes a copy of this frame to keep.
Cleared { keeping: bool },
}
/// What a frame changes against the frame its target kept, what it draws
/// for that, and which target kept it.
#[derive(Clone, Copy)]
pub(super) struct FrameDamagePlan {
pub(super) damage: Damage,
pub(super) repaint: Repaint,
pub(super) keeper: Keeper,
/// The frame as its target holds it once drawn.
pub(super) held: Held,
/// Whether the frame draws whole as the first of its target's frames
/// whose frame before is known.
pub(super) baseline: bool,
// Why the frame draws whole for the target's sake.
pub(super) own_sake: Option<OwnSake>,
}
impl FrameDamagePlan {
// The frame as its target holds it once drawn; while it may show
// `stand_ins`, with the parts it drew marked for the next frame to draw
// again, the whole window when it drew whole.
pub(super) fn held_with(self, stand_ins: bool) -> Held {
match self.repaint {
Repaint::Window(rects) if stand_ins => self.held.with_stand_ins(Damage::Part(rects)),
Repaint::Whole if stand_ins => self.held.with_stand_ins(Damage::Full),
_ => self.held,
}
}
// Keeps the frame of the plan in its target as `held`, none when it did
// not draw there; returns the oldest packet the window's next frame
// finds in its target with the parts it draws again, `None` when a
// target holds none known.
pub(super) fn keep(self, held: Option<Held>, keepers: Keepers<'_>) -> Option<HeldWindow> {
let Keepers {
composition,
swapchain,
cleared,
} = keepers;
match self.keeper {
Keeper::Composition => {
let kept = composition?;
*kept = held;
held.map(Held::window)
}
Keeper::Image(image) => {
swapchain.record(image, held);
swapchain.held()
}
Keeper::Cleared { keeping } => {
cleared.kept = held;
let kept = composition?;
if keeping {
cleared.hand_over(kept);
}
cleared.copied.then_some(*kept).flatten().map(Held::window)
}
}
}
// How `packet` went into the target the plan draws it into.
pub(super) fn drawn(self, packet: &FramePacket) -> TargetDrawn {
TargetDrawn {
damage: self.damage,
skipped: packet.skipped,
knew_before: !self.baseline,
own_sake: self.own_sake,
}
}
}
// Whether `plan` draws only where `packet` holds every draw.
pub(super) fn admits(plan: Option<&FrameDamagePlan>, packet: &FramePacket) -> bool {
packet
.culled
.is_none_or(|region| plan.is_some_and(|plan| plan.repaint.within(®ion)))
}
// Counts what a frame of `plan` changes and draws of a page of `viewport`
// in `stats`, and returns what it draws.
pub(super) fn count_damage(
plan: Option<FrameDamagePlan>,
viewport: (u32, u32),
stats: &FrameStats,
) -> Repaint {
stats
.damage
.set(plan.map_or(FrameDamage::Untracked, |plan| plan.damage.stats()));
let repaint = plan.map_or(Repaint::Whole, |plan| plan.repaint);
stats.repainted_pixels.set(repaint.pixels(viewport));
repaint
}