ActionScript 3, AIR, Android, iOS, Mobile, Tutorials

Building a AIR Native Extension for iOS and Android

I just found few articles from Digital Primates on how to build and debug a ANE for iOS and Android I want to share.

Here are the links, they’re a very good reading:

Building a Native Extension part 1: the problem and the plan

Building a Native Extension part 2: the actionscript code

Building a Native Extension part 3: the iOS code

Building a Native Extension part 4: the Android code

ActionScript 3, AIR, Flash Player

Light on BitmapData memory allocation

Recently at the office we’ve been dealing with a strange BitmapData memory occupation behavior I want to share with you guys.

On this subject the AS3 reference reads “A BitmapData object contains an array of pixel data. This data can represent either a fully opaque bitmap or a transparent bitmap that contains alpha channel data. Either type of BitmapData object is stored as a buffer of 32-bit integers. Each 32-bit integer determines the properties of a single pixel in the bitmap.”

This should be meaning that a BitmapData object should be represented in memory as a sequence of 32 bits integers, one for each pixel in the bitmap, hence width*height*4 bytes which definitely makes sense.
However the actual behavior is significantly different and you can easily find out that the real BitmapData size in memory is not proportional to its area as it should be.
Let’s see an example (code shared at wonderfl)

BitmapData Memory Allocation – wonderfl build flash online

The example shows how different the size of a BitmapData is depending on its maximum dimension: the code instantiates 4096 images with an increasing area from 1 up to 4096 pixels, first with a horizontal extension (width is the increasing dimension, blue line in the chart) then with a vertical extension (height is the increasing dimension, red line in the chart).
You can notice that the behaviors of the lines in the chart are very different and the blue line’s Y is increasing only few times whilst the red one’s (vertical bitmap) is increasing much more frequently and is reaching much higher values.
So what’s the real memory representation of a BitmapData? For sure it isn’t a width*height*4 matrix as it should be and it is stricly related to the dimension the BitmapData is extended on.

We worked a bit on this thing at the office (thanx to Matteo Lanzi) and we found out that BitmapData object memory allocator has a strange behavior: it allocates N chunks of memory aligned to 256bytes (64 pixels) at time.
This would be a useful behavior if the BitmapData object would be resizable via actionscript, but it is not, or if the freed chunks were put in a memory pool and used by the next BitmapData requiring memory, but it doesn’t seem to do that ( the only case we noticed that the memory is reused is when a NxM BitmapData is created, deleted and recreated with the same N and M dimensions ).
If it was only this however it wouldn’t have been such a big problem to deal with, the real big issue is that a 256b chunk is representing a sequence of pixels with a defined horizontal orientation.
This hardcoded behavior leads to a very unpleasant side effect: a BitmapData instance with a high number of little rows (high height, little width,, hence a ‘vertical’ orientation) is occupying a HUGE AMOUNT OF MEMORY while it should be occupying the same memory of a bitmap with the same area rotated by 90°.
Let’s seesome more example just to clear out the problem:

Case 1:
– width 1px
– height 1px
– expected allocation: 4 bytes
– actual allocation: 256 bytes (minimum chunk)

a 1×1 px BitmapData allocates in the heap 1 chunk of 256 bytes while only 4 bytes would be needed!! 63x overhead!!!

Case 2:
– width 64px
– height 1px
– expected allocation: 256 bytes
– actual allocation: 256 bytes (fits exactly the minimum chunk)

the same 256 bytes are allocated by a 64×1 bitmap and the overhead disappears, what does happen with a 65×1 one?

Case 3:
– width 65px
– height 1px
– expected allocation: 260 bytes
– actual allocation: 512 bytes (2 chunks)

with a ‘max chunk size plus one’ size a 65 px wide image allocates 2 chunks, the overhead is set to approx 1.9x.

Case 4
– width 1px
– height 64px
– expected allocation: 256 bytes (same area of the 64×1 one…)
– actual allocation: 16 Kb!!!!!

this is the worst case, every in 1xN bitmaps have the maximum overhead possible: 63x!
So try to think about an image very narrow and high, let’s say a 1×4096 one: you would expect it to be 16kb in memory (4096*4 bytes) but it actually is 1 whole megabyte (4096*4*256!!)!!

So, the formula to calculate the ACTUAL memory allocated by a BitmapData expressed in bytes I came up with is:

Math.max( 1 , height*(width/64)>>0 )*256

You can see a more detailed and complete dataset in this google spreadsheet (feel free to spread the link) containing data for BitmapData memory occupation from 1×1 to 1×4096 and from 1×1 to 4096×1.
The test to reproduce the dataset is available on github at this url (feel free to share this link as well)

This behavior has been tested in AIR 3.1 for Mac OSX 10.6 (snow leopard), 10.7 (lion) (thanx to Marco Nava for testing on lion), Window 7, Android 4.0, iOS 5 (thanx to Shawn Blais for testing on mobile),
Flash Player 11,1 by yourself (if you ran the swf linked backward in the post and noticed the same behavior).

