andreasgal/j2me.js — pull request #395 by mykmelez (2014-10-03 09:27:48+00:00)
Title: override CGraphicsUtil.draw9pcsBackground with native impl

This is basically a straight port of *CGraphicsUtil.draw9pcsBackground* to JavaScript (with a bit of refactoring of common code with *Graphics.render*).

Before this change, a test that exercises that method chews up time in *Graphics.drawImage* and *Graphics.render*:

> 9169ms 141018 javax/microedition/lcdui/Graphics.render.(Ljavax/microedition/lcdui/Image;III)Z
> 6241ms 102 com/sun/midp/chameleon/CGraphicsUtil.draw9pcsBackground.(Ljavax/microedition/lcdui/Graphics;IIII[Ljavax/microedition/lcdui/Image;)V
> 3361ms 141018 javax/microedition/lcdui/Graphics.drawImage.(Ljavax/microedition/lcdui/Image;III)V

After the change, *CGraphicsUtil.draw9pcsBackground* is still expensive (a bit more so, in fact), but *Graphics.drawImage* and *Graphics.render* are both so cheap, because *CGraphicsUtil.draw9pcsBackground* now bypasses them, that the overall cost is less than half:

> 6843ms 102 com/sun/midp/chameleon/CGraphicsUtil.draw9pcsBackground.(Ljavax/microedition/lcdui/Graphics;IIII[Ljavax/microedition/lcdui/Image;)V
> 88ms 1380 javax/microedition/lcdui/Graphics.render.(Ljavax/microedition/lcdui/Image;III)Z
> 32ms 1380 javax/microedition/lcdui/Graphics.drawImage.(Ljavax/microedition/lcdui/Image;III)V

And I've confirmed that perf is better enough on my Flame to fix #317.

I also added a gfx test of Alert, which uses the method to draw its background and border. The test output looks like this:

![screen shot 2014-10-02 at 15 55 11](https://cloud.githubusercontent.com/assets/305455/4503940/05c2af64-4adf-11e4-9269-0c805bf795e8.png)

We could squeeze out the rest of the expense for Alert layers (which appears to be the type of layer causing a problem in that issue) by commenting out these lines in AlertResources.java:

> AlertSkin.IMAGE_BG = SkinLoader.getCompositeImage(
> SkinPropertiesIDs.ALERT_IMAGE_BG, 9);

Then alerts would have a simple colored background:

![screen shot 2014-10-02 at 16 07 02](https://cloud.githubusercontent.com/assets/305455/4503942/15b8b4f4-4adf-11e4-8be4-fe5c7e9a5e91.png)

But this perf improvement would be helpful in any case for other classes that use the method. And perhaps what we really need to do next is something more radical: implementing the Alert layer (and other layers in the MIDP UI library) in HTML.