chrisbatt / AndroidFastImageProcessing

A framework for speeding up image processing on android devices by taking advantage of shaders on the GPU.
MIT License
285 stars 118 forks source link

Proper way to change matrix from TransformFilter #28

Open puelocesar opened 9 years ago

puelocesar commented 9 years ago

So, I'm testing a very basic setup:

             Image1 \
                     \
                       - >  blend -> view
                     /
Image2 -> transform /

Problem is, I want to modify the transform matrix based on user input. As I couldn't find any way to modify TransformFilter matrix after it's created, I'm destroying it and recreating it with the new matrix.

It's working fine on emulator, but when running on device, it always crash after a few edits. Sometimes it crashes due OutOfMemory error, and sometimes it crashes with:

08-26 15:56:13.173    4312-4327/? E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 270
    java.lang.ArrayIndexOutOfBoundsException: length=1; index=-2
            at project.android.imageprocessing.filter.MultiInputFilter.newTextureReady(MultiInputFilter.java:87)
            at project.android.imageprocessing.input.GLTextureOutputRenderer.drawFrame(GLTextureOutputRenderer.java:88)
            at project.android.imageprocessing.input.ImageResourceInput.drawFrame(ImageResourceInput.java:67)
            at project.android.imageprocessing.GLRenderer.onDrawFrame(GLRenderer.java:232)
            at project.android.imageprocessing.FastImageProcessingPipeline.onDrawFrame(FastImageProcessingPipeline.java:91)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1463)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1217)

And here are the methods I'm calling to remove the transform, and then reinstall it:

private void clean() {
        pipeline.pauseRendering();

        input2.removeTarget(transform);
        blend.clearRegisteredFilterLocations();

        pipeline.addFilterToDestroy(transform);

        transform.removeTarget(blend);
        transform.destroy();
    }

    private void reinstallFilters() {

        Matrix.translateM(matrix, 0, matrix, 0, translatex, translatey, 0);

        transform = new TransformFilter(matrix, false, false);

//        debugMatrix();

        input2.addTarget(transform);
        transform.addTarget(blend);

        blend.registerFilterLocation(input);
        blend.registerFilterLocation(transform);

        pipeline.startRendering();
        preview.requestRender();
    }