Skip to content

Commit

Permalink
Implement faster version of ClearRect
Browse files Browse the repository at this point in the history
Is 30% faster than the Pixman version so lets use it everywhere
  • Loading branch information
Ghabry committed Aug 30, 2024
1 parent 74fee9e commit 745121e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
16 changes: 5 additions & 11 deletions src/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,18 +799,12 @@ void Bitmap::Clear() {
}

void Bitmap::ClearRect(Rect const& dst_rect) {
pixman_color_t pcolor = {};
pixman_box32_t box = {
dst_rect.x,
dst_rect.y,
dst_rect.x + dst_rect.width,
dst_rect.y + dst_rect.height
};

box.x2 = Utils::Clamp<int32_t>(box.x2, 0, width());
box.y2 = Utils::Clamp<int32_t>(box.y2, 0, height());
if (dst_rect == GetRect()) {
Clear();
return;
}

pixman_image_fill_boxes(PIXMAN_OP_CLEAR, bitmap.get(), &pcolor, 1, &box);
BitmapBlit::ClearRect(*this, dst_rect);
}

// Hard light lookup table mapping source color to destination color
Expand Down
16 changes: 16 additions & 0 deletions src/bitmap_blit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,20 @@ bool BlitFast(Bitmap& dest, int x, int y, Bitmap const& src, Rect src_rect,
return true;
}

void ClearRect(Bitmap& dest, Rect src_rect) {
src_rect.Adjust(dest.GetRect());
Rect& dst_rect = src_rect;

int bpp = dest.bpp();
int dst_pitch = dest.pitch();

uint8_t* dst_pixels = (uint8_t*)dest.pixels() + dst_rect.x * bpp + dst_rect.y * dst_pitch;

int line_width = src_rect.width * bpp;

for (int y = 0; y < src_rect.height; ++y) {
memset(dst_pixels + y * dst_pitch, '\0', line_width);
}
}

} // namespace BitmapBlit
2 changes: 2 additions & 0 deletions src/bitmap_blit.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ namespace BitmapBlit {

bool BlitFast(Bitmap& dest, int x, int y, Bitmap const& src, Rect src_rect,
Opacity const& opacity);

void ClearRect(Bitmap& dest, Rect src_rect);
}

#endif

0 comments on commit 745121e

Please sign in to comment.