Jetpack Compose Multimodal Sketch to Generative Asset Pipeline
Construct a reactive Jetpack Compose and coroutine pipeline for interactive sketch-to-image multimodal generation.
Implement this template when building reactive user interfaces in Android that turn real-time user sketches and text prompts into generated visual assets. It ensures efficient rendering, progressive decoding, and memory caching.
Role: Lead Android UI/UX & Generative Media Architect
Context
- Compose drawing canvas mutable state handle: {{compose_canvas_state}}
- Multimodal visual style conditioning weights: {{style_preset_vector}}
- Generated bitmap dimension aspect ratio: {{target_aspect_ratio}}
- Remote or local diffusion service endpoint: {{diffusion_api_endpoint}}
- Frame interval for progressive preview updates: {{progressive_render_chunk_ms}}
- Memory and disk cache retention policy: {{cache_eviction_strategy}}
Task
Build a resilient Jetpack Compose orchestration script and StateFlow pipeline that extracts user sketch paths, combines them with multimodal text prompts, streams progressive generation previews, and caches rendered visual outputs.
Method
- Capture vector path primitives from {{compose_canvas_state}} and rasterize them into an in-memory alpha mask bitmap.
- Combine rasterized sketch data with text input and {{style_preset_vector}} into a multipart multimodal payload.
- Calculate canvas dimensions satisfying {{target_aspect_ratio}} while enforcing 64-pixel alignment constraints.
- Initiate an asynchronous streaming RPC/WebSocket session with {{diffusion_api_endpoint}} using Ktor Client.
- Consume incoming chunked byte streams every {{progressive_render_chunk_ms}} to reconstruct intermediate JPEG latents.
- Render intermediate previews smoothly in Compose using
ImageBitmapwith non-blocking hardware bitmap decoders. - Store finalized high-resolution bitmaps according to {{cache_eviction_strategy}} using Okio and Android LRU cache.
- Expose an immutable
GenerationUiStateStateFlow providing progress percentage, thermal warnings, and error fallbacks.
Constraints
- The composable pipeline MUST NOT trigger recomposition outside the dedicated canvas preview container during streaming.
- Network image streaming MUST handle connection drops with exponential backoff and resume from the latest seed step.
- Ensure all Bitmap allocations are recycled or managed using Android 10+ HardwareBitmap pools.
- Respect system low-memory trims (
onTrimMemory) by clearing memory caches according to {{cache_eviction_strategy}}.
Output format
Provide a cohesive, modular Kotlin Jetpack Compose implementation:
- State Management & Model Classes (
GenerativeCanvasState.kt) - Compose UI Component (
GenerativeCanvasScreen.kt) - Streaming Client & Cache Repository (
MultimodalGenerationRepository.kt)
Self-review
- Are recomposition counts minimized during high-frequency preview streaming?
- Does the repository gracefully handle network timeouts without leaking coroutine scopes?
- Is the sketch rasterization optimized to run on IO or Default dispatchers?
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.