-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "stress test" android activity that draws lots of Martys
This also required adding a "transform" method to the low level rendering API. Diffs= 74e53aa12 Add a "stress test" android activity that draws lots of Martys (#5398) Co-authored-by: Chris Dalton <[email protected]>
- Loading branch information
1 parent
0eddc41
commit 4c2ab0c
Showing
11 changed files
with
238 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
6e07d855c7adaba20b0fd985987f2ec32a5d2dab | ||
74e53aa129f5a03160ed395b23e5074d592fc307 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
app/src/main/java/app/rive/runtime/example/StressTestActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package app.rive.runtime.example | ||
|
||
import android.content.Context | ||
import android.graphics.RectF | ||
import android.os.Bundle | ||
import android.view.ViewGroup | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
import app.rive.runtime.kotlin.RiveTextureView | ||
import app.rive.runtime.kotlin.core.* | ||
import app.rive.runtime.kotlin.renderers.Renderer | ||
import java.util.* | ||
import android.view.MotionEvent | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import androidx.lifecycle.LifecycleObserver | ||
import kotlin.math.* | ||
|
||
|
||
class StressTestActivity : AppCompatActivity() { | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_stress_test) | ||
|
||
// Hides the app/action bar | ||
supportActionBar?.hide() | ||
|
||
// Attach the Rive view to the activity's root layout | ||
val layout = findViewById<ViewGroup>(R.id.low_level_view_root) | ||
val riveView = StressTestView(this) | ||
|
||
layout.addView(riveView) | ||
} | ||
} | ||
|
||
class StressTestView(context: Context) : RiveTextureView(context) { | ||
private var instanceCount : Int = 1 | ||
private var totalElapsed : Float = 0f | ||
private var totalFrames : Int = 0 | ||
|
||
override fun createObserver(): LifecycleObserver { | ||
return object : DefaultLifecycleObserver { | ||
/* Optionally override lifecycle methods. */ | ||
// override fun onCreate(owner: LifecycleOwner) { | ||
// super.onCreate(owner) | ||
// } | ||
// override fun onDestroy(owner: LifecycleOwner) { | ||
// super.onDestroy(owner) | ||
// } | ||
} | ||
} | ||
|
||
override fun createRenderer(): Renderer { | ||
val renderer = object : Renderer() { | ||
|
||
override fun draw() { | ||
val step = animationInstance.effectiveDurationInSeconds / instanceCount | ||
artboard.let { | ||
save() | ||
align(Fit.CONTAIN, Alignment.CENTER, RectF(0f, 0f, width, height), it.bounds) | ||
val rows = (instanceCount + 6) / 7 | ||
val cols = min(instanceCount, 7) | ||
translate(0f, (rows - 1) * -.5f * 200f) | ||
for (j in 1 .. rows) { | ||
save() | ||
translate((cols - 1) * -.5f * 125f, 0f) | ||
for (i in 1..cols) { | ||
it.drawSkia(cppPointer) | ||
// Draw each Marty at a slightly different animation offset to make sure | ||
// the renderer can't cache things. This loop will advance the animation | ||
// until it loops back around to the original point we started at before | ||
// drawing. | ||
animationInstance.advance(step) | ||
animationInstance.apply() | ||
artboard.advance(step) | ||
translate(125f, 0f) | ||
} | ||
restore() | ||
translate(0f, 200f) | ||
} | ||
restore() | ||
} | ||
} | ||
|
||
override fun advance(elapsed: Float) { | ||
// Actually advance the animation here. draw() will also call advance(), but the | ||
// purpose of that is to draw each Marty at a slightly different animation offset, | ||
// and it will loop back around to the original animation location. | ||
animationInstance.advance(elapsed) | ||
animationInstance.apply() | ||
artboard.advance(elapsed) | ||
|
||
totalElapsed += elapsed | ||
totalFrames++ | ||
|
||
if (totalElapsed > 1f) { | ||
val fps = totalFrames / totalElapsed | ||
val fpsView = | ||
((getParent() as ViewGroup).getParent() as ViewGroup).getChildAt(1) as TextView | ||
fpsView.text = | ||
java.lang.String.format( | ||
Locale.US, | ||
"%d instances @ %.1f FPS (%.2f ms)", | ||
instanceCount, | ||
fps, | ||
1e3f / fps | ||
) | ||
totalElapsed = 0f | ||
totalFrames = 0 | ||
} | ||
} | ||
} | ||
// Call setup file only once we created the renderer. | ||
setupFile(renderer) | ||
return renderer | ||
} | ||
|
||
private lateinit var file: File | ||
|
||
// Objects that the renderer needs for drawing | ||
private lateinit var artboard: Artboard | ||
private lateinit var animationInstance: LinearAnimationInstance | ||
|
||
private fun setupFile(renderer: Renderer) { | ||
// Keep a reference to the file to keep resources around. | ||
file = resources.openRawResource(R.raw.marty).use { File(it.readBytes()) } | ||
artboard = file.firstArtboard | ||
animationInstance = artboard.animation(1) | ||
|
||
// This will be deleted with its dependents. | ||
renderer.dependencies.add(file) | ||
} | ||
|
||
override fun onTouchEvent(event: MotionEvent): Boolean { | ||
|
||
val action: Int = event.getActionMasked() | ||
|
||
return when (action) { | ||
MotionEvent.ACTION_DOWN -> { | ||
if (instanceCount < 7) | ||
instanceCount += 2 | ||
else | ||
instanceCount += 7 | ||
totalElapsed = 0f | ||
totalFrames = 0 | ||
val fpsView = ((getParent() as ViewGroup).getParent() as ViewGroup).getChildAt(1) as TextView | ||
fpsView.text = java.lang.String.format("%d instances", instanceCount) | ||
true | ||
} | ||
else -> super.onTouchEvent(event) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".StressTestActivity"> | ||
|
||
<LinearLayout | ||
android:id="@+id/container" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical" | ||
android:padding="24dp"> | ||
|
||
<androidx.constraintlayout.widget.ConstraintLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/low_level_view_root" | ||
android:layout_width="match_parent" | ||
android:layout_height="0dp" | ||
android:layout_weight="99" /> | ||
|
||
<TextView | ||
android:id="@+id/fps" | ||
android:layout_width="match_parent" | ||
android:layout_height="44dp" | ||
android:layout_weight="1" | ||
android:layout_marginTop="6dp" | ||
android:text="1 instances" | ||
android:textAppearance="@style/InfoTextMedium" /> | ||
</LinearLayout> | ||
|
||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters