wijjit.layout.dirty.DirtyRegionManager
- class wijjit.layout.dirty.DirtyRegionManager[source]
Manages dirty regions for optimized screen rendering.
This class tracks rectangular regions of the screen that need to be redrawn and automatically merges overlapping or adjacent regions to minimize rendering overhead.
The manager limits the number of tracked regions to avoid O(n^2) worst-case performance in the merging algorithm. When the region count exceeds MAX_REGIONS, the manager switches to full-screen dirty mode for efficiency.
- Variables:
_regions (
list[DirtyRegion]) – List of dirty regions_full_screen_dirty (
bool) – Whether the entire screen is marked as dirty_screen_width (
intorNone) – Screen width (set when marking full screen dirty)_screen_height (
intorNone) – Screen height (set when marking full screen dirty)Attributes (Class)
----------------
MAX_REGIONS (
int) – Maximum number of regions to track before switching to full-screen dirty mode. Default is 64, which provides good balance between region tracking precision and merge algorithm performance.
Examples
>>> manager = DirtyRegionManager() >>> manager.mark_dirty(0, 0, 10, 5) >>> manager.mark_dirty(5, 0, 10, 5) # Overlaps, will be merged >>> regions = manager.get_merged_regions() >>> len(regions) 1 >>> regions[0] DirtyRegion(x=0, y=0, width=15, height=5)
Methods
__init__()Initialize the dirty region manager.
clear()Clear all dirty regions.
Get all dirty regions as merged rectangles.
is_dirty()Check if there are any dirty regions.
Check if the full screen is marked as dirty.
mark_dirty(x, y, width, height)Mark a rectangular region as dirty.
mark_dirty_bounds(bounds)Mark a region as dirty using a Bounds object.
mark_full_screen(width, height)Mark the entire screen as dirty.
Attributes
- mark_dirty(x, y, width, height)[source]
Mark a rectangular region as dirty.
The region will be merged with any overlapping or adjacent regions automatically to optimize rendering.
- Parameters:
- Return type:
None
Notes
If the full screen is already marked as dirty, this method has no effect since the entire screen will be redrawn anyway.
- mark_dirty_bounds(bounds)[source]
Mark a region as dirty using a Bounds object.
- Parameters:
bounds (
Bounds) – The bounds to mark as dirty- Return type:
None
- mark_full_screen(width, height)[source]
Mark the entire screen as dirty.
This clears all individual dirty regions and marks the whole screen for redraw. Use this for major layout changes or when the cost of tracking individual regions exceeds the benefit.
- get_merged_regions()[source]
Get all dirty regions as merged rectangles.
Returns dirty regions with overlapping/adjacent regions merged to minimize the number of screen updates.
- Returns:
List of dirty regions as (x, y, width, height) tuples
- Return type:
Notes
If the full screen is marked as dirty, returns a single region covering the entire screen.
- is_full_screen_dirty()[source]
Check if the full screen is marked as dirty.
- Returns:
True if full screen needs redraw
- Return type:
- is_dirty()[source]
Check if there are any dirty regions.
- Returns:
True if there are dirty regions or full screen is dirty
- Return type: