Introducing Direct2D

Kam VedBrat

Microsoft Corporation

October 2008

Applies to:

Windows® 7

Summary: Introduces Microsoft Direct2D, a new API in the Windows 7.

Direct2D provides Win32 developers with the ability to perform 2-D graphics rendering tasks with superior performance and visual quality.

Legal Notice

This is a preliminary document and may be changed substantially prior to final commercial release of the software described herein.

The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication.

This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR

STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT.

Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.

© 2008 Microsoft Corporation. All rights reserved.

Microsoft, MS-DOS, Windows, Windows NT, Windows Server, Windows Vista, Active Directory, ActiveSync, ActiveX,

Direct3D, DirectDraw, DirectInput, DirectMusic, DirectPlay, DirectShow, DirectSound, DirectX, Expression, FrontPage,

HighMAT, Internet Explorer, JScript, Microsoft Press, MSN, Outlook, PowerPoint, SideShow, Silverlight, Visual Basic, Visual

C++, Visual InterDev, Visual J++, Visual Studio, WebTV, Windows Media, Win32, Win32s, and Zune are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.

The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

Ошибка! Используйте вкладку "Главная" для применения Title к тексту, который должен здесь

отображаться.

Introduction

There are three primary motivations behind creating a new 2-D graphics API in Microsoft®

Windows®: keeping pace with the rapidly increasing level of visual richness that Windows users are accustomed to, enabling developers to write 2-D rendering code that scales directly with the graphics processing hardware of the PC it is running on, and enabling developers to write code for rendering 2-D graphics that can run in the context of a service.

End-user expectation levels around visual fidelity in digital experiences have been rising dramatically in recent years. This trend is reflected in consumer electronics: GPS devices, media playback devices, mobile phones, and digital cameras are all delivering consistently richer experiences year after year. This can also be seen in the diversity of graphical content in film, television, video games, and on the Web. To keep pace with these changes, developers are consistently getting asked to take their existing Windows applications to the next level of visual richness.

Graphics processors in modern Windows PCs have also been steadily evolving, driven by advances in video game graphics, as well as parts of the Windows experience like Windows Media Center and

Aero®. Some applications on Windows are able to take advantage of modern GPUs using Microsoft

Direct3D® and Windows Presentation Foundation (WPF). While Direct3D serves high-end 3-D graphics applications well, and WPF addresses the needs of .NET developers, there are gaps for developers who have large existing codebases of rendering code based on GDI and GDI+, or wish to incorporate high-quality 2-D graphics in their Direct3D-based applications.

Finally, the need for a graphics API that can be used in the context of a service has become an emerging requirement for developers working in enterprise and Web-development scenarios.

Existing rendering APIs have been very focused on client-side rendering in a single user session. As such, they can fall short in areas of robustness and scalability when used in a service context. A new API is required to address this.

Direct2D Overview

Direct2D is a hardware-accelerated, immediate-mode 2-D graphics API that provides high performance and high quality rendering for 2-D geometry, bitmaps, and text. The Direct2D API is designed to interoperate well with developers existing code that uses GDI, GDI+, or Direct3D.

Direct2D is designed primarily for use by the following classes of developers:

Developers of large, enterprise-scale, native applications, such as Microsoft Office®

ISVs creating control toolkits and libraries for consumption by downstream developers

 Developers who require server-side rendering of 2-D graphics

 Developers primarily using Direct3D who desire simple, high-performance, 2-D, and text rendering for menus, user-interface elements, and heads-up displays

High Performance with Maximum Availability

Direct2D is implemented as a user-mode library that is built using the Direct3D® 10.1 API. As such, Direct2D applications will benefit from hardware-accelerated rendering on modern mainstream GPUs. Hardware acceleration is also achieved on older Direct3D 9 hardware using

Direct3D 10-level-9 rendering. This combination provides excellent performance on the vast majority of graphics hardware on existing Windows® PCs.

For scenarios where the use of hardware acceleration is either not possible or not desired, Direct2D includes a high-performance software rasterizer. When rendering in software, applications using

© 2008 Microsoft Corporation. All rights reserved.

1

