forked from mirrors/gecko-dev
This improves the encapsulation and consistency in our WebGL
implementation.
Also allows to implement new methods such as `getShaderSource()`.
It will also allow us to use `delete()` in the destructors of them (note
that we will probably want to keep track of them from the context before).
Some concerns:
**Trait method repetition**:
I'm aware that the traits `WebGL{Buffer,Renderbuffer,Framebuffer,Texture}Helpers` are basically the same, but `delete()` and `id()` methods are everywhere. I've thought something like:
```rust
pub trait WebGLIdentifiable {
type WebGLId; // id is sometimes i32 (see WebGLUniformLocation)
fn id(&self) -> Self::WebGLId;
}
pub trait WebGLBindable {
fn bind(&self);
}
pub trait WebGLDeletable {
fn delete(&self);
}
```
But I'd want to know your opinion first.
**`renderer` repetition**:
Thought of moving the field: `renderer: Sender<CanvasMsg>` to `WebGLObject`, but I think it makes it way more complicated to read, and also a bit unnecessary, at least IMO (`WebGLObject` will never interact with the field directly). It would also mean that all `WebGLObject`s should have one, which is true at this moment, but maybe not with WebGL 2, for example.
Source-Repo: https://github.com/servo/servo
Source-Revision: 0f8095b950dd144497919cfea65a1f154ed3ae9a
69 lines
2.3 KiB
Rust
69 lines
2.3 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 dom::bindings::codegen::Bindings::WebGLFramebufferBinding;
|
|
use dom::bindings::global::GlobalRef;
|
|
use dom::bindings::js::Root;
|
|
use dom::bindings::utils::reflect_dom_object;
|
|
use dom::webglobject::WebGLObject;
|
|
|
|
use canvas_traits::{CanvasMsg, CanvasWebGLMsg, WebGLFramebufferBindingRequest};
|
|
use std::sync::mpsc::{channel, Sender};
|
|
use std::cell::Cell;
|
|
|
|
#[dom_struct]
|
|
pub struct WebGLFramebuffer {
|
|
webgl_object: WebGLObject,
|
|
id: u32,
|
|
is_deleted: Cell<bool>,
|
|
renderer: Sender<CanvasMsg>,
|
|
}
|
|
|
|
impl WebGLFramebuffer {
|
|
fn new_inherited(renderer: Sender<CanvasMsg>, id: u32) -> WebGLFramebuffer {
|
|
WebGLFramebuffer {
|
|
webgl_object: WebGLObject::new_inherited(),
|
|
id: id,
|
|
is_deleted: Cell::new(false),
|
|
renderer: renderer,
|
|
}
|
|
}
|
|
|
|
pub fn maybe_new(global: GlobalRef, renderer: Sender<CanvasMsg>) -> Option<Root<WebGLFramebuffer>> {
|
|
let (sender, receiver) = channel();
|
|
renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::CreateFramebuffer(sender))).unwrap();
|
|
|
|
let result = receiver.recv().unwrap();
|
|
result.map(|fb_id| WebGLFramebuffer::new(global, renderer, *fb_id))
|
|
}
|
|
|
|
pub fn new(global: GlobalRef, renderer: Sender<CanvasMsg>, id: u32) -> Root<WebGLFramebuffer> {
|
|
reflect_dom_object(box WebGLFramebuffer::new_inherited(renderer, id), global, WebGLFramebufferBinding::Wrap)
|
|
}
|
|
}
|
|
|
|
pub trait WebGLFramebufferHelpers {
|
|
fn id(self) -> u32;
|
|
fn bind(self, target: u32);
|
|
fn delete(self);
|
|
}
|
|
|
|
impl<'a> WebGLFramebufferHelpers for &'a WebGLFramebuffer {
|
|
fn id(self) -> u32 {
|
|
self.id
|
|
}
|
|
|
|
fn bind(self, target: u32) {
|
|
let cmd = CanvasWebGLMsg::BindFramebuffer(target, WebGLFramebufferBindingRequest::Explicit(self.id));
|
|
self.renderer.send(CanvasMsg::WebGL(cmd)).unwrap();
|
|
}
|
|
|
|
fn delete(self) {
|
|
if !self.is_deleted.get() {
|
|
self.is_deleted.set(true);
|
|
self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::DeleteFramebuffer(self.id))).unwrap();
|
|
}
|
|
}
|
|
}
|