<!-- Please describe your changes on the following line: --> Info about the big picture and the goals of the WebGL refactor in this thread: https://groups.google.com/forum/#!topic/mozilla.dev.servo/0WMGz60kKzQ I tried to reduce this PR as much as possible as requested in the thread. I'll do separate PRs for other features (e.g.: Batch messages or use shared memory to improve frame times) or fixes. Some tips to ease the review process: - Most changes in DOM objects follow the same pattern (remove CanvasMsg wrapper and use the new sender method). - WebGLCommands are the same ones as before (moved from webrender_api). So those lines are already reviewed. - See WebGL traits in [components/canvas_traits/webgl.rs](https://github.com/servo/servo/pull/17891/files#diff-8701045d01505418701d0631d4d45562) - See WebGLThread and WR External Image bridge in [components/canvas/webgl_thread.rs](https://github.com/servo/servo/pull/17891/files#diff-281554879f39a2a041f7a69d442a5d2e) - The implementation submitted in this PR creates a single `WebGLThread` for all ScriptThread/Pipelines. See that in [components/canvas/webgl_mode/inprocess.rs](https://github.com/servo/servo/pull/17891/files#diff-250070c6c5a38c7f9fa0f5b3c101f68b) The conformance tests will help to guarantee that we don't miss anything. --- <!-- 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: --> - [x] 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. --> Source-Repo: https://github.com/servo/servo Source-Revision: 90f55ea4580e2a15f7d70d0491444f18b972d450
156 lines
4.8 KiB
Rust
156 lines
4.8 KiB
Rust
/* 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 http://mozilla.org/MPL/2.0/. */
|
|
|
|
// https://www.khronos.org/registry/webgl/specs/latest/1.0/webgl.idl
|
|
use canvas_traits::webgl::{WebGLBufferId, WebGLCommand, WebGLError, WebGLMsgSender, WebGLResult, WebGLVertexArrayId};
|
|
use canvas_traits::webgl::webgl_channel;
|
|
use dom::bindings::cell::DOMRefCell;
|
|
use dom::bindings::codegen::Bindings::WebGLBufferBinding;
|
|
use dom::bindings::js::Root;
|
|
use dom::bindings::reflector::reflect_dom_object;
|
|
use dom::webglobject::WebGLObject;
|
|
use dom::window::Window;
|
|
use dom_struct::dom_struct;
|
|
use std::cell::Cell;
|
|
use std::collections::HashSet;
|
|
|
|
|
|
#[dom_struct]
|
|
pub struct WebGLBuffer {
|
|
webgl_object: WebGLObject,
|
|
id: WebGLBufferId,
|
|
/// The target to which this buffer was bound the first time
|
|
target: Cell<Option<u32>>,
|
|
capacity: Cell<usize>,
|
|
is_deleted: Cell<bool>,
|
|
// The Vertex Array Objects that are referencing this buffer
|
|
vao_references: DOMRefCell<Option<HashSet<WebGLVertexArrayId>>>,
|
|
pending_delete: Cell<bool>,
|
|
#[ignore_heap_size_of = "Defined in ipc-channel"]
|
|
renderer: WebGLMsgSender,
|
|
}
|
|
|
|
impl WebGLBuffer {
|
|
fn new_inherited(renderer: WebGLMsgSender,
|
|
id: WebGLBufferId)
|
|
-> WebGLBuffer {
|
|
WebGLBuffer {
|
|
webgl_object: WebGLObject::new_inherited(),
|
|
id: id,
|
|
target: Cell::new(None),
|
|
capacity: Cell::new(0),
|
|
is_deleted: Cell::new(false),
|
|
vao_references: DOMRefCell::new(None),
|
|
pending_delete: Cell::new(false),
|
|
renderer: renderer,
|
|
}
|
|
}
|
|
|
|
pub fn maybe_new(window: &Window, renderer: WebGLMsgSender)
|
|
-> Option<Root<WebGLBuffer>> {
|
|
let (sender, receiver) = webgl_channel().unwrap();
|
|
renderer.send(WebGLCommand::CreateBuffer(sender)).unwrap();
|
|
|
|
let result = receiver.recv().unwrap();
|
|
result.map(|buffer_id| WebGLBuffer::new(window, renderer, buffer_id))
|
|
}
|
|
|
|
pub fn new(window: &Window,
|
|
renderer: WebGLMsgSender,
|
|
id: WebGLBufferId)
|
|
-> Root<WebGLBuffer> {
|
|
reflect_dom_object(box WebGLBuffer::new_inherited(renderer, id),
|
|
window, WebGLBufferBinding::Wrap)
|
|
}
|
|
}
|
|
|
|
|
|
impl WebGLBuffer {
|
|
pub fn id(&self) -> WebGLBufferId {
|
|
self.id
|
|
}
|
|
|
|
// NB: Only valid buffer targets come here
|
|
pub fn bind(&self, target: u32) -> WebGLResult<()> {
|
|
if let Some(previous_target) = self.target.get() {
|
|
if target != previous_target {
|
|
return Err(WebGLError::InvalidOperation);
|
|
}
|
|
} else {
|
|
self.target.set(Some(target));
|
|
}
|
|
let msg = WebGLCommand::BindBuffer(target, Some(self.id));
|
|
self.renderer.send(msg).unwrap();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn buffer_data(&self, target: u32, data: &[u8], usage: u32) -> WebGLResult<()> {
|
|
if let Some(previous_target) = self.target.get() {
|
|
if target != previous_target {
|
|
return Err(WebGLError::InvalidOperation);
|
|
}
|
|
}
|
|
self.capacity.set(data.len());
|
|
self.renderer.send(WebGLCommand::BufferData(target, data.to_vec(), usage)).unwrap();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub fn capacity(&self) -> usize {
|
|
self.capacity.get()
|
|
}
|
|
|
|
pub fn delete(&self) {
|
|
if !self.is_deleted.get() {
|
|
self.is_deleted.set(true);
|
|
let _ = self.renderer.send(WebGLCommand::DeleteBuffer(self.id));
|
|
}
|
|
}
|
|
|
|
pub fn is_deleted(&self) -> bool {
|
|
self.is_deleted.get()
|
|
}
|
|
|
|
pub fn target(&self) -> Option<u32> {
|
|
self.target.get()
|
|
}
|
|
|
|
pub fn is_attached_to_vao(&self) -> bool {
|
|
self.vao_references.borrow().as_ref().map_or(false, |vaos| !vaos.is_empty())
|
|
}
|
|
|
|
pub fn set_pending_delete(&self) {
|
|
self.pending_delete.set(true);
|
|
}
|
|
|
|
pub fn add_vao_reference(&self, id: WebGLVertexArrayId) {
|
|
let mut vao_refs = self.vao_references.borrow_mut();
|
|
if let Some(ref mut vao_refs) = *vao_refs {
|
|
vao_refs.insert(id);
|
|
return;
|
|
}
|
|
|
|
let mut map = HashSet::new();
|
|
map.insert(id);
|
|
*vao_refs = Some(map);
|
|
}
|
|
|
|
pub fn remove_vao_reference(&self, id: WebGLVertexArrayId) {
|
|
if let Some(ref mut vao_refs) = *self.vao_references.borrow_mut() {
|
|
if vao_refs.take(&id).is_some() && self.pending_delete.get() {
|
|
// WebGL spec: The deleted buffers should no longer be valid when the VAOs are deleted
|
|
let _ = self.renderer.send(WebGLCommand::DeleteBuffer(self.id));
|
|
self.is_deleted.set(true);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for WebGLBuffer {
|
|
fn drop(&mut self) {
|
|
self.delete();
|
|
}
|
|
}
|