drawing_api/common/
context.rs

1use std::borrow::Cow;
2
3use crate::{Capabilities, ColorSource, GraphicsApi, ImageFilter, TextureDescriptor};
4
5/// An abstraction over graphics context (like OpenGL or Vulkan context).
6///
7/// This object contains common methods for all backends.
8pub trait Context: Clone + 'static {
9    type ColorSourceFragment: crate::ColorSourceFragment;
10    type DisplayList: crate::DisplayList;
11    type DisplayListBuilder: crate::DisplayListBuilder<
12        DisplayList = Self::DisplayList,
13        ImageFilterFragment = Self::ImageFilterFragment,
14        Paint = Self::Paint,
15        ParagraphBuilder = Self::ParagraphBuilder,
16        PathBuilder = Self::PathBuilder,
17        Texture = Self::Texture,
18    >;
19    type Fonts: crate::Fonts;
20    type FragmentProgram: crate::FragmentProgram;
21    type ImageFilterFragment: crate::ImageFilterFragment;
22    type Paint: crate::Paint<
23        ColorSourceFragment = Self::ColorSourceFragment,
24        ImageFilterFragment = Self::ImageFilterFragment,
25        Texture = Self::Texture,
26    >;
27    type ParagraphBuilder: crate::ParagraphBuilder<
28        Texture = Self::Texture,
29        Paint = Self::Paint,
30        Fonts = Self::Fonts,
31    >;
32    type PathBuilder: crate::PathBuilder;
33    type Surface: crate::Surface<DisplayList = Self::DisplayList>;
34    type Texture: crate::Texture;
35
36    /// Gets implementation capabilities depending on graphics API.
37    fn get_api_capabilities(api: GraphicsApi) -> Option<Capabilities>;
38
39    /// Gets implementation capabilities of the current instance.
40    fn get_capabilities(&self) -> Capabilities;
41
42    /// Creates a new texture.
43    unsafe fn create_texture(
44        &self,
45        contents: Cow<'static, [u8]>,
46        descriptor: TextureDescriptor,
47    ) -> Result<Self::Texture, &'static str>;
48
49    /// Creates a color source whose pixels are shaded by a fragment program.
50    unsafe fn new_color_source_from_fragment_program(
51        &self,
52        frag_program: &Self::FragmentProgram,
53        samplers: &[Self::Texture],
54        uniform_data: &[u8],
55    ) -> ColorSource<Self::Texture, Self::ColorSourceFragment>;
56
57    /// Creates an image filter where each pixel is shaded by a fragment program.
58    unsafe fn new_image_filter_from_fragment_program(
59        &self,
60        frag_program: &Self::FragmentProgram,
61        samplers: &[Self::Texture],
62        uniform_data: &[u8],
63    ) -> ImageFilter<Self::ImageFilterFragment>;
64}