简体   繁体   English

为什么使用带有破折号模式的笔会在WPF自定义2D绘图中导致巨大的(!)性能降级?

[英]Why does use of pens with dash patterns cause huge (!) performance degredation in WPF custom 2D drawing?

Hope anyone can shed light on this so I can use pens with dash patterns? 希望任何人都可以阐明这一点,以便我可以使用带有破折号模式的笔?

I am writing a scrollable chart (a Panel inside ScrollViewer that implements IScrollInfo ) in WPF using DrawingVisual 's DataContext.Draw X . 我写一个滚动的图表(一个PanelScrollViewer实现IScrollInfo使用WPF) DrawingVisualDataContext.Draw X。 I have several thousand DrawingVisual s that get scrolled by using TranslateTransform on the Panel that hosts them. 我有几千个DrawingVisual可以通过在托管它们的Panel上使用TranslateTransform来滚动。 I implemented a grid by placing a Panel on top of it and drawing simple horizontal lines from one edge to the other using DataContext.DrawLine(pen, new Point(0, y), new Point(widthOfPanel, y)); 我通过在其顶部放置一个Panel并使用DataContext.DrawLine(pen, new Point(0, y), new Point(widthOfPanel, y));从一个边缘到另一个边缘绘制简单的水平线来实现网格DataContext.DrawLine(pen, new Point(0, y), new Point(widthOfPanel, y)); //(note: these lines are always static, they never move). //(注意:这些线总是静态的,它们永远不会移动)。

The scroll performance is absolutely insane (ie DrawingVisual's are drawn instantly and scrolling is instant). 滚动表现绝对疯狂(即立即绘制DrawingVisual并立即滚动)。 But if I use a Pen that uses dash patterns (see below for example) to draw the grid lines, then scrolling is very jerky and the performance seems to have been decreased by a factor of 100 (an estimate). 但是如果我使用一个使用短划线模式的Pen (例如见下面)来绘制网格线,那么滚动非常不稳定,性能似乎已降低了100倍(估计值)。 Can anyone explain why that happens and how I can workaround this? 谁能解释为什么会发生这种情况以及如何解决这个问题?

Example of Pen with dash pattern: 带划线图案的笔示例:

<Pen x:Key="PenUsingDashPatterns" Brush="Black" Thickness="1">
   <Pen.DashStyle >
      <DashStyle Dashes="3, 3" />
   </Pen.DashStyle>
</Pen>

Here's a possible workaround - if you're only drawing horizontal and/or vertical lines you could try creating your Pen with a checker pattern DrawingBrush such as: 这是一个可能的解决方法 - 如果您只绘制水平和/或垂直线,您可以尝试使用检查器模式DrawingBrush创建您的Pen ,例如:

  <Pen x:Key="PenUsingDashPatterns" Thickness="1">
        <Pen.Brush>
            <DrawingBrush TileMode="Tile" 
                          Viewport="0 0 6 6" ViewportUnits="Absolute">
                <DrawingBrush.Drawing>
                    <GeometryDrawing Brush="Black">
                        <GeometryDrawing.Geometry>
                            <GeometryGroup>
                                <RectangleGeometry  Rect="0 0 3 3"/>
                                <RectangleGeometry  Rect="3 3 3 3"/>
                            </GeometryGroup>
                        </GeometryDrawing.Geometry>
                    </GeometryDrawing>
                </DrawingBrush.Drawing>
            </DrawingBrush>
        </Pen.Brush>
    </Pen>   

Alternatively, you could use different brushes for verical and horizontal lines, or, possibly, an ImageBrush for better performance. 或者,您可以使用不同的画笔用于垂直和水平线,或者可能使用ImageBrush以获得更好的性能。

Are the pens getting frozen? 笔被冷冻了吗? Freezing drawing objects helps performance a lot. 冻结绘图对象有助于提高性能。

You could set up a Loaded handler and debug to see if your pens are frozen. 您可以设置一个Loaded处理程序并进行调试,以查看您的笔是否已冻结。 If not, Call the Pen.Freeze() button manually on them. 如果没有,请手动调用Pen.Freeze()按钮。

Note that freeze also makes the pens read-only... you will be unable to modify them after you freeze them. 请注意,冻结也会使笔变为只读...冻结后您将无法修改它们。

You should use Perforator to dig deeper into the performance issue. 您应该使用Perforator深入挖掘性能问题。 Here's a link to the MSDN site talking about various WPF performance tools . 这是MSDN网站的链接,讨论各种WPF性能工具 Perforator would probably be the tool that will help you the most, specially in determining if the lines are being drawn using the software renderer (which would be the greatest factor in giving you such bad performance). 穿孔器可能是最能帮助您的工具,特别是在确定是否使用软件渲染器绘制线条时(这将是给您如此糟糕表现的最大因素)。

If the problem is that they are being drawn in software, you might have to write your own ShaderEffect , but that will probably get tricky fast unless you are familiar with HLSL. 如果问题是它们是用软件绘制的,那么你可能必须编写自己的ShaderEffect ,但除非你熟悉HLSL,否则这可能会很快变得棘手。

这很可能是因为这种特殊的绘制操作不能委托给视频卡,这会迫使内存中的合成和视频卡的blitting。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM