Skip to main content

webrender/pattern/
repeat.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! A pattern tha repeats another pattern.
6//! See ps_quad_repeat.glsl.
7
8use api::units::*;
9use api::ColorF;
10use crate::pattern::PatternBuilder;
11use crate::pattern::PatternBuilderContext;
12use crate::pattern::PatternBuilderState;
13use crate::pattern::{Pattern, PatternKind, PatternShaderInput, PatternTextureInput};
14use crate::render_task_graph::RenderTaskId;
15use crate::renderer::{BlendMode, GpuBufferBuilder};
16
17pub struct RepeatedPattern {
18    pub stretch_size: LayoutSize,
19    pub spacing: LayoutSize,
20    pub src_task_id: RenderTaskId,
21    pub src_is_opaque: bool,
22}
23
24pub fn repeated_pattern(
25    repeat: &RepeatedPattern,
26    gpu_buffer_builder: &mut GpuBufferBuilder,
27) -> Pattern {
28    let mut writer = gpu_buffer_builder.f32.write_blocks(1);
29    writer.push_one([
30        repeat.stretch_size.width,
31        repeat.stretch_size.height,
32        repeat.spacing.width,
33        repeat.spacing.height,
34    ]);
35    let repeat_address = writer.finish();
36
37    Pattern {
38        kind: PatternKind::Repeat,
39        shader_input: PatternShaderInput(
40            repeat_address.as_int(),
41            0,
42        ),
43        texture_input: PatternTextureInput::new(repeat.src_task_id),
44        base_color: ColorF::WHITE,
45        is_opaque: repeat.src_is_opaque
46            && repeat.spacing.width <= 0.0
47            && repeat.spacing.height <= 0.0,
48        blend_mode: BlendMode::PremultipliedAlpha,
49    }
50}
51
52impl PatternBuilder for RepeatedPattern {
53    fn build(
54        &self,
55        _sub_rect: Option<DeviceRect>,
56        _offset: LayoutVector2D,
57        _ctx: &PatternBuilderContext,
58        state: &mut PatternBuilderState,
59    ) -> Pattern {
60        repeated_pattern(self, state.frame_gpu_data)
61    }
62}