Skip to content

Commit

Permalink
fix(android): Canvas was getting blank because of unexpected flush ca…
Browse files Browse the repository at this point in the history
…lls (#85)
  • Loading branch information
CatchABus authored Aug 8, 2022
1 parent a541f93 commit cee733e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ class CPUView @JvmOverloads constructor(
canvasView?.get()?.let { canvas ->
if (canvas.nativeContext != 0L) {
canvas.queueEvent {
// We don't want pending flags that were set up to this point
canvas.invalidateState = canvas.invalidateState and TNSCanvas.INVALIDATE_STATE_PENDING.inv()
TNSCanvas.nativeCustomWithBitmapFlush(canvas.nativeContext, it)
handler!!.post {
canvas.invalidateState = TNSCanvas.InvalidateState.NONE
canvas.invalidateState = canvas.invalidateState and TNSCanvas.INVALIDATE_STATE_INVALIDATING.inv()
invalidate()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,23 @@ internal class GLContext {
queueEvent {
if (reference != null) {
val canvasView = reference!!.get()
if (canvasView != null && canvasView.nativeContext != 0L && canvasView.invalidateState == TNSCanvas.InvalidateState.INVALIDATING) {
if (canvasView != null && canvasView.nativeContext != 0L && (canvasView.invalidateState and TNSCanvas.INVALIDATE_STATE_INVALIDATING) == TNSCanvas.INVALIDATE_STATE_INVALIDATING) {
// We don't want pending flags that were set up to this point
canvasView.invalidateState = canvasView.invalidateState and TNSCanvas.INVALIDATE_STATE_PENDING.inv()

TNSCanvas.nativeFlush(canvasView.nativeContext)
if (!mGLThread!!.getPaused() && !swapBuffers(mEGLSurface)) {
Log.e("JS", "GLContext: Cannot swap buffers!")
}
canvasView.invalidateState = TNSCanvas.InvalidateState.NONE
canvasView.invalidateState = canvasView.invalidateState and TNSCanvas.INVALIDATE_STATE_INVALIDATING.inv()
} else {
// WebGL
if (!mGLThread!!.getPaused() && !swapBuffers(mEGLSurface)) {
Log.e("JS", "GLContext: Cannot swap buffers!")
}
if (canvasView != null) {
canvasView.invalidateState = TNSCanvas.InvalidateState.NONE
// If this point is reached, it means something went wrong so set flag to none
canvasView.invalidateState = TNSCanvas.INVALIDATE_STATE_NONE
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class TNSCanvas : FrameLayout, FrameCallback, ActivityLifecycleCallbacks {
}

@JvmField
internal var invalidateState = InvalidateState.NONE
internal var invalidateState = TNSCanvas.INVALIDATE_STATE_NONE // bitwise flag
internal var contextType = ContextType.NONE
internal var actualContextType = ""
internal var useCpu = false
Expand Down Expand Up @@ -122,14 +122,11 @@ class TNSCanvas : FrameLayout, FrameCallback, ActivityLifecycleCallbacks {

private val mainHandler = Handler(Looper.getMainLooper())

enum class InvalidateState {
NONE, PENDING, INVALIDATING
}

override fun doFrame(frameTimeNanos: Long) {
if (!isHandleInvalidationManually) {
if (invalidateState == InvalidateState.PENDING) {
invalidateState = InvalidateState.INVALIDATING
// Only pending state flag is accepted
if (invalidateState == INVALIDATE_STATE_PENDING) {
invalidateState = INVALIDATE_STATE_INVALIDATING
flush()
}
}
Expand Down Expand Up @@ -600,6 +597,11 @@ class TNSCanvas : FrameLayout, FrameCallback, ActivityLifecycleCallbacks {
companion object {
var views: ConcurrentHashMap<*, *> = ConcurrentHashMap<Any?, Any?>()

// Invalidate state bitwise flags
internal const val INVALIDATE_STATE_NONE = 0
internal const val INVALIDATE_STATE_PENDING = 1
internal const val INVALIDATE_STATE_INVALIDATING = 2

@JvmStatic
fun layoutView(width: Int, height: Int, tnsCanvas: TNSCanvas) {
var rootParams = tnsCanvas.layoutParams
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class TNSCanvasRenderingContext2D internal constructor(val canvas: TNSCanvas) :

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ open class TNSWebGLRenderingContext : TNSCanvasRenderingContext {

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

Expand Down

0 comments on commit cee733e

Please sign in to comment.