Search code examples
androidimageandroid-ndkjava-native-interface

Application crashing due to use of ANativeWindow API


I'm calling my native function that uses ANativeWindow API from the java code from an UI thread as shown below:

MainActivity.this.runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // your stuff to update the UI
                        mainFunction(3,inputFile,outputFile,surface);
                        Log.d(TAG, "Thread executed successfully");
                    }
                });

Then in the native code while copying the data from the rgba buffer to the bits, the application is carshing. Native code snippet is shown below:

void yuv2rgba(UINT8* y,UINT8* u,UINT8* v,int w, int h,FILE *fp_Out_File,jobject surface)
{
     int mem1=w*h,i=0,j=0,k=0;
__android_log_print(ANDROID_LOG_DEBUG,"MYDECODER","yuv2rgba is entered.....",NULL);

    JNIEnv *env;
    ANativeWindow* window;
    ANativeWindow_Buffer buffer;
//buffer1: holds the rgba data
    UINT8 *buffer1=(UINT8*)malloc(sizeof(UINT8)*(4*mem1));
    UINT8 *r=(UINT8*)malloc(sizeof(UINT8)*mem1);
    UINT8 *g=(UINT8*)malloc(sizeof(UINT8)*mem1);
    UINT8 *b=(UINT8*)malloc(sizeof(UINT8)*mem1);
    int y1,u1,v1,rcomp,gcomp,bcomp;

    int getEnvStat = (*g_vm)->GetEnv(g_vm,(void **)&env, JNI_VERSION_1_6);
    if (getEnvStat == JNI_EDETACHED) {
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: not attached.....",NULL);
    }
    else if (getEnvStat == JNI_OK) {

        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","getEnvStat == JNI_OK.....",NULL);
    }
    else if (getEnvStat == JNI_EVERSION) {

        __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: version not supported.....",NULL);
    }

    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","computing rgb.....",NULL);

    for(i=0;i<mem1;++i)
     {
        y1=y[i]-16;
        u1=u[i]-128;
        v1=v[i]-128;

        rcomp=((298 * y1) +( 409 * v1))>>8;
        r[i]=(UINT8)((rcomp<0)?0:((rcomp > 255)?255:rcomp));

        gcomp=((298 * y1) - (100 *u1) - (208 * v1))>>8;
        g[i]=(UINT8)((gcomp<0)?0:((gcomp > 255)?255:gcomp));
        bcomp=((298 * y1) + (516 * u1))>>8;
        b[i]=(UINT8)((bcomp<0)?0:((bcomp > 255)?255:bcomp));
    }
    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","assigning rgba to buffer....",NULL);
    // Storing the RGB value into a buffer
    for(i=0,j=0;(i<(mem1*4))&&(j<mem1);i=i+4,++j)
       {
        buffer1[i]=r[j];
        buffer1[i+1]=g[j];
        buffer1[i+2]=b[j];
        buffer1[i+3]=0xff;
       }
    free(r);
    free(g);
    free(b);
    //fwrite to an output file
    //fwrite(buffer1,sizeof(UINT8),w*h*3,fp_Out_File);


    __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","before creating a window.....",NULL);

     window = ANativeWindow_fromSurface(env, surface);

     __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","window created.....",NULL);

     if (ANativeWindow_lock(window, &buffer, NULL) == 0) {

       memcpy(buffer.bits, buffer1,  sizeof(UINT8)*(4*mem1));

       __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","memcpy successfull.....",NULL);

       ANativeWindow_unlockAndPost(window);

       __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","unlock and post  successful..." ,NULL);
     }
     ANativeWindow_release(window);
     __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","release successful.....",NULL);

    free(buffer1);
}

When the application is run, the application crashes after the comment: "window created....". along with the below message in the logcat:

02-18 14:46:55.344: I/dalvikvm(2080): threadid=4: reacting to signal 3
02-18 14:46:55.348: I/dalvikvm(2080): Wrote stack traces to '/data/anr/traces.txt'

In the above code my aim is to display the rgba data present in buffer1 on to the device screen using the ANativeWindow API


Solution

  • Finally - after a lots of trial and error - I was able to figure out the crash.

    The reason is in the above code before:

    if (ANativeWindow_lock(window, &buffer, NULL) == 0)
    {
      memcpy(buffer.bits, buffer1,  sizeof(UINT8)*(4*mem1));
    
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","memcpy successfull.....",NULL);
    
      ANativeWindow_unlockAndPost(window);
    
      __android_log_print(ANDROID_LOG_DEBUG,"MYAPP","unlock and post  successful..." ,NULL);
    }
    

    We need to add :

    ANativeWindow_setBuffersGeometry(window,w,h,WINDOW_FORMAT_RGBA_8888)
    

    where, w ->width of the frame, h -> height of the frame, and the RGBA format.