Why does using pens with dash patterns lead to a huge (!) Performance improvement in a WPF custom 2D drawing?

Hope anyone can shed some light on this so I can use pens with dashes?

I am writing a scrollable diagram ( Panel inside ScrollViewer that implements IScrollInfo ) in WPF using DrawingVisual DataContext.Draw X. I have several thousand DrawingVisual that scroll using TranslateTransform on the Panel that places them. I implemented the grid by placing the 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)); // (note: these lines are always static, they never move).

Scrolling performance is absolutely insane (i.e. DrawingVisual is instantly drawn, and scrolling is instantly). But if I use a Pen that uses dash patterns (see below, for example) to draw grid lines, the scrolling is very jerky, and the performance seems to have been reduced 100 times (estimate). Can someone explain why this is happening and how can I solve it?

Pen example with a dash:

 <Pen x:Key="PenUsingDashPatterns" Brush="Black" Thickness="1"> <Pen.DashStyle > <DashStyle Dashes="3, 3" /> </Pen.DashStyle> </Pen> 
+4
source share
4 answers

Are the pens frozen? Freezing drawing objects helps a lot in performance.

You can configure the Loaded handler and debug to check if your handles are locked. If not, call Pen.Freeze () manually on them.

Please note that freezing also makes read-only pens ... you cannot change them after freezing them.

+3
source

A workaround is possible here - if you are only drawing horizontal and / or vertical lines, you can try creating your Pen using the DrawingBrush validation DrawingBrush , for example:

  <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 can use different brushes for vertical and horizontal lines, or perhaps ImageBrush for better performance.

+6
source

You must use Perforator to delve into the performance issue. Here's a link to the MSDN site that talks about various WPF performance tools . A puncher is likely to be the tool that will help you the most, especially when deciding if lines will be drawn using a software renderer (which would be the biggest factor in giving you such poor performance).

If the problem is that they are used in the software, you may have to write your own ShaderEffect , but it will probably get tricky quickly if you are not familiar with HLSL.

+3
source

Most likely, this is due to the fact that this drawing operation is not something that can be delegated to the video card, which would make the composition in memory and blitting to the video card.

+2
source

All Articles