use api::{
ColorF, ColorU, RasterSpace,
LineOrientation, LineStyle, PremultipliedColorF, Shadow,
};
use api::units::*;
use euclid::Scale;
use crate::gpu_types::ImageBrushPrimitiveData;
use crate::render_task::{RenderTask, RenderTaskKind};
use crate::render_task_cache::{RenderTaskCacheKey, RenderTaskCacheKeyKind, RenderTaskParent};
use crate::render_task_graph::RenderTaskId;
use crate::renderer::GpuBufferAddress;
use crate::scene_building::{CreateShadow, IsVisible};
use crate::frame_builder::{FrameBuildingContext, FrameBuildingState};
use crate::intern;
use crate::internal_types::LayoutPrimitiveInfo;
use crate::prim_store::{
PrimKey, PrimTemplate, PrimTemplateCommonData,
InternablePrimitive, PrimitiveStore,
};
use crate::prim_store::PrimitiveKind;
use crate::spatial_tree::SpatialNodeIndex;
use crate::util::clamp_to_scale_factor;
pub const MAX_LINE_DECORATION_RESOLUTION: u32 = 4096;
#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "capture", derive(Serialize))]
pub struct LineDecorationScratch {
pub task_id: RenderTaskId,
pub gpu_address: GpuBufferAddress,
}
#[derive(Clone, Debug, Hash, MallocSizeOf, PartialEq, Eq)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct LineDecorationCacheKey {
pub style: LineStyle,
pub orientation: LineOrientation,
pub wavy_line_thickness: Au,
pub size: LayoutSizeAu,
}
#[derive(Clone, Debug, Hash, MallocSizeOf, PartialEq, Eq)]
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
pub struct LineDecoration {
pub style: LineStyle,
pub orientation: LineOrientation,
pub wavy_line_thickness: Au,
pub color: ColorU,
}
pub type LineDecorationKey = PrimKey<LineDecoration>;
impl LineDecorationKey {
pub fn new(
info: &LayoutPrimitiveInfo,
line_dec: LineDecoration,
) -> Self {
LineDecorationKey {
common: info.into(),
kind: line_dec,
}
}
}
impl intern::InternDebug for LineDecorationKey {}
#[cfg_attr(feature = "capture", derive(Serialize))]
#[cfg_attr(feature = "replay", derive(Deserialize))]
#[derive(MallocSizeOf)]
pub struct LineDecorationData {
pub style: LineStyle,
pub orientation: LineOrientation,
pub wavy_line_thickness: Au,
pub color: ColorF,
}
impl LineDecorationData {
pub fn prepare(
&self,
prim_size: LayoutSize,
prim_spatial_node_index: SpatialNodeIndex,
frame_context: &FrameBuildingContext,
frame_state: &mut FrameBuildingState,
) -> (RenderTaskId, GpuBufferAddress) {
let cache_key = get_line_decoration_size(
&prim_size,
self.orientation,
self.style,
self.wavy_line_thickness.to_f32_px(),
).map(|size| LineDecorationCacheKey {
style: self.style,
orientation: self.orientation,
wavy_line_thickness: self.wavy_line_thickness,
size: size.to_au(),
});
let mut writer = frame_state.frame_gpu_data.f32.write_blocks(3);
match cache_key.as_ref() {
Some(cache_key) => {
writer.push(&ImageBrushPrimitiveData {
color: self.color.premultiplied(),
background_color: PremultipliedColorF::WHITE,
stretch_size: LayoutSize::new(
cache_key.size.width.to_f32_px(),
cache_key.size.height.to_f32_px(),
),
});
}
None => {
writer.push_one(self.color.premultiplied());
}
}
let gpu_address = writer.finish();
let task_id = match cache_key {
Some(cache_key) => self.allocate_render_task(
cache_key,
prim_spatial_node_index,
frame_context,
frame_state,
),
None => RenderTaskId::INVALID,
};
(task_id, gpu_address)
}
fn allocate_render_task(
&self,
cache_key: LineDecorationCacheKey,
prim_spatial_node_index: SpatialNodeIndex,
frame_context: &FrameBuildingContext,
frame_state: &mut FrameBuildingState,
) -> RenderTaskId {
let scale = frame_context
.spatial_tree
.get_world_transform(prim_spatial_node_index)
.scale_factors();
let scale_width = clamp_to_scale_factor(scale.0, false);
let scale_height = clamp_to_scale_factor(scale.1, false);
let scale_factor = LayoutToDeviceScale::new(scale_width.max(scale_height));
let task_size_f = (LayoutSize::from_au(cache_key.size) * scale_factor).ceil();
let mut task_size = if task_size_f.width > MAX_LINE_DECORATION_RESOLUTION as f32 ||
task_size_f.height > MAX_LINE_DECORATION_RESOLUTION as f32 {
let max_extent = task_size_f.width.max(task_size_f.height);
let task_scale_factor = Scale::new(MAX_LINE_DECORATION_RESOLUTION as f32 / max_extent);
let task_size = (LayoutSize::from_au(cache_key.size) * scale_factor * task_scale_factor)
.ceil().to_i32();
task_size
} else {
task_size_f.to_i32()
};
task_size.width = task_size.width.max(1);
task_size.height = task_size.height.max(1);
frame_state.resource_cache.request_render_task(
Some(RenderTaskCacheKey {
origin: DeviceIntPoint::zero(),
size: task_size,
kind: RenderTaskCacheKeyKind::LineDecoration(cache_key.clone()),
}),
false,
RenderTaskParent::Surface,
&mut frame_state.frame_gpu_data.f32,
frame_state.rg_builder,
&mut frame_state.surface_builder,
&mut |rg_builder, _| {
rg_builder.add().init(RenderTask::new_dynamic(
task_size,
RenderTaskKind::new_line_decoration(
cache_key.style,
cache_key.orientation,
cache_key.wavy_line_thickness.to_f32_px(),
LayoutSize::from_au(cache_key.size),
),
))
}
)
}
}
pub type LineDecorationTemplate = PrimTemplate<LineDecorationData>;
impl From<LineDecorationKey> for LineDecorationTemplate {
fn from(line_dec: LineDecorationKey) -> Self {
let common = PrimTemplateCommonData::with_key_common(line_dec.common);
LineDecorationTemplate {
common,
kind: LineDecorationData {
style: line_dec.kind.style,
orientation: line_dec.kind.orientation,
wavy_line_thickness: line_dec.kind.wavy_line_thickness,
color: line_dec.kind.color.into(),
}
}
}
}
pub type LineDecorationDataHandle = intern::Handle<LineDecoration>;
impl intern::Internable for LineDecoration {
type Key = LineDecorationKey;
type StoreData = LineDecorationTemplate;
type InternData = ();
const PROFILE_COUNTER: usize = crate::profiler::INTERNED_LINE_DECORATIONS;
}
impl InternablePrimitive for LineDecoration {
fn into_key(
self,
info: &LayoutPrimitiveInfo,
) -> LineDecorationKey {
LineDecorationKey::new(
info,
self,
)
}
fn make_instance_kind(
_key: LineDecorationKey,
data_handle: LineDecorationDataHandle,
_: &mut PrimitiveStore,
) -> PrimitiveKind {
PrimitiveKind::LineDecoration {
data_handle,
}
}
}
impl CreateShadow for LineDecoration {
fn create_shadow(
&self,
shadow: &Shadow,
_: bool,
_: RasterSpace,
) -> Self {
LineDecoration {
style: self.style,
orientation: self.orientation,
wavy_line_thickness: self.wavy_line_thickness,
color: shadow.color.into(),
}
}
}
impl IsVisible for LineDecoration {
fn is_visible(&self) -> bool {
self.color.a > 0
}
}
pub fn get_line_decoration_size(
rect_size: &LayoutSize,
orientation: LineOrientation,
style: LineStyle,
wavy_line_thickness: f32,
) -> Option<LayoutSize> {
let h = match orientation {
LineOrientation::Horizontal => rect_size.height,
LineOrientation::Vertical => rect_size.width,
};
let (parallel, perpendicular) = match style {
LineStyle::Solid => {
return None;
}
LineStyle::Dashed => {
let dash_length = (3.0 * h).min(64.0).max(1.0);
(2.0 * dash_length, 4.0)
}
LineStyle::Dotted => {
let diameter = h.min(64.0).max(1.0);
let period = 2.0 * diameter;
(period, diameter)
}
LineStyle::Wavy => {
let line_thickness = wavy_line_thickness.max(1.0);
let slope_length = h - line_thickness;
let flat_length = ((line_thickness - 1.0) * 2.0).max(1.0);
let approx_period = 2.0 * (slope_length + flat_length);
(approx_period, h)
}
};
Some(match orientation {
LineOrientation::Horizontal => LayoutSize::new(parallel, perpendicular),
LineOrientation::Vertical => LayoutSize::new(perpendicular, parallel),
})
}
#[test]
#[cfg(target_pointer_width = "64")]
fn test_struct_sizes() {
use std::mem;
assert_eq!(mem::size_of::<LineDecoration>(), 12, "LineDecoration size changed");
assert_eq!(mem::size_of::<LineDecorationTemplate>(), 32, "LineDecorationTemplate size changed");
assert_eq!(mem::size_of::<LineDecorationKey>(), 16, "LineDecorationKey size changed");
}