script/dom/webgl/
webglsync.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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            self.upcast().send_with_fallibility(
90                WebGLCommand::DeleteSync(self.sync_id),
91                operation_fallibility,
92            );
93        }
94    }
95
96    pub(crate) fn get_sync_status(
97        &self,
98        pname: u32,
99        context: &WebGLRenderingContext,
100    ) -> Option<u32> {
101        match self.sync_status.get() {
102            Some(constants::UNSIGNALED) | None => {
103                let this = Trusted::new(self);
104                let context = Trusted::new(context);
105                let task = task!(request_sync_status: move || {
106                    let this = this.root();
107                    let context = context.root();
108                    let (sender, receiver) = webgl_channel().unwrap();
109                    context.send_command(WebGLCommand::GetSyncParameter(this.sync_id, pname, sender));
110                    this.sync_status.set(Some(receiver.recv().unwrap()));
111                });
112                self.global()
113                    .task_manager()
114                    .dom_manipulation_task_source()
115                    .queue(task);
116            },
117            _ => {},
118        }
119        self.sync_status.get()
120    }
121
122    pub(crate) fn is_valid(&self) -> bool {
123        !self.marked_for_deletion.get()
124    }
125
126    pub(crate) fn id(&self) -> WebGLSyncId {
127        self.sync_id
128    }
129}
130
131impl Drop for WebGLSync {
132    fn drop(&mut self) {
133        self.delete(Operation::Fallible);
134    }
135}