adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
MIT License
3.98k stars 1.17k forks source link

lines drawn with bitmaptools.draw_polygon to a bitmap can partially fail to update a screen #9239

Open kevinjwalters opened 2 months ago

kevinjwalters commented 2 months ago

CircuitPython version

Adafruit CircuitPython 9.0.4 on 2024-04-16; Adafruit CLUE nRF52840 Express with nRF52840

Code/REPL

### displayio or bitmaptools bug with missing pixels
### perhaps related to dirty region code for efficient screen updates?

### Demonstrated on Adafruit CLUE with 9.0.3 and 9.0.4

import array
import time

import board

import displayio
import bitmaptools

p1 = [(170, 120), (168, 105), (160, 91), (149, 80), (135, 72), (120, 70)]
p2 = [(120, 70), (105, 72), (91, 80), (80, 91), (72, 105), (70, 120)]
p3 = [(70, 120), (72, 135), (80, 149), (91, 160), (105, 168), (120, 170)]
p4 = [(120, 170), (135, 168), (149, 160), (160, 149), (168, 135), (170, 120)]

display = board.DISPLAY
main_group = displayio.Group()
display.root_group = main_group

bmp = displayio.Bitmap(240, 240, 8)
palette = displayio.Palette(8)
palette[1] = 0xe00000
palette[2] = 0x00e000
palette[3] = 0x0000e0
palette[4] = 0xe0e0e0
palette[5] = 0x606060
tg = displayio.TileGrid(bitmap=bmp, pixel_shader=palette)
main_group.append(tg)

pal_idx = 1
for points in (p1, p2, p3, p4):
    bitmaptools.draw_polygon(bmp,
                             array.array("h",[xy[0] for xy in points]), 
                             array.array("h",[xy[1] for xy in points]),      
                             pal_idx,
                             close=False)
    pal_idx += 1
    time.sleep(3)

print("some pixels are missing when drawing p4")

bitmaptools.draw_polygon(bmp,
                         array.array("h",[0, 239]),
                         array.array("h",[0, 239]),
                         pal_idx,
                         close=False)
print("they appear after this line is drawn across whole display")

while True:
    pass

Behavior

Some pixels are missing from the final pass of the for loop for p4. Might be one or two missing for p3 too.

Description

I did this on a CLUE. I'd suggest using that or something with a 240x240xsame-bit-depth resolution display to investigate further/indepdently reproduce.

Additional information

I think there's some code for "dirty" regions of the display signifying needing an update of a rectangular area sent to the physical screen. I'd suspect there is a fault somewhere between bitmaptools and displayio ...

jepler commented 1 month ago

@matemaciek can you take a look at this? It looks like you contributed this code.