diff options
| author | Dan Halbert <halbert@adafruit.com> | 2019-06-13 08:29:12 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-06-13 08:29:12 -0400 |
| commit | 6be7cf7440acb827d4b0f767b6083a859499b045 (patch) | |
| tree | 55834cab63123bb95a2b7d329ff78442f34cfe6b /shared-module/displayio/__init__.c | |
| parent | a06a97e2cba36233d229ba44c3acd9007affe1c9 (diff) | |
| parent | 6f6dcafd906b2ac49291f4220f3fcc7cea6112e1 (diff) | |
Merge pull request #1940 from tannewt/dirty_area
Add partial display update support.
Diffstat (limited to 'shared-module/displayio/__init__.c')
| -rw-r--r-- | shared-module/displayio/__init__.c | 280 |
1 files changed, 167 insertions, 113 deletions
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index 35bdad8fe..b6709e0eb 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -1,5 +1,6 @@ #include <string.h> + #include "shared-module/displayio/__init__.h" #include "lib/utils/interrupt_char.h" @@ -19,14 +20,83 @@ primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; -static inline void swap(int16_t* a, int16_t* b) { - int16_t temp = *a; - *a = *b; - *b = temp; +bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area) { + uint16_t buffer_size = 512; + + displayio_area_t clipped; + // Clip the area to the display by overlapping the areas. If there is no overlap then we're done. + if (!displayio_display_clip_area(display, area, &clipped)) { + return true; + } + uint16_t subrectangles = 1; + uint16_t rows_per_buffer = displayio_area_height(&clipped); + if (displayio_area_size(area) > buffer_size) { + rows_per_buffer = buffer_size / displayio_area_width(&clipped); + subrectangles = displayio_area_height(&clipped) / rows_per_buffer; + if (displayio_area_height(&clipped) % rows_per_buffer != 0) { + subrectangles++; + } + buffer_size = rows_per_buffer * displayio_area_width(&clipped); + } + uint32_t buffer[buffer_size / 2]; + uint16_t remaining_rows = displayio_area_height(&clipped); + + for (uint16_t j = 0; j < subrectangles; j++) { + displayio_area_t subrectangle = { + .x1 = clipped.x1, + .y1 = clipped.y1 + rows_per_buffer * j, + .x2 = clipped.x2, + .y2 = clipped.y1 + rows_per_buffer * (j + 1) + }; + if (remaining_rows < rows_per_buffer) { + subrectangle.y2 = subrectangle.y1 + remaining_rows; + } + remaining_rows -= rows_per_buffer; + + displayio_display_begin_transaction(display); + displayio_display_set_region_to_update(display, subrectangle.x1, subrectangle.y1, + subrectangle.x2, subrectangle.y2); + displayio_display_end_transaction(display); + + uint32_t mask[(buffer_size / 32) + 1]; + for (uint16_t k = 0; k < (buffer_size / 32) + 1; k++) { + mask[k] = 0x00000000; + } + + bool full_coverage = displayio_display_fill_area(display, &subrectangle, mask, buffer); + if (!full_coverage) { + uint32_t index = 0; + uint32_t current_mask = 0; + for (int16_t y = subrectangle.y1; y < subrectangle.y2; y++) { + for (int16_t x = subrectangle.x1; x < subrectangle.x2; x++) { + if (index % 32 == 0) { + current_mask = mask[index / 32]; + } + if ((current_mask & (1 << (index % 32))) == 0) { + ((uint16_t*) buffer)[index] = 0x0000; + } + index++; + } + } + } + + if (!displayio_display_begin_transaction(display)) { + // Can't acquire display bus; skip the rest of the data. Try next display. + return false; + } + displayio_display_send_pixels(display, (uint8_t*) buffer, displayio_area_size(&subrectangle) * sizeof(uint16_t)); + displayio_display_end_transaction(display); + + // TODO(tannewt): Make refresh displays faster so we don't starve other + // background tasks. + usb_background(); + } + return true; } // Check for recursive calls to displayio_refresh_displays. bool refresh_displays_in_progress = false; +uint32_t frame_count = 0; void displayio_refresh_displays(void) { if (mp_hal_is_interrupted()) { @@ -57,115 +127,19 @@ void displayio_refresh_displays(void) { // Too soon. Try next display. continue; } - if (displayio_display_refresh_queued(display)) { - if (!displayio_display_begin_transaction(display)) { - // Can't acquire display bus; skip updating this display. Try next display. - continue; - } - displayio_display_end_transaction(display); - - displayio_area_t whole_screen = { - .x1 = 0, - .y1 = 0, - .x2 = display->width, - .y2 = display->height - }; - if (display->transpose_xy) { - swap(&whole_screen.x2, &whole_screen.y2); - } - - uint16_t buffer_size = 512; - - uint16_t subrectangles = 1; - uint16_t rows_per_buffer = displayio_area_height(&whole_screen); - if (displayio_area_size(&whole_screen) > buffer_size) { - rows_per_buffer = buffer_size / displayio_area_width(&whole_screen); - subrectangles = displayio_area_height(&whole_screen) / rows_per_buffer; - buffer_size = rows_per_buffer * displayio_area_width(&whole_screen); - } - uint32_t buffer[buffer_size / 2]; - - for (uint16_t j = 0; j < subrectangles; j++) { - displayio_area_t subrectangle = { - .x1 = 0, - .y1 = rows_per_buffer * j, - .x2 = displayio_area_width(&whole_screen), - .y2 = rows_per_buffer * (j + 1) - }; - - displayio_display_begin_transaction(display); - displayio_display_set_region_to_update(display, subrectangle.x1, subrectangle.y1, - subrectangle.x2, subrectangle.y2); - displayio_display_end_transaction(display); - - // Handle display mirroring and transpose. - displayio_area_t transformed_subrectangle; - displayio_buffer_transform_t transform; - if (display->mirror_x) { - uint16_t width = displayio_area_width(&whole_screen); - transformed_subrectangle.x1 = width - subrectangle.x2; - transformed_subrectangle.x2 = width - subrectangle.x1; - } else { - transformed_subrectangle.x1 = subrectangle.x1; - transformed_subrectangle.x2 = subrectangle.x2; - } - if (display->mirror_y != display->transpose_xy) { - uint16_t height = displayio_area_height(&whole_screen); - transformed_subrectangle.y1 = height - subrectangle.y2; - transformed_subrectangle.y2 = height - subrectangle.y1; - } else { - transformed_subrectangle.y1 = subrectangle.y1; - transformed_subrectangle.y2 = subrectangle.y2; - } - transform.width = transformed_subrectangle.x2 - transformed_subrectangle.x1; - transform.height = transformed_subrectangle.y2 - transformed_subrectangle.y1; - if (display->transpose_xy) { - int16_t y1 = transformed_subrectangle.y1; - int16_t y2 = transformed_subrectangle.y2; - transformed_subrectangle.y1 = transformed_subrectangle.x1; - transformed_subrectangle.y2 = transformed_subrectangle.x2; - transformed_subrectangle.x1 = y1; - transformed_subrectangle.x2 = y2; - } - transform.transpose_xy = display->transpose_xy; - transform.mirror_x = display->mirror_x; - transform.mirror_y = display->mirror_y; - transform.scale = 1; - - uint32_t mask[(buffer_size / 32) + 1]; - for (uint16_t k = 0; k < (buffer_size / 32) + 1; k++) { - mask[k] = 0x00000000; - } - bool full_coverage = displayio_group_get_area(display->current_group, &transform, &transformed_subrectangle, mask, buffer); - if (!full_coverage) { - uint32_t index = 0; - uint32_t current_mask = 0; - for (int16_t y = subrectangle.y1; y < subrectangle.y2; y++) { - for (int16_t x = subrectangle.x1; x < subrectangle.x2; x++) { - if (index % 32 == 0) { - current_mask = mask[index / 32]; - } - if ((current_mask & (1 << (index % 32))) == 0) { - ((uint16_t*) buffer)[index] = 0x0000; - } - index++; - } - } - } - - if (!displayio_display_begin_transaction(display)) { - // Can't acquire display bus; skip the rest of the data. Try next display. - break; - } - displayio_display_send_pixels(display, buffer, buffer_size / 2); - displayio_display_end_transaction(display); - - // TODO(tannewt): Make refresh displays faster so we don't starve other - // background tasks. - usb_background(); - } + if (!displayio_display_begin_transaction(display)) { + // Can't acquire display bus; skip updating this display. Try next display. + continue; + } + displayio_display_end_transaction(display); + displayio_display_start_refresh(display); + const displayio_area_t* current_area = displayio_display_get_refresh_areas(display); + while (current_area != NULL) { + refresh_area(display, current_area); + current_area = current_area->next; } displayio_display_finish_refresh(display); + frame_count++; } // All done. @@ -244,6 +218,35 @@ void displayio_gc_collect(void) { } } +void displayio_area_expand(displayio_area_t* original, const displayio_area_t* addition) { + if (addition->x1 < original->x1) { + original->x1 = addition->x1; + } + if (addition->y1 < original->y1) { + original->y1 = addition->y1; + } + if (addition->x2 > original->x2) { + original->x2 = addition->x2; + } + if (addition->y2 > original->y2) { + original->y2 = addition->y2; + } +} + +void displayio_area_copy(const displayio_area_t* src, displayio_area_t* dst) { + dst->x1 = src->x1; + dst->y1 = src->y1; + dst->x2 = src->x2; + dst->y2 = src->y2; +} + +void displayio_area_scale(displayio_area_t* area, uint16_t scale) { + area->x1 *= scale; + area->y1 *= scale; + area->x2 *= scale; + area->y2 *= scale; +} + void displayio_area_shift(displayio_area_t* area, int16_t dx, int16_t dy) { area->x1 += dx; area->y1 += dy; @@ -262,7 +265,7 @@ bool displayio_area_compute_overlap(const displayio_area_t* a, if (b->x2 < overlap->x2) { overlap->x2 = b->x2; } - if (overlap->x1 > overlap->x2) { + if (overlap->x1 >= overlap->x2) { return false; } overlap->y1 = a->y1; @@ -273,12 +276,34 @@ bool displayio_area_compute_overlap(const displayio_area_t* a, if (b->y2 < overlap->y2) { overlap->y2 = b->y2; } - if (overlap->y1 > overlap->y2) { + if (overlap->y1 >= overlap->y2) { return false; } return true; } +void displayio_area_union(const displayio_area_t* a, + const displayio_area_t* b, + displayio_area_t* u) { + u->x1 = a->x1; + if (b->x1 < u->x1) { + u->x1 = b->x1; + } + u->x2 = a->x2; + if (b->x2 > u->x2) { + u->x2 = b->x2; + } + + u->y1 = a->y1; + if (b->y1 < u->y1) { + u->y1 = b->y1; + } + u->y2 = a->y2; + if (b->y2 > u->y2) { + u->y2 = b->y2; + } +} + uint16_t displayio_area_width(const displayio_area_t* area) { return area->x2 - area->x1; } @@ -297,3 +322,32 @@ bool displayio_area_equal(const displayio_area_t* a, const displayio_area_t* b) a->x2 == b->x2 && a->y2 == b->y2; } + +// Original and whole must be in the same coordinate space. +void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpose_xy, + const displayio_area_t* original, + const displayio_area_t* whole, + displayio_area_t* transformed) { + if (mirror_x) { + transformed->x1 = whole->x1 + (whole->x2 - original->x2); + transformed->x2 = whole->x2 - (original->x1 - whole->x1); + } else { + transformed->x1 = original->x1; + transformed->x2 = original->x2; + } + if (mirror_y) { + transformed->y1 = whole->y1 + (whole->y2 - original->y2); + transformed->y2 = whole->y2 - (original->y1 - whole->y1); + } else { + transformed->y1 = original->y1; + transformed->y2 = original->y2; + } + if (transpose_xy) { + int16_t y1 = transformed->y1; + int16_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); + transformed->x1 = whole->x1 + (y1 - whole->y1); + } +} |
