From a1b5abf3ea4457066da86b1dbb906f6792590a25 Mon Sep 17 00:00:00 2001 From: temcguir Date: Tue, 10 Nov 2026 00:02:14 +0000 Subject: [PATCH 1/3] Read the zoom range once per bound camera instead of on every frame The preview capture callback read CameraInfo.zoomState on every capture result to clamp the target zoom ratio. For a camera extension session, zoomState queries the extension's characteristics through the vendor extension service on each call. At preview frame rate this saturates the CameraX camera thread, which also completes still captures and opens and closes the camera, so captures stall and the camera takes tens of seconds to close and reopen. The zoom range doesn't change while a camera is bound, so read it once and reuse it. --- .../jetpackcamera/core/camera/CameraSession.kt | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt index 044b6bbfc..26d6275ef 100644 --- a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt +++ b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt @@ -1515,6 +1515,12 @@ private fun Preview.Builder.updateCameraStateWithCaptureResults( ): Preview.Builder { val isFirstFrameTimestampUpdated = atomic(false) val targetCameraLogicalId = Camera2CameraInfo.from(targetCameraInfo).cameraId + // The zoom range doesn't change while this camera is bound, so it is read once instead of + // on every frame. For a camera extension session, reading zoomState queries the extension's + // characteristics through the vendor extension service, which is too slow to do for each + // capture result and backs up the camera thread that also completes captures and opens and + // closes the camera. + var zoomRange: ClosedFloatingPointRange? = null Camera2Interop.Extender(this).setSessionCaptureCallback( object : CameraCaptureSession.CaptureCallback() { override fun onCaptureCompleted( @@ -1558,10 +1564,11 @@ private fun Preview.Builder.updateCameraStateWithCaptureResults( val targetZoomRatio = transientSettings.value?.zoomRatios?.get(targetCameraInfo.appLensFacing) ?: 1.0f - val zoomState = targetCameraInfo.zoomState.value - val minZoom = zoomState?.minZoomRatio ?: 1.0f - val maxZoom = zoomState?.maxZoomRatio ?: 1.0f - val clampedTarget = targetZoomRatio.coerceIn(minZoom, maxZoom) + val range = zoomRange + ?: targetCameraInfo.zoomState.value + ?.let { it.minZoomRatio..it.maxZoomRatio } + ?.also { zoomRange = it } + val clampedTarget = targetZoomRatio.coerceIn(range ?: 1.0f..1.0f) val isTargetEssentiallyOne = abs(clampedTarget - 1.0f) < 0.01f currentCameraState.update { old -> val currentZoomRatio = old.zoomRatios[targetCameraInfo.appLensFacing] From d9f9deca3f5859716a9cb14538f3d965e9334d19 Mon Sep 17 00:00:00 2001 From: temcguir Date: Tue, 10 Nov 2026 00:05:58 +0000 Subject: [PATCH 2/3] Avoid allocating a range when the zoom range is not yet known --- .../java/com/google/jetpackcamera/core/camera/CameraSession.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt index 26d6275ef..1fcaa4df3 100644 --- a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt +++ b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt @@ -1568,7 +1568,7 @@ private fun Preview.Builder.updateCameraStateWithCaptureResults( ?: targetCameraInfo.zoomState.value ?.let { it.minZoomRatio..it.maxZoomRatio } ?.also { zoomRange = it } - val clampedTarget = targetZoomRatio.coerceIn(range ?: 1.0f..1.0f) + val clampedTarget = range?.let { targetZoomRatio.coerceIn(it) } ?: 1.0f val isTargetEssentiallyOne = abs(clampedTarget - 1.0f) < 0.01f currentCameraState.update { old -> val currentZoomRatio = old.zoomRatios[targetCameraInfo.appLensFacing] From ab972186552ca2ff3bb2a0193c537efa76f9052c Mon Sep 17 00:00:00 2001 From: temcguir Date: Tue, 10 Nov 2026 00:10:38 +0000 Subject: [PATCH 3/3] Shorten the zoom range comment --- .../com/google/jetpackcamera/core/camera/CameraSession.kt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt index 1fcaa4df3..55465e4cd 100644 --- a/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt +++ b/core/camera/src/main/java/com/google/jetpackcamera/core/camera/CameraSession.kt @@ -1515,11 +1515,7 @@ private fun Preview.Builder.updateCameraStateWithCaptureResults( ): Preview.Builder { val isFirstFrameTimestampUpdated = atomic(false) val targetCameraLogicalId = Camera2CameraInfo.from(targetCameraInfo).cameraId - // The zoom range doesn't change while this camera is bound, so it is read once instead of - // on every frame. For a camera extension session, reading zoomState queries the extension's - // characteristics through the vendor extension service, which is too slow to do for each - // capture result and backs up the camera thread that also completes captures and opens and - // closes the camera. + // Read once per bound camera, since reading zoomState can be expensive for camera extensions. var zoomRange: ClosedFloatingPointRange? = null Camera2Interop.Extender(this).setSessionCaptureCallback( object : CameraCaptureSession.CaptureCallback() {