Optimizing the Scratch Buffer Size
Scratch Buffer Basics
The Legato Graphics Library utilizes a scratch buffer to update the display frame buffer. Pixel updates are first drawn into the scratch buffer. The scratch buffer is then copied into the frame buffer memory.
Scratch Buffer Size Impacts Draw Performance
On a screen update, the library tries its best to update the screen based on the size of the scratch buffer. This means if the scratch buffer is smaller than the features on the screen, it may take multiple draw passes to complete. On the display, it can catch the eye as unpleasant screen tearing.
By balancing the design of the screen and managing the scratch buffer size, draw tearing can be reduced and often eliminated.
Identifying the Appropriate Scratch Buffer Size
The key to eliminating screen tearing is to identify the largest feature on the screen and allocate enough scratch buffer memory such that the library can update that feature in a single pass.
We can use the Legato Quickstart application design as an example. The design is composed of:
- The Microchip logo as an image widget (120x28 pixels):
- Three label widgets (65x40), (70x40), (180x40):
- A button widget (160x74):
- And another image widget for the MPLAB® Harmony Graphics Suite logo (180x169):
The MHGS logo image widget is the largest feature. For a design with color format RGB565 (16 bits per pixel, a recommended scratch buffer size would be 180 x 169 x 2 = 60840 bytes).
However, depending on how frequently the feature is updated, the scratch buffer may not have to be sized to the largest feature. In the case of the Legato Quickstart design, the button image is the one that is updated most often and gets the most visual attention, while the MHGS logo is only updated on-screen draw. Therefore, a scratch buffer size of 160 x 74 x 2 = 23680 bytes may be enough.
On the other hand, for the best draw performance, it is ideal to allocate the remaining available memory for the scratch buffer if there is free memory available in the system.
Note that a scratch buffer can be as large as a single-frame buffer. The design would essentially be a double buffer configuration.
The scratch buffer size can be adjusted under Project Settings > Renderer.
Enabling Scratch Buffer Padding for PIC32MZ DA 2D GPU Support
For proper blit operations, the PIC32MZ DA 2D GPU requires word-aligned x4 pixel-wide buffers. Failing to do so may cause undesirable visual artifacts.
If "Use GPU for Blits" is enabled for the LE GLCD component:
The scratch buffer padding should be enabled in Legato Graphics Composer under Project Settings > Renderer.