skia_safe/gpu/ganesh/
recording_context.rs

1use std::fmt;
2
3use crate::{
4    gpu::{BackendAPI, BackendFormat, DirectContext, Renderable},
5    prelude::*,
6    ColorType, TextureCompressionType,
7};
8use skia_bindings::{self as sb, GrRecordingContext, SkRefCntBase};
9
10pub type RecordingContext = RCHandle<GrRecordingContext>;
11
12impl NativeRefCountedBase for GrRecordingContext {
13    type Base = SkRefCntBase;
14}
15
16impl From<DirectContext> for RecordingContext {
17    fn from(direct_context: DirectContext) -> Self {
18        unsafe { std::mem::transmute(direct_context) }
19    }
20}
21
22impl fmt::Debug for RecordingContext {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.debug_struct("RecordingContext")
25            .field("backend", &self.backend())
26            .field("max_texture_size", &self.max_texture_size())
27            .field("max_render_target_size", &self.max_render_target_size())
28            .finish()
29    }
30}
31
32impl RecordingContext {
33    // From GrContext_Base
34    pub fn as_direct_context(&mut self) -> Option<DirectContext> {
35        DirectContext::from_unshared_ptr(unsafe {
36            sb::C_GrRecordingContext_asDirectContext(self.native_mut())
37        })
38    }
39
40    // From GrContext_Base
41    pub fn backend(&self) -> BackendAPI {
42        unsafe { sb::C_GrRecordingContext_backend(self.native()) }
43    }
44
45    pub fn default_backend_format(&self, ct: ColorType, renderable: Renderable) -> BackendFormat {
46        let mut format = BackendFormat::new_invalid();
47        unsafe {
48            sb::C_GrRecordingContext_defaultBackendFormat(
49                self.native(),
50                ct.into_native(),
51                renderable,
52                format.native_mut(),
53            )
54        };
55        format
56    }
57
58    // From GrContext_Base
59    pub fn compressed_backend_format(
60        &self,
61        compression_type: TextureCompressionType,
62    ) -> BackendFormat {
63        let mut format = BackendFormat::new_invalid();
64        unsafe {
65            sb::C_GrRecordingContext_compressedBackendFormat(
66                self.native(),
67                compression_type,
68                format.native_mut(),
69            )
70        }
71        format
72    }
73
74    // TODO: GrContext_Base::threadSafeProxy
75
76    pub fn abandoned(&mut self) -> bool {
77        unsafe { sb::C_GrRecordingContext_abandoned(self.native_mut()) }
78    }
79
80    pub fn color_type_supported_as_surface(&self, color_type: ColorType) -> bool {
81        unsafe {
82            sb::C_GrRecordingContext_colorTypeSupportedAsSurface(
83                self.native(),
84                color_type.into_native(),
85            )
86        }
87    }
88
89    pub fn max_texture_size(&self) -> i32 {
90        unsafe { self.native().maxTextureSize() }
91    }
92
93    pub fn max_render_target_size(&self) -> i32 {
94        unsafe { self.native().maxRenderTargetSize() }
95    }
96
97    pub fn color_type_supported_as_image(&self, color_type: ColorType) -> bool {
98        unsafe {
99            self.native()
100                .colorTypeSupportedAsImage(color_type.into_native())
101        }
102    }
103
104    pub fn supports_protected_content(&self) -> bool {
105        unsafe { self.native().supportsProtectedContent() }
106    }
107
108    pub fn max_surface_sample_count_for_color_type(&self, color_type: ColorType) -> usize {
109        unsafe {
110            sb::C_GrRecordingContext_maxSurfaceSampleCountForColorType(
111                self.native(),
112                color_type.into_native(),
113            )
114        }
115        .try_into()
116        .unwrap()
117    }
118
119    // TODO: Wrap Arenas (if used).
120}