Skip to content

Commit

Permalink
fix: remove all suspending methods from rendering to allow blocking c…
Browse files Browse the repository at this point in the history
…ode inside render and cleanup blocks
  • Loading branch information
silenium-dev committed Oct 8, 2024
1 parent d399347 commit 11c0133
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class FBOFifoSwapChain(capacity: Int, override val fboCreator: (IntSize) -> FBO)
return@withLock result
}

override suspend fun <R> render(block: suspend (FBO) -> R): R? {
override fun <R> render(block: (FBO) -> R): R? {
val fbo = renderQueue.poll() ?: return null
if (fbo.size != size) {
fbo.destroy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class FBOMailboxSwapChain(private val capacity: Int, override val fboCreator: (I
return@stateLock result
}

override suspend fun <R> render(block: suspend (FBO) -> R): R? = stateLock.read {
override fun <R> render(block: (FBO) -> R): R? = stateLock.read {
val fbos = fbos ?: return null
val next = (current + 1) % fbos.size
if (fbos[next].size != size) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/silenium/compose/gl/fbo/FBOPool.kt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class FBOPool(
* @param block The block to render the frame.
* @return wait time for the next frame, or null, if there was no frame rendered due to no framebuffers being available.
*/
suspend fun render(deltaTime: Duration, block: suspend GLDrawScope.() -> Unit): Result<Duration?> = try {
fun render(deltaTime: Duration, block: GLDrawScope.() -> Unit): Result<Duration?> = try {
ensureContext(ContextType.RENDER) {
if (swapChain.size != size) {
swapChain.resize(size)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/silenium/compose/gl/fbo/FBOSwapChain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class FBOSwapChain {
* @param block The block to run with the current FBO for rendering
* @return The result of the block
*/
abstract suspend fun <R> render(block: suspend (FBO) -> R): R?
abstract fun <R> render(block: (FBO) -> R): R?
abstract fun resize(size: IntSize)
abstract fun destroyFBOs()
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/dev/silenium/compose/gl/surface/GLSurfaceView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ import dev.silenium.compose.gl.context.GLContextProvider
import dev.silenium.compose.gl.context.GLContextProviderFactory
import dev.silenium.compose.gl.directContext
import dev.silenium.compose.gl.fbo.*
import kotlinx.coroutines.*
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.isActive
import kotlinx.coroutines.withContext
import org.jetbrains.skia.*
import org.lwjgl.opengl.GL
import org.lwjgl.opengl.GL30.GL_RGBA8
Expand Down Expand Up @@ -60,8 +63,8 @@ fun GLSurfaceView(
presentMode: GLSurfaceView.PresentMode = GLSurfaceView.PresentMode.FIFO,
swapChainSize: Int = 10,
fboSizeOverride: FBOSizeOverride? = null,
cleanup: suspend () -> Unit = {},
draw: suspend GLDrawScope.() -> Unit,
cleanup: () -> Unit = {},
draw: GLDrawScope.() -> Unit,
) {
var invalidations by remember { mutableStateOf(0) }
val surfaceView = remember {
Expand Down Expand Up @@ -139,8 +142,8 @@ fun GLSurfaceView(
class GLSurfaceView internal constructor(
private val state: GLSurfaceState,
private val parentContext: GLContext<*>,
private val drawBlock: suspend GLDrawScope.() -> Unit,
private val cleanupBlock: suspend () -> Unit = {},
private val drawBlock: GLDrawScope.() -> Unit,
private val cleanupBlock: () -> Unit = {},
private val invalidate: () -> Unit = {},
private val paint: Paint = Paint(),
private val presentMode: PresentMode = PresentMode.MAILBOX,
Expand Down Expand Up @@ -232,10 +235,8 @@ class GLSurfaceView internal constructor(
var lastFrame: Long? = null
while (!isInterrupted) {
val renderStart = System.nanoTime()
val renderResult = runBlocking {
val deltaTime = lastFrame?.let { renderStart - it } ?: 0
fboPool!!.render(deltaTime.nanoseconds, drawBlock)
}
val deltaTime = lastFrame?.let { renderStart - it } ?: 0
val renderResult = fboPool!!.render(deltaTime.nanoseconds, drawBlock)
val e = renderResult.exceptionOrNull()
if (e is NoRenderFBOAvailable) {
logger.debug("No FBO available, waiting for the next frame")
Expand Down Expand Up @@ -266,7 +267,7 @@ class GLSurfaceView internal constructor(
}
}
logger.debug("GLSurfaceView stopped")
runBlocking { cleanupBlock() }
cleanupBlock()
fboPool?.destroy()
fboPool = null
directContext?.close()
Expand Down

0 comments on commit 11c0133

Please sign in to comment.