Skip to content

Commit

Permalink
call arcore.destroy off of main thread Fixes #407 (#408)
Browse files Browse the repository at this point in the history
* call arcore.destroy off of main thread

* rename method as per PR comment

---------

Co-authored-by: Sameer Jiwani <>
  • Loading branch information
sameerjj authored Feb 6, 2024
1 parent 2230c5a commit ad8778b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions arsceneview/src/main/java/io/github/sceneview/ar/ARSceneView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.activity.ComponentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.coroutineScope
import com.google.android.filament.Engine
import com.google.android.filament.IndirectLight
import com.google.android.filament.MaterialInstance
Expand Down Expand Up @@ -44,6 +45,9 @@ import io.github.sceneview.model.Model
import io.github.sceneview.model.ModelInstance
import io.github.sceneview.node.LightNode
import io.github.sceneview.node.Node
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* A SurfaceView that integrates with ARCore and renders a scene
Expand Down Expand Up @@ -561,7 +565,7 @@ open class ARSceneView @JvmOverloads constructor(

override fun destroy() {
if (!isDestroyed) {
arCore.destroy()
destroyARCore()

defaultCameraNode?.destroy()
defaultCameraStream?.destroy()
Expand All @@ -573,6 +577,14 @@ open class ARSceneView @JvmOverloads constructor(
super.destroy()
}

fun destroyARCore() {
val scope = lifecycle?.coroutineScope ?: CoroutineScope(Dispatchers.IO)
scope.launch(Dispatchers.IO) {
// destroy should be called off the main thread since it hangs for many seconds
arCore.destroy()
}
}

class DefaultARCameraNode(engine: Engine) : ARCameraNode(engine) {
init {
// Set the exposure on the camera, this exposure follows the sunny f/16 rule
Expand All @@ -596,7 +608,7 @@ open class ARSceneView @JvmOverloads constructor(
}

override fun onDestroy(owner: LifecycleOwner) {
arCore.destroy()
destroyARCore()
}
}

Expand Down

0 comments on commit ad8778b

Please sign in to comment.