Skip to content

fix(gfx): platform-dependent drawPolygon default MAXN for low-memory targets - #5288

Merged
zackees merged 1 commit into
masterfrom
fix/poly-default-maxn-avr
Nov 18, 2026
Merged

zackees merged 1 commit into
masterfrom
fix/poly-default-maxn-avr

Conversation

@zackees

@zackees zackees commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Fixes #5287

Problem

drawPolygon defaulted to MAXN=32, CHUNK=64 on every platform. On AVR that is a 4272 B stack frame for the Exact kernel and 2139 B for the ABuffer kernel, measured with avr-gcc 7.3 -Os -fstack-usage. Both are larger than an Uno's 2 KB of SRAM, so a default-argument call silently overflows the stack.

Fix

The default tier now depends on the platform, through two overridable config macros in src/fl/gfx/canvas.h:

FL_GFX_POLYGON_DEFAULT_MAXN FL_GFX_POLYGON_DEFAULT_CHUNK
FL_PLATFORM_HAS_LARGE_MEMORY (host, ESP32, Teensy 4, ...) 32 (unchanged) 64 (unchanged)
!FL_PLATFORM_HAS_LARGE_MEMORY (AVR, ESP8266, STM32F1, LPC, ...) 8 16

All six default-argument sites in canvas.h and gfx.h use them. Explicit MAXN/CHUNK arguments behave as before. CHUNK does not change the rendered pixels. MAXN only matters for polygons with more than MAXN vertices, which are truncated as before. The doc comments in primitives.h and poly_abuffer.h now give the measured stack figures. The host test asserts that the host default stays 32, because the pinned 32- and 34-vertex cases rely on it.

Evidence

  • AVR (atmega328p) -fstack-usage frames for the kernel alone, before -> after: Exact 4272 B -> 1032 B, ABuffer 2139 B -> 603 B.
  • Uno firmware built from a scratch copy of PolyShow that uses the default canvas.drawPolygon<fl::s16x16>(...): it instantiates drawPolygonCore<CRGB, s16x16, true, 8, 16>, and the prologue reserves 0x3FA = 1018 B.
  • bash compile mega --examples PolyShow,PolyDemo,PolyKernels: all three build. The poly examples are filtered out on uno because of their LED buffers.
  • bash test --cpp: 369/369 unit and 98/98 example tests pass. bash lint is clean.

Summary by CodeRabbit

  • New Features

    • Polygon drawing now uses platform-appropriate default limits: up to 32 vertices on large-memory platforms and 8 on other platforms. Defaults can also be configured, and callers can specify a limit for individual drawing calls.
    • Inputs exceeding the selected vertex limit are truncated. Polygons within the limit produce the same pixels across supported configurations.
  • Documentation

    • Clarified how polygon vertex limits and memory use vary by platform, including constraints for devices with limited memory.

…emory targets

drawPolygon defaulted to MAXN=32, CHUNK=64 everywhere: a 4272 B Exact
(2139 B ABuffer) stack frame on AVR, more than an Uno's 2 KB of SRAM.
Default to 8/16 when !FL_PLATFORM_HAS_LARGE_MEMORY (1032 B / 603 B) and
keep 32/64 elsewhere via FL_GFX_POLYGON_DEFAULT_MAXN/_CHUNK. Explicit
tiers are unchanged.

Fixes #5287
@zackees zackees added ci-platform:uno Select FastLED board CI cells ci-platform:mega Select FastLED board CI cells labels Nov 18, 2026
@zackees
zackees enabled auto-merge November 18, 2026 18:02
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

drawPolygon now uses platform-dependent defaults for MAXN and CHUNK, with build-wide overrides available. Documentation describes the defaults, memory use, and truncation behavior. A host test checks that the default MAXN is 32.

Changes: Polygon drawing defaults

Layer / File(s) Summary
Define and apply polygon defaults
src/fl/gfx/canvas.h, src/fl/gfx/gfx.h
MAXN and CHUNK defaults are 32 and 64 on large-memory platforms, and 8 and 16 elsewhere. The macros can be overridden. The free-function and Canvas overloads use these defaults.
Document and check polygon limits
src/fl/gfx/poly_abuffer.h, src/fl/gfx/primitives.h, src/fl/gfx/README.md, tests/fl/gfx/draw_polygon.hpp
Documentation describes platform-dependent defaults, memory use, and vertex truncation. A compile-time test checks that the host default MAXN is 32.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related PRs

  • FastLED/FastLED#5269: Adds the drawPolygon backends and API whose default arguments this PR changes.
Merge Risk: Minimal · up to 1d7e7

