If you’re familiar with BlackBerry UI programming, you may know that there are two ways to draw a Bitmap to the display. One is Graphics.drawBitmap(...), the other is Graphics.rop(...) using ROP_SRC_COPY, ROP_SRC_ALPHA, or ROP_SRC_ALPHA_GLOBALPHA.
Which one should you use? I decided to do some performance testing, using the following method (full source file available here):
Methodology
protected void paint(Graphics graphics) {
int imageWidth = bmpTest.getWidth();
int imageHeight = bmpTest.getHeight();
int width = getWidth();
int height = getHeight();
long startTime = System.currentTimeMillis();
for(int i = 0; i < 1000; i++) {
if (!drawImage) {
graphics.rop(Graphics.ROP_SRC_ALPHA_GLOBALALPHA, i%width, i%height, imageWidth, imageHeight, bmpTest, 0, 0);
}
else {
graphics.drawBitmap(i%width, i%height, imageWidth, imageHeight, bmpTest, 0, 0);
}
}
long endTime = System.currentTimeMillis();
elapsedTime = endTime - startTime;
graphics.setColor(Color.BLACK);
graphics.drawText("ELAPSED: " + elapsedTime, 0, 0);
System.out.println("ELAPSED: " + elapsedTime);
}
drawImage was set via an Alternate Entry Point to the same application, so I shut down and re-ran the app between each test, to minimize any potential interference with the results.
Results
With a 320 x 240 pixel bitmap, no transparency, on an 8800, I got the following in 3 runs of each:
| rop | drawBitmap |
|---|---|
| 5660 | 2624 |
| 5660 | 2728 |
| 5692 | 2656 |
Results were very similar on a Pearl, Curve, and various simulators.
So drawImage is faster. Well, let’s not jump to a conclusion just yet. I was using ROP_SRC_ALPHA_GLOBALALPHA. What if I use ROP_SRC_ALPHA?
| rop | drawBitmap |
|---|---|
| 2624 | 2624 |
| 2624 | 2728 |
| 2728 | 2656 |
Interesting! But drawBitmap does global alpha blending - how can it be faster?
Well, notice that I hadn’t set a global alpha value in my paint method - it’s at the default of 255, which is totally opaque. If I switch it to another smaller value (using setGlobalAlpha), the times for drawBitmap suddenly increase, to where they’re basically identical to the times for ROP_SRC_ALPHA_GLOBALALPHA!
So it looks like what’s happening is that drawBitmap has some intelligence that if there’s a non-opaque global alpha value set, it uses the same algorithm as ROP_SRC_ALPHA_GLOBALALPHA, but otherwise uses the same algorithm as ROP_SRC_ALPHA.
This leads to the question: What about ROP_SRC_COPY? Can we do better drawBitmap if we know that our Bitmap has no transparency? Unfortunately no, performance for ROP_SRC_COPY is the same as ROP_SRC_ALPHA.
The (Obvious) Conclusion
Well, since there’s no performance with rop over drawBitmap, and with rop you need to remember to use ROP_SRC_ALPHA or ROP_SRC_ALPHA_GLOBALALPHA as needed, but drawBitmap automatically takes care of it, so - you should use drawBitmap for all your Bitmap drawing.






3 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
cool thanks!
Perfect. Thanks.
What’s wrong with code!!!
[Sorry, we're not sure what you're looking for here.]
:OOOOOO