android fast pixel access and manipulation

I'm trying to port an emulator that i have written in java to android. Things have been going nicely, I was able to port most of my codes with minor changes however due to how emulation works, I need to render image at pixel level.

As for desktop java I use

int[] pixelsA = ((DataBufferInt) src.getRaster().getDataBuffer()).getData(); 

which allow me to get the reference to the pixel buffer and update it on the fly(minimize object creations)

Currently this is what my emulator for android does for every frame

@Override
public void onDraw(Canvas canvas)
{
    buffer = Bitmap.createBitmap(pixelsA, 256, 192, Bitmap.Config.RGB_565);
    canvas.drawBitmap(buffer, 0, 0, null);

}   

pixelsA is an array int[], pixelsA contains all the colour informations, so every frame it will have to create a bitmap object by doing

buffer = Bitmap.createBitmap(pixelsA, 256, 192, Bitmap.Config.RGB_565);

which I believe is quite expensive and slow.

Is there any way to draw pixels efficiently with canvas?

Answers


One quite low-level method, but working fine for me (with native code):

Create Bitmap object, as big as your visible screen. Also create a View object and implement onDraw method.

Then in native code you'd load libjnigraphics.so native library, lookup functions AndroidBitmap_lockPixels and AndroidBitmap_unlockPixels. These functions are defined in Android source in bitmap.h.

Then you'd call lock/unlock on a bitmap, receiving address to raw pixels. You must interpret RGB format of pixels accordingly to what it really is (16-bit 565 or 32-bit 8888).

After changing content of the bitmap, you want to present this on screen. Call View.invalidate() on your View. In its onDraw, blit your bitmap into given Canvas.

This method is very low level and dependent on actual implementation of Android, however it's very fast, you may get 60fps no problem.

bitmap.h is part of Android NDK since platform version 8, so this IS official way to do this from Android 2.2.


You can use the drawBitmap method that avoids creating a Bitmap each time, or even as a last resort, draw the pixels one by one with drawPoint.


Don't recreate the bitmap every single time. Try something like this:

Bitmap buffer = null;
@Override
public void onDraw(Canvas canvas)
{
    if(buffer == null) buffer = Bitmap.createBitmap(256, 192, Bitmap.Config.RGB_565);
    buffer.copyPixelsFromBuffer(pixelsA);
    canvas.drawBitmap(buffer, 0, 0, null);
}

EDIT: as pointed out, you need to update the pixel buffer. And the bitmap must be mutable for that to happen.


if pixelsA is already an array of pixels (which is what I would infer from your statement about containing colors) then you can just render them directly without converting with:

canvas.drawBitmap(pixelsA, 0, 256, 0, 0, 256, 192, false, null);

Need Your Help

Parsing code files faster

c++ string parsing c++11

I wrote a fairly complex parser for a stack-based language which loads a file into memory and then proceeds by comparing tokens to see if it is recognized as operand or instruction.