Monday, November 14, 2011

Flash Optimization Research

Due to the working build's less-than-ideal framerate of late, we dedicated some time to researching how to optimize the performance of Flash projects, specifically on mobile platforms. One specific idea was to divide the FLA into scenes, theoretically splitting the load times between them and lowering the maximum memory usage of the game.

In Flash CS5: The Missing Manual, Fourth Edition, they explain that while scenes are excellent for organization, they provide little to no benefit for the audience of the flash project. This is because Flash doesn't treat scenes the way I had anticipated; when it publishes the game into a SWF file, it stores everything in one big timeline. This means using scenes doesn't actually provide the performance boost that I had thought it might, although it does make the file more organized and easy to work with.

In fact, the only method of optimization explained by the book is that of multiple SWF files. The idea is that if you divide the project into multiple SWF's and have one main file load the others, it reduces the amount that the user has to have loaded into memory at any given time. As a side note, this is how I (mistakenly) hoped scenes would work.

So I set off to find out more options for optimization. In my travels, I discovered a forum post that practically oozed information. What I discovered was that the methods are highly dependent on whether your performance bottleneck is due to CPU or memory deficiency. Specifically, how animated sprites are loaded or drawn on the game's canvas. Several methods were explained in the first post alone.

The first option was pixel Blitting. Essentially, what previously was a bunch of MovieClip objects is shoved into one large bitmap object. The code tells it which part of the file is needed for each object within it, and loads that. This drastically reduces the amount of RAM needed for loading all the objects in your game, but unfortunately they mention that it suffers large performance drops when used in high-resolution mobile devices like the iPad, probably due to high CPU load.

The second option was using a different MovieClip object (with bitmap data, not vector images) and moving those individually around the stage. While this method seems to work fine in situations like the iPod example above, it requires substantially more RAM than pixel Blitting. This option is closest to what Picturiffic currently does, except Picturiffic uses a fair number of vector data as well as bitmaps.

It's important to note that these two options can be combined, by using multiple MovieClip objects, while having each object contain a single bitmap that uses Blitting for the animation.

Flash has two methods of manipulating bitmap data; the draw method and the copyPixel method. In general, draw is slower than copyPixel. The draw method is used to convert vector data into bitmap data which reduces RAM usage but increases CPU load (and actually runs faster in CPU render mode), and the copyPixel method is used to simply load existing images that must be stored as BitmapData.

The author of the post also mentions the distinction of CPU vs GPU render modes for mobile devices within the publish settings. Choosing the right mode for the optimization methods you use can make a huge difference. They also mention some serious disadvantages to the Android GPU render mode; it doesn't support filter rendering for objects on the stage. This means all the glow and drop shadow effects used in Picturiffic aren't supported. In addition, any objects on or near the filtered object will also suffer decreased performance. Apparently it is still possible to achieve filter effects by using the draw method on the filtered objects off-stage, drawing it as a bitmap, and then placing it on the stage.

The author also provides helpful links at the bottom of the page.

Sources:

Flash CS5: The Missing Manual, Fourth Edition

http://www.flashgameblog.at/blog-post/2010/04/08/blitting-the-art-of-fast-pixel-drawing/
http://www.kirupa.com/forum/showthread.php?359867-How-to-Optimize-Flash-Sprite-Animations-for-Mobile
http://www.unitedmindset.com/jonbcampos/2010/09/08/optimization-techniques-for-air-for-android-apps/
http://blog.newmovieclip.com/2010/11/13/adobe-air-mobile-application-performance-optimization-on-android/


No comments:

Post a Comment