diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index c416e4f3a3..b4fd3fd4b6 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -330,6 +330,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var glassesScreen: GlassesScreen? = null private var glassesOutputActive by mutableStateOf(false) private var pausedFrame by mutableStateOf(null) + private var pausedFrameWanted = false private var trackingRecovery by mutableStateOf(null) private var glasses3dForVr = false private var ended = false @@ -1014,14 +1015,20 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionAchievements(inFront.game, inFront.title, onClose = ::closeAchievements) } com.droiddeck.launcher.ui.AchievementNotificationOverlay() + // Auto and Native resume on their own as the session comes back; only a failed resume needs the button. + val autoResume = suspendPolicy == SessionPrefs.SUSPEND_AUTO || suspendPolicy == SessionPrefs.SUSPEND_NATIVE SessionPausedOverlay( - suspended = SessionState.suspended, + suspended = SessionState.suspended && (!autoResume || SessionState.resumeFailed), engaged = SessionState.resumeRequested || SessionState.resuming, failures = SessionState.resumeFailures, frame = pausedFrame, title = pausedTitle(), onResume = { SessionService.resume(this@SessionActivity) }, - onHidden = { pausedFrame = null }, + onFrameNeeded = ::requestPausedFrame, + onHidden = { + pausedFrame = null + pausedFrameWanted = false + }, ) if (leaving) com.droiddeck.launcher.ui.FloodSpread( com.droiddeck.launcher.ui.LocalPalette.current.signal, @@ -2869,6 +2876,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { // The frozen game cannot draw again, so the paused overlay shows a copy taken while the surface still holds its frame. private fun capturePausedFrame() { if (!SessionState.running || pipUi || isInPictureInPictureMode || glassesOutputActive) return + if (SessionState.suspended && pausedFrame != null) return if (suspendPolicy == SessionPrefs.SUSPEND_NEVER || !::surfaceView.isInitialized) return if (surfaceView.holder.surface?.isValid != true) return val width = surfaceView.width @@ -2891,6 +2899,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { android.view.PixelCopy.request(surfaceView, bitmap, { result -> if (result == android.view.PixelCopy.SUCCESS && !isDestroyed) { pausedFrame = com.droiddeck.launcher.ui.PausedFrame(bitmap.asImageBitmap(), bounds) + pausedFrameWanted = false } else { bitmap.recycle() } @@ -2900,6 +2909,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } } + // Sessions that froze from picture-in-picture, or before onPause could copy a frame, have none; the + // compositor shows the frozen frame again once its output is back, so copy it from there. + private fun requestPausedFrame() { + if (pausedFrame != null) return + pausedFrameWanted = true + if (resumed && surfaceW > 0) capturePausedFrame() + } + private fun pipBounds(): android.graphics.Rect { val rect = android.graphics.Rect() if (!::surfaceView.isInitialized || !surfaceView.getGlobalVisibleRect(rect)) return rect @@ -3295,6 +3312,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { surfaceView.visibility = if (glassesOutputActive) View.INVISIBLE else View.VISIBLE Log.i(TAG, "compositor output attached: " + if (glassesOutputActive) "glasses display ${glassesScreen?.display?.displayId}" else "phone") + if (pausedFrameWanted) uiHandler.postDelayed({ + if (pausedFrameWanted && pausedFrame == null && resumed) capturePausedFrame() + }, PAUSED_FRAME_REDRAW_MS) } private fun restorePhoneOutput(screen: GlassesScreen) { @@ -3555,6 +3575,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { const val EXTRA_REPAIR_RUNTIME = "repairRuntime" private const val LEAVE_TIMEOUT_MS = 1_500L private const val PAUSED_FRAME_MAX_EDGE = 1920f + private const val PAUSED_FRAME_REDRAW_MS = 150L private const val TAG = "SessionActivity" private var startedScreens = 0 private const val FEX_MISSING = "droiddeck-fex: x86 Linux programs need" diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt index cfead3d70f..3db04cabc7 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -330,6 +330,7 @@ class SessionService : Service() { SessionState.suspended = false SessionState.resumeRequested = false SessionState.resuming = false + SessionState.resumeFailed = false SessionState.pipActive = false pipTask = false SessionState.program = intent?.getStringExtra(EXTRA_PROGRAM) @@ -1414,6 +1415,9 @@ class SessionService : Service() { suspendPolicy != SessionPrefs.SUSPEND_NEVER && hidden && downloadActive && !suspendDownloadNow if (suspendPolicy == SessionPrefs.SUSPEND_MANUAL && hidden && !downloadsBlockingSuspend) { manualPauseRequested = true + } else if (!hidden && !SessionState.suspended) { + // Coming back before the freeze finished keeps the game running instead of pausing it a moment later. + manualPauseRequested = false } val controller = suspendController ?: return if (downloadsBlockingSuspend) acquireLocks() @@ -1463,6 +1467,7 @@ class SessionService : Service() { freezePending = false if (success) { SessionState.suspended = true + SessionState.resumeFailed = false SessionEvents.transition(SessionPhase.SUSPENDED, "session.suspended") releaseLocks() refreshNotification() @@ -1490,11 +1495,13 @@ class SessionService : Service() { SessionState.resumeRequested = false if (success) { SessionState.suspended = false + SessionState.resumeFailed = false SessionEvents.transition(SessionEvents.resumePhase(), "session.resumed") refreshNotification() } else { suspendAttemptFailed = true SessionState.resumeFailures++ + SessionState.resumeFailed = true Log.w(TAG, "could not confirm that the session resumed") } updateSuspendPolicy() @@ -1586,6 +1593,7 @@ class SessionService : Service() { SessionState.suspended = false SessionState.resumeRequested = false SessionState.resuming = false + SessionState.resumeFailed = false SessionState.pipActive = false pipTask = false SessionState.guestPid = -1 diff --git a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt index 03741cd0d4..5ff739e478 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -50,6 +50,7 @@ object SessionState { var resumeRequested by mutableStateOf(false) var resuming by mutableStateOf(false) var resumeFailures by mutableIntStateOf(0) + var resumeFailed by mutableStateOf(false) @Volatile var pipActive = false @Volatile var program: String? = null diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt index 691c25bd91..b7f5cbfebe 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt @@ -34,6 +34,8 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember +import androidx.compose.runtime.rememberUpdatedState +import androidx.compose.runtime.snapshotFlow import androidx.compose.runtime.setValue import androidx.compose.runtime.withFrameNanos import androidx.compose.ui.Alignment @@ -41,7 +43,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.blur import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.drawWithContent -import androidx.compose.ui.draw.shadow +import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester import androidx.compose.ui.geometry.Offset @@ -65,13 +67,15 @@ import androidx.compose.ui.unit.IntOffset import androidx.compose.ui.unit.dp import com.droiddeck.launcher.R import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.flow.first import kotlinx.coroutines.launch +import kotlinx.coroutines.withTimeoutOrNull /** The last game frame, captured as the session left the screen, and where it sat in the overlay. */ class PausedFrame(val image: ImageBitmap, val bounds: android.graphics.Rect) private const val CARD_SCALE = 0.82f -private val PopEasing = CubicBezierEasing(0.3f, 1.5f, 0.4f, 1f) +private const val FRAME_WAIT_MS = 600L private fun tone(saturation: Float, brightness: Float): ColorMatrix = ColorMatrix().apply { @@ -87,8 +91,10 @@ fun SessionPausedOverlay( frame: PausedFrame?, title: String, onResume: () -> Unit, + onFrameNeeded: () -> Unit, onHidden: () -> Unit, ) { + val currentFrame by rememberUpdatedState(frame) var shown by remember { mutableStateOf(suspended) } val lift = remember { Animatable(if (suspended) 1f else 0f) } val orb = remember { Animatable(if (suspended) 1f else 0f) } @@ -99,16 +105,26 @@ fun SessionPausedOverlay( LaunchedEffect(suspended, engaged) { when { suspended && !engaged -> { - shown = true + if (!shown) { + if (currentFrame == null) { + onFrameNeeded() + withTimeoutOrNull(FRAME_WAIT_MS) { snapshotFlow { currentFrame }.first { it != null } } + } + // Pausing mostly happens while the screen is off or the app is away, so it appears already settled. + press.snapTo(1f) + ring.snapTo(0f) + lift.snapTo(1f) + orb.snapTo(1f) + shown = true + return@LaunchedEffect + } launch { press.animateTo(1f, Motion.sp(0.5f, Spring.StiffnessMedium)) } launch { ring.animateTo(0f, Motion.tw(200)) } - launch { lift.animateTo(1f, Motion.tw(520)) } - orb.animateTo(1f, Motion.tw(380, delay = if (orb.value < 0.5f) 260 else 0, easing = PopEasing)) + orb.animateTo(1f, Motion.tw(200)) } suspended -> { - shown = true + if (!shown) return@LaunchedEffect launch { orb.animateTo(1f, Motion.tw(200)) } - launch { lift.animateTo(1f, Motion.tw(300)) } launch { press.animateTo(0.92f, Motion.tw(140)) } ring.animateTo(0.25f, Motion.tw(140)) ring.animateTo(0.8f, Motion.tw(650, easing = CubicBezierEasing(0.1f, 0.6f, 0.3f, 1f))) @@ -223,7 +239,10 @@ private fun ResumeOrb(presence: Float, press: Float, ring: Float, shake: Float, alpha = presence.coerceIn(0f, 1f) translationX = shake.dp.toPx() } - .shadow(16.dp, CircleShape, clip = false) + .drawBehind { + val halo = size.minDimension * 0.8f + drawCircle(Brush.radialGradient(0.6f to Color.Black.copy(alpha = 0.35f), 1f to Color.Transparent, radius = halo), radius = halo) + } .clip(CircleShape) .background(Color.White.copy(alpha = 0.08f)) .drawWithContent {