forma/buffer.rs
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use std::cell::RefCell;
use std::rc::{Rc, Weak};
pub use surpass::layout;
use surpass::painter::{CachedTile, Color};
use layout::{Flusher, Layout};
use crate::small_bit_set::SmallBitSet;
/// A short-lived description of the buffer being rendered into for the current frame.
///
/// # Examples
///
/// ```
/// # use forma::buffer::{BufferBuilder, layout::LinearLayout};
/// let width = 100;
/// let height = 100;
/// let mut buffer = vec![0; 100 * 4 * 100];
///
/// let _buffer = BufferBuilder::new(
/// &mut buffer,
/// &mut LinearLayout::new(width, width * 4, height),
/// ).build();
/// ```
#[derive(Debug)]
pub struct Buffer<'b, 'l, L: Layout> {
pub(crate) buffer: &'b mut [u8],
pub(crate) layout: &'l mut L,
pub(crate) layer_cache: Option<BufferLayerCache>,
pub(crate) flusher: Option<Box<dyn Flusher>>,
}
/// A builder for the [`Buffer`].
///
/// # Examples
///
/// ```
/// # use forma::buffer::{BufferBuilder, layout::LinearLayout};
/// let width = 100;
/// let height = 100;
/// let mut buffer = vec![0; 100 * 4 * 100];
/// let mut layout = LinearLayout::new(width, width * 4, height);
/// let _buffer = BufferBuilder::new(&mut buffer, &mut layout).build();
/// ```
#[derive(Debug)]
pub struct BufferBuilder<'b, 'l, L: Layout> {
buffer: Buffer<'b, 'l, L>,
}
impl<'b, 'l, L: Layout> BufferBuilder<'b, 'l, L> {
#[inline]
pub fn new(buffer: &'b mut [u8], layout: &'l mut L) -> Self {
Self { buffer: Buffer { buffer, layout, layer_cache: None, flusher: None } }
}
#[inline]
pub fn layer_cache(mut self, layer_cache: BufferLayerCache) -> Self {
self.buffer.layer_cache = Some(layer_cache);
self
}
#[inline]
pub fn flusher(mut self, flusher: Box<dyn Flusher>) -> Self {
self.buffer.flusher = Some(flusher);
self
}
#[inline]
pub fn build(self) -> Buffer<'b, 'l, L> {
self.buffer
}
}
#[derive(Debug)]
struct IdDropper {
id: u8,
buffers_with_caches: Weak<RefCell<SmallBitSet>>,
}
impl Drop for IdDropper {
fn drop(&mut self) {
if let Some(buffers_with_caches) = Weak::upgrade(&self.buffers_with_caches) {
buffers_with_caches.borrow_mut().remove(self.id);
}
}
}
#[derive(Debug)]
pub struct CacheInner {
pub clear_color: Option<Color>,
pub tiles: Vec<CachedTile>,
pub width: Option<usize>,
pub height: Option<usize>,
_id_dropper: IdDropper,
}
/// A per-[`Buffer`] cache that enables forma to skip rendering to buffer
/// regions that have not changed.
///
/// # Examples
///
/// ```
/// # use forma::{
/// # buffer::{BufferBuilder, layout::LinearLayout},
/// # Color, Composition, CpuRenderer, RGBA,
/// # };
/// let mut buffer = vec![0; 4];
///
/// let mut composition = Composition::new();
/// let mut renderer = CpuRenderer::new();
/// let layer_cache = renderer.create_buffer_layer_cache().unwrap();
///
/// renderer.render(
/// &mut composition,
/// &mut BufferBuilder::new(&mut buffer, &mut LinearLayout::new(1, 1 * 4, 1))
/// .layer_cache(layer_cache.clone())
/// .build(),
/// RGBA,
/// Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 },
/// None,
/// );
///
/// // Rendered white on first frame.
/// assert_eq!(buffer, [255; 4]);
///
/// // Reset buffer manually.
/// buffer = vec![0; 4];
///
/// renderer.render(
/// &mut composition,
/// &mut BufferBuilder::new(&mut buffer, &mut LinearLayout::new(1, 1 * 4, 1))
/// .layer_cache(layer_cache.clone())
/// .build(),
/// RGBA,
/// Color { r: 1.0, g: 1.0, b: 1.0, a: 1.0 },
/// None,
/// );
///
/// // Skipped rendering on second frame since nothing changed.
/// assert_eq!(buffer, [0; 4]);
/// ```
#[derive(Clone, Debug)]
pub struct BufferLayerCache {
pub(crate) id: u8,
pub(crate) cache: Rc<RefCell<CacheInner>>,
}
impl BufferLayerCache {
pub(crate) fn new(id: u8, buffers_with_caches: Weak<RefCell<SmallBitSet>>) -> Self {
Self {
id,
cache: Rc::new(RefCell::new(CacheInner {
clear_color: None,
tiles: Vec::new(),
width: None,
height: None,
_id_dropper: IdDropper { id, buffers_with_caches },
})),
}
}
#[inline]
pub fn clear(&self) {
let mut cache = self.cache.borrow_mut();
cache.clear_color = None;
cache.tiles.fill(CachedTile::default());
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::mem;
fn new_cache(bit_set: &Rc<RefCell<SmallBitSet>>) -> BufferLayerCache {
bit_set
.borrow_mut()
.first_empty_slot()
.map(|id| BufferLayerCache::new(id, Rc::downgrade(bit_set)))
.unwrap()
}
#[test]
fn clone_and_drop() {
let bit_set = Rc::new(RefCell::new(SmallBitSet::default()));
let cache0 = new_cache(&bit_set);
let cache1 = new_cache(&bit_set);
let cache2 = new_cache(&bit_set);
assert!(bit_set.borrow().contains(&0));
assert!(bit_set.borrow().contains(&1));
assert!(bit_set.borrow().contains(&2));
mem::drop(cache0.clone());
mem::drop(cache1.clone());
mem::drop(cache2.clone());
assert!(bit_set.borrow().contains(&0));
assert!(bit_set.borrow().contains(&1));
assert!(bit_set.borrow().contains(&2));
mem::drop(cache1);
assert!(bit_set.borrow().contains(&0));
assert!(!bit_set.borrow().contains(&1));
assert!(bit_set.borrow().contains(&2));
let cache1 = new_cache(&bit_set);
assert!(bit_set.borrow().contains(&0));
assert!(bit_set.borrow().contains(&1));
assert!(bit_set.borrow().contains(&2));
mem::drop(cache0);
mem::drop(cache1);
mem::drop(cache2);
assert!(!bit_set.borrow().contains(&0));
assert!(!bit_set.borrow().contains(&1));
assert!(!bit_set.borrow().contains(&2));
}
}