script/dom/webgl/
webglsync.rs1use std::cell::Cell;
6
7use canvas_traits::webgl::{WebGLCommand, WebGLSyncId, webgl_channel};
8use dom_struct::dom_struct;
9
10use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
11use crate::dom::bindings::inheritance::Castable;
12use crate::dom::bindings::refcounted::Trusted;
13use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::webgl::webglobject::WebGLObject;
16use crate::dom::webgl::webglrenderingcontext::{Operation, WebGLRenderingContext};
17use crate::script_runtime::CanGc;
18
19#[dom_struct]
20pub(crate) struct WebGLSync {
21 webgl_object: WebGLObject,
22 #[no_trace]
23 sync_id: WebGLSyncId,
24 marked_for_deletion: Cell<bool>,
25 client_wait_status: Cell<Option<u32>>,
26 sync_status: Cell<Option<u32>>,
27}
28
29impl WebGLSync {
30 fn new_inherited(context: &WebGLRenderingContext, sync_id: WebGLSyncId) -> Self {
31 Self {
32 webgl_object: WebGLObject::new_inherited(context),
33 sync_id,
34 marked_for_deletion: Cell::new(false),
35 client_wait_status: Cell::new(None),
36 sync_status: Cell::new(None),
37 }
38 }
39
40 pub(crate) fn new(context: &WebGLRenderingContext, can_gc: CanGc) -> DomRoot<Self> {
41 let (sender, receiver) = webgl_channel().unwrap();
42 context.send_command(WebGLCommand::FenceSync(sender));
43 let sync_id = receiver.recv().unwrap();
44
45 reflect_dom_object(
46 Box::new(WebGLSync::new_inherited(context, sync_id)),
47 &*context.global(),
48 can_gc,
49 )
50 }
51}
52
53impl WebGLSync {
54 pub(crate) fn client_wait_sync(
55 &self,
56 context: &WebGLRenderingContext,
57 flags: u32,
58 timeout: u64,
59 ) -> Option<u32> {
60 match self.client_wait_status.get() {
61 Some(constants::TIMEOUT_EXPIRED) | Some(constants::WAIT_FAILED) | None => {
62 let this = Trusted::new(self);
63 let context = Trusted::new(context);
64 let task = task!(request_client_wait_status: move || {
65 let this = this.root();
66 let context = context.root();
67 let (sender, receiver) = webgl_channel().unwrap();
68 context.send_command(WebGLCommand::ClientWaitSync(
69 this.sync_id,
70 flags,
71 timeout,
72 sender,
73 ));
74 this.client_wait_status.set(Some(receiver.recv().unwrap()));
75 });
76 self.global()
77 .task_manager()
78 .dom_manipulation_task_source()
79 .queue(task);
80 },
81 _ => {},
82 }
83 self.client_wait_status.get()
84 }
85
86 pub(crate) fn delete(&self, operation_fallibility: Operation) {
87 if self.is_valid() {
88 self.marked_for_deletion.set(true);
89 let context = self.upcast::<WebGLObject>().context();
90 let cmd = WebGLCommand::DeleteSync(self.sync_id);
91 match operation_fallibility {
92 Operation::Fallible => context.send_command_ignored(cmd),
93 Operation::Infallible => context.send_command(cmd),
94 }
95 }
96 }
97
98 pub(crate) fn get_sync_status(
99 &self,
100 pname: u32,
101 context: &WebGLRenderingContext,
102 ) -> Option<u32> {
103 match self.sync_status.get() {
104 Some(constants::UNSIGNALED) | None => {
105 let this = Trusted::new(self);
106 let context = Trusted::new(context);
107 let task = task!(request_sync_status: move || {
108 let this = this.root();
109 let context = context.root();
110 let (sender, receiver) = webgl_channel().unwrap();
111 context.send_command(WebGLCommand::GetSyncParameter(this.sync_id, pname, sender));
112 this.sync_status.set(Some(receiver.recv().unwrap()));
113 });
114 self.global()
115 .task_manager()
116 .dom_manipulation_task_source()
117 .queue(task);
118 },
119 _ => {},
120 }
121 self.sync_status.get()
122 }
123
124 pub(crate) fn is_valid(&self) -> bool {
125 !self.marked_for_deletion.get()
126 }
127
128 pub(crate) fn id(&self) -> WebGLSyncId {
129 self.sync_id
130 }
131}
132
133impl Drop for WebGLSync {
134 fn drop(&mut self) {
135 self.delete(Operation::Fallible);
136 }
137}