skia_safe/gpu/ganesh/
recording_context.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::fmt;

use crate::{
    gpu::{BackendAPI, BackendFormat, DirectContext, Renderable},
    prelude::*,
    ColorType, TextureCompressionType,
};
use skia_bindings::{self as sb, GrRecordingContext, SkRefCntBase};

pub type RecordingContext = RCHandle<GrRecordingContext>;

impl NativeRefCountedBase for GrRecordingContext {
    type Base = SkRefCntBase;
}

impl From<DirectContext> for RecordingContext {
    fn from(direct_context: DirectContext) -> Self {
        unsafe { std::mem::transmute(direct_context) }
    }
}

impl fmt::Debug for RecordingContext {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("RecordingContext")
            .field("backend", &self.backend())
            .field("max_texture_size", &self.max_texture_size())
            .field("max_render_target_size", &self.max_render_target_size())
            .finish()
    }
}

impl RecordingContext {
    // From GrContext_Base
    pub fn as_direct_context(&mut self) -> Option<DirectContext> {
        DirectContext::from_unshared_ptr(unsafe {
            sb::C_GrRecordingContext_asDirectContext(self.native_mut())
        })
    }

    // From GrContext_Base
    pub fn backend(&self) -> BackendAPI {
        unsafe { sb::C_GrRecordingContext_backend(self.native()) }
    }

    pub fn default_backend_format(&self, ct: ColorType, renderable: Renderable) -> BackendFormat {
        let mut format = BackendFormat::new_invalid();
        unsafe {
            sb::C_GrRecordingContext_defaultBackendFormat(
                self.native(),
                ct.into_native(),
                renderable,
                format.native_mut(),
            )
        };
        format
    }

    // From GrContext_Base
    pub fn compressed_backend_format(
        &self,
        compression_type: TextureCompressionType,
    ) -> BackendFormat {
        let mut format = BackendFormat::new_invalid();
        unsafe {
            sb::C_GrRecordingContext_compressedBackendFormat(
                self.native(),
                compression_type,
                format.native_mut(),
            )
        }
        format
    }

    // TODO: GrContext_Base::threadSafeProxy

    pub fn abandoned(&mut self) -> bool {
        unsafe { sb::C_GrRecordingContext_abandoned(self.native_mut()) }
    }

    pub fn color_type_supported_as_surface(&self, color_type: ColorType) -> bool {
        unsafe {
            sb::C_GrRecordingContext_colorTypeSupportedAsSurface(
                self.native(),
                color_type.into_native(),
            )
        }
    }

    pub fn max_texture_size(&self) -> i32 {
        unsafe { self.native().maxTextureSize() }
    }

    pub fn max_render_target_size(&self) -> i32 {
        unsafe { self.native().maxRenderTargetSize() }
    }

    pub fn color_type_supported_as_image(&self, color_type: ColorType) -> bool {
        unsafe {
            self.native()
                .colorTypeSupportedAsImage(color_type.into_native())
        }
    }

    pub fn supports_protected_content(&self) -> bool {
        unsafe { self.native().supportsProtectedContent() }
    }

    pub fn max_surface_sample_count_for_color_type(&self, color_type: ColorType) -> usize {
        unsafe {
            sb::C_GrRecordingContext_maxSurfaceSampleCountForColorType(
                self.native(),
                color_type.into_native(),
            )
        }
        .try_into()
        .unwrap()
    }

    // TODO: Wrap Arenas (if used).
}