1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::rc::Rc;

use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use webgpu::identity::WebGPUOpResult;
use webgpu::{wgt, WebGPU, WebGPUQueue, WebGPURequest, WebGPUResponse};

use super::bindings::codegen::Bindings::WebGPUBinding::{GPUImageCopyTexture, GPUImageDataLayout};
use super::gpu::{response_async, AsyncWGPUListener};
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::WebGPUBinding::{
    GPUExtent3D, GPUQueueMethods, GPUSize64,
};
use crate::dom::bindings::codegen::UnionTypes::ArrayBufferViewOrArrayBuffer as BufferSource;
use crate::dom::bindings::error::{Error, Fallible};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::USVString;
use crate::dom::globalscope::GlobalScope;
use crate::dom::gpubuffer::{GPUBuffer, GPUBufferState};
use crate::dom::gpucommandbuffer::GPUCommandBuffer;
use crate::dom::gpuconvert::{
    convert_ic_texture, convert_image_data_layout, convert_texture_size_to_dict,
    convert_texture_size_to_wgt,
};
use crate::dom::gpudevice::GPUDevice;
use crate::dom::promise::Promise;

#[dom_struct]
pub struct GPUQueue {
    reflector_: Reflector,
    #[ignore_malloc_size_of = "defined in webgpu"]
    #[no_trace]
    channel: WebGPU,
    device: DomRefCell<Option<Dom<GPUDevice>>>,
    label: DomRefCell<USVString>,
    #[no_trace]
    queue: WebGPUQueue,
}

impl GPUQueue {
    fn new_inherited(channel: WebGPU, queue: WebGPUQueue) -> Self {
        GPUQueue {
            channel,
            reflector_: Reflector::new(),
            device: DomRefCell::new(None),
            label: DomRefCell::new(USVString::default()),
            queue,
        }
    }

    pub fn new(global: &GlobalScope, channel: WebGPU, queue: WebGPUQueue) -> DomRoot<Self> {
        reflect_dom_object(Box::new(GPUQueue::new_inherited(channel, queue)), global)
    }
}

impl GPUQueue {
    pub fn set_device(&self, device: &GPUDevice) {
        *self.device.borrow_mut() = Some(Dom::from_ref(device));
    }
}

impl GPUQueueMethods for GPUQueue {
    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
    fn Label(&self) -> USVString {
        self.label.borrow().clone()
    }

    /// <https://gpuweb.github.io/gpuweb/#dom-gpuobjectbase-label>
    fn SetLabel(&self, value: USVString) {
        *self.label.borrow_mut() = value;
    }

    /// <https://gpuweb.github.io/gpuweb/#dom-gpuqueue-submit>
    fn Submit(&self, command_buffers: Vec<DomRoot<GPUCommandBuffer>>) {
        let valid = command_buffers.iter().all(|cb| {
            cb.buffers()
                .iter()
                .all(|b| matches!(b.state(), GPUBufferState::Unmapped))
        });
        let scope_id = self.device.borrow().as_ref().unwrap().use_current_scope();
        if !valid {
            self.device.borrow().as_ref().unwrap().handle_server_msg(
                scope_id,
                WebGPUOpResult::ValidationError(String::from(
                    "Referenced GPUBuffer(s) are not Unmapped",
                )),
            );
            return;
        }
        let command_buffers = command_buffers.iter().map(|cb| cb.id().0).collect();
        self.channel
            .0
            .send((
                scope_id,
                WebGPURequest::Submit {
                    queue_id: self.queue.0,
                    command_buffers,
                },
            ))
            .unwrap();
    }

    /// <https://gpuweb.github.io/gpuweb/#dom-gpuqueue-writebuffer>
    #[allow(unsafe_code)]
    fn WriteBuffer(
        &self,
        buffer: &GPUBuffer,
        buffer_offset: GPUSize64,
        data: BufferSource,
        data_offset: GPUSize64,
        size: Option<GPUSize64>,
    ) -> Fallible<()> {
        let bytes = match data {
            BufferSource::ArrayBufferView(d) => d.to_vec(),
            BufferSource::ArrayBuffer(d) => d.to_vec(),
        };
        let content_size = if let Some(s) = size {
            s
        } else {
            bytes.len() as GPUSize64 - data_offset
        };
        let valid = data_offset + content_size <= bytes.len() as u64 &&
            buffer.state() == GPUBufferState::Unmapped &&
            content_size % wgt::COPY_BUFFER_ALIGNMENT == 0 &&
            buffer_offset % wgt::COPY_BUFFER_ALIGNMENT == 0;

        if !valid {
            return Err(Error::Operation);
        }

        let final_data = IpcSharedMemory::from_bytes(
            &bytes[data_offset as usize..(data_offset + content_size) as usize],
        );
        if let Err(e) = self.channel.0.send((
            self.device.borrow().as_ref().unwrap().use_current_scope(),
            WebGPURequest::WriteBuffer {
                queue_id: self.queue.0,
                buffer_id: buffer.id().0,
                buffer_offset,
                data: final_data,
            },
        )) {
            warn!("Failed to send WriteBuffer({:?}) ({})", buffer.id(), e);
            return Err(Error::Operation);
        }

        Ok(())
    }

    /// <https://gpuweb.github.io/gpuweb/#dom-gpuqueue-writetexture>
    fn WriteTexture(
        &self,
        destination: &GPUImageCopyTexture,
        data: BufferSource,
        data_layout: &GPUImageDataLayout,
        size: GPUExtent3D,
    ) -> Fallible<()> {
        let (bytes, len) = match data {
            BufferSource::ArrayBufferView(d) => (d.to_vec(), d.len() as u64),
            BufferSource::ArrayBuffer(d) => (d.to_vec(), d.len() as u64),
        };
        let valid = data_layout.offset <= len;

        if !valid {
            return Err(Error::Operation);
        }

        let texture_cv = convert_ic_texture(destination);
        let texture_layout = convert_image_data_layout(data_layout);
        let write_size = convert_texture_size_to_wgt(&convert_texture_size_to_dict(&size));
        let final_data = IpcSharedMemory::from_bytes(&bytes);

        if let Err(e) = self.channel.0.send((
            self.device.borrow().as_ref().unwrap().use_current_scope(),
            WebGPURequest::WriteTexture {
                queue_id: self.queue.0,
                texture_cv,
                data_layout: texture_layout,
                size: write_size,
                data: final_data,
            },
        )) {
            warn!(
                "Failed to send WriteTexture({:?}) ({})",
                destination.texture.id().0,
                e
            );
            return Err(Error::Operation);
        }

        Ok(())
    }

    /// <https://gpuweb.github.io/gpuweb/#dom-gpuqueue-onsubmittedworkdone>
    fn OnSubmittedWorkDone(&self) -> Rc<Promise> {
        let global = self.global();
        let promise = Promise::new(&global);
        let sender = response_async(&promise, self);
        if let Err(e) = self.channel.0.send((
            self.device.borrow().as_ref().unwrap().use_current_scope(),
            WebGPURequest::QueueOnSubmittedWorkDone {
                sender,
                queue_id: self.queue.0,
            },
        )) {
            warn!("QueueOnSubmittedWorkDone failed with {e}")
        }
        promise
    }
}

impl AsyncWGPUListener for GPUQueue {
    fn handle_response(
        &self,
        response: Option<Result<webgpu::WebGPUResponse, String>>,
        promise: &Rc<Promise>,
    ) {
        match response {
            Some(Ok(WebGPUResponse::SubmittedWorkDone)) => {
                promise.resolve_native(&());
            },
            _ => {
                warn!("GPUQueue received wrong WebGPUResponse");
                promise.reject_error(Error::Operation);
            },
        }
    }
}