Ошибка! Используйте вкладку "Главная" для применения Title к тексту, который должен здесь

отображаться.

Direct2D will experience substantially better rendering performance than GDI+ with similar visual quality. The software rasterizer is also designed for use in a service context.

Content rendered using Direct2D can also be displayed remotely by using the Remote Desktop

Protocol (RDP) infrastructure in the Windows 7 operating system. Developers have the option of selecting whether rendering should be handled by the GPU on the display computer or rendered locally and transmitted as bitmaps. This choice can be made based on required fill-rate and primitive count characteristics of the content being rendered. When the display computer is running an operating system older than Windows 7, remote display rendering is performed by transmitting bitmaps over the network.

By providing a single API that combines the performance of Direct3D and great availability via software fallback, remote desktop, and service rendering, Direct2D enables developers have a single implementation for high-performance rendering in many different scenarios.

Architectural Layering of Direct2D

Visual Quality

Applications that use Direct2D for graphics can deliver much higher visual quality than what can be easily achieved using GDI. Direct2D uses per-primitive anti-aliasing to deliver smoother-looking curves and lines in rendered content. There is also full support for transparency and alpha-blending when rendering 2-D primitives.

Aliased vs. Anti-Aliased Curves and Lines

Developers can also specify aliased rendering of vector graphics for scenarios where it is required.

This can be used in scenarios where snapping to hard pixel boundaries is required, such as user interface elements like pointers or rulers, if matching the style of output of GDI is desired, or if anti-aliasing will be performed downstream in the rendering process via MSAA or some other mechanism.

© 2008 Microsoft Corporation. All rights reserved.

2

Ошибка! Используйте вкладку "Главная" для применения Title к тексту, который должен здесь

отображаться.

Interoperability

Integrating Direct2D-based rendering is made easier for developers through surface-level interoperability with GDI and Direct3D. Applications that primarily render their content with GDI,

GDI+ or Direct3D, can begin by using Direct2D to render specific areas of their application, and over time move to a model where rendering is performed primarily via Direct2D, using GDI primarily for plug-ins or legacy extensibility.

The Direct2D API

Direct2D is available to developers for use with C or C++, similar to Direct3D®. The API exposes a set of functionality including:

Render targets for display and off-screen rendering using Direct2D, Direct3D, or GDI

Objects for managing drawing state such as coordinate space transforms and aliasing modes

Representations for geometric data, and functions for geometric processing

 Rendering functionality for bitmaps, geometries, and text

 Resources for use in rendering such as brushes

 Provisions for using graphical content created using GDI or Direct3D

Access to Direct2D functionality begins with the ID2D1Factory interface. The ID2D1Factory is used for creating render targets, objects to represent drawing state, and objects to represent shapes as geometric data.

Render Targets

Render targets are used to create resources for drawing, and to perform actual drawing operations.

There are several kinds of render targets that can be created for various purposes. The

ID2D1RenderTarget interface is the base interface for rendering. There are several interfaces derived from ID2D1RenderTarget that are used to render graphics in different ways:

ID2D1HwndRenderTarget renders content to an HWND.

ID2D1DCRenderTarget is for drawing to a GDI device context.

 Bitmap render targets render content into an off-screen bitmap for use with WIC, DXGI, or as a

D2D bitmap resource.

 An ID2D1RenderTarget for rendering to a DXGI surface can be created using

CreateDxgiSurfaceRenderTarget

Render targets can create new render targets that are compatible with themselves, which is useful for intermediate, off-screen rendering while retaining the various render-target properties that were set on the original.

Render targets have default behavior for hardware acceleration and remote display. Developers can specify whether rendering that occurs on a render target should happen using hardware acceleration, and also whether rendering for remote display should happen on the local or remote computer. Render targets can be set up for aliased or anti-aliased rendering. For rendering scenes with a large number of primitives, a developer can also render 2-D graphics in aliased mode and use D3D's MSAA to achieve greater scalability.

Render targets can also group drawing operations into layers represented by the ID2D1Layer interface. Layers are useful for collecting a set of drawing operations to be composited together in the course of rendering a frame. For some scenarios, this can be a useful alternative to rendering to a bitmap render target and then reusing the bitmap contents as its allocation costs are dramatically lower than that of an ID2D1BitmapRenderTarget.

