-
Notifications
You must be signed in to change notification settings - Fork 502
/
renderer.rs
870 lines (796 loc) · 33.7 KB
/
renderer.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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
mod draw_functions;
use crate::lengths::PhysicalPx;
use crate::{
LogicalItemGeometry, LogicalLength, LogicalPoint, LogicalRect, PhysicalLength, PhysicalPoint,
PhysicalRect, PhysicalSize, PointLengths, RectLengths, ScaleFactor, SizeLengths,
};
use alloc::rc::Rc;
use alloc::{vec, vec::Vec};
use core::pin::Pin;
pub use draw_functions::TargetPixel;
use embedded_graphics::pixelcolor::Rgb888;
use i_slint_core::graphics::{FontRequest, IntRect, PixelFormat, Rect as RectF};
use i_slint_core::item_rendering::{ItemRenderer, PartialRenderingCache};
use i_slint_core::items::{ImageFit, ItemRc};
use i_slint_core::textlayout::{FontMetrics as _, TextParagraphLayout};
use i_slint_core::{Color, Coord, ImageInner, StaticTextures};
type DirtyRegion = PhysicalRect;
pub trait LineBufferProvider {
/// Called before the frame is being drawn, with the dirty region. Return the actual dirty region
///
/// The default implementation simply returns the dirty_region
fn set_dirty_region(&mut self, dirty_region: PhysicalRect) -> PhysicalRect {
dirty_region
}
/// Called once per line, you will have to call the render_fn back with the buffer.
fn process_line(
&mut self,
line: PhysicalLength,
render_fn: impl FnOnce(&mut [super::TargetPixel]),
);
}
#[derive(Default)]
pub struct LineRenderer {
partial_cache: i_slint_core::item_rendering::PartialRenderingCache,
}
impl LineRenderer {
/// Render the window, line by line, into the buffer provided by the `line_buffer` function.
///
/// The renderer uses a cache internally and will only render the part of the window
/// which are dirty. The `initial_dirty_region` is an extra dirty regin which will also
/// be rendered.
///
/// TODO: the window should be the public slint::Window type
/// TODO: what about async and threading.
/// (can we call the line_buffer function from different thread?)
/// TODO: should `initial_dirty_region` be set from a different call?
pub fn render(
&mut self,
window: Rc<i_slint_core::window::Window>,
initial_dirty_region: i_slint_core::item_rendering::DirtyRegion,
line_buffer: impl LineBufferProvider,
) {
let component_rc = window.component();
let component = i_slint_core::component::ComponentRc::borrow_pin(&component_rc);
if let Some(window_item) = i_slint_core::items::ItemRef::downcast_pin::<
i_slint_core::items::WindowItem,
>(component.as_ref().get_item_ref(0))
{
let size = euclid::size2(window_item.width() as f32, window_item.height() as f32)
* ScaleFactor::new(window.scale_factor());
let background = to_rgb888_color_discard_alpha(window_item.background());
render_window_frame(
window,
background.into(),
size.cast(),
initial_dirty_region,
&mut self.partial_cache,
line_buffer,
);
}
}
pub fn free_graphics_resources(
&mut self,
items: &mut dyn Iterator<Item = Pin<i_slint_core::items::ItemRef<'_>>>,
) {
for item in items {
let cache_entry = item.cached_rendering_data_offset().release(&mut self.partial_cache);
drop(cache_entry);
}
}
}
fn render_window_frame(
runtime_window: Rc<i_slint_core::window::Window>,
background: super::TargetPixel,
size: PhysicalSize,
initial_dirty_region: i_slint_core::item_rendering::DirtyRegion,
cache: &mut PartialRenderingCache,
mut line_buffer: impl LineBufferProvider,
) {
let mut scene =
prepare_scene(runtime_window, size, initial_dirty_region, &mut line_buffer, cache);
let dirty_region = scene.dirty_region;
debug_assert!(scene.current_line >= dirty_region.origin.y_length());
while scene.current_line < dirty_region.origin.y_length() + dirty_region.size.height_length() {
line_buffer.process_line(scene.current_line, |line_buffer| {
line_buffer[dirty_region.min_x() as usize..dirty_region.max_x() as usize]
.fill(background);
for span in scene.items[0..scene.current_items_index].iter().rev() {
debug_assert!(scene.current_line >= span.pos.y_length());
debug_assert!(scene.current_line < span.pos.y_length() + span.size.height_length(),);
match span.command {
SceneCommand::Rectangle { color } => {
TargetPixel::blend_buffer(
&mut line_buffer[span.pos.x as usize
..(span.pos.x_length() + span.size.width_length()).get() as usize],
color,
);
}
SceneCommand::Texture { texture_index } => {
let texture = &scene.textures[texture_index as usize];
draw_functions::draw_texture_line(
span,
scene.current_line,
texture,
line_buffer,
);
}
SceneCommand::RoundedRectangle { rectangle_index } => {
let rr = &scene.rounded_rectangles[rectangle_index as usize];
draw_functions::draw_rounded_rectangle_line(
span,
scene.current_line,
rr,
line_buffer,
);
}
}
}
});
if scene.current_line < dirty_region.origin.y_length() + dirty_region.size.height_length() {
scene.next_line();
}
}
}
struct Scene {
/// the next line to be processed
current_line: PhysicalLength,
/// The items are sorted like so:
/// - `items[future_items_index..]` are the items that have `y > current_line`.
/// They must be sorted by `y` (top to bottom), then by `z` (front to back)
/// - `items[..current_items_index]` are the items that overlap with the current_line,
/// sorted by z (front to back)
items: Vec<SceneItem>,
future_items_index: usize,
current_items_index: usize,
textures: Vec<SceneTexture>,
rounded_rectangles: Vec<RoundedRectangle>,
dirty_region: DirtyRegion,
}
impl Scene {
pub fn new(
mut items: Vec<SceneItem>,
textures: Vec<SceneTexture>,
rounded_rectangles: Vec<RoundedRectangle>,
dirty_region: DirtyRegion,
) -> Self {
let current_line = dirty_region.origin.y_length();
items.retain(|i| i.pos.y_length() + i.size.height_length() > current_line);
items.sort_unstable_by(|a, b| compare_scene_item(a, b));
let current_items_index = items.partition_point(|i| i.pos.y_length() <= current_line);
items[..current_items_index].sort_unstable_by(|a, b| b.z.cmp(&a.z));
Self {
items,
current_line,
current_items_index,
future_items_index: current_items_index,
textures,
rounded_rectangles,
dirty_region,
}
}
/// Updates `current_items_index` and `future_items_index` to match the invariant
pub fn next_line(&mut self) {
self.current_line += PhysicalLength::new(1);
// The items array is split in part:
// 1. [0..i] are the items that have already been processed, that are on this line
// 2. [j..current_items_index] are the items from the previous line that might still be
// valid on this line
// 3. [tmp1, tmp2] is a buffer where we swap items so we can make room for the items in [0..i]
// 4. [future_items_index..] are the items which might get processed now
// 5. [current_items_index..tmp1], [tmp2..future_items_index] and [i..j] is garbage
//
// At each step, we selecting the item with the higher z from the list 2 or 3 or 4 and take it from
// that list. Then we add it to the list [0..i] if it needs more processing. If needed,
// we move the first item from list 2. to list 3. to make some room
let (mut i, mut j, mut tmp1, mut tmp2) =
(0, 0, self.current_items_index, self.current_items_index);
'outer: loop {
let future_next_z = self
.items
.get(self.future_items_index)
.filter(|i| i.pos.y_length() <= self.current_line)
.map(|i| i.z);
let item = loop {
if tmp1 != tmp2 {
if future_next_z.map_or(true, |z| self.items[tmp1].z > z) {
let idx = tmp1;
tmp1 += 1;
if tmp1 == tmp2 {
tmp1 = self.current_items_index;
tmp2 = self.current_items_index;
}
break self.items[idx];
}
} else if j < self.current_items_index {
let item = &self.items[j];
if item.pos.y_length() + item.size.height_length() <= self.current_line {
j += 1;
continue;
}
if future_next_z.map_or(true, |z| item.z > z) {
j += 1;
break *item;
}
}
if future_next_z.is_some() {
self.future_items_index += 1;
break self.items[self.future_items_index - 1];
}
break 'outer;
};
if i != j {
// there is room
} else if j >= self.current_items_index && tmp1 == tmp2 {
// the current_items list is empty
j += 1
} else if self.items[j].pos.y_length() + self.items[j].size.height_length()
<= self.current_line
{
// next item in the current_items array is no longer in this line
j += 1;
} else if tmp2 < self.future_items_index && j < self.current_items_index {
// move the next item in current_items
let to_move = self.items[j];
self.items[tmp2] = to_move;
j += 1;
tmp2 += 1;
} else {
debug_assert!(tmp1 >= self.current_items_index);
let sort_begin = i;
// merge sort doesn't work because we don't have enough tmp space, just bring all items and use a normal sort.
while j < self.current_items_index {
let item = self.items[j];
if item.pos.y_length() + item.size.height_length() > self.current_line {
self.items[i] = item;
i += 1;
}
j += 1;
}
self.items.copy_within(tmp1..tmp2, i);
i += tmp2 - tmp1;
debug_assert!(i < self.future_items_index);
self.items[i] = item;
i += 1;
while self.future_items_index < self.items.len() {
let item = self.items[self.future_items_index];
if item.pos.y_length() > self.current_line {
break;
}
self.items[i] = item;
i += 1;
self.future_items_index += 1;
}
self.items[sort_begin..i].sort_unstable_by(|a, b| b.z.cmp(&a.z));
break;
}
self.items[i] = item;
i += 1;
}
self.current_items_index = i;
// check that current items are properly sorted
debug_assert!(self.items[0..self.current_items_index].windows(2).all(|x| x[0].z >= x[1].z));
}
}
#[derive(Clone, Copy, Debug)]
struct SceneItem {
pos: PhysicalPoint,
size: PhysicalSize,
// this is the order of the item from which it is in the item tree
z: u16,
command: SceneCommand,
}
fn compare_scene_item(a: &SceneItem, b: &SceneItem) -> core::cmp::Ordering {
// First, order by line (top to bottom)
match a.pos.y.partial_cmp(&b.pos.y) {
None | Some(core::cmp::Ordering::Equal) => {}
Some(ord) => return ord,
}
// Then by the reverse z (front to back)
match a.z.partial_cmp(&b.z) {
None | Some(core::cmp::Ordering::Equal) => {}
Some(ord) => return ord.reverse(),
}
// anything else, we don't care
core::cmp::Ordering::Equal
}
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
enum SceneCommand {
Rectangle {
color: Color,
},
/// texture_index is an index in the Scene::textures array
Texture {
texture_index: u16,
},
/// rectangle_index is an index in the Scene::rounded_rectangle array
RoundedRectangle {
rectangle_index: u16,
},
}
struct SceneTexture {
data: &'static [u8],
format: PixelFormat,
/// bytes between two lines in the source
stride: u16,
source_size: PhysicalSize,
color: Color,
}
#[derive(Debug)]
struct RoundedRectangle {
radius: PhysicalLength,
/// the border's width
width: PhysicalLength,
border_color: Color,
inner_color: Color,
/// The clips is the amount of pixels of the rounded rectangle that is clipped away.
/// For example, if left_clip > width, then the left border will not be visible, and
/// if left_clip > radius, then no radius will be seen in the left side
left_clip: PhysicalLength,
right_clip: PhysicalLength,
top_clip: PhysicalLength,
bottom_clip: PhysicalLength,
}
fn prepare_scene(
runtime_window: Rc<i_slint_core::window::Window>,
size: PhysicalSize,
initial_dirty_region: i_slint_core::item_rendering::DirtyRegion,
line_buffer: &mut impl LineBufferProvider,
cache: &mut PartialRenderingCache,
) -> Scene {
let factor = ScaleFactor::new(runtime_window.scale_factor());
let prepare_scene = PrepareScene::new(size, factor, runtime_window.default_font_properties());
let mut renderer = i_slint_core::item_rendering::PartialRenderer::new(
cache,
initial_dirty_region,
prepare_scene,
);
let mut dirty_region = PhysicalRect::default();
runtime_window.draw_contents(|components| {
for (component, origin) in components {
renderer.compute_dirty_regions(component, *origin);
}
dirty_region = (LogicalRect::from_untyped(&renderer.dirty_region.to_rect()).cast()
* factor)
.round_out()
.cast()
.intersection(&PhysicalRect { origin: euclid::point2(0, 0), size })
.unwrap_or_default();
dirty_region = line_buffer.set_dirty_region(dirty_region);
renderer.combine_clip((dirty_region.cast() / factor).to_untyped().cast(), 0 as _, 0 as _);
for (component, origin) in components {
i_slint_core::item_rendering::render_component_items(component, &mut renderer, *origin);
}
});
let prepare_scene = renderer.into_inner();
Scene::new(
prepare_scene.items,
prepare_scene.textures,
prepare_scene.rounded_rectangles,
dirty_region,
)
}
struct PrepareScene {
items: Vec<SceneItem>,
textures: Vec<SceneTexture>,
rounded_rectangles: Vec<RoundedRectangle>,
state_stack: Vec<RenderState>,
current_state: RenderState,
scale_factor: ScaleFactor,
default_font: FontRequest,
}
impl PrepareScene {
fn new(size: PhysicalSize, scale_factor: ScaleFactor, default_font: FontRequest) -> Self {
Self {
items: vec![],
rounded_rectangles: vec![],
textures: vec![],
state_stack: vec![],
current_state: RenderState {
alpha: 1.,
offset: LogicalPoint::default(),
clip: LogicalRect::new(
LogicalPoint::default(),
(size.cast() / scale_factor).cast(),
),
},
scale_factor,
default_font,
}
}
fn should_draw(&self, rect: &LogicalRect) -> bool {
!rect.size.is_empty()
&& self.current_state.alpha > 0.01
&& self.current_state.clip.intersects(rect)
}
fn new_scene_texture(&mut self, geometry: PhysicalRect, texture: SceneTexture) {
let size = geometry.size;
if !size.is_empty() {
let texture_index = self.textures.len() as u16;
self.textures.push(texture);
self.items.push(SceneItem {
pos: geometry.origin,
size,
z: self.items.len() as u16,
command: SceneCommand::Texture { texture_index },
});
}
}
fn new_scene_item(&mut self, geometry: LogicalRect, command: SceneCommand) {
let geometry = (geometry.translate(self.current_state.offset.to_vector()).cast()
* self.scale_factor)
.round()
.cast();
let size = geometry.size;
if !size.is_empty() {
let z = self.items.len() as u16;
let pos = geometry.origin;
self.items.push(SceneItem { pos, size, z, command });
}
}
fn draw_image_impl(
&mut self,
geom: LogicalRect,
source: &i_slint_core::graphics::Image,
mut source_rect: IntRect,
image_fit: ImageFit,
colorize: Color,
) {
let image_inner: &ImageInner = source.into();
match image_inner {
ImageInner::None => (),
ImageInner::AbsoluteFilePath(_) | ImageInner::EmbeddedData { .. } => {
unimplemented!()
}
ImageInner::EmbeddedImage(_) => todo!(),
ImageInner::StaticTextures(StaticTextures { data, textures, .. }) => {
let size: euclid::default::Size2D<u32> = source_rect.size.cast();
let phys_size = geom.size_length().cast() * self.scale_factor;
let source_to_target_x = phys_size.width / (size.width as f32);
let source_to_target_y = phys_size.height / (size.height as f32);
let mut image_fit_offset = euclid::Vector2D::default();
let (source_to_target_x, source_to_target_y) = match image_fit {
ImageFit::fill => (source_to_target_x, source_to_target_y),
ImageFit::cover => {
let ratio = f32::max(source_to_target_x, source_to_target_y);
if size.width as f32 > phys_size.width / ratio {
let diff = (size.width as f32 - phys_size.width / ratio) as i32;
source_rect.origin.x += diff / 2;
source_rect.size.width -= diff;
}
if size.height as f32 > phys_size.height / ratio {
let diff = (size.height as f32 - phys_size.height / ratio) as i32;
source_rect.origin.y += diff / 2;
source_rect.size.height -= diff;
}
(ratio, ratio)
}
ImageFit::contain => {
let ratio = f32::min(source_to_target_x, source_to_target_y);
if (size.width as f32) < phys_size.width / ratio {
image_fit_offset.x = (phys_size.width - size.width as f32 * ratio) / 2.;
}
if (size.height as f32) < phys_size.height / ratio {
image_fit_offset.y =
(phys_size.height - size.height as f32 * ratio) / 2.;
}
(ratio, ratio)
}
};
let offset = self.current_state.offset.to_vector().cast() * self.scale_factor
+ image_fit_offset;
let renderer_clip_in_source_rect_space = (self.current_state.clip.cast()
* self.scale_factor)
.scale(1. / source_to_target_x, 1. / source_to_target_y);
for t in textures.as_slice() {
if let Some(clipped_relative_source_rect) =
t.rect.intersection(&source_rect).and_then(|clipped_source_rect| {
let relative_clipped_source_rect = clipped_source_rect
.translate(-source_rect.origin.to_vector())
.cast();
euclid::Rect::<_, PhysicalPx>::from_untyped(
&relative_clipped_source_rect,
)
.intersection(&renderer_clip_in_source_rect_space)
})
{
let target_rect = clipped_relative_source_rect
.scale(source_to_target_x, source_to_target_y)
.translate(offset)
.round();
let actual_x = clipped_relative_source_rect.origin.x as usize
+ source_rect.origin.x as usize
- t.rect.origin.x as usize;
let actual_y = clipped_relative_source_rect.origin.y as usize
+ source_rect.origin.y as usize
- t.rect.origin.y as usize;
let stride = t.rect.width() as u16 * bpp(t.format);
self.new_scene_texture(
target_rect.cast(),
SceneTexture {
data: &data.as_slice()[(t.index
+ (stride as usize) * actual_y
+ (bpp(t.format) as usize) * actual_x)..],
stride,
source_size: clipped_relative_source_rect.size.ceil().cast(),
format: t.format,
color: if colorize.alpha() > 0 { colorize } else { t.color },
},
);
}
}
}
};
}
}
#[derive(Clone, Copy)]
struct RenderState {
alpha: f32,
offset: LogicalPoint,
clip: LogicalRect,
}
impl i_slint_core::item_rendering::ItemRenderer for PrepareScene {
fn draw_rectangle(&mut self, rect: Pin<&i_slint_core::items::Rectangle>, _: &ItemRc) {
let geom = LogicalRect::new(LogicalPoint::default(), rect.logical_geometry().size_length());
if self.should_draw(&geom) {
let geom = match geom.intersection(&self.current_state.clip) {
Some(geom) => geom,
None => return,
};
// FIXME: gradients
let color = rect.background().color();
if color.alpha() == 0 {
return;
}
self.new_scene_item(geom, SceneCommand::Rectangle { color: color });
}
}
fn draw_border_rectangle(
&mut self,
rect: Pin<&i_slint_core::items::BorderRectangle>,
_: &ItemRc,
) {
let geom = LogicalRect::new(LogicalPoint::default(), rect.logical_geometry().size_length());
if self.should_draw(&geom) {
let border = rect.border_width();
let radius = rect.border_radius();
// FIXME: gradients
let color = rect.background().color();
if radius > 0 as _ {
let radius = LogicalLength::new(radius)
.min(geom.width_length() / 2 as Coord)
.min(geom.height_length() / 2 as Coord);
if let Some(clipped) = geom.intersection(&self.current_state.clip) {
let geom2 = geom.cast() * self.scale_factor;
let clipped2 = clipped.cast() * self.scale_factor;
let rectangle_index = self.rounded_rectangles.len() as u16;
// Add a small value to make sure that the clip is always positive despite floating point shenanigans
const E: f32 = 0.00001;
self.rounded_rectangles.push(RoundedRectangle {
radius: (radius.cast() * self.scale_factor).cast(),
width: (LogicalLength::new(border).cast() * self.scale_factor).cast(),
border_color: rect.border_color().color(),
inner_color: color,
top_clip: PhysicalLength::new((clipped2.min_y() - geom2.min_y() + E) as _),
bottom_clip: PhysicalLength::new(
(geom2.max_y() - clipped2.max_y() + E) as _,
),
left_clip: PhysicalLength::new((clipped2.min_x() - geom2.min_x() + E) as _),
right_clip: PhysicalLength::new(
(geom2.max_x() - clipped2.max_x() + E) as _,
),
});
self.new_scene_item(
clipped,
SceneCommand::RoundedRectangle { rectangle_index },
);
}
return;
}
if color.alpha() > 0 {
if let Some(r) =
geom.inflate(-border, -border).intersection(&self.current_state.clip)
{
self.new_scene_item(r, SceneCommand::Rectangle { color });
}
}
if border > 0.01 as Coord {
// FIXME: radius
// FIXME: gradients
let border_color = rect.border_color().color();
if border_color.alpha() > 0 {
let mut add_border = |r: LogicalRect| {
if let Some(r) = r.intersection(&self.current_state.clip) {
self.new_scene_item(r, SceneCommand::Rectangle { color: border_color });
}
};
let b = border;
add_border(euclid::rect(0 as _, 0 as _, geom.width(), b));
add_border(euclid::rect(0 as _, geom.height() - b, geom.width(), b));
add_border(euclid::rect(0 as _, b, b, geom.height() - b - b));
add_border(euclid::rect(geom.width() - b, b, b, geom.height() - b - b));
}
}
}
}
fn draw_image(&mut self, image: Pin<&i_slint_core::items::ImageItem>, _: &ItemRc) {
let geom =
LogicalRect::new(LogicalPoint::default(), image.logical_geometry().size_length());
if self.should_draw(&geom) {
let source = image.source();
self.draw_image_impl(
geom,
&source,
euclid::Rect::new(Default::default(), source.size().cast()),
image.image_fit(),
Default::default(),
);
}
}
fn draw_clipped_image(&mut self, image: Pin<&i_slint_core::items::ClippedImage>, _: &ItemRc) {
let geom =
LogicalRect::new(LogicalPoint::default(), image.logical_geometry().size_length());
if self.should_draw(&geom) {
let source = image.source();
let source_clip_x = image.source_clip_x();
let source_clip_y = image.source_clip_y();
let source_size = source.size();
let mut source_clip_width = image.source_clip_width();
// when the source_clip size is empty, make it full
if source_clip_width == 0 {
source_clip_width = source_size.width as i32 - source_clip_x;
}
let mut source_clip_height = image.source_clip_height();
if source_clip_height == 0 {
source_clip_height = source_size.height as i32 - source_clip_y;
}
self.draw_image_impl(
geom,
&source,
euclid::rect(source_clip_x, source_clip_y, source_clip_width, source_clip_height),
image.image_fit(),
image.colorize().color(),
);
}
}
fn draw_text(&mut self, text: Pin<&i_slint_core::items::Text>, _: &ItemRc) {
let string = text.text();
if string.trim().is_empty() {
return;
}
let geom = LogicalRect::new(LogicalPoint::default(), text.logical_geometry().size_length());
if !self.should_draw(&geom) {
return;
}
let font_request = text.unresolved_font_request().merge(&self.default_font);
let font = crate::fonts::match_font(&font_request, self.scale_factor);
let layout = crate::fonts::text_layout_for_font(&font, &font_request, self.scale_factor);
let color = text.color().color();
let max_size = (geom.size.cast() * self.scale_factor).cast();
let paragraph = TextParagraphLayout {
string: &string,
layout,
max_width: max_size.width_length(),
max_height: max_size.height_length(),
horizontal_alignment: text.horizontal_alignment(),
vertical_alignment: text.vertical_alignment(),
wrap: text.wrap(),
overflow: text.overflow(),
single_line: false,
};
paragraph.layout_lines(|glyphs, line_x, line_y| {
let baseline_y = line_y + font.ascent();
while let Some(positioned_glyph) = glyphs.next() {
let src_rect = PhysicalRect::new(
PhysicalPoint::from_lengths(
line_x + positioned_glyph.x + positioned_glyph.platform_glyph.x(),
baseline_y
- positioned_glyph.platform_glyph.y()
- positioned_glyph.platform_glyph.height(),
),
positioned_glyph.platform_glyph.size(),
)
.cast();
if let Some(clipped_src) =
src_rect.intersection(&(self.current_state.clip.cast() * self.scale_factor))
{
let offset = self.current_state.offset.to_vector().cast() * self.scale_factor;
let geometry = clipped_src.translate(offset).round();
let origin = (geometry.origin - offset.round()).cast::<usize>();
let actual_x = origin.x - src_rect.origin.x as usize;
let actual_y = origin.y - src_rect.origin.y as usize;
let stride = positioned_glyph.platform_glyph.width().get() as u16;
let geometry = geometry.cast();
self.new_scene_texture(
geometry,
SceneTexture {
data: &positioned_glyph.platform_glyph.data().as_slice()
[actual_x + actual_y * stride as usize..],
stride,
source_size: geometry.size,
format: PixelFormat::AlphaMap,
color,
},
);
}
}
});
}
fn draw_text_input(&mut self, text_input: Pin<&i_slint_core::items::TextInput>, _: &ItemRc) {
text_input.logical_geometry();
// TODO
}
#[cfg(feature = "std")]
fn draw_path(&mut self, path: Pin<&i_slint_core::items::Path>, _: &ItemRc) {
path.logical_geometry();
// TODO
}
fn draw_box_shadow(&mut self, box_shadow: Pin<&i_slint_core::items::BoxShadow>, _: &ItemRc) {
box_shadow.logical_geometry();
// TODO
}
fn combine_clip(&mut self, other: RectF, _radius: Coord, _border_width: Coord) {
match self.current_state.clip.intersection(&LogicalRect::from_untyped(&other)) {
Some(r) => {
self.current_state.clip = r;
}
None => {
self.current_state.clip = LogicalRect::default();
}
};
// TODO: handle radius and border
}
fn get_current_clip(&self) -> i_slint_core::graphics::Rect {
self.current_state.clip.to_untyped()
}
fn translate(&mut self, x: Coord, y: Coord) {
self.current_state.offset.x += x;
self.current_state.offset.y += y;
self.current_state.clip = self.current_state.clip.translate((-x, -y).into())
}
fn rotate(&mut self, _angle_in_degrees: f32) {
todo!()
}
fn apply_opacity(&mut self, opacity: f32) {
self.current_state.alpha *= opacity;
}
fn save_state(&mut self) {
self.state_stack.push(self.current_state);
}
fn restore_state(&mut self) {
self.current_state = self.state_stack.pop().unwrap();
}
fn scale_factor(&self) -> f32 {
self.scale_factor.0
}
fn draw_cached_pixmap(
&mut self,
_: &ItemRc,
_update_fn: &dyn Fn(&mut dyn FnMut(u32, u32, &[u8])),
) {
todo!()
}
fn draw_string(&mut self, _string: &str, _color: Color) {
todo!()
}
fn window(&self) -> i_slint_core::window::WindowRc {
unreachable!("this backend don't query the window")
}
fn as_any(&mut self) -> &mut dyn core::any::Any {
self
}
}
/// bytes per pixels
fn bpp(format: PixelFormat) -> u16 {
match format {
PixelFormat::Rgb => 3,
PixelFormat::Rgba => 4,
PixelFormat::AlphaMap => 1,
}
}
pub fn to_rgb888_color_discard_alpha(col: Color) -> Rgb888 {
Rgb888::new(col.red(), col.green(), col.blue())
}