Some Notes on Direct2D

Direct2D is the backbone of Windows graphics API. Although Graphics Device Interface (GDI) still exists, it is now built on top of Direct2D. Here we give brief overview of the architecture of Direct2D.

Intended Audience

Programming Windows consists of two main parts:

  • Messaging and
  • Drawing.

A line of business application consisting of forms for, data entry and viewing text, like a consumer banking application or a leave application form does not concern itself with drawing graphics. Such applications can blissfully ignore D2D (Direct2D)or its predecessor GDI. Windows messaging architecture remains the same in both cases. The intended reader is one who wants to do graphics such as drawing plots, annotate pictures, write games etc.


D2D is layered as shown below [msdn]

Diagram of the Direct2D layered architecture

As can be seen Direct2D can use DirectX, which uses the GPU (Graphics Processor Unit) if it exists, or the Software Rasterizer if there is no special hardware. Most desktops today have a GPU built-in. Hence graphics rendering is much faster. The GPU is used for other applications involving vector algrebra but that is another topic altogether.

Direct2D is based on COM (Component Object Model), which comes with its memory and threading model that is well understood.

Class Layout

A subset of the class diagram taken from Adam Sawicki is shown below:

The names of the classes have been truncated; the ID2D1 prefix has been elided for clarity. A few points in the above diagram are worthy of note. The unbroken arrows follow the UML convention for inheritance. All classes except Factory and SimplifiedGeometrySink and its immediate descendants inherit from Resource. The dashed arrows indicate creation relationship. For example, RenderTarget is created using Factory and Brush is created using RenderTarget

Using a factory we can create a render target from a window handle (HWND), a device context (DC), Bitmap, DirectX (DXGI) or Windows Imaging Component (WIC). The factory object is created using a D2D1 API:

hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pDirect2dFactory);

Using the Factory a HWND target can be created as follows:

ID2D1HwndRenderTarget *pRenderTarget;
hr = pDirect2dFactory->CreateHwndRenderTarget(
                    D2D1::HwndRenderTargetProperties(hwnd, size),

To draw a line a brush has to be created using the render target. RenderTarget has got drawing primitives for line, rectangle, ellipse etc. There are a number of samples that illustrate the use of various D2D features. To begin with start here, the sample application at msdn.

Writing text using D2D needs another factory object from DirectWrite. It is created as follows

IDWriteFactory *pDWriteFactory;
hr = DWriteCreateFactory(

A sample application can be found here.

Displaying an image requires yet another factory object from WIC . This object is created using CoCreateInstance:

hr = CoCreateInstance(

WICViewerD2D is a sample application that can get you started.


A brief discussion describing the many features of Direct2D (D2D) is described in Wikipedia. See Microsoft for a detailed description.


About The Sunday Programmer

Joe is an experienced C++/C# developer on Windows. Currently looking out for an opening in C/C++ on Windows or Linux.
This entry was posted in Graphics, Software Engineering, Windows. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s