Best practices for DirectComposition

Note

For apps on Windows 10, we recommend using Windows.UI.Composition APIs instead of DirectComposition. For more info, see Modernize your desktop app using the Visual layer.

This topic describes best practices for using Microsoft DirectComposition.

Best practices

The following table presents the practices recommended for working with Microsoft DirectComposition visuals.

Practice Description
After creating a DirectComposition device, call the IDCompositionDevice::CheckDeviceState method in response to each WM_PAINT message to ensure that the device is still valid.
If the Microsoft DirectX Graphics Infrastructure (DXGI) device is lost, the DirectComposition device associated with the DXGI device is also lost. When it detects a lost device, DirectComposition sends the WM_PAINT message to all windows. Calling CheckDeviceState in response to each WM_PAINT message lets you determine whether the DirectComposition device object is still valid and, if not, take steps to recover content.
For more information, see Device object.
Create only the number of visuals needed for a composition or animation, and destroy the visuals immediately after DirectComposition finishes using them.
DirectComposition uses the graphics processing unit (GPU), a resource that your application shares with other applications and the operating system. This practice ensures that all applications and the operating system receive adequate GPU resources.
For more information, see Visuals.
Do not hide visuals by setting opacity to 0%; instead, remove visuals from the visual tree.
Setting opacity to 0% requires more system resources than removing them from the visual tree.
For more information, see Opacity and Visual tree.
Do not hide a visual by applying an empty (zero sized) clip rectangle to a visual. Instead, remove the visual from the visual tree.
Removing a visual from the visual tree results in better performance than applying an empty clip rectangle.
For more information, see Clipping.
Do not apply a clip rectangle to a visual if the clip rectangle is not needed, such as a clip rectangle that includes the entire bitmap content of the visual.
Unnecessary clip rectangles harm system performance.
For more information, see Clipping.
If you need a large, single-color bitmap, create a smaller bitmap surface and then apply a scale transform instead of creating a full-sized surface.
Applying a scale transform to a smaller surface uses fewer system resources than a full-size surface.
For more information, see Bitmap objects and Transforms.
Avoid applying 3D transforms to multiple levels of a visual tree, such as to a parent and its descendent(s).
Applying 3D transforms to multiple levels of a visual tree can produce unintended results because 3D transformations are not multiplied out in the tree. For example, a 90 degree rotation around the y-axis on a child, and a 90 degree rotation around the y-axis on a parent, results in both visuals being rotated away to nothing.
For more information, see Effects.

Security considerations

The following articles provide guidance for writing secure C++ code.

How to Use DirectComposition