Author Archives: Ovidiu Cucu

About Ovidiu Cucu

Co-founder CODEXPERT Follow me on Twitter: https://twitter.com/ovidiucucu

MFC Support for Direct2D – Part 8: Lost Render Target

As stated in the Direct2D documentation, the graphics device might become unavailable. If the device is lost, the render target also becomes invalid, along with any device-dependent resources that were associated with the device. That happens infrequently (e.g. if the user removes the display adapter), but once something bad can happen, a good programmer must handle it, except case he/she likes to see happy QA-tester faces. 🙂

Handling render target loss when using raw Direct2D interfaces

Direct2D signals a lost device by returning the error code D2DERR_RECREATE_TARGET from the ID2D1RenderTarget::EndDraw method. This case we have to release the render target and all its associated device-dependent resources (bitmaps, brushes and so on) in order to be further re-created. Here is a simplified example:

In a real program it’s a lot of work to do (discard, then re-create the render target and resources) but fortunately, the programmer’s life becomes much easier if he/she is using MFC.

Handling render target loss when using MFC

MFC does the work for you: if CRenderTarget::EndDraw function returns D2DERR_RECREATE_TARGET, the framework calls CRenderTarget::ReCreate which discards the render target, then re-creates all its associated resources. Additionally, it sends AFX_WM_RECREATED2DRESOURCES registered message. So usually, we have nothing to do in this case. At most we can handle AFX_WM_RECREATED2DRESOURCES, for example, in order to write what happened in the application’s log.

A little CD2DBitmapBrush issue

Doing tests on Direct2D resources re-creation, I have discovered a little issue in CD2DBitmapBrush MFC class. It always re-creates the bitmap brush using D2D1_EXTEND_MODE_CLAMP. Usually, we use a bitmap brush to fill an area by repeating its content (i.e. having D2D1_EXTEND_MODE_WRAP extend mode) so I fix it by overriding CD2DBitmapBrush::ReCreate, as shown below.

Demo project

The demo project is a simple MFC application which uses a Direct2D bitmap brush to fill the window’s background. In case the render target was previously lost and re-created, it also displays a message. Because the render target loss is quite hard to be reproduced, for testing purpose, you can hit the Test/Re-create render target menu item.

Lost Render Target demo

Lost Render Target demo

Download: Lost Render Target Demo (116)

Resources

Codexpert – 2017 Articles Summary

See also

Note

  • MFC (still) rulz! 🙂

MFC Support for Windows Animation

Let’s say we have to make a slide show presentation using Cross Fade effect. If the target system is Windows 10, that’s quite easy because Direct2D offers built-in Cross Fade effect.

Cross Fade effect example

Further we can set a timer to periodically change m_dWeight value from 0.0f to 1.0f. So far, so good but a better solution is to use Windows Animation interfaces that allow to implement animations that are smooth, natural, and interactive. Moreover, MFC offers wrapper classes that can make programmer’s life much more easier than in case of direct using COM interfaces.

Using MFC support for animation

To use MFC support for animation do the following:

  1. first of all we need an object of type CAnimationController which is the key class that manages animations;
  2. call CAnimationController::SetRelatedWnd in order to establish a window that will receive WM_PAINT message when animation manager status has changed or animation timer has been updated;
  3. call CAnimationController::EnableAnimationTimerEventHandler;
  4. add an animation object, in our case of type CAnimationValue as long as we only need an “animated” FLOAT to set cross-fade weight property; here is a list of MFC animation object classes defined in afxanimationcontroller.h
    • CAnimationValue
    • CAnimationPoint
    • CAnimationSize
    • CAnimationColor
    • CAnimationRect
  5. add transition(s) to animation object; here is a list of transition MFC classes:
    • CAccelerateDecelerateTransition
    • CConstantTransition
    • CCubicTransition
    • CDiscreteTransition
    • CInstantaneousTransition
    • CLinearTransition
    • CLinearTransitionFromSpeed
    • CSmoothStopTransition
    • CParabolicTransitionFromAcceleration
    • CReversalTransition
    • CSinusoidalTransitionFromRange
    • CSinusoidalTransitionFromVelocity
    • CSinusoidalTransitionFromVelocity
  6. uncomment the line containing m_spAnimCrossFadeWeightValue->GetValue(m_dWeight) in the previous example;
  7. finally, call CAnimationController::AnimateGroup.

Here is some sample code:

More details can be found in the attached demo project.

Demo project

Download: MFC Support for Animation Demo.zip (708)

Cross Fade Effect with Animation 1

