smithay 0.7.0

Smithay is a library for writing wayland compositors.
Documentation
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
//! Utilities and helpers around the `Element` trait.

use crate::{
    backend::renderer::{
        element::{AsRenderElements, Element, Id, Kind, RenderElement, UnderlyingStorage},
        utils::{DamageSet, OpaqueRegions},
        Renderer,
    },
    utils::{Buffer, Physical, Point, Rectangle, Scale},
};

/// A element that allows to re-scale another element
#[derive(Debug)]
pub struct RescaleRenderElement<E> {
    element: E,
    origin: Point<i32, Physical>,
    scale: Scale<f64>,
}

impl<E: Element> RescaleRenderElement<E> {
    /// Create a new re-scale element for an existing element
    ///
    /// The origin can be used to scale the element geometry relative to a [`Point`].
    /// One use case of this is to only scale the location for some elements in a group, like
    /// a surface tree.
    pub fn from_element(element: E, origin: Point<i32, Physical>, scale: impl Into<Scale<f64>>) -> Self {
        RescaleRenderElement {
            element,
            origin,
            scale: scale.into(),
        }
    }
}

impl<E: Element> Element for RescaleRenderElement<E> {
    fn id(&self) -> &Id {
        self.element.id()
    }

    fn current_commit(&self) -> crate::backend::renderer::utils::CommitCounter {
        self.element.current_commit()
    }

    fn src(&self) -> crate::utils::Rectangle<f64, crate::utils::Buffer> {
        self.element.src()
    }

    fn geometry(
        &self,
        scale: crate::utils::Scale<f64>,
    ) -> crate::utils::Rectangle<i32, crate::utils::Physical> {
        let mut element_geometry = self.element.geometry(scale);
        // First we make the element relative to the origin
        element_geometry.loc -= self.origin;
        // Then we scale it by our scale
        element_geometry = element_geometry.to_f64().upscale(self.scale).to_i32_round();
        // At last we move it back to the origin
        element_geometry.loc += self.origin;
        element_geometry
    }

    fn transform(&self) -> crate::utils::Transform {
        self.element.transform()
    }

    fn damage_since(
        &self,
        scale: crate::utils::Scale<f64>,
        commit: Option<crate::backend::renderer::utils::CommitCounter>,
    ) -> DamageSet<i32, Physical> {
        self.element
            .damage_since(scale, commit)
            .into_iter()
            .map(|rect| rect.to_f64().upscale(self.scale).to_i32_up())
            .collect::<DamageSet<_, _>>()
    }

    fn opaque_regions(&self, scale: crate::utils::Scale<f64>) -> OpaqueRegions<i32, Physical> {
        self.element
            .opaque_regions(scale)
            .into_iter()
            .map(|rect| rect.to_f64().upscale(self.scale).to_i32_round())
            .collect::<OpaqueRegions<_, _>>()
    }

    fn alpha(&self) -> f32 {
        self.element.alpha()
    }

    fn kind(&self) -> Kind {
        self.element.kind()
    }
}

impl<R: Renderer, E: RenderElement<R>> RenderElement<R> for RescaleRenderElement<E> {
    fn draw(
        &self,
        frame: &mut R::Frame<'_, '_>,
        src: crate::utils::Rectangle<f64, crate::utils::Buffer>,
        dst: crate::utils::Rectangle<i32, crate::utils::Physical>,
        damage: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
        opaque_regions: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
    ) -> Result<(), R::Error> {
        self.element.draw(frame, src, dst, damage, opaque_regions)
    }

    #[inline]
    fn underlying_storage(&self, renderer: &mut R) -> Option<UnderlyingStorage<'_>> {
        self.element.underlying_storage(renderer)
    }
}

/// A element that allows to crop another element
#[derive(Debug)]
pub struct CropRenderElement<E> {
    element: E,
    src: Rectangle<f64, Buffer>,
    crop_rect: Rectangle<i32, Physical>,
}

