fix: restore LUT state after app resume

This commit is contained in:
zhoubinjia committed 2026-04-10 11:09:11 +08:00
1 parent 5c34e3a535
commit dfe2fca9bd
4 files changed
+86

No files matched your search

@@ -1770,6 +1770,30 @@ class LutRenderer : GLSurfaceView.Renderer {
}
}
/**
* App 从后台恢复时,GLSurfaceView 可能保留了 Kotlin 层的 LUT 选择状态,
* 但底层 GL texture 已随 Surface/Context 生命周期失效。按当前配置强制重建,
* 避免 UI 仍显示已选 LUT 而预览实际走无 LUT 路径。
*/
fun restoreLutTexturesAfterResume() {
if (!surfaceReady) {
pendingLutConfig = currentLutConfig
pendingBaselineLutConfig = currentBaselineLutConfig
PLog.d(TAG, "restore LUT deferred: surface not ready")
return
}
currentBaselineLutConfig?.let { config ->
PLog.d(TAG, "restore baseline LUT texture after resume: ${config.title}")
setBaselineLutInternal(config)
}
currentLutConfig?.let { config ->
PLog.d(TAG, "restore LUT texture after resume: ${config.title}")
setLutInternal(config)
}
}
/**
* 设置预览尺寸
*/
@@ -282,6 +282,13 @@ class CameraGLSurfaceView @JvmOverloads constructor(
PLog.d(TAG, "onResume")
}
fun restoreRenderStateAfterResume() {
queueEvent {
renderer.restoreLutTexturesAfterResume()
requestRender()
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
PLog.d(TAG, "onDetachedFromWindow")
@@ -14,6 +14,9 @@ import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.viewinterop.AndroidView
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.LocalLifecycleOwner
import com.hinnka.mycamera.livephoto.LivePhotoRecorder
import com.hinnka.mycamera.lut.LutConfig
import com.hinnka.mycamera.model.ColorRecipeParams
@@ -55,6 +58,26 @@ fun CameraPreviewGL(
modifier: Modifier = Modifier
) {
val rotationDegrees = OrientationObserver.rotationDegrees
val lifecycleOwner = LocalLifecycleOwner.current
var glSurfaceViewRef by remember { mutableStateOf<CameraGLSurfaceView?>(null) }
DisposableEffect(lifecycleOwner) {
val observer = LifecycleEventObserver { _, event ->
when (event) {
Lifecycle.Event.ON_PAUSE -> glSurfaceViewRef?.onPause()
Lifecycle.Event.ON_RESUME -> {
glSurfaceViewRef?.onResume()
glSurfaceViewRef?.restoreRenderStateAfterResume()
}
else -> Unit
}
}
lifecycleOwner.lifecycle.addObserver(observer)
onDispose {
lifecycleOwner.lifecycle.removeObserver(observer)
}
}
// 计算预览区域尺寸,保持目标比例
BoxWithConstraints(
modifier = modifier
@@ -111,6 +134,7 @@ fun CameraPreviewGL(
AndroidView(
factory = { ctx ->
CameraGLSurfaceView(ctx).apply {
glSurfaceViewRef = this
// 通知 GLSurfaceView 已准备好
onGLSurfaceViewReady?.invoke(this)
}
@@ -133,6 +133,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
initialValue = ColorRecipeParams.DEFAULT
)
@OptIn(ExperimentalCoroutinesApi::class)
val currentBaselineRecipeParams: StateFlow<ColorRecipeParams> =
userPreferencesRepository.userPreferences.flatMapLatest { prefs ->
val target = if (prefs.useRaw) {
@@ -688,6 +689,36 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
cameraController.openCamera(texture)
}
}
restorePreviewLutAfterResume()
}
private fun restorePreviewLutAfterResume() {
val lutId = currentLutId.value
PLog.d(TAG, "restorePreviewLutAfterResume: lutId=$lutId")
viewModelScope.launch {
val loadedLut = withContext(Dispatchers.IO) {
contentRepository.lutManager.loadLut(lutId)
}
currentLutConfig = loadedLut
cameraController.setLutEnabled(loadedLut != null)
glSurfaceView?.let { view ->
val currentState = state.value
view.setBaselineLut(currentBaselineLutConfig)
view.setBaselineLutEnabled(currentBaselineLutConfig != null)
view.setBaselineParams(currentBaselineRecipeParams.value)
view.setLut(loadedLut)
view.setLutEnabled(loadedLut != null)
view.setParams(currentRecipeParams.value, if (currentState.isVirtualApertureEnabled) {
currentState.virtualAperture
} else {
0f
})
view.setColorRecipeEnabled(!currentRecipeParams.value.isDefault())
view.setVideoRecorder(cameraController.videoRecorder)
view.setVideoLogProfile(currentState.videoConfig.logProfile)
view.restoreRenderStateAfterResume()
}
}
}
private suspend fun buildPhotoMetadata(