Skip to content

Commit d399347

Browse files
committed
fix: add back invalidations
1 parent c2dfdb9 commit d399347

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

src/main/java/dev/silenium/compose/gl/surface/GLSurfaceView.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ fun GLSurfaceView(
6363
cleanup: suspend () -> Unit = {},
6464
draw: suspend GLDrawScope.() -> Unit,
6565
) {
66+
var invalidations by remember { mutableStateOf(0) }
6667
val surfaceView = remember {
6768
val currentContext = glContextProvider.fromCurrent() ?: error("No current EGL context")
6869
GLSurfaceView(
6970
state = state,
7071
parentContext = currentContext,
72+
invalidate = { invalidations++ },
7173
paint = paint,
7274
presentMode = presentMode,
7375
swapChainSize = swapChainSize,
@@ -115,9 +117,11 @@ fun GLSurfaceView(
115117
}
116118
}
117119
) {
120+
invalidations.let {
118121
directContext?.let { directContext ->
119122
surfaceView.display(drawContext.canvas.nativeCanvas, directContext)
120123
}
124+
}
121125
}
122126
}
123127
DisposableEffect(surfaceView) {
@@ -137,6 +141,7 @@ class GLSurfaceView internal constructor(
137141
private val parentContext: GLContext<*>,
138142
private val drawBlock: suspend GLDrawScope.() -> Unit,
139143
private val cleanupBlock: suspend () -> Unit = {},
144+
private val invalidate: () -> Unit = {},
140145
private val paint: Paint = Paint(),
141146
private val presentMode: PresentMode = PresentMode.MAILBOX,
142147
private val swapChainSize: Int = 10,
@@ -181,6 +186,7 @@ class GLSurfaceView internal constructor(
181186
internal fun display(canvas: Canvas, displayContext: DirectContext) {
182187
val t1 = System.nanoTime()
183188
fboPool?.display { displayImpl(canvas, displayContext) }
189+
invalidate()
184190
val t2 = System.nanoTime()
185191
state.onDisplay(t2, (t2 - t1).nanoseconds)
186192
}
@@ -242,6 +248,7 @@ class GLSurfaceView internal constructor(
242248
break
243249
}
244250
val waitTime = renderResult.getOrNull()
251+
invalidate()
245252
val renderEnd = System.nanoTime()
246253
state.onRender(renderEnd, (renderEnd - renderStart).nanoseconds)
247254
lastFrame = renderStart

src/test/kotlin/Main.kt

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.compose.foundation.background
66
import androidx.compose.foundation.layout.*
77
import androidx.compose.material.Button
88
import androidx.compose.material.MaterialTheme
9+
import androidx.compose.material.Surface
910
import androidx.compose.material.Text
1011
import androidx.compose.runtime.*
1112
import androidx.compose.ui.Alignment
@@ -62,7 +63,6 @@ fun ApplicationScope.App() {
6263
glClearColor(color.red, color.green, color.blue, color.alpha)
6364
glClear(GL_COLOR_BUFFER_BIT)
6465
glBegin(GL_QUADS)
65-
// fat rectangle from top left to bottom right
6666
glColor3f(1f, 0f, 0f)
6767
glVertex2f(-1f, 0f)
6868
glColor3f(0f, 1f, 0f)
@@ -76,20 +76,22 @@ fun ApplicationScope.App() {
7676
val wait = (1000.0 / 60).milliseconds
7777
redrawAfter(null)
7878
}
79-
Column(modifier = Modifier.align(Alignment.TopStart).padding(4.dp)) {
80-
val display by state.displayStatistics.collectAsState()
81-
Text("Display datapoints: ${display.frameTimes.values.size}")
82-
Text("Display frame time: ${display.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
83-
Text("Display frame time (99th): ${display.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
84-
Text("Display FPS: ${display.fps.median}")
85-
Text("Display FPS (99th): ${display.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
79+
Surface(modifier = Modifier.align(Alignment.TopStart).padding(4.dp)) {
80+
Column(modifier = Modifier.padding(4.dp).width(400.dp)) {
81+
val display by state.displayStatistics.collectAsState()
82+
Text("Display datapoints: ${display.frameTimes.values.size}")
83+
Text("Display frame time: ${display.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
84+
Text("Display frame time (99th): ${display.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
85+
Text("Display FPS: ${display.fps.median}")
86+
Text("Display FPS (99th): ${display.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
8687

87-
val render by state.renderStatistics.collectAsState()
88-
Text("Render datapoints: ${render.frameTimes.values.size}")
89-
Text("Render frame time: ${render.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
90-
Text("Render frame time (99th): ${render.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
91-
Text("Render FPS: ${render.fps.median} ms")
92-
Text("Render FPS (99th): ${render.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
88+
val render by state.renderStatistics.collectAsState()
89+
Text("Render datapoints: ${render.frameTimes.values.size}")
90+
Text("Render frame time: ${render.frameTimes.median.inWholeMicroseconds / 1000.0} ms")
91+
Text("Render frame time (99th): ${render.frameTimes.percentile(0.99).inWholeMicroseconds / 1000.0} ms")
92+
Text("Render FPS: ${render.fps.median} ms")
93+
Text("Render FPS (99th): ${render.fps.percentile(0.99, Stats.Percentile.LOWEST)}")
94+
}
9395
}
9496
Button(
9597
onClick = ::exitApplication,

0 commit comments

Comments
 (0)