转 未来的GDI:WPF技术纵览

现在,Windows Presentation Foundation(WPF)这个名词已经不再新鲜,至少对很多关注前沿技术的开始人员而是这样的。Windows Vista已经正式发布,新的开发平台.NET Framework 3.0也开始逐步进入到人们的视野之中,并得到了更多的焦点。

   作为下一代的图形系统,WPF所带来的新概念、新的技术也相当的庞大、复杂。现在,在.NET相关领域的开发中,用户界面主要由Windows Forms或者Web Forms实现。虽然Windows Forms已经出现了很多年,也提供了快速、高效的功能。但是,Windows Forms开发的UI界面相对比较单调,而且经过.NET Framework封装后的开发库在自定义和各种底层功能上缺少真正的支持。下面,我们将看看WPF是如何提供一种快速、高效、而且更加震撼的用户界面开 发技术的。

  WPFWin32WinForms

  GDI是当今Window桌面应用程序的主流图形库,GDI 形系统已经形成了很多年。它提供了2D图形和文本功能,以及受限的图像处理功能。虽然在一些图形卡上支持部分GDI的加速,但是与当今主流的 Direct3D加速相比还是很弱小。GDI+开始出现是在2001年,它引入了2D图形的反走样,浮点数坐标,渐变以及单个象素的Alpha支持,还支持多种图像格式。但是,GDI+没有任何的加速功能(全部是用软件实现)

  熟悉Windows XPWindows Forms技术的朋友可能都知道,在Windows Forms中也支持一种高级的合成,支持半透明的表单,但是,在Windows Forms中的半透明等技术是伪装实现。比如,显示半透明窗口时,父窗口的背景是通过子窗口实现的。

  在WPF中,引入一种新的图形合成引擎。可以提供更高丰富的图形显示合成,而且不会有任何的窗口闪烁问题。这种新的显示提交引擎与传统的Win32Windows Forms相比主要有两个优点:

  1)每个窗口元素都可以任意的交替重叠;
  2)所有的合成操作都是在Direct3D中的离屏表面(Off-screen Surface)中实现的;

 

在这种新的方式中,元素的背景变得不再是必须的。比如,我们希望在窗口中显示一个文字字符串,通常,在Win32或者Windows Forms中显示的效果如同一个静态文本标签一样:它具有系统默认的控件背景色。而在WPF中,文本字符串会直接画到目标位置,而不管目标区域的背景颜 色。

  在Windows Vista中,GDIGDI+仍然支持,但是基本上没有任何功能性的改进。对GDIGDI+的改进主要集中在安全性和客户相关问题上。在 Windows Vista中,GDIGDI+WPF并行存在,WPF的所有提交都不依赖于GDIGDI+,而是Direct3D。并且所有的Primitive 是通过Direct3D的本地接口实现的。WPF系统中milcore组件,它以非托管代码的形式封装了与Direct3D交互的所有功能。由于WPF 大部分代码都是以托管代码的形式存在的,所以WPF中有很多托管、非托管的交互。当然,在一些图形卡不支持WPF所需要的功能时,WPF也提供了稍微低效 的软件实现来支持在某些PC上运行WPF应用程序。

  在WPF的实现中,每个画图对象都对应于一个离屏表面。显示窗口时不是直接地在屏幕显示区域进行的,而是利用显示卡提供的额外的离屏表面内存。当画图操作在离屏表面中完成了之后,合成提交引擎再把每个需要显示离屏表面合成为一个单独 的可显示表面。这种显示方式不再需要我们用传统的双缓冲绘图来避免窗口闪烁,这项功能已经有WPF默认实现了。

  同时,离屏表面的使用也可以更加方便的利用显示卡GPU(图形处理单元,Graphics Processing Unit)来实现放缩、旋转、变换等等。这也就意味着这些普通的3D功能也能很方便的在WPF中实现了。

   关于离屏表面需要注意一点:不要错误的理解为WPF一定需要使用现代的图形显示卡(具有丰富功能的3D显示卡)。的确,在将来实现真正的3D用户界面是 可能的。但是,WPF做了很大的努力来充分的利用GPU,把3D的元素抽象、封装后,让人们用类似2D的编程来实现3D的功能。

  当前版本的WPF中,对一些Win32功能还没有很好的支持,比如WMF/EMF文件,单个象素宽度的线条等等。对于这些需求还需要使用GDI/GDI+来实现。

