From 617d2463d1fd82f7bf5536b77196a0c98249f40b Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Thu, 19 Nov 2026 13:41:08 -0800 Subject: [PATCH 1/2] displayio: support fractional Group scales in LVGL LVGL supports scaling in 1/256th increments by storing scale as a fixed point Q8. This change allows scaling from 1/256 upwards. If the given float scale isn't exactly divisible by 1/256th then the next lowest Q8 value will be used internally. This prevents objects from getting bigger than expected. The requested scale will be read back so that it can be used in computing a subsequent scale. --- .../tests/test_displayio_fractional.py | 258 ++++++++++++++++++ shared-bindings/displayio/Group.c | 44 ++- shared-bindings/displayio/Group.h | 6 + shared-module/displayio/lvgl/Group.c | 58 ++-- shared-module/displayio/lvgl/Group.h | 9 +- shared-module/displayio/lvgl/TileGrid.c | 129 ++++++--- shared-module/displayio/lvgl/TileGrid.h | 2 +- shared-module/displayio/lvgl/lvgl_internal.h | 10 + shared-module/displayio/lvgl/lvgl_protocol.h | 8 +- shared-module/vectorio/lvgl/VectorShape.c | 60 ++-- shared-module/vectorio/lvgl/VectorShape.h | 12 +- shared-module/videocore/lvgl/VideoBuffer.c | 26 +- shared-module/videocore/lvgl/VideoBuffer.h | 3 + 13 files changed, 506 insertions(+), 119 deletions(-) create mode 100644 ports/zephyr-cp/tests/test_displayio_fractional.py diff --git a/ports/zephyr-cp/tests/test_displayio_fractional.py b/ports/zephyr-cp/tests/test_displayio_fractional.py new file mode 100644 index 00000000000..669b55e3208 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_fractional.py @@ -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() diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index 31b1b77f34f..e0de572ad69 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -17,26 +17,41 @@ //| 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.""" //| ... //| +#if CIRCUITPY_DISPLAYIO_LVGL +static mp_float_t _validate_scale(mp_obj_t scale_obj) { + mp_float_t scale = mp_arg_validate_obj_float_range(scale_obj, 0, 32767, MP_QSTR_scale); + if (scale < (mp_float_t)1 / LV_SCALE_NONE) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q out of range"), MP_QSTR_scale); + } + return scale; +} +#endif + 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); + #if CIRCUITPY_DISPLAYIO_LVGL + mp_float_t scale = _validate_scale(args[ARG_scale].u_obj); + #else + mp_int_t scale = mp_arg_validate_int_range(mp_obj_get_int(args[ARG_scale].u_obj), 1, 32767, MP_QSTR_scale); + #endif 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); @@ -75,19 +90,34 @@ 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); + #if CIRCUITPY_DISPLAYIO_LVGL + mp_float_t scale = common_hal_displayio_group_get_scale(self); + mp_int_t integer_scale = (mp_int_t)scale; + if (scale == integer_scale) { + return MP_OBJ_NEW_SMALL_INT(integer_scale); + } + return mp_obj_new_float(scale); + #else return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_scale(self)); + #endif } 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); + #if CIRCUITPY_DISPLAYIO_LVGL + mp_float_t scale = _validate_scale(scale_obj); + #else mp_int_t scale = mp_arg_validate_int_min(mp_obj_get_int(scale_obj), 1, MP_QSTR_scale); + #endif common_hal_displayio_group_set_scale(self, scale); return mp_const_none; diff --git a/shared-bindings/displayio/Group.h b/shared-bindings/displayio/Group.h index 2eb25215961..79aedb7f621 100644 --- a/shared-bindings/displayio/Group.h +++ b/shared-bindings/displayio/Group.h @@ -12,9 +12,15 @@ extern const mp_obj_type_t displayio_group_type; displayio_group_t *native_group(mp_obj_t group_obj); +#if CIRCUITPY_DISPLAYIO_LVGL +void common_hal_displayio_group_construct(displayio_group_t *self, mp_float_t scale, mp_int_t x, mp_int_t y); +mp_float_t common_hal_displayio_group_get_scale(displayio_group_t *self); +void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_float_t scale); +#else 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); +#endif 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); diff --git a/shared-module/displayio/lvgl/Group.c b/shared-module/displayio/lvgl/Group.c index 8580748d6e0..6c6f8af3aef 100644 --- a/shared-module/displayio/lvgl/Group.c +++ b/shared-module/displayio/lvgl/Group.c @@ -7,7 +7,7 @@ // LVGL-based displayio Group implementation. Each Group is mirrored into a plain lv_obj // container; members are appended as children so the lv object tree's child order matches // the displayio z-order (later members draw on top). Group scale is applied by scaling -// child positions and images directly. That keeps displayio's integer nearest-neighbor +// child positions and images directly. That keeps displayio's nearest-neighbor // scaling and per-child invalidation; LVGL transform styles use an ARGB8888 transform // layer and bilinear filtering instead. // @@ -83,11 +83,17 @@ void displayio_lvgl_unlock_with_cleanup(void) { // redraws the member's old and new area. (A group sized to its content would be redrawn // from its origin to its farthest member whenever a member moves.) // +static uint32_t _scale_to_q8(mp_float_t scale) { + // Keep the stored scale at or below the requested value. All Q8 steps are supported by + // LVGL's software renderer, so the closest one is the truncated fixed-point value. + return (uint32_t)(scale * LV_SCALE_NONE); +} + // Recompute members' inherited scale and screen positions after this group's scale or // position changed. Returns false when a member rejects the transform; some members may // already have taken it then, so callers restore the group's own transform and push again. static bool _update_children(displayio_group_t *self) { - uint16_t child_scale = self->lv_scale * self->scale; + uint32_t child_scale = displayio_lvgl_compose_scale(self->lv_scale, self->scale); for (size_t i = 0; i < self->members->len; i++) { mp_obj_t layer = self->members->items[i]; const lvgl_protocol_t *protocol = _layer_protocol(&layer); @@ -240,21 +246,22 @@ void displayio_lvgl_init(void) { displayio_lvgl_unlock(); } -void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, uint32_t scale, mp_int_t x, mp_int_t y) { +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_float_t scale, mp_int_t x, mp_int_t y) { self->x = x; self->y = y; self->members = members; self->item_removed = false; - self->scale = scale; + self->scale = _scale_to_q8(scale); + self->requested_scale = scale; self->in_group = false; self->readonly = false; self->lv_obj = NULL; - self->lv_scale = 1; + self->lv_scale = LV_SCALE_NONE; self->lv_abs_x = x; self->lv_abs_y = y; } -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_float_t scale, mp_int_t x, mp_int_t y) { mp_obj_list_t *members = mp_obj_new_list(0, NULL); displayio_group_construct(self, members, scale, x, y); } @@ -317,21 +324,29 @@ void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden) displayio_lvgl_unlock_with_cleanup(); } -uint32_t common_hal_displayio_group_get_scale(displayio_group_t *self) { - return self->scale; +mp_float_t common_hal_displayio_group_get_scale(displayio_group_t *self) { + return self->requested_scale; } -void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scale) { - if (self->scale == scale) { +void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_float_t scale) { + if (self->requested_scale == scale) { return; } check_readonly(self); + uint32_t scale_q8 = _scale_to_q8(scale); + if (self->scale == scale_q8) { + self->requested_scale = scale; + return; + } if (self->lv_obj == NULL) { - self->scale = scale; + self->scale = scale_q8; + self->requested_scale = scale; return; } - uint16_t previous_scale = self->scale; - self->scale = scale; + uint32_t previous_scale = self->scale; + mp_float_t previous_requested_scale = self->requested_scale; + self->scale = scale_q8; + self->requested_scale = scale; nlr_jump_callback_node_t cleanup; displayio_lvgl_lock_with_cleanup(&cleanup); bool accepted = _update_children(self); @@ -339,6 +354,7 @@ void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scal // A member can't be shown at this scale. Put the group back the way it was, push that // to the members that already took the new scale, and report it. self->scale = previous_scale; + self->requested_scale = previous_requested_scale; _update_children(self); } displayio_lvgl_unlock_with_cleanup(); @@ -357,7 +373,8 @@ void common_hal_displayio_group_set_x(displayio_group_t *self, mp_int_t x) { } check_readonly(self); // The origin is in screen pixels, so the move is scaled by the parent chain. - self->lv_abs_x += (x - self->x) * self->lv_scale; + self->lv_abs_x += displayio_lvgl_scale_coordinate(x, self->lv_scale) - + displayio_lvgl_scale_coordinate(self->x, self->lv_scale); self->x = x; if (self->lv_obj != NULL) { nlr_jump_callback_node_t cleanup; @@ -378,7 +395,8 @@ void common_hal_displayio_group_set_y(displayio_group_t *self, mp_int_t y) { } check_readonly(self); // The origin is in screen pixels, so the move is scaled by the parent chain. - self->lv_abs_y += (y - self->y) * self->lv_scale; + self->lv_abs_y += displayio_lvgl_scale_coordinate(y, self->lv_scale) - + displayio_lvgl_scale_coordinate(self->y, self->lv_scale); self->y = y; if (self->lv_obj != NULL) { nlr_jump_callback_node_t cleanup; @@ -421,7 +439,7 @@ static void _adopt_member(displayio_group_t *self, mp_obj_t layer) { if (nlr_push(&nlr) == 0) { if (protocol->set_transform != NULL && !protocol->set_transform(layer, self->lv_abs_x, self->lv_abs_y, - self->lv_scale * self->scale)) { + displayio_lvgl_compose_scale(self->lv_scale, self->scale))) { mp_raise_ValueError(MP_ERROR_TEXT("Layer does not support scaling")); } if (protocol->set_hidden_by_parent != NULL) { @@ -578,13 +596,13 @@ static void _group_protocol_detach(mp_obj_t layer) { } static bool _group_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, - uint16_t scale) { + uint32_t scale) { displayio_group_t *self = MP_OBJ_TO_PTR(layer); self->lv_scale = scale; // abs_x/y is where this group's parent origin is on the screen; shift by this group's // own position, scaled the same way as its members are. - self->lv_abs_x = abs_x + self->x * scale; - self->lv_abs_y = abs_y + self->y * scale; + self->lv_abs_x = abs_x + displayio_lvgl_scale_coordinate(self->x, scale); + self->lv_abs_y = abs_y + displayio_lvgl_scale_coordinate(self->y, scale); if (self->lv_obj == NULL) { return true; } @@ -669,7 +687,7 @@ void displayio_lvgl_set_root_group(displayio_group_t *group) { cp_lvgl_root_group = group; if (cp_lvgl_root_group != NULL) { // The root group's origin is its own position. - group->lv_scale = 1; + group->lv_scale = LV_SCALE_NONE; group->lv_abs_x = group->x; group->lv_abs_y = group->y; displayio_lvgl_group_attach(cp_lvgl_root_group, lv_screen_active()); diff --git a/shared-module/displayio/lvgl/Group.h b/shared-module/displayio/lvgl/Group.h index b94242545fc..8e01e4d6603 100644 --- a/shared-module/displayio/lvgl/Group.h +++ b/shared-module/displayio/lvgl/Group.h @@ -27,7 +27,10 @@ typedef struct { mp_obj_list_t *members; int16_t x; int16_t y; - uint16_t scale; + uint32_t scale; // Own scale in LVGL's Q8 format; LV_SCALE_NONE is 1x. + // Keep the requested value for Python readback. In particular, consecutive animation + // values may quantize to the same Q8 step but must not become stuck at the old value. + mp_float_t requested_scale; bool item_removed : 1; bool in_group : 1; bool hidden : 1; @@ -36,14 +39,14 @@ typedef struct { uint8_t padding : 3; // LVGL mirror state. lv_obj_t *lv_obj; // Plain lv_obj container created when the group is attached. - uint16_t lv_scale; // Scale inherited from the parent group for child positioning. + uint32_t lv_scale; // Inherited parent scale in Q8 format. // Absolute position of this group's coordinate origin. Children use it to keep their // own absolute position (needed by vectorio's contains()) in sync. int32_t lv_abs_x; int32_t lv_abs_y; } displayio_group_t; -void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, uint32_t scale, mp_int_t x, mp_int_t y); +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_float_t scale, mp_int_t x, mp_int_t y); void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden); // Shared LVGL compositor API. Implemented in shared-module/displayio/lvgl/Group.c. diff --git a/shared-module/displayio/lvgl/TileGrid.c b/shared-module/displayio/lvgl/TileGrid.c index 39b0d66f845..001973a61b7 100644 --- a/shared-module/displayio/lvgl/TileGrid.c +++ b/shared-module/displayio/lvgl/TileGrid.c @@ -393,10 +393,16 @@ static bool _source_is_indexed(displayio_tilegrid_t *self) { return LV_COLOR_FORMAT_IS_INDEXED(_source_cf(self->bitmap, self->pixel_shader)); } +static bool _has_fractional_scale(displayio_tilegrid_t *self) { + return (self->lv_scale & (LV_SCALE_NONE - 1)) != 0; +} + // A single cell showing a direct color source is exactly the source pixels, so there is nothing -// to compose and nothing to copy. +// to compose and nothing to copy. Fractional scales use a composed image with one duplicated +// bottom/right pixel, because LVGL's nearest-neighbor transform otherwise omits those edges. static bool _wants_direct(displayio_tilegrid_t *self) { - return self->width_in_tiles == 1 && self->height_in_tiles == 1 && !_source_is_indexed(self); + return self->width_in_tiles == 1 && self->height_in_tiles == 1 && + !_source_is_indexed(self) && !_has_fractional_scale(self); } // Point the source descriptor at the whole bitmap. Caller holds the lock and has no source bound. @@ -411,7 +417,7 @@ static bool _bind_source(displayio_tilegrid_t *self) { } // LVGL can't transform a partially decoded image, but a composed grid draws its cells // unscaled and scales the views of its own buffer instead. - self->lv_src_odb_full = self->lv_direct && self->lv_scale != 1; + self->lv_src_odb_full = self->lv_direct && self->lv_scale != LV_SCALE_NONE; self->lv_src = displayio_ondiskbitmap_lv_acquire(odb, self->lv_src_odb_full, self->lv_obj); self->lv_argb = _source_has_alpha(self, cf); return self->lv_src != NULL; @@ -490,21 +496,22 @@ static void _set_lv_pos(displayio_tilegrid_t *self) { if (self->lv_obj == NULL) { return; } - _place(self->lv_obj, self->lv_origin_x + self->x * self->lv_scale, - self->lv_origin_y + self->y * self->lv_scale); + _place(self->lv_obj, self->lv_origin_x + displayio_lvgl_scale_coordinate(self->x, self->lv_scale), + self->lv_origin_y + displayio_lvgl_scale_coordinate(self->y, self->lv_scale)); } // Scale a view from its top left corner. Moving a pivot makes LVGL lay out the whole screen, and // the pivot only matters once the view is scaled. -static void _set_view_scale(lv_obj_t *img, uint16_t scale) { - if (scale != 1) { +static void _set_view_scale(lv_obj_t *img, uint32_t scale) { + if (scale != LV_SCALE_NONE) { lv_image_set_pivot(img, 0, 0); } - lv_image_set_scale(img, (int32_t)scale * LV_SCALE_NONE); + lv_image_set_antialias(img, false); + lv_image_set_scale(img, scale); } -// Create one view: an lv_image that shows one window of the composed buffer, scaled about its top -// left corner like the legacy compositor's whole-pixel scaling. +// Create one view: an lv_image that shows one window of the composed buffer, +// scaled about its top left corner. static lv_obj_t *_create_view(displayio_tilegrid_t *self, lv_obj_t *parent) { lv_obj_t *img = lv_image_create(parent); if (img == NULL) { @@ -513,7 +520,6 @@ static lv_obj_t *_create_view(displayio_tilegrid_t *self, lv_obj_t *parent) { lv_obj_clear_flag(img, LV_OBJ_FLAG_CLICKABLE); lv_obj_clear_flag(img, LV_OBJ_FLAG_SCROLLABLE); lv_image_set_inner_align(img, LV_IMAGE_ALIGN_TOP_LEFT); - lv_image_set_antialias(img, false); _set_view_scale(img, self->lv_scale); return img; } @@ -550,8 +556,8 @@ static bool _create_objects(displayio_tilegrid_t *self, lv_obj_t *parent) { lv_obj_add_flag(container, LV_OBJ_FLAG_OVERFLOW_VISIBLE); lv_obj_clear_flag(container, LV_OBJ_FLAG_CLICKABLE); lv_obj_clear_flag(container, LV_OBJ_FLAG_SCROLLABLE); - lv_obj_set_size(container, self->pixel_width * self->lv_scale, - self->pixel_height * self->lv_scale); + lv_obj_set_size(container, displayio_lvgl_scale_coordinate(self->pixel_width, self->lv_scale), + displayio_lvgl_scale_coordinate(self->pixel_height, self->lv_scale)); self->lv_obj = container; return _ensure_canvas(self); } @@ -578,8 +584,11 @@ static void _destroy_objects(displayio_tilegrid_t *self) { // --- The composed buffer --------------------------------------------------- static bool _ensure_buffer(displayio_tilegrid_t *self) { - uint32_t w = self->pixel_width; - uint32_t h = self->pixel_height; + // Fractional transforms otherwise omit the outermost source pixels. Reserve one extra + // right column and bottom row; composition duplicates the edges into them so LVGL's + // center-based sampling covers the whole scaled image. + uint32_t w = self->pixel_width + (_has_fractional_scale(self) ? 1 : 0); + uint32_t h = self->pixel_height + (_has_fractional_scale(self) ? 1 : 0); if (w == 0 || h == 0 || self->lv_obj == NULL) { return false; } @@ -665,10 +674,10 @@ static void _layout_views(displayio_tilegrid_t *self) { _tile_origin(self, _tile_index(self, 0), &x0, &y0); lv_image_set_src(self->lv_views[0], self->lv_src != NULL && self->lv_src->data != NULL ? self->lv_src : NULL); - lv_image_set_offset_x(self->lv_views[0], -(int32_t)x0 * self->lv_scale); - lv_image_set_offset_y(self->lv_views[0], -(int32_t)y0 * self->lv_scale); - lv_obj_set_size(self->lv_views[0], self->tile_width * self->lv_scale, - self->tile_height * self->lv_scale); + lv_image_set_offset_x(self->lv_views[0], -displayio_lvgl_scale_coordinate(x0, self->lv_scale)); + lv_image_set_offset_y(self->lv_views[0], -displayio_lvgl_scale_coordinate(y0, self->lv_scale)); + lv_obj_set_size(self->lv_views[0], displayio_lvgl_scale_coordinate(self->tile_width, self->lv_scale), + displayio_lvgl_scale_coordinate(self->tile_height, self->lv_scale)); lv_obj_clear_flag(self->lv_views[0], LV_OBJ_FLAG_HIDDEN); _place(self->lv_views[0], 0, 0); for (uint8_t i = 1; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { @@ -707,24 +716,34 @@ static void _layout_views(displayio_tilegrid_t *self) { lv_memzero(dsc, sizeof(*dsc)); dsc->header.magic = LV_IMAGE_HEADER_MAGIC; dsc->header.cf = cf; - dsc->header.w = cols[c].len; - dsc->header.h = rows[r].len; + uint32_t src_w = cols[c].len; + uint32_t src_h = rows[r].len; + if (_has_fractional_scale(self)) { + // Only the run touching the composed image's bottom/right owns + // the duplicated padding pixels. + src_w += cols[c].src + cols[c].len == grid_w; + src_h += rows[r].src + rows[r].len == grid_h; + } + dsc->header.w = src_w; + dsc->header.h = src_h; dsc->header.stride = stride; dsc->data = self->lv_buf->data + rows[r].src * stride + cols[c].src * bytes_per_pixel; - dsc->data_size = stride * rows[r].len; + dsc->data_size = stride * src_h; lv_obj_t *img = self->lv_views[view]; lv_image_set_src(img, dsc); lv_image_set_offset_x(img, 0); lv_image_set_offset_y(img, 0); - lv_obj_set_size(img, cols[c].len * self->lv_scale, rows[r].len * self->lv_scale); + lv_obj_set_size(img, displayio_lvgl_scale_coordinate(cols[c].len, self->lv_scale), + displayio_lvgl_scale_coordinate(rows[r].len, self->lv_scale)); lv_obj_clear_flag(img, LV_OBJ_FLAG_HIDDEN); - _place(img, cols[c].dst * self->lv_scale, rows[r].dst * self->lv_scale); + _place(img, displayio_lvgl_scale_coordinate(cols[c].dst, self->lv_scale), + displayio_lvgl_scale_coordinate(rows[r].dst, self->lv_scale)); DISPLAYIO_TILEGRID_DEBUG( "views %p view %d buf %p cf %d %dx%d stride %lu data %p at (%d,%d)", self, view, self->lv_buf->data, (int)cf, cols[c].len, rows[r].len, - (unsigned long)stride, (void *)dsc->data, (int)(cols[c].dst * self->lv_scale), - (int)(rows[r].dst * self->lv_scale)); + (unsigned long)stride, (void *)dsc->data, (int)displayio_lvgl_scale_coordinate(cols[c].dst, self->lv_scale), + (int)displayio_lvgl_scale_coordinate(rows[r].dst, self->lv_scale)); view++; } } @@ -760,6 +779,30 @@ static void _union_area(lv_area_t *area, const lv_area_t *other) { } } +// LVGL's nearest-neighbor extent is based on the outer source-pixel centers. +// Duplicate the final source row and column into one pixel of padding so the +// real edges are sampled at fractional scales. This is one pixel per composed +// TileGrid, not per atlas tile. +static void _duplicate_fractional_edges(displayio_tilegrid_t *self, lv_area_t *dirty) { + if (!_has_fractional_scale(self)) { + return; + } + uint32_t w = self->pixel_width; + uint32_t h = self->pixel_height; + uint32_t bpp = lv_color_format_get_size(self->lv_buf->header.cf); + uint32_t stride = self->lv_buf->header.stride; + uint8_t *data = self->lv_buf->data; + for (uint32_t y = 0; y < h; y++) { + memcpy(data + y * stride + w * bpp, data + y * stride + (w - 1) * bpp, bpp); + } + memcpy(data + h * stride, data + (h - 1) * stride, (w + 1) * bpp); + + lv_area_t right = {(int32_t)w, 0, (int32_t)w, (int32_t)h}; + lv_area_t bottom = {0, (int32_t)h, (int32_t)w, (int32_t)h}; + _union_area(dirty, &right); + _union_area(dirty, &bottom); +} + // Cells whose tile overlaps the changed source rect need composing. This is the inverse of the // tile map; the terminal's 560 byte compares are free next to a frame. static void _mark_cells_for_source(displayio_tilegrid_t *self, int32_t x1, int32_t y1, @@ -810,10 +853,10 @@ static void _cell_display_area(displayio_tilegrid_t *self, uint32_t x, uint32_t if (py < 0) { py += grid_h; } - area->x1 = px * self->lv_scale; - area->y1 = py * self->lv_scale; - area->x2 = area->x1 + self->tile_width * self->lv_scale - 1; - area->y2 = area->y1 + self->tile_height * self->lv_scale - 1; + area->x1 = displayio_lvgl_scale_coordinate(px, self->lv_scale); + area->y1 = displayio_lvgl_scale_coordinate(py, self->lv_scale); + area->x2 = displayio_lvgl_scale_coordinate(px + self->tile_width, self->lv_scale) - 1; + area->y2 = displayio_lvgl_scale_coordinate(py + self->tile_height, self->lv_scale) - 1; } // The most cells a compose pass queues before drawing them. LVGL walks the whole task list to add @@ -969,6 +1012,7 @@ static bool _compose(displayio_tilegrid_t *self, lv_area_t *dirty) { lv_canvas_finish_layer(self->lv_canvas, &layer); if (any) { + _duplicate_fractional_edges(self, &buf_dirty); // The compose pass may have run in software, so write those lines out before a DMA // consumer (the display flush, or the PPA on the display pass) reads the buffer. lv_draw_buf_flush_cache(self->lv_buf, &buf_dirty); @@ -1163,8 +1207,8 @@ static bool _rebind(displayio_tilegrid_t *self) { // rebinding their bitmap. Keep the mirror geometry in sync too: resizing the canvas // or its views does not resize or move their container, and LVGL clips invalidation // to that container even when OVERFLOW_VISIBLE is set. - lv_obj_set_size(self->lv_obj, self->pixel_width * self->lv_scale, - self->pixel_height * self->lv_scale); + lv_obj_set_size(self->lv_obj, displayio_lvgl_scale_coordinate(self->pixel_width, self->lv_scale), + displayio_lvgl_scale_coordinate(self->pixel_height, self->lv_scale)); _set_lv_pos(self); _mark_all(self); _mark_views(self); @@ -1220,15 +1264,24 @@ void displayio_lvgl_tilegrid_update_scale(displayio_tilegrid_t *self) { } nlr_jump_callback_node_t cleanup; displayio_lvgl_lock_with_cleanup(&cleanup); - if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type) && self->lv_direct && - (self->lv_scale != 1) != self->lv_src_odb_full) { + bool was_direct = self->lv_direct; + if (was_direct != _wants_direct(self)) { + // Fractional direct images need the composed path's edge padding. + _unbind_source(self); + if (!was_direct && self->lv_buf != NULL) { + lv_draw_buf_destroy(self->lv_buf); + self->lv_buf = NULL; + } + _rebind(self); + } else if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type) && self->lv_direct && + (self->lv_scale != LV_SCALE_NONE) != self->lv_src_odb_full) { // Re-source: whether the file's rows can be streamed changed with the scale. _unbind_source(self); _bind_source(self); } if (!self->lv_direct) { - lv_obj_set_size(self->lv_obj, self->pixel_width * self->lv_scale, - self->pixel_height * self->lv_scale); + lv_obj_set_size(self->lv_obj, displayio_lvgl_scale_coordinate(self->pixel_width, self->lv_scale), + displayio_lvgl_scale_coordinate(self->pixel_height, self->lv_scale)); } for (uint8_t i = 0; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { if (self->lv_views[i] != NULL) { @@ -1268,7 +1321,7 @@ static void _tilegrid_protocol_detach(mp_obj_t layer) { } static bool _tilegrid_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, - uint16_t scale) { + uint32_t scale) { displayio_tilegrid_t *self = MP_OBJ_TO_PTR(layer); // A TileGrid has no position or scale of its own: it places itself from the parent // origin plus its own x/y, and renders at the scale of the parent chain. @@ -1372,7 +1425,7 @@ void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_ self->lv_view_count = 0; self->lv_next = NULL; self->lv_bitmap_next = NULL; - self->lv_scale = 1; + self->lv_scale = LV_SCALE_NONE; self->lv_origin_x = 0; self->lv_origin_y = 0; for (uint8_t i = 0; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { diff --git a/shared-module/displayio/lvgl/TileGrid.h b/shared-module/displayio/lvgl/TileGrid.h index 1b97e55c8e2..045847536f2 100644 --- a/shared-module/displayio/lvgl/TileGrid.h +++ b/shared-module/displayio/lvgl/TileGrid.h @@ -88,7 +88,7 @@ typedef struct displayio_tilegrid { bool lv_src_dirty; struct displayio_tilegrid *lv_next; // The compositor's list of attached TileGrids. struct displayio_tilegrid *lv_bitmap_next; // The Bitmap's list of grids drawing it. - uint16_t lv_scale; // Scale inherited from the parent group. + uint32_t lv_scale; // Scale inherited from the parent group, in Q8 format. int32_t lv_origin_x; // The parent group's origin in screen pixels. int32_t lv_origin_y; } displayio_tilegrid_t; diff --git a/shared-module/displayio/lvgl/lvgl_internal.h b/shared-module/displayio/lvgl/lvgl_internal.h index 8facf95c06f..340a40f8d52 100644 --- a/shared-module/displayio/lvgl/lvgl_internal.h +++ b/shared-module/displayio/lvgl/lvgl_internal.h @@ -24,6 +24,16 @@ #if CIRCUITPY_DISPLAYIO_LVGL +// LVGL image scales are Q8 fixed point: 256 means 1x. Use a 64-bit intermediate +// so nested groups and large coordinates cannot overflow the multiplication. +static inline int32_t displayio_lvgl_scale_coordinate(int32_t value, uint32_t scale) { + return (int32_t)(((int64_t)value * scale + (LV_SCALE_NONE / 2)) >> 8); +} + +static inline uint32_t displayio_lvgl_compose_scale(uint32_t parent, uint32_t own) { + return (uint32_t)(((uint64_t)parent * own + (LV_SCALE_NONE / 2)) >> 8); +} + // Port hook: initialize LVGL itself and its tick source. Implemented by each port's // common-hal and safe to call repeatedly. void displayio_lvgl_port_init(void); diff --git a/shared-module/displayio/lvgl/lvgl_protocol.h b/shared-module/displayio/lvgl/lvgl_protocol.h index daaa08cfa02..a14b3a823ab 100644 --- a/shared-module/displayio/lvgl/lvgl_protocol.h +++ b/shared-module/displayio/lvgl/lvgl_protocol.h @@ -53,10 +53,10 @@ typedef struct _lvgl_protocol_t { // Push the inherited transform. abs_x/abs_y is the parent group's origin in screen // pixels and scale is the composed scale of the whole ancestor chain; this layer's own - // position and scale are not applied here. Return false when the layer can't be shown - // that way (a VideoBuffer can't be scaled, for example); Group then restores its own - // transform, pushes that again, and raises ValueError. - bool (*set_transform)(mp_obj_t self, int32_t abs_x, int32_t abs_y, uint16_t scale); + // position and scale are not applied here. scale is the composed scale in Q8 format + // (LV_SCALE_NONE is 1x). Return false when the layer can't be shown that way; Group then + // restores its own transform, pushes that again, and raises ValueError. + bool (*set_transform)(mp_obj_t self, int32_t abs_x, int32_t abs_y, uint32_t scale); // Propagate an ancestor's hidden state. NULL means always drawn while attached. void (*set_hidden_by_parent)(mp_obj_t self, bool hidden); diff --git a/shared-module/vectorio/lvgl/VectorShape.c b/shared-module/vectorio/lvgl/VectorShape.c index 7503177a216..80813e80e2a 100644 --- a/shared-module/vectorio/lvgl/VectorShape.c +++ b/shared-module/vectorio/lvgl/VectorShape.c @@ -173,10 +173,10 @@ static void _update_visible(vectorio_vector_shape_t *self) { // Rebuild the polygon coverage mask. LVGL treats an A8 image as a mask and blends it with // the image recolor color, so the mask only needs to say where the shape is, not what color -// it is. Group scale is baked into the mask resolution, which reproduces displayio's -// nearest-neighbor sampling of the shape. +// it is. Group scale is baked into the mask resolution, including fractional Q8 scales, which +// reproduces displayio's nearest-neighbor sampling of the shape. static void _refresh_mask(vectorio_vector_shape_t *self, int32_t x1, int32_t y1, int32_t w, - int32_t h, uint16_t scale) { + int32_t h, uint32_t scale) { if (self->mask == NULL || (int32_t)self->mask->header.w != w || (int32_t)self->mask->header.h != h) { // The image borrows the mask. Clear its source before freeing the old buffer: // if the replacement allocation fails it must draw nothing, not freed memory. @@ -197,11 +197,10 @@ static void _refresh_mask(vectorio_vector_shape_t *self, int32_t x1, int32_t y1, uint32_t stride = self->mask->header.stride; for (int32_t y = 0; y < h; y++) { uint8_t *row = self->mask->data + (uint32_t)y * stride; - int16_t shape_y = y1 + y / scale; + int16_t shape_y = y1 + (int64_t)y * LV_SCALE_NONE / scale; for (int32_t x = 0; x < w; x++) { - row[x] = self->ishape.get_pixel(self->ishape.shape, x1 + x / scale, shape_y) == 0 - ? 0 - : COVERAGE_OPAQUE; + row[x] = self->ishape.get_pixel(self->ishape.shape, + x1 + (int64_t)x * LV_SCALE_NONE / scale, shape_y) == 0 ? 0 : COVERAGE_OPAQUE; } } } @@ -227,13 +226,14 @@ static void _sync_mirror(vectorio_vector_shape_t *self, bool rebuild_mask) { int32_t x2; int32_t y2; _shape_bounds(self, &x1, &y1, &x2, &y2); - uint16_t scale = self->abs_scale == 0 ? 1 : self->abs_scale; - // The bounds are inclusive, so the extent is one more than the difference. - int32_t w = (x2 - x1 + 1) * scale; - int32_t h = (y2 - y1 + 1) * scale; - // A shape with an empty extent (a rectangle with no width or height) covers no - // pixels at all, so it must not be painted. - bool empty = w < 1 || h < 1; + uint32_t scale = self->abs_scale == 0 ? LV_SCALE_NONE : self->abs_scale; + int32_t source_w = x2 - x1 + 1; + int32_t source_h = y2 - y1 + 1; + // A shape with an empty source extent (a rectangle with no width or height) covers no + // pixels at all. A nonempty shape still gets one output pixel when scaled far down. + bool empty = source_w < 1 || source_h < 1; + int32_t w = displayio_lvgl_scale_coordinate(source_w, scale); + int32_t h = displayio_lvgl_scale_coordinate(source_h, scale); if (w < 1) { w = 1; } @@ -244,8 +244,8 @@ static void _sync_mirror(vectorio_vector_shape_t *self, bool rebuild_mask) { bool polygon = _is_polygon(self); // abs_x/y is the parent group's origin on the screen; groups are screen sized // objects at the screen origin, so members are positioned in screen coordinates. - _place(self->lv_obj, self->abs_x + (self->x + x1) * scale, - self->abs_y + (self->y + y1) * scale); + _place(self->lv_obj, self->abs_x + displayio_lvgl_scale_coordinate(self->x + x1, scale), + self->abs_y + displayio_lvgl_scale_coordinate(self->y + y1, scale)); if (lv_obj_get_width(self->lv_obj) != w || lv_obj_get_height(self->lv_obj) != h) { lv_obj_set_size(self->lv_obj, w, h); // A new size means a new mask buffer too. @@ -271,14 +271,16 @@ static void _sync_mirror(vectorio_vector_shape_t *self, bool rebuild_mask) { // scaled group scales the object's size, so the shape's own pixels have to be scaled too // to keep the corners and the outline proportional. bool circle = mp_obj_is_type(self->ishape.shape, &vectorio_circle_type); - int32_t radius = circle ? LV_RADIUS_CIRCLE : (int32_t)_shape_radius(self) * scale; + int32_t radius = circle ? LV_RADIUS_CIRCLE : + displayio_lvgl_scale_coordinate(_shape_radius(self), scale); uint16_t outline_width = _shape_outline_width(self); lv_color_t outline_color; bool outlined = !empty && outline_width > 0 && _resolve_color(self->pixel_shader, _biased_outline_color_index(self), &outline_color); lv_obj_set_style_radius(self->lv_obj, radius, 0); - lv_obj_set_style_border_width(self->lv_obj, outlined ? (int32_t)outline_width * scale : 0, 0); + lv_obj_set_style_border_width(self->lv_obj, + outlined ? displayio_lvgl_scale_coordinate(outline_width, scale) : 0, 0); if (painted) { lv_obj_set_style_bg_color(self->lv_obj, color, 0); } @@ -321,7 +323,7 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, self->ishape = ishape; self->abs_x = 0; self->abs_y = 0; - self->abs_scale = 1; + self->abs_scale = LV_SCALE_NONE; self->lv_obj = NULL; self->mask = NULL; self->hidden = false; @@ -336,8 +338,8 @@ void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) { _set_dirty(vector_shape, true); } -static int32_t _screen_to_shape(int32_t screen, int32_t origin, uint16_t scale) { - int32_t relative = screen - origin; +static int32_t _screen_to_shape(int32_t screen, int32_t origin, uint32_t scale) { + int64_t relative = ((int64_t)screen - origin) * LV_SCALE_NONE; if (relative < 0) { // Floor division so that negative coordinates map to the pixel that contains them. return -((-relative + scale - 1) / scale); @@ -348,9 +350,11 @@ static int32_t _screen_to_shape(int32_t screen, int32_t origin, uint16_t scale) bool common_hal_vectorio_vector_shape_contains(vectorio_vector_shape_t *self, mp_int_t x, mp_int_t y) { x = mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x); y = mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y); - uint16_t scale = self->abs_scale == 0 ? 1 : self->abs_scale; - int16_t shape_x = (int16_t)(_screen_to_shape(x, self->abs_x, scale) - self->x); - int16_t shape_y = (int16_t)(_screen_to_shape(y, self->abs_y, scale) - self->y); + uint32_t scale = self->abs_scale == 0 ? LV_SCALE_NONE : self->abs_scale; + int32_t origin_x = self->abs_x + displayio_lvgl_scale_coordinate(self->x, scale); + int32_t origin_y = self->abs_y + displayio_lvgl_scale_coordinate(self->y, scale); + int16_t shape_x = (int16_t)_screen_to_shape(x, origin_x, scale); + int16_t shape_y = (int16_t)_screen_to_shape(y, origin_y, scale); return self->ishape.get_pixel(self->ishape.shape, shape_x, shape_y) != 0; } @@ -491,14 +495,14 @@ void displayio_lvgl_vector_shape_detach(vectorio_vector_shape_t *self) { } void displayio_lvgl_vector_shape_set_transform(vectorio_vector_shape_t *self, int32_t abs_x, - int32_t abs_y, uint16_t scale) { + int32_t abs_y, uint32_t scale) { if (self->abs_x == abs_x && self->abs_y == abs_y && self->abs_scale == scale) { return; } self->abs_x = abs_x; self->abs_y = abs_y; - uint16_t previous_scale = self->abs_scale; - self->abs_scale = scale == 0 ? 1 : scale; + uint32_t previous_scale = self->abs_scale; + self->abs_scale = scale == 0 ? LV_SCALE_NONE : scale; // The mask is rasterized at the scaled resolution, so a scale change rebuilds it. _set_dirty(self, self->abs_scale != previous_scale); } @@ -533,7 +537,7 @@ static void _vector_shape_protocol_detach(mp_obj_t layer) { } static bool _vector_shape_protocol_set_transform(mp_obj_t layer, int32_t abs_x, - int32_t abs_y, uint16_t scale) { + int32_t abs_y, uint32_t scale) { displayio_lvgl_vector_shape_set_transform(MP_OBJ_TO_PTR(layer), abs_x, abs_y, scale); return true; } diff --git a/shared-module/vectorio/lvgl/VectorShape.h b/shared-module/vectorio/lvgl/VectorShape.h index 55050cabfeb..3aac6ac0bfb 100644 --- a/shared-module/vectorio/lvgl/VectorShape.h +++ b/shared-module/vectorio/lvgl/VectorShape.h @@ -28,10 +28,10 @@ // winding rule, tinted with the image recolor styles. The mask is rebuilt when the // shape changes, so a frame costs one A8 blend. // -// Group scale is applied by scaling the mirror's position, size and mask resolution -// instead of using lv_obj transform styles. Transform styles route the object through an -// ARGB8888 transform layer that cannot be sliced, which would defeat partial redraw and -// replace displayio's integer nearest-neighbor scaling with bilinear filtering. See +// Group scale is applied in Q8 fixed point by scaling the mirror's position, size and mask +// resolution instead of using lv_obj transform styles. Transform styles route the object through an +// ARGB8888 transform layer that cannot be sliced, which would defeat partial redraw and replace +// displayio's nearest-neighbor scaling with bilinear filtering. See // shared-module/displayio/lvgl/Group.c for the same reasoning applied to TileGrids. typedef struct { @@ -45,7 +45,7 @@ typedef struct { // in screen coordinates from these. int32_t abs_x; int32_t abs_y; - uint16_t abs_scale; + uint32_t abs_scale; lv_obj_t *lv_obj; // Mirror: lv_obj for rectangles and circles, lv_image for polygons. lv_draw_buf_t *mask; // Polygon coverage (A8), NULL for the style-drawn shapes. bool hidden : 1; @@ -59,7 +59,7 @@ typedef struct { void displayio_lvgl_vector_shape_attach(vectorio_vector_shape_t *self, lv_obj_t *parent); void displayio_lvgl_vector_shape_detach(vectorio_vector_shape_t *self); void displayio_lvgl_vector_shape_set_transform(vectorio_vector_shape_t *self, int32_t abs_x, - int32_t abs_y, uint16_t scale); + int32_t abs_y, uint32_t scale); void displayio_lvgl_vector_shape_set_hidden_by_parent(vectorio_vector_shape_t *self, bool hidden); // A pixel shader this shape registered with has changed. void displayio_lvgl_vector_shape_shader_changed(vectorio_vector_shape_t *self); diff --git a/shared-module/videocore/lvgl/VideoBuffer.c b/shared-module/videocore/lvgl/VideoBuffer.c index 4255cd6ca21..7d78dcce78f 100644 --- a/shared-module/videocore/lvgl/VideoBuffer.c +++ b/shared-module/videocore/lvgl/VideoBuffer.c @@ -169,11 +169,13 @@ static void _sync_mirror(videocore_videobuffer_obj_t *self) { lv_image_set_src(self->backend.lv_obj, &self->backend.lv_dsc); } - // Group scaling is refused before it can change anything (see - // _video_protocol_set_transform), so the position is the parent's origin - // plus the video's own position. - const int32_t x = self->absolute_transform.x + self->x; - const int32_t y = self->absolute_transform.y + self->y; + // The position is the parent's origin plus the video's scaled own position. + // LVGL applies the scale to the image itself, using an intermediate buffer when the + // draw unit needs one, while the video source keeps writing unscaled frames. + const int32_t x = self->absolute_transform.x + + displayio_lvgl_scale_coordinate(self->x, self->backend.lv_scale); + const int32_t y = self->absolute_transform.y + + displayio_lvgl_scale_coordinate(self->y, self->backend.lv_scale); if (lv_obj_get_x(self->backend.lv_obj) != x || lv_obj_get_y(self->backend.lv_obj) != y) { if (!was_hidden) { lv_obj_invalidate(self->backend.lv_obj); @@ -187,6 +189,7 @@ static void _sync_mirror(videocore_videobuffer_obj_t *self) { } lv_obj_set_size(self->backend.lv_obj, self->source_width, self->source_height); } + lv_image_set_scale(self->backend.lv_obj, self->backend.lv_scale); if (was_hidden) { lv_obj_clear_flag(self->backend.lv_obj, LV_OBJ_FLAG_HIDDEN); } @@ -200,6 +203,7 @@ void videocore_videobuffer_backend_construct(videocore_videobuffer_obj_t *self) self->backend.lv_width = 0; self->backend.lv_height = 0; self->backend.lv_stride = 0; + self->backend.lv_scale = LV_SCALE_NONE; self->backend.lv_next = NULL; self->backend.lv_registered = false; self->backend.lv_pending = false; @@ -349,21 +353,19 @@ static void _video_protocol_detach(mp_obj_t layer) { } static bool _video_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, - uint16_t scale) { + uint32_t scale) { videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); - // displayio's scale is whole pixels; LVGL's image scaling would change - // that and take the video off the draw unit's fast path. - if (scale != 1) { - return false; - } nlr_jump_callback_node_t cleanup; displayio_lvgl_lock_with_cleanup(&cleanup); + self->backend.lv_scale = scale; displayio_buffer_transform_t transform = { .x = abs_x, .y = abs_y, .dx = 1, .dy = 1, - .scale = scale, + // The shared streaming code uses scale as a shown/hidden marker and moves the + // unscaled DMA destination. The LVGL mirror holds the actual Q8 scale. + .scale = scale == 0 ? 0 : 1, }; self->absolute_transform = transform; videocore_videobuffer_update_area(self); diff --git a/shared-module/videocore/lvgl/VideoBuffer.h b/shared-module/videocore/lvgl/VideoBuffer.h index aef6fc0f783..5f7a4f2b65b 100644 --- a/shared-module/videocore/lvgl/VideoBuffer.h +++ b/shared-module/videocore/lvgl/VideoBuffer.h @@ -36,6 +36,9 @@ typedef struct { uint16_t lv_width; uint16_t lv_height; uint16_t lv_stride; + // Inherited Group scale in Q8 format. LVGL transforms the sampled image through an + // intermediate buffer when needed, leaving the source destination unscaled. + uint32_t lv_scale; // The compositor's list of attached mirrors, rooted during GC. struct _videocore_videobuffer_obj_t *lv_next; bool lv_registered; From d1142d22db7501d6b79f1cd6576473d342d7462b Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 20 Nov 2026 11:26:55 -0800 Subject: [PATCH 2/2] Store scale as mp_obj_t for flexibility and correctness --- shared-bindings/displayio/Group.c | 35 +++----------------------- shared-bindings/displayio/Group.h | 13 +++------- shared-module/displayio/legacy/Group.c | 10 +++++--- shared-module/displayio/lvgl/Group.c | 23 +++++++++++------ shared-module/displayio/lvgl/Group.h | 4 +-- 5 files changed, 31 insertions(+), 54 deletions(-) diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index e0de572ad69..a78b61f4ff4 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -27,16 +27,6 @@ //| :param int y: Initial y position within the parent.""" //| ... //| -#if CIRCUITPY_DISPLAYIO_LVGL -static mp_float_t _validate_scale(mp_obj_t scale_obj) { - mp_float_t scale = mp_arg_validate_obj_float_range(scale_obj, 0, 32767, MP_QSTR_scale); - if (scale < (mp_float_t)1 / LV_SCALE_NONE) { - mp_raise_ValueError_varg(MP_ERROR_TEXT("%q out of range"), MP_QSTR_scale); - } - return scale; -} -#endif - 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[] = { @@ -47,11 +37,7 @@ static mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg 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); - #if CIRCUITPY_DISPLAYIO_LVGL - mp_float_t scale = _validate_scale(args[ARG_scale].u_obj); - #else - mp_int_t scale = mp_arg_validate_int_range(mp_obj_get_int(args[ARG_scale].u_obj), 1, 32767, MP_QSTR_scale); - #endif + 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); @@ -97,29 +83,14 @@ MP_PROPERTY_GETSET(displayio_group_hidden_obj, //| 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); - #if CIRCUITPY_DISPLAYIO_LVGL - mp_float_t scale = common_hal_displayio_group_get_scale(self); - mp_int_t integer_scale = (mp_int_t)scale; - if (scale == integer_scale) { - return MP_OBJ_NEW_SMALL_INT(integer_scale); - } - return mp_obj_new_float(scale); - #else - return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_group_get_scale(self)); - #endif + 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); - #if CIRCUITPY_DISPLAYIO_LVGL - mp_float_t scale = _validate_scale(scale_obj); - #else - mp_int_t scale = mp_arg_validate_int_min(mp_obj_get_int(scale_obj), 1, MP_QSTR_scale); - #endif - - 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); diff --git a/shared-bindings/displayio/Group.h b/shared-bindings/displayio/Group.h index 79aedb7f621..0cae6b4e789 100644 --- a/shared-bindings/displayio/Group.h +++ b/shared-bindings/displayio/Group.h @@ -12,15 +12,10 @@ extern const mp_obj_type_t displayio_group_type; displayio_group_t *native_group(mp_obj_t group_obj); -#if CIRCUITPY_DISPLAYIO_LVGL -void common_hal_displayio_group_construct(displayio_group_t *self, mp_float_t scale, mp_int_t x, mp_int_t y); -mp_float_t common_hal_displayio_group_get_scale(displayio_group_t *self); -void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_float_t scale); -#else -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); -#endif +// 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); diff --git a/shared-module/displayio/legacy/Group.c b/shared-module/displayio/legacy/Group.c index 01fb441672f..ae0a01320f2 100644 --- a/shared-module/displayio/legacy/Group.c +++ b/shared-module/displayio/legacy/Group.c @@ -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); } @@ -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) { @@ -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; } diff --git a/shared-module/displayio/lvgl/Group.c b/shared-module/displayio/lvgl/Group.c index 6c6f8af3aef..28a4f25dc99 100644 --- a/shared-module/displayio/lvgl/Group.c +++ b/shared-module/displayio/lvgl/Group.c @@ -89,6 +89,14 @@ static uint32_t _scale_to_q8(mp_float_t scale) { return (uint32_t)(scale * LV_SCALE_NONE); } +static mp_float_t _validate_scale(mp_obj_t scale_obj) { + mp_float_t scale = mp_arg_validate_obj_float_range(scale_obj, 0, 32767, MP_QSTR_scale); + if (scale < (mp_float_t)1 / LV_SCALE_NONE) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("%q out of range"), MP_QSTR_scale); + } + return scale; +} + // Recompute members' inherited scale and screen positions after this group's scale or // position changed. Returns false when a member rejects the transform; some members may // already have taken it then, so callers restore the group's own transform and push again. @@ -246,12 +254,12 @@ void displayio_lvgl_init(void) { displayio_lvgl_unlock(); } -void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_float_t scale, mp_int_t x, mp_int_t y) { +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_obj_t scale, mp_int_t x, mp_int_t y) { self->x = x; self->y = y; self->members = members; self->item_removed = false; - self->scale = _scale_to_q8(scale); + self->scale = _scale_to_q8(_validate_scale(scale)); self->requested_scale = scale; self->in_group = false; self->readonly = false; @@ -261,7 +269,7 @@ void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, self->lv_abs_y = y; } -void common_hal_displayio_group_construct(displayio_group_t *self, mp_float_t scale, mp_int_t x, mp_int_t y) { +void common_hal_displayio_group_construct(displayio_group_t *self, mp_obj_t scale, mp_int_t x, mp_int_t y) { mp_obj_list_t *members = mp_obj_new_list(0, NULL); displayio_group_construct(self, members, scale, x, y); } @@ -324,16 +332,17 @@ void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden) displayio_lvgl_unlock_with_cleanup(); } -mp_float_t common_hal_displayio_group_get_scale(displayio_group_t *self) { +mp_obj_t common_hal_displayio_group_get_scale(displayio_group_t *self) { return self->requested_scale; } -void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_float_t scale) { +void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_obj_t scale) { + mp_float_t scale_value = _validate_scale(scale); if (self->requested_scale == scale) { return; } check_readonly(self); - uint32_t scale_q8 = _scale_to_q8(scale); + uint32_t scale_q8 = _scale_to_q8(scale_value); if (self->scale == scale_q8) { self->requested_scale = scale; return; @@ -344,7 +353,7 @@ void common_hal_displayio_group_set_scale(displayio_group_t *self, mp_float_t sc return; } uint32_t previous_scale = self->scale; - mp_float_t previous_requested_scale = self->requested_scale; + mp_obj_t previous_requested_scale = self->requested_scale; self->scale = scale_q8; self->requested_scale = scale; nlr_jump_callback_node_t cleanup; diff --git a/shared-module/displayio/lvgl/Group.h b/shared-module/displayio/lvgl/Group.h index 8e01e4d6603..06411b4775e 100644 --- a/shared-module/displayio/lvgl/Group.h +++ b/shared-module/displayio/lvgl/Group.h @@ -30,7 +30,7 @@ typedef struct { uint32_t scale; // Own scale in LVGL's Q8 format; LV_SCALE_NONE is 1x. // Keep the requested value for Python readback. In particular, consecutive animation // values may quantize to the same Q8 step but must not become stuck at the old value. - mp_float_t requested_scale; + mp_obj_t requested_scale; bool item_removed : 1; bool in_group : 1; bool hidden : 1; @@ -46,7 +46,7 @@ typedef struct { int32_t lv_abs_y; } displayio_group_t; -void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_float_t scale, mp_int_t x, mp_int_t y); +void displayio_group_construct(displayio_group_t *self, mp_obj_list_t *members, mp_obj_t scale, mp_int_t x, mp_int_t y); void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden); // Shared LVGL compositor API. Implemented in shared-module/displayio/lvgl/Group.c.