对于许多不断变化的形状,DrawingVisual 与 Canvas.OnRender 的性能 [英] Performance of DrawingVisual vs Canvas.OnRender for lots of constantly changing shapes

查看:47
本文介绍了对于许多不断变化的形状,DrawingVisual 与 Canvas.OnRender 的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个类似游戏的应用程序,它有多达一千个形状(椭圆和线条),以 60fps 的速度不断变化.阅读了关于渲染许多移动形状的优秀文章,我使用覆盖 OnRender 以通过 DrawingContext 进行绘图的自定义 Canvas 后代.虽然CPU使用率一直很高,但性能还是相当合理的.

I'm working on a game-like app which has up to a thousand shapes (ellipses and lines) that constantly change at 60fps. Having read an excellent article on rendering many moving shapes, I implemented this using a custom Canvas descendant that overrides OnRender to do the drawing via a DrawingContext. The performance is quite reasonable, although the CPU usage stays high.

然而,文章建议 不断移动形状的最有效方法是使用大量 DrawingVisual 实例而不是 OnRender.不幸的是,虽然它没有解释为什么在这种情况下应该更快.

However, the article suggests that the most efficient approach for constantly moving shapes is to use lots of DrawingVisual instances instead of OnRender. Unfortunately though it doesn't explain why that should be faster for this scenario.

以这种方式改变实现是一个不小的努力,所以我想在决定进行转换之前了解原因以及它们是否适用于我.在这种情况下,为什么 DrawingVisual 方法会导致 CPU 使用率低于 OnRender 方法?

Changing the implementation in this way is not a small effort, so I'd like to understand the reasons and whether they are applicable to me before deciding to make the switch. Why could the DrawingVisual approach result in lower CPU usage than the OnRender approach in this scenario?

推荐答案

我以为 Petzold 在这一段中解释了;

I thought Petzold explains in this paragraph;

ScatterPlotVisual 类的工作原理是创建一个 DrawingVisual 对象每个数据点.当属性为一个 DataPoint 对象变化,类只需要改变DrawingVisual与该数据点相关联.

The ScatterPlotVisual class works by creating a DrawingVisual object for each DataPoint. When the properties of a DataPoint object change, the class only needs to alter the DrawingVisual associated with that DataPoint.

基于早期的解释;

每当 ItemsSource 属性更改,或集合更改,或DataPoint 对象的一个​​属性集合发生变化,ScatterPlotRender 调用无效视觉.这会产生一个调用 OnRender,它绘制整个散点图.

Whenever the ItemsSource property changes, or the collection changes, or a property of the DataPoint objects in the collection changes, ScatterPlotRender calls InvalidateVisual. This generates a call to OnRender, which draws the entire scatter plot.

这是你要问的吗?

顺便说一下,这个是一个相当新的高性能 WPF 教程,该图中有数万个点,它也进行了 3D 渲染和动画处理(甚至使用鼠标输入来驱动一些变换).

By the way, this is a fairly recent high-performance WPF tutorial, many tens of thousands of points in that plot, it is 3D rendered and animated also (even uses mouse input to drive some of the transforms).

这篇关于对于许多不断变化的形状,DrawingVisual 与 Canvas.OnRender 的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