Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 258 additions & 0 deletions ports/zephyr-cp/tests/test_displayio_fractional.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries
# SPDX-License-Identifier: MIT

"""Fractional Group scaling in the LVGL compositor."""

import pytest
from PIL import Image


FRACTIONAL_SCENE = """\
import board
import displayio
import time

minimum = displayio.Group(scale=1 / 256)
assert minimum.scale == 1 / 256
minimum.scale = 1
minimum.scale = 1 / 256
assert minimum.scale == 1 / 256
requested = displayio.Group(scale=1.1)
assert requested.scale == 1.1
# Readback follows sub-Q8 changes too, so animations cannot stick at one step.
next_scale = 1.1 + 1 / 4096
requested.scale = next_scale
assert requested.scale == next_scale
try:
displayio.Group(scale=1 / 512)
except ValueError:
pass
else:
raise AssertionError('constructor accepted scale below Q8 minimum')
try:
minimum.scale = 1 / 512
except ValueError:
pass
else:
raise AssertionError('setter accepted scale below Q8 minimum')

palette = displayio.Palette(4)
palette[0] = 0xFF0000
palette[1] = 0x00FF00
palette[2] = 0x00FF00
palette[3] = 0xFFFFFF
checker = displayio.Bitmap(4, 4, 4)
for y in range(4):
for x in range(4):
checker[x, y] = 1 if (x + y) % 2 else 2

outer = displayio.Group(scale=1.25, x=11, y=17)
inner = displayio.Group(scale=1.2, x=2, y=1)
inner.append(displayio.TileGrid(checker, pixel_shader=palette))
outer.append(inner)
assert outer.scale == 1.25
assert inner.scale == 1.2

rgb565 = displayio.Bitmap(4, 4, 65536)
for y in range(4):
for x in range(4):
rgb565[x, y] = 0xF800 if x < 2 else 0x07E0
converter = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565)
direct = displayio.TileGrid(rgb565, pixel_shader=converter, x=30, y=0)
outer.append(direct)

root = displayio.Group()
root.append(outer)
display = board.DISPLAY
display.auto_refresh = False
display.root_group = root
display.refresh()
print('first')
time.sleep(5)

# Exercise a live scale change, movement, palette update and direct-pixel update.
inner.scale = 1.25
assert inner.scale == 1.25
inner.x = 3
palette[1] = 0xFF00FF
palette[2] = 0xFF00FF
rgb565[0, 0] = 0x001F
display.refresh()
print('second')
while True:
time.sleep(1)
"""

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
MAGENTA = (255, 0, 255)
BLACK = (0, 0, 0)


def _expected(width, height, changed):
frame = Image.new("RGB", (width, height), BLACK)
inner_scale = 400 if changed else 384 # Q8: 1.25 * 1.25 or 1.25 * 1.2

def draw(x0, y0, scale, color_at):
# CircuitPython duplicates the bottom/right source pixels into one
# pixel of padding, so LVGL's center-based extent includes the edges.
w = (4 * scale + 128) // 256
h = w
for y in range(h):
for x in range(w):
sx = x * 256 // scale
sy = y * 256 // scale
frame.putpixel((x0 + x, y0 + y), color_at(sx, sy))

inner_x = 11 + ((3 if changed else 2) * 320 + 128) // 256
inner_y = 17 + (320 + 128) // 256
draw(inner_x, inner_y, inner_scale, lambda x, y: MAGENTA if changed else GREEN)

direct_x = 11 + (30 * 320 + 128) // 256
draw(
direct_x,
17,
320,
lambda x, y: BLUE if changed and (x, y) == (0, 0) else RED if x < 2 else GREEN,
)
return frame


@pytest.mark.parametrize(
"board", ["native_native_sim_lvgl", "native_native_sim_lvgl_asan"], indirect=True
)
@pytest.mark.circuitpy_drive({"code.py": FRACTIONAL_SCENE})
@pytest.mark.display(capture_times_ns=[4_000_000_000, 9_000_000_000])
@pytest.mark.duration(11)
def test_displayio_fractional_group(circuitpython):
"""Fractional Q8 scales render and invalidate indexed/direct TileGrids."""
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "first" in output
assert "second" in output

captures = circuitpython.display_capture_paths()
if len(captures) < 2 or not captures[1].exists():
pytest.skip("display capture was not produced")

for path, changed in ((captures[0], False), (captures[1], True)):
with Image.open(path) as image:
actual = image.convert("RGB")
expected = _expected(actual.width, actual.height, changed)
mismatches = []
for y in range(actual.height):
for x in range(actual.width):
if actual.getpixel((x, y)) != expected.getpixel((x, y)):
mismatches.append((x, y, expected.getpixel((x, y)), actual.getpixel((x, y))))
assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}"