This change lowers the default polygon vertex limit and scratch size on small-memory targets such as AVR, which avoids stack overflow. Polygons with more than 8 vertices on those targets are truncated unless the caller sets a larger limit, and the documentation says so. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: drawPolygon's default MAXN becomes platform-dependent for low-memory targets, which matches the raw summary and PR objectives (defaults 32/64 on large-memory platforms…
Linked Issues check Passed Issue #5287 asks for a platform-dependent default MAXN, a compile-time guard or doc note, and verification by an Uno default-argument build with a stack estimate. The PR meets these as follows:
(1) Pl…
Out of Scope Changes check Passed All changed files relate to the issue objective. canvas.h and gfx.h change the default template arguments to the new macros. primitives.h, poly_abuffer.h, and src/fl/gfx/README.md add documentation of…
Full details: Title check

Explanation

The title names the main change: drawPolygon's default MAXN becomes platform-dependent for low-memory targets, which matches the raw summary and PR objectives (defaults 32/64 on large-memory platforms, 8/16 elsewhere, via FL_GFX_POLYGON_DEFAULT_MAXN/CHUNK). The 'fix(gfx):' prefix is a conventional commit type and does not hurt clarity. The title is specific and concise.

Full details: Linked Issues check

Explanation

Issue #5287 asks for a platform-dependent default MAXN, a compile-time guard or doc note, and verification by an Uno default-argument build with a stack estimate. The PR meets these as follows:
(1) Platform-dependent default: src/fl/gfx/canvas.h defines FL_GFX_POLYGON_DEFAULT_MAXN and FL_GFX_POLYGON_DEFAULT_CHUNK. Large-memory platforms keep 32/64. Platforms without FL_PLATFORM_HAS_LARGE_MEMORY (including AVR) use 8/16. All six default-argument sites in canvas.h and gfx.h use these macros.
(2) Doc note: primitives.h and poly_abuffer.h document the tiers and the AVR 2 KB SRAM constraint. src/fl/gfx/README.md documents the defaults.
(3) Verification: the PR description reports a stack-usage measurement for the AVR kernels (Exact 4272 B to 1032 B, ABuffer 2139 B to 603 B). It also reports an Uno build of a default-argument call that reserves 1018 B in the prologue. This Uno result comes from a scratch build cited in the PR. It is not a file in the repository, so it is reported evidence and was not independently re-run.
(4) Host test: tests/fl/gfx/draw_polygon.hpp asserts that FL_GFX_POLYGON_DEFAULT_MAXN is 32 on host.
The issue's 'compile-time guard' option is not implemented. The issue lists it with the doc note as 'also add a compile-time guard or doc note', so the doc note satisfies the requirement.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the issue objective. canvas.h and gfx.h change the default template arguments to the new macros. primitives.h, poly_abuffer.h, and src/fl/gfx/README.md add documentation of the new defaults and stack figures. tests/fl/gfx/draw_polygon.hpp adds a static_assert on the host default. Explicit MAXN and CHUNK arguments are unchanged, per the PR description and the summary. No unrelated change was found in the summary.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
Fix failing CI checks

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6e5e78e and 1d7e782.

Files selected for processing: 6

  • src/fl/gfx/README.md
  • src/fl/gfx/canvas.h
  • src/fl/gfx/gfx.h
  • src/fl/gfx/poly_abuffer.h
  • src/fl/gfx/primitives.h
  • tests/fl/gfx/draw_polygon.hpp

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@github-actions github-actions Bot added the sanitized Title, body and conversation screened; review text requires separate screening. label Nov 18, 2026
@fastled-project-sync fastled-project-sync Bot moved this to Triage in FastLED Tracker Nov 18, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 18, 2026
@zackees
zackees added this pull request to the merge queue Nov 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Memory usage (this PR vs. its merge base)

Change in examples/Blink per target. The left number is this PR's image flash / attributed RAM in bytes; the colored delta is the change from the merge base (red ↑ is larger, green ↓ is smaller, Same is unchanged).

Target FLASH [bytes] RAM [bytes]
attiny85 2,460 (Same) 136 (Same)
uno 3,600 (Same) 134 (Same)

@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 18, 2026
@github-actions github-actions Bot added sanitized Title, body and conversation screened; review text requires separate screening. and removed sanitized Title, body and conversation screened; review text requires separate screening. labels Nov 18, 2026
Merged via the queue into master with commit 502b454 Nov 18, 2026
364 of 382 checks passed
@zackees
zackees deleted the fix/poly-default-maxn-avr branch November 18, 2026 18:12
@fastled-project-sync fastled-project-sync Bot moved this from Triage to Done in FastLED Tracker Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-platform:mega Select FastLED board CI cells ci-platform:uno Select FastLED board CI cells sanitized Title, body and conversation screened; review text requires separate screening.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

gfx: drawPolygon default MAXN=32 needs ~4.2 KB stack, overflows AVR (Uno)

1 participant