forma_render/cpu/
channel.rs

1// Copyright 2022 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::utils::simd::f32x8;
16
17pub(crate) trait Mask {
18    fn zero() -> Self;
19    fn one() -> Self;
20}
21
22impl Mask for f32x8 {
23    #[inline]
24    fn zero() -> Self {
25        f32x8::splat(0.0)
26    }
27
28    #[inline]
29    fn one() -> Self {
30        f32x8::splat(1.0)
31    }
32}
33
34#[derive(Clone, Copy, Debug, Eq, PartialEq)]
35pub enum Channel {
36    Red,
37    Green,
38    Blue,
39    Alpha,
40    Zero,
41    One,
42}
43
44impl Channel {
45    pub(crate) fn select<M: Mask>(self, r: M, g: M, b: M, a: M) -> M {
46        match self {
47            Channel::Red => r,
48            Channel::Green => g,
49            Channel::Blue => b,
50            Channel::Alpha => a,
51            Channel::Zero => M::zero(),
52            Channel::One => M::one(),
53        }
54    }
55}
56
57pub const RGBA: [Channel; 4] = [Channel::Red, Channel::Green, Channel::Blue, Channel::Alpha];
58pub const BGRA: [Channel; 4] = [Channel::Blue, Channel::Green, Channel::Red, Channel::Alpha];
59pub const RGB0: [Channel; 4] = [Channel::Red, Channel::Green, Channel::Blue, Channel::Zero];
60pub const BGR0: [Channel; 4] = [Channel::Blue, Channel::Green, Channel::Red, Channel::Zero];
61pub const RGB1: [Channel; 4] = [Channel::Red, Channel::Green, Channel::Blue, Channel::One];
62pub const BGR1: [Channel; 4] = [Channel::Blue, Channel::Green, Channel::Red, Channel::One];