The issue of dynamic flickering of light and dark in the screen when using Direct2D for rendering
I'm learning to use Direct2D to draw some graphics. However, during each drawing process, the screen first becomes darker, then slightly brighter after the drawing stabilizes and remains unchanged. So every time a drawing occurs, the screen appears to flicker once. In this context, each drawing refers to the process starting from context->BeginDraw()
and ending with swapchain->Present()
.
Some experiments and known information:
- Currently, I am using D2D device context for rendering. When I previously used hwndrendertarget for drawing, there was no issue of flickering on the screen.
It is noteworthy that, theoretically, only one frame is rendered during each draw cycle, but the screen first dims and then brightens, resulting in two distinct changes. The dimming seems to occur even before the begindraw() and enddraw() calls complete, and before the present() function finishes.
If the subsequent drawing processes are connected fast enough, the screen remains relatively dim. Once there are no further drawing processes, the screen returns to a brighter state and remains so until the next drawing process initiates, causing the screen to dim again.
I attempted to remove ValidateRect from the WM_PAINT message handler in my program. This time, the screen no longer flickered, but it caused the program to continuously consume significant GPU and CPU resources, even though no user interaction was taking place and no animations were being played on the screen.
- The resources used for drawing, along with their parameters, are essentially the same as those in the official examples.When using the color 0x1e4a56, you are more likely to observe this flickering.