Single vs. double buffering in TouchGFX
TouchGFX can use either one or two frame buffers. This choice can have a great impact on both memory usage and performance, and there are several things to take into account. It depends both on your platform (MCU and display size) and UI complexity, but also to a large degree the type of display interface (TFT controller based or 8080-based)
On most platforms double buffering requires external memory (e.g. SDRAM), since there will typically not be enough RAM available on the MCU to fit two frame buffers. Adding an SDRAM is a significant cost increase and also expensive in terms of power consumption. This article will outline in which cases single buffering is sufficient for various platforms.
TFT Controller based systems
This sections deals with TFT controller based system where the display is connected using parallel RGB lines and VSYNC/HSYNC/DE control signals.
Traditionally, double buffering is used on these systems to avoid the risk of screen tearing. With double buffering, TouchGFX will render the next frame into one frame buffer, whereas the TFT controller accesses the other frame buffer. When the next frame has been rendered, the buffers are swapped. With this approach there is never any risk of tearing regardless of how long it takes to render the next frame, since the TFT controller always has access to the latest complete frame data. The drawback is, of course, that two buffers are required which typically requires an external RAM.
In TouchGFX 4.7.0 we introduced a new implementation of single buffering, which carefully tracks the current progress of the TFT controller and gradually render the portions of the screen that are “safe” to modify. This greatly increases the possible frame render time without tearing compared to more naive implementations, thereby making single buffering possible on more platforms. Tearing can still occur if the UI takes a long time to render (e.g. makes extensive use of texture mapping or alpha blending). As a rule of thumb, the following applies, assuming a 60Hz display refresh rate:
- Render time less than 16.67ms: guaranteed no tearing, TouchGFX runs at 60FPS
- Render time 17-32ms: tearing unlikely, TouchGFX runs at 30FPS
- Render time 33ms+: tearing will occur, TouchGFX runs at 20FPS
Note that render time of an application can be measured via the GPIO::RENDER_TIME signal.
It can be difficult to predict beforehand whether your UI will have rendering times that make single buffering impossible. As a guideline we have verified that the following applications run without tearing in single buffer mode, and thereby require no external memory whatsoever:
- The “touchgfx_demo2015_480x272_8MB” on the STM32F746-G Discovery board
- The “touchgfx_demo2014_240x320” on the STM32F429I Discovery board
If your platform and desired UI resembles one of these cases, you can probably get by with single buffering. Otherwise you will need double buffering (and external RAM).
Whether to use single or double buffering is controlled in the touchgfx_init() function located in BoardConfiguration.cpp. Note that for the boards STM32F746-G Discovery and STM32F429I Discovery, the BoardConfiguration has a
which can be used to control this. For custom board configurations, use the following code:
//setup for single buffering hal.setFrameBufferStartAddress((uint16_t*)frameBuf0, 16, false, false); // The optimized strategy for single buffering requires the presence of a // task delay function. hal.registerTaskDelayFunction(&OSWrappers::taskDelay); // Enable single buffer strategy. hal.setFrameRefreshStrategy(HAL::REFRESH_STRATEGY_OPTIM_SINGLE_BUFFER_TFT_CTRL); // Allow immediate drawing hal.lockDMAToFrontPorch(false);
hal.setFrameBufferStartAddress((uint16_t*)frameBuf0); // Allow immediate drawing hal.lockDMAToFrontPorch(false);
8080/SPI based systems
On such systems, the display itself has an integrated frame buffer. Therefore a long frame rendering time does not cause tearing even in a single frame buffer system. There is no gain in using double buffering here. See this article on how to configure TouchGFX on 8080/SPI based systems.