Writeup

Alexandru Pana edited this page Aug 17, 2014 · 4 revisions
Clone this wiki locally

Purpose of the project

What Direct2D is

Direct2D is the new API developed by Microsoft which aims to replace GDI+. It is supposed to be faster and produce better graphics.

Why use it instead of GDI+

Aside from the slight improvements in rendering quality (screenshots), the benchmarks show an increase in run speed (tables).

What are the differences

Come up with all the differences

Current limitations

  • There is no printerDC or metafileDC wrapper.
  • Hatch brushes and pens are subject to artifacts such as tearing due to poor sampling. This is because Direct2D dropped support for hatches, and the current wxGraphicsContext uses patterned images.
  • Ellipses have stretched outlines (do they still?)
  • Pies have longer outlines
  • No image or primitive blending

Direct2D support

MSVC

There are currently two versions of Direct2D, which people call 1.0 and 1.1, for lack of any official versioning. The minimum requirements for the two versions are:

  • Direct2D 1.0: Windows 7, Windows Vista with SP2 and Platform Update for Windows Vista
  • Direct2D 1.1: Windows 8 and Platform Update for Windows 7

For the purposes of this implementation, Direct2D 1.1 introduces a way mechanism for blending images, something not supported in Direct2D 1.0. Internally, this requires the use of a completely new render target which is more tightly coupled with Direct3D. More than this, it introduces a new interpolation mode enumeration.

wxInterpolationQuality Direct2D 1.0 Direct2D 1.1
wxINTERPOLATION_DEFAULT D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
wxINTERPOLATION_NONE D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
wxINTERPOLATION_FAST D2D1_INTERPOLATION_MODE_NEAREST_NEIGHBOR D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
wxINTERPOLATION_GOOD D2D1_INTERPOLATION_MODE_LINEAR D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR
wxINTERPOLATION_BEST D2D1_INTERPOLATION_MODE_CUBIC D2D1_BITMAP_INTERPOLATION_MODE_LINEAR

Mingw support

There are currently no direct2d headers for mingw, and support for direct2d is disabled by default for any compiler other than Microsoft's Visual Studio 2008.

Usage example

When supported, the Direct2D backend is used by default (e.g. wxGraphicsRenderer::GetDefaultRenderer returns a D2D renderer). However, you can still get a GDI+ renderer by using the wxGraphicsRenderer::GetGdiPlusRenderer() or a D2D renderer by using the wxGraphicsRenderer::GetDirect2DRenderer(). To have the GetDefaultRenderer method return a specific renderer you can use the SetDefaultRenderer method.

Performance considerations

Since Direct2D uses device-dependent objects which are stored in VRAM, their use is extremely fast, but their creation is not. Internally, the wxWidgets implementation keeps track of the native resources using the wxGraphics* classes. Even though the native resources might be flushed from VRAM for various reasons, Direct2D notifies this (either when flushing or when ending the drawing process). The wxGraphicsContext implementation makes sure that all native resources are re-acquired when needed.

This leads to two important rules when using the direct2d implementation:

  1. Create wxGraphics* objects and keep on to them. The internal implementation will take care of re-acquiring the native resources when they are flushed. Using DC methods with wxWidgets objects (such as wxBitmap or wxImage) will require the implementation to always acquire a new resource and release at the end.

  2. Flush often! If at any time during the draw process, the resources are flushed, the render target will be invalidated and you will lose everything that was drawn so far. Flushing gives the implementation a chance to re-acquire the resources if they were previously flushed.