Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync changes from mozilla-central gfx/wr #3844

Merged
merged 6 commits into from Jan 25, 2020
Next

Bug 1558926 - Part 1: Add data structures and pref for display item c…

  • Loading branch information
mikokm authored and moz-gfx committed Jan 25, 2020
commit 13be220819413c39808aaf805137fcefddd9a6f2
@@ -33,6 +33,10 @@ pub const MAX_BLUR_RADIUS: f32 = 300.;
/// events.
pub type ItemTag = (u64, u16);

/// An identifier used to refer to previously sent display items. Currently it
/// refers to individual display items, but this may change later.
pub type ItemKey = u16;

bitflags! {
#[repr(C)]
#[derive(Deserialize, MallocSizeOf, Serialize, PeekPoke)]
@@ -74,6 +78,8 @@ pub struct CommonItemProperties {
pub hit_info: Option<ItemTag>,
/// Various flags describing properties of this primitive.
pub flags: PrimitiveFlags,
/// The unique id of this display item.
pub item_key: Option<ItemKey>
}

impl CommonItemProperties {
@@ -88,6 +94,7 @@ impl CommonItemProperties {
clip_id: space_and_clip.clip_id,
hit_info: None,
flags: PrimitiveFlags::default(),
item_key: None,
}
}
}
@@ -0,0 +1,106 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::display_item::*;
use crate::display_list::*;

#[derive(Debug, Deserialize, PartialEq, Serialize)]
pub struct CachedDisplayItem {
item: DisplayItem,
data: Vec<u8>,
}

impl CachedDisplayItem {
pub fn item(&self) -> &DisplayItem {
&self.item
}

pub fn data_as_item_range<T>(&self) -> ItemRange<T> {
ItemRange::new(&self.data)
}
}

impl From<DisplayItemRef<'_, '_>> for CachedDisplayItem {
fn from (item_ref: DisplayItemRef) -> Self {
let item = item_ref.item();

match item {
DisplayItem::Text(..) => CachedDisplayItem {
item: *item,
data: item_ref.glyphs().bytes().to_vec(),
},
DisplayItem::Rectangle(..) |
DisplayItem::Image(..) => CachedDisplayItem {
item: *item,
data: Vec::new(),
},
_ => { unimplemented!("Unsupported display item type"); }
}
}
}

#[derive(Default, Deserialize, Serialize)]
pub struct DisplayItemCache {
items: Vec<Option<CachedDisplayItem>>
}

impl DisplayItemCache {
fn grow_if_needed(
&mut self,
capacity: usize
) {
if capacity > self.items.len() {
self.items.resize_with(capacity, || None::<CachedDisplayItem>);
// println!("Current cache size: {:?}",
// mem::size_of::<CachedDisplayItem>() * capacity);
}
}

pub fn add_item(
&mut self,
key: Option<ItemKey>,
item: DisplayItemRef
) {
let index = usize::from(key.expect("Cached item without key"));
self.items[index] = Some(CachedDisplayItem::from(item));
}

pub fn get_item(
&self,
key: ItemKey
) -> Option<&CachedDisplayItem> {
self.items[key as usize].as_ref()
}

pub fn update(
&mut self,
display_list: &BuiltDisplayList
) {
self.grow_if_needed(display_list.cache_size());

let mut iter = display_list.extra_data_iter();

loop {
let item = match iter.next() {
Some(item) => item,
None => break,
};

match item.item() {
DisplayItem::Rectangle(ref info) => {
self.add_item(info.common.item_key, item);
}
DisplayItem::Text(ref info) => {
self.add_item(info.common.item_key, item);
}
DisplayItem::Image(ref info) => {
self.add_item(info.common.item_key, item);
}
item @ _ => {
unimplemented!("Unexpected item in extra data: {:?}", item);
}
}
}
}
}
@@ -62,10 +62,21 @@ impl<'a, T> Default for ItemRange<'a, T> {
}

impl<'a, T> ItemRange<'a, T> {
pub fn new(bytes: &'a [u8]) -> Self {
Self {
bytes,
_boo: PhantomData
}
}

pub fn is_empty(&self) -> bool {
// Nothing more than space for a length (0).
self.bytes.len() <= mem::size_of::<usize>()
}

pub fn bytes(&self) -> &[u8] {
&self.bytes
}
}

impl<'a, T: Default> ItemRange<'a, T> {
@@ -44,6 +44,7 @@ mod api;
pub mod channel;
mod color;
mod display_item;
mod display_item_cache;
mod display_list;
mod font;
mod gradient_builder;
@@ -53,6 +54,7 @@ pub mod units;
pub use crate::api::*;
pub use crate::color::*;
pub use crate::display_item::*;
pub use crate::display_item_cache::DisplayItemCache;
pub use crate::display_list::*;
pub use crate::font::*;
pub use crate::gradient_builder::*;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.