This commit is contained in:
zhoubinjia committed 2026-07-23 20:02:05 +08:00
1 parent f5d6d6764d
commit 4eee16abce
2 files changed
+20 -6

No files matched your search

@@ -2,13 +2,20 @@ package com.hinnka.mycamera.ml
import android.content.Context
import android.graphics.Bitmap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
object SharedFaceDetLiteFocusDetector {
private val mutex = Mutex()
// TFLite's OpenGL delegate must be created, invoked, and closed on the same OS thread.
private val detectorDispatcher = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable, "SharedFaceDetLiteFocusDetector").apply {
isDaemon = true
}
}.asCoroutineDispatcher()
private var detector: FaceDetLiteFocusDetector? = null
suspend fun prewarm(context: Context) {
@@ -26,7 +33,7 @@ object SharedFaceDetLiteFocusDetector {
}
suspend fun release() {
withContext(Dispatchers.Default) {
withContext(detectorDispatcher) {
mutex.withLock {
detector?.close()
detector = null
@@ -37,7 +44,7 @@ object SharedFaceDetLiteFocusDetector {
private suspend fun <T> withDetector(
context: Context,
block: (FaceDetLiteFocusDetector) -> T,
): T = withContext(Dispatchers.Default) {
): T = withContext(detectorDispatcher) {
mutex.withLock {
val activeDetector = detector ?: FaceDetLiteFocusDetector(context.applicationContext).also {
detector = it
@@ -2,13 +2,20 @@ package com.hinnka.mycamera.ml
import android.content.Context
import com.hinnka.mycamera.data.AiFocusTargetMode
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.util.concurrent.Executors
object SharedYoloXObjectDetector {
private val mutex = Mutex()
// TFLite's OpenGL delegate must be created, invoked, and closed on the same OS thread.
private val detectorDispatcher = Executors.newSingleThreadExecutor { runnable ->
Thread(runnable, "SharedYoloXObjectDetector").apply {
isDaemon = true
}
}.asCoroutineDispatcher()
private var detector: YoloXObjectDetector? = null
suspend fun prewarm(context: Context) {
@@ -29,7 +36,7 @@ object SharedYoloXObjectDetector {
}
suspend fun release() {
withContext(Dispatchers.Default) {
withContext(detectorDispatcher) {
mutex.withLock {
detector?.close()
detector = null
@@ -40,7 +47,7 @@ object SharedYoloXObjectDetector {
private suspend fun <T> withDetector(
context: Context,
block: (YoloXObjectDetector) -> T,
): T = withContext(Dispatchers.Default) {
): T = withContext(detectorDispatcher) {
mutex.withLock {
val activeDetector = detector ?: YoloXObjectDetector(context.applicationContext).also {
detector = it