diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 6de07a00dc..c416e4f3a3 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -329,6 +329,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { private var glassesSettings by mutableStateOf(Glasses.settings.value) private var glassesScreen: GlassesScreen? = null private var glassesOutputActive by mutableStateOf(false) + private var pausedFrame by mutableStateOf(null) private var trackingRecovery by mutableStateOf(null) private var glasses3dForVr = false private var ended = false @@ -1013,9 +1014,14 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { SessionAchievements(inFront.game, inFront.title, onClose = ::closeAchievements) } com.droiddeck.launcher.ui.AchievementNotificationOverlay() - if (SessionState.suspended) SessionPausedOverlay( + SessionPausedOverlay( + suspended = SessionState.suspended, + engaged = SessionState.resumeRequested || SessionState.resuming, + failures = SessionState.resumeFailures, + frame = pausedFrame, title = pausedTitle(), onResume = { SessionService.resume(this@SessionActivity) }, + onHidden = { pausedFrame = null }, ) if (leaving) com.droiddeck.launcher.ui.FloodSpread( com.droiddeck.launcher.ui.LocalPalette.current.signal, @@ -2860,6 +2866,40 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { if (down || code !in gestureKeysDown) WaylandCompositor.nativeSendKey(code, if (down) 1 else 0) } + // 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 (suspendPolicy == SessionPrefs.SUSPEND_NEVER || !::surfaceView.isInitialized) return + if (surfaceView.holder.surface?.isValid != true) return + val width = surfaceView.width + val height = surfaceView.height + if (width <= 0 || height <= 0) return + val scale = minOf(1f, PAUSED_FRAME_MAX_EDGE / maxOf(width, height).toFloat()) + val bitmap = try { + android.graphics.Bitmap.createBitmap( + maxOf(1, (width * scale).toInt()), maxOf(1, (height * scale).toInt()), android.graphics.Bitmap.Config.ARGB_8888, + ) + } catch (_: OutOfMemoryError) { + return + } + val surfaceAt = IntArray(2).also { surfaceView.getLocationInWindow(it) } + val overlayAt = IntArray(2).also { sessionOverlay.getLocationInWindow(it) } + val left = surfaceAt[0] - overlayAt[0] + val top = surfaceAt[1] - overlayAt[1] + val bounds = android.graphics.Rect(left, top, left + width, top + height) + try { + android.view.PixelCopy.request(surfaceView, bitmap, { result -> + if (result == android.view.PixelCopy.SUCCESS && !isDestroyed) { + pausedFrame = com.droiddeck.launcher.ui.PausedFrame(bitmap.asImageBitmap(), bounds) + } else { + bitmap.recycle() + } + }, uiHandler) + } catch (_: IllegalArgumentException) { + bitmap.recycle() + } + } + private fun pipBounds(): android.graphics.Rect { val rect = android.graphics.Rect() if (!::surfaceView.isInitialized || !surfaceView.getGlobalVisibleRect(rect)) return rect @@ -2990,6 +3030,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { } applyFollowOrientation(resources.configuration) if (runtimeRemovalBlocked) return + if (!SessionState.suspended) pausedFrame = null com.droiddeck.launcher.ui.Motion.refresh(this) refreshHomeApp() if (intent?.action == SessionService.ACTION_RESUME) { @@ -3015,6 +3056,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { override fun onPause() { if (runtimeRemovalBlocked) { super.onPause(); return } + capturePausedFrame() sessionClipboard.stop() if (::touchscreenGestures.isInitialized) touchscreenGestures.cancel() if (::touchpad.isInitialized) touchpad.cancel() @@ -3512,6 +3554,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback { const val EXTRA_STEAMVR = "steamVr" 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 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 31c63ff469..cfead3d70f 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionService.kt @@ -328,6 +328,8 @@ class SessionService : Service() { suspendAttemptFailed = false suspendController = null SessionState.suspended = false + SessionState.resumeRequested = false + SessionState.resuming = false SessionState.pipActive = false pipTask = false SessionState.program = intent?.getStringExtra(EXTRA_PROGRAM) @@ -1210,6 +1212,7 @@ class SessionService : Service() { } private fun resumeSession(panelVisible: Boolean = true) { + if (SessionState.suspended) SessionState.resumeRequested = true if (panelVisible) activityVisible = true screenOn = readScreenOn() manualPauseRequested = false @@ -1436,6 +1439,7 @@ class SessionService : Service() { freezeCancelled = true controller.cancelFreeze() } + if (shouldSuspend && SessionState.suspended && !suspendOperationPending) SessionState.resumeRequested = false if (suspendOperationPending || suspendAttemptFailed || shouldSuspend == SessionState.suspended) return val gen = sessionGen suspendOperationPending = true @@ -1477,16 +1481,20 @@ class SessionService : Service() { } } else { acquireLocks() + SessionState.resuming = true controller.resume { success -> mainHandler.post { if (gen != sessionGen || !SessionState.running) return@post suspendOperationPending = false + SessionState.resuming = false + SessionState.resumeRequested = false if (success) { SessionState.suspended = false SessionEvents.transition(SessionEvents.resumePhase(), "session.resumed") refreshNotification() } else { suspendAttemptFailed = true + SessionState.resumeFailures++ Log.w(TAG, "could not confirm that the session resumed") } updateSuspendPolicy() @@ -1576,6 +1584,8 @@ class SessionService : Service() { return@post } SessionState.suspended = false + SessionState.resumeRequested = false + SessionState.resuming = false SessionState.pipActive = false pipTask = false SessionState.guestPid = -1 @@ -2022,6 +2032,7 @@ class SessionService : Service() { fun resume(context: Context) { if (!SessionState.running) return + if (SessionState.suspended) SessionState.resumeRequested = true context.startService(Intent(context, SessionService::class.java).setAction(ACTION_RESUME)) } 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 cebc33caa0..03741cd0d4 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/SessionState.kt @@ -2,6 +2,7 @@ package com.droiddeck.launcher.session import java.io.File import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue @@ -46,6 +47,9 @@ object SessionState { @Volatile var runtimeDriver: String? = null var suspended by mutableStateOf(false) + var resumeRequested by mutableStateOf(false) + var resuming by mutableStateOf(false) + var resumeFailures by mutableIntStateOf(0) @Volatile var pipActive = false @Volatile var program: String? = null diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionLoading.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionLoading.kt index 4a0adea2d4..d1c478eb08 100644 --- a/app/src/main/java/com/droiddeck/launcher/ui/SessionLoading.kt +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionLoading.kt @@ -24,7 +24,6 @@ import androidx.compose.animation.core.FastOutSlowInEasing import androidx.compose.animation.core.tween import androidx.compose.material.icons.Icons import androidx.compose.foundation.layout.heightIn -import androidx.compose.material.icons.filled.Pause import androidx.compose.foundation.rememberScrollState import androidx.compose.material3.Icon import androidx.compose.foundation.Canvas @@ -345,37 +344,3 @@ private fun CancelHint(onCancel: () -> Unit, modifier: Modifier = Modifier) { Text(stringResource(R.string.common_cancel), fontSize = 13.sp, color = colors.onSurfaceVariant) } } - -@Composable -fun SessionPausedOverlay(title: String = stringResource(R.string.load_steam_paused), onResume: () -> Unit) { - val colors = MaterialTheme.colorScheme - val pal = LocalPalette.current - val resumeFocus = remember { FocusRequester() } - LaunchedEffect(Unit) { - androidx.compose.runtime.withFrameNanos { } - runCatching { resumeFocus.requestFocus() } - } - Box( - modifier = Modifier - .fillMaxSize() - .background(pal.background.copy(alpha = 0.94f)) - .blockTouches(), - contentAlignment = Alignment.Center, - ) { - Column( - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.spacedBy(10.dp), - modifier = Modifier.widthIn(max = 440.dp).padding(32.dp), - ) { - Box(contentAlignment = Alignment.Center, modifier = Modifier.padding(bottom = 6.dp).size(64.dp).border(2.dp, pal.line2, CircleShape)) { - Icon(Icons.Filled.Pause, contentDescription = null, tint = colors.onBackground, modifier = Modifier.size(30.dp)) - } - Text(title, fontSize = 28.sp, fontWeight = FontWeight.Bold, color = colors.onBackground, textAlign = TextAlign.Center) - PrimaryButton( - stringResource(R.string.resume_session), - modifier = Modifier.padding(top = 14.dp).focusRequester(resumeFocus).controllerConfirm(onClick = onResume), - onClick = onResume, - ) - } - } -} diff --git a/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt b/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt new file mode 100644 index 0000000000..691c25bd91 --- /dev/null +++ b/app/src/main/java/com/droiddeck/launcher/ui/SessionPausedOverlay.kt @@ -0,0 +1,257 @@ +package com.droiddeck.launcher.ui + +import android.os.Build +import androidx.compose.animation.core.Animatable +import androidx.compose.animation.core.CubicBezierEasing +import androidx.compose.animation.core.FastOutLinearInEasing +import androidx.compose.animation.core.LinearEasing +import androidx.compose.animation.core.LinearOutSlowInEasing +import androidx.compose.animation.core.Spring +import androidx.compose.animation.core.animateFloat +import androidx.compose.animation.core.animateFloatAsState +import androidx.compose.animation.core.infiniteRepeatable +import androidx.compose.animation.core.keyframes +import androidx.compose.animation.core.rememberInfiniteTransition +import androidx.compose.animation.core.tween +import androidx.compose.foundation.Image +import androidx.compose.foundation.background +import androidx.compose.foundation.clickable +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.shape.CircleShape +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.rounded.PlayArrow +import androidx.compose.material3.Icon +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.State +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.runtime.withFrameNanos +import androidx.compose.ui.Alignment +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.focus.FocusRequester +import androidx.compose.ui.focus.focusRequester +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.ColorFilter +import androidx.compose.ui.graphics.ColorMatrix +import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.StrokeCap +import androidx.compose.ui.graphics.drawscope.Stroke +import androidx.compose.ui.graphics.drawscope.rotate +import androidx.compose.ui.graphics.graphicsLayer +import androidx.compose.ui.layout.ContentScale +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.semantics.Role +import androidx.compose.ui.semantics.contentDescription +import androidx.compose.ui.semantics.semantics +import androidx.compose.ui.unit.IntOffset +import androidx.compose.ui.unit.dp +import com.droiddeck.launcher.R +import kotlinx.coroutines.coroutineScope +import kotlinx.coroutines.launch + +/** 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 fun tone(saturation: Float, brightness: Float): ColorMatrix = + ColorMatrix().apply { + setToSaturation(saturation) + timesAssign(ColorMatrix().apply { setToScale(brightness, brightness, brightness, 1f) }) + } + +@Composable +fun SessionPausedOverlay( + suspended: Boolean, + engaged: Boolean, + failures: Int, + frame: PausedFrame?, + title: String, + onResume: () -> Unit, + onHidden: () -> Unit, +) { + var shown by remember { mutableStateOf(suspended) } + val lift = remember { Animatable(if (suspended) 1f else 0f) } + val orb = remember { Animatable(if (suspended) 1f else 0f) } + val press = remember { Animatable(1f) } + val ring = remember { Animatable(0f) } + val shake = remember { Animatable(0f) } + + LaunchedEffect(suspended, engaged) { + when { + suspended && !engaged -> { + shown = true + 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)) + } + suspended -> { + shown = true + 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))) + ring.animateTo(0.92f, Motion.tw(2000, easing = LinearOutSlowInEasing)) + } + shown -> { + ring.animateTo(1f, Motion.tw(110)) + coroutineScope { + launch { orb.animateTo(0f, Motion.tw(220, easing = FastOutLinearInEasing)) } + lift.animateTo(0f, Motion.sp(0.62f, 380f)) + } + shown = false + ring.snapTo(0f) + press.snapTo(1f) + onHidden() + } + } + } + + val seenFailures = remember { mutableIntStateOf(failures) } + LaunchedEffect(failures) { + if (failures == seenFailures.intValue) return@LaunchedEffect + seenFailures.intValue = failures + if (!shown || Motion.scale == 0f) return@LaunchedEffect + shake.animateTo(0f, keyframes { + durationMillis = Motion.ms(360) + 10f at Motion.ms(60) + -8f at Motion.ms(140) + 5f at Motion.ms(220) + -2f at Motion.ms(300) + }) + } + + if (!shown) return + + val presence = lift.value.coerceIn(0f, 1f) + val idle = suspended && !engaged + val focus = remember { FocusRequester() } + LaunchedEffect(Unit) { + withFrameNanos { } + runCatching { focus.requestFocus() } + } + val resumeLabel = stringResource(R.string.resume_session) + Box( + contentAlignment = Alignment.Center, + modifier = Modifier + .fillMaxSize() + .semantics { contentDescription = title } + .focusRequester(focus) + .controllerConfirm(enabled = idle, onClick = onResume) + .clickable( + interactionSource = remember { MutableInteractionSource() }, + indication = null, + role = Role.Button, + onClickLabel = resumeLabel, + ) { if (idle) onResume() }, + ) { + if (frame != null) { + PausedCard(frame, lift.value, presence) + } else { + Box(Modifier.fillMaxSize().graphicsLayer { alpha = presence }.background(Color.Black.copy(alpha = 0.55f))) + } + ResumeOrb(orb.value, press.value, ring.value, shake.value, idle) + } +} + +@Composable +private fun BoxScope.PausedCard(frame: PausedFrame, lift: Float, presence: Float) { + val density = LocalDensity.current + val bounds = frame.bounds + val ambient = remember { ColorFilter.colorMatrix(tone(0.1f, 0.3f)) } + Box(Modifier.fillMaxSize().graphicsLayer { alpha = presence }.background(Color.Black)) { + Image( + frame.image, contentDescription = null, contentScale = ContentScale.Crop, colorFilter = ambient, + modifier = Modifier.fillMaxSize().then(if (Build.VERSION.SDK_INT >= 31) Modifier.blur(32.dp) else Modifier), + ) + } + Image( + frame.image, contentDescription = null, contentScale = ContentScale.FillBounds, + colorFilter = ColorFilter.colorMatrix(tone(1f - 0.92f * presence, 1f - 0.45f * presence)), + modifier = Modifier + .align(Alignment.TopStart) + .offset { IntOffset(bounds.left, bounds.top) } + .size(with(density) { bounds.width().toDp() }, with(density) { bounds.height().toDp() }) + .graphicsLayer { + val scale = 1f - (1f - CARD_SCALE) * lift + scaleX = scale + scaleY = scale + shape = RoundedCornerShape((20f * presence).dp) + clip = true + shadowElevation = 24.dp.toPx() * presence + }, + ) +} + +@Composable +private fun ResumeOrb(presence: Float, press: Float, ring: Float, shake: Float, idle: Boolean) { + val glintAlpha by animateFloatAsState(if (idle) 1f else 0f, Motion.tw(160), label = "glintAlpha") + val glint: State? = if (Motion.scale > 0f) { + rememberInfiniteTransition(label = "glint").animateFloat( + 0f, 360f, infiniteRepeatable(tween(Motion.ms(3600), easing = LinearEasing)), label = "glintAngle", + ) + } else null + Box( + contentAlignment = Alignment.Center, + modifier = Modifier + .size(96.dp) + .graphicsLayer { + val scale = press * (0.4f + 0.6f * presence) + scaleX = scale + scaleY = scale + alpha = presence.coerceIn(0f, 1f) + translationX = shake.dp.toPx() + } + .shadow(16.dp, CircleShape, clip = false) + .clip(CircleShape) + .background(Color.White.copy(alpha = 0.08f)) + .drawWithContent { + drawContent() + val stroke = 2.5.dp.toPx() + val radius = size.minDimension / 2f * 0.92f + if (glint != null && glintAlpha > 0f) { + rotate(glint.value) { + drawCircle( + Brush.sweepGradient( + 0f to Color.Transparent, + 0.083f to Color.White.copy(alpha = 0.4f * glintAlpha), + 0.166f to Color.Transparent, + 1f to Color.Transparent, + ), + radius = radius, style = Stroke(stroke), + ) + } + } + if (ring > 0f) { + drawArc( + Color.White, startAngle = -90f, sweepAngle = 360f * ring.coerceAtMost(1f), useCenter = false, + topLeft = Offset(center.x - radius, center.y - radius), size = Size(radius * 2f, radius * 2f), + style = Stroke(stroke, cap = StrokeCap.Round), + ) + } + }, + ) { + Icon(Icons.Rounded.PlayArrow, contentDescription = null, tint = Color.White, modifier = Modifier.size(48.dp)) + } +}