After figuring out the problem we opened a bug in adobe’s jira, if you have comments which can raise the attention of adobe on this stuff or add something to the discussion please feel free to comment at this url (you’re welcome to comment here too btw :D)

Few more points worth some attention:
I tried to measure the memory in 3 ways:
– the data got back from flash.sampler.getSize() was not accurate, most of the time it was totally wrong and not predictable (or perhaps I didn’t get how it works in relation with BitmapData objects)
– the delta calculated with a pre and post sampling of System.privateMemory is more accurate than getSize() and fits the expected results in AIR but it’s not as accurate as I expected: in the spreadsheet linked backwards you can see that the results have a coherent trend, but there’s a lot of garbage data and little bitmaps weren’t detected at all.
– the delta calculated witn a pre and post sampling of System.totalMemory is more accurate than System.privateMemory only in flash player.


That said,
I find this allocation behavior weird, and the worst thing is that it UNDOCUMENTED and the docs are saying a different thing. I see one VERY significant bad side effects in this way of allocating bitmaps memory that involve how BitmapData objects are stricly tied to the Flash Player’s and AIR runtime’s APIs:
flash.display.Loader generates BitmapData instances when it loads images from URL or streams, so if an application needs to load many bitmaps that don’t fit the memory alignment there will possibly be a HUGE memory waste.
A possible solution could be the creation of few additional functions such as

loadInto(urlrequest:URLRequest, destination:ByteArray):void



which both take a data source and a preallocated bytearray as input and put outputs to the preallocated bytearray called destination.


This is the first shot, I’d really like you guys to spread the word and help me to make adobe sensible on this topic as it’s really worth some effort to make flash and air runtimes even better than what they are now.

Thank you all in advance,



Air 2 Multitouch and YouTube subtitles failure

I just found a very exciting demo from Ryan Stewart on how to mash up Google Maps 3D APIs and Air 2 multitouch gestures.
You can find his experiment’s source code on his blog here with a great video demo.

By the way, I started looking at the video from YouTube and noticed a new awesome feature: audio transcription and audio translation! w00t!
I tried out that wonderful piece of technology. I am Italian, so I tried to translate it on the fly…the result was ASTONISHING: no word was translated correctly, no sentence had a real meaning, if you run a Math.random() on the italian dictionary and start making a speech you’re likely having something better.

I guessed the problem was with the translation, but I was wrong :
at 1’46” you can find out that “To change the rules the perspective of a man San Diego freemen will concede you and all of the police on the street is the ability to going to fly over the map, she’s the perspective”… wtf!?
at 2’25” is even better: “but combining the there to gestures and grownups three the FBI a critic of the Iraq war unique way to browse and that’s really true that Bosnia really customize how the contents of the views that no one of the three demoted from a player and disturbing experience”….

Cool! An Adobe evangelist talking about Iraq war, FBI and Bosnia while free men will concede to you in San Diego…
Please, don’t trust that feature.
#google #fail

ActionScript 3, AIR, Flash Player 10, News

Adobe Stratus on Adobe Labs

Did you remember “Pacifica” project? Now it’ [UPDATE: Adobe Stratus is a rendezvous service for RTMFP, a new protocol built in to Flash Player 10 and AIR 1.5. Neither RTMFP nor Stratus are related to the project codenamed Pacifica.] Something new is on adobe labs and its name is “Stratus“..WONDERFUL! 😀

Let’s explain what i’m talking about:

Stratus is “hosted rendezvous service that aids establishing communications between Flash Player endpoints”.
This technology enables clients’ flash player (10 +, or AIR 1.5) to connect directly each other to share runtime informations…actually PEER TO PEER!! (underline this: Stratus is a service by Adobe, not a technology to run on own servers).

Stratus does support only “end to end” p2p, multicast or swarming are not supported. This means we’re not enabled to create a new air-mule service over stratus, but we can build our p2p video chat, p2p real time games, etc…

Stratus introduces a new data transfer protocol: RTMFP, which uses UDP instead of clean RTMP which uses TCP. (note, RTMFP is not RTMP*, which is the encrypted protocol for FMS).

Stratus is now beta, and you can test a sample application hosted on the labs Stratus page

Stratus is going to be released next year (hopefully “early”) …it seems we’re going to have real time “anything” in few months 😀

This could be a new red pill for our webapps,
Stay tuned 😉

AIR, Anything else, Speaking

My 30onAir at 360 Flex Europe

I’ve been searching for a while this video of me that Lucinda and Andrea took at 360 flex in milan last month.

I had just finished my session +
that night i slept just few hours =

[flash w=425 h=373]

thanks to Andrea and Lucinda..I hope to meet you soon 🙂

[sat 10 2008 edit]...and thanx Sumi too :)[/edit]