locked
Problems with SurfaceImageSource for a custom control

    Question

  • I have a custom control which does it's own painting.  To accomplish this, I am using a Rectangle control and setting it's 'Fill' property to an ImageBrush derived from a SurfaceImageSource and then using an ID2D1RenderTarget to do the painting.  For painting bitmaps I am using ID2D1RenderTarget::CreateBitmapFromWicBitmap and am saving the ID2D1Bitmap that is returned.  The control is animated and I create and all of the bitmaps that I need and then continuously paint the scene.

    This all works well for the most part.  However I am finding that on a random basis, suddenly all of the ID2D1Bitmaps that I have squirreled away stop painting anything.  I can trace through the code, and there seems to be nothing wrong, but when I call ID2D1RenderTarget::DrawBitmap, nothing happens.  I find, however, that if I force the ID2D1Bitmap to be recreated from the file, it then works fine again.  It is almost as if the bitmap has been invalidated or something.

    This loss of the bitmaps happens very infrequently and appears to be random, but I can pretty much guarantee that it will fail within an hour or so of playing.  I am pretty sure that it is not a memory leak because I can run the app for hours at a time without any increase in memory used.  Also, if I force the bitmaps to reload, I can then continue on as if nothing had happened.

    Can anyone give me a clue as to what might be going on here?

    Thanks.

    Friday, February 8, 2013 5:59 PM

Answers

  • Thanks for the reply.  After much trial and error (not helped by the fact that it was VERY difficult to reproduce the problem), I discovered that the root of the problem was that the call to ID2DRenderTarget::BeginDraw was returning DXGI_ERROR_DEVICE_REMOVED.  In the example code upon which I based my code, it showed that in response to this error, I needed to re-create the context, which I was doing.  However I was not deleting all of the cached bitmaps associated with the old context, and those bitmaps would not work with the new context.  I fixed that and now my app recovers from this error.

    However, having to reload all of the graphic in the middle of playing creates a large hiccup in the display.  Can you explain why I get the 'Device Removed' error at seemingly random times?  Is this due to something else that I am doing wrong, or is this just to be expected?

    Thanks.

    • Marked as answer by John Gaby Wednesday, February 13, 2013 2:51 PM
    Tuesday, February 12, 2013 3:15 AM

All replies

  • Did you check if all the API return S_OK, after you call them to create a bitmap?

    Best regards,


    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 11, 2013 4:35 AM
  • Thanks for the reply.  After much trial and error (not helped by the fact that it was VERY difficult to reproduce the problem), I discovered that the root of the problem was that the call to ID2DRenderTarget::BeginDraw was returning DXGI_ERROR_DEVICE_REMOVED.  In the example code upon which I based my code, it showed that in response to this error, I needed to re-create the context, which I was doing.  However I was not deleting all of the cached bitmaps associated with the old context, and those bitmaps would not work with the new context.  I fixed that and now my app recovers from this error.

    However, having to reload all of the graphic in the middle of playing creates a large hiccup in the display.  Can you explain why I get the 'Device Removed' error at seemingly random times?  Is this due to something else that I am doing wrong, or is this just to be expected?

    Thanks.

    • Marked as answer by John Gaby Wednesday, February 13, 2013 2:51 PM
    Tuesday, February 12, 2013 3:15 AM