• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! This file contains a dummy backend whose only purpose is to let the decoder
6 //! run so we can test it in isolation.
7 
8 use std::cell::RefCell;
9 use std::rc::Rc;
10 
11 use crate::decoder::stateless::PoolLayer;
12 use crate::decoder::stateless::StatelessCodec;
13 use crate::decoder::stateless::StatelessDecoderBackend;
14 use crate::decoder::stateless::StatelessDecoderBackendPicture;
15 use crate::decoder::stateless::TryFormat;
16 use crate::decoder::DecodedHandle;
17 use crate::decoder::DynHandle;
18 use crate::decoder::FramePool;
19 use crate::decoder::MappableHandle;
20 use crate::decoder::StreamInfo;
21 use crate::DecodedFormat;
22 use crate::Resolution;
23 
24 #[derive(Default)]
25 pub struct BackendHandle(());
26 
27 impl MappableHandle for BackendHandle {
read(&mut self, _: &mut [u8]) -> anyhow::Result<()>28     fn read(&mut self, _: &mut [u8]) -> anyhow::Result<()> {
29         Ok(())
30     }
31 
image_size(&mut self) -> usize32     fn image_size(&mut self) -> usize {
33         1
34     }
35 }
36 
37 impl<'a> DynHandle for std::cell::RefMut<'a, BackendHandle> {
dyn_mappable_handle<'b>(&'b mut self) -> anyhow::Result<Box<dyn MappableHandle + 'b>>38     fn dyn_mappable_handle<'b>(&'b mut self) -> anyhow::Result<Box<dyn MappableHandle + 'b>> {
39         Ok(Box::<BackendHandle>::default())
40     }
41 }
42 
43 pub struct Handle {
44     pub handle: Rc<RefCell<BackendHandle>>,
45 }
46 
47 impl Clone for Handle {
clone(&self) -> Self48     fn clone(&self) -> Self {
49         Self { handle: Rc::clone(&self.handle) }
50     }
51 }
52 
53 impl DecodedHandle for Handle {
54     type Descriptor = ();
55 
coded_resolution(&self) -> Resolution56     fn coded_resolution(&self) -> Resolution {
57         Default::default()
58     }
59 
display_resolution(&self) -> Resolution60     fn display_resolution(&self) -> Resolution {
61         Default::default()
62     }
63 
timestamp(&self) -> u6464     fn timestamp(&self) -> u64 {
65         0
66     }
67 
dyn_picture<'a>(&'a self) -> Box<dyn DynHandle + 'a>68     fn dyn_picture<'a>(&'a self) -> Box<dyn DynHandle + 'a> {
69         Box::new(self.handle.borrow())
70     }
71 
sync(&self) -> anyhow::Result<()>72     fn sync(&self) -> anyhow::Result<()> {
73         Ok(())
74     }
75 
is_ready(&self) -> bool76     fn is_ready(&self) -> bool {
77         true
78     }
79 
resource(&self) -> std::cell::Ref<()>80     fn resource(&self) -> std::cell::Ref<()> {
81         std::cell::Ref::map(self.handle.borrow(), |h| &h.0)
82     }
83 }
84 
85 /// Dummy backend that can be used for any codec.
86 pub struct Backend {
87     stream_info: StreamInfo,
88 }
89 
90 impl Backend {
new() -> Self91     pub(crate) fn new() -> Self {
92         Self {
93             stream_info: StreamInfo {
94                 format: DecodedFormat::I420,
95                 min_num_frames: 4,
96                 coded_resolution: Resolution::from((320, 200)),
97                 display_resolution: Resolution::from((320, 200)),
98             },
99         }
100     }
101 }
102 
103 impl FramePool for Backend {
104     type Descriptor = ();
105 
coded_resolution(&self) -> Resolution106     fn coded_resolution(&self) -> Resolution {
107         Resolution::from((320, 200))
108     }
109 
set_coded_resolution(&mut self, _resolution: Resolution)110     fn set_coded_resolution(&mut self, _resolution: Resolution) {}
111 
add_frames(&mut self, _descriptors: Vec<Self::Descriptor>) -> Result<(), anyhow::Error>112     fn add_frames(&mut self, _descriptors: Vec<Self::Descriptor>) -> Result<(), anyhow::Error> {
113         Ok(())
114     }
115 
num_free_frames(&self) -> usize116     fn num_free_frames(&self) -> usize {
117         4
118     }
119 
num_managed_frames(&self) -> usize120     fn num_managed_frames(&self) -> usize {
121         4
122     }
123 
clear(&mut self)124     fn clear(&mut self) {}
125 }
126 
127 impl<Codec: StatelessCodec> StatelessDecoderBackendPicture<Codec> for Backend {
128     type Picture = ();
129 }
130 
131 impl<Codec: StatelessCodec> TryFormat<Codec> for Backend {
try_format(&mut self, _: &Codec::FormatInfo, _: DecodedFormat) -> anyhow::Result<()>132     fn try_format(&mut self, _: &Codec::FormatInfo, _: DecodedFormat) -> anyhow::Result<()> {
133         Ok(())
134     }
135 }
136 
137 impl StatelessDecoderBackend for Backend {
138     type Handle = Handle;
139 
140     type FramePool = Self;
141 
stream_info(&self) -> Option<&StreamInfo>142     fn stream_info(&self) -> Option<&StreamInfo> {
143         Some(&self.stream_info)
144     }
145 
frame_pool(&mut self, _: PoolLayer) -> Vec<&mut Self::FramePool>146     fn frame_pool(&mut self, _: PoolLayer) -> Vec<&mut Self::FramePool> {
147         vec![self]
148     }
149 }
150