Skip to content

Commit 11c0133

Browse files
committed
fix: remove all suspending methods from rendering to allow blocking code inside render and cleanup blocks
1 parent d399347 commit 11c0133

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

src/main/java/dev/silenium/compose/gl/fbo/FBOFifoSwapChain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class FBOFifoSwapChain(capacity: Int, override val fboCreator: (IntSize) -> FBO)
2525
return@withLock result
2626
}
2727

28-
override suspend fun <R> render(block: suspend (FBO) -> R): R? {
28+
override fun <R> render(block: (FBO) -> R): R? {
2929
val fbo = renderQueue.poll() ?: return null
3030
if (fbo.size != size) {
3131
fbo.destroy()

src/main/java/dev/silenium/compose/gl/fbo/FBOMailboxSwapChain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FBOMailboxSwapChain(private val capacity: Int, override val fboCreator: (I
1919
return@stateLock result
2020
}
2121

22-
override suspend fun <R> render(block: suspend (FBO) -> R): R? = stateLock.read {
22+
override fun <R> render(block: (FBO) -> R): R? = stateLock.read {
2323
val fbos = fbos ?: return null
2424
val next = (current + 1) % fbos.size
2525
if (fbos[next].size != size) {

src/main/java/dev/silenium/compose/gl/fbo/FBOPool.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class FBOPool(
7979
* @param block The block to render the frame.
8080
* @return wait time for the next frame, or null, if there was no frame rendered due to no framebuffers being available.
8181
*/
82-
suspend fun render(deltaTime: Duration, block: suspend GLDrawScope.() -> Unit): Result<Duration?> = try {
82+
fun render(deltaTime: Duration, block: GLDrawScope.() -> Unit): Result<Duration?> = try {
8383
ensureContext(ContextType.RENDER) {
8484
if (swapChain.size != size) {
8585
swapChain.resize(size)

src/main/java/dev/silenium/compose/gl/fbo/FBOSwapChain.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract class FBOSwapChain {
2626
* @param block The block to run with the current FBO for rendering
2727
* @return The result of the block
2828
*/
29-
abstract suspend fun <R> render(block: suspend (FBO) -> R): R?
29+
abstract fun <R> render(block: (FBO) -> R): R?
3030
abstract fun resize(size: IntSize)
3131
abstract fun destroyFBOs()
3232
}

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ import dev.silenium.compose.gl.context.GLContextProvider
1818
import dev.silenium.compose.gl.context.GLContextProviderFactory
1919
import dev.silenium.compose.gl.directContext
2020
import dev.silenium.compose.gl.fbo.*
21-
import kotlinx.coroutines.*
21+
import kotlinx.coroutines.CancellationException
22+
import kotlinx.coroutines.Dispatchers
23+
import kotlinx.coroutines.isActive
24+
import kotlinx.coroutines.withContext
2225
import org.jetbrains.skia.*
2326
import org.lwjgl.opengl.GL
2427
import org.lwjgl.opengl.GL30.GL_RGBA8
@@ -60,8 +63,8 @@ fun GLSurfaceView(
6063
presentMode: GLSurfaceView.PresentMode = GLSurfaceView.PresentMode.FIFO,
6164
swapChainSize: Int = 10,
6265
fboSizeOverride: FBOSizeOverride? = null,
63-
cleanup: suspend () -> Unit = {},
64-
draw: suspend GLDrawScope.() -> Unit,
66+
cleanup: () -> Unit = {},
67+
draw: GLDrawScope.() -> Unit,
6568
) {
6669
var invalidations by remember { mutableStateOf(0) }
6770
val surfaceView = remember {
@@ -139,8 +142,8 @@ fun GLSurfaceView(
139142
class GLSurfaceView internal constructor(
140143
private val state: GLSurfaceState,
141144
private val parentContext: GLContext<*>,
142-
private val drawBlock: suspend GLDrawScope.() -> Unit,
143-
private val cleanupBlock: suspend () -> Unit = {},
145+
private val drawBlock: GLDrawScope.() -> Unit,
146+
private val cleanupBlock: () -> Unit = {},
144147
private val invalidate: () -> Unit = {},
145148
private val paint: Paint = Paint(),
146149
private val presentMode: PresentMode = PresentMode.MAILBOX,
@@ -232,10 +235,8 @@ class GLSurfaceView internal constructor(
232235
var lastFrame: Long? = null
233236
while (!isInterrupted) {
234237
val renderStart = System.nanoTime()
235-
val renderResult = runBlocking {
236-
val deltaTime = lastFrame?.let { renderStart - it } ?: 0
237-
fboPool!!.render(deltaTime.nanoseconds, drawBlock)
238-
}
238+
val deltaTime = lastFrame?.let { renderStart - it } ?: 0
239+
val renderResult = fboPool!!.render(deltaTime.nanoseconds, drawBlock)
239240
val e = renderResult.exceptionOrNull()
240241
if (e is NoRenderFBOAvailable) {
241242
logger.debug("No FBO available, waiting for the next frame")
@@ -266,7 +267,7 @@ class GLSurfaceView internal constructor(
266267
}
267268
}
268269
logger.debug("GLSurfaceView stopped")
269-
runBlocking { cleanupBlock() }
270+
cleanupBlock()
270271
fboPool?.destroy()
271272
fboPool = null
272273
directContext?.close()

0 commit comments

Comments
 (0)