glreadpixels使用pbo cant work

时间:2016-12-01 03:04:00

标签: android opengl-es fbo opengl-es-3.0 pbo

我想使用pbo来保存图像,而不是使用cpu。 所以我没有在glreadpixels中使用bytebuff,并使用 GLES30.glReadPixels(0,0,getWidth(),getHeight(),GLES30.GL_RGBA,GLES30.GL_UNSIGNED_BYTE,null);

但它无法工作......

这是我设置的fbo和pbo代码:

        GLES30.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        GLES30.glEnable(GLES30.GL_TEXTURE_2D);
        GLES30.glEnable(GLES30.GL_LEQUAL);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        GLES30.glGenFramebuffers(2,fbo,0);
        GLES30.glGenRenderbuffers(2,fboDepth,0);
        GLES30.glGenTextures(2, TextureString, 0);
        GLES30.glGenBuffers(2,pbo,0);

        if (half1 != null ) {
            /////////////one
            textureId = TextureString[0];
            Log.e("textureId", String.valueOf(textureId));

            GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId);
            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, half1, 0);
            initTexture();

            GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER,fboDepth[0]);
            GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER,GLES30.GL_DEPTH_COMPONENT16,2001,1001);

            GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER,fbo[0]);
            GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER,GLES30.GL_DEPTH_ATTACHMENT,GLES30.GL_RENDERBUFFER,fboDepth[0]);

            GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER,GLES30.GL_COLOR_ATTACHMENT0,GLES30.GL_TEXTURE_2D,textureId,0);

            int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
            if (status == GLES30.GL_FRAMEBUFFER_COMPLETE) {
                Log.d("MainActivity", "success");
            } else {
                Log.d("MainActivity", "error");
            }

            GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,pbo[0]);
            GLES30.glBufferData(GLES30.GL_PIXEL_UNPACK_BUFFER,getWidth()*getHeight()*4,null,GLES30.GL_DYNAMIC_READ);

            GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,0);

            GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
            GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, 0);
            half1.recycle();
        }

        if (half2 != null )
        {
            //////////////////////three
            textureId1 = TextureString[1];
            Log.e("textureId1", String.valueOf(textureId1));

            GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
            GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId1);
            GLUtils.texImage2D(GLES30.GL_TEXTURE_2D, 0, half2, 0);
            initTexture();

            GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER,fboDepth[1]);
            GLES30.glRenderbufferStorage(GLES30.GL_RENDERBUFFER,GLES30.GL_DEPTH_COMPONENT16,2001,1001);
            GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER,fbo[1]);GLES30.glFramebufferRenderbuffer(GLES30.GL_FRAMEBUFFER,GLES30.GL_DEPTH_ATTACHMENT,GLES30.GL_RENDERBUFFER,fboDepth[1]);
            GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER,GLES30.GL_COLOR_ATTACHMENT0,GLES30.GL_TEXTURE_2D,textureId1,0);

            int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
            if (status == GLES30.GL_FRAMEBUFFER_COMPLETE) {
                Log.d("MainActivity", "success");
            } else {
                Log.d("MainActivity", "error");
            }

            GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,pbo[1]);
            GLES30.glBufferData(GLES30.GL_PIXEL_UNPACK_BUFFER,getWidth()*getHeight()*4,null,GLES30.GL_DYNAMIC_READ);


            GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,0);

            GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
            GLES30.glBindRenderbuffer(GLES30.GL_RENDERBUFFER, 0);
            half2.recycle();
        }

绘制图片后,我的pbo readpixels无法正常工作,并且fbo读取错误,它会获得原始fbo [0]或fbo [1],而不是最终图像。

            Buffer rgbaBuf = null;
            GLES30.glBlitFramebuffer(0,0,getWidth(),getHeight(),0,0,getWidth(),getHeight(),GLES30.GL_COLOR_BUFFER_BIT,GLES30.GL_NEAREST);
            GLES30.glBindFramebuffer(GLES30.GL_READ_FRAMEBUFFER,fbo[0]);//TODO NOT SAVE last image
            GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT,1);

            GLES30.glBufferData(GLES30.GL_PIXEL_UNPACK_BUFFER,getWidth()*getHeight()*4,null,GLES30.GL_DYNAMIC_READ);
            GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,pbo[0]);
            GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0);
            GLES30.glReadPixels(0, 0, getWidth(), getHeight(), GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);//TODO ERROR
            rgbaBuf = ((ByteBuffer) GLES30.glMapBufferRange(GLES30.GL_PIXEL_UNPACK_BUFFER,0,1001 *2001*4,GLES30.GL_MAP_WRITE_BIT)).order(ByteOrder.nativeOrder());//
            Util save = new Util();

            save.saveRgb2Bitmap(rgbaBuf, Environment.getExternalStorageDirectory().getAbsolutePath()
                    + "/gl" + getWidth() + "_" + getHeight() + ".png", getWidth(),getHeight());
                GLES30.glUnmapBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER);
                GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER, 0);

                GLES30.glDeleteFramebuffers(2,fbo,0);
                GLES30.glDeleteRenderbuffers(2,fboDepth,0);
                }

关于glreadpixels的错误:

