应用程序崩溃是由于使用ANativeWindow的API [英] Application crashing due to use of ANativeWindow API

查看:790
本文介绍了应用程序崩溃是由于使用ANativeWindow的API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话,使用我的本地函数 ANativeWindow API 从一个java的code UI 下面跟帖如下所示:

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");
                    }
                });

如果是天然$ C $℃,同时从RGBA缓冲器的位复制的数据时,应用程序被carshing。本地code片段如下所示:

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);
}

在运行应用程序时,注释后,应用程序崩溃:创建的窗口......。随着在logcat的以下信息:

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'

在上面的code我的目的是用来显示缓冲器1 的RGBA数据present到设备屏幕上的 ANativeWindow API

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

推荐答案

最后一个大量试错技术后,我能够找出崩溃。

Finally after a lots of trail and error technique i was able to figure out the crash.

究其原因是,在上述code面前:

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);
     }

我们需要添加:

ANativeWindow_setBuffersGeometry(window,w,h,WINDOW_FORMAT_RGBA_8888)

,其中,W->帧,H->框架的高度和RGBA格式的宽度

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

这篇关于应用程序崩溃是由于使用ANativeWindow的API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