From 3f41d2b9d7fb9b11d2b496d0f35f0dc8984a3887 Mon Sep 17 00:00:00 2001 From: Sassan Haradji Date: Tue, 29 Oct 2024 16:01:27 +0400 Subject: [PATCH] feat: add `show_update_regions` property to `HeadlessWidget` to show the regions that are updated in each frame as a debug feature --- CHANGELOG.md | 1 + headless_kivy/_debug.py | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d05e95e..8650182 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - feat: divide each frame into multiple rectangles, compare rectangle with the same rectangle in the previous frame and update only the changed ones - feat: add two throttling mechanisms to avoid sending too many frames to the display, one based on total bandwidth of all draw commands of all `HeadlessWidget`s and the other based on set fps of a particular `HeadlessWidget` +- feat: add `show_update_regions` property to `HeadlessWidget` to show the regions that are updated in each frame as a debug feature ## Version 0.11.1 diff --git a/headless_kivy/_debug.py b/headless_kivy/_debug.py index 9dda04a..3736e23 100644 --- a/headless_kivy/_debug.py +++ b/headless_kivy/_debug.py @@ -6,13 +6,14 @@ from pathlib import Path from typing import TYPE_CHECKING, ClassVar, Self +import numpy as np from kivy.metrics import dp +from kivy.properties import BooleanProperty from headless_kivy import config from headless_kivy.logger import logger if TYPE_CHECKING: - import numpy as np from kivy.graphics.context_instructions import Color from kivy.graphics.fbo import Fbo from kivy.graphics.vertex_instructions import Rectangle @@ -23,6 +24,7 @@ class DebugMixin: fbo: Fbo fbo_render_color: Color fbo_render_rectangle: Rectangle + show_update_regions = BooleanProperty(0) x: int y: int @@ -56,9 +58,29 @@ def set_debug_info(self: Self) -> None: def render_debug_info( self: Self, rect_: tuple[int, int, int, int], - _: list[tuple[int, int, int, int]], + regions: list[tuple[int, int, int, int]], data: NDArray[np.uint8], ) -> None: + if self.show_update_regions: + for rect in regions: + self.update_region_seed = (self.update_region_seed + 1) % 3 + data[ + rect[1] : rect[3], + rect[0] : rect[2], + :, + ] = ( + data[ + rect[1] : rect[3], + rect[0] : rect[2], + :, + ] + * 0.5 + ).astype(np.uint8) + data[ + rect[1] : rect[3], + rect[0] : rect[2], + [(rect[0] + rect[1] + self.update_region_seed) % 7 % 3, 3], + ] += 127 if config.is_debug_mode(): x1, y1, x2, y2 = rect_ self.rendered_frames += 1