feat: mfsr logs process

This commit is contained in:
zhoubinjia committed 2026-07-08 14:06:17 +08:00
1 parent 2ec368bc0e
commit c4bde5f53b
6 files changed
+118 -72

No files matched your search

@@ -255,15 +255,14 @@ class GlesRawStacker(
initPrograms()
initResources()
applyRawRenderState()
PLog.d(
TAG,
RawStackRuntimeDebug.d(TAG) {
"HWMF RAW stack mode=${tuning.mode} frames=${images.size} " +
"out=${outputWidth}x$outputHeight srScale=${superResolutionScale.formatScale()} " +
"${registrationSummary.compactSummary()} regProxy=REG_OUT " +
"regCtx=${registrationSetup.referenceContextSummary()} " +
"prefilterLevels=$pyramidLevels flowGrid=$flowGridSpacing " +
"blendLk=$lkRefinePasses blendSmooth=$flowSmoothPasses"
)
}
images[0].use {
uploadRawTexture(it, refRaw, "reference")
}
@@ -312,7 +311,9 @@ class GlesRawStacker(
} else {
accumulateFrame(curRaw, isReference = false)
}
recordAlignmentDiagnostics()
if (hwmfDebug.collectMetrics) {
recordAlignmentDiagnostics()
}
alignedFrameCount += 1
GlesGpuScheduler.yieldToUiRenderer()
}
@@ -330,19 +331,22 @@ class GlesRawStacker(
GlesGpuScheduler.yieldToUiRenderer()
val readTiming = readOutput(outputBuffer)
outputBuffer.rewind()
val diagnostics = collectFinalDiagnostics(
frameCount = images.size,
alignedFrameCount = alignedFrameCount,
elapsedMs = System.currentTimeMillis() - startTime,
superResolutionDecision = superResolutionDecision,
)
val diagnostics = if (hwmfDebug.collectMetrics) {
collectFinalDiagnostics(
frameCount = images.size,
alignedFrameCount = alignedFrameCount,
elapsedMs = System.currentTimeMillis() - startTime,
superResolutionDecision = superResolutionDecision,
)
} else {
null
}
returned = true
PLog.i(
TAG,
RawStackRuntimeDebug.i(TAG) {
"GLES RAW stacking completed in ${System.currentTimeMillis() - startTime}ms " +
"readback=${readTiming.elapsedMs}ms glRead=${readTiming.glReadMs}ms " +
"copy=${readTiming.copyMs}ms alloc=${readTiming.allocMs}ms mode=${readTiming.mode}"
)
}
RawStackResult(
fusedBayerBuffer = outputBuffer,
width = outputWidth,
@@ -400,14 +404,13 @@ class GlesRawStacker(
initHdrPrograms()
initResources()
applyRawRenderState()
PLog.d(
TAG,
RawStackRuntimeDebug.d(TAG) {
"HWMF RAW HDR stack mode=${tuning.mode} normalFrames=${normalFrames.size} " +
"${registrationSummary.compactSummary()} regProxy=REG_OUT " +
"regCtx=${registrationSetup.referenceContextSummary()} " +
"prefilterLevels=$pyramidLevels flowGrid=$flowGridSpacing " +
"blendLk=$lkRefinePasses blendSmooth=$flowSmoothPasses"
)
}
val shortExposureProduct = validExposureProduct(shortFrame.exposureProduct)
val referenceExposureProduct = validExposureProduct(normalFrames.first().exposureProduct)
@@ -475,7 +478,9 @@ class GlesRawStacker(
exposureScale = outputScale,
hdrMode = true,
)
recordAlignmentDiagnostics()
if (hwmfDebug.collectMetrics) {
recordAlignmentDiagnostics()
}
alignedFrameCount += 1
GlesGpuScheduler.waitForGpuCheckpoint(TAG, "hdr normal frame $frameIndex accumulation")
}
@@ -491,18 +496,21 @@ class GlesRawStacker(
val readTiming = readOutput(outputBuffer)
outputBuffer.rewind()
val profileGainTableMap = computeHdrProfileGainTableMap(rawHdrBaselineExposureEv)
val diagnostics = collectFinalDiagnostics(
frameCount = normalFrames.size + 1,
alignedFrameCount = alignedFrameCount,
elapsedMs = System.currentTimeMillis() - startTime,
)
val diagnostics = if (hwmfDebug.collectMetrics) {
collectFinalDiagnostics(
frameCount = normalFrames.size + 1,
alignedFrameCount = alignedFrameCount,
elapsedMs = System.currentTimeMillis() - startTime,
)
} else {
null
}
returned = true
PLog.i(
TAG,
RawStackRuntimeDebug.i(TAG) {
"GLES RAW HDR stacking completed in ${System.currentTimeMillis() - startTime}ms " +
"readback=${readTiming.elapsedMs}ms glRead=${readTiming.glReadMs}ms " +
"copy=${readTiming.copyMs}ms alloc=${readTiming.allocMs}ms mode=${readTiming.mode}"
)
}
RawStackResult(
fusedBayerBuffer = outputBuffer,
width = width,
@@ -654,22 +662,21 @@ class GlesRawStacker(
"RAW ${tuning.mode} output ${outputWidth}x$outputHeight exceeds GL_MAX_TEXTURE_SIZE=$maxSize",
)
}
if (superResolutionEnabled) {
if (superResolutionEnabled && RawStackRuntimeDebug.enabled) {
val accumulatorBytes = outputWidth.toLong() * outputHeight.toLong() * 8L
val accumulatorTotalBytes = accumulatorBytes * 2L
val baseAccumulatorBytes = width.toLong() * height.toLong() * 8L
val baseAccumulatorTotalBytes = baseAccumulatorBytes * 2L
val baseOutputBytes = width.toLong() * height.toLong() * 2L
val outputBytes = outputWidth.toLong() * outputHeight.toLong() * 2L
PLog.d(
TAG,
RawStackRuntimeDebug.d(TAG) {
"HWMF MFSR resources out=${outputWidth}x$outputHeight maxTex=$maxSize " +
"srAccumulator=${accumulatorBytes.mibString()}x2 " +
"srAccumulatorTotal=${accumulatorTotalBytes.mibString()} " +
"baseAccumulator=${baseAccumulatorBytes.mibString()}x2 " +
"baseAccumulatorTotal=${baseAccumulatorTotalBytes.mibString()} " +
"baseOutput=${baseOutputBytes.mibString()} output=${outputBytes.mibString()}",
)
"baseOutput=${baseOutputBytes.mibString()} output=${outputBytes.mibString()}"
}
}
}
@@ -844,7 +851,9 @@ class GlesRawStacker(
val pgtmGridHeight = grid.getOrElse(1) { 0 }
if (pgtmGridWidth <= 0 || pgtmGridHeight <= 0) return null
if (!pgtmStatsBounds.isFullImage()) {
PLog.d(TAG, "GPU HDR PGTM stats bounds: source=${width}x$height bounds=$pgtmStatsBounds")
RawStackRuntimeDebug.d(TAG) {
"GPU HDR PGTM stats bounds: source=${width}x$height bounds=$pgtmStatsBounds"
}
}
val cellCount = pgtmGridWidth * pgtmGridHeight
@@ -1243,8 +1252,10 @@ class GlesRawStacker(
val acceptance = resolveRegistrationAcceptance(estimate, seedTranslation)
currentRegistrationTransform = acceptance.transform
currentRegistrationSrWeight = acceptance.srWeight
recordRegistrationGlobalEstimate(globalEstimate)
recordRegistrationEstimate(estimate, currentRegistrationTransform)
if (hwmfDebug.collectMetrics) {
recordRegistrationGlobalEstimate(globalEstimate)
recordRegistrationEstimate(estimate, currentRegistrationTransform)
}
val accepted = acceptance.accepted
if (hwmfDebug.logCompactSummary) {
val candidateTranslation = registrationTranslation(estimate.candidateTransform)
@@ -2065,15 +2076,17 @@ class GlesRawStacker(
}
val fallbackReason = reasons.takeIf { it.isNotEmpty() }?.joinToString("+")
val mode = if (fallbackReason == null) "SUPER_RESOLUTION" else "BASE_UPSCALE"
val message = "HWMF MFSR output effective=$mode " +
"reason=${fallbackReason ?: "ok"} aligned=$alignedFrameCount " +
"srFrames=$superResolutionDetailFrameCount " +
"srWeightSum=${superResolutionDetailWeightSum.formatWeight()} " +
"srWeightMax=${superResolutionDetailWeightMax.formatWeight()}"
if (fallbackReason == null) {
PLog.d(TAG, message)
} else {
PLog.i(TAG, message)
if (RawStackRuntimeDebug.enabled) {
val message = "HWMF MFSR output effective=$mode " +
"reason=${fallbackReason ?: "ok"} aligned=$alignedFrameCount " +
"srFrames=$superResolutionDetailFrameCount " +
"srWeightSum=${superResolutionDetailWeightSum.formatWeight()} " +
"srWeightMax=${superResolutionDetailWeightMax.formatWeight()}"
if (fallbackReason == null) {
RawStackRuntimeDebug.d(TAG) { message }
} else {
RawStackRuntimeDebug.i(TAG) { message }
}
}
return SuperResolutionOutputDecision(
mode = mode,
@@ -2253,6 +2266,7 @@ class GlesRawStacker(
elapsedMs: Long,
superResolutionDecision: SuperResolutionOutputDecision = SuperResolutionOutputDecision.Disabled,
): RawStackDiagnostics? {
if (!hwmfDebug.collectMetrics) return null
val diagnosticAccumulatorTexture = if (superResolutionEnabled) {
currentSuperResolutionAccumulatorTexture
} else {
@@ -190,11 +190,11 @@ class GlesYuvStacker(
ensureGles31()
initPrograms()
initResources()
PLog.d(
TAG,
"GLES stack format=${formatName(inputFormat)} internal=${if (highPrecisionInput) "R16F/RG16F" else "R8/RG8"} " +
RawStackRuntimeDebug.d(TAG) {
"GLES stack format=${formatName(inputFormat)} " +
"internal=${if (highPrecisionInput) "R16F/RG16F" else "R8/RG8"} " +
"flowGrid=${FLOW_GRID_SPACING}px grid=${gridWidth}x${gridHeight}"
)
}
if (!uploadImagePlanes(images.first(), refY, refCbCr, refYStaging, refCbCrStaging, "reference")) {
return null
@@ -225,7 +225,9 @@ class GlesYuvStacker(
normalizeOutput()
GlesGpuScheduler.yieldToUiRenderer()
val bitmap = readOutputBitmap() ?: return null
PLog.i(TAG, "GLES YUV stacking completed in ${System.currentTimeMillis() - startTime}ms")
RawStackRuntimeDebug.i(TAG) {
"GLES YUV stacking completed in ${System.currentTimeMillis() - startTime}ms"
}
return bitmap
} catch (e: Exception) {
PLog.e(TAG, "GLES YUV stacking failed", e)
@@ -546,10 +548,10 @@ class GlesYuvStacker(
return false
}
if (label == "reference") {
PLog.d(
TAG,
"GLES plane upload yRow=${planes[0].rowStride} cbRow=${planes[1].rowStride} cbPixel=${planes[1].pixelStride} chroma=${chromaWidth}x${chromaHeight}"
)
RawStackRuntimeDebug.d(TAG) {
"GLES plane upload yRow=${planes[0].rowStride} cbRow=${planes[1].rowStride} " +
"cbPixel=${planes[1].pixelStride} chroma=${chromaWidth}x${chromaHeight}"
}
}
return if (highPrecisionInput) {
@@ -95,10 +95,9 @@ object MultiFrameStacker {
images.forEach { it.close() }
return null
}
PLog.i(
TAG,
RawStackRuntimeDebug.i(TAG) {
"Starting GLES streaming stacking process for ${images.size} frames ($width x $height)"
)
}
val glesBitmap = GlesYuvStacker(
width = width,
height = height,
@@ -118,10 +117,9 @@ object MultiFrameStacker {
}
// Fallback or legacy path
PLog.i(
TAG,
RawStackRuntimeDebug.i(TAG) {
"Starting legacy stacking process for ${images.size} frames ($width x $height). SR=$enableSuperResolution"
)
}
val stackerPtr = createStackerNative(width, height, enableSuperResolution)
if (stackerPtr == 0L) return null
@@ -160,7 +158,9 @@ object MultiFrameStacker {
aspectRatio?.heightRatio ?: height
)
PLog.i(TAG, "Legacy stacking completed in ${System.currentTimeMillis() - startTime}ms")
RawStackRuntimeDebug.i(TAG) {
"Legacy stacking completed in ${System.currentTimeMillis() - startTime}ms"
}
return previewBitmap
} finally {
releaseStackerNative(stackerPtr)
@@ -244,15 +244,14 @@ object MultiFrameStacker {
}
val width = shortFrame.image.width
val height = shortFrame.image.height
PLog.d(
TAG,
RawStackRuntimeDebug.d(TAG) {
"Starting RAW HDR stacking for short+${normalFrames.size} normal frames. " +
"Pattern=$cfaPattern GPU=$useGpuAcceleration BL=${masterBlackLevel.joinToString()} WL=$whiteLevel"
)
}
if (!useGpuAcceleration) {
PLog.w(TAG, "RAW HDR denoise stack requires GLES; GPU acceleration setting is ignored")
}
PLog.i(TAG, "Using GLES RAW HDR stacker")
RawStackRuntimeDebug.i(TAG) { "Using GLES RAW HDR stacker" }
val tuning = RawStackTuningResolver.resolve(
mode = RawStackMode.HDR_MFNR,
frameCount = normalFrames.size + 1,
@@ -277,7 +276,7 @@ object MultiFrameStacker {
colorCorrectionMatrix = colorCorrectionMatrix,
pgtmStatsBounds = pgtmStatsBounds,
tuning = tuning,
debugConfig = RawStackDebugConfig.CompactSummary,
debugConfig = RawStackRuntimeDebug.debugConfig,
).processHdr(
shortFrame = GlesRawStacker.HdrInputFrame(shortFrame.image, shortFrame.exposureProduct),
normalFrames = normalFrames.map { GlesRawStacker.HdrInputFrame(it.image, it.exposureProduct) },
@@ -304,16 +303,17 @@ object MultiFrameStacker {
val width = images[0].width
val height = images[0].height
PLog.d(
TAG,
"Starting RAW stacking for ${images.size} frames. Pattern=$cfaPattern SR=$enableSuperResolution scale=$superResolutionScale GPU=$useGpuAcceleration BL=${masterBlackLevel.joinToString()} WL=$whiteLevel"
)
RawStackRuntimeDebug.d(TAG) {
"Starting RAW stacking for ${images.size} frames. Pattern=$cfaPattern " +
"SR=$enableSuperResolution scale=$superResolutionScale GPU=$useGpuAcceleration " +
"BL=${masterBlackLevel.joinToString()} WL=$whiteLevel"
}
val outputScale = if (enableSuperResolution) superResolutionScale.coerceIn(1.0f, 2.0f) else 1.0f
val useNativeSuperResolution = outputScale > 1.0f
if (useGpuAcceleration) {
val mode = if (enableSuperResolution) RawStackMode.MFSR else RawStackMode.MFNR
PLog.i(TAG, "Using GLES RAW stacker mode=$mode")
RawStackRuntimeDebug.i(TAG) { "Using GLES RAW stacker mode=$mode" }
val stackLensShading = validLensShadingOrNull(
lensShading = lensShading,
width = lensShadingWidth,
@@ -337,11 +337,11 @@ object MultiFrameStacker {
lensShadingWidth = if (stackLensShading != null) lensShadingWidth else 0,
lensShadingHeight = if (stackLensShading != null) lensShadingHeight else 0,
tuning = tuning,
debugConfig = RawStackDebugConfig.CompactSummary,
debugConfig = RawStackRuntimeDebug.debugConfig,
).process(images)
}
PLog.i(TAG, "Using CPU RAW stacker")
RawStackRuntimeDebug.i(TAG) { "Using CPU RAW stacker" }
val stackerPtr = createRawStackerNative(width, height, useNativeSuperResolution)
if (stackerPtr == 0L) {
PLog.e(TAG, "Failed to create CPU raw stacker")
@@ -373,7 +373,7 @@ object MultiFrameStacker {
processRawStackWithBufferNative(stackerPtr, cpuFusedBayerBuffer)
cpuFusedBayerBuffer.rewind()
PLog.i(TAG, "CPU RAW stacking completed successfully")
RawStackRuntimeDebug.i(TAG) { "CPU RAW stacking completed successfully" }
returnsCpuFusedBayer = true
return RawStackResult(
fusedBayerBuffer = cpuFusedBayerBuffer,
@@ -1,5 +1,7 @@
package com.hinnka.mycamera.processor
import com.hinnka.mycamera.BuildConfig
data class RawStackDebugConfig(
val collectMetrics: Boolean = false,
val logCompactSummary: Boolean = false,
@@ -17,6 +19,7 @@ data class RawStackDebugConfig(
get() = collectMetrics || logCompactSummary
fun normalized(): RawStackDebugConfig {
if (!BuildConfig.DEBUG) return Disabled
return copy(
collectMetrics = needsMetrics,
sampleStep = sampleStep.coerceAtLeast(1),
@@ -37,5 +40,9 @@ data class RawStackDebugConfig(
collectMetrics = true,
logCompactSummary = true,
)
fun forCurrentBuild(): RawStackDebugConfig {
return if (BuildConfig.DEBUG) CompactSummary else Disabled
}
}
}
@@ -0,0 +1,24 @@
package com.hinnka.mycamera.processor
import com.hinnka.mycamera.BuildConfig
import com.hinnka.mycamera.utils.PLog
internal object RawStackRuntimeDebug {
val enabled: Boolean
get() = BuildConfig.DEBUG
val debugConfig: RawStackDebugConfig
get() = RawStackDebugConfig.forCurrentBuild()
inline fun d(tag: String, message: () -> String) {
if (enabled) {
PLog.d(tag, message())
}
}
inline fun i(tag: String, message: () -> String) {
if (enabled) {
PLog.i(tag, message())
}
}
}