1use std::fmt::Debug;
6use std::num::NonZeroU32;
7use std::sync::atomic::{AtomicUsize, Ordering};
8
9use euclid::{Rect, Size2D};
10use malloc_size_of_derive::MallocSizeOf;
11use serde::{Deserialize, Serialize};
12use servo_base::id::PainterId;
13
14use crate::{Error, Viewport, Viewports};
15
16#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Deserialize, Serialize)]
17pub struct ContextId(pub u64);
18
19pub trait GLTypes {
20 type Device;
21 type Context;
22 type Bindings;
23}
24
25pub trait GLContexts<GL: GLTypes> {
26 fn device(&self, context_id: ContextId) -> Option<GL::Device>;
27 fn device_for_painter(&mut self, painter_id: PainterId) -> Option<GL::Device>;
30 fn bindings(&mut self, context_id: ContextId) -> Option<&GL::Bindings>;
31 fn context(&mut self, context_id: ContextId) -> Option<&mut GL::Context>;
32}
33
34impl GLTypes for () {
35 type Bindings = ();
36 type Device = ();
37 type Context = ();
38}
39
40impl GLContexts<()> for () {
41 fn device(&self, _: ContextId) -> Option<()> {
42 Some(())
43 }
44
45 fn device_for_painter(&mut self, _: PainterId) -> Option<()> {
46 Some(())
47 }
48
49 fn context(&mut self, _: ContextId) -> Option<&mut ()> {
50 Some(self)
51 }
52
53 fn bindings(&mut self, _: ContextId) -> Option<&()> {
54 Some(self)
55 }
56}
57
58pub trait LayerGrandManagerAPI<GL: GLTypes> {
59 fn create_layer_manager(&self, factory: LayerManagerFactory<GL>)
60 -> Result<LayerManager, Error>;
61
62 fn clone_layer_grand_manager(&self) -> LayerGrandManager<GL>;
63}
64
65pub struct LayerGrandManager<GL>(Box<dyn Send + LayerGrandManagerAPI<GL>>);
66
67impl<GL: GLTypes> Clone for LayerGrandManager<GL> {
68 fn clone(&self) -> Self {
69 self.0.clone_layer_grand_manager()
70 }
71}
72
73impl<GL> Debug for LayerGrandManager<GL> {
74 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
75 "LayerGrandManager(...)".fmt(fmt)
76 }
77}
78
79impl<GL: GLTypes> LayerGrandManager<GL> {
80 pub fn new<GM>(grand_manager: GM) -> LayerGrandManager<GL>
81 where
82 GM: 'static + Send + LayerGrandManagerAPI<GL>,
83 {
84 LayerGrandManager(Box::new(grand_manager))
85 }
86
87 pub fn create_layer_manager<F, M>(&self, factory: F) -> Result<LayerManager, Error>
88 where
89 F: 'static + Send + FnOnce(&mut dyn GLContexts<GL>) -> Result<M, Error>,
90 M: 'static + LayerManagerAPI<GL>,
91 {
92 self.0
93 .create_layer_manager(LayerManagerFactory::new(factory))
94 }
95}
96
97pub trait LayerManagerAPI<GL: GLTypes> {
98 fn create_layer(
99 &mut self,
100 contexts: &mut dyn GLContexts<GL>,
101 context_id: ContextId,
102 init: LayerInit,
103 ) -> Result<LayerId, Error>;
104
105 fn destroy_layer(
106 &mut self,
107 contexts: &mut dyn GLContexts<GL>,
108 context_id: ContextId,
109 layer_id: LayerId,
110 );
111
112 fn layers(&self) -> &[(ContextId, LayerId)];
113
114 fn begin_frame(
115 &mut self,
116 contexts: &mut dyn GLContexts<GL>,
117 layers: &[(ContextId, LayerId)],
118 ) -> Result<Vec<SubImages>, Error>;
119
120 fn end_frame(
121 &mut self,
122 contexts: &mut dyn GLContexts<GL>,
123 layers: &[(ContextId, LayerId)],
124 ) -> Result<(), Error>;
125}
126
127pub struct LayerManager(Box<dyn Send + LayerManagerAPI<()>>);
128
129impl Debug for LayerManager {
130 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
131 "LayerManager(...)".fmt(fmt)
132 }
133}
134
135impl LayerManager {
136 pub fn create_layer(
137 &mut self,
138 context_id: ContextId,
139 init: LayerInit,
140 ) -> Result<LayerId, Error> {
141 self.0.create_layer(&mut (), context_id, init)
142 }
143
144 pub fn destroy_layer(&mut self, context_id: ContextId, layer_id: LayerId) {
145 self.0.destroy_layer(&mut (), context_id, layer_id);
146 }
147
148 pub fn begin_frame(
149 &mut self,
150 layers: &[(ContextId, LayerId)],
151 ) -> Result<Vec<SubImages>, Error> {
152 self.0.begin_frame(&mut (), layers)
153 }
154
155 pub fn end_frame(&mut self, layers: &[(ContextId, LayerId)]) -> Result<(), Error> {
156 self.0.end_frame(&mut (), layers)
157 }
158}
159
160impl LayerManager {
161 pub fn new<M>(manager: M) -> LayerManager
162 where
163 M: 'static + Send + LayerManagerAPI<()>,
164 {
165 LayerManager(Box::new(manager))
166 }
167}
168
169impl Drop for LayerManager {
170 fn drop(&mut self) {
171 log::debug!("Dropping LayerManager");
172 let layers: Vec<_> = self.0.layers().to_vec();
173 for (context_id, layer_id) in layers.into_iter() {
174 self.destroy_layer(context_id, layer_id);
175 }
176 }
177}
178
179#[expect(clippy::type_complexity)]
180pub struct LayerManagerFactory<GL: GLTypes>(
181 Box<dyn Send + FnOnce(&mut dyn GLContexts<GL>) -> Result<Box<dyn LayerManagerAPI<GL>>, Error>>,
182);
183
184impl<GL: GLTypes> Debug for LayerManagerFactory<GL> {
185 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
186 "LayerManagerFactory(...)".fmt(fmt)
187 }
188}
189
190impl<GL: GLTypes> LayerManagerFactory<GL> {
191 pub fn new<F, M>(factory: F) -> LayerManagerFactory<GL>
192 where
193 F: 'static + Send + FnOnce(&mut dyn GLContexts<GL>) -> Result<M, Error>,
194 M: 'static + LayerManagerAPI<GL>,
195 {
196 LayerManagerFactory(Box::new(move |contexts| Ok(Box::new(factory(contexts)?))))
197 }
198
199 pub fn build(
200 self,
201 contexts: &mut dyn GLContexts<GL>,
202 ) -> Result<Box<dyn LayerManagerAPI<GL>>, Error> {
203 (self.0)(contexts)
204 }
205}
206
207#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Deserialize, Serialize, MallocSizeOf)]
208pub struct LayerId(usize);
209
210static NEXT_LAYER_ID: AtomicUsize = AtomicUsize::new(0);
211
212impl Default for LayerId {
213 fn default() -> Self {
214 LayerId(NEXT_LAYER_ID.fetch_add(1, Ordering::SeqCst))
215 }
216}
217
218#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
219pub enum LayerInit {
220 WebGLLayer {
222 antialias: bool,
223 depth: bool,
224 stencil: bool,
225 alpha: bool,
226 ignore_depth_values: bool,
227 framebuffer_scale_factor: f32,
228 },
229 ProjectionLayer {
231 depth: bool,
232 stencil: bool,
233 alpha: bool,
234 scale_factor: f32,
235 },
236 }
238
239impl LayerInit {
240 pub fn texture_size(&self, viewports: &Viewports) -> Size2D<i32, Viewport> {
241 match self {
242 LayerInit::WebGLLayer {
243 framebuffer_scale_factor: scale,
244 ..
245 } |
246 LayerInit::ProjectionLayer {
247 scale_factor: scale,
248 ..
249 } => {
250 let native_size = viewports
251 .viewports
252 .iter()
253 .fold(Rect::zero(), |acc, view| acc.union(view))
254 .size;
255 (native_size.to_f32() * *scale).to_i32()
256 },
257 }
258 }
259}
260
261#[derive(Clone, Copy, Debug, Deserialize, Serialize)]
263pub enum LayerLayout {
264 Mono,
267 StereoLeftRight,
269 StereoTopBottom,
271}
272
273#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
274pub struct SubImages {
275 pub layer_id: LayerId,
276 pub sub_image: Option<SubImage>,
277 pub view_sub_images: Vec<SubImage>,
278}
279
280#[derive(Clone, Debug, Deserialize, Serialize, MallocSizeOf)]
282pub struct SubImage {
283 pub color_texture: Option<NonZeroU32>,
284 pub depth_stencil_texture: Option<NonZeroU32>,
285 pub texture_array_index: Option<u32>,
286 pub viewport: Rect<i32, Viewport>,
287}