This is an addition to this similar question: Which is faster when animating the UI: a Control or a Picture?.
I am recursively iterating through a tree of shapes and drawing each visible shape to the screen. At about 2-3 generations, things handle smoothly when the user zooms in or pans in real time, but when it gets to about 4-6 generations, things slow down.
To help increase performance, would it be better to draw to a bitmap on every tree-update and then draw that bitmap every invalidation, or to go through the recursion it’s self every invalidation?
I’m working on the solution right now, but your answers are still appreciated before I finish!
Drawing to a bitmap in memory will be faster than drawing to the screen. If you’re re-drawing the entire screen on every update, then you’re much better off composing the bitmap and then blitting that bitmap to the screen. If you’re updating only part of the screen, then you have to weigh whether multiple blits to the screen will be faster than composing the bitmap and drawing the entire thing to the screen.
In general, you’re better off composing the bitmap in memory and then writing the composed bitmap to the screen. That’s usually faster, and also reduces the flicker that you’ll see if overlapping regions are updated in quick succession.
Additional info:
Depending on how large the bitmap is, you might not want to draw the entire thing on every invalidate call. One solution is to update the bitmap in memory as described above, but also keep track of the updated regions (bounding boxes). You can then combine bounding boxes and possibly do several small draw operations that will be faster than drawing the entire bitmap to the screen. This can be much more efficient, but the code becomes pretty complex. Typically, I’ve seen code that says, in effect, “If the number of regions is greater than N, or if the multiple regions cover more than X% of the total area, then just draw the entire bitmap.” The numbers for N and X are determined at program startup time.