script/dom/webgl/
webglbuffer.rs1use std::cell::Cell;
7
8use dom_struct::dom_struct;
9use script_bindings::reflector::DomObject;
10use script_bindings::weakref::WeakRef;
11use servo_base::generic_channel;
12use servo_canvas_traits::webgl::{
13 WebGLBufferId, WebGLCommand, WebGLError, WebGLResult, webgl_channel,
14};
15
16use crate::dom::bindings::codegen::Bindings::WebGL2RenderingContextBinding::WebGL2RenderingContextConstants;
17use crate::dom::bindings::codegen::Bindings::WebGLRenderingContextBinding::WebGLRenderingContextConstants;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::reflector::{DomGlobal, reflect_dom_object};
20use crate::dom::bindings::root::DomRoot;
21use crate::dom::webgl::webglobject::WebGLObject;
22use crate::dom::webgl::webglrenderingcontext::{Operation, WebGLRenderingContext};
23use crate::script_runtime::CanGc;
24
25fn target_is_copy_buffer(target: u32) -> bool {
26 target == WebGL2RenderingContextConstants::COPY_READ_BUFFER ||
27 target == WebGL2RenderingContextConstants::COPY_WRITE_BUFFER
28}
29
30#[derive(JSTraceable, MallocSizeOf)]
31struct DroppableWebGLBuffer {
32 #[no_trace]
33 id: WebGLBufferId,
34 marked_for_deletion: Cell<bool>,
35 attached_counter: Cell<u32>,
36 context: WeakRef<WebGLRenderingContext>,
37}
38
39impl DroppableWebGLBuffer {
40 pub(crate) fn new(
41 id: WebGLBufferId,
42 marked_for_deletion: Cell<bool>,
43 attached_counter: Cell<u32>,
44 context: WeakRef<WebGLRenderingContext>,
45 ) -> Self {
46 Self {
47 id,
48 marked_for_deletion,
49 attached_counter,
50 context,
51 }
52 }
53}
54
55impl DroppableWebGLBuffer {
56 pub(crate) fn is_marked_for_deletion(&self) -> bool {
57 self.marked_for_deletion.get()
58 }
59
60 pub(crate) fn set_marked_for_deletion(&self, marked_for_deletion: bool) {
61 self.marked_for_deletion.set(marked_for_deletion);
62 }
63
64 pub(crate) fn get_attached_counter(&self) -> u32 {
65 self.attached_counter.get()
66 }
67
68 pub(crate) fn set_attached_counter(&self, attached_counter: u32) {
69 self.attached_counter.set(attached_counter);
70 }
71
72 pub(crate) fn id(&self) -> WebGLBufferId {
73 self.id
74 }
75
76 pub(crate) fn is_attached(&self) -> bool {
77 self.get_attached_counter() != 0
78 }
79
80 pub(crate) fn is_deleted(&self) -> bool {
81 self.is_marked_for_deletion() && !self.is_attached()
82 }
83
84 pub(crate) fn delete(&self, operation_fallibility: Operation) {
85 assert!(self.is_deleted());
86 if let Some(context) = self.context.root() {
87 let cmd = WebGLCommand::DeleteBuffer(self.id);
88 match operation_fallibility {
89 Operation::Fallible => context.send_command_ignored(cmd),
90 Operation::Infallible => context.send_command(cmd),
91 }
92 }
93 }
94
95 pub(crate) fn mark_for_deletion(&self, operation_fallibility: Operation) {
96 if self.is_marked_for_deletion() {
97 return;
98 }
99 self.set_marked_for_deletion(true);
100 if self.is_deleted() {
101 self.delete(operation_fallibility);
102 }
103 }
104}
105
106impl Drop for DroppableWebGLBuffer {
107 fn drop(&mut self) {
108 self.mark_for_deletion(Operation::Fallible);
109 }
110}
111
112#[dom_struct(associated_memory)]
113pub(crate) struct WebGLBuffer {
114 webgl_object: WebGLObject,
115 target: Cell<Option<u32>>,
117 capacity: Cell<usize>,
118 usage: Cell<u32>,
120 droppable: DroppableWebGLBuffer,
121}
122
123impl WebGLBuffer {
124 fn new_inherited(context: &WebGLRenderingContext, id: WebGLBufferId) -> Self {
125 Self {
126 webgl_object: WebGLObject::new_inherited(context),
127 target: Default::default(),
128 capacity: Default::default(),
129 usage: Cell::new(WebGLRenderingContextConstants::STATIC_DRAW),
130 droppable: DroppableWebGLBuffer::new(
131 id,
132 Default::default(),
133 Default::default(),
134 WeakRef::new(context),
135 ),
136 }
137 }
138
139 pub(crate) fn maybe_new(
140 context: &WebGLRenderingContext,
141 can_gc: CanGc,
142 ) -> Option<DomRoot<Self>> {
143 let (sender, receiver) = webgl_channel().unwrap();
144 context.send_command(WebGLCommand::CreateBuffer(sender));
145 receiver
146 .recv()
147 .unwrap()
148 .map(|id| WebGLBuffer::new(context, id, can_gc))
149 }
150
151 pub(crate) fn new(
152 context: &WebGLRenderingContext,
153 id: WebGLBufferId,
154 can_gc: CanGc,
155 ) -> DomRoot<Self> {
156 reflect_dom_object(
157 Box::new(WebGLBuffer::new_inherited(context, id)),
158 &*context.global(),
159 can_gc,
160 )
161 }
162}
163
164impl WebGLBuffer {
165 pub(crate) fn id(&self) -> WebGLBufferId {
166 self.droppable.id()
167 }
168
169 pub(crate) fn buffer_data(&self, target: u32, data: &[u8], usage: u32) -> WebGLResult<()> {
170 match usage {
171 WebGLRenderingContextConstants::STREAM_DRAW |
172 WebGLRenderingContextConstants::STATIC_DRAW |
173 WebGLRenderingContextConstants::DYNAMIC_DRAW |
174 WebGL2RenderingContextConstants::STATIC_READ |
175 WebGL2RenderingContextConstants::DYNAMIC_READ |
176 WebGL2RenderingContextConstants::STREAM_READ |
177 WebGL2RenderingContextConstants::STATIC_COPY |
178 WebGL2RenderingContextConstants::DYNAMIC_COPY |
179 WebGL2RenderingContextConstants::STREAM_COPY => (),
180 _ => return Err(WebGLError::InvalidEnum),
181 }
182
183 self.capacity.set(data.len());
184 self.reflector()
185 .update_memory_size(self, self.capacity.get());
186 self.usage.set(usage);
187 let (sender, receiver) = generic_channel::channel().unwrap();
188 self.upcast()
189 .send_command(WebGLCommand::BufferData(target, receiver, usage));
190 let buffer = generic_channel::GenericSharedMemory::from_bytes(data);
191 sender.send(buffer).unwrap();
192 Ok(())
193 }
194
195 pub(crate) fn capacity(&self) -> usize {
196 self.capacity.get()
197 }
198
199 pub(crate) fn mark_for_deletion(&self, operation_fallibility: Operation) {
200 self.droppable.mark_for_deletion(operation_fallibility);
201 }
202
203 fn delete(&self, operation_fallibility: Operation) {
204 self.droppable.delete(operation_fallibility);
205 }
206
207 pub(crate) fn is_marked_for_deletion(&self) -> bool {
208 self.droppable.is_marked_for_deletion()
209 }
210
211 fn get_attached_counter(&self) -> u32 {
212 self.droppable.get_attached_counter()
213 }
214
215 fn set_attached_counter(&self, attached_counter: u32) {
216 self.droppable.set_attached_counter(attached_counter);
217 }
218
219 pub(crate) fn is_deleted(&self) -> bool {
220 self.droppable.is_deleted()
221 }
222
223 pub(crate) fn target(&self) -> Option<u32> {
224 self.target.get()
225 }
226
227 fn can_bind_to(&self, new_target: u32) -> bool {
229 if let Some(current_target) = self.target.get() {
230 if [current_target, new_target]
231 .contains(&WebGLRenderingContextConstants::ELEMENT_ARRAY_BUFFER)
232 {
233 return target_is_copy_buffer(new_target) || new_target == current_target;
234 }
235 }
236 true
237 }
238
239 pub(crate) fn set_target_maybe(&self, target: u32) -> WebGLResult<()> {
240 if !self.can_bind_to(target) {
241 return Err(WebGLError::InvalidOperation);
242 }
243
244 if self.target.get().is_none() {
245 if target_is_copy_buffer(target) {
246 self.target
249 .set(Some(WebGLRenderingContextConstants::ARRAY_BUFFER));
250 } else {
251 self.target.set(Some(target));
255 }
256 }
257
258 Ok(())
259 }
260
261 pub(crate) fn increment_attached_counter(&self) {
262 self.set_attached_counter(
263 self.get_attached_counter()
264 .checked_add(1)
265 .expect("refcount overflowed"),
266 );
267 }
268
269 pub(crate) fn decrement_attached_counter(&self, operation_fallibility: Operation) {
270 self.set_attached_counter(
271 self.get_attached_counter()
272 .checked_sub(1)
273 .expect("refcount underflowed"),
274 );
275 if self.is_deleted() {
276 self.delete(operation_fallibility);
277 }
278 }
279
280 pub(crate) fn usage(&self) -> u32 {
281 self.usage.get()
282 }
283}