forge
Explore
Log in

Don't you dare go Hollow

Two pictures in flight only on the seat it was measured on

Browse files at this commit

fa1ce8e70a4f4365627c6bebcf43cb297f93555bAhmet Kaan GümüşParent: e5b7da5
  • fix
    • two pictures in flight only on the seat it was measured on
Show full message
🐛 fix(engine-hw): two pictures in flight only on the seat it was measured on

Depth 2 makes a promise about the driver: a picture submitted must hand its
access unit back on the very next poll, and `encode` treats a seat that does
not as an error, because it cannot write into a buffer the GPU may still be
reading. That was measured on the H.264 seat and nowhere else, and it was
applied to all three. HEVC and AV1 on this plugin are intra-only through the
same driver and very probably behave the same way -- "very probably" is not
what a hard error should rest on, and the GPU has been unavailable since the
measurement was possible. They take depth 1, which is the one-buffer
behaviour every seat had before this lane: the picture coded before the next
is written. `enc_depth` says so in one place, and the number is per session
rather than a constant, so raising it is a measurement away.

Also in the vendored flush: a plane of zero length at a non-zero offset would
have flushed the byte one past the mapping, where upstream's byte loop
flushed nothing at all for `0..0`. Guarded. No such plane exists on this
path -- both NV12 planes are non-empty -- so this is the edge kept closed
rather than a bug seen.
crates/engine-hw/src/lib.rsmodified+33 -7
@@ -771,7 +771,7 @@ fn fix_slice_nal_headers(au: &mut [u8]) {
771 771
772 /// One hardware H.264 encode session.772 /// One hardware H.264 encode session.
773 ///773 ///
774-/// Input goes through [`ENC_DEPTH`] reusable NV12 buffer objects: the caller's774+/// Input goes through [`enc_depth`] reusable NV12 buffer objects: the caller's
775 /// I420 planes are interleaved into the next one and the encoder imports it as a775 /// I420 planes are interleaved into the next one and the encoder imports it as a
776 /// VA surface. Reuse is safe because a buffer is only written again once the776 /// VA surface. Reuse is safe because a buffer is only written again once the
777 /// access unit coded *from* it has been polled back -- the encoder runs in777 /// access unit coded *from* it has been polled back -- the encoder runs in
@@ -796,8 +796,11 @@ struct EncSession {
796 codec: EncCodec,796 codec: EncCodec,
797 /// Owns the DRM node the buffer objects below were allocated from.797 /// Owns the DRM node the buffer objects below were allocated from.
798 _gbm: gbm::Device<std::fs::File>,798 _gbm: gbm::Device<std::fs::File>,
799- /// [`ENC_DEPTH`] input buffers, used round-robin.799+ /// [`enc_depth`] input buffers, used round-robin.
800 frames: Vec<GenericDmaVideoFrame>,800 frames: Vec<GenericDmaVideoFrame>,
801+ /// How many of them there are, which is how many pictures may be in the
802+ /// encoder at once.
803+ depth: usize,
801 /// Which of them the next picture is written into.804 /// Which of them the next picture is written into.
802 slot: usize,805 slot: usize,
803 /// Pictures submitted whose access unit has not been polled back yet, which806 /// Pictures submitted whose access unit has not been polled back yet, which
@@ -826,6 +829,25 @@ struct EncSession {
826 /// hold a picture waiting for a lane that is never idle.829 /// hold a picture waiting for a lane that is never idle.
827 const ENC_DEPTH: usize = 2;830 const ENC_DEPTH: usize = 2;
828 831
832+/// How many pictures *this* seat may hold, which is [`ENC_DEPTH`] for H.264 and
833+/// one for the other two.
834+///
835+/// Depth 2 means a submitted picture must hand its access unit back on the next
836+/// poll, and [`EncSession::encode`] treats a seat that does not as an error --
837+/// it cannot write into a buffer the GPU may still be reading. That contract
838+/// was measured on the H.264 seat and only there. HEVC and AV1 on this plugin
839+/// are intra-only and go through the same driver, so they very probably behave
840+/// the same way; "very probably" is not what a hard error should rest on, and
841+/// the GPU has been unavailable since the measurement was possible. They keep
842+/// the old one-buffer behaviour -- the picture coded before the next is
843+/// written, exactly as every seat did before -- until someone measures them.
844+fn enc_depth(codec: EncCodec) -> usize {
845+ match codec {
846+ EncCodec::H264 => ENC_DEPTH,
847+ EncCodec::Hevc | EncCodec::Av1 => 1,
848+ }
849+}
850+
829 /// Which codec an [`EncSession`] was opened for. Decided once, at the open, the851 /// Which codec an [`EncSession`] was opened for. Decided once, at the open, the
830 /// way the H.264 seat's dimensions are -- everything after it is one trait.852 /// way the H.264 seat's dimensions are -- everything after it is one trait.
831 #[derive(Clone, Copy, PartialEq, Eq)]853 #[derive(Clone, Copy, PartialEq, Eq)]
@@ -1003,7 +1025,8 @@ impl EncSession {
1003 };1025 };
1004 // Allocated at the aligned size, so the buffer's pitch is never smaller1026 // Allocated at the aligned size, so the buffer's pitch is never smaller
1005 // than a padded row.1027 // than a padded row.
1006- let frames: Vec<_> = (0..ENC_DEPTH)1028+ let depth = enc_depth(codec);
1029+ let frames: Vec<_> = (0..depth)
1007 .map(|_| alloc_nv12(&gbm, coded, false))1030 .map(|_| alloc_nv12(&gbm, coded, false))
1008 .collect::<Result<_, _>>()1031 .collect::<Result<_, _>>()
1009 .ok()?;1032 .ok()?;
@@ -1013,6 +1036,7 @@ impl EncSession {
1013 codec,1036 codec,
1014 _gbm: gbm,1037 _gbm: gbm,
1015 frames,1038 frames,
1039+ depth,
1016 slot: 0,1040 slot: 0,
1017 in_flight: 0,1041 in_flight: 0,
1018 layout,1042 layout,
@@ -1095,12 +1119,14 @@ impl EncSession {
1095 /// As [`EncSession::upload`].1119 /// As [`EncSession::upload`].
1096 unsafe fn encode(&mut self, src: &VhFrame, force_keyframe: bool) -> Result<(), String> {1120 unsafe fn encode(&mut self, src: &VhFrame, force_keyframe: bool) -> Result<(), String> {
1097 // The buffer this picture is written into is the one whose own access1121 // The buffer this picture is written into is the one whose own access
1098- // unit is already in hand -- `in_flight` is held below [`ENC_DEPTH`] at1122+ // unit is already in hand -- `in_flight` is held below `depth` at the
1099- // the end of every call, so the round-robin never catches the GPU up.1123+ // end of every call, so the round-robin never catches the GPU up. At
1124+ // depth 1 that is the picture just coded, which is the one-buffer
1125+ // behaviour every seat had before ([`enc_depth`]).
1100 let slot = self.slot;1126 let slot = self.slot;
1101 // SAFETY: caller contract, forwarded.1127 // SAFETY: caller contract, forwarded.
1102 unsafe { self.upload(src, slot)? };1128 unsafe { self.upload(src, slot)? };
1103- self.slot = (self.slot + 1) % ENC_DEPTH;1129+ self.slot = (self.slot + 1) % self.depth;
1104 let meta = FrameMetadata {1130 let meta = FrameMetadata {
1105 timestamp: self.timestamp,1131 timestamp: self.timestamp,
1106 layout: self.layout.clone(),1132 layout: self.layout.clone(),
@@ -1114,7 +1140,7 @@ impl EncSession {
1114 // ...and the picture before it is collected here rather than this one:1140 // ...and the picture before it is collected here rather than this one:
1115 // that is the whole overlap, the GPU coding what was just submitted1141 // that is the whole overlap, the GPU coding what was just submitted
1116 // while the caller decodes and interleaves the next picture.1142 // while the caller decodes and interleaves the next picture.
1117- while self.in_flight >= ENC_DEPTH {1143+ while self.in_flight >= self.depth {
1118 if !self.pull()? {1144 if !self.pull()? {
1119 // Blocking mode: a picture accepted and no coded unit for the1145 // Blocking mode: a picture accepted and no coded unit for the
1120 // one before it means the encoder is not answering, and the1146 // one before it means the encoder is not answering, and the
vendor/cros-codecs/src/video_frame/generic_dma_video_frame.rsmodified+7 -0
@@ -338,6 +338,13 @@ impl<'a> Drop for DmaMapping<'a> {
338 // reports a race when it was tried; the mfence pair either338 // reports a race when it was tried; the mfence pair either
339 // side is what orders the flushes, and the sixty-four339 // side is what orders the flushes, and the sixty-four
340 // redundant flushes per line were never what did.340 // redundant flushes per line were never what did.
341+ // A plane of no bytes has no line to flush, and the last-line
342+ // flush below would touch the byte after the mapping.
343+ // Upstream's byte loop flushed nothing at all for `0..0`,
344+ // and this keeps that.
345+ if *len == 0 {
346+ continue;
347+ }
341 let base = addr.as_ptr() as *const u8;348 let base = addr.as_ptr() as *const u8;
342 for offset in (0..*len).step_by(CACHE_LINE) {349 for offset in (0..*len).step_by(CACHE_LINE) {
343 _mm_clflush(base.offset(offset as isize));350 _mm_clflush(base.offset(offset as isize));