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 dom_struct::dom_struct;
8use script_bindings::weakref::WeakRef;
9use servo_canvas_traits::webgl::{WebGLCommand, WebGLSyncId, webgl_channel};
10
11use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants as constants;
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::dom::webglrenderingcontext::capture_webgl_backtrace;
18use crate::script_runtime::CanGc;
19
20#[derive(JSTraceable, MallocSizeOf)]
21struct DroppableWebGLSync {
22    context: WeakRef<WebGLRenderingContext>,
23    #[no_trace]
24    sync_id: WebGLSyncId,
25    marked_for_deletion: Cell<bool>,
26}
27
28impl DroppableWebGLSync {
29    fn send_with_fallibility(&self, command: WebGLCommand, fallibility: Operation) {
30        if let Some(root) = self.context.root() {
31            let result = root.sender().send(command, capture_webgl_backtrace());
32            if matches!(fallibility, Operation::Infallible) {
33                result.expect("Operation failed");
34            }
35        }
36    }
37
38    fn delete(&self, operation_fallibility: Operation) {
39        if self.is_valid() {
40            self.marked_for_deletion.set(true);
41            self.send_with_fallibility(
42                WebGLCommand::DeleteSync(self.sync_id),
43                operation_fallibility,
44            );
45        }
46    }
47
48    fn is_valid(&self) -> bool {
49        !self.marked_for_deletion.get()
50    }
51}
52
53impl Drop for DroppableWebGLSync {
54    fn drop(&mut self) {
55        self.delete(Operation::Fallible);
56    }
57}
58
59#[dom_struct(associated_memory)]
60pub(crate) struct WebGLSync {
61    webgl_object: WebGLObject,
62    client_wait_status: Cell<Option<u32>>,
63    sync_status: Cell<Option<u32>>,
64    droppable: DroppableWebGLSync,
65}
66
67impl WebGLSync {
68    fn new_inherited(context: &WebGLRenderingContext, sync_id: WebGLSyncId) -> Self {
69        Self {
70            webgl_object: WebGLObject::new_inherited(context),
71            client_wait_status: Cell::new(None),
72            sync_status: Cell::new(None),
73            droppable: DroppableWebGLSync {
74                context: WeakRef::new(context),
75                sync_id,
76                marked_for_deletion: Cell::new(false),
77            },
78        }
79    }
80
81    pub(crate) fn new(context: &WebGLRenderingContext, can_gc: CanGc) -> DomRoot<Self> {
82        let (sender, receiver) = webgl_channel().unwrap();
83        context.send_command(WebGLCommand::FenceSync(sender));
84        let sync_id = receiver.recv().unwrap();
85
86        reflect_dom_object(
87            Box::new(WebGLSync::new_inherited(context, sync_id)),
88            &*context.global(),
89            can_gc,
90        )
91    }
92}
93
94impl WebGLSync {
95    pub(crate) fn client_wait_sync(
96        &self,
97        context: &WebGLRenderingContext,
98        flags: u32,
99        timeout: u64,
100    ) -> Option<u32> {
101        match self.client_wait_status.get() {
102            Some(constants::TIMEOUT_EXPIRED) | Some(constants::WAIT_FAILED) | None => {
103                let this = Trusted::new(self);
104                let context = Trusted::new(context);
105                let task = task!(request_client_wait_status: move || {
106                    let this = this.root();
107                    let context = context.root();
108                    let (sender, receiver) = webgl_channel().unwrap();
109                    context.send_command(WebGLCommand::ClientWaitSync(
110                        this.id(),
111                        flags,
112                        timeout,
113                        sender,
114                    ));
115                    this.client_wait_status.set(Some(receiver.recv().unwrap()));
116                });
117                self.global()
118                    .task_manager()
119                    .dom_manipulation_task_source()
120                    .queue(task);
121            },
122            _ => {},
123        }
124        self.client_wait_status.get()
125    }
126
127    pub(crate) fn delete(&self, operation_fallibility: Operation) {
128        self.droppable.delete(operation_fallibility);
129    }
130
131    pub(crate) fn get_sync_status(
132        &self,
133        pname: u32,
134        context: &WebGLRenderingContext,
135    ) -> Option<u32> {
136        match self.sync_status.get() {
137            Some(constants::UNSIGNALED) | None => {
138                let this = Trusted::new(self);
139                let context = Trusted::new(context);
140                let task = task!(request_sync_status: move || {
141                    let this = this.root();
142                    let context = context.root();
143                    let (sender, receiver) = webgl_channel().unwrap();
144                    context.send_command(WebGLCommand::GetSyncParameter(this.id(), pname, sender));
145                    this.sync_status.set(Some(receiver.recv().unwrap()));
146                });
147                self.global()
148                    .task_manager()
149                    .dom_manipulation_task_source()
150                    .queue(task);
151            },
152            _ => {},
153        }
154        self.sync_status.get()
155    }
156
157    pub(crate) fn is_valid(&self) -> bool {
158        self.droppable.is_valid()
159    }
160
161    pub(crate) fn id(&self) -> WebGLSyncId {
162        self.droppable.sync_id
163    }
164}