canvas/
backend.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 https://mozilla.org/MPL/2.0/. */
4
5use canvas_traits::canvas::{
6    CompositionOptions, FillOrStrokeStyle, FillRule, LineOptions, Path, ShadowOptions, TextRun,
7};
8use compositing_traits::SerializableImageData;
9use euclid::default::{Point2D, Rect, Size2D, Transform2D};
10use pixels::Snapshot;
11use webrender_api::ImageDescriptor;
12
13use crate::canvas_data::Filter;
14
15// This defines required methods for a DrawTarget. The prototypes are derived from the now-removed
16// Azure backend's methods.
17pub(crate) trait GenericDrawTarget {
18    type SourceSurface;
19
20    fn new(size: Size2D<u32>) -> Self;
21    fn create_similar_draw_target(&self, size: &Size2D<i32>) -> Self;
22
23    fn clear_rect(&mut self, rect: &Rect<f32>, transform: Transform2D<f64>);
24    fn copy_surface(
25        &mut self,
26        surface: Self::SourceSurface,
27        source: Rect<i32>,
28        destination: Point2D<i32>,
29    );
30    fn create_source_surface_from_data(&self, data: Snapshot) -> Option<Self::SourceSurface>;
31    fn draw_surface(
32        &mut self,
33        surface: Self::SourceSurface,
34        dest: Rect<f64>,
35        source: Rect<f64>,
36        filter: Filter,
37        composition_options: CompositionOptions,
38        transform: Transform2D<f64>,
39    );
40    fn draw_surface_with_shadow(
41        &self,
42        surface: Self::SourceSurface,
43        dest: &Point2D<f32>,
44        shadow_options: ShadowOptions,
45        composition_options: CompositionOptions,
46    );
47    fn fill(
48        &mut self,
49        path: &Path,
50        fill_rule: FillRule,
51        style: FillOrStrokeStyle,
52        composition_options: CompositionOptions,
53        transform: Transform2D<f64>,
54    );
55    fn fill_text(
56        &mut self,
57        text_runs: Vec<TextRun>,
58        style: FillOrStrokeStyle,
59        composition_options: CompositionOptions,
60        transform: Transform2D<f64>,
61    );
62    fn fill_rect(
63        &mut self,
64        rect: &Rect<f32>,
65        style: FillOrStrokeStyle,
66        composition_options: CompositionOptions,
67        transform: Transform2D<f64>,
68    );
69    fn get_size(&self) -> Size2D<i32>;
70    fn pop_clip(&mut self);
71    fn push_clip(&mut self, path: &Path, fill_rule: FillRule, transform: Transform2D<f64>);
72    fn push_clip_rect(&mut self, rect: &Rect<i32>);
73    fn stroke(
74        &mut self,
75        path: &Path,
76        style: FillOrStrokeStyle,
77        line_options: LineOptions,
78        composition_options: CompositionOptions,
79        transform: Transform2D<f64>,
80    );
81    fn stroke_rect(
82        &mut self,
83        rect: &Rect<f32>,
84        style: FillOrStrokeStyle,
85        line_options: LineOptions,
86        composition_options: CompositionOptions,
87        transform: Transform2D<f64>,
88    );
89    fn surface(&mut self) -> Self::SourceSurface;
90    fn image_descriptor_and_serializable_data(
91        &mut self,
92    ) -> (ImageDescriptor, SerializableImageData);
93    fn snapshot(&mut self) -> Snapshot;
94}
95
96#[allow(dead_code)] // used by gated backends
97/// A version of the `Into<T>` trait from the standard library that can be used
98/// to convert between two types that are not defined in the canvas crate.
99pub(crate) trait Convert<T> {
100    fn convert(self) -> T;
101}