[osflash] Q:Flash bitmap and double buffering

Guy Watson - FlashGuru LTD guy.watson at gmail.com
Wed Jul 11 21:51:04 EDT 2007


Double Buffering was necessary in Flash 8, but in Flash 9 with Actionscript
3 the speed difference between setting pixels on a bitmapData object that is
on the display list and settings pixels on a bitmapData object that isnt on
the display can be reduced quite significantly with the introduction of the
bitmapData.lock() and bitmapData.unlock() methods.

To decrease the difference in speed some more simply hide the Bitmap
container of the bitmapData object you are setting pixels on, until you have
finished, for example:

package
{
    import flash.display.Sprite
    import flash.display.Bitmap
    import flash.display.BitmapData
    import flash.utils.getTimer

    public class Test extends Sprite
    {
        public function Test()
        {
            var bmp1:BitmapData = new BitmapData(500,500,true,0)
            var bmp2:BitmapData = bmp1.clone()
            var bmp3:BitmapData = bmp2.clone()

            var holder1:Bitmap = new Bitmap(bmp1)
            addChild(holder1)

            var holder2:Bitmap = new Bitmap(bmp2)

            var start:int
            var i:uint

            start=getTimer()
            bmp1.lock()
            holder1.visible=false
            for(i=0;i<1000000;++i)
            {
                bmp1.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            holder1.visible=false
            bmp1.unlock()
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
                bmp2.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
                bmp3.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)
        }

    }
}

I get the following results in the output window:

1160
1366
1143

Which is a bit different from the results you get when you dont use the
lock() and unlock() methods, and dont hide the holder bitmap:

package
{
    import flash.display.Sprite
    import flash.display.Bitmap
    import flash.display.BitmapData
    import flash.utils.getTimer

    public class Test extends Sprite
    {
        public function Test()
        {
            var bmp1:BitmapData = new BitmapData(500,500,true,0)
            var bmp2:BitmapData = bmp1.clone()
            var bmp3:BitmapData = bmp2.clone()

            var holder1:Bitmap = new Bitmap(bmp1)
            addChild(holder1)

            var holder2:Bitmap = new Bitmap(bmp2)

            var start:int
            var i:uint

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
                bmp1.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
                bmp2.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)

            start=getTimer()
            for(i=0;i<1000000;++i)
            {
                bmp3.setPixel(Math.random()*500,Math.random()*500,
Math.random()*0xFFFFFFFF)
            }
            trace(getTimer()-start)
        }

    }
}

Which outputs:

1377
1356
1130

Kind Regards
Guy
-----------------------------
www.flashguru.co.uk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://osflash.org/pipermail/osflash_osflash.org/attachments/20070712/02232561/attachment.html 


More information about the osflash mailing list