Skip to main content

webrender/pattern/
image.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
5use api::{ImageBufferKind, ColorF, units::*};
6
7use crate::pattern::{Pattern, PatternBuilder, PatternBuilderContext, PatternBuilderState, PatternKind};
8use crate::render_task_graph::RenderTaskId;
9use crate::renderer::BlendMode;
10
11pub struct ImagePattern {
12    pub src_task_id: RenderTaskId,
13    pub src_is_opaque: bool,
14    pub premultiplied: bool,
15    pub sampler_kind: ImageBufferKind,
16    pub color: ColorF,
17}
18
19impl PatternBuilder for ImagePattern {
20    fn build(
21        &self,
22        _sub_rect: Option<DeviceRect>,
23        _offset: LayoutVector2D,
24        _ctx: &PatternBuilderContext,
25        _state: &mut PatternBuilderState,
26    ) -> Pattern {
27        let blend_mode = if self.premultiplied || self.src_is_opaque {
28            BlendMode::PremultipliedAlpha
29        } else {
30            BlendMode::Alpha
31        };
32
33        let mut pattern = Pattern::texture(self.src_task_id, self.src_is_opaque)
34            .with_base_color(self.color)
35            .with_blend_mode(blend_mode);
36
37        pattern.kind = match self.sampler_kind {
38            ImageBufferKind::Texture2D => PatternKind::ColorOrTexture,
39            ImageBufferKind::TextureExternal => PatternKind::TextureExternal,
40            ImageBufferKind::TextureExternalBT709 => PatternKind::TextureExternalBT709,
41            ImageBufferKind::TextureRect => PatternKind::TextureRect,
42        };
43
44        pattern
45    }
46}