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