Optimize MicroGraphics drawing performance via native color caching #1253
+382
−32
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Per-pixel color conversion (e.g.,
Color.Color16bppRgb565) is the real bottleneck in drawing operations, not bounds checking or rotation calculations. This caches the converted pen color in native buffer format to eliminate redundant conversions when drawing many pixels with the same color.Changes
MicroGraphics.cs
_penColor16bpp,_penColor8bpp,_penColor1bpp) with lazy initializationPenColorsetterDrawPixelWithCachedColor()that bypasses color conversion for known buffer typescolor == PenColor:DrawSingleWidthLine()(Bresenham)DrawCircleOutline()(8-way symmetry)DrawBitmap()(text rendering)Buffer classes
Add native line drawing methods with bounds checking:
BufferRgb565:DrawHorizontalLine/DrawVerticalLine(x, y, length, ushort color)BufferRgb332:DrawHorizontalLine/DrawVerticalLine(x, y, length, byte color)Buffer1bpp:DrawHorizontalLine/DrawVerticalLine(x, y, length, bool enabled)Example
Expected Impact
Original prompt
Summary
Improve MicroGraphics drawing performance by optimizing at the buffer level. Previous attempts to optimize at the MicroGraphics layer showed no measurable improvement - the real bottleneck is in the per-pixel color conversion and buffer access patterns.
Problem Analysis
Benchmarks show that optimizations to bounds checking and rotation calculations in MicroGraphics had zero impact on performance. The actual bottlenecks are:
1. Per-Pixel Color Conversion
Every
SetPixel(x, y, Color color)call triggers a color format conversion:The
Color16bppRgb565property performs bit manipulation every time:2. No Native-Format Drawing Methods in MicroGraphics
MicroGraphics always works with
Colorobjects, forcing conversion even when drawing many pixels with the same color (lines, fills, text).Proposed Solution
1. Add Native Color Caching to MicroGraphics
Cache the converted pen color in the native buffer format to avoid repeated conversions:
2. Add Native-Format SetPixel Methods to Buffer Classes
Add methods that accept pre-converted colors to skip conversion:
3. Add Native Drawing Path in MicroGraphics
For operations that draw many pixels with PenColor, use the cached native color:
4. Optimize DrawSingleWidthLine to Use Cached Color
5. Add Horizontal Line Drawing with Native Color
For filled shapes that use horizontal line fills:
Then in MicroGraphics, use it for horizontal lines when possible: