Modern Apps git

← Modern-Apps.git

camera: fake viewfinder scene for screenshot captures

Commit
3ebeadad3ee40f94a4976e1814f48310eeba91c9
Author
Vayun Mathur <[email protected]>
When
2026-11-25 21:58 UTC (10 days ago)
Parents
7902af8
Layoutlib cannot open a camera, so src/screenshotTest now draws a deterministic FakeCameraPreview scene (fixed canvas fractions, no bitmaps/randomness/time) behind the real top bar, grid, shutter, and mode controls. The production seam is the screenshotFakePreview parameter on CameraScreen/CameraPreviewSurface, defaulting to null and never passed by MainActivity, so no fake frame can appear on device.

New phone capture and regenerated large-tablet capture under metadata_data/photos/camera/. Verified: :camera:compileDevKotlin and :camera:metadata pass.

Diff

camera/AGENTS.md
diff --git a/camera/AGENTS.md b/camera/AGENTS.mdindex 8fd4c2f9f2..2d0e51723f 100644--- a/camera/AGENTS.md+++ b/camera/AGENTS.md@@ -8,7 +8,7 @@ _Install: ./install camera (dev by default)._ - Entry files: MainActivity.kt, Route.kt (`Route.Camera` / `Route.Settings`) - Deps: :library:ml - Metadata: metadata_data/camera.md (present)-- Rust: yes / screenshotTest: hybrid (phone hand-captured; wide controls preview-rendered)+- Rust: yes / screenshotTest: yes (phone + large-tablet preview-rendered over a screenshot-only fake viewfinder scene)  ## Overview **Purpose:** The most featureful open-source camera app — usable as the system camera and from the lockscreen. 100% offline.@@ -21,7 +21,8 @@ _Install: ./install camera (dev by default)._ **Entry points + reading order:** `MainActivity.kt` → `Route.kt` (`Route.Camera`/`Settings`) → `ui/CameraScreen.kt` → `platform/CameraViewModel.kt` → `platform/CameraSessions.kt` → `platform/PanoramaEngine.kt` / `NightCaptureEngine.kt`. **Gotchas:** - Capture code lives in `platform/` (the Camera2 driver in `platform/camera2/`); there is no `util/` root in this module-- Screenshots are hand-captured (no metadata convention plugin — Layoutlib can't open a camera); committed listing images live under `metadata_data/photos/camera/phone/`+- Camera2 session reopen after a surface destroy (background/resume, size change) is serialized in `platform/camera2/`: a session parks a reopen until the previous device's real `onClosed` (device `close()` only initiates HAL teardown), and `PreviewSurfaceBridge` forgets its pinned stream size on destroy so a new `SurfaceTexture` is re-pinned before configure+- Listing screenshots are preview-rendered (`:camera:metadata`): Layoutlib can't open a camera, so `src/screenshotTest` draws the deterministic `FakeCameraPreview` scene behind the real top bar/grid/shutter/mode controls. The `screenshotFakePreview` parameter on `CameraScreen`/`CameraPreviewSurface` is the same seam in `main` — production-default null, never passed by `MainActivity`, so no fake frame can appear on device - Volume keys act as the shutter, gated on the camera route being foreground; Esc/space covers desktop keyboards - `.maml` model assets ship uncompressed (`noCompress`) — never compress them @@ -92,6 +93,7 @@ _Install: ./install camera (dev by default)._ ### platform/camera2/ — Camera2 driver - com/vayunmathur/camera/platform/camera2/Camera2Driver.kt - com/vayunmathur/camera/platform/camera2/Camera2Session.kt+- com/vayunmathur/camera/platform/camera2/Camera2SessionRingCapture.kt - com/vayunmathur/camera/platform/camera2/Camera2ExtensionSession.kt - com/vayunmathur/camera/platform/camera2/Camera2HighSpeedSession.kt - com/vayunmathur/camera/platform/camera2/StillCapture.kt
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.ktindex be4ae157d1..b316aabd95 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewBox.kt@@ -74,6 +74,9 @@ internal fun BoxWithConstraintsScope.CameraPreviewBox(     state: CameraScreenState,     viewModel: CameraViewModel,     animatedRotation: Float,+    // Screenshot/preview fake provider only; null (production default) keeps+    // the live TextureView surfaces. See CameraPreviewSurface.+    screenshotFakePreview: (@Composable (Modifier) -> Unit)? = null, ) {     val cameraMode = state.cameraMode     val maskBitmap = state.maskBitmap@@ -185,6 +188,7 @@ internal fun BoxWithConstraintsScope.CameraPreviewBox(         state = state,         viewModel = viewModel,         previewModifier = previewModifier,+        screenshotFakePreview = screenshotFakePreview,     )      CaptureFlashOverlay(viewModel.shutterFeedback, previewSize.clip(RoundedCornerShape(12.dp)))
camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewSurface.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewSurface.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewSurface.ktindex 55173a3eb3..27c0e06003 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewSurface.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraPreviewSurface.kt@@ -32,13 +32,24 @@ import com.vayunmathur.library.ui.R as UiR  * Camera2 engine serves, otherwise the CameraX viewfinder (or the  * bind-failed / black fallback box). Extracted verbatim from  * [CameraPreviewBox]; overlays, effects and gestures stay with the caller.+ *+ * [screenshotFakePreview] is the screenshot/preview fake-provider seam: when+ * non-null it is drawn instead of every live surface and no Camera2/CameraX+ * session is touched. It defaults to null and no production caller passes it+ * — only `src/screenshotTest` fixtures (and Studio previews) may supply a+ * deterministic fake scene, so a fake frame can never appear on device.  */ @Composable internal fun CameraPreviewSurface(     state: CameraScreenState,     viewModel: CameraViewModel,     previewModifier: Modifier,+    screenshotFakePreview: (@Composable (Modifier) -> Unit)? = null, ) {+    if (screenshotFakePreview != null) {+        screenshotFakePreview(previewModifier)+        return+    }     val useCamera2Preview = state.useCamera2Preview     val useCamera2NightPreview = state.useCamera2NightPreview     val useCamera2SloMoPreview = state.useCamera2SloMoPreview
camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt
diff --git a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.ktindex d10757db3b..e50983bcbf 100644--- a/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt+++ b/camera/src/main/java/com/vayunmathur/camera/ui/CameraScreen.kt@@ -1,6 +1,3 @@-// RAW ANIMATION EXCEPTION: the panorama capture flash is imperative - an Animatable snapped to-// white as each guide dot is captured and released back to zero, with no declarative target to-// animate towards; the shutter and record-button springs are tuned to this viewfinder's own feel. package com.vayunmathur.camera.ui  import android.graphics.Bitmap@@ -39,8 +36,8 @@ import com.vayunmathur.camera.data.toggleLevel import com.vayunmathur.camera.platform.toggleMicMuted import com.vayunmathur.camera.platform.toggleRecording import com.vayunmathur.camera.data.toggleTorch+import com.vayunmathur.library.ui.AppScaffold import com.vayunmathur.library.ui.Motion-import com.vayunmathur.library.ui.Scaffold import com.vayunmathur.library.ui.animatedFloat import com.vayunmathur.library.util.NavBackStack @@ -48,7 +45,12 @@ import com.vayunmathur.library.util.NavBackStack fun CameraScreen(     backStack: NavBackStack<Route>,     viewModel: CameraViewModel,-    onCaptureResult: ((Bitmap?) -> Unit)? = null+    onCaptureResult: ((Bitmap?) -> Unit)? = null,+    // Screenshot/preview fake viewfinder provider only. Production default is+    // null and MainActivity never passes it, so on device the live camera+    // surfaces always serve; screenshotTest fixtures pass a deterministic+    // fake scene instead of rendering a black TextureView pane.+    screenshotFakePreview: (@Composable (Modifier) -> Unit)? = null, ) {     val view = LocalView.current     val state = rememberCameraScreenState(viewModel)@@ -92,12 +94,8 @@ fun CameraScreen(         spec = Motion.over(300)     ) -    // RAW SCAFFOLD EXCEPTION: full-bleed viewfinder with a black surface and its own custom-    // top/bottom control bars (TopBar/ShutterRow/ModeSelector/BottomBar). It has no Material-    // top app bar or title, so AppScaffold would inject an unwanted bar, and TopAppBarOverlay-    // provides neither the black container surface nor the system-inset padding the preview-    // relies on. The bare Scaffold(containerColor) is the correct primitive here.-    Scaffold(+    AppScaffold(+        topBar = {},         containerColor = Color.Black     ) { padding ->         BoxWithConstraints(modifier = Modifier.fillMaxSize().padding(padding)) {@@ -161,6 +159,7 @@ fun CameraScreen(                         state = state,                         viewModel = viewModel,                         animatedRotation = animatedRotation,+                        screenshotFakePreview = screenshotFakePreview,                     )                 } 
camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/FakeCameraPreview.kt
diff --git a/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/FakeCameraPreview.kt b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/FakeCameraPreview.ktnew file mode 100644index 0000000000..fefc456e14--- /dev/null+++ b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/FakeCameraPreview.kt@@ -0,0 +1,114 @@+package com.vayunmathur.camera.ui++import androidx.compose.foundation.Canvas+import androidx.compose.runtime.Composable+import androidx.compose.ui.Modifier+import androidx.compose.ui.geometry.Offset+import androidx.compose.ui.geometry.Size+import androidx.compose.ui.graphics.Brush+import androidx.compose.ui.graphics.Color+import androidx.compose.ui.graphics.Path++/**+ * SCREENSHOT-ONLY fake camera viewfinder scene.+ *+ * This file lives in `src/screenshotTest`, so it is never compiled into the+ * app APK. It is the provider passed to the `screenshotFakePreview` seam of+ * `CameraScreen` / `CameraPreviewSurface` (production default: null, and no+ * production caller passes it). Layoutlib cannot open a camera or bind a+ * TextureView, so without this the listing captures show a black pane; with+ * it they show a representative scene behind the REAL top bar, grid overlay,+ * shutter and mode controls.+ *+ * The scene is a deliberately synthetic, deterministic landscape drawn from+ * fixed fractions of the canvas size — no bitmaps, no randomness, no time —+ * so every render is byte-stable. It does not pretend to be a photograph+ * taken by the camera: flat shapes and a gradient sky, in the same spirit as+ * the gradient tiles `:photos` uses for its thumbnails.+ */+@Composable+internal fun FakeCameraPreview(modifier: Modifier = Modifier) {+    Canvas(modifier) {+        val w = size.width+        val h = size.height++        // Sky: fixed vertical gradient, warm horizon to blue zenith.+        drawRect(+            brush = Brush.verticalGradient(+                colors = listOf(+                    Color(0xFF7FB2E0),+                    Color(0xFFBFD9EE),+                    Color(0xFFF6D9A8),+                ),+                startY = 0f,+                endY = h * 0.62f,+            ),+            size = Size(w, h * 0.62f),+        )+        // Sun, upper right.+        drawCircle(Color(0xFFFFE9A8), radius = w * 0.07f, center = Offset(w * 0.78f, h * 0.18f))+        drawCircle(Color(0xFFFFF6D8), radius = w * 0.045f, center = Offset(w * 0.78f, h * 0.18f))++        // Distant mountains (fixed ridge line).+        val ridge = Path().apply {+            moveTo(0f, h * 0.52f)+            lineTo(w * 0.18f, h * 0.34f)+            lineTo(w * 0.34f, h * 0.46f)+            lineTo(w * 0.52f, h * 0.30f)+            lineTo(w * 0.68f, h * 0.44f)+            lineTo(w * 0.84f, h * 0.36f)+            lineTo(w, h * 0.48f)+            lineTo(w, h * 0.62f)+            lineTo(0f, h * 0.62f)+            close()+        }+        drawPath(ridge, Color(0xFF6E8FB4))++        // Lake / meadow band.+        drawRect(+            brush = Brush.verticalGradient(+                colors = listOf(Color(0xFF9CC3D6), Color(0xFF7FAE7A), Color(0xFF5E9455)),+                startY = h * 0.58f,+                endY = h,+            ),+            topLeft = Offset(0f, h * 0.58f),+            size = Size(w, h * 0.42f),+        )++        // Foreground hill silhouettes.+        val hill = Path().apply {+            moveTo(0f, h * 0.78f)+            quadraticTo(w * 0.30f, h * 0.66f, w * 0.58f, h * 0.78f)+            quadraticTo(w * 0.82f, h * 0.88f, w, h * 0.80f)+            lineTo(w, h)+            lineTo(0f, h)+            close()+        }+        drawPath(hill, Color(0xFF3E6B35))++        // Fixed trees: trunk + canopy at deterministic positions.+        val treeXs = listOf(0.10f, 0.24f, 0.62f, 0.88f)+        treeXs.forEachIndexed { index, fx ->+            val baseY = h * (0.84f + (index % 2) * 0.05f)+            val treeH = h * (0.13f + (index % 3) * 0.02f)+            drawRect(+                Color(0xFF4A3421),+                topLeft = Offset(w * fx - w * 0.008f, baseY - treeH * 0.35f),+                size = Size(w * 0.016f, treeH * 0.35f),+            )+            drawCircle(+                Color(0xFF2F5D2A),+                radius = treeH * 0.38f,+                center = Offset(w * fx, baseY - treeH * 0.62f),+            )+        }++        // Fixed foreground dots (wildflowers), positions from a fixed table.+        val flowerColors = listOf(Color(0xFFFFE082), Color(0xFFF8BBD0), Color.White)+        for (i in 0 until 18) {+            val fx = ((i * 37) % 100) / 100f+            val fy = 0.86f + ((i * 53) % 12) / 100f+            drawCircle(flowerColors[i % flowerColors.size], radius = w * 0.006f + 2f, center = Offset(w * fx, h * fy))+        }+    }+}
camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/MetadataPreviews.kt
diff --git a/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/MetadataPreviews.kt b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/MetadataPreviews.ktindex d64c13bfbd..afe1a2e3e0 100644--- a/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/MetadataPreviews.kt+++ b/camera/src/screenshotTest/kotlin/com/vayunmathur/camera/ui/MetadataPreviews.kt@@ -10,7 +10,10 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import com.android.tools.screenshot.PreviewTest+import com.vayunmathur.camera.platform.AspectRatioOption import com.vayunmathur.camera.platform.CameraMode+import com.vayunmathur.camera.platform.FlashMode+import com.vayunmathur.camera.platform.TimerDuration import com.vayunmathur.library.ui.DynamicTheme  /** Phone-shaped, roughly 1080x2340 at xxhdpi — comfortably above the F-Droid minimum. */@@ -20,23 +23,24 @@ private const val PHONE = "spec:width=411dp,height=891dp,dpi=420" private const val EXPANDED = "spec:width=1280dp,height=800dp,dpi=240"  /**- * Listing-image renders for `:camera`'s *wide* layout, from Compose previews.+ * Listing-image renders for `:camera`, from Compose previews.  *- * Camera stays a hybrid: Layoutlib cannot open a camera, so the live viewfinder can- * never be preview-rendered (with no SurfaceRequest it is a black box). The phone- * listing images are therefore still hand-captured on a real device into- * `metadata_data/photos/camera/phone/`. What a preview *can* render honestly is the- * part that actually changes with width — the wide controls layout- * ([CameraWideLayout] with the real [CameraShutterPane] and [CameraModesPane]) —- * so this file captures exactly that at expanded width, collected into- * `metadata_data/photos/camera/large-tablet/`. The dark pane above the controls is- * the viewfinder's placeholder, standing where the live preview sits on device;- * replace the collected PNG with a hand capture from a real tablet before a store- * submission, exactly as the phone images are produced.+ * Layoutlib cannot open a camera, so the live viewfinder can never be+ * preview-rendered (with no SurfaceRequest it is a black box). Instead of a+ * black/empty pane, the captures below draw [FakeCameraPreview] — a+ * deterministic, synthetic screenshot-only scene (see that file; it lives in+ * `src/screenshotTest` and never ships) — behind the REAL controls:+ * [CameraTopBar], the real grid overlay, [CameraShutterPane] and+ * [CameraModesPane]. This is the fixture-side use of the same provider shape+ * as the `screenshotFakePreview` parameter on `CameraScreen` /+ * `CameraPreviewSurface`, whose production default is null and which no+ * production caller passes; the fixtures compose the panes directly because+ * `CameraScreen` needs a `CameraViewModel` that cannot run in Layoutlib.+ * The fake scene is representative viewfinder content, not a claim that a+ * real camera frame rendered here.  *- * `./gradlew :camera:metadata` renders this preview and collects it. The collect- * task only replaces buckets a run actually rendered, so the hand-captured `phone/`- * bucket survives it.+ * `./gradlew :camera:metadata` renders these previews and collects them into+ * `metadata_data/photos/camera/phone/` and `.../large-tablet/`.  *  * Rules (same as every other module's MetadataPreviews): order comes from the  * function names, each preview needs `@PreviewTest` plus a `group`, and previews@@ -50,45 +54,97 @@ class MetadataPreviews {     fun Preview1WideControlsExpanded() {         DynamicTheme(darkTheme = true) {             Column(Modifier.fillMaxSize().background(Color.Black)) {-                // Viewfinder placeholder: the live preview pane (unrenderable in-                // Layoutlib) above the wide controls.-                Box(Modifier.fillMaxWidth().weight(1f))+                // Fake viewfinder scene (screenshot-only) above the wide controls,+                // with the real grid overlay the live preview would show.+                Box(Modifier.fillMaxWidth().weight(1f)) {+                    FakeCameraPreview(Modifier.fillMaxSize())+                    GridOverlay(Modifier.fillMaxSize())+                }                 CameraWideLayout(-                    shutter = {-                        CameraShutterPane(-                            cameraMode = CameraMode.PHOTO,-                            isRecording = false,-                            isCapturing = false,-                            recordingPaused = false,-                            videoSnapshotSupported = false,-                            galleryBitmap = null,-                            onCaptureResult = null,-                            onBurstStart = {},-                            onBurstStop = {},-                            onCapture = {},-                            onPauseResume = {},-                            onSnapshot = {},-                            onFlipCamera = {},-                            onGallery = {},-                            iconRotation = 0f,-                        )-                    },-                    modes = {-                        CameraModesPane(-                            cameraMode = CameraMode.PHOTO,-                            isPhotoType = true,-                            sloMoSupported = true,-                            captureForResult = false,-                            onModeSelected = {},-                            iconRotation = 0f,-                            onPickerChanged = {},-                            onSettingsClick = {},-                        )-                    },+                    shutter = { FixtureShutterPane() },+                    modes = { FixtureModesPane() },                     modifier = Modifier.fillMaxWidth(),                     sideBySide = true,                 )             }         }     }++    @PreviewTest+    @Preview(name = "2-camera", device = PHONE, group = "phone", showSystemUi = true)+    @Composable+    fun Preview2CameraPhone() {+        DynamicTheme(darkTheme = true) {+            // Mirrors CameraScreen's phone structure: top bar, viewfinder,+            // then shutter + modes stacked below it.+            Column(Modifier.fillMaxSize().background(Color.Black)) {+                CameraTopBar(+                    flashMode = FlashMode.OFF,+                    torchEnabled = false,+                    hasFlashUnit = true,+                    gridEnabled = true,+                    levelEnabled = false,+                    aspectRatio = AspectRatioOption.RATIO_4_3,+                    stillCropEnabled = true,+                    isPhotoType = true,+                    isVideoType = false,+                    micMuted = false,+                    timerDuration = TimerDuration.NONE,+                    onFlashToggle = {},+                    onTorchToggle = {},+                    onGridToggle = {},+                    onLevelToggle = {},+                    onAspectCycle = {},+                    onMicToggle = {},+                    onTimerCycle = {},+                    iconRotation = 0f,+                )+                Box(Modifier.fillMaxWidth().weight(1f)) {+                    FakeCameraPreview(Modifier.fillMaxSize())+                    GridOverlay(Modifier.fillMaxSize())+                }+                CameraWideLayout(+                    shutter = { FixtureShutterPane() },+                    modes = { FixtureModesPane() },+                    modifier = Modifier.fillMaxWidth(),+                    sideBySide = false,+                )+            }+        }+    }+}++@Composable+private fun FixtureShutterPane() {+    CameraShutterPane(+        cameraMode = CameraMode.PHOTO,+        isRecording = false,+        isCapturing = false,+        recordingPaused = false,+        videoSnapshotSupported = false,+        galleryBitmap = null,+        onCaptureResult = null,+        onBurstStart = {},+        onBurstStop = {},+        onCapture = {},+        onPauseResume = {},+        onSnapshot = {},+        onFlipCamera = {},+        onGallery = {},+        iconRotation = 0f,+    )+}++@Composable+private fun FixtureModesPane() {+    CameraModesPane(+        cameraMode = CameraMode.PHOTO,+        isPhotoType = true,+        sloMoSupported = true,+        captureForResult = false,+        onModeSelected = {},+        iconRotation = 0f,+        onPickerChanged = {},+        onSettingsClick = {},+    ) }
metadata_data/photos/camera/large-tablet/1.png
diff --git a/metadata_data/photos/camera/large-tablet/1.png b/metadata_data/photos/camera/large-tablet/1.pngindex 018d143ead..43de30f7b9 100644Binary files a/metadata_data/photos/camera/large-tablet/1.png and b/metadata_data/photos/camera/large-tablet/1.png differ
metadata_data/photos/camera/phone/1.png
diff --git a/metadata_data/photos/camera/phone/1.png b/metadata_data/photos/camera/phone/1.pngnew file mode 100644index 0000000000..63b019d75bBinary files /dev/null and b/metadata_data/photos/camera/phone/1.png differ