12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art:        art/runtime/java_vm_ext.cc:410] JNI DETECTED ERROR IN APPLICATION: field   operation on NULL object: 0x0
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]     in call to GetIntField
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]     from void android.opengl.GLES20.glReadPixels(int, int, int, int, int, int, java.nio.Buffer)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410] "GLThread 1121" prio=5 tid=11 Runnable
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   | group="main" sCount=0 dsCount=0 obj=0x12ecee00 self=0xacbdb300
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   | sysTid=10494 nice=0 cgrp=default sched=0/0 handle=0xaf05f930
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   | state=R schedstat=( 381520053 43641615 347 ) utm=21 stm=17 core=1 HZ=100
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   | stack=0xaef5d000-0xaef5f000 stackSize=1038KB
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   | held mutexes= "mutator lock"(shared held)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #00 pc 00370e01  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiPKcPNS_9ArtMethodEPv+160)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #01 pc 0035046f  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+150)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #02 pc 0025a725  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+740)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #03 pc 0025adfd  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+64)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #04 pc 000fd1d1  /system/lib/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+32)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #05 pc 0011517d  /system/lib/libart.so (_ZN3art11ScopedCheck16CheckFieldAccessERNS_18ScopedObjectAccessEP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+2192)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #06 pc 001164d5  /system/lib/libart.so (_ZN3art8CheckJNI8GetFieldEPKcP7_JNIEnvP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+524)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #07 pc 00116b47  /system/lib/libart.so (_ZN3art8CheckJNI11GetIntFieldEP7_JNIEnvP8_jobjectP9_jfieldID+30)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #08 pc 0006a1e3  /system/lib/libandroid_runtime.so (???)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #09 pc 0006a673  /system/lib/libandroid_runtime.so (???)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   native: #10 pc 00d48135  /data/dalvik-cache/arm/system@framework@boot.oat (Java_android_opengl_GLES20_glReadPixels__IIIIIILjava_nio_Buffer_2+152)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   at android.opengl.GLES20.glReadPixels(Native method)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   at com.android.gles3jni.GLES3JNIView$Renderer.onDrawFrame(GLES3JNIView.java:133)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1535)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410]   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
12-01 10:56:47.055 10342-10494/com.android.gles3jni A/art: art/runtime/java_vm_ext.cc:410] 
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366] Runtime aborting...
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366] Aborting thread:
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366] "GLThread 1121" prio=5 tid=11 Native
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   | group="" sCount=0 dsCount=0 obj=0x12ecee00 self=0xacbdb300
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   | sysTid=10494 nice=0 cgrp=default sched=0/0 handle=0xaf05f930
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   | state=R schedstat=( 403422291 43879741 354 ) utm=23 stm=17 core=1 HZ=100
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   | stack=0xaef5d000-0xaef5f000 stackSize=1038KB
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   | held mutexes= "abort lock"
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #00 pc 00370e01  /system/lib/libart.so (_ZN3art15DumpNativeStackERNSt3__113basic_ostreamIcNS0_11char_traitsIcEEEEiPKcPNS_9ArtMethodEPv+160)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #01 pc 0035046f  /system/lib/libart.so (_ZNK3art6Thread4DumpERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEE+150)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #02 pc 00333827  /system/lib/libart.so (_ZNK3art10AbortState10DumpThreadERNSt3__113basic_ostreamIcNS1_11char_traitsIcEEEEPNS_6ThreadE+26)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #03 pc 00333abf  /system/lib/libart.so (_ZN3art7Runtime5AbortEv+562)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #04 pc 000f45fb  /system/lib/libart.so (_ZN3art10LogMessageD2Ev+2226)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #05 pc 0025aa4f  /system/lib/libart.so (_ZN3art9JavaVMExt8JniAbortEPKcS2_+1550)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #06 pc 0025adfd  /system/lib/libart.so (_ZN3art9JavaVMExt9JniAbortVEPKcS2_St9__va_list+64)
12-01 10:56:47.243 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #07 pc 000fd1d1  /system/lib/libart.so (_ZN3art11ScopedCheck6AbortFEPKcz+32)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #08 pc 0011517d  /system/lib/libart.so (_ZN3art11ScopedCheck16CheckFieldAccessERNS_18ScopedObjectAccessEP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+2192)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #09 pc 001164d5  /system/lib/libart.so (_ZN3art8CheckJNI8GetFieldEPKcP7_JNIEnvP8_jobjectP9_jfieldIDbNS_9Primitive4TypeE+524)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #10 pc 00116b47  /system/lib/libart.so (_ZN3art8CheckJNI11GetIntFieldEP7_JNIEnvP8_jobjectP9_jfieldID+30)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #11 pc 0006a1e3  /system/lib/libandroid_runtime.so (???)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #12 pc 0006a673  /system/lib/libandroid_runtime.so (???)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   native: #13 pc 00d48135  /data/dalvik-cache/arm/system@framework@boot.oat (???)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   at android.opengl.GLES20.glReadPixels(Native method)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   at com.android.gles3jni.GLES3JNIView$Renderer.onDrawFrame(GLES3JNIView.java:133)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1535)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366]   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
12-01 10:56:47.244 10342-10494/com.android.gles3jni A/art: art/runtime/runtime.cc:366] Dumping all threads without appropriate locks held: thread list lock mutator lock

谢谢!

1 个答案:

答案 0 :(得分:0)

一些想法:

GLES30.glBufferData(GLES30.GL_PIXEL_UNPACK_BUFFER,getWidth()*getHeight()*4,null,GLES30.GL_DYNAMIC_READ);
GLES30.glBindBuffer(GLES30.GL_PIXEL_UNPACK_BUFFER,pbo[0]);

如果您尝试初始化pbo [0],则上述2行应该是值得尊敬的。可能是缓冲区从未初始化并导致空指针错误。