Search code examples
c#optimizationgraphicscompact-frameworkbuffer

Graphics caching for Generated user controls


This question is specifically for .net CF, but it should apply to any slow, archaic system.

I've written a custom ListPanel type object that holds custom ListItem type objects. Each ListItem contains 0-5 different graphics and 1-4 different sets of text (each set of text has a different font or color). Each item is also between 480x14-480x800 in size (WxH)

The ListPanel object has a Buffer of type Bitmap which is the size of the phone's screen (usually 480x800) that is used for Double Buffering. Each ListItem also has a Buffer that is the size of the item (this is because the ListItems rarely change once they are created). The ListPanel sends its Buffer's Graphics object to all of its ListItems, and each ListItem uses it to draw its Buffer Bitmap. After all the visible items have drawn themselves with the Graphics Object, the panel's Buffer is drawn to the screen

The average list contains 10-27 items.

I have a few questions regarding this buffer caching pattern.
First, is it over kill to save this many buffers? In a list with 26 objects, there are at least 28 bitmaps in memory, which on the .net CF is a great amount of space. If this is over kill, what would be a better method for rendering the screen (taking into account that the worst case ListItem will draw 6 graphics and 4 sets of text with different fonts).

Also, there are a few reoccuring graphics (stored either as an embedded resource or copied locally to the the output directory and read in as a file) and I want to know if it's better to save those images in memory or read/close them for every use?

Finally, (assuming the items above) when should I render the ListItem's Buffer? Should I render it in the item's ctor or the first time the item enters the display? Rendering the Buffer in the ctor will cause a delay when the page is being created but will be smoother when the user scrolls. Rendering on first time display can be make for choppy scrolling, but the page loads faster and some items that aren't seen don't need to be loaded into memory

Any guidance would be excellent


Solution

  • I think you should take a look on the accepted answer for this SO question. One small fragment that applies to your case for example says: "Avoid doing a lot of work in a Form's ctor - off load it for lazy load or in a background thread"