1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//! `PlayerSurface`: the pictures of a hardware video decoder, drawn in the layout.
use cranpose_core::{MutableState, rememberMutableStateOf};
use cranpose_services::DecodedFrames;
use cranpose_ui::{
Alignment, Box, BoxSpec, BoxWithConstraints, BoxWithConstraintsScope, ContentScale, Image,
Modifier, Painter, composable,
};
/// Shows the pictures of a [`VideoDecoder`](crate::VideoDecoder) in the box that the layout gives
/// it, scaled by `content_scale`, as Media3's `PlayerSurface` shows a player's video. `frames`
/// are the pictures of a decoder whose output is
/// [`DecoderOutput::Frames`](crate::DecoderOutput::Frames).
///
/// Media3's `PlayerSurface` takes a `Player`, and Cranpose has none: the pictures come from the
/// decoder that the app feeds. The `content_scale` is Media3's `ContentFrame` parameter.
///
/// The surface draws the newest picture in the frame that follows its output, and shows the last
/// picture again while no newer one comes. It drops the pictures that it did not show. It waits
/// for the decoder: no work is done in a frame that has no new picture. A picture is converted
/// once on the GPU, with no copy through the CPU, and the decoder's own buffer is the source.
/// The surface keeps each picture until the GPU has read it, which takes up to three frames, so
/// give the decoder a `depth` of 6 or more for a stream of 60 pictures a second. The decoder
/// drops its oldest pictures first when the app falls behind, as it does while a window starts.
///
/// Platforms: macOS and iOS draw through Metal. Android draws through Vulkan: the `video` feature
/// that brings this composable opens the app's device with the extensions that import a hardware
/// buffer, which the GPU's driver offers on Android 8 and later. Where the device lacks them, or where Android renders with OpenGL ES, the
/// surface draws nothing. No other platform has a hardware decoder that Cranpose drives. A box
/// with no picture draws nothing and keeps its size.
#[composable]
pub fn PlayerSurface(frames: DecodedFrames, modifier: Modifier, content_scale: ContentScale) {
let shown = rememberMutableStateOf(|| None::<Painter>);
pump(frames, shown);
Picture(shown, modifier, content_scale);
}
// Draws the picture that `shown` holds, or an empty box of the same size.
#[composable]
pub(crate) fn Picture(
shown: MutableState<Option<Painter>>,
modifier: Modifier,
content_scale: ContentScale,
) {
BoxWithConstraints(modifier, move |scope| {
let constraints = scope.constraints();
let bounded = constraints.max_width.is_finite() && constraints.max_height.is_finite();
let fill = if bounded {
Modifier::empty().fill_max_size()
} else {
Modifier::empty()
};
if let Some(painter) = shown.value() {
Image(
painter,
None,
fill,
Alignment::CENTER,
content_scale,
1.0,
None,
);
} else {
Box(fill, BoxSpec::default(), || {});
}
});
}
// Takes each picture that the decoder outputs and puts its painter in `shown`.
#[cfg(not(target_arch = "wasm32"))]
#[composable]
fn pump(frames: DecodedFrames, shown: MutableState<Option<Painter>>) {
use crate::player_surface_intake::pump_with;
pump_with(frames, shown, |frames, presenter| {
// A picture that is not shown drops, and the last one stays on the screen
if let Some(frame) = frames.latest_frame() {
presenter.present(frame, None).unwrap_or_default();
}
});
}
// A web page has no decoder.
#[cfg(target_arch = "wasm32")]
#[composable]
fn pump(_frames: DecodedFrames, _shown: MutableState<Option<Painter>>) {}