refactor: use SharedDepthEstimator for depth estimation tasks

This commit is contained in:
zhoubinjia committed 2026-05-08 15:00:30 +08:00
1 parent cb78ba052d
commit 082c542878
6 files changed
+73 -32

No files matched your search

@@ -205,7 +205,7 @@ class LutManager(private val context: Context) {
// 查找 LUT 信息 // 查找 LUT 信息
val lutInfo = getLutInfo(id) ?: run { val lutInfo = getLutInfo(id) ?: run {
PLog.w(TAG, "LUT not found: $id") // PLog.w(TAG, "LUT not found: $id")
return null return null
} }
@@ -20,7 +20,7 @@ import java.nio.ByteOrder
class DepthEstimator( class DepthEstimator(
context: Context, context: Context,
private val modelAssetName: String = MODEL_MIDAS val modelAssetName: String = MODEL_MIDAS
) { ) {
private var interpreter: Interpreter? = null private var interpreter: Interpreter? = null
private var gpuDelegate: GpuDelegate? = null private var gpuDelegate: GpuDelegate? = null
@@ -0,0 +1,56 @@
package com.hinnka.mycamera.ml
import android.content.Context
import android.graphics.Bitmap
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
object SharedDepthEstimator {
private val mutex = Mutex()
@Volatile
private var estimator: DepthEstimator? = null
suspend fun prewarm(context: Context, modelAssetName: String = DepthEstimator.MODEL_MIDAS) {
withEstimator(context, modelAssetName) { }
}
suspend fun estimateDepth(
context: Context,
inputBitmap: Bitmap,
modelAssetName: String = DepthEstimator.MODEL_MIDAS
): Bitmap? {
return withEstimator(context, modelAssetName) { estimator ->
estimator.estimateDepth(inputBitmap)
}
}
private suspend fun <T> withEstimator(
context: Context,
modelAssetName: String,
block: (DepthEstimator) -> T
): T = withContext(Dispatchers.Default) {
mutex.withLock {
val current = estimator
val resolved = if (current?.modelAssetName == modelAssetName) {
current
} else {
current?.close()
DepthEstimator(context.applicationContext, modelAssetName).also {
estimator = it
}
}
block(resolved)
}
}
suspend fun close() {
withContext(Dispatchers.Default) {
mutex.withLock {
estimator?.close()
estimator = null
}
}
}
}
@@ -2,7 +2,7 @@ package com.hinnka.mycamera.preview
import android.content.Context import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import com.hinnka.mycamera.ml.DepthEstimator import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.utils.PLog import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.StartupTrace import com.hinnka.mycamera.utils.StartupTrace
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@@ -22,8 +22,8 @@ class PreviewDepthProcessor(private val context: Context) {
} }
private val scope = CoroutineScope(Dispatchers.Default + Job()) private val scope = CoroutineScope(Dispatchers.Default + Job())
private var depthEstimator: DepthEstimator? = null
private var isPrewarming = false private var isPrewarming = false
private var isPrewarmed = false
private var isProcessing = false private var isProcessing = false
// Expose the latest depth map as a StateFlow for the UI or GL pipeline // Expose the latest depth map as a StateFlow for the UI or GL pipeline
@@ -31,14 +31,15 @@ class PreviewDepthProcessor(private val context: Context) {
val latestDepthMap = _latestDepthMap.asStateFlow() val latestDepthMap = _latestDepthMap.asStateFlow()
fun prewarm() { fun prewarm() {
if (depthEstimator != null || isPrewarming) { if (isPrewarmed || isPrewarming) {
return return
} }
isPrewarming = true isPrewarming = true
scope.launch { scope.launch {
try { try {
StartupTrace.mark("PreviewDepthProcessor.prewarm start") StartupTrace.mark("PreviewDepthProcessor.prewarm start")
depthEstimator = DepthEstimator(context) SharedDepthEstimator.prewarm(context)
isPrewarmed = true
StartupTrace.mark("PreviewDepthProcessor.prewarm end") StartupTrace.mark("PreviewDepthProcessor.prewarm end")
} catch (e: Exception) { } catch (e: Exception) {
PLog.e(TAG, "Failed to prewarm depth estimator", e) PLog.e(TAG, "Failed to prewarm depth estimator", e)
@@ -57,8 +58,7 @@ class PreviewDepthProcessor(private val context: Context) {
return return
} }
val estimator = depthEstimator if (!isPrewarmed) {
if (estimator == null) {
prewarm() prewarm()
return return
} }
@@ -68,7 +68,7 @@ class PreviewDepthProcessor(private val context: Context) {
// Process on background thread // Process on background thread
scope.launch { scope.launch {
try { try {
val depthMap = estimator.estimateDepth(bitmap) val depthMap = SharedDepthEstimator.estimateDepth(context, bitmap)
_latestDepthMap.value = depthMap _latestDepthMap.value = depthMap
} catch (e: Exception) { } catch (e: Exception) {
PLog.e(TAG, "Error processing preview bitmap for depth", e) PLog.e(TAG, "Error processing preview bitmap for depth", e)
@@ -79,7 +79,6 @@ class PreviewDepthProcessor(private val context: Context) {
} }
fun release() { fun release() {
depthEstimator?.close() isPrewarmed = false
depthEstimator = null
} }
} }
@@ -4,7 +4,7 @@ import android.content.Context
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.BitmapFactory import android.graphics.BitmapFactory
import com.hinnka.mycamera.gallery.GalleryManager import com.hinnka.mycamera.gallery.GalleryManager
import com.hinnka.mycamera.ml.DepthEstimator import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.utils.PLog import com.hinnka.mycamera.utils.PLog
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
@@ -21,10 +21,6 @@ class DepthBokehProcessor(context: Context) {
private val appContext = context.applicationContext private val appContext = context.applicationContext
private val processor = OglBokehProcessor() private val processor = OglBokehProcessor()
private val depthEstimator by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
// DepthEstimator(appContext, DepthEstimator.MODEL_DEPTH_ANYTHING)
DepthEstimator(appContext, DepthEstimator.MODEL_MIDAS)
}
private val mutex = Mutex() private val mutex = Mutex()
/** /**
@@ -59,7 +55,7 @@ class DepthBokehProcessor(context: Context) {
val inputForBokeh = ensureArgb8888(originalImage) val inputForBokeh = ensureArgb8888(originalImage)
if (depthMap == null) { if (depthMap == null) {
depthMap = depthEstimator.estimateDepth(inputForBokeh) depthMap = SharedDepthEstimator.estimateDepth(appContext, inputForBokeh)
if (depthMap != null && depthFile != null) { if (depthMap != null && depthFile != null) {
try { try {
@@ -111,7 +107,5 @@ class DepthBokehProcessor(context: Context) {
return converted ?: bitmap return converted ?: bitmap
} }
fun close() { fun close() = Unit
depthEstimator.close()
}
} }
@@ -7,7 +7,7 @@ import android.media.Image
import android.opengl.* import android.opengl.*
import com.hinnka.mycamera.data.ContentRepository import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.camera.AspectRatio import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.ml.DepthEstimator import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.utils.BitmapUtils import com.hinnka.mycamera.utils.BitmapUtils
import com.hinnka.mycamera.utils.PLog import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.RawProcessor import com.hinnka.mycamera.utils.RawProcessor
@@ -201,17 +201,9 @@ class RawDemosaicProcessor {
private var gfHeight = 0 private var gfHeight = 0
private var gfChromaTexId = 0 private var gfChromaTexId = 0
private var depthEstimator: DepthEstimator? = null
private fun getDepthEstimator(context: Context): DepthEstimator {
return depthEstimator ?: synchronized(this) {
depthEstimator ?: DepthEstimator(context.applicationContext).also { depthEstimator = it }
}
}
suspend fun prewarmDepthEstimator(context: Context) = withContext(Dispatchers.Default) { suspend fun prewarmDepthEstimator(context: Context) = withContext(Dispatchers.Default) {
val start = System.currentTimeMillis() val start = System.currentTimeMillis()
getDepthEstimator(context.applicationContext) SharedDepthEstimator.prewarm(context.applicationContext)
PLog.d(TAG, "RAW DepthEstimator prewarmed, took=${System.currentTimeMillis() - start}ms") PLog.d(TAG, "RAW DepthEstimator prewarmed, took=${System.currentTimeMillis() - start}ms")
} }
@@ -2132,7 +2124,7 @@ class RawDemosaicProcessor {
drawQuad(linearProgram) drawQuad(linearProgram)
} }
private fun resolveRawAutoExposureEv( private suspend fun resolveRawAutoExposureEv(
context: Context, context: Context,
metadata: RawMetadata, metadata: RawMetadata,
sourceTextureId: Int, sourceTextureId: Int,
@@ -2170,7 +2162,7 @@ class RawDemosaicProcessor {
bitmap.copyPixelsFromBuffer(buffer) bitmap.copyPixelsFromBuffer(buffer)
buffer.position(0) // Reset for MeteringSystem buffer.position(0) // Reset for MeteringSystem
val depthMap = getDepthEstimator(context).estimateDepth(bitmap) val depthMap = SharedDepthEstimator.estimateDepth(context, bitmap)
val weightBuffer = depthMap?.let { val weightBuffer = depthMap?.let {
val wb = ByteBuffer.allocateDirect(it.byteCount) val wb = ByteBuffer.allocateDirect(it.byteCount)
it.copyPixelsToBuffer(wb) it.copyPixelsToBuffer(wb)