fix(android): paint the letterbox bars, so stale pixels stop surviving in them
Native video left debris in the padding around the video: the "previous frame" flash on rotation, a ghost copy of the control bar stranded in the top bar, each new clock digit drawn over the one before it (35:42 with the 1 still showing through the 2), and the sleep/quality menus leaving their imprint after closing. One cause under all of it — nothing painted those bars. The window surface is opaque; the theme is not translucent and dumpsys window shows no translucency flag. For an opaque surface HWUI deliberately does NOT clear the damaged region before replaying a frame: it assumes the view hierarchy covers every pixel it owns. Here that hierarchy is window background → video TextureView → transparent WebView, and fitSurfaceToScreen sizes the TextureView to the letterboxed video rect. So the bars were the window background's alone to paint, and setTransparent(true) cleared it to TRANSPARENT — leaving them painted by nobody, with whatever was last in the framebuffer surviving there. The window background now stays opaque black while compositing. It cannot hide the video: the TextureView is drawn on top of it, and the WebView's own background is what lets the picture through. Three previous attempts missed because they aimed at the window's rotation animation and at TextureView frame-retention — two postOnAnimation hops, an onSurfaceTextureUpdated reveal, then ROTATION_ANIMATION_JUMPCUT with FLAG_FULLSCREEN to make it stick. The pixels were never the animation's, which is also why the artefact reproduces standing still, with no rotation involved. Those are removed. The alpha-hiding among them actively made things worse: it blanked the one view that reliably paints its own rect. FLAG_FULLSCREEN goes too — it fought edge-to-edge insets for no gain. Verified on device (HONOR ROD2-W09, Android 16): reproduced with native video on — ghost control bar in the top bar, doubled clock digit — then absent after the fix across playback, the control bar and a rotation round-trip. DR-194 is rewritten to record the real mechanism and marked Done.
This commit is contained in:
@@ -191,37 +191,6 @@ class MainActivity : TauriActivity() {
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotation (and any other config change this Activity handles itself).
|
||||
*
|
||||
* Two things have to happen here rather than later, and both are about the
|
||||
* *previous* video frame surviving the transition:
|
||||
*
|
||||
* - The video view is hidden until a new frame arrives. The equivalent call
|
||||
* in `fitSurfaceToScreen` runs from the content view's layout listener,
|
||||
* which is after the rotation — by then the stale frame has been on screen
|
||||
* for the whole transition.
|
||||
* - The window's rotation animation is a **cross-fade of a screenshot** of
|
||||
* the old orientation, and that screenshot contains the old video frame at
|
||||
* the old size. No amount of TextureView bookkeeping can touch it, which is
|
||||
* why hiding on frame-arrival alone did not stop the flash. `JUMPCUT` drops
|
||||
* the cross-fade, so there is no old frame to fade through; it is set only
|
||||
* while native compositing is active (see setTransparent) so the rest of
|
||||
* the app keeps the normal animation.
|
||||
*
|
||||
* TRACES: UR-003, UR-066 | DR-194
|
||||
*/
|
||||
override fun onConfigurationChanged(newConfig: android.content.res.Configuration) {
|
||||
super.onConfigurationChanged(newConfig)
|
||||
try {
|
||||
if (com.dtourolle.jellytau.player.JellyTauPlayer.isInitialized()) {
|
||||
com.dtourolle.jellytau.player.JellyTauPlayer.getInstance().hideUntilFreshFrame()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
android.util.Log.w("MainActivity", "hideUntilFreshFrame on config change failed", e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onPictureInPictureModeChanged(
|
||||
isInPictureInPictureMode: Boolean,
|
||||
newConfig: android.content.res.Configuration
|
||||
@@ -401,45 +370,46 @@ class MainActivity : TauriActivity() {
|
||||
@JavascriptInterface
|
||||
fun setTransparent(transparent: Boolean) {
|
||||
handler.post {
|
||||
val color = if (transparent) {
|
||||
android.graphics.Color.TRANSPARENT
|
||||
} else {
|
||||
android.graphics.Color.BLACK
|
||||
}
|
||||
mediaWebView?.setBackgroundColor(color)
|
||||
// The WebView's window/surface must also stop painting opaque, or a
|
||||
// hardware-accelerated WebView still composites its own background.
|
||||
window.setBackgroundDrawable(
|
||||
android.graphics.drawable.ColorDrawable(color)
|
||||
mediaWebView?.setBackgroundColor(
|
||||
if (transparent) {
|
||||
android.graphics.Color.TRANSPARENT
|
||||
} else {
|
||||
android.graphics.Color.BLACK
|
||||
}
|
||||
)
|
||||
// The WINDOW background stays OPAQUE — including while compositing.
|
||||
// It is the only thing that paints the pixels the video does not
|
||||
// cover, and clearing it was the whole defect.
|
||||
//
|
||||
// This window's surface is opaque: the theme is not translucent and
|
||||
// `dumpsys window` shows no translucency flag on it. For an opaque
|
||||
// surface HWUI deliberately does NOT clear the damaged region before
|
||||
// replaying a frame — it assumes the view hierarchy paints every
|
||||
// pixel it owns. That hierarchy is: window background, then the video
|
||||
// TextureView, then this transparent WebView. `fitSurfaceToScreen`
|
||||
// sizes the TextureView to the *letterboxed* video rect, so the bars
|
||||
// around the video are painted by the window background and nothing
|
||||
// else.
|
||||
//
|
||||
// Setting that background TRANSPARENT therefore left the bars painted
|
||||
// by nobody, and stale framebuffer content simply survived in them:
|
||||
// a whole ghost copy of the control bar stranded in the top bar, and
|
||||
// each new clock digit composited over the one before it ("35:42"
|
||||
// with the 1 still showing through the 2). The rotation flash is the
|
||||
// same bug at full-screen scale — the pre-rotation image persisting
|
||||
// in what became the new bars — which is why neither
|
||||
// ROTATION_ANIMATION_JUMPCUT nor revealing on frame arrival ever
|
||||
// touched it. Both were aimed at the window animation; the pixels
|
||||
// were never the animation's.
|
||||
//
|
||||
// The WebView's own background, set above, is what lets the video
|
||||
// through. An opaque window background cannot hide it: the
|
||||
// TextureView is drawn on top of it, not under it.
|
||||
//
|
||||
// TRACES: UR-003, UR-066 | DR-194
|
||||
window.setBackgroundDrawable(
|
||||
android.graphics.drawable.ColorDrawable(android.graphics.Color.BLACK)
|
||||
)
|
||||
// Drop the rotation cross-fade while a native video surface is
|
||||
// composited behind the page. The animation fades a *screenshot* of
|
||||
// the old orientation, which still holds the previous video frame at
|
||||
// the old size — that is the "previous frame flashing in the black
|
||||
// bars", and it lives in the window animation rather than in
|
||||
// anything the TextureView owns. (DR-194)
|
||||
val attrs = window.attributes
|
||||
attrs.rotationAnimation = if (transparent) {
|
||||
android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_JUMPCUT
|
||||
} else {
|
||||
android.view.WindowManager.LayoutParams.ROTATION_ANIMATION_ROTATE
|
||||
}
|
||||
window.attributes = attrs
|
||||
// `rotationAnimation` is honoured only for a **fullscreen** window —
|
||||
// the platform says so out loud, logging
|
||||
// "VRI[MainActivity]: setLayoutParams: not fullscreen" when the
|
||||
// attribute is set on ours, and then animating normally regardless.
|
||||
// Without this the JUMPCUT above is accepted and ignored, and the
|
||||
// cross-fade keeps showing the old orientation's screenshot, stale
|
||||
// video frame and all. FLAG_FULLSCREEN is deprecated for *hiding
|
||||
// system bars* (immersive mode does that, on player entry), but it
|
||||
// is still what marks the window fullscreen for this decision.
|
||||
@Suppress("DEPRECATION")
|
||||
if (transparent) {
|
||||
window.addFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||
} else {
|
||||
window.clearFlags(android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||
}
|
||||
android.util.Log.d("MainActivity", "WebView transparent = $transparent")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,12 +38,6 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
/** AudioEffect priority. Positive = higher priority than the default. */
|
||||
private const val EFFECT_PRIORITY = 1000
|
||||
|
||||
/**
|
||||
* How long to wait for a fresh frame after a resize before revealing the
|
||||
* view anyway. Playback may be paused, in which case no frame is coming.
|
||||
*/
|
||||
private const val FRESH_FRAME_TIMEOUT_MS = 400L
|
||||
|
||||
/**
|
||||
* Canonical 10-band ISO centre frequencies (Hz), mirroring EQ_BANDS in
|
||||
* settings.rs. Kept in sync deliberately: Rust owns the band layout, this
|
||||
@@ -236,12 +230,6 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
/** The Surface handed to ExoPlayer, owned here rather than by the player. */
|
||||
private var videoSurface: android.view.Surface? = null
|
||||
|
||||
/**
|
||||
* True while the view is hidden waiting for a new frame after a resize.
|
||||
* See fitSurfaceToScreen (DR-194).
|
||||
*/
|
||||
@Volatile
|
||||
private var awaitingFreshFrame = false
|
||||
/** Last reported video frame size, used to fit the surface to the screen preserving aspect ratio */
|
||||
private var videoWidth: Int = 0
|
||||
private var videoHeight: Int = 0
|
||||
@@ -1141,12 +1129,15 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
isOpaque = true
|
||||
|
||||
// Own the listener rather than calling `setVideoTextureView`,
|
||||
// which installs ExoPlayer's own and leaves us blind to frame
|
||||
// arrival. `onSurfaceTextureUpdated` is the only honest signal
|
||||
// that a NEW frame has landed in the texture, and that is
|
||||
// precisely what the letterbox artefact waits on — see
|
||||
// fitSurfaceToScreen. Handing ExoPlayer the Surface directly is
|
||||
// the same wiring `setVideoTextureView` does internally.
|
||||
// which installs ExoPlayer's own. Handing ExoPlayer the Surface
|
||||
// directly is the same wiring `setVideoTextureView` does
|
||||
// internally, and owning the listener keeps surface creation and
|
||||
// teardown symmetrical with `videoSurface` below.
|
||||
//
|
||||
// (This was originally introduced to observe frame arrival for
|
||||
// the letterbox artefact. That turned out to be the wrong lead —
|
||||
// see fitSurfaceToScreen — but the explicit wiring is worth
|
||||
// keeping on its own terms.)
|
||||
//
|
||||
// TRACES: UR-003, UR-004 | DR-194
|
||||
surfaceTextureListener = object : TextureView.SurfaceTextureListener {
|
||||
@@ -1180,12 +1171,6 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
override fun onSurfaceTextureUpdated(
|
||||
texture: android.graphics.SurfaceTexture
|
||||
) {
|
||||
// A genuinely new frame is now in the texture, so
|
||||
// whatever was retained from before the resize is gone.
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1258,31 +1243,6 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
* pillarbox). A raw SurfaceView with MATCH_PARENT otherwise stretches the
|
||||
* video to the surface bounds, which crops the bottom on rotation.
|
||||
*/
|
||||
/**
|
||||
* Hide the video view now, and keep it hidden until a genuinely new frame
|
||||
* arrives (or the timeout fires).
|
||||
*
|
||||
* Called from `MainActivity.onConfigurationChanged`, i.e. at the *start* of a
|
||||
* rotation. [fitSurfaceToScreen] is too late for this: it runs from the
|
||||
* content view's layout listener, after the rotation has already happened,
|
||||
* so the stale frame has been on screen for the whole transition by then.
|
||||
*
|
||||
* TRACES: UR-003, UR-066 | DR-194
|
||||
*/
|
||||
fun hideUntilFreshFrame() {
|
||||
mainHandler.post {
|
||||
val view = videoView ?: return@post
|
||||
awaitingFreshFrame = true
|
||||
view.alpha = 0f
|
||||
mainHandler.postDelayed({
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}, FRESH_FRAME_TIMEOUT_MS)
|
||||
}
|
||||
}
|
||||
|
||||
fun fitSurfaceToScreen() {
|
||||
mainHandler.post {
|
||||
val view = videoView ?: return@post
|
||||
@@ -1318,43 +1278,19 @@ class JellyTauPlayer(private val appContext: Context) {
|
||||
lp.gravity = android.view.Gravity.CENTER
|
||||
}
|
||||
|
||||
// Hide the view across a resize, and reveal it when a genuinely NEW
|
||||
// video frame lands in the texture.
|
||||
// Deliberately no alpha-hiding across the resize.
|
||||
//
|
||||
// A TextureView retains its last frame. Between a rotation and this
|
||||
// re-fit landing, that retained frame is stretched across the OLD
|
||||
// rect — larger than the new one along at least one axis — so the
|
||||
// previous frame flashes in what should be the letterbox bars.
|
||||
//
|
||||
// Waiting a fixed number of animation frames does NOT fix it, which
|
||||
// the first attempt at this proved on device: an animation frame is
|
||||
// not a video frame, and at 24fps the next decoded frame can be
|
||||
// several vsyncs away. The tell was that pausing and playing cleared
|
||||
// the artefact by hand — that forces a fresh frame, which is the
|
||||
// real precondition. So the reveal is driven by
|
||||
// `onSurfaceTextureUpdated` instead.
|
||||
//
|
||||
// The timeout is not belt-and-braces, it is required: if playback is
|
||||
// paused when the resize happens, no new frame is coming and the
|
||||
// video would stay invisible forever. Revealing a stale frame after
|
||||
// a beat is strictly better than a permanently black player.
|
||||
//
|
||||
// Scoped to an actual size change so steady-state playback never
|
||||
// touches alpha.
|
||||
// Two earlier attempts hid the view here (and from
|
||||
// onConfigurationChanged) until a fresh frame landed, on the reading
|
||||
// that the letterbox flash was a retained TextureView frame drawn at
|
||||
// the old size. It was not: the bars were showing stale *framebuffer*
|
||||
// content because nothing painted them — see the window-background
|
||||
// note in MainActivity.setTransparent. Hiding the video view made
|
||||
// that strictly worse, since the TextureView is the one view in the
|
||||
// hierarchy that reliably paints its own rect; dropping its alpha to
|
||||
// 0 simply widened the un-painted area.
|
||||
//
|
||||
// TRACES: UR-003, UR-066 | DR-194
|
||||
val sizeChanged = lp.width != targetW || lp.height != targetH
|
||||
if (sizeChanged) {
|
||||
awaitingFreshFrame = true
|
||||
view.alpha = 0f
|
||||
mainHandler.postDelayed({
|
||||
if (awaitingFreshFrame) {
|
||||
awaitingFreshFrame = false
|
||||
videoView?.alpha = 1f
|
||||
}
|
||||
}, FRESH_FRAME_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
lp.width = targetW
|
||||
lp.height = targetH
|
||||
view.layoutParams = lp
|
||||
|
||||
Reference in New Issue
Block a user