Is TDirect2DCanvas slow or am I doing something wrong?

后端 未结 4 1105
梦谈多话
梦谈多话 2021-02-01 18:43

While looking for alternatives to replace GDI, I was trying to test Delphi\'s 2010 TDirect2DCanvas performance in Windows 7.

I tested it by drawing a huge polyl

4条回答
  •  有刺的猬
    2021-02-01 19:26

    The problem is antialiasing is turned on. Disable antialiasing and the performance of Direct2D will be on par or faster than GDI. To do that after TDirect2DCanvas is created, make this call:

    
      FD2DCanvas.RenderTarget.SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
    

    TDirect2DCanvas is interface compatible where possible with TCanvas so it can be a drop in replacement with TCanvas, so some of the drawing routines are are a bit inefficient. For example, Polyline creates a geometry each time it is called and throws it away. To increase performance keeping the geometry around.

    Take a look at the implementation for TDirect2DCanvas.Polyline and hoist that out into your application for something like this:

    
    procedure TForm2.CreateWnd;
    var
      i: Integer;
      HR: HRESULT;
      Sink: ID2D1GeometrySink;
    begin
    ...
      D2DFactory.CreatePathGeometry(FGeometry);
      HR := FGeometry.Open(Sink);
      try
        Sink.BeginFigure(D2D1PointF(FData[0].X + 0.5, FData[0].Y + 0.5), 
          D2D1_FIGURE_BEGIN_HOLLOW);
        try
          for I := Low(FData) + 1 to High(FData) - 1 do
            Sink.AddLine(D2D1PointF(FData[I].X + 0.5, FData[I].Y + 0.5));
        finally
          Sink.EndFigure(D2D1_FIGURE_END_OPEN);
        end;
      finally
        hr := Sink.Close;
      end;
    

    And then draw it like so:

    
    procedure TForm2.WMPaint(var Message: TWMPaint);
    begin
      FD2DCanvas.BeginDraw;
      FD2DCanvas.Pen.Color := clRed;
      FD2DCanvas.RenderTarget.DrawGeometry(FGeometry, FD2DCanvas.Pen.Brush.Handle);
      FD2DCanvas.EndDraw;
    end;
    

提交回复
热议问题