with Image.open(captures[0]) as image:
first_direct_pixel = image.convert("RGB").getpixel((49, 17))
with Image.open(captures[1]) as image:
second_direct_pixel = image.convert("RGB").getpixel((49, 17))
assert first_direct_pixel != BLACK
assert second_direct_pixel != BLACK
assert first_direct_pixel != second_direct_pixel


VECTOR_SCENE = """\
import board
import displayio
import time
import vectorio

palette = displayio.Palette(3)
palette[0] = 0x000000
palette[1] = 0x00FF00
palette[2] = 0x0000FF

group = displayio.Group(scale=0.5, x=10, y=20)
rectangle = vectorio.Rectangle(pixel_shader=palette, width=8, height=4, color_index=1)
polygon = vectorio.Polygon(pixel_shader=palette, points=[(0, 0), (7, 0), (0, 3)],
x=12, color_index=2)
group.append(rectangle)
group.append(polygon)
root = displayio.Group()
root.append(group)

assert rectangle.contains(10, 20)
assert rectangle.contains(13, 21)
assert not rectangle.contains(14, 20)

display = board.DISPLAY
display.auto_refresh = False
display.root_group = root
display.refresh()
print('first')
time.sleep(5)

# Exercise both the style-drawn shape and the polygon mask at a new fractional scale.
group.scale = 1.5
assert rectangle.contains(21, 25)
assert not rectangle.contains(22, 20)
display.refresh()
print('second')
while True:
time.sleep(1)
"""


def _polygon_covered(x, y):
points = ((0, 0), (7, 0), (0, 3))
winding = 0
x1, y1 = points[0]
for x2, y2 in points[1:] + points[:1]:
side = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1)
if y1 <= y:
if y2 > y and side < 0:
winding += 1
elif y2 <= y and side > 0:
winding -= 1
x1, y1 = x2, y2
return winding != 0


def _expected_vectors(scale):
frame = Image.new("RGB", (320, 240), BLACK)
scale_q8 = int(scale * 256)

def scaled(value):
return (value * scale_q8 + 128) // 256

for y in range(scaled(4)):
for x in range(scaled(8)):
frame.putpixel((10 + x, 20 + y), GREEN)

polygon_x = 10 + scaled(12)
for y in range(scaled(4)):
source_y = y * 256 // scale_q8
for x in range(scaled(8)):
source_x = x * 256 // scale_q8
if _polygon_covered(source_x, source_y):
frame.putpixel((polygon_x + x, 20 + y), BLUE)
return frame


@pytest.mark.parametrize(
"board", ["native_native_sim_lvgl", "native_native_sim_lvgl_asan"], indirect=True
)
@pytest.mark.circuitpy_drive({"code.py": VECTOR_SCENE})
@pytest.mark.display(capture_times_ns=[4_000_000_000, 9_000_000_000])
@pytest.mark.duration(11)
def test_displayio_fractional_vector_shapes(circuitpython):
"""Fractional scales work for style-drawn shapes and polygon masks."""
circuitpython.wait_until_done()
output = circuitpython.serial.all_output
assert "first" in output
assert "second" in output

captures = circuitpython.display_capture_paths()
if len(captures) < 2 or not captures[1].exists():
pytest.skip("display capture was not produced")

