-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathsurface_gl.rs
More file actions
248 lines (215 loc) · 7.43 KB
/
surface_gl.rs
File metadata and controls
248 lines (215 loc) · 7.43 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use crate::context::paths::path::Path;
use crate::context::text_styles::text_direction::TextDirection;
use crate::context::{Context, State, SurfaceData, SurfaceEngine, SurfaceState};
use canvas_core::context_attributes::PowerPreference;
use skia_safe::gpu::gl::Interface;
use skia_safe::{gpu, surfaces, AlphaType, Color, ColorType, ISize, ImageInfo, PixelGeometry};
use std::ffi::c_void;
use std::ptr::NonNull;
const GR_GL_RGB565: u32 = 0x8D62;
const GR_GL_RGBA8: u32 = 0x8058;
#[cfg(feature = "gl")]
impl Context {
pub fn new_gl(
view: *mut c_void,
width: f32,
height: f32,
density: f32,
alpha: bool,
font_color: i32,
ppi: f32,
direction: TextDirection,
) -> Self {
let mut attr = canvas_core::context_attributes::ContextAttributes::new(
alpha,
false,
false,
false,
PowerPreference::Default,
true,
false,
false,
false,
false,
true,
false,
);
let bounds = skia_safe::Rect::from_wh(width, height);
let mut engine = SurfaceEngine::GL;
let mut zero_size = false;
let mut width = width;
if width <= 0. {
zero_size = true;
width = 1.
}
let mut height = height;
if height <= 0. {
zero_size = true;
height = 1.
}
let gl_context = if zero_size {
canvas_core::gpu::gl::GLContext::create_offscreen_context(&mut attr, width as i32, height as i32)
} else if let Some(view) = NonNull::new(view) {
#[cfg(target_os = "android")]{
let handle = raw_window_handle::AndroidNdkWindowHandle::new(view);
let handle = raw_window_handle::RawWindowHandle::AndroidNdk(handle);
canvas_core::gpu::gl::GLContext::create_window_context(&mut attr, width as i32, height as i32, handle)
}
#[cfg(not(target_os = "android"))]{
canvas_core::gpu::gl::GLContext::create_window_context(&mut attr, view)
}
} else {
canvas_core::gpu::gl::GLContext::create_offscreen_context(&mut attr, width as i32, height as i32)
}.unwrap();
gl_context.make_current();
let mut buffer_id = [0i32];
unsafe { gl_bindings::GetIntegerv(gl_bindings::FRAMEBUFFER_BINDING, buffer_id.as_mut_ptr()) }
let interface = Interface::new_native();
let mut ctx = gpu::direct_contexts::make_gl(interface.unwrap(), None).unwrap();
let mut frame_buffer = gpu::gl::FramebufferInfo::from_fboid(buffer_id[0] as u32);
if alpha {
frame_buffer.format = GR_GL_RGBA8;
} else {
frame_buffer.format = GR_GL_RGB565;
}
let target = gpu::backend_render_targets::make_gl(
(width as i32, height as i32),
Some(0),
0,
frame_buffer,
);
let surface_props = skia_safe::SurfaceProps::new(
skia_safe::SurfacePropsFlags::default(),
PixelGeometry::Unknown,
);
let mut color_type = ColorType::RGBA8888;
if !alpha {
color_type = ColorType::RGB565;
}
let surface = gpu::surfaces::wrap_backend_render_target(
&mut ctx,
&target,
gpu::SurfaceOrigin::BottomLeft,
color_type,
None,
Some(&surface_props),
)
.unwrap();
let direct_context = Some(ctx);
let mut state = State::default();
state.direction = direction;
Context {
direct_context,
#[cfg(feature = "metal")]
metal_context: None,
#[cfg(feature = "metal")]
metal_texture_info: None,
gl_context: Some(gl_context),
#[cfg(feature = "vulkan")]
vulkan_context: None,
#[cfg(feature = "vulkan")]
vulkan_texture: None,
surface_data: SurfaceData {
bounds,
scale: density,
ppi,
engine,
state: Default::default(),
is_opaque: !alpha
},
surface,
path: Path::default(),
state,
state_stack: vec![],
font_color: Color::new(font_color as u32),
surface_state: SurfaceState::None,
}
}
pub fn resize_gl(
context: &mut Context,
width: f32,
height: f32,
density: f32,
buffer_id: i32,
samples: i32,
alpha: bool,
ppi: f32,
) {
let bounds = skia_safe::Rect::from_wh(width, height);
let mut direct_context = None;
let mut engine = SurfaceEngine::GL;
let surface = if bounds.is_empty() {
let color_type = if alpha {
ColorType::RGBA8888
} else {
ColorType::RGB565
};
let alpha_type = if alpha {
AlphaType::Unpremul
} else {
AlphaType::Premul
};
let mut width = width;
if width <= 0. {
width = 1.
}
let mut height = height;
if height <= 0. {
height = 1.
}
engine = SurfaceEngine::CPU;
let info = ImageInfo::new(ISize::new(width as i32, height as i32), color_type, alpha_type, None);
surfaces::raster(&info, None, None)
} else {
let interface = Interface::new_native();
let ctx = gpu::direct_contexts::make_gl(interface.unwrap(), None);
if ctx.is_none() {
return;
}
let mut ctx = ctx.unwrap();
// ctx.reset(None);
let mut frame_buffer = gpu::gl::FramebufferInfo::from_fboid(buffer_id as u32);
if alpha {
frame_buffer.format = GR_GL_RGBA8;
} else {
frame_buffer.format = GR_GL_RGB565;
}
let target = gpu::backend_render_targets::make_gl(
(width as i32, height as i32),
Some(samples as usize),
0,
frame_buffer,
);
let surface_props = skia_safe::SurfaceProps::new(
skia_safe::SurfacePropsFlags::default(),
PixelGeometry::Unknown,
);
let mut color_type = ColorType::RGBA8888;
if !alpha {
color_type = ColorType::RGB565;
}
let surface = gpu::surfaces::wrap_backend_render_target(
&mut ctx,
&target,
gpu::SurfaceOrigin::BottomLeft,
color_type,
None,
Some(&surface_props),
);
direct_context = Some(ctx);
surface
};
if let Some(surface) = surface {
context.direct_context = direct_context;
context.surface_state = SurfaceState::None;
context.surface_data.engine = engine;
context.surface_data.bounds = bounds;
context.surface_data.scale = density;
context.surface_data.ppi = ppi;
context.surface_data.is_opaque = !alpha;
context.path = Path::default();
context.reset_state();
context.surface = surface;
}
}
}