Friday, 15 January 2021

Drawing to Surface from JNI while moving SurfaceView around

In my application I have a SurfaceView in my Activity layout so its surface is not affected by any Fragment lifecycles. I use the surface to draw to it from JNI using following code (some of the varialbes are global):

void onDraw() {
    int height=0, width=0;
    ANativeWindow_Buffer nativeBuffer;
    ARect redrawRect;

    if (nativeWindow != NULL) {
        height = ANativeWindow_getHeight(nativeWindow);
        width = ANativeWindow_getWidth(nativeWindow);

        redrawRect.left = 0;
        redrawRect.top = 0;
        redrawRect.right = width;
        redrawRect.bottom = height;
    
        if (ANativeWindow_lock(nativeWindow, &nativeBuffer, &redrawRect) == 0) {
            height = (redrawRect.bottom - redrawRect.top);
            width = (redrawRect.right - redrawRect.left);
    
            bufferSize= height * width * 4;
            memcpy(nativeBuffer.bits, pbuf, bufferSize);
            ANativeWindow_unlockAndPost(nativeWindow);
        }
    }
}

As long as I do not change the size and position of the SurfaceView this works great. However, when I start drawing, adjust the SurfaceView in size and position (content is adjusted fine), stop the drawing and then start it again I get the following error:

E/BufferQueue: [SurfaceView] connect: already connected (cur=2, req=2)

Do you have any ideas for this? Is that actually a problem with my drawing or maybe with the window of the surface? If I do not draw at all this error does not appear. Any help is highly appreciated.



from Drawing to Surface from JNI while moving SurfaceView around

No comments:

Post a Comment