forked from mirrors/gecko-dev
		
	 4f37e0ba01
			
		
	
	
		4f37e0ba01
		
	
	
	
	
		
			
			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
		
	
			
		
			
				
	
	
		
			68 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			68 lines
		
	
	
	
		
			2.1 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::WebGLBufferBinding;
 | |
| 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};
 | |
| use std::sync::mpsc::{channel, Sender};
 | |
| use std::cell::Cell;
 | |
| 
 | |
| #[dom_struct]
 | |
| pub struct WebGLBuffer {
 | |
|     webgl_object: WebGLObject,
 | |
|     id: u32,
 | |
|     is_deleted: Cell<bool>,
 | |
|     renderer: Sender<CanvasMsg>,
 | |
| }
 | |
| 
 | |
| impl WebGLBuffer {
 | |
|     fn new_inherited(renderer: Sender<CanvasMsg>, id: u32) -> WebGLBuffer {
 | |
|         WebGLBuffer {
 | |
|             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<WebGLBuffer>> {
 | |
|         let (sender, receiver) = channel();
 | |
|         renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::CreateBuffer(sender))).unwrap();
 | |
| 
 | |
|         let result = receiver.recv().unwrap();
 | |
|         result.map(|buffer_id| WebGLBuffer::new(global, renderer, *buffer_id))
 | |
|     }
 | |
| 
 | |
|     pub fn new(global: GlobalRef, renderer: Sender<CanvasMsg>, id: u32) -> Root<WebGLBuffer> {
 | |
|         reflect_dom_object(box WebGLBuffer::new_inherited(renderer, id), global, WebGLBufferBinding::Wrap)
 | |
|     }
 | |
| }
 | |
| 
 | |
| pub trait WebGLBufferHelpers {
 | |
|     fn id(self) -> u32;
 | |
|     fn bind(self, target: u32);
 | |
|     fn delete(self);
 | |
| }
 | |
| 
 | |
| impl<'a> WebGLBufferHelpers for &'a WebGLBuffer {
 | |
|     fn id(self) -> u32 {
 | |
|         self.id
 | |
|     }
 | |
| 
 | |
|     fn bind(self, target: u32) {
 | |
|         self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::BindBuffer(target, self.id))).unwrap();
 | |
|     }
 | |
| 
 | |
|     fn delete(self) {
 | |
|         if !self.is_deleted.get() {
 | |
|             self.is_deleted.set(true);
 | |
|             self.renderer.send(CanvasMsg::WebGL(CanvasWebGLMsg::DeleteBuffer(self.id))).unwrap();
 | |
|         }
 | |
|     }
 | |
| }
 |