Slow CreateCompatibleBitmap() and BitBlt()
-
I am experiencing extremely slow performance when using the MFC CBitmap::CreateCompatibleBitmap() and CDC::BitBlt() to copy compatible bitmaps between device contexts. Sometimes the calls are extremely fast, as expected from the wealth of documentation that recommends using memory bitmaps to optimize graphics. However, they are frequently slow, meaning that a single call to the either method takes approximately 0.5 seconds (sometimes more), which is obvious to our applications’ users. I need to understand why some calls to these two methods are so slow and what we can do to prevent this from happening. The zip file I have attached contains a DevStudio 6.0 MFC application with a few lines of code added to show the problem. Test Procedure. 1) Run the application. 2) Resize the view - larger windows fail sooner. 3) Use File/DC Test to bring up the test dialog 4) Press the Blit button repeatedly, and look at the time reported in the message on the dialog. After a while, it will suddenly be much slower. Test Implementation. The application stores a number of CDC's and CBitmap's that are all the same size and compatible with the CView's CDC. On the first blit button press there is one CDC/CBitmap pair, then two on the second, etc. Each time the button is pressed, the old CDC/CBitmap's are destroyed, and new ones are created. The first CDC is BitBlt’ed to the second, the second to the third, etc, and finally the top CDC is blitted to the screen. Basically, this is done in two simple functions, controlled from the dialog callback: CDeviceContextBufferCollection::CreateBuffer() - 5 Lines of Code CDeviceContextBufferCollection::Copy() - 7 Lines of Code CDCDrawer::OnBlit() - < 20 Lines All the other code is for house keeping, and there is not much of it. The application was created with the MFC application wizard and is largely unchanged. The times for each method are measured using a high performance timer, and are shown on the dialog. The time for each BitBlt() is also measured and sent to the TRACE window and can be seen when debugging. For the first few executions, both calls are fast. However, at some point their performance will degrade. The trace output will show that some BitBlt()s are still very fast and some now are very slow. Note that all of these timed BitBlt()s are from one memory bitmap to another (i.e. not to the screen) and all the CDC's and CBitmap's are compatible (i.e. there are no DIBs). Sample Execution: Machine Info: P4 1.4GHz, Screen size 1600x1200, 16-bit color depth