4

I am developing an application which needs to draw around 70k rectangles in form of a grid (one grid 40x250 and one 250x250). Not all rectangles will be visible on a screen at the time.

After reading http://msdn.microsoft.com/en-us/magazine/dd483292.aspx I created simple control (inherits from FrameworkElement, parts are listed below) which uses DrawingVisual descant to create one column of rectangles.

// Removing items after collection change
private void RemoveItems(IList oldItems) {
    foreach (var oldItem in oldItems) {
        var visualChild = _visuals.FirstOrDefault(v => v.Data == oldItem);
        if (visualChild != null) {
            _visuals.Remove(visualChild);
            RemoveVisualChild(visualChild);
        }

        _translateTransform.X -= 8;
    }
}

// Adding items after collection change
private void AddItems(IList newItems) {
    foreach (var newItem in newItems) {
        var newArray = (byte[]) newItem;
        var visual = CreateVisual(newArray);
        _visuals.Add(visual);
        AddVisualChild(visual);
        _col++;
    }

}

// Creating visual element
private StripeVisual CreateVisual(byte[] data) {
    var result = new StripeVisual {
        Data = data,
        Transform = _translateTransform
    };

    using (var dc = result.RenderOpen()) {
        for (int i = 0; i < data.Length; i++) {
            dc.DrawRectangle(_brushes[data[i]], _strokePen, new Rect(_col * 8, i * 20, 8, 20));
        }
    }

    return result;
}

All brushes are frozen (members of Brushes class) and _strokePen is frozen. New vertical stripe is added to collection every 200ms.

Program has been run on 3 computers:

  1. Notebook: i7-2670QM, GeForce GT 540M/Intel HD Graphics 3000, 4GB RAM, Win7 x64, DirectX11,
  2. PC: Core2Duo E7400, Radeon HD 4800, 3GB RAM, Win7 x64, DirectX11,
  3. PC: i3 3,07GHz, Inter HD Graphics, 3.3GB RAM, Win7 x86, DirectX11.

The problem is that on my first computer program runs smoothly and without big impact on CPU (usage between 1-10%). On both second and third computers programs performance is unacceptable: with about 30x100 grid it takes up to 50% of processor and everything runs very slow. Program runs slowly even when only small part of grid is visible.

It looks like program is using software rendering (rendering thread is using 50% of processor), but RenderCapability.Tier >> 16 returns 2, and RenderOption.ProcessRenderMode is set to Default. Also there is no key in [HKCU/Software/Microsoft/Avalon.Graphics] that would disable hardware acceleration.

Is it possible to run this application smoothly listed computers? What else can I do to maximize performance of this program?

Update

Setting DrawingVisual.CacheMode = new BitmapCache(); helped a lot. Does it mean that CPU<=>GPU communication was the bottleneck?

aadam
  • 713
  • 5
  • 18

0 Answers0