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.
Programming Windows consists of two main parts:
- Messaging and
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]
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.
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::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hwnd, size), &pRenderTarget );
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( DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), reinterpret_cast(&pDWriteFactory_));
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( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast(&pWICFactory_) );
WICViewerD2D is a sample application that can get you started.