impl<E: Element> CropRenderElement<E> {
    /// Create a cropping render element for an existing element
    ///
    /// The crop rect is expected to be relative to the same origin the element is relative to.
    /// It can extend outside of the element geometry but the resulting geometry will always
    /// be equal or smaller than the geometry of the original element.
    ///
    /// The scale is used to calculate the intersection between the crop rect and the
    /// original element geometry and should therefore equal the scale that was used to
    /// calculate the crop rect.
    ///
    /// Returns `None` if there is no overlap between the crop rect and the element geometry
    pub fn from_element(
        element: E,
        scale: impl Into<Scale<f64>>,
        crop_rect: Rectangle<i32, Physical>,
    ) -> Option<Self> {
        let scale = scale.into();

        let element_geometry = element.geometry(scale);

        if let Some(intersection) = element_geometry.intersection(crop_rect) {
            // FIXME: intersection sometimes return a 0 size element
            if intersection.is_empty() {
                return None;
            }

            // We need to know how much we should crop from the element
            let mut element_relative_intersection = intersection;
            element_relative_intersection.loc -= element_geometry.loc;

            // Now we calculate the scale from the original element geometry to the
            // original element src. We need that scale to bring our intersection
            // into buffer space.
            // We also have to consider the buffer transform for this or otherwise
            // the scale would be wrong
            let element_src = element.src();
            let transform = element.transform();
            let physical_to_buffer_scale =
                element_src.size / transform.invert().transform_size(element_geometry.size).to_f64();

            // Ok, for the src we need to know how much we cropped from the element geometry
            // and then bring that rectangle into buffer space. For this we have to first
            // apply the element transform and then scale it to buffer space.
            let mut src = element_relative_intersection.to_f64().to_logical(1.0).to_buffer(
                physical_to_buffer_scale,
                transform,
                &element_geometry.size.to_f64().to_logical(1.0),
            );

            // Ensure cropping of the existing element is respected.
            src.loc += element_src.loc;

            Some(CropRenderElement {
                element,
                src,
                crop_rect,
            })
        } else {
            None
        }
    }

    fn element_crop_rect(&self, scale: Scale<f64>) -> Option<Rectangle<i32, Physical>> {
        let element_geometry = self.element.geometry(scale);

        if let Some(mut intersection) = element_geometry.intersection(self.crop_rect) {
            // FIXME: intersection sometimes return a 0 size element
            if intersection.is_empty() {
                return None;
            }

            intersection.loc -= element_geometry.loc;
            Some(intersection)
        } else {
            None
        }
    }
}

impl<E: Element> Element for CropRenderElement<E> {
    fn id(&self) -> &Id {
        self.element.id()
    }

    fn current_commit(&self) -> crate::backend::renderer::utils::CommitCounter {
        self.element.current_commit()
    }

    fn src(&self) -> crate::utils::Rectangle<f64, crate::utils::Buffer> {
        self.src
    }

    fn geometry(&self, scale: Scale<f64>) -> crate::utils::Rectangle<i32, Physical> {
        let element_geometry = self.element.geometry(scale);
        if let Some(intersection) = element_geometry.intersection(self.crop_rect) {
            // FIXME: intersection sometimes return a 0 size element
            if intersection.is_empty() {
                return Default::default();
            }

            intersection
        } else {
            Default::default()
        }
    }

    fn transform(&self) -> crate::utils::Transform {
        self.element.transform()
    }

    fn damage_since(
        &self,
        scale: Scale<f64>,
        commit: Option<crate::backend::renderer::utils::CommitCounter>,
    ) -> DamageSet<i32, Physical> {
        if let Some(element_crop_rect) = self.element_crop_rect(scale) {
            self.element
                .damage_since(scale, commit)
                .into_iter()
                .flat_map(|rect| {
                    rect.intersection(element_crop_rect).map(|mut rect| {
                        rect.loc -= element_crop_rect.loc;
                        rect
                    })
                })
                .collect::<DamageSet<_, _>>()
        } else {
            Default::default()
        }
    }

    fn opaque_regions(&self, scale: Scale<f64>) -> OpaqueRegions<i32, Physical> {
        if let Some(element_crop_rect) = self.element_crop_rect(scale) {
            self.element
                .opaque_regions(scale)
                .into_iter()
                .flat_map(|rect| {
                    rect.intersection(element_crop_rect).map(|mut rect| {
                        rect.loc -= element_crop_rect.loc;
                        rect
                    })
                })
                .collect::<OpaqueRegions<_, _>>()
        } else {
            Default::default()
        }
    }

    fn alpha(&self) -> f32 {
        self.element.alpha()
    }

    fn kind(&self) -> Kind {
        self.element.kind()
    }
}

impl<R: Renderer, E: RenderElement<R>> RenderElement<R> for CropRenderElement<E> {
    fn draw(
        &self,
        frame: &mut R::Frame<'_, '_>,
        src: crate::utils::Rectangle<f64, crate::utils::Buffer>,
        dst: crate::utils::Rectangle<i32, crate::utils::Physical>,
        damage: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
        opaque_regions: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
    ) -> Result<(), R::Error> {
        self.element.draw(frame, src, dst, damage, opaque_regions)
    }

    #[inline]
    fn underlying_storage(&self, renderer: &mut R) -> Option<UnderlyingStorage<'_>> {
        self.element.underlying_storage(renderer)
    }
}