© 2008 Microsoft Corporation. All rights reserved.

3

Ошибка! Используйте вкладку "Главная" для применения Title к тексту, который должен здесь

отображаться.

It is also possible to render using GDI on a Direct2D render target by calling QueryInterface on a render target for IGdiInteropRenderTarget, which has GetDC and ReleaseDC methods on it that can be used to retrieve a GDI device context. Rendering via GDI will be possible only if the render target was created with the D2D1_RENDER_TARGET_USAGE_GDI_COMPATIBLE flag set.

This is useful for applications that are primarily rendering with Direct2D but have an extensibility model or other legacy content that requires the ability to render with GDI.

Geometry

Data to describe shapes is handled using the ID2D1Geometry interface. Direct2D has interfaces for representing simple and complex geometries. The factory exposes methods for creating objects to represent rectangles, rounded rectangles, and ellipses. More complex shapes can be created using the ID2D1GeometrySink interface to specify a series of figures. The ID2D1GeometrySink is passed to the Open method of an ID2D1PathGeometry to generate a complex geometry.

ID2D1GeometrySink can also be used along with the DirectWrite API to extract path outlines of formatted text for artistic rendering.

A set of geometric operations is also exposed off the ID2D1Geometry object for various purposes. Methods are present for creating new geometries by widening or simplifying existing geometries, or generating the intersection or union of multiple geometries. Analysis methods are available for determining whether geometries are intersecting or overlapping, retrieving bounds information, computing the area or length of a geometry, or interpolating locations along a geometry. Direct2D also provides the ability to create a mesh of triangles that is tessellated from a geometry.

Once a desired geometry object is created, it can be rendered using the DrawGeometry and

FillGeometry methods.

Bitmaps and Text

Direct2D provides the ability to create bitmap objects via Windows Imaging Component (WIC) and formatted text objects via DirectWrite.

Bitmap resources can be loaded via WIC and then used to create an ID2DBitmap via the

ID2D1RenderTarget’s CreateBitmapFromWicBitmap. Bitmaps can also be created from inmemory data that was set up through independent means. Once a bitmap has been created it can be drawn via the render target’s DrawBitmap method or with a bitmap brush.

Because creating bitmap resources on hardware render targets is often an expensive operation,

Direct2D can update the contents of a bitmap (or portion of the bitmap) via ID2DBitmap’s

CopyFromBitmap, CopyFromRenderTarget, and CopyFromMemory. Using these methods can potentially save the costs associated with additional GPU texture allocations.

Render targets have three methods for drawing text using Direct2D: DrawText,

DrawTextLayout, and DrawGlyphRun. DrawText should be used for the simplest scenarios involving rendering a string of Unicode text with minimal formatting. More complex layout and typographic flexibility is provided via DrawTextLayout, which takes an IDWriteTextLayout object as input to specify the content and formatting to be rendered. For scenarios where precise control of glyph-level layout is required, DrawGlyphRun can be used in conjunction with the measurement facilities provided by DirectWrite.

The shared glyph rendering infrastructure that is used by each of the Direct2D drawing APIs has superior performance characteristics to that of GDI. Direct2D uses the GPU in the ClearType® text rendering process to accomplish the same operations with lower CPU usage than GDI, and better scalability as more GPU processing power is available.

© 2008 Microsoft Corporation. All rights reserved.

4

Ошибка! Используйте вкладку "Главная" для применения Title к тексту, который должен здесь

отображаться.

Summary

Microsoft® Direct2D® enables developers to build 2-D graphics features in their applications that deliver improved visual quality over GDI, and performance characteristics that scale with modern

GPUs. The Direct2D interoperability model enables developers to selectively migrate portions of their application at a time alongside GDI, GDI+, or Direct3D-based rendering.

For more information, refer to the Windows® Software Development Kit (SDK) for Windows 7 and

.NET Framework 3.5 Service Pack 1: Pre-Beta distributed to PDC attendees and the PDC 2008 hands-on lab exercises.

© 2008 Microsoft Corporation. All rights reserved.

5