| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | use super::block::PlaneBuf; |
| 21 | use super::TxParams; |
| 22 | use super::{CflSrc, FrameCtx, Reach, RefPix, SharedRefs, WithRef, exec_intra, exec_mc}; |
| 23 | use std::borrow::Cow; |
| 24 | use std::cell::{Cell, RefCell}; |
| 25 | use crate::encode::Picture; |
| 26 | use ec_core::Result; |
| 27 | use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock, PoisonError, mpsc}; |
| 28 | use crate::par::Batch; |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | pub(super) enum Residual { |
| 34 | |
| 35 | Zero, |
| 36 | |
| 37 | Ready(usize, usize), |
| 38 | |
| 39 | |
| 40 | Tx(TxParams, usize, usize), |
| 41 | } |
| 42 | |
| 43 | impl Residual { |
| 44 | |
| 45 | |
| 46 | pub(super) fn ready(residual: &[i32]) -> Self { |
| 47 | if residual.is_empty() || std::ptr::eq(residual.as_ptr(), super::ZERO_RESIDUAL.as_ptr()) { |
| 48 | Self::Zero |
| 49 | } else { |
| 50 | let off = push_coeffs(residual); |
| 51 | Self::Ready(off, residual.len()) |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | pub(super) fn tx(tx: TxParams, grid: &[i32]) -> Self { |
| 57 | if super::is_zero_grid(grid) { |
| 58 | Self::Zero |
| 59 | } else { |
| 60 | let off = push_coeffs(grid); |
| 61 | Self::Tx(tx, off, grid.len()) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | fn resolve<'a>(self, arena: &'a [i32], scratch: &'a mut Vec<i32>) -> &'a [i32] { |
| 67 | match self { |
| 68 | Self::Zero => &[], |
| 69 | Self::Ready(off, len) => &arena[off..][..len], |
| 70 | Self::Tx(tx, off, len) => { |
| 71 | *scratch = tx.run(&arena[off..][..len]); |
| 72 | scratch |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | fn push_coeffs(v: &[i32]) -> usize { |
| 80 | QUEUE.with(|q| { |
| 81 | let mut q = q.borrow_mut(); |
| 82 | let off = q.coeffs.len(); |
| 83 | q.coeffs.extend_from_slice(v); |
| 84 | off |
| 85 | }) |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
| 90 | #[derive(Default)] |
| 91 | struct Queue { |
| 92 | ops: Vec<ReconOp>, |
| 93 | coeffs: Vec<i32>, |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | pub(super) type PredFn = Box< |
| 99 | dyn FnOnce( |
| 100 | &mut PlaneBuf<'static>, |
| 101 | &mut PlaneBuf<'static>, |
| 102 | &mut PlaneBuf<'static>, |
| 103 | &RefPix<'_>, |
| 104 | &FrameCtx, |
| 105 | &mut [Vec<u16>; 3], |
| 106 | ) + Send, |
| 107 | >; |
| 108 | |
| 109 | |
| 110 | pub(super) enum PredSrc { |
| 111 | |
| 112 | Owned(Vec<u16>), |
| 113 | |
| 114 | |
| 115 | Cur { plane: usize, off: usize, src: usize }, |
| 116 | } |
| 117 | |
| 118 | |
| 119 | pub(super) enum ReconOp { |
| 120 | |
| 121 | Pred(PredFn), |
| 122 | Intra { |
| 123 | plane: usize, |
| 124 | x: usize, |
| 125 | oy: usize, |
| 126 | bw: usize, |
| 127 | bh: usize, |
| 128 | rect: bool, |
| 129 | mode: usize, |
| 130 | angle_delta: i32, |
| 131 | reach: Reach, |
| 132 | residual: Residual, |
| 133 | cfl: Option<(i32, CflSrc)>, |
| 134 | filter_intra: Option<usize>, |
| 135 | smooth_neighbor: bool, |
| 136 | pred: Option<Vec<u16>>, |
| 137 | }, |
| 138 | Mc { |
| 139 | plane: usize, |
| 140 | x: usize, |
| 141 | oy: usize, |
| 142 | stride: usize, |
| 143 | w: usize, |
| 144 | h: usize, |
| 145 | prediction: PredSrc, |
| 146 | residual: Residual, |
| 147 | }, |
| 148 | } |
| 149 | |
| 150 | thread_local! { |
| 151 | |
| 152 | static ACTIVE: Cell<bool> = const { Cell::new(false) }; |
| 153 | |
| 154 | static GUARD: RefCell<Option<WaveGuard>> = const { RefCell::new(None) }; |
| 155 | |
| 156 | static CURRENT: Cell<Option<(usize, usize)>> = const { Cell::new(None) }; |
| 157 | |
| 158 | static QUEUE: RefCell<Queue> = const { RefCell::new(Queue { ops: Vec::new(), coeffs: Vec::new() }) }; |
| 159 | |
| 160 | |
| 161 | static POOL: RefCell<Vec<[Vec<u16>; 3]>> = const { RefCell::new(Vec::new()) }; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | const POOL_SETS: usize = 4; |
| 166 | |
| 167 | |
| 168 | |
| 169 | pub(super) struct TileScope; |
| 170 | |
| 171 | impl Drop for TileScope { |
| 172 | fn drop(&mut self) { |
| 173 | let guard = GUARD.with(|g| g.borrow_mut().take()); |
| 174 | drop(guard); |
| 175 | ACTIVE.with(|a| a.set(false)); |
| 176 | CURRENT.with(|c| c.set(None)); |
| 177 | QUEUE.with(|q| { |
| 178 | let mut q = q.borrow_mut(); |
| 179 | q.ops.clear(); |
| 180 | q.coeffs.clear(); |
| 181 | }); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | |
| 186 | pub(super) fn active() -> bool { |
| 187 | ACTIVE.with(Cell::get) |
| 188 | } |
| 189 | |
| 190 | |
| 191 | pub(super) fn push(op: ReconOp) { |
| 192 | QUEUE.with(|q| q.borrow_mut().ops.push(op)); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | #[derive(Clone, Copy)] |
| 197 | struct Grid { |
| 198 | |
| 199 | ux: [usize; 3], |
| 200 | uy: [usize; 3], |
| 201 | |
| 202 | row0: usize, |
| 203 | col0: usize, |
| 204 | |
| 205 | cols: usize, |
| 206 | |
| 207 | rows: usize, |
| 208 | } |
| 209 | |
| 210 | |
| 211 | struct Job { |
| 212 | row: usize, |
| 213 | col: usize, |
| 214 | ops: Vec<ReconOp>, |
| 215 | |
| 216 | coeffs: Vec<i32>, |
| 217 | } |
| 218 | |
| 219 | |
| 220 | struct Shared { |
| 221 | |
| 222 | done: Vec<usize>, |
| 223 | |
| 224 | |
| 225 | lines: Vec<[Vec<u16>; 3]>, |
| 226 | |
| 227 | abort: bool, |
| 228 | } |
| 229 | |
| 230 | struct Link { |
| 231 | state: Mutex<Shared>, |
| 232 | moved: Condvar, |
| 233 | } |
| 234 | |
| 235 | impl Link { |
| 236 | fn lock(&self) -> MutexGuard<'_, Shared> { |
| 237 | self.state.lock().unwrap_or_else(PoisonError::into_inner) |
| 238 | } |
| 239 | |
| 240 | fn abort(&self) { |
| 241 | self.lock().abort = true; |
| 242 | self.moved.notify_all(); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | |
| 247 | struct Out { |
| 248 | planes: [PlaneBuf<'static>; 3], |
| 249 | corrupt: Option<&'static str>, |
| 250 | |
| 251 | err: Option<ec_core::Error>, |
| 252 | } |
| 253 | |
| 254 | |
| 255 | struct AbortOnPanic(Arc<Link>); |
| 256 | |
| 257 | impl Drop for AbortOnPanic { |
| 258 | fn drop(&mut self) { |
| 259 | if std::thread::panicking() { |
| 260 | self.0.abort(); |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | struct Worker { |
| 266 | link: Arc<Link>, |
| 267 | grid: Grid, |
| 268 | planes: [PlaneBuf<'static>; 3], |
| 269 | fctx: FrameCtx, |
| 270 | |
| 271 | |
| 272 | refs: Option<SharedRefs>, |
| 273 | |
| 274 | cur: [Vec<u16>; 3], |
| 275 | } |
| 276 | |
| 277 | impl Worker { |
| 278 | fn run(mut self, jobs: mpsc::Receiver<Job>) -> Out { |
| 279 | let _abort = AbortOnPanic(Arc::clone(&self.link)); |
| 280 | let shared = self.refs.take(); |
| 281 | let pics: OnceLock<[Option<Arc<Picture>>; 8]> = OnceLock::new(); |
| 282 | let wait = || -> Result<[Option<&Picture>; 8]> { |
| 283 | let Some(shared) = &shared else { |
| 284 | return Ok([None; 8]); |
| 285 | }; |
| 286 | if pics.get().is_none() { |
| 287 | let _ = pics.set((shared.wait)()?); |
| 288 | } |
| 289 | let got = pics.get().expect("the pictures were just set"); |
| 290 | Ok(std::array::from_fn(|i| got[i].as_deref())) |
| 291 | }; |
| 292 | let refpix = RefPix::lazy(shared.as_ref().map_or([None; 8], |s| s.dims), &wait, None); |
| 293 | while let Ok(job) = jobs.recv() { |
| 294 | if !self.job(job, &refpix) { |
| 295 | break; |
| 296 | } |
| 297 | } |
| 298 | Out { |
| 299 | planes: self.planes, |
| 300 | corrupt: self.fctx.corrupt.get(), |
| 301 | err: refpix.take_err().err(), |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | |
| 306 | fn job(&mut self, job: Job, refpix: &RefPix<'_>) -> bool { |
| 307 | crate::fault::point("recon"); |
| 308 | let Job { row, col, ops, coeffs } = job; |
| 309 | let mut scratch = Vec::new(); |
| 310 | if row > 0 && !self.halo(row, col) { |
| 311 | return false; |
| 312 | } |
| 313 | let [y, u, v] = &mut self.planes; |
| 314 | for op in ops { |
| 315 | match op { |
| 316 | ReconOp::Pred(build) => build(y, u, v, refpix, &self.fctx, &mut self.cur), |
| 317 | ReconOp::Intra { |
| 318 | plane, |
| 319 | x, |
| 320 | oy, |
| 321 | bw, |
| 322 | bh, |
| 323 | rect, |
| 324 | mode, |
| 325 | angle_delta, |
| 326 | reach, |
| 327 | residual, |
| 328 | cfl, |
| 329 | filter_intra, |
| 330 | smooth_neighbor, |
| 331 | pred, |
| 332 | } => { |
| 333 | let res = residual.resolve(&coeffs, &mut scratch); |
| 334 | exec_intra( |
| 335 | y, |
| 336 | u, |
| 337 | v, |
| 338 | plane, |
| 339 | x, |
| 340 | oy, |
| 341 | bw, |
| 342 | bh, |
| 343 | rect, |
| 344 | mode, |
| 345 | angle_delta, |
| 346 | reach, |
| 347 | res, |
| 348 | cfl, |
| 349 | filter_intra, |
| 350 | smooth_neighbor, |
| 351 | pred, |
| 352 | &self.fctx, |
| 353 | ); |
| 354 | } |
| 355 | ReconOp::Mc { |
| 356 | plane, |
| 357 | x, |
| 358 | oy, |
| 359 | stride, |
| 360 | w, |
| 361 | h, |
| 362 | prediction, |
| 363 | residual, |
| 364 | } => { |
| 365 | let res = residual.resolve(&coeffs, &mut scratch); |
| 366 | match prediction { |
| 367 | PredSrc::Owned(p) => { |
| 368 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &p, res, &self.fctx); |
| 369 | } |
| 370 | PredSrc::Cur { plane: sp, off, src } => { |
| 371 | let block = &self.cur[sp][off..]; |
| 372 | if src == stride { |
| 373 | exec_mc(y, u, v, plane, x, oy, stride, w, h, block, res, &self.fctx); |
| 374 | } else { |
| 375 | |
| 376 | |
| 377 | let mut window = Vec::with_capacity(w * h); |
| 378 | for row in 0..h { |
| 379 | window.extend_from_slice(&block[row * src..][..w]); |
| 380 | } |
| 381 | exec_mc(y, u, v, plane, x, oy, stride, w, h, &window, res, &self.fctx); |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | self.publish(row, col); |
| 389 | true |
| 390 | } |
| 391 | |
| 392 | |
| 393 | |
| 394 | fn halo(&mut self, row: usize, col: usize) -> bool { |
| 395 | let g = self.grid; |
| 396 | let need = (col + 2).min(g.cols); |
| 397 | let mut s = self.link.lock(); |
| 398 | while s.done[row - 1] < need && !s.abort { |
| 399 | s = self |
| 400 | .link |
| 401 | .moved |
| 402 | .wait(s) |
| 403 | .unwrap_or_else(PoisonError::into_inner); |
| 404 | } |
| 405 | if s.abort { |
| 406 | return false; |
| 407 | } |
| 408 | for p in 0..3 { |
| 409 | let w = self.planes[p].width; |
| 410 | if w == 0 { |
| 411 | continue; |
| 412 | } |
| 413 | let halo_row = (g.row0 + row) * g.uy[p] - 1; |
| 414 | let c0 = (g.col0 + col) * g.ux[p]; |
| 415 | let from = c0.saturating_sub(1); |
| 416 | let to = (c0 + 2 * g.ux[p]).min(w); |
| 417 | self.planes[p].data.to_mut()[halo_row * w + from..halo_row * w + to] |
| 418 | .copy_from_slice(&s.lines[row - 1][p][from..to]); |
| 419 | } |
| 420 | true |
| 421 | } |
| 422 | |
| 423 | |
| 424 | |
| 425 | fn publish(&self, row: usize, col: usize) { |
| 426 | let g = self.grid; |
| 427 | let mut s = self.link.lock(); |
| 428 | for p in 0..3 { |
| 429 | let w = self.planes[p].width; |
| 430 | if w == 0 { |
| 431 | continue; |
| 432 | } |
| 433 | let h = self.planes[p].height; |
| 434 | let last = ((g.row0 + row + 1) * g.uy[p]).min(h).saturating_sub(1); |
| 435 | let c0 = (g.col0 + col) * g.ux[p]; |
| 436 | let end = (c0 + g.ux[p]).min(w); |
| 437 | s.lines[row][p][c0..end] |
| 438 | .copy_from_slice(&self.planes[p].data[last * w + c0..last * w + end]); |
| 439 | } |
| 440 | s.done[row] = col + 1; |
| 441 | drop(s); |
| 442 | self.link.moved.notify_all(); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | pub(super) struct WaveGuard { |
| 449 | link: Arc<Link>, |
| 450 | grid: Grid, |
| 451 | jobs: Vec<mpsc::Sender<Job>>, |
| 452 | |
| 453 | |
| 454 | batch: Option<Batch>, |
| 455 | outs: mpsc::Receiver<(usize, Out)>, |
| 456 | } |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | pub(super) fn install( |
| 462 | fctx: &FrameCtx, |
| 463 | refpix: &RefPix<'_>, |
| 464 | planes: [&PlaneBuf<'static>; 3], |
| 465 | unit: usize, |
| 466 | span: (usize, usize, usize, usize), |
| 467 | ) { |
| 468 | let (row0, rows, col0, cols) = span; |
| 469 | let workers = crate::par::recon_threads().min(rows); |
| 470 | if workers < 2 || cols == 0 { |
| 471 | return; |
| 472 | } |
| 473 | let (ss_x, ss_y) = (super::ss_x(fctx), super::ss_y(fctx)); |
| 474 | let grid = Grid { |
| 475 | ux: [unit, unit >> ss_x, unit >> ss_x], |
| 476 | uy: [unit, unit >> ss_y, unit >> ss_y], |
| 477 | row0, |
| 478 | col0, |
| 479 | cols, |
| 480 | rows, |
| 481 | }; |
| 482 | let link = Arc::new(Link { |
| 483 | state: Mutex::new(Shared { |
| 484 | done: vec![0; rows], |
| 485 | lines: (0..rows) |
| 486 | .map(|_| [0, 1, 2].map(|p| vec![0u16; planes[p].width])) |
| 487 | .collect(), |
| 488 | abort: false, |
| 489 | }), |
| 490 | moved: Condvar::new(), |
| 491 | }); |
| 492 | let mut jobs = Vec::with_capacity(workers); |
| 493 | let batch = Batch::new("ec-av1-recon"); |
| 494 | let (out_tx, outs) = mpsc::channel(); |
| 495 | for w in 0..workers { |
| 496 | let mut own = take_planes(planes); |
| 497 | for rr in (w..rows).step_by(workers) { |
| 498 | for p in 0..3 { |
| 499 | let src = planes[p]; |
| 500 | let (top, end, x0, x1) = strip(&grid, p, row0 + rr, src); |
| 501 | for y in top..end { |
| 502 | own[p].data.to_mut()[y * src.width + x0..y * src.width + x1] |
| 503 | .copy_from_slice(&src.data[y * src.width + x0..y * src.width + x1]); |
| 504 | } |
| 505 | } |
| 506 | } |
| 507 | let worker = Worker { |
| 508 | link: Arc::clone(&link), |
| 509 | grid, |
| 510 | planes: own, |
| 511 | fctx: recon_ctx(fctx), |
| 512 | refs: refpix.shared(), |
| 513 | cur: Default::default(), |
| 514 | }; |
| 515 | let (tx, rx) = mpsc::channel(); |
| 516 | jobs.push(tx); |
| 517 | let out_tx = out_tx.clone(); |
| 518 | batch.submit(move || { |
| 519 | let out = worker.run(rx); |
| 520 | let _ = out_tx.send((w, out)); |
| 521 | }); |
| 522 | } |
| 523 | CURRENT.with(|c| c.set(None)); |
| 524 | ACTIVE.with(|a| a.set(true)); |
| 525 | let guard = WaveGuard { |
| 526 | link, |
| 527 | grid, |
| 528 | jobs, |
| 529 | batch: Some(batch), |
| 530 | outs, |
| 531 | }; |
| 532 | GUARD.with(|g| *g.borrow_mut() = Some(guard)); |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | pub(super) fn begin_sb(row: usize, col: usize) { |
| 538 | if !active() { |
| 539 | return; |
| 540 | } |
| 541 | if let Some((r, c)) = CURRENT.with(|cur| cur.replace(Some((row, col)))) { |
| 542 | send(r, c); |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | |
| 547 | fn send(row: usize, col: usize) { |
| 548 | let (ops, coeffs) = QUEUE.with(|q| { |
| 549 | let mut q = q.borrow_mut(); |
| 550 | let next = Queue { |
| 551 | ops: Vec::with_capacity(q.ops.len()), |
| 552 | coeffs: Vec::with_capacity(q.coeffs.len()), |
| 553 | }; |
| 554 | let Queue { ops, coeffs } = std::mem::replace(&mut *q, next); |
| 555 | (ops, coeffs) |
| 556 | }); |
| 557 | GUARD.with(|g| { |
| 558 | if let Some(guard) = g.borrow().as_ref() { |
| 559 | |
| 560 | |
| 561 | let _ = guard.jobs[row % guard.jobs.len()].send(Job { row, col, ops, coeffs }); |
| 562 | } |
| 563 | }); |
| 564 | } |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | pub(super) fn finish_tile( |
| 572 | fctx: &FrameCtx, |
| 573 | refpix: &RefPix<'_>, |
| 574 | planes: [&mut PlaneBuf<'static>; 3], |
| 575 | ) { |
| 576 | if !active() { |
| 577 | return; |
| 578 | } |
| 579 | if let Some((row, col)) = CURRENT.with(|c| c.take()) { |
| 580 | send(row, col); |
| 581 | } |
| 582 | ACTIVE.with(|a| a.set(false)); |
| 583 | let guard = GUARD.with(|g| g.borrow_mut().take()); |
| 584 | if let Some(guard) = guard { |
| 585 | guard.finish(fctx, refpix, planes); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | impl WaveGuard { |
| 590 | |
| 591 | |
| 592 | pub(super) fn finish( |
| 593 | mut self, |
| 594 | fctx: &FrameCtx, |
| 595 | refpix: &RefPix<'_>, |
| 596 | mut planes: [&mut PlaneBuf<'static>; 3], |
| 597 | ) { |
| 598 | |
| 599 | self.jobs.clear(); |
| 600 | |
| 601 | drop(self.batch.take()); |
| 602 | let mut outs: Vec<(usize, Out)> = self.outs.try_iter().collect(); |
| 603 | outs.sort_by_key(|(w, _)| *w); |
| 604 | let nw = outs.len(); |
| 605 | let g = self.grid; |
| 606 | for (w, out) in outs { |
| 607 | for rr in (w..g.rows).step_by(nw) { |
| 608 | for (p, dst) in planes.iter_mut().enumerate() { |
| 609 | let width = dst.width; |
| 610 | let (top, end, x0, x1) = strip(&g, p, g.row0 + rr, dst); |
| 611 | for y in top..end { |
| 612 | dst.data.to_mut()[y * width + x0..y * width + x1] |
| 613 | .copy_from_slice(&out.planes[p].data[y * width + x0..y * width + x1]); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | if let Some(why) = out.corrupt { |
| 618 | fctx.flag_corrupt(why); |
| 619 | } |
| 620 | if let Some(e) = out.err { |
| 621 | refpix.park_err(e); |
| 622 | } |
| 623 | let [a, b, c] = out.planes; |
| 624 | give_planes([a.data.into_owned(), b.data.into_owned(), c.data.into_owned()]); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | impl Drop for WaveGuard { |
| 630 | fn drop(&mut self) { |
| 631 | |
| 632 | self.link.abort(); |
| 633 | self.jobs.clear(); |
| 634 | |
| 635 | if let Some(batch) = self.batch.take() { |
| 636 | if std::thread::panicking() { |
| 637 | std::mem::forget(batch); |
| 638 | } else { |
| 639 | drop(batch); |
| 640 | } |
| 641 | } |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | |
| 646 | |
| 647 | fn strip(g: &Grid, p: usize, unit_row: usize, plane: &PlaneBuf<'static>) -> (usize, usize, usize, usize) { |
| 648 | let top = unit_row * g.uy[p]; |
| 649 | let end = ((unit_row + 1) * g.uy[p]).min(plane.height); |
| 650 | let x0 = (g.col0 * g.ux[p]).min(plane.width); |
| 651 | let x1 = ((g.col0 + g.cols) * g.ux[p]).min(plane.width); |
| 652 | if top >= end { |
| 653 | (0, 0, 0, 0) |
| 654 | } else { |
| 655 | (top, end, x0, x1) |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | fn take_planes(planes: [&PlaneBuf<'static>; 3]) -> [PlaneBuf<'static>; 3] { |
| 664 | let lens = planes.map(|p| p.data.len()); |
| 665 | let reuse = POOL.with(|pool| { |
| 666 | let mut pool = pool.borrow_mut(); |
| 667 | let at = pool |
| 668 | .iter() |
| 669 | .position(|set| [0, 1, 2].iter().all(|&p| set[p].len() == lens[p]))?; |
| 670 | Some(pool.swap_remove(at)) |
| 671 | }); |
| 672 | let bufs = reuse.unwrap_or_else(|| lens.map(|n| vec![0u16; n])); |
| 673 | let [a, b, c] = bufs; |
| 674 | [ |
| 675 | blank_like(planes[0], a), |
| 676 | blank_like(planes[1], b), |
| 677 | blank_like(planes[2], c), |
| 678 | ] |
| 679 | } |
| 680 | |
| 681 | |
| 682 | fn give_planes(set: [Vec<u16>; 3]) { |
| 683 | POOL.with(|pool| { |
| 684 | let mut pool = pool.borrow_mut(); |
| 685 | if pool.len() < POOL_SETS { |
| 686 | pool.push(set); |
| 687 | } |
| 688 | }); |
| 689 | } |
| 690 | |
| 691 | |
| 692 | fn blank_like(src: &PlaneBuf<'static>, data: Vec<u16>) -> PlaneBuf<'static> { |
| 693 | PlaneBuf { |
| 694 | data: Cow::Owned(data), |
| 695 | width: src.width, |
| 696 | height: src.height, |
| 697 | true_width: src.true_width, |
| 698 | true_height: src.true_height, |
| 699 | tile_x0: src.tile_x0, |
| 700 | tile_y0: src.tile_y0, |
| 701 | tile_x1: src.tile_x1, |
| 702 | tile_y1: src.tile_y1, |
| 703 | plane: src.plane, |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | |
| 708 | fn recon_ctx(fctx: &FrameCtx) -> FrameCtx { |
| 709 | let out = super::filter_ctx_copy(fctx); |
| 710 | super::set_enable_edge_filter(fctx.enable_edge_filter.with(Cell::get), &out); |
| 711 | out |
| 712 | } |