/// Defines how the location parameter should apply in [`RelocateRenderElement::from_element`]
#[derive(Debug, Copy, Clone)]
pub enum Relocate {
    /// The supplied location replaces the original location
    Absolute,
    /// The supplied location offsets the original location
    Relative,
}

/// A element that allows to offset the location of an existing element
#[derive(Debug)]
pub struct RelocateRenderElement<E> {
    element: E,
    relocate: Relocate,
    location: Point<i32, Physical>,
}

impl<E: Element> RelocateRenderElement<E> {
    /// Crate an re-locate element for an existing element
    pub fn from_element(element: E, location: impl Into<Point<i32, Physical>>, relocate: Relocate) -> Self {
        let location = location.into();

        RelocateRenderElement {
            element,
            location,
            relocate,
        }
    }
}

impl<E: Element> Element for RelocateRenderElement<E> {
    fn id(&self) -> &Id {
        self.element.id()
    }

    fn current_commit(&self) -> crate::backend::renderer::utils::CommitCounter {
        self.element.current_commit()
    }

    fn src(&self) -> Rectangle<f64, Buffer> {
        self.element.src()
    }

    fn geometry(&self, scale: Scale<f64>) -> Rectangle<i32, Physical> {
        let mut geo = self.element.geometry(scale);
        match self.relocate {
            Relocate::Absolute => geo.loc = self.location,
            Relocate::Relative => geo.loc += self.location,
        }
        geo
    }

    fn location(&self, scale: Scale<f64>) -> Point<i32, Physical> {
        match self.relocate {
            Relocate::Absolute => self.location,
            Relocate::Relative => self.element.location(scale) + self.location,
        }
    }

    fn transform(&self) -> crate::utils::Transform {
        self.element.transform()
    }

    fn damage_since(
        &self,
        scale: Scale<f64>,
        commit: Option<crate::backend::renderer::utils::CommitCounter>,
    ) -> DamageSet<i32, Physical> {
        self.element.damage_since(scale, commit)
    }

    fn opaque_regions(&self, scale: Scale<f64>) -> OpaqueRegions<i32, Physical> {
        self.element.opaque_regions(scale)
    }

    fn alpha(&self) -> f32 {
        self.element.alpha()
    }

    fn kind(&self) -> Kind {
        self.element.kind()
    }
}

impl<R: Renderer, E: RenderElement<R>> RenderElement<R> for RelocateRenderElement<E> {
    fn draw(
        &self,
        frame: &mut R::Frame<'_, '_>,
        src: crate::utils::Rectangle<f64, crate::utils::Buffer>,
        dst: crate::utils::Rectangle<i32, crate::utils::Physical>,
        damage: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
        opaque_regions: &[crate::utils::Rectangle<i32, crate::utils::Physical>],
    ) -> Result<(), R::Error> {
        self.element.draw(frame, src, dst, damage, opaque_regions)
    }

    #[inline]
    fn underlying_storage(&self, renderer: &mut R) -> Option<UnderlyingStorage<'_>> {
        self.element.underlying_storage(renderer)
    }
}

/// Defines the scale behavior for the constrain
#[derive(Debug, Copy, Clone)]
pub enum ConstrainScaleBehavior {
    /// Fit the element into the size, nothing will be cropped
    Fit,
    /// Zoom the element into the size, crops if the aspect ratio
    /// of the element does not match the aspect of the constrain
    /// size
    Zoom,
    /// Always stretch the element to the constrain size
    Stretch,
    /// Do not scale, but cut off at the constrain size
    CutOff,
}

bitflags::bitflags! {
    /// Defines how the elements should be aligned during constrain
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
    pub struct ConstrainAlign: u8 {
        /// Align to the top
        const TOP = 0b000001;
        /// Align to the left
        const LEFT = 0b000010;
        /// Align to the right
        const RIGHT = 0b000100;
        /// Align to the bottom
        const BOTTOM = 0b001000;
        /// Align to the top left
        ///
        /// Equals TOP | LEFT
        const TOP_LEFT = Self::TOP.bits() | Self::LEFT.bits();
        /// Align to the top right
        ///
        /// Equals TOP | RIGHT
        const TOP_RIGHT = Self::TOP.bits() | Self::RIGHT.bits();
        /// Align to the bottom left
        ///
        /// Equals BOTTOM | LEFt
        const BOTTOM_LEFT = Self::BOTTOM.bits() | Self::LEFT.bits();
        /// Align to the bottom left
        ///
        /// Equals BOTTOM | LEFT
        const BOTTOM_RIGHT = Self::BOTTOM.bits() | Self::RIGHT.bits();
        /// Align to the center
        ///
        /// Equals TOP | LEFT | BOTTOM | RIGHT
        const CENTER = Self::TOP.bits() | Self::LEFT.bits() | Self::BOTTOM.bits() | Self::RIGHT.bits();
    }
}

