Bug Vanquisher

23 April 2007

Deep drive into WPF graphics internals Part 1

Filed under: WPF — Tanveer Badar @ 12:28 PM

[I cannot make up my mind how to best start this post. Instead of delaying it for another month, I opted to post it anyway. <warning>Take it with a pound of salt, I may be terribly wrong in this article.</warning>]

A few pictures from MSDN should be the best start. They show the interaction between Win32 provided API, DirectX and WPF.

  1. How WPF leverages Windows Platform

  2. The Visual tree

  3. How WPF draws your application.

These articles are mainly focused on graphics implementation in PresentationFramework, PresentationCore and WindowsBase. The unmanaged part will discuss milcore.dll, dwm.exe, dwmapi.exe, user32.dll’s redirection APIs.

To dig into all of this, we need a working application. This code will be used through out this post.

using System;
using System.Windows;

namespace WPF
     class App : Window
        public static void Main( )
            ( new Application( ) ).Run( new App( ) );

         protected override void OnRender( System.Windows.Media.DrawingContext drawingContext )
            System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace( System.Threading.Thread.CurrentThread , true );
             Console.WriteLine( st.ToString( ) );
            base.OnRender( drawingContext );

As the name suggest, OnRender is the only method called to do all the drawing. A debug tip shows that it is really an object of type System.Windows.Media.VisualDrawingContext.

Fire up Reflector and have a look at this class:

As we see VisualDrawingContext does not contain many things. _ownerVisual is any Visual having an associated VisualDrawingContext and CloseCore just calls RenderClose on the contained Visual. DrawingContext is the base class which defines the characteristics of what can be drawn and how. DispatcherObject is of no concern here, but in the going, it is essentially a wrapper around System.Threading.Thread and provides some services which check whether, through VerifyAccess and CheckAccess, the caller is on same thread as UI thread or not.

Furthermore, DrawingContext is abstract, therefore, an instance can never be created. RenderDataDrawingContext has an internal constructor. Same goes for its instance too. A transitive closure with the help of Reflector shows that there is no other way to create an instance publically without going through RenderOpen, which returns an instance of VisualDrawingContext.

You may have noticed by now that I skipped RenderDataDrawingContext’s description. Well that’s the topic for next post.


  1. […] the blogs I frequently read (a mighty >86 item list) which you will find very informative, a deep drive into the Visual system and milcore.dll of WPF, the new APIs of user32.dll concerning off-screen […]

    Pingback by Bug Vanquisher More coming up in a couple of days « — 16 May 2007 @ 10:20 AM

  2. There are not a lot of people talking about this class. Consider that PresentationCore.dll on it’s own has most of the classes required to write a full fledged user control.

    Have you considered exclusively developing inside PresentationCore.dll as a means of increasing performance and flexibility? A good MVP implementation could go a long way with the DrawingContext api.

    The last mile to this implementation would be compatibility with silverlight. That wouldn’t be too difficult if you standardized on Geometry data objects for your render data. Currently the only common ground between silverlight and WPF is shapes which is framework level.

    Could you possibly comment on PresentationCore.dll as a public api. Do you believe that API will be supported for generations to come? Silverlight has a UIElement class however it seems to do a lot with Framework elements rather than “core” classes.

    Comment by Rob FireGarden — 18 March 2008 @ 9:58 PM

  3. I had its part 2 written, then, framework code got published by Microsoft. But your idea is sound, I’ll give it more thought.

    Comment by Tanveer Badar — 18 March 2008 @ 11:35 PM

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: