diff --git a/ports/zephyr-cp/tests/test_vectorio.py b/ports/zephyr-cp/tests/test_vectorio.py index 657bbddef45..b31fb990882 100644 --- a/ports/zephyr-cp/tests/test_vectorio.py +++ b/ports/zephyr-cp/tests/test_vectorio.py @@ -294,6 +294,99 @@ def test_vectorio_move_and_hide(circuitpython): assert _pixel_at(after, 160, 45) == green +# A Group holding shapes is removed from its parent. The area of the shapes has +# to be redrawn without them (#12003). The rectangle that stays in the root +# group shows that the rest of the screen is kept. +REMOVE_GROUP_CODE = ( + _DISPLAY_HELPER + + """\ +import displayio +import time +import vectorio + +palette = displayio.Palette(4) +palette[0] = 0x000000 +palette[1] = 0xFF0000 +palette[2] = 0x00FF00 +palette[3] = 0x0000FF + +root = displayio.Group() + +kept = vectorio.Rectangle( + pixel_shader=palette, width=40, height=20, x=220, y=40, color_index=3 +) +root.append(kept) + +shapes = displayio.Group() +shapes.append( + vectorio.Circle(pixel_shader=palette, radius=20, x=40, y=40, color_index=1) +) +shapes.append( + vectorio.Rectangle( + pixel_shader=palette, width=40, height=20, x=100, y=40, color_index=2 + ) +) +root.append(shapes) + +display.auto_refresh = False +display.root_group = root +display.refresh() +print('shown') +time.sleep(5) +root.remove(shapes) +display.refresh() +print('removed') +while True: + time.sleep(1) +""" +) + + +@pytest.mark.circuitpy_drive({"code.py": REMOVE_GROUP_CODE}) +@pytest.mark.display(capture_times_ns=[4_000_000_000, 12_000_000_000]) +@pytest.mark.duration(16) +def test_vectorio_remove_group(circuitpython): + """Removing a Group that holds shapes clears the shapes from the screen.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "shown" in output + assert "removed" in output + + captures = circuitpython.display_capture_paths() + if len(captures) < 2 or not all(capture.exists() for capture in captures): + pytest.skip("display capture was not produced") + + with Image.open(captures[0]) as img: + before = img.convert("RGB") + with Image.open(captures[1]) as img: + after = img.convert("RGB") + + red = (255, 0, 0) + green = (0, 255, 0) + blue = (0, 0, 255) + black = (0, 0, 0) + + # The first capture is taken while the shapes are shown. + assert _pixel_at(before, 40, 40) == red + assert _pixel_at(before, 120, 50) == green + assert _pixel_at(before, 240, 50) == blue + + # After the removal only the rectangle in the root group is left. + expected = Image.new("RGB", after.size, black) + for y in range(40, 60): + for x in range(220, 260): + expected.putpixel((x, y), blue) + mismatches = [] + for y in range(after.height): + for x in range(after.width): + actual_pixel = after.getpixel((x, y)) + expected_pixel = expected.getpixel((x, y)) + if actual_pixel != expected_pixel: + mismatches.append((x, y, expected_pixel, actual_pixel)) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:5]}" + + # Corner radius, outlines and the geometry that display_shapes needs. Both # compositors answer contains() from the same coverage test, so this is the same # on both even though they rasterize differently. diff --git a/shared-module/displayio/legacy/Group.c b/shared-module/displayio/legacy/Group.c index ae0a01320f2..779c8ab959f 100644 --- a/shared-module/displayio/legacy/Group.c +++ b/shared-module/displayio/legacy/Group.c @@ -116,20 +116,30 @@ bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t for (size_t i = 0; i < self->members->len; i++) { mp_obj_t layer; displayio_area_t layer_area; + bool has_area = false; layer = mp_obj_cast_to_native_base( self->members->items[i], &displayio_tilegrid_type); if (layer != MP_OBJ_NULL) { - if (!displayio_tilegrid_get_previous_area(layer, &layer_area)) { - continue; - } + has_area = displayio_tilegrid_get_previous_area(layer, &layer_area); } else { layer = mp_obj_cast_to_native_base( self->members->items[i], &displayio_group_type); if (layer != MP_OBJ_NULL) { - if (!displayio_group_get_previous_area(layer, &layer_area)) { - continue; + has_area = displayio_group_get_previous_area(layer, &layer_area); + } + #if CIRCUITPY_VECTORIO || CIRCUITPY_VIDEOCORE + else { + // Draw protocol layers (vectorio shapes, videocore buffers), as in _remove_layer(). + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, self->members->items[i]); + if (draw_protocol != NULL) { + layer = draw_protocol->draw_get_protocol_self(self->members->items[i]); + has_area = draw_protocol->draw_protocol_impl->draw_get_dirty_area(layer, &layer_area); } } + #endif + } + if (!has_area) { + continue; } if (first) { displayio_area_copy(&layer_area, area);