Skip to content

Commit 8eac382

Browse files
ryanbreenclaude
andcommitted
fix(graphics): correct stride calculation in fill_rect and draw_hline
The stride value from Canvas::stride() is in pixels, not bytes. When calculating row byte offsets, we need to multiply by bytes_per_pixel to get the correct memory location. This fixes the garbled/repeated graphics output where drawing was happening at 1/4 of the correct y-offsets. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c9bd736 commit 8eac382

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

kernel/src/graphics/primitives.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,16 @@ pub fn draw_hline(canvas: &mut impl Canvas, x1: i32, x2: i32, y: i32, color: Col
155155
}
156156

157157
let stride = canvas.stride();
158+
let stride_bytes = stride * bpp; // stride is in pixels, convert to bytes
158159
let is_bgr = canvas.is_bgr();
159160
let pixel = color.to_pixel_bytes(bpp, is_bgr);
160161
let buffer = canvas.buffer_mut();
161162

162-
let row_start = (y as usize).saturating_mul(stride);
163+
let row_start = (y as usize).saturating_mul(stride_bytes);
163164
if row_start >= buffer.len() {
164165
return;
165166
}
166-
let row_end = min(row_start + stride, buffer.len());
167+
let row_end = min(row_start + stride_bytes, buffer.len());
167168
let row_slice = &mut buffer[row_start..row_end];
168169

169170
let start_byte = (start as usize).saturating_mul(bpp);
@@ -262,6 +263,7 @@ pub fn fill_rect(canvas: &mut impl Canvas, rect: Rect, color: Color) {
262263
return;
263264
}
264265
let stride = canvas.stride();
266+
let stride_bytes = stride * bpp; // stride is in pixels, convert to bytes
265267
let is_bgr = canvas.is_bgr();
266268
let pixel = color.to_pixel_bytes(bpp, is_bgr);
267269
let buffer = canvas.buffer_mut();
@@ -272,11 +274,11 @@ pub fn fill_rect(canvas: &mut impl Canvas, rect: Rect, color: Color) {
272274
let end_x = start_x.saturating_add(clipped.width as usize);
273275

274276
for y in start_y..end_y {
275-
let row_start = y.saturating_mul(stride);
277+
let row_start = y.saturating_mul(stride_bytes);
276278
if row_start >= buffer.len() {
277279
break;
278280
}
279-
let row_end = min(row_start + stride, buffer.len());
281+
let row_end = min(row_start + stride_bytes, buffer.len());
280282
let row_slice = &mut buffer[row_start..row_end];
281283

282284
let start_byte = start_x.saturating_mul(bpp);

0 commit comments

Comments
 (0)