NativeScript / canvas

Apache License 2.0
88 stars 18 forks source link

Android canvas becomes blank after few updates #84

Closed CatchABus closed 2 years ago

CatchABus commented 2 years ago

It seems that android canvas becomes completely blank after multiple updates and it's random. May occur after 2, 5 or 20 drawings or during next drawing to canvas.

triniwiz commented 2 years ago

Can you edit the Canvas source in node_module to this._canvas = new org.nativescript.canvas.TNSCanvas(activity, true); and lmk if that changes anything for u

CatchABus commented 2 years ago

Can you edit the Canvas source in node_module to this._canvas = new org.nativescript.canvas.TNSCanvas(activity, true); and lmk if that changes anything for u

Yes, CPU mode seems to be stable.

CatchABus commented 2 years ago

I wonder if it's related to when pending invalidate flag is set. This enum flag was added to solve a similar problem in the past. Maybe it's not set early enough? For example, plugin attempts to make native call first, then update canvas state to pending. Sample:

        private fun updateCanvas() {
        // synchronized (canvasView.lock) {
        canvas.invalidateState = TNSCanvas.InvalidateState.PENDING
        //}
    }

    fun clearRect(x: Float, y: Float, width: Float, height: Float) {
        printLog("clearRect")
        canvas.queueEvent(Runnable {
            nativeClearRect(canvas.nativeContext, x, y, width, height)
            updateCanvas()
        })
    }

    fun fillRect(x: Float, y: Float, width: Float, height: Float) {
        printLog("fillRect")
        canvas.queueEvent(Runnable {
            nativeFillRect(canvas.nativeContext, x, y, width, height)
            updateCanvas()
        })
    }

    fun strokeRect(x: Float, y: Float, width: Float, height: Float) {
        printLog("strokeRect")
        canvas.queueEvent(Runnable {
            nativeStrokeRect(canvas.nativeContext, x, y, width, height)
            updateCanvas()
        })
    }