Skip to content

Commit

Permalink
Re-init surface on resize, add test
Browse files Browse the repository at this point in the history
[As reported by the Duos on our Slack](https://2dimensions.slack.com/archives/C029X99PETE/p1697729811100089), it looks like we weren't re-initializing the surface/EGLSurface when the size changed.
I tried adding a test for this, but I'm not quite sure how flaky that's going to be. If our bots freak out we can think of something else.

Diffs=
5347f24f2 Re-init surface on resize (#6127)

Co-authored-by: Umberto Sonnino <[email protected]>
  • Loading branch information
umberto-sonnino and umberto-sonnino committed Oct 23, 2023
1 parent f169a91 commit ee3933a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
d65b239c5c6ce44c3b58cd71ff17721d874a4ab1
5347f24f289451c5861943bf3a8e0d8468d189ba
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package app.rive.runtime.example

import TestUtils.Companion.waitUntil
import android.view.ViewGroup
import android.widget.Button
import androidx.core.view.updateLayoutParams
import androidx.test.core.app.ActivityScenario
import androidx.test.ext.junit.runners.AndroidJUnit4
import app.rive.runtime.kotlin.RiveAnimationView
Expand Down Expand Up @@ -84,4 +87,50 @@ class RiveActivityLifecycleTest {
assertNull(controller.file)
assertNull(controller.activeArtboard)
}

@Test
fun resizeRiveView() {
val activityScenario = ActivityScenario.launch(SingleActivity::class.java);
lateinit var riveView: RiveAnimationView
lateinit var controller: RiveFileController
var ogWidth = 0f
// Start the Activity.
activityScenario.onActivity {
riveView = it.findViewById(R.id.rive_single)
controller = riveView.controller

assertEquals(2, controller.refCount)
assertTrue(controller.isActive)
assertNotNull(controller.file)
assertNotNull(controller.activeArtboard)

ogWidth = riveView.artboardRenderer!!.width

var isResized = false

Button(it).apply {
text = "RESIZE"
setOnClickListener {
riveView.updateLayoutParams {
width = (ogWidth - 1).toInt()
}
isResized = true
}
(riveView.parent as ViewGroup).addView(this)
performClick()
}
assert(isResized)
}

// Close it down.
activityScenario.close()
// This assert is not very robust...
assertEquals(ogWidth - 1f, riveView.artboardRenderer?.width)
// Background thread deallocates asynchronously.
waitUntil(1500.milliseconds) { controller.refCount == 0 }
assertFalse(controller.isActive)
assertNull(controller.file)
assertNull(controller.activeArtboard)
}

}
10 changes: 9 additions & 1 deletion app/src/main/java/app/rive/runtime/example/SimpleActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package app.rive.runtime.example

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.updateLayoutParams
import app.rive.runtime.example.databinding.SimpleBinding

class SimpleActivity : AppCompatActivity() {

private lateinit var binding : SimpleBinding

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.simple)
binding = SimpleBinding.inflate(layoutInflater)
setContentView(binding.root)
}
}
12 changes: 10 additions & 2 deletions kotlin/src/main/java/app/rive/runtime/kotlin/RiveTextureView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ abstract class RiveTextureView(context: Context, attrs: AttributeSet? = null) :
}

override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {} // called every time when swapBuffers is called
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
onSurfaceTextureAvailable(surface, width, height)
}

@CallSuper
override fun onAttachedToWindow() {
Expand All @@ -75,8 +77,14 @@ abstract class RiveTextureView(context: Context, attrs: AttributeSet? = null) :
width: Int,
height: Int
) {
if (this::viewSurface.isInitialized) {
viewSurface.release()
}
viewSurface = Surface(surfaceTexture)
renderer?.setSurface(viewSurface)
renderer?.apply {
stop()
setSurface(viewSurface)
}
}

@CallSuper
Expand Down

0 comments on commit ee3933a

Please sign in to comment.