bitmap - OpenGL screenshot taking too much time on android?



I am using OpenGL render method to capture screenshot.but take it takes 7 to 8 seconds on NEXUS 7 to read pixel information. I am using this code to read pixel information and save it as Bitmap.

 public Bitmap grabPixels(GL10 mGL) {

        final int mWidth = mViewWidth;
        final int mHeight = mViewHeight;

        IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
        IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
        mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

        for (int i = 0; i < mHeight; i++) {
            for (int j = 0; j < mWidth; j++) {
                ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));

        Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight, Bitmap.Config.ARGB_8888);
        return mBitmap;


How can i increase speed of reading the pixel information and convert it into the Bitmap ?

1 Answer: 

I wrote another answer about saving GIFs on gamedev, which might be useful.

You can't call glReadPixels() on another thread. But it's most likely (as you said) the process of saving the image that takes the majority of time. As such the solution is to do the saving on a separate thread.

new Thread(new Runnable() {
    public void run() {
        ... save screenshot ...

Before anybody comes at me with pitchforks. Then yes you can call glReadPixels() on another thread. It however requires a shared context, which can be specified when calling eglCreateContext().

EGLContext shared = ...
EGLDisplay display = ...
EGLConfig eglConfig = ...

int[] attrib_list = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL10.EGL_NONE };
EGLContext context = egl.eglCreateContext(display, eglConfig, shared, attrib_list);

But before you start digging into shared contexts (which can be a pain in themselves) then make sure your bottleneck isn't the saving the image, which it most likely is.