views:

346

answers:

4

Consider the following code snippet:

HRGN clip = ::CreateRectRgn(50, 50, 100, 100);
::SelectClipRgn(context, clip);
::Rectangle(context, 0, 0, 50, 50);

Is Microsoft's clipping implementation intelligent enough to return immediately from the call to Rectangle without attempting to draw anything? I suspect it probably is but have been unable to find confirmation anywhere. The MSDN documentation for clipping is sparse on under the hood details, and Googling turns up nothing but how to use GDI clipping.

EDIT: It dawned on me that I failed to point out that this is running on Windows Mobile so the toolset I have for analysis is limited.

+2  A: 

I actually pulled my Petzold (2nd Edition) from the shelf and found that, not surprisingly, clipping was an important feature in the early versions of the Windows SDK. Back then it was necessary for performance reasons to not draw objects that are invisible. I would say that GDI clipping is in fact smart enough and will do nothing when you draw outside the clipping region.

cdonner
A: 

You can see the source code of SelectClipRgn() (Win32 api Group, many years ago, 16 or 32 bits)

I'm pretty sure that the source for SelectClipRgn won't tell me how clipping is actually handled because clipping only happens when I call Rectangle. Also which win32 api group and approximately how many years ago is many years ago.
Kevin Loney
+1  A: 

Test it.

Attach your favorite debugger (Windbg, CDB) and use the WT command to trace the execution path statistics. Compare ::Rectangle(context, 0, 0, 50, 50); with ::Rectangle(context, 0, 0, 51, 51);

Mo Flanagan
A: 

My estimate would be that it's just using a simple Cohen-Sutherland line clipping under the hood, though I'm not sure of that. However, if you have lots of data that is outside of the screen (and shouldn't be drawn) you might be better off with a higher level culling algorithm based on, for example, a Quad-tree, so that you can skip those objects all together in an earlier stage.

Jasper Bekkers