Skip to content

Commit ab16475

Browse files
committed
fix(web): Fix damage regions outside the pixel buffer
1 parent 1ec2833 commit ab16475

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
- **Breaking:** Removed generic type parameters `D` and `W` from `Buffer<'_>` struct.
77
- **Breaking:** Removed `Deref[Mut]` implementation on `Buffer<'_>`. Use `Buffer::pixels()` instead.
88
- **Breaking:** Removed `DamageOutOfRange` error case. If the damage value is greater than the backend supports, it is instead clamped to an appropriate value.
9-
- Fixed `present_with_damage` with bounds out of range on Windows.
9+
- Fixed `present_with_damage` with bounds out of range on Windows and Web.
1010

1111
# 0.4.7
1212

src/backends/web.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl BufferInterface for BufferImpl<'_> {
332332

333333
/// Push the buffer to the canvas.
334334
fn present_with_damage(self, damage: &[Rect]) -> Result<(), SoftBufferError> {
335-
let (buffer_width, _buffer_height) = self
335+
let (buffer_width, buffer_height) = self
336336
.size
337337
.expect("Must set size of surface before calling `present_with_damage()`");
338338

@@ -358,8 +358,10 @@ impl BufferInterface for BufferImpl<'_> {
358358
.collect();
359359

360360
debug_assert_eq!(
361-
bitmap.len() as u32,
362-
union_damage.width.get() * union_damage.height.get() * 4
361+
bitmap.len(),
362+
union_damage.width.get().min(buffer_width.get()) as usize
363+
* union_damage.height.get().min(buffer_height.get()) as usize
364+
* 4
363365
);
364366

365367
#[cfg(target_feature = "atomics")]
@@ -382,14 +384,14 @@ impl BufferInterface for BufferImpl<'_> {
382384
let array = Uint8Array::new_with_length(bitmap.len() as u32);
383385
array.copy_from(&bitmap);
384386
let array = Uint8ClampedArray::new(&array);
385-
ImageDataExt::new(array, union_damage.width.get())
387+
ImageDataExt::new(array, union_damage.width.get().min(buffer_width.get()))
386388
.map(JsValue::from)
387389
.map(ImageData::unchecked_from_js)
388390
};
389391
#[cfg(not(target_feature = "atomics"))]
390392
let result = ImageData::new_with_u8_clamped_array(
391393
wasm_bindgen::Clamped(&bitmap),
392-
union_damage.width.get(),
394+
union_damage.width.get().min(buffer_width.get()),
393395
);
394396
// This should only throw an error if the buffer we pass's size is incorrect.
395397
let image_data = result.unwrap();
@@ -399,12 +401,12 @@ impl BufferInterface for BufferImpl<'_> {
399401
self.canvas
400402
.put_image_data(
401403
&image_data,
402-
union_damage.x.into(),
403-
union_damage.y.into(),
404+
union_damage.x.min(buffer_width.get()).into(),
405+
union_damage.y.min(buffer_height.get()).into(),
404406
(rect.x - union_damage.x).into(),
405407
(rect.y - union_damage.y).into(),
406-
rect.width.get().into(),
407-
rect.height.get().into(),
408+
rect.width.get().min(buffer_width.get()).into(),
409+
rect.height.get().min(buffer_height.get()).into(),
408410
)
409411
.unwrap();
410412
}

0 commit comments

Comments
 (0)