perf: add caching and prewarming for DCP and depth estimation; reuse gainmap results

This commit is contained in:
zhoubinjia committed 2026-05-08 14:03:27 +08:00
1 parent 24c45e8b5f
commit 8ab1456c5e
4 files changed
+87 -11

No files matched your search

@@ -433,6 +433,7 @@ object GalleryManager {
chromaNoiseReduction: Float = 0f,
quality: Int = 92,
preparedUltraHdrSource: GainmapSourceSet? = null,
preparedGainmapResult: GainmapResult? = null,
): Boolean = withContext(Dispatchers.IO) {
beginHdrWork(photoId)
try {
@@ -468,10 +469,13 @@ object GalleryManager {
return@withContext false
}
val gainmapResult = gainmapProducer.build(
val gainmapResult = preparedGainmapResult ?: gainmapProducer.build(
ultraHdrSource,
HdrGainmapStrength.coerce(resolvedMetadata.hdrEffectStrength)
)
if (preparedGainmapResult != null) {
PLog.d(TAG, "buildDetailHdrCache reused prepared gainmap for $photoId")
}
FileOutputStream(tempFile).use { outputStream ->
if (!writeFinalJpeg(ultraHdrSource.sdrBase, outputStream, quality, gainmapResult)) {
tempFile.delete()
@@ -539,6 +543,7 @@ object GalleryManager {
photoQuality: Int = 95,
suffix: String? = null,
preparedUltraHdrSource: GainmapSourceSet? = null,
preparedGainmapResult: GainmapResult? = null,
): Boolean {
return withContext(Dispatchers.IO) {
val tempExportFile = File(context.cacheDir, "temp_export_${System.nanoTime()}.jpg")
@@ -574,13 +579,17 @@ object GalleryManager {
} else {
PLog.d(TAG, "prepareUltraHdrSource reused in-memory source for export: $id")
}
var gainmapResult: GainmapResult? = null
val gainmapElapsed = measureTimeMillis {
gainmapResult = ultraHdrSource?.let {
gainmapProducer.build(it, HdrGainmapStrength.coerce(metadata.hdrEffectStrength))
var gainmapResult: GainmapResult? = preparedGainmapResult
if (preparedGainmapResult == null) {
val gainmapElapsed = measureTimeMillis {
gainmapResult = ultraHdrSource?.let {
gainmapProducer.build(it, HdrGainmapStrength.coerce(metadata.hdrEffectStrength))
}
}
PLog.d(TAG, "gainmapProducer.build took ${gainmapElapsed}ms, enabled=${gainmapResult != null}")
} else {
PLog.d(TAG, "gainmapProducer.build reused prepared result, enabled=true")
}
PLog.d(TAG, "gainmapProducer.build took ${gainmapElapsed}ms, enabled=${gainmapResult != null}")
// 读取照片
val processedBitmap = (ultraHdrSource?.sdrBase ?: if (metadata.hasAiDenoisedBase) {
@@ -1201,6 +1210,14 @@ object GalleryManager {
} else {
null
}
val preparedGainmapResult = preparedUltraHdrSource?.let { source ->
var result: GainmapResult? = null
val gainmapElapsed = measureTimeMillis {
result = gainmapProducer.build(source, HdrGainmapStrength.coerce(metadata.hdrEffectStrength))
}
PLog.d(TAG, "saveRawPhoto prepared gainmap for reuse, took=${gainmapElapsed}ms")
result
}
preparedUltraHdrSource?.let {
PLog.d(TAG, "saveRawPhoto building detail HDR from in-memory RAW result: $photoId")
buildDetailHdrCache(
@@ -1210,7 +1227,8 @@ object GalleryManager {
sharpening = sharpeningValue,
noiseReduction = noiseReductionValue,
chromaNoiseReduction = chromaNoiseReductionValue,
preparedUltraHdrSource = it
preparedUltraHdrSource = it,
preparedGainmapResult = preparedGainmapResult
)
}
if (shouldAutoSave) {
@@ -1224,7 +1242,8 @@ object GalleryManager {
noiseReductionValue,
chromaNoiseReductionValue,
photoQuality,
preparedUltraHdrSource = preparedUltraHdrSource
preparedUltraHdrSource = preparedUltraHdrSource,
preparedGainmapResult = preparedGainmapResult
)
}
preparedUltraHdrSource?.hdrReference?.bitmap?.let {
@@ -102,6 +102,7 @@ object DcpProfileParser {
)
private val cache = mutableMapOf<String, CacheEntry>()
private val renderPlanCache = mutableMapOf<String, DcpRenderPlan>()
fun parse(filePath: String): DcpProfile? {
val file = File(filePath)
@@ -134,7 +135,15 @@ object DcpProfileParser {
metadata: RawMetadata
): DcpRenderPlan? {
if (dcpInfo == null) return null
val profile = parse(resolveFilePath(context, dcpInfo) ?: return null) ?: return null
val filePath = resolveFilePath(context, dcpInfo) ?: return null
val file = File(filePath)
val stamp = file.lastModified() xor file.length()
val renderPlanKey = buildRenderPlanKey(filePath, stamp, metadata)
synchronized(renderPlanCache) {
renderPlanCache[renderPlanKey]?.let { return it }
}
val profile = parse(filePath) ?: return null
val selectedHueSat = interpolateHueSatMap(profile, metadata)
val selectedMatrix = computeInterpolatedCameraToXyz(profile, metadata)?.let { cameraToXyz ->
val xyzToWorking = computeXyzD50ToGamut(ColorSpace.ProPhoto) ?: return@let cameraToXyz
@@ -147,13 +156,35 @@ object DcpProfileParser {
hueSatMap = selectedHueSat,
lookTable = profile.lookTable?.takeIf { it.isValid },
toneCurveLut = profile.toneCurve?.takeIf { it.isValid }?.toLut()
)
).also { plan ->
synchronized(renderPlanCache) {
renderPlanCache[renderPlanKey] = plan
}
}
}
fun prewarm(context: Context, dcpInfo: DcpInfo?) {
if (dcpInfo == null) return
val start = System.currentTimeMillis()
val filePath = resolveFilePath(context, dcpInfo) ?: return
parse(filePath)
PLog.d(TAG, "Prewarmed DCP: ${dcpInfo.id}, took=${System.currentTimeMillis() - start}ms")
}
fun clearCache() {
synchronized(cache) {
cache.clear()
}
synchronized(renderPlanCache) {
renderPlanCache.clear()
}
}
private fun buildRenderPlanKey(filePath: String, stamp: Long, metadata: RawMetadata): String {
val wbKey = metadata.whiteBalanceGains.joinToString(",") { gain ->
(gain * 1000f).toInt().toString()
}
return "$filePath|$stamp|$wbKey|${metadata.colorCorrectionMatrix.contentHashCode()}"
}
private fun resolveFilePath(context: Context, dcpInfo: DcpInfo): String? {
@@ -12,6 +12,7 @@ import com.hinnka.mycamera.utils.BitmapUtils
import com.hinnka.mycamera.utils.PLog
import com.hinnka.mycamera.utils.RawProcessor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.coroutines.withContext
import java.io.File
@@ -207,6 +208,13 @@ class RawDemosaicProcessor {
depthEstimator ?: DepthEstimator(context.applicationContext).also { depthEstimator = it }
}
}
suspend fun prewarmDepthEstimator(context: Context) = withContext(Dispatchers.Default) {
val start = System.currentTimeMillis()
getDepthEstimator(context.applicationContext)
PLog.d(TAG, "RAW DepthEstimator prewarmed, took=${System.currentTimeMillis() - start}ms")
}
private var gfChromaFboId = 0
// 缓冲区
@@ -35,7 +35,9 @@ import com.hinnka.mycamera.model.ColorRecipeParams
import com.hinnka.mycamera.model.SafeImage
import com.hinnka.mycamera.phantom.PhantomWidgetProvider
import com.hinnka.mycamera.raw.ColorSpace
import com.hinnka.mycamera.raw.DcpProfileParser
import com.hinnka.mycamera.raw.DcpInfo
import com.hinnka.mycamera.raw.RawDemosaicProcessor
import com.hinnka.mycamera.color.TransferCurve
import com.hinnka.mycamera.raw.RawProfile
import com.hinnka.mycamera.screencapture.PhantomPipCrop
@@ -713,7 +715,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
}
fun setRawDcpId(dcpId: String?) {
viewModelScope.launch { userPreferencesRepository.saveRawDcpId(dcpId) }
viewModelScope.launch {
userPreferencesRepository.saveRawDcpId(dcpId)
prewarmRawDcp(dcpId)
}
}
fun setRawBaselineLutId(lutId: String?) {
viewModelScope.launch {
@@ -809,6 +814,19 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
fun prewarmDepthEstimator() {
cameraController.previewDepthProcessor.prewarm()
viewModelScope.launch {
RawDemosaicProcessor.getInstance().prewarmDepthEstimator(getApplication<Application>())
}
viewModelScope.launch {
prewarmRawDcp(rawDcpId.firstOrNull())
}
}
private suspend fun prewarmRawDcp(dcpId: String?) = withContext(Dispatchers.IO) {
val dcpInfo = dcpId?.let { id ->
contentRepository.getAvailableDcps().firstOrNull { it.id == id }
} ?: return@withContext
DcpProfileParser.prewarm(getApplication<Application>(), dcpInfo)
}
/**