1use super::*;
2
3impl ShowcaseWebPresentationPath {
5 #[must_use]
11 pub fn context_family(self) -> ShowcaseWebCanvasContextFamily {
12 match self {
13 Self::GpuDirect | Self::GpuUpload => ShowcaseWebCanvasContextFamily::WebGl2,
14 Self::Canvas2d => ShowcaseWebCanvasContextFamily::Canvas2d,
15 }
16 }
17
18 #[must_use]
24 pub fn uses_cpu_mirror(self) -> bool {
25 self != Self::GpuDirect
26 }
27}
28
29impl Default for ShowcaseWebDualCanvasCoordinator {
31 fn default() -> Self {
37 Self::with_initial_path(ShowcaseWebPresentationPath::GpuUpload, 1200, 800)
38 }
39}
40
41impl ShowcaseWebDualCanvasCoordinator {
43 #[must_use]
55 pub fn with_initial_path(
56 initial_path: ShowcaseWebPresentationPath,
57 width: u32,
58 height: u32,
59 ) -> Self {
60 let active: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
61 canvas_id: 1,
62 context_family: initial_path.context_family(),
63 path: initial_path,
64 visible: true,
65 z_index: 1,
66 width: width.max(1),
67 height: height.max(1),
68 scene_version: 0,
69 ready: true,
70 };
71 Self {
72 active,
73 standby: None,
74 cpu_mirror_active: initial_path.uses_cpu_mirror(),
75 timing_source: ShowcaseWebTimingSource::SubmissionProxy,
76 next_canvas_id: 2,
77 completed_swaps: 0,
78 aborted_swaps: 0,
79 raf_proxy_ticks: 0,
80 last_upload_bytes: 0,
81 retired_canvas_ids: Vec::new(),
82 }
83 }
84
85 #[must_use]
91 pub fn standby_slot(&self) -> Option<ShowcaseWebCanvasSlot> {
92 let Self { standby, .. } = self;
93 *standby
94 }
95
96 pub fn begin_switch(
108 &mut self,
109 target_path: ShowcaseWebPresentationPath,
110 width: u32,
111 height: u32,
112 ) -> bool {
113 let target_family: ShowcaseWebCanvasContextFamily = target_path.context_family();
114 if self.get_active().get_context_family() == target_family {
115 let mut active: ShowcaseWebCanvasSlot = self.get_active();
116 active.set_path(target_path);
117 active.set_width(width.max(1));
118 active.set_height(height.max(1));
119 self.set_active(active);
120 self.set_cpu_mirror_active(target_path.uses_cpu_mirror());
121 if !target_path.uses_cpu_mirror() {
122 self.set_last_upload_bytes(0);
123 }
124 return false;
125 }
126 let next_id: u64 = self.get_next_canvas_id();
127 self.set_next_canvas_id(next_id.saturating_add(1));
128 let slot: ShowcaseWebCanvasSlot = ShowcaseWebCanvasSlot {
129 canvas_id: next_id,
130 context_family: target_family,
131 path: target_path,
132 visible: false,
133 z_index: 0,
134 width: width.max(1),
135 height: height.max(1),
136 scene_version: 0,
137 ready: false,
138 };
139 let Self {
140 standby,
141 retired_canvas_ids,
142 ..
143 } = self;
144 if let Some(prev_standby) = standby.replace(slot) {
145 retired_canvas_ids.push(prev_standby.get_canvas_id());
146 }
147 true
148 }
149
150 pub fn mark_standby_ready(&mut self, width: u32, height: u32, scene_version: u64) -> bool {
162 let Self { standby, .. } = self;
163 if let Some(slot) = standby.as_mut() {
164 slot.set_width(width.max(1));
165 slot.set_height(height.max(1));
166 slot.set_scene_version(scene_version);
167 slot.set_ready(true);
168 true
169 } else {
170 false
171 }
172 }
173
174 pub fn commit_at_frame_boundary(
186 &mut self,
187 expected_width: u32,
188 expected_height: u32,
189 expected_scene_version: u64,
190 ) -> bool {
191 let matches_frame: bool = self
192 .standby_slot()
193 .map(|s: ShowcaseWebCanvasSlot| {
194 s.get_ready()
195 && s.get_width() == expected_width.max(1)
196 && s.get_height() == expected_height.max(1)
197 && s.get_scene_version() == expected_scene_version
198 })
199 .unwrap_or(false);
200 if !matches_frame {
201 self.abort_switch();
202 return false;
203 }
204 let Some(mut next_active) = ({
205 let Self { standby, .. } = self;
206 standby.take()
207 }) else {
208 return false;
209 };
210 let old_id: u64 = self.get_active().get_canvas_id();
211 next_active.set_visible(true);
212 next_active.set_z_index(1);
213 let uses_mirror: bool = next_active.get_path().uses_cpu_mirror();
214 self.set_active(next_active);
215 self.set_cpu_mirror_active(uses_mirror);
216 self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
217 if !uses_mirror {
218 self.set_last_upload_bytes(0);
219 }
220 self.set_completed_swaps(self.get_completed_swaps().saturating_add(1));
221 self.get_mut_retired_canvas_ids().push(old_id);
222 true
223 }
224
225 pub fn abort_switch(&mut self) {
227 let prev_standby: Option<ShowcaseWebCanvasSlot> = {
228 let Self { standby, .. } = self;
229 standby.take()
230 };
231 if let Some(dropped) = prev_standby {
232 self.get_mut_retired_canvas_ids()
233 .push(dropped.get_canvas_id());
234 self.set_aborted_swaps(self.get_aborted_swaps().saturating_add(1));
235 }
236 }
237
238 pub fn record_raf_proxy_tick(&mut self, upload_bytes: u64) {
244 self.set_timing_source(ShowcaseWebTimingSource::SubmissionProxy);
245 self.set_raf_proxy_ticks(self.get_raf_proxy_ticks().saturating_add(1));
246 if self.get_cpu_mirror_active() {
247 self.set_last_upload_bytes(upload_bytes);
248 } else {
249 self.set_last_upload_bytes(0);
250 }
251 }
252}