for path, scale in ((captures[0], 0.5), (captures[1], 1.5)):
with Image.open(path) as image:
actual = image.convert("RGB")
expected = _expected_vectors(scale)
assert actual.tobytes() == expected.tobytes()
23 changes: 12 additions & 11 deletions shared-bindings/displayio/Group.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@
//| class Group:
//| """Manage a group of sprites and groups and how they are inter-related."""
//|
//| def __init__(self, *, scale: int = 1, x: int = 0, y: int = 0) -> None:
//| def __init__(self, *, scale: int | float = 1, x: int = 0, y: int = 0) -> None:
//| """Create a Group of a given size and scale. Scale is in one dimension. For example, scale=2
//| leads to a layer's pixel being 2x2 pixels when in the group.
//| leads to a layer's pixel being 2x2 pixels when in the group. The LVGL compositor also
//| supports fractional scales.
//|
//| :param int scale: Scale of layer pixels in one dimension.
//| :param int | float scale: Scale of layer pixels in one dimension.
//| :param int x: Initial x position within the parent.
//| :param int y: Initial y position within the parent."""
//| ...
//|
static mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
enum { ARG_scale, ARG_x, ARG_y };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_scale, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 1} },
{ MP_QSTR_scale, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_rom_obj = MP_ROM_INT(1)} },
{ MP_QSTR_x, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
{ MP_QSTR_y, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);

mp_int_t scale = mp_arg_validate_int_range(args[ARG_scale].u_int, 1, 32767, MP_QSTR_scale);
mp_obj_t scale = args[ARG_scale].u_obj;

displayio_group_t *self = mp_obj_malloc(displayio_group_t, &displayio_group_type);
common_hal_displayio_group_construct(self, scale, args[ARG_x].u_int, args[ARG_y].u_int);
Expand Down Expand Up @@ -75,21 +76,21 @@ MP_PROPERTY_GETSET(displayio_group_hidden_obj,
(mp_obj_t)&displayio_group_get_hidden_obj,
(mp_obj_t)&displayio_group_set_hidden_obj);

//| scale: int
//| scale: int | float
//| """Scales each pixel within the Group in both directions. For example, when scale=2 each pixel
//| will be represented by 2x2 pixels."""
//| will be represented by 2x2 pixels. The LVGL compositor also supports fractional scales.
//| Different hardware can accelerate different fractional scales. It will pick the closest scale
//| that doesn't go over the given scale. Reading this property returns the requested scale."""
static mp_obj_t displayio_group_obj_get_scale(mp_obj_t self_in) {
displayio_group_t *self = native_group(self_in);
return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_scale(self));
return common_hal_displayio_group_get_scale(self);
}
MP_DEFINE_CONST_FUN_OBJ_1(displayio_group_get_scale_obj, displayio_group_obj_get_scale);

static mp_obj_t displayio_group_obj_set_scale(mp_obj_t self_in, mp_obj_t scale_obj) {
displayio_group_t *self = native_group(self_in);

mp_int_t scale = mp_arg_validate_int_min(mp_obj_get_int(scale_obj), 1, MP_QSTR_scale);

common_hal_displayio_group_set_scale(self, scale);
common_hal_displayio_group_set_scale(self, scale_obj);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(displayio_group_set_scale_obj, displayio_group_obj_set_scale);
Expand Down
7 changes: 4 additions & 3 deletions shared-bindings/displayio/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ extern const mp_obj_type_t displayio_group_type;

displayio_group_t *native_group(mp_obj_t group_obj);

void common_hal_displayio_group_construct(displayio_group_t *self, uint32_t scale, mp_int_t x, mp_int_t y);
uint32_t common_hal_displayio_group_get_scale(displayio_group_t *self);
void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scale);
// scale may be an int or float, depending on the implementation.
void common_hal_displayio_group_construct(displayio_group_t *self, mp_obj_t scale, mp_int_t x, mp_int_t y);
mp_obj_t common_hal_displayio_group_get_scale(displayio_group_t *self);
void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_obj_t scale);
bool common_hal_displayio_group_get_hidden(displayio_group_t *self);
void common_hal_displayio_group_set_hidden(displayio_group_t *self, bool hidden);
mp_int_t common_hal_displayio_group_get_x(displayio_group_t *self);
Expand Down
10 changes: 6 additions & 4 deletions shared-module/displayio/legacy/Group.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ static void check_readonly(displayio_group_t *self) {
}
}

void common_hal_displayio_group_construct(displayio_group_t *self, uint32_t scale, mp_int_t x, mp_int_t y) {
void common_hal_displayio_group_construct(displayio_group_t *self, mp_obj_t scale_obj, mp_int_t x, mp_int_t y) {
uint32_t scale = mp_arg_validate_int_range(mp_obj_get_int(scale_obj), 1, 32767, MP_QSTR_scale);
mp_obj_list_t *members = mp_obj_new_list(0, NULL);
displayio_group_construct(self, members, scale, x, y);
}
Expand Down Expand Up @@ -106,8 +107,8 @@ void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden)
}
}

uint32_t common_hal_displayio_group_get_scale(displayio_group_t *self) {
return self->scale;
mp_obj_t common_hal_displayio_group_get_scale(displayio_group_t *self) {
return MP_OBJ_NEW_SMALL_INT(self->scale);
}

bool displayio_group_get_previous_area(displayio_group_t *self, displayio_area_t *area) {
Expand Down Expand Up @@ -208,7 +209,8 @@ void displayio_group_update_transform(displayio_group_t *self,
_update_child_transforms(self);
}

void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scale) {
void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_obj_t scale_obj) {
uint32_t scale = mp_arg_validate_int_min(mp_obj_get_int(scale_obj), 1, MP_QSTR_scale);
if (self->scale == scale) {
return;
}
Expand Down
Loading
Loading