diff --git a/.github/workflows/run-zephyr-tests.yml b/.github/workflows/run-zephyr-tests.yml index ebdd5daba21..ab51e43a44a 100644 --- a/.github/workflows/run-zephyr-tests.yml +++ b/.github/workflows/run-zephyr-tests.yml @@ -19,7 +19,13 @@ jobs: include: - shard: native_sim boards: native_native_sim - pytest_args: tests/ --ignore=tests/bsim -k 'not native_native_sim_asan and not native_native_sim_lfs' -n $(nproc) --dist loadfile + pytest_args: tests/ --ignore=tests/bsim -k 'not native_native_sim_asan and not native_native_sim_lfs and not native_native_sim_lvgl' -n $(nproc) --dist loadfile + - shard: native_sim_lvgl + boards: native_native_sim_lvgl + pytest_args: tests/ --ignore=tests/bsim -k 'native_native_sim_lvgl and not native_native_sim_lvgl_asan' -n $(nproc) --dist loadfile + - shard: native_sim_lvgl_asan + boards: native_native_sim_lvgl_asan + pytest_args: tests/ --ignore=tests/bsim -k native_native_sim_lvgl_asan -n $(nproc) --dist loadfile - shard: native_sim_asan boards: native_native_sim_asan pytest_args: tests/ --ignore=tests/bsim -k native_native_sim_asan -n $(nproc) --dist loadfile diff --git a/.gitmodules b/.gitmodules index 7be72505556..aa93868bd55 100644 --- a/.gitmodules +++ b/.gitmodules @@ -427,3 +427,7 @@ [submodule "ports/espressif/esp-adf-libs"] path = ports/espressif/esp-adf-libs url = https://github.com/espressif/esp-adf-libs.git +[submodule "lib/lvgl"] + path = lib/lvgl + url = https://github.com/adafruit/lvgl.git + branch = circuitpython-v9.6 diff --git a/lib/lvgl b/lib/lvgl new file mode 160000 index 00000000000..c99ebe06718 --- /dev/null +++ b/lib/lvgl @@ -0,0 +1 @@ +Subproject commit c99ebe067185c9dc4ae074730473755f396ac0c3 diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index df122905cc2..05262d77da0 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -837,9 +837,9 @@ msgid "soft reboot\n" msgstr "" #: ports/analog/common-hal/busio/SPI.c ports/analog/common-hal/busio/UART.c -#: ports/stm/common-hal/audioio/AudioOut.c +#: ports/cxd56/common-hal/busio/SPI.c ports/stm/common-hal/audioio/AudioOut.c #: shared-bindings/digitalio/DigitalInOutProtocol.c -#: shared-module/busdisplay/BusDisplay.c shared-module/ssl/SSLContext.c +#: shared-module/displayio/bus_core.c shared-module/ssl/SSLContext.c msgid "%q init failed" msgstr "" @@ -876,7 +876,7 @@ msgstr "" msgid "RS485" msgstr "" -#: ports/analog/common-hal/busio/UART.c +#: ports/analog/common-hal/busio/UART.c ports/cxd56/common-hal/busio/UART.c msgid "UART needs TX & RX" msgstr "" @@ -967,6 +967,7 @@ msgstr "" #: ports/nordic/common-hal/pulseio/PulseIn.c #: ports/nordic/peripherals/nrf/timers.c #: ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c +#: ports/stm/common-hal/pulseio/PulseOut.c msgid "All timers in use" msgstr "" @@ -1026,6 +1027,7 @@ msgid "Too many channels in sample" msgstr "" #: ports/atmel-samd/common-hal/audiobusio/I2SOut.c +#: ports/atmel-samd/common-hal/audiobusio/PDMIn.c #: ports/atmel-samd/common-hal/audioio/AudioOut.c #: ports/raspberrypi/common-hal/audiobusio/I2SOut.c #: ports/raspberrypi/common-hal/audiopwmio/PWMAudioOut.c @@ -1214,6 +1216,9 @@ msgstr "" #: ports/cxd56/common-hal/camera/Camera.c #: ports/espressif/common-hal/jpegio/JpegDecoder.c #: shared-module/audiocore/WaveFile.c +#: shared-module/busdisplay/lvgl/BusDisplay.c +#: shared-module/displayio/lvgl/TileGrid.c +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c msgid "Format not supported" msgstr "" @@ -1689,6 +1694,8 @@ msgstr "" #: ports/espressif/common-hal/espidf/__init__.c #: ports/espressif/common-hal/jpegio/JpegDecoder.c #: ports/raspberrypi/common-hal/usb_host_bulk/InStream.c +#: shared-bindings/busdisplay/BusDisplay.c +#: shared-bindings/framebufferio/FramebufferDisplay.c #: shared-bindings/picogame/Canvas.c shared-bindings/picogame/__init__.c #: shared-module/jpegio/JpegDecoder.c shared-module/picogame/Display.c msgid "Operation or feature not supported" @@ -1935,7 +1942,8 @@ msgid "Must provide MISO or MOSI pin" msgstr "" #: ports/mimxrt10xx/common-hal/busio/SPI.c -#: ports/mimxrt10xx/common-hal/busio/UART.c ports/stm/common-hal/busio/I2C.c +#: ports/mimxrt10xx/common-hal/busio/UART.c +#: ports/mimxrt10xx/common-hal/canio/CAN.c ports/stm/common-hal/busio/I2C.c #: ports/stm/common-hal/busio/SPI.c ports/stm/common-hal/busio/UART.c #: ports/stm/common-hal/canio/CAN.c ports/stm/common-hal/sdioio/SDCard.c #: shared-bindings/emmcio/EMMC.c @@ -2043,8 +2051,8 @@ msgstr "" #: shared-bindings/bitmapfilter/__init__.c shared-bindings/canio/CAN.c #: shared-bindings/digitalio/Pull.c shared-bindings/picogame/Sprite.c #: shared-bindings/supervisor/__init__.c shared-module/audiofilters/__init__.c -#: shared-module/displayio/__init__.c shared-module/synthio/Synthesizer.c -#: shared-module/videocore/VideoBuffer.c +#: shared-module/displayio/legacy/__init__.c +#: shared-module/synthio/Synthesizer.c shared-module/videocore/VideoBuffer.c msgid "%q must be of type %q or %q, not %q" msgstr "" @@ -2185,10 +2193,6 @@ msgstr "" msgid "Invalid %q and %q" msgstr "" -#: ports/raspberrypi/common-hal/mdns/Server.c -msgid "Failed to add service TXT record" -msgstr "" - #: ports/raspberrypi/common-hal/mdns/Server.c msgid "Out of MDNS service slots" msgstr "" @@ -2452,6 +2456,14 @@ msgstr "" msgid "SDIO GetCardInfo Error %d" msgstr "" +#: ports/zephyr-cp/bindings/zephyr_display/Display.c +#: shared-bindings/busdisplay/BusDisplay.c +#: shared-bindings/epaperdisplay/EPaperDisplay.c +#: shared-bindings/framebufferio/FramebufferDisplay.c +#: shared-bindings/mipidsi/Display.c +msgid "Display rotation must be in 90 degree increments" +msgstr "" + #: ports/zephyr-cp/bindings/zephyr_display/Display.c #: shared-bindings/busdisplay/BusDisplay.c #: shared-bindings/epaperdisplay/EPaperDisplay.c @@ -2475,8 +2487,9 @@ msgstr "" #: ports/zephyr-cp/bindings/zephyr_display/Display.c #: shared-bindings/framebufferio/FramebufferDisplay.c -#: shared-module/busdisplay/BusDisplay.c -#: shared-module/framebufferio/FramebufferDisplay.c +#: shared-module/busdisplay/legacy/BusDisplay.c +#: shared-module/framebufferio/legacy/FramebufferDisplay.c +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c msgid "Group already used" msgstr "" @@ -2507,9 +2520,11 @@ msgstr "" msgid "Failed to set hostname" msgstr "" -#: ports/zephyr-cp/common-hal/zephyr_display/Display.c -#: shared-module/busdisplay/BusDisplay.c -#: shared-module/framebufferio/FramebufferDisplay.c +#: ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.c +#: ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.c +#: shared-module/busdisplay/legacy/BusDisplay.c +#: shared-module/framebufferio/legacy/FramebufferDisplay.c +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c msgid "Below minimum frame rate" msgstr "" @@ -3908,7 +3923,9 @@ msgstr "" #: shared-bindings/_bleio/__init__.c shared-bindings/emmcio/EMMC.c #: shared-bindings/memorymonitor/AllocationSize.c #: shared-bindings/pulseio/PulseIn.c shared-module/bitmaptools/__init__.c -#: shared-module/displayio/Bitmap.c shared-module/displayio/Group.c +#: shared-module/displayio/legacy/Bitmap.c +#: shared-module/displayio/legacy/Group.c shared-module/displayio/lvgl/Bitmap.c +#: shared-module/displayio/lvgl/Group.c msgid "Read-only" msgstr "" @@ -4170,13 +4187,6 @@ msgstr "" msgid "No default %q bus" msgstr "" -#: shared-bindings/busdisplay/BusDisplay.c -#: shared-bindings/epaperdisplay/EPaperDisplay.c -#: shared-bindings/framebufferio/FramebufferDisplay.c -#: shared-bindings/mipidsi/Display.c -msgid "Display rotation must be in 90 degree increments" -msgstr "" - #: shared-bindings/busdisplay/BusDisplay.c msgid "%q must be 1 when %q is True" msgstr "" @@ -4284,7 +4294,8 @@ msgstr "" #: shared-bindings/displayio/TileGrid.c #: shared-bindings/tilepalettemapper/TilePaletteMapper.c -#: shared-module/displayio/TileGrid.c +#: shared-module/displayio/legacy/TileGrid.c +#: shared-module/displayio/lvgl/TileGrid.c msgid "Tile index out of bounds" msgstr "" @@ -4341,6 +4352,10 @@ msgstr "" msgid "Scale dimensions must divide by 3" msgstr "" +#: shared-bindings/is31fl3741/FrameBuffer.c +msgid "LED mappings must match display size" +msgstr "" + #: shared-bindings/is31fl3741/IS31FL3741.c msgid "Mapping must be a tuple" msgstr "" @@ -4583,50 +4598,92 @@ msgstr "" msgid "unsupported bitmap depth" msgstr "" +#: shared-module/busdisplay/lvgl/BusDisplay.c +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c +msgid "LVGL display create failed" +msgstr "" + +#: shared-module/busdisplay/lvgl/BusDisplay.c +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c +msgid "LVGL buffer alloc failed" +msgstr "" + #: shared-module/cryptography/hazmat/primitives/asymmetric/rsa/DigitalSignatureKey.c msgid "OAEP requires this build to enable TLS 1.3 support" msgstr "" -#: shared-module/displayio/Bitmap.c +#: shared-module/displayio/bus_core.c +msgid "Unsupported display bus type" +msgstr "" + +#: shared-module/displayio/legacy/Bitmap.c +#: shared-module/displayio/lvgl/Bitmap.c msgid "Invalid bits per value" msgstr "" -#: shared-module/displayio/ColorConverter.c +#: shared-module/displayio/legacy/ColorConverter.c +#: shared-module/displayio/lvgl/ColorConverter.c msgid "Only one color can be transparent at a time" msgstr "" -#: shared-module/displayio/Group.c +#: shared-module/displayio/legacy/Group.c shared-module/displayio/lvgl/Group.c msgid "Layer already in a group" msgstr "" -#: shared-module/displayio/Group.c +#: shared-module/displayio/legacy/Group.c msgid "Layer must be a Group or TileGrid subclass" msgstr "" -#: shared-module/displayio/OnDiskBitmap.c +#: shared-module/displayio/legacy/OnDiskBitmap.c +#: shared-module/displayio/lvgl/OnDiskBitmap.c #, c-format msgid "" "Only Windows format, uncompressed BMP supported: given header size is %d" msgstr "" -#: shared-module/displayio/OnDiskBitmap.c +#: shared-module/displayio/legacy/OnDiskBitmap.c +#: shared-module/displayio/lvgl/OnDiskBitmap.c msgid "RLE-compressed BMP not supported" msgstr "" -#: shared-module/displayio/OnDiskBitmap.c +#: shared-module/displayio/legacy/OnDiskBitmap.c +#: shared-module/displayio/lvgl/OnDiskBitmap.c msgid "Unable to read color palette data" msgstr "" -#: shared-module/displayio/__init__.c +#: shared-module/displayio/legacy/__init__.c msgid "Too many displays" msgstr "" -#: shared-module/displayio/__init__.c +#: shared-module/displayio/legacy/__init__.c msgid "Too many display busses; forgot displayio.release_displays() ?" msgstr "" -#: shared-module/displayio/bus_core.c -msgid "Unsupported display bus type" +#: shared-module/displayio/lvgl/Group.c +msgid "Layer does not support scaling" +msgstr "" + +#: shared-module/displayio/lvgl/Group.c +msgid "Layer must be a Group, TileGrid, vector shape or VideoBuffer" +msgstr "" + +#: shared-module/displayio/lvgl/TileGrid.c +msgid "LVGL displayio backend does not support flips/transpose yet" +msgstr "" + +#: shared-module/displayio/lvgl/TileGrid.c +msgid "OnDiskBitmap requires its own pixel_shader" +msgstr "" + +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c +msgid "LVGL dirty row alloc failed" +msgstr "" + +#: shared-module/framebufferio/lvgl/FramebufferDisplay.c +#: shared-module/videocore/VideoBuffer.c +#: shared-module/videocore/legacy/VideoBuffer.c +#: shared-module/videocore/lvgl/VideoBuffer.c +msgid "VideoBuffer can't stream into a rotated display" msgstr "" #: shared-module/gifio/GifWriter.c @@ -4655,10 +4712,6 @@ msgstr "" msgid "This microcontroller does not support continuous capture." msgstr "" -#: shared-module/is31fl3741/FrameBuffer.c -msgid "LED mappings must match display size" -msgstr "" - #: shared-module/jpegio/JpegDecoder.c msgid "Interrupted by output function" msgstr "" @@ -4875,15 +4928,15 @@ msgid "video size changed since play; stop and play again" msgstr "" #: shared-module/videocore/VideoBuffer.c -msgid "VideoBuffer can't stream into a rotated display" +msgid "VideoBuffer is playing into a different framebuffer" msgstr "" -#: shared-module/videocore/VideoBuffer.c +#: shared-module/videocore/legacy/VideoBuffer.c msgid "VideoBuffer does not support transforms" msgstr "" -#: shared-module/videocore/VideoBuffer.c -msgid "VideoBuffer is playing into a different framebuffer" +#: shared-module/videocore/lvgl/VideoBuffer.c +msgid "video format must be 16, 24 or 32 bits per pixel" msgstr "" #: supervisor/shared/bluetooth/bluetooth.c @@ -4972,6 +5025,10 @@ msgstr "" msgid "Interrupt error." msgstr "" +#: supervisor/shared/safe_mode.c +msgid "LVGL ran out of memory." +msgstr "" + #: supervisor/shared/safe_mode.c msgid "NLR jump failed. Likely memory corruption." msgstr "" diff --git a/ports/espressif/AGENTS.md b/ports/espressif/AGENTS.md new file mode 100644 index 00000000000..2e39b1ba086 --- /dev/null +++ b/ports/espressif/AGENTS.md @@ -0,0 +1 @@ +Source esp-idf/export.sh before running make. diff --git a/ports/espressif/CMakeLists.txt b/ports/espressif/CMakeLists.txt index e31099b3cf2..c644a966446 100644 --- a/ports/espressif/CMakeLists.txt +++ b/ports/espressif/CMakeLists.txt @@ -9,7 +9,7 @@ set(ENV{IDF_PATH} ${CMAKE_SOURCE_DIR}/esp-idf) # Core components set(COMPONENTS bt driver esptool_py freertos log main mbedtls pthread soc) # Peripheral drivers -list(APPEND COMPONENTS esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_i2s esp_driver_jpeg esp_driver_ledc esp_driver_pcnt esp_driver_rmt esp_driver_sdmmc esp_driver_spi esp_driver_touch_sens esp_driver_tsens esp_driver_uart) +list(APPEND COMPONENTS esp_driver_dac esp_driver_gpio esp_driver_gptimer esp_driver_i2c esp_driver_i2s esp_driver_jpeg esp_driver_ledc esp_driver_pcnt esp_driver_ppa esp_driver_rmt esp_driver_sdmmc esp_driver_spi esp_driver_touch_sens esp_driver_tsens esp_driver_uart) # Networking list(APPEND COMPONENTS esp_eth esp-tls esp_event esp_netif esp_wifi lwip mdns wpa_supplicant) # Misc @@ -22,6 +22,7 @@ if(CIRCUITPY_MIPICSI) endif() set(EXTRA_COMPONENT_DIRS "esp-protocols/components/mdns" "esp-camera") +set(EXTRA_COMPONENT_DIRS "${EXTRA_COMPONENT_DIRS}" "${CMAKE_SOURCE_DIR}/../../lib/lvgl") if(CIRCUITPY_ESP_HOSTED) list(APPEND EXTRA_COMPONENT_DIRS "esp-hosted") diff --git a/ports/espressif/Makefile b/ports/espressif/Makefile index 820772d2ffe..c972a835326 100644 --- a/ports/espressif/Makefile +++ b/ports/espressif/Makefile @@ -22,6 +22,10 @@ endif ####################################### INC += \ + -I../../lib/lvgl \ + -I../../lib/lvgl/include \ + -I../../lib/lvgl/src \ + -I./lvgl \ -I.\ -I./boards \ -I./boards/$(BOARD) \ @@ -72,6 +76,7 @@ INC += \ -isystem esp-idf/components/esp_driver_ledc/include \ -isystem esp-idf/components/esp_driver_parlio/include \ -isystem esp-idf/components/esp_driver_pcnt/include \ + -isystem esp-idf/components/esp_driver_ppa/include \ -isystem esp-idf/components/esp_driver_rmt/include \ -isystem esp-idf/components/esp_driver_sdio/include \ -isystem esp-idf/components/esp_driver_sdmmc/include \ @@ -115,6 +120,8 @@ INC += \ -isystem esp-idf/components/hal/platform_port/include \ -isystem esp-idf/components/esp_hal_gpio/include \ -isystem esp-idf/components/esp_hal_gpio/$(IDF_TARGET)/include \ + -isystem esp-idf/components/esp_hal_ppa/include \ + -isystem esp-idf/components/esp_hal_ppa/$(IDF_TARGET)/include \ -isystem esp-idf/components/esp_hal_timg/include \ -isystem esp-idf/components/esp_hal_ana_conv/include \ -isystem esp-idf/components/esp_hal_touch_sens/include \ @@ -703,8 +710,19 @@ endif # source ###################################### +ifeq ($(CIRCUITPY_DISPLAYIO_LVGL),1) +# Compile LVGL as part of CircuitPython's build rather than as an ESP-IDF component so +# that it uses the port's lv_conf.h. The library and the display code must agree on the +# LVGL configuration or struct layouts will differ. +LVGL_SRC = $(patsubst $(TOP)/%,%,$(shell find $(TOP)/lib/lvgl/src -name '*.c')) +SRC_MOD += $(LVGL_SRC) +# LVGL isn't warning-clean under CircuitPython's strict warning flags. +$(addprefix $(BUILD)/, $(LVGL_SRC:.c=.o)): CFLAGS += -w +endif + SRC_C += \ background.c \ + supervisor/lvgl_port.c \ mphalport.c \ boards/$(BOARD)/board.c \ boards/$(BOARD)/pins.c \ @@ -917,8 +935,9 @@ $(filter $(PY_BUILD)/../extmod/vfs_fat_%.o, $(PY_O)): COPT += -Os $(BUILD)/lib/protomatter/src/core.o: CFLAGS += -DESP32 -# List of sources for qstr extraction -SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(SRC_MOD) $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON) +# List of sources for qstr extraction. LVGL sources contain no qstrs and aren't +# -Wundef clean, so skip preprocessing them. +SRC_QSTR += $(SRC_C) $(SRC_SUPERVISOR) $(filter-out lib/lvgl/%, $(SRC_MOD)) $(SRC_COMMON_HAL_SHARED_MODULE_EXPANDED) $(SRC_CIRCUITPY_COMMON) # IDF build commands IDF_PATH = $(realpath ./esp-idf) @@ -1148,6 +1167,10 @@ endif ifneq ($(CIRCUITPY_ESP_PSRAM_SIZE),0) ESP_IDF_COMPONENTS_LINK += esp_psram endif +# The LVGL PPA draw unit uses the P4/S31 Pixel Processing Accelerator. +ifneq ($(CIRCUITPY_DISPLAYIO_LVGL),0) + ESP_IDF_COMPONENTS_LINK += esp_driver_ppa esp_hal_ppa +endif ifneq ($(CIRCUITPY_DOTCLOCKFRAMEBUFFER),0) ESP_IDF_COMPONENTS_LINK += esp_lcd endif diff --git a/ports/espressif/background.c b/ports/espressif/background.c index df63aeb3dd2..08a58a2128e 100644 --- a/ports/espressif/background.c +++ b/ports/espressif/background.c @@ -16,6 +16,7 @@ #include "tusb.h" #endif +#include "py/mpconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" diff --git a/ports/espressif/boards/espressif_esp32p4_function_ev/mpconfigboard.mk b/ports/espressif/boards/espressif_esp32p4_function_ev/mpconfigboard.mk index 867773aeb8d..3c5ba5ffc0e 100644 --- a/ports/espressif/boards/espressif_esp32p4_function_ev/mpconfigboard.mk +++ b/ports/espressif/boards/espressif_esp32p4_function_ev/mpconfigboard.mk @@ -17,6 +17,7 @@ CIRCUITPY_ESP_PSRAM_FREQ = 200m CIRCUITPY_MII = 1 CIRCUITPY_SSL = 1 +CIRCUITPY_USB_VIDEO = 1 # mdns on Espressif requires the wifi-based ESP-IDF mdns component CIRCUITPY_MDNS = 0 diff --git a/ports/espressif/boards/espressif_esp32s31_korvo_1/board.c b/ports/espressif/boards/espressif_esp32s31_korvo_1/board.c index 48bf85aed15..3f5352b5a24 100644 --- a/ports/espressif/boards/espressif_esp32s31_korvo_1/board.c +++ b/ports/espressif/boards/espressif_esp32s31_korvo_1/board.c @@ -6,6 +6,7 @@ #include "supervisor/board.h" #include "mpconfigboard.h" +#include "common-hal/dotclockframebuffer/DotClockFramebuffer.h" #include "shared-bindings/dotclockframebuffer/DotClockFramebuffer.h" #include "shared-bindings/framebufferio/FramebufferDisplay.h" #include "shared-bindings/microcontroller/Pin.h" diff --git a/ports/espressif/boards/m5stack_tab5/board.c b/ports/espressif/boards/m5stack_tab5/board.c index 3acdd432510..d318fe33260 100644 --- a/ports/espressif/boards/m5stack_tab5/board.c +++ b/ports/espressif/boards/m5stack_tab5/board.c @@ -11,6 +11,7 @@ #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/mipidsi/Bus.h" #include "shared-bindings/mipidsi/Display.h" +#include "shared-bindings/framebufferio/FramebufferDisplay.h" #include "shared-module/displayio/__init__.h" #include "shared-module/framebufferio/__init__.h" #include "shared-module/framebufferio/FramebufferDisplay.h" diff --git a/ports/espressif/boards/m5stack_tab5/pins.c b/ports/espressif/boards/m5stack_tab5/pins.c index c64d8803c02..e486b2d0b1a 100644 --- a/ports/espressif/boards/m5stack_tab5/pins.c +++ b/ports/espressif/boards/m5stack_tab5/pins.c @@ -127,6 +127,6 @@ static const mp_rom_map_elem_t board_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&board_spi_obj) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&board_uart_obj) }, - { MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display)} + { MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].display) }, }; MP_DEFINE_CONST_DICT(board_module_globals, board_module_globals_table); diff --git a/ports/espressif/boards/unexpectedmaker_feathers2/mpconfigboard.mk b/ports/espressif/boards/unexpectedmaker_feathers2/mpconfigboard.mk index f3a6e36ef25..58f053f36c8 100644 --- a/ports/espressif/boards/unexpectedmaker_feathers2/mpconfigboard.mk +++ b/ports/espressif/boards/unexpectedmaker_feathers2/mpconfigboard.mk @@ -17,3 +17,10 @@ CIRCUITPY_BITBANG_APA102 = 1 # Include these Python libraries in firmware. # FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_DotStar + +# Fast e-paper does not fit in internal RAM. +CIRCUITPY_EPAPERDISPLAY_FAST = 0 + +# The ESP32-S2 shares its 152KB SRAM between IRAM and DRAM. -O2 overflows +# dram0_0_seg, so use the same optimization level as the smaller-flash boards. +OPTIMIZATION_FLAGS ?= -Os diff --git a/ports/espressif/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.mk b/ports/espressif/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.mk index f3a6e36ef25..58f053f36c8 100644 --- a/ports/espressif/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.mk +++ b/ports/espressif/boards/unexpectedmaker_feathers2_prerelease/mpconfigboard.mk @@ -17,3 +17,10 @@ CIRCUITPY_BITBANG_APA102 = 1 # Include these Python libraries in firmware. # FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_DotStar + +# Fast e-paper does not fit in internal RAM. +CIRCUITPY_EPAPERDISPLAY_FAST = 0 + +# The ESP32-S2 shares its 152KB SRAM between IRAM and DRAM. -O2 overflows +# dram0_0_seg, so use the same optimization level as the smaller-flash boards. +OPTIMIZATION_FLAGS ?= -Os diff --git a/ports/espressif/common-hal/busio/UART.c b/ports/espressif/common-hal/busio/UART.c index 4c9f0d49d9b..793b43f88cd 100644 --- a/ports/espressif/common-hal/busio/UART.c +++ b/ports/espressif/common-hal/busio/UART.c @@ -96,6 +96,17 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self, #endif for (uart_port_t num = 0; num < UART_LIMIT; num++) { + // The IDF log console usually runs on the ROM's UART code, so no driver is installed on + // it and uart_is_driver_installed() can't see it. Skip it explicitly: uart_set_pin() + // below would move the console onto our pins, and its log output would then share our + // wire, which corrupts both the logs and the data. CircuitPython builds that own the + // console UART (CIRCUITPY_CONSOLE_UART) allocate it through this function, so only + // reserve the IDF console's UART when it isn't ours. + #if !CIRCUITPY_CONSOLE_UART && defined(CONFIG_ESP_CONSOLE_UART) && CONFIG_ESP_CONSOLE_UART + if (num == (uart_port_t)CONFIG_ESP_CONSOLE_UART_NUM) { + continue; + } + #endif if (!uart_is_driver_installed(num)) { self->uart_num = num; break; diff --git a/ports/espressif/common-hal/jpegio/JpegDecoder.c b/ports/espressif/common-hal/jpegio/JpegDecoder.c index 24c883d193d..33387d2686f 100644 --- a/ports/espressif/common-hal/jpegio/JpegDecoder.c +++ b/ports/espressif/common-hal/jpegio/JpegDecoder.c @@ -178,8 +178,7 @@ bool jpegio_jpegdecoder_hw_decode(jpegio_jpegdecoder_obj_t *self, const mp_buffe } if (direct) { - displayio_area_t area = { 0, 0, width, height, NULL }; - displayio_bitmap_set_dirty_area(bitmap, &area); + displayio_bitmap_set_dirty(bitmap, 0, 0, width, height); return true; } diff --git a/ports/espressif/common-hal/mipidsi/Display.c b/ports/espressif/common-hal/mipidsi/Display.c index 7e0e2360c00..f9b47f34526 100644 --- a/ports/espressif/common-hal/mipidsi/Display.c +++ b/ports/espressif/common-hal/mipidsi/Display.c @@ -14,6 +14,7 @@ #include #include #include +#include "hal/axi_icm_ll.h" #include "py/runtime.h" // Cache write-back function (should be from rom/cache.h but it's not always available) @@ -33,6 +34,33 @@ static void clear_panel_handles(mipidsi_display_obj_t *self) { } } +// The vsync ISR reads the frame callback and its data as a pair, so they change together. +static portMUX_TYPE frame_cb_lock = portMUX_INITIALIZER_UNLOCKED; + +static void set_frame_cb(mipidsi_display_obj_t *self, framebuffer_frame_callback_t cb, void *data) { + portENTER_CRITICAL(&frame_cb_lock); + self->frame_cb = cb; + self->frame_cb_data = data; + portEXIT_CRITICAL(&frame_cb_lock); +} + +// The DPI driver requires this callback to live in IRAM. It runs in interrupt context, so it only +// forwards to the framebuffer's frame callback (which hands off to the LVGL port) and returns. +static bool IRAM_ATTR mipidsi_display_vsync_cb(esp_lcd_panel_handle_t panel, + esp_lcd_dpi_panel_event_data_t *edata, void *user_ctx) { + (void)panel; + (void)edata; + mipidsi_display_obj_t *self = user_ctx; + portENTER_CRITICAL_ISR(&frame_cb_lock); + framebuffer_frame_callback_t cb = self->frame_cb; + void *data = self->frame_cb_data; + portEXIT_CRITICAL_ISR(&frame_cb_lock); + if (cb != NULL) { + cb(data); + } + return false; +} + void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, mipidsi_bus_obj_t *bus, const uint8_t *init_sequence, @@ -66,6 +94,8 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, self->framebuffer = NULL; self->dbi_io_handle = NULL; self->dpi_panel_handle = NULL; + self->frame_cb = NULL; + self->frame_cb_data = NULL; self->backlight_inout.base.type = &mp_type_NoneType; // Create the DBI interface for sending commands @@ -118,6 +148,12 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, CHECK_ESP_RESULT(ret); } + // Scanout continuously reads the framebuffer from PSRAM through DW-GDMA. + // Give those reads the highest AXI-ICM QoS priority so CPU, cache and PPA + // traffic cannot starve the DSI bridge FIFO. DSI does not write to PSRAM. + axi_icm_ll_set_dw_gdma_qos_arbiter_prio(0, 0, 15); + axi_icm_ll_set_dw_gdma_qos_arbiter_prio(1, 0, 15); + // The DPI divider is an integer, so record the clock it really produces. float pixel_clock_mhz = 0; if (esp_lcd_dpi_panel_get_pixel_clock(self->dpi_panel_handle, &pixel_clock_mhz) == ESP_OK) { @@ -165,6 +201,17 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, CHECK_ESP_RESULT(ret); } + // VSYNC is the frame boundary a FramebufferDisplay can pace itself with. The callback stays + // registered for the life of the panel; frame_cb is only set when a display asks for it. + esp_lcd_dpi_panel_event_callbacks_t dpi_cbs = { + .on_vsync = mipidsi_display_vsync_cb, + }; + ret = esp_lcd_dpi_panel_register_event_callbacks(self->dpi_panel_handle, &dpi_cbs, self); + if (ret != ESP_OK) { + clear_panel_handles(self); + CHECK_ESP_RESULT(ret); + } + // Send display-ON (0x29) after the DPI/video stream is running, matching // the reference BSP flow order. esp_lcd_panel_io_tx_param(self->dbi_io_handle, 0x29, NULL, 0); @@ -207,6 +254,9 @@ void common_hal_mipidsi_display_deinit(mipidsi_display_obj_t *self) { return; } + // Stop the vsync ISR from reaching a display that is going away before the panel is deleted. + set_frame_cb(self, NULL, NULL); + // Cleanup backlight #if (CIRCUITPY_PWMIO) if (self->backlight_pwm.base.type == &pwmio_pwmout_type) { @@ -238,6 +288,11 @@ void common_hal_mipidsi_display_refresh(mipidsi_display_obj_t *self) { // sends data from the framebuffer to the display } +void common_hal_mipidsi_display_set_frame_callback(mipidsi_display_obj_t *self, + framebuffer_frame_callback_t cb, void *data) { + set_frame_cb(self, cb, data); +} + void common_hal_mipidsi_display_refresh_rows(mipidsi_display_obj_t *self, const uint8_t *dirty_row_bitmap) { if (dirty_row_bitmap == NULL) { common_hal_mipidsi_display_refresh(self); diff --git a/ports/espressif/common-hal/mipidsi/Display.h b/ports/espressif/common-hal/mipidsi/Display.h index 70c66943279..5fd254f5039 100644 --- a/ports/espressif/common-hal/mipidsi/Display.h +++ b/ports/espressif/common-hal/mipidsi/Display.h @@ -7,6 +7,7 @@ #pragma once #include "py/obj.h" +#include "shared-module/framebufferio/__init__.h" #include "shared-bindings/mipidsi/Bus.h" #include "common-hal/microcontroller/Pin.h" #include "common-hal/digitalio/DigitalInOut.h" @@ -36,4 +37,6 @@ typedef struct { }; bool backlight_on_high; mp_float_t current_brightness; + framebuffer_frame_callback_t frame_cb; + void *frame_cb_data; } mipidsi_display_obj_t; diff --git a/ports/espressif/common-hal/wifi/__init__.c b/ports/espressif/common-hal/wifi/__init__.c index 08818ce5022..df75ebeb0b4 100644 --- a/ports/espressif/common-hal/wifi/__init__.c +++ b/ports/espressif/common-hal/wifi/__init__.c @@ -634,9 +634,16 @@ void wifi_reset(void) { IP_EVENT_STA_GOT_IP, radio->handler_instance_got_ip)); ESP_ERROR_CHECK(esp_wifi_deinit()); - esp_netif_destroy(radio->netif); + // Use the default-netif destructor so the Wi-Fi default event handlers let go of + // these first. esp_wifi_stop() posts WIFI_EVENT_STA_STOP/AP_STOP, which the default + // event loop handles asynchronously (and, over ESP-Hosted, after an RPC round trip), + // so it can run after this function. The handler would then call esp_netif_stop() on + // the freed netif (whose DHCP pointer is already reused memory), which asserts in + // heap_caps_free(). Clearing the handler's netif pointers here makes that stop event + // a no-op. + esp_netif_destroy_default_wifi(radio->netif); radio->netif = NULL; - esp_netif_destroy(radio->ap_netif); + esp_netif_destroy_default_wifi(radio->ap_netif); radio->ap_netif = NULL; wifi_inited = false; #endif diff --git a/ports/espressif/lvgl/lv_conf.h b/ports/espressif/lvgl/lv_conf.h new file mode 100644 index 00000000000..00f178c4ef9 --- /dev/null +++ b/ports/espressif/lvgl/lv_conf.h @@ -0,0 +1,102 @@ +/** + * LVGL config for CircuitPython on ESP32 P4/S31 + * Based on lv_conf_template.h with P4 optimizations + */ +#ifndef LV_CONF_H +#define LV_CONF_H + +#define LV_USE_OS LV_OS_NONE + +/* Color depth: 16 for RGB565, 32 for ARGB8888 - individual displays set their own + * format when they are created. */ +#define LV_COLOR_FORMAT_DEFAULT LV_COLOR_FORMAT_RGB565 + +/* Memory: allocate from the port heap instead of the garbage collected VM heap. + * Displays live outside the VM, so LVGL allocations must not be collected. */ +#define LV_USE_STDLIB_MALLOC LV_STDLIB_CUSTOM +#define LV_USE_STDLIB_STRING LV_STDLIB_CLIB +#define LV_USE_STDLIB_SPRINTF LV_STDLIB_CLIB + +/* Display and rendering */ +#define LV_USE_DRAW_SW 1 +#define LV_USE_IMAGE 1 +#define LV_USE_LABEL 1 +#define LV_USE_OBJ 1 + +/* Use the ESP32-P4/S31 Pixel Processing Accelerator for fills, blends and simple + * images. PPA works on cache lines, so draw buffers must be 64-byte aligned. */ +#define LV_USE_PPA 1 +#define LV_USE_PPA_IMG 1 +/* Leave regular PSRAM arbitration gaps for the DSI framebuffer scanout DMA. */ +#define LV_PPA_BURST_LENGTH 64 +/* LVGL 9.6 checks this before defining it, which trips -Wundef. */ +#define LV_PPA_NONBLOCKING_OPS 0 +#define LV_DRAW_BUF_ALIGN 64 +#define CONFIG_LV_DRAW_BUF_ALIGN 64 + +/* displayio hands indexed bitmaps to LVGL as I1/I2/I4/I8 images. Decode them fully + * (instead of line by line) so they can be scaled, and cache the decoded image so it is + * only decoded again when the bitmap or palette changes. An image that doesn't fit in the + * cache isn't drawn. */ +#define LV_BIN_DECODER_RAM_LOAD 1 + +/* The image cache only evicts against its own size limit, and the decoded image is allocated + * before the cache is consulted. A limit anywhere near the 16 MB of PSRAM therefore lets one + * decode exhaust the heap before anything is evicted, and the next small allocation (a draw + * task) fails. With LV_USE_ASSERT_MALLOC off that failure is a NULL dereference, not an error + * message. Keep the limit far below PSRAM: it has to fit the largest single decode (a 720p + * ARGB8888 image is 3.7 MB) with room left for the display buffers and the VM heap. */ +#define LV_CACHE_DEF_SIZE (4 * 1024 * 1024) + +/* A TileGrid composes a changed cell at a time under one batched invalidation, and scroll + * invalidates one object, so a handful of pending areas is plenty. */ +#define LV_INV_BUF_SIZE 64 + +/* Font: use built-in */ +#define LV_FONT_MONTSERRAT_14 1 + +/* Logging */ +#define LV_USE_LOG 0 + +/* displayio draws plain objects with explicit styles, so the themes aren't used. + * The default theme pulls in widgets that this config disables. */ +#define LV_USE_THEME_DEFAULT 0 +#define LV_USE_THEME_SIMPLE 0 +#define LV_USE_THEME_MONO 0 + +/* displayio uses lv_obj, lv_image, lv_canvas (the compose pass draws through a canvas + * layer) and lv_label for text. The rest of the widgets are enabled by default, and many + * of them require each other, so turn them all off to keep the build small and the + * dependency checks happy. */ +#define LV_USE_ANIMIMG 0 +#define LV_USE_ARC 0 +#define LV_USE_ARCLABEL 0 +#define LV_USE_BAR 0 +#define LV_USE_BUTTON 0 +#define LV_USE_BUTTONMATRIX 0 +#define LV_USE_CALENDAR 0 +#define LV_USE_CANVAS 1 +#define LV_USE_CHART 0 +#define LV_USE_CHECKBOX 0 +#define LV_USE_DROPDOWN 0 +#define LV_USE_IMAGEBUTTON 0 +#define LV_USE_KEYBOARD 0 +#define LV_USE_LED 0 +#define LV_USE_LINE 0 +#define LV_USE_LIST 0 +#define LV_USE_MENU 0 +#define LV_USE_MSGBOX 0 +#define LV_USE_ROLLER 0 +#define LV_USE_SCALE 0 +#define LV_USE_SLIDER 0 +#define LV_USE_SPAN 0 +#define LV_USE_SPINBOX 0 +#define LV_USE_SPINNER 0 +#define LV_USE_SWITCH 0 +#define LV_USE_TABLE 0 +#define LV_USE_TABVIEW 0 +#define LV_USE_TEXTAREA 0 +#define LV_USE_TILEVIEW 0 +#define LV_USE_WIN 0 + +#endif diff --git a/ports/espressif/mpconfigport.mk b/ports/espressif/mpconfigport.mk index 279eff952e8..42457843c91 100644 --- a/ports/espressif/mpconfigport.mk +++ b/ports/espressif/mpconfigport.mk @@ -445,6 +445,8 @@ CIRCUITPY_ESPCAMERA = 0 # P4 has MIPI-DSI CIRCUITPY_MIPIDSI = 1 +CIRCUITPY_DISPLAYIO_LVGL = 1 +CFLAGS += -DLV_CONF_INCLUDE_SIMPLE # P4 has MIPI-CSI CIRCUITPY_MIPICSI = 1 @@ -501,6 +503,8 @@ CIRCUITPY_RGBMATRIX = 0 CIRCUITPY_ALARM_TOUCH = 0 CIRCUITPY_TOUCHIO = 0 CIRCUITPY_TOUCHIO_USE_NATIVE = 0 +CIRCUITPY_DISPLAYIO_LVGL = 1 +CFLAGS += -DLV_CONF_INCLUDE_SIMPLE #### esp32s2 ########################################################## else ifeq ($(IDF_TARGET),esp32s2) @@ -544,6 +548,12 @@ CIRCUITPY_AUDIOMIXER_USE_PORT_MIX ?= 1 endif #### end chip-specific choices ######################################## +# The LVGL compositing core replaces the legacy scanline core. EPaperDisplay still +# depends on the legacy display core, so it isn't available with LVGL yet. +ifeq ($(CIRCUITPY_DISPLAYIO_LVGL),1) +CIRCUITPY_EPAPERDISPLAY = 0 +endif + # By default, load native .mpy files. Boards without the flash space turn it off. CIRCUITPY_LOAD_NATIVE ?= 1 diff --git a/ports/espressif/supervisor/lvgl_port.c b/ports/espressif/supervisor/lvgl_port.c new file mode 100644 index 00000000000..0c5f6bd260d --- /dev/null +++ b/ports/espressif/supervisor/lvgl_port.c @@ -0,0 +1,263 @@ +// LVGL port for espressif: a dedicated FreeRTOS task pumps lv_timer_handler(). +#include "py/mpconfig.h" +#if CIRCUITPY_DISPLAYIO_LVGL +#include "lvgl.h" +#include "esp_attr.h" +#include "esp_heap_caps.h" +#include "esp_log.h" +#include "esp_task.h" +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" +#include "py/runtime.h" +#include "core/lv_global.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "supervisor/background_callback.h" +#include "supervisor/shared/safe_mode.h" +#include "supervisor/shared/tick.h" + +static const char *lvgl_tag = "LVGL"; + +static esp_timer_handle_t lvgl_tick_timer = NULL; + +// Recursive so the shared display code can nest displayio_lvgl_lock() calls and so the render +// task can take the same lock. FreeRTOS recursive mutexes also give priority inheritance, which +// keeps the low-priority CircuitPython task from starving rendering while it holds the lock. +static SemaphoreHandle_t lvgl_mutex = NULL; +static TaskHandle_t lvgl_task_handle = NULL; + +// Incremented by a framebuffer's vsync ISR. The task coalesces multiple boundaries into one +// render, so a burst of vsyncs while it was busy doesn't cause extra frames. +static volatile uint32_t lvgl_frame_generation = 0; +static uint32_t lvgl_frame_seen = 0; + +// BusDisplay flushes stay on the CircuitPython thread. Its LVGL refresh timer is proxied to this +// background callback, so no supervisor tick is involved. +static background_callback_t lvgl_cp_refresh_cb; + +// LVGL's allocator hooks (LV_USE_STDLIB_MALLOC == LV_STDLIB_CUSTOM). Displays and +// their buffers outlive the VM, so they come from the port heap. +void lv_mem_init(void) { +} + +void lv_mem_deinit(void) { +} + +lv_mem_pool_t lv_mem_add_pool(void *mem, size_t bytes) { + // Pools aren't supported with a custom allocator. + LV_UNUSED(mem); + LV_UNUSED(bytes); + return NULL; +} + +void lv_mem_remove_pool(lv_mem_pool_t pool) { + LV_UNUSED(pool); +} + +// LVGL allocates and frees small blocks (draw tasks, objects, styles) on every frame. +// port_malloc() aligns every PSRAM allocation to MICROPY_BYTES_PER_GC_BLOCK so that heap areas, +// and therefore the bytearrays VideoBuffers stream from, are cache-line aligned. Getting that +// alignment through heap_caps_aligned_alloc() over-allocates and splits off a free remainder on +// every call, which wastes ~64 bytes and shatters PSRAM when it happens millions of times. LVGL +// aligns its own draw buffers inside the allocation (LV_DRAW_BUF_ALIGN), so give it plain +// allocations and leave the aligned port_malloc() path to the GC and DMA buffers. +static void *lvgl_heap_alloc(size_t size) { + void *ptr = NULL; + #ifdef CONFIG_SPIRAM + ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); + #endif + if (ptr == NULL) { + ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT); + } + return ptr; +} + +// A NULL from lv_malloc_core is fatal in most of LVGL (the callers dereference the result), so +// record why it happened and reset into safe mode before LVGL can dereference it. Raising a +// Python MemoryError isn't possible here because this runs during display refreshes, which may +// be on the background display task, not in a Python frame. +static MP_NORETURN void lvgl_heap_log_oom(const char *what, size_t size) { + ESP_LOGE(lvgl_tag, "%s(%u) failed: internal %u free (%u largest), PSRAM %u free (%u largest)", + what, (unsigned)size, + (unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), + (unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), + (unsigned)heap_caps_get_free_size(MALLOC_CAP_SPIRAM), + (unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_SPIRAM)); + reset_into_safe_mode(SAFE_MODE_LVGL_OUT_OF_MEMORY); +} + +void *lv_malloc_core(size_t size) { + void *ptr = lvgl_heap_alloc(size); + if (ptr == NULL) { + lvgl_heap_log_oom("lv_malloc", size); + } + return ptr; +} + +void *lv_realloc_core(void *p, size_t new_size) { + void *grown = NULL; + #ifdef CONFIG_SPIRAM + grown = heap_caps_realloc(p, new_size, MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); + #endif + if (grown == NULL) { + // A failed realloc leaves p alone, so trying the internal heap can't lose it. + grown = heap_caps_realloc(p, new_size, MALLOC_CAP_8BIT); + } + if (grown == NULL) { + lvgl_heap_log_oom("lv_realloc", new_size); + } + return grown; +} + +void lv_free_core(void *p) { + heap_caps_free(p); +} + +void lv_mem_monitor_core(lv_mem_monitor_t *mon_p) { + // LVGL only uses this for its own logging. Report the whole port heap (internal RAM plus + // PSRAM) rather than pretending to track LVGL's share of it. + size_t total = heap_caps_get_total_size(MALLOC_CAP_8BIT); + size_t free_size = heap_caps_get_free_size(MALLOC_CAP_8BIT); + size_t largest = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); + lv_memzero(mon_p, sizeof(*mon_p)); + mon_p->total_size = total; + mon_p->free_size = free_size; + mon_p->free_biggest_size = largest; + mon_p->max_used = total > free_size ? total - free_size : 0; + mon_p->used_pct = total > 0 ? (uint8_t)((total - free_size) * 100 / total) : 0; + mon_p->frag_pct = free_size > 0 ? (uint8_t)(100 - largest * 100 / free_size) : 0; +} + +lv_result_t lv_mem_test_core(void) { + return LV_RESULT_OK; +} + +static void lvgl_tick_cb(void *arg) { + lv_tick_inc(5); +} + +void displayio_lvgl_port_lock(void) { + if (lvgl_mutex != NULL) { + xSemaphoreTakeRecursive(lvgl_mutex, portMAX_DELAY); + } +} + +void displayio_lvgl_port_unlock(void) { + // The shared code used to count lock depth and made an unmatched unlock a no-op. Keep that + // property at the port so a future early return can't assert inside FreeRTOS. + if (lvgl_mutex != NULL && xSemaphoreGetMutexHolder(lvgl_mutex) == (void *)xTaskGetCurrentTaskHandle()) { + xSemaphoreGiveRecursive(lvgl_mutex); + } +} + +static void lvgl_cp_refresh(void *unused) { + LV_UNUSED(unused); + displayio_lvgl_refresh_unsafe_displays(); +} + +// BusDisplay's LVGL refresh timer normally flushes on the render task. Replace its callback with +// one that stops the timer and asks the CircuitPython thread to run lv_refr_now() instead. The +// timer is resumed by LVGL whenever the display is invalidated, so this stays event driven. +static void lvgl_busdisplay_refr_timer_cb(lv_timer_t *timer) { + lv_timer_pause(timer); + background_callback_add(&lvgl_cp_refresh_cb, lvgl_cp_refresh, NULL); +} + +void displayio_lvgl_port_install_busdisplay_refresh(lv_display_t *display) { + lv_timer_t *refr_timer = lv_display_get_refr_timer(display); + if (refr_timer != NULL) { + lv_timer_set_cb(refr_timer, lvgl_busdisplay_refr_timer_cb); + } +} + +IRAM_ATTR void displayio_lvgl_port_frame_boundary(void) { + if (xPortInIsrContext()) { + lvgl_frame_generation++; + BaseType_t woken = pdFALSE; + if (lvgl_task_handle != NULL) { + vTaskNotifyGiveFromISR(lvgl_task_handle, &woken); + } + if (woken == pdTRUE) { + portYIELD_FROM_ISR(); + } + return; + } + // UVC invokes its frame callback from a background task on the CircuitPython thread. Render + // directly there; there is no task handoff. + displayio_lvgl_refresh_paced_displays(); +} + +static void lvgl_task(void *arg) { + LV_UNUSED(arg); + for (;;) { + uint32_t wait = LV_DEF_REFR_PERIOD; + if (displayio_lvgl_auto_refresh()) { + displayio_lvgl_port_lock(); + wait = lv_timer_handler(); + displayio_lvgl_port_unlock(); + // Keep animations and the REPL responsive even when LVGL has nothing to do for a + // long time. The display refresh timer itself runs at LV_DEF_REFR_PERIOD. + if (wait == LV_NO_TIMER_READY || wait > LV_DEF_REFR_PERIOD) { + wait = LV_DEF_REFR_PERIOD; + } + if (wait == 0) { + wait = 1; + } + } + // Invalidations do not wake the task: they are batched until the next timer tick or the + // framebuffer's frame boundary. + ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(wait)); + uint32_t frame_generation = lvgl_frame_generation; + if (frame_generation != lvgl_frame_seen) { + lvgl_frame_seen = frame_generation; + displayio_lvgl_refresh_paced_displays(); + } + } +} + +void displayio_lvgl_port_init(void) { + static bool inited = false; + if (inited) { + return; + } + inited = true; + // The lock has to exist before the task starts and before any display constructor takes it. + lvgl_mutex = xSemaphoreCreateRecursiveMutex(); + if (lvgl_mutex == NULL) { + ESP_LOGE(lvgl_tag, "LVGL mutex create failed"); + } + lv_init(); + // The LVGL compositor is pumped by the dedicated task below. The supervisor tick stays + // enabled for filesystem, USB and BLE background work; it does not pump LVGL. + supervisor_enable_tick(); + // Show which rendering backends LVGL registered. With PPA enabled this includes + // "ESP_PPA" and confirms the P4/S31 accelerator is available. + lv_draw_unit_t *unit = LV_GLOBAL_DEFAULT()->draw_info.unit_head; + while (unit != NULL) { + ESP_LOGI(lvgl_tag, "draw unit: %s", unit->name); + unit = unit->next; + } + const esp_timer_create_args_t args = { + .callback = lvgl_tick_cb, + .name = "lvgl_tick", + }; + esp_timer_create(&args, &lvgl_tick_timer); + esp_timer_start_periodic(lvgl_tick_timer, 5000); // 5ms + + // The task must run above the CircuitPython task so a tight Python loop can't starve + // rendering. Pin it to the CircuitPython core as well: background_callback_add() (used by the + // BusDisplay proxy) is interrupt-safe on a single core, and sharing the core keeps it from + // racing the CircuitPython thread's callback list. A recursive mutex (priority inheritance) + // keeps that from blocking the main task for long. 16 KB covers the draw paths plus LVGL's + // internal compositor state; 8 KB overflowed on complex screens. + BaseType_t ok = xTaskCreatePinnedToCore(lvgl_task, "lvgl", 16384, NULL, + ESP_TASK_MAIN_PRIO + 1, &lvgl_task_handle, CONFIG_ESP_MAIN_TASK_AFFINITY); + if (ok != pdPASS) { + lvgl_task_handle = NULL; + ESP_LOGE(lvgl_tag, "LVGL task create failed"); + } else { + ESP_LOGI(lvgl_tag, "render task started"); + } +} +#endif diff --git a/ports/espressif/supervisor/port.c b/ports/espressif/supervisor/port.c index e4538636c57..5b41fe3c50d 100644 --- a/ports/espressif/supervisor/port.c +++ b/ports/espressif/supervisor/port.c @@ -17,7 +17,6 @@ #include "py/runtime.h" #if MICROPY_PERSISTENT_CODE_LOAD_NATIVE #include "py/persistentcode.h" -#include "soc/soc_caps.h" #if defined(SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE) && SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE #include "esp_cache.h" #endif @@ -25,6 +24,7 @@ #include "esp_mac.h" #include "esp_memory_utils.h" +#include "soc/soc_caps.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -332,6 +332,8 @@ void port_heap_init(void) { // The IDF sets up the heap, so we don't need to. } +static void port_heap_log_usage(size_t size, void *ptr); + void *port_malloc(size_t size, bool dma_capable) { if (dma_capable) { // SPIRAM is not DMA-capable, so don't bother to ask for it. @@ -354,9 +356,32 @@ void *port_malloc(size_t size, bool dma_capable) { if (ptr == NULL) { ptr = heap_caps_malloc(size, MALLOC_CAP_8BIT); } + port_heap_log_usage(size, ptr); return ptr; } +// Debug aid for running out of PSRAM: a board that exhausts it takes the display stack (LVGL) +// down with a hard fault, and the crash dump doesn't say what used the memory. Log the big +// allocations (a GC heap split is at least megabytes) and, once the heap is nearly gone, the +// small ones too, so the log shows which consumer took the last of it. Silent otherwise. +static void port_heap_log_usage(size_t size, void *ptr) { + #ifdef CONFIG_SPIRAM + size_t psram_free = heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM); + const size_t big = 1024 * 1024; + const size_t low_water = 512 * 1024; + if (size < big && (psram_free >= low_water || size < 1024)) { + return; + } + ESP_LOGI(TAG, "port_malloc(%u) = %p: PSRAM %u free (%u largest), internal %u free", + (unsigned)size, ptr, (unsigned)psram_free, + (unsigned)heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM), + (unsigned)heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)); + #else + (void)size; + (void)ptr; + #endif +} + #if !CIRCUITPY_ALL_MEMORY_DMA_CAPABLE bool port_buffer_is_dma_capable(const void *ptr, size_t len) { if (len == 0 || !esp_ptr_dma_capable(ptr) || @@ -386,8 +411,27 @@ void *port_realloc(void *ptr, size_t size, bool dma_capable) { } size_t port_heap_get_largest_free_size(void) { - size_t free_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); - return free_size; + size_t largest = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); + #ifdef CONFIG_SPIRAM + // The VM heap only ever grows, and gc_try_add_heap() takes up to the whole largest free block + // when it doesn't need the full doubling. Without a reserve a Python allocation can therefore + // swallow the last of PSRAM, and the next display/LVGL allocation (a framebuffer, a TileGrid + // compose buffer, a draw task) fails. LVGL doesn't check for NULL there, so the board hard + // faults in the renderer instead of the VM raising MemoryError. Keep up to a quarter of the + // heap (4 MB on a 16 MB board) for the port side; gc.mem_free() reports the reduced number, + // which is the honest one. + size_t total = heap_caps_get_total_size(MALLOC_CAP_8BIT); + size_t reserve = total / 4; + const size_t max_reserve = 4 * 1024 * 1024; + if (reserve > max_reserve) { + reserve = max_reserve; + } + if (largest <= reserve) { + return 0; + } + largest -= reserve; + #endif + return largest; } #if MICROPY_PERSISTENT_CODE_LOAD_NATIVE diff --git a/ports/unix/displayio_min.c b/ports/unix/displayio_min.c index 1f5b87048a3..cb8a083b3ef 100644 --- a/ports/unix/displayio_min.c +++ b/ports/unix/displayio_min.c @@ -76,16 +76,3 @@ const mp_obj_module_t displayio_module = { }; MP_REGISTER_MODULE(MP_QSTR_displayio, displayio_module); - -displayio_buffer_transform_t null_transform = { - .x = 0, - .y = 0, - .dx = 1, - .dy = 1, - .scale = 1, - .width = 0, - .height = 0, - .mirror_x = false, - .mirror_y = false, - .transpose_xy = false -}; diff --git a/ports/unix/variants/coverage/mpconfigvariant.mk b/ports/unix/variants/coverage/mpconfigvariant.mk index 94bc015cb6d..9196577f69a 100644 --- a/ports/unix/variants/coverage/mpconfigvariant.mk +++ b/ports/unix/variants/coverage/mpconfigvariant.mk @@ -106,9 +106,9 @@ SRC_BITMAP := \ shared-module/bitmapfilter/__init__.c \ shared-module/bitmaptools/__init__.c \ shared-module/displayio/area.c \ - shared-module/displayio/Bitmap.c \ - shared-module/displayio/ColorConverter.c \ - shared-module/displayio/Palette.c \ + shared-module/displayio/legacy/Bitmap.c \ + shared-module/displayio/legacy/ColorConverter.c \ + shared-module/displayio/legacy/Palette.c \ shared-module/floppyio/__init__.c \ shared-module/jpegio/__init__.c \ shared-module/jpegio/JpegDecoder.c \ @@ -126,7 +126,7 @@ SRC_BITMAP := \ shared-module/vectorio/__init__.c \ shared-module/vectorio/Polygon.c \ shared-module/vectorio/Rectangle.c \ - shared-module/vectorio/VectorShape.c \ + shared-module/vectorio/legacy/VectorShape.c \ shared-module/traceback/__init__.c \ shared-module/zlib/__init__.c \ diff --git a/ports/zephyr-cp/CMakeLists.txt b/ports/zephyr-cp/CMakeLists.txt index 1e9696d86b1..b75a8fbb5e9 100644 --- a/ports/zephyr-cp/CMakeLists.txt +++ b/ports/zephyr-cp/CMakeLists.txt @@ -16,6 +16,10 @@ set(ZEPHYR_EXTRA_MODULES # Kconfig-only module with defaults that must merge before Zephyr's # subsystem Kconfig files (see internal-modules/circuitpython-kconfig/Kconfig). ${CMAKE_CURRENT_SOURCE_DIR}/internal-modules/circuitpython-kconfig + # The same LVGL copy the Espressif port builds (lib/lvgl), instead of the one + # west checks out for Zephyr. It has the same module name, which zephyr_module.py + # resolves in favor of extra modules, so both ports build the same LVGL. + ${CMAKE_CURRENT_SOURCE_DIR}/../../lib/lvgl ) find_package(Zephyr REQUIRED HINTS ${ZEPHYR_BASE} $ENV{ZEPHYR_BASE} lib/zephyr) @@ -66,8 +70,24 @@ set(external_project_cflags zephyr_get(TRANSLATION SYSBUILD GLOBAL) zephyr_get(CONFIG_LTO SYSBUILD GLOBAL) +zephyr_get(CONFIG_LVGL SYSBUILD GLOBAL) zephyr_get(BOARD_ALIAS SYSBUILD GLOBAL) +# displayio hands indexed bitmaps to LVGL as I1/I2/I4/I8 images. Decode them fully +# (instead of line by line) so they can be scaled and cached. LVGL's lv_conf_internal.h +# doesn't map a Kconfig option for this, so define it directly. +if(CONFIG_LVGL) + zephyr_compile_definitions(LV_BIN_DECODER_RAM_LOAD=1) +endif() + +# LVGL module (used by the displayio LVGL compositing core) passes its include paths to the +# CircuitPython static library build explicitly, since the core build does not consume CMake +# target information. +set(LVGL_MODULE_DIR "") +if(CONFIG_LVGL AND DEFINED ZEPHYR_LVGL_MODULE_DIR) + set(LVGL_MODULE_DIR ${ZEPHYR_LVGL_MODULE_DIR}) +endif() + ExternalProject_Add(circuitpython DOWNLOAD_COMMAND "" CONFIGURE_COMMAND "" @@ -84,6 +104,8 @@ ExternalProject_Add(circuitpython PORT_SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR} TRANSLATION=${TRANSLATION} LTO=${CONFIG_LTO} + LVGL=${CONFIG_LVGL} + LVGL_DIR=${LVGL_MODULE_DIR} BUILD_BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/libcircuitpython.a BUILD_JOB_SERVER_AWARE TRUE BUILD_ALWAYS TRUE diff --git a/ports/zephyr-cp/Makefile b/ports/zephyr-cp/Makefile index 3cb1cf5ed14..69af73f9171 100644 --- a/ports/zephyr-cp/Makefile +++ b/ports/zephyr-cp/Makefile @@ -248,10 +248,11 @@ sim: clean-sim: rm -rf $(wildcard build-native_*) -# Every board the test suite uses: native sim (non-asan + asan) and the bsim -# boards (tests/bsim/conftest.py parametrizes over both). Zephyr samples for -# bsim tests are built on demand by the zephyr_sample fixture. -TEST_BOARDS := native_native_sim native_native_sim_asan native_native_sim_lfs native_nrf5340bsim native_nrf54lm20bsim +# Every board the test suite uses: native sim (legacy + lvgl compositors, each +# with an asan variant, plus lfs) and the bsim boards (tests/bsim/conftest.py parametrizes over +# both). Zephyr samples for bsim tests are built on demand by the zephyr_sample +# fixture. +TEST_BOARDS := native_native_sim native_native_sim_lvgl native_native_sim_lvgl_asan native_native_sim_asan native_native_sim_lfs native_nrf5340bsim native_nrf54lm20bsim # Delegate to a sub-make with BOARD set so the per-board build rule (and its # shield args + bsim prep) applies. The targets are phony: the west builds are diff --git a/ports/zephyr-cp/README.md b/ports/zephyr-cp/README.md index c72f6a95f29..241eb0380e3 100644 --- a/ports/zephyr-cp/README.md +++ b/ports/zephyr-cp/README.md @@ -109,6 +109,23 @@ python -m serial.tools.miniterm /dev/pts/1 115200 If the container has no display (it was created without `DISPLAY` or with `ZCP_NO_DISPLAY=1`), run the simulator as `SDL_VIDEODRIVER=dummy make run-sim`. +### Testing LVGL with AddressSanitizer + +The `native_native_sim_lvgl_asan` board runs the LVGL displayio compositor with +AddressSanitizer enabled for CircuitPython and LVGL. It also poisons allocations in +LVGL's private `sys_heap` to catch buffer overruns and use-after-free, and stops on +the first ASan error. ASan does not replace explicit leak checks. + +```sh +make BOARD=native_native_sim_lvgl_asan +make test TEST_BOARDS=native_native_sim_lvgl_asan \ + PYTEST_ARGS="tests/ --ignore=tests/bsim -k native_native_sim_lvgl_asan" +``` + +The executable is `build-native_native_sim_lvgl_asan/zephyr-cp/zephyr/zephyr.exe`. +The regular `make test` target includes this board as well as the unsanitized LVGL +and legacy-compositor variants. + ## Running the core test suite `make test-run-tests` runs `tests/run-tests.py` against the native simulator. It needs diff --git a/ports/zephyr-cp/bindings/zephyr_display/Display.c b/ports/zephyr-cp/bindings/zephyr_display/Display.c index 0923618c50a..5bece7b3839 100644 --- a/ports/zephyr-cp/bindings/zephyr_display/Display.c +++ b/ports/zephyr-cp/bindings/zephyr_display/Display.c @@ -6,22 +6,67 @@ #include "bindings/zephyr_display/Display.h" +#include + +#include "bindings/zephyr_kernel/__init__.h" #include "py/objproperty.h" #include "py/objtype.h" #include "py/runtime.h" #include "shared-bindings/displayio/Group.h" #include "shared-module/displayio/__init__.h" +//| class Display: +//| """Manage the display selected by the Zephyr devicetree +//| +//| There is no display bus argument because the device comes from the +//| ``zephyr,display`` chosen devicetree node. +//| +//| Creating a `Display` releases the display built during startup +//| (``supervisor.runtime.display``), as with other display classes.""" +//| +// The current implementation composites with LVGL, which drives only one display and draws +// from its own fixed-size heap (CONFIG_LV_Z_MEM_POOL_SIZE). Those are implementation details, +// so they stay out of the docs above, but they explain why only one Display can exist and why +// complex scenes can fail inside the draw code instead of raising MemoryError. +//| def __init__(self, *, rotation: int = 0, auto_refresh: bool = True) -> None: +//| """Create a `Display` for the ``zephyr,display`` device. +//| +//| :param int rotation: The rotation of the display in degrees. Must be a multiple +//| of 90. +//| :param bool auto_refresh: Automatically refresh the screen. +//| """ +//| ... +//| static mp_obj_t zephyr_display_display_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { - (void)type; - (void)n_args; - (void)n_kw; - (void)all_args; - mp_raise_NotImplementedError(NULL); - return mp_const_none; + enum { ARG_rotation, ARG_auto_refresh }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_rotation, MP_ARG_KW_ONLY | MP_ARG_INT, { .u_int = 0 } }, + { MP_QSTR_auto_refresh, MP_ARG_KW_ONLY | MP_ARG_BOOL, { .u_bool = true } }, + }; + 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 rotation = args[ARG_rotation].u_int; + if (rotation % 90 != 0) { + mp_raise_ValueError(MP_ERROR_TEXT("Display rotation must be in 90 degree increments")); + } + + const struct device *display_dev = device_get_binding(DEVICE_DT_NAME(DT_CHOSEN(zephyr_display))); + if (display_dev == NULL || !device_is_ready(display_dev)) { + raise_zephyr_error(-ENODEV); + } + + // The display device is a singleton and LVGL drives only one display at a time, so the one + // board_init() made has to be released first (as with any other display class). + primary_display_t *display_obj = allocate_display_or_raise(); + zephyr_display_display_obj_t *self = &display_obj->zephyr_display; + self->base.type = &zephyr_display_display_type; + common_hal_zephyr_display_display_construct_from_device(self, display_dev, rotation, + args[ARG_auto_refresh].u_bool); + return MP_OBJ_FROM_PTR(self); } static zephyr_display_display_obj_t *native_display(mp_obj_t display_obj) { @@ -38,6 +83,34 @@ static mp_obj_t zephyr_display_display_obj_show(mp_obj_t self_in, mp_obj_t group } MP_DEFINE_CONST_FUN_OBJ_2(zephyr_display_display_show_obj, zephyr_display_display_obj_show); +//| def refresh( +//| self, +//| *, +//| target_frames_per_second: Optional[int] = None, +//| minimum_frames_per_second: int = 0, +//| ) -> bool: +//| """When auto_refresh is off, and ``target_frames_per_second`` is not `None` this +//| waits for the target frame rate and then refreshes the display, returning +//| `True`. If the call has taken too long since the last refresh call for the given +//| target frame rate, then the refresh returns `False` immediately without updating +//| the screen to try to keep latency low. +//| +//| If the time since the last successful refresh is below the minimum frame rate, +//| then an exception will be raised. The default ``minimum_frames_per_second`` of 0 +//| disables this behavior. +//| +//| When auto_refresh is off, and ``target_frames_per_second`` is `None` this will +//| update the display immediately. When auto_refresh is on, it updates the display +//| immediately too; the display also updates without calls to this. +//| +//| :param Optional[int] target_frames_per_second: The target frame rate that +//| :py:func:`refresh` should try to achieve. Set to `None` for immediate +//| refresh. +//| :param int minimum_frames_per_second: The minimum number of times the screen +//| should be updated per second. +//| """ +//| ... +//| static mp_obj_t zephyr_display_display_obj_refresh(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_target_frames_per_second, @@ -73,6 +146,8 @@ static mp_obj_t zephyr_display_display_obj_refresh(size_t n_args, const mp_obj_t } MP_DEFINE_CONST_FUN_OBJ_KW(zephyr_display_display_refresh_obj, 1, zephyr_display_display_obj_refresh); +//| auto_refresh: bool +//| """True when the display is refreshed automatically.""" static mp_obj_t zephyr_display_display_obj_get_auto_refresh(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); return mp_obj_new_bool(common_hal_zephyr_display_display_get_auto_refresh(self)); @@ -90,6 +165,11 @@ MP_PROPERTY_GETSET(zephyr_display_display_auto_refresh_obj, (mp_obj_t)&zephyr_display_display_get_auto_refresh_obj, (mp_obj_t)&zephyr_display_display_set_auto_refresh_obj); +//| brightness: float +//| """The brightness of the display as a float. 0.0 is off and 1.0 is full brightness. +//| +//| .. note:: Zephyr displays do not implement brightness control yet, so reading or +//| writing this raises `RuntimeError`.""" static mp_obj_t zephyr_display_display_obj_get_brightness(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); mp_float_t brightness = common_hal_zephyr_display_display_get_brightness(self); @@ -118,6 +198,8 @@ MP_PROPERTY_GETSET(zephyr_display_display_brightness_obj, (mp_obj_t)&zephyr_display_display_get_brightness_obj, (mp_obj_t)&zephyr_display_display_set_brightness_obj); +//| width: int +//| """The width of the display in pixels, with rotation applied.""" static mp_obj_t zephyr_display_display_obj_get_width(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); return MP_OBJ_NEW_SMALL_INT(common_hal_zephyr_display_display_get_width(self)); @@ -125,6 +207,8 @@ static mp_obj_t zephyr_display_display_obj_get_width(mp_obj_t self_in) { MP_DEFINE_CONST_FUN_OBJ_1(zephyr_display_display_get_width_obj, zephyr_display_display_obj_get_width); MP_PROPERTY_GETTER(zephyr_display_display_width_obj, (mp_obj_t)&zephyr_display_display_get_width_obj); +//| height: int +//| """The height of the display in pixels, with rotation applied.""" static mp_obj_t zephyr_display_display_obj_get_height(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); return MP_OBJ_NEW_SMALL_INT(common_hal_zephyr_display_display_get_height(self)); @@ -132,6 +216,8 @@ static mp_obj_t zephyr_display_display_obj_get_height(mp_obj_t self_in) { MP_DEFINE_CONST_FUN_OBJ_1(zephyr_display_display_get_height_obj, zephyr_display_display_obj_get_height); MP_PROPERTY_GETTER(zephyr_display_display_height_obj, (mp_obj_t)&zephyr_display_display_get_height_obj); +//| rotation: int +//| """The rotation of the display in degrees.""" static mp_obj_t zephyr_display_display_obj_get_rotation(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); return MP_OBJ_NEW_SMALL_INT(common_hal_zephyr_display_display_get_rotation(self)); @@ -149,6 +235,12 @@ MP_PROPERTY_GETSET(zephyr_display_display_rotation_obj, (mp_obj_t)&zephyr_display_display_get_rotation_obj, (mp_obj_t)&zephyr_display_display_set_rotation_obj); +//| root_group: Optional[displayio.Group] +//| """The `displayio.Group` that is drawn on the display, or `None` when the display +//| is cleared. +//| +//| A `displayio.Group` can only be the root group of one display at a time. Setting a +//| group that is already in use raises `ValueError`.""" static mp_obj_t zephyr_display_display_obj_get_root_group(mp_obj_t self_in) { zephyr_display_display_obj_t *self = native_display(self_in); return common_hal_zephyr_display_display_get_root_group(self); diff --git a/ports/zephyr-cp/bindings/zephyr_display/Display.h b/ports/zephyr-cp/bindings/zephyr_display/Display.h index a50dda8fe8b..80693228520 100644 --- a/ports/zephyr-cp/bindings/zephyr_display/Display.h +++ b/ports/zephyr-cp/bindings/zephyr_display/Display.h @@ -11,6 +11,10 @@ extern const mp_obj_type_t zephyr_display_display_type; +// Port hooks for the LVGL compositor (see shared-module/displayio/lvgl/lvgl_internal.h). +void displayio_lvgl_port_lock(void); +void displayio_lvgl_port_unlock(void); + #define NO_FPS_LIMIT 0xffffffff void common_hal_zephyr_display_display_construct_from_device(zephyr_display_display_obj_t *self, diff --git a/ports/zephyr-cp/bindings/zephyr_display/__init__.c b/ports/zephyr-cp/bindings/zephyr_display/__init__.c index eecfeeaec58..180f8572977 100644 --- a/ports/zephyr-cp/bindings/zephyr_display/__init__.c +++ b/ports/zephyr-cp/bindings/zephyr_display/__init__.c @@ -9,6 +9,21 @@ #include "bindings/zephyr_display/Display.h" +//| """Display access for Zephyr boards +//| +//| This module provides `Display`, a `displayio` display class for the device selected by +//| the ``zephyr,display`` devicetree chosen property. +//| +//| .. note:: This module is Zephyr-specific. On other ports the equivalent class is one of +//| the `displayio` display classes, such as `busdisplay.BusDisplay`. +//| """ +// +// The current implementation composites with LVGL, which draws from its own fixed-size heap +// (CONFIG_LV_Z_MEM_POOL_SIZE) instead of the Python heap and drives only one display. Both are +// implementation details rather than part of this class's contract, so they aren't documented +// above, but they explain why complex scenes can fail inside LVGL and why only one `Display` +// can exist. + static const mp_rom_map_elem_t zephyr_display_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_zephyr_display) }, { MP_ROM_QSTR(MP_QSTR_Display), MP_ROM_PTR(&zephyr_display_display_type) }, diff --git a/ports/zephyr-cp/boards/board_aliases.cmake b/ports/zephyr-cp/boards/board_aliases.cmake index 71e2b3edbd7..7efe36f80fd 100644 --- a/ports/zephyr-cp/boards/board_aliases.cmake +++ b/ports/zephyr-cp/boards/board_aliases.cmake @@ -38,6 +38,8 @@ cp_board_alias(adafruit_metro_m7_airlift_zephyr adafruit_metro_m7_airlift) cp_board_alias(native_native_sim native_sim/native) cp_board_alias(native_native_sim_asan native_sim/native) cp_board_alias(native_native_sim_lfs native_sim/native) +cp_board_alias(native_native_sim_lvgl native_sim/native) +cp_board_alias(native_native_sim_lvgl_asan native_sim/native) cp_board_alias(native_nrf5340bsim nrf5340bsim/nrf5340/cpuapp) cp_board_alias(native_nrf54lm20bsim nrf54lm20bsim/nrf54lm20a/cpuapp) cp_board_alias(nordic_nrf5340dk nrf5340dk/nrf5340/cpuapp) diff --git a/ports/zephyr-cp/boards/native/native_sim/board.conf b/ports/zephyr-cp/boards/native/native_sim/board.conf index af7c546765e..6374db35701 100644 --- a/ports/zephyr-cp/boards/native/native_sim/board.conf +++ b/ports/zephyr-cp/boards/native/native_sim/board.conf @@ -27,6 +27,11 @@ CONFIG_SDL_DISPLAY=y # Don't require hardware acceleration so the software renderer can be used. CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n +# Legacy scanline displayio compositor (CIRCUITPY_DISPLAYIO_LVGL=0). The +# LVGL-based compositor is exercised by the native_sim_lvgl board; this board +# keeps the scanline core and the display-side modules it needs (busdisplay, +# fourwire, i2cdisplaybus, vectorio, ...). + # EEPROM emulation for testing CONFIG_EEPROM=y CONFIG_EEPROM_AT24=y diff --git a/ports/zephyr-cp/boards/native/native_sim_asan/board.conf b/ports/zephyr-cp/boards/native/native_sim_asan/board.conf index 89dfb6deda6..34f319ed8a2 100644 --- a/ports/zephyr-cp/boards/native/native_sim_asan/board.conf +++ b/ports/zephyr-cp/boards/native/native_sim_asan/board.conf @@ -30,6 +30,10 @@ CONFIG_SDL_DISPLAY=y # Don't require hardware acceleration so the software renderer can be used. CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n +# Legacy scanline displayio compositor (CIRCUITPY_DISPLAYIO_LVGL=0); see +# native_sim/board.conf. The ASan board exists to catch memory errors in the +# default (legacy) build. + # EEPROM emulation for testing CONFIG_EEPROM=y CONFIG_EEPROM_AT24=y diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl/autogen_board_info.toml b/ports/zephyr-cp/boards/native/native_sim_lvgl/autogen_board_info.toml new file mode 100644 index 00000000000..8166c133c96 --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl/autogen_board_info.toml @@ -0,0 +1,142 @@ +# This file is autogenerated when a board is built. Do not edit. Do commit it to git. Other scripts use its info. +name = "zephyr Native simulator - LVGL displayio compositor" + +[modules] +__future__ = true +_airlift = false +_bleio = false +_eve = false +_pew = false +_pixelmap = false +_stage = false +adafruit_bus_device = true +adafruit_pixelbuf = false +aesio = true +alarm = false +analogbufio = false +analogio = true # Zephyr board has analogio +async_spi = true # Zephyr board has async_spi +atexit = true +audiobusio = true # Zephyr board has audiobusio +audiocore = true # Zephyr board has audiobusio +audiodelays = true # Zephyr board has audiobusio +audiofilewriter = false +audiofilters = true # Zephyr board has audiobusio +audiofreeverb = true # Zephyr board has audiobusio +audioi2sin = false +audioio = false +audiomixer = true # Zephyr board has audiobusio +audiomp3 = true # Zephyr board has audiobusio +audiopwmio = false +audiospeed = false +audiotap = false +aurora_epaper = false +bitbangio = true +bitmapfilter = true # Zephyr board has busio +bitmaptools = false # Legacy scanline display module; excluded from LVGL builds +bitops = false +board = true # Always enabled +busdisplay = true # Zephyr board has busio +busio = true # Zephyr board has busio +camera = false +canio = false +codeop = true +countio = false +cryptography = false +digitalio = true +displayio = true # Zephyr board has displayio +dotclockframebuffer = false +dualbank = false +emmcio = false +epaperdisplay = false # Legacy scanline display module; excluded from LVGL builds +floppyio = false +fontio = true # Zephyr board has busio +fourwire = true # Zephyr board has busio +framebufferio = true # Zephyr board has busio +frequencyio = false +getpass = true +gifio = true # Zephyr board has busio +gnss = false +hardwarekey = false +hashlib = true # Zephyr networking enabled +hmac = false +hostnetwork = true # Zephyr board has hostnetwork +i2cdisplaybus = true # Zephyr board has busio +i2cioexpander = false +i2ctarget = false +imagecapture = false +ipaddress = true # Zephyr networking enabled +is31fl3741 = false +jpegio = false # Legacy scanline display module; excluded from LVGL builds +keypad = true +keypad_demux = true +locale = true +lvfontio = true # Zephyr board has busio +math = true +max3421e = false +mcp4822 = false +mdns = false +memorymap = false +memorymonitor = false +microcontroller = true +mii = false +mipicsi = false +mipidsi = false +msgpack = true +neopixel_write = false +nvm = true # Board has NVM partition +onewireio = false +os = true +paralleldisplaybus = false +picogame = false +ps2io = false +pulseio = false +pwmio = true # Zephyr board has pwmio +qrio = false +qspibus = false +rainbowio = true +random = true +rclcpy = false +rgbmatrix = false +rotaryio = true # Zephyr board has rotaryio +rtc = true # Zephyr board has rtc +sdcardio = true # Zephyr board has busio +sdioio = false +sharpdisplay = false # Legacy scanline display module; excluded from LVGL builds +socketpool = true # Zephyr networking enabled +spitarget = false +ssl = true # Zephyr hostnetwork enabled +storage = true # Zephyr board has flash +struct = true +supervisor = true +synthio = true # Zephyr board has audiobusio +terminalio = true # Zephyr board has busio +tilepalettemapper = false # Legacy scanline display module; excluded from LVGL builds +time = true +touchio = true +traceback = true +uheap = false +usb = false +usb_audio = false +usb_cdc = false +usb_hid = false +usb_host = false +usb_host_audio = false +usb_host_bulk = false +usb_midi = false +usb_video = false +ustack = false +vectorio = true # Zephyr board has busio +videocore = false +warnings = true +watchdog = true # Zephyr board has watchdog +wifi = false +zephyr_display = true # Zephyr board has zephyr_display +zephyr_kernel = false +zlib = true +# extmod modules shared with MicroPython +asyncio = true +binascii = true +json = true +re = true +select = true diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf new file mode 100644 index 00000000000..13f7a316086 --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.conf @@ -0,0 +1,75 @@ +# No Bluetooth hardware on native_sim +CONFIG_BT=n + +# native_sim has no physical package: use the identity package pin map so +# pin objects (LED, P_00..P_31) resolve to their GPIO controller. +CONFIG_IOBROKER_PACKAGE_ONE_TO_ONE=y + +CONFIG_EMUL=y +CONFIG_GPIO=y +CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=n + +# So we can test safe mode +CONFIG_NATIVE_SIM_REBOOT=y + +CONFIG_TRACING=y +CONFIG_TRACING_PERFETTO=y +CONFIG_TRACING_SYNC=y +CONFIG_TRACING_BACKEND_POSIX=y +CONFIG_TRACING_GPIO=y + +# I2C emulation for testing +CONFIG_I2C_EMUL=y + +# Display emulation for display/terminal golden tests. +CONFIG_DISPLAY=y +CONFIG_SDL_DISPLAY=y +# Don't require hardware acceleration so the software renderer can be used. +CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n + +# LVGL-based displayio compositing core. LVGL renders on its own RTOS thread (this +# workqueue, also matching the RTOS deployment model); CircuitPython's compositor mirror +# mutations take the glue's LVGL mutex so they don't interleave with lv_timer_handler. +CONFIG_LVGL=y +CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y +# LVGL allocates objects and draw buffers from its own heap sizing. +CONFIG_LV_Z_MEM_POOL_SIZE=16777216 +# Cache decoded images (OnDiskBitmaps and other LVGL images) so they're only decoded +# again when the source or palette changes. +CONFIG_LV_CACHE_DEF_SIZE=4194304 + +# EEPROM emulation for testing +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y +CONFIG_EEPROM_AT2X_EMUL=y + +# ADC emulation for analogio testing +CONFIG_ADC=y +CONFIG_ADC_EMUL=y + +# DAC emulation for analogio testing (the vnd,dac node in the board overlay +# turns on CONFIG_DAC_TEST) +CONFIG_DAC=y + +# PWM emulation for pwmio testing (the adafruit,pwm-emul nodes in the board +# overlay turn on CONFIG_PWM_ADAFRUIT_EMUL) +CONFIG_PWM=y + +# I2S SDL emulation for audio testing +CONFIG_I2S_SDL=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_TCP=y +CONFIG_NET_SOCKETS=y +CONFIG_ETH_NATIVE_TAP=n +CONFIG_NET_DRIVERS=y +CONFIG_NET_SOCKETS_OFFLOAD=y +CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y +CONFIG_HEAP_MEM_POOL_SIZE=1024 + +CONFIG_NET_LOG=y + +CONFIG_MBEDTLS=y +CONFIG_PSA_WANT_ALG_SHA_1=y +CONFIG_PSA_WANT_ALG_SHA_256=y diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl/board.overlay b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.overlay new file mode 100644 index 00000000000..aebcb45bb14 --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl/board.overlay @@ -0,0 +1,9 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Device tree overlay for CircuitPython on native_sim with the LVGL-based + * displayio compositor. Identical hardware description to the legacy-compositor + * board, so just reuse it. + */ + +#include "../native_sim/board.overlay" diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl/circuitpython.toml b/ports/zephyr-cp/boards/native/native_sim_lvgl/circuitpython.toml new file mode 100644 index 00000000000..bcff2a77f75 --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl/circuitpython.toml @@ -0,0 +1,2 @@ +NAME = "Native simulator - LVGL displayio compositor" +CIRCUITPY_BUILD_EXTENSIONS = ["exe"] diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/autogen_board_info.toml b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/autogen_board_info.toml new file mode 100644 index 00000000000..51a9d95117c --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/autogen_board_info.toml @@ -0,0 +1,143 @@ +# This file is autogenerated when a board is built. Do not edit. Do commit it to git. Other scripts use its info. +name = "zephyr Native simulator - LVGL + ASan" + +[modules] +__future__ = true +_airlift = false +_bleio = false +_eve = false +_pew = false +_pixelmap = false +_stage = false +adafruit_bus_device = true +adafruit_pixelbuf = false +aesio = true +alarm = false +analogbufio = false +analogio = true # Zephyr board has analogio +async_spi = true # Zephyr board has async_spi +atexit = true +audiobusio = true # Zephyr board has audiobusio +audiocore = true # Zephyr board has audiobusio +audiodelays = true # Zephyr board has audiobusio +audiofilewriter = false +audiofilters = true # Zephyr board has audiobusio +audiofreeverb = true # Zephyr board has audiobusio +audioi2sin = false +audioio = false +audiomixer = true # Zephyr board has audiobusio +audiomp3 = true # Zephyr board has audiobusio +audiopwmio = false +audiospeed = false +audiotap = false +aurora_epaper = false +bitbangio = true +bitmapfilter = true # Zephyr board has busio +bitmaptools = true # Zephyr board has busio +bitops = false +board = true # Always enabled +busdisplay = true # Zephyr board has busio +busio = true # Zephyr board has busio +camera = false +canio = false +codeop = true +countio = false +cryptography = false +digitalio = true +displayio = true # Zephyr board has displayio +dotclockframebuffer = false +dualbank = false +emmcio = false +epaperdisplay = false # Legacy scanline display module; excluded from LVGL builds +floppyio = false +fontio = true # Zephyr board has busio +fourwire = true # Zephyr board has busio +framebufferio = true # Zephyr board has busio +frequencyio = false +getpass = true +gifio = true # Zephyr board has busio +gnss = false +hardwarekey = false +hashlib = true # Zephyr networking enabled +hmac = false +hostnetwork = true # Zephyr board has hostnetwork +i2cdisplaybus = true # Zephyr board has busio +i2cioexpander = false +i2ctarget = false +imagecapture = false +ipaddress = true # Zephyr networking enabled +is31fl3741 = false +jpegio = true # Zephyr board has busio +keypad = true +keypad_demux = true +locale = true +lvfontio = true # Zephyr board has busio +math = true +max3421e = false +mcp4822 = false +mdns = false +memorymap = false +memorymonitor = false +microcontroller = true +mii = false +mipicsi = false +mipidsi = false +msgpack = true +neopixel_write = false +nvm = true # Board has NVM partition +onewireio = false +os = true +paralleldisplaybus = false +picogame = false +ps2io = false +pulseio = false +pwmio = true # Zephyr board has pwmio +qrio = false +qspibus = false +rainbowio = true +random = true +rclcpy = false +rgbmatrix = false +rotaryio = true # Zephyr board has rotaryio +rtc = true # Zephyr board has rtc +sdcardio = true # Zephyr board has busio +sdioio = false +sharpdisplay = false # Legacy scanline display module; excluded from LVGL builds +socketpool = true # Zephyr networking enabled +spitarget = false +ssl = true # Zephyr hostnetwork enabled +storage = true # Zephyr board has flash +struct = true +supervisor = true +synthio = true # Zephyr board has audiobusio +terminalio = true # Zephyr board has busio +tilepalettemapper = false # Legacy scanline display module; excluded from LVGL builds +time = true +touchio = true +traceback = true +uheap = false +usb = false +usb_audio = false +usb_cdc = false +usb_hid = false +usb_host = false +usb_host_audio = false +usb_host_bulk = false +usb_midi = false +usb_video = false +ustack = false +vectorio = true # Zephyr board has busio +videocore = false +videogif = false +warnings = true +watchdog = true # Zephyr board has watchdog +wifi = false +zephyr_display = true # Zephyr board has zephyr_display +zephyr_kernel = false +zlib = true +# extmod modules shared with MicroPython +asyncio = true +binascii = true +json = true +re = true +select = true diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.conf b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.conf new file mode 100644 index 00000000000..926d977fe9c --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.conf @@ -0,0 +1,83 @@ +# AddressSanitizer covers both CircuitPython and LVGL. Poison allocations in +# LVGL's sys_heap too, so accesses to freed buffers and buffer overruns are caught. +# The big-heap format provides the 8-byte alignment ASan's shadow requires. +CONFIG_ASAN=y +CONFIG_ASAN_RECOVER=n +CONFIG_SYS_HEAP_BIG_ONLY=y +CONFIG_SYS_HEAP_SANITIZER_ASAN=y + +# No Bluetooth hardware on native_sim +CONFIG_BT=n + +# native_sim has no physical package: use the identity package pin map so +# pin objects (LED, P_00..P_31) resolve to their GPIO controller. +CONFIG_IOBROKER_PACKAGE_ONE_TO_ONE=y + +CONFIG_EMUL=y +CONFIG_GPIO=y +CONFIG_NATIVE_SIM_SLOWDOWN_TO_REAL_TIME=n + +# So we can test safe mode +CONFIG_NATIVE_SIM_REBOOT=y + +CONFIG_TRACING=y +CONFIG_TRACING_PERFETTO=y +CONFIG_TRACING_SYNC=y +CONFIG_TRACING_BACKEND_POSIX=y +CONFIG_TRACING_GPIO=y + +# I2C emulation for testing +CONFIG_I2C_EMUL=y + +# Display emulation for display/terminal golden tests. +CONFIG_DISPLAY=y +CONFIG_SDL_DISPLAY=y +# Don't require hardware acceleration so the software renderer can be used. +CONFIG_SDL_DISPLAY_USE_HARDWARE_ACCELERATOR=n + +# LVGL-based displayio compositing core. LVGL renders on its own RTOS thread (this +# workqueue, also matching the RTOS deployment model); CircuitPython's compositor mirror +# mutations take the glue's LVGL mutex so they don't interleave with lv_timer_handler. +CONFIG_LVGL=y +CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE=y +# LVGL allocates objects and draw buffers from its own heap sizing. +CONFIG_LV_Z_MEM_POOL_SIZE=16777216 +# Cache decoded images (OnDiskBitmaps and other LVGL images) so they're only decoded +# again when the source or palette changes. +CONFIG_LV_CACHE_DEF_SIZE=4194304 + +# EEPROM emulation for testing +CONFIG_EEPROM=y +CONFIG_EEPROM_AT24=y +CONFIG_EEPROM_AT2X_EMUL=y + +# ADC emulation for analogio testing +CONFIG_ADC=y +CONFIG_ADC_EMUL=y + +# DAC emulation for analogio testing (the vnd,dac node in the board overlay +# turns on CONFIG_DAC_TEST) +CONFIG_DAC=y + +# PWM emulation for pwmio testing (the adafruit,pwm-emul nodes in the board +# overlay turn on CONFIG_PWM_ADAFRUIT_EMUL) +CONFIG_PWM=y + +# I2S SDL emulation for audio testing +CONFIG_I2S_SDL=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_TCP=y +CONFIG_NET_SOCKETS=y +CONFIG_ETH_NATIVE_TAP=n +CONFIG_NET_DRIVERS=y +CONFIG_NET_SOCKETS_OFFLOAD=y +CONFIG_NET_NATIVE_OFFLOADED_SOCKETS=y +CONFIG_HEAP_MEM_POOL_SIZE=1024 + +CONFIG_NET_LOG=y + +CONFIG_MBEDTLS=y +CONFIG_PSA_WANT_ALG_SHA_1=y +CONFIG_PSA_WANT_ALG_SHA_256=y diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.overlay b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.overlay new file mode 100644 index 00000000000..78b57d64cef --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/board.overlay @@ -0,0 +1,7 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Reuse the LVGL native simulator's hardware description for its ASan variant. + */ + +#include "../native_sim_lvgl/board.overlay" diff --git a/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/circuitpython.toml b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/circuitpython.toml new file mode 100644 index 00000000000..913adb512b3 --- /dev/null +++ b/ports/zephyr-cp/boards/native/native_sim_lvgl_asan/circuitpython.toml @@ -0,0 +1,2 @@ +NAME = "Native simulator - LVGL + ASan" +CIRCUITPY_BUILD_EXTENSIONS = ["exe"] diff --git a/ports/zephyr-cp/common-hal/zephyr_display/Display.h b/ports/zephyr-cp/common-hal/zephyr_display/Display.h index bdec1e96ba5..78d2e588b1d 100644 --- a/ports/zephyr-cp/common-hal/zephyr_display/Display.h +++ b/ports/zephyr-cp/common-hal/zephyr_display/Display.h @@ -6,26 +6,10 @@ #pragma once -#include -#include - -#include "py/obj.h" -#include "shared-module/displayio/display_core.h" - -typedef struct { - mp_obj_base_t base; - displayio_display_core_t core; - const struct device *device; - struct display_capabilities capabilities; - enum display_pixel_format pixel_format; - uint64_t last_refresh_call; - uint16_t native_frames_per_second; - uint16_t native_ms_per_frame; - bool auto_refresh; - bool first_manual_refresh; -} zephyr_display_display_obj_t; - -void zephyr_display_display_background(zephyr_display_display_obj_t *self); -void zephyr_display_display_collect_ptrs(zephyr_display_display_obj_t *self); -void zephyr_display_display_reset(zephyr_display_display_obj_t *self); -void release_zephyr_display(zephyr_display_display_obj_t *self); +// The Zephyr display object layout and API are the same for both compositors; only the +// implementation differs. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "common-hal/zephyr_display/lvgl/Display.h" +#else +#include "common-hal/zephyr_display/legacy/Display.h" +#endif diff --git a/ports/zephyr-cp/common-hal/zephyr_display/Display.c b/ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.c similarity index 100% rename from ports/zephyr-cp/common-hal/zephyr_display/Display.c rename to ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.c diff --git a/ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.h b/ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.h new file mode 100644 index 00000000000..bdec1e96ba5 --- /dev/null +++ b/ports/zephyr-cp/common-hal/zephyr_display/legacy/Display.h @@ -0,0 +1,31 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + const struct device *device; + struct display_capabilities capabilities; + enum display_pixel_format pixel_format; + uint64_t last_refresh_call; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + bool auto_refresh; + bool first_manual_refresh; +} zephyr_display_display_obj_t; + +void zephyr_display_display_background(zephyr_display_display_obj_t *self); +void zephyr_display_display_collect_ptrs(zephyr_display_display_obj_t *self); +void zephyr_display_display_reset(zephyr_display_display_obj_t *self); +void release_zephyr_display(zephyr_display_display_obj_t *self); diff --git a/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.c b/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.c new file mode 100644 index 00000000000..3f34c0957a0 --- /dev/null +++ b/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.c @@ -0,0 +1,766 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "bindings/zephyr_display/Display.h" + +#include + +#include "bindings/zephyr_kernel/__init__.h" +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/ColorConverter.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/tick.h" + + +#include +#include +#include + +#include "lvgl/draw/lv_draw_utils.h" + +#include "shared-bindings/displayio/Group.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "shared-bindings/displayio/__init__.h" + +// Display objects live in displayio's static slot registry. Each slot owns its LVGL +// display and buffers until release_displays(); soft reload keeps the primary slot. + +#if defined(CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE) +// The Zephyr LVGL glue creates its display during SYS_INIT from the driver's current +// pixel format. If that format is monochrome, the glue's flush callback expects a +// conversion buffer that it only allocates when LVGL itself is built at 1bpp (which it +// isn't when the panel format is chosen at runtime). Pause rendering until CircuitPython +// has recreated the display with the matching buffers; running the workqueue before that +// flushes through a NULL conversion buffer. +static int zephyr_display_lvgl_pause_rendering(void) { + lvgl_timer_handler_pause(); + return 0; +} +SYS_INIT(zephyr_display_lvgl_pause_rendering, APPLICATION, 0); +#endif + +// LVGL is initialized by the Zephyr glue at boot. +void displayio_lvgl_port_init(void) { +} + +// The LVGL compositor's port hooks: the RTOS renders LVGL on its workqueue thread, and +// CircuitPython-side mirror mutations take the glue's LVGL lock. +void displayio_lvgl_port_lock(void) { + lvgl_lock(); +} + +void displayio_lvgl_port_unlock(void) { + lvgl_unlock(); +} + +void displayio_lvgl_port_install_busdisplay_refresh(lv_display_t *display) { + // The Zephyr glue flushes every display on the LVGL workqueue, including BusDisplay. + LV_UNUSED(display); +} + +void displayio_lvgl_port_frame_boundary(void) { + // No Zephyr framebuffer exposes a frame callback yet. If one does, this must be made ISR + // safe (for example by scheduling the refresh on the LVGL workqueue). + displayio_lvgl_refresh_paced_displays(); +} + +static void zephyr_display_lvgl_flush_argb_alpha0(lv_display_t *display, const lv_area_t *area, + uint8_t *px_map) { + // The legacy compositor's transfer buffer for 32-bit displays carried no alpha + // channel (0x00RRGGBB), which the SDL capture surfaces as its transparency grid. + // Match that: clear the alpha byte of every pixel before handing the buffer to the + // glue's 32-bit flush. + uint32_t size = (uint32_t)(area->x2 - area->x1 + 1) * (uint32_t)(area->y2 - area->y1 + 1); + for (uint32_t i = 0; i < size; i++) { + px_map[i * 4 + 3] = 0x00; + } + lvgl_flush_cb_32bit(display, area, px_map); +} + +static void zephyr_display_lvgl_flush_565x(lv_display_t *display, const lv_area_t *area, + uint8_t *px_map) { + // The driver for RGB_565X panels reads big-endian pixels. Keep LVGL's buffer in + // plain RGB565 and swap on the way out (mirrors the legacy colorspace-level swap). + uint32_t size = (uint32_t)(area->x2 - area->x1 + 1) * (uint32_t)(area->y2 - area->y1 + 1); + uint16_t *px = (uint16_t *)px_map; + for (uint32_t i = 0; i < size; i++) { + px[i] = (uint16_t)((px[i] << 8) | (px[i] >> 8)); + } + lvgl_flush_cb_16bit(display, area, px_map); +} + +// LVGL borrows user_data and render buffers; lv_display_delete() does not free them. +// Keep our allocations together and release them on every delete, including failed setup. +typedef struct { + struct lvgl_disp_data driver; + void *buf; + // Grayscale and mono displays render through LVGL in RGB888 so the transfer can use + // displayio's luminance coefficients (which the legacy compositor uses). + void *flush_buf; + // The Zephyr glue's flush callbacks copy rendered areas straight to the display and + // don't rotate them. When the display is rotated, the rendered partial buffer is + // rotated into this scratch buffer before the glue callback runs. + void *rotation_buf; + uint32_t rotation_buf_size; + lv_display_flush_cb_t base_flush_cb; +} circuitpython_lvgl_display_data_t; + +static void zephyr_display_rotate_i1(const uint8_t *src, uint8_t *dest, + int32_t width, int32_t height, int32_t rotated_height, + uint32_t src_stride, uint32_t dest_stride, lv_display_rotation_t rotation) { + // I1 buffers start with the two-entry palette, and the glue's mono flush strips it + // itself, so keep the header at the front of the rotated buffer. + const uint32_t palette_size = LV_COLOR_INDEXED_PALETTE_SIZE(LV_COLOR_FORMAT_I1) * 4; + memcpy(dest, src, palette_size); + const uint8_t *src_bits = src + palette_size; + uint8_t *dest_bits = dest + palette_size; + memset(dest_bits, 0, (size_t)dest_stride * rotated_height); + + for (int32_t y = 0; y < height; y++) { + for (int32_t x = 0; x < width; x++) { + uint8_t bit = (src_bits[y * src_stride + x / 8] >> (7 - (x % 8))) & 1; + if (bit == 0) { + continue; + } + int32_t dest_x; + int32_t dest_y; + switch (rotation) { + case LV_DISPLAY_ROTATION_90: + dest_x = y; + dest_y = width - 1 - x; + break; + case LV_DISPLAY_ROTATION_270: + dest_x = height - 1 - y; + dest_y = x; + break; + default: // LV_DISPLAY_ROTATION_180 + dest_x = width - 1 - x; + dest_y = height - 1 - y; + break; + } + dest_bits[dest_y * dest_stride + dest_x / 8] |= 1 << (7 - (dest_x % 8)); + } + } +} + +static void zephyr_display_lvgl_rotate_flush(lv_display_t *display, const lv_area_t *area, + uint8_t *px_map) { + circuitpython_lvgl_display_data_t *data = lv_display_get_user_data(display); + if (data->base_flush_cb == NULL) { + return; + } + lv_display_rotation_t rotation = lv_display_get_rotation(display); + if (rotation == LV_DISPLAY_ROTATION_0) { + data->base_flush_cb(display, area, px_map); + return; + } + + lv_color_format_t color_format = lv_display_get_color_format(display); + int32_t width = lv_area_get_width(area); + int32_t height = lv_area_get_height(area); + lv_area_t rotated_area = *area; + lv_display_rotate_area(display, &rotated_area); + int32_t rotated_width = lv_area_get_width(&rotated_area); + int32_t rotated_height = lv_area_get_height(&rotated_area); + uint32_t src_stride = lv_draw_buf_width_to_stride(width, color_format); + uint32_t dest_stride = lv_draw_buf_width_to_stride(rotated_width, color_format); + uint32_t palette_size = LV_COLOR_INDEXED_PALETTE_SIZE(color_format) * 4; + + if ((size_t)dest_stride * rotated_height + palette_size > data->rotation_buf_size) { + // Don't fall back to writing the unrotated buffer: it would land at the wrong + // physical coordinates. Skipping the flush is safer than overflowing the buffer. + lv_display_flush_ready(display); + return; + } + + if (color_format == LV_COLOR_FORMAT_I1) { + zephyr_display_rotate_i1(px_map, data->rotation_buf, width, height, + rotated_height, src_stride, dest_stride, rotation); + } else { + // LVGL's rotate only lists core formats. RGB565_SWAPPED and AL88 use the same + // 16-bit pixel layout for the purpose of moving pixels around. + lv_color_format_t rotate_format = color_format; + if (rotate_format == LV_COLOR_FORMAT_RGB565_SWAPPED || + rotate_format == LV_COLOR_FORMAT_AL88) { + rotate_format = LV_COLOR_FORMAT_RGB565; + } + lv_draw_rotate(px_map, data->rotation_buf, width, height, + src_stride, dest_stride, rotation, rotate_format); + } + + data->base_flush_cb(display, &rotated_area, data->rotation_buf); +} + +static void zephyr_display_lvgl_delete_cb(lv_event_t *event) { + circuitpython_lvgl_display_data_t *data = lv_event_get_user_data(event); + lv_free(data->rotation_buf); + lv_free(data->flush_buf); + lv_free(data->buf); + lv_free(data); +} + +// Grayscale and mono panels need the same luminance calculation as the legacy +// compositor. LVGL's own conversion uses different coefficients, so render those +// displays in RGB888 and convert the flushed pixels with displayio's routine. +static void zephyr_display_lvgl_flush_luma(lv_display_t *display, const lv_area_t *area, + uint8_t *px_map) { + circuitpython_lvgl_display_data_t *data = lv_display_get_user_data(display); + const struct display_capabilities *cap = &data->driver.cap; + uint16_t width = lv_area_get_width(area); + uint16_t height = lv_area_get_height(area); + uint32_t pixel_count = (uint32_t)width * (uint32_t)height; + uint32_t src_stride = lv_draw_buf_width_to_stride(width, LV_COLOR_FORMAT_RGB888); + uint8_t *out = data->flush_buf; + struct display_buffer_descriptor desc = { + .width = width, + .pitch = width, + .height = height, + .frame_incomplete = !lv_display_flush_is_last(display), + }; + + if (cap->current_pixel_format == PIXEL_FORMAT_L_8 || + cap->current_pixel_format == PIXEL_FORMAT_AL_88) { + uint8_t *dest = out; + for (uint16_t y = 0; y < height; y++) { + const uint8_t *src = px_map + (uint32_t)y * src_stride; + for (uint16_t x = 0; x < width; x++) { + // LVGL's RGB888 is stored in memory as B, G, R. + uint8_t luma = displayio_colorconverter_compute_luma( + (uint32_t)src[x * 3 + 2] << 16 | + (uint32_t)src[x * 3 + 1] << 8 | + src[x * 3]); + *dest++ = luma; + if (cap->current_pixel_format == PIXEL_FORMAT_AL_88) { + *dest++ = 0xff; + } + } + } + desc.buf_size = pixel_count * + (cap->current_pixel_format == PIXEL_FORMAT_AL_88 ? 2 : 1); + } else { + // Pack the luminance into the display's row- or column-major 1bpp layout, + // matching the Zephyr glue's mono conversion. + uint32_t out_size = (pixel_count + 7) / 8; + bool vtiled = cap->screen_info & SCREEN_INFO_MONO_VTILED; + bool msb_first = cap->screen_info & SCREEN_INFO_MONO_MSB_FIRST; + #ifdef CONFIG_LV_Z_COLOR_MONO_HW_INVERSION + uint8_t clear_color = 0x00; + #else + uint8_t clear_color = cap->current_pixel_format == PIXEL_FORMAT_MONO01 ? 0x00 : 0xff; + #endif + memset(out, clear_color, out_size); + for (uint16_t y = 0; y < height; y++) { + const uint8_t *src = px_map + (uint32_t)y * src_stride; + for (uint16_t x = 0; x < width; x++) { + uint8_t luma = displayio_colorconverter_compute_luma( + (uint32_t)src[x * 3 + 2] << 16 | + (uint32_t)src[x * 3 + 1] << 8 | + src[x * 3]); + if (luma < 128) { + continue; + } + uint32_t bit; + uint8_t *byte; + if (vtiled) { + bit = msb_first ? 7 - (y % 8) : y % 8; + byte = &out[x + (y / 8) * width]; + } else { + bit = msb_first ? 7 - (x % 8) : x % 8; + byte = &out[x / 8 + y * (width / 8)]; + } + #ifdef CONFIG_LV_Z_COLOR_MONO_HW_INVERSION + *byte |= 1 << bit; + #else + if (cap->current_pixel_format == PIXEL_FORMAT_MONO01) { + *byte |= 1 << bit; + } else { + *byte &= ~(1 << bit); + } + #endif + } + } + desc.buf_size = out_size; + desc.pitch = width; + } + + const bool is_epd = cap->screen_info & SCREEN_INFO_EPD; + const bool is_last = lv_display_flush_is_last(display); + if (is_epd && !data->driver.blanking_on && !is_last) { + display_blanking_on(data->driver.display_dev); + data->driver.blanking_on = true; + } + display_write(data->driver.display_dev, area->x1, area->y1, &desc, out); + if (cap->screen_info & SCREEN_INFO_DOUBLE_BUFFER) { + display_write(data->driver.display_dev, area->x1, area->y1, &desc, out); + } + if (is_epd && is_last && data->driver.blanking_on) { + display_blanking_off(data->driver.display_dev); + data->driver.blanking_on = false; + } + lv_display_flush_ready(display); +} + +// Recreate the default LVGL display for the (possibly newly selected) driver pixel format, +// mirroring the glue's lvgl_init() display setup. The caller holds the LVGL lock. +static int zephyr_display_lvgl_recreate_display(zephyr_display_display_obj_t *self) { + lv_display_t *old = lv_display_get_default(); + if (old != NULL) { + lv_display_delete(old); + } + + lv_display_t *display = lv_display_create(self->capabilities.x_resolution, + self->capabilities.y_resolution); + if (display == NULL) { + return -ENOMEM; + } + + circuitpython_lvgl_display_data_t *data = lv_malloc_zeroed(sizeof(*data)); + if (data == NULL) { + lv_display_delete(display); + return -ENOMEM; + } + lv_display_add_event_cb(display, zephyr_display_lvgl_delete_cb, LV_EVENT_DELETE, data); + data->driver.display_dev = self->device; + display_get_capabilities(self->device, &data->driver.cap); + lv_display_set_user_data(display, &data->driver); + + int result = set_lvgl_rendering_cb(display); + if (result != 0) { + lv_display_delete(display); + return result; + } + + // Override the glue's flush choice where the legacy-compatible transfer differs. + bool legacy_luma = self->pixel_format == PIXEL_FORMAT_L_8 || + self->pixel_format == PIXEL_FORMAT_AL_88 || + self->pixel_format == PIXEL_FORMAT_MONO01 || + self->pixel_format == PIXEL_FORMAT_MONO10; + if (legacy_luma) { + // Render in RGB888, then convert with displayio's luminance coefficients in + // zephyr_display_lvgl_flush_luma(). + lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB888); + lv_display_set_flush_cb(display, zephyr_display_lvgl_flush_luma); + } else if (self->pixel_format == PIXEL_FORMAT_RGB_565X) { + lv_display_set_color_format(display, LV_COLOR_FORMAT_RGB565); + lv_display_set_flush_cb(display, zephyr_display_lvgl_flush_565x); + } else if (self->pixel_format == PIXEL_FORMAT_ABGR_8888 || + self->pixel_format == PIXEL_FORMAT_BGRA_8888) { + // Legacy treated ABGR/BGRA as 32-bit passthrough without alpha as well. + lv_display_set_flush_cb(display, zephyr_display_lvgl_flush_argb_alpha0); + } else if (self->pixel_format == PIXEL_FORMAT_ARGB_8888) { + lv_display_set_flush_cb(display, zephyr_display_lvgl_flush_argb_alpha0); + } + + // Partial-render-mode buffer, sized like the glue does. + uint16_t hor_res = lv_display_get_horizontal_resolution(display); + uint16_t ver_res = lv_display_get_vertical_resolution(display); + uint32_t buf_nbr_pixels = (CONFIG_LV_Z_VDB_SIZE * hor_res * ver_res) / 100; + if (buf_nbr_pixels < hor_res) { + buf_nbr_pixels = hor_res; + } + uint32_t buf_size; + bool al88 = self->pixel_format == PIXEL_FORMAT_AL_88; + switch (self->pixel_format) { + case PIXEL_FORMAT_ARGB_8888: + case PIXEL_FORMAT_ABGR_8888: + buf_size = 4 * buf_nbr_pixels; + break; + case PIXEL_FORMAT_RGB_888: + case PIXEL_FORMAT_BGR_888: + buf_size = 3 * buf_nbr_pixels; + break; + case PIXEL_FORMAT_RGB_565: + case PIXEL_FORMAT_RGB_565X: + buf_size = 2 * buf_nbr_pixels; + break; + case PIXEL_FORMAT_L_8: + buf_size = buf_nbr_pixels; + break; + case PIXEL_FORMAT_MONO01: + case PIXEL_FORMAT_MONO10: + buf_size = buf_nbr_pixels / 8 + 8; + buf_size += (buf_nbr_pixels % 8) == 0 ? 0 : 1; + break; + default: + buf_size = 2 * buf_nbr_pixels; + break; + } + if (legacy_luma) { + // The temporary RGB888 render buffer. + buf_size = 3 * buf_nbr_pixels; + } else if (al88) { + buf_size = 2 * buf_nbr_pixels; + } + + data->buf = lv_malloc(buf_size); + if (data->buf == NULL) { + lv_display_delete(display); + return -ENOMEM; + } + lv_display_set_buffers(display, data->buf, NULL, buf_size, LV_DISPLAY_RENDER_MODE_PARTIAL); + + if (legacy_luma) { + uint32_t flush_buf_size = buf_nbr_pixels; + if (al88) { + flush_buf_size *= 2; + } else if (self->pixel_format == PIXEL_FORMAT_MONO01 || + self->pixel_format == PIXEL_FORMAT_MONO10) { + flush_buf_size = buf_nbr_pixels / 8 + 8; + } + data->flush_buf = lv_malloc(flush_buf_size); + if (data->flush_buf == NULL) { + lv_display_delete(display); + return -ENOMEM; + } + } + + // A rotated flush has the same number of pixels as the rendered area, but may need + // the I1 palette header in front of the pixel data. + data->rotation_buf_size = buf_size + LV_COLOR_INDEXED_PALETTE_SIZE(LV_COLOR_FORMAT_I1) * 4; + data->rotation_buf = lv_malloc(data->rotation_buf_size); + if (data->rotation_buf == NULL) { + lv_display_delete(display); + return -ENOMEM; + } + + data->base_flush_cb = lv_display_get_flush_cb(display); + lv_display_set_flush_cb(display, zephyr_display_lvgl_rotate_flush); + + self->lv_display = display; + return 0; +} + + + +static enum display_pixel_format zephyr_display_select_pixel_format(const struct display_capabilities *caps) { + uint32_t formats = caps->supported_pixel_formats; + + if (formats & PIXEL_FORMAT_RGB_565) { + return PIXEL_FORMAT_RGB_565; + } + if (formats & PIXEL_FORMAT_RGB_888) { + return PIXEL_FORMAT_RGB_888; + } + if (formats & PIXEL_FORMAT_ARGB_8888) { + return PIXEL_FORMAT_ARGB_8888; + } + if (formats & PIXEL_FORMAT_RGB_565X) { + return PIXEL_FORMAT_RGB_565X; + } + if (formats & PIXEL_FORMAT_L_8) { + return PIXEL_FORMAT_L_8; + } + if (formats & PIXEL_FORMAT_AL_88) { + return PIXEL_FORMAT_AL_88; + } + if (formats & PIXEL_FORMAT_MONO01) { + return PIXEL_FORMAT_MONO01; + } + if (formats & PIXEL_FORMAT_MONO10) { + return PIXEL_FORMAT_MONO10; + } + return caps->current_pixel_format; +} + +static void zephyr_display_select_colorspace(zephyr_display_display_obj_t *self, + uint16_t *color_depth, + uint8_t *bytes_per_cell, + bool *grayscale, + bool *pixels_in_byte_share_row, + bool *reverse_pixels_in_byte, + bool *reverse_bytes_in_word) { + *color_depth = 16; + *bytes_per_cell = 2; + *grayscale = false; + *pixels_in_byte_share_row = false; + *reverse_pixels_in_byte = false; + *reverse_bytes_in_word = false; + + if (self->pixel_format == PIXEL_FORMAT_RGB_565X) { + // RGB_565X is big-endian RGB_565, so byte-swap from native LE. + *reverse_bytes_in_word = true; + } else if (self->pixel_format == PIXEL_FORMAT_RGB_888) { + *color_depth = 24; + *bytes_per_cell = 3; + *reverse_bytes_in_word = false; + } else if (self->pixel_format == PIXEL_FORMAT_ARGB_8888) { + *color_depth = 32; + *bytes_per_cell = 4; + *reverse_bytes_in_word = false; + } else if (self->pixel_format == PIXEL_FORMAT_L_8 || + self->pixel_format == PIXEL_FORMAT_AL_88) { + *color_depth = 8; + *bytes_per_cell = 1; + *grayscale = true; + *reverse_bytes_in_word = false; + } else if (self->pixel_format == PIXEL_FORMAT_MONO01 || + self->pixel_format == PIXEL_FORMAT_MONO10) { + bool vtiled = self->capabilities.screen_info & SCREEN_INFO_MONO_VTILED; + bool msb_first = self->capabilities.screen_info & SCREEN_INFO_MONO_MSB_FIRST; + *color_depth = 1; + *bytes_per_cell = 1; + *grayscale = true; + *pixels_in_byte_share_row = !vtiled; + *reverse_pixels_in_byte = msb_first; + *reverse_bytes_in_word = false; + } +} + +void common_hal_zephyr_display_display_construct_from_device(zephyr_display_display_obj_t *self, + const struct device *device, + uint16_t rotation, + bool auto_refresh) { + self->auto_refresh = false; + self->lv_display = NULL; + + if (device == NULL || !device_is_ready(device)) { + raise_zephyr_error(-ENODEV); + } + + self->device = device; + display_get_capabilities(self->device, &self->capabilities); + + // Lock for the whole LVGL setup: the workqueue may already be running timers. + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Pick the pixel format exactly like the legacy compositor does (RGB565 preferred). + self->pixel_format = zephyr_display_select_pixel_format(&self->capabilities); + if (self->pixel_format != self->capabilities.current_pixel_format) { + (void)display_set_pixel_format(self->device, self->pixel_format); + display_get_capabilities(self->device, &self->capabilities); + self->pixel_format = self->capabilities.current_pixel_format; + } + // The LVGL display glue configured itself from the driver's original current format at + // boot. Recreate the LVGL display so its buffers and flush callbacks match the format + // actually selected above. + int result = zephyr_display_lvgl_recreate_display(self); + if (result != 0) { + self->device = NULL; + self->base.type = &mp_type_NoneType; + raise_zephyr_error(result); + } + + uint16_t color_depth; + uint8_t bytes_per_cell; + bool grayscale; + bool pixels_in_byte_share_row; + bool reverse_pixels_in_byte; + bool reverse_bytes_in_word; + zephyr_display_select_colorspace(self, &color_depth, &bytes_per_cell, + &grayscale, &pixels_in_byte_share_row, &reverse_pixels_in_byte, + &reverse_bytes_in_word); + + displayio_display_core_construct( + &self->core, + self->capabilities.x_resolution, + self->capabilities.y_resolution, + 0, + color_depth, + grayscale, + pixels_in_byte_share_row, + bytes_per_cell, + reverse_pixels_in_byte, + reverse_bytes_in_word); + + self->native_frames_per_second = 60; + self->native_ms_per_frame = 1000 / self->native_frames_per_second; + self->first_manual_refresh = !auto_refresh; + + if (rotation != 0) { + common_hal_zephyr_display_display_set_rotation(self, rotation); + } + + (void)display_blanking_off(self->device); + + displayio_lvgl_init(); + displayio_lvgl_register_display(lv_display_get_default()); + + displayio_display_core_set_root_group(&self->core, &circuitpython_splash); + displayio_lvgl_set_root_group(&circuitpython_splash); + displayio_lvgl_unlock_with_cleanup(); + #if defined(CONFIG_LV_Z_RUN_LVGL_ON_WORKQUEUE) + // Buffers and flush callbacks now match the panel; let the workqueue render. + lvgl_timer_handler_resume(); + #endif + common_hal_zephyr_display_display_set_auto_refresh(self, auto_refresh); +} + +uint16_t common_hal_zephyr_display_display_get_width(zephyr_display_display_obj_t *self) { + return displayio_display_core_get_width(&self->core); +} + +uint16_t common_hal_zephyr_display_display_get_height(zephyr_display_display_obj_t *self) { + return displayio_display_core_get_height(&self->core); +} + +mp_float_t common_hal_zephyr_display_display_get_brightness(zephyr_display_display_obj_t *self) { + (void)self; + return -1; +} + +bool common_hal_zephyr_display_display_set_brightness(zephyr_display_display_obj_t *self, mp_float_t brightness) { + (void)self; + (void)brightness; + return false; +} + + +void common_hal_zephyr_display_display_set_rotation(zephyr_display_display_obj_t *self, int rotation) { + bool transposed = (self->core.rotation == 90 || self->core.rotation == 270); + bool will_transposed = (rotation == 90 || rotation == 270); + if (transposed != will_transposed) { + int tmp = self->core.width; + self->core.width = self->core.height; + self->core.height = tmp; + } + + displayio_display_core_set_rotation(&self->core, rotation); + + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + + if (self->lv_display != NULL) { + // LVGL maps the logical (rotated) coordinates to the panel's physical orientation + // while flushing; the port's rotate flush callback moves the pixels into place. + // LVGL rotates the opposite way from displayio for the transposed orientations. + lv_display_rotation_t lv_rotation; + switch (rotation) { + case 90: + lv_rotation = LV_DISPLAY_ROTATION_270; + break; + case 270: + lv_rotation = LV_DISPLAY_ROTATION_90; + break; + case 180: + lv_rotation = LV_DISPLAY_ROTATION_180; + break; + default: + lv_rotation = LV_DISPLAY_ROTATION_0; + break; + } + lv_display_set_rotation(self->lv_display, lv_rotation); + lv_display_set_default(self->lv_display); + } + + if (self == &displays[0].zephyr_display) { + supervisor_stop_terminal(); + supervisor_start_terminal(self->core.width, self->core.height); + } + + if (self->core.current_group != NULL) { + displayio_lvgl_set_root_group(self->core.current_group); + } + lv_obj_t *screen = lv_screen_active(); + if (screen != NULL) { + lv_obj_invalidate(screen); + } + + displayio_lvgl_unlock_with_cleanup(); +} + +uint16_t common_hal_zephyr_display_display_get_rotation(zephyr_display_display_obj_t *self) { + return self->core.rotation; +} + +bool common_hal_zephyr_display_display_refresh(zephyr_display_display_obj_t *self, + uint32_t target_ms_per_frame, + uint32_t maximum_ms_per_real_frame) { + if (!self->auto_refresh && !self->first_manual_refresh && (target_ms_per_frame != NO_FPS_LIMIT)) { + uint64_t current_time = supervisor_ticks_ms64(); + uint32_t current_ms_since_real_refresh = current_time - self->core.last_refresh; + if (current_ms_since_real_refresh > maximum_ms_per_real_frame) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Below minimum frame rate")); + } + uint32_t current_ms_since_last_call = current_time - self->last_refresh_call; + self->last_refresh_call = current_time; + if (current_ms_since_last_call > target_ms_per_frame) { + return false; + } + uint32_t remaining_time = target_ms_per_frame - (current_ms_since_real_refresh % target_ms_per_frame); + while (supervisor_ticks_ms64() - self->last_refresh_call < remaining_time) { + RUN_BACKGROUND_TASKS; + } + } + self->first_manual_refresh = false; + self->core.last_refresh = supervisor_ticks_ms64(); + // Manual refresh must render immediately, not on LVGL's timer cadence. The + // workqueue's own handler already holds the lock, so no re-entrancy here. + lvgl_lock(); + lv_refr_now(lv_display_get_default()); + lvgl_unlock(); + return true; +} + +bool common_hal_zephyr_display_display_get_auto_refresh(zephyr_display_display_obj_t *self) { + return self->auto_refresh; +} + +void common_hal_zephyr_display_display_set_auto_refresh(zephyr_display_display_obj_t *self, bool auto_refresh) { + self->first_manual_refresh = !auto_refresh; + if (auto_refresh != self->auto_refresh) { + if (auto_refresh) { + supervisor_enable_tick(); + } else { + supervisor_disable_tick(); + } + } + self->auto_refresh = auto_refresh; +} + +void zephyr_display_display_background(zephyr_display_display_obj_t *self) { + // LVGL renders on its own RTOS thread (the LVGL workqueue), so the CircuitPython + // background task has nothing to pump. + (void)self; +} + +void release_zephyr_display(zephyr_display_display_obj_t *self) { + common_hal_zephyr_display_display_set_auto_refresh(self, false); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + displayio_lvgl_set_root_group(NULL); + if (self->lv_display != NULL) { + lv_display_delete(self->lv_display); + self->lv_display = NULL; + } + release_display_core(&self->core); + self->device = NULL; + self->base.type = &mp_type_NoneType; + displayio_lvgl_unlock_with_cleanup(); +} + +void zephyr_display_display_collect_ptrs(zephyr_display_display_obj_t *self) { + (void)self; + displayio_display_core_collect_ptrs(&self->core); +} + +void zephyr_display_display_reset(zephyr_display_display_obj_t *self) { + if (self->device != NULL && device_is_ready(self->device)) { + common_hal_zephyr_display_display_set_auto_refresh(self, true); + displayio_display_core_set_root_group(&self->core, &circuitpython_splash); + displayio_lvgl_set_root_group(&circuitpython_splash); + displayio_lvgl_lock(); + lv_refr_now(lv_display_get_default()); + displayio_lvgl_unlock(); + } else { + release_zephyr_display(self); + } +} + +mp_obj_t common_hal_zephyr_display_display_get_root_group(zephyr_display_display_obj_t *self) { + if (self->core.current_group == NULL) { + return mp_const_none; + } + return self->core.current_group; +} + +bool common_hal_zephyr_display_display_set_root_group(zephyr_display_display_obj_t *self, displayio_group_t *root_group) { + bool ok = displayio_display_core_set_root_group(&self->core, root_group); + displayio_lvgl_set_root_group(ok ? root_group : NULL); + return ok; +} diff --git a/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.h b/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.h new file mode 100644 index 00000000000..50efe626da4 --- /dev/null +++ b/ports/zephyr-cp/common-hal/zephyr_display/lvgl/Display.h @@ -0,0 +1,33 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "lvgl.h" +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + lv_display_t *lv_display; + const struct device *device; + struct display_capabilities capabilities; + enum display_pixel_format pixel_format; + uint64_t last_refresh_call; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + bool auto_refresh; + bool first_manual_refresh; +} zephyr_display_display_obj_t; + +void zephyr_display_display_background(zephyr_display_display_obj_t *self); +void zephyr_display_display_collect_ptrs(zephyr_display_display_obj_t *self); +void zephyr_display_display_reset(zephyr_display_display_obj_t *self); +void release_zephyr_display(zephyr_display_display_obj_t *self); diff --git a/ports/zephyr-cp/cptools/build_circuitpython.py b/ports/zephyr-cp/cptools/build_circuitpython.py index 96612d5c2e4..89ef4fa0845 100644 --- a/ports/zephyr-cp/cptools/build_circuitpython.py +++ b/ports/zephyr-cp/cptools/build_circuitpython.py @@ -299,23 +299,24 @@ async def generate_root_pointer_header(build_path): ) -async def generate_display_resources(output_path, translation, font, extra_characters): - await cpbuild.run_command( - [ - "python", - srcdir / "tools" / "gen_display_resources.py", - "--font", - srcdir / font, - "--sample_file", - srcdir / "locale" / f"{translation}.po", - "--extra_characters", - repr(extra_characters), - "--output_c_file", - output_path, - ], - srcdir, - check_hash=[output_path], - ) +async def generate_display_resources( + output_path, translation, font, extra_characters, lvgl_tilegrid +): + cmd = [ + "python", + srcdir / "tools" / "gen_display_resources.py", + "--font", + srcdir / font, + "--sample_file", + srcdir / "locale" / f"{translation}.po", + "--extra_characters", + repr(extra_characters), + "--output_c_file", + output_path, + ] + if lvgl_tilegrid: + cmd.append("--lvgl-tilegrid") + await cpbuild.run_command(cmd, srcdir, check_hash=[output_path]) async def generate_web_workflow_static(output_path, static_files): @@ -438,6 +439,13 @@ async def build_circuitpython(): # noqa: C901 for module in ALWAYS_ON_MODULES: circuitpython_flags.append(f"-DCIRCUITPY_{module.upper()}=1") lto = cmake_args.get("LTO", "n") == "y" + # LVGL module enabled for this board. The flag comes from CONFIG_LVGL via + # ExternalProject args so it stays in sync with the Kconfig choice. Any board that + # sets CONFIG_LVGL gets the LVGL-based displayio compositing core; boards without + # LVGL build the legacy scanline compositor. + lvgl_enabled = cmake_args.get("LVGL", "n") == "y" + lvgl_dir = cmake_args.get("LVGL_DIR", "") + logger.info(f"LVGL compositing core enabled: {lvgl_enabled} (dir={lvgl_dir or 'none'})") circuitpython_flags.append(f"-DCIRCUITPY_ENABLE_MPY_NATIVE={1 if enable_mpy_native else 0}") circuitpython_flags.append(f"-DCIRCUITPY_FULL_BUILD={1 if full_build else 0}") circuitpython_flags.append("-DCIRCUITPY_OPT_LOAD_ATTR_FAST_PATH=1") @@ -448,6 +456,17 @@ async def build_circuitpython(): # noqa: C901 circuitpython_flags.append(f"-DMICROPY_PY_SELECT={1 if full_build else 0}") circuitpython_flags.append(f"-DMICROPY_PY_SELECT_SELECT={1 if full_build else 0}") circuitpython_flags.append("-DCIRCUITPY_STATUS_BAR=1") + if lvgl_enabled: + circuitpython_flags.append("-DCIRCUITPY_DISPLAYIO_LVGL=1") + circuitpython_flags.extend(("-isystem", lvgl_dir)) + circuitpython_flags.extend(("-isystem", lvgl_dir + "/include")) + # The zephyr-provided LV_CONF_PATH definition loses its shell quoting while + # crossing the ExternalProject boundary. Re-supply it quoted so LVGL resolves + # the config header. + lv_conf_path = portdir / "zephyr" / "modules" / "lvgl" / "include" / "lv_conf.h" + circuitpython_flags.append("-ULV_CONF_PATH") + # The command runs through a shell, so escape the quotes for it. + circuitpython_flags.append(f'-DLV_CONF_PATH=\\"{lv_conf_path}\\"') circuitpython_flags.append(f"-DCIRCUITPY_USB_HOST={1 if usb_host else 0}") circuitpython_flags.append(f"-DCIRCUITPY_BOARD_ID='\"{board}\"'") circuitpython_flags.append(f"-DCIRCUITPY_TRANSLATE_OBJECT={1 if lto else 0}") @@ -523,6 +542,23 @@ async def build_circuitpython(): # noqa: C901 for m in mpconfigboard.get("DISABLED_MODULES", []): enabled_modules.discard(m) + if lvgl_enabled: + # These display-side modules render through the scanline compositor and do not + # work with the LVGL one. Exclude them so LVGL builds don't link legacy rendering + # machinery. BusDisplay, FramebufferDisplay, FourWire and I2CDisplayBus have LVGL + # implementations and stay enabled. bitmaptools and jpegio stay enabled because + # bitmaptools now writes through the Bitmap dirty API and jpegio decodes through + # bitmaptools.blit. + legacy_display_modules = [ + "epaperdisplay", + "sharpdisplay", + "tilepalettemapper", + ] + for m in legacy_display_modules: + if m in enabled_modules: + enabled_modules.discard(m) + module_reasons[m] = "Legacy scanline display module; excluded from LVGL builds" + web_workflow_enabled = ( board_info.get("wifi", False) or board_info.get("hostnetwork", False) @@ -702,10 +738,59 @@ async def build_circuitpython(): # noqa: C901 if enabled: hal_source.extend(portdir.glob(f"bindings/{module.name}/*.c")) len_before = len(hal_source) - hal_source.extend(top.glob(f"ports/zephyr-cp/common-hal/{module.name}/*.c")) + if module.name == "zephyr_display": + backend_dir = "lvgl" if lvgl_enabled else "legacy" + hal_source.extend( + top.glob(f"ports/zephyr-cp/common-hal/zephyr_display/{backend_dir}/*.c") + ) + else: + hal_source.extend(top.glob(f"ports/zephyr-cp/common-hal/{module.name}/*.c")) # Only include shared-module/*.c if no common-hal/*.c files were found if len(hal_source) == len_before or module.name in SHARED_MODULE_AND_COMMON_HAL: - hal_source.extend(top.glob(f"shared-module/{module.name}/**/*.c")) + shared_module_sources = top.glob(f"shared-module/{module.name}/**/*.c") + if module.name == "displayio": + # The compositing core (Group/TileGrid) is selected by backend directory. + backend_dir = "lvgl" if lvgl_enabled else "legacy" + backend_root = top / "shared-module" / "displayio" + shared_module_sources = [ + s + for s in shared_module_sources + # The legacy slot registry is shared by the LVGL compositor so that + # displayio.release_displays() can find displays. + if s.parent.parent != backend_root + or s.parent.name == backend_dir + or (s.parent.name == "legacy" and s.name == "__init__.c") + ] + if module.name in ("busdisplay", "framebufferio"): + # These have legacy scanline implementations and LVGL ones. + backend_dir = "lvgl" if lvgl_enabled else "legacy" + backend_root = top / "shared-module" / module.name + shared_module_sources = [ + s + for s in shared_module_sources + if s.parent == backend_root or s.parent.name == backend_dir + ] + if module.name == "vectorio": + # The vector shape wrapper is selected by backend directory too; the + # shape math (Circle/Polygon/Rectangle) is backend agnostic. + backend_dir = "lvgl" if lvgl_enabled else "legacy" + backend_root = top / "shared-module" / "vectorio" + shared_module_sources = [ + s + for s in shared_module_sources + if s.parent.parent != backend_root or s.parent.name == backend_dir + ] + if module.name == "videocore": + # The video source/target state is shared; only the VideoBuffer's + # compositor mirror is backend specific. + backend_dir = "lvgl" if lvgl_enabled else "legacy" + backend_root = top / "shared-module" / "videocore" + shared_module_sources = [ + s + for s in shared_module_sources + if s.parent == backend_root or s.parent.name == backend_dir + ] + hal_source.extend(shared_module_sources) # The USB HID report descriptors are shared with the TinyUSB ports and # have no common-hal override, so always compile them. if module.name == "usb_hid": @@ -857,7 +942,9 @@ async def build_circuitpython(): # noqa: C901 ) extra_characters = mpconfigboard.get("CIRCUITPY_FONT_EXTRA_CHARACTERS", "") tg.create_task( - generate_display_resources(output_path, translation, font_path, extra_characters) + generate_display_resources( + output_path, translation, font_path, extra_characters, lvgl_enabled + ) ) source_files.append(output_path) diff --git a/ports/zephyr-cp/cptools/zephyr2cp.py b/ports/zephyr-cp/cptools/zephyr2cp.py index e00598f63c5..b19a2b88ade 100644 --- a/ports/zephyr-cp/cptools/zephyr2cp.py +++ b/ports/zephyr-cp/cptools/zephyr2cp.py @@ -968,7 +968,9 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig "ethernet": False, "usb_device": False, "_bleio": False, - "hostnetwork": board_id in ["native_sim"], + # The native_sim board and its variant boards (asan, lfs, lvgl) all run + # on the host and can use the host's network stack. + "hostnetwork": board_id.startswith("native_sim"), "audiobusio": False, "neopixel_write": False, } @@ -1963,9 +1965,9 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig } #endif """.strip() - zephyr_display_board_entry = ( - "{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].zephyr_display) }," - ) + zephyr_display_board_entry = """ +{ MP_ROM_QSTR(MP_QSTR_DISPLAY), MP_ROM_PTR(&displays[0].zephyr_display) }, + """.strip() ethernet_init = "" ethernet_static_obj = "" @@ -2029,7 +2031,7 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig max_size = max(max_size, size) # We always start at the end of a Zephyr linker section so we need the externs and &. # Native/simulated boards don't have real memory-mapped RAM, so we allocate static arrays. - if board_id in ["native_sim"] or "bsim" in board_id: + if board_id.startswith("native_sim") or "bsim" in board_id: ram_externs.append("// This is a native board so we provide all of RAM for our heaps.") ram_externs.append(f"static uint32_t _{device}[{size // 4}]; // {path}") start = f"(const uint32_t *) (_{device})" @@ -2104,6 +2106,7 @@ def zephyr_dts_to_cp_board(board_id, portdir, builddir, zephyrbuilddir, mpconfig {iobroker_includes} {zephyr_display_header} + const struct device* const flashes[] = {{ {", ".join(flashes)} }}; const int circuitpy_flash_device_count = {len(flashes)}; diff --git a/ports/zephyr-cp/tests/conftest.py b/ports/zephyr-cp/tests/conftest.py index fb2e67417e0..8b71196226f 100644 --- a/ports/zephyr-cp/tests/conftest.py +++ b/ports/zephyr-cp/tests/conftest.py @@ -100,6 +100,8 @@ def pytest_configure(config): # with mtools. FAT_FILESYSTEM_BOARDS = { "native_native_sim", + "native_native_sim_lvgl", + "native_native_sim_lvgl_asan", "native_native_sim_asan", "native_nrf5340bsim", "native_nrf54lm20bsim", @@ -186,11 +188,13 @@ def log_uart_trace_output(trace_file: Path) -> None: ) -# Native_sim boards each test runs against: the FAT default, the littlefs -# build, and the asan-enabled build, so filesystem and memory errors fail -# tests. +# Native_sim boards each test runs against: the FAT legacy-compositor default, +# the LVGL-compositor build, the littlefs build, and ASan builds of both +# compositors, so compositor, filesystem and memory errors all fail tests. NATIVE_BOARDS = [ "native_native_sim", + "native_native_sim_lvgl", + "native_native_sim_lvgl_asan", "native_native_sim_lfs", "native_native_sim_asan", ] diff --git a/ports/zephyr-cp/tests/test_bitmaptools.py b/ports/zephyr-cp/tests/test_bitmaptools.py new file mode 100644 index 00000000000..6473aead8d9 --- /dev/null +++ b/ports/zephyr-cp/tests/test_bitmaptools.py @@ -0,0 +1,106 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""bitmaptools writes to a Bitmap that a TileGrid is showing. + +bitmaptools writes pixels directly and then reports the changed rectangle through the +Bitmap dirty API. Both compositors have to pick that rectangle up and redraw the part of +the TileGrid it reaches. +""" + +import pytest +from PIL import Image + +_SCENE = """\ +import board +import bitmaptools +import displayio +import time + +display = board.DISPLAY + +palette = displayio.Palette(4) +palette[0] = 0x000000 +palette[1] = 0xFF0000 +palette[2] = 0x00FF00 +palette[3] = 0x0000FF + +bitmap = displayio.Bitmap(40, 40, 4) +bitmap.fill(0) + +group = displayio.Group() +group.append(displayio.TileGrid(bitmap, pixel_shader=palette, x=40, y=40)) + +display.auto_refresh = False +display.root_group = group +display.refresh() +print('first') +time.sleep(10) + +# A filled rectangle, a line and a blit. Each reports its changed area before it +# writes the pixels, as bitmaptools does. +bitmaptools.fill_region(bitmap, 0, 0, 20, 10, 1) +bitmaptools.draw_line(bitmap, 0, 20, 39, 20, 2) +source = displayio.Bitmap(10, 10, 4) +source.fill(3) +bitmaptools.blit(bitmap, source, x=25, y=25) +display.refresh() +print('second') +while True: + time.sleep(1) +""" + +RED = (255, 0, 0) +GREEN = (0, 255, 0) +BLUE = (0, 0, 255) +BLACK = (0, 0, 0) + + +def _expected(width, height, changed): + frame = [[BLACK] * width for _ in range(height)] + if not changed: + return frame + # fill_region(bitmap, 0, 0, 20, 10, 1) + for y in range(40, 50): + for x in range(40, 60): + frame[y][x] = RED + # draw_line(bitmap, 0, 20, 39, 20, 2) + for x in range(40, 80): + frame[60][x] = GREEN + # blit(bitmap, source, x=25, y=25) + for y in range(65, 75): + for x in range(65, 75): + frame[y][x] = BLUE + return frame + + +def _compare(path, changed): + with Image.open(path) as img: + rgb = img.convert("RGB") + expected = _expected(rgb.width, rgb.height, changed) + mismatches = [] + for y in range(rgb.height): + for x in range(rgb.width): + actual = rgb.getpixel((x, y)) + if actual != expected[y][x]: + mismatches.append((x, y, expected[y][x], actual)) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}" + + +@pytest.mark.circuitpy_drive({"code.py": _SCENE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_bitmaptools_render(board, circuitpython): + """bitmaptools changes on a displayed Bitmap reach both compositors.""" + 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") + + _compare(captures[0], changed=False) + _compare(captures[1], changed=True) diff --git a/ports/zephyr-cp/tests/test_displayio_background.py b/ports/zephyr-cp/tests/test_displayio_background.py new file mode 100644 index 00000000000..f1563e619a1 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_background.py @@ -0,0 +1,89 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""The undrawn display background is black, including after a display is re-created. + +LVGL fills its bottom layer with white wherever nothing else covers an area, so +the LVGL compositor makes each display's screen opaque black. That setup used +to run once, for whichever screen existed at the time. Display classes +(BusDisplay, FramebufferDisplay) delete and re-create the LVGL display when +they are constructed, so the screen of the next one came up white. + +This draws a red square on a black screen, releases the display, creates a new +one and draws the same scene again, then checks a captured frame from each +display. A compositor that doesn't style the re-created screen fails on the +second capture. +""" + +import pytest +from PIL import Image + + +BACKGROUND_CODE = """\ +import board +import displayio +import time +import zephyr_display + + +def draw(display): + # A 16-bit bitmap needs no palette, so the square shows the same color before + # and after the display is re-created. + bitmap = displayio.Bitmap(8, 8, 65536) + for y in range(8): + for x in range(8): + bitmap[x, y] = 0xF800 + group = displayio.Group() + group.append(displayio.TileGrid(bitmap, + pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565), + x=16, y=16)) + display.auto_refresh = False + display.root_group = group + display.refresh() + + +display = board.DISPLAY +draw(display) +print('first') +time.sleep(10) + +displayio.release_displays() +display = zephyr_display.Display(auto_refresh=False) +draw(display) +print('second') + +while True: + time.sleep(1) +""" + + +def _assert_black_background(path, index): + with Image.open(path) as img: + rgb = img.convert("RGB") + background = rgb.getpixel((0, 0)) + assert background == (0, 0, 0), ( + f"capture {index}: undrawn background is {background}, expected black" + ) + square = rgb.getpixel((16, 16)) + assert square == (255, 0, 0), ( + f"capture {index}: the TileGrid didn't draw at (16, 16): got {square}" + ) + + +@pytest.mark.circuitpy_drive({"code.py": BACKGROUND_CODE}) +@pytest.mark.display(capture_times_ns=[6_000_000_000, 16_000_000_000]) +@pytest.mark.duration(20) +def test_background_is_black_after_display_recreate(circuitpython): + """A re-created display still has a black background.""" + 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") + + _assert_black_background(captures[0], 0) + _assert_black_background(captures[1], 1) diff --git a/ports/zephyr-cp/tests/test_displayio_lvgl_lifecycle.py b/ports/zephyr-cp/tests/test_displayio_lvgl_lifecycle.py new file mode 100644 index 00000000000..e89c55f2d80 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_lvgl_lifecycle.py @@ -0,0 +1,355 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# +# SPDX-License-Identifier: MIT + +"""LVGL allocation lifetimes, including recovery from allocation/validation failures.""" + +import pytest +from PIL import Image + + +@pytest.fixture(params=["native_native_sim_lvgl", "native_native_sim_lvgl_asan"]) +def board(request): + return request.param + + +def _pixel(path, xy): + assert path.exists(), f"display capture missing: {path}" + with Image.open(path) as image: + return image.convert("RGB").getpixel(xy) + + +POLYGON_OOM_CODE = """\ +import board +import displayio +import time +import vectorio + +display = board.DISPLAY +display.auto_refresh = False +palette = displayio.Palette(1) +palette[0] = 0xffffff +polygon = vectorio.Polygon(pixel_shader=palette, + points=[(0, 0), (4, 0), (0, 4)], x=20, y=20) +group = displayio.Group() +group.append(polygon) +display.root_group = group +display.refresh() +# A 6001x6001 A8 mask cannot fit in the native LVGL board's 16 MiB heap. +# The old mask must no longer be the image source if its replacement fails. +polygon.points = [(0, 0), (6000, 0), (0, 6000)] +display.refresh() +print('oom refresh survived') +time.sleep(10) +polygon.points = [(0, 0), (4, 0), (0, 4)] +display.refresh() +print('mask recovered') +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": POLYGON_OOM_CODE}) +@pytest.mark.display(capture_times_ns=[6_000_000_000, 16_000_000_000]) +@pytest.mark.duration(20) +def test_polygon_mask_allocation_failure(circuitpython): + circuitpython.wait_until_done() + assert "oom refresh survived" in circuitpython.serial.all_output + assert "mask recovered" in circuitpython.serial.all_output + before, after = circuitpython.display_capture_paths() + assert _pixel(before, (21, 21)) == (0, 0, 0) + assert _pixel(after, (21, 21)) == (255, 255, 255) + + +BITMAP_DEINIT_CODE = """\ +import board +import displayio +import time + +display = board.DISPLAY +display.auto_refresh = False +group = displayio.Group() +bitmaps = [] +# Exercise direct sources, converted/cached sources and composed indexed sources. +for i, colorspace in enumerate((displayio.Colorspace.RGB565, + displayio.Colorspace.RGB565_SWAPPED, None)): + bitmap = displayio.Bitmap(64, 64, 2 if colorspace is None else 65536) + if colorspace is None: + shader = displayio.Palette(2) + shader[1] = 0xff0000 + bitmap.fill(1) + else: + shader = displayio.ColorConverter(input_colorspace=colorspace) + bitmap.fill(0xf800 if i == 0 else 0x00f8) + bitmaps.append(bitmap) + group.append(displayio.TileGrid(bitmap, pixel_shader=shader, x=i * 80)) +display.root_group = group +display.refresh() +time.sleep(10) +for bitmap in bitmaps: + bitmap.deinit() + bitmap.deinit() # teardown is idempotent +# Reuse freed GC storage: a stale direct source would now show someone else's pixels. +reused = [displayio.Bitmap(64, 64, 65536) for _ in range(4)] +for bitmap in reused: + bitmap.fill(0x07e0) +display.refresh() +print('deinit refreshed') +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": BITMAP_DEINIT_CODE}) +@pytest.mark.display(capture_times_ns=[6_000_000_000, 16_000_000_000]) +@pytest.mark.duration(20) +def test_deinitialized_bitmap_drops_borrowed_pixels(circuitpython): + circuitpython.wait_until_done() + assert "deinit refreshed" in circuitpython.serial.all_output + before, after = circuitpython.display_capture_paths() + for x in (1, 81, 161): + assert _pixel(before, (x, 1)) == (255, 0, 0) + assert _pixel(after, (x, 1)) == (0, 0, 0) + + +GROUP_ERROR_CODE = """\ +import board +import displayio +import time + +display = board.DISPLAY +palette = displayio.Palette(1) +palette[0] = 0xff0000 +bitmap = displayio.Bitmap(32, 32, 1) +grid = displayio.TileGrid(bitmap, pixel_shader=palette) +group = displayio.Group() +group.append(grid) +display.root_group = group +display.refresh() +time.sleep(10) +other = displayio.Group() +member = displayio.Group() +other.append(member) +for invalid in (object(), member): + try: + group[0] = invalid + raise AssertionError('assignment should have failed') + except ValueError: + pass + assert group[0] is grid +# Assigning the existing member to itself is a no-op, not a failed adoption. +group[0] = grid +palette[0] = 0x00ff00 +# Do NOT manually refresh: the workqueue must be able to acquire the LVGL mutex. +print('rejected replacements') +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": GROUP_ERROR_CODE}) +@pytest.mark.display(capture_times_ns=[6_000_000_000, 16_000_000_000]) +@pytest.mark.duration(20) +def test_failed_group_assignment_preserves_mirror_and_unlocks(circuitpython): + circuitpython.wait_until_done() + assert "rejected replacements" in circuitpython.serial.all_output + before, after = circuitpython.display_capture_paths() + assert _pixel(before, (1, 1)) == (255, 0, 0) + assert _pixel(after, (1, 1)) == (0, 255, 0) + + +DISPLAY_RECREATE_CODE = """\ +import board +import displayio +import gc +import zephyr_display + +display = board.DISPLAY +for i in range(1200): + displayio.release_displays() + display = zephyr_display.Display(auto_refresh=False) + if i % 50 == 0: + gc.collect() +# Leaking one 320x24 RGB565 rendering buffer per recreation exhausts 16 MiB. +group = displayio.Group() +bitmap = displayio.Bitmap(4, 4, 65536) +bitmap.fill(0xf800) +group.append(displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter( + input_colorspace=displayio.Colorspace.RGB565))) +display.root_group = group +display.refresh() +print('recreated 1200 displays') +""" + + +@pytest.mark.circuitpy_drive({"code.py": DISPLAY_RECREATE_CODE}) +@pytest.mark.duration(60) +def test_display_recreation_releases_port_allocations(circuitpython): + circuitpython.wait_until_done() + assert "recreated 1200 displays" in circuitpython.serial.all_output + + +TILEGRID_CHURN_CODE = """\ +import board +import displayio +import gc + +display = board.DISPLAY +display.auto_refresh = False +group = displayio.Group() +display.root_group = group +bitmap = displayio.Bitmap(2, 1, 2) +bitmap[1, 0] = 1 +palette = displayio.Palette(2) +palette[1] = 0xff0000 +for i in range(550): + grid = displayio.TileGrid(bitmap, pixel_shader=palette, width=512, height=512, + tile_width=1, tile_height=1) + group.append(grid) + display.refresh() + group.pop() + del grid + gc.collect() +# Each discarded grid used a 32 KiB port-heap dirty-cell bitset. If those leaked, +# the next, larger composed grid cannot allocate its buffer. Same-sized grids +# can still draw with no bitset when that allocation fails, so require more buffer +# space than one iteration did and check pixels, not just that the loop finished. +grid = displayio.TileGrid(bitmap, pixel_shader=palette, width=512, height=1024, + tile_width=1, tile_height=1) +grid[0] = 1 +group.append(grid) +display.refresh() +print('tilegrid churn survived') +import time +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": TILEGRID_CHURN_CODE}) +@pytest.mark.display(capture_times_ns=[80_000_000_000]) +@pytest.mark.duration(90) +def test_discarded_tilegrids_release_dirty_cell_bits(circuitpython): + circuitpython.wait_until_done() + assert "tilegrid churn survived" in circuitpython.serial.all_output + (capture,) = circuitpython.display_capture_paths() + assert _pixel(capture, (0, 0)) == (255, 0, 0) + + +DIRECT_UPDATE_CODE = """\ +import board +import displayio +import time + +display = board.DISPLAY +display.auto_refresh = False +bitmap = displayio.Bitmap(16, 16, 65536) +bitmap.fill(0x00f8) # byte-swapped RGB565 red +shader = displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED) +group = displayio.Group() +group.append(displayio.TileGrid(bitmap, pixel_shader=shader)) +display.root_group = group +display.refresh() +time.sleep(10) +bitmap.fill(0xe007) # byte-swapped RGB565 green +display.refresh() +print('direct source updated') +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": DIRECT_UPDATE_CODE}) +@pytest.mark.display(capture_times_ns=[6_000_000_000, 16_000_000_000]) +@pytest.mark.duration(20) +def test_direct_source_updates_drop_cached_conversion(circuitpython): + circuitpython.wait_until_done() + assert "direct source updated" in circuitpython.serial.all_output + before, after = circuitpython.display_capture_paths() + assert _pixel(before, (1, 1)) == (255, 0, 0) + assert _pixel(after, (1, 1)) == (0, 255, 0) + + +BUSDISPLAY_CLEANUP_CODE = """\ +import board +import busdisplay +import digitalio +import displayio +import fourwire +from microcontroller import pin + +displayio.release_displays() +bus = fourwire.FourWire(board.SPI(), command=pin.P_10, chip_select=pin.P_11) +# An unsupported format must not leave the static display slot occupied. +try: + busdisplay.BusDisplay(bus, b'', width=32, height=32, color_depth=1) + raise AssertionError('packed format should have been rejected') +except NotImplementedError: + pass +# Buffers for this display exceed the native simulator's 1 MiB port heap. +# This fails after acquiring the backlight and creating an LVGL display. +try: + busdisplay.BusDisplay(bus, b'', width=60000, height=32, color_depth=32, + backlight_pin=pin.P_12, auto_refresh=False) + raise AssertionError('oversized buffers should have been rejected') +except MemoryError: + pass +backlight = digitalio.DigitalInOut(pin.P_12) +backlight.deinit() +# Both the slot and the bus must still be usable after failed construction. +display = busdisplay.BusDisplay(bus, b'', width=32, height=32, + backlight_pin=pin.P_12, auto_refresh=False) +displayio.release_displays() +backlight = digitalio.DigitalInOut(pin.P_12) +backlight.deinit() +print('busdisplay cleanup survived') +""" + + +@pytest.mark.circuitpy_drive({"code.py": BUSDISPLAY_CLEANUP_CODE}) +@pytest.mark.duration(20) +def test_busdisplay_failed_construct_and_release_free_resources(circuitpython): + circuitpython.wait_until_done() + assert "busdisplay cleanup survived" in circuitpython.serial.all_output + + +BUSDISPLAY_ROTATION_CODE = """\ +import board +import busdisplay +import displayio +import fourwire +from microcontroller import pin + +displayio.release_displays() +bus = fourwire.FourWire(board.SPI(), command=pin.P_10, chip_select=pin.P_11) +display = busdisplay.BusDisplay(bus, b'', width=64, height=32, rotation=90, + auto_refresh=False) +print('start', display.width, display.height, display.rotation) + +bitmap = displayio.Bitmap(16, 16, 1) +bitmap.fill(1) +palette = displayio.Palette(1) +palette[0] = 0xff0000 +group = displayio.Group() +group.append(displayio.TileGrid(bitmap, pixel_shader=palette)) +display.root_group = group +display.refresh() + +for rotation in (180, 270, 0): + display.rotation = rotation + display.refresh() + print('rotated', display.width, display.height, display.rotation) +print('busdisplay rotation survived') +""" + + +@pytest.mark.circuitpy_drive({"code.py": BUSDISPLAY_ROTATION_CODE}) +@pytest.mark.duration(20) +def test_busdisplay_rotation_dimensions_and_flush(circuitpython): + circuitpython.wait_until_done() + output = circuitpython.serial.all_output + assert "start 32 64 90" in output + assert "rotated 64 32 180" in output + assert "rotated 32 64 270" in output + assert "rotated 64 32 0" in output + assert "busdisplay rotation survived" in output diff --git a/ports/zephyr-cp/tests/test_displayio_ondiskbitmap.py b/ports/zephyr-cp/tests/test_displayio_ondiskbitmap.py new file mode 100644 index 00000000000..a87e598efd3 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_ondiskbitmap.py @@ -0,0 +1,228 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""OnDiskBitmap rendering on both displayio compositors. + +The BMPs are generated here and copied onto the simulated drive. The expected +frame is computed from the same pixel descriptions, so the legacy scanline +compositor and the LVGL compositor's OnDiskBitmap image decoder are checked +against an independent model. Only fully saturated colors are used so the +result doesn't depend on the display's pixel format. +""" + +import struct + +import pytest +from PIL import Image + +_DISPLAY_HELPER = """\ +import board +display = board.DISPLAY +""" + +_CODE = ( + _DISPLAY_HELPER + + """\ +import displayio +import time + +group = displayio.Group() + +# 8 bit indexed, bottom-up rows. +quad = displayio.OnDiskBitmap('/quad.bmp') +group.append(displayio.TileGrid(quad, pixel_shader=quad.pixel_shader, x=10, y=10)) + +# A sprite sheet with two 4x4 tiles. +sheet = displayio.OnDiskBitmap('/sheet.bmp') +sheet_grid = displayio.TileGrid(sheet, pixel_shader=sheet.pixel_shader, + width=2, height=1, tile_width=4, tile_height=4, x=10, y=40) +sheet_grid[1] = 1 +group.append(sheet_grid) + +# 16 bit 565 with explicit bitfields. +rgb565 = displayio.OnDiskBitmap('/rgb565.bmp') +group.append(displayio.TileGrid(rgb565, pixel_shader=rgb565.pixel_shader, x=40, y=10)) + +# 16 bit 555 (the BI_RGB default). +rgb555 = displayio.OnDiskBitmap('/rgb555.bmp') +group.append(displayio.TileGrid(rgb555, pixel_shader=rgb555.pixel_shader, x=70, y=10)) + +# 24 bit BGR. +rgb888 = displayio.OnDiskBitmap('/rgb888.bmp') +group.append(displayio.TileGrid(rgb888, pixel_shader=rgb888.pixel_shader, x=100, y=10)) + +# 8 bit indexed, top-down rows (negative height). +topdown = displayio.OnDiskBitmap('/topdown.bmp') +group.append(displayio.TileGrid(topdown, pixel_shader=topdown.pixel_shader, x=130, y=10)) + +display.auto_refresh = False +display.root_group = group +display.refresh() +print('first') +time.sleep(10) + +# Palette changes have to reach the image decoder. +quad.pixel_shader[0] = 0xFF00FF +display.refresh() +print('second') +while True: + time.sleep(1) +""" +) + +RED = (255, 0, 0) +GREEN = (0, 255, 0) +BLUE = (0, 0, 255) +WHITE = (255, 255, 255) +MAGENTA = (255, 0, 255) +BLACK = (0, 0, 0) + + +def _bmp(width, height, bpp, rows, palette=None, top_down=False, bitfields=None): + """Build a BMP. rows are top to bottom; pixels are palette indices, 16 bit + values, or (r, g, b) tuples for 24/32 bit.""" + row_bytes = ((width * bpp + 31) // 32) * 4 + pixels = bytearray() + for row in rows if top_down else reversed(rows): + out = bytearray(row_bytes) + if bpp == 8: + out[:width] = bytes(row) + elif bpp == 16: + for x, value in enumerate(row): + out[x * 2 : x * 2 + 2] = struct.pack("= 2 else 0) + (1 if x >= 2 else 0)] + + draw(10, 10, 4, 4, quad) + draw(10, 40, 8, 4, lambda x, y: RED if x < 4 else GREEN) + + draw(40, 10, 4, 4, lambda x, y: RED if x < 2 else GREEN) + draw(70, 10, 4, 4, lambda x, y: RED if x < 2 else BLUE) + draw(100, 10, 4, 4, lambda x, y: BLUE if x < 2 else WHITE) + + def unchanged_quad(x, y): + return _QUAD_PALETTE[(2 if y >= 2 else 0) + (1 if x >= 2 else 0)] + + draw(130, 10, 4, 4, unchanged_quad) + + return frame + + +def _compare(path, changed): + with Image.open(path) as img: + rgb = img.convert("RGB") + expected = _expected(rgb.width, rgb.height, changed) + mismatches = [] + for y in range(rgb.height): + for x in range(rgb.width): + actual = rgb.getpixel((x, y)) + if actual != expected[y][x]: + mismatches.append((x, y, expected[y][x], actual)) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}" + + +_CODE_FILE = {"code.py": _CODE, **_FILES} + + +@pytest.mark.parametrize( + "board", ["native_native_sim_lvgl", "native_native_sim_lvgl_asan"], indirect=True +) +@pytest.mark.circuitpy_drive(_CODE_FILE) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_ondiskbitmap_render(circuitpython): + """Indexed, 16 bit and 24 bit BMPs render, including top-down rows and tiles.""" + 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") + + _compare(captures[0], changed=False) + _compare(captures[1], changed=True) diff --git a/ports/zephyr-cp/tests/test_displayio_primary.py b/ports/zephyr-cp/tests/test_displayio_primary.py new file mode 100644 index 00000000000..dcd24854427 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_primary.py @@ -0,0 +1,46 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""A display created when no primary display is set becomes the primary display. + +The supervisor keeps only the primary display across the end of a VM. If a +display created by user code (a USB video framebuffer, for example) didn't +default to primary, it was released when code.py finished and the terminal and +REPL had nowhere to draw. +""" + +import pytest + + +PRIMARY_CODE = """\ +import board +import displayio +import supervisor +import time +import zephyr_display + +print('board primary:', supervisor.runtime.display is board.DISPLAY) + +displayio.release_displays() +print('released:', supervisor.runtime.display is None) + +display = zephyr_display.Display() +print('new primary:', supervisor.runtime.display is display) +print('done') + +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": PRIMARY_CODE}) +@pytest.mark.duration(8) +def test_display_defaults_to_primary(circuitpython): + """Releasing and re-creating the display leaves the new one primary.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "board primary: True" in output + assert "released: True" in output + assert "new primary: True" in output + assert "done" in output diff --git a/ports/zephyr-cp/tests/test_displayio_tilegrid.py b/ports/zephyr-cp/tests/test_displayio_tilegrid.py new file mode 100644 index 00000000000..b55233ba266 --- /dev/null +++ b/ports/zephyr-cp/tests/test_displayio_tilegrid.py @@ -0,0 +1,631 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""displayio TileGrid rendering on both displayio compositors. + +The scene exercises indexed bitmaps with palettes, which the LVGL compositor +hands to LVGL as indexed images whose palette is kept separately and shared +between the TileGrids that use it, and the cases that go through its custom +decoder (scaled groups and OnDiskBitmaps). The expected frame is computed here +from the same description, so both compositors are checked against an +independent model instead of a golden image. Only fully saturated colors are used so the result doesn't depend on the +display's pixel format. +""" + +import re + +import pytest +from PIL import Image + +_DISPLAY_HELPER = """\ +import board +display = board.DISPLAY +""" + +_SCENE_TEMPLATE = ( + _DISPLAY_HELPER + + """\ +import displayio +import time + +palette = displayio.Palette(4) +palette[0] = 0xFF0000 +palette[1] = 0x00FF00 +palette[2] = 0x0000FF +palette[3] = 0xFFFFFF + +group = displayio.Group() + +# Four vertical color bands in a 2 bit bitmap. +bands = displayio.Bitmap(40, 20, 4) +for y in range(bands.height): + for x in range(bands.width): + bands[x, y] = (x * 4) // bands.width +group.append(displayio.TileGrid(bands, pixel_shader=palette, x=10, y=10)) + +# A sprite sheet with two 8x8 tiles: tile 0 green, tile 1 blue with a white dot. +sheet = displayio.Bitmap(16, 8, 4) +for y in range(8): + for x in range(16): + sheet[x, y] = 1 if x < 8 else 2 +sheet[9, 1] = 3 +sheet_grid = displayio.TileGrid(sheet, pixel_shader=palette, width=3, height=1, + tile_width=8, tile_height=8, x=100, y=10) +sheet_grid[0] = 1 +sheet_grid[1] = 0 +sheet_grid[2] = 1 +group.append(sheet_grid) + +# Transparency: a white square partially covered by a sprite whose index 0 is +# transparent. +white = displayio.Bitmap(30, 30, 2) +white_palette = displayio.Palette(2) +white_palette[0] = 0xFFFFFF +white_palette[1] = 0xFFFFFF +group.append(displayio.TileGrid(white, pixel_shader=white_palette, x=200, y=10)) +overlay_palette = displayio.Palette(2) +overlay_palette[0] = 0x000000 +overlay_palette[1] = 0xFF0000 +overlay_palette.make_transparent(0) +overlay = displayio.Bitmap(10, 10, 2) +for y in range(10): + for x in range(5, 10): + overlay[x, y] = 1 +group.append(displayio.TileGrid(overlay, pixel_shader=overlay_palette, x=210, y=20)) + +# An 8 bit bitmap with a large palette. +big_palette = displayio.Palette(256) +for i in range(256): + big_palette[i] = 0x000000 +big_palette[200] = 0x00FFFF +big_palette[201] = 0xFFFF00 +eight = displayio.Bitmap(20, 10, 256) +for y in range(10): + for x in range(20): + eight[x, y] = 200 if x < 10 else 201 +group.append(displayio.TileGrid(eight, pixel_shader=big_palette, x=10, y=100)) + +{scaled}# One bitmap shown with two different palettes. +other_palette = displayio.Palette(4) +other_palette[0] = 0xFF00FF +other_palette[1] = 0x00FFFF +other_palette[2] = 0xFFFF00 +other_palette[3] = 0x000000 +group.append(displayio.TileGrid(bands, pixel_shader=other_palette, x=10, y=150)) +# A second TileGrid with the same alternate palette shares its LVGL palette. +group.append(displayio.TileGrid(bands, pixel_shader=other_palette, x=10, y=180)) + +# A 16 bit bitmap with a pass-through ColorConverter. +rgb565 = displayio.Bitmap(10, 10, 65536) +for y in range(10): + for x in range(10): + rgb565[x, y] = 0xF800 if x < 5 else 0x07E0 +group.append(displayio.TileGrid(rgb565, + pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565), + x=250, y=100)) + +# Nested groups with a TileGrid and a vectorio shape; the inner group moves later. +import vectorio +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(x=10, y=0) +inner = displayio.Group(x=60, y=200) +inner.append(displayio.TileGrid(checker, pixel_shader=palette, x=5, y=5)) +inner.append(vectorio.Rectangle(pixel_shader=palette, width=6, height=4, x=20, y=0, + color_index=2)) +outer.append(inner) +group.append(outer) + +# A sprite that is moved many times before the frame is captured. +mover = displayio.TileGrid(bands, pixel_shader=palette, x=0, y=0) +group.append(mover) + +display.auto_refresh = False +display.root_group = group +display.refresh() +for i in range(200): + mover.x = 150 + (i % 20) + mover.y = 150 + (i % 10) + display.refresh() +mover.x = 160 +mover.y = 200 +display.refresh() +print('first') +time.sleep(10) + +# Content changes after the first frame. +palette[3] = 0xFF00FF +bands[0, 0] = 3 +sheet_grid[1] = 1 +# The two alternate-palette TileGrids share one LVGL palette, so the Palette change +# reaches both, and the pixel change reaches every grid drawing the bitmap. +other_palette[3] = 0x00FF00 +bands[39, 19] = 2 +# A direct-color bitmap has no palette, only its own pixels. +rgb565[0, 0] = 0x001F +inner.x = 80 +display.refresh() +print('second') +while True: + time.sleep(1) +""" +) + +_SCALED_GROUP = """\ +# A scaled group. +small = displayio.Bitmap(4, 4, 4) +for y in range(4): + for x in range(4): + small[x, y] = 1 if (x + y) % 2 else 2 +scaled = displayio.Group(scale=2, x=100, y=100) +scaled.append(displayio.TileGrid(small, pixel_shader=palette, x=2, y=1)) +group.append(scaled) + +""" + +SCENE_CODE = _SCENE_TEMPLATE.replace("{scaled}", "") +SCALED_SCENE_CODE = _SCENE_TEMPLATE.replace("{scaled}", _SCALED_GROUP) + +RED = (255, 0, 0) +GREEN = (0, 255, 0) +BLUE = (0, 0, 255) +WHITE = (255, 255, 255) +BLACK = (0, 0, 0) +MAGENTA = (255, 0, 255) +CYAN = (0, 255, 255) +YELLOW = (255, 255, 0) + + +def _expected(width, height, changed, scaled): + frame = [[BLACK] * width for _ in range(height)] + + def draw(x0, y0, w, h, color_at, scale=1): + for y in range(h * scale): + for x in range(w * scale): + color = color_at(x // scale, y // scale) + if color is not None: + frame[y0 + y][x0 + x] = color + + palette = [RED, GREEN, BLUE, MAGENTA if changed else WHITE] + other = [MAGENTA, CYAN, YELLOW, GREEN if changed else BLACK] + + def bands(colors): + def color_at(x, y): + if changed and (x, y) == (0, 0): + return colors[3] + if changed and (x, y) == (39, 19): + return colors[2] + return colors[(x * 4) // 40] + + return color_at + + draw(10, 10, 40, 20, bands(palette)) + + tiles = [1, 1 if changed else 0, 1] + + def sheet(x, y): + tile = tiles[x // 8] + sx = (x % 8) + tile * 8 + if (sx, y) == (9, 1): + return palette[3] + return palette[1] if sx < 8 else palette[2] + + draw(100, 10, 24, 8, sheet) + + draw(200, 10, 30, 30, lambda x, y: WHITE) + draw(210, 20, 10, 10, lambda x, y: RED if x >= 5 else None) + + draw(10, 100, 20, 10, lambda x, y: CYAN if x < 10 else YELLOW) + + if scaled: + draw( + 104, + 102, + 4, + 4, + lambda x, y: palette[1] if (x + y) % 2 else palette[2], + scale=2, + ) + + draw( + 250, + 100, + 10, + 10, + lambda x, y: BLUE if changed and (x, y) == (0, 0) else RED if x < 5 else GREEN, + ) + + inner_x = 10 + (80 if changed else 60) + draw(inner_x + 5, 205, 4, 4, lambda x, y: palette[1] if (x + y) % 2 else palette[2]) + draw(inner_x + 20, 200, 6, 4, lambda x, y: palette[2]) + + draw(10, 150, 40, 20, bands(other)) + draw(10, 180, 40, 20, bands(other)) + + draw(160, 200, 40, 20, bands(palette)) + return frame + + +def _compare(path, changed, scaled=False): + with Image.open(path) as img: + rgb = img.convert("RGB") + expected = _expected(rgb.width, rgb.height, changed, scaled) + mismatches = [] + for y in range(rgb.height): + for x in range(rgb.width): + actual = rgb.getpixel((x, y)) + if actual != expected[y][x]: + mismatches.append((x, y, expected[y][x], actual)) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}" + + +@pytest.mark.circuitpy_drive({"code.py": SCENE_CODE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_tilegrid_render(circuitpython): + """Indexed bitmaps, sprite sheets, transparency and updates render exactly.""" + 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") + + _compare(captures[0], changed=False) + _compare(captures[1], changed=True) + + +@pytest.mark.circuitpy_drive({"code.py": SCALED_SCENE_CODE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_tilegrid_render_scaled_group(request, board, circuitpython): + """A TileGrid in a scaled group is scaled by whole pixels from its corner.""" + if board.startswith("native_native_sim_lvgl"): + # LVGL scales images about pixel centers ((w - 1) * scale wide) and resamples, + # so it doesn't reproduce displayio's whole pixel scaling yet. + request.applymarker(pytest.mark.xfail(reason="LVGL image scaling isn't pixel exact")) + circuitpython.wait_until_done() + + captures = circuitpython.display_capture_paths() + if len(captures) < 2 or not captures[1].exists(): + pytest.skip("display capture was not produced") + + _compare(captures[0], changed=False, scaled=True) + _compare(captures[1], changed=True, scaled=True) + + +# Nonconsecutive atlas tiles are essential here: overflow from tile N looks correct +# when the next displayed tile happens to be N + 1 (as in an alphabet test). +CROPPED_ATLAS_CODE = """\ +import board +import displayio +import time + +display = board.DISPLAY +palette = displayio.Palette(4) +for i, color in enumerate((0xFF0000, 0x00FF00, 0x0000FF, 0xFFFFFF)): + palette[i] = color +sheet = displayio.Bitmap(12, 10, 4) +for y in range(10): + for x in range(12): + sheet[x, y] = (x // 3 + 2 * (y // 5)) % 4 +root = displayio.Group() +grids = [] +for scale, y in ((1, 8), (2, 50), (3, 110)): + group = displayio.Group(scale=scale, x=8, y=y) + grid = displayio.TileGrid(sheet, pixel_shader=palette, width=5, height=3, + tile_width=3, tile_height=5, x=1, y=1) + for i in range(15): + grid[i] = (7 - i * 3) % 8 + group.append(grid) + # Single-cell TileGrids use a separate LVGL path. Leave space around the sprite + # so drawing outside its rectangle cannot be hidden by neighboring cells. + sprite = displayio.TileGrid(sheet, pixel_shader=palette, + tile_width=3, tile_height=5, default_tile=2, x=30, y=1) + group.append(sprite) + root.append(group) + grids.append((grid, sprite)) +display.auto_refresh = False +display.root_group = root +display.refresh() +print('first') +time.sleep(10) +for grid, sprite in grids: + for i in range(15): + grid[i] = (i * 5 + 1) % 8 + sprite[0] = 5 +display.refresh() +print('second') +while True: + time.sleep(1) +""" + + +def _cropped_atlas_expected(width, height, changed): + frame = Image.new("RGB", (width, height), BLACK) + colors = (RED, GREEN, BLUE, WHITE) + + def tile(x0, y0, index, scale): + for y in range(5 * scale): + for x in range(3 * scale): + color = colors[(index % 4 + 2 * (index // 4)) % 4] + frame.putpixel((x0 + x, y0 + y), color) + + for scale, y0 in ((1, 8), (2, 50), (3, 110)): + for i in range(15): + index = (i * 5 + 1) % 8 if changed else (7 - i * 3) % 8 + tile(8 + (1 + i % 5 * 3) * scale, y0 + (1 + i // 5 * 5) * scale, index, scale) + tile(8 + 30 * scale, y0 + scale, 5 if changed else 2, scale) + return frame + + +@pytest.mark.circuitpy_drive({"code.py": CROPPED_ATLAS_CODE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_tilegrid_scaled_atlas_clipping(circuitpython): + """Scaled atlas windows never draw neighboring tiles outside their cells.""" + circuitpython.wait_until_done() + 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 = _cropped_atlas_expected(actual.width, actual.height, changed) + # Check the entire exterior and the center of every cell. Zephyr's older + # LVGL has a separate nearest-neighbor rounding bug at tile edges; this + # regression isolates clipping rather than depending on that sampler fix. + checked = { + (x, y) + for y in range(actual.height) + for x in range(actual.width) + if expected.getpixel((x, y)) == BLACK + } + for scale, y0 in ((1, 8), (2, 50), (3, 110)): + for i in range(15): + checked.add((8 + (2 + i % 5 * 3) * scale, y0 + (3 + i // 5 * 5) * scale)) + checked.add((8 + 31 * scale, y0 + 3 * scale)) + mismatches = [ + (x, y, expected.getpixel((x, y)), actual.getpixel((x, y))) + for x, y in sorted(checked) + if actual.getpixel((x, y)) != expected.getpixel((x, y)) + ] + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}" + + +UNSUPPORTED_CODE = """\ +import displayio + +bitmap = displayio.Bitmap(4, 4, 65536) +try: + displayio.TileGrid(bitmap, pixel_shader=displayio.ColorConverter()) + print('converting shader: ok') +except NotImplementedError: + print('converting shader: NotImplementedError') +print('done') +""" + + +@pytest.mark.circuitpy_drive({"code.py": UNSUPPORTED_CODE}) +@pytest.mark.parametrize( + "board", ["native_native_sim_lvgl", "native_native_sim_lvgl_asan"], indirect=True +) +@pytest.mark.duration(10) +def test_tilegrid_unsupported(circuitpython): + """Combinations LVGL can't draw directly raise NotImplementedError.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "converting shader: NotImplementedError" in output + assert "done" in output + + +VIEW_LIFECYCLE_CODE = """\ +import board +import displayio +import time + +display = board.DISPLAY + +def split_bands(): + bitmap = displayio.Bitmap(8, 8, 4) + for y in range(8): + for x in range(8): + bitmap[x, y] = 1 if x < 4 else 2 + return bitmap + +first = split_bands() +second = displayio.Bitmap(8, 8, 4) +for y in range(8): + for x in range(8): + second[x, y] = 3 + +canonical = displayio.Palette(4) +canonical[1] = 0x00FF00 +canonical[2] = 0x0000FF +canonical[3] = 0xFFFFFF +alternate = displayio.Palette(4) +alternate[1] = 0xFFFF00 +alternate[2] = 0x00FFFF +third = displayio.Palette(4) +third[1] = 0xFF00FF +third[2] = 0x00FF00 + +group = displayio.Group() +canonical_grid = displayio.TileGrid(first, pixel_shader=canonical, x=0, y=0) +copy_a = displayio.TileGrid(first, pixel_shader=alternate, x=20, y=0) +copy_b = displayio.TileGrid(first, pixel_shader=alternate, x=40, y=0) +second_grid = displayio.TileGrid(second, pixel_shader=canonical, x=0, y=20) +for grid in (canonical_grid, copy_a, copy_b, second_grid): + group.append(grid) + +display.auto_refresh = False +display.root_group = group +display.refresh() +print('first') +time.sleep(10) + +# Drop one of the two users of the shared LVGL palette. +group.remove(copy_b) +# Point the second grid at the first bitmap; both are 8x8. +second_grid.bitmap = first +# Give the canonical grid another palette. +canonical_grid.pixel_shader = third +# Change the shared bitmap and the remaining copy's palette. +first[0, 0] = 2 +alternate[1] = 0x000000 +display.refresh() +print('second') +while True: + time.sleep(1) +""" + + +def _lifecycle_expected(width, height, changed): + frame = [[BLACK] * width for _ in range(height)] + + def draw(x0, y0, colors): + for y in range(8): + for x in range(8): + frame[y0 + y][x0 + x] = colors[0] if x < 4 else colors[1] + + if not changed: + draw(0, 0, (GREEN, BLUE)) + draw(20, 0, (YELLOW, CYAN)) + draw(40, 0, (YELLOW, CYAN)) + draw(0, 20, (WHITE, WHITE)) + return frame + + # The canonical grid switched to the third palette, and pixel (0, 0) now holds index 2. + draw(0, 0, (MAGENTA, GREEN)) + frame[0][0] = GREEN + # The removed grid leaves a hole; the other copy uses its changed palette. + draw(20, 0, (BLACK, CYAN)) + frame[0][20] = CYAN + # The second grid now shows the first bitmap with the canonical palette, including the + # changed pixel. + draw(0, 20, (GREEN, BLUE)) + frame[20][0] = BLUE + return frame + + +@pytest.mark.circuitpy_drive({"code.py": VIEW_LIFECYCLE_CODE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 20_000_000_000]) +@pytest.mark.duration(24) +def test_tilegrid_view_lifecycle(circuitpython): + """Shared images survive detaching one user, and bitmap/palette swaps rebind cleanly.""" + 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 img: + rgb = img.convert("RGB") + expected = _lifecycle_expected(rgb.width, rgb.height, changed) + mismatches = [] + for y in range(rgb.height): + for x in range(rgb.width): + actual = rgb.getpixel((x, y)) + if actual != expected[y][x]: + mismatches.append((x, y, expected[y][x], actual)) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:8]}" + + +# Scrolling is not a Python-visible TileGrid property, but the terminal scrolls its TileGrid +# by moving the wrapped window over the tile matrix instead of moving tiles. So writing a +# fifth line into a four line terminal shifts the visible content up by exactly one cell row: +# the first three rows of the scrolled frame must equal the last three rows of the frame +# before it, and the bottom row must hold the new line. The wrapped window is two row runs +# once the terminal has scrolled. +SCROLL_CODE = """\ +import board +import displayio +import terminalio +import time + +display = board.DISPLAY + +font = terminalio.FONT +cell_width, cell_height = font.get_bounding_box() +palette = displayio.Palette(2) +palette[0] = 0x000000 +palette[1] = 0xFFFFFF + +group = displayio.Group() +grid = displayio.TileGrid(font.bitmap, pixel_shader=palette, width=4, height=4, + tile_width=cell_width, tile_height=cell_height, x=4, y=4) +group.append(grid) +terminal = terminalio.Terminal(grid, font) + +display.auto_refresh = False +display.root_group = group +# Fill the terminal without going past the last row, so this frame doesn't scroll yet. +terminal.write("aa\\r\\nbb\\r\\ncc\\r\\ndd") +display.refresh() +print('filled', cell_width, cell_height) +time.sleep(2) + +# Moving to the row below the last one makes the terminal scroll by one line. +terminal.write("\\r\\nee") +display.refresh() +print('scrolled') +time.sleep(2) +while True: + time.sleep(1) +""" + + +@pytest.mark.circuitpy_drive({"code.py": SCROLL_CODE}) +@pytest.mark.display(capture_times_ns=[4_000_000_000, 8_000_000_000]) +@pytest.mark.duration(14) +def test_tilegrid_scroll(circuitpython): + """A terminal scroll shifts the visible tile rows and wraps the window.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "scrolled" in output + match = re.search(r"filled (\d+) (\d+)", output) + assert match, "the scene didn't report the font's cell size" + cell_w = int(match.group(1)) + cell_h = int(match.group(2)) + + captures = circuitpython.display_capture_paths() + if len(captures) < 2 or not captures[1].exists(): + pytest.skip("display capture was not produced") + + with Image.open(captures[0]) as img: + filled = img.convert("RGB") + with Image.open(captures[1]) as img: + scrolled = img.convert("RGB") + + origin_x = 4 + origin_y = 4 + width = 4 * cell_w + # Row j of the filled frame is row j - 1 of the scrolled one, for every row but the last. + for row in range(1, 4): + for y in range(cell_h): + for x in range(width): + before = filled.getpixel((origin_x + x, origin_y + row * cell_h + y)) + after = scrolled.getpixel((origin_x + x, origin_y + (row - 1) * cell_h + y)) + assert before == after, f"row {row - 1} did not shift up (x={x}, y={y})" + + # The new line landed on the bottom row. + bottom = origin_y + 3 * cell_h + changed = any( + scrolled.getpixel((origin_x + x, bottom + y)) + != filled.getpixel((origin_x + x, bottom + y)) + for y in range(cell_h) + for x in range(width) + ) + assert changed, "the new line did not appear at the bottom of the scrolled frame" diff --git a/ports/zephyr-cp/tests/test_vectorio.py b/ports/zephyr-cp/tests/test_vectorio.py new file mode 100644 index 00000000000..5237d50db3e --- /dev/null +++ b/ports/zephyr-cp/tests/test_vectorio.py @@ -0,0 +1,290 @@ +# SPDX-FileCopyrightText: 2026 Scott Shawcroft for Adafruit Industries +# SPDX-License-Identifier: MIT + +"""vectorio tests that run against both displayio compositors. + +native_sim is built twice: once with the legacy scanline compositor +(``native_native_sim``) and once with the LVGL compositor +(``native_native_sim_lvgl``). vectorio has an implementation for each one, so +these tests check that the LVGL implementation behaves like the legacy one: the +same API, the same shapes in the same place and the same pixels on the screen. + +The golden image was captured from the legacy compositor and is compared by +both. The only pixels that are not compared are the ones along the outline of +the circle: LVGL draws a circle as a rounded rectangle and its rasterizer +supersamples the curve, so the outline is a blend of the shape and the +background there while the legacy compositor fills whole pixels. The band +around the outline is skipped, everything else has to match exactly. + +Two differences between the two implementations are known and are not tested +here: + +- LVGL antialiases the outline of a circle, the legacy compositor does not. +- The legacy compositor does not scale vectorio shapes with the scale of the + group they are in, while the LVGL one does. A shape in a scaled group is + therefore bigger on an LVGL display. +""" + +import math +import shutil +from pathlib import Path + +import pytest +from PIL import Image + +_DISPLAY_HELPER = """\ +import board +display = board.DISPLAY +""" + +# The scene both compositors render. The shapes are placed so that none of them +# overlap and so that the circle is far away from the other two. +SCENE_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 + +group = displayio.Group() + +circle = vectorio.Circle(pixel_shader=palette, radius=20, x=40, y=40, color_index=1) +group.append(circle) + +rectangle = vectorio.Rectangle( + pixel_shader=palette, width=40, height=20, x=100, y=40, color_index=2 +) +group.append(rectangle) + +polygon = vectorio.Polygon( + pixel_shader=palette, + points=[(0, 0), (40, 10), (20, 40)], + x=180, + y=40, + color_index=3, +) +group.append(polygon) + +# Replacing the root group hides the console so the frame only has the shapes. +display.auto_refresh = False +display.root_group = group +display.refresh() +print('rendered') +while True: + time.sleep(1) +""" +) + +# The scene with the rectangle moved and the circle hidden. It is captured twice: +# once before the move and once after it. +MOVE_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 + +group = displayio.Group() + +circle = vectorio.Circle(pixel_shader=palette, radius=20, x=40, y=40, color_index=1) +circle.hidden = True +group.append(circle) + +rectangle = vectorio.Rectangle( + pixel_shader=palette, width=40, height=20, x=100, y=40, color_index=2 +) +group.append(rectangle) + +display.auto_refresh = False +display.root_group = group +display.refresh() +print('first') +time.sleep(2) +rectangle.x = 160 +display.refresh() +print('second') +while True: + time.sleep(1) +""" +) + +API_CODE = """\ +import displayio +import vectorio + +palette = displayio.Palette(4) +palette[0] = 0x000000 +palette[1] = 0xFF0000 +palette[2] = 0x00FF00 +palette[3] = 0x0000FF + +circle = vectorio.Circle(pixel_shader=palette, radius=10, x=20, y=20) +rectangle = vectorio.Rectangle(pixel_shader=palette, width=30, height=20, x=5, y=5) +polygon = vectorio.Polygon(pixel_shader=palette, points=[(0, 0), (10, 0), (0, 10)]) + +print('circle', circle.x, circle.y, circle.radius, circle.color_index) +print('rectangle', rectangle.x, rectangle.y, rectangle.width, rectangle.height) +print('polygon', polygon.points, polygon.color_index) + +# The circle is centered on its position and covers the pixels whose center is +# within the radius. +print('circle contains', circle.contains(20, 20), circle.contains(30, 20), + circle.contains(31, 20), circle.contains(20, 31)) + +# The rectangle's position is its top left corner. +print('rectangle contains', rectangle.contains(5, 5), rectangle.contains(34, 24), + rectangle.contains(35, 24), rectangle.contains(4, 5)) + +# The polygon's points are relative to its position. +print('polygon contains', polygon.contains(0, 0), polygon.contains(5, 1), + polygon.contains(9, 9)) + +# Moving a shape moves its coverage with it. +circle.x = 50 +print('moved circle contains', circle.contains(60, 20), circle.contains(20, 20)) + +# The color index selects the palette entry. +circle.color_index = 2 +print('color index', circle.color_index) + +# A shape can only be in one group. +group = displayio.Group() +group.append(circle) +print('in group', len(group)) +other = displayio.Group() +try: + other.append(circle) + print('second group: no error') +except ValueError as e: + print('second group: ValueError') +print('done') +""" + + +def _read_image(path): + with Image.open(path) as img: + rgb = img.convert("RGB") + return rgb.width, rgb.height, rgb.tobytes() + + +def _pixel_at(image, x, y): + return tuple(image.getpixel((x, y))) + + +def _circle_outline_band(width, height, center_x, center_y, radius): + """Return the pixels LVGL may antialias when it draws a circle. + + LVGL draws the circle as a rounded rectangle and supersamples the curve, so + the pixels along the outline are a blend of the shape and the background + while the legacy compositor fills whole pixels. The pixels inside the + outline are filled completely by both and the pixels outside it are not + touched by either, so only the outline itself is skipped. + """ + band = set() + for y in range(height): + for x in range(width): + distance = math.hypot(x - center_x, y - center_y) + if radius - 1 < distance <= radius + 1.5: + band.add((x, y)) + return band + + +@pytest.mark.circuitpy_drive({"code.py": API_CODE}) +@pytest.mark.duration(15) +def test_vectorio_api(circuitpython): + """The vectorio API behaves the same on both compositors.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "circle 20 20 10 0" in output + assert "rectangle 5 5 30 20" in output + assert "polygon [(0, 0), (10, 0), (0, 10)] 0" in output + # The circle covers the pixels whose center is within the radius. + assert "circle contains True True False False" in output + # The rectangle's position is its top left corner. + assert "rectangle contains True True False False" in output + assert "polygon contains True True False" in output + # Moving a shape moves its coverage with it. + assert "moved circle contains True False" in output + assert "color index 2" in output + assert "in group 1" in output + assert "second group: ValueError" in output + assert "done" in output + + +@pytest.mark.circuitpy_drive({"code.py": SCENE_CODE}) +@pytest.mark.display(capture_times_ns=[4_000_000_000]) +@pytest.mark.duration(8) +def test_vectorio_render_golden(request, circuitpython): + """The shapes are rendered exactly like the legacy compositor renders them.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "rendered" in output + + captures = circuitpython.display_capture_paths() + if not captures or not captures[0].exists(): + pytest.skip("display capture was not produced") + + golden = Path(__file__).parent / "vectorio" / "golden" / "vectorio_shapes_320x240.png" + if request.config.getoption("--update-goldens"): + golden.parent.mkdir(parents=True, exist_ok=True) + shutil.copy2(captures[0], golden) + return + + gw, gh, gpx = _read_image(golden) + dw, dh, dpx = _read_image(captures[0]) + assert (dw, dh) == (gw, gh) + + band = _circle_outline_band(dw, dh, 40, 40, 20) + mismatches = [] + for y in range(dh): + for x in range(dw): + if (x, y) in band: + continue + i = (y * dw + x) * 3 + if gpx[i : i + 3] != dpx[i : i + 3]: + mismatches.append((x, y, tuple(gpx[i : i + 3]), tuple(dpx[i : i + 3]))) + assert not mismatches, f"{len(mismatches)} pixels differ, first: {mismatches[:5]}" + + +@pytest.mark.circuitpy_drive({"code.py": MOVE_CODE}) +@pytest.mark.display(capture_times_ns=[8_000_000_000, 10_000_000_000, 12_000_000_000]) +@pytest.mark.duration(16) +def test_vectorio_move_and_hide(circuitpython): + """Moving a shape updates the screen and a hidden shape is not drawn.""" + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "first" in output + assert "second" in output + + captures = circuitpython.display_capture_paths() + if not captures or not captures[-1].exists(): + pytest.skip("display capture was not produced") + + # The last capture is the one taken after the shape was moved. + with Image.open(captures[-1]) as img: + after = img.convert("RGB") + + green = (0, 255, 0) + black = (0, 0, 0) + + # The hidden circle is not drawn at all. + assert _pixel_at(after, 40, 40) == black + # After the move the rectangle is at its new position and the old one is + # cleared again. + assert _pixel_at(after, 110, 45) == black + assert _pixel_at(after, 160, 45) == green diff --git a/ports/zephyr-cp/tests/vectorio/golden/vectorio_shapes_320x240.png b/ports/zephyr-cp/tests/vectorio/golden/vectorio_shapes_320x240.png new file mode 100644 index 00000000000..5ad281784f8 Binary files /dev/null and b/ports/zephyr-cp/tests/vectorio/golden/vectorio_shapes_320x240.png differ diff --git a/ports/zephyr-cp/tests/zephyr_display/README.md b/ports/zephyr-cp/tests/zephyr_display/README.md index 6b502021543..419fe386950 100644 --- a/ports/zephyr-cp/tests/zephyr_display/README.md +++ b/ports/zephyr-cp/tests/zephyr_display/README.md @@ -7,6 +7,7 @@ This directory contains native_sim golden-image tests for the Zephyr-specific `z - `board.DISPLAY` is present and usable. - CircuitPython terminal/console tilegrids are attached to the default display root group. - Deterministic console terminal output matches a checked-in golden image. +- `terminalio.Terminal` output matches checked-in golden images at 90, 180 and 270 degree rotations. - `zephyr_display` pixel format constants are exposed. - `displayio` rendering produces expected stripe colors at sampled pixel locations. @@ -14,6 +15,7 @@ This directory contains native_sim golden-image tests for the Zephyr-specific `z - `test_zephyr_display.py` – pytest tests. - `golden/terminal_console_output_320x240.png` – console terminal output golden reference image. +- `golden/terminal_rotated_{90,180,270}_320x240.png` – rotated terminal golden reference images. ## How capture works diff --git a/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_180_320x240.png b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_180_320x240.png new file mode 100644 index 00000000000..e1caef2f3c6 Binary files /dev/null and b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_180_320x240.png differ diff --git a/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_270_320x240.png b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_270_320x240.png new file mode 100644 index 00000000000..ed6a08fedb7 Binary files /dev/null and b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_270_320x240.png differ diff --git a/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_90_320x240.png b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_90_320x240.png new file mode 100644 index 00000000000..b31594281be Binary files /dev/null and b/ports/zephyr-cp/tests/zephyr_display/golden/terminal_rotated_90_320x240.png differ diff --git a/ports/zephyr-cp/tests/zephyr_display/test_gifio.py b/ports/zephyr-cp/tests/zephyr_display/test_gifio.py index d4a665e2d44..a7ae91d4fdf 100644 --- a/ports/zephyr-cp/tests/zephyr_display/test_gifio.py +++ b/ports/zephyr-cp/tests/zephyr_display/test_gifio.py @@ -183,7 +183,7 @@ def test_gifio_metadata(circuitpython): @pytest.mark.circuitpy_drive(_GIFIO_DECODE_DRIVE) @pytest.mark.display(capture_times_ns=_DECODE_CAPTURE_TIMES_NS) @pytest.mark.duration(60) -def test_gifio_decode(request, circuitpython): +def test_gifio_decode(request, board, circuitpython): circuitpython.wait_until_done() output = circuitpython.serial.all_output @@ -193,6 +193,11 @@ def test_gifio_decode(request, circuitpython): assert f"rendered {frame_index}" in output assert "done" in output + if board.startswith("native_native_sim_lvgl"): + # The frames are decoded correctly, but LVGL scales the TileGrid about pixel + # centers and resamples, so it doesn't match displayio's whole-pixel scaling. + request.applymarker(pytest.mark.xfail(reason="LVGL image scaling isn't pixel exact")) + captures = circuitpython.display_capture_paths() assert len(captures) == _DECODE_FRAME_COUNT golden_dir = Path(__file__).parent / "golden" diff --git a/ports/zephyr-cp/tests/zephyr_display/test_jpegio.py b/ports/zephyr-cp/tests/zephyr_display/test_jpegio.py index 6368be7a5cf..ad03845e36f 100644 --- a/ports/zephyr-cp/tests/zephyr_display/test_jpegio.py +++ b/ports/zephyr-cp/tests/zephyr_display/test_jpegio.py @@ -76,12 +76,17 @@ def _golden_compare_or_update(request, captures, golden_path): @pytest.mark.circuitpy_drive(_JPEGIO_DRIVE) @pytest.mark.display(capture_times_ns=[14_000_000_000]) @pytest.mark.duration(18) -def test_jpegio_decode(request, circuitpython): +def test_jpegio_decode(request, board, circuitpython): circuitpython.wait_until_done() output = circuitpython.serial.all_output assert "size 20 20" in output assert "rendered" in output + if board.startswith("native_native_sim_lvgl"): + # The image is decoded correctly, but LVGL scales it about pixel centers and + # resamples, so it doesn't match displayio's whole-pixel scaling. + request.applymarker(pytest.mark.xfail(reason="LVGL image scaling isn't pixel exact")) + golden = Path(__file__).parent / "golden" / "jpegio_test_pattern_320x240.png" _golden_compare_or_update(request, circuitpython.display_capture_paths(), golden) diff --git a/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py b/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py index 43eb69e999e..fb223a78111 100644 --- a/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py +++ b/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py @@ -167,6 +167,60 @@ def test_console_output_golden_pixel_format(pixel_format, request, circuitpython ) +ROTATED_TERMINAL_CODE = """\ +import board +import displayio +import terminalio +import time +import zephyr_display + +displayio.release_displays() +display = zephyr_display.Display(auto_refresh=False) + +font = terminalio.FONT +cell_width, cell_height = font.get_bounding_box() +palette = displayio.Palette(2) +palette[0] = 0x000000 +palette[1] = 0xFFFFFF +group = displayio.Group() +grid = displayio.TileGrid(font.bitmap, pixel_shader=palette, width=20, height=10, + tile_width=cell_width, tile_height=cell_height) +group.append(grid) +terminal = terminalio.Terminal(grid, font) +terminal.write("ROTATED\\r\\nTERMINAL\\r\\n{rotation} DEG") + +display.root_group = group +display.rotation = {rotation} +display.refresh() +print('shown', display.rotation, display.width, display.height) +while True: + time.sleep(1) +""" + + +@pytest.fixture(params=[90, 180, 270]) +def terminal_rotation(request): + """Add a drive whose code sets the display to this rotation.""" + rotation = request.param + request.node.add_marker( + pytest.mark.circuitpy_drive({"code.py": ROTATED_TERMINAL_CODE.format(rotation=rotation)}) + ) + return rotation + + +@pytest.mark.display(capture_times_ns=[4_000_000_000]) +@pytest.mark.duration(8) +def test_terminal_rotated(terminal_rotation, request, circuitpython): + circuitpython.wait_until_done() + + width, height = (240, 320) if terminal_rotation in (90, 270) else (320, 240) + output = circuitpython.serial.all_output + assert f"shown {terminal_rotation} {width} {height}" in output + + golden = Path(__file__).parent / "golden" / f"terminal_rotated_{terminal_rotation}_320x240.png" + _golden_compare_or_update(request, circuitpython.display_capture_paths(), golden) + + MONO_NO_VTILED_FORMATS = ["MONO01", "MONO10"] diff --git a/ports/zephyr-cp/zephyr-config/west.yml b/ports/zephyr-cp/zephyr-config/west.yml index 1cc4fa5e9be..3b7e7cdc843 100644 --- a/ports/zephyr-cp/zephyr-config/west.yml +++ b/ports/zephyr-cp/zephyr-config/west.yml @@ -53,7 +53,6 @@ manifest: - libsbc - lora-basics-modem - loramac-node - - lvgl - mipi-sys-t - mldsa-native - net-tools diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 55c5c130a7e..9fd8abce783 100755 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -696,6 +696,7 @@ $(filter $(SRC_PATTERNS), \ _bleio/ScanEntry.c \ _eve/__init__.c \ __future__/__init__.c \ + busdisplay/BusDisplay.c \ camera/ImageFormat.c \ canio/Match.c \ codeop/__init__.c \ @@ -704,8 +705,16 @@ $(filter $(SRC_PATTERNS), \ digitalio/Direction.c \ digitalio/DriveMode.c \ digitalio/Pull.c \ + displayio/Bitmap.c \ + displayio/ColorConverter.c \ + displayio/Group.c \ + displayio/OnDiskBitmap.c \ + displayio/Palette.c \ + displayio/TileGrid.c \ + displayio/__init__.c \ displayio/Colorspace.c \ fontio/Glyph.c \ + framebufferio/FramebufferDisplay.c \ imagecapture/ParallelImageCapture.c \ locale/__init__.c \ math/__init__.c \ @@ -722,6 +731,7 @@ $(filter $(SRC_PATTERNS), \ supervisor/RunReason.c \ supervisor/Runtime.c \ supervisor/StatusBar.c \ + vectorio/VectorShape.c \ videogif/__init__.c \ wifi/AuthMode.c \ wifi/Packet.c \ @@ -818,18 +828,9 @@ SRC_SHARED_MODULE_ALL = \ adafruit_bus_device/i2c_device/I2CDevice.c \ adafruit_bus_device/spi_device/SPIDevice.c \ busdisplay/__init__.c \ - busdisplay/BusDisplay.c \ canio/Match.c \ canio/Message.c \ canio/RemoteTransmissionRequest.c \ - displayio/Bitmap.c \ - displayio/ColorConverter.c \ - displayio/Group.c \ - displayio/OnDiskBitmap.c \ - displayio/Palette.c \ - displayio/TileGrid.c \ - displayio/area.c \ - displayio/__init__.c \ dotclockframebuffer/__init__.c \ emmcio/__init__.c \ epaperdisplay/__init__.c \ @@ -844,7 +845,6 @@ SRC_SHARED_MODULE_ALL = \ lvfontio/__init__.c \ fourwire/__init__.c \ fourwire/FourWire.c \ - framebufferio/FramebufferDisplay.c \ framebufferio/__init__.c \ getpass/__init__.c \ gifio/__init__.c \ @@ -930,7 +930,6 @@ SRC_SHARED_MODULE_ALL = \ vectorio/Circle.c \ vectorio/Polygon.c \ vectorio/Rectangle.c \ - vectorio/VectorShape.c \ vectorio/__init__.c \ videocore/VideoBuffer.c \ videocore/__init__.c \ @@ -939,6 +938,43 @@ SRC_SHARED_MODULE_ALL = \ watchdog/__init__.c \ zlib/__init__.c \ +# The displayio and vectorio implementation sources live in one of the two backend +# directories. vectorio's shape math (Circle, Polygon, Rectangle) is backend agnostic and +# lives in shared-module/vectorio/ directly. +ifeq ($(CIRCUITPY_DISPLAYIO_LVGL),1) +SRC_SHARED_MODULE_INTERNAL += \ +$(filter $(SRC_PATTERNS), \ + displayio/lvgl/Bitmap.c \ + displayio/lvgl/ColorConverter.c \ + displayio/lvgl/Group.c \ + displayio/lvgl/ImageRoot.c \ + displayio/lvgl/OnDiskBitmap.c \ + displayio/lvgl/Palette.c \ + busdisplay/lvgl/BusDisplay.c \ + displayio/lvgl/TileGrid.c \ + displayio/legacy/__init__.c \ + framebufferio/lvgl/FramebufferDisplay.c \ + vectorio/lvgl/VectorShape.c \ + videocore/lvgl/VideoBuffer.c \ +) +else +SRC_SHARED_MODULE_INTERNAL += \ +$(filter $(SRC_PATTERNS), \ + busdisplay/legacy/BusDisplay.c \ + displayio/legacy/__init__.c \ + displayio/legacy/Bitmap.c \ + displayio/legacy/ColorConverter.c \ + displayio/legacy/display_core_scanline.c \ + displayio/legacy/Group.c \ + displayio/legacy/OnDiskBitmap.c \ + displayio/legacy/Palette.c \ + displayio/legacy/TileGrid.c \ + framebufferio/legacy/FramebufferDisplay.c \ + vectorio/legacy/VectorShape.c \ + videocore/legacy/VideoBuffer.c \ +) +endif + # All possible sources are listed here, and are filtered by SRC_PATTERNS. SRC_SHARED_MODULE = $(filter $(SRC_PATTERNS), $(SRC_SHARED_MODULE_ALL)) @@ -1173,13 +1209,16 @@ $(BUILD)/lib/uzlib/tinflate.o: CFLAGS += -Wno-missing-braces -Wno-missing-protot endif # All possible sources are listed here, and are filtered by SRC_PATTERNS. -SRC_SHARED_MODULE_INTERNAL = \ +# area.c is shared by both compositors (vectorio needs the area helpers) so it is +# always compiled. +SRC_SHARED_MODULE_INTERNAL += \ $(filter $(SRC_PATTERNS), \ _airlift/network.c \ _airlift/socketpool.c \ _airlift/transport.c \ displayio/bus_core.c \ displayio/display_core.c \ + displayio/area.c \ epaperdisplay/lag_drive.c \ usb/utf16le.c \ ) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index 49f91bc945d..90f695bef82 100755 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -293,6 +293,12 @@ CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO) CIRCUITPY_DISPLAYIO ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_DISPLAYIO=$(CIRCUITPY_DISPLAYIO) +# Select the displayio compositing core implementation: 0 = legacy scanline core (default), +# 1 = LVGL-based core (shared-module/displayio/lvgl). The LVGL variant requires LVGL be +# available to the port at build time. +CIRCUITPY_DISPLAYIO_LVGL ?= 0 +CFLAGS += -DCIRCUITPY_DISPLAYIO_LVGL=$(CIRCUITPY_DISPLAYIO_LVGL) + # Stack bytes a display refresh composes at once. Bigger means fewer, larger pieces per refresh. CIRCUITPY_DISPLAY_AREA_BUFFER_SIZE ?= 512 CFLAGS += -DCIRCUITPY_DISPLAY_AREA_BUFFER_SIZE=$(CIRCUITPY_DISPLAY_AREA_BUFFER_SIZE) @@ -734,6 +740,10 @@ CIRCUITPY_LVFONTIO ?= $(CIRCUITPY_TERMINALIO) CFLAGS += -DCIRCUITPY_LVFONTIO=$(CIRCUITPY_LVFONTIO) CIRCUITPY_TILEPALETTEMAPPER ?= $(CIRCUITPY_DISPLAYIO) +# tilepalettemapper uses the scanline palette conversion API. +ifeq ($(CIRCUITPY_DISPLAYIO_LVGL),1) +CIRCUITPY_TILEPALETTEMAPPER = 0 +endif CFLAGS += -DCIRCUITPY_TILEPALETTEMAPPER=$(CIRCUITPY_TILEPALETTEMAPPER) CIRCUITPY_TIME ?= 1 diff --git a/shared-bindings/busdisplay/BusDisplay.c b/shared-bindings/busdisplay/BusDisplay.c index dd3be20d5c5..c1bf072d646 100644 --- a/shared-bindings/busdisplay/BusDisplay.c +++ b/shared-bindings/busdisplay/BusDisplay.c @@ -455,6 +455,15 @@ static mp_obj_t busdisplay_busdisplay_obj_fill_row(size_t n_args, const mp_obj_t mp_int_t y = args[ARG_y].u_int; mp_obj_t *result = args[ARG_buffer].u_obj; + #if CIRCUITPY_DISPLAYIO_LVGL + // LVGL renders into its own draw buffers, so there is no CPU-readable row to + // return. Keep the API available and report that this backend can't do it. + (void)self; + (void)y; + (void)result; + mp_raise_NotImplementedError(MP_ERROR_TEXT("Operation or feature not supported")); + #else + mp_buffer_info_t bufinfo; mp_get_buffer_raise(result, &bufinfo, MP_BUFFER_WRITE); @@ -491,6 +500,7 @@ static mp_obj_t busdisplay_busdisplay_obj_fill_row(size_t n_args, const mp_obj_t } else { mp_raise_ValueError(MP_ERROR_TEXT("Buffer too small")); } + #endif } MP_DEFINE_CONST_FUN_OBJ_KW(busdisplay_busdisplay_fill_row_obj, 1, busdisplay_busdisplay_obj_fill_row); diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c index 2cd93ee9f57..90f05106821 100644 --- a/shared-bindings/displayio/Bitmap.c +++ b/shared-bindings/displayio/Bitmap.c @@ -235,14 +235,11 @@ static mp_obj_t displayio_bitmap_obj_dirty(size_t n_args, const mp_obj_t *pos_ar mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - displayio_area_t dirty_area = { - .x1 = args[ARG_x1].u_int, - .y1 = args[ARG_y1].u_int, - .x2 = args[ARG_x2].u_int == -1 ? self->width : args[ARG_x2].u_int, - .y2 = args[ARG_y2].u_int == -1 ? self->height : args[ARG_y2].u_int, - }; - - displayio_bitmap_set_dirty_area(self, &dirty_area); + displayio_bitmap_set_dirty(self, + args[ARG_x1].u_int, + args[ARG_y1].u_int, + args[ARG_x2].u_int == -1 ? self->width : args[ARG_x2].u_int, + args[ARG_y2].u_int == -1 ? self->height : args[ARG_y2].u_int); return mp_const_none; } diff --git a/shared-bindings/displayio/Group.c b/shared-bindings/displayio/Group.c index 94ee2e58de1..31b1b77f34f 100644 --- a/shared-bindings/displayio/Group.c +++ b/shared-bindings/displayio/Group.c @@ -350,5 +350,6 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &displayio_group_locals_dict, subscr, group_subscr, unary_op, group_unary_op, - iter, mp_obj_generic_subscript_getiter + iter, mp_obj_generic_subscript_getiter, + protocol, DISPLAYIO_GROUP_LVGL_PROTOCOL ); diff --git a/shared-bindings/displayio/TileGrid.c b/shared-bindings/displayio/TileGrid.c index fb5cabf05bf..2a1cf254b43 100644 --- a/shared-bindings/displayio/TileGrid.c +++ b/shared-bindings/displayio/TileGrid.c @@ -487,5 +487,6 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, make_new, displayio_tilegrid_make_new, locals_dict, &displayio_tilegrid_locals_dict, - subscr, tilegrid_subscr + subscr, tilegrid_subscr, + protocol, DISPLAYIO_TILEGRID_LVGL_PROTOCOL ); diff --git a/shared-bindings/displayio/TileGrid.h b/shared-bindings/displayio/TileGrid.h index 35b2b2fd00c..521ee34583c 100644 --- a/shared-bindings/displayio/TileGrid.h +++ b/shared-bindings/displayio/TileGrid.h @@ -10,6 +10,9 @@ extern const mp_obj_type_t displayio_tilegrid_type; +// Raises TypeError if pixel_shader isn't a Palette, ColorConverter or TilePaletteMapper. +void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader); + void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap, uint16_t bitmap_width_in_tiles, uint16_t bitmap_height_in_tiles, mp_obj_t pixel_shader, uint16_t width, uint16_t height, diff --git a/shared-bindings/framebufferio/FramebufferDisplay.c b/shared-bindings/framebufferio/FramebufferDisplay.c index 7faaab56fa1..82ad30f22f7 100644 --- a/shared-bindings/framebufferio/FramebufferDisplay.c +++ b/shared-bindings/framebufferio/FramebufferDisplay.c @@ -287,6 +287,15 @@ static mp_obj_t framebufferio_framebufferdisplay_obj_fill_row(size_t n_args, con mp_int_t y = args[ARG_y].u_int; mp_obj_t *result = args[ARG_buffer].u_obj; + #if CIRCUITPY_DISPLAYIO_LVGL + // LVGL renders into its own draw buffers, so there is no CPU-readable row to + // return. Keep the API available and report that this backend can't do it. + (void)self; + (void)y; + (void)result; + mp_raise_NotImplementedError(MP_ERROR_TEXT("Operation or feature not supported")); + #else + mp_buffer_info_t bufinfo; mp_get_buffer_raise(result, &bufinfo, MP_BUFFER_WRITE); @@ -326,6 +335,7 @@ static mp_obj_t framebufferio_framebufferdisplay_obj_fill_row(size_t n_args, con } else { mp_raise_ValueError(MP_ERROR_TEXT("Buffer too small")); } + #endif } MP_DEFINE_CONST_FUN_OBJ_KW(framebufferio_framebufferdisplay_fill_row_obj, 1, framebufferio_framebufferdisplay_obj_fill_row); diff --git a/shared-bindings/hashlib/__init__.c b/shared-bindings/hashlib/__init__.c index 354529cfa77..430c2f14cc1 100644 --- a/shared-bindings/hashlib/__init__.c +++ b/shared-bindings/hashlib/__init__.c @@ -20,7 +20,8 @@ //| //| def new(name: str, data: bytes = b"") -> hashlib.Hash: //| """Returns a Hash object setup for the named algorithm. Raises ValueError when the named -//| algorithm is unsupported. Supported algorithms for ``name`` are ``'sha1'`` and ``'sha256'``. +//| algorithm is unsupported. Supported algorithms for ``name`` are ``'md5'``, ``'sha1'``, and +//| ``'sha256'``. //| //| :return: a hash object for the given algorithm //| :rtype: hashlib.Hash""" diff --git a/shared-bindings/mipidsi/Display.c b/shared-bindings/mipidsi/Display.c index b4c3d23ce06..dd102397412 100644 --- a/shared-bindings/mipidsi/Display.c +++ b/shared-bindings/mipidsi/Display.c @@ -265,6 +265,10 @@ static void mipidsi_display_deinit_proto(mp_obj_t self_in) { common_hal_mipidsi_display_deinit(self_in); } +static void mipidsi_display_set_frame_callback(mp_obj_t self_in, framebuffer_frame_callback_t cb, void *data) { + common_hal_mipidsi_display_set_frame_callback(self_in, cb, data); +} + static int mipidsi_display_get_width_proto(mp_obj_t self_in) { return common_hal_mipidsi_display_get_width(self_in); } @@ -312,6 +316,7 @@ static const framebuffer_p_t mipidsi_display_proto = { .get_pixels_in_byte_share_row = mipidsi_display_get_pixels_in_byte_share_row_proto, .swapbuffers = mipidsi_display_swapbuffers, .deinit = mipidsi_display_deinit_proto, + .set_frame_callback = mipidsi_display_set_frame_callback, }; MP_DEFINE_CONST_OBJ_TYPE( diff --git a/shared-bindings/mipidsi/Display.h b/shared-bindings/mipidsi/Display.h index df998fcfe54..981bde4da12 100644 --- a/shared-bindings/mipidsi/Display.h +++ b/shared-bindings/mipidsi/Display.h @@ -36,6 +36,7 @@ void common_hal_mipidsi_display_construct(mipidsi_display_obj_t *self, void common_hal_mipidsi_display_deinit(mipidsi_display_obj_t *self); bool common_hal_mipidsi_display_deinited(mipidsi_display_obj_t *self); void common_hal_mipidsi_display_refresh(mipidsi_display_obj_t *self); +void common_hal_mipidsi_display_set_frame_callback(mipidsi_display_obj_t *self, framebuffer_frame_callback_t cb, void *data); // Make the rows marked in dirty_row_bitmap (one bit per row, LSB first) visible to the // display. NULL refreshes every row. This backs the framebuffer protocol's swapbuffers // callback (mipidsi_display_swapbuffers in Display.c), which FramebufferDisplay calls diff --git a/shared-bindings/supervisor/SafeModeReason.c b/shared-bindings/supervisor/SafeModeReason.c index ff85d8a3d5c..1aaa8f834c7 100644 --- a/shared-bindings/supervisor/SafeModeReason.c +++ b/shared-bindings/supervisor/SafeModeReason.c @@ -18,6 +18,7 @@ MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, FLASH_WRITE_ MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, GC_ALLOC_OUTSIDE_VM, SAFE_MODE_GC_ALLOC_OUTSIDE_VM); MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, HARD_FAULT, SAFE_MODE_HARD_FAULT); MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, INTERRUPT_ERROR, SAFE_MODE_INTERRUPT_ERROR); +MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, LVGL_OUT_OF_MEMORY, SAFE_MODE_LVGL_OUT_OF_MEMORY); MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, NLR_JUMP_FAIL, SAFE_MODE_NLR_JUMP_FAIL); MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, NO_CIRCUITPY, SAFE_MODE_NO_CIRCUITPY); MAKE_ENUM_VALUE(supervisor_safe_mode_reason_type, safe_mode_reason, NO_HEAP, SAFE_MODE_NO_HEAP); @@ -71,6 +72,11 @@ MAKE_ENUM_MAP(supervisor_safe_mode_reason) { //| MAKE_ENUM_MAP_ENTRY(safe_mode_reason, INTERRUPT_ERROR), +//| LVGL_OUT_OF_MEMORY: object +//| """LVGL could not allocate memory.""" +//| + MAKE_ENUM_MAP_ENTRY(safe_mode_reason, LVGL_OUT_OF_MEMORY), + //| NLR_JUMP_FAIL: object //| """An error occurred during exception handling, possibly due to memory corruption.""" //| diff --git a/shared-bindings/usb_video/USBFramebuffer.c b/shared-bindings/usb_video/USBFramebuffer.c index 25151809df5..efff704a876 100644 --- a/shared-bindings/usb_video/USBFramebuffer.c +++ b/shared-bindings/usb_video/USBFramebuffer.c @@ -94,14 +94,17 @@ static void usb_video_uvcframebuffer_get_bufinfo(mp_obj_t self_in, mp_buffer_inf // These version exists so that the prototype matches the protocol, // avoiding a type cast that can hide errors static void usb_video_uvcframebuffer_swapbuffers(mp_obj_t self_in, uint8_t *dirty_row_bitmap) { - (void)dirty_row_bitmap; - shared_module_usb_video_uvcframebuffer_refresh(self_in); + shared_module_usb_video_uvcframebuffer_swapbuffers(self_in, dirty_row_bitmap); } static void usb_video_uvcframebuffer_deinit_proto(mp_obj_t self_in) { /* NOTHING */ } +static void usb_video_uvcframebuffer_set_frame_callback(mp_obj_t self_in, framebuffer_frame_callback_t cb, void *data) { + shared_module_usb_video_uvcframebuffer_set_frame_callback(self_in, cb, data); +} + static int usb_video_uvcframebuffer_get_width_proto(mp_obj_t self_in) { return shared_module_usb_video_uvcframebuffer_get_width(self_in); } @@ -128,6 +131,7 @@ static const framebuffer_p_t usb_video_uvcframebuffer_proto = { .swapbuffers = usb_video_uvcframebuffer_swapbuffers, .deinit = usb_video_uvcframebuffer_deinit_proto, .get_reverse_pixels_in_word = usb_video_uvcframebuffer_get_reverse_pixels_in_word_proto, + .set_frame_callback = usb_video_uvcframebuffer_set_frame_callback, }; static mp_int_t usb_video_uvcframebuffer_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { diff --git a/shared-bindings/usb_video/USBFramebuffer.h b/shared-bindings/usb_video/USBFramebuffer.h index b974f61578a..eb04fa19816 100644 --- a/shared-bindings/usb_video/USBFramebuffer.h +++ b/shared-bindings/usb_video/USBFramebuffer.h @@ -6,6 +6,8 @@ #pragma once +#include "shared-module/framebufferio/__init__.h" + typedef struct usb_video_uvcframebuffer_obj usb_video_uvcframebuffer_obj_t; extern const mp_obj_type_t usb_video_USBFramebuffer_type; @@ -13,5 +15,8 @@ extern usb_video_uvcframebuffer_obj_t usb_video_uvcframebuffer_singleton_obj; void shared_module_usb_video_uvcframebuffer_get_bufinfo(usb_video_uvcframebuffer_obj_t *self, mp_buffer_info_t *bufinfo); void shared_module_usb_video_uvcframebuffer_refresh(usb_video_uvcframebuffer_obj_t *self); +void shared_module_usb_video_uvcframebuffer_swapbuffers(usb_video_uvcframebuffer_obj_t *self, const uint8_t *dirty_row_bitmap); +void shared_module_usb_video_uvcframebuffer_set_frame_callback(usb_video_uvcframebuffer_obj_t *self, framebuffer_frame_callback_t cb, void *data); +void shared_module_usb_video_uvcframebuffer_frame_boundary(usb_video_uvcframebuffer_obj_t *self); int shared_module_usb_video_uvcframebuffer_get_width(usb_video_uvcframebuffer_obj_t *self); int shared_module_usb_video_uvcframebuffer_get_height(usb_video_uvcframebuffer_obj_t *self); diff --git a/shared-bindings/usb_video/__init__.c b/shared-bindings/usb_video/__init__.c index 06050188d66..512ffe3f950 100644 --- a/shared-bindings/usb_video/__init__.c +++ b/shared-bindings/usb_video/__init__.c @@ -10,6 +10,7 @@ #include "shared-bindings/usb_video/__init__.h" #include "shared-bindings/usb_video/USBFramebuffer.h" #include "shared-module/usb_video/__init__.h" +#include "shared-module/usb_video/uvc_usb_descriptors.h" //| """Allows streaming bitmaps to a host computer via USB //| @@ -48,12 +49,14 @@ //| //| -//| def enable_framebuffer(width: int, height: int) -> None: +//| def enable_framebuffer(width: int, height: int, *, frame_rate: int = 15) -> None: //| """Enable a USB video framebuffer, setting the given width & height //| //| This function may only be used from ``boot.py``. //| -//| Width is rounded up to a multiple of 2. +//| Width is rounded up to a multiple of 2. ``frame_rate`` is the advertised +//| UVC frame rate; uncompressed YUYV uses 2 bytes per pixel, so larger frames +//| need a lower rate to fit in USB high-speed bandwidth. //| //| After boot.py completes, the framebuffer will be allocated. Total storage //| of 4×``width``×``height`` bytes is required, reducing the amount available @@ -63,10 +66,11 @@ //| static mp_obj_t usb_video_enable_framebuffer(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_width, ARG_height }; + enum { ARG_width, ARG_height, ARG_frame_rate }; static const mp_arg_t allowed_args[] = { { MP_QSTR_width, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } }, { MP_QSTR_height, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } }, + { MP_QSTR_frame_rate, MP_ARG_KW_ONLY | MP_ARG_INT, { .u_int = DEFAULT_FRAME_RATE } }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); @@ -74,7 +78,8 @@ static mp_obj_t usb_video_enable_framebuffer(size_t n_args, const mp_obj_t *pos_ // (but note that most devices will not be able to allocate this much memory. uint32_t width = mp_arg_validate_int_range(args[ARG_width].u_int, 0, 32767, MP_QSTR_width); uint32_t height = mp_arg_validate_int_range(args[ARG_height].u_int, 0, 32767, MP_QSTR_height); - if (!shared_module_usb_video_enable(width, height)) { + uint32_t frame_rate = mp_arg_validate_int_range(args[ARG_frame_rate].u_int, 1, 60, MP_QSTR_frame_rate); + if (!shared_module_usb_video_enable(width, height, frame_rate)) { mp_raise_RuntimeError(MP_ERROR_TEXT("Cannot change USB devices now")); } diff --git a/shared-bindings/usb_video/__init__.h b/shared-bindings/usb_video/__init__.h index 634a6f4a2da..19e0eee7392 100644 --- a/shared-bindings/usb_video/__init__.h +++ b/shared-bindings/usb_video/__init__.h @@ -7,6 +7,7 @@ #pragma once #include "shared-module/displayio/Bitmap.h" -bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height); +bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height, mp_int_t frame_rate); bool shared_module_usb_video_disable(void); void shared_module_usb_video_swapbuffers(void); +void shared_module_usb_video_swapbuffers_dirty(const uint8_t *dirty_row_bitmap); diff --git a/shared-bindings/vectorio/Circle.c b/shared-bindings/vectorio/Circle.c index 7fb20b59b4d..2f3bc6bb5d2 100644 --- a/shared-bindings/vectorio/Circle.c +++ b/shared-bindings/vectorio/Circle.c @@ -61,11 +61,24 @@ static mp_obj_t vectorio_circle_make_new(const mp_obj_type_t *type, size_t n_arg return MP_OBJ_FROM_PTR(self); } +#if CIRCUITPY_DISPLAYIO_LVGL +static mp_obj_t circle_lvgl_protocol_self(mp_obj_t self_in) { + return common_hal_vectorio_circle_get_draw_protocol(MP_OBJ_TO_PTR(self_in)); +} + +static const lvgl_protocol_t circle_lvgl_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = circle_lvgl_protocol_self, +}; +#define VECTORIO_CIRCLE_PROTOCOL &circle_lvgl_protocol +#else static const vectorio_draw_protocol_t circle_draw_protocol = { MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw) .draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_circle_get_draw_protocol, .draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl }; +#define VECTORIO_CIRCLE_PROTOCOL &circle_draw_protocol +#endif //| radius: int @@ -146,5 +159,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, make_new, vectorio_circle_make_new, locals_dict, &vectorio_circle_locals_dict, - protocol, &circle_draw_protocol + protocol, VECTORIO_CIRCLE_PROTOCOL ); diff --git a/shared-bindings/vectorio/Polygon.c b/shared-bindings/vectorio/Polygon.c index 0f45da21e61..c78e838cc8c 100644 --- a/shared-bindings/vectorio/Polygon.c +++ b/shared-bindings/vectorio/Polygon.c @@ -69,11 +69,24 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar return MP_OBJ_FROM_PTR(self); } +#if CIRCUITPY_DISPLAYIO_LVGL +static mp_obj_t polygon_lvgl_protocol_self(mp_obj_t self_in) { + return common_hal_vectorio_polygon_get_draw_protocol(MP_OBJ_TO_PTR(self_in)); +} + +static const lvgl_protocol_t polygon_lvgl_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = polygon_lvgl_protocol_self, +}; +#define VECTORIO_POLYGON_PROTOCOL &polygon_lvgl_protocol +#else static const vectorio_draw_protocol_t polygon_draw_protocol = { MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw) .draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_polygon_get_draw_protocol, .draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl }; +#define VECTORIO_POLYGON_PROTOCOL &polygon_draw_protocol +#endif //| points: List[Tuple[int, int]] @@ -155,5 +168,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, make_new, vectorio_polygon_make_new, locals_dict, &vectorio_polygon_locals_dict, - protocol, &polygon_draw_protocol + protocol, VECTORIO_POLYGON_PROTOCOL ); diff --git a/shared-bindings/vectorio/Rectangle.c b/shared-bindings/vectorio/Rectangle.c index 3582b010149..6e1a0eedfff 100644 --- a/shared-bindings/vectorio/Rectangle.c +++ b/shared-bindings/vectorio/Rectangle.c @@ -65,11 +65,24 @@ static mp_obj_t vectorio_rectangle_make_new(const mp_obj_type_t *type, size_t n_ return MP_OBJ_FROM_PTR(self); } +#if CIRCUITPY_DISPLAYIO_LVGL +static mp_obj_t rectangle_lvgl_protocol_self(mp_obj_t self_in) { + return common_hal_vectorio_rectangle_get_draw_protocol(MP_OBJ_TO_PTR(self_in)); +} + +static const lvgl_protocol_t rectangle_lvgl_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = rectangle_lvgl_protocol_self, +}; +#define VECTORIO_RECTANGLE_PROTOCOL &rectangle_lvgl_protocol +#else static const vectorio_draw_protocol_t rectangle_draw_protocol = { MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw) .draw_get_protocol_self = (draw_get_protocol_self_fun)common_hal_vectorio_rectangle_get_draw_protocol, .draw_protocol_impl = &vectorio_vector_shape_draw_protocol_impl }; +#define VECTORIO_RECTANGLE_PROTOCOL &rectangle_draw_protocol +#endif //| width: int //| """The width of the rectangle in pixels.""" @@ -168,5 +181,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, make_new, vectorio_rectangle_make_new, locals_dict, &vectorio_rectangle_locals_dict, - protocol, &rectangle_draw_protocol + protocol, VECTORIO_RECTANGLE_PROTOCOL ); diff --git a/shared-bindings/vectorio/VectorShape.c b/shared-bindings/vectorio/VectorShape.c index 834f1fbff50..c6e2949840a 100644 --- a/shared-bindings/vectorio/VectorShape.c +++ b/shared-bindings/vectorio/VectorShape.c @@ -22,6 +22,31 @@ #include "py/objtype.h" #include "py/runtime.h" +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/lvgl_protocol.h" +#endif + +// The VectorShape wrapper behind a Python-visible shape (Rectangle, Circle or Polygon). A +// shape's compositor state lives in the wrapper, so the properties below are defined on it +// and reach it through the shape's protocol slot: protocol_lvgl's protocol_self() in LVGL +// builds, the draw protocol's draw_get_protocol_self() in legacy builds. +static vectorio_vector_shape_t *_wrapper_shape(mp_obj_t wrapper_shape) { + #if CIRCUITPY_DISPLAYIO_LVGL + const lvgl_protocol_t *protocol = mp_proto_get(MP_QSTR_protocol_lvgl, wrapper_shape); + if (protocol == NULL || protocol->protocol_self == NULL) { + return NULL; + } + return MP_OBJ_TO_PTR(protocol->protocol_self(wrapper_shape)); + #else + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, + wrapper_shape); + if (draw_protocol == NULL) { + return NULL; + } + return MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + #endif +} + // shape: The shape implementation to draw. // pixel_shader: The pixel shader that produces colors from values. The shader can be a displayio.Palette(1); it will be asked to color pixel value 0. @@ -37,15 +62,21 @@ mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pix // Wire up shape functions if (mp_obj_is_type(shape, &vectorio_polygon_type)) { ishape.shape = shape; + #if !CIRCUITPY_DISPLAYIO_LVGL ishape.get_area = &common_hal_vectorio_polygon_get_area; + #endif ishape.get_pixel = &common_hal_vectorio_polygon_get_pixel; } else if (mp_obj_is_type(shape, &vectorio_rectangle_type)) { ishape.shape = shape; + #if !CIRCUITPY_DISPLAYIO_LVGL ishape.get_area = &common_hal_vectorio_rectangle_get_area; + #endif ishape.get_pixel = &common_hal_vectorio_rectangle_get_pixel; } else if (mp_obj_is_type(shape, &vectorio_circle_type)) { ishape.shape = shape; + #if !CIRCUITPY_DISPLAYIO_LVGL ishape.get_area = &common_hal_vectorio_circle_get_area; + #endif ishape.get_pixel = &common_hal_vectorio_circle_get_pixel; } else { mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_shape); @@ -76,13 +107,17 @@ mp_obj_t vectorio_vector_shape_make_new(const mp_obj_t shape, const mp_obj_t pix } vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl = { + .draw_set_dirty = (draw_set_dirty_fun)common_hal_vectorio_vector_shape_set_dirty, + .draw_set_in_group = (draw_set_in_group_fun)vectorio_vector_shape_set_in_group, + #if !CIRCUITPY_DISPLAYIO_LVGL + // The scanline compositor asks shapes to fill spans and to track dirty areas. LVGL + // draws and invalidates the shape's mirror object instead, so none of this exists. .draw_fill_area = (draw_fill_area_fun)vectorio_vector_shape_fill_area, .draw_get_dirty_area = (draw_get_dirty_area_fun)vectorio_vector_shape_get_dirty_area, .draw_update_transform = (draw_update_transform_fun)vectorio_vector_shape_update_transform, .draw_finish_refresh = (draw_finish_refresh_fun)vectorio_vector_shape_finish_refresh, .draw_get_refresh_areas = (draw_get_refresh_areas_fun)vectorio_vector_shape_get_refresh_areas, - .draw_set_dirty = (draw_set_dirty_fun)common_hal_vectorio_vector_shape_set_dirty, - .draw_set_in_group = (draw_set_in_group_fun)vectorio_vector_shape_set_in_group, + #endif }; // Stub checker does not approve of these shared properties. @@ -91,9 +126,7 @@ vectorio_draw_protocol_impl_t vectorio_vector_shape_draw_protocol_impl = { // """true if x,y lies inside the shape.""" // static mp_obj_t vectorio_vector_shape_obj_contains(mp_obj_t wrapper_shape, mp_obj_t x_obj, mp_obj_t y_obj) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); mp_int_t x = mp_obj_get_int(x_obj); mp_int_t y = mp_obj_get_int(y_obj); @@ -106,18 +139,14 @@ MP_DEFINE_CONST_FUN_OBJ_3(vectorio_vector_shape_contains_obj, vectorio_vector_sh // """X position of the center point of the shape in the parent.""" // static mp_obj_t vectorio_vector_shape_obj_get_x(mp_obj_t wrapper_shape) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_x(self)); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_x_obj, vectorio_vector_shape_obj_get_x); static mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t x_obj) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); mp_int_t x = mp_obj_get_int(x_obj); common_hal_vectorio_vector_shape_set_x(self, x); @@ -134,18 +163,14 @@ MP_PROPERTY_GETSET(vectorio_vector_shape_x_obj, // """Y position of the center point of the shape in the parent.""" // static mp_obj_t vectorio_vector_shape_obj_get_y(mp_obj_t wrapper_shape) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); return MP_OBJ_NEW_SMALL_INT(common_hal_vectorio_vector_shape_get_y(self)); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_y_obj, vectorio_vector_shape_obj_get_y); static mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t y_obj) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); mp_int_t y = mp_obj_get_int(y_obj); common_hal_vectorio_vector_shape_set_y(self, y); @@ -162,18 +187,14 @@ MP_PROPERTY_GETSET(vectorio_vector_shape_y_obj, // """location of the center point of the shape in the parent.""" // static mp_obj_t vectorio_vector_shape_obj_get_location(mp_obj_t wrapper_shape) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); return MP_OBJ_TO_PTR(common_hal_vectorio_vector_shape_get_location(self)); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_location_obj, vectorio_vector_shape_obj_get_location); static mp_obj_t vectorio_vector_shape_obj_set_location(mp_obj_t wrapper_shape, mp_obj_t location_obj) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); common_hal_vectorio_vector_shape_set_location(self, location_obj); return mp_const_none; @@ -190,17 +211,13 @@ MP_PROPERTY_GETSET(vectorio_vector_shape_location_obj, // """Hide the shape or not.""" // static mp_obj_t vectorio_vector_shape_obj_get_hidden(mp_obj_t wrapper_shape) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); return mp_obj_new_bool(common_hal_vectorio_vector_shape_get_hidden(self)); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_hidden_obj, vectorio_vector_shape_obj_get_hidden); static mp_obj_t vectorio_vector_shape_obj_set_hidden(mp_obj_t wrapper_shape, mp_obj_t hidden_obj) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); common_hal_vectorio_vector_shape_set_hidden(self, mp_obj_is_true(hidden_obj)); return mp_const_none; @@ -216,18 +233,14 @@ MP_PROPERTY_GETSET(vectorio_vector_shape_hidden_obj, // """The pixel shader of the shape.""" // static mp_obj_t vectorio_vector_shape_obj_get_pixel_shader(mp_obj_t wrapper_shape) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); return common_hal_vectorio_vector_shape_get_pixel_shader(self); } MP_DEFINE_CONST_FUN_OBJ_1(vectorio_vector_shape_get_pixel_shader_obj, vectorio_vector_shape_obj_get_pixel_shader); static mp_obj_t vectorio_vector_shape_obj_set_pixel_shader(mp_obj_t wrapper_shape, mp_obj_t pixel_shader) { - // Relies on the fact that only vector_shape impl gets matched with a VectorShape. - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, wrapper_shape); - vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape)); + vectorio_vector_shape_t *self = _wrapper_shape(wrapper_shape); if (!mp_obj_is_type(pixel_shader, &displayio_palette_type) && !mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) { mp_raise_TypeError_varg(MP_ERROR_TEXT("unsupported %q type"), MP_QSTR_pixel_shader); @@ -252,5 +265,6 @@ MP_DEFINE_CONST_OBJ_TYPE( vectorio_vector_shape_type, MP_QSTR_VectorShape, MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, - locals_dict, &vectorio_vector_shape_locals_dict + locals_dict, &vectorio_vector_shape_locals_dict, + protocol, VECTORIO_VECTOR_SHAPE_LVGL_PROTOCOL ); diff --git a/shared-bindings/videocore/VideoBuffer.c b/shared-bindings/videocore/VideoBuffer.c index 20e7aa0391e..08eb830982c 100644 --- a/shared-bindings/videocore/VideoBuffer.c +++ b/shared-bindings/videocore/VideoBuffer.c @@ -24,14 +24,23 @@ //| Add a VideoBuffer to a `displayio.Group` to show it on a display. A //| framebuffer target only streams while it is shown. //| +//| Displayio composites the video with the other layers in its Group. On +//| displays that use the LVGL compositor (for example ESP32-P4), LVGL +//| samples the target buffer directly, so no copy is made per frame and +//| widgets can be drawn over a live framebuffer target. On the original +//| scanline compositor a plain buffer is copied into the display instead, +//| and its pixel format must match the display exactly. +//| //| Implementations may restrict size and position. For example, on //| ESP32-P4 a framebuffer target must be full width because the DMA can't //| skip between rows without an intermediate buffer. Setting an //| unsupported position directly raises an error, but moving a containing //| Group may give garbled output instead. //| -//| Assigning a Group that contains a framebuffer target as a rotated -//| display's ``root_group`` raises `ValueError`. +//| A framebuffer target is only shown on the display that wraps that +//| framebuffer, and only while that display is unrotated. Assigning a +//| Group that contains one as a rotated display's ``root_group`` raises +//| `ValueError`. //| //| When the target is a plain buffer, frames are written with no gap //| between rows, so each row takes ``width * 2`` bytes for @@ -46,6 +55,8 @@ //| On a tricolor display with separate black and color RAM, a 1 bit video //| only draws black and white, and a highlight colored layer below it shows //| through. Other tricolor displays don't draw it. +//| A framebuffer target may use 16, 24 or 32 bits per pixel with the LVGL +//| compositor; the scanline compositor only supports 16-bit pixels. //| //| See the `videocore` module overview for complete examples. //| """ @@ -115,7 +126,8 @@ static mp_obj_t videocore_videobuffer_make_new(const mp_obj_type_t *type, size_t //| the framebuffer. //| //| When the target is a plain buffer, it must be large enough for a -//| whole frame. +//| whole frame. The LVGL compositor samples the buffer, so it must be +//| 16, 24 or 32 bits per pixel there. //| //| :param ~circuitpython_typing.VideoSource source: The video source to //| play, such as `mipicsi.Camera` or `videogif.GifDecoder` @@ -265,5 +277,5 @@ MP_DEFINE_CONST_OBJ_TYPE( MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS, make_new, videocore_videobuffer_make_new, locals_dict, &videocore_videobuffer_locals_dict, - protocol, &videocore_videobuffer_draw_protocol + protocol, VIDEOCORE_VIDEOBUFFER_PROTOCOL ); diff --git a/shared-bindings/videocore/__init__.c b/shared-bindings/videocore/__init__.c index ae142e4e693..2de780cc3da 100644 --- a/shared-bindings/videocore/__init__.c +++ b/shared-bindings/videocore/__init__.c @@ -47,9 +47,9 @@ //| display.root_group = root //| //| Capture a smaller part of the sensor's image into a `bytearray` and let -//| displayio place it in a Group, together with other layers. displayio bulk -//| copies new frames into the group's area of the display (no color -//| conversion): +//| displayio place it in a Group, together with other layers. The scanline +//| compositor bulk copies new frames into the group's area of the display (no +//| color conversion); the LVGL compositor samples the buffer directly: //| //| .. code-block:: python //| diff --git a/shared-module/_stage/__init__.c b/shared-module/_stage/__init__.c index c23d0567345..f99cb6156c7 100644 --- a/shared-module/_stage/__init__.c +++ b/shared-module/_stage/__init__.c @@ -26,7 +26,7 @@ void render_stage( area.y1 = y0 * scale; area.x2 = x1 * scale; area.y2 = y1 * scale; - displayio_display_bus_set_region_to_update(&display->bus, &display->core, &area); + displayio_display_bus_set_region_to_update(&display->bus, &display->core.colorspace, &area); while (!displayio_display_bus_begin_transaction(&display->bus)) { RUN_BACKGROUND_TASKS; diff --git a/shared-module/bitmaptools/__init__.c b/shared-module/bitmaptools/__init__.c index b9d2e25f966..0116b6810aa 100644 --- a/shared-module/bitmaptools/__init__.c +++ b/shared-module/bitmaptools/__init__.c @@ -322,7 +322,7 @@ void common_hal_bitmaptools_rotozoom(displayio_bitmap_t *self, int16_t ox, int16 mp_float_t rowu = startu + miny * duCol; mp_float_t rowv = startv + miny * dvCol; - displayio_area_t dirty_area = {minx, miny, maxx + 1, maxy + 1, NULL}; + displayio_area_t dirty_area = {minx, miny, maxx + 1, maxy + 1 }; displayio_bitmap_set_dirty_area(self, &dirty_area); #if CIRCUITPY_BITMAPTOOLS_FAST_ROTOZOOM @@ -374,10 +374,10 @@ void common_hal_bitmaptools_fill_region(displayio_bitmap_t *destination, // // input checks should ensure that x1 < x2 and y1 < y2 and are within the bitmap region - displayio_area_t area = { x1, y1, x2, y2, NULL }; + displayio_area_t area = { x1, y1, x2, y2 }; displayio_area_canon(&area); - displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; displayio_area_compute_overlap(&area, &bitmap_area, &area); // update the dirty rectangle @@ -529,7 +529,7 @@ void common_hal_bitmaptools_boundary_fill(displayio_bitmap_t *destination, } // set dirty the area so displayio will draw - displayio_area_t area = { minx, miny, maxx + 1, maxy + 1, NULL}; + displayio_area_t area = { minx, miny, maxx + 1, maxy + 1 }; displayio_bitmap_set_dirty_area(destination, &area); } @@ -637,8 +637,8 @@ void common_hal_bitmaptools_draw_line(displayio_bitmap_t *destination, ybb0 = y1; ybb1 = y0 + 1; } - displayio_area_t area = { xbb0, ybb0, xbb1, ybb1, NULL }; - displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; + displayio_area_t area = { xbb0, ybb0, xbb1, ybb1 }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; displayio_area_compute_overlap(&area, &bitmap_area, &area); displayio_bitmap_set_dirty_area(destination, &area); @@ -684,8 +684,8 @@ void common_hal_bitmaptools_draw_polygon(displayio_bitmap_t *destination, void * draw_line(destination, xprev, yprev, x0, y0, value); } - displayio_area_t area = { xmin, ymin, xmax + 1, ymax + 1, NULL }; - displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; + displayio_area_t area = { xmin, ymin, xmax + 1, ymax + 1 }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; displayio_area_compute_overlap(&area, &bitmap_area, &area); displayio_bitmap_set_dirty_area(destination, &area); } @@ -716,7 +716,7 @@ void common_hal_bitmaptools_arrayblit(displayio_bitmap_t *self, void *data, int } } } - displayio_area_t area = { x1, y1, x2, y2, NULL }; + displayio_area_t area = { x1, y1, x2, y2 }; displayio_bitmap_set_dirty_area(self, &area); } @@ -725,7 +725,7 @@ void common_hal_bitmaptools_readinto(displayio_bitmap_t *self, mp_obj_t *file, i mp_get_stream_raise(file, MP_STREAM_OP_READ); - displayio_area_t a = {0, 0, self->width, self->height, NULL}; + displayio_area_t a = {0, 0, self->width, self->height }; displayio_bitmap_set_dirty_area(self, &a); size_t elements_per_row = (self->width * bits_per_pixel + element_size * 8 - 1) / (element_size * 8); @@ -1001,14 +1001,14 @@ void common_hal_bitmaptools_dither(displayio_bitmap_t *dest_bitmap, displayio_bi fill_row(source_bitmap, swap, rows[2], y + 3, info->mx); } - displayio_area_t a = { 0, 0, width, height, NULL }; + displayio_area_t a = { 0, 0, width, height }; displayio_bitmap_set_dirty_area(dest_bitmap, &a); } void common_hal_bitmaptools_alphablend(displayio_bitmap_t *dest, displayio_bitmap_t *source1, displayio_bitmap_t *source2, displayio_colorspace_t colorspace, mp_float_t factor1, mp_float_t factor2, bitmaptools_blendmode_t blendmode, uint32_t skip_source1_index, bool skip_source1_index_none, uint32_t skip_source2_index, bool skip_source2_index_none, displayio_bitmap_t *mask) { - displayio_area_t a = {0, 0, dest->width, dest->height, NULL}; + displayio_area_t a = {0, 0, dest->width, dest->height }; displayio_bitmap_set_dirty_area(dest, &a); int ifactor1_base = (int)(factor1 * 256); @@ -1220,8 +1220,8 @@ void common_hal_bitmaptools_draw_circle(displayio_bitmap_t *destination, ybb0 = y - radius; ybb1 = y + radius + 1; - displayio_area_t area = { xbb0, ybb0, xbb1, ybb1, NULL }; - displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height, NULL }; + displayio_area_t area = { xbb0, ybb0, xbb1, ybb1 }; + displayio_area_t bitmap_area = { 0, 0, destination->width, destination->height }; displayio_area_compute_overlap(&area, &bitmap_area, &area); displayio_bitmap_set_dirty_area(destination, &area); @@ -1277,6 +1277,7 @@ static bool blit_rows_fast(displayio_bitmap_t *destination, displayio_bitmap_t * void common_hal_bitmaptools_blit(displayio_bitmap_t *destination, displayio_bitmap_t *source, int16_t x, int16_t y, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t skip_source_index, bool skip_source_index_none, uint32_t skip_dest_index, bool skip_dest_index_none) { + // ESP_LOGI(TAG, "blit start"); if (destination->read_only) { mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); @@ -1296,7 +1297,7 @@ void common_hal_bitmaptools_blit(displayio_bitmap_t *destination, displayio_bitm dirty_y_max = destination->height; } - displayio_area_t a = { x, y, dirty_x_max, dirty_y_max, NULL}; + displayio_area_t a = { x, y, dirty_x_max, dirty_y_max }; displayio_bitmap_set_dirty_area(destination, &a); bool x_reverse = false; @@ -1312,6 +1313,7 @@ void common_hal_bitmaptools_blit(displayio_bitmap_t *destination, displayio_bitm if (skip_source_index_none && skip_dest_index_none && blit_rows_fast(destination, source, x, y, x1, y1, x2, y2)) { + // ESP_LOGI(TAG, "blit_rows_fast succeeded"); return; } @@ -1345,4 +1347,5 @@ void common_hal_bitmaptools_blit(displayio_bitmap_t *destination, displayio_bitm } } } + // ESP_LOGI(TAG, "blit end"); } diff --git a/shared-module/busdisplay/BusDisplay.h b/shared-module/busdisplay/BusDisplay.h index 87e09a821e5..e7f5d9d5a5c 100644 --- a/shared-module/busdisplay/BusDisplay.h +++ b/shared-module/busdisplay/BusDisplay.h @@ -7,37 +7,30 @@ #pragma once #include "shared-bindings/digitalio/DigitalInOut.h" -#include "shared-bindings/displayio/Group.h" #if CIRCUITPY_PWMIO #include "shared-bindings/pwmio/PWMOut.h" #endif -#include "shared-module/displayio/area.h" -#include "shared-module/displayio/bus_core.h" -#include "shared-module/displayio/display_core.h" +// Backlight state shared by both BusDisplay backends. +typedef union { + digitalio_digitalinout_obj_t inout; + #if CIRCUITPY_PWMIO + pwmio_pwmout_obj_t pwm; + #endif +} busdisplay_backlight_t; -typedef struct { - mp_obj_base_t base; - displayio_display_core_t core; - displayio_display_bus_t bus; - union { - digitalio_digitalinout_obj_t backlight_inout; - #if CIRCUITPY_PWMIO - pwmio_pwmout_obj_t backlight_pwm; - #endif - }; - uint64_t last_refresh_call; - mp_float_t current_brightness; - uint16_t brightness_command; - uint16_t native_frames_per_second; - uint16_t native_ms_per_frame; - uint8_t write_ram_command; - bool auto_refresh; - bool first_manual_refresh; - bool backlight_on_high; -} busdisplay_busdisplay_obj_t; +void busdisplay_backlight_construct(busdisplay_backlight_t *self, const mcu_pin_obj_t *pin, + uint16_t pwm_frequency); +bool busdisplay_backlight_set(busdisplay_backlight_t *self, mp_float_t brightness); +void busdisplay_backlight_deinit(busdisplay_backlight_t *self); -void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self); -void release_busdisplay(busdisplay_busdisplay_obj_t *self); -void reset_busdisplay(busdisplay_busdisplay_obj_t *self); -void busdisplay_busdisplay_collect_ptrs(busdisplay_busdisplay_obj_t *self); +// The BusDisplay implementation has two backends selected at build time with +// CIRCUITPY_DISPLAYIO_LVGL: +// +// legacy/ - the original scanline compositor, built on display_core/bus_core. +// lvgl/ - an LVGL display with its own draw buffers and flush callback. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/busdisplay/lvgl/BusDisplay.h" +#else +#include "shared-module/busdisplay/legacy/BusDisplay.h" +#endif diff --git a/shared-module/busdisplay/__init__.c b/shared-module/busdisplay/__init__.c index 6283dbe0c6a..6caf4de9d9e 100644 --- a/shared-module/busdisplay/__init__.c +++ b/shared-module/busdisplay/__init__.c @@ -4,4 +4,53 @@ // // SPDX-License-Identifier: MIT -// Nothing now. +#include "shared-module/busdisplay/BusDisplay.h" + +#include "shared-bindings/microcontroller/Pin.h" + +void busdisplay_backlight_construct(busdisplay_backlight_t *self, const mcu_pin_obj_t *pin, + uint16_t pwm_frequency) { + // Always set the backlight type in case we're reusing memory. + self->inout.base.type = &mp_type_NoneType; + if (pin == NULL || !common_hal_mcu_pin_is_free(pin)) { + return; + } + #if CIRCUITPY_PWMIO + pwmout_result_t result = common_hal_pwmio_pwmout_construct(&self->pwm, pin, 0, pwm_frequency, false); + if (result != PWMOUT_OK) { + self->inout.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->inout, pin); + } else { + self->pwm.base.type = &pwmio_pwmout_type; + } + #else + self->inout.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->inout, pin); + #endif +} + +bool busdisplay_backlight_set(busdisplay_backlight_t *self, mp_float_t brightness) { + #if CIRCUITPY_PWMIO + if (self->pwm.base.type == &pwmio_pwmout_type) { + common_hal_pwmio_pwmout_set_duty_cycle(&self->pwm, (uint16_t)(0xffff * brightness)); + return true; + } + #endif + if (self->inout.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_set_value(&self->inout, brightness > 0.99); + return true; + } + return false; +} + +void busdisplay_backlight_deinit(busdisplay_backlight_t *self) { + #if CIRCUITPY_PWMIO + if (self->pwm.base.type == &pwmio_pwmout_type) { + common_hal_pwmio_pwmout_deinit(&self->pwm); + return; + } + #endif + if (self->inout.base.type == &digitalio_digitalinout_type) { + common_hal_digitalio_digitalinout_deinit(&self->inout); + } +} diff --git a/shared-module/busdisplay/BusDisplay.c b/shared-module/busdisplay/legacy/BusDisplay.c similarity index 82% rename from shared-module/busdisplay/BusDisplay.c rename to shared-module/busdisplay/legacy/BusDisplay.c index 1a1ccfe0d63..330db3b6cf6 100644 --- a/shared-module/busdisplay/BusDisplay.c +++ b/shared-module/busdisplay/legacy/BusDisplay.c @@ -36,7 +36,6 @@ #include #include -#define DELAY 0x80 void common_hal_busdisplay_busdisplay_construct(busdisplay_busdisplay_obj_t *self, mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, @@ -70,59 +69,9 @@ void common_hal_busdisplay_busdisplay_construct(busdisplay_busdisplay_obj_t *sel self->native_frames_per_second = native_frames_per_second; self->native_ms_per_frame = 1000 / native_frames_per_second; - uint32_t i = 0; - while (i < init_sequence_len) { - uint8_t *cmd = init_sequence + i; - uint8_t data_size = *(cmd + 1); - bool delay = (data_size & DELAY) != 0; - data_size &= ~DELAY; - uint8_t *data = cmd + 2; - while (!displayio_display_bus_begin_transaction(&self->bus)) { - RUN_BACKGROUND_TASKS; - if (mp_hal_is_interrupted()) { - mp_raise_RuntimeError_varg(MP_ERROR_TEXT("%q init failed"), MP_QSTR_display); - } - } - if (self->bus.data_as_commands) { - uint8_t full_command[data_size + 1]; - full_command[0] = cmd[0]; - memcpy(full_command + 1, data, data_size); - self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, full_command, data_size + 1); - } else { - self->bus.send(self->bus.bus, DISPLAY_COMMAND, data_size > 0 ? CHIP_SELECT_TOGGLE_EVERY_BYTE : CHIP_SELECT_UNTOUCHED, cmd, 1); - self->bus.send(self->bus.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, data, data_size); - } - displayio_display_bus_end_transaction(&self->bus); - uint16_t delay_length_ms = 10; - if (delay) { - data_size++; - delay_length_ms = *(cmd + 1 + data_size); - if (delay_length_ms == 255) { - delay_length_ms = 500; - } - } - common_hal_time_delay_ms(delay_length_ms); - i += 2 + data_size; - } + displayio_display_bus_send_init_sequence(&self->bus, init_sequence, init_sequence_len); - // Always set the backlight type in case we're reusing memory. - self->backlight_inout.base.type = &mp_type_NoneType; - if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) { - // Avoid PWM types and functions when the module isn't enabled - #if (CIRCUITPY_PWMIO) - pwmout_result_t result = common_hal_pwmio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, backlight_pwm_frequency, false); - if (result != PWMOUT_OK) { - self->backlight_inout.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin); - } else { - self->backlight_pwm.base.type = &pwmio_pwmout_type; - } - #else - // Otherwise default to digital - self->backlight_inout.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin); - #endif - } + busdisplay_backlight_construct(&self->backlight, backlight_pin, backlight_pwm_frequency); common_hal_busdisplay_busdisplay_set_brightness(self, brightness); @@ -150,26 +99,9 @@ bool common_hal_busdisplay_busdisplay_set_brightness(busdisplay_busdisplay_obj_t if (!self->backlight_on_high) { brightness = 1.0 - brightness; } - bool ok = false; + bool ok = busdisplay_backlight_set(&self->backlight, brightness); - // Avoid PWM types and functions when the module isn't enabled - #if (CIRCUITPY_PWMIO) - bool ispwm = (self->backlight_pwm.base.type == &pwmio_pwmout_type) ? true : false; - #else - bool ispwm = false; - #endif - - if (ispwm) { - #if (CIRCUITPY_PWMIO) - common_hal_pwmio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t)(0xffff * brightness)); - ok = true; - #else - ok = false; - #endif - } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { - common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99); - ok = true; - } else if (self->brightness_command != NO_BRIGHTNESS_COMMAND) { + if (!ok && self->brightness_command != NO_BRIGHTNESS_COMMAND) { ok = displayio_display_bus_begin_transaction(&self->bus); if (ok) { if (self->bus.data_as_commands) { @@ -333,7 +265,7 @@ static bool _refresh_area(busdisplay_busdisplay_obj_t *self, const displayio_are _run_background_tasks(); } - displayio_display_bus_set_region_to_update(&self->bus, &self->core, &subrectangle); + displayio_display_bus_set_region_to_update(&self->bus, &self->core.colorspace, &subrectangle); // Can't acquire display bus; skip the rest of the data. if (!displayio_display_bus_begin_transaction(&self->bus)) { @@ -462,15 +394,7 @@ void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self) { void release_busdisplay(busdisplay_busdisplay_obj_t *self) { common_hal_busdisplay_busdisplay_set_auto_refresh(self, false); release_display_core(&self->core); - #if (CIRCUITPY_PWMIO) - if (self->backlight_pwm.base.type == &pwmio_pwmout_type) { - common_hal_pwmio_pwmout_deinit(&self->backlight_pwm); - } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { - common_hal_digitalio_digitalinout_deinit(&self->backlight_inout); - } - #else - common_hal_digitalio_digitalinout_deinit(&self->backlight_inout); - #endif + busdisplay_backlight_deinit(&self->backlight); } void reset_busdisplay(busdisplay_busdisplay_obj_t *self) { diff --git a/shared-module/busdisplay/legacy/BusDisplay.h b/shared-module/busdisplay/legacy/BusDisplay.h new file mode 100644 index 00000000000..a268bc5cb05 --- /dev/null +++ b/shared-module/busdisplay/legacy/BusDisplay.h @@ -0,0 +1,38 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/displayio/Group.h" +#if CIRCUITPY_PWMIO +#include "shared-bindings/pwmio/PWMOut.h" +#endif + +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/bus_core.h" +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + displayio_display_bus_t bus; + busdisplay_backlight_t backlight; + uint64_t last_refresh_call; + mp_float_t current_brightness; + uint16_t brightness_command; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + uint8_t write_ram_command; + bool auto_refresh; + bool first_manual_refresh; + bool backlight_on_high; +} busdisplay_busdisplay_obj_t; + +void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self); +void release_busdisplay(busdisplay_busdisplay_obj_t *self); +void reset_busdisplay(busdisplay_busdisplay_obj_t *self); +void busdisplay_busdisplay_collect_ptrs(busdisplay_busdisplay_obj_t *self); diff --git a/shared-module/busdisplay/lvgl/BusDisplay.c b/shared-module/busdisplay/lvgl/BusDisplay.c new file mode 100644 index 00000000000..76e33ff79f1 --- /dev/null +++ b/shared-module/busdisplay/lvgl/BusDisplay.c @@ -0,0 +1,450 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// LVGL BusDisplay implementation for fourwire/SPI displays +// Direct render via LVGL partial buffers, SPI flush +#include "shared-bindings/busdisplay/BusDisplay.h" +#include "shared-bindings/fourwire/FourWire.h" +#include "shared-bindings/displayio/Group.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "supervisor/port.h" +#include "supervisor/port_heap.h" +#include "supervisor/shared/display.h" +#include "py/runtime.h" +#include "py/gc.h" +#include +#include "lvgl.h" +#include "lvgl/draw/lv_draw_utils.h" + +// Port allocations must be aligned enough for LVGL draw buffers and the PPA. +MP_STATIC_ASSERT(MICROPY_BYTES_PER_GC_BLOCK % LV_DRAW_BUF_ALIGN == 0); + +#define LVGL_DRAW_BUF_LINES 10 + +static void busdisplay_lvgl_flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map) { + busdisplay_busdisplay_obj_t *self = lv_display_get_user_data(disp); + lv_display_rotation_t rotation = lv_display_get_rotation(disp); + lv_area_t rotated_area; + if (rotation != LV_DISPLAY_ROTATION_0) { + int32_t width = lv_area_get_width(area); + int32_t height = lv_area_get_height(area); + rotated_area = *area; + lv_display_rotate_area(disp, &rotated_area); + uint32_t src_stride = lv_draw_buf_width_to_stride(width, self->lv_color_format); + uint32_t dest_stride = lv_draw_buf_width_to_stride( + lv_area_get_width(&rotated_area), self->lv_color_format); + if ((size_t)dest_stride * lv_area_get_height(&rotated_area) > + self->lv_rotation_buf_size) { + lv_display_flush_ready(disp); + return; + } + lv_color_format_t rotate_format = self->lv_color_format; + if (rotate_format == LV_COLOR_FORMAT_RGB565_SWAPPED) { + rotate_format = LV_COLOR_FORMAT_RGB565; + } + lv_draw_rotate(px_map, self->lv_rotation_buf, width, height, + src_stride, dest_stride, rotation, rotate_format); + area = &rotated_area; + px_map = self->lv_rotation_buf; + } + uint16_t w = area->x2 - area->x1 + 1; + uint16_t h = area->y2 - area->y1 + 1; + size_t len = (size_t)w * h * ((self->color_depth + 7) / 8); + if (self->color_depth == 1) { + len = (w * h + 7) / 8; + if (self->bytes_per_cell > 1) { + len *= self->bytes_per_cell; + } + } + + if (!displayio_display_bus_is_free(&self->bus)) { + // Don't run background tasks from here: on ports that render LVGL on a task, this flush + // would otherwise execute background callbacks (including the BusDisplay proxy) from + // inside LVGL's render. Block instead and let the bus owner run. + while (!displayio_display_bus_begin_transaction(&self->bus)) { + port_task_sleep_ms(1); + } + displayio_display_bus_end_transaction(&self->bus); + } + if (self->bus.bus == mp_const_none) { + lv_display_flush_ready(disp); + return; + } + + displayio_area_t displayio_area = { + .x1 = area->x1, + .y1 = area->y1, + .x2 = area->x2 + 1, + .y2 = area->y2 + 1, + }; + displayio_display_bus_set_region_to_update(&self->bus, &self->colorspace, &displayio_area); + + while (!displayio_display_bus_begin_transaction(&self->bus)) { + port_task_sleep_ms(1); + } + if (!self->bus.data_as_commands) { + uint8_t command = self->write_ram_command; + self->bus.send(self->bus.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &command, 1); + } + self->bus.send(self->bus.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, px_map, len); + displayio_display_bus_end_transaction(&self->bus); + + lv_display_flush_ready(disp); +} + +static lv_color_format_t busdisplay_lvgl_color_format(busdisplay_busdisplay_obj_t *self) { + switch (self->color_depth) { + case 16: + return LV_COLOR_FORMAT_RGB565; + case 24: + return LV_COLOR_FORMAT_RGB888; + case 32: + return LV_COLOR_FORMAT_ARGB8888; + case 8: + return LV_COLOR_FORMAT_L8; + case 1: + return LV_COLOR_FORMAT_I1; + default: + return LV_COLOR_FORMAT_RGB565; + } +} + +typedef struct { + nlr_jump_callback_node_t node; + busdisplay_busdisplay_obj_t *self; + bool locked; +} busdisplay_construct_cleanup_t; + +static void busdisplay_construct_abort(void *ctx) { + busdisplay_construct_cleanup_t *cleanup = ctx; + busdisplay_busdisplay_obj_t *self = cleanup->self; + if (!cleanup->locked) { + displayio_lvgl_lock(); + } + if (self->root_group != NULL) { + displayio_lvgl_set_root_group(NULL); + self->root_group = NULL; + } + if (self->lv_display != NULL) { + lv_display_delete(self->lv_display); + self->lv_display = NULL; + } + port_free(self->lv_buf1); + port_free(self->lv_buf2); + port_free(self->lv_rotation_buf); + self->lv_buf1 = NULL; + self->lv_buf2 = NULL; + self->lv_rotation_buf = NULL; + self->lv_buf_size = 0; + self->lv_rotation_buf_size = 0; + busdisplay_backlight_deinit(&self->backlight); + self->deinited = true; + self->base.type = &mp_type_NoneType; + displayio_lvgl_unlock(); +} + +void common_hal_busdisplay_busdisplay_construct(busdisplay_busdisplay_obj_t *self, + mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, + uint16_t rotation, uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, + uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word, uint8_t set_column_command, + uint8_t set_row_command, uint8_t write_ram_command, + uint8_t *init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t *backlight_pin, + uint16_t brightness_command, mp_float_t brightness, + bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second, + bool backlight_on_high, bool SH1107_addressing, uint16_t backlight_pwm_frequency) { + + self->lv_display = NULL; + self->lv_buf1 = NULL; + self->lv_buf2 = NULL; + self->lv_rotation_buf = NULL; + self->lv_buf_size = 0; + self->lv_rotation_buf_size = 0; + self->root_group = NULL; + self->backlight.inout.base.type = &mp_type_NoneType; + self->auto_refresh = false; + busdisplay_construct_cleanup_t cleanup = { .self = self, .locked = false }; + nlr_push_jump_callback(&cleanup.node, busdisplay_construct_abort); + // Packed pixels require panel-specific packing and LVGL's indexed palette + // header must not be sent to the panel as pixels. + if (color_depth < 8) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("Format not supported")); + } + + uint16_t ram_width = 0x100; + uint16_t ram_height = 0x100; + if (single_byte_bounds) { + ram_width = 0xff; + ram_height = 0xff; + } + displayio_display_bus_construct(&self->bus, bus, ram_width, ram_height, colstart, rowstart, + set_column_command, set_row_command, NO_COMMAND, NO_COMMAND, data_as_commands, + false /* always_toggle_chip_select */, SH1107_addressing && color_depth == 1, + false /* address_little_endian */); + self->width = width; + self->height = height; + self->rotation = 0; + self->color_depth = color_depth; + self->grayscale = grayscale; + self->pixels_in_byte_share_row = pixels_in_byte_share_row; + self->bytes_per_cell = bytes_per_cell; + self->reverse_pixels_in_byte = reverse_pixels_in_byte; + self->reverse_bytes_in_word = reverse_bytes_in_word; + self->colorspace.depth = color_depth; + self->colorspace.grayscale = grayscale; + self->colorspace.grayscale_bit = 8 - color_depth; + self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row; + self->colorspace.bytes_per_cell = bytes_per_cell; + self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte; + self->colorspace.reverse_bytes_in_word = reverse_bytes_in_word; + self->write_ram_command = write_ram_command; + self->brightness_command = brightness_command; + self->backlight_on_high = backlight_on_high; + self->auto_refresh = false; + self->first_manual_refresh = !auto_refresh; + self->native_frames_per_second = native_frames_per_second; + self->native_ms_per_frame = 1000 / (native_frames_per_second ? native_frames_per_second : 60); + self->current_brightness = brightness; + self->deinited = false; + self->root_group = NULL; + busdisplay_backlight_construct(&self->backlight, backlight_pin, backlight_pwm_frequency); + common_hal_busdisplay_busdisplay_set_brightness(self, brightness); + + displayio_display_bus_send_init_sequence(&self->bus, init_sequence, init_sequence_len); + + displayio_lvgl_port_init(); + // Hold the render lock across the whole setup so the render task can't see a half-configured + // display. The port lock is recursive. + displayio_lvgl_lock(); + cleanup.locked = true; + + // Create LVGL display + self->lv_display = lv_display_create(width, height); + if (self->lv_display == NULL) { + mp_raise_RuntimeError(MP_ERROR_TEXT("LVGL display create failed")); + } + lv_display_set_user_data(self->lv_display, self); + self->lv_color_format = busdisplay_lvgl_color_format(self); + lv_display_set_color_format(self->lv_display, self->lv_color_format); + + // Allocate draw buffers via port_malloc. Displays outlive the VM, so these + // can't come from the garbage collected heap. + size_t bpp = (color_depth + 7) / 8; + if (bpp == 0) { + bpp = 1; + } + size_t buf_lines = LVGL_DRAW_BUF_LINES; + if (buf_lines > height) { + buf_lines = height; + } + size_t buf_size = width * buf_lines * bpp; + // Align to 4 bytes + buf_size = (buf_size + 3) & ~3; + // Ports with LVGL keep port_malloc() allocations aligned for the PPA and LVGL draw + // buffers. + self->lv_buf1 = port_malloc(buf_size, false); + self->lv_buf2 = port_malloc(buf_size, false); + self->lv_rotation_buf = port_malloc(buf_size, false); + if (self->lv_buf1 == NULL || self->lv_buf2 == NULL || self->lv_rotation_buf == NULL) { + mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("LVGL buffer alloc failed")); + } + self->lv_buf_size = buf_size; + self->lv_rotation_buf_size = buf_size; + lv_display_set_buffers(self->lv_display, self->lv_buf1, self->lv_buf2, buf_size, LV_DISPLAY_RENDER_MODE_PARTIAL); + lv_display_set_flush_cb(self->lv_display, busdisplay_lvgl_flush_cb); + + // BusDisplay's flush drives the display bus and may run background tasks, so ports that + // render LVGL on a separate task route this display's refresh through a proxy that runs + // lv_refr_now() on the CircuitPython thread. Ports that render BusDisplay on their own LVGL + // thread (Zephyr) leave the refresh timer alone. + displayio_lvgl_port_install_busdisplay_refresh(self->lv_display); + + displayio_lvgl_init(); + + // Set initial root group to splash - make this display default first + self->root_group = &circuitpython_splash; + lv_display_set_default(self->lv_display); + displayio_lvgl_register_display(self->lv_display); + displayio_lvgl_set_root_group(&circuitpython_splash); + nlr_pop_jump_callback(false); + displayio_lvgl_unlock(); + + if (rotation != 0) { + common_hal_busdisplay_busdisplay_set_rotation(self, rotation); + } + + common_hal_busdisplay_busdisplay_set_auto_refresh(self, auto_refresh); +} + +// Stubs for remaining API - to be implemented +uint16_t common_hal_busdisplay_busdisplay_get_width(busdisplay_busdisplay_obj_t *self) { + return self->width; +} +uint16_t common_hal_busdisplay_busdisplay_get_height(busdisplay_busdisplay_obj_t *self) { + return self->height; +} +uint16_t common_hal_busdisplay_busdisplay_get_rotation(busdisplay_busdisplay_obj_t *self) { + return self->rotation; +} +void common_hal_busdisplay_busdisplay_set_rotation(busdisplay_busdisplay_obj_t *self, int rotation) { + rotation %= 360; + bool transposed = (self->rotation == 90 || self->rotation == 270); + bool will_transposed = (rotation == 90 || rotation == 270); + if (transposed != will_transposed) { + int tmp = self->width; + self->width = self->height; + self->height = tmp; + } + self->rotation = rotation; + + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + if (self->lv_display != NULL) { + // LVGL rotates the opposite way from displayio for the transposed orientations. + lv_display_rotation_t lv_rotation; + switch (rotation) { + case 90: + lv_rotation = LV_DISPLAY_ROTATION_270; + break; + case 270: + lv_rotation = LV_DISPLAY_ROTATION_90; + break; + case 180: + lv_rotation = LV_DISPLAY_ROTATION_180; + break; + default: + lv_rotation = LV_DISPLAY_ROTATION_0; + break; + } + lv_display_set_rotation(self->lv_display, lv_rotation); + lv_display_set_default(self->lv_display); + } + + if (self == &displays[0].display) { + supervisor_stop_terminal(); + supervisor_start_terminal(self->width, self->height); + } + if (self->root_group != NULL) { + displayio_lvgl_set_root_group(self->root_group); + } + lv_obj_t *screen = lv_screen_active(); + if (screen != NULL) { + lv_obj_invalidate(screen); + } + displayio_lvgl_unlock_with_cleanup(); +} +bool common_hal_busdisplay_busdisplay_get_auto_refresh(busdisplay_busdisplay_obj_t *self) { + return self->auto_refresh; +} +void common_hal_busdisplay_busdisplay_set_auto_refresh(busdisplay_busdisplay_obj_t *self, bool auto_refresh) { + self->auto_refresh = auto_refresh; +} +bool common_hal_busdisplay_busdisplay_refresh(busdisplay_busdisplay_obj_t *self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) { + if (self->lv_display) { + // The renderer refreshes on its own thread too, so this has to exclude it. + displayio_lvgl_lock(); + lv_refr_now(self->lv_display); + displayio_lvgl_unlock(); + } + return true; +} +mp_obj_t common_hal_busdisplay_busdisplay_get_bus(busdisplay_busdisplay_obj_t *self) { + return self->bus.bus; +} +mp_obj_t common_hal_busdisplay_busdisplay_get_root_group(busdisplay_busdisplay_obj_t *self) { + return self->root_group ? self->root_group : mp_const_none; +} +mp_obj_t common_hal_busdisplay_busdisplay_set_root_group(busdisplay_busdisplay_obj_t *self, displayio_group_t *root_group) { + self->root_group = root_group; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Group can only be on one display at a time. + if (root_group != NULL) { + lv_display_set_default(self->lv_display); + } + displayio_lvgl_set_root_group(root_group); + displayio_lvgl_unlock_with_cleanup(); + return mp_const_none; +} +mp_float_t common_hal_busdisplay_busdisplay_get_brightness(busdisplay_busdisplay_obj_t *self) { + return self->current_brightness; +} +bool common_hal_busdisplay_busdisplay_set_brightness(busdisplay_busdisplay_obj_t *self, mp_float_t brightness) { + if (!self->backlight_on_high) { + brightness = 1.0 - brightness; + } + bool ok = busdisplay_backlight_set(&self->backlight, brightness); + if (ok) { + self->current_brightness = brightness; + } + return ok; +} +bool common_hal_busdisplay_busdisplay_get_dither(busdisplay_busdisplay_obj_t *self) { + return false; +} +void common_hal_busdisplay_busdisplay_set_dither(busdisplay_busdisplay_obj_t *self, bool dither) { +} +void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self) { + // The port's LVGL render task owns the pump. Keeping this free of lv_timer_handler() + // guarantees LVGL only renders on its own tick, a frame boundary, or display.refresh(). + (void)self; +} +void release_busdisplay(busdisplay_busdisplay_obj_t *self) { + self->deinited = true; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + displayio_lvgl_set_root_group(NULL); + if (self->lv_display != NULL) { + lv_display_delete(self->lv_display); + self->lv_display = NULL; + } + displayio_lvgl_unlock_with_cleanup(); + // Draw buffers are allocated from the port heap, not LVGL's heap, so free them + // explicitly after the display that referenced them is gone. + port_free(self->lv_buf1); + port_free(self->lv_buf2); + port_free(self->lv_rotation_buf); + self->lv_buf1 = NULL; + self->lv_buf2 = NULL; + self->lv_rotation_buf = NULL; + self->lv_buf_size = 0; + self->lv_rotation_buf_size = 0; + self->root_group = NULL; + busdisplay_backlight_deinit(&self->backlight); + self->base.type = &mp_type_NoneType; +} +void reset_busdisplay(busdisplay_busdisplay_obj_t *self) { + common_hal_busdisplay_busdisplay_set_auto_refresh(self, true); + circuitpython_splash.x = 0; // reset position in case someone moved it. + circuitpython_splash.y = 0; + supervisor_start_terminal(self->width, self->height); + // Show the splash if it is free, otherwise nothing. Other groups are on the VM heap. + if (!circuitpython_splash.in_group) { + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + self->root_group = &circuitpython_splash; + displayio_lvgl_set_root_group(&circuitpython_splash); + if (self->lv_display != NULL) { + lv_refr_now(self->lv_display); + } + displayio_lvgl_unlock_with_cleanup(); + } else { + self->root_group = NULL; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + displayio_lvgl_set_root_group(NULL); + if (self->lv_display != NULL) { + lv_refr_now(self->lv_display); + } + displayio_lvgl_unlock_with_cleanup(); + } +} +void busdisplay_busdisplay_collect_ptrs(busdisplay_busdisplay_obj_t *self) { + gc_collect_ptr(self->root_group); + displayio_display_bus_collect_ptrs(&self->bus); +} diff --git a/shared-module/busdisplay/lvgl/BusDisplay.h b/shared-module/busdisplay/lvgl/BusDisplay.h new file mode 100644 index 00000000000..6f719b372ff --- /dev/null +++ b/shared-module/busdisplay/lvgl/BusDisplay.h @@ -0,0 +1,60 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "lvgl.h" + +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/displayio/Group.h" +#include "shared-module/displayio/bus_core.h" +#if CIRCUITPY_PWMIO +#include "shared-bindings/pwmio/PWMOut.h" +#endif + +// LVGL-backed BusDisplay. There is no display_core/bus_core; the display owns an +// lv_display_t and keeps the display bus as an mp_obj_t so the GC can see it. +typedef struct { + mp_obj_base_t base; + // Bus the display is attached to (fourwire, etc.). + displayio_display_bus_t bus; + // LVGL display handle - owns draw buffers and flush CB + lv_display_t *lv_display; + lv_color_format_t lv_color_format; + void *lv_buf1; + void *lv_buf2; + size_t lv_buf_size; + void *lv_rotation_buf; + size_t lv_rotation_buf_size; + displayio_group_t *root_group; + // Display geometry / commands retained for flush + uint16_t width; + uint16_t height; + uint16_t rotation; + uint16_t color_depth; + _displayio_colorspace_t colorspace; + bool grayscale; + bool pixels_in_byte_share_row; + uint8_t bytes_per_cell; + bool reverse_pixels_in_byte; + bool reverse_bytes_in_word; + uint8_t write_ram_command; + busdisplay_backlight_t backlight; + uint64_t last_refresh_call; + mp_float_t current_brightness; + uint16_t brightness_command; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + bool auto_refresh; + bool first_manual_refresh; + bool backlight_on_high; + bool deinited; +} busdisplay_busdisplay_obj_t; + +void busdisplay_busdisplay_background(busdisplay_busdisplay_obj_t *self); +void release_busdisplay(busdisplay_busdisplay_obj_t *self); +void reset_busdisplay(busdisplay_busdisplay_obj_t *self); +void busdisplay_busdisplay_collect_ptrs(busdisplay_busdisplay_obj_t *self); diff --git a/shared-module/displayio/Bitmap.h b/shared-module/displayio/Bitmap.h index a4a57587207..7c7edfb7f2d 100644 --- a/shared-module/displayio/Bitmap.h +++ b/shared-module/displayio/Bitmap.h @@ -10,24 +10,13 @@ #include #include "py/obj.h" -#include "shared-module/displayio/area.h" -typedef struct { - mp_obj_base_t base; - uint16_t width; - uint16_t height; - uint32_t *data; - uint16_t stride; // uint32_t's - uint8_t bits_per_value; - uint8_t x_shift; - size_t x_mask; - displayio_area_t dirty_area; - uint16_t bitmask; - bool read_only; - bool data_alloc; // did bitmap allocate data or someone else -} displayio_bitmap_t; +// The pixel storage format (uint32_t word rows, MSB-first sub-byte values) and the +// higher-level dirty API are shared by both compositing backends; the dirty-tracking +// fields and scanline refresh API differ. See legacy/Bitmap.h and lvgl/Bitmap.h. -void displayio_bitmap_finish_refresh(displayio_bitmap_t *self); -displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail); -void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area); -void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value); +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/Bitmap.h" +#else +#include "shared-module/displayio/legacy/Bitmap.h" +#endif diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h index d0a3bd353ec..fbff3ddd028 100644 --- a/shared-module/displayio/Group.h +++ b/shared-module/displayio/Group.h @@ -11,30 +11,24 @@ #include "py/obj.h" #include "py/objlist.h" -#include "shared-module/displayio/area.h" #include "shared-module/displayio/Palette.h" -typedef struct { - mp_obj_base_t base; - mp_obj_list_t *members; - displayio_buffer_transform_t absolute_transform; - displayio_area_t dirty_area; // Catch all for changed area - int16_t x; - int16_t y; - uint16_t scale; - bool item_removed : 1; - bool in_group : 1; - bool active : 1; - bool hidden : 1; - bool hidden_by_parent : 1; - bool readonly : 1; - uint8_t padding : 2; -} 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_set_hidden_by_parent(displayio_group_t *self, bool hidden); -bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t *area); -bool displayio_group_fill_area(displayio_group_t *group, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); -void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t *parent_transform); -void displayio_group_finish_refresh(displayio_group_t *self); -displayio_area_t *displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t *tail); +// The compositing core (Group and TileGrid) has two implementations, selected at build +// time with CIRCUITPY_DISPLAYIO_LVGL: +// +// legacy/ — the original scanline compositing core used by display_core.c and every +// BusDisplay/EPaperDisplay/FramebufferDisplay variant. +// lvgl/ — an implementation that mirrors Groups and TileGrids into an LVGL object tree +// which is rendered by LVGL. Groups are plain lv_obj containers. A TileGrid is a +// single lv_image (single-tile sprites) or an lv_obj container with one lv_image per +// tile. Images share the source bitmap's pixel data directly when the source format +// matches the display format; otherwise a custom LVGL image decoder converts tiles. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/Group.h" +// The protocol slot an LVGL-backed Group exposes to its parent Group. Legacy Groups are found +// by type, so the slot stays empty there. See displayio/lvgl/lvgl_protocol.h. +#define DISPLAYIO_GROUP_LVGL_PROTOCOL &displayio_lvgl_group_protocol +#else +#include "shared-module/displayio/legacy/Group.h" +#define DISPLAYIO_GROUP_LVGL_PROTOCOL NULL +#endif diff --git a/shared-module/displayio/OnDiskBitmap.h b/shared-module/displayio/OnDiskBitmap.h index 10efcdb353b..96b39efa7f1 100644 --- a/shared-module/displayio/OnDiskBitmap.h +++ b/shared-module/displayio/OnDiskBitmap.h @@ -6,49 +6,10 @@ #pragma once -#include -#include - -#include "py/obj.h" - -typedef struct { - mp_obj_base_t base; - uint16_t width; - uint16_t height; - uint16_t data_offset; - uint16_t stride; - uint32_t r_bitmask; - uint32_t g_bitmask; - uint32_t b_bitmask; - mp_obj_t file; - union { - mp_obj_base_t *pixel_shader_base; - struct displayio_palette *palette; - struct displayio_colorconverter *colorconverter; - }; - bool bitfield_compressed; - uint8_t bits_per_pixel; - // The last bytes read from the file. Pixels are read along rows, so most reads hit it. - uint8_t cache_len; - #if CIRCUITPY_STORAGE_MAP_FILE - // Flash runs the pixels are read from instead of the cache; 0 = read through the cache. - uint8_t map_count; - // 8-bit file with every row inside one run, so TileGrid can read whole rows. - bool rows_mapped; - #endif - uint32_t cache_start; - union { - uint8_t cache[64]; - #if CIRCUITPY_STORAGE_MAP_FILE - struct { - const uint8_t *addr; - uint32_t len; - } map[8]; - #endif - }; -} displayio_ondiskbitmap_t; - -#if CIRCUITPY_STORAGE_MAP_FILE -// Row y of an 8-bit file in flash, or NULL if it spans two runs or the file isn't mapped. -const uint8_t *displayio_ondiskbitmap_row(displayio_ondiskbitmap_t *self, int16_t y); +// The BMP parser and pixel access have separate legacy and LVGL implementations. The LVGL +// version also keeps decoder state for streaming pixels from the file. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/OnDiskBitmap.h" +#else +#include "shared-module/displayio/legacy/OnDiskBitmap.h" #endif diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h index 24ae5c4b7a5..086c377f302 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -60,7 +60,8 @@ typedef struct displayio_palette { } displayio_palette_t; +#if !CIRCUITPY_DISPLAYIO_LVGL void displayio_palette_get_color(displayio_palette_t *palette, const _displayio_colorspace_t *colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color); -; +#endif bool displayio_palette_needs_refresh(displayio_palette_t *self); void displayio_palette_finish_refresh(displayio_palette_t *self); diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h index 0051cc0ebf1..413bd3ef352 100644 --- a/shared-module/displayio/TileGrid.h +++ b/shared-module/displayio/TileGrid.h @@ -10,63 +10,15 @@ #include #include "py/obj.h" -#include "shared-module/displayio/area.h" #include "shared-module/displayio/Palette.h" -typedef struct { - mp_obj_base_t base; - mp_obj_t bitmap; - mp_obj_t pixel_shader; - int16_t x; - int16_t y; - uint16_t pixel_width; - uint16_t pixel_height; - uint16_t bitmap_width_in_tiles; - ; - uint16_t tiles_in_bitmap; - uint16_t width_in_tiles; - uint16_t height_in_tiles; - uint16_t tile_width; - uint16_t tile_height; - uint16_t top_left_x; - uint16_t top_left_y; - void *tiles; // Can be either uint8_t* or uint16_t* depending on tiles_in_bitmap - const displayio_buffer_transform_t *absolute_transform; - displayio_area_t dirty_area; // Stored as a relative area until the refresh area is fetched. - displayio_area_t previous_area; // Stored as an absolute area. - displayio_area_t current_area; // Stored as an absolute area so it applies across frames. - bool partial_change : 1; - bool full_change : 1; - bool moved : 1; - bool inline_tiles : 1; - bool in_group : 1; - bool flip_x : 1; - bool flip_y : 1; - bool transpose_xy : 1; - bool hidden : 1; - bool hidden_by_parent : 1; - bool rendered_hidden : 1; - uint8_t padding : 6; -} displayio_tilegrid_t; - -void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden); - -// Updating the screen is a three stage process. - -// The first stage is used to determine i -displayio_area_t *displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t *tail); - -// Area is always in absolute screen coordinates. Update transform is used to inform TileGrids how -// they relate to it. -bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); -void displayio_tilegrid_update_transform(displayio_tilegrid_t *group, const displayio_buffer_transform_t *parent_transform); - -// Fills in area with the maximum bounds of all related pixels in the last rendered frame. Returns -// false if the tilegrid wasn't rendered in the last frame. -bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t *area); -void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self); - -bool displayio_tilegrid_get_rendered_hidden(displayio_tilegrid_t *self); -void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader); - -void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y); +// See Group.h for the compositing core backend selection. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/TileGrid.h" +// The protocol slot an LVGL-backed TileGrid exposes to its Group. Legacy TileGrids are found +// by type, so the slot stays empty there. See displayio/lvgl/lvgl_protocol.h. +#define DISPLAYIO_TILEGRID_LVGL_PROTOCOL &displayio_lvgl_tilegrid_protocol +#else +#include "shared-module/displayio/legacy/TileGrid.h" +#define DISPLAYIO_TILEGRID_LVGL_PROTOCOL NULL +#endif diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index b15145b488d..c4508aef23c 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -4,115 +4,9 @@ // // SPDX-License-Identifier: MIT -#if CIRCUITPY_BUSDISPLAY -#include "shared-bindings/busdisplay/BusDisplay.h" -#endif -#if CIRCUITPY_AURORA_EPAPER -#include "shared-module/aurora_epaper/aurora_framebuffer.h" -#endif -#include "shared-bindings/displayio/Group.h" -#if CIRCUITPY_EPAPERDISPLAY -#include "shared-bindings/epaperdisplay/EPaperDisplay.h" -#endif -#if CIRCUITPY_FRAMEBUFFERIO -#include "shared-bindings/framebufferio/FramebufferDisplay.h" -#endif -#if CIRCUITPY_FOURWIRE -#include "shared-bindings/fourwire/FourWire.h" -#endif -#if CIRCUITPY_I2CDISPLAYBUS -#include "shared-bindings/i2cdisplaybus/I2CDisplayBus.h" -#endif -#if CIRCUITPY_PARALLELDISPLAYBUS -#include "shared-bindings/paralleldisplaybus/ParallelBus.h" -#endif -#if CIRCUITPY_RGBMATRIX -#include "shared-bindings/rgbmatrix/RGBMatrix.h" -#endif -#if CIRCUITPY_IS31FL3741 -#include "shared-bindings/is31fl3741/FrameBuffer.h" -#endif -#if CIRCUITPY_SHARPDISPLAY -#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" -#endif -#if CIRCUITPY_DOTCLOCKFRAMEBUFFER -#include "common-hal/dotclockframebuffer/DotClockFramebuffer.h" -#endif -#if CIRCUITPY_MIPIDSI -#include "shared-bindings/mipidsi/Display.h" -#endif -#if CIRCUITPY_QSPIBUS -#include "shared-bindings/qspibus/QSPIBus.h" -#endif -#if CIRCUITPY_ZEPHYR_DISPLAY -#include "bindings/zephyr_display/Display.h" -#endif -// Port unique frame buffers. -#if CIRCUITPY_PICODVI -#include "bindings/picodvi/Framebuffer.h" -#endif - #pragma once -typedef struct { - union { - mp_obj_base_t bus_base; - #if CIRCUITPY_FOURWIRE - fourwire_fourwire_obj_t fourwire_bus; - #endif - #if CIRCUITPY_I2CDISPLAYBUS - i2cdisplaybus_i2cdisplaybus_obj_t i2cdisplay_bus; - #endif - #if CIRCUITPY_PARALLELDISPLAYBUS - paralleldisplaybus_parallelbus_obj_t parallel_bus; - #endif - #if CIRCUITPY_RGBMATRIX - rgbmatrix_rgbmatrix_obj_t rgbmatrix; - #endif - #if CIRCUITPY_IS31FL3741 - is31fl3741_framebuffer_obj_t is31fl3741; - #endif - #if CIRCUITPY_SHARPDISPLAY - sharpdisplay_framebuffer_obj_t sharpdisplay; - #endif - #if CIRCUITPY_PICODVI - picodvi_framebuffer_obj_t picodvi; - #endif - #if CIRCUITPY_DOTCLOCKFRAMEBUFFER - dotclockframebuffer_framebuffer_obj_t dotclock; - #endif - #if CIRCUITPY_AURORA_EPAPER - aurora_epaper_framebuffer_obj_t aurora_epaper; - #endif - #if CIRCUITPY_MIPIDSI - mipidsi_display_obj_t mipidsi; - #endif - #if CIRCUITPY_QSPIBUS - qspibus_qspibus_obj_t qspi_bus; - #endif - }; -} primary_display_bus_t; - -typedef struct { - union { - mp_obj_base_t display_base; - #if CIRCUITPY_BUSDISPLAY - busdisplay_busdisplay_obj_t display; - #endif - #if CIRCUITPY_EPAPERDISPLAY - epaperdisplay_epaperdisplay_obj_t epaper_display; - #endif - #if CIRCUITPY_FRAMEBUFFERIO - framebufferio_framebufferdisplay_obj_t framebuffer_display; - #endif - #if CIRCUITPY_ZEPHYR_DISPLAY - zephyr_display_display_obj_t zephyr_display; - #endif - }; -} primary_display_t; - -extern primary_display_bus_t display_buses[CIRCUITPY_DISPLAY_LIMIT]; -extern primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; +#include "shared-bindings/displayio/Group.h" extern displayio_group_t circuitpython_splash; @@ -122,9 +16,13 @@ void reset_displays(void); // Raises if a display using lag_frames can't finish its drive, before any display is released. void displayio_prepare_release_displays(void); #endif +// Both compositor backends implement this. It roots the displayed object tree into the +// GC so that user displays survive garbage collection. void displayio_gc_collect(void); -primary_display_t *allocate_display(void); -primary_display_t *allocate_display_or_raise(void); -primary_display_bus_t *allocate_display_bus(void); -primary_display_bus_t *allocate_display_bus_or_raise(void); +#include "shared-module/displayio/legacy/__init__.h" + +// Static slot registries for displays and buses. LVGL and the legacy compositor both use +// them so that board resets, release_displays() and GC can find active displays. +extern primary_display_bus_t display_buses[CIRCUITPY_DISPLAY_LIMIT]; +extern primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; diff --git a/shared-module/displayio/area.c b/shared-module/displayio/area.c index 6a79e2aeeea..edeb6ecdff8 100644 --- a/shared-module/displayio/area.c +++ b/shared-module/displayio/area.c @@ -8,6 +8,21 @@ #include "py/misc.h" +// Identity transform. Shared by vectorio and the legacy compositor; the LVGL +// compositor ignores display transforms. +displayio_buffer_transform_t null_transform = { + .x = 0, + .y = 0, + .dx = 1, + .dy = 1, + .scale = 1, + .width = 0, + .height = 0, + .mirror_x = false, + .mirror_y = false, + .transpose_xy = false +}; + void displayio_area_copy(const displayio_area_t *src, displayio_area_t *dst) { dst->x1 = src->x1; dst->y1 = src->y1; @@ -63,12 +78,12 @@ bool displayio_area_empty(const displayio_area_t *a) { void displayio_area_canon(displayio_area_t *a) { if (a->x1 > a->x2) { - int16_t t = a->x1; + int32_t t = a->x1; a->x1 = a->x2; a->x2 = t; } if (a->y1 > a->y2) { - int16_t t = a->y1; + int32_t t = a->y1; a->y1 = a->y2; a->y2 = t; } @@ -131,8 +146,8 @@ void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpos transformed->y2 = original->y2; } if (transpose_xy) { - int16_t y1 = transformed->y1; - int16_t y2 = transformed->y2; + int32_t y1 = transformed->y1; + int32_t y2 = transformed->y2; transformed->y1 = whole->y1 + (transformed->x1 - whole->x1); transformed->y2 = whole->y1 + (transformed->x2 - whole->x1); transformed->x2 = whole->x1 + (y2 - whole->y1); diff --git a/shared-module/displayio/area.h b/shared-module/displayio/area.h index eed54613a5a..028e38d9c1f 100644 --- a/shared-module/displayio/area.h +++ b/shared-module/displayio/area.h @@ -9,6 +9,12 @@ #include #include +#if CIRCUITPY_DISPLAYIO_LVGL +#include "lvgl.h" +// LVGL owns invalidation and tracks its own areas, so displayio only needs the +// coordinate type there. The linked-list area below is a scanline-compositor detail. +typedef lv_area_t displayio_area_t; +#else // Implementations are in area.c typedef struct _displayio_area_t displayio_area_t; @@ -19,6 +25,7 @@ struct _displayio_area_t { int16_t y2; const displayio_area_t *next; // Next area in the linked list. }; +#endif typedef struct { uint16_t x; diff --git a/shared-module/displayio/bus_core.c b/shared-module/displayio/bus_core.c index ac05938b986..8e668f553a4 100644 --- a/shared-module/displayio/bus_core.c +++ b/shared-module/displayio/bus_core.c @@ -7,7 +7,9 @@ #include "shared-module/displayio/bus_core.h" #include "py/gc.h" +#include "py/mphal.h" #include "py/runtime.h" +#include "shared/runtime/interrupt_char.h" #if CIRCUITPY_FOURWIRE #include "shared-bindings/fourwire/FourWire.h" #endif @@ -30,6 +32,8 @@ #include #include +#define DELAY 0x80 + #define DISPLAYIO_CORE_DEBUG(...) (void)0 // #define DISPLAYIO_CORE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__) @@ -149,21 +153,59 @@ void displayio_display_bus_end_transaction(displayio_display_bus_t *self) { self->end_transaction(self->bus); } -void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, displayio_display_core_t *display, displayio_area_t *area) { +void displayio_display_bus_send_init_sequence(displayio_display_bus_t *self, + const uint8_t *init_sequence, uint16_t init_sequence_len) { + uint32_t i = 0; + while (i < init_sequence_len) { + const uint8_t *cmd = init_sequence + i; + uint8_t data_size = *(cmd + 1); + bool delay = (data_size & DELAY) != 0; + data_size &= ~DELAY; + const uint8_t *data = cmd + 2; + while (!displayio_display_bus_begin_transaction(self)) { + RUN_BACKGROUND_TASKS; + if (mp_hal_is_interrupted()) { + mp_raise_RuntimeError_varg(MP_ERROR_TEXT("%q init failed"), MP_QSTR_display); + } + } + if (self->data_as_commands) { + uint8_t full_command[data_size + 1]; + full_command[0] = cmd[0]; + memcpy(full_command + 1, data, data_size); + self->send(self->bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, full_command, data_size + 1); + } else { + self->send(self->bus, DISPLAY_COMMAND, data_size > 0 ? CHIP_SELECT_TOGGLE_EVERY_BYTE : CHIP_SELECT_UNTOUCHED, cmd, 1); + self->send(self->bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, data, data_size); + } + displayio_display_bus_end_transaction(self); + uint16_t delay_length_ms = 10; + if (delay) { + data_size++; + delay_length_ms = *(cmd + 1 + data_size); + if (delay_length_ms == 255) { + delay_length_ms = 500; + } + } + common_hal_time_delay_ms(delay_length_ms); + i += 2 + data_size; + } +} + +void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, const _displayio_colorspace_t *colorspace, displayio_area_t *area) { uint16_t x1 = area->x1 + self->colstart; uint16_t x2 = area->x2 + self->colstart; uint16_t y1 = area->y1 + self->rowstart; uint16_t y2 = area->y2 + self->rowstart; // Collapse down the dimension where multiple pixels are in a byte. - if (display->colorspace.depth < 8) { - uint8_t pixels_per_byte = 8 / display->colorspace.depth; - if (display->colorspace.pixels_in_byte_share_row) { - x1 /= pixels_per_byte * display->colorspace.bytes_per_cell; - x2 /= pixels_per_byte * display->colorspace.bytes_per_cell; + if (colorspace->depth < 8) { + uint8_t pixels_per_byte = 8 / colorspace->depth; + if (colorspace->pixels_in_byte_share_row) { + x1 /= pixels_per_byte * colorspace->bytes_per_cell; + x2 /= pixels_per_byte * colorspace->bytes_per_cell; } else { - y1 /= pixels_per_byte * display->colorspace.bytes_per_cell; - y2 /= pixels_per_byte * display->colorspace.bytes_per_cell; + y1 /= pixels_per_byte * colorspace->bytes_per_cell; + y2 /= pixels_per_byte * colorspace->bytes_per_cell; } } diff --git a/shared-module/displayio/bus_core.h b/shared-module/displayio/bus_core.h index 4bfdab6bed4..4f3dcd6043e 100644 --- a/shared-module/displayio/bus_core.h +++ b/shared-module/displayio/bus_core.h @@ -11,6 +11,7 @@ #include "shared-module/displayio/area.h" #include "shared-module/displayio/display_core.h" +#include "shared-module/displayio/Palette.h" #define NO_COMMAND 0x100 @@ -56,7 +57,12 @@ bool displayio_display_bus_is_free(displayio_display_bus_t *self); bool displayio_display_bus_begin_transaction(displayio_display_bus_t *self); void displayio_display_bus_end_transaction(displayio_display_bus_t *self); -void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, displayio_display_core_t *display, displayio_area_t *area); +void displayio_display_bus_set_region_to_update(displayio_display_bus_t *self, const _displayio_colorspace_t *colorspace, displayio_area_t *area); + +// Send a BusDisplay-style init sequence. Each command is command, length byte (with 0x80 +// marking a delay), data, optional delay in ms. +void displayio_display_bus_send_init_sequence(displayio_display_bus_t *self, + const uint8_t *init_sequence, uint16_t init_sequence_len); // Drain any pending asynchronous transfers on the bus. // No-op for synchronous buses (FourWire, I2C, ParallelBus). diff --git a/shared-module/displayio/display_core.c b/shared-module/displayio/display_core.c index 66837748eb3..8cfecc2822a 100644 --- a/shared-module/displayio/display_core.c +++ b/shared-module/displayio/display_core.c @@ -40,15 +40,20 @@ void displayio_display_core_construct(displayio_display_core_t *self, self->height = height; displayio_display_core_set_rotation(self, rotation); + + // If nothing has claimed the primary display yet, this display becomes it. A display created + // by user code (for example a USB video framebuffer) must be kept across the end of the VM so + // that the supervisor terminal and REPL have somewhere to draw. + common_hal_displayio_auto_primary_display(); } void displayio_display_core_set_rotation(displayio_display_core_t *self, int rotation) { - int height = self->height; - int width = self->width; - rotation = rotation % 360; self->rotation = rotation; + #if !CIRCUITPY_DISPLAYIO_LVGL + int height = self->height; + int width = self->width; self->transform.x = 0; self->transform.y = 0; self->transform.scale = 1; @@ -98,6 +103,7 @@ void displayio_display_core_set_rotation(displayio_display_core_t *self, self->transform.dy = -1; } } + #endif } bool displayio_display_core_set_root_group(displayio_display_core_t *self, displayio_group_t *root_group) { @@ -115,16 +121,24 @@ bool displayio_display_core_set_root_group(displayio_display_core_t *self, displ // Deactivate the outgoing tree recursively. This clears its transforms // and tells layers (such as a VideoBuffer) that they are no longer // shown. + #if !CIRCUITPY_DISPLAYIO_LVGL displayio_group_update_transform(self->current_group, NULL); + #endif self->current_group->in_group = false; } if (root_group != NULL) { + // The LVGL compositor updates its own state when the group is attached; the + // legacy compositor re-applies transforms at the start of every display refresh. + #if !CIRCUITPY_DISPLAYIO_LVGL displayio_group_update_transform(root_group, &self->transform); + #endif root_group->in_group = true; } self->current_group = root_group; + #if !CIRCUITPY_DISPLAYIO_LVGL self->full_refresh = true; + #endif return true; } @@ -144,6 +158,7 @@ bool displayio_display_core_get_dither(displayio_display_core_t *self) { return self->colorspace.dither; } +#if !CIRCUITPY_DISPLAYIO_LVGL bool displayio_display_core_start_refresh(displayio_display_core_t *self) { if (self->refresh_in_progress) { return false; @@ -154,20 +169,23 @@ bool displayio_display_core_start_refresh(displayio_display_core_t *self) { } void displayio_display_core_finish_refresh(displayio_display_core_t *self) { - if (self->current_group != NULL) { - DISPLAYIO_CORE_DEBUG("displayiocore group_finish_refresh\n"); - displayio_group_finish_refresh(self->current_group); - } self->full_refresh = false; self->refresh_in_progress = false; self->last_refresh = supervisor_ticks_ms64(); + if (self->current_group != NULL) { + displayio_group_finish_refresh(self->current_group); + } } +#endif + void release_display_core(displayio_display_core_t *self) { if (self->current_group != NULL) { // Deactivate the tree so layers stop acting on it (e.g. a VideoBuffer // pauses its source). + #if !CIRCUITPY_DISPLAYIO_LVGL displayio_group_update_transform(self->current_group, NULL); + #endif self->current_group->in_group = false; } } @@ -175,38 +193,3 @@ void release_display_core(displayio_display_core_t *self) { void displayio_display_core_collect_ptrs(displayio_display_core_t *self) { gc_collect_ptr(self->current_group); } - -bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { - if (self->current_group != NULL) { - return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer); - } - return false; -} - -bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped) { - bool overlaps = displayio_area_compute_overlap(&self->area, area, clipped); - if (!overlaps) { - return false; - } - // Expand the area if we have multiple pixels per byte and we need to byte - // align the bounds. - if (self->colorspace.depth < 8) { - uint8_t pixels_per_byte = 8 / self->colorspace.depth * self->colorspace.bytes_per_cell; - if (self->colorspace.pixels_in_byte_share_row) { - if (clipped->x1 % pixels_per_byte != 0) { - clipped->x1 -= clipped->x1 % pixels_per_byte; - } - if (clipped->x2 % pixels_per_byte != 0) { - clipped->x2 += pixels_per_byte - clipped->x2 % pixels_per_byte; - } - } else { - if (clipped->y1 % pixels_per_byte != 0) { - clipped->y1 -= clipped->y1 % pixels_per_byte; - } - if (clipped->y2 % pixels_per_byte != 0) { - clipped->y2 += pixels_per_byte - clipped->y2 % pixels_per_byte; - } - } - } - return true; -} diff --git a/shared-module/displayio/display_core.h b/shared-module/displayio/display_core.h index f3fe2023636..53c0fc609e0 100644 --- a/shared-module/displayio/display_core.h +++ b/shared-module/displayio/display_core.h @@ -9,22 +9,27 @@ #include "shared-bindings/displayio/__init__.h" #include "shared-bindings/displayio/Group.h" +#if !CIRCUITPY_DISPLAYIO_LVGL #include "shared-module/displayio/area.h" +#endif #define NO_COMMAND 0x100 typedef struct { displayio_group_t *current_group; uint64_t last_refresh; - displayio_buffer_transform_t transform; - displayio_area_t area; uint16_t width; uint16_t height; uint16_t rotation; _displayio_colorspace_t colorspace; + #if !CIRCUITPY_DISPLAYIO_LVGL + // Scanline-compositor refresh state. LVGL invalidates its own objects. + displayio_buffer_transform_t transform; + displayio_area_t area; bool full_refresh; // New group means we need to refresh the whole display. bool refresh_in_progress; + #endif } displayio_display_core_t; void displayio_display_core_construct(displayio_display_core_t *self, @@ -43,11 +48,13 @@ void displayio_display_core_set_rotation(displayio_display_core_t *self, int rot void release_display_core(displayio_display_core_t *self); +#if !CIRCUITPY_DISPLAYIO_LVGL bool displayio_display_core_start_refresh(displayio_display_core_t *self); void displayio_display_core_finish_refresh(displayio_display_core_t *self); -void displayio_display_core_collect_ptrs(displayio_display_core_t *self); - bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer); bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped); +#endif + +void displayio_display_core_collect_ptrs(displayio_display_core_t *self); diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/legacy/Bitmap.c similarity index 94% rename from shared-module/displayio/Bitmap.c rename to shared-module/displayio/legacy/Bitmap.c index 29f32fd40ce..df222d2a72c 100644 --- a/shared-module/displayio/Bitmap.c +++ b/shared-module/displayio/legacy/Bitmap.c @@ -109,18 +109,22 @@ uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t return 0; } -void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *dirty_area) { +void displayio_bitmap_set_dirty(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2) { if (self->read_only) { mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); } - displayio_area_t area = *dirty_area; + displayio_area_t area = {x1, y1, x2, y2, NULL}; displayio_area_canon(&area); displayio_area_union(&area, &self->dirty_area, &area); displayio_area_t bitmap_area = {0, 0, self->width, self->height, NULL}; displayio_area_compute_overlap(&area, &bitmap_area, &self->dirty_area); } +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area) { + displayio_bitmap_set_dirty(self, area->x1, area->y1, area->x2, area->y2); +} + void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { if (self->read_only) { mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); @@ -160,8 +164,7 @@ void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); } // update the dirty region - displayio_area_t a = {x, y, x + 1, y + 1, NULL}; - displayio_bitmap_set_dirty_area(self, &a); + displayio_bitmap_set_dirty(self, x, y, x + 1, y + 1); // write the pixel displayio_bitmap_write_pixel(self, x, y, value); @@ -188,8 +191,7 @@ void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) if (self->read_only) { mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); } - displayio_area_t a = {0, 0, self->width, self->height, NULL}; - displayio_bitmap_set_dirty_area(self, &a); + displayio_bitmap_set_dirty(self, 0, 0, self->width, self->height); // build the packed word uint32_t word = 0; diff --git a/shared-module/displayio/legacy/Bitmap.h b/shared-module/displayio/legacy/Bitmap.h new file mode 100644 index 00000000000..0146af1848a --- /dev/null +++ b/shared-module/displayio/legacy/Bitmap.h @@ -0,0 +1,35 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/area.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t *data; + uint16_t stride; // uint32_t's + uint8_t bits_per_value; + uint8_t x_shift; + size_t x_mask; + displayio_area_t dirty_area; + uint16_t bitmask; + bool read_only; + bool data_alloc; // did bitmap allocate data or someone else +} displayio_bitmap_t; + +void displayio_bitmap_finish_refresh(displayio_bitmap_t *self); +displayio_area_t *displayio_bitmap_get_refresh_areas(displayio_bitmap_t *self, displayio_area_t *tail); +void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value); +void displayio_bitmap_set_dirty(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2); +// Compatibility for callers that still pass an area. +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area); diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/legacy/ColorConverter.c similarity index 100% rename from shared-module/displayio/ColorConverter.c rename to shared-module/displayio/legacy/ColorConverter.c diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/legacy/Group.c similarity index 100% rename from shared-module/displayio/Group.c rename to shared-module/displayio/legacy/Group.c diff --git a/shared-module/displayio/legacy/Group.h b/shared-module/displayio/legacy/Group.h new file mode 100644 index 00000000000..d0a3bd353ec --- /dev/null +++ b/shared-module/displayio/legacy/Group.h @@ -0,0 +1,40 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "py/objlist.h" +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_list_t *members; + displayio_buffer_transform_t absolute_transform; + displayio_area_t dirty_area; // Catch all for changed area + int16_t x; + int16_t y; + uint16_t scale; + bool item_removed : 1; + bool in_group : 1; + bool active : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + bool readonly : 1; + uint8_t padding : 2; +} 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_set_hidden_by_parent(displayio_group_t *self, bool hidden); +bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t *area); +bool displayio_group_fill_area(displayio_group_t *group, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); +void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t *parent_transform); +void displayio_group_finish_refresh(displayio_group_t *self); +displayio_area_t *displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t *tail); diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/legacy/OnDiskBitmap.c similarity index 100% rename from shared-module/displayio/OnDiskBitmap.c rename to shared-module/displayio/legacy/OnDiskBitmap.c diff --git a/shared-module/displayio/legacy/OnDiskBitmap.h b/shared-module/displayio/legacy/OnDiskBitmap.h new file mode 100644 index 00000000000..c3ce4c417ab --- /dev/null +++ b/shared-module/displayio/legacy/OnDiskBitmap.h @@ -0,0 +1,54 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t data_offset; + uint16_t stride; + uint32_t r_bitmask; + uint32_t g_bitmask; + uint32_t b_bitmask; + mp_obj_t file; + union { + mp_obj_base_t *pixel_shader_base; + struct displayio_palette *palette; + struct displayio_colorconverter *colorconverter; + }; + bool bitfield_compressed; + uint8_t bits_per_pixel; + // The last bytes read from the file. Pixels are read along rows, so most reads hit it. + uint8_t cache_len; + #if CIRCUITPY_STORAGE_MAP_FILE + // Flash runs the pixels are read from instead of the cache; 0 = read through the cache. + uint8_t map_count; + // 8-bit file with every row inside one run, so TileGrid can read whole rows. + bool rows_mapped; + #endif + uint32_t cache_start; + union { + uint8_t cache[64]; + #if CIRCUITPY_STORAGE_MAP_FILE + struct { + const uint8_t *addr; + uint32_t len; + } map[8]; + #endif + }; +} displayio_ondiskbitmap_t; + +#if CIRCUITPY_STORAGE_MAP_FILE +// Row y of an 8-bit file in flash, or NULL if it spans two runs or the file isn't mapped. +const uint8_t *displayio_ondiskbitmap_row(displayio_ondiskbitmap_t *self, int16_t y); +#endif diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/legacy/Palette.c similarity index 100% rename from shared-module/displayio/Palette.c rename to shared-module/displayio/legacy/Palette.c diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/legacy/TileGrid.c similarity index 100% rename from shared-module/displayio/TileGrid.c rename to shared-module/displayio/legacy/TileGrid.c diff --git a/shared-module/displayio/legacy/TileGrid.h b/shared-module/displayio/legacy/TileGrid.h new file mode 100644 index 00000000000..0051cc0ebf1 --- /dev/null +++ b/shared-module/displayio/legacy/TileGrid.h @@ -0,0 +1,72 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" + +typedef struct { + mp_obj_base_t base; + mp_obj_t bitmap; + mp_obj_t pixel_shader; + int16_t x; + int16_t y; + uint16_t pixel_width; + uint16_t pixel_height; + uint16_t bitmap_width_in_tiles; + ; + uint16_t tiles_in_bitmap; + uint16_t width_in_tiles; + uint16_t height_in_tiles; + uint16_t tile_width; + uint16_t tile_height; + uint16_t top_left_x; + uint16_t top_left_y; + void *tiles; // Can be either uint8_t* or uint16_t* depending on tiles_in_bitmap + const displayio_buffer_transform_t *absolute_transform; + displayio_area_t dirty_area; // Stored as a relative area until the refresh area is fetched. + displayio_area_t previous_area; // Stored as an absolute area. + displayio_area_t current_area; // Stored as an absolute area so it applies across frames. + bool partial_change : 1; + bool full_change : 1; + bool moved : 1; + bool inline_tiles : 1; + bool in_group : 1; + bool flip_x : 1; + bool flip_y : 1; + bool transpose_xy : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + bool rendered_hidden : 1; + uint8_t padding : 6; +} displayio_tilegrid_t; + +void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden); + +// Updating the screen is a three stage process. + +// The first stage is used to determine i +displayio_area_t *displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t *tail); + +// Area is always in absolute screen coordinates. Update transform is used to inform TileGrids how +// they relate to it. +bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); +void displayio_tilegrid_update_transform(displayio_tilegrid_t *group, const displayio_buffer_transform_t *parent_transform); + +// Fills in area with the maximum bounds of all related pixels in the last rendered frame. Returns +// false if the tilegrid wasn't rendered in the last frame. +bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t *area); +void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self); + +bool displayio_tilegrid_get_rendered_hidden(displayio_tilegrid_t *self); +void displayio_tilegrid_validate_pixel_shader(mp_obj_t pixel_shader); + +void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y); diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/legacy/__init__.c similarity index 95% rename from shared-module/displayio/__init__.c rename to shared-module/displayio/legacy/__init__.c index 856b7e5deac..567f8cf8f83 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/legacy/__init__.c @@ -18,7 +18,6 @@ #include "shared-bindings/displayio/Bitmap.h" #include "shared-bindings/displayio/Group.h" #include "shared-bindings/displayio/Palette.h" -#include "shared-module/displayio/area.h" #include "supervisor/shared/display.h" #include "supervisor/shared/reload.h" @@ -69,19 +68,6 @@ static bool primary_display_set_by_user = false; primary_display_bus_t display_buses[CIRCUITPY_DISPLAY_LIMIT]; primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; -displayio_buffer_transform_t null_transform = { - .x = 0, - .y = 0, - .dx = 1, - .dy = 1, - .scale = 1, - .width = 0, - .height = 0, - .mirror_x = false, - .mirror_y = false, - .transpose_xy = false -}; - #if CIRCUITPY_RGBMATRIX || CIRCUITPY_IS31FL3741 || CIRCUITPY_PICODVI || CIRCUITPY_MIPIDSI static bool any_display_uses_this_framebuffer(mp_obj_base_t *obj) { for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { @@ -135,37 +121,34 @@ void displayio_background(void) { } // Displays and buses are allocated separately, so a display's bus may be at a different index. -static mp_obj_t get_primary_display_bus(void) { - if (primary_display_number == -1) { +static mp_obj_t get_display_bus(int display_number) { + if (display_number < 0 || display_number >= CIRCUITPY_DISPLAY_LIMIT) { return NULL; } - primary_display_t *primary = &displays[primary_display_number]; + primary_display_t *display = &displays[display_number]; if (false) { #if CIRCUITPY_BUSDISPLAY - } else if (primary->display_base.type == &busdisplay_busdisplay_type) { - return primary->display.bus.bus; + } else if (display->display_base.type == &busdisplay_busdisplay_type) { + return display->display.bus.bus; #endif #if CIRCUITPY_EPAPERDISPLAY - } else if (primary->display_base.type == &epaperdisplay_epaperdisplay_type) { - return primary->epaper_display.bus.bus; + } else if (display->display_base.type == &epaperdisplay_epaperdisplay_type) { + return display->epaper_display.bus.bus; #endif #if CIRCUITPY_FRAMEBUFFERIO - } else if (primary->display_base.type == &framebufferio_framebufferdisplay_type) { - return primary->framebuffer_display.framebuffer; + } else if (display->display_base.type == &framebufferio_framebufferdisplay_type) { + return display->framebuffer_display.framebuffer; #endif } return NULL; } -static void common_hal_displayio_release_displays_impl(bool keep_primary) { +void displayio_release_displays_slots(int keep_index) { // Release displays before busses so that they can send any final commands to turn the display // off properly. - if (!keep_primary) { - primary_display_number = -1; - primary_display_set_by_user = false; - } + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (i == primary_display_number) { + if (i == keep_index) { continue; } mp_const_obj_t display_type = displays[i].display_base.type; @@ -178,7 +161,7 @@ static void common_hal_displayio_release_displays_impl(bool keep_primary) { #if CIRCUITPY_EPAPERDISPLAY } else if (display_type == &epaperdisplay_epaperdisplay_type) { #if CIRCUITPY_EPAPERDISPLAY_FAST - if (keep_primary) { + if (keep_index >= 0) { epaperdisplay_epaperdisplay_end_held_bus(&displays[i].epaper_display); } #endif @@ -195,9 +178,10 @@ static void common_hal_displayio_release_displays_impl(bool keep_primary) { } displays[i].display_base.type = &mp_type_NoneType; } - mp_obj_t primary_bus = get_primary_display_bus(); + mp_obj_t keep_bus = get_display_bus(keep_index); for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (MP_OBJ_FROM_PTR(&display_buses[i]) == primary_bus) { + if (MP_OBJ_FROM_PTR(&display_buses[i]) == keep_bus) { + continue; } mp_const_obj_t bus_type = display_buses[i].bus_base.type; @@ -246,8 +230,16 @@ static void common_hal_displayio_release_displays_impl(bool keep_primary) { } display_buses[i].bus_base.type = &mp_type_NoneType; } +} - if (primary_display_number == -1) { +static void common_hal_displayio_release_displays_impl(bool keep_primary) { + int keep_index = keep_primary ? primary_display_number : -1; + if (!keep_primary) { + primary_display_number = -1; + primary_display_set_by_user = false; + } + displayio_release_displays_slots(keep_index); + if (!keep_primary) { supervisor_stop_terminal(); } } @@ -421,7 +413,7 @@ void reset_displays(void) { } } -void displayio_gc_collect(void) { +void displayio_display_slots_gc_collect(void) { for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { mp_const_obj_t display_bus_type = display_buses[i].bus_base.type; if (display_bus_type == NULL || display_bus_type == &mp_type_NoneType) { @@ -481,6 +473,12 @@ void displayio_gc_collect(void) { } } +#if !CIRCUITPY_DISPLAYIO_LVGL +void displayio_gc_collect(void) { + displayio_display_slots_gc_collect(); +} +#endif + static bool is_display_active(mp_obj_base_t *display_maybe) { return display_maybe->type != &mp_type_NoneType && display_maybe->type != NULL; } diff --git a/shared-module/displayio/legacy/__init__.h b/shared-module/displayio/legacy/__init__.h new file mode 100644 index 00000000000..5be85a66e3c --- /dev/null +++ b/shared-module/displayio/legacy/__init__.h @@ -0,0 +1,130 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#if CIRCUITPY_BUSDISPLAY +#include "shared-bindings/busdisplay/BusDisplay.h" +#endif +#if CIRCUITPY_AURORA_EPAPER +#include "shared-module/aurora_epaper/aurora_framebuffer.h" +#endif +#include "shared-bindings/displayio/Group.h" +#if CIRCUITPY_EPAPERDISPLAY +#include "shared-bindings/epaperdisplay/EPaperDisplay.h" +#endif +#if CIRCUITPY_FRAMEBUFFERIO +#include "shared-bindings/framebufferio/FramebufferDisplay.h" +#endif +#if CIRCUITPY_FOURWIRE +#include "shared-bindings/fourwire/FourWire.h" +#endif +#if CIRCUITPY_I2CDISPLAYBUS +#include "shared-bindings/i2cdisplaybus/I2CDisplayBus.h" +#endif +#if CIRCUITPY_PARALLELDISPLAYBUS +#include "shared-bindings/paralleldisplaybus/ParallelBus.h" +#endif +#if CIRCUITPY_RGBMATRIX +#include "shared-bindings/rgbmatrix/RGBMatrix.h" +#endif +#if CIRCUITPY_IS31FL3741 +#include "shared-bindings/is31fl3741/FrameBuffer.h" +#endif +#if CIRCUITPY_SHARPDISPLAY +#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h" +#endif +#if CIRCUITPY_DOTCLOCKFRAMEBUFFER +#include "common-hal/dotclockframebuffer/DotClockFramebuffer.h" +#endif +#if CIRCUITPY_MIPIDSI +#include "shared-bindings/mipidsi/Display.h" +#endif +#if CIRCUITPY_QSPIBUS +#include "shared-bindings/qspibus/QSPIBus.h" +#endif +#if CIRCUITPY_ZEPHYR_DISPLAY +#include "bindings/zephyr_display/Display.h" +#endif +// Port unique frame buffers. +#if CIRCUITPY_PICODVI +#include "bindings/picodvi/Framebuffer.h" +#endif + +#pragma once + +typedef struct { + union { + mp_obj_base_t bus_base; + #if CIRCUITPY_FOURWIRE + fourwire_fourwire_obj_t fourwire_bus; + #endif + #if CIRCUITPY_I2CDISPLAYBUS + i2cdisplaybus_i2cdisplaybus_obj_t i2cdisplay_bus; + #endif + #if CIRCUITPY_PARALLELDISPLAYBUS + paralleldisplaybus_parallelbus_obj_t parallel_bus; + #endif + #if CIRCUITPY_RGBMATRIX + rgbmatrix_rgbmatrix_obj_t rgbmatrix; + #endif + #if CIRCUITPY_IS31FL3741 + is31fl3741_framebuffer_obj_t is31fl3741; + #endif + #if CIRCUITPY_SHARPDISPLAY + sharpdisplay_framebuffer_obj_t sharpdisplay; + #endif + #if CIRCUITPY_PICODVI + picodvi_framebuffer_obj_t picodvi; + #endif + #if CIRCUITPY_DOTCLOCKFRAMEBUFFER + dotclockframebuffer_framebuffer_obj_t dotclock; + #endif + #if CIRCUITPY_AURORA_EPAPER + aurora_epaper_framebuffer_obj_t aurora_epaper; + #endif + #if CIRCUITPY_MIPIDSI + mipidsi_display_obj_t mipidsi; + #endif + #if CIRCUITPY_QSPIBUS + qspibus_qspibus_obj_t qspi_bus; + #endif + }; +} primary_display_bus_t; + +typedef struct { + union { + mp_obj_base_t display_base; + #if CIRCUITPY_BUSDISPLAY + busdisplay_busdisplay_obj_t display; + #endif + #if CIRCUITPY_EPAPERDISPLAY + epaperdisplay_epaperdisplay_obj_t epaper_display; + #endif + #if CIRCUITPY_FRAMEBUFFERIO + framebufferio_framebufferdisplay_obj_t framebuffer_display; + #endif + #if CIRCUITPY_ZEPHYR_DISPLAY + zephyr_display_display_obj_t zephyr_display; + #endif + }; +} primary_display_t; + +extern displayio_group_t circuitpython_splash; + +void displayio_background(void); +void reset_displays(void); +void displayio_gc_collect(void); + +// The legacy display slot registry is also used by the LVGL compositor so that +// displayio.release_displays() can find and release displays. +// Release every slot except keep_index (pass -1 for all). +void displayio_release_displays_slots(int keep_index); +// Root the GC objects referenced by displays in the slot registry. +void displayio_display_slots_gc_collect(void); + +primary_display_t *allocate_display(void); +primary_display_t *allocate_display_or_raise(void); +primary_display_bus_t *allocate_display_bus(void); +primary_display_bus_t *allocate_display_bus_or_raise(void); diff --git a/shared-module/displayio/legacy/display_core_scanline.c b/shared-module/displayio/legacy/display_core_scanline.c new file mode 100644 index 00000000000..46a00fe9592 --- /dev/null +++ b/shared-module/displayio/legacy/display_core_scanline.c @@ -0,0 +1,47 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Scanline (legacy) compositor helpers that display the group tree. Defined here +// instead of in ../display_core.c so that display-side modules such as busdisplay, +// epaperdisplay and framebufferio implement them on top of the legacy compositor +// while LVGL-backed builds do not carry the scanline rendering machinery at all. + +#include "shared-module/displayio/display_core.h" + +bool displayio_display_core_fill_area(displayio_display_core_t *self, displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { + if (self->current_group != NULL) { + return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer); + } + return false; +} + +bool displayio_display_core_clip_area(displayio_display_core_t *self, const displayio_area_t *area, displayio_area_t *clipped) { + bool overlaps = displayio_area_compute_overlap(&self->area, area, clipped); + if (!overlaps) { + return false; + } + // Expand the area if we have multiple pixels per byte and we need to byte + // align the bounds. + if (self->colorspace.depth < 8) { + uint8_t pixels_per_byte = 8 / self->colorspace.depth * self->colorspace.bytes_per_cell; + if (self->colorspace.pixels_in_byte_share_row) { + if (clipped->x1 % pixels_per_byte != 0) { + clipped->x1 -= clipped->x1 % pixels_per_byte; + } + if (clipped->x2 % pixels_per_byte != 0) { + clipped->x2 += pixels_per_byte - clipped->x2 % pixels_per_byte; + } + } else { + if (clipped->y1 % pixels_per_byte != 0) { + clipped->y1 -= clipped->y1 % pixels_per_byte; + } + if (clipped->y2 % pixels_per_byte != 0) { + clipped->y2 += pixels_per_byte - clipped->y2 % pixels_per_byte; + } + } + } + return true; +} diff --git a/shared-module/displayio/lvgl/Bitmap.c b/shared-module/displayio/lvgl/Bitmap.c new file mode 100644 index 00000000000..b954c5dc5f4 --- /dev/null +++ b/shared-module/displayio/lvgl/Bitmap.c @@ -0,0 +1,227 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/displayio/Bitmap.h" + +#include + +#include "py/runtime.h" +#include "py/gc.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" + +#if CIRCUITPY_ESPIDF +#include "esp_log.h" +static const char *TAG = "CP"; +#endif + +enum { ALIGN_BITS = 8 * sizeof(uint32_t) }; + +static int stride(uint32_t width, uint32_t bits_per_value) { + uint32_t row_width = width * bits_per_value; + // align to uint32_t + return (row_width + ALIGN_BITS - 1) / ALIGN_BITS; +} + +void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t width, + uint32_t height, uint32_t bits_per_value) { + common_hal_displayio_bitmap_construct_from_buffer(self, width, height, bits_per_value, NULL, false); +} + +void common_hal_displayio_bitmap_construct_from_buffer(displayio_bitmap_t *self, uint32_t width, + uint32_t height, uint32_t bits_per_value, uint32_t *data, bool read_only) { + if (bits_per_value > 8 && bits_per_value != 16 && bits_per_value != 32) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("Invalid bits per value")); + } + + self->width = width; + self->height = height; + self->stride = stride(width, bits_per_value); + self->data_alloc = false; + if (!data) { + size_t allocation_size = self->stride * height * sizeof(uint32_t); + #if CIRCUITPY_ESPIDF + ESP_LOGI(TAG, "Allocating data for %ux%u bitmap of size %zu", self->stride, height, allocation_size); + #endif + data = m_malloc_without_collect(allocation_size); + self->data_alloc = true; + } + self->data = data; + self->read_only = read_only; + self->bits_per_value = bits_per_value; + self->lv_watch_head = NULL; + + // Division and modulus can be slow because it has to handle any integer. We know bits_per_value + // is a power of two. We divide and mod by bits_per_value to compute the offset into the byte + // array. So, we can the offset computation to simplify to a shift for division and mask for mod. + + self->x_shift = 0; // Used to divide the index by the number of pixels per word. Its used in a + // shift which effectively divides by 2 ** x_shift. + uint32_t power_of_two = 1; + while (power_of_two < 8 / bits_per_value) { + self->x_shift++; + power_of_two <<= 1; + } + self->x_mask = (1u << self->x_shift) - 1u; // Used as a modulus on the x value + self->bitmask = (1u << bits_per_value) - 1u; + +} + +void common_hal_displayio_bitmap_deinit(displayio_bitmap_t *self) { + // Stop every mirror and cached decode from sampling the storage before freeing it. + // The render task must not run between clearing the descriptors and freeing the pixels. + displayio_lvgl_lock(); + uint32_t *data = self->data; + self->data = NULL; + displayio_lvgl_tilegrid_bitmap_changed(self, 0, 0, self->width, self->height); + if (self->data_alloc && data != NULL) { + gc_free(data); + } + displayio_lvgl_unlock(); +} + +bool common_hal_displayio_bitmap_deinited(displayio_bitmap_t *self) { + return self->data == NULL; +} + +uint16_t common_hal_displayio_bitmap_get_height(displayio_bitmap_t *self) { + return self->height; +} + +uint16_t common_hal_displayio_bitmap_get_width(displayio_bitmap_t *self) { + return self->width; +} + +uint32_t common_hal_displayio_bitmap_get_bits_per_value(displayio_bitmap_t *self) { + return self->bits_per_value; +} + +uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t x, int16_t y) { + if (x >= self->width || x < 0 || y >= self->height || y < 0) { + return 0; + } + int32_t row_start = y * self->stride; + uint32_t *row = self->data + row_start; + uint8_t bytes_per_value = self->bits_per_value >> 3; + if (bytes_per_value == 0) { + // values_per_byte is a power of two, and x_shift is its log2. + uint8_t values_per_byte = 1 << self->x_shift; + uint8_t bits = ((uint8_t *)row)[x >> self->x_shift]; + uint8_t bit_position = (values_per_byte - (x & self->x_mask) - 1) * self->bits_per_value; + return (bits >> bit_position) & self->bitmask; + } else { + if (bytes_per_value == 1) { + return ((uint8_t *)row)[x]; + } else if (bytes_per_value == 2) { + return ((uint16_t *)row)[x]; + } else if (bytes_per_value == 4) { + return ((uint32_t *)row)[x]; + } + } + return 0; +} + +void displayio_bitmap_set_dirty(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2) { + // Nothing draws this bitmap, so no mirror needs to know. This is the common case while a + // shape is being rasterized into a fresh Bitmap: avoid the LVGL lock and mirror walk. + if (self->data == NULL || self->lv_watch_head == NULL) { + return; + } + displayio_lvgl_tilegrid_bitmap_changed(self, x1, y1, x2, y2); +} + +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area) { + displayio_bitmap_set_dirty(self, area->x1, area->y1, area->x2, area->y2); +} + +// Store one pixel. The caller has already checked read_only, bounds and data. +static void _write_pixel_unchecked(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { + int32_t row_start = y * self->stride; + uint32_t *row = self->data + row_start; + uint8_t bytes_per_value = self->bits_per_value >> 3; + if (bytes_per_value == 0) { + // values_per_byte is a power of two, and x_shift is its log2. + uint8_t values_per_byte = 1 << self->x_shift; + uint8_t bits = ((uint8_t *)row)[x >> self->x_shift]; + uint8_t bit_position = (values_per_byte - (x & self->x_mask) - 1) * self->bits_per_value; + bits &= ~(self->bitmask << bit_position); + bits |= (value & self->bitmask) << bit_position; + ((uint8_t *)row)[x >> self->x_shift] = bits; + } else { + if (bytes_per_value == 1) { + ((uint8_t *)row)[x] = value; + } else if (bytes_per_value == 2) { + ((uint16_t *)row)[x] = value; + } else if (bytes_per_value == 4) { + ((uint32_t *)row)[x] = value; + } + } +} + +void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { + if (self->read_only) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); + } + // Writes the color index value into a pixel position + // Must update the dirty area separately + + // Don't write if out of area + if (0 > x || x >= self->width || 0 > y || y >= self->height) { + return; + } + + _write_pixel_unchecked(self, x, y, value); +} + +void common_hal_displayio_bitmap_set_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value) { + if (self->read_only) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); + } + // An out of bounds write changes nothing, so don't dirty the display for it. The Python + // binding raises first; C callers may write clipped shapes. + if (0 > x || x >= self->width || 0 > y || y >= self->height || self->data == NULL) { + return; + } + // Mark before writing: the compose pass must re-read a region that is already changing. + displayio_bitmap_set_dirty(self, x, y, x + 1, y + 1); + _write_pixel_unchecked(self, x, y, value); +} + +void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) { + if (self->read_only) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); + } + displayio_bitmap_set_dirty(self, 0, 0, self->width, self->height); + + // build the packed word + uint32_t word = 0; + for (uint8_t i = 0; i < 32 / self->bits_per_value; i++) { + word |= (value & self->bitmask) << (32 - ((i + 1) * self->bits_per_value)); + } + // copy it in + for (uint32_t i = 0; i < self->stride * self->height; i++) { + self->data[i] = word; + } +} + +int common_hal_displayio_bitmap_get_buffer(displayio_bitmap_t *self, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + if ((flags & MP_BUFFER_WRITE) && self->read_only) { + return 1; + } + bufinfo->len = self->stride * self->height * sizeof(uint32_t); + bufinfo->buf = self->data; + switch (self->bits_per_value) { + case 32: + bufinfo->typecode = 'I'; + break; + case 16: + bufinfo->typecode = 'H'; + break; + default: + bufinfo->typecode = 'B'; + break; + } + return 0; +} diff --git a/shared-module/displayio/lvgl/Bitmap.h b/shared-module/displayio/lvgl/Bitmap.h new file mode 100644 index 00000000000..3ecbc725277 --- /dev/null +++ b/shared-module/displayio/lvgl/Bitmap.h @@ -0,0 +1,51 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/area.h" + +// LVGL-based version of displayio Bitmap. Same pixel storage as the legacy version (rows +// packed into uint32_t words, sub-byte values MSB-first). +// +// Deliberately no LVGL state: a TileGrid composes the tiles it shows into its own buffer, and +// reads this Bitmap's pixels (through its Palette) while doing it. So a Bitmap never owns an +// image or a palette copy; a write only has to tell the TileGrids showing it which pixels +// changed. Those grids are kept on lv_watch_head, so the write path never walks the global +// mirror list and skips LVGL entirely when nothing is showing this Bitmap. + +// Forward declaration: the attached TileGrids that draw this bitmap form a list through +// their lv_bitmap_next pointer. +struct displayio_tilegrid; + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t *data; + uint16_t stride; // uint32_t's + uint8_t bits_per_value; + uint8_t x_shift; + size_t x_mask; + uint16_t bitmask; + bool read_only; + bool data_alloc; // did bitmap allocate data or someone else + // Attached TileGrids drawing this bitmap. Empty means a pixel write has nothing to + // notify, so it can skip the LVGL lock and the mirror walk entirely. + struct displayio_tilegrid *lv_watch_head; +} displayio_bitmap_t; + +void displayio_bitmap_write_pixel(displayio_bitmap_t *self, int16_t x, int16_t y, uint32_t value); +// Higher-level dirty notification. The rectangular region is in bitmap pixel coordinates. The +// TileGrids drawing this bitmap note the region and re-read it before the next refresh, since +// writers mark the area *before* they change the pixels. +void displayio_bitmap_set_dirty(displayio_bitmap_t *self, int16_t x1, int16_t y1, int16_t x2, int16_t y2); +// Compatibility for callers that still pass an area, such as bitmaptools. +void displayio_bitmap_set_dirty_area(displayio_bitmap_t *self, const displayio_area_t *area); diff --git a/shared-module/displayio/lvgl/ColorConverter.c b/shared-module/displayio/lvgl/ColorConverter.c new file mode 100644 index 00000000000..d4e64ee286e --- /dev/null +++ b/shared-module/displayio/lvgl/ColorConverter.c @@ -0,0 +1,411 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/displayio/ColorConverter.h" + +#include "py/misc.h" +#include "py/runtime.h" + +#define NO_TRANSPARENT_COLOR (0x1000000) + +uint32_t displayio_colorconverter_dither_noise_1(uint32_t n) { + n = (n >> 13) ^ n; + int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; + return (uint32_t)(((float)nn / (1073741824.0f * 2)) * 255); +} + +uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) { + return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF); +} + +void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t *self, bool dither, displayio_colorspace_t input_colorspace) { + self->dither = dither; + self->transparent_color = NO_TRANSPARENT_COLOR; + self->input_colorspace = input_colorspace; + self->output_colorspace.depth = 16; +} + +uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) { + uint32_t r5 = (color_rgb888 >> 19); + uint32_t g6 = (color_rgb888 >> 10) & 0x3f; + uint32_t b5 = (color_rgb888 >> 3) & 0x1f; + return r5 << 11 | g6 << 5 | b5; +} + +uint8_t displayio_colorconverter_compute_rgb332(uint32_t color_rgb888) { + uint32_t r3 = (color_rgb888 >> 21); + uint32_t g3 = (color_rgb888 >> 13) & 0x7; + uint32_t b2 = (color_rgb888 >> 6) & 0x3; + return r3 << 5 | g3 << 2 | b2; +} + +uint8_t displayio_colorconverter_compute_rgbd(uint32_t color_rgb888) { + uint32_t r1 = (color_rgb888 >> 23) & 0x1; + uint32_t g1 = (color_rgb888 >> 15) & 0x1; + uint32_t b1 = (color_rgb888 >> 7) & 0x1; + return r1 << 3 | g1 << 2 | b1 << 1 /* | dummy */; +} + +uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + return (r8 * 19 + g8 * 182 + b8 * 54) / 255; +} + +uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + uint8_t max = MAX(r8, MAX(g8, b8)); + uint8_t min = MIN(r8, MIN(g8, b8)); + return max - min; +} + +uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + uint8_t max = MAX(r8, MAX(g8, b8)); + uint8_t min = MIN(r8, MIN(g8, b8)); + uint8_t c = max - min; + if (c == 0) { + return 0; + } + + int32_t hue = 0; + if (max == r8) { + hue = (((int32_t)(g8 - b8) * 40) / c) % 240; + } else if (max == g8) { + hue = (((int32_t)(b8 - r8) + (2 * c)) * 40) / c; + } else if (max == b8) { + hue = (((int32_t)(r8 - g8) + (4 * c)) * 40) / c; + } + if (hue < 0) { + hue += 240; + } + + return hue; +} + +uint8_t displayio_colorconverter_compute_sixcolor(uint32_t color_rgb888) { + // This is DDX=1, the default for the displays. + uint8_t chroma = displayio_colorconverter_compute_chroma(color_rgb888); + if (chroma >= 64) { + uint8_t hue = displayio_colorconverter_compute_hue(color_rgb888); + // Red 0 + if (hue < 10) { + return 0x3; + } + // Yellow 42 + if (hue < 42 + 21) { + return 0x2; + } + // Green 85 + if (hue < 85 + 42) { + return 0x6; + } + // Blue 170 + if (hue < 170 + 42) { + return 0x5; + } + + // The rest is red to 255 + return 0x3; + } else { + uint8_t luma = displayio_colorconverter_compute_luma(color_rgb888); + if (luma >= 128) { + return 0x1; // White + } else { + return 0x0; // Black + } + } +} + +uint8_t displayio_colorconverter_compute_sevencolor(uint32_t color_rgb888) { + // This is DDX=1, the default for the displays. + uint8_t chroma = displayio_colorconverter_compute_chroma(color_rgb888); + if (chroma >= 64) { + uint8_t hue = displayio_colorconverter_compute_hue(color_rgb888); + // Red 0 + if (hue < 10) { + return 0x4; + } + // Orange 21 + if (hue < 21 + 10) { + return 0x6; + } + // Yellow 42 + if (hue < 42 + 21) { + return 0x5; + } + // Green 85 + if (hue < 85 + 42) { + return 0x2; + } + // Blue 170 + if (hue < 170 + 42) { + return 0x3; + } + + // The rest is red to 255 + return 0x4; + } else { + uint8_t luma = displayio_colorconverter_compute_luma(color_rgb888); + if (luma >= 128) { + return 0x1; // White + } else { + return 0x0; // Black + } + } +} + +void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t *colorspace, uint8_t pixel_chroma, uint8_t pixel_hue, uint32_t *color) { + if (pixel_chroma <= 16) { + if (!colorspace->grayscale) { + *color = 0; + } + return; + } + int16_t hue_diff = colorspace->tricolor_hue - pixel_hue; + if ((-10 <= hue_diff && hue_diff <= 10) || hue_diff <= -220 || hue_diff >= 220) { + if (colorspace->grayscale) { + *color = 0; + } else { + *color = 1; + } + } else if (!colorspace->grayscale) { + *color = 0; + } +} + +void displayio_colorconverter_compute_fourcolor(const _displayio_colorspace_t *colorspace, uint8_t pixel_chroma, uint8_t pixel_hue, uint32_t *color) { + *color >>= 1; + if (pixel_chroma <= 16) { + return; + } + int16_t hue_diff = colorspace->tricolor_hue - pixel_hue; + if ((-10 <= hue_diff && hue_diff <= 10) || hue_diff <= -220 || hue_diff >= 220) { + *color = 2; + } + int16_t hue_diff2 = colorspace->fourcolor_hue - pixel_hue; + if ((-10 <= hue_diff2 && hue_diff2 <= 10) || hue_diff2 <= -220 || hue_diff2 >= 220) { + *color = 3; + } +} + +void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t *colorspace, uint32_t input_color, uint32_t *output_color) { + displayio_input_pixel_t input_pixel; + input_pixel.pixel = input_color; + input_pixel.x = input_pixel.y = input_pixel.tile = input_pixel.tile_x = input_pixel.tile_y = 0; + + displayio_output_pixel_t output_pixel; + output_pixel.pixel = 0; + output_pixel.opaque = false; + + displayio_colorconverter_convert(self, colorspace, &input_pixel, &output_pixel); + + (*output_color) = output_pixel.pixel; +} + +void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t *self, bool dither) { + self->dither = dither; +} + +bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t *self) { + return self->dither; +} + +void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t *self, uint32_t transparent_color) { + if (self->transparent_color != NO_TRANSPARENT_COLOR) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Only one color can be transparent at a time")); + } + self->transparent_color = transparent_color; +} + +void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t *self, uint32_t transparent_color) { + (void)transparent_color; + // NO_TRANSPARENT_COLOR will never equal a valid color + self->transparent_color = NO_TRANSPARENT_COLOR; +} + + +// Convert a single input pixel to RGB888 +uint32_t displayio_colorconverter_convert_pixel(displayio_colorspace_t colorspace, uint32_t pixel) { + switch (colorspace) { + case DISPLAYIO_COLORSPACE_RGB565_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_RGB565: { + uint32_t r8 = (pixel >> 11) << 3; + uint32_t g8 = ((pixel >> 5) << 2) & 0xff; + uint32_t b8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_RGB555_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_RGB555: { + uint32_t r8 = (pixel >> 10) & 0x1f; + uint32_t g8 = (pixel >> 5) & 0x1f; + uint32_t b8 = pixel & 0x1f; + r8 = (r8 << 3) | ((r8 >> 2) & 0b111); + g8 = (g8 << 3) | ((g8 >> 2) & 0b111); + b8 = (b8 << 3) | ((b8 >> 2) & 0b111); + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_BGR565_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_BGR565: { + uint32_t b8 = (pixel >> 11) << 3; + uint32_t g8 = ((pixel >> 5) << 2) & 0xff; + uint32_t r8 = (pixel << 3) & 0xff; + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + case DISPLAYIO_COLORSPACE_BGR555_SWAPPED: + pixel = __builtin_bswap16(pixel); + MP_FALLTHROUGH; + case DISPLAYIO_COLORSPACE_BGR555: { + uint32_t b8 = (pixel >> 10) & 0x1f; + uint32_t g8 = (pixel >> 5) & 0x1f; + uint32_t r8 = pixel & 0x1f; + r8 = (r8 << 3) | ((r8 >> 2) & 0b111); + g8 = (g8 << 3) | ((g8 >> 2) & 0b111); + b8 = (b8 << 3) | ((b8 >> 2) & 0b111); + pixel = (r8 << 16) | (g8 << 8) | b8; + } + break; + + default: + case DISPLAYIO_COLORSPACE_RGB888: + break; + + case DISPLAYIO_COLORSPACE_L8: { + uint32_t l8 = pixel & 0xff; + pixel = l8 * 0x010101; + } + break; + } + + return pixel; +} + +void displayio_convert_color(const _displayio_colorspace_t *colorspace, bool dither, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { + uint32_t pixel = input_pixel->pixel; + if (dither) { + uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x, input_pixel->tile_y)); + uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x + 33, input_pixel->tile_y)); + uint8_t randb = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x, input_pixel->tile_y + 33)); + + uint32_t r8 = (pixel >> 16); + uint32_t g8 = (pixel >> 8) & 0xff; + uint32_t b8 = pixel & 0xff; + + if (colorspace->depth == 16) { + b8 = MIN(255, b8 + (randb & 0x07)); + r8 = MIN(255, r8 + (randr & 0x07)); + g8 = MIN(255, g8 + (randg & 0x03)); + } else { + int bitmask = 0xFF >> colorspace->depth; + b8 = MIN(255, b8 + (randb & bitmask)); + r8 = MIN(255, r8 + (randr & bitmask)); + g8 = MIN(255, g8 + (randg & bitmask)); + } + pixel = r8 << 16 | g8 << 8 | b8; + } + + if (colorspace->depth == 16) { + uint16_t packed = displayio_colorconverter_compute_rgb565(pixel); + if (colorspace->reverse_bytes_in_word) { + // swap bytes + packed = __builtin_bswap16(packed); + } + output_color->pixel = packed; + output_color->opaque = true; + return; + } else if (colorspace->tricolor || colorspace->fourcolor) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + uint8_t pixel_chroma = displayio_colorconverter_compute_chroma(pixel); + output_color->pixel = luma >> (8 - colorspace->depth); + uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel); + if (colorspace->tricolor) { + displayio_colorconverter_compute_tricolor(colorspace, pixel_chroma, pixel_hue, &output_color->pixel); + } else if (colorspace->fourcolor) { + displayio_colorconverter_compute_fourcolor(colorspace, pixel_chroma, pixel_hue, &output_color->pixel); + } + output_color->opaque = true; + return; + } else if (colorspace->grayscale && colorspace->depth <= 8) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + size_t bitmask = (1 << colorspace->depth) - 1; + output_color->pixel = (luma >> colorspace->grayscale_bit) & bitmask; + output_color->opaque = true; + return; + } else if (colorspace->depth == 32 || colorspace->depth == 24) { + output_color->pixel = pixel; + output_color->opaque = true; + return; + } else if (colorspace->depth == 8 && !colorspace->grayscale) { + uint8_t packed = displayio_colorconverter_compute_rgb332(pixel); + output_color->pixel = packed; + output_color->opaque = true; + return; + } else if (colorspace->depth == 4) { + uint8_t packed; + if (colorspace->sixcolor) { + packed = displayio_colorconverter_compute_sixcolor(pixel); + } else if (colorspace->sevencolor) { + packed = displayio_colorconverter_compute_sevencolor(pixel); + } else { + packed = displayio_colorconverter_compute_rgbd(pixel); + } + output_color->pixel = packed; + output_color->opaque = true; + return; + } + output_color->opaque = false; +} + +void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t *colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { + uint32_t pixel = input_pixel->pixel; + + if (self->transparent_color == pixel) { + output_color->opaque = false; + return; + } + + if (!self->dither && self->cached_colorspace == colorspace && self->cached_input_pixel == input_pixel->pixel) { + output_color->pixel = self->cached_output_color; + return; + } + + displayio_input_pixel_t rgb888_pixel = *input_pixel; + rgb888_pixel.pixel = displayio_colorconverter_convert_pixel(self->input_colorspace, input_pixel->pixel); + displayio_convert_color(colorspace, self->dither, &rgb888_pixel, output_color); + + if (!self->dither) { + self->cached_colorspace = colorspace; + self->cached_input_pixel = input_pixel->pixel; + self->cached_output_color = output_color->pixel; + } +} + + + +// Currently no refresh logic is needed for a ColorConverter. +bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) { + return false; +} + +void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self) { +} diff --git a/shared-module/displayio/lvgl/Group.c b/shared-module/displayio/lvgl/Group.c new file mode 100644 index 00000000000..70a4395321d --- /dev/null +++ b/shared-module/displayio/lvgl/Group.c @@ -0,0 +1,700 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// 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 +// scaling and per-child invalidation; LVGL transform styles use an ARGB8888 transform +// layer and bilinear filtering instead. +// +// The compositor pushes every state change into the LVGL mirror synchronously, under the +// port's LVGL lock: LVGL itself renders on an RTOS thread (e.g. a Zephyr workqueue), and +// this keeps tree mutation off the render thread. + +#include "shared-bindings/displayio/Group.h" + +#include +#include + +#include "py/gc.h" +#include "py/objtype.h" +#include "py/runtime.h" +#include "py/objlist.h" +#include "shared-bindings/displayio/TileGrid.h" +#include "shared-bindings/displayio/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "shared-module/displayio/lvgl/lvgl_protocol.h" + +#if CIRCUITPY_VIDEOCORE +#include "shared-module/videocore/VideoBuffer.h" +#endif + +#if CIRCUITPY_DISPLAYIO_LVGL + +// Resolve a member's protocol and, in place, the object it implements the protocol on. Layers +// mirror themselves into the LVGL tree through protocol_lvgl, so nothing here has to know a +// member's type. See lvgl_protocol.h. +static const lvgl_protocol_t *_layer_protocol(mp_obj_t *layer) { + const lvgl_protocol_t *protocol = mp_proto_get(MP_QSTR_protocol_lvgl, *layer); + if (protocol != NULL && protocol->protocol_self != NULL) { + *layer = protocol->protocol_self(*layer); + protocol = mp_proto_get(MP_QSTR_protocol_lvgl, *layer); + } + return protocol; +} + +static displayio_group_t *cp_lvgl_root_group = NULL; +static bool cp_lvgl_inited = false; + +// The port lock is recursive. Keeping the recursion at the port instead of counting here matters +// once LVGL renders on its own task: a shared depth counter would let the render task believe it +// owns a lock that the CircuitPython task is holding. +void displayio_lvgl_lock(void) { + displayio_lvgl_port_lock(); +} + +void displayio_lvgl_unlock(void) { + displayio_lvgl_port_unlock(); +} + +static void _unlock_on_exception(void *ctx) { + (void)ctx; + displayio_lvgl_unlock(); +} + +void displayio_lvgl_lock_with_cleanup(nlr_jump_callback_node_t *cleanup) { + displayio_lvgl_lock(); + nlr_push_jump_callback(cleanup, _unlock_on_exception); +} + +void displayio_lvgl_unlock_with_cleanup(void) { + nlr_pop_jump_callback(true); +} + +// Groups are mirrored as transparent, screen sized LVGL objects at the screen origin and +// their members are positioned in screen coordinates (lv_abs_x/y is the group's origin on +// the screen). A group's LVGL object never moves or resizes, so moving a member only +// 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.) +// +// 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; + 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); + if (protocol == NULL || protocol->set_transform == NULL) { + continue; + } + if (!protocol->set_transform(layer, self->lv_abs_x, self->lv_abs_y, child_scale)) { + return false; + } + } + return true; +} + +static void check_readonly(displayio_group_t *self) { + if (self->readonly) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Read-only")); + } +} + +// Bitmap writers mark the dirty area before writing, and TileGrid writes only set bits, so the +// composed buffers are brought up to date here: at the start of a refresh, after the writes and +// before anything is drawn. The lock is held, and LVGL doesn't render while this runs. +static void refresh_start_cb(lv_event_t *e) { + (void)e; + // Take any video frames that landed since the last background tick, so a + // manual display.refresh() shows them too. + #if CIRCUITPY_VIDEOCORE + displayio_lvgl_video_background(); + #endif + displayio_lvgl_tilegrid_refresh_all(); +} + +void displayio_lvgl_register_display(lv_display_t *display) { + if (display == NULL) { + return; + } + displayio_lvgl_lock(); + // Match the legacy compositor, where undrawn pixels are zero (black). LVGL draws its bottom + // layer -- white, out of the box -- wherever nothing else covers an area, so the screen has to + // be opaque black. This belongs to each display rather than the one-time init: displays are + // deleted and re-created on every soft reload (and by the display classes themselves), and a + // screen without this style comes up white. + lv_obj_t *screen = lv_display_get_screen_active(display); + if (screen != NULL) { + lv_obj_set_style_bg_color(screen, lv_color_black(), 0); + lv_obj_set_style_bg_opa(screen, LV_OPA_COVER, 0); + lv_obj_clear_flag(screen, LV_OBJ_FLAG_SCROLLABLE); + } + lv_display_add_event_cb(display, refresh_start_cb, LV_EVENT_REFR_START, NULL); + displayio_lvgl_unlock(); +} + +// Whether any active display mirrored into LVGL still wants automatic repaints. The display slots +// are the shared registry (the same ones the legacy compositor uses), so this works for both +// display classes the compositor supports. +bool displayio_lvgl_auto_refresh(void) { + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display_base.type; + if (display_type == NULL || display_type == &mp_type_NoneType) { + continue; + } + #if CIRCUITPY_BUSDISPLAY + if (display_type == &busdisplay_busdisplay_type) { + if (displays[i].display.auto_refresh) { + return true; + } + continue; + } + #endif + #if CIRCUITPY_FRAMEBUFFERIO + if (display_type == &framebufferio_framebufferdisplay_type) { + if (displays[i].framebuffer_display.auto_refresh) { + return true; + } + continue; + } + #endif + } + // No display yet: pumping LVGL is harmless and keeps its own timers running. + return true; +} + +// Render the displays that can't run on the render task. The guard keeps the proxy from +// re-entering LVGL rendering if a background callback is processed while a refresh is already +// active on the CircuitPython thread. +void displayio_lvgl_refresh_unsafe_displays(void) { + #if CIRCUITPY_BUSDISPLAY + static bool refreshing = false; + if (refreshing) { + return; + } + refreshing = true; + displayio_lvgl_lock(); + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display_base.type; + if (display_type != &busdisplay_busdisplay_type) { + continue; + } + busdisplay_busdisplay_obj_t *busdisplay = &displays[i].display; + if (busdisplay->auto_refresh && busdisplay->lv_display != NULL) { + lv_refr_now(busdisplay->lv_display); + } + } + displayio_lvgl_unlock(); + refreshing = false; + #endif +} + +// Render the framebuffer displays that pace themselves from a frame boundary. Like the BusDisplay +// proxy, this runs on the CircuitPython thread and takes the LVGL lock; a frame callback may be +// running on the render task at the same time, so the lock serializes them. +void displayio_lvgl_refresh_paced_displays(void) { + #if CIRCUITPY_FRAMEBUFFERIO + static bool refreshing = false; + if (refreshing) { + return; + } + refreshing = true; + displayio_lvgl_lock(); + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + mp_const_obj_t display_type = displays[i].display_base.type; + if (display_type != &framebufferio_framebufferdisplay_type) { + continue; + } + framebufferio_framebufferdisplay_obj_t *display = &displays[i].framebuffer_display; + if (display->lv_paced && display->auto_refresh && display->lv_display != NULL) { + lv_refr_now(display->lv_display); + } + } + displayio_lvgl_unlock(); + refreshing = false; + #endif +} + +void displayio_lvgl_init(void) { + if (cp_lvgl_inited) { + return; + } + displayio_lvgl_lock(); + // The OnDiskBitmap decoder has to be in place before any TileGrid attaches. Once a display + // exists the compositor is in use; until then leave this unset so the decoder is initialized + // by the display that does show up. + if (lv_screen_active() != NULL) { + displayio_lvgl_ondiskbitmap_decoder_init(); + cp_lvgl_inited = true; + } + 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) { + self->x = x; + self->y = y; + self->members = members; + self->item_removed = false; + self->scale = scale; + self->in_group = false; + self->readonly = false; + self->lv_obj = NULL; + self->lv_scale = 1; + 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) { + mp_obj_list_t *members = mp_obj_new_list(0, NULL); + displayio_group_construct(self, members, scale, x, y); +} + +bool common_hal_displayio_group_get_hidden(displayio_group_t *self) { + return self->hidden; +} + +// Update the mirror object for a group's effective visibility. +static void _update_lvgl_visible(displayio_group_t *self) { + if (self->lv_obj == NULL) { + return; + } + if (self->hidden || self->hidden_by_parent) { + lv_obj_add_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } +} + +static void _propagate_hidden(displayio_group_t *self, bool hidden) { + 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); + if (protocol != NULL && protocol->set_hidden_by_parent != NULL) { + protocol->set_hidden_by_parent(layer, hidden); + } + } +} + +void common_hal_displayio_group_set_hidden(displayio_group_t *self, bool hidden) { + if (self->hidden == hidden) { + return; + } + check_readonly(self); + self->hidden = hidden; + if (self->hidden_by_parent) { + return; + } + _propagate_hidden(self, hidden); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + _update_lvgl_visible(self); + displayio_lvgl_unlock_with_cleanup(); +} + +void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden) { + if (self->hidden_by_parent == hidden) { + return; + } + check_readonly(self); + self->hidden_by_parent = hidden; + if (self->hidden) { + return; + } + _propagate_hidden(self, hidden); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + _update_lvgl_visible(self); + displayio_lvgl_unlock_with_cleanup(); +} + +uint32_t common_hal_displayio_group_get_scale(displayio_group_t *self) { + return self->scale; +} + +void common_hal_displayio_group_set_scale(displayio_group_t *self, uint32_t scale) { + if (self->scale == scale) { + return; + } + check_readonly(self); + if (self->lv_obj == NULL) { + self->scale = scale; + return; + } + uint16_t previous_scale = self->scale; + self->scale = scale; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + bool accepted = _update_children(self); + if (!accepted) { + // 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; + _update_children(self); + } + displayio_lvgl_unlock_with_cleanup(); + if (!accepted) { + mp_raise_ValueError(MP_ERROR_TEXT("Layer does not support scaling")); + } +} + +mp_int_t common_hal_displayio_group_get_x(displayio_group_t *self) { + return self->x; +} + +void common_hal_displayio_group_set_x(displayio_group_t *self, mp_int_t x) { + if (self->x == x) { + return; + } + 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->x = x; + if (self->lv_obj != NULL) { + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Every layer can be translated; only set_scale() can be rejected. + (void)_update_children(self); + displayio_lvgl_unlock_with_cleanup(); + } +} + +mp_int_t common_hal_displayio_group_get_y(displayio_group_t *self) { + return self->y; +} + +void common_hal_displayio_group_set_y(displayio_group_t *self, mp_int_t y) { + if (self->y == y) { + return; + } + 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->y = y; + if (self->lv_obj != NULL) { + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Every layer can be translated; only set_scale() can be rejected. + (void)_update_children(self); + displayio_lvgl_unlock_with_cleanup(); + } +} + +// Resolve a group member to its mirror object, or NULL if it has none yet. +static lv_obj_t *_member_mirror(mp_obj_t layer) { + const lvgl_protocol_t *protocol = _layer_protocol(&layer); + return protocol == NULL ? NULL : protocol->root_obj(layer); +} + +// Create the mirror for a member that doesn't have one yet. Unsupported layers stay NULL. +static lv_obj_t *_attach_member(displayio_group_t *self, mp_obj_t layer) { + const lvgl_protocol_t *protocol = _layer_protocol(&layer); + if (protocol == NULL) { + return NULL; + } + protocol->attach(layer, self->lv_obj); + return protocol->root_obj(layer); +} + +// Adopt a member: validate that it can join, update Python-visible state, and push it into +// the mirror if the group is mirrored. +static void _adopt_member(displayio_group_t *self, mp_obj_t layer) { + const lvgl_protocol_t *protocol = _layer_protocol(&layer); + if (protocol == NULL || protocol->set_in_group == NULL) { + mp_raise_ValueError(MP_ERROR_TEXT("Layer must be a Group, TileGrid, vector shape or VideoBuffer")); + } + if (protocol->set_in_group(layer, true)) { + mp_raise_ValueError(MP_ERROR_TEXT("Layer already in a group")); + } + // Transform/visibility updates can raise too (for example when resuming video). + // A rejected adoption must not leave an otherwise unused layer marked in_group. + nlr_buf_t nlr; + 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)) { + mp_raise_ValueError(MP_ERROR_TEXT("Layer does not support scaling")); + } + if (protocol->set_hidden_by_parent != NULL) { + protocol->set_hidden_by_parent(layer, self->hidden || self->hidden_by_parent); + } + nlr_pop(); + } else { + protocol->set_in_group(layer, false); + nlr_jump(nlr.ret_val); + } +} + +// Release a member and remove its mirror. Must be called under the displayio lock. +static void _release_member(displayio_group_t *self, mp_obj_t layer) { + const lvgl_protocol_t *protocol = _layer_protocol(&layer); + if (protocol == NULL) { + return; + } + if (protocol->set_in_group != NULL) { + protocol->set_in_group(layer, false); + } + protocol->detach(layer); +} + +// Push the mirror children into the same order as the members list. Newly added members +// get mirrors here; children that no longer belong to a member are deleted. Must be +// called under the displayio lock. +static void _sync_members(displayio_group_t *self) { + if (self->lv_obj == NULL) { + return; + } + size_t child_index = 0; + for (size_t i = 0; i < self->members->len; i++) { + mp_obj_t layer = self->members->items[i]; + lv_obj_t *obj = _member_mirror(layer); + if (obj == NULL) { + // Not mirrored yet: try to attach it now (unsupported layers stay NULL). + obj = _attach_member(self, layer); + } + if (obj == NULL) { + continue; + } + if (lv_obj_get_parent(obj) != self->lv_obj) { + lv_obj_set_parent(obj, self->lv_obj); + } + if (lv_obj_get_index(obj) != (int32_t)child_index) { + lv_obj_move_to_index(obj, child_index); + } + child_index++; + } + // Delete mirrors of members that were removed from the group. + while ((uint32_t)lv_obj_get_child_count(self->lv_obj) > child_index) { + lv_obj_t *child = lv_obj_get_child(self->lv_obj, (int32_t)(lv_obj_get_child_count(self->lv_obj) - 1)); + lv_obj_delete(child); + } + // Newly attached members need their position, scale and absolute offset pushed in. Every + // member already accepted this transform in _adopt_member(). + (void)_update_children(self); +} + +void common_hal_displayio_group_insert(displayio_group_t *self, size_t index, mp_obj_t layer) { + check_readonly(self); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + _adopt_member(self, layer); + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + mp_obj_list_insert(self->members, index, layer); + nlr_pop(); + } else { + mp_obj_t native = layer; + const lvgl_protocol_t *protocol = _layer_protocol(&native); + protocol->set_in_group(native, false); + nlr_jump(nlr.ret_val); + } + if (self->lv_obj != NULL) { + _sync_members(self); + } + displayio_lvgl_unlock_with_cleanup(); +} + +mp_obj_t common_hal_displayio_group_pop(displayio_group_t *self, size_t index) { + check_readonly(self); + mp_obj_t layer = self->members->items[index]; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + _release_member(self, layer); + mp_obj_list_pop(self->members, index); + if (self->lv_obj != NULL) { + _sync_members(self); + } + displayio_lvgl_unlock_with_cleanup(); + return layer; +} + +mp_int_t common_hal_displayio_group_index(displayio_group_t *self, mp_obj_t layer) { + mp_obj_t args[] = {self->members, layer}; + mp_obj_t *index = mp_seq_index_obj(self->members->items, self->members->len, 2, args); + return MP_OBJ_SMALL_INT_VALUE(index); +} + +size_t common_hal_displayio_group_get_len(displayio_group_t *self) { + return self->members->len; +} + +mp_obj_t common_hal_displayio_group_get(displayio_group_t *self, size_t index) { + return self->members->items[index]; +} + +void common_hal_displayio_group_set(displayio_group_t *self, size_t index, mp_obj_t layer) { + check_readonly(self); + mp_obj_t previous = self->members->items[index]; + if (previous == layer) { + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Validate the replacement before detaching the current member. Failed assignment + // must leave both the Python list and its LVGL mirrors intact. + _adopt_member(self, layer); + _release_member(self, previous); + mp_obj_list_store(self->members, MP_OBJ_NEW_SMALL_INT(index), layer); + if (self->lv_obj != NULL) { + _sync_members(self); + } + displayio_lvgl_unlock_with_cleanup(); +} + +// protocol_lvgl implementation. A Group is a member of another Group like any other layer; +// its own position and scale are inherited, so it recomputes its origin from the parent's. +// +// A Python subclass of Group (PageLayout, a Tile, ...) is an mp_obj_instance_t that holds the +// native Group in subobj[0], so every function below has to see that native object instead of +// the instance. Resolving it here keeps the type slot on the native type. +static mp_obj_t _group_protocol_self(mp_obj_t layer) { + mp_obj_t native = mp_obj_cast_to_native_base(layer, MP_OBJ_FROM_PTR(&displayio_group_type)); + if (native == MP_OBJ_NULL) { + return layer; + } + mp_obj_assert_native_inited(native); + return native; +} + +static lv_obj_t *_group_protocol_root_obj(mp_obj_t layer) { + return ((displayio_group_t *)MP_OBJ_TO_PTR(layer))->lv_obj; +} + +static void _group_protocol_attach(mp_obj_t layer, lv_obj_t *parent) { + displayio_lvgl_group_attach(MP_OBJ_TO_PTR(layer), parent); +} + +static void _group_protocol_detach(mp_obj_t layer) { + displayio_lvgl_group_detach(MP_OBJ_TO_PTR(layer)); +} + +static bool _group_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, + uint16_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; + if (self->lv_obj == NULL) { + return true; + } + return _update_children(self); +} + +static void _group_protocol_set_hidden_by_parent(mp_obj_t layer, bool hidden) { + displayio_group_set_hidden_by_parent(MP_OBJ_TO_PTR(layer), hidden); +} + +static bool _group_protocol_set_in_group(mp_obj_t layer, bool in_group) { + displayio_group_t *self = MP_OBJ_TO_PTR(layer); + bool was_in_group = self->in_group; + self->in_group = in_group; + return was_in_group; +} + +const lvgl_protocol_t displayio_lvgl_group_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = _group_protocol_self, + .root_obj = _group_protocol_root_obj, + .attach = _group_protocol_attach, + .detach = _group_protocol_detach, + .set_transform = _group_protocol_set_transform, + .set_hidden_by_parent = _group_protocol_set_hidden_by_parent, + .set_in_group = _group_protocol_set_in_group, +}; + +void displayio_lvgl_group_attach(displayio_group_t *self, lv_obj_t *parent) { + if (self->lv_obj == NULL) { + self->lv_obj = lv_obj_create(parent); + if (self->lv_obj == NULL) { + return; + } + // Plain, transparent, screen sized container at the screen origin. See + // _update_children() for why it doesn't follow the group's position. + lv_obj_remove_style_all(self->lv_obj); + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_CLICKABLE); + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_SCROLLABLE); + lv_obj_add_flag(self->lv_obj, LV_OBJ_FLAG_OVERFLOW_VISIBLE); + lv_obj_set_size(self->lv_obj, lv_pct(100), lv_pct(100)); + lv_obj_set_pos(self->lv_obj, 0, 0); + _update_lvgl_visible(self); + } else if (lv_obj_get_parent(self->lv_obj) != parent) { + lv_obj_set_parent(self->lv_obj, parent); + } + _sync_members(self); +} + +void displayio_lvgl_group_detach(displayio_group_t *self) { + if (self->lv_obj == NULL) { + return; + } + // Detach the members first so their object pointers don't dangle when the lv tree + // is deleted below us. + 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); + if (protocol != NULL) { + protocol->detach(layer); + } + } + lv_obj_delete(self->lv_obj); + self->lv_obj = NULL; +} + +void displayio_lvgl_set_root_group(displayio_group_t *group) { + displayio_lvgl_init(); + if (!cp_lvgl_inited) { + // The port never created the LVGL screen (no display); nothing to mirror. + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + if (cp_lvgl_root_group == group) { + displayio_lvgl_unlock_with_cleanup(); + return; + } + if (cp_lvgl_root_group != NULL) { + displayio_lvgl_group_detach(cp_lvgl_root_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_abs_x = group->x; + group->lv_abs_y = group->y; + displayio_lvgl_group_attach(cp_lvgl_root_group, lv_screen_active()); + } + // Repaint the whole screen. The incoming tree may not cover everything the outgoing one did + // (the terminal leaves the top row for its status bar and logo), so the screen background has + // to replace the old content there instead of leaving it on the display. + lv_obj_t *screen = lv_screen_active(); + if (screen != NULL) { + lv_obj_invalidate(screen); + } + displayio_lvgl_unlock_with_cleanup(); +} + +void displayio_gc_collect(void) { + void *root_ptrs[1] = { cp_lvgl_root_group }; + if (root_ptrs[0] != NULL) { + gc_collect_root(root_ptrs, 1); + } + // The shader registry holds vectorio pointers in port memory; root them explicitly. + displayio_lvgl_mirror_gc(); + // So does the compositor's list of attached TileGrids. + displayio_lvgl_tilegrid_gc_collect(); + // Displays live in the static slot registry, which the GC can't see. + displayio_display_slots_gc_collect(); + #if CIRCUITPY_VIDEOCORE + // So does the compositor's list of attached VideoBuffer mirrors. + displayio_lvgl_video_gc_collect(); + #endif +} + +#endif // CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/displayio/lvgl/Group.h b/shared-module/displayio/lvgl/Group.h new file mode 100644 index 00000000000..b94242545fc --- /dev/null +++ b/shared-module/displayio/lvgl/Group.h @@ -0,0 +1,59 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "py/objlist.h" +#include "shared-module/displayio/Palette.h" +#include "shared-module/displayio/lvgl/lvgl_protocol.h" + +#include "lvgl.h" + +// LVGL-based version of displayio Group. The Python-visible state is mirrored into an +// LVGL object tree: a Group maps to a plain lv_obj container whose children are the +// mirrors of its TileGrid and subgroup members. Only the fields below are used by this +// implementation; the scanline compositor's transform/area state lives in +// shared-module/displayio/legacy/Group.h. + +typedef struct { + mp_obj_base_t base; + mp_obj_list_t *members; + int16_t x; + int16_t y; + uint16_t scale; + bool item_removed : 1; + bool in_group : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + bool readonly : 1; + 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. + // 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_set_hidden_by_parent(displayio_group_t *self, bool hidden); + +// Shared LVGL compositor API. Implemented in shared-module/displayio/lvgl/Group.c. +void displayio_lvgl_init(void); +void displayio_lvgl_tick(void); // Run LVGL timers from the background task. +void displayio_lvgl_set_root_group(displayio_group_t *group); // NULL detaches. + +// Group mirror lifecycle. Implemented in Group.c and called by the compositor. +void displayio_lvgl_group_attach(displayio_group_t *self, lv_obj_t *parent); +void displayio_lvgl_group_detach(displayio_group_t *self); + +// How a parent Group attaches, positions and hides this Group. See lvgl_protocol.h. +extern const lvgl_protocol_t displayio_lvgl_group_protocol; diff --git a/shared-module/displayio/lvgl/ImageRoot.c b/shared-module/displayio/lvgl/ImageRoot.c new file mode 100644 index 00000000000..491f493a867 --- /dev/null +++ b/shared-module/displayio/lvgl/ImageRoot.c @@ -0,0 +1,53 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Descriptor-level invalidation, shared by OnDiskBitmap. See ImageRoot.h. + +#include "shared-module/displayio/lvgl/ImageRoot.h" + +#if CIRCUITPY_DISPLAYIO_LVGL + +void displayio_lvgl_image_root_add(displayio_lvgl_image_root_t **roots, lv_obj_t *obj) { + for (displayio_lvgl_image_root_t *root = *roots; root != NULL; root = root->next) { + if (root->obj == obj) { + return; + } + } + displayio_lvgl_image_root_t *root = lv_malloc_zeroed(sizeof(*root)); + if (root == NULL) { + return; + } + root->obj = obj; + root->next = *roots; + *roots = root; +} + +void displayio_lvgl_image_root_remove(displayio_lvgl_image_root_t **roots, lv_obj_t *obj) { + displayio_lvgl_image_root_t **link = roots; + while (*link != NULL) { + if ((*link)->obj == obj) { + displayio_lvgl_image_root_t *found = *link; + *link = found->next; + lv_free(found); + return; + } + link = &(*link)->next; + } +} + +void displayio_lvgl_image_invalidate_all(displayio_lvgl_image_root_t *roots, + const lv_image_dsc_t *dsc) { + if (dsc->data == NULL) { + return; + } + for (displayio_lvgl_image_root_t *root = roots; root != NULL; root = root->next) { + // A TileGrid mirror is either the image itself or a container holding the images, and + // both cover exactly the grid's area. + lv_obj_invalidate(root->obj); + } +} + +#endif // CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/displayio/lvgl/ImageRoot.h b/shared-module/displayio/lvgl/ImageRoot.h new file mode 100644 index 00000000000..cf97b400f29 --- /dev/null +++ b/shared-module/displayio/lvgl/ImageRoot.h @@ -0,0 +1,33 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" + +#include "lvgl.h" + +// Descriptor-level invalidation. An image descriptor can be drawn by several lv_objs (an +// OnDiskBitmap hands one descriptor per decode mode to every TileGrid that shows the file), +// so the owner keeps the list of objects to invalidate when the descriptor's content changes. +// +// The composed TileGrid path doesn't need this: its descriptors belong to the TileGrid, which +// invalidates its own objects. + +typedef struct displayio_lvgl_image_root { + lv_obj_t *obj; + struct displayio_lvgl_image_root *next; +} displayio_lvgl_image_root_t; + +// Add/remove an object that draws a descriptor. Idempotent. Caller holds the LVGL lock. +void displayio_lvgl_image_root_add(displayio_lvgl_image_root_t **roots, lv_obj_t *obj); +void displayio_lvgl_image_root_remove(displayio_lvgl_image_root_t **roots, lv_obj_t *obj); +// Invalidate every object that draws the descriptor. Caller holds the LVGL lock. +void displayio_lvgl_image_invalidate_all(displayio_lvgl_image_root_t *roots, + const lv_image_dsc_t *dsc); diff --git a/shared-module/displayio/lvgl/OnDiskBitmap.c b/shared-module/displayio/lvgl/OnDiskBitmap.c new file mode 100644 index 00000000000..ae22ef4948c --- /dev/null +++ b/shared-module/displayio/lvgl/OnDiskBitmap.c @@ -0,0 +1,816 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2025 SamantazFox +// +// SPDX-License-Identifier: MIT + +// LVGL-based displayio OnDiskBitmap implementation. +// +// The BMP is parsed here, but the pixels stay in the file. A custom LVGL image decoder +// (registered by displayio_lvgl_init()) serves them to LVGL: +// +// - When the file lives in mapped flash (CIRCUITPY_STORAGE_MAP_FILE), each request is a +// one-row draw buffer that points straight at the flash bytes. No copy, no seek. +// - Otherwise, the whole image is decoded into an lv_draw_buf when a TileGrid attaches +// (on the CircuitPython thread, so the render thread never touches VFS). That is the +// memory cost OnDiskBitmap normally avoids, but it is the only thread-safe option for +// files on SD cards. +// - A TileGrid that inherits a scale needs a whole image too: LVGL can't transform a +// partially decoded image. +// +// The file owns one lv_image_dsc_t per decode mode (streaming rows and whole image), whose +// `data` points at this object and whose `reserved` carries the decoder tag. TileGrids share +// them, so a Palette change reaches every image drawn from the file without going through a +// TileGrid. For indexed files the Palette is converted to lv_color32_t entries here, and the +// decoder expands rows through them. + +#include "shared-bindings/displayio/OnDiskBitmap.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-module/displayio/ColorConverter.h" +#include "shared-module/displayio/Palette.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +// Private image-decoder descriptor (lives in different directories in LVGL trees). +#include "lvgl_private.h" + +#include + +#include "py/mperrno.h" +#include "py/runtime.h" +#include "py/stream.h" +#if CIRCUITPY_STORAGE_MAP_FILE +#include "shared-bindings/storage/__init__.h" +#endif + + +#define DISPLAYIO_ODBMP_DEBUG(...) (void)0 +// #define DISPLAYIO_ODBMP_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__) + +// Tags an lv_image_dsc_t as describing a displayio OnDiskBitmap. Shared with TileGrid.c. +const char displayio_lvgl_ondiskbitmap_tag = 0; + +typedef struct odb_decoder { + displayio_ondiskbitmap_t *odb; + // One-row scratch used when a mapped row is split across flash runs or needs conversion. + uint8_t *row_data; + uint32_t row_stride; + lv_draw_buf_t row; + lv_draw_buf_t wrap; +} odb_decoder_t; + +// The decoder opens once per draw of every image, so its scratch lives on the OnDiskBitmap +// instead of being allocated and freed each time. Opens never nest: LVGL draws one task at a +// time on its own thread, and `open`/`close` are the only places that touch it. +static void odb_decoder_free(displayio_ondiskbitmap_t *odb) { + odb_decoder_t *s = (odb_decoder_t *)odb->lv_decoder; + if (s == NULL) { + return; + } + if (s->row_data != NULL) { + lv_free(s->row_data); + } + lv_free(s); + odb->lv_decoder = NULL; +} + +static uint32_t read_word(uint16_t *bmp_header, uint16_t index) { + return bmp_header[index] | bmp_header[index + 1] << 16; +} + +// Bytes of pixel data in one row, without the BMP row padding. +static uint32_t row_bytes(displayio_ondiskbitmap_t *self) { + return ((uint32_t)self->width * self->bits_per_pixel + 7) / 8; +} + +// True when file rows have to be converted instead of passed through to LVGL (indexed +// pixels expand through the Palette, 555/bitfield 16bpp becomes XRGB8888). +static bool row_converts(displayio_ondiskbitmap_t *self) { + return self->bits_per_pixel <= 8 || + (self->bits_per_pixel == 16 && self->g_bitmask != 0x07e0); +} + +lv_color_format_t displayio_ondiskbitmap_lv_color_format(displayio_ondiskbitmap_t *self) { + switch (self->bits_per_pixel) { + case 1: + case 2: + case 4: + case 8: + // The software renderer only blends I1 natively, so indexed rows are + // expanded through the Palette to ARGB8888 (transparent entries keep alpha 0). + return LV_COLOR_FORMAT_ARGB8888; + case 16: + // 565 is LVGL's native little-endian RGB565; 555 and bitfields are converted. + return row_converts(self) ? LV_COLOR_FORMAT_XRGB8888 : LV_COLOR_FORMAT_RGB565; + case 24: + // BMP stores B, G, R, which is LVGL's RGB888 byte order. + return LV_COLOR_FORMAT_RGB888; + case 32: + // BMP stores B, G, R, X; LVGL's XRGB8888 ignores the top byte like the legacy + // compositor does. + return LV_COLOR_FORMAT_XRGB8888; + default: + return LV_COLOR_FORMAT_UNKNOWN; + } +} + +static void ensure_full_image(displayio_ondiskbitmap_t *self); + +// Rebuild the decoder's LVGL copy of the Palette after it changed. +static void refresh_palette(displayio_ondiskbitmap_t *self) { + if (self->lv_palette == NULL || self->palette == NULL) { + return; + } + displayio_lvgl_palette_fill(self->palette, self->lv_palette, + 1u << self->bits_per_pixel); + self->lv_palette_generation++; + // The decoded image has the old palette baked in; redo it on the CP thread. + if (self->lv_decoded != NULL && self->lv_decoded_generation != self->lv_palette_generation) { + lv_draw_buf_destroy(self->lv_decoded); + self->lv_decoded = NULL; + ensure_full_image(self); + } +} + +#if CIRCUITPY_STORAGE_MAP_FILE +static void add_run(void *self_in, const uint8_t *addr, size_t len) { + displayio_ondiskbitmap_t *self = self_in; + if (self->map_count < MP_ARRAY_SIZE(self->map)) { + self->map[self->map_count].addr = addr; + self->map[self->map_count].len = len; + self->map_count++; + } else { + self->map_count = MP_ARRAY_SIZE(self->map) + 1; // too fragmented: decode into RAM instead + } +} + +// Flash address of len bytes at offset if they lie in one run, else NULL. +static const uint8_t *map_at(displayio_ondiskbitmap_t *self, uint32_t offset, uint32_t len) { + for (uint8_t i = 0; i < self->map_count && i < MP_ARRAY_SIZE(self->map); i++) { + if (offset < self->map[i].len) { + return offset + len <= self->map[i].len ? self->map[i].addr + offset : NULL; + } + offset -= self->map[i].len; + } + return NULL; +} + +// Copy len bytes at offset out of the mapped runs. Works across run boundaries by going +// byte by byte; returns false if the file isn't mapped at that offset. +static bool copy_mapped(displayio_ondiskbitmap_t *self, uint32_t offset, uint32_t len, uint8_t *dst) { + const uint8_t *p = map_at(self, offset, len); + if (p != NULL) { + memcpy(dst, p, len); + return true; + } + for (uint32_t i = 0; i < len; i++) { + p = map_at(self, offset + i, 1); + if (p == NULL) { + return false; + } + dst[i] = *p; + } + return true; +} + +// Row y of an 8-bit file in flash, or NULL if it spans two runs or the file isn't mapped. +const uint8_t *displayio_ondiskbitmap_row(displayio_ondiskbitmap_t *self, int16_t y) { + uint32_t file_y = self->top_down ? y : self->height - y - 1; + return map_at(self, self->data_offset + file_y * self->stride, self->width); +} +#endif + +static bool is_mapped(displayio_ondiskbitmap_t *self) { + #if CIRCUITPY_STORAGE_MAP_FILE + return self->map_count > 0 && self->map_count <= MP_ARRAY_SIZE(self->map); + #else + (void)self; + return false; + #endif +} + +// Read one whole row of pixel data into dst. Runs on the CircuitPython thread. +static bool read_row(displayio_ondiskbitmap_t *self, uint32_t file_y, uint8_t *dst, uint32_t len) { + uint32_t offset = self->data_offset + file_y * self->stride; + #if CIRCUITPY_STORAGE_MAP_FILE + if (is_mapped(self)) { + return copy_mapped(self, offset, len, dst); + } + #endif + int errcode = 0; + mp_stream_seek(self->file, offset, MP_SEEK_SET, &errcode); + if (errcode != 0) { + return false; + } + mp_uint_t n = mp_stream_rw(self->file, dst, len, &errcode, MP_STREAM_RW_ONCE); + return errcode == 0 && n == len; +} + +// Pull one BGR channel out of a 16bpp pixel with arbitrary bitfields. +static uint8_t extract_channel(uint32_t pixel, uint32_t mask) { + if (mask == 0) { + return 0; + } + uint32_t shift = 0; + while (((mask >> shift) & 1u) == 0) { + shift++; + } + uint32_t max = mask >> shift; + return (uint8_t)((((pixel & mask) >> shift) * 255u + max / 2) / max); +} + +// Convert a 16bpp row (BGR in little-endian) to XRGB8888. +static void convert_row_16(displayio_ondiskbitmap_t *self, const uint8_t *src, uint8_t *dst, + uint32_t width) { + for (uint32_t x = 0; x < width; x++) { + uint16_t pixel = src[0] | (src[1] << 8); + dst[0] = extract_channel(pixel, self->b_bitmask); + dst[1] = extract_channel(pixel, self->g_bitmask); + dst[2] = extract_channel(pixel, self->r_bitmask); + dst[3] = 0xff; + src += 2; + dst += 4; + } +} + +// Expand one indexed row (MSB first, like BMP) through the Palette to ARGB8888. +static void convert_row_indexed(displayio_ondiskbitmap_t *self, const uint8_t *src, uint8_t *dst, + uint32_t width) { + uint32_t mask = (1u << self->bits_per_pixel) - 1u; + for (uint32_t x = 0; x < width; x++) { + uint32_t bit = x * self->bits_per_pixel; + uint8_t shift = 8 - self->bits_per_pixel - (bit & 7); + uint32_t index = (src[bit >> 3] >> shift) & mask; + lv_color32_t color = self->lv_palette[index]; + dst[0] = color.blue; + dst[1] = color.green; + dst[2] = color.red; + dst[3] = color.alpha; + dst += 4; + } +} + +// Decode the whole image into RAM. Called on the CircuitPython thread while a TileGrid +// attaches (or starts scaling), never from the render thread. +static lv_draw_buf_t *decode_all(displayio_ondiskbitmap_t *self) { + lv_color_format_t cf = displayio_ondiskbitmap_lv_color_format(self); + if (cf == LV_COLOR_FORMAT_UNKNOWN) { + return NULL; + } + lv_draw_buf_t *buf = lv_draw_buf_create(self->width, self->height, cf, LV_STRIDE_AUTO); + if (buf == NULL) { + return NULL; + } + uint32_t src_len = row_bytes(self); + uint8_t *src = lv_malloc(src_len); + if (src == NULL) { + lv_draw_buf_destroy(buf); + return NULL; + } + bool converts = row_converts(self); + uint32_t dst_stride = buf->header.stride; + for (uint32_t y = 0; y < self->height; y++) { + uint32_t file_y = self->top_down ? y : self->height - 1 - y; + if (!read_row(self, file_y, src, src_len)) { + lv_free(src); + lv_draw_buf_destroy(buf); + return NULL; + } + uint8_t *dst = (uint8_t *)buf->data + y * dst_stride; + if (self->bits_per_pixel <= 8) { + convert_row_indexed(self, src, dst, self->width); + } else if (converts) { + convert_row_16(self, src, dst, self->width); + } else { + memcpy(dst, src, src_len); + } + } + lv_free(src); + return buf; +} + +// Does any attached TileGrid need the whole image in RAM / as one draw buffer? +static bool uses_full_image(displayio_ondiskbitmap_t *self) { + return self->lv_full_users > 0 || !is_mapped(self); +} + +static void ensure_full_image(displayio_ondiskbitmap_t *self) { + if (self->lv_decoded != NULL) { + return; + } + // A top-down, contiguous mapped file of a direct color format can be drawn from + // flash directly (see decoder open); indexed rows need the palette expansion. + if (!row_converts(self) && self->lv_mapped_all != NULL && self->top_down && + self->height > 0) { + return; + } + self->lv_decoded = decode_all(self); + self->lv_decoded_generation = self->lv_palette_generation; +} + +// Build the descriptor for one decode mode. LVGL's decoder recognizes an OnDiskBitmap by +// these two fields: data points at the file object and reserved carries the tag. +static void init_dsc(displayio_ondiskbitmap_t *self, lv_image_dsc_t *dsc) { + lv_image_dsc_t *d = dsc; + lv_memzero(d, sizeof(*d)); + lv_color_format_t cf = displayio_ondiskbitmap_lv_color_format(self); + d->header.magic = LV_IMAGE_HEADER_MAGIC; + d->header.cf = cf; + d->header.w = self->width; + d->header.h = self->height; + d->header.stride = lv_draw_buf_width_to_stride(self->width, cf); + d->data = (const uint8_t *)self; + d->reserved = &displayio_lvgl_ondiskbitmap_tag; +} + +const lv_image_dsc_t *displayio_ondiskbitmap_lv_acquire(displayio_ondiskbitmap_t *self, bool full, + lv_obj_t *root) { + uint8_t mode = full ? 1 : 0; + self->lv_users++; + if (full) { + self->lv_full_users++; + } + if (uses_full_image(self)) { + ensure_full_image(self); + } + init_dsc(self, &self->lv_dsc[mode]); + displayio_lvgl_image_root_add((displayio_lvgl_image_root_t **)&self->lv_roots[mode], root); + if (self->lv_users == 1 && self->palette != NULL) { + // Indexed files draw through their own Palette, so it has to reach the decoder. + displayio_lvgl_shader_register_user(MP_OBJ_FROM_PTR(self->palette), self, + DISPLAYIO_LVGL_USER_ONDISKBITMAP); + } + return &self->lv_dsc[mode]; +} + +void displayio_ondiskbitmap_lv_release(displayio_ondiskbitmap_t *self, bool full, lv_obj_t *root) { + uint8_t mode = full ? 1 : 0; + displayio_lvgl_image_root_remove((displayio_lvgl_image_root_t **)&self->lv_roots[mode], root); + lv_image_cache_drop(&self->lv_dsc[mode]); + if (self->lv_users > 0) { + self->lv_users--; + } + if (full && self->lv_full_users > 0) { + self->lv_full_users--; + } + if (self->lv_users == 0) { + self->lv_full_users = 0; + lv_memzero(&self->lv_dsc[0], sizeof(self->lv_dsc[0])); + lv_memzero(&self->lv_dsc[1], sizeof(self->lv_dsc[1])); + if (self->palette != NULL) { + displayio_lvgl_shader_unregister_user(MP_OBJ_FROM_PTR(self->palette), self); + } + if (self->lv_decoded != NULL) { + lv_draw_buf_destroy(self->lv_decoded); + self->lv_decoded = NULL; + } + odb_decoder_free(self); + } else if (!uses_full_image(self) && self->lv_decoded != NULL) { + // Back to streaming rows: give the RAM copy back. + lv_draw_buf_destroy(self->lv_decoded); + self->lv_decoded = NULL; + } +} + +void displayio_ondiskbitmap_lv_palette_changed(displayio_ondiskbitmap_t *self) { + refresh_palette(self); + for (uint8_t mode = 0; mode < 2; mode++) { + lv_image_cache_drop(&self->lv_dsc[mode]); + displayio_lvgl_image_invalidate_all( + (displayio_lvgl_image_root_t *)self->lv_roots[mode], &self->lv_dsc[mode]); + } +} + +// --- LVGL image decoder ------------------------------------------------------ + +static lv_result_t odb_decoder_info(lv_image_decoder_t *decoder, lv_image_decoder_dsc_t *dsc, + lv_image_header_t *header) { + (void)decoder; + if (dsc->src_type != LV_IMAGE_SRC_VARIABLE) { + return LV_RESULT_INVALID; + } + const lv_image_dsc_t *src = dsc->src; + if (src->reserved != &displayio_lvgl_ondiskbitmap_tag || src->data == NULL) { + return LV_RESULT_INVALID; + } + displayio_ondiskbitmap_t *odb = (displayio_ondiskbitmap_t *)(void *)src->data; + lv_color_format_t cf = displayio_ondiskbitmap_lv_color_format(odb); + if (cf == LV_COLOR_FORMAT_UNKNOWN) { + return LV_RESULT_INVALID; + } + header->cf = cf; + header->w = odb->width; + header->h = odb->height; + header->stride = lv_draw_buf_width_to_stride(odb->width, cf); + return LV_RESULT_OK; +} + +static lv_result_t odb_decoder_open(lv_image_decoder_t *decoder, lv_image_decoder_dsc_t *dsc) { + (void)decoder; + const lv_image_dsc_t *src = dsc->src; + displayio_ondiskbitmap_t *odb = (displayio_ondiskbitmap_t *)(void *)src->data; + lv_color_format_t cf = dsc->header.cf; + if (cf == LV_COLOR_FORMAT_UNKNOWN) { + return LV_RESULT_INVALID; + } + + odb_decoder_t *s = (odb_decoder_t *)odb->lv_decoder; + if (s == NULL) { + s = lv_malloc_zeroed(sizeof(odb_decoder_t)); + if (s == NULL) { + return LV_RESULT_INVALID; + } + s->odb = odb; + odb->lv_decoder = s; + } + dsc->user_data = s; + + if (uses_full_image(odb)) { + if (odb->lv_decoded != NULL) { + dsc->decoded = odb->lv_decoded; + return LV_RESULT_OK; + } + if (!row_converts(odb) && odb->lv_mapped_all != NULL && odb->top_down && + odb->height > 0) { + uint32_t size = (uint32_t)odb->stride * odb->height; + if (lv_draw_buf_init(&s->wrap, odb->width, odb->height, cf, odb->stride, + (void *)odb->lv_mapped_all, size) == LV_RESULT_OK) { + dsc->decoded = &s->wrap; + return LV_RESULT_OK; + } + } + // Fall through to row streaming when the file is mapped. + } + + if (s->row_data == NULL) { + s->row_stride = lv_draw_buf_width_to_stride(odb->width, cf); + if (s->row_stride < odb->stride) { + s->row_stride = odb->stride; + } + s->row_data = lv_malloc(s->row_stride); + if (s->row_data == NULL) { + return LV_RESULT_INVALID; + } + } + return LV_RESULT_OK; +} + +static lv_result_t odb_decoder_get_area(lv_image_decoder_t *decoder, lv_image_decoder_dsc_t *dsc, + const lv_area_t *full_area, lv_area_t *decoded_area) { + (void)decoder; + odb_decoder_t *s = dsc->user_data; + displayio_ondiskbitmap_t *odb = s->odb; + + if (decoded_area->y1 == LV_COORD_MIN) { + *decoded_area = *full_area; + decoded_area->y2 = decoded_area->y1; + } else { + decoded_area->y1++; + decoded_area->y2++; + if (decoded_area->y1 > full_area->y2) { + return LV_RESULT_INVALID; + } + } + + // Serve whole rows in image coordinates. LVGL intersects the decoded area with the + // clip area, so this also covers a TileGrid cell showing a horizontal window. + decoded_area->x1 = 0; + decoded_area->x2 = odb->width - 1; + + if (decoded_area->y1 < 0 || decoded_area->y1 >= odb->height) { + return LV_RESULT_INVALID; + } + uint32_t y = decoded_area->y1; + lv_color_format_t cf = dsc->header.cf; + const uint8_t *row = NULL; + + #if CIRCUITPY_STORAGE_MAP_FILE + uint32_t file_y = odb->top_down ? y : odb->height - 1 - y; + uint32_t offset = odb->data_offset + file_y * odb->stride; + uint32_t len = row_bytes(odb); + if (is_mapped(odb)) { + row = map_at(odb, offset, len); + if (row == NULL) { + // The row crosses a run boundary; copy it together in RAM. + if (!copy_mapped(odb, offset, len, s->row_data)) { + return LV_RESULT_INVALID; + } + row = s->row_data; + } + } + #else + (void)y; + #endif + if (row == NULL) { + // Not mapped. Attach decodes the whole image, so streaming here would mean the + // decode failed; don't touch the file from the render thread. + return LV_RESULT_INVALID; + } + + if (row_converts(odb)) { + if (odb->bits_per_pixel <= 8) { + convert_row_indexed(odb, row, s->row_data, odb->width); + } else { + convert_row_16(odb, row, s->row_data, odb->width); + } + if (lv_draw_buf_init(&s->row, odb->width, 1, cf, s->row_stride, s->row_data, + s->row_stride) != LV_RESULT_OK) { + return LV_RESULT_INVALID; + } + } else if (lv_draw_buf_init(&s->row, odb->width, 1, cf, odb->stride, (void *)row, + odb->stride) != LV_RESULT_OK) { + return LV_RESULT_INVALID; + } + dsc->decoded = &s->row; + return LV_RESULT_OK; +} + +static void odb_decoder_close(lv_image_decoder_t *decoder, lv_image_decoder_dsc_t *dsc) { + (void)decoder; + // The scratch stays on the OnDiskBitmap for the next draw. + dsc->user_data = NULL; +} + +void displayio_lvgl_ondiskbitmap_decoder_init(void) { + lv_image_decoder_t *dec = lv_image_decoder_create(); + if (dec == NULL) { + return; + } + lv_image_decoder_set_info_cb(dec, odb_decoder_info); + lv_image_decoder_set_open_cb(dec, odb_decoder_open); + lv_image_decoder_set_get_area_cb(dec, odb_decoder_get_area); + lv_image_decoder_set_close_cb(dec, odb_decoder_close); + dec->name = "CP OnDiskBitmap"; +} + +// --- displayio API ----------------------------------------------------------- + +void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, mp_obj_t file) { + self->file = file; + self->cache_len = 0; + self->cache_start = 0; + self->lv_mapped_all = NULL; + self->lv_decoded = NULL; + self->lv_palette = NULL; + self->lv_palette_generation = 0; + self->lv_decoded_generation = 0; + lv_memzero(self->lv_dsc, sizeof(self->lv_dsc)); + self->lv_decoder = NULL; + self->lv_roots[0] = NULL; + self->lv_roots[1] = NULL; + self->lv_users = 0; + self->lv_full_users = 0; + uint16_t bmp_header[69]; + int errcode = 0; + + mp_stream_seek(file, 0, MP_SEEK_SET, &errcode); + if (errcode != 0) { + mp_raise_OSError(errcode); + } + + // Read the minimum amount of bytes required to parse a BITMAPCOREHEADER. + // If needed, we will read more bytes down below. + mp_uint_t bytes_read = mp_stream_rw(file, bmp_header, 26, &errcode, MP_STREAM_RW_ONCE); + if (errcode != 0) { + mp_raise_OSError(errcode); + } + if (bytes_read != 26 || memcmp(bmp_header, "BM", 2) != 0) { + mp_arg_error_invalid(MP_QSTR_file); + } + + // Read header size to determine if more header bytes needs to be read. + uint32_t header_size = read_word(bmp_header, 7); + DISPLAYIO_ODBMP_DEBUG("header_size: %d\n", header_size); + + if (header_size == 40 || header_size == 108 || header_size == 124) { + // Read the remaining header bytes + bytes_read = mp_stream_rw(file, bmp_header + 13, header_size - 12, &errcode, MP_STREAM_RW_ONCE); + if (errcode != 0) { + mp_raise_OSError(errcode); + } + DISPLAYIO_ODBMP_DEBUG("bytes_read: %d\n", bytes_read); + if (bytes_read != (header_size - 12)) { + mp_arg_error_invalid(MP_QSTR_file); + } + } else if (header_size != 12) { + mp_raise_ValueError_varg(MP_ERROR_TEXT("Only Windows format, uncompressed BMP supported: given header size is %d"), header_size); + } + + + uint32_t compression = read_word(bmp_header, 15); + DISPLAYIO_ODBMP_DEBUG("compression: %d\n", compression); + + // 0 is uncompressed; 3 is bitfield compressed. 1 and 2 are RLE compression. + if (compression != 0 && compression != 3) { + mp_raise_ValueError(MP_ERROR_TEXT("RLE-compressed BMP not supported")); + } + + // We can't cast because we're not aligned. + self->data_offset = read_word(bmp_header, 5); + + self->bitfield_compressed = (compression == 3); + self->bits_per_pixel = bmp_header[14]; + self->width = read_word(bmp_header, 9); + // A negative height means the rows are stored top to bottom. + int32_t bmp_height = (int16_t)read_word(bmp_header, 11); + self->top_down = bmp_height < 0; + if (bmp_height < 0) { + bmp_height = -bmp_height; + } + self->height = (uint16_t)bmp_height; + + DISPLAYIO_ODBMP_DEBUG("data offset: %d\n", self->data_offset); + DISPLAYIO_ODBMP_DEBUG("width: %d\n", self->width); + DISPLAYIO_ODBMP_DEBUG("height: %d\n", self->height); + DISPLAYIO_ODBMP_DEBUG("bpp: %d\n", self->bits_per_pixel); + + + displayio_colorconverter_t *colorconverter = + mp_obj_malloc(displayio_colorconverter_t, &displayio_colorconverter_type); + common_hal_displayio_colorconverter_construct(colorconverter, false, DISPLAYIO_COLORSPACE_RGB888); + self->colorconverter = colorconverter; + + if (self->bits_per_pixel == 16) { + if (((header_size >= 56)) || (self->bitfield_compressed)) { + self->r_bitmask = read_word(bmp_header, 27); + self->g_bitmask = read_word(bmp_header, 29); + self->b_bitmask = read_word(bmp_header, 31); + + } else { // no compression or short header means 5:5:5 + self->r_bitmask = 0x7c00; + self->g_bitmask = 0x3e0; + self->b_bitmask = 0x1f; + } + } else if (self->bits_per_pixel <= 8) { // indexed + uint32_t number_of_colors = 0; + if (header_size >= 40) { + number_of_colors = read_word(bmp_header, 23); + } + if (number_of_colors == 0) { + number_of_colors = 1 << self->bits_per_pixel; + } + + displayio_palette_t *palette = mp_obj_malloc(displayio_palette_t, &displayio_palette_type); + common_hal_displayio_palette_construct(palette, number_of_colors, false); + + if (number_of_colors > 1) { + uint16_t palette_size = number_of_colors * sizeof(uint32_t); + uint16_t palette_offset = 0xe + header_size; + + uint32_t *palette_data = m_malloc_without_collect(palette_size); + + mp_stream_seek(file, palette_offset, MP_SEEK_SET, &errcode); + if (errcode != 0) { + mp_raise_OSError(errcode); + } + + mp_uint_t palette_bytes_read = mp_stream_rw(file, palette_data, palette_size, &errcode, MP_STREAM_RW_ONCE); + if (errcode != 0) { + mp_raise_OSError(errcode); + } + if (palette_bytes_read != palette_size) { + mp_raise_ValueError(MP_ERROR_TEXT("Unable to read color palette data")); + } + for (uint16_t i = 0; i < number_of_colors; i++) { + common_hal_displayio_palette_set_color(palette, i, palette_data[i]); + } + + #if MICROPY_MALLOC_USES_ALLOCATED_SIZE + m_free(palette_data, palette_size); + #else + m_free(palette_data); + #endif + } else { + common_hal_displayio_palette_set_color(palette, 0, 0x0); + common_hal_displayio_palette_set_color(palette, 1, 0xffffff); + } + self->palette = palette; + + // The decoder expands indexed rows through this LVGL copy of the palette. + uint32_t lv_palette_count = 1u << self->bits_per_pixel; + self->lv_palette = m_malloc_without_collect(lv_palette_count * sizeof(lv_color32_t)); + refresh_palette(self); + } + + uint8_t bytes_per_pixel = (self->bits_per_pixel / 8) ? (self->bits_per_pixel / 8) : 1; + uint8_t pixels_per_byte = 8 / self->bits_per_pixel; + if (pixels_per_byte == 0) { + self->stride = (self->width * bytes_per_pixel); + // Rows are word aligned. + if (self->stride % 4 != 0) { + self->stride += 4 - self->stride % 4; + } + } else { + uint32_t bit_stride = self->width * self->bits_per_pixel; + if (bit_stride % 32 != 0) { + bit_stride += 32 - bit_stride % 32; + } + self->stride = (bit_stride / 8); + } + + #if CIRCUITPY_STORAGE_MAP_FILE + self->map_count = 0; + self->rows_mapped = false; + if (storage_map_runs(file, add_run, self) != 0 || self->map_count > MP_ARRAY_SIZE(self->map)) { + self->map_count = 0; + } + if (self->map_count > 0 && self->height > 0) { + // Whole pixel block in one run? Then a transform can draw straight from flash. + uint32_t size = (uint32_t)(self->height - 1) * self->stride + row_bytes(self); + self->lv_mapped_all = map_at(self, self->data_offset, size); + } + #endif +} + + +void common_hal_displayio_ondiskbitmap_deinit(displayio_ondiskbitmap_t *self) { + if (common_hal_displayio_ondiskbitmap_deinited(self)) { + return; + } + mp_stream_close(self->file); + self->file = NULL; + #if CIRCUITPY_STORAGE_MAP_FILE + // A TileGrid may still draw this bitmap, so it must stop reading rows from mapped + // flash: the file is closed and the mapping can go away. Nothing decodes after this, + // so a redraw paints color 0 like the legacy compositor does. + displayio_lvgl_lock(); + self->map_count = 0; + self->lv_mapped_all = NULL; + displayio_lvgl_unlock(); + #endif +} + +bool common_hal_displayio_ondiskbitmap_deinited(displayio_ondiskbitmap_t *self) { + return self->file == NULL; +} + +uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *self, + int16_t x, int16_t y) { + if (common_hal_displayio_ondiskbitmap_deinited(self) || + x < 0 || x >= self->width || y < 0 || y >= self->height) { + return 0; + } + + uint32_t location; + uint8_t bytes_per_pixel = (self->bits_per_pixel / 8) ? (self->bits_per_pixel / 8) : 1; + uint8_t pixels_per_byte = 8 / self->bits_per_pixel; + uint32_t file_y = self->top_down ? y : self->height - y - 1; + if (pixels_per_byte == 0) { + location = self->data_offset + file_y * self->stride + x * bytes_per_pixel; + } else { + location = self->data_offset + file_y * self->stride + x / pixels_per_byte; + } + int errcode = 0; + if (location < self->cache_start || location + bytes_per_pixel > self->cache_start + self->cache_len) { + mp_stream_seek(self->file, location, MP_SEEK_SET, &errcode); + if (errcode != 0) { + return 0; + } + self->cache_start = location; + self->cache_len = mp_stream_rw(self->file, self->cache, sizeof(self->cache), &errcode, MP_STREAM_RW_ONCE); + if (errcode != 0 || self->cache_len < bytes_per_pixel) { + self->cache_len = 0; + return 0; + } + } + uint32_t pixel_data = 0; + memcpy(&pixel_data, self->cache + (location - self->cache_start), bytes_per_pixel); + uint32_t tmp = 0; + uint8_t red; + uint8_t green; + uint8_t blue; + if (bytes_per_pixel == 1) { + uint8_t offset = (x % pixels_per_byte) * self->bits_per_pixel; + uint8_t mask = (1 << self->bits_per_pixel) - 1; + + return (pixel_data >> ((8 - self->bits_per_pixel) - offset)) & mask; + } else if (bytes_per_pixel == 2) { + if (self->g_bitmask == 0x07e0) { // 565 + red = ((pixel_data & self->r_bitmask) >> 11); + green = ((pixel_data & self->g_bitmask) >> 5); + blue = ((pixel_data & self->b_bitmask) >> 0); + } else { // 555 + red = ((pixel_data & self->r_bitmask) >> 10); + green = ((pixel_data & self->g_bitmask) >> 4); + blue = ((pixel_data & self->b_bitmask) >> 0); + } + tmp = (red << 19 | green << 10 | blue << 3); + return tmp; + } else if ((bytes_per_pixel == 4) && (self->bitfield_compressed)) { + return pixel_data & 0x00FFFFFF; + } else { + return pixel_data; + } +} + +uint16_t common_hal_displayio_ondiskbitmap_get_height(displayio_ondiskbitmap_t *self) { + return self->height; +} + +uint16_t common_hal_displayio_ondiskbitmap_get_width(displayio_ondiskbitmap_t *self) { + return self->width; +} + +mp_obj_t common_hal_displayio_ondiskbitmap_get_pixel_shader(displayio_ondiskbitmap_t *self) { + return MP_OBJ_FROM_PTR(self->pixel_shader_base); +} diff --git a/shared-module/displayio/lvgl/OnDiskBitmap.h b/shared-module/displayio/lvgl/OnDiskBitmap.h new file mode 100644 index 00000000000..ec924d6ed3a --- /dev/null +++ b/shared-module/displayio/lvgl/OnDiskBitmap.h @@ -0,0 +1,89 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "lvgl.h" + +typedef struct { + mp_obj_base_t base; + uint16_t width; + uint16_t height; + uint32_t data_offset; + uint16_t stride; + uint32_t r_bitmask; + uint32_t g_bitmask; + uint32_t b_bitmask; + mp_obj_t file; + union { + mp_obj_base_t *pixel_shader_base; + struct displayio_palette *palette; + struct displayio_colorconverter *colorconverter; + }; + bool bitfield_compressed; + uint8_t bits_per_pixel; + // The last bytes read from the file. Pixels are read along rows, so most reads hit it. + uint8_t cache_len; + #if CIRCUITPY_STORAGE_MAP_FILE + // Flash runs the pixels are read from instead of the cache; 0 = read through the cache. + uint8_t map_count; + // 8-bit file with every row inside one run, so TileGrid can read whole rows. + bool rows_mapped; + #endif + uint32_t cache_start; + union { + uint8_t cache[64]; + #if CIRCUITPY_STORAGE_MAP_FILE + struct { + const uint8_t *addr; + uint32_t len; + } map[8]; + #endif + }; + // A negative BMP height means the rows are stored top to bottom. + bool top_down; + // LVGL backend state. The pixels are exposed through a custom image decoder (see + // shared-module/displayio/lvgl/OnDiskBitmap.c) so the file can be drawn without + // loading it into RAM when it lives in mapped flash. + const uint8_t *lv_mapped_all; + lv_draw_buf_t *lv_decoded; + lv_color32_t *lv_palette; + uint32_t lv_palette_generation; + uint32_t lv_decoded_generation; + // One image per decode mode, shared by every TileGrid drawing the file that way: [0] + // streams rows, [1] holds the whole decode (unmapped files and scaled TileGrids). + lv_image_dsc_t lv_dsc[2]; + struct displayio_lvgl_image_root *lv_roots[2]; + // The decoder's scratch (row buffer and draw buffer wrappers), owned here so that opening + // an image per draw doesn't allocate. See odb_decoder_open(). + struct odb_decoder *lv_decoder; + uint16_t lv_users; + uint16_t lv_full_users; +} displayio_ondiskbitmap_t; + +#if CIRCUITPY_STORAGE_MAP_FILE +// Row y of an 8-bit file in flash, or NULL if it spans two runs or the file isn't mapped. +const uint8_t *displayio_ondiskbitmap_row(displayio_ondiskbitmap_t *self, int16_t y); +#endif + +// Image decoder used by OnDiskBitmap. The descriptors it hands out carry this address in +// reserved and point data at the OnDiskBitmap. +extern const char displayio_lvgl_ondiskbitmap_tag; +void displayio_lvgl_ondiskbitmap_decoder_init(void); +lv_color_format_t displayio_ondiskbitmap_lv_color_format(displayio_ondiskbitmap_t *self); +// Attach/detach a TileGrid's root object as a user of the file's LVGL image. full requests a +// whole image (the file isn't in mapped flash, or the TileGrid inherits a scale). The +// returned descriptor stays valid until the matching release. Caller holds the LVGL lock. +const lv_image_dsc_t *displayio_ondiskbitmap_lv_acquire(displayio_ondiskbitmap_t *self, bool full, + lv_obj_t *root); +void displayio_ondiskbitmap_lv_release(displayio_ondiskbitmap_t *self, bool full, lv_obj_t *root); +// The file's Palette changed: rebuild the decoder's LVGL palette and the whole decode, and +// invalidate every image drawn from the file. +void displayio_ondiskbitmap_lv_palette_changed(displayio_ondiskbitmap_t *self); diff --git a/shared-module/displayio/lvgl/Palette.c b/shared-module/displayio/lvgl/Palette.c new file mode 100644 index 00000000000..7f5a38fb0c2 --- /dev/null +++ b/shared-module/displayio/lvgl/Palette.c @@ -0,0 +1,237 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/displayio/Palette.h" + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-module/displayio/ColorConverter.h" +#include "shared-module/displayio/lvgl/Bitmap.h" +#include "shared-module/displayio/lvgl/OnDiskBitmap.h" +#include "shared-module/displayio/lvgl/Palette.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" + +#if CIRCUITPY_VECTORIO +#include "shared-module/vectorio/VectorShape.h" +#endif + +// --- The shader registry ----------------------------------------------------- +// +// Bitmaps keep their views in the Bitmap itself, but Palettes are shared and may be `const`, +// so whoever draws with one registers in this port-memory registry keyed by the +// shader object. A Palette change then walks the list and updates each mirror. + +typedef struct { + void *shader; // displayio Palette or ColorConverter object. + displayio_lvgl_mirror_user_t *users; + // LVGL's view of a Palette's colors, shared by every image drawing with it. NULL for + // ColorConverters and until an indexed image needs it. + lv_color32_t *colors; + struct displayio_lvgl_mirror_entry *next; // as displayio_lvgl_mirror_entry_t +} displayio_lvgl_mirror_entry_t; + +static displayio_lvgl_mirror_entry_t *mirror_registry = NULL; + +static displayio_lvgl_mirror_entry_t *_mirror_entry(void *shader) { + for (displayio_lvgl_mirror_entry_t *entry = mirror_registry; entry != NULL; entry = (displayio_lvgl_mirror_entry_t *)entry->next) { + if (entry->shader == shader) { + return entry; + } + } + return NULL; +} + +void displayio_lvgl_shader_register_user(mp_obj_t pixel_shader, void *user, uint8_t kind) { + void *shader = MP_OBJ_TO_PTR(pixel_shader); + displayio_lvgl_mirror_entry_t *entry = _mirror_entry(shader); + if (entry == NULL) { + entry = lv_malloc(sizeof(displayio_lvgl_mirror_entry_t)); + if (entry == NULL) { + return; + } + entry->shader = shader; + entry->users = NULL; + entry->colors = NULL; + entry->next = NULL; + displayio_lvgl_mirror_entry_t **link = (displayio_lvgl_mirror_entry_t **)&mirror_registry; + while (*link != NULL) { + link = (displayio_lvgl_mirror_entry_t **)&(*link)->next; + } + *link = entry; + } + for (displayio_lvgl_mirror_user_t *existing = entry->users; existing != NULL; existing = existing->next) { + if (existing->user == user) { + return; + } + } + displayio_lvgl_mirror_user_t *new_user = lv_malloc(sizeof(displayio_lvgl_mirror_user_t)); + if (new_user == NULL) { + return; + } + new_user->user = user; + new_user->kind = kind; + new_user->next = entry->users; + entry->users = new_user; +} + +void displayio_lvgl_shader_unregister_user(mp_obj_t pixel_shader, void *user) { + displayio_lvgl_mirror_entry_t *entry = _mirror_entry(MP_OBJ_TO_PTR(pixel_shader)); + if (entry == NULL) { + return; + } + displayio_lvgl_mirror_user_t **link = &entry->users; + while (*link != NULL) { + if ((*link)->user == user) { + displayio_lvgl_mirror_user_t *found = *link; + *link = found->next; + lv_free(found); + break; + } + link = &(*link)->next; + } + if (entry->users == NULL) { + // Walk with entry-typed links to remove the entry itself. + displayio_lvgl_mirror_entry_t **prev_entry_link = &mirror_registry; + while (*prev_entry_link != entry) { + prev_entry_link = (displayio_lvgl_mirror_entry_t **)&(*prev_entry_link)->next; + } + *prev_entry_link = (displayio_lvgl_mirror_entry_t *)entry->next; + lv_free(entry->colors); + lv_free(entry); + } +} + +void displayio_lvgl_shader_notify(mp_obj_t pixel_shader) { + displayio_lvgl_mirror_entry_t *entry = _mirror_entry(MP_OBJ_TO_PTR(pixel_shader)); + if (entry == NULL) { + return; + } + displayio_lvgl_lock(); + if (entry->colors != NULL && mp_obj_is_type(pixel_shader, &displayio_palette_type)) { + displayio_lvgl_palette_fill(MP_OBJ_TO_PTR(pixel_shader), entry->colors, + LV_COLOR_INDEXED_PALETTE_SIZE(LV_COLOR_FORMAT_I8)); + } + for (displayio_lvgl_mirror_user_t *user = entry->users; user != NULL; user = user->next) { + switch (user->kind) { + case DISPLAYIO_LVGL_USER_TILEGRID: + displayio_lvgl_tilegrid_shader_changed(user->user); + break; + case DISPLAYIO_LVGL_USER_ONDISKBITMAP: + displayio_ondiskbitmap_lv_palette_changed(user->user); + break; + #if CIRCUITPY_VECTORIO + case DISPLAYIO_LVGL_USER_VECTOR_SHAPE: + displayio_lvgl_vector_shape_shader_changed(user->user); + break; + #endif + default: + break; + } + } + displayio_lvgl_unlock(); +} + +void displayio_lvgl_mirror_gc(void) { + for (displayio_lvgl_mirror_entry_t *entry = mirror_registry; entry != NULL; entry = (displayio_lvgl_mirror_entry_t *)entry->next) { + for (displayio_lvgl_mirror_user_t *user = entry->users; user != NULL; user = user->next) { + if (user->kind != DISPLAYIO_LVGL_USER_VECTOR_SHAPE && + user->kind != DISPLAYIO_LVGL_USER_TILEGRID) { + // OnDiskBitmaps live in port memory and hold no Python objects of their own. + continue; + } + gc_collect_ptr(user->user); + } + } +} + +// --- Palette ----------------------------------------------------------------- + +void displayio_lvgl_palette_fill(displayio_palette_t *palette, lv_color32_t *colors, + uint32_t count) { + for (uint32_t i = 0; i < count; i++) { + if (i >= palette->color_count || palette->colors[i].transparent) { + colors[i] = (lv_color32_t) {0}; + continue; + } + uint32_t rgb888 = palette->colors[i].rgb888; + colors[i] = lv_color32_make((rgb888 >> 16) & 0xff, (rgb888 >> 8) & 0xff, rgb888 & 0xff, + LV_OPA_COVER); + } +} + +const lv_color32_t *displayio_lvgl_shader_palette(mp_obj_t pixel_shader) { + displayio_lvgl_mirror_entry_t *entry = _mirror_entry(MP_OBJ_TO_PTR(pixel_shader)); + if (entry == NULL || !mp_obj_is_type(pixel_shader, &displayio_palette_type)) { + return NULL; + } + if (entry->colors == NULL) { + // 256 entries (the largest indexed palette) so the address stays stable when the + // same Palette is used with bitmaps of different depths. + entry->colors = lv_malloc(LV_COLOR_INDEXED_PALETTE_SIZE(LV_COLOR_FORMAT_I8) * sizeof(lv_color32_t)); + if (entry->colors == NULL) { + return NULL; + } + displayio_lvgl_palette_fill(MP_OBJ_TO_PTR(pixel_shader), entry->colors, + LV_COLOR_INDEXED_PALETTE_SIZE(LV_COLOR_FORMAT_I8)); + } + return entry->colors; +} + +void common_hal_displayio_palette_construct(displayio_palette_t *self, uint16_t color_count, bool dither) { + self->color_count = color_count; + self->colors = (_displayio_color_t *)m_malloc_without_collect(color_count * sizeof(_displayio_color_t)); + self->dither = dither; +} + +void common_hal_displayio_palette_set_dither(displayio_palette_t *self, bool dither) { + self->dither = dither; +} + +bool common_hal_displayio_palette_get_dither(displayio_palette_t *self) { + return self->dither; +} + +void common_hal_displayio_palette_make_opaque(displayio_palette_t *self, uint32_t palette_index) { + self->colors[palette_index].transparent = false; + self->needs_refresh = true; + displayio_lvgl_shader_notify(MP_OBJ_FROM_PTR(self)); +} + +void common_hal_displayio_palette_make_transparent(displayio_palette_t *self, uint32_t palette_index) { + self->colors[palette_index].transparent = true; + self->needs_refresh = true; + displayio_lvgl_shader_notify(MP_OBJ_FROM_PTR(self)); +} + +bool common_hal_displayio_palette_is_transparent(displayio_palette_t *self, uint32_t palette_index) { + return self->colors[palette_index].transparent; +} + +uint32_t common_hal_displayio_palette_get_len(displayio_palette_t *self) { + return self->color_count; +} + +void common_hal_displayio_palette_set_color(displayio_palette_t *self, uint32_t palette_index, uint32_t color) { + if (self->colors[palette_index].rgb888 == color) { + return; + } + self->colors[palette_index].rgb888 = color; + self->colors[palette_index].cached_colorspace = NULL; + self->needs_refresh = true; + displayio_lvgl_shader_notify(MP_OBJ_FROM_PTR(self)); +} + +uint32_t common_hal_displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index) { + return self->colors[palette_index].rgb888; +} + +bool displayio_palette_needs_refresh(displayio_palette_t *self) { + return self->needs_refresh; +} + +void displayio_palette_finish_refresh(displayio_palette_t *self) { + self->needs_refresh = false; +} diff --git a/shared-module/displayio/lvgl/Palette.h b/shared-module/displayio/lvgl/Palette.h new file mode 100644 index 00000000000..91ee95bd6b3 --- /dev/null +++ b/shared-module/displayio/lvgl/Palette.h @@ -0,0 +1,48 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "shared-module/displayio/Bitmap.h" +#include "shared-module/displayio/Palette.h" + +#include "lvgl.h" + +// Palette changes notify the LVGL images that draw with it. Those mirrors register here; +// Bitmaps keep pointers to their views themselves. The objects may be `const` statistics (the +// supervisor's generated splash resources), so the registry lives in port memory rather +// than in them. + +// What a registered user is, which decides how a Palette change reaches it. +typedef enum { + DISPLAYIO_LVGL_USER_ONDISKBITMAP = 0, // user is a displayio_ondiskbitmap_t. + DISPLAYIO_LVGL_USER_TILEGRID, // user is a displayio_tilegrid_t. + DISPLAYIO_LVGL_USER_VECTOR_SHAPE, // user is a vectorio_vector_shape_t. +} displayio_lvgl_user_kind_t; + +typedef struct displayio_lvgl_mirror_user { + void *user; + uint8_t kind; // displayio_lvgl_user_kind_t + struct displayio_lvgl_mirror_user *next; +} displayio_lvgl_mirror_user_t; + +void displayio_lvgl_shader_register_user(mp_obj_t pixel_shader, void *user, uint8_t kind); +void displayio_lvgl_shader_unregister_user(mp_obj_t pixel_shader, void *user); +// The pixel shader's content changed: every mirror that draws with it has to update. +void displayio_lvgl_shader_notify(mp_obj_t pixel_shader); +// Root the registry's user pointers during GC. +void displayio_lvgl_mirror_gc(void); + +// Write count LVGL palette entries for palette. Entries past its end and transparent ones +// are fully transparent, which leaves those pixels to the layers below. +void displayio_lvgl_palette_fill(displayio_palette_t *palette, lv_color32_t *colors, + uint32_t count); + +// The shared LVGL palette of `pixel_shader`, allocated and filled on first use. It lives in +// the shader's registry entry, so it is shared by every image drawing with the Palette and +// released when the last of them unregisters. Returns NULL for a non-Palette shader or when +// there is no memory. Call after registering a user. +const lv_color32_t *displayio_lvgl_shader_palette(mp_obj_t pixel_shader); diff --git a/shared-module/displayio/lvgl/TileGrid.c b/shared-module/displayio/lvgl/TileGrid.c new file mode 100644 index 00000000000..15cdab1db9c --- /dev/null +++ b/shared-module/displayio/lvgl/TileGrid.c @@ -0,0 +1,1658 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// LVGL-based displayio TileGrid implementation. +// +// A TileGrid owns one composed buffer holding exactly one tile grid of pixels, in tile-matrix +// order: the pixels of tiles[] cell (i, j) live at (i * tile_width, j * tile_height), with the +// tile indirection and the palette already resolved. Cells are composed through LVGL's own draw +// pipeline (a hidden lv_canvas plus lv_draw_image() per changed cell), so the shim has no pixel +// loop: palette expansion, alpha, format conversion and clipping are LVGL's work. +// +// The display shows that buffer through up to four lv_images ("views"): the 2x2 split of the +// wrapped window that displayio's scrolling describes, where display column i shows matrix column +// (i + top_left_x) % width_in_tiles. A scroll therefore re-points the views and invalidates; no +// pixels move and no cell is re-sourced. The common cases (no scroll, or scrolling by whole rows +// with top_left_x == 0) are one and two views. +// +// A single-cell grid of a direct color source skips the buffer entirely: one view draws the source +// pixels as they are, which is what plain image viewers and the boot logo want. +// +// Composition resolves the palette, so a Bitmap never owns an LVGL image, a descriptor or a +// palette copy. It only tells the TileGrids drawing it which pixels changed +// (displayio_bitmap_set_dirty()), and they re-read that area before the next refresh. +// +// Everything here runs synchronously on the CircuitPython thread under the port's LVGL lock, +// except the compose pass, which runs at the start of an LVGL refresh with the lock already held +// by the renderer (see displayio_lvgl_register_display()). + +#include "shared-bindings/displayio/TileGrid.h" + +#include + +#include "py/gc.h" +#include "py/objtype.h" +#include "py/runtime.h" +#include "shared-bindings/displayio/Bitmap.h" +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/OnDiskBitmap.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/displayio/__init__.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" + +#include "misc/cache/instance/lv_image_cache.h" + +#if CIRCUITPY_DISPLAYIO_LVGL + +// Tile-index debug logging goes through ESP_LOG, not mp_printf: mp_printf ends up in +// serial_write_substring(), which writes the display terminal, which calls back into +// common_hal_displayio_tilegrid_set_tile() -- recursion, then a crash. The IDF log console is a +// separate sink, so it can be used from the write path and from the compose pass. +// +// Off by default: this prints several lines per grid per refresh, which buries the heap and +// out-of-memory logging in lvgl_port.c and port.c. Flip to 1 to watch the compose pass; the +// memory logging is independent and stays on either way. +#define DISPLAYIO_TILEGRID_DEBUG_ENABLED 0 + +#if DISPLAYIO_TILEGRID_DEBUG_ENABLED && CIRCUITPY_ESPIDF +#include "esp_log.h" +#define DISPLAYIO_TILEGRID_DEBUG(...) ESP_LOGI("TileGrid", __VA_ARGS__) +#else +#define DISPLAYIO_TILEGRID_DEBUG(...) ((void)0) +#endif + +// Tile index 0 is the blank tile: the terminal fills itself with spaces, and every tiles[] array +// starts zeroed. Logging those hides the glyph changes we care about. +#define DISPLAYIO_TILEGRID_BLANK_TILE 0 + +static void _cell_display_area(displayio_tilegrid_t *self, uint32_t x, uint32_t y, lv_area_t *area); + +static void _raise_unsupported(void) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("LVGL displayio backend does not support flips/transpose yet")); +} + +// --- Attached mirrors ------------------------------------------------------- + +// Every attached TileGrid. The compose pass walks it, the Bitmap write path looks for the grids +// that draw a bitmap in it, and displayio_lvgl_tilegrid_gc_collect() roots it: the list lives in +// port memory, which the GC can't see. +static displayio_tilegrid_t *mirror_head = NULL; + +static bool _is_linked(displayio_tilegrid_t *self) { + for (displayio_tilegrid_t *other = mirror_head; other != NULL; other = other->lv_next) { + if (other == self) { + return true; + } + } + return false; +} + +// A Bitmap keeps its own list of the attached grids drawing it. A pixel write only walks the +// grids that can actually be affected, and an empty list means the write can skip LVGL entirely. +// Only the CircuitPython thread mutates these lists, under the LVGL lock. +static void _watch_bitmap(displayio_tilegrid_t *self) { + if (!mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { + return; + } + displayio_bitmap_t *bitmap = MP_OBJ_TO_PTR(self->bitmap); + self->lv_bitmap_next = bitmap->lv_watch_head; + bitmap->lv_watch_head = self; +} + +static void _unwatch_bitmap(displayio_tilegrid_t *self) { + if (!mp_obj_is_type(self->bitmap, &displayio_bitmap_type)) { + return; + } + displayio_bitmap_t *bitmap = MP_OBJ_TO_PTR(self->bitmap); + displayio_tilegrid_t **link = &bitmap->lv_watch_head; + while (*link != NULL) { + if (*link == self) { + *link = self->lv_bitmap_next; + break; + } + link = &(*link)->lv_bitmap_next; + } + self->lv_bitmap_next = NULL; +} + +static void _link_mirror(displayio_tilegrid_t *self) { + if (_is_linked(self)) { + return; + } + self->lv_next = mirror_head; + mirror_head = self; + _watch_bitmap(self); +} + +static void _unlink_mirror(displayio_tilegrid_t *self) { + bool linked = false; + displayio_tilegrid_t **link = &mirror_head; + while (*link != NULL) { + if (*link == self) { + *link = self->lv_next; + linked = true; + break; + } + link = &(*link)->lv_next; + } + self->lv_next = NULL; + if (linked) { + _unwatch_bitmap(self); + } +} + +void displayio_lvgl_tilegrid_gc_collect(void) { + for (displayio_tilegrid_t *self = mirror_head; self != NULL; self = self->lv_next) { + gc_collect_ptr(self); + } +} + +// --- Cells ----------------------------------------------------------------- + +static uint32_t _cell_count(displayio_tilegrid_t *self) { + return (uint32_t)self->width_in_tiles * self->height_in_tiles; +} + +// Tile pixel origin in the source bitmap for a tile index. +static void _tile_origin(displayio_tilegrid_t *self, uint16_t tile_index, uint16_t *x0, uint16_t *y0) { + *x0 = (tile_index % self->bitmap_width_in_tiles) * self->tile_width; + *y0 = (tile_index / self->bitmap_width_in_tiles) * self->tile_height; +} + +// Read one index from an indexed source row. LVGL packs indices MSB first within each byte. +static uint8_t _source_index(const uint8_t *row, uint32_t x, uint8_t bpp) { + uint32_t bit = x * bpp; + return (row[bit >> 3] >> (8 - bpp - (bit & 7))) & ((1u << bpp) - 1); +} + +// Whether a tile holds only palette entry 0. A compose buffer is cleared to entry 0 (black or +// transparent), so only such a cell can be left out of the compose draw entirely. +static bool _tile_is_blank(displayio_tilegrid_t *self, uint16_t tile_index) { + const lv_image_dsc_t *src = self->lv_src; + if (src == NULL || src->data == NULL) { + return false; + } + uint8_t bpp = lv_color_format_get_bpp(src->header.cf); + if (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8) { + return false; + } + uint16_t x0, y0; + _tile_origin(self, tile_index, &x0, &y0); + for (uint16_t y = 0; y < self->tile_height; y++) { + const uint8_t *row = src->data + (uint32_t)(y0 + y) * src->header.stride; + for (uint16_t x = 0; x < self->tile_width; x++) { + if (_source_index(row, x0 + x, bpp) != 0) { + return false; + } + } + } + return true; +} + +// Read the tile index stored for a cell of the tile matrix, in the same order as the composed +// buffer: cell (i, j) shows tiles[j * width_in_tiles + i]. +static uint16_t _tile_index(displayio_tilegrid_t *self, uint32_t cell_index) { + if (self->tiles == NULL) { + return 0; + } + if (self->tiles_in_bitmap > 255) { + return ((uint16_t *)self->tiles)[cell_index]; + } + return ((uint8_t *)self->tiles)[cell_index]; +} + +// A composed grid is brought up to date by the compose pass, which runs at the start of a refresh +// (LV_EVENT_REFR_START, before LVGL reads its invalid-area list), so a change has to invalidate +// something or LVGL never runs a refresh and the grid stays stale forever. Invalidate the area the +// changed cell occupies on screen: that is the same rectangle the compose pass would invalidate, +// and it keeps `display.refresh()`/auto_refresh-independent redraws honest even before the compose +// runs. Only the first change of a burst does this -- invalidating every changed cell would +// overflow LVGL's invalidation buffer (LV_INV_BUF_SIZE, 32 rects) and repaint the whole screen -- +// and the compose pass invalidates the union of everything it recomposed in the same pass. +// `cell` < 0 means "not one cell", i.e. invalidate the whole grid. +static void _wake_refresh(displayio_tilegrid_t *self, int32_t cell) { + if (self->lv_obj == NULL || lv_obj_get_display(self->lv_obj) == NULL) { + // Not attached yet: attaching marks everything dirty, so a later write wakes it. + return; + } + if (cell < 0 || self->lv_full_dirty || self->width_in_tiles == 0 || + self->height_in_tiles == 0) { + DISPLAYIO_TILEGRID_DEBUG("wake %p whole obj", self); + lv_obj_invalidate(self->lv_obj); + return; + } + lv_area_t area; + _cell_display_area(self, (uint32_t)cell % self->width_in_tiles, + (uint32_t)cell / self->width_in_tiles, &area); + if (area.x2 < area.x1) { + DISPLAYIO_TILEGRID_DEBUG("wake %p whole obj (no cell area)", self); + lv_obj_invalidate(self->lv_obj); + return; + } + // The area comes back in container coordinates; LVGL invalidates in screen coordinates. + lv_area_t obj_coords; + lv_obj_get_coords(self->lv_obj, &obj_coords); + lv_area_move(&area, obj_coords.x1, obj_coords.y1); + DISPLAYIO_TILEGRID_DEBUG("wake %p cell %ld at (%ld,%ld)-(%ld,%ld)", self, (long)cell, + (long)area.x1, (long)area.y1, (long)area.x2, (long)area.y2); + lv_obj_invalidate_area(self->lv_obj, &area); +} + +static void _mark_work(displayio_tilegrid_t *self) { + if (self->lv_work) { + return; + } + self->lv_work = true; + _wake_refresh(self, -1); +} + +static void _mark_all(displayio_tilegrid_t *self) { + self->lv_full_dirty = true; + _mark_work(self); +} + +static void _mark_views(displayio_tilegrid_t *self) { + self->lv_views_dirty = true; + _mark_work(self); +} + +static void _mark_cell(displayio_tilegrid_t *self, uint32_t cell) { + if (self->lv_cell_dirty == NULL || cell >= _cell_count(self)) { + _mark_all(self); + return; + } + uint32_t *word = &self->lv_cell_dirty[cell / 32]; + uint32_t bit = 1u << (cell % 32); + bool was_working = self->lv_work; + if ((*word & bit) == 0) { + *word |= bit; + self->lv_dirty_count++; + } + self->lv_work = true; + if (!was_working) { + _wake_refresh(self, (int32_t)cell); + } +} + +static bool _cell_is_marked(displayio_tilegrid_t *self, uint32_t cell) { + return (self->lv_cell_dirty[cell / 32] & (1u << (cell % 32))) != 0; +} + +static void _clear_cell(displayio_tilegrid_t *self, uint32_t cell) { + if (self->lv_cell_dirty != NULL) { + self->lv_cell_dirty[cell / 32] &= ~(1u << (cell % 32)); + } +} + +// The supervisor pokes grid dimensions straight into the static terminal grids, so the bitset is +// checked against the current dimensions instead of assuming it still fits. +static void _ensure_cell_bits(displayio_tilegrid_t *self) { + uint32_t words = (_cell_count(self) + 31) / 32; + if (words == 0) { + words = 1; + } + if (self->lv_cell_dirty != NULL && self->lv_cell_words >= words) { + return; + } + if (self->lv_cell_dirty != NULL) { + lv_free(self->lv_cell_dirty); + } + self->lv_cell_dirty = lv_malloc_zeroed(words * sizeof(uint32_t)); + self->lv_cell_words = words; + self->lv_dirty_count = 0; + self->lv_full_dirty = true; +} + +// --- The source atlas ------------------------------------------------------- + +// The LVGL color format that draws bitmap with pixel_shader. Raises NotImplementedError for the +// combinations that haven't been ported to LVGL. +static lv_color_format_t _source_cf(mp_obj_t bitmap, mp_obj_t pixel_shader) { + if (mp_obj_is_type(bitmap, &displayio_ondiskbitmap_type)) { + displayio_ondiskbitmap_t *odb = MP_OBJ_TO_PTR(bitmap); + lv_color_format_t cf = displayio_ondiskbitmap_lv_color_format(odb); + if (cf == LV_COLOR_FORMAT_UNKNOWN) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("Format not supported")); + } + // The file owns its pixel shader (Palette for indexed files, pass-through + // ColorConverter otherwise), so a different one can't be applied. + mp_obj_t odb_shader = common_hal_displayio_ondiskbitmap_get_pixel_shader(odb); + if (MP_OBJ_TO_PTR(pixel_shader) != MP_OBJ_TO_PTR(odb_shader)) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("OnDiskBitmap requires its own pixel_shader")); + } + return cf; + } + if (mp_obj_is_type(bitmap, &displayio_bitmap_type)) { + uint8_t bpp = ((displayio_bitmap_t *)MP_OBJ_TO_PTR(bitmap))->bits_per_value; + if (mp_obj_is_type(pixel_shader, &displayio_palette_type)) { + switch (bpp) { + case 1: + return LV_COLOR_FORMAT_I1; + case 2: + return LV_COLOR_FORMAT_I2; + case 4: + return LV_COLOR_FORMAT_I4; + case 8: + return LV_COLOR_FORMAT_I8; + } + } else if (mp_obj_is_type(pixel_shader, &displayio_colorconverter_type)) { + displayio_colorconverter_t *converter = MP_OBJ_TO_PTR(pixel_shader); + if (converter->transparent_color == NO_TRANSPARENT_COLOR) { + if (bpp == 16 && converter->input_colorspace == DISPLAYIO_COLORSPACE_RGB565) { + return LV_COLOR_FORMAT_RGB565; + } + if (bpp == 16 && converter->input_colorspace == DISPLAYIO_COLORSPACE_RGB565_SWAPPED) { + return LV_COLOR_FORMAT_RGB565_SWAPPED; + } + if (bpp == 32 && converter->input_colorspace == DISPLAYIO_COLORSPACE_RGB888) { + return LV_COLOR_FORMAT_XRGB8888; + } + if (bpp == 8 && converter->input_colorspace == DISPLAYIO_COLORSPACE_L8) { + return LV_COLOR_FORMAT_L8; + } + } + } + } + mp_raise_NotImplementedError(MP_ERROR_TEXT("Format not supported")); +} + +// displayio's only alpha is the palette's per-index "transparent" flag (plus indices past the end +// of the palette). A composed buffer has to carry that through to the display, so those grids +// compose to ARGB8888 with real alpha, and everything else composes to RGB565, which the display +// path takes directly. +static bool _palette_has_transparency(displayio_palette_t *palette, uint32_t slots) { + for (uint32_t i = 0; i < palette->color_count; i++) { + if (palette->colors[i].transparent) { + return true; + } + } + return palette->color_count < slots; +} + +static bool _source_has_alpha(displayio_tilegrid_t *self, lv_color_format_t cf) { + if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + displayio_ondiskbitmap_t *odb = MP_OBJ_TO_PTR(self->bitmap); + if (odb->bits_per_pixel > 8 || odb->palette == NULL) { + return false; + } + return _palette_has_transparency(odb->palette, 1u << odb->bits_per_pixel); + } + if (!LV_COLOR_FORMAT_IS_INDEXED(cf) || !mp_obj_is_type(self->pixel_shader, &displayio_palette_type)) { + return false; + } + return _palette_has_transparency(MP_OBJ_TO_PTR(self->pixel_shader), + LV_COLOR_INDEXED_PALETTE_SIZE(cf)); +} + +static bool _source_is_indexed(displayio_tilegrid_t *self) { + if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + // OnDiskBitmap always expands indexed files to a direct color format. + return false; + } + return LV_COLOR_FORMAT_IS_INDEXED(_source_cf(self->bitmap, self->pixel_shader)); +} + +// A single cell showing a direct color source is exactly the source pixels, so there is nothing +// to compose and nothing to copy. +static bool _wants_direct(displayio_tilegrid_t *self) { + return self->width_in_tiles == 1 && self->height_in_tiles == 1 && !_source_is_indexed(self); +} + +// Point the source descriptor at the whole bitmap. Caller holds the lock and has no source bound. +static bool _bind_source(displayio_tilegrid_t *self) { + lv_color_format_t cf; + if (mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + displayio_ondiskbitmap_t *odb = MP_OBJ_TO_PTR(self->bitmap); + cf = displayio_ondiskbitmap_lv_color_format(odb); + if (cf == LV_COLOR_FORMAT_UNKNOWN) { + DISPLAYIO_TILEGRID_DEBUG("bind %p ondisk bitmap has no LVGL format", self); + return false; + } + // 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 = 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; + } + + displayio_bitmap_t *bitmap = MP_OBJ_TO_PTR(self->bitmap); + cf = _source_cf(self->bitmap, self->pixel_shader); + uint32_t stride = (uint32_t)bitmap->stride * sizeof(uint32_t); + lv_image_dsc_t *dsc = &self->lv_src_dsc; + lv_memzero(dsc, sizeof(*dsc)); + dsc->header.magic = LV_IMAGE_HEADER_MAGIC; + dsc->header.cf = cf; + dsc->header.w = bitmap->width; + dsc->header.h = bitmap->height; + dsc->header.stride = stride; + if (LV_COLOR_FORMAT_IS_INDEXED(cf)) { + // The indices are handed to LVGL in place and the palette is a separate, shared LVGL + // view of the displayio Palette (see displayio_lvgl_shader_palette()), so neither the + // bitmap nor the palette is copied into the TileGrid. + if (bitmap->data == NULL) { + DISPLAYIO_TILEGRID_DEBUG("bind %p bitmap has no data", self); + return false; + } + dsc->header.flags |= LV_IMAGE_FLAGS_PALETTE_EXTERNAL; + dsc->data = (const uint8_t *)bitmap->data; + dsc->data_size = (uint32_t)stride * bitmap->height; + displayio_lvgl_shader_register_user(self->pixel_shader, self, + DISPLAYIO_LVGL_USER_TILEGRID); + dsc->palette = displayio_lvgl_shader_palette(self->pixel_shader); + if (dsc->palette == NULL) { + DISPLAYIO_TILEGRID_DEBUG("bind %p palette has no LVGL view", self); + displayio_lvgl_shader_unregister_user(self->pixel_shader, self); + return false; + } + } else if (bitmap->data != NULL) { + dsc->data = (const uint8_t *)bitmap->data; + dsc->data_size = (uint32_t)stride * bitmap->height; + } + self->lv_src = dsc; + self->lv_argb = _source_has_alpha(self, cf); + DISPLAYIO_TILEGRID_DEBUG("bind %p cf %d %dx%d stride %lu data %p palette %p", + self, (int)cf, bitmap->width, bitmap->height, (unsigned long)stride, dsc->data, + dsc->palette); + return true; +} + +// Undo _bind_source(). Caller holds the lock. +static void _unbind_source(displayio_tilegrid_t *self) { + if (self->lv_src == &self->lv_src_dsc) { + // A Bitmap source is a descriptor over the Bitmap's own pixels. Nothing is owned, but + // the Palette registry and the image cache still point at it. + lv_image_cache_drop(&self->lv_src_dsc); + displayio_lvgl_shader_unregister_user(self->pixel_shader, self); + lv_memzero(&self->lv_src_dsc, sizeof(self->lv_src_dsc)); + } else if (self->lv_src != NULL && mp_obj_is_type(self->bitmap, &displayio_ondiskbitmap_type)) { + displayio_ondiskbitmap_lv_release(MP_OBJ_TO_PTR(self->bitmap), self->lv_src_odb_full, + self->lv_obj); + } + self->lv_src = NULL; + self->lv_src_odb_full = false; +} + +// --- Objects --------------------------------------------------------------- + +// lv_obj_set_pos() only records a style that a later layout pass applies, and lv_obj_move_to() +// leaves that style behind, so an unrelated size change would then snap the object back to where +// the style says. Set both: the coordinates are right immediately and nothing can undo them. +static void _place(lv_obj_t *obj, int32_t x, int32_t y) { + lv_obj_set_style_x(obj, x, 0); + lv_obj_set_style_y(obj, y, 0); + lv_obj_move_to(obj, x, y); +} + +// Place the mirror in screen coordinates. +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); +} + +// 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) { + lv_image_set_pivot(img, 0, 0); + } + lv_image_set_scale(img, (int32_t)scale * LV_SCALE_NONE); +} + +// 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. +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) { + return NULL; + } + 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; +} + +static bool _ensure_canvas(displayio_tilegrid_t *self) { + if (self->lv_canvas != NULL) { + return true; + } + lv_obj_t *canvas = lv_canvas_create(self->lv_obj); + if (canvas == NULL) { + return false; + } + // The canvas is only here to provide a layer and a display for the compose pass; it is hidden + // and never draws itself. + lv_obj_remove_style_all(canvas); + lv_obj_add_flag(canvas, LV_OBJ_FLAG_HIDDEN); + lv_obj_clear_flag(canvas, LV_OBJ_FLAG_CLICKABLE); + lv_obj_clear_flag(canvas, LV_OBJ_FLAG_SCROLLABLE); + self->lv_canvas = canvas; + return true; +} + +// Build the container the views live in. One object per TileGrid keeps the group's z-order right, +// and the container carries the grid's position and scale so moving a grid never touches its +// views. +static bool _create_objects(displayio_tilegrid_t *self, lv_obj_t *parent) { + lv_obj_t *container = lv_obj_create(parent); + if (container == NULL) { + return false; + } + // Plain, transparent, no layout: the views are placed explicitly, so the LAYOUT_UPDATE that + // lv_obj_set_size() carries resolves to a pass over a handful of children. + lv_obj_remove_style_all(container); + 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); + self->lv_obj = container; + return _ensure_canvas(self); +} + +// Tear the objects down. The composed buffer is ours (a canvas never frees its draw buffer), so it +// goes after the tree that might still reference it. Caller holds the lock. +static void _destroy_objects(displayio_tilegrid_t *self) { + lv_draw_buf_t *buf = self->lv_buf; + self->lv_buf = NULL; + if (self->lv_obj != NULL) { + lv_obj_delete(self->lv_obj); + } + self->lv_obj = NULL; + self->lv_canvas = NULL; + self->lv_view_count = 0; + for (uint8_t i = 0; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { + self->lv_views[i] = NULL; + } + if (buf != NULL) { + lv_draw_buf_destroy(buf); + } +} + +// --- The composed buffer --------------------------------------------------- + +static bool _ensure_buffer(displayio_tilegrid_t *self) { + uint32_t w = self->pixel_width; + uint32_t h = self->pixel_height; + if (w == 0 || h == 0 || self->lv_obj == NULL) { + return false; + } + lv_color_format_t cf = self->lv_argb ? LV_COLOR_FORMAT_ARGB8888 : LV_COLOR_FORMAT_RGB565; + if (self->lv_buf != NULL && self->lv_buf->header.w == w && self->lv_buf->header.h == h && + self->lv_buf->header.cf == cf) { + return true; + } + if (!_ensure_canvas(self)) { + return false; + } + lv_draw_buf_t *old = self->lv_buf; + // ESP_LOGI(TAG, "creating draw buf %d x %d", w, h); + lv_draw_buf_t *buf = lv_draw_buf_create(w, h, cf, 0); + if (buf == NULL) { + return false; + } + lv_canvas_set_draw_buf(self->lv_canvas, buf); + self->lv_buf = buf; + if (old != NULL) { + lv_draw_buf_destroy(old); + } + _ensure_cell_bits(self); + _mark_all(self); + _mark_views(self); + return true; +} + +// One run of the scroll window: pixels [src, src + len) of the buffer go to display offset dst. +typedef struct { + uint32_t src; + uint32_t len; + uint32_t dst; +} _run_t; + +static void _runs(uint32_t offset, uint32_t extent, _run_t runs[2], uint8_t *count) { + if (offset == 0) { + runs[0].src = 0; + runs[0].len = extent; + runs[0].dst = 0; + *count = 1; + return; + } + runs[0].src = offset; + runs[0].len = extent - offset; + runs[0].dst = 0; + runs[1].src = 0; + runs[1].len = offset; + runs[1].dst = extent - offset; + *count = 2; +} + +// Create views up to `needed`. Returns false when one can't be allocated. +static bool _ensure_views(displayio_tilegrid_t *self, uint8_t needed) { + for (uint8_t i = 0; i < needed; i++) { + if (self->lv_views[i] == NULL) { + self->lv_views[i] = _create_view(self, self->lv_obj); + if (self->lv_views[i] == NULL) { + // Out of memory: try again on the next refresh. + self->lv_views_dirty = true; + return false; + } + } + } + return true; +} + +// Point the views at the current window of the buffer, or at the source itself on the direct path. +// Caller holds the lock. +static void _layout_views(displayio_tilegrid_t *self) { + self->lv_views_dirty = false; + if (self->lv_obj == NULL) { + return; + } + // Views are created on demand, so an unscrolled grid has one and the direct path has one too. + if (self->lv_direct) { + if (!_ensure_views(self, 1)) { + return; + } + // The view is the source: the tile's origin in the atlas becomes a negative offset, in + // scaled pixels, so that the tile's top left lands on the grid's top left. + uint16_t x0, y0; + _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_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++) { + if (self->lv_views[i] != NULL) { + lv_obj_add_flag(self->lv_views[i], LV_OBJ_FLAG_HIDDEN); + } + } + self->lv_view_count = 1; + return; + } + if (self->lv_buf == NULL || self->width_in_tiles == 0 || self->height_in_tiles == 0 || + self->pixel_width == 0 || self->pixel_height == 0) { + return; + } + uint32_t grid_w = self->pixel_width; + uint32_t grid_h = self->pixel_height; + uint32_t tx = (self->top_left_x % self->width_in_tiles) * self->tile_width; + uint32_t ty = (self->top_left_y % self->height_in_tiles) * self->tile_height; + _run_t rows[2]; + _run_t cols[2]; + uint8_t row_count; + uint8_t col_count; + _runs(ty, grid_h, rows, &row_count); + _runs(tx, grid_w, cols, &col_count); + if (!_ensure_views(self, row_count * col_count)) { + return; + } + + uint32_t stride = self->lv_buf->header.stride; + lv_color_format_t cf = self->lv_buf->header.cf; + uint32_t bytes_per_pixel = lv_color_format_get_size(cf); + uint8_t view = 0; + for (uint8_t r = 0; r < row_count; r++) { + for (uint8_t c = 0; c < col_count; c++) { + lv_image_dsc_t *dsc = &self->lv_view_dsc[view]; + 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; + 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; + 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_clear_flag(img, LV_OBJ_FLAG_HIDDEN); + _place(img, cols[c].dst * self->lv_scale, 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)); + view++; + } + } + self->lv_view_count = view; + DISPLAYIO_TILEGRID_DEBUG( + "views %p count %d grid %lux%lu scroll (%lu,%lu) -> (%lu,%lu) scale %u", + self, view, (unsigned long)grid_w, (unsigned long)grid_h, + (unsigned long)(self->top_left_x % self->width_in_tiles), + (unsigned long)(self->top_left_y % self->height_in_tiles), (unsigned long)tx, + (unsigned long)ty, (unsigned)self->lv_scale); + for (uint8_t i = view; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { + if (self->lv_views[i] != NULL) { + lv_obj_add_flag(self->lv_views[i], LV_OBJ_FLAG_HIDDEN); + } + } +} + +// --- Compose --------------------------------------------------------------- + +// Grow `area` to cover `other` too. +static void _union_area(lv_area_t *area, const lv_area_t *other) { + if (other->x1 < area->x1) { + area->x1 = other->x1; + } + if (other->y1 < area->y1) { + area->y1 = other->y1; + } + if (other->x2 > area->x2) { + area->x2 = other->x2; + } + if (other->y2 > area->y2) { + area->y2 = other->y2; + } +} + +// 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, + int32_t x2, int32_t y2) { + if (self->lv_full_dirty) { + return; + } + uint32_t cells = _cell_count(self); + for (uint32_t cell = 0; cell < cells; cell++) { + uint16_t x0, y0; + _tile_origin(self, _tile_index(self, cell), &x0, &y0); + if ((int32_t)x0 < x2 && (int32_t)x0 + self->tile_width > x1 && + (int32_t)y0 < y2 && (int32_t)y0 + self->tile_height > y1) { + _mark_cell(self, cell); + } + } +} + +// The bitmap's pixels changed, so drop any decoded view and mark the cells the area reaches. +// Indexed sources are drawn from the Bitmap's own memory, so there is nothing to copy here. +static void _sync_source(displayio_tilegrid_t *self) { + if (!self->lv_src_dirty) { + return; + } + self->lv_src_dirty = false; + if (self->lv_src != NULL) { + lv_image_cache_drop(self->lv_src); + } + _mark_cells_for_source(self, self->lv_src_x1, self->lv_src_y1, self->lv_src_x2, + self->lv_src_y2); +} + +// Where cell (x, y) of the tile matrix lands on the display, in container coordinates. +static void _cell_display_area(displayio_tilegrid_t *self, uint32_t x, uint32_t y, lv_area_t *area) { + int32_t grid_w = self->pixel_width; + int32_t grid_h = self->pixel_height; + if (grid_w <= 0 || grid_h <= 0 || self->width_in_tiles == 0 || self->height_in_tiles == 0) { + *area = (lv_area_t) {0, 0, -1, -1}; + return; + } + int32_t tx = (self->top_left_x % self->width_in_tiles) * self->tile_width; + int32_t ty = (self->top_left_y % self->height_in_tiles) * self->tile_height; + int32_t px = ((int32_t)x * self->tile_width - tx) % grid_w; + int32_t py = ((int32_t)y * self->tile_height - ty) % grid_h; + if (px < 0) { + px += grid_w; + } + 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; +} + +// The most cells a compose pass queues before drawing them. LVGL walks the whole task list to add +// or dispatch each task, so drawing a long queue at once takes time with the square of its length. +#define DISPLAYIO_LVGL_COMPOSE_BATCH 64 + +// Compose every cell that changed into the buffer. Returns false when nothing was drawn so the +// caller can skip the invalidation. `dirty` comes back as the union of the composed cells, in +// container coordinates. +static bool _compose(displayio_tilegrid_t *self, lv_area_t *dirty) { + if (!self->lv_full_dirty && self->lv_dirty_count == 0) { + return false; + } + if (self->lv_buf == NULL || self->lv_canvas == NULL) { + DISPLAYIO_TILEGRID_DEBUG("compose %p no buffer: buf %p canvas %p", + self, self->lv_buf, self->lv_canvas); + return false; + } + bool full = self->lv_full_dirty; + bool any = false; + lv_area_t buf_dirty = {0, 0, -1, -1}; + lv_area_t drawn_cell_area = {0, 0, -1, -1}; + + // A cell whose tile is all palette entry 0 can be left untouched: the cleared buffer already + // holds entry 0's color (black in RGB565, transparent in ARGB8888). Tile index 0 isn't + // necessarily blank (it is for the terminal's space glyph, but it's an ordinary tile in any + // other atlas), so check the tile's pixels instead of assuming it. + bool skip_blank = self->lv_src == &self->lv_src_dsc && self->lv_src->palette != NULL && + LV_COLOR_FORMAT_IS_INDEXED(self->lv_src->header.cf) && + (self->lv_argb ? self->lv_src->palette[0].alpha == 0 : + (self->lv_src->palette[0].red | self->lv_src->palette[0].green | + self->lv_src->palette[0].blue) == 0) && + _tile_is_blank(self, 0); + + // A cell is a cropped window of the atlas, not an image allowed to grow beyond it, so the + // layer's clip area is narrowed to the cell while its draw task is created; lv_draw_add_task() + // saves the clip area into the task. + lv_layer_t layer; + lv_canvas_init_layer(self->lv_canvas, &layer); + lv_area_t layer_clip = layer._clip_area; + uint32_t queued = 0; + + uint32_t cells = _cell_count(self); + uint32_t tiles_w = self->width_in_tiles; + for (uint32_t cell = 0; cell < cells; cell++) { + if (!full && !_cell_is_marked(self, cell)) { + continue; + } + uint32_t cx = cell % tiles_w; + uint32_t cy = cell / tiles_w; + lv_area_t cell_area = { + .x1 = cx * self->tile_width, + .y1 = cy * self->tile_height, + }; + cell_area.x2 = cell_area.x1 + self->tile_width - 1; + cell_area.y2 = cell_area.y1 + self->tile_height - 1; + + // Start from transparent (ARGB8888) or black (RGB565): lv_draw_image() writes nothing + // where the source is transparent, which is exactly where the hole has to stay. + // ESP_LOGI(TAG, "clearing cell area %d,%d-%d,%d", cell_area.x1, cell_area.y1, cell_area.x2, cell_area.y2); + lv_draw_buf_clear(self->lv_buf, &cell_area); + + if (self->lv_src != NULL && self->lv_src->data != NULL) { + uint16_t x0, y0; + uint16_t tile_index = _tile_index(self, cell); + bool blank = tile_index == DISPLAYIO_TILEGRID_BLANK_TILE; + if (!(blank && skip_blank)) { + _tile_origin(self, tile_index, &x0, &y0); + if (!blank) { + DISPLAYIO_TILEGRID_DEBUG( + "compose %p cell %d (%d,%d) tile %d origin (%d,%d) full %d", + self, cell, cx, cy, tile_index, x0, y0, full); + } + drawn_cell_area = cell_area; + lv_draw_image_dsc_t dsc; + lv_draw_image_dsc_init(&dsc); + dsc.src = self->lv_src; + dsc.antialias = 0; + layer._clip_area = cell_area; + // The whole atlas is placed so that the tile's origin lands on the cell's corner; + // the clip above limits the draw to the cell. + int32_t dest_x = cell_area.x1 - (int32_t)x0; + int32_t dest_y = cell_area.y1 - (int32_t)y0; + lv_area_t coords = { + .x1 = dest_x, + .y1 = dest_y, + .x2 = dest_x + (int32_t)self->lv_src->header.w - 1, + .y2 = dest_y + (int32_t)self->lv_src->header.h - 1, + }; + lv_draw_image(&layer, &dsc, &coords); + layer._clip_area = layer_clip; + queued++; + if (queued == DISPLAYIO_LVGL_COMPOSE_BATCH) { + lv_canvas_finish_layer(self->lv_canvas, &layer); + lv_canvas_init_layer(self->lv_canvas, &layer); + queued = 0; + } + } + } + _clear_cell(self, cell); + lv_area_t cell_display; + _cell_display_area(self, cx, cy, &cell_display); + if (!any) { + buf_dirty = cell_area; + *dirty = cell_display; + any = true; + } else { + _union_area(&buf_dirty, &cell_area); + _union_area(dirty, &cell_display); + } + } + self->lv_dirty_count = 0; + self->lv_full_dirty = false; + + if (!any) { + DISPLAYIO_TILEGRID_DEBUG("compose %p drew nothing: src %p srcdata %p buf %d x %d", + self, self->lv_src, self->lv_src == NULL ? NULL : self->lv_src->data, + (int)self->lv_buf->header.w, (int)self->lv_buf->header.h); + } + + lv_canvas_finish_layer(self->lv_canvas, &layer); + if (any) { + // 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); + if (drawn_cell_area.x2 < drawn_cell_area.x1) { + DISPLAYIO_TILEGRID_DEBUG("compose %p done, nothing drawn", self); + return any; + } + #if DISPLAYIO_TILEGRID_DEBUG_ENABLED + // Did the draw actually put pixels in the buffer? Zero here means the compose draw wrote + // nothing, so the problem is upstream of the views; non-zero means the buffer has content + // and the display side is at fault. Scans the composed cell, so it only runs when the + // logging that uses it is compiled in. + uint32_t stride = self->lv_buf->header.stride; + uint32_t bpp = lv_color_format_get_size(self->lv_buf->header.cf); + const uint8_t *sample = self->lv_buf->data + drawn_cell_area.y1 * stride + + drawn_cell_area.x1 * bpp; + uint32_t nonzero = 0; + for (int32_t y = 0; y <= drawn_cell_area.y2 - drawn_cell_area.y1; y++) { + for (int32_t x = 0; x < (drawn_cell_area.x2 - drawn_cell_area.x1 + 1) * (int32_t)bpp; x++) { + nonzero += sample[y * stride + x] != 0; + } + } + DISPLAYIO_TILEGRID_DEBUG("compose %p done buf %p cell (%d,%d)-(%d,%d) nonzero %lu", + self, self->lv_buf->data, (int)drawn_cell_area.x1, (int)drawn_cell_area.y1, + (int)drawn_cell_area.x2, (int)drawn_cell_area.y2, (unsigned long)nonzero); + #endif + } + return any; +} + +// --- Refresh --------------------------------------------------------------- + +static void _refresh(displayio_tilegrid_t *self) { + if (!self->lv_work) { + return; + } + if (self->lv_obj == NULL) { + self->lv_work = false; + DISPLAYIO_TILEGRID_DEBUG("refresh %p unattached, dropping work", self); + return; + } + lv_display_t *display = lv_obj_get_display(self->lv_obj); + if (display == NULL) { + DISPLAYIO_TILEGRID_DEBUG("refresh %p no display, work stays pending", self); + return; + } + DISPLAYIO_TILEGRID_DEBUG("refresh %p direct %d buf %p canvas %p full %d views %d dirty %lu", + self, self->lv_direct, self->lv_buf, self->lv_canvas, self->lv_full_dirty, + self->lv_views_dirty, (unsigned long)self->lv_dirty_count); + DISPLAYIO_TILEGRID_DEBUG("refresh %p obj %dx%d at (%d,%d) scale %u hidden %d", + self, (int)lv_obj_get_width(self->lv_obj), (int)lv_obj_get_height(self->lv_obj), + (int)lv_obj_get_x(self->lv_obj), (int)lv_obj_get_y(self->lv_obj), (unsigned)self->lv_scale, + (int)lv_obj_has_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN)); + bool full = self->lv_full_dirty || self->lv_views_dirty; + bool changed = false; + lv_area_t dirty = {0, 0, -1, -1}; + + // Batch: the compose pass rewrites many objects and only the final invalidation matters. + lv_display_enable_invalidation(display, false); + if (self->lv_direct) { + // The view is the source pixels, so a content change is a plain invalidation. + changed = self->lv_src_dirty; + // Direct sources can still need a cached conversion (RGB565_SWAPPED, for + // example). Pixel writes must invalidate that decode just like composed sources. + if (changed && self->lv_src != NULL) { + lv_image_cache_drop(self->lv_src); + } + self->lv_src_dirty = false; + if (self->lv_views_dirty) { + _layout_views(self); + } + } else { + if (!_ensure_buffer(self)) { + // Out of memory (or a zero-sized grid): there is nothing to compose into. Keep the + // pending work (lv_full_dirty/lv_dirty_count) so the next invalidation retries, but + // don't invalidate here. Invalidating is what makes LVGL run a refresh, and the + // refresh is what calls this function, so invalidating on failure spins the renderer + // at 100% retrying the allocation (and flooding the log) forever. + lv_display_enable_invalidation(display, true); + self->lv_work = false; + DISPLAYIO_TILEGRID_DEBUG("refresh %p no buffer, backing off", self); + return; + } + _sync_source(self); + if (self->lv_views_dirty) { + _layout_views(self); + } + changed = _compose(self, &dirty); + } + lv_display_enable_invalidation(display, true); + + if (full || changed) { + lv_area_t area; + lv_obj_get_coords(self->lv_obj, &area); + if (!full && dirty.x2 >= dirty.x1) { + lv_area_move(&dirty, area.x1, area.y1); + DISPLAYIO_TILEGRID_DEBUG("refresh %p invalidate area (%d,%d)-(%d,%d)", + self, (int)dirty.x1, (int)dirty.y1, (int)dirty.x2, (int)dirty.y2); + lv_obj_invalidate_area(self->lv_obj, &dirty); + } else { + DISPLAYIO_TILEGRID_DEBUG("refresh %p invalidate whole obj at (%d,%d)-(%d,%d)", + self, (int)area.x1, (int)area.y1, (int)area.x2, (int)area.y2); + lv_obj_invalidate(self->lv_obj); + } + } else { + DISPLAYIO_TILEGRID_DEBUG("refresh %p nothing to invalidate", self); + } + self->lv_work = false; +} + +void displayio_lvgl_tilegrid_refresh_all(void) { + for (displayio_tilegrid_t *self = mirror_head; self != NULL; self = self->lv_next) { + _refresh(self); + } +} + +// --- Bitmap and Palette plumbing ------------------------------------------ + +void displayio_lvgl_tilegrid_bitmap_changed(displayio_bitmap_t *bitmap, int16_t x1, int16_t y1, + int16_t x2, int16_t y2) { + if (bitmap->lv_watch_head == NULL) { + return; + } + displayio_lvgl_lock(); + bool gone = bitmap->data == NULL; + for (displayio_tilegrid_t *self = bitmap->lv_watch_head; self != NULL; + self = self->lv_bitmap_next) { + if (self->lv_obj == NULL) { + continue; + } + if (gone) { + // A cached decoder can borrow the Bitmap's pixels too. Drop it while the + // storage still exists, then stop the source descriptor from exposing it. + if (self->lv_src == &self->lv_src_dsc) { + lv_image_cache_drop(self->lv_src); + self->lv_src_dsc.data = NULL; + } + _mark_all(self); + _mark_views(self); + continue; + } + if (self->lv_src_dirty) { + self->lv_src_x1 = LV_MIN(self->lv_src_x1, x1); + self->lv_src_y1 = LV_MIN(self->lv_src_y1, y1); + self->lv_src_x2 = LV_MAX(self->lv_src_x2, x2); + self->lv_src_y2 = LV_MAX(self->lv_src_y2, y2); + } else { + self->lv_src_x1 = x1; + self->lv_src_y1 = y1; + self->lv_src_x2 = x2; + self->lv_src_y2 = y2; + self->lv_src_dirty = true; + } + _mark_work(self); + } + displayio_lvgl_unlock(); +} + +void displayio_lvgl_tilegrid_shader_changed(displayio_tilegrid_t *self) { + if (self->lv_obj == NULL) { + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + if (self->lv_src == &self->lv_src_dsc) { + // The shared palette array was updated in place, so only the decoded view is stale. + lv_image_cache_drop(self->lv_src); + } + if (!self->lv_direct) { + // Gaining or losing transparency changes the composed buffer's format. + lv_color_format_t cf = _source_cf(self->bitmap, self->pixel_shader); + bool argb = _source_has_alpha(self, cf); + if (argb != self->lv_argb) { + self->lv_argb = argb; + _mark_views(self); + } + } + _mark_all(self); + displayio_lvgl_unlock_with_cleanup(); +} + +// --- Lifecycle ------------------------------------------------------------- + +// Bind the source for the current bitmap and pixel shader and schedule a full compose. Caller +// holds the lock and has no source bound. +static bool _rebind(displayio_tilegrid_t *self) { + self->lv_direct = _wants_direct(self); + if (!_bind_source(self)) { + return false; + } + // The supervisor resizes and positions its static terminal grids directly before + // 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); + _set_lv_pos(self); + _mark_all(self); + _mark_views(self); + return true; +} + +void displayio_lvgl_tilegrid_attach(displayio_tilegrid_t *self, lv_obj_t *parent) { + if (!self->in_group || parent == NULL) { + return; + } + // ESP_LOGI(TAG, "attaching tilegrid %p to parent %p", self, parent); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + if (self->lv_obj == NULL) { + if (_create_objects(self, parent)) { + _rebind(self); + _set_lv_pos(self); + _link_mirror(self); + } else { + // Do not root a failed attachment forever. Discard a partially created + // container too; a later group sync can retry attaching it. + _destroy_objects(self); + } + } else if (lv_obj_get_parent(self->lv_obj) != parent) { + lv_obj_set_parent(self->lv_obj, parent); + } + displayio_lvgl_unlock_with_cleanup(); +} + +void displayio_lvgl_tilegrid_detach(displayio_tilegrid_t *self) { + displayio_lvgl_lock(); + _unbind_source(self); + _destroy_objects(self); + _unlink_mirror(self); + // This allocation is outside the Python heap, so collecting the TileGrid cannot + // free it. Drop it even when attachment failed before creating an LVGL object. + lv_free(self->lv_cell_dirty); + self->lv_cell_dirty = NULL; + self->lv_cell_words = 0; + self->lv_dirty_count = 0; + self->lv_full_dirty = true; + self->lv_views_dirty = true; + self->lv_src_dirty = false; + self->lv_work = false; + displayio_lvgl_unlock(); +} + +// Apply the inherited scale chain to the grid. TileGrids have no scale of their own; they render +// at the scale of the parent group chain. +void displayio_lvgl_tilegrid_update_scale(displayio_tilegrid_t *self) { + if (self->lv_obj == NULL) { + return; + } + 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) { + // 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); + } + for (uint8_t i = 0; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { + if (self->lv_views[i] != NULL) { + _set_view_scale(self->lv_views[i], self->lv_scale); + } + } + _set_lv_pos(self); + _mark_views(self); + displayio_lvgl_unlock_with_cleanup(); +} + +// protocol_lvgl implementation. Group owns the tree; these only translate its calls into the +// TileGrid mirror operations above. +// +// A Python subclass of TileGrid (a Label, a RoundRect, ...) is an mp_obj_instance_t that holds +// the native TileGrid in subobj[0], so every function below has to see that native object +// instead of the instance. Resolving it here keeps the type slot on the native type. +static mp_obj_t _tilegrid_protocol_self(mp_obj_t layer) { + mp_obj_t native = mp_obj_cast_to_native_base(layer, MP_OBJ_FROM_PTR(&displayio_tilegrid_type)); + if (native == MP_OBJ_NULL) { + return layer; + } + mp_obj_assert_native_inited(native); + return native; +} + +static lv_obj_t *_tilegrid_protocol_root_obj(mp_obj_t layer) { + return ((displayio_tilegrid_t *)MP_OBJ_TO_PTR(layer))->lv_obj; +} + +static void _tilegrid_protocol_attach(mp_obj_t layer, lv_obj_t *parent) { + displayio_lvgl_tilegrid_attach(MP_OBJ_TO_PTR(layer), parent); +} + +static void _tilegrid_protocol_detach(mp_obj_t layer) { + displayio_lvgl_tilegrid_detach(MP_OBJ_TO_PTR(layer)); +} + +static bool _tilegrid_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, + uint16_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. + self->lv_scale = scale; + self->lv_origin_x = abs_x; + self->lv_origin_y = abs_y; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _set_lv_pos(self); + displayio_lvgl_unlock(); + displayio_lvgl_tilegrid_update_scale(self); + } + return true; +} + +static void _tilegrid_protocol_set_hidden_by_parent(mp_obj_t layer, bool hidden) { + displayio_tilegrid_set_hidden_by_parent(MP_OBJ_TO_PTR(layer), hidden); +} + +static bool _tilegrid_protocol_set_in_group(mp_obj_t layer, bool in_group) { + displayio_tilegrid_t *self = MP_OBJ_TO_PTR(layer); + bool was_in_group = self->in_group; + self->in_group = in_group; + return was_in_group; +} + +const lvgl_protocol_t displayio_lvgl_tilegrid_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = _tilegrid_protocol_self, + .root_obj = _tilegrid_protocol_root_obj, + .attach = _tilegrid_protocol_attach, + .detach = _tilegrid_protocol_detach, + .set_transform = _tilegrid_protocol_set_transform, + .set_hidden_by_parent = _tilegrid_protocol_set_hidden_by_parent, + .set_in_group = _tilegrid_protocol_set_in_group, +}; + +// --- displayio API --------------------------------------------------------- + +void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_t bitmap, + uint16_t bitmap_width_in_tiles, uint16_t bitmap_height_in_tiles, + mp_obj_t pixel_shader, uint16_t width, uint16_t height, + uint16_t tile_width, uint16_t tile_height, uint16_t x, uint16_t y, uint16_t default_tile) { + // Raise before anything is allocated if LVGL can't draw this combination. + _source_cf(bitmap, pixel_shader); + + uint32_t total_tiles = width * height; + self->bitmap_width_in_tiles = bitmap_width_in_tiles; + self->tiles_in_bitmap = bitmap_width_in_tiles * bitmap_height_in_tiles; + + // Determine if we need uint16_t or uint8_t for tile indices + bool use_uint16 = self->tiles_in_bitmap > 255; + + // ESP_LOGI(TAG, "allocating tile grid %d x %d", width, height); + if (use_uint16) { + uint16_t *tiles16 = (uint16_t *)m_malloc_without_collect(total_tiles * sizeof(uint16_t)); + for (uint32_t i = 0; i < total_tiles; i++) { + tiles16[i] = default_tile; + } + self->tiles = tiles16; + } else { + uint8_t *tiles8 = (uint8_t *)m_malloc_without_collect(total_tiles); + for (uint32_t i = 0; i < total_tiles; i++) { + tiles8[i] = (uint8_t)default_tile; + } + self->tiles = tiles8; + } + + self->width_in_tiles = width; + self->height_in_tiles = height; + self->x = x; + self->y = y; + self->pixel_width = width * tile_width; + self->pixel_height = height * tile_height; + self->tile_width = tile_width; + self->tile_height = tile_height; + self->bitmap = bitmap; + self->pixel_shader = pixel_shader; + self->in_group = false; + self->hidden = false; + self->hidden_by_parent = false; + self->flip_x = false; + self->flip_y = false; + self->transpose_xy = false; + self->top_left_x = 0; + self->top_left_y = 0; + self->lv_obj = NULL; + self->lv_canvas = NULL; + self->lv_buf = NULL; + self->lv_src = NULL; + self->lv_cell_dirty = NULL; + self->lv_cell_words = 0; + self->lv_dirty_count = 0; + self->lv_full_dirty = true; + self->lv_work = false; + self->lv_views_dirty = true; + self->lv_src_dirty = false; + self->lv_direct = false; + self->lv_src_odb_full = false; + self->lv_argb = false; + self->lv_view_count = 0; + self->lv_next = NULL; + self->lv_bitmap_next = NULL; + self->lv_scale = 1; + self->lv_origin_x = 0; + self->lv_origin_y = 0; + for (uint8_t i = 0; i < DISPLAYIO_LVGL_TILEGRID_VIEWS; i++) { + self->lv_views[i] = NULL; + } + lv_memzero(&self->lv_src_dsc, sizeof(self->lv_src_dsc)); + lv_memzero(self->lv_view_dsc, sizeof(self->lv_view_dsc)); +} + +bool common_hal_displayio_tilegrid_get_hidden(displayio_tilegrid_t *self) { + return self->hidden; +} + +bool displayio_tilegrid_get_rendered_hidden(displayio_tilegrid_t *self) { + (void)self; + return false; +} + +void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden) { + self->hidden_by_parent = hidden; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + if (self->hidden || self->hidden_by_parent) { + lv_obj_add_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } + displayio_lvgl_unlock(); + } +} + +void common_hal_displayio_tilegrid_set_hidden(displayio_tilegrid_t *self, bool hidden) { + self->hidden = hidden; + displayio_tilegrid_set_hidden_by_parent(self, self->hidden_by_parent); +} + +// Mark the cell of the tile matrix that shows tile (x, y) of the grid for recomposing. +void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + if (self->lv_obj == NULL || self->width_in_tiles == 0 || self->height_in_tiles == 0) { + // Not mirrored yet; binding composes everything. + return; + } + displayio_lvgl_lock(); + uint32_t cell = (uint32_t)(y % self->height_in_tiles) * self->width_in_tiles + + (x % self->width_in_tiles); + _mark_cell(self, cell); + displayio_lvgl_unlock(); +} + +mp_int_t common_hal_displayio_tilegrid_get_x(displayio_tilegrid_t *self) { + return self->x; +} + +void common_hal_displayio_tilegrid_set_x(displayio_tilegrid_t *self, mp_int_t x) { + if (self->x == x) { + return; + } + self->x = x; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _set_lv_pos(self); + displayio_lvgl_unlock(); + } +} + +mp_int_t common_hal_displayio_tilegrid_get_y(displayio_tilegrid_t *self) { + return self->y; +} + +void common_hal_displayio_tilegrid_set_y(displayio_tilegrid_t *self, mp_int_t y) { + if (self->y == y) { + return; + } + self->y = y; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _set_lv_pos(self); + displayio_lvgl_unlock(); + } +} + +mp_obj_t common_hal_displayio_tilegrid_get_pixel_shader(displayio_tilegrid_t *self) { + return self->pixel_shader; +} + +void common_hal_displayio_tilegrid_set_pixel_shader(displayio_tilegrid_t *self, mp_obj_t pixel_shader) { + _source_cf(self->bitmap, pixel_shader); + if (self->lv_obj == NULL) { + self->pixel_shader = pixel_shader; + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // Give up the old shader's source while the descriptor still belongs to it. + _unbind_source(self); + self->pixel_shader = pixel_shader; + _rebind(self); + displayio_lvgl_unlock_with_cleanup(); +} + +mp_obj_t common_hal_displayio_tilegrid_get_bitmap(displayio_tilegrid_t *self) { + return self->bitmap; +} + +void common_hal_displayio_tilegrid_set_bitmap(displayio_tilegrid_t *self, mp_obj_t bitmap) { + _source_cf(bitmap, self->pixel_shader); + if (self->lv_obj == NULL) { + self->bitmap = bitmap; + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + bool linked = _is_linked(self); + // Release the old bitmap's source before the reference to it is replaced. + _unbind_source(self); + if (linked) { + _unwatch_bitmap(self); + } + self->bitmap = bitmap; + _rebind(self); + if (linked) { + _watch_bitmap(self); + } + displayio_lvgl_unlock_with_cleanup(); +} + +uint16_t common_hal_displayio_tilegrid_get_width(displayio_tilegrid_t *self) { + return self->width_in_tiles; +} + +uint16_t common_hal_displayio_tilegrid_get_height(displayio_tilegrid_t *self) { + return self->height_in_tiles; +} + +uint16_t common_hal_displayio_tilegrid_get_tile_width(displayio_tilegrid_t *self) { + return self->tile_width; +} + +uint16_t common_hal_displayio_tilegrid_get_tile_height(displayio_tilegrid_t *self) { + return self->tile_height; +} + +uint16_t common_hal_displayio_tilegrid_get_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + if (self->tiles == NULL) { + return 0; + } + + uint32_t index = y * self->width_in_tiles + x; + if (self->tiles_in_bitmap > 255) { + return ((uint16_t *)self->tiles)[index]; + } + return ((uint8_t *)self->tiles)[index]; +} + +void common_hal_displayio_tilegrid_set_tile(displayio_tilegrid_t *self, uint16_t x, uint16_t y, uint16_t tile_index) { + if (tile_index >= self->tiles_in_bitmap) { + mp_raise_ValueError(MP_ERROR_TEXT("Tile index out of bounds")); + } + + if (self->tiles == NULL) { + return; + } + + uint32_t index = y * self->width_in_tiles + x; + uint16_t old_tile_index; + if (self->tiles_in_bitmap > 255) { + old_tile_index = ((uint16_t *)self->tiles)[index]; + ((uint16_t *)self->tiles)[index] = tile_index; + } else { + old_tile_index = ((uint8_t *)self->tiles)[index]; + ((uint8_t *)self->tiles)[index] = (uint8_t)tile_index; + } + if (old_tile_index != tile_index && tile_index != DISPLAYIO_TILEGRID_BLANK_TILE) { + DISPLAYIO_TILEGRID_DEBUG( + "set_tile %p (%d,%d) tile %d -> %d cell %d mirrored %d", + self, x, y, old_tile_index, tile_index, index, + self->lv_obj != NULL && self->width_in_tiles != 0 && self->height_in_tiles != 0); + } + displayio_tilegrid_mark_tile_dirty(self, x, y); +} + +void common_hal_displayio_tilegrid_set_all_tiles(displayio_tilegrid_t *self, uint16_t tile_index) { + if (tile_index >= self->tiles_in_bitmap) { + mp_raise_ValueError(MP_ERROR_TEXT("Tile index out of bounds")); + } + + if (self->tiles == NULL) { + return; + } + + uint32_t changed = 0; + if (self->tiles_in_bitmap > 255) { + uint16_t *tiles16 = (uint16_t *)self->tiles; + for (uint16_t y = 0; y < self->height_in_tiles; y++) { + for (uint16_t x = 0; x < self->width_in_tiles; x++) { + uint32_t i = y * self->width_in_tiles + x; + if (tiles16[i] != tile_index) { + changed++; + } + tiles16[i] = tile_index; + } + } + } else { + uint8_t *tiles8 = (uint8_t *)self->tiles; + for (uint16_t y = 0; y < self->height_in_tiles; y++) { + for (uint16_t x = 0; x < self->width_in_tiles; x++) { + uint32_t i = y * self->width_in_tiles + x; + if (tiles8[i] != (uint8_t)tile_index) { + changed++; + } + tiles8[i] = (uint8_t)tile_index; + } + } + } + if (changed > 0) { + DISPLAYIO_TILEGRID_DEBUG( + "set_all_tiles %p tile %d changed %d of %d cells mirrored %d", + self, tile_index, changed, self->width_in_tiles * self->height_in_tiles, + self->lv_obj != NULL); + } + + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _mark_all(self); + displayio_lvgl_unlock(); + } +} + +bool common_hal_displayio_tilegrid_get_flip_x(displayio_tilegrid_t *self) { + return self->flip_x; +} + +void common_hal_displayio_tilegrid_set_flip_x(displayio_tilegrid_t *self, bool flip_x) { + if (self->flip_x == flip_x) { + return; + } + if (flip_x) { + _raise_unsupported(); + } + self->flip_x = flip_x; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _mark_all(self); + displayio_lvgl_unlock(); + } +} + +bool common_hal_displayio_tilegrid_get_flip_y(displayio_tilegrid_t *self) { + return self->flip_y; +} + +void common_hal_displayio_tilegrid_set_flip_y(displayio_tilegrid_t *self, bool flip_y) { + if (self->flip_y == flip_y) { + return; + } + if (flip_y) { + _raise_unsupported(); + } + self->flip_y = flip_y; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _mark_all(self); + displayio_lvgl_unlock(); + } +} + +bool common_hal_displayio_tilegrid_get_transpose_xy(displayio_tilegrid_t *self) { + return self->transpose_xy; +} + +void common_hal_displayio_tilegrid_set_transpose_xy(displayio_tilegrid_t *self, bool transpose_xy) { + if (self->transpose_xy == transpose_xy) { + return; + } + if (transpose_xy) { + _raise_unsupported(); + } + self->transpose_xy = transpose_xy; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _mark_all(self); + displayio_lvgl_unlock(); + } +} + +bool common_hal_displayio_tilegrid_contains(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + uint16_t right_edge = self->x + (self->width_in_tiles * self->tile_width); + uint16_t bottom_edge = self->y + (self->height_in_tiles * self->tile_height); + return x >= self->x && x < right_edge && + y >= self->y && y < bottom_edge; +} + +// Scrolling moves the window over the composed buffer, not the pixels: no cell is re-sourced and +// nothing is copied, the views just point somewhere else. +void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint16_t x, uint16_t y) { + // Only the wrapped position matters; the buffer doesn't know the window moved. + uint16_t old_x = self->width_in_tiles == 0 ? 0 : self->top_left_x % self->width_in_tiles; + uint16_t old_y = self->height_in_tiles == 0 ? 0 : self->top_left_y % self->height_in_tiles; + uint16_t new_x = self->width_in_tiles == 0 ? 0 : x % self->width_in_tiles; + uint16_t new_y = self->height_in_tiles == 0 ? 0 : y % self->height_in_tiles; + self->top_left_x = x; + self->top_left_y = y; + if (old_x == new_x && old_y == new_y) { + // Same window: nothing moves. + return; + } + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _mark_views(self); + displayio_lvgl_unlock(); + } +} + +#endif // CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/displayio/lvgl/TileGrid.h b/shared-module/displayio/lvgl/TileGrid.h new file mode 100644 index 00000000000..1b97e55c8e2 --- /dev/null +++ b/shared-module/displayio/lvgl/TileGrid.h @@ -0,0 +1,117 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/Bitmap.h" +#include "shared-module/displayio/Palette.h" +#include "shared-module/displayio/lvgl/lvgl_protocol.h" + +#include "lvgl.h" + +// LVGL-based version of displayio TileGrid. +// +// A TileGrid owns one composed buffer holding exactly one tile grid of pixels, in tile-matrix +// order: cell (i, j) of tiles[] is at (i * tile_width, j * tile_height). The tiles are drawn +// into it through LVGL's draw pipeline, so the palette and the tile indirection are resolved +// once, when a cell changes, instead of on every frame. +// +// The display shows that buffer through up to four lv_images ("views"), because displayio +// scrolls by moving a wrapped window over the tile matrix. A scroll re-points the views and +// invalidates; no pixels move. See lvgl/TileGrid.c for the whole design. +// +// Dirty tracking: the buffer is composed at the start of an LVGL refresh (see +// displayio_lvgl_tilegrid_refresh_all()), so the write path only has to set bits. + +// A wrapped window over the composed buffer is at most two row runs by two column runs. +#define DISPLAYIO_LVGL_TILEGRID_VIEWS 4 + +typedef struct displayio_tilegrid { + mp_obj_base_t base; + mp_obj_t bitmap; + mp_obj_t pixel_shader; + int16_t x; + int16_t y; + uint16_t pixel_width; + uint16_t pixel_height; + uint16_t bitmap_width_in_tiles; + uint16_t tiles_in_bitmap; + uint16_t width_in_tiles; + uint16_t height_in_tiles; + uint16_t tile_width; + uint16_t tile_height; + uint16_t top_left_x; + uint16_t top_left_y; + void *tiles; // Can be either uint8_t* or uint16_t* depending on tiles_in_bitmap + bool in_group : 1; + bool flip_x : 1; + bool flip_y : 1; + bool transpose_xy : 1; + bool hidden : 1; + bool hidden_by_parent : 1; + uint16_t padding : 10; + // LVGL mirror state. + lv_obj_t *lv_obj; // Container of the views, or the single image on the direct path. + lv_obj_t *lv_views[DISPLAYIO_LVGL_TILEGRID_VIEWS]; + lv_image_dsc_t lv_view_dsc[DISPLAYIO_LVGL_TILEGRID_VIEWS]; + uint8_t lv_view_count; + lv_draw_buf_t *lv_buf; // Composed pixels; owned by lv_canvas once it exists. + lv_obj_t *lv_canvas; // Hidden canvas that hosts the compose layer. + // The source atlas: the whole bitmap as one LVGL image, drawn one cell at a time. + const lv_image_dsc_t *lv_src; + lv_image_dsc_t lv_src_dsc; // Our own descriptor for a Bitmap source. + // A single-cell grid of a direct color source shows those pixels as they are instead of + // copying them into a composed buffer. + bool lv_direct; + bool lv_src_odb_full; // lv_src is the whole-image OnDiskBitmap decode. + bool lv_argb; // Composed buffer carries alpha (the source has transparent pixels). + // Cells of the composed buffer that need composing, one bit per cell. + uint32_t *lv_cell_dirty; + uint32_t lv_cell_words; + uint32_t lv_dirty_count; + bool lv_full_dirty; + bool lv_work; // Has something to do at the next refresh. + bool lv_views_dirty; // The scroll window moved; re-point the views. + // Pixels of the source changed. Writers mark the area before they change it, so the + // next compose pass rereads the bitmap and recomposes the cells the area reaches. + int16_t lv_src_x1; + int16_t lv_src_y1; + int16_t lv_src_x2; + int16_t lv_src_y2; + 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. + int32_t lv_origin_x; // The parent group's origin in screen pixels. + int32_t lv_origin_y; +} displayio_tilegrid_t; + +void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden); +bool displayio_tilegrid_get_rendered_hidden(displayio_tilegrid_t *self); +void displayio_tilegrid_mark_tile_dirty(displayio_tilegrid_t *self, uint16_t x, uint16_t y); + +// TileGrid mirror lifecycle, implemented in TileGrid.c and called by Group.c. +void displayio_lvgl_tilegrid_attach(displayio_tilegrid_t *self, lv_obj_t *parent); +void displayio_lvgl_tilegrid_detach(displayio_tilegrid_t *self); +void displayio_lvgl_tilegrid_update_scale(displayio_tilegrid_t *self); + +// Compose whatever changed since the last refresh. Called at the start of every LVGL refresh, +// before anything is drawn, with the LVGL lock already held by the renderer. +void displayio_lvgl_tilegrid_refresh_all(void); +// The pixels of bitmap changed (or went away). Called from the Bitmap write path. +void displayio_lvgl_tilegrid_bitmap_changed(displayio_bitmap_t *bitmap, + int16_t x1, int16_t y1, int16_t x2, int16_t y2); +// The Palette this TileGrid draws with changed. +void displayio_lvgl_tilegrid_shader_changed(displayio_tilegrid_t *self); +// Root the attached TileGrids during GC. They live in a port-memory list. +void displayio_lvgl_tilegrid_gc_collect(void); + +// How Group attaches, positions and hides a TileGrid. See lvgl_protocol.h. +extern const lvgl_protocol_t displayio_lvgl_tilegrid_protocol; diff --git a/shared-module/displayio/lvgl/lvgl_internal.h b/shared-module/displayio/lvgl/lvgl_internal.h new file mode 100644 index 00000000000..3c5da8c4863 --- /dev/null +++ b/shared-module/displayio/lvgl/lvgl_internal.h @@ -0,0 +1,83 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Internal declarations shared by the files that implement the LVGL-based +// displayio compositing core (shared-module/displayio/lvgl/). + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "py/nlr.h" +#include "shared-module/displayio/Group.h" +#include "shared-module/displayio/TileGrid.h" +#include "shared-module/displayio/Bitmap.h" +#include "shared-module/displayio/Palette.h" +#include "shared-module/displayio/OnDiskBitmap.h" +#include "shared-module/displayio/lvgl/ImageRoot.h" +#include "shared-module/displayio/lvgl/Palette.h" + +#if CIRCUITPY_DISPLAYIO_LVGL + +// 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); + +// Port-supplied recursion-safe lock around LVGL. The RTOS renders LVGL on its own +// thread (e.g. a Zephyr workqueue or the ESP32 LVGL task), so CircuitPython-side mirror +// mutations must be excluded from rendering. The lock must be recursive: the shared +// wrappers may nest, and the render thread uses the same lock. +void displayio_lvgl_port_lock(void); +void displayio_lvgl_port_unlock(void); + +// Port hook: arrange for BusDisplay refreshes to run on the CircuitPython thread when the port +// renders LVGL on a task. Called once per BusDisplay, after its refresh timer exists. Ports that +// render BusDisplay on their own LVGL thread (Zephyr) do nothing. +void displayio_lvgl_port_install_busdisplay_refresh(lv_display_t *display); + +// Port hook: a framebuffer reached a frame boundary. May be called from an interrupt handler, so +// ports that have a render task must defer it safely and render on the task. In task context, the +// ESP port renders synchronously. +void displayio_lvgl_port_frame_boundary(void); + +// Shared lock wrappers. All display-side (CP thread) LVGL calls must happen inside these. +void displayio_lvgl_lock(void); +void displayio_lvgl_unlock(void); + +// CircuitPython-thread mutations that may raise must register an unlock on exception. +// The caller owns the stack node until unlock_with_cleanup(). Do not use these from +// the render task, which has no active Python exception/cleanup stack. +void displayio_lvgl_lock_with_cleanup(nlr_jump_callback_node_t *cleanup); +void displayio_lvgl_unlock_with_cleanup(void); + +// Set up LVGL for displayio. Called by the port's display constructor. +void displayio_lvgl_init(void); + +// Register displayio's per-display state: the screen background and the refresh hooks, including +// the compose pass that brings every attached TileGrid's buffer up to date before each refresh +// draws anything. Called once per LVGL display, after it is created (and made default). +void displayio_lvgl_register_display(lv_display_t *display); + +// True when the displays the compositor drives still want automatic repaints +// (display.auto_refresh). A port's render task consults this so turning auto_refresh off stops +// repaints without also stopping the rest of the port's background work. +bool displayio_lvgl_auto_refresh(void); + +// Render displays that can't flush from the LVGL task (currently BusDisplay when the port asks +// for it). Called on the CircuitPython thread by the port's proxy, under the LVGL lock. +void displayio_lvgl_refresh_unsafe_displays(void); + +// Render the framebuffer displays that pace themselves from a frame boundary. Called on the +// CircuitPython thread (directly or from the render task after an ISR notification), under the +// LVGL lock. +void displayio_lvgl_refresh_paced_displays(void); + +// Attach/detach the compositor's root group. Implemented in lvgl/Group.c. NULL detach. +void displayio_lvgl_set_root_group(displayio_group_t *group); + +#endif // CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/displayio/lvgl/lvgl_protocol.h b/shared-module/displayio/lvgl/lvgl_protocol.h new file mode 100644 index 00000000000..daaa08cfa02 --- /dev/null +++ b/shared-module/displayio/lvgl/lvgl_protocol.h @@ -0,0 +1,67 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "py/proto.h" + +#include "lvgl.h" + +// Everything an LVGL-backed displayio layer has to provide to displayio.Group, independent +// of its type. This is the LVGL counterpart of the scanline compositor's protocol_draw (see +// shared-bindings/vectorio/__init__.h): Group mirrors its members into the LVGL object tree +// through these calls and nothing else in the compositor needs to know what a member is. +// +// The layer owns its mirror. Once attached, LVGL owns the lv_obj, so a layer never sets the +// mirror's parent or z-order itself; Group does that through root_obj(). +// +// A type has a single protocol slot, so a layer implements either this (LVGL builds) or +// protocol_draw (legacy builds), never both. Each backend's dispatcher header picks the +// right object with a *_LVGL_PROTOCOL macro; it is NULL in legacy builds. +// +// Group calls every method above with the LVGL lock held, and passes the object protocol_self +// resolved. Implementations may take the lock again (it is depth counted). +typedef struct _lvgl_protocol_t { + MP_PROTOCOL_HEAD // MP_QSTR_protocol_lvgl + + // The object the rest of the calls are made on, or the layer itself when the protocol is + // implemented on the layer. This must resolve two things: vectorio's shapes (the + // Python-visible Rectangle, Circle or Polygon owns the wrapper that holds the shape's + // compositor state, so it forwards to that wrapper) and Python subclasses of a layer (a + // PageLayout, Tile or Label is an mp_obj_instance_t whose native base is in subobj[0]). + // May be NULL. + mp_obj_t (*protocol_self)(mp_obj_t self); + + // The layer's root lv_obj, or NULL before attach(). Group uses it for z-order and + // reparenting. + lv_obj_t *(*root_obj)(mp_obj_t self); + + // Create the mirror under parent if needed, reparent it, and push every Python-visible + // property into it. + void (*attach)(mp_obj_t self, lv_obj_t *parent); + + // Delete the mirror and any port allocations behind it, and clear root_obj. Must leave + // the layer reusable: a later attach() rebuilds the mirror. Does not touch group state. + void (*detach)(mp_obj_t self); + + // 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); + + // Propagate an ancestor's hidden state. NULL means always drawn while attached. + void (*set_hidden_by_parent)(mp_obj_t self, bool hidden); + + // Set or clear group membership, returning the previous value so Group can reject a + // layer that is already in one. NULL means the layer can't be a group member. + bool (*set_in_group)(mp_obj_t self, bool in_group); +} lvgl_protocol_t; diff --git a/shared-module/epaperdisplay/EPaperDisplay.c b/shared-module/epaperdisplay/EPaperDisplay.c index b1fa15a1241..a7d4f1262ad 100644 --- a/shared-module/epaperdisplay/EPaperDisplay.c +++ b/shared-module/epaperdisplay/EPaperDisplay.c @@ -599,7 +599,7 @@ static bool epaperdisplay_epaperdisplay_refresh_area(epaperdisplay_epaperdisplay uint16_t remaining_rows = displayio_area_height(&clipped); if (self->bus.row_command != NO_COMMAND) { - displayio_display_bus_set_region_to_update(&self->bus, &self->core, &clipped); + displayio_display_bus_set_region_to_update(&self->bus, &self->core.colorspace, &clipped); } uint8_t write_command = self->write_black_ram_command; diff --git a/shared-module/epaperdisplay/lag_drive.c b/shared-module/epaperdisplay/lag_drive.c index 69f875edefb..1e0eb7e9f98 100644 --- a/shared-module/epaperdisplay/lag_drive.c +++ b/shared-module/epaperdisplay/lag_drive.c @@ -80,7 +80,7 @@ static bool lag_set_region(epaperdisplay_epaperdisplay_obj_t *self, displayio_ar nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { self->lag.bus_held = true; - displayio_display_bus_set_region_to_update(&self->bus, &self->core, box); + displayio_display_bus_set_region_to_update(&self->bus, &self->core.colorspace, box); self->lag.bus_held = false; nlr_pop(); return true; diff --git a/shared-module/framebufferio/FramebufferDisplay.h b/shared-module/framebufferio/FramebufferDisplay.h index 871d34abcbe..a1761382353 100644 --- a/shared-module/framebufferio/FramebufferDisplay.h +++ b/shared-module/framebufferio/FramebufferDisplay.h @@ -7,77 +7,8 @@ #pragma once -#include "py/obj.h" -#include "py/proto.h" - -#include "shared-bindings/displayio/Group.h" - -#include "shared-module/displayio/area.h" -#include "shared-module/displayio/display_core.h" - -typedef struct { - mp_obj_base_t base; - displayio_display_core_t core; - mp_obj_t framebuffer; - const struct _framebuffer_p_t *framebuffer_protocol; - mp_buffer_info_t bufinfo; - uint64_t last_refresh_call; - uint16_t native_frames_per_second; - uint16_t native_ms_per_frame; - uint16_t first_pixel_offset; - uint16_t row_stride; - bool auto_refresh; - bool first_manual_refresh; -} framebufferio_framebufferdisplay_obj_t; - -void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t *self); -void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t *self); -void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t *self); - -void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t *self); - -mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t *self); - -typedef bool (*framebuffer_get_reverse_pixels_in_byte_fun)(mp_obj_t); -typedef bool (*framebuffer_get_reverse_pixels_in_word_fun)(mp_obj_t); -typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t); -typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t); -typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t); -typedef int (*framebuffer_get_first_pixel_offset_fun)(mp_obj_t); -typedef bool (*framebuffer_get_grayscale_fun)(mp_obj_t); -typedef int (*framebuffer_get_height_fun)(mp_obj_t); -typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t); -typedef bool (*framebuffer_get_pixels_in_byte_share_row_fun)(mp_obj_t); -typedef int (*framebuffer_get_row_stride_fun)(mp_obj_t); -typedef int (*framebuffer_get_width_fun)(mp_obj_t); -typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t); -typedef void (*framebuffer_deinit_fun)(mp_obj_t); -typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo); -typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t, uint8_t *dirty_row_bitmask); - -typedef struct _framebuffer_p_t { - MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer - - // Mandatory - framebuffer_get_bufinfo_fun get_bufinfo; - framebuffer_swapbuffers_fun swapbuffers; - framebuffer_deinit_fun deinit; - framebuffer_get_width_fun get_width; - framebuffer_get_height_fun get_height; - - // Optional getters - framebuffer_get_bytes_per_cell_fun get_bytes_per_cell; // default: 2 - framebuffer_get_color_depth_fun get_color_depth; // default: 16 - framebuffer_get_first_pixel_offset_fun get_first_pixel_offset; // default: 0 - framebuffer_get_grayscale_fun get_grayscale; // default: grayscale if depth < 8 - framebuffer_get_native_frames_per_second_fun get_native_frames_per_second; // default: 60 - framebuffer_get_pixels_in_byte_share_row_fun get_pixels_in_byte_share_row; // default: false - framebuffer_get_reverse_pixels_in_byte_fun get_reverse_pixels_in_byte; // default: false - framebuffer_get_reverse_pixels_in_word_fun get_reverse_pixels_in_word; // default: false - framebuffer_get_row_stride_fun get_row_stride; // default: 0 (no extra row padding) - - // Optional -- default is no brightness control - framebuffer_get_brightness_fun get_brightness; - framebuffer_set_brightness_fun set_brightness; - -} framebuffer_p_t; +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/framebufferio/lvgl/FramebufferDisplay.h" +#else +#include "shared-module/framebufferio/legacy/FramebufferDisplay.h" +#endif diff --git a/shared-module/framebufferio/__init__.h b/shared-module/framebufferio/__init__.h index c9069db9fd7..5b840d8e6e9 100644 --- a/shared-module/framebufferio/__init__.h +++ b/shared-module/framebufferio/__init__.h @@ -5,3 +5,8 @@ // SPDX-License-Identifier: MIT #pragma once + +// Optional frame-boundary callback. The framebuffer invokes it once per frame from a +// task/background context, never an interrupt handler. A display uses it to pace rendering to +// the source (for example a UVC frame or a panel vsync) instead of the autorefresh tick. +typedef void (*framebuffer_frame_callback_t)(void *user_data); diff --git a/shared-module/framebufferio/FramebufferDisplay.c b/shared-module/framebufferio/legacy/FramebufferDisplay.c similarity index 99% rename from shared-module/framebufferio/FramebufferDisplay.c rename to shared-module/framebufferio/legacy/FramebufferDisplay.c index d0d31faeace..d9d9bdbd494 100644 --- a/shared-module/framebufferio/FramebufferDisplay.c +++ b/shared-module/framebufferio/legacy/FramebufferDisplay.c @@ -21,6 +21,7 @@ #include "supervisor/shared/web_workflow/display_stream.h" #endif + #if CIRCUITPY_TINYUSB #include "supervisor/usb.h" #endif @@ -33,6 +34,7 @@ ? self->framebuffer_protocol->method(self->framebuffer) \ : (default_value)) + void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebufferdisplay_obj_t *self, mp_obj_t framebuffer, uint16_t rotation, @@ -72,6 +74,7 @@ void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebu self->native_frames_per_second = fb_getter_default(get_native_frames_per_second, 60); self->native_ms_per_frame = 1000 / self->native_frames_per_second; + if (rotation != 0) { common_hal_framebufferio_framebufferdisplay_set_rotation(self, rotation); } diff --git a/shared-module/framebufferio/legacy/FramebufferDisplay.h b/shared-module/framebufferio/legacy/FramebufferDisplay.h new file mode 100644 index 00000000000..9e98e5f5d04 --- /dev/null +++ b/shared-module/framebufferio/legacy/FramebufferDisplay.h @@ -0,0 +1,88 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2020 Jeff Epler for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "shared-module/framebufferio/__init__.h" +#include "py/obj.h" +#include "py/proto.h" + +#include "shared-bindings/displayio/Group.h" + + +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + mp_obj_t framebuffer; + const struct _framebuffer_p_t *framebuffer_protocol; + mp_buffer_info_t bufinfo; + uint64_t last_refresh_call; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + uint16_t first_pixel_offset; + uint16_t row_stride; + bool auto_refresh; + bool first_manual_refresh; +} framebufferio_framebufferdisplay_obj_t; + +void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t *self); +void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t *self); +void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t *self); + +void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t *self); + +mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t *self); + +typedef bool (*framebuffer_get_reverse_pixels_in_byte_fun)(mp_obj_t); +typedef bool (*framebuffer_get_reverse_pixels_in_word_fun)(mp_obj_t); +typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t); +typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t); +typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t); +typedef int (*framebuffer_get_first_pixel_offset_fun)(mp_obj_t); +typedef bool (*framebuffer_get_grayscale_fun)(mp_obj_t); +typedef int (*framebuffer_get_height_fun)(mp_obj_t); +typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t); +typedef bool (*framebuffer_get_pixels_in_byte_share_row_fun)(mp_obj_t); +typedef int (*framebuffer_get_row_stride_fun)(mp_obj_t); +typedef int (*framebuffer_get_width_fun)(mp_obj_t); +typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t); +typedef void (*framebuffer_deinit_fun)(mp_obj_t); +typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo); +typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t, uint8_t *dirty_row_bitmask); +typedef void (*framebuffer_set_frame_callback_fun)(mp_obj_t, framebuffer_frame_callback_t, void *); + +typedef struct _framebuffer_p_t { + MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer + + // Mandatory + framebuffer_get_bufinfo_fun get_bufinfo; + framebuffer_swapbuffers_fun swapbuffers; + framebuffer_deinit_fun deinit; + framebuffer_get_width_fun get_width; + framebuffer_get_height_fun get_height; + + // Optional getters + framebuffer_get_bytes_per_cell_fun get_bytes_per_cell; // default: 2 + framebuffer_get_color_depth_fun get_color_depth; // default: 16 + framebuffer_get_first_pixel_offset_fun get_first_pixel_offset; // default: 0 + framebuffer_get_grayscale_fun get_grayscale; // default: grayscale if depth < 8 + framebuffer_get_native_frames_per_second_fun get_native_frames_per_second; // default: 60 + framebuffer_get_pixels_in_byte_share_row_fun get_pixels_in_byte_share_row; // default: false + framebuffer_get_reverse_pixels_in_byte_fun get_reverse_pixels_in_byte; // default: false + framebuffer_get_reverse_pixels_in_word_fun get_reverse_pixels_in_word; // default: false + framebuffer_get_row_stride_fun get_row_stride; // default: 0 (no extra row padding) + + // Optional -- default is no brightness control + framebuffer_get_brightness_fun get_brightness; + framebuffer_set_brightness_fun set_brightness; + + // Optional -- called at each frame boundary. Pass a NULL callback to clear it. + framebuffer_set_frame_callback_fun set_frame_callback; + +} framebuffer_p_t; diff --git a/shared-module/framebufferio/lvgl/FramebufferDisplay.c b/shared-module/framebufferio/lvgl/FramebufferDisplay.c new file mode 100644 index 00000000000..78eb2d5ae13 --- /dev/null +++ b/shared-module/framebufferio/lvgl/FramebufferDisplay.c @@ -0,0 +1,587 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2018 Scott Shawcroft for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2020 Jeff Epler for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#include "shared-bindings/framebufferio/FramebufferDisplay.h" + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/time/__init__.h" +#include "shared-module/displayio/__init__.h" +#include "shared-module/displayio/display_core.h" +#include "supervisor/shared/display.h" +#include "supervisor/shared/tick.h" + +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#if CIRCUITPY_VIDEOCORE +#include "shared-module/videocore/VideoBuffer.h" +#endif +#include "supervisor/port_heap.h" + +#if CIRCUITPY_ESPIDF +#include "esp_attr.h" +#include +#define FRAMEBUFFERIO_LVGL_FRAME_CB IRAM_ATTR +#else +#define FRAMEBUFFERIO_LVGL_FRAME_CB +#endif + +#include "lvgl.h" +#include "lvgl/draw/lv_draw_utils.h" + +// Port allocations must be aligned enough for LVGL draw buffers and the PPA. +MP_STATIC_ASSERT(MICROPY_BYTES_PER_GC_BLOCK % LV_DRAW_BUF_ALIGN == 0); + +#if CIRCUITPY_TINYUSB +#include "supervisor/usb.h" +#endif + +#include +#include + +#define fb_getter_default(method, default_value) \ + (self->framebuffer_protocol->method \ + ? self->framebuffer_protocol->method(self->framebuffer) \ + : (default_value)) + + +// Rendering directly into a display's scanout framebuffer while its DMA is active can +// cause tearing or corruption on some controllers. Use the aligned partial buffer path +// by default; enable this only after validating direct rendering on a given port. +#define FRAMEBUFFERIO_LVGL_DIRECT 0 + +// Height of the aligned draw buffer used when the provided framebuffer can't be +// rendered into directly (for example, because it isn't aligned for LVGL's draw units). +#define FRAMEBUFFERIO_LVGL_PARTIAL_LINES 20 + +#define MARK_ROW_DIRTY(r) (self->lv_dirty_row_bitmask[(r) / 8] |= (1 << ((r) & 7))) + +static lv_color_format_t framebufferio_lvgl_color_format(framebufferio_framebufferdisplay_obj_t *self) { + switch (self->core.colorspace.depth) { + case 16: + return self->core.colorspace.reverse_bytes_in_word ? LV_COLOR_FORMAT_RGB565_SWAPPED : LV_COLOR_FORMAT_RGB565; + case 24: + return LV_COLOR_FORMAT_RGB888; + case 32: + return LV_COLOR_FORMAT_ARGB8888; + case 8: + return LV_COLOR_FORMAT_L8; + case 4: + return LV_COLOR_FORMAT_I4; + case 2: + return LV_COLOR_FORMAT_I2; + case 1: + return LV_COLOR_FORMAT_I1; + default: + return LV_COLOR_FORMAT_RGB565; + } +} + +static bool framebufferio_lvgl_can_render_direct(framebufferio_framebufferdisplay_obj_t *self) { + // Rotation is handled while flushing, which requires an intermediate buffer. + if (self->core.rotation != 0) { + return false; + } + uint8_t depth = self->core.colorspace.depth; + if (depth < 8 || depth % 8 != 0) { + return false; + } + size_t bytes_per_pixel = depth / 8; + if (self->row_stride != self->core.width * bytes_per_pixel || + self->row_stride != lv_draw_buf_width_to_stride(self->core.width, self->lv_color_format)) { + return false; + } + if (self->bufinfo.buf == NULL) { + return false; + } + // PPA also requires its output buffer size to be cache-line aligned. + if ((self->row_stride * self->core.height) % LV_DRAW_BUF_ALIGN != 0) { + return false; + } + uint8_t *framebuffer = (uint8_t *)self->bufinfo.buf + self->first_pixel_offset; + return lv_draw_buf_align(framebuffer, self->lv_color_format) == framebuffer; +} + +static void framebufferio_lvgl_free_partial_buf(framebufferio_framebufferdisplay_obj_t *self) { + if (self->lv_buf != NULL) { + port_free(self->lv_buf); + self->lv_buf = NULL; + self->lv_buf_size = 0; + } +} + +static bool framebufferio_lvgl_ensure_partial_buf(framebufferio_framebufferdisplay_obj_t *self) { + if (self->lv_buf != NULL) { + return true; + } + uint16_t max_width = self->core.width > self->core.height ? self->core.width : self->core.height; + size_t lines = FRAMEBUFFERIO_LVGL_PARTIAL_LINES; + if (lines > max_width) { + lines = max_width; + } + size_t buf_size = lv_draw_buf_width_to_stride(max_width, self->lv_color_format) * lines; + // The PPA draw unit requires its output buffer size to be cache-line aligned too. + buf_size = (buf_size + LV_DRAW_BUF_ALIGN - 1) & ~(LV_DRAW_BUF_ALIGN - 1); + self->lv_buf = port_malloc(buf_size, false); + if (self->lv_buf == NULL) { + return false; + } + self->lv_buf_size = buf_size; + return true; +} + +static bool framebufferio_lvgl_configure_buffers(framebufferio_framebufferdisplay_obj_t *self) { + if (FRAMEBUFFERIO_LVGL_DIRECT && framebufferio_lvgl_can_render_direct(self)) { + framebufferio_lvgl_free_partial_buf(self); + uint8_t *framebuffer = (uint8_t *)self->bufinfo.buf + self->first_pixel_offset; + lv_display_set_buffers(self->lv_display, framebuffer, NULL, + self->row_stride * self->core.height, LV_DISPLAY_RENDER_MODE_DIRECT); + self->lv_direct = true; + return true; + } + + if (!framebufferio_lvgl_ensure_partial_buf(self)) { + return false; + } + lv_display_set_buffers(self->lv_display, self->lv_buf, NULL, self->lv_buf_size, + LV_DISPLAY_RENDER_MODE_PARTIAL); + self->lv_direct = false; + return true; +} + +// Framebuffers that can signal a frame boundary pace LVGL instead of using the autorefresh tick. +// The refresh timer callback only pauses the timer: invalidations resume it, but nothing is drawn +// until the framebuffer reports a frame. That batches all changes made within one frame. +static void framebufferio_lvgl_paced_refr_cb(lv_timer_t *timer) { + lv_timer_pause(timer); +} + +// Called by the framebuffer at its frame boundary. It may run from an ISR (DPI vsync), so this +// must stay in IRAM on ESP and only hand off to the port hook. +static void FRAMEBUFFERIO_LVGL_FRAME_CB framebufferio_lvgl_frame_cb(void *user_data) { + framebufferio_framebufferdisplay_obj_t *self = user_data; + if (!self->auto_refresh || self->lv_display == NULL) { + return; + } + displayio_lvgl_port_frame_boundary(); +} + +static void framebufferio_lvgl_flush_cb(lv_display_t *disp, const lv_area_t *area, uint8_t *px_map) { + framebufferio_framebufferdisplay_obj_t *self = lv_display_get_user_data(disp); + lv_color_format_t color_format = lv_display_get_color_format(disp); + lv_display_rotation_t rotation = lv_display_get_rotation(disp); + uint16_t width = lv_area_get_width(area); + uint16_t height = lv_area_get_height(area); + size_t bytes_per_pixel = (self->core.colorspace.depth + 7) / 8; + uint8_t *framebuffer = (uint8_t *)self->bufinfo.buf + self->first_pixel_offset; + + if (rotation == LV_DISPLAY_ROTATION_0) { + if (!self->lv_direct) { + size_t source_stride = lv_draw_buf_width_to_stride(width, color_format); + size_t row_bytes = (width * self->core.colorspace.depth + 7) / 8; + for (uint16_t y = 0; y < height; y++) { + uint8_t *dest = framebuffer + (area->y1 + y) * self->row_stride + area->x1 * bytes_per_pixel; + memcpy(dest, px_map + y * source_stride, row_bytes); + } + } + for (uint16_t y = area->y1; y <= area->y2; y++) { + MARK_ROW_DIRTY(y); + } + } else { + lv_area_t rotated_area = *area; + lv_display_rotate_area(disp, &rotated_area); + size_t source_stride = lv_draw_buf_width_to_stride(width, color_format); + uint8_t *dest = framebuffer + rotated_area.y1 * self->row_stride + rotated_area.x1 * bytes_per_pixel; + // RGB565_SWAPPED uses the same 16-bit pixel layout for the purpose of moving pixels. + lv_color_format_t rotate_format = color_format; + if (rotate_format == LV_COLOR_FORMAT_RGB565_SWAPPED) { + rotate_format = LV_COLOR_FORMAT_RGB565; + } + lv_draw_rotate(px_map, dest, width, height, source_stride, self->row_stride, rotation, rotate_format); + for (uint16_t y = rotated_area.y1; y <= rotated_area.y2; y++) { + MARK_ROW_DIRTY(y); + } + } + + // Publish each flushed area now. LVGL doesn't reliably report the last flush part in all + // port/display combinations, and relying on it can leave new pixels in the CPU cache forever. + self->framebuffer_protocol->swapbuffers(self->framebuffer, self->lv_dirty_row_bitmask); + memset(self->lv_dirty_row_bitmask, 0, self->lv_dirty_row_bitmask_size); + lv_display_flush_ready(disp); +} + + +typedef struct { + nlr_jump_callback_node_t node; + framebufferio_framebufferdisplay_obj_t *self; + bool locked; +} framebufferdisplay_construct_cleanup_t; + +static void framebufferdisplay_construct_abort(void *ctx) { + framebufferdisplay_construct_cleanup_t *cleanup = ctx; + framebufferio_framebufferdisplay_obj_t *self = cleanup->self; + if (!cleanup->locked) { + displayio_lvgl_lock(); + } + self->auto_refresh = false; + if (self->lv_paced) { + self->framebuffer_protocol->set_frame_callback(self->framebuffer, NULL, NULL); + self->lv_paced = false; + } + if (self->core.current_group != NULL) { + displayio_lvgl_set_root_group(NULL); + release_display_core(&self->core); + self->core.current_group = NULL; + } + if (self->lv_display != NULL) { + lv_display_delete(self->lv_display); + self->lv_display = NULL; + } + framebufferio_lvgl_free_partial_buf(self); + port_free(self->lv_dirty_row_bitmask); + self->lv_dirty_row_bitmask = NULL; + self->lv_dirty_row_bitmask_size = 0; + // A failed display constructor does not take ownership of the framebuffer. + self->framebuffer = mp_const_none; + self->base.type = &mp_type_NoneType; + displayio_lvgl_unlock(); +} + +void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebufferdisplay_obj_t *self, + mp_obj_t framebuffer, + uint16_t rotation, + bool auto_refresh) { + // Turn off auto-refresh as we init. + self->auto_refresh = false; + self->framebuffer = framebuffer; + self->framebuffer_protocol = NULL; + self->core.current_group = NULL; + self->lv_display = NULL; + self->lv_color_format = LV_COLOR_FORMAT_UNKNOWN; + self->lv_buf = NULL; + self->lv_buf_size = 0; + self->lv_dirty_row_bitmask = NULL; + self->lv_dirty_row_bitmask_size = 0; + self->lv_direct = false; + self->lv_paced = false; + framebufferdisplay_construct_cleanup_t cleanup = { .self = self, .locked = false }; + nlr_push_jump_callback(&cleanup.node, framebufferdisplay_construct_abort); + self->framebuffer_protocol = mp_proto_get_or_throw(MP_QSTR_protocol_framebuffer, framebuffer); + + uint16_t depth = fb_getter_default(get_color_depth, 16); + // The flush path copies whole pixels. Packed sub-byte framebuffers need a + // separate bit-addressed transfer path; accepting them would overrun rows. + if (depth < 8) { + mp_raise_NotImplementedError(MP_ERROR_TEXT("Format not supported")); + } + displayio_display_core_construct( + &self->core, + self->framebuffer_protocol->get_width(self->framebuffer), + self->framebuffer_protocol->get_height(self->framebuffer), + 0, // rotation + depth, + fb_getter_default(get_grayscale, (depth < 8)), + fb_getter_default(get_pixels_in_byte_share_row, false), + fb_getter_default(get_bytes_per_cell, 2), + fb_getter_default(get_reverse_pixels_in_byte, false), + fb_getter_default(get_reverse_pixels_in_word, false) + ); + + self->first_pixel_offset = fb_getter_default(get_first_pixel_offset, 0); + self->row_stride = fb_getter_default(get_row_stride, 0); + if (self->row_stride == 0) { + self->row_stride = self->core.width * self->core.colorspace.depth / 8; + } + + self->framebuffer_protocol->get_bufinfo(self->framebuffer, &self->bufinfo); + size_t framebuffer_size = self->first_pixel_offset + self->row_stride * (self->core.height - 1) + self->core.width * self->core.colorspace.depth / 8; + + mp_arg_validate_length_min(self->bufinfo.len, framebuffer_size, MP_QSTR_framebuffer); + + self->first_manual_refresh = !auto_refresh; + + self->native_frames_per_second = fb_getter_default(get_native_frames_per_second, 60); + self->native_ms_per_frame = 1000 / self->native_frames_per_second; + + displayio_lvgl_port_init(); + // Hold the render lock across the whole setup. The render task must not see this display as + // the default before it has buffers and a flush callback. The port lock is recursive, so the + // displayio_lvgl_* helpers below may take it again. + displayio_lvgl_lock(); + cleanup.locked = true; + + self->lv_display = lv_display_create(self->core.width, self->core.height); + if (self->lv_display == NULL) { + mp_raise_RuntimeError(MP_ERROR_TEXT("LVGL display create failed")); + } + lv_display_set_user_data(self->lv_display, self); + lv_display_set_default(self->lv_display); + displayio_lvgl_register_display(self->lv_display); + self->lv_color_format = framebufferio_lvgl_color_format(self); + lv_display_set_color_format(self->lv_display, self->lv_color_format); + lv_display_set_flush_cb(self->lv_display, framebufferio_lvgl_flush_cb); + if (self->framebuffer_protocol->set_frame_callback != NULL) { + lv_timer_t *refr_timer = lv_display_get_refr_timer(self->lv_display); + if (refr_timer != NULL) { + lv_timer_set_cb(refr_timer, framebufferio_lvgl_paced_refr_cb); + self->lv_paced = true; + self->framebuffer_protocol->set_frame_callback(self->framebuffer, + framebufferio_lvgl_frame_cb, self); + } + } + uint16_t max_dimension = self->core.width > self->core.height ? self->core.width : self->core.height; + self->lv_dirty_row_bitmask_size = (max_dimension + 7) / 8; + self->lv_dirty_row_bitmask = port_malloc(self->lv_dirty_row_bitmask_size, false); + if (self->lv_dirty_row_bitmask == NULL) { + mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("LVGL dirty row alloc failed")); + } + memset(self->lv_dirty_row_bitmask, 0, self->lv_dirty_row_bitmask_size); + displayio_lvgl_init(); + if (!framebufferio_lvgl_configure_buffers(self)) { + mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("LVGL buffer alloc failed")); + } + #if CIRCUITPY_ESPIDF + ESP_LOGI("LVGL", "FramebufferDisplay %ux%u depth %u (%s)", + self->core.width, self->core.height, self->core.colorspace.depth, + self->lv_direct ? "direct" : "partial"); + #endif + + if (rotation != 0) { + common_hal_framebufferio_framebufferdisplay_set_rotation(self, rotation); + } + + // Set the group after initialization otherwise we may send pixels while we delay in + // initialization. + displayio_display_core_set_root_group(&self->core, &circuitpython_splash); + displayio_lvgl_set_root_group(&circuitpython_splash); + nlr_pop_jump_callback(false); + displayio_lvgl_unlock(); + common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, auto_refresh); +} + +uint16_t common_hal_framebufferio_framebufferdisplay_get_width(framebufferio_framebufferdisplay_obj_t *self) { + return displayio_display_core_get_width(&self->core); +} + +uint16_t common_hal_framebufferio_framebufferdisplay_get_height(framebufferio_framebufferdisplay_obj_t *self) { + return displayio_display_core_get_height(&self->core); +} + +mp_float_t common_hal_framebufferio_framebufferdisplay_get_brightness(framebufferio_framebufferdisplay_obj_t *self) { + if (self->framebuffer_protocol->get_brightness) { + return self->framebuffer_protocol->get_brightness(self->framebuffer); + } + return -1; +} + +bool common_hal_framebufferio_framebufferdisplay_set_brightness(framebufferio_framebufferdisplay_obj_t *self, mp_float_t brightness) { + bool ok = false; + if (self->framebuffer_protocol->set_brightness) { + self->framebuffer_protocol->set_brightness(self->framebuffer, brightness); + ok = true; + } + return ok; +} + +mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t *self) { + return self->framebuffer; +} + + +void common_hal_framebufferio_framebufferdisplay_set_rotation(framebufferio_framebufferdisplay_obj_t *self, int rotation) { + rotation %= 360; + // LVGL rotates the opposite way from displayio for the transposed orientations. + lv_display_rotation_t lv_rotation; + switch (rotation) { + case 90: + lv_rotation = LV_DISPLAY_ROTATION_270; + break; + case 270: + lv_rotation = LV_DISPLAY_ROTATION_90; + break; + case 180: + lv_rotation = LV_DISPLAY_ROTATION_180; + break; + default: + lv_rotation = LV_DISPLAY_ROTATION_0; + break; + } + #if CIRCUITPY_VIDEOCORE + // A framebuffer-target VideoBuffer writes into the framebuffer's physical + // memory, so it can't follow LVGL's whole-scene rotation. + if (self->lv_display != NULL && + !displayio_lvgl_video_can_rotate(self->lv_display, lv_rotation)) { + mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer can't stream into a rotated display")); + } + #endif + bool transposed = (self->core.rotation == 90 || self->core.rotation == 270); + bool will_transposed = (rotation == 90 || rotation == 270); + if (transposed != will_transposed) { + int tmp = self->core.width; + self->core.width = self->core.height; + self->core.height = tmp; + } + displayio_display_core_set_rotation(&self->core, rotation); + if (self->lv_display != NULL) { + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + lv_display_set_rotation(self->lv_display, lv_rotation); + if (!framebufferio_lvgl_configure_buffers(self)) { + displayio_lvgl_unlock_with_cleanup(); + mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("LVGL buffer alloc failed")); + } + lv_display_set_default(self->lv_display); + if (self == &displays[0].framebuffer_display) { + supervisor_stop_terminal(); + supervisor_start_terminal(self->core.width, self->core.height); + } + if (self->core.current_group != NULL) { + displayio_lvgl_set_root_group(self->core.current_group); + } + lv_obj_t *screen = lv_screen_active(); + if (screen != NULL) { + lv_obj_invalidate(screen); + } + displayio_lvgl_unlock_with_cleanup(); + } +} + +uint16_t common_hal_framebufferio_framebufferdisplay_get_rotation(framebufferio_framebufferdisplay_obj_t *self) { + return self->core.rotation; +} + + +bool common_hal_framebufferio_framebufferdisplay_refresh(framebufferio_framebufferdisplay_obj_t *self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) { + if (!self->auto_refresh && !self->first_manual_refresh && (target_ms_per_frame != NO_FPS_LIMIT)) { + uint64_t current_time = supervisor_ticks_ms64(); + uint32_t current_ms_since_real_refresh = current_time - self->core.last_refresh; + // Test to see if the real frame time is below our minimum. + if (current_ms_since_real_refresh > maximum_ms_per_real_frame) { + mp_raise_RuntimeError(MP_ERROR_TEXT("Below minimum frame rate")); + } + uint32_t current_ms_since_last_call = current_time - self->last_refresh_call; + self->last_refresh_call = current_time; + // Skip the actual refresh to help catch up. + if (current_ms_since_last_call > target_ms_per_frame) { + return false; + } + uint32_t remaining_time = target_ms_per_frame - (current_ms_since_real_refresh % target_ms_per_frame); + // We're ahead of the game so wait until we align with the frame rate. + while (supervisor_ticks_ms64() - self->last_refresh_call < remaining_time) { + RUN_BACKGROUND_TASKS; + } + } + self->first_manual_refresh = false; + if (self->lv_display != NULL) { + // The renderer refreshes on its own thread too, so this has to exclude it. + displayio_lvgl_lock(); + lv_refr_now(self->lv_display); + displayio_lvgl_unlock(); + } + self->core.last_refresh = supervisor_ticks_ms64(); + return true; +} + +bool common_hal_framebufferio_framebufferdisplay_get_auto_refresh(framebufferio_framebufferdisplay_obj_t *self) { + return self->auto_refresh; +} + +void common_hal_framebufferio_framebufferdisplay_set_auto_refresh(framebufferio_framebufferdisplay_obj_t *self, + bool auto_refresh) { + self->first_manual_refresh = !auto_refresh; + // No supervisor_enable/disable_tick() here: under LVGL that would stop the whole supervisor + // background tick (filesystem, USB, BLE, port work). The port's LVGL pump asks + // displayio_lvgl_auto_refresh() instead, so this flag only decides whether the compositor + // repaints on its own. + self->auto_refresh = auto_refresh; +} + +static void _update_backlight(framebufferio_framebufferdisplay_obj_t *self) { + // TODO(tannewt): Fade the backlight based on it's existing value and a target value. The target + // should account for ambient light when possible. +} + +void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t *self) { + _update_backlight(self); + // The port's LVGL render task owns the pump. Keeping this free of lv_timer_handler() + // guarantees LVGL only renders on its own tick, a frame boundary, or display.refresh(). +} + +void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t *self) { + common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, false); + if (self->framebuffer_protocol->set_frame_callback != NULL) { + self->framebuffer_protocol->set_frame_callback(self->framebuffer, NULL, NULL); + } + // Hold the lock across detaching the tree and deleting the display so the render task can't + // be in the middle of a flush when its buffers go away. + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + self->lv_paced = false; + displayio_lvgl_set_root_group(NULL); + if (self->lv_display != NULL) { + lv_display_delete(self->lv_display); + self->lv_display = NULL; + } + displayio_lvgl_unlock_with_cleanup(); + framebufferio_lvgl_free_partial_buf(self); + if (self->lv_dirty_row_bitmask != NULL) { + port_free(self->lv_dirty_row_bitmask); + self->lv_dirty_row_bitmask = NULL; + self->lv_dirty_row_bitmask_size = 0; + } + release_display_core(&self->core); + self->framebuffer_protocol->deinit(self->framebuffer); + self->base.type = &mp_type_NoneType; +} + +void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t *self) { + gc_collect_ptr(self->framebuffer); + displayio_display_core_collect_ptrs(&self->core); +} + +void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t *self) { + const mp_obj_type_t *fb_type = mp_obj_get_type(self->framebuffer); + if (fb_type != NULL && fb_type != &mp_type_NoneType) { + common_hal_framebufferio_framebufferdisplay_set_auto_refresh(self, true); + displayio_display_core_set_root_group(&self->core, &circuitpython_splash); + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + if (self->lv_display != NULL) { + lv_display_set_default(self->lv_display); + } + displayio_lvgl_set_root_group(&circuitpython_splash); + // Render the terminal now. The REPL starts next, and the display may not get another + // background tick before its first writes; make sure the framebuffer already holds the + // splash rather than whatever the user's code left on screen. + if (self->lv_display != NULL) { + lv_refr_now(self->lv_display); + } + displayio_lvgl_unlock_with_cleanup(); + } else { + release_framebufferdisplay(self); + } +} + +mp_obj_t common_hal_framebufferio_framebufferdisplay_get_root_group(framebufferio_framebufferdisplay_obj_t *self) { + if (self->core.current_group == NULL) { + return mp_const_none; + } + return self->core.current_group; +} + +mp_obj_t common_hal_framebufferio_framebufferdisplay_set_root_group(framebufferio_framebufferdisplay_obj_t *self, displayio_group_t *root_group) { + bool ok = displayio_display_core_set_root_group(&self->core, root_group); + if (!ok) { + mp_raise_ValueError(MP_ERROR_TEXT("Group already used")); + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + lv_display_set_default(self->lv_display); + displayio_lvgl_set_root_group(root_group); + displayio_lvgl_unlock_with_cleanup(); + return mp_const_none; +} diff --git a/shared-module/framebufferio/lvgl/FramebufferDisplay.h b/shared-module/framebufferio/lvgl/FramebufferDisplay.h new file mode 100644 index 00000000000..639b4af317b --- /dev/null +++ b/shared-module/framebufferio/lvgl/FramebufferDisplay.h @@ -0,0 +1,101 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2019 Scott Shawcroft for Adafruit Industries +// SPDX-FileCopyrightText: Copyright (c) 2020 Jeff Epler for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include "shared-module/framebufferio/__init__.h" +#include "py/obj.h" +#include "py/proto.h" + +#include "shared-bindings/displayio/Group.h" + +#include "lvgl.h" + +#include "shared-module/displayio/display_core.h" + +typedef struct { + mp_obj_base_t base; + displayio_display_core_t core; + mp_obj_t framebuffer; + const struct _framebuffer_p_t *framebuffer_protocol; + mp_buffer_info_t bufinfo; + uint64_t last_refresh_call; + uint16_t native_frames_per_second; + uint16_t native_ms_per_frame; + uint16_t first_pixel_offset; + uint16_t row_stride; + bool auto_refresh; + bool first_manual_refresh; + // The LVGL display renders either directly into the provided framebuffer or into + // an aligned partial buffer that the flush callback copies into it. + lv_display_t *lv_display; + lv_color_format_t lv_color_format; + uint8_t *lv_buf; + size_t lv_buf_size; + uint8_t *lv_dirty_row_bitmask; + size_t lv_dirty_row_bitmask_size; + bool lv_direct; + // The framebuffer reports frame boundaries, so this display renders on those instead of the + // LVGL autorefresh tick. + bool lv_paced; +} framebufferio_framebufferdisplay_obj_t; + +void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t *self); +void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t *self); +void framebufferio_framebufferdisplay_reset(framebufferio_framebufferdisplay_obj_t *self); + +void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t *self); + +mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t *self); + +typedef bool (*framebuffer_get_reverse_pixels_in_byte_fun)(mp_obj_t); +typedef bool (*framebuffer_get_reverse_pixels_in_word_fun)(mp_obj_t); +typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t); +typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t); +typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t); +typedef int (*framebuffer_get_first_pixel_offset_fun)(mp_obj_t); +typedef bool (*framebuffer_get_grayscale_fun)(mp_obj_t); +typedef int (*framebuffer_get_height_fun)(mp_obj_t); +typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t); +typedef bool (*framebuffer_get_pixels_in_byte_share_row_fun)(mp_obj_t); +typedef int (*framebuffer_get_row_stride_fun)(mp_obj_t); +typedef int (*framebuffer_get_width_fun)(mp_obj_t); +typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t); +typedef void (*framebuffer_deinit_fun)(mp_obj_t); +typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo); +typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t, uint8_t *dirty_row_bitmask); +typedef void (*framebuffer_set_frame_callback_fun)(mp_obj_t, framebuffer_frame_callback_t, void *); + +typedef struct _framebuffer_p_t { + MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer + + // Mandatory + framebuffer_get_bufinfo_fun get_bufinfo; + framebuffer_swapbuffers_fun swapbuffers; + framebuffer_deinit_fun deinit; + framebuffer_get_width_fun get_width; + framebuffer_get_height_fun get_height; + + // Optional getters + framebuffer_get_bytes_per_cell_fun get_bytes_per_cell; // default: 2 + framebuffer_get_color_depth_fun get_color_depth; // default: 16 + framebuffer_get_first_pixel_offset_fun get_first_pixel_offset; // default: 0 + framebuffer_get_grayscale_fun get_grayscale; // default: grayscale if depth < 8 + framebuffer_get_native_frames_per_second_fun get_native_frames_per_second; // default: 60 + framebuffer_get_pixels_in_byte_share_row_fun get_pixels_in_byte_share_row; // default: false + framebuffer_get_reverse_pixels_in_byte_fun get_reverse_pixels_in_byte; // default: false + framebuffer_get_reverse_pixels_in_word_fun get_reverse_pixels_in_word; // default: false + framebuffer_get_row_stride_fun get_row_stride; // default: 0 (no extra row padding) + + // Optional -- default is no brightness control + framebuffer_get_brightness_fun get_brightness; + framebuffer_set_brightness_fun set_brightness; + + // Optional -- called at each frame boundary. Pass a NULL callback to clear it. + framebuffer_set_frame_callback_fun set_frame_callback; + +} framebuffer_p_t; diff --git a/shared-module/gifio/OnDiskGif.c b/shared-module/gifio/OnDiskGif.c index 1062e146ebd..e974264f9bb 100644 --- a/shared-module/gifio/OnDiskGif.c +++ b/shared-module/gifio/OnDiskGif.c @@ -252,14 +252,7 @@ uint32_t common_hal_gifio_ondiskgif_next_frame(gifio_ondiskgif_t *self, bool set result = GIF_playFrame(&self->gif, &nextDelay, self); if ((result >= 0) && (setDirty)) { - displayio_area_t dirty_area = { - .x1 = 0, - .y1 = 0, - .x2 = self->bitmap->width, - .y2 = self->bitmap->height, - }; - - displayio_bitmap_set_dirty_area(self->bitmap, &dirty_area); + displayio_bitmap_set_dirty(self->bitmap, 0, 0, self->bitmap->width, self->bitmap->height); } return nextDelay; diff --git a/shared-module/hashlib/__init__.c b/shared-module/hashlib/__init__.c index 50477a4dc2d..11d5395bf91 100644 --- a/shared-module/hashlib/__init__.c +++ b/shared-module/hashlib/__init__.c @@ -10,7 +10,9 @@ #include "psa/crypto.h" bool common_hal_hashlib_new(hashlib_hash_obj_t *self, const char *algorithm) { - if (strcmp(algorithm, "sha1") == 0) { + if (strcmp(algorithm, "md5") == 0) { + self->hash_alg = PSA_ALG_MD5; + } else if (strcmp(algorithm, "sha1") == 0) { self->hash_alg = PSA_ALG_SHA_1; } else if (strcmp(algorithm, "sha256") == 0) { self->hash_alg = PSA_ALG_SHA_256; diff --git a/shared-module/picogame/__init__.c b/shared-module/picogame/__init__.c index 8988d8b8a9c..680f7572e7e 100644 --- a/shared-module/picogame/__init__.c +++ b/shared-module/picogame/__init__.c @@ -1021,7 +1021,7 @@ bool picogame_strip_begin( area.x2 = x1; area.y2 = y1; area.next = NULL; - displayio_display_bus_set_region_to_update(&display->bus, &display->core, &area); + displayio_display_bus_set_region_to_update(&display->bus, &display->core.colorspace, &area); while (!displayio_display_bus_begin_transaction(&display->bus)) { RUN_BACKGROUND_TASKS; diff --git a/shared-module/usb_video/USBFramebuffer.c b/shared-module/usb_video/USBFramebuffer.c index 6cbe137191c..e0bf016e125 100644 --- a/shared-module/usb_video/USBFramebuffer.c +++ b/shared-module/usb_video/USBFramebuffer.c @@ -22,6 +22,21 @@ void shared_module_usb_video_uvcframebuffer_refresh(usb_video_uvcframebuffer_obj shared_module_usb_video_swapbuffers(); } +void shared_module_usb_video_uvcframebuffer_swapbuffers(usb_video_uvcframebuffer_obj_t *self, const uint8_t *dirty_row_bitmap) { + shared_module_usb_video_swapbuffers_dirty(dirty_row_bitmap); +} + +void shared_module_usb_video_uvcframebuffer_set_frame_callback(usb_video_uvcframebuffer_obj_t *self, framebuffer_frame_callback_t cb, void *data) { + self->frame_cb = cb; + self->frame_cb_data = data; +} + +void shared_module_usb_video_uvcframebuffer_frame_boundary(usb_video_uvcframebuffer_obj_t *self) { + if (self->frame_cb != NULL) { + self->frame_cb(self->frame_cb_data); + } +} + int shared_module_usb_video_uvcframebuffer_get_width(usb_video_uvcframebuffer_obj_t *self) { return usb_video_frame_width; } diff --git a/shared-module/usb_video/USBFramebuffer.h b/shared-module/usb_video/USBFramebuffer.h index 0db85ac378c..398ea2b4710 100644 --- a/shared-module/usb_video/USBFramebuffer.h +++ b/shared-module/usb_video/USBFramebuffer.h @@ -8,6 +8,10 @@ #include "py/obj.h" +#include "shared-module/framebufferio/__init__.h" + typedef struct usb_video_uvcframebuffer_obj { mp_obj_base_t base; + framebuffer_frame_callback_t frame_cb; + void *frame_cb_data; } usb_video_uvcframebuffer_obj_t; diff --git a/shared-module/usb_video/__init__.c b/shared-module/usb_video/__init__.c index 2a37ab482ba..e7e1bd574a7 100644 --- a/shared-module/usb_video/__init__.c +++ b/shared-module/usb_video/__init__.c @@ -9,9 +9,13 @@ #include "class/video/video_device.h" #include "shared-bindings/displayio/Bitmap.h" #include "shared-bindings/usb_video/__init__.h" +#include "shared-bindings/usb_video/USBFramebuffer.h" #include "shared-module/bitmapfilter/macros.h" #include "shared-module/usb_video/__init__.h" #include "shared-module/usb_video/uvc_usb_descriptors.h" +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#endif #include "supervisor/background_callback.h" #include "supervisor/shared/tick.h" #include "device/usbd.h" @@ -29,14 +33,22 @@ static unsigned frame_num = 0; static unsigned tx_busy = 0; static unsigned interval_ms = 1000 / DEFAULT_FRAME_RATE; +// Rows of the RGB565 framebuffer that changed since the last UVC frame was converted. The +// display's flush tells us which rows it wrote, so only those rows need the RGB565 -> YUYV +// conversion; the rest of the YUYV buffer still holds the previous frame. +static uint8_t *dirty_rows; +static size_t dirty_rows_size; +static bool convert_all = true; + // TODO must dynamically allocate this, otherwise everyone pays for it static uint8_t *frame_buffer_yuyv; uint16_t *usb_video_framebuffer_rgb565; static bool usb_video_is_enabled = false; uint16_t usb_video_frame_width, usb_video_frame_height; +static mp_int_t usb_video_frame_rate = DEFAULT_FRAME_RATE; -bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height) { +bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height, mp_int_t frame_rate) { if (tud_connected()) { return false; } @@ -51,6 +63,8 @@ bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height) usb_video_frame_width = frame_width; usb_video_frame_height = frame_height; + usb_video_frame_rate = frame_rate; + interval_ms = 1000 / frame_rate; size_t framebuffer_size = usb_video_frame_width * usb_video_frame_height * 2; frame_buffer_yuyv = port_malloc(framebuffer_size, false); @@ -66,6 +80,14 @@ bool shared_module_usb_video_enable(mp_int_t frame_width, mp_int_t frame_height) memset(frame_buffer_yuyv, 0, framebuffer_size); memset(usb_video_framebuffer_rgb565, 0, framebuffer_size); + dirty_rows_size = (usb_video_frame_height + 7) / 8; + dirty_rows = port_malloc(dirty_rows_size, false); + if (dirty_rows != NULL) { + memset(dirty_rows, 0, dirty_rows_size); + } + convert_all = true; + do_convert = true; + usb_video_is_enabled = true; return true; @@ -78,8 +100,12 @@ bool shared_module_usb_video_disable(void) { usb_video_is_enabled = false; port_free(frame_buffer_yuyv); port_free(usb_video_framebuffer_rgb565); + port_free(dirty_rows); frame_buffer_yuyv = NULL; usb_video_framebuffer_rgb565 = NULL; + dirty_rows = NULL; + dirty_rows_size = 0; + convert_all = true; return true; } @@ -89,22 +115,17 @@ bool usb_video_enabled(void) { size_t usb_video_descriptor_length(void) { #if CFG_TUD_VIDEO_STREAMING_BULK - return sizeof((char[]) {TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR_BULK(0, 0, DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT, DEFAULT_FRAME_RATE, USB_VIDEO_BULK_PACKET_SIZE, 0, 0)}); + return sizeof((char[]) {TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR_BULK(0, 0, DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT, usb_video_frame_rate, USB_VIDEO_BULK_PACKET_SIZE, 0, 0)}); #else - return sizeof((char[]) {TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR(0, 0, DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT, DEFAULT_FRAME_RATE, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE, 0, 0)}); + return sizeof((char[]) {TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR(0, 0, DEFAULT_FRAME_WIDTH, DEFAULT_FRAME_HEIGHT, usb_video_frame_rate, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE, 0, 0)}); #endif } -static void convert_framebuffer_maybe(void) { - if (!do_convert) { - return; // new data not ready yet - } - do_convert = false; // assumes this happens via background, not interrupt - - uint8_t *dest = frame_buffer_yuyv; - uint16_t *src = usb_video_framebuffer_rgb565; +static void convert_row(uint16_t y) { + uint8_t *dest = frame_buffer_yuyv + (size_t)y * usb_video_frame_width * 2; + uint16_t *src = usb_video_framebuffer_rgb565 + (size_t)y * usb_video_frame_width; - for (int i = 0; i < usb_video_frame_width * usb_video_frame_height / 2; i++) { + for (int x = 0; x < usb_video_frame_width; x += 2) { uint16_t p1 = IMAGE_GET_RGB565_PIXEL_FAST(src, 0); uint16_t p2 = IMAGE_GET_RGB565_PIXEL_FAST(src, 1); src += 2; @@ -124,7 +145,68 @@ static void convert_framebuffer_maybe(void) { } } +static void convert_framebuffer_maybe(void) { + if (!do_convert) { + return; // new data not ready yet + } + do_convert = false; // assumes this happens via background, not interrupt + + if (convert_all || dirty_rows == NULL) { + for (uint16_t y = 0; y < usb_video_frame_height; y++) { + convert_row(y); + } + convert_all = false; + if (dirty_rows != NULL) { + memset(dirty_rows, 0, dirty_rows_size); + } + return; + } + + for (uint16_t y = 0; y < usb_video_frame_height; y++) { + uint8_t bit = 1 << (y & 7); + if (dirty_rows[y >> 3] & bit) { + convert_row(y); + dirty_rows[y >> 3] &= ~bit; + } + } +} + +// Under LVGL the framebuffer can be flushed by the port's render task while the UVC background +// callback converts it. Both paths take the displayio LVGL lock, so the dirty rows this reads are +// not being changed and the pixels it copies are not being rewritten. Without LVGL every step is +// on the CircuitPython thread already. +static void convert_framebuffer_locked(void) { + #if CIRCUITPY_DISPLAYIO_LVGL + displayio_lvgl_lock(); + convert_framebuffer_maybe(); + displayio_lvgl_unlock(); + #else + convert_framebuffer_maybe(); + #endif +} + +// The frame boundary is the UVC frame rate. A FramebufferDisplay pacing itself off this callback +// renders the latest display state before the rows are converted, so the frame on the wire +// matches the framebuffer. +static void usb_video_frame_boundary(void) { + shared_module_usb_video_uvcframebuffer_frame_boundary(&usb_video_uvcframebuffer_singleton_obj); +} + void shared_module_usb_video_swapbuffers(void) { + convert_all = true; + do_convert = true; +} + +void shared_module_usb_video_swapbuffers_dirty(const uint8_t *dirty_row_bitmap) { + // Under LVGL this is called from the display flush path, which holds the displayio LVGL lock + // against convert_framebuffer_locked(). + if (dirty_row_bitmap == NULL || dirty_rows == NULL) { + convert_all = true; + } else { + for (size_t i = 0; i < dirty_rows_size; i++) { + dirty_rows[i] |= dirty_row_bitmap[i]; + } + } do_convert = true; } @@ -132,9 +214,9 @@ size_t usb_video_add_descriptor(uint8_t *descriptor_buf, descriptor_counts_t *de usb_add_interface_name(*current_interface_string, NULL, "UVC"); const uint8_t usb_video_descriptor[] = { #if CFG_TUD_VIDEO_STREAMING_BULK - TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR_BULK(*current_interface_string, descriptor_counts->current_endpoint | 0x80, usb_video_frame_width, usb_video_frame_height, DEFAULT_FRAME_RATE, USB_VIDEO_BULK_PACKET_SIZE, descriptor_counts->current_interface, descriptor_counts->current_interface + 1) + TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR_BULK(*current_interface_string, descriptor_counts->current_endpoint | 0x80, usb_video_frame_width, usb_video_frame_height, usb_video_frame_rate, USB_VIDEO_BULK_PACKET_SIZE, descriptor_counts->current_interface, descriptor_counts->current_interface + 1) #else - TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR(*current_interface_string, descriptor_counts->current_endpoint | 0x80, usb_video_frame_width, usb_video_frame_height, DEFAULT_FRAME_RATE, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE, descriptor_counts->current_interface, descriptor_counts->current_interface + 1) + TUD_VIDEO_CAPTURE_DESCRIPTOR_UNCOMPR(*current_interface_string, descriptor_counts->current_endpoint | 0x80, usb_video_frame_width, usb_video_frame_height, usb_video_frame_rate, CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE, descriptor_counts->current_interface, descriptor_counts->current_interface + 1) #endif }; (*current_interface_string)++; @@ -165,9 +247,12 @@ static void usb_video_cb_fun(void *unused) { if (!already_sent) { already_sent = 1; start_ms = supervisor_ticks_ms32(); - convert_framebuffer_maybe(); + usb_video_frame_boundary(); + convert_framebuffer_locked(); bool result = tud_video_n_frame_xfer(0, 0, (void *)frame_buffer_yuyv, usb_video_frame_width * usb_video_frame_height * 16 / 8); - (void)result; + if (result) { + tx_busy = 1; + } } unsigned cur = supervisor_ticks_ms32(); @@ -179,11 +264,17 @@ static void usb_video_cb_fun(void *unused) { background_callback_add(&usb_video_cb, usb_video_cb_fun, NULL); // re-queue return; } - start_ms += interval_ms; - - convert_framebuffer_maybe(); + usb_video_frame_boundary(); + convert_framebuffer_locked(); bool result = tud_video_n_frame_xfer(0, 0, (void *)frame_buffer_yuyv, usb_video_frame_width * usb_video_frame_height * 16 / 8); - (void)result; + if (result) { + tx_busy = 1; + start_ms += interval_ms; + } else { + // No completion callback will arrive when the transfer wasn't queued, so keep the + // background callback alive and try again. + background_callback_add(&usb_video_cb, usb_video_cb_fun, NULL); + } } diff --git a/shared-module/usb_video/uvc_usb_descriptors.h b/shared-module/usb_video/uvc_usb_descriptors.h index ad9e5470cdc..0f4c04d810b 100644 --- a/shared-module/usb_video/uvc_usb_descriptors.h +++ b/shared-module/usb_video/uvc_usb_descriptors.h @@ -17,7 +17,7 @@ #define DEFAULT_FRAME_WIDTH 128 #define DEFAULT_FRAME_HEIGHT 96 -#define DEFAULT_FRAME_RATE 10 +#define DEFAULT_FRAME_RATE 15 #define TUD_VIDEO_CAPTURE_DESC_UNCOMPR_LEN ( \ TUD_VIDEO_DESC_IAD_LEN \ diff --git a/shared-module/vectorio/VectorShape.h b/shared-module/vectorio/VectorShape.h index f66569f97d4..70840ec7c8e 100644 --- a/shared-module/vectorio/VectorShape.h +++ b/shared-module/vectorio/VectorShape.h @@ -4,56 +4,54 @@ // // SPDX-License-Identifier: MIT - #pragma once #include #include #include "py/obj.h" + +// Everything a vectorio shape has to provide to the compositor, independent of backend: +// +// get_pixel(x, y) - the shape's coverage test, in the shape's own coordinate space where +// (0, 0) is the shape's origin. Returns 0 for "not covered" and the shape's color +// index biased by one otherwise. (The bias keeps 0, which means "not covered", from +// ever naming a palette entry.) +// +// Everything else is backend specific: +// +// legacy/ - the scanline compositor. Shapes report a bounding area, the compositor fills +// screen spans through get_pixel(), and shapes track dirty areas so the display core +// knows what to repaint. +// lvgl/ - the LVGL compositor. Shapes are mirrored into lv_obj widgets that LVGL draws +// and invalidates itself, so there is no area and no span filling at all. +typedef uint32_t get_pixel_function(mp_obj_t shape, int16_t x, int16_t y); + +#if !CIRCUITPY_DISPLAYIO_LVGL #include "shared-module/displayio/area.h" -#include "shared-module/displayio/Palette.h" typedef void get_area_function(mp_obj_t shape, displayio_area_t *out_area); -typedef uint32_t get_pixel_function(mp_obj_t shape, int16_t x, int16_t y); +#endif -// This struct binds a shape's common Shape support functions (its vector shape interface) -// to its instance pointer. We only check at construction time what the type of the -// associated shape is and link the correct functions up. -// Later when using the shape for drawing logic these functions may be invoked -// unconditionally. This simplifies the addition of new types and restricts the -// respective responsibilities of VectorShape and actual shape implementations. +// This struct binds a shape's support functions to its instance pointer. A shape's type is +// only inspected when its vector shape is constructed; after that the compositor can call +// these unconditionally. That keeps the shape implementations and the vector shape +// wrapper responsible for their own pieces. typedef struct { mp_obj_t shape; + #if !CIRCUITPY_DISPLAYIO_LVGL get_area_function *get_area; + #endif get_pixel_function *get_pixel; } vectorio_ishape_t; -typedef struct { - mp_obj_base_t base; - vectorio_ishape_t ishape; - mp_obj_t pixel_shader; - int16_t x; - int16_t y; - displayio_buffer_transform_t *absolute_transform; - // Tracks current shape footprint and expands outward as the shape dirties and changes. - // This is suboptimal if you move your shape far. Could add more state to only redraw - // exactly what we left behind. - displayio_area_t ephemeral_dirty_area; - displayio_area_t current_area; - bool current_area_dirty; - bool hidden; - bool in_group; -} vectorio_vector_shape_t; - -displayio_area_t *vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t *tail); - -bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *current_dirty_area); - -// Area is always in absolute screen coordinates. -bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); - -// Fills in out_area with the maximum bounds of all related pixels in the last rendered frame. Returns -// false if the vector shape wasn't rendered in the last frame. -bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area); -void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self); +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/vectorio/lvgl/VectorShape.h" +// The protocol slot an LVGL-backed shape exposes to its Group. Legacy shapes are found by +// type through protocol_draw, so the slot stays empty there. See +// displayio/lvgl/lvgl_protocol.h. +#define VECTORIO_VECTOR_SHAPE_LVGL_PROTOCOL &displayio_lvgl_vector_shape_protocol +#else +#include "shared-module/vectorio/legacy/VectorShape.h" +#define VECTORIO_VECTOR_SHAPE_LVGL_PROTOCOL NULL +#endif diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/legacy/VectorShape.c similarity index 99% rename from shared-module/vectorio/VectorShape.c rename to shared-module/vectorio/legacy/VectorShape.c index 0d8da20edc1..0ae48eb1054 100644 --- a/shared-module/vectorio/VectorShape.c +++ b/shared-module/vectorio/legacy/VectorShape.c @@ -4,6 +4,8 @@ // // SPDX-License-Identifier: MIT +#if CIRCUITPY_VECTORIO + #include "stdlib.h" #include "shared-module/vectorio/__init__.h" @@ -565,3 +567,5 @@ bool vectorio_vector_shape_set_in_group(vectorio_vector_shape_t *self, bool in_g self->in_group = in_group; return was_in_group; } + +#endif // CIRCUITPY_VECTORIO diff --git a/shared-module/vectorio/legacy/VectorShape.h b/shared-module/vectorio/legacy/VectorShape.h new file mode 100644 index 00000000000..d45cdbe33a6 --- /dev/null +++ b/shared-module/vectorio/legacy/VectorShape.h @@ -0,0 +1,50 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2020 by kvc0/WarriorOfWire +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" +#include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" + +// Legacy scanline compositor state for a vector shape. +// +// The shape's footprint is tracked in screen space twice: current_area is where the shape +// is now and ephemeral_dirty_area is the accumulation of where it has been since the last +// refresh. get_refresh_areas() hands both to the display core so it can repaint exactly the +// pixels that changed. absolute_transform mirrors the parent groups' transforms so that +// screen coordinates can be mapped back into the shape's own coordinate space. +typedef struct { + mp_obj_base_t base; + vectorio_ishape_t ishape; + mp_obj_t pixel_shader; + int16_t x; + int16_t y; + displayio_buffer_transform_t *absolute_transform; + // Tracks current shape footprint and expands outward as the shape dirties and changes. + // This is suboptimal if you move your shape far. Could add more state to only redraw + // exactly what we left behind. + displayio_area_t ephemeral_dirty_area; + displayio_area_t current_area; + bool current_area_dirty; + bool hidden; + bool in_group; +} vectorio_vector_shape_t; + +displayio_area_t *vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t *tail); + +bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *current_dirty_area); + +// Area is always in absolute screen coordinates. +bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer); + +// Fills in out_area with the maximum bounds of all related pixels in the last rendered frame. Returns +// false if the vector shape wasn't rendered in the last frame. +bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area); +void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self); diff --git a/shared-module/vectorio/lvgl/VectorShape.c b/shared-module/vectorio/lvgl/VectorShape.c new file mode 100644 index 00000000000..10fc13dbb85 --- /dev/null +++ b/shared-module/vectorio/lvgl/VectorShape.c @@ -0,0 +1,511 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// LVGL implementation of the vectorio VectorShape wrapper. See lvgl/VectorShape.h for the +// design. In short: a shape is one lv_obj, LVGL does all drawing and invalidation, and none +// of the scanline compositor's area/mask/dirty-area machinery exists here. + +#include "shared-bindings/vectorio/VectorShape.h" + +#include + +#include "py/runtime.h" + +#include "shared-bindings/displayio/ColorConverter.h" +#include "shared-bindings/displayio/Palette.h" +#include "shared-bindings/vectorio/Circle.h" +#include "shared-bindings/vectorio/Polygon.h" +#include "shared-bindings/vectorio/Rectangle.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "shared-module/vectorio/__init__.h" + +#if CIRCUITPY_VECTORIO && CIRCUITPY_DISPLAYIO_LVGL + +#define COVERAGE_OPAQUE 0xff + +static bool _is_polygon(vectorio_vector_shape_t *self) { + return mp_obj_is_type(self->ishape.shape, &vectorio_polygon_type); +} + +// The shape's color_index is biased by one so that 0 can always mean "not covered". +static uint16_t _biased_color_index(vectorio_vector_shape_t *self) { + if (mp_obj_is_type(self->ishape.shape, &vectorio_rectangle_type)) { + return ((vectorio_rectangle_t *)MP_OBJ_TO_PTR(self->ishape.shape))->color_index; + } + if (mp_obj_is_type(self->ishape.shape, &vectorio_circle_type)) { + return ((vectorio_circle_t *)MP_OBJ_TO_PTR(self->ishape.shape))->color_index; + } + return ((vectorio_polygon_t *)MP_OBJ_TO_PTR(self->ishape.shape))->color_index; +} + +// Resolve the pixel shader to the color to paint. Returns false when nothing should be +// drawn at all, which is what the "not covered" color index, a transparent palette entry +// and a ColorConverter's transparent color all mean. +static bool _resolve_color(mp_obj_t pixel_shader, uint16_t biased_index, lv_color_t *color) { + if (biased_index == 0 || pixel_shader == mp_const_none) { + return false; + } + uint32_t index = (uint32_t)biased_index - 1; + if (mp_obj_is_type(pixel_shader, &displayio_palette_type)) { + displayio_palette_t *palette = MP_OBJ_TO_PTR(pixel_shader); + if (index >= common_hal_displayio_palette_get_len(palette) || + common_hal_displayio_palette_is_transparent(palette, index)) { + return false; + } + *color = lv_color_hex(common_hal_displayio_palette_get_color(palette, index)); + return true; + } + displayio_colorconverter_t *converter = MP_OBJ_TO_PTR(pixel_shader); + if (index == converter->transparent_color) { + return false; + } + // A ColorConverter maps a pixel in its input colorspace onto the display's output + // colorspace. LVGL does the output conversion itself while blending, so all that is + // needed here is the rgb888 value of the input pixel. + *color = lv_color_hex(displayio_colorconverter_convert_pixel( + (displayio_colorspace_t)converter->input_colorspace, index)); + return true; +} + +// The shape's extent in its own coordinate space. Rectangles start at the origin, circles +// are centered on it, and polygons use the bounds of their points. This is the same +// geometry the legacy compositor gets from get_area(), just computed where it is needed +// instead of being part of the shape's contract. +// +// The bounds are inclusive of both ends, matching the pixel range the legacy compositor +// asks get_pixel() about: the rectangle covers [0, width) x [0, height), the circle covers +// [-radius, radius] on both axes and the polygon covers the bounding box of its points. +static void _shape_bounds(vectorio_vector_shape_t *self, int32_t *x1, int32_t *y1, int32_t *x2, int32_t *y2) { + if (mp_obj_is_type(self->ishape.shape, &vectorio_rectangle_type)) { + vectorio_rectangle_t *rectangle = MP_OBJ_TO_PTR(self->ishape.shape); + *x1 = 0; + *y1 = 0; + *x2 = rectangle->width - 1; + *y2 = rectangle->height - 1; + return; + } + if (mp_obj_is_type(self->ishape.shape, &vectorio_circle_type)) { + vectorio_circle_t *circle = MP_OBJ_TO_PTR(self->ishape.shape); + *x1 = -(int32_t)circle->radius; + *y1 = -(int32_t)circle->radius; + *x2 = (int32_t)circle->radius; + *y2 = (int32_t)circle->radius; + return; + } + vectorio_polygon_t *polygon = MP_OBJ_TO_PTR(self->ishape.shape); + if (polygon->points_list == NULL || polygon->len < 2) { + *x1 = 0; + *y1 = 0; + *x2 = 0; + *y2 = 0; + return; + } + int32_t min_x = polygon->points_list[0]; + int32_t max_x = min_x; + int32_t min_y = polygon->points_list[1]; + int32_t max_y = min_y; + for (uint16_t i = 2; i + 1 < polygon->len; i += 2) { + int32_t x = polygon->points_list[i]; + int32_t y = polygon->points_list[i + 1]; + if (x < min_x) { + min_x = x; + } + if (x > max_x) { + max_x = x; + } + if (y < min_y) { + min_y = y; + } + if (y > max_y) { + max_y = y; + } + } + *x1 = min_x; + *y1 = min_y; + *x2 = max_x; + *y2 = max_y; +} + +static void _update_visible(vectorio_vector_shape_t *self) { + if (self->lv_obj == NULL) { + return; + } + if (self->hidden || self->hidden_by_parent) { + lv_obj_add_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } else { + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_HIDDEN); + } +} + +// 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. +static void _refresh_mask(vectorio_vector_shape_t *self, int32_t x1, int32_t y1, int32_t w, + int32_t h, uint16_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. + lv_image_set_src(self->lv_obj, NULL); + if (self->mask != NULL) { + lv_draw_buf_destroy(self->mask); + self->mask = NULL; + } + self->mask = lv_draw_buf_create(w, h, LV_COLOR_FORMAT_A8, 0); + if (self->mask == NULL) { + return; + } + // Hand the new buffer to the image. The bin decoder wraps a variable A8 image + // instead of copying it, so rewriting the pixels below is visible without dropping + // any cache entry. + lv_image_set_src(self->lv_obj, self->mask); + } + 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; + 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; + } + } +} + +// lv_obj_set_pos() only records a style that a later layout pass applies, and lv_obj_move_to() +// leaves that style behind, so an unrelated size change would snap the object back. Set both: the +// coordinates are right immediately and nothing can undo them. +static void _place(lv_obj_t *obj, int32_t x, int32_t y) { + lv_obj_set_style_x(obj, x, 0); + lv_obj_set_style_y(obj, y, 0); + lv_obj_move_to(obj, x, y); +} + +// Push every Python-visible property of the shape into its mirror. Called under the LVGL +// lock from everything that can change the shape. `rebuild_mask` is false for a pure move, +// which is the one change that can't affect the polygon's coverage. +static void _sync_mirror(vectorio_vector_shape_t *self, bool rebuild_mask) { + if (self->lv_obj == NULL) { + return; + } + int32_t x1; + int32_t y1; + 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; + if (w < 1) { + w = 1; + } + if (h < 1) { + h = 1; + } + + 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); + 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. + rebuild_mask = true; + } + if (polygon && rebuild_mask) { + _refresh_mask(self, x1, y1, w, h, scale); + } else if (!polygon) { + bool circle = mp_obj_is_type(self->ishape.shape, &vectorio_circle_type); + lv_obj_set_style_radius(self->lv_obj, circle ? LV_RADIUS_CIRCLE : 0, 0); + } + + lv_color_t color; + bool painted = !empty && _resolve_color(self->pixel_shader, _biased_color_index(self), &color); + if (polygon) { + if (painted) { + lv_obj_set_style_image_recolor(self->lv_obj, color, 0); + } + lv_obj_set_style_image_recolor_opa(self->lv_obj, painted ? LV_OPA_COVER : LV_OPA_TRANSP, 0); + } else { + if (painted) { + lv_obj_set_style_bg_color(self->lv_obj, color, 0); + } + lv_obj_set_style_bg_opa(self->lv_obj, painted ? LV_OPA_COVER : LV_OPA_TRANSP, 0); + } +} + +// A move changes nothing but where the shape is, so the polygon mask survives it. +static void _set_dirty(vectorio_vector_shape_t *self, bool rebuild_mask) { + if (self->lv_obj == NULL) { + return; + } + displayio_lvgl_lock(); + // The shape's old area has to be redrawn too, and batching below would drop that + // invalidation once the move has already happened. + lv_obj_invalidate(self->lv_obj); + // Batch: the sync writes several styles and only the final invalidation matters. + lv_display_t *display = lv_obj_get_display(self->lv_obj); + if (display != NULL) { + lv_display_enable_invalidation(display, false); + } + _sync_mirror(self, rebuild_mask); + if (display != NULL) { + lv_display_enable_invalidation(display, true); + } + lv_obj_invalidate(self->lv_obj); + displayio_lvgl_unlock(); +} + +void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self, + vectorio_ishape_t ishape, mp_obj_t pixel_shader, int32_t x, int32_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); + self->x = x; + self->y = y; + self->pixel_shader = pixel_shader; + self->ishape = ishape; + self->abs_x = 0; + self->abs_y = 0; + self->abs_scale = 1; + self->lv_obj = NULL; + self->mask = NULL; + self->hidden = false; + self->hidden_by_parent = false; + self->in_group = false; +} + +// This must be invoked after each time a shape changes its position, shape or appearance +// in any way. +void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) { + // The shape itself changed (points, size, color index), so its coverage has to be redone. + _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; + if (relative < 0) { + // Floor division so that negative coordinates map to the pixel that contains them. + return -((-relative + scale - 1) / scale); + } + return relative / 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); + return self->ishape.get_pixel(self->ishape.shape, shape_x, shape_y) != 0; +} + +mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) { + return self->x; +} + +void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) { + if (self->x == x) { + return; + } + self->x = mp_arg_validate_int_range(x, SHRT_MIN, SHRT_MAX, MP_QSTR_x); + _set_dirty(self, false); +} + +mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) { + return self->y; +} + +void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) { + if (self->y == y) { + return; + } + self->y = mp_arg_validate_int_range(y, SHRT_MIN, SHRT_MAX, MP_QSTR_y); + _set_dirty(self, false); +} + +mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) { + mp_obj_tuple_t *pair = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL)); + pair->items[0] = mp_obj_new_int((mp_int_t)self->x); + pair->items[1] = mp_obj_new_int((mp_int_t)self->y); + return pair; +} + +void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy) { + size_t tuple_len = 0; + mp_obj_t *tuple_items; + mp_obj_tuple_get(xy, &tuple_len, &tuple_items); + mp_arg_validate_length(tuple_len, 2, MP_QSTR_location); + + mp_int_t x = mp_arg_validate_type_int(tuple_items[0], MP_QSTR_x); + mp_int_t y = mp_arg_validate_type_int(tuple_items[1], MP_QSTR_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); + if (self->x == x && self->y == y) { + return; + } + self->x = x; + self->y = y; + _set_dirty(self, false); +} + +mp_int_t common_hal_vectorio_vector_shape_get_hidden(vectorio_vector_shape_t *self) { + return self->hidden; +} + +void common_hal_vectorio_vector_shape_set_hidden(vectorio_vector_shape_t *self, bool hidden) { + self->hidden = hidden; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _update_visible(self); + displayio_lvgl_unlock(); + } + _set_dirty(self, false); +} + +mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) { + return self->pixel_shader; +} + +void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader) { + if (self->pixel_shader != pixel_shader) { + displayio_lvgl_shader_unregister_user(self->pixel_shader, self); + self->pixel_shader = pixel_shader; + if (self->lv_obj != NULL) { + displayio_lvgl_shader_register_user(pixel_shader, + self, DISPLAYIO_LVGL_USER_VECTOR_SHAPE); + } + } + // A different shader is a different color, not different coverage. + _set_dirty(self, false); +} + +bool vectorio_vector_shape_set_in_group(vectorio_vector_shape_t *self, bool in_group) { + bool was_in_group = self->in_group; + self->in_group = in_group; + return was_in_group; +} + +void displayio_lvgl_vector_shape_attach(vectorio_vector_shape_t *self, lv_obj_t *parent) { + if (parent == NULL || !self->in_group) { + return; + } + displayio_lvgl_lock(); + if (self->lv_obj == NULL) { + bool polygon = _is_polygon(self); + self->lv_obj = polygon ? lv_image_create(parent) : lv_obj_create(parent); + if (self->lv_obj == NULL) { + displayio_lvgl_unlock(); + return; + } + // Take the theme out of the picture so the shape is exactly what we style it to be. + lv_obj_remove_style_all(self->lv_obj); + lv_obj_set_style_pad_all(self->lv_obj, 0, 0); + lv_obj_set_style_border_width(self->lv_obj, 0, 0); + lv_obj_set_style_radius(self->lv_obj, 0, 0); + lv_obj_set_style_bg_opa(self->lv_obj, LV_OPA_TRANSP, 0); + lv_obj_set_style_opa(self->lv_obj, LV_OPA_COVER, 0); + if (polygon) { + lv_obj_set_style_image_opa(self->lv_obj, LV_OPA_COVER, 0); + } + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_CLICKABLE); + lv_obj_clear_flag(self->lv_obj, LV_OBJ_FLAG_SCROLLABLE); + _update_visible(self); + _sync_mirror(self, true); + } else if (lv_obj_get_parent(self->lv_obj) != parent) { + lv_obj_set_parent(self->lv_obj, parent); + } + displayio_lvgl_shader_register_user(self->pixel_shader, + self, DISPLAYIO_LVGL_USER_VECTOR_SHAPE); + displayio_lvgl_unlock(); +} + +void displayio_lvgl_vector_shape_detach(vectorio_vector_shape_t *self) { + displayio_lvgl_shader_unregister_user(self->pixel_shader, self); + if (self->lv_obj == NULL) { + return; + } + displayio_lvgl_lock(); + lv_obj_delete(self->lv_obj); + self->lv_obj = NULL; + if (self->mask != NULL) { + lv_draw_buf_destroy(self->mask); + self->mask = NULL; + } + displayio_lvgl_unlock(); +} + +void displayio_lvgl_vector_shape_set_transform(vectorio_vector_shape_t *self, int32_t abs_x, + int32_t abs_y, uint16_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; + // The mask is rasterized at the scaled resolution, so a scale change rebuilds it. + _set_dirty(self, self->abs_scale != previous_scale); +} + +void displayio_lvgl_vector_shape_set_hidden_by_parent(vectorio_vector_shape_t *self, bool hidden) { + self->hidden_by_parent = hidden; + if (self->lv_obj != NULL) { + displayio_lvgl_lock(); + _update_visible(self); + lv_obj_invalidate(self->lv_obj); + displayio_lvgl_unlock(); + } +} + +void displayio_lvgl_vector_shape_shader_changed(vectorio_vector_shape_t *self) { + // Called with the LVGL lock held by the shader's notify path. + _sync_mirror(self, false); +} + +// protocol_lvgl implementation. Group owns the tree; these only translate its calls into the +// shape mirror operations above. +static lv_obj_t *_vector_shape_protocol_root_obj(mp_obj_t layer) { + return ((vectorio_vector_shape_t *)MP_OBJ_TO_PTR(layer))->lv_obj; +} + +static void _vector_shape_protocol_attach(mp_obj_t layer, lv_obj_t *parent) { + displayio_lvgl_vector_shape_attach(MP_OBJ_TO_PTR(layer), parent); +} + +static void _vector_shape_protocol_detach(mp_obj_t layer) { + displayio_lvgl_vector_shape_detach(MP_OBJ_TO_PTR(layer)); +} + +static bool _vector_shape_protocol_set_transform(mp_obj_t layer, int32_t abs_x, + int32_t abs_y, uint16_t scale) { + displayio_lvgl_vector_shape_set_transform(MP_OBJ_TO_PTR(layer), abs_x, abs_y, scale); + return true; +} + +static void _vector_shape_protocol_set_hidden_by_parent(mp_obj_t layer, bool hidden) { + displayio_lvgl_vector_shape_set_hidden_by_parent(MP_OBJ_TO_PTR(layer), hidden); +} + +static bool _vector_shape_protocol_set_in_group(mp_obj_t layer, bool in_group) { + return vectorio_vector_shape_set_in_group(MP_OBJ_TO_PTR(layer), in_group); +} + +const lvgl_protocol_t displayio_lvgl_vector_shape_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .root_obj = _vector_shape_protocol_root_obj, + .attach = _vector_shape_protocol_attach, + .detach = _vector_shape_protocol_detach, + .set_transform = _vector_shape_protocol_set_transform, + .set_hidden_by_parent = _vector_shape_protocol_set_hidden_by_parent, + .set_in_group = _vector_shape_protocol_set_in_group, +}; + +#endif // CIRCUITPY_VECTORIO && CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/vectorio/lvgl/VectorShape.h b/shared-module/vectorio/lvgl/VectorShape.h new file mode 100644 index 00000000000..55050cabfeb --- /dev/null +++ b/shared-module/vectorio/lvgl/VectorShape.h @@ -0,0 +1,68 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" + +#include "shared-module/displayio/lvgl/lvgl_protocol.h" + +#include "lvgl.h" + +// LVGL compositor state for a vector shape. +// +// Each shape is mirrored into exactly one lv_obj, so LVGL owns drawing, clipping and +// invalidation. There are no dirty areas, no masks and no screen-space footprint here; +// that bookkeeping only exists because the scanline compositor has no invalidation. +// +// Rectangle, Circle - a plain lv_obj drawn entirely with styles (background color and +// opacity, plus LV_RADIUS_CIRCLE for circles). No draw callback and no per-shape +// pixel memory. +// Polygon - an lv_image whose source is an A8 coverage mask rasterized from the shape's +// 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 +// shared-module/displayio/lvgl/Group.c for the same reasoning applied to TileGrids. + +typedef struct { + mp_obj_base_t base; + vectorio_ishape_t ishape; + mp_obj_t pixel_shader; + int16_t x; + int16_t y; + // Absolute position of the parent group's origin plus the group scale inherited from + // it. Groups are screen sized objects at the screen origin, so the mirror is placed + // in screen coordinates from these. + int32_t abs_x; + int32_t abs_y; + uint16_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; + bool hidden_by_parent : 1; + bool in_group : 1; + uint8_t padding : 5; +} vectorio_vector_shape_t; + +// Mirror lifecycle. Implemented in shared-module/vectorio/lvgl/VectorShape.c and called +// from shared-module/displayio/lvgl/Group.c, which owns the object tree. +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); +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); + +// How Group attaches, positions and hides a shape. See lvgl_protocol.h. +extern const lvgl_protocol_t displayio_lvgl_vector_shape_protocol; diff --git a/shared-module/videocore/VideoBuffer.c b/shared-module/videocore/VideoBuffer.c index a0d4c81f89e..928f82a1046 100644 --- a/shared-module/videocore/VideoBuffer.c +++ b/shared-module/videocore/VideoBuffer.c @@ -21,11 +21,6 @@ #include "shared-bindings/framebufferio/FramebufferDisplay.h" #endif -#define FB_GETTER(method, default_value) \ - (self->framebuffer_protocol->method \ - ? self->framebuffer_protocol->method(self->framebuffer) \ - : (default_value)) - // Add the screen area of a frame that landed at frame to vacated_area. Uses // only the cached framebuffer geometry since this runs in interrupt context. // A shifted frame can start mid-row and wrap its rows; then cover the full @@ -52,37 +47,20 @@ static void videocore_videobuffer_add_vacated_frame(videocore_videobuffer_obj_t area.x2 = self->fb_width; area.y2 += 1; } - displayio_area_union(&self->vacated_area, &area, &self->vacated_area); + displayio_area_union(&self->vacated_area.area, &area, &self->vacated_area.area); } // Called from interrupt context or a background task when the source lands a // frame, so keep it tiny. static void videocore_videobuffer_on_frame(void *ctx, const void *frame, const displayio_area_t *changed) { videocore_videobuffer_obj_t *self = ctx; - if (frame == self->stream_base) { - const displayio_area_t whole_frame = { - .x2 = self->source_width, - .y2 = self->source_height, - }; - if (changed == NULL) { - self->frame_changed_area = whole_frame; - self->frame_replaceable = true; - } else { - // Keep a bad source rect from dirtying other layers. - displayio_area_t clipped; - if (!displayio_area_compute_overlap(changed, &whole_frame, &clipped)) { - return; - } - displayio_area_union(&self->frame_changed_area, &clipped, &self->frame_changed_area); - self->frame_replaceable = false; - } - self->frame_ready = true; - return; + if (frame != self->stream_base) { + // It landed where the video used to be (the frame in flight when it + // moved or paused). Repaint there, since that area may already have + // been repainted. + videocore_videobuffer_add_vacated_frame(self, frame); } - // It landed where the video used to be (the frame in flight when it moved - // or paused). Repaint there, since that area may already have been - // repainted. - videocore_videobuffer_add_vacated_frame(self, frame); + videocore_videobuffer_backend_on_frame(self, frame, changed); } // Called from interrupt context or a background task when the source reached @@ -94,12 +72,8 @@ static void videocore_videobuffer_on_stream_end(void *ctx) { } // Drop any landed frame the refresh hasn't taken yet. -static void videocore_videobuffer_forget_frame(videocore_videobuffer_obj_t *self) { - displayio_area_t empty = {}; - common_hal_mcu_disable_interrupts(); - self->frame_ready = false; - self->frame_changed_area = empty; - common_hal_mcu_enable_interrupts(); +void videocore_videobuffer_forget_frame(videocore_videobuffer_obj_t *self) { + videocore_videobuffer_backend_forget_frame(self); } // Cache the framebuffer geometry for on_frame (see fb_pixels). @@ -113,17 +87,6 @@ static void videocore_videobuffer_cache_fb_geometry(videocore_videobuffer_obj_t self->fb_height = fb_height; } -// Add the currently rendered rect to the dirty area so the next refresh -// repaints what a move leaves behind. Accumulates across moves. Does nothing -// while the video isn't shown. -static void videocore_videobuffer_dirty_current_area(videocore_videobuffer_obj_t *self) { - if (self->absolute_transform.scale == 0 || self->hidden) { - return; - } - displayio_area_union( - &self->ephemeral_dirty_area, &self->current_area, &self->ephemeral_dirty_area); -} - // Compute the rect (in screen coordinates, which are the framebuffer's // physical coordinates) that a frame written at the logical position x/y // covers. A framebuffer target's frames are never rotated, and a rotated @@ -132,25 +95,15 @@ static void videocore_videobuffer_dirty_current_area(videocore_videobuffer_obj_t // that isn't shown has no transform, which translates by nothing, so the // areas reported while detached stay sensible. static void videocore_videobuffer_frame_rect(videocore_videobuffer_obj_t *self, - mp_int_t x, mp_int_t y, displayio_area_t *area) { + mp_int_t x, mp_int_t y, videocore_area_t *area) { const displayio_buffer_transform_t *t = &self->absolute_transform; - area->x1 = t->x + x; - area->y1 = t->y + y; - area->x2 = area->x1 + self->source_width; - area->y2 = area->y1 + self->source_height; + area->area.x1 = t->x + x; + area->area.y1 = t->y + y; + area->area.x2 = area->area.x1 + self->source_width; + area->area.y2 = area->area.y1 + self->source_height; area->next = NULL; } -// The source pixel that a screen pixel shows along one axis. d and origin come -// from the transform. A mirrored axis (d < 0) runs the other way, so the source -// pixel ends at the origin edge instead of starting there. -static mp_int_t videocore_videobuffer_source_axis(int8_t d, mp_int_t screen, - mp_int_t origin, mp_int_t offset) { - if (d > 0) { - return screen - origin - offset; - } - return origin - screen - 1 - offset; -} // Map a point in the source's coordinates (0, 0 to source_width/ // source_height) to screen coordinates, which are the framebuffer's physical @@ -182,18 +135,18 @@ static void videocore_videobuffer_source_point(videocore_videobuffer_obj_t *self // Map a rect in source coordinates to the screen rect it covers once it is // rotated. Mirroring and transposing a rect keeps it axis aligned, only // swapping its width and height on a transposed (90 or 270 degree) display. -static void videocore_videobuffer_source_rect(videocore_videobuffer_obj_t *self, - const displayio_area_t *source, displayio_area_t *screen) { +void videocore_videobuffer_source_rect(videocore_videobuffer_obj_t *self, + const displayio_area_t *source, videocore_area_t *screen) { mp_int_t x1; mp_int_t y1; mp_int_t x2; mp_int_t y2; videocore_videobuffer_source_point(self, source->x1, source->y1, &x1, &y1); videocore_videobuffer_source_point(self, source->x2, source->y2, &x2, &y2); - screen->x1 = MIN(x1, x2); - screen->y1 = MIN(y1, y2); - screen->x2 = MAX(x1, x2); - screen->y2 = MAX(y1, y2); + screen->area.x1 = MIN(x1, x2); + screen->area.y1 = MIN(y1, y2); + screen->area.x2 = MAX(x1, x2); + screen->area.y2 = MAX(y1, y2); screen->next = NULL; } @@ -201,7 +154,7 @@ static void videocore_videobuffer_source_rect(videocore_videobuffer_obj_t *self, // absolute transform. A framebuffer target only follows the transform's // position because its frames are never rotated. A plain buffer target is // rotated with the display, so its whole frame is transformed. -static void videocore_videobuffer_update_area(videocore_videobuffer_obj_t *self) { +void videocore_videobuffer_update_area(videocore_videobuffer_obj_t *self) { if (self->framebuffer_protocol != NULL) { videocore_videobuffer_frame_rect(self, self->x, self->y, &self->current_area); return; @@ -209,7 +162,6 @@ static void videocore_videobuffer_update_area(videocore_videobuffer_obj_t *self) displayio_area_t whole = { .x2 = self->source_width, .y2 = self->source_height, - .next = NULL, }; videocore_videobuffer_source_rect(self, &whole, &self->current_area); } @@ -273,11 +225,12 @@ static void videocore_videobuffer_compute_destination( // Frames keep the framebuffer's orientation: the containing group's // transform only translates them. The whole frame must stay within the // framebuffer and the buffer. - displayio_area_t rect; + videocore_area_t rect; videocore_videobuffer_frame_rect(self, self->x, self->y, &rect); if (self->x < 0 || self->y < 0 || - rect.x1 < 0 || rect.y1 < 0 || rect.x2 > fb_width || rect.y2 > fb_height || - first_pixel_offset + rect.y1 * row_stride + rect.x1 * bytes_per_pixel + + rect.area.x1 < 0 || rect.area.y1 < 0 || + rect.area.x2 > fb_width || rect.area.y2 > fb_height || + first_pixel_offset + rect.area.y1 * row_stride + rect.area.x1 * bytes_per_pixel + videocore_videobuffer_frame_span(self, row_stride) > bufinfo.len) { mp_raise_ValueError(MP_ERROR_TEXT("video doesn't fit in framebuffer")); @@ -286,7 +239,7 @@ static void videocore_videobuffer_compute_destination( self, &bufinfo, fb_width, fb_height, row_stride, first_pixel_offset); dest->base = (uint8_t *)bufinfo.buf + first_pixel_offset + - rect.y1 * row_stride + rect.x1 * bytes_per_pixel; + rect.area.y1 * row_stride + rect.area.x1 * bytes_per_pixel; dest->stride = row_stride; dest->row_bytes = row_bytes; dest->height = self->source_height; @@ -297,7 +250,7 @@ static void videocore_videobuffer_compute_destination( // Move the framebuffer destination for a new absolute position, capping the // shift so the frame stays completely within the framebuffer. Best effort: // never raises, because this runs from displayio's refresh. -static void videocore_videobuffer_move_dest(videocore_videobuffer_obj_t *self) { +void videocore_videobuffer_move_dest(videocore_videobuffer_obj_t *self) { if (!self->playing || self->paused) { return; } @@ -316,10 +269,10 @@ static void videocore_videobuffer_move_dest(videocore_videobuffer_obj_t *self) { return; } - displayio_area_t rect; + videocore_area_t rect; videocore_videobuffer_frame_rect(self, self->x, self->y, &rect); - mp_int_t shift_x = MAX(rect.x1, 0); - mp_int_t shift_y = MAX(rect.y1, 0); + mp_int_t shift_x = MAX(rect.area.x1, 0); + mp_int_t shift_y = MAX(rect.area.y1, 0); // Cap the start and the end. Cap the rows first: enough rows must fit // above the frame for it to fit at all. const mp_int_t max_end = (mp_int_t)bufinfo.len - first_pixel_offset - @@ -349,6 +302,7 @@ static void videocore_videobuffer_move_dest(videocore_videobuffer_obj_t *self) { // A rejected destination leaves the stream where it was. if (videosource_set_destination(self->source, &dest, videocore_videobuffer_on_frame, self)) { self->stream_base = dest.base; + videocore_videobuffer_backend_source_changed(self); } } @@ -358,6 +312,14 @@ void common_hal_videocore_videobuffer_construct(videocore_videobuffer_obj_t *sel self->source = mp_const_none; self->stream_base = NULL; self->playing = false; + // The finaliser can run after any of the raises below, so the backend state + // has to be valid before anything else can fail. + videocore_videobuffer_backend_construct(self); + displayio_area_t empty = {}; + self->current_area.area = empty; + self->current_area.next = NULL; + self->vacated_area.area = empty; + self->vacated_area.next = NULL; self->framebuffer = framebuffer; self->framebuffer_protocol = mp_proto_get(MP_QSTR_protocol_framebuffer, framebuffer); if (self->framebuffer_protocol != NULL) { @@ -379,7 +341,6 @@ void common_hal_videocore_videobuffer_construct(videocore_videobuffer_obj_t *sel self->x = 0; self->y = 0; - self->frame_ready = false; self->fb_pixels = NULL; self->fb_pixels_len = 0; self->fb_row_stride = 0; @@ -389,13 +350,6 @@ void common_hal_videocore_videobuffer_construct(videocore_videobuffer_obj_t *sel self->hidden = false; self->stale_size = false; self->source_ended = false; - displayio_area_t empty = {}; - self->current_area = empty; - self->ephemeral_dirty_area = empty; - self->vacated_area = empty; - self->frame_changed_area = empty; - self->frame_replaceable = false; - self->frame_dirty_area = empty; displayio_buffer_transform_t empty_transform = {}; self->absolute_transform = empty_transform; // Nothing is shown until a Group attached to a display updates our @@ -423,6 +377,8 @@ static void videocore_videobuffer_read_source_format(videocore_videobuffer_obj_t if (color_depth % 8 != 0 || bits_per_pixel != color_depth) { mp_raise_ValueError(MP_ERROR_TEXT("video format doesn't match framebuffer")); } + } else { + videocore_videobuffer_backend_check_buffer_format(self, bits_per_pixel); } self->source_width = width; @@ -447,6 +403,7 @@ static void videocore_videobuffer_start_stream(videocore_videobuffer_obj_t *self } self->stream_base = dest.base; self->paused = false; + videocore_videobuffer_backend_stream_changed(self); } // Un-show a framebuffer target by pausing its source. The play session and @@ -461,6 +418,7 @@ static void videocore_videobuffer_pause_stream(videocore_videobuffer_obj_t *self // lands after this, and on_frame adds it to vacated_area to repaint over. self->stream_base = NULL; self->paused = true; + videocore_videobuffer_backend_stream_changed(self); nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { videosource_pause(self->source); @@ -492,11 +450,12 @@ static void videocore_videobuffer_resume_stream(videocore_videobuffer_obj_t *sel self->paused = false; // Wait for the first frame at the new destination. videocore_videobuffer_forget_frame(self); + videocore_videobuffer_backend_stream_changed(self); } // Stop a source that reached the end of its stream. stream_base stays, so the // last frame stays shown until stop() or the next play(). Never raises. -static void videocore_videobuffer_end_stream(videocore_videobuffer_obj_t *self) { +void videocore_videobuffer_end_stream(videocore_videobuffer_obj_t *self) { if (!self->playing || !self->source_ended) { return; } @@ -512,13 +471,13 @@ static void videocore_videobuffer_end_stream(videocore_videobuffer_obj_t *self) // Start, resume or pause a framebuffer target's source to match whether it is // shown (on a display and not hidden). While hidden the source stays active // but paused, so showing it again is quick. -static void videocore_videobuffer_update_stream(videocore_videobuffer_obj_t *self) { +void videocore_videobuffer_update_stream(videocore_videobuffer_obj_t *self) { if (self->framebuffer_protocol == NULL) { return; // Plain buffer targets always stream. } // An ended source must not resume. videocore_videobuffer_end_stream(self); - const bool shown = self->absolute_transform.scale != 0 && !self->hidden; + const bool shown = videocore_videobuffer_backend_shown(self); if (!self->playing) { // Remember whether the target is shown so play() can start the source // in the right state. @@ -527,6 +486,7 @@ static void videocore_videobuffer_update_stream(videocore_videobuffer_obj_t *sel // A repaint overwrites an ended stream's last frame. self->stream_base = NULL; } + videocore_videobuffer_backend_stream_changed(self); return; } if (shown && !self->stale_size) { @@ -536,6 +496,7 @@ static void videocore_videobuffer_update_stream(videocore_videobuffer_obj_t *sel } else if (!shown && !self->paused) { videocore_videobuffer_pause_stream(self); } + videocore_videobuffer_backend_stream_changed(self); } void common_hal_videocore_videobuffer_play(videocore_videobuffer_obj_t *self, mp_obj_t source) { @@ -568,6 +529,7 @@ void common_hal_videocore_videobuffer_play(videocore_videobuffer_obj_t *self, mp if (!start_paused) { videocore_videobuffer_start_stream(self); } + videocore_videobuffer_backend_source_changed(self); nlr_pop(); } else { videocore_videobuffer_stop_quiet(self); @@ -584,13 +546,13 @@ void common_hal_videocore_videobuffer_set_position(videocore_videobuffer_obj_t * videocore_videobuffer_end_stream(self); // Repaint where the video used to be. Union (rather than overwrite) so a // move before the previous one was refreshed isn't lost. - videocore_videobuffer_dirty_current_area(self); + videocore_videobuffer_backend_dirty_current_area(self); self->x = x; self->y = y; videocore_videobuffer_update_area(self); if (self->framebuffer_protocol == NULL) { // Draw the last frame at its new position. - videocore_videobuffer_dirty_current_area(self); + videocore_videobuffer_backend_dirty_current_area(self); } if (!self->playing && self->framebuffer_protocol != NULL) { // A repaint overwrites an ended stream's last frame. @@ -600,6 +562,7 @@ void common_hal_videocore_videobuffer_set_position(videocore_videobuffer_obj_t * // Raw buffer targets are rendered by displayio; their capture // destination doesn't move with the position. A paused framebuffer // target has no destination to move yet. + videocore_videobuffer_backend_source_changed(self); return; } // The source's frame size was fixed when play() was called. Reject @@ -620,6 +583,7 @@ void common_hal_videocore_videobuffer_set_position(videocore_videobuffer_obj_t * if (videosource_set_destination(self->source, &dest, videocore_videobuffer_on_frame, self)) { self->stream_base = dest.base; } + videocore_videobuffer_backend_source_changed(self); } static void videocore_videobuffer_clear(videocore_videobuffer_obj_t *self) { @@ -627,6 +591,7 @@ static void videocore_videobuffer_clear(videocore_videobuffer_obj_t *self) { videocore_videobuffer_forget_frame(self); self->stream_base = NULL; self->source = mp_const_none; + videocore_videobuffer_backend_stream_changed(self); } void common_hal_videocore_videobuffer_stop(videocore_videobuffer_obj_t *self) { @@ -669,485 +634,14 @@ mp_obj_t common_hal_videocore_videobuffer_get_source(videocore_videobuffer_obj_t return self->source; } -// Draw protocol implementation. displayio copies plain-buffer frames into the -// display. Framebuffer-target frames are already in place; fill_area only -// copies them into displayio's scratch buffer when other layers overlap. - -static mp_obj_t videocore_videobuffer_draw_protocol_self(mp_obj_t protocol_container) { - return protocol_container; -} - -// Whether a layer above already claimed a pixel near the overlap. Checks whole words, which may -// include pixels between overlap rows or just outside the overlap. That can only make the caller -// take its slower path; it never misses a claimed pixel. -static bool videocore_videobuffer_overlap_claimed(const uint32_t *mask, const displayio_area_t *area, - const displayio_area_t *overlap) { - const size_t span_px = displayio_area_width(area); - const size_t first_px = (size_t)(overlap->y1 - area->y1) * span_px + (overlap->x1 - area->x1); - const size_t end_px = (size_t)(overlap->y2 - 1 - area->y1) * span_px + (overlap->x2 - area->x1); - for (size_t word = first_px / 32; word <= (end_px - 1) / 32; word++) { - if (mask[word] != 0) { - return true; - } - } - return false; -} - -// Copy a 1 bit per pixel plain buffer into a 1 bit display's buffer. Both are MSB first with -// 1 = white, the same as a monochrome EPaperDisplay's first RAM pass before any inversion. -// On a rotated display each screen pixel is walked back to its source pixel, as the 16 bit copy -// does. Whole bytes are copied when the source runs forwards along screen x, the rects line up -// and nothing above claimed these pixels. -static bool videocore_videobuffer_fill_area_1bit(videocore_videobuffer_obj_t *self, - const _displayio_colorspace_t *colorspace, const displayio_area_t *area, const displayio_area_t *overlap, - bool full_coverage, uint32_t *mask, uint32_t *buffer) { - // Every grayscale bit of a 1 bit pixel is that pixel, so this also serves each grayscale pass. - if (!colorspace->pixels_in_byte_share_row || !colorspace->reverse_pixels_in_byte || - !colorspace->grayscale) { - return false; - } - const size_t span_px = displayio_area_width(area); - const size_t width = displayio_area_width(overlap); - const size_t row_bytes = videocore_videobuffer_source_row_bytes(self); - const size_t x_in_area = overlap->x1 - area->x1; - const uint8_t *frame = self->stream_base; - uint8_t *out = (uint8_t *)buffer; - - // While the video isn't shown there is no transform and the orientations match. - const displayio_buffer_transform_t *t = &self->absolute_transform; - const bool shown = t->scale != 0; - const int8_t dx = shown ? t->dx : 1; - const int8_t dy = shown ? t->dy : 1; - const bool transpose = shown && t->transpose_xy; - const mp_int_t origin_x = shown ? t->x : 0; - const mp_int_t origin_y = shown ? t->y : 0; - const bool contiguous = !transpose && dx > 0; - // The first source column of every row when the source runs along screen x. - const mp_int_t first_x = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->x); - - const bool whole_bytes = contiguous && span_px % 8 == 0 && x_in_area % 8 == 0 && first_x % 8 == 0 && - width % 8 == 0 && !videocore_videobuffer_overlap_claimed(mask, area, overlap); - for (int16_t y = overlap->y1; y < overlap->y2; y++) { - const size_t px = (size_t)(y - area->y1) * span_px + x_in_area; - if (!contiguous) { - // Mirrored or transposed: walk each screen pixel back to its source pixel. Screen x - // steps along a source row, or down a source column on a transposed display. - mp_int_t source_x; - mp_int_t source_y; - if (transpose) { - source_x = videocore_videobuffer_source_axis(dy, y, origin_y, self->x); - source_y = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->y); - } else { - source_x = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->x); - source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); - } - for (size_t i = 0; i < width; i++) { - const size_t p = px + i; - if (mask[p / 32] & (1u << (p % 32))) { - continue; - } - const mp_int_t step = (mp_int_t)i * dx; - const mp_int_t fx = transpose ? source_x : source_x + step; - const mp_int_t fy = transpose ? source_y + step : source_y; - if (frame[(size_t)fy * row_bytes + (size_t)fx / 8] & (0x80 >> (fx % 8))) { - out[p / 8] |= 0x80 >> (p % 8); - } - mask[p / 32] |= 1u << (p % 32); - } - continue; - } - const size_t x_in_frame = first_x; - const mp_int_t source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); - const uint8_t *row = frame + (size_t)source_y * row_bytes; - if (whole_bytes) { - memcpy(out + px / 8, row + x_in_frame / 8, width / 8); - // Full coverage ends the layer walk, so filling the mask once below is enough. - if (!full_coverage) { - for (size_t p = px; p < px + width; p++) { - mask[p / 32] |= 1u << (p % 32); - } - } - continue; - } - for (size_t i = 0; i < width; i++) { - const size_t p = px + i; - if (mask[p / 32] & (1u << (p % 32))) { - continue; - } - const size_t f = x_in_frame + i; - if (row[f / 8] & (0x80 >> (f % 8))) { - out[p / 8] |= 0x80 >> (p % 8); - } - mask[p / 32] |= 1u << (p % 32); - } - } - if (whole_bytes && full_coverage) { - // A FramebufferDisplay writes nothing back when the mask is empty. - memset(mask, 0xff, (displayio_area_size(area) + 31) / 32 * sizeof(uint32_t)); - } - return full_coverage; -} - -static bool videocore_videobuffer_draw_fill_area(mp_obj_t self_in, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - // buffer isn't necessarily the framebuffer. It may be a scratch buffer. - if (self->stream_base == NULL || self->hidden) { - return false; - } - - displayio_area_t overlap; - if (!displayio_area_compute_overlap(area, &self->current_area, &overlap)) { - return false; - } - const bool full_coverage = displayio_area_equal(area, &overlap); - const uint16_t span_px = displayio_area_width(area); - const size_t overlap_width = displayio_area_width(&overlap); - - if (self->framebuffer_protocol != NULL) { - // The source already wrote these pixels into the framebuffer. If - // nothing else covers this area, claim no pixels so the display skips - // its write-back. Otherwise copy the video's unclaimed pixels into - // the scratch buffer and claim them, since the scratch buffer is - // written back over the framebuffer. - const size_t bytes_per_pixel = self->source_bits_per_pixel / 8; - const mp_int_t fb_width = FB_GETTER(get_width, 0); - const mp_int_t row_stride = FB_GETTER(get_row_stride, fb_width * self->source_bits_per_pixel / 8); - const uint8_t *frame = (const uint8_t *)self->stream_base; - uint8_t *out = (uint8_t *)buffer; - - if (full_coverage && !videocore_videobuffer_overlap_claimed(mask, area, &overlap)) { - // Sole coverer: nothing was composed, so the driver skips the copy. - return true; - } - - for (int16_t y = overlap.y1; y < overlap.y2; y++) { - const size_t px = (size_t)(y - area->y1) * span_px + (overlap.x1 - area->x1); - const int16_t fy = y - self->current_area.y1; - const uint8_t *row = frame + (size_t)fy * row_stride + - (size_t)(overlap.x1 - self->current_area.x1) * bytes_per_pixel; - size_t i = 0; - while (i < overlap_width) { - // Skip pixels a higher layer already claimed or drew. - while (i < overlap_width && (mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { - i++; - } - const size_t start = i; - while (i < overlap_width && !(mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { - mask[(px + i) / 32] |= 1u << ((px + i) % 32); - i++; - } - if (i > start) { - memcpy( - out + (px + start) * bytes_per_pixel, - row + start * bytes_per_pixel, - (i - start) * bytes_per_pixel); - } - } - } - return full_coverage; - } - - if (colorspace->depth == 1 && self->source_bits_per_pixel == 1) { - return videocore_videobuffer_fill_area_1bit(self, colorspace, area, &overlap, full_coverage, mask, buffer); - } - - // Raw buffer target: copy the pixels, with no color conversion: the - // display depth must match the video's pixel format byte for byte. - if (colorspace->depth != self->source_bits_per_pixel || colorspace->depth != 16) { - return false; - } - - const size_t bytes_per_pixel = self->source_bits_per_pixel / 8; - const uint8_t *frame = (const uint8_t *)self->stream_base; - // displayio renders in screen coordinates (the framebuffer's physical - // ones) while the frame is stored in the source's own orientation, so - // walk from each screen pixel back to its source pixel. A plain buffer - // target is rotated with the display; while the video isn't shown there - // is no transform and the orientations match. - const displayio_buffer_transform_t *t = &self->absolute_transform; - const bool shown = t->scale != 0; - const int8_t dx = shown ? t->dx : 1; - const int8_t dy = shown ? t->dy : 1; - const bool transpose = shown && t->transpose_xy; - const mp_int_t origin_x = shown ? t->x : 0; - const mp_int_t origin_y = shown ? t->y : 0; - // Source pixels run along screen x only when the frame isn't transposed, - // and forwards only when screen x isn't mirrored. - const bool contiguous = !transpose && dx > 0; - - for (int16_t y = overlap.y1; y < overlap.y2; y++) { - const size_t px = (size_t)(y - area->y1) * span_px + (overlap.x1 - area->x1); - uint16_t *out = (uint16_t *)buffer + px; - // The first source pixel this row shows. The frame's column and row - // follow the transform, which swaps them on a transposed display. - mp_int_t source_x; - mp_int_t source_y; - if (transpose) { - source_x = videocore_videobuffer_source_axis(dy, y, origin_y, self->x); - source_y = videocore_videobuffer_source_axis(dx, overlap.x1, origin_x, self->y); - } else { - source_x = videocore_videobuffer_source_axis(dx, overlap.x1, origin_x, self->x); - source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); - } - // Where the next screen x leads: the next source column, or on a - // transposed display the next source row. - const ptrdiff_t step = transpose - ? (ptrdiff_t)dx * (ptrdiff_t)self->source_width * (ptrdiff_t)bytes_per_pixel - : (ptrdiff_t)dx * (ptrdiff_t)bytes_per_pixel; - const uint8_t *in = frame + - ((size_t)source_y * self->source_width + (size_t)source_x) * bytes_per_pixel; - if (contiguous) { - size_t i = 0; - while (i < overlap_width) { - // Skip pixels a higher layer already claimed or drew. - while (i < overlap_width && (mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { - i++; - } - const size_t start = i; - while (i < overlap_width && !(mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { - mask[(px + i) / 32] |= 1u << ((px + i) % 32); - i++; - } - if (i > start) { - memcpy( - out + start, - in + start * bytes_per_pixel, - (i - start) * bytes_per_pixel); - if (colorspace->reverse_bytes_in_word) { - for (size_t j = start; j < i; j++) { - out[j] = __builtin_bswap16(out[j]); - } - } - } - } - } else { - // Mirrored or transposed: the source pixels between screen pixels - // aren't next to each other, so copy them one by one. - for (size_t i = 0; i < overlap_width; i++) { - if (mask[(px + i) / 32] & (1u << ((px + i) % 32))) { - continue; - } - mask[(px + i) / 32] |= 1u << ((px + i) % 32); - uint16_t value; - memcpy(&value, in + (ptrdiff_t)i * step, bytes_per_pixel); - if (colorspace->reverse_bytes_in_word) { - value = __builtin_bswap16(value); - } - out[i] = value; - } - } - } - return full_coverage; -} - -static bool videocore_videobuffer_draw_get_dirty_area(mp_obj_t self_in, displayio_area_t *out_area) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - out_area->x1 = out_area->x2; - displayio_area_union(&self->ephemeral_dirty_area, &self->current_area, out_area); - return true; -} - -static void videocore_videobuffer_draw_update_transform(mp_obj_t self_in, displayio_buffer_transform_t *group_transform) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - // Remember where the video was so a move can repaint the pixels it - // vacates. The area is only valid while the video is shown; a layer being - // attached or detached is handled by the display's full refresh. - displayio_area_t previous_area = self->current_area; - const bool was_shown = self->absolute_transform.scale != 0 && !self->hidden; - // A group that isn't attached to a display has no transform, and its - // update_transform clears the absolute transform we receive. A real - // (shown) transform always has scale >= 1. - if (group_transform == NULL || group_transform->scale == 0) { - displayio_buffer_transform_t no_transform = {}; - self->absolute_transform = no_transform; - } else { - // Validate the transform. A framebuffer target streams rows straight - // into the framebuffer's memory, so it can't turn with a rotated - // display. This is also checked when a group becomes the display's - // root group, but rotation can change afterwards. - if (self->framebuffer_protocol != NULL && - (group_transform->mirror_x || group_transform->mirror_y || - group_transform->transpose_xy)) { - mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer can't stream into a rotated display")); - } - // A plain buffer target is copied into the display, so a rotated - // display works: its transform mirrors and/or transposes the logical - // position, which the copy applies to the frame. Neither can scale. - if (group_transform->scale != 1 || - group_transform->dx == 0 || group_transform->dy == 0) { - mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer does not support transforms")); - } - self->absolute_transform = *group_transform; - } - - // A framebuffer target starts or stops with whether it is shown (the - // absolute transform is known now). This runs from Python contexts, so a - // (re)start may raise. - videocore_videobuffer_update_stream(self); - - videocore_videobuffer_update_area(self); - - // Moved by a containing Group (or a transform change): dirty the rect it - // used to occupy so the refresh repaints those pixels instead of leaving - // the previous frame there. - if (was_shown && self->absolute_transform.scale != 0 && - !displayio_area_equal(&previous_area, &self->current_area)) { - displayio_area_union( - &self->ephemeral_dirty_area, &previous_area, &self->ephemeral_dirty_area); - if (self->framebuffer_protocol == NULL) { - // Draw the last frame at its new position. - videocore_videobuffer_dirty_current_area(self); - } else if (!self->playing) { - // A repaint overwrites an ended stream's last frame. - self->stream_base = NULL; - } - } - - // Framebuffer targets stream straight into the buffer: move the - // destination along with the shift. Best effort, since this can't raise: - // the shift may land the start mid-row, which won't look right but is - // obvious; the frame is capped to stay completely within the buffer. - if (self->framebuffer_protocol != NULL) { - videocore_videobuffer_move_dest(self); - } -} - -static void videocore_videobuffer_draw_finish_refresh(mp_obj_t self_in) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - displayio_area_t empty = {}; - self->ephemeral_dirty_area = empty; - // A frame that landed during the refresh waits for the next one, unless its - // successor repaints everything anyway. - if (self->frame_replaceable) { - videocore_videobuffer_forget_frame(self); - } - videocore_videobuffer_end_stream(self); -} - -static displayio_area_t *videocore_videobuffer_draw_get_refresh_areas(mp_obj_t self_in, displayio_area_t *tail) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - // Take what landed so far in one step: a frame landing from here on waits - // for the next refresh. - displayio_area_t empty = {}; - common_hal_mcu_disable_interrupts(); - displayio_area_t vacated = self->vacated_area; - const bool frame_ready = self->frame_ready; - self->frame_dirty_area = self->frame_changed_area; - self->vacated_area = empty; - self->frame_ready = false; - self->frame_changed_area = empty; - common_hal_mcu_enable_interrupts(); - // Frames that landed where the video used to be, possibly over the repaint - // of that area: repaint there again. - displayio_area_union(&self->ephemeral_dirty_area, &vacated, &self->ephemeral_dirty_area); - // Always repaint the area the video vacated, even once it is hidden or - // stopped, so its last frame doesn't stay on the target. - if (!displayio_area_empty(&self->ephemeral_dirty_area)) { - // The video moved or was hidden: repaint with the lower layers. - self->ephemeral_dirty_area.next = tail; - tail = &self->ephemeral_dirty_area; - } - if (self->stream_base == NULL || self->paused || self->hidden || self->stale_size) { - return tail; - } - if (frame_ready) { - // New frame: copy the part that changed. A plain buffer target is - // rotated as it is copied, so map the changed source pixels to the - // screen; a framebuffer target's frame lands unrotated at the current - // area, so shift them there. - if (self->framebuffer_protocol == NULL) { - displayio_area_t changed = self->frame_dirty_area; - videocore_videobuffer_source_rect(self, &changed, &self->frame_dirty_area); - } else { - displayio_area_shift(&self->frame_dirty_area, self->current_area.x1, self->current_area.y1); - } - self->frame_dirty_area.next = tail; - tail = &self->frame_dirty_area; - } - return tail; -} - -static void videocore_videobuffer_draw_set_dirty(mp_obj_t self_in) { - (void)self_in; -} - -static void videocore_videobuffer_draw_set_hidden(mp_obj_t self_in, bool hidden) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - if (self->hidden == hidden) { - return; - } - videocore_videobuffer_end_stream(self); - self->hidden = hidden; - // Repaint what the video showed or vacated. - self->ephemeral_dirty_area = self->current_area; - if (!hidden && self->playing) { - // The source normally can't change while the sensor stays streaming, - // but validate its sizing again in case it did: if it no longer - // matches, don't raise; simply stop showing the stream until stop and - // play again. - const videosource_p_t *protocol = mp_proto_get(MP_QSTR_protocol_videosource, self->source); - if (protocol != NULL && - (protocol->get_width(self->source) != self->source_width || - protocol->get_height(self->source) != self->source_height || - protocol->get_bits_per_pixel(self->source) != self->source_bits_per_pixel)) { - self->stale_size = true; - } - } - // Hiding or unhiding a framebuffer target pauses or resumes its source. - videocore_videobuffer_update_stream(self); -} - -static bool videocore_videobuffer_draw_set_in_group(mp_obj_t self_in, bool in_group) { - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); - bool was_in_group = self->in_group; - self->in_group = in_group; - return was_in_group; -} - -static vectorio_draw_protocol_impl_t videocore_videobuffer_draw_protocol_impl = { - .draw_fill_area = videocore_videobuffer_draw_fill_area, - .draw_get_dirty_area = videocore_videobuffer_draw_get_dirty_area, - .draw_update_transform = videocore_videobuffer_draw_update_transform, - .draw_finish_refresh = videocore_videobuffer_draw_finish_refresh, - .draw_get_refresh_areas = videocore_videobuffer_draw_get_refresh_areas, - .draw_set_dirty = videocore_videobuffer_draw_set_dirty, - .draw_set_hidden = videocore_videobuffer_draw_set_hidden, - .draw_set_in_group = videocore_videobuffer_draw_set_in_group, -}; - -const vectorio_draw_protocol_t videocore_videobuffer_draw_protocol = { - MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw) - .draw_get_protocol_self = videocore_videobuffer_draw_protocol_self, - .draw_protocol_impl = &videocore_videobuffer_draw_protocol_impl, -}; - // Root group validation: a VideoBuffer that streams straight into a // framebuffer bypasses displayio rendering, so it may only be shown on a // display that renders that same framebuffer, and only while that display is -// unrotated: its frames go straight into the framebuffer's memory, so they -// can't turn with the display. Plain buffer targets are drawn by displayio, -// rotate with the display and are fine anywhere. Walks the group tree and -// raises when a VideoBuffer can't be shown on the display it would render on. -static void videocore_validate_member(mp_obj_t member, mp_obj_t display, mp_int_t rotation) { - displayio_group_t *group = mp_obj_cast_to_native_base(member, &displayio_group_type); - if (group != MP_OBJ_NULL) { - for (size_t i = 0; i < group->members->len; i++) { - videocore_validate_member(group->members->items[i], display, rotation); - } - return; - } - const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, member); - if (draw_protocol == NULL) { - return; - } - mp_obj_t layer = draw_protocol->draw_get_protocol_self(member); - if (mp_obj_get_type(layer) != &videocore_videobuffer_type) { - return; - } - videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); - if (self->framebuffer_protocol == NULL) { - return; // Plain buffer targets are drawn, and rotated, by displayio. - } +// unrotated: its frames go straight into the framebuffer's memory, while LVGL +// rotates everything it renders. Plain buffer targets are drawn or sampled by +// the compositor, so they rotate with the display and are fine anywhere. +void videocore_videobuffer_validate_framebuffer_target(videocore_videobuffer_obj_t *self, + mp_obj_t display, mp_int_t rotation) { if (rotation != 0) { mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer can't stream into a rotated display")); } @@ -1168,6 +662,18 @@ static void videocore_validate_member(mp_obj_t member, mp_obj_t display, mp_int_ mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer is playing into a different framebuffer")); } +// Walks the group tree and lets the backend check each VideoBuffer it finds. +static void videocore_validate_member(mp_obj_t member, mp_obj_t display, mp_int_t rotation) { + displayio_group_t *group = mp_obj_cast_to_native_base(member, &displayio_group_type); + if (group != MP_OBJ_NULL) { + for (size_t i = 0; i < group->members->len; i++) { + videocore_validate_member(group->members->items[i], display, rotation); + } + return; + } + videocore_videobuffer_backend_validate_member(member, display, rotation); +} + void videocore_validate_root_group(mp_obj_t root_group, mp_obj_t display, mp_int_t rotation) { videocore_validate_member(root_group, display, rotation); } diff --git a/shared-module/videocore/VideoBuffer.h b/shared-module/videocore/VideoBuffer.h index c17124fde20..e7c69fee760 100644 --- a/shared-module/videocore/VideoBuffer.h +++ b/shared-module/videocore/VideoBuffer.h @@ -6,15 +6,39 @@ #pragma once +#include +#include + #include "py/obj.h" -#include "shared-bindings/vectorio/__init__.h" // draw protocol #include "shared-module/displayio/area.h" +// Screen areas handed between VideoBuffer's shared state and its backend. The legacy +// compositor walks linked lists of them, so keep the link in a wrapper: displayio_area_t +// itself is just lv_area_t in LVGL builds. +typedef struct _videocore_area_t { + displayio_area_t area; + const struct _videocore_area_t *next; +} videocore_area_t; + #include "shared-module/framebufferio/FramebufferDisplay.h" // framebuffer protocol #include "shared-module/videocore/__init__.h" -typedef struct { +typedef struct _videocore_videobuffer_obj_t videocore_videobuffer_obj_t; + +// The compositor-specific half of a VideoBuffer lives in one of the two backend directories. +// See legacy/VideoBuffer.h and lvgl/VideoBuffer.h. +#if CIRCUITPY_DISPLAYIO_LVGL +#include "shared-module/videocore/lvgl/VideoBuffer.h" +// The protocol slot a VideoBuffer exposes to its Group. Legacy VideoBuffers are part of the +// draw protocol; LVGL ones provide protocol_lvgl. +#define VIDEOCORE_VIDEOBUFFER_PROTOCOL &videocore_videobuffer_lvgl_protocol +#else +#include "shared-module/videocore/legacy/VideoBuffer.h" +#define VIDEOCORE_VIDEOBUFFER_PROTOCOL &videocore_videobuffer_draw_protocol +#endif + +struct _videocore_videobuffer_obj_t { mp_obj_base_t base; // Object frames are streamed into: an object implementing the framebuffer // protocol, such as mipidsi.Display, or a writable buffer such as a @@ -38,22 +62,12 @@ typedef struct { // Base of the destination the source last accepted, or NULL when there // is none. const void *stream_base; - // Set from interrupt context when the source lands a frame in the - // destination; cleared when the refresh takes the changed area. - volatile bool frame_ready; - // What changed in the frames landed since the last refresh, in source - // coordinates. Grown with frame_ready and cleared with it. - displayio_area_t frame_changed_area; - // The last frame landed passed NULL, so the next one repaints everything. - volatile bool frame_replaceable; - // frame_changed_area in screen coordinates, as handed to the refresh. - displayio_area_t frame_dirty_area; // Screen area of frames that landed where the video used to be: the frame // in flight when the destination moves or the source pauses still lands // at the old destination, possibly after that area was repainted. Grown - // by on_frame in interrupt context; the refresh moves it into - // ephemeral_dirty_area with interrupts disabled. - displayio_area_t vacated_area; + // by on_frame in interrupt context; the refresh moves it into its own + // dirty bookkeeping with interrupts disabled. + videocore_area_t vacated_area; // Framebuffer geometry cached for the destination and for on_frame, which // can't call the framebuffer protocol from interrupt context. fb_pixels is // NULL for plain buffer targets. fb_width and fb_height are the @@ -63,11 +77,9 @@ typedef struct { size_t fb_row_stride; mp_int_t fb_width; mp_int_t fb_height; - // displayio draw protocol state: the rect currently rendered (in screen - // coordinates, which are the framebuffer's physical coordinates) and the - // rect that changed since the last refresh. - displayio_area_t current_area; - displayio_area_t ephemeral_dirty_area; + // The rect currently rendered, in screen coordinates, which are the + // framebuffer's physical coordinates. + videocore_area_t current_area; // Maps the video's Group-local coordinates to screen coordinates, which are // the framebuffer's physical coordinates. displayio_buffer_transform_t absolute_transform; @@ -86,7 +98,15 @@ typedef struct { // Tracked while stopped too, so play() can start the source paused. // Plain buffer targets are never paused. bool paused; -} videocore_videobuffer_obj_t; + // Compositor-specific mirror or draw state. + videocore_videobuffer_backend_t backend; +}; + +// Read an optional framebuffer protocol getter, or default_value when it is NULL. +#define FB_GETTER(method, default_value) \ + (self->framebuffer_protocol->method \ + ? self->framebuffer_protocol->method(self->framebuffer) \ + : (default_value)) void common_hal_videocore_videobuffer_construct(videocore_videobuffer_obj_t *self, mp_obj_t framebuffer, mp_int_t bits_per_pixel); @@ -99,7 +119,20 @@ void videocore_videobuffer_stop_quiet(videocore_videobuffer_obj_t *self); bool common_hal_videocore_videobuffer_get_playing(videocore_videobuffer_obj_t *self); mp_obj_t common_hal_videocore_videobuffer_get_source(videocore_videobuffer_obj_t *self); -// Draw protocol that lets a VideoBuffer be added to a displayio Group. -// displayio copies plain-buffer frames into the display; framebuffer-target -// frames are already in place, so displayio only marks their rows. -extern const vectorio_draw_protocol_t videocore_videobuffer_draw_protocol; +// Update the stream to match whether the VideoBuffer is shown (on a display, +// attached, and not hidden). May raise when resuming a framebuffer target. +void videocore_videobuffer_update_stream(videocore_videobuffer_obj_t *self); +// Recompute the rect the video covers from its position and transform. +void videocore_videobuffer_update_area(videocore_videobuffer_obj_t *self); +// Map a rect in source coordinates to the screen rect it covers once rotated. +void videocore_videobuffer_source_rect(videocore_videobuffer_obj_t *self, + const displayio_area_t *source, videocore_area_t *screen); +// Follow a move of a playing framebuffer target. Best effort, never raises. +void videocore_videobuffer_move_dest(videocore_videobuffer_obj_t *self); +// Drop any landed frame the next refresh hasn't taken yet. +void videocore_videobuffer_forget_frame(videocore_videobuffer_obj_t *self); +// Stop a source that reached the end of its stream. Never raises. +void videocore_videobuffer_end_stream(videocore_videobuffer_obj_t *self); +// Raise when a framebuffer target can't be shown on this display. +void videocore_videobuffer_validate_framebuffer_target(videocore_videobuffer_obj_t *self, + mp_obj_t display, mp_int_t rotation); diff --git a/shared-module/videocore/legacy/VideoBuffer.c b/shared-module/videocore/legacy/VideoBuffer.c new file mode 100644 index 00000000000..c8b138b2ec9 --- /dev/null +++ b/shared-module/videocore/legacy/VideoBuffer.c @@ -0,0 +1,581 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// Legacy scanline compositor implementation of videocore.VideoBuffer. The shared +// stream/target state lives in shared-module/videocore/VideoBuffer.c; this file owns the +// draw protocol and the dirty-area bookkeeping the scanline compositor needs. + +#include + +#include "shared-module/videocore/VideoBuffer.h" + +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/videocore/VideoBuffer.h" + +void videocore_videobuffer_backend_construct(videocore_videobuffer_obj_t *self) { + displayio_area_t empty = {}; + self->backend.frame_ready = false; + self->backend.frame_changed_area.area = empty; + self->backend.frame_changed_area.next = NULL; + self->backend.frame_replaceable = false; + self->backend.frame_dirty_area.area = empty; + self->backend.frame_dirty_area.next = NULL; + self->backend.ephemeral_dirty_area.area = empty; + self->backend.ephemeral_dirty_area.next = NULL; +} + +bool videocore_videobuffer_backend_shown(videocore_videobuffer_obj_t *self) { + return self->absolute_transform.scale != 0 && !self->hidden; +} + +void videocore_videobuffer_backend_stream_changed(videocore_videobuffer_obj_t *self) { + (void)self; +} + +void videocore_videobuffer_backend_source_changed(videocore_videobuffer_obj_t *self) { + (void)self; +} + +void videocore_videobuffer_backend_check_buffer_format(videocore_videobuffer_obj_t *self, + mp_int_t bits_per_pixel) { + // The scanline compositor checks the display's depth when it copies the frame. + (void)self; + (void)bits_per_pixel; +} + +// Called from interrupt context. A frame that landed where the video used to be was +// already added to the shared vacated_area by the common on_frame() dispatcher. +void videocore_videobuffer_backend_on_frame(videocore_videobuffer_obj_t *self, + const void *frame, const displayio_area_t *changed) { + if (frame != self->stream_base) { + return; + } + const displayio_area_t whole_frame = { + .x2 = self->source_width, + .y2 = self->source_height, + }; + if (changed == NULL) { + self->backend.frame_changed_area.area = whole_frame; + self->backend.frame_replaceable = true; + } else { + // Keep a bad source rect from dirtying other layers. + displayio_area_t clipped; + if (!displayio_area_compute_overlap(changed, &whole_frame, &clipped)) { + return; + } + displayio_area_union(&self->backend.frame_changed_area.area, &clipped, + &self->backend.frame_changed_area.area); + self->backend.frame_replaceable = false; + } + self->backend.frame_ready = true; +} + +void videocore_videobuffer_backend_forget_frame(videocore_videobuffer_obj_t *self) { + displayio_area_t empty = {}; + common_hal_mcu_disable_interrupts(); + self->backend.frame_ready = false; + self->backend.frame_changed_area.area = empty; + common_hal_mcu_enable_interrupts(); +} + +// Add the currently rendered rect to the dirty area so the next refresh repaints what a +// move leaves behind. Accumulates across moves. Does nothing while the video isn't shown. +void videocore_videobuffer_backend_dirty_current_area(videocore_videobuffer_obj_t *self) { + if (self->absolute_transform.scale == 0 || self->hidden) { + return; + } + displayio_area_union(&self->backend.ephemeral_dirty_area.area, &self->current_area.area, + &self->backend.ephemeral_dirty_area.area); +} + +void videocore_videobuffer_backend_validate_member(mp_obj_t member, mp_obj_t display, + mp_int_t rotation) { + const vectorio_draw_protocol_t *draw_protocol = mp_proto_get(MP_QSTR_protocol_draw, member); + if (draw_protocol == NULL) { + return; + } + mp_obj_t layer = draw_protocol->draw_get_protocol_self(member); + if (mp_obj_get_type(layer) != &videocore_videobuffer_type) { + return; + } + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + if (self->framebuffer_protocol == NULL) { + return; // Plain buffer targets are drawn, and rotated, by displayio. + } + videocore_videobuffer_validate_framebuffer_target(self, display, rotation); +} + +// The source pixel that a screen pixel shows along one axis. d and origin come +// from the transform. A mirrored axis (d < 0) runs the other way, so the source +// pixel ends at the origin edge instead of starting there. +static mp_int_t videocore_videobuffer_source_axis(int8_t d, mp_int_t screen, + mp_int_t origin, mp_int_t offset) { + if (d > 0) { + return screen - origin - offset; + } + return origin - screen - 1 - offset; +} + +// Bytes in one row of a source frame. 1-bit pixels are packed and each row is +// padded to a whole byte. (The shared code has the same helper; it is static there.) +static size_t videocore_videobuffer_source_row_bytes(videocore_videobuffer_obj_t *self) { + return ((size_t)self->source_width * self->source_bits_per_pixel + 7) / 8; +} + +// Draw protocol implementation. displayio copies plain-buffer frames into the +// display. Framebuffer-target frames are already in place; fill_area only +// copies them into displayio's scratch buffer when other layers overlap. + +static mp_obj_t videocore_videobuffer_draw_protocol_self(mp_obj_t protocol_container) { + return protocol_container; +} + +// Whether a layer above already claimed a pixel near the overlap. Checks whole words, which may +// include pixels between overlap rows or just outside the overlap. That can only make the caller +// take its slower path; it never misses a claimed pixel. +static bool videocore_videobuffer_overlap_claimed(const uint32_t *mask, const displayio_area_t *area, + const displayio_area_t *overlap) { + const size_t span_px = displayio_area_width(area); + const size_t first_px = (size_t)(overlap->y1 - area->y1) * span_px + (overlap->x1 - area->x1); + const size_t end_px = (size_t)(overlap->y2 - 1 - area->y1) * span_px + (overlap->x2 - area->x1); + for (size_t word = first_px / 32; word <= (end_px - 1) / 32; word++) { + if (mask[word] != 0) { + return true; + } + } + return false; +} + +// Copy a 1 bit per pixel plain buffer into a 1 bit display's buffer. Both are MSB first with +// 1 = white, the same as a monochrome EPaperDisplay's first RAM pass before any inversion. +// On a rotated display each screen pixel is walked back to its source pixel, as the 16 bit copy +// does. Whole bytes are copied when the source runs forwards along screen x, the rects line up +// and nothing above claimed these pixels. +static bool videocore_videobuffer_fill_area_1bit(videocore_videobuffer_obj_t *self, + const _displayio_colorspace_t *colorspace, const displayio_area_t *area, const displayio_area_t *overlap, + bool full_coverage, uint32_t *mask, uint32_t *buffer) { + // Every grayscale bit of a 1 bit pixel is that pixel, so this also serves each grayscale pass. + if (!colorspace->pixels_in_byte_share_row || !colorspace->reverse_pixels_in_byte || + !colorspace->grayscale) { + return false; + } + const size_t span_px = displayio_area_width(area); + const size_t width = displayio_area_width(overlap); + const size_t row_bytes = videocore_videobuffer_source_row_bytes(self); + const size_t x_in_area = overlap->x1 - area->x1; + const uint8_t *frame = self->stream_base; + uint8_t *out = (uint8_t *)buffer; + + // While the video isn't shown there is no transform and the orientations match. + const displayio_buffer_transform_t *t = &self->absolute_transform; + const bool shown = t->scale != 0; + const int8_t dx = shown ? t->dx : 1; + const int8_t dy = shown ? t->dy : 1; + const bool transpose = shown && t->transpose_xy; + const mp_int_t origin_x = shown ? t->x : 0; + const mp_int_t origin_y = shown ? t->y : 0; + const bool contiguous = !transpose && dx > 0; + // The first source column of every row when the source runs along screen x. + const mp_int_t first_x = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->x); + + const bool whole_bytes = contiguous && span_px % 8 == 0 && x_in_area % 8 == 0 && first_x % 8 == 0 && + width % 8 == 0 && !videocore_videobuffer_overlap_claimed(mask, area, overlap); + for (int16_t y = overlap->y1; y < overlap->y2; y++) { + const size_t px = (size_t)(y - area->y1) * span_px + x_in_area; + if (!contiguous) { + // Mirrored or transposed: walk each screen pixel back to its source pixel. Screen x + // steps along a source row, or down a source column on a transposed display. + mp_int_t source_x; + mp_int_t source_y; + if (transpose) { + source_x = videocore_videobuffer_source_axis(dy, y, origin_y, self->x); + source_y = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->y); + } else { + source_x = videocore_videobuffer_source_axis(dx, overlap->x1, origin_x, self->x); + source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); + } + for (size_t i = 0; i < width; i++) { + const size_t p = px + i; + if (mask[p / 32] & (1u << (p % 32))) { + continue; + } + const mp_int_t step = (mp_int_t)i * dx; + const mp_int_t fx = transpose ? source_x : source_x + step; + const mp_int_t fy = transpose ? source_y + step : source_y; + if (frame[(size_t)fy * row_bytes + (size_t)fx / 8] & (0x80 >> (fx % 8))) { + out[p / 8] |= 0x80 >> (p % 8); + } + mask[p / 32] |= 1u << (p % 32); + } + continue; + } + const size_t x_in_frame = first_x; + const mp_int_t source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); + const uint8_t *row = frame + (size_t)source_y * row_bytes; + if (whole_bytes) { + memcpy(out + px / 8, row + x_in_frame / 8, width / 8); + // Full coverage ends the layer walk, so filling the mask once below is enough. + if (!full_coverage) { + for (size_t p = px; p < px + width; p++) { + mask[p / 32] |= 1u << (p % 32); + } + } + continue; + } + for (size_t i = 0; i < width; i++) { + const size_t p = px + i; + if (mask[p / 32] & (1u << (p % 32))) { + continue; + } + const size_t f = x_in_frame + i; + if (row[f / 8] & (0x80 >> (f % 8))) { + out[p / 8] |= 0x80 >> (p % 8); + } + mask[p / 32] |= 1u << (p % 32); + } + } + if (whole_bytes && full_coverage) { + // A FramebufferDisplay writes nothing back when the mask is empty. + memset(mask, 0xff, (displayio_area_size(area) + 31) / 32 * sizeof(uint32_t)); + } + return full_coverage; +} + +static bool videocore_videobuffer_draw_fill_area(mp_obj_t self_in, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + // buffer isn't necessarily the framebuffer. It may be a scratch buffer. + if (self->stream_base == NULL || self->hidden) { + return false; + } + + displayio_area_t overlap; + if (!displayio_area_compute_overlap(area, &self->current_area.area, &overlap)) { + return false; + } + const bool full_coverage = displayio_area_equal(area, &overlap); + const uint16_t span_px = displayio_area_width(area); + const size_t overlap_width = displayio_area_width(&overlap); + + if (self->framebuffer_protocol != NULL) { + // The source already wrote these pixels into the framebuffer. If + // nothing else covers this area, claim no pixels so the display skips + // its write-back. Otherwise copy the video's unclaimed pixels into + // the scratch buffer and claim them, since the scratch buffer is + // written back over the framebuffer. + const size_t bytes_per_pixel = self->source_bits_per_pixel / 8; + const mp_int_t fb_width = FB_GETTER(get_width, 0); + const mp_int_t row_stride = FB_GETTER(get_row_stride, fb_width * self->source_bits_per_pixel / 8); + const uint8_t *frame = (const uint8_t *)self->stream_base; + uint8_t *out = (uint8_t *)buffer; + + if (full_coverage && !videocore_videobuffer_overlap_claimed(mask, area, &overlap)) { + // Sole coverer: nothing was composed, so the driver skips the copy. + return true; + } + + for (int16_t y = overlap.y1; y < overlap.y2; y++) { + const size_t px = (size_t)(y - area->y1) * span_px + (overlap.x1 - area->x1); + const int16_t fy = y - self->current_area.area.y1; + const uint8_t *row = frame + (size_t)fy * row_stride + + (size_t)(overlap.x1 - self->current_area.area.x1) * bytes_per_pixel; + size_t i = 0; + while (i < overlap_width) { + // Skip pixels a higher layer already claimed or drew. + while (i < overlap_width && (mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { + i++; + } + const size_t start = i; + while (i < overlap_width && !(mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { + mask[(px + i) / 32] |= 1u << ((px + i) % 32); + i++; + } + if (i > start) { + memcpy( + out + (px + start) * bytes_per_pixel, + row + start * bytes_per_pixel, + (i - start) * bytes_per_pixel); + } + } + } + return full_coverage; + } + + if (colorspace->depth == 1 && self->source_bits_per_pixel == 1) { + return videocore_videobuffer_fill_area_1bit(self, colorspace, area, &overlap, full_coverage, mask, buffer); + } + + // Raw buffer target: copy the pixels, with no color conversion: the + // display depth must match the video's pixel format byte for byte. + if (colorspace->depth != self->source_bits_per_pixel || colorspace->depth != 16) { + return false; + } + + const size_t bytes_per_pixel = self->source_bits_per_pixel / 8; + const uint8_t *frame = (const uint8_t *)self->stream_base; + // displayio renders in screen coordinates (the framebuffer's physical + // ones) while the frame is stored in the source's own orientation, so + // walk from each screen pixel back to its source pixel. A plain buffer + // target is rotated with the display; while the video isn't shown there + // is no transform and the orientations match. + const displayio_buffer_transform_t *t = &self->absolute_transform; + const bool shown = t->scale != 0; + const int8_t dx = shown ? t->dx : 1; + const int8_t dy = shown ? t->dy : 1; + const bool transpose = shown && t->transpose_xy; + const mp_int_t origin_x = shown ? t->x : 0; + const mp_int_t origin_y = shown ? t->y : 0; + // Source pixels run along screen x only when the frame isn't transposed, + // and forwards only when screen x isn't mirrored. + const bool contiguous = !transpose && dx > 0; + + for (int16_t y = overlap.y1; y < overlap.y2; y++) { + const size_t px = (size_t)(y - area->y1) * span_px + (overlap.x1 - area->x1); + uint16_t *out = (uint16_t *)buffer + px; + // The first source pixel this row shows. The frame's column and row + // follow the transform, which swaps them on a transposed display. + mp_int_t source_x; + mp_int_t source_y; + if (transpose) { + source_x = videocore_videobuffer_source_axis(dy, y, origin_y, self->x); + source_y = videocore_videobuffer_source_axis(dx, overlap.x1, origin_x, self->y); + } else { + source_x = videocore_videobuffer_source_axis(dx, overlap.x1, origin_x, self->x); + source_y = videocore_videobuffer_source_axis(dy, y, origin_y, self->y); + } + // Where the next screen x leads: the next source column, or on a + // transposed display the next source row. + const ptrdiff_t step = transpose + ? (ptrdiff_t)dx * (ptrdiff_t)self->source_width * (ptrdiff_t)bytes_per_pixel + : (ptrdiff_t)dx * (ptrdiff_t)bytes_per_pixel; + const uint8_t *in = frame + + ((size_t)source_y * self->source_width + (size_t)source_x) * bytes_per_pixel; + if (contiguous) { + size_t i = 0; + while (i < overlap_width) { + // Skip pixels a higher layer already claimed or drew. + while (i < overlap_width && (mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { + i++; + } + const size_t start = i; + while (i < overlap_width && !(mask[(px + i) / 32] & (1u << ((px + i) % 32)))) { + mask[(px + i) / 32] |= 1u << ((px + i) % 32); + i++; + } + if (i > start) { + memcpy( + out + start, + in + start * bytes_per_pixel, + (i - start) * bytes_per_pixel); + if (colorspace->reverse_bytes_in_word) { + for (size_t j = start; j < i; j++) { + out[j] = __builtin_bswap16(out[j]); + } + } + } + } + } else { + // Mirrored or transposed: the source pixels between screen pixels + // aren't next to each other, so copy them one by one. + for (size_t i = 0; i < overlap_width; i++) { + if (mask[(px + i) / 32] & (1u << ((px + i) % 32))) { + continue; + } + mask[(px + i) / 32] |= 1u << ((px + i) % 32); + uint16_t value; + memcpy(&value, in + (ptrdiff_t)i * step, bytes_per_pixel); + if (colorspace->reverse_bytes_in_word) { + value = __builtin_bswap16(value); + } + out[i] = value; + } + } + } + return full_coverage; +} + +static bool videocore_videobuffer_draw_get_dirty_area(mp_obj_t self_in, displayio_area_t *out_area) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + out_area->x1 = out_area->x2; + displayio_area_union(&self->backend.ephemeral_dirty_area.area, &self->current_area.area, out_area); + return true; +} + +static void videocore_videobuffer_draw_update_transform(mp_obj_t self_in, displayio_buffer_transform_t *group_transform) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + // Remember where the video was so a move can repaint the pixels it + // vacates. The area is only valid while the video is shown; a layer being + // attached or detached is handled by the display's full refresh. + displayio_area_t previous_area = self->current_area.area; + const bool was_shown = self->absolute_transform.scale != 0 && !self->hidden; + // A group that isn't attached to a display has no transform, and its + // update_transform clears the absolute transform we receive. A real + // (shown) transform always has scale >= 1. + if (group_transform == NULL || group_transform->scale == 0) { + displayio_buffer_transform_t no_transform = {}; + self->absolute_transform = no_transform; + } else { + // Validate the transform. A framebuffer target streams rows straight + // into the framebuffer's memory, so it can't turn with a rotated + // display. This is also checked when a group becomes the display's + // root group, but rotation can change afterwards. + if (self->framebuffer_protocol != NULL && + (group_transform->mirror_x || group_transform->mirror_y || + group_transform->transpose_xy)) { + mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer can't stream into a rotated display")); + } + // A plain buffer target is copied into the display, so a rotated + // display works: its transform mirrors and/or transposes the logical + // position, which the copy applies to the frame. Neither can scale. + if (group_transform->scale != 1 || + group_transform->dx == 0 || group_transform->dy == 0) { + mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer does not support transforms")); + } + self->absolute_transform = *group_transform; + } + + // A framebuffer target starts or stops with whether it is shown (the + // absolute transform is known now). This runs from Python contexts, so a + // (re)start may raise. + videocore_videobuffer_update_stream(self); + + videocore_videobuffer_update_area(self); + + // Moved by a containing Group (or a transform change): dirty the rect it + // used to occupy so the refresh repaints those pixels instead of leaving + // the previous frame there. + if (was_shown && self->absolute_transform.scale != 0 && + !displayio_area_equal(&previous_area, &self->current_area.area)) { + displayio_area_union( + &self->backend.ephemeral_dirty_area.area, &previous_area, &self->backend.ephemeral_dirty_area.area); + if (self->framebuffer_protocol == NULL) { + // Draw the last frame at its new position. + videocore_videobuffer_backend_dirty_current_area(self); + } else if (!self->playing) { + // A repaint overwrites an ended stream's last frame. + self->stream_base = NULL; + } + } + + // Framebuffer targets stream straight into the buffer: move the + // destination along with the shift. Best effort, since this can't raise: + // the shift may land the start mid-row, which won't look right but is + // obvious; the frame is capped to stay completely within the buffer. + if (self->framebuffer_protocol != NULL) { + videocore_videobuffer_move_dest(self); + } +} + +static void videocore_videobuffer_draw_finish_refresh(mp_obj_t self_in) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + displayio_area_t empty = {}; + self->backend.ephemeral_dirty_area.area = empty; + self->backend.ephemeral_dirty_area.next = NULL; + // A frame that landed during the refresh waits for the next one, unless its + // successor repaints everything anyway. + if (self->backend.frame_replaceable) { + videocore_videobuffer_forget_frame(self); + } + videocore_videobuffer_end_stream(self); +} + +static displayio_area_t *videocore_videobuffer_draw_get_refresh_areas(mp_obj_t self_in, displayio_area_t *tail) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + // Take what landed so far in one step: a frame landing from here on waits + // for the next refresh. + displayio_area_t empty = {}; + common_hal_mcu_disable_interrupts(); + displayio_area_t vacated = self->vacated_area.area; + const bool frame_ready = self->backend.frame_ready; + self->backend.frame_dirty_area.area = self->backend.frame_changed_area.area; + self->vacated_area.area = empty; + self->vacated_area.next = NULL; + self->backend.frame_ready = false; + self->backend.frame_changed_area.area = empty; + common_hal_mcu_enable_interrupts(); + // Frames that landed where the video used to be, possibly over the repaint + // of that area: repaint there again. + displayio_area_union(&self->backend.ephemeral_dirty_area.area, &vacated, &self->backend.ephemeral_dirty_area.area); + // Always repaint the area the video vacated, even once it is hidden or + // stopped, so its last frame doesn't stay on the target. + if (!displayio_area_empty(&self->backend.ephemeral_dirty_area.area)) { + // The video moved or was hidden: repaint with the lower layers. + tail = videocore_area_link(&self->backend.ephemeral_dirty_area, tail); + } + if (self->stream_base == NULL || self->paused || self->hidden || self->stale_size) { + return tail; + } + if (frame_ready) { + // New frame: copy the part that changed. A plain buffer target is + // rotated as it is copied, so map the changed source pixels to the + // screen; a framebuffer target's frame lands unrotated at the current + // area, so shift them there. + if (self->framebuffer_protocol == NULL) { + displayio_area_t changed = self->backend.frame_dirty_area.area; + videocore_videobuffer_source_rect(self, &changed, &self->backend.frame_dirty_area); + } else { + displayio_area_shift( + &self->backend.frame_dirty_area.area, self->current_area.area.x1, self->current_area.area.y1); + } + tail = videocore_area_link(&self->backend.frame_dirty_area, tail); + } + return tail; +} + +static void videocore_videobuffer_draw_set_dirty(mp_obj_t self_in) { + (void)self_in; +} + +static void videocore_videobuffer_draw_set_hidden(mp_obj_t self_in, bool hidden) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + if (self->hidden == hidden) { + return; + } + videocore_videobuffer_end_stream(self); + self->hidden = hidden; + // Repaint what the video showed or vacated. + self->backend.ephemeral_dirty_area.area = self->current_area.area; + if (!hidden && self->playing) { + // The source normally can't change while the sensor stays streaming, + // but validate its sizing again in case it did: if it no longer + // matches, don't raise; simply stop showing the stream until stop and + // play again. + const videosource_p_t *protocol = mp_proto_get(MP_QSTR_protocol_videosource, self->source); + if (protocol != NULL && + (protocol->get_width(self->source) != self->source_width || + protocol->get_height(self->source) != self->source_height || + protocol->get_bits_per_pixel(self->source) != self->source_bits_per_pixel)) { + self->stale_size = true; + } + } + // Hiding or unhiding a framebuffer target pauses or resumes its source. + videocore_videobuffer_update_stream(self); +} + +static bool videocore_videobuffer_draw_set_in_group(mp_obj_t self_in, bool in_group) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(self_in); + bool was_in_group = self->in_group; + self->in_group = in_group; + return was_in_group; +} + +static vectorio_draw_protocol_impl_t videocore_videobuffer_draw_protocol_impl = { + .draw_fill_area = videocore_videobuffer_draw_fill_area, + .draw_get_dirty_area = videocore_videobuffer_draw_get_dirty_area, + .draw_update_transform = videocore_videobuffer_draw_update_transform, + .draw_finish_refresh = videocore_videobuffer_draw_finish_refresh, + .draw_get_refresh_areas = videocore_videobuffer_draw_get_refresh_areas, + .draw_set_dirty = videocore_videobuffer_draw_set_dirty, + .draw_set_hidden = videocore_videobuffer_draw_set_hidden, + .draw_set_in_group = videocore_videobuffer_draw_set_in_group, +}; + +const vectorio_draw_protocol_t videocore_videobuffer_draw_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_draw) + .draw_get_protocol_self = videocore_videobuffer_draw_protocol_self, + .draw_protocol_impl = &videocore_videobuffer_draw_protocol_impl, +}; diff --git a/shared-module/videocore/legacy/VideoBuffer.h b/shared-module/videocore/legacy/VideoBuffer.h new file mode 100644 index 00000000000..5b134491a31 --- /dev/null +++ b/shared-module/videocore/legacy/VideoBuffer.h @@ -0,0 +1,67 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" + +#include "shared-bindings/vectorio/__init__.h" // draw protocol +#include "shared-module/displayio/area.h" + +struct _videocore_videobuffer_obj_t; + +// Legacy scanline compositor state for a VideoBuffer. +// +// Frames land in the source's destination directly. The draw protocol reports where the +// video is now (current_area, shared with the stream code) and where it has been since the +// last refresh (ephemeral_dirty_area) so displayio repaints exactly what changed. A frame +// that lands at a destination the video no longer occupies is reported through the shared +// vacated_area. +typedef struct { + // Set from interrupt context when the source lands a frame in the destination; cleared + // when the refresh takes the changed area. + volatile bool frame_ready; + // What changed in the frames landed since the last refresh, in source coordinates. + // Grown with frame_ready and cleared with it. + videocore_area_t frame_changed_area; + // The last frame landed passed NULL, so the next one repaints everything. + volatile bool frame_replaceable; + // frame_changed_area in screen coordinates, as handed to the refresh. + videocore_area_t frame_dirty_area; + // Rect the video has occupied since the last refresh, for repainting what it leaves + // behind when it moves or is hidden. + videocore_area_t ephemeral_dirty_area; +} videocore_videobuffer_backend_t; + +// Draw protocol that lets a VideoBuffer be added to a displayio Group. +// displayio copies plain-buffer frames into the display; framebuffer-target +// frames are already in place, so displayio only marks their rows. +extern const vectorio_draw_protocol_t videocore_videobuffer_draw_protocol; + +// Chain node's area in front of tail and return the new list head. The scanline compositor +// walks displayio_area_t.next, which LVGL's lv_area_t doesn't have, so the link lives in +// the wrapper. +static inline displayio_area_t *videocore_area_link(videocore_area_t *node, displayio_area_t *tail) { + node->area.next = tail; + return &node->area; +} + +// Backend hooks called by the shared stream code. See shared-module/videocore/VideoBuffer.c. +void videocore_videobuffer_backend_construct(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_on_frame(struct _videocore_videobuffer_obj_t *self, + const void *frame, const displayio_area_t *changed); +void videocore_videobuffer_backend_forget_frame(struct _videocore_videobuffer_obj_t *self); +bool videocore_videobuffer_backend_shown(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_stream_changed(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_source_changed(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_dirty_current_area(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_check_buffer_format(struct _videocore_videobuffer_obj_t *self, + mp_int_t bits_per_pixel); +void videocore_videobuffer_backend_validate_member(mp_obj_t member, mp_obj_t display, + mp_int_t rotation); diff --git a/shared-module/videocore/lvgl/VideoBuffer.c b/shared-module/videocore/lvgl/VideoBuffer.c new file mode 100644 index 00000000000..4255cd6ca21 --- /dev/null +++ b/shared-module/videocore/lvgl/VideoBuffer.c @@ -0,0 +1,490 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +// LVGL implementation of videocore.VideoBuffer. See lvgl/VideoBuffer.h and +// shared-module/displayio/lvgl/lvgl_protocol.h. +// +// A VideoBuffer is one lv_image that samples the source's current destination +// directly: the user's buffer for a plain-buffer target, or the framebuffer +// band a framebuffer target streams into. Frames are written in place, so a +// landed frame only has to invalidate the image; there is no copy and no +// second render path. Invalidation is deferred out of the source's interrupt +// context to displayio_lvgl_video_background(), which runs on the +// CircuitPython thread at every supervisor tick. + +#include "shared-module/videocore/VideoBuffer.h" + +#include + +#include "py/gc.h" +#include "py/objtype.h" +#include "py/runtime.h" + +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/videocore/VideoBuffer.h" +#include "shared-module/displayio/lvgl/lvgl_internal.h" +#include "shared-module/videocore/__init__.h" + +#include "misc/cache/instance/lv_image_cache.h" + +#if CIRCUITPY_VIDEOCORE && CIRCUITPY_DISPLAYIO_LVGL + +// Attached mirrors. The list lives in port memory, so displayio_gc_collect() +// roots it explicitly. +static videocore_videobuffer_obj_t *lvgl_videos = NULL; + +static void _register(videocore_videobuffer_obj_t *self) { + if (self->backend.lv_registered) { + return; + } + self->backend.lv_next = lvgl_videos; + lvgl_videos = self; + self->backend.lv_registered = true; +} + +static void _unregister(videocore_videobuffer_obj_t *self) { + if (!self->backend.lv_registered) { + return; + } + for (videocore_videobuffer_obj_t **link = &lvgl_videos; *link != NULL; link = &(*link)->backend.lv_next) { + if (*link == self) { + *link = self->backend.lv_next; + break; + } + } + self->backend.lv_next = NULL; + self->backend.lv_registered = false; +} + +// The LVGL format of the pixels the source writes. +static lv_color_format_t _video_color_format(videocore_videobuffer_obj_t *self) { + switch (self->source_bits_per_pixel) { + case 16: + if (self->framebuffer_protocol != NULL && + self->framebuffer_protocol->get_reverse_pixels_in_word != NULL && + self->framebuffer_protocol->get_reverse_pixels_in_word(self->framebuffer)) { + return LV_COLOR_FORMAT_RGB565_SWAPPED; + } + return LV_COLOR_FORMAT_RGB565; + case 24: + return LV_COLOR_FORMAT_RGB888; + case 32: + return LV_COLOR_FORMAT_XRGB8888; + default: + return LV_COLOR_FORMAT_UNKNOWN; + } +} + +// Bytes between rows of the destination the mirror samples. +static uint32_t _video_stride(videocore_videobuffer_obj_t *self) { + if (self->framebuffer_protocol != NULL) { + return self->fb_row_stride; + } + return (uint32_t)self->source_width * self->source_bits_per_pixel / 8; +} + +// lv_obj_set_pos() only records a style that a later layout pass applies, and +// lv_obj_move_to() leaves that style behind, so an unrelated size change would +// snap the object back. Set both: the coordinates are right immediately and +// nothing can undo them. +static void _place(lv_obj_t *obj, int32_t x, int32_t y) { + lv_obj_set_style_x(obj, x, 0); + lv_obj_set_style_y(obj, y, 0); + lv_obj_move_to(obj, x, y); +} + +// Drop any cached decode of the current source. A descriptor that was never +// handed to LVGL has no cache entry. +static void _drop_cached_source(videocore_videobuffer_obj_t *self) { + if (self->backend.lv_dsc.header.magic == LV_IMAGE_HEADER_MAGIC) { + lv_image_cache_drop(&self->backend.lv_dsc); + } +} + +// Repaint the area an object covered. Used before moving or hiding it, when +// invalidating the object itself no longer covers where it was. +static void _invalidate_screen_area(lv_obj_t *obj, const lv_area_t *area) { + lv_obj_t *screen = lv_obj_get_screen(obj); + if (screen != NULL) { + lv_obj_invalidate_area(screen, area); + } +} + +// Point the mirror at the current destination and place it where the video is. +// Called under the LVGL lock from every state change. +static void _sync_mirror(videocore_videobuffer_obj_t *self) { + if (self->backend.lv_obj == NULL) { + return; + } + + const uint8_t *data = (const uint8_t *)self->stream_base; + lv_color_format_t cf = _video_color_format(self); + const bool visible = data != NULL && cf != LV_COLOR_FORMAT_UNKNOWN && + self->source_width > 0 && self->source_height > 0 && !self->hidden; + + lv_area_t old_coords; + lv_obj_get_coords(self->backend.lv_obj, &old_coords); + const bool was_hidden = lv_obj_has_flag(self->backend.lv_obj, LV_OBJ_FLAG_HIDDEN); + + if (!visible) { + if (!was_hidden) { + // Repaint the last frame with whatever is underneath it before it + // goes away. + lv_obj_invalidate(self->backend.lv_obj); + lv_obj_add_flag(self->backend.lv_obj, LV_OBJ_FLAG_HIDDEN); + } else if (old_coords.x1 != 0 || old_coords.y1 != 0) { + // A stopped or moved video may have left pixels on the screen + // while its mirror was already hidden. + _invalidate_screen_area(self->backend.lv_obj, &old_coords); + } + return; + } + + const uint32_t stride = _video_stride(self); + const uint32_t row_bytes = (uint32_t)self->source_width * self->source_bits_per_pixel / 8; + const uint32_t data_size = stride * (self->source_height - 1) + row_bytes; + + if (data != self->backend.lv_data || cf != self->backend.lv_cf || + self->source_width != self->backend.lv_width || self->source_height != self->backend.lv_height || + stride != self->backend.lv_stride) { + // The storage or the way it is read changed. Drop cached decodes of the + // old view; per-frame pixel writes do not need this. + _drop_cached_source(self); + lv_memzero(&self->backend.lv_dsc, sizeof(self->backend.lv_dsc)); + self->backend.lv_dsc.header.magic = LV_IMAGE_HEADER_MAGIC; + self->backend.lv_dsc.header.cf = cf; + self->backend.lv_dsc.header.w = self->source_width; + self->backend.lv_dsc.header.h = self->source_height; + self->backend.lv_dsc.header.stride = stride; + self->backend.lv_dsc.data = data; + self->backend.lv_dsc.data_size = data_size; + self->backend.lv_data = data; + self->backend.lv_cf = cf; + self->backend.lv_width = self->source_width; + self->backend.lv_height = self->source_height; + self->backend.lv_stride = stride; + 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; + 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); + } + _place(self->backend.lv_obj, x, y); + } + if (lv_obj_get_width(self->backend.lv_obj) != self->source_width || + lv_obj_get_height(self->backend.lv_obj) != self->source_height) { + if (!was_hidden) { + lv_obj_invalidate(self->backend.lv_obj); + } + lv_obj_set_size(self->backend.lv_obj, self->source_width, self->source_height); + } + if (was_hidden) { + lv_obj_clear_flag(self->backend.lv_obj, LV_OBJ_FLAG_HIDDEN); + } + lv_obj_invalidate(self->backend.lv_obj); +} + +void videocore_videobuffer_backend_construct(videocore_videobuffer_obj_t *self) { + self->backend.lv_obj = NULL; + self->backend.lv_data = NULL; + self->backend.lv_cf = LV_COLOR_FORMAT_UNKNOWN; + self->backend.lv_width = 0; + self->backend.lv_height = 0; + self->backend.lv_stride = 0; + self->backend.lv_next = NULL; + self->backend.lv_registered = false; + self->backend.lv_pending = false; + lv_memzero(&self->backend.lv_dsc, sizeof(self->backend.lv_dsc)); +} + +bool videocore_videobuffer_backend_shown(videocore_videobuffer_obj_t *self) { + return self->absolute_transform.scale != 0 && !self->hidden && + self->backend.lv_obj != NULL; +} + +void videocore_videobuffer_backend_check_buffer_format(videocore_videobuffer_obj_t *self, + mp_int_t bits_per_pixel) { + // LVGL samples the buffer itself, so it has to know the pixel format. + (void)self; + if (bits_per_pixel != 16 && bits_per_pixel != 24 && bits_per_pixel != 32) { + mp_raise_ValueError(MP_ERROR_TEXT("video format must be 16, 24 or 32 bits per pixel")); + } +} + +void videocore_videobuffer_backend_dirty_current_area(videocore_videobuffer_obj_t *self) { + // LVGL invalidates the mirror itself when it moves. + (void)self; +} + +// Called from interrupt context. The common dispatcher already added a frame that landed at +// an old destination to vacated_area. +void videocore_videobuffer_backend_on_frame(videocore_videobuffer_obj_t *self, + const void *frame, const displayio_area_t *changed) { + (void)frame; + (void)changed; + self->backend.lv_pending = true; +} + +void videocore_videobuffer_backend_forget_frame(videocore_videobuffer_obj_t *self) { + common_hal_mcu_disable_interrupts(); + // Keep a pending repaint of a vacated area: an in-flight frame still has to be erased + // even though the frame itself no longer needs drawing. + if (displayio_area_empty(&self->vacated_area.area)) { + self->backend.lv_pending = false; + } + common_hal_mcu_enable_interrupts(); +} + +void videocore_videobuffer_backend_validate_member(mp_obj_t member, mp_obj_t display, + mp_int_t rotation) { + const lvgl_protocol_t *protocol = mp_proto_get(MP_QSTR_protocol_lvgl, member); + if (protocol == NULL) { + return; + } + mp_obj_t layer = protocol->protocol_self != NULL ? protocol->protocol_self(member) : member; + if (mp_obj_get_type(layer) != &videocore_videobuffer_type) { + return; + } + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + if (self->framebuffer_protocol == NULL) { + return; // Plain buffer targets are sampled by LVGL and rotate with the display. + } + videocore_videobuffer_validate_framebuffer_target(self, display, rotation); +} + +void videocore_videobuffer_backend_source_changed(videocore_videobuffer_obj_t *self) { + if (self->backend.lv_obj == NULL) { + // Not attached: there is no mirror to update. attach() syncs from the + // current state, including a frame that already landed. + return; + } + displayio_lvgl_lock(); + _sync_mirror(self); + displayio_lvgl_unlock(); +} + +void videocore_videobuffer_backend_stream_changed(videocore_videobuffer_obj_t *self) { + if (self->backend.lv_obj == NULL) { + return; + } + displayio_lvgl_lock(); + _sync_mirror(self); + displayio_lvgl_unlock(); +} + +// protocol_lvgl implementation. + +static lv_obj_t *_video_protocol_root_obj(mp_obj_t layer) { + return ((videocore_videobuffer_obj_t *)MP_OBJ_TO_PTR(layer))->backend.lv_obj; +} + +static void _video_protocol_attach(mp_obj_t layer, lv_obj_t *parent) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + if (parent == NULL) { + return; + } + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + // A framebuffer target's pixels land in the framebuffer's physical memory, + // so it can only be shown on an unrotated display. LVGL rotates the whole + // scene at flush time, which would make the image and its pixels disagree. + if (self->framebuffer_protocol != NULL) { + lv_display_t *display = lv_obj_get_display(parent); + if (display != NULL && lv_display_get_rotation(display) != LV_DISPLAY_ROTATION_0) { + displayio_lvgl_unlock_with_cleanup(); + mp_raise_ValueError(MP_ERROR_TEXT("VideoBuffer can't stream into a rotated display")); + } + } + if (self->backend.lv_obj == NULL) { + self->backend.lv_obj = lv_image_create(parent); + if (self->backend.lv_obj == NULL) { + displayio_lvgl_unlock_with_cleanup(); + return; + } + lv_obj_clear_flag(self->backend.lv_obj, LV_OBJ_FLAG_CLICKABLE); + lv_obj_clear_flag(self->backend.lv_obj, LV_OBJ_FLAG_SCROLLABLE); + lv_image_set_inner_align(self->backend.lv_obj, LV_IMAGE_ALIGN_TOP_LEFT); + lv_image_set_pivot(self->backend.lv_obj, 0, 0); + lv_image_set_antialias(self->backend.lv_obj, false); + lv_image_set_scale(self->backend.lv_obj, LV_SCALE_NONE); + _register(self); + } else if (lv_obj_get_parent(self->backend.lv_obj) != parent) { + lv_obj_set_parent(self->backend.lv_obj, parent); + } + // The transform may already be known from when the member joined the + // group; apply it now that there is a mirror and a display to drive. + videocore_videobuffer_update_stream(self); + _sync_mirror(self); + displayio_lvgl_unlock_with_cleanup(); +} + +static void _video_protocol_detach(mp_obj_t layer) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + displayio_lvgl_lock(); + _unregister(self); + if (self->backend.lv_obj != NULL) { + lv_obj_delete(self->backend.lv_obj); + self->backend.lv_obj = NULL; + } + self->backend.lv_data = NULL; + self->backend.lv_cf = LV_COLOR_FORMAT_UNKNOWN; + self->backend.lv_width = 0; + self->backend.lv_height = 0; + self->backend.lv_stride = 0; + _drop_cached_source(self); + lv_memzero(&self->backend.lv_dsc, sizeof(self->backend.lv_dsc)); + // No mirror means the video isn't shown, which pauses a framebuffer target + // until it is attached again. + videocore_videobuffer_update_stream(self); + displayio_lvgl_unlock(); +} + +static bool _video_protocol_set_transform(mp_obj_t layer, int32_t abs_x, int32_t abs_y, + uint16_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); + displayio_buffer_transform_t transform = { + .x = abs_x, + .y = abs_y, + .dx = 1, + .dy = 1, + .scale = scale, + }; + self->absolute_transform = transform; + videocore_videobuffer_update_area(self); + // May resume a paused framebuffer target, so it may raise. + videocore_videobuffer_update_stream(self); + // A playing framebuffer target follows the group's translation by moving + // its DMA destination with it. + if (self->framebuffer_protocol != NULL) { + videocore_videobuffer_move_dest(self); + } + _sync_mirror(self); + displayio_lvgl_unlock_with_cleanup(); + return true; +} + +static void _video_protocol_set_hidden_by_parent(mp_obj_t layer, bool hidden) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + if (self->hidden == hidden) { + return; + } + self->hidden = hidden; + nlr_jump_callback_node_t cleanup; + displayio_lvgl_lock_with_cleanup(&cleanup); + videocore_videobuffer_update_stream(self); + _sync_mirror(self); + displayio_lvgl_unlock_with_cleanup(); +} + +static bool _video_protocol_set_in_group(mp_obj_t layer, bool in_group) { + videocore_videobuffer_obj_t *self = MP_OBJ_TO_PTR(layer); + bool was_in_group = self->in_group; + self->in_group = in_group; + return was_in_group; +} + +// A Python subclass of VideoBuffer is an mp_obj_instance_t that holds the native VideoBuffer in +// subobj[0]. Resolve it here so the methods above never see the instance. +static mp_obj_t _video_protocol_self(mp_obj_t layer) { + mp_obj_t native = mp_obj_cast_to_native_base(layer, + MP_OBJ_FROM_PTR(&videocore_videobuffer_type)); + if (native == MP_OBJ_NULL) { + return layer; + } + mp_obj_assert_native_inited(native); + return native; +} + +const lvgl_protocol_t videocore_videobuffer_lvgl_protocol = { + MP_PROTO_IMPLEMENT(MP_QSTR_protocol_lvgl) + .protocol_self = _video_protocol_self, + .root_obj = _video_protocol_root_obj, + .attach = _video_protocol_attach, + .detach = _video_protocol_detach, + .set_transform = _video_protocol_set_transform, + .set_hidden_by_parent = _video_protocol_set_hidden_by_parent, + .set_in_group = _video_protocol_set_in_group, +}; + +void displayio_lvgl_video_background(void) { + bool pending = false; + for (videocore_videobuffer_obj_t *self = lvgl_videos; self != NULL; self = self->backend.lv_next) { + if (self->backend.lv_pending) { + pending = true; + break; + } + } + if (!pending) { + return; + } + displayio_lvgl_lock(); + for (videocore_videobuffer_obj_t *self = lvgl_videos; self != NULL; self = self->backend.lv_next) { + if (self->backend.lv_obj == NULL) { + continue; + } + displayio_area_t empty = {}; + displayio_area_t vacated; + // Take what landed so far in one step: a frame landing from here on + // waits for the next background pass. + common_hal_mcu_disable_interrupts(); + if (!self->backend.lv_pending) { + common_hal_mcu_enable_interrupts(); + continue; + } + vacated = self->vacated_area.area; + self->vacated_area.area = empty; + self->vacated_area.next = NULL; + self->backend.lv_pending = false; + common_hal_mcu_enable_interrupts(); + + if (!displayio_area_empty(&vacated)) { + // A frame in flight landed where the video used to be. Repaint that + // area so the stale frame doesn't stay on top of the new layout. + // This has to happen even while hidden: the frame can land after + // the last repaint of the area. + _invalidate_screen_area(self->backend.lv_obj, &vacated); + } + if (self->hidden) { + continue; + } + // The image already points at the destination the frame landed in, so + // re-rendering the image samples the new pixels. + lv_obj_invalidate(self->backend.lv_obj); + } + displayio_lvgl_unlock(); +} + +void displayio_lvgl_video_gc_collect(void) { + gc_collect_root((void **)&lvgl_videos, 1); +} + +bool displayio_lvgl_video_can_rotate(lv_display_t *display, lv_display_rotation_t rotation) { + if (rotation == LV_DISPLAY_ROTATION_0) { + return true; + } + for (videocore_videobuffer_obj_t *self = lvgl_videos; self != NULL; self = self->backend.lv_next) { + if (self->framebuffer_protocol != NULL && self->backend.lv_obj != NULL && + lv_obj_get_display(self->backend.lv_obj) == display) { + return false; + } + } + return true; +} + +#endif // CIRCUITPY_VIDEOCORE && CIRCUITPY_DISPLAYIO_LVGL diff --git a/shared-module/videocore/lvgl/VideoBuffer.h b/shared-module/videocore/lvgl/VideoBuffer.h new file mode 100644 index 00000000000..aef6fc0f783 --- /dev/null +++ b/shared-module/videocore/lvgl/VideoBuffer.h @@ -0,0 +1,72 @@ +// This file is part of the CircuitPython project: https://circuitpython.org +// +// SPDX-FileCopyrightText: Copyright (c) 2026 Scott Shawcroft for Adafruit Industries +// +// SPDX-License-Identifier: MIT + +#pragma once + +#include +#include + +#include "py/obj.h" + +#include "shared-module/displayio/area.h" + +struct _videocore_videobuffer_obj_t; +#include "shared-module/displayio/lvgl/lvgl_protocol.h" + +#include "lvgl.h" + +// LVGL compositor state for a VideoBuffer. +// +// The video is one lv_image that samples the source's current destination directly: the +// user's buffer for a plain-buffer target, or the framebuffer band a framebuffer target +// streams into. Frames are rewritten in place, so a landed frame only invalidates the +// image; there is no copy. The shared stream code reports frame arrivals and stream +// changes through the backend hooks below. +typedef struct { + // The image view of the current destination. + lv_obj_t *lv_obj; + lv_image_dsc_t lv_dsc; + // Descriptor contents last pushed into LVGL, so a move can drop the image cache and + // repoint the view without doing either on every frame. + const uint8_t *lv_data; + lv_color_format_t lv_cf; + uint16_t lv_width; + uint16_t lv_height; + uint16_t lv_stride; + // The compositor's list of attached mirrors, rooted during GC. + struct _videocore_videobuffer_obj_t *lv_next; + bool lv_registered; + // Set from interrupt context when a frame (or a vacated frame) needs an invalidation. + // Lets the background tick skip the LVGL lock when idle. + volatile bool lv_pending; +} videocore_videobuffer_backend_t; + +// How the LVGL compositor attaches and positions a VideoBuffer. See +// displayio/lvgl/lvgl_protocol.h. +extern const lvgl_protocol_t videocore_videobuffer_lvgl_protocol; + +// Backend hooks called by the shared stream code. See shared-module/videocore/VideoBuffer.c. +void videocore_videobuffer_backend_construct(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_on_frame(struct _videocore_videobuffer_obj_t *self, + const void *frame, const displayio_area_t *changed); +void videocore_videobuffer_backend_forget_frame(struct _videocore_videobuffer_obj_t *self); +bool videocore_videobuffer_backend_shown(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_stream_changed(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_source_changed(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_dirty_current_area(struct _videocore_videobuffer_obj_t *self); +void videocore_videobuffer_backend_check_buffer_format(struct _videocore_videobuffer_obj_t *self, + mp_int_t bits_per_pixel); +void videocore_videobuffer_backend_validate_member(mp_obj_t member, mp_obj_t display, + mp_int_t rotation); + +// Invalidate the screen rects frames landed in since the last call. Runs from the +// supervisor tick and at the start of a refresh, on the CircuitPython thread. +void displayio_lvgl_video_background(void); +// Root the attached mirrors during GC. They live in a port-memory list. +void displayio_lvgl_video_gc_collect(void); +// False when a framebuffer target is attached to display and can't stream into it at the +// given rotation. +bool displayio_lvgl_video_can_rotate(lv_display_t *display, lv_display_rotation_t rotation); diff --git a/supervisor/port_heap.h b/supervisor/port_heap.h index ff37d02700d..376322ada4d 100644 --- a/supervisor/port_heap.h +++ b/supervisor/port_heap.h @@ -22,7 +22,6 @@ void port_heap_init(void); void *port_malloc(size_t size, bool dma_capable); void *port_malloc_zero(size_t size, bool dma_capable); - void port_free(void *ptr); void *port_realloc(void *ptr, size_t size, bool dma_capable); diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index f6bb42e450b..abcedb518a1 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -227,7 +227,9 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) { // Right align the status bar. status_bar->x = width_px - status_bar->pixel_width; status_bar->top_left_y = 0; + #if !CIRCUITPY_DISPLAYIO_LVGL status_bar->full_change = true; + #endif scroll_area->tile_width = glyph_width; scroll_area->tile_height = glyph_height; @@ -242,7 +244,9 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) { // Align the scroll area to the bottom so that the newest line isn't cutoff. The top line // may be clipped by the status bar and that's ok. scroll_area->y = height_px - scroll_area->pixel_height; + #if !CIRCUITPY_DISPLAYIO_LVGL scroll_area->full_change = true; + #endif mp_obj_t new_bitmap = mp_const_none; mp_obj_t new_font = mp_const_none; @@ -297,8 +301,11 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) { port_free(tilegrid_tiles); tilegrid_tiles = NULL; } + // A TileGrid stores a tile index in one byte when the bitmap has at most 255 tiles and in + // two when it has more (tiles_in_bitmap, which is total_values below). The number of cells + // on screen says nothing about how wide an index is. size_t bytes_per_tile = 1; - if (total_tiles > 255) { + if (total_values > 255) { // Two bytes per tile. bytes_per_tile = 2; } diff --git a/supervisor/shared/safe_mode.c b/supervisor/shared/safe_mode.c index fba4c0fb37f..d156dcd6af7 100644 --- a/supervisor/shared/safe_mode.c +++ b/supervisor/shared/safe_mode.c @@ -218,6 +218,9 @@ void print_safe_mode_message(safe_mode_t reason) { case SAFE_MODE_INTERRUPT_ERROR: message = MP_ERROR_TEXT("Interrupt error."); break; + case SAFE_MODE_LVGL_OUT_OF_MEMORY: + message = MP_ERROR_TEXT("LVGL ran out of memory."); + break; case SAFE_MODE_NLR_JUMP_FAIL: message = MP_ERROR_TEXT("NLR jump failed. Likely memory corruption."); break; diff --git a/supervisor/shared/safe_mode.h b/supervisor/shared/safe_mode.h index 87f65d867ea..1021cad980c 100644 --- a/supervisor/shared/safe_mode.h +++ b/supervisor/shared/safe_mode.h @@ -17,6 +17,7 @@ typedef enum { SAFE_MODE_GC_ALLOC_OUTSIDE_VM, SAFE_MODE_HARD_FAULT, SAFE_MODE_INTERRUPT_ERROR, + SAFE_MODE_LVGL_OUT_OF_MEMORY, SAFE_MODE_MANUAL, SAFE_MODE_NLR_JUMP_FAIL, SAFE_MODE_NO_CIRCUITPY, diff --git a/supervisor/shared/tick.c b/supervisor/shared/tick.c index 86eda75e436..a6fb814aa9f 100644 --- a/supervisor/shared/tick.c +++ b/supervisor/shared/tick.c @@ -23,6 +23,10 @@ #include "shared-module/displayio/__init__.h" #endif +#if CIRCUITPY_DISPLAYIO && CIRCUITPY_DISPLAYIO_LVGL && CIRCUITPY_VIDEOCORE +#include "shared-module/videocore/VideoBuffer.h" +#endif + #if CIRCUITPY_KEYPAD #include "shared-module/keypad/__init__.h" #endif @@ -69,8 +73,13 @@ static void supervisor_background_tick(void *unused) { bleio_hci_background(); #endif - #if CIRCUITPY_DISPLAYIO + #if CIRCUITPY_DISPLAYIO && !CIRCUITPY_DISPLAYIO_LVGL + // The LVGL compositor renders on an RTOS thread of its own; nothing to pump here. displayio_background(); + #elif CIRCUITPY_DISPLAYIO && CIRCUITPY_DISPLAYIO_LVGL && CIRCUITPY_VIDEOCORE + // Video frames land in interrupt context, so repaint them here, from a + // normal background context, for the next LVGL render. + displayio_lvgl_video_background(); #endif filesystem_background(); diff --git a/supervisor/shared/web_workflow/display_stream.c b/supervisor/shared/web_workflow/display_stream.c index 4e554bfeba3..1ce81ea48e1 100644 --- a/supervisor/shared/web_workflow/display_stream.c +++ b/supervisor/shared/web_workflow/display_stream.c @@ -450,7 +450,9 @@ static void _check_display(void) { new_pixels = displays[i].framebuffer_display.bufinfo.buf; } #endif - #if CIRCUITPY_BUSDISPLAY + // The LVGL compositor's BusDisplay has no display_core and no readable + // pixels, so only framebuffer displays can be streamed there. + #if CIRCUITPY_BUSDISPLAY && !CIRCUITPY_DISPLAYIO_LVGL if (type == &busdisplay_busdisplay_type) { core = &displays[i].display.core; } @@ -458,10 +460,18 @@ static void _check_display(void) { (void)type; } uint8_t new_format = FORMAT_NONE; + // The panel's size, whatever the rotation. The legacy compositor keeps it in the + // full refresh area; LVGL tracks invalidation itself, so the core keeps only the size. + #if CIRCUITPY_DISPLAYIO_LVGL + uint16_t new_width = core != NULL ? core->width : width; + uint16_t new_height = core != NULL ? core->height : height; + #else + uint16_t new_width = core != NULL ? core->area.x2 : width; + uint16_t new_height = core != NULL ? core->area.y2 : height; + #endif if (core != NULL) { new_format = _format_of(&core->colorspace); - // The core's area is the panel's size, whatever the rotation. - if (core->area.x2 > MAX_SIZE || core->area.y2 > MAX_SIZE) { + if (new_width > MAX_SIZE || new_height > MAX_SIZE) { new_format = FORMAT_NONE; } if (fb != NULL ? new_pixels == NULL : core->colorspace.depth < 8) { @@ -473,8 +483,6 @@ static void _check_display(void) { new_pixels = NULL; } } - uint16_t new_width = core != NULL ? core->area.x2 : width; - uint16_t new_height = core != NULL ? core->area.y2 : height; if (core == target && (core == NULL || (new_format == format && new_width == width && new_height == height && new_pixels == pixels))) { @@ -551,7 +559,8 @@ static void _load_block(uint16_t x, uint16_t y, uint16_t w, uint16_t h) { x0 = x; } #endif - #if CIRCUITPY_BUSDISPLAY + // Rendering a bus display's areas again needs the scanline compositor. + #if CIRCUITPY_BUSDISPLAY && !CIRCUITPY_DISPLAYIO_LVGL if (src == NULL) { // Rendered into block itself. Converting from the end doesn't overwrite // a pixel before it's read, since pixels are at most 4 bytes. diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk index 67b2e8224ae..6a8b419edf5 100644 --- a/supervisor/supervisor.mk +++ b/supervisor/supervisor.mk @@ -289,6 +289,10 @@ $(BUILD)/supervisor/shared/translate/translate.o: $(HEADER_BUILD)/qstrdefs.gener CIRCUITPY_DISPLAY_FONT ?= "../../tools/fonts/ter-u12n.bdf" CIRCUITPY_FONT_EXTRA_CHARACTERS ?= "" +ifeq ($(CIRCUITPY_DISPLAYIO_LVGL),1) +CIRCUITPY_GEN_DISPLAY_RESOURCES_LVGL = --lvgl-tilegrid +endif + $(BUILD)/autogen_display_resources-$(TRANSLATION).c: ../../tools/gen_display_resources.py $(TOP)/locale/$(TRANSLATION).po Makefile | $(HEADER_BUILD) $(STEPECHO) "GEN $@" $(Q)install -d $(BUILD)/genhdr @@ -296,4 +300,5 @@ $(BUILD)/autogen_display_resources-$(TRANSLATION).c: ../../tools/gen_display_res --font $(CIRCUITPY_DISPLAY_FONT) \ --sample_file $(TOP)/locale/$(TRANSLATION).po \ --extra_characters $(CIRCUITPY_FONT_EXTRA_CHARACTERS) \ - --output_c_file $@ + --output_c_file $@ \ + $(CIRCUITPY_GEN_DISPLAY_RESOURCES_LVGL) diff --git a/tools/gen_display_resources.py b/tools/gen_display_resources.py index f857754e95f..97d216ab31b 100644 --- a/tools/gen_display_resources.py +++ b/tools/gen_display_resources.py @@ -23,8 +23,23 @@ ) parser.add_argument("--output_c_file", type=argparse.FileType("w"), required=True) +parser.add_argument( + "--lvgl-tilegrid", + action="store_true", + help="Set when building the LVGL compositing core: TileGrids have no partial_change field", +) + args = parser.parse_args() +# The legacy TileGrid carries a partial_change flag for scanline dirty tracking; the +# LVGL TileGrid does not, and the static initializer must match the struct in use. +if args.lvgl_tilegrid: + partial_change_line = "" +else: + partial_change_line = ( + " .partial_change = false,\n .full_change = false,\n .moved = false,\n" + ) + class BitmapStub: def __init__(self, width, height, color_depth): @@ -108,6 +123,13 @@ def _load_row(self, y, row): #include "shared-bindings/displayio/Palette.h" #include "supervisor/shared/display.h" +// LVGL builds keep these bitmaps in RAM. Other builds keep them in flash to save RAM. +#if CIRCUITPY_DISPLAYIO_LVGL +#define DISPLAY_RESOURCE_CONST +#else +#define DISPLAY_RESOURCE_CONST const +#endif + """ ) @@ -121,7 +143,7 @@ def _load_row(self, y, row): blinka_size = 16 c_file.write( """\ -const uint32_t blinka_bitmap_data[32] = { +DISPLAY_RESOURCE_CONST uint32_t blinka_bitmap_data[32] = { 0x11000000, 0x00000011, 0x11010000, 0x00001053, 0x11010000, 0x00001156, @@ -145,7 +167,7 @@ def _load_row(self, y, row): blinka_size = 12 c_file.write( """\ -const uint32_t blinka_bitmap_data[28] = { +DISPLAY_RESOURCE_CONST uint32_t blinka_bitmap_data[28] = { 0x11010000, 0x00000000, 0x53110000, 0x00000010, 0x56110000, 0x00000011, @@ -164,7 +186,7 @@ def _load_row(self, y, row): c_file.write( """\ -const displayio_bitmap_t blinka_bitmap = {{ +displayio_bitmap_t blinka_bitmap = {{ .base = {{.type = &displayio_bitmap_type }}, .width = {0}, .height = {0}, @@ -225,16 +247,16 @@ def _load_row(self, y, row): .top_left_x = {0}, .top_left_y = {0}, .tiles = 0, - .partial_change = false, - .full_change = false, +{partial_change_line} .hidden = false, .hidden_by_parent = false, - .moved = false, +#if !CIRCUITPY_DISPLAYIO_LVGL .inline_tiles = true, +#endif .in_group = true }}; #endif -""".format(blinka_size) +""".format(blinka_size, partial_change_line=partial_change_line) ) c_file.write( @@ -275,15 +297,15 @@ def _load_row(self, y, row): .tile_width = {1}, .tile_height = {2}, .tiles = NULL, - .partial_change = false, - .full_change = false, +{partial_change_line} .hidden = false, .hidden_by_parent = false, - .moved = false, +#if !CIRCUITPY_DISPLAYIO_LVGL .inline_tiles = false, +#endif .in_group = true }}; -""".format(len(filtered_characters), tile_x, tile_y) +""".format(len(filtered_characters), tile_x, tile_y, partial_change_line=partial_change_line) ) c_file.write( @@ -303,20 +325,20 @@ def _load_row(self, y, row): .tile_width = {1}, .tile_height = {2}, .tiles = NULL, - .partial_change = false, - .full_change = false, +{partial_change_line} .hidden = false, .hidden_by_parent = false, - .moved = false, +#if !CIRCUITPY_DISPLAYIO_LVGL .inline_tiles = false, +#endif .in_group = true }}; -""".format(len(filtered_characters), tile_x, tile_y) +""".format(len(filtered_characters), tile_x, tile_y, partial_change_line=partial_change_line) ) c_file.write( """\ -const uint32_t font_bitmap_data[{}] = {{ +DISPLAY_RESOURCE_CONST uint32_t font_bitmap_data[{}] = {{ """.format(bytes_per_row * tile_y // 4) )