1 | // Copyright 2006 The Android Open Source Project |
2 | // Copyright 2020 Yevhenii Reizner |
3 | // |
4 | // Use of this source code is governed by a BSD-style license that can be |
5 | // found in the LICENSE file. |
6 | |
7 | // Skia uses fixed points pretty chaotically, therefore we cannot use |
8 | // strongly typed wrappers. Which is unfortunate. |
9 | |
10 | use tiny_skia_path::SaturateCast; |
11 | |
12 | use crate::math::{bound, left_shift, left_shift64}; |
13 | |
14 | /// A 26.6 fixed point. |
15 | pub type FDot6 = i32; |
16 | |
17 | /// A 24.8 fixed point. |
18 | pub type FDot8 = i32; |
19 | |
20 | /// A 16.16 fixed point. |
21 | pub type FDot16 = i32; |
22 | |
23 | pub mod fdot6 { |
24 | use super::*; |
25 | use core::convert::TryFrom; |
26 | |
27 | pub const ONE: FDot6 = 64; |
28 | |
29 | pub fn from_i32(n: i32) -> FDot6 { |
30 | debug_assert!(n as i16 as i32 == n); |
31 | n << 6 |
32 | } |
33 | |
34 | pub fn from_f32(n: f32) -> FDot6 { |
35 | (n * 64.0) as i32 |
36 | } |
37 | |
38 | pub fn floor(n: FDot6) -> FDot6 { |
39 | n >> 6 |
40 | } |
41 | |
42 | pub fn ceil(n: FDot6) -> FDot6 { |
43 | (n + 63) >> 6 |
44 | } |
45 | |
46 | pub fn round(n: FDot6) -> FDot6 { |
47 | (n + 32) >> 6 |
48 | } |
49 | |
50 | pub fn to_fdot16(n: FDot6) -> FDot16 { |
51 | debug_assert!((left_shift(n, 10) >> 10) == n); |
52 | left_shift(n, 10) |
53 | } |
54 | |
55 | pub fn div(a: FDot6, b: FDot6) -> FDot16 { |
56 | debug_assert_ne!(b, 0); |
57 | |
58 | if i16::try_from(a).is_ok() { |
59 | left_shift(a, 16) / b |
60 | } else { |
61 | fdot16::div(a, b) |
62 | } |
63 | } |
64 | |
65 | pub fn can_convert_to_fdot16(n: FDot6) -> bool { |
66 | let max_dot6 = i32::MAX >> (16 - 6); |
67 | n.abs() <= max_dot6 |
68 | } |
69 | |
70 | pub fn small_scale(value: u8, dot6: FDot6) -> u8 { |
71 | debug_assert!(dot6 as u32 <= 64); |
72 | ((value as i32 * dot6) >> 6) as u8 |
73 | } |
74 | } |
75 | |
76 | pub mod fdot8 { |
77 | use super::*; |
78 | |
79 | // Extracted from SkScan_Antihair.cpp |
80 | |
81 | pub fn from_fdot16(x: FDot16) -> FDot8 { |
82 | (x + 0x80) >> 8 |
83 | } |
84 | } |
85 | |
86 | pub mod fdot16 { |
87 | use super::*; |
88 | |
89 | pub const HALF: FDot16 = (1 << 16) / 2; |
90 | pub const ONE: FDot16 = 1 << 16; |
91 | |
92 | // `from_f32` seems to lack a rounding step. For all fixed-point |
93 | // values, this version is as accurate as possible for (fixed -> float -> fixed). Rounding reduces |
94 | // accuracy if the intermediate floats are in the range that only holds integers (adding 0.5 to an |
95 | // odd integer then snaps to nearest even). Using double for the rounding math gives maximum |
96 | // accuracy for (float -> fixed -> float), but that's usually overkill. |
97 | pub fn from_f32(x: f32) -> FDot16 { |
98 | i32::saturate_from(x * ONE as f32) |
99 | } |
100 | |
101 | pub fn floor_to_i32(x: FDot16) -> i32 { |
102 | x >> 16 |
103 | } |
104 | |
105 | pub fn ceil_to_i32(x: FDot16) -> i32 { |
106 | (x + ONE - 1) >> 16 |
107 | } |
108 | |
109 | pub fn round_to_i32(x: FDot16) -> i32 { |
110 | (x + HALF) >> 16 |
111 | } |
112 | |
113 | // The divide may exceed 32 bits. Clamp to a signed 32 bit result. |
114 | pub fn mul(a: FDot16, b: FDot16) -> FDot16 { |
115 | ((i64::from(a) * i64::from(b)) >> 16) as FDot16 |
116 | } |
117 | |
118 | // The divide may exceed 32 bits. Clamp to a signed 32 bit result. |
119 | pub fn div(numer: FDot6, denom: FDot6) -> FDot16 { |
120 | let v = left_shift64(numer as i64, 16) / denom as i64; |
121 | let n = bound(i32::MIN as i64, v, i32::MAX as i64); |
122 | n as i32 |
123 | } |
124 | |
125 | pub fn fast_div(a: FDot6, b: FDot6) -> FDot16 { |
126 | debug_assert!((left_shift(a, 16) >> 16) == a); |
127 | debug_assert!(b != 0); |
128 | left_shift(a, 16) / b |
129 | } |
130 | } |
131 | |