Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Direct2D vs GDI+ Issue


This question is answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: Nov 30, 2016 11:58 AM Last Post By: Richard Zarr
Richard Zarr

Posts: 74
Registered: 7/1/98
Direct2D vs GDI+ Issue  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 11, 2016 8:05 AM
We're building a graphics intensive application for mapping which renders thousands of primitives to animate a rotating globe. We've written the basic rendering in two versions... one with GDI+ and the other with Direct2D Canvas. The GDI+ is significantly faster than direct 2D (running on NVidia and Intel GPU hardware). We haven't measured it, but it appears to be roughly 10x difference in performance... I would expect Direct 2D to be 10x FASTER, not slower! Does anyone have any insight to what might be going on... here's our paint routines for both versions (see below). Anti-Aliasing is turned off for both versions for the tests...

// Direct 2D implementation
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
 Canvas : TDirect2DCanvas;
begin
 if not TDirect2DCanvas.Supported then exit;
 Canvas := TDirect2DCanvas.Create(PaintBox1.Canvas, PaintBox1.ClientRect);
 try
  Canvas.BeginDraw;
  Canvas.RenderTarget.Clear(D2D1ColorF(clBlack));
  DrawMaps(Canvas.RenderTarget);
 finally
  Canvas.EndDraw;
  Canvas.Free;
 end;
 
// GDI+ Implementation (Note: DrawBackground renders the maps to a TBitmap which is then copied to the paint box's canvas when the control requires painting)
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
 if Assigned(FBackground) then PaintBox1.Canvas.Draw(0, 0, FBackground);
end;
 
var
 G : TGP; // record of TGPGraphics objects used for drawing.  Prevents recreating them for each major method in the DrawMaps routine.
begin
 // if overlay not instantiated, bail
 if not Assigned(FBackground) then exit;
 // lock the canvas
 FBackground.Canvas.Lock;
 // draw the motion background bitmap
 try
  // create a GDI+ graphics object to associate to the BMP object
  G.GP := TGPGraphics.Create(FBackground.Canvas.Handle);
  G.GP.Clear(0); // clear the image
  // setup the font formatting
  G.GPFontFormat := TGPStringFormat.Create;
  // setup the font
  G.GPFontFamily := TGPFontFamily.Create(FontFamily);
  // font
  G.GPFont := TGPFont.Create(G.GPFontFamily, FontSize, FontStyleRegular, UnitPixel);
 
 
// all map primitives are drawn to the FBackground TBitmap here if any of the presentation is changed (e.g. globe rotated)
// the final rendered bitmap is then copied to the Paintbox1 canvas in the Paintbox1Paint method above
 
  DrawMaps(G);
 
  // release resources
 
  G.GPFont.Free;
  G.GPFontFamily.Free;
  G.GPFontFormat.Free;
  G.GP.Free;
 finally
  FBackground.Canvas.Unlock;
 end;
end


In both implementations, if the mouse is moved while the left button is down it will move the center longitude and latitude which causes a Paintbox1.Refresh to be called (DrawBackground is called first in the GDI+ implementation). So both versions are rendering on the fly while the mouse is being moved. The GDI+ version is fairly smooth, while the Direct 2D version is very slow and causes the image to not animate correctly (jitters). Thoughts?
Wojciech Pomian...

Posts: 11
Registered: 6/5/04
Re: Direct2D vs GDI+ Issue  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 6, 2016 11:27 AM   in response to: Richard Zarr in response to: Richard Zarr
I didnt' compare Direct2D with GDI+, but I did compare with plain GDI. My experience is that, with utmost care, you can get equal speed, nothing more. What you really get is quality.

The trick with Direct2D is that you create resources permanently (for form's lifetime rather than for OnPaint event duration), otherwise you pay big performance penalty. Take a look at your DrawMaps keeping this in mind.

The other thing is that Emb. wrapper, namely TDirect2DCanvas is too simplistic for Direct2D philosophy and you will not be able to approach reasonable speed anyway. Once I was trying to convince Emb. to get more serious about Direct2D, but with no results.
Richard Zarr

Posts: 74
Registered: 7/1/98
Re: Direct2D vs GDI+ Issue  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 30, 2016 11:58 AM   in response to: Richard Zarr in response to: Richard Zarr
We've done extensive testing and settled on GDI+ for now. Direct2D was also excellent in performance, but required additional code to handle scaling DPI...
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02