Cross Fade Effect with Animation 1

Cross Fade Effect with Animation 2

Cross Fade Effect with Animation 2

Cross Fade Effect with Animation 3

Cross Fade Effect with Animation 3

Resources and related articles

MFC Support for Direct2D – Part 7: Saving to files

Once have enabled Direct2D support in an MFC application, there is no sweat to load an image from a file, by using one of CD2DBitmap constructors.
Unfortunately, we cannot find a CD2DBitmap method to save it into a image file (something similar to CImage::Save or Gdiplus::Image::Save).
No problem, we can try to do it ourselves. Here is an example:

Create and use a WIC bitmap render target

Looks good although it can be refactorized and/or can be improved for saving multiple frames, metadata and so on. However, it’s a problem: if the Direct2D bitmap (pBitmap) has been created in the window’s render target, EndDraw returns D2DERR_WRONG_RESOURCE_DOMAIN error (the resource was realized on the wrong render target). That is because CWnd::EnableD2DSupport creates a render target of type D2D1_RENDER_TARGET_TYPE_DEFAULT and as stated in the documentation, it cannot share its resources with another render targets. So, let’s replace EnableD2DSupport call with a call to another one (let’s say it EnableD2DSoftwareSupport).

Replace CWnd::EnableD2DSupport

More details can be found in the demo application attached below. Just to note that forcing software rendering for window’s render target leads in performance penalty but generally, we can live with that until finding a better solution. 🙂

Demo application

The demo application is a simple image viewer using MFC support for Direct2D and WIC to load/save images from/into into files.
DownloadSave Direct2D bitmap in a file - Demo.zip (693)

Resources and related articles

Enumerate WIC Components

In an older article, I presented a function that enumerates Windows Imaging Component codecs. Recently I had the idea to make a set of WIC Interfaces wrapper classes for easily enumerate and get info about all types of WIC components: bitmap decoders and encoders, metadata readers and writers, format converters, and supported pixel formats. Here are just few samples of code.

CComponentEnumT – a template class for enumerating WIC components

CComponentInfoBaseT – base class for all other component info classes

CPixelFormatInfoT – just one of classes getting WIC component info

Just note that, for further easier using, I’ve defined here CPixelFormatInfo, an alias for CPixelFormatInfoT<IWICPixelFormatInfo2>.

CComponentEnumT specializations and aliases

Finally, let’s see how to use these classes!

Enumerating WIC pixel formats

Quite easy, isn’t it?

Get the pixel format info

You can find the complete source code in the attached demo solution.

Demo solution

Download: Enumerate WIC Components - Demo Solution.zip (540)

Enumerate WIC Components - Demo

Enumerate WIC Components – Demo

The demo solution contains an MFC and a console project that use the classes described above in order to enumerate WIC components and get info about each one. The MFC one allows choosing the component type(s) as well as enum options (include the disabled components or enum only Windows built-in components). The console application is simpler and I wrote it just to demonstrate that my little library of wrapper classes can be used both in MFC and non-MFC applications.

Notes

  • I hope, this article can be useful.
  • If you have any observation or remarks, please do not hesitate to post a reply!

Resources and related articles

Three Ways to Find Files

Let’ say we have to implement a function that search a folder to recursively find files having an extension from a given list of extensions. This article shows three possible implementations: one using FindFirstFile and FindNextFile WinAPI functions, one using CFileFind MFC class and one using Filesystem Library.

Find files using FindFirstFile and FindNextFile WinAPI functions

Althugh this implementation uses some ATL stuff to make things easier, it appears still quite complicated.

Find files using CFileFind MFC class

Using CFileFind MFC class can make code a little bit shorter and the programmer’s life easier.

Note: IsStringInListNoCase searches a list for a string, not case sensitive. You can find its implementation in the attached demo solution.

Find files using Filesystem Library

Here it is:

Hurray, we did it in just few lines of code! 🙂
Of course, it can be used in MFC, ATL, Win32 and Console applications, as well.

Demo solution

The demo solution contains three projects, each having one of the above implementations.
Download: Find Files Samples (Visual Studio 2015).zip (659)

Resources and related articles

MFC Support for Direct2D – Part 6: Composite Effects

We can combine two or more images and/or effects by using the composite effect. Here is an example.

Drawing shadows using Direct2D composite effects

Demo project

Download: Composite Effect Demo.zip (776)
You can set the amount of blur and the color as well as the distance of the shadow.

Composite Effect Demo

Composite Effect Demo

Resources and related articles