cacheAsBitmapOptions

I just filed a feature request for Flash player, asking for a cacheAsBitmapOptions property on display objects. The intent behind this is to make the current cacheAsBitmap functionality more useful. Currently, when you use cacheAsBitmap, the cache is redrawn any time you modify the transform the display object in any way other than translation (moving it in x/y). For example, if you rotate, alpha, or scale your display object it forces the cache to redraw.
It’s often useful to maintain a cache through other transformations, such as rotation, changes to alpha, and scaling. I frequently implement this functionality manually by drawing the display object to a BitmapData, and swapping the Bitmap for the original Sprite. This works, but it’s very clumsy, and requires a lot of manual management.
In conjunction with this, it would be useful to specify the resolution that the bitmap cache is created at, as a scale multiplier of the DO on stage.
The cacheAsBitmapOptions feature I am proposing would let you programmatically specify what transformations would result in a cache redraw. For example:

import flash.display.CacheAsBitmapOptions; // name based on NetStreamPlayOptions
var myOpts:CacheAsBitmapOptions = new CacheAsBitmapOptions();
myOpts.updateOnRotation = false;
myOpts.updateOnAlpha = false;
myOpts.updateOnScale = false;
myOpts.cacheScale = 2;
characterSprite.cacheAsBitmapOptions = myOpts; // passing null would revert to defaults.
otherSprite.cacheAsBitmapOptions = new CacheAsBitmapOptions(2, false, false, false);

If you think this functionality would be useful for your projects, feel free to vote it up. The bug is filed in the Flash Player bugbase as bug #FP-2524.
On Monday I will post a blog entry walking through how to implement this type of functionality manually, with a handy helper class.

Grant Skinner

The "g" in gskinner. Also the "skinner".

@gskinner

15 Comments

  1. That’s a good idea, i hope you will be listened by Adobe

  2. you got my vote. If adobe wants to improve there platform for games they should put there time in performance enhancements like this.

  3. Flash could definitely do a better job caching with more information from programmers, like these settings.

    I’m posting a related feature request, which is timeline caching. ( http://bugs.adobe.com/jira/browse/FP-2527 ) MovieClip timelines that are short and loop create and destroy lots of display objects in quick succession (making garbage), but these objects cannot be cached because MovieClip timelines can also take a long time to loop, or be set to never loop. A cacheTimeline property would let us specify whether the MovieClip should attempt to cache its timeline objects.

  4. What is it that you’re trying to accomplish? It is misguided to believe that the same performance benefits cacheAsBitmap achieves for translations will carry over to rotations and scaling.

    The cacheAsBitmap feature is fast because it redraws a display object pixel for pixel on the screen, no calculations. When a bitmap is rotated or scaled those pixels have to be recalculated and anti-aliased into ‘screen-pixels’, which of course are always vertically and horizontally arrayed. In fact, the Flash Player’s vector drawing processes are so fast that it’s most often faster to let vectors recalculate and redraw when rotating or scaling than to rotate or scale a bitmap. In summary only the alpha would benefit from the extra option – your manual work and any efforts by Adobe to cache rotating and scaling display objects are less effective.

    The real next step is to tie into hardware acceleration used for 3D graphics and allow 2D objects to receive full advantage.

  5. Hi Grant, yeah this is on my list too! I did actually write a superCacheAsBitmap class that did this ages ago – it even takes into account the multiple transformations of the clip’s parents and readjusts the bitmap to fix. Matrix hell! I never properly converted it to AS3 but I’m sure it’d be quite easy. The main difficulty I had was trying to figure out what the API should be.

    And embarrassingly, I don’t think I ever got around to releasing the source… but more info about it here : http://www.sebleedelisle.com/?p=32

    cheers!

    Seb

  6. Tyler – yes, this is correct for simpler vectors. However for more complex and/or deeply nested display structures, bitmap caching through transforms provides a significant performance benefit (obviously scaling with the complexity of the vectors). Text is a good example.

  7. That’s good idea for me.

    I hope can see this at fp11 🙂

  8. +1 Vote, and I can’t wait to see your caching class.

    Although when I created my caching class, I did it not just because of the update issue, but also because cacheAsBitmap = true forces pixelSnapping on for that display object, which looks very bad when an object is moving slowly. Could disabling that (seemingly pointless) binding be part of the new feature?

    -Matt

  9. I would loved to have this feature. I wrote something similar too, to get the performance and behavior that I was looking for. My biggest concern was the transparency, not wanting every nested sprite to get transparent and intersecting eachother, but the complete cached bitmap as a solid object.

    +1 Vote for me. =D

  10. Don’t forget to vote for gotoAndStop():

    https://bugs.adobe.com/jira/browse/FP-901

    which is also an important feature of Flash.

  11. I do like the options idea, as it will automate the process, and work for timeline animations as well as programmatic tweens.

    I’m not quite sure how the alpha flag would work – would it basically ignore alpha changes, or apply alpha changes to the cached bitmap?

    A simpler solution might be to just being able to disable the redraw altogether, and re-enable it when a transition is done.

    Most of the time, it’s probably ok if not desired to completely ‘freeze’ the visual state, even if the content changes, during a transitional animation, then go back to the ‘live’ view. In essence that’s what you do when you take that snapshot currently.

  12. I’d like to see more control over the bitmap caching as well. One other option you didn’t mention is the ability to smooth the resulting bitmap. Without smoothing, using filters (which forces cacheAsBitmap) causes very jerky low quality motion (when scale and rotation not involved) at slow speeds because it snaps to pixels. Basically I want to see CacheAsBitmapOptions.smoothing.

    https://bugs.adobe.com/jira/browse/FP-2539

  13. This is on my wishlist too. I would love to have this. My vote is in favor of this feature.

  14. Is it monday, yet?

  15. Grant,

    Can you not maintain a cached state through rotation by setting sprite.z = 0 ?

    I know that is not a solution, but I believe it solves the rotation problem in flash 10…

Leave a Reply

Your email address will not be published. Required fields are marked *