/// Convenience function to constrain something that implements [`AsRenderElements`]
///
/// See [`constrain_render_elements`] for more information
#[profiling::function]
#[allow(clippy::too_many_arguments)]
pub fn constrain_as_render_elements<R, E, C>(
    element: &E,
    renderer: &mut R,
    location: impl Into<Point<i32, Physical>>,
    alpha: f32,
    constrain: Rectangle<i32, Physical>,
    reference: Rectangle<i32, Physical>,
    behavior: ConstrainScaleBehavior,
    align: ConstrainAlign,
    output_scale: impl Into<Scale<f64>>,
) -> impl Iterator<Item = C>
where
    R: Renderer,
    E: AsRenderElements<R>,
    C: From<
        CropRenderElement<
            RelocateRenderElement<RescaleRenderElement<<E as AsRenderElements<R>>::RenderElement>>,
        >,
    >,
{
    let location = location.into();
    let output_scale = output_scale.into();
    let elements: Vec<<E as AsRenderElements<R>>::RenderElement> =
        AsRenderElements::<R>::render_elements(element, renderer, location, output_scale, alpha);
    constrain_render_elements(
        elements,
        location,
        constrain,
        reference,
        behavior,
        align,
        output_scale,
    )
    .map(C::from)
}

/// Constrain render elements on a specific location with a specific size
///
/// * `origin` - Defines the origin for re-scaling
/// * `constrain` - Defines the rectangle on screen the elements should be constrain within
/// * `reference` - Defines the reference that should be used for constraining the elements,
///                 this is most commonly the bounding box or geometry of the elements
/// * `behavior` - Defines the behavior for scaling the elements reference in the constrain
/// * `align` - Defines how the scaled elements should be aligned within the constrain
/// * `scale` - The scale that was used to create the original elements
#[profiling::function]
pub fn constrain_render_elements<E>(
    elements: impl IntoIterator<Item = E>,
    origin: impl Into<Point<i32, Physical>>,
    constrain: Rectangle<i32, Physical>,
    reference: Rectangle<i32, Physical>,
    behavior: ConstrainScaleBehavior,
    align: ConstrainAlign,
    scale: impl Into<Scale<f64>>,
) -> impl Iterator<Item = CropRenderElement<RelocateRenderElement<RescaleRenderElement<E>>>>
where
    E: Element,
{
    let location = origin.into();
    let scale = scale.into();

    let element_scale = match behavior {
        ConstrainScaleBehavior::Fit => {
            let reference = reference.to_f64();
            let size = constrain.size.to_f64();
            let element_scale: Scale<f64> = size / reference.size;
            Scale::from(f64::min(element_scale.x, element_scale.y))
        }
        ConstrainScaleBehavior::Zoom => {
            let reference = reference.to_f64();
            let size = constrain.size.to_f64();
            let element_scale: Scale<f64> = size / reference.size;
            Scale::from(f64::max(element_scale.x, element_scale.y))
        }
        ConstrainScaleBehavior::Stretch => {
            let reference = reference.to_f64();
            let size = constrain.size.to_f64();
            size / reference.size
        }
        ConstrainScaleBehavior::CutOff => Scale::from(1.0),
    };

    let scaled_reference = reference.to_f64().upscale(element_scale);

    // Calculate the align offset
    let top_offset: f64 = if align.contains(ConstrainAlign::TOP | ConstrainAlign::BOTTOM) {
        (constrain.size.h as f64 - scaled_reference.size.h) / 2f64
    } else if align.contains(ConstrainAlign::BOTTOM) {
        constrain.size.h as f64 - scaled_reference.size.h
    } else {
        0f64
    };

    let left_offset: f64 = if align.contains(ConstrainAlign::LEFT | ConstrainAlign::RIGHT) {
        (constrain.size.w as f64 - scaled_reference.size.w) / 2f64
    } else if align.contains(ConstrainAlign::RIGHT) {
        constrain.size.w as f64 - scaled_reference.size.w
    } else {
        0f64
    };

    let align_offset: Point<f64, Physical> = Point::from((left_offset, top_offset));

    // We need to offset the elements by the reference loc or otherwise
    // the element could be positioned outside of our constrain rect.
    let reference_offset =
        reference.loc.to_f64() - Point::from((scaled_reference.loc.x, scaled_reference.loc.y));

    // Final offset
    let offset = (reference_offset + align_offset).to_i32_round();

    elements
        .into_iter()
        .map(move |e| RescaleRenderElement::from_element(e, location, element_scale))
        .map(move |e| RelocateRenderElement::from_element(e, offset, Relocate::Relative))
        .filter_map(move |e| CropRenderElement::from_element(e, scale, constrain))
}