locked
How to cache and restore Direct2D Context to optimize performance (DirectX, Direct2D, SwapChainBackgroundPanel, SurfaceImageSource)

    Question

  • Hello,

    all examples showing how to use SurfaceImageSource creates every time a new instance of this type and render the whole contents. This is not efficient in all scenarios.

    Could you please provide an example of how to continously draw on the same instance of SurfaceImageSource using cache/buffers like it is possible in SwapChainBackgroundPanel scenarios!?

    BeginDraw -> [Restore from cache] -> Draw some primitives -> EndDraw -> Write to cache

    Please show how to handle this using SurfaceImageSource and Direct2D Context.

    Thank you in advance

    Regards

    Alex

    Wednesday, June 20, 2012 9:14 AM

Answers

  • Hi Alexander,

    I'm not sure I understand what you are asking for here. Can you please explain your overall scenario in more detail?

    SurfaceImageSource and SwapChainBackgroundPanel work quite differently and are designed for different scenarios. With SwapChainBackgroundPanel the DirectX app does its normal processing and has full control of the drawing process. The Xaml SwapChainBackgroundPanel is composed on top, but the DirectX processing is in charge.

    With SurfaceImageSource the rendering is owned by the Xaml layer. DirectX can be used to render into the ImageSource which can be used as any other ImageSource when the Xaml render thread performs its rendering.

    If you want to cache the rendering you can do so by creating your own copy of the rendered surface. Render from your cache surface to the SurfaceImageSource's surface, do whatever new rendering you want to add, and then copy that off into your cache surface to reuse next time.

    I'm not aware of any existing samples for SurfaceImageSource which do this, but I'll add it to the request list.

    --Rob

    Thursday, June 21, 2012 11:35 PM
    Owner

All replies

  • Hi Alexander,

    I'm not sure I understand what you are asking for here. Can you please explain your overall scenario in more detail?

    SurfaceImageSource and SwapChainBackgroundPanel work quite differently and are designed for different scenarios. With SwapChainBackgroundPanel the DirectX app does its normal processing and has full control of the drawing process. The Xaml SwapChainBackgroundPanel is composed on top, but the DirectX processing is in charge.

    With SurfaceImageSource the rendering is owned by the Xaml layer. DirectX can be used to render into the ImageSource which can be used as any other ImageSource when the Xaml render thread performs its rendering.

    If you want to cache the rendering you can do so by creating your own copy of the rendered surface. Render from your cache surface to the SurfaceImageSource's surface, do whatever new rendering you want to add, and then copy that off into your cache surface to reuse next time.

    I'm not aware of any existing samples for SurfaceImageSource which do this, but I'll add it to the request list.

    --Rob

    Thursday, June 21, 2012 11:35 PM
    Owner
  • Hi Rob,

    thank you for the reply.

    The overall scenario is an InkCanvas control (like it exists in Silverlight) that should be used within a C# based app. Actually I realized this using a SwapChainBackgroundPanel, but this implies a full screen ui. It's not really a problem, but it would be nice to have a control that could be placed everywhere in the xaml tree.

    So I'll definitelly try your suggestion. An example would be nice.

    Regards

    Alex

    Friday, June 22, 2012 6:42 AM
  • Hi,Alex,

    So glad to see you post.

    Actually, I meet the similar problem.My Problem is that I have a image and some InkStrokes on it and I want to save the image and InkStrokes to a file. As far as I know, There is no way in C#, So I think DirextX can do it. I plan to use SurfaceImageSource to draw the image and InkStroks, then save the SurfaceImageSource to a file. I do not know if if it is a feasible way. Could you give me some help about it , Thanks.

    W

    Tuesday, June 25, 2013 3:16 PM
  • Hi,

    I accomplished a Version of some sort of InkPanel using DirectX but finally I threw it. I realized some side effects in other Controls like FlipView. The FlipView becomes slower after the usage of SurfaceImageSource. The other disadvantage for me was, that this native component was just a small part in a large C# Project. I won't make the Project platform dependent only for this one control.

    Finally I realized the InkPanel using Canvas and adding Path's to it. For my Project it was ok.

    To save the ink strokes to file is very simple, since there is a InkManager.SaveAsync method for this. So you can do it on the managed side.

    Let me know if i can help you further.

    Regards,

    Alex

    Tuesday, June 25, 2013 4:05 PM
  • Hi, Alex,

    Thanks for you reply.

    I know the InkManager.SaveAsync method can save the ink strokes to a file easily, and the most hard part for me is,  I should compose an image and some ink strokes to a file , not only just some ink strokes. My project is also a C# project, and I tried many ways to realize it with C#, but all ways I tried is infeasible。

    I think I should use the SurfaceImageSource and Draw the Image and ink strokes on it, then save the SurfaceImageSource to a file, I am not sure if it works. Can you give me some tips about it.

    Thanks in advance,

    W

    Wednesday, June 26, 2013 2:35 AM
  • Thanks,Alex.

    The thread helps a lot.

    W

    Friday, June 28, 2013 2:19 AM