Skip to content

Commit

Permalink
Auto merge of #27154 - kunalmohan:async-presentation, r=kvark
Browse files Browse the repository at this point in the history
WebGPU update presentation data asynchronously

<!-- Please describe your changes on the following line: -->
This PR aims to make updating Webrender presentation data non-blocking.
A callback is passed to the `buffer_map_async()` fn wherein the data is read and a message sent to the server itself to write the data into the shared `PresentationData` struct object.

r?@kvark

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: -->
- [X] `./mach build -d` does not report any errors
- [X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #___ (GitHub issue number if applicable)

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because ___

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
  • Loading branch information
bors-servo committed Jul 3, 2020
2 parents 4504eeb + b672b78 commit 39060ca
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 72 deletions.
5 changes: 3 additions & 2 deletions components/script/dom/gpubuffer.rs
Expand Up @@ -16,6 +16,7 @@ use crate::dom::promise::Promise;
use crate::realms::InRealm;
use crate::script_runtime::JSContext;
use dom_struct::dom_struct;
use ipc_channel::ipc::IpcSharedMemory;
use js::jsapi::DetachArrayBuffer;
use js::jsapi::NewExternalArrayBuffer;
use js::jsapi::{Heap, JSObject};
Expand Down Expand Up @@ -150,7 +151,7 @@ impl GPUBufferMethods for GPUBuffer {
let m_range = m_info.mapping_range.clone();
if let Err(e) = self.channel.0.send(WebGPURequest::UnmapBuffer {
buffer_id: self.id().0,
array_buffer: m_info.mapping.borrow().clone(),
array_buffer: IpcSharedMemory::from_bytes(m_info.mapping.borrow().as_slice()),
is_map_read: m_info.map_mode == Some(GPUMapModeConstants::READ),
offset: m_range.start,
size: m_range.end - m_range.start,
Expand Down Expand Up @@ -325,7 +326,7 @@ impl AsyncWGPUListener for GPUBuffer {
.as_mut()
.unwrap()
.mapping
.borrow_mut() = bytes;
.borrow_mut() = bytes.to_vec();
promise.resolve_native(&());
self.state.set(GPUBufferState::Mapped);
},
Expand Down
20 changes: 12 additions & 8 deletions components/script/dom/gpucanvascontext.rs
Expand Up @@ -18,12 +18,13 @@ use crate::dom::globalscope::GlobalScope;
use crate::dom::gpuswapchain::GPUSwapChain;
use crate::dom::htmlcanvaselement::{HTMLCanvasElement, LayoutCanvasRenderingContextHelpers};
use crate::dom::node::{document_from_node, Node, NodeDamage};
use arrayvec::ArrayVec;
use dom_struct::dom_struct;
use euclid::default::Size2D;
use ipc_channel::ipc;
use script_layout_interface::HTMLCanvasDataSource;
use std::cell::Cell;
use webgpu::{wgt, WebGPU, WebGPURequest};
use webgpu::{wgpu::id, wgt, WebGPU, WebGPURequest};

#[derive(Clone, Copy, Debug, Eq, Hash, MallocSizeOf, Ord, PartialEq, PartialOrd)]
pub struct WebGPUContextId(pub u64);
Expand Down Expand Up @@ -91,7 +92,6 @@ impl GPUCanvasContext {
external_id: self.context_id.0,
texture_id,
encoder_id,
image_key: self.webrender_image.get().unwrap(),
}) {
warn!(
"Failed to send UpdateWebrenderData({:?}) ({})",
Expand Down Expand Up @@ -130,11 +130,15 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
}
*self.swap_chain.borrow_mut() = None;

let buffer_id = self
.global()
.wgpu_id_hub()
.lock()
.create_buffer_id(descriptor.device.id().0.backend());
let mut buffer_ids = ArrayVec::<[id::BufferId; 5]>::new();
for _ in 0..5 {
buffer_ids.push(
self.global()
.wgpu_id_hub()
.lock()
.create_buffer_id(descriptor.device.id().0.backend()),
);
}

let image_desc = webrender_api::ImageDescriptor {
format: match descriptor.format {
Expand Down Expand Up @@ -166,7 +170,7 @@ impl GPUCanvasContextMethods for GPUCanvasContext {
.0
.send(WebGPURequest::CreateSwapChain {
device_id: descriptor.device.id().0,
buffer_id,
buffer_ids,
external_id: self.context_id.0,
sender,
image_desc,
Expand Down

0 comments on commit 39060ca

Please sign in to comment.