WPF - GDI/GDI+的末路, Direct3D的盛宴

GDI/GDI+ 是什么

GDI is the main graphics library for Windows applications today. It provides 2D graphics and text functionality, as well as limited imaging functionality. There is some level of acceleration support in graphics cards for GDI, but it is almost negligible nowadays (especially when compared to the level of investment in Direct3D acceleration).

GDI+ was introduced around 2001 and it brought anti-aliased 2D graphics, floating point coordinates, gradients, per-pixel alpha value support, and support for multiple image formats to the Windows platform, but no hardware acceleration abstraction (all rendering is done in software). Windows Form provides managed wrappers on top of the GDI+ interfaces.

GDI/GDI+的前途

如果微软按下面的计划执行,那么GDI/GDI+在未来不会有什么发展,可能会逐渐淡出主流了。

We continue to put a lot of effort in ensuring compatibility for GDI and GDI+ (and Windows as a whole I might add) for every Windows release, and both technologies will continue to be supported, but no functionality improvements are planned in these technologies for future Windows releases. Of course, we will continue to address security and customer issues; if any arise (the GDI and GDI+ development work takes place as part of the Avalon organization – we invest significant time on GDI/GDI+ work).

WPF如何实现的

WPF 跟GDI/GDI+是不同技术实现。它是对DirectX的managed封装,准确的说是Direct3D,所有2D的表现也都是通过其实现。由于Direct3D是基于硬件的Native API,而WPF是managed code,因此WPF存在managed code于native资源的交互开销。这一点很象GDI+是对native 的GDI的封装,同样存在类似开销。因此,对于性能要求甚高的3D游戏,直接使用DirectX要比WPF 在性能表现上要强。

Windows Presentation Foundation does not depend on either GDI or GDI+ for its rendering (GDI, GDI+, and WPF coexist side by side). The mainstream scenario has Windows Presentation Foundation doing all its rendering through Direct3D, for all primitives (2D, text, imaging, video, and of course 3D), through Direct3D’s native interface (the managed/native transition takes place within the Windows Presentation Foundation stack, as the bulk of WPF was written in managed code).

正由于WPF依赖于Direct3D,因此它的图形的表现依赖于硬件 – 显卡。如果,硬件本身不支持一些图形特效的硬实现,那么会不会对WPF编程带来复杂性,代码中还需要判断硬件是否适用?

不必担心,既然是对Direct3D的一种更高层次的封装,开发人员可以不必考虑硬件:硬实现不支持(GPU的工作),那么会有软实现(CPU的工作)

Windows Presentation Foundation takes advantage of hardware rendering as appropriate. It uses Direct3D to provide accelerated rendering on DirectX 7 or above, with further optimization for graphics cards with DirectX 9 and Pixel Shader 2.0 hardware. For machines without these capabilities, Windows Presentation Foundation uses software rendering, a CPU-based, SSE and SSE2 optimized rasterizer. This is also used when Windows Presentation Foundation is unable to render something using the hardware pipeline, and guarantees that rendering output is available across all machine hardware configurations.

As a result, Windows Presentation Foundation has both hardware and software pipelines, both with difference performance characteristics. This could make application performance very difficult to predict, since there are two code paths. In practice, hardware is reliably faster; for a given application, it's simply a matter of setting a minimum requirement. Windows Presentation Foundation supports simple runtime hardware detection and allows you to dynamically tailor your application for the platform it's running on using resource dictionaries.

Will Windows Presentation Foundation require a certain level of video card?

Windows Presentation Foundation will take advantage of the latest graphics cards, but it can also use software emulation if a system doesn't have a supported video card.

“软实现”是需要耗费更多的CPU资源,因此硬件对于WPF在性能方面的表现有很大影响。那么发挥WPF价值的硬件有什么要求?

What graphics card should I use for Windows Presentation Foundation?

Any high performance DX9 Pixel Shader 2.0 card will give good results. The fastest PS 2.0 card with the most memory your bank account can afford is the ticket.

若干年前上演过 Win+Intel的神话,今天一场WPF+显卡硬件厂商的盛宴又要开始了。

 

posted on 2010-06-28 10:47  chuncn  阅读(931)  评论(0编辑  收藏  举报

导航