azul_webrender_api/
units.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! A collection of coordinate spaces and their corresponding Point, Size and Rect types.
6//!
7//! Physical pixels take into account the device pixel ratio and their dimensions tend
8//! to correspond to the allocated size of resources in memory, while logical pixels
9//! don't have the device pixel ratio applied which means they are agnostic to the usage
10//! of hidpi screens and the like.
11//!
12//! The terms "layer" and "stacking context" can be used interchangeably
13//! in the context of coordinate systems.
14
15pub use app_units::Au;
16use euclid::{Length, Rect, Scale, Size2D, Transform3D, Translation2D};
17use euclid::{Point2D, Point3D, Vector2D, Vector3D, SideOffsets2D, Box2D};
18use euclid::HomogeneousVector;
19use peek_poke::PeekPoke;
20// local imports
21use crate::image::DirtyRect;
22
23/// Geometry in the coordinate system of the render target (screen or intermediate
24/// surface) in physical pixels.
25#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
26pub struct DevicePixel;
27
28pub type DeviceIntRect = Box2D<i32, DevicePixel>;
29pub type DeviceIntPoint = Point2D<i32, DevicePixel>;
30pub type DeviceIntSize = Size2D<i32, DevicePixel>;
31pub type DeviceIntLength = Length<i32, DevicePixel>;
32pub type DeviceIntSideOffsets = SideOffsets2D<i32, DevicePixel>;
33
34pub type DeviceRect = Box2D<f32, DevicePixel>;
35pub type DeviceBox2D = Box2D<f32, DevicePixel>;
36pub type DevicePoint = Point2D<f32, DevicePixel>;
37pub type DeviceVector2D = Vector2D<f32, DevicePixel>;
38pub type DeviceSize = Size2D<f32, DevicePixel>;
39pub type DeviceHomogeneousVector = HomogeneousVector<f32, DevicePixel>;
40
41/// Geometry in the coordinate system of the framebuffer in physical pixels.
42/// It's Y-flipped comparing to DevicePixel.
43#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
44pub struct FramebufferPixel;
45
46pub type FramebufferIntPoint = Point2D<i32, FramebufferPixel>;
47pub type FramebufferIntSize = Size2D<i32, FramebufferPixel>;
48pub type FramebufferIntRect = Box2D<i32, FramebufferPixel>;
49
50/// Geometry in the coordinate system of a Picture (intermediate
51/// surface) in physical pixels.
52#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
53pub struct PicturePixel;
54
55pub type PictureIntRect = Box2D<i32, PicturePixel>;
56pub type PictureIntPoint = Point2D<i32, PicturePixel>;
57pub type PictureIntSize = Size2D<i32, PicturePixel>;
58pub type PictureRect = Box2D<f32, PicturePixel>;
59pub type PicturePoint = Point2D<f32, PicturePixel>;
60pub type PictureSize = Size2D<f32, PicturePixel>;
61pub type PicturePoint3D = Point3D<f32, PicturePixel>;
62pub type PictureVector2D = Vector2D<f32, PicturePixel>;
63pub type PictureVector3D = Vector3D<f32, PicturePixel>;
64pub type PictureBox2D = Box2D<f32, PicturePixel>;
65
66/// Geometry gets rasterized in a given root coordinate space. This
67/// is often the root spatial node (world space), but may be a local
68/// space for a variety of reasons (e.g. perspective).
69#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
70pub struct RasterPixel;
71
72pub type RasterIntRect = Box2D<i32, RasterPixel>;
73pub type RasterIntPoint = Point2D<i32, RasterPixel>;
74pub type RasterIntSize = Size2D<i32, RasterPixel>;
75pub type RasterRect = Box2D<f32, RasterPixel>;
76pub type RasterPoint = Point2D<f32, RasterPixel>;
77pub type RasterSize = Size2D<f32, RasterPixel>;
78pub type RasterPoint3D = Point3D<f32, RasterPixel>;
79pub type RasterVector2D = Vector2D<f32, RasterPixel>;
80pub type RasterVector3D = Vector3D<f32, RasterPixel>;
81
82/// Geometry in a stacking context's local coordinate space (logical pixels).
83#[derive(Hash, Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, Ord, PartialOrd, Deserialize, Serialize, PeekPoke)]
84pub struct LayoutPixel;
85
86pub type LayoutRect = Box2D<f32, LayoutPixel>;
87pub type LayoutPoint = Point2D<f32, LayoutPixel>;
88pub type LayoutPoint3D = Point3D<f32, LayoutPixel>;
89pub type LayoutVector2D = Vector2D<f32, LayoutPixel>;
90pub type LayoutVector3D = Vector3D<f32, LayoutPixel>;
91pub type LayoutSize = Size2D<f32, LayoutPixel>;
92pub type LayoutSideOffsets = SideOffsets2D<f32, LayoutPixel>;
93
94pub type LayoutIntRect = Box2D<i32, LayoutPixel>;
95pub type LayoutIntPoint = Point2D<i32, LayoutPixel>;
96pub type LayoutIntSize = Size2D<i32, LayoutPixel>;
97
98/// Geometry in the document's coordinate space (logical pixels).
99#[derive(Hash, Clone, Copy, Debug, Eq, MallocSizeOf, PartialEq, Ord, PartialOrd)]
100pub struct WorldPixel;
101
102pub type WorldRect = Box2D<f32, WorldPixel>;
103pub type WorldIntRect = Box2D<i32, WorldPixel>;
104pub type WorldPoint = Point2D<f32, WorldPixel>;
105pub type WorldSize = Size2D<f32, WorldPixel>;
106pub type WorldPoint3D = Point3D<f32, WorldPixel>;
107pub type WorldVector2D = Vector2D<f32, WorldPixel>;
108pub type WorldVector3D = Vector3D<f32, WorldPixel>;
109
110/// Offset in number of tiles.
111#[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
112pub struct Tiles;
113pub type TileOffset = Point2D<i32, Tiles>;
114pub type TileRange = Box2D<i32, Tiles>;
115
116/// Scaling ratio from world pixels to device pixels.
117pub type DevicePixelScale = Scale<f32, WorldPixel, DevicePixel>;
118/// Scaling ratio from layout to world. Used for cases where we know the layout
119/// is in world space, or specifically want to treat it this way.
120pub type LayoutToWorldScale = Scale<f32, LayoutPixel, WorldPixel>;
121/// A complete scaling ratio from layout space to device pixel space.
122pub type LayoutToDeviceScale = Scale<f32, LayoutPixel, DevicePixel>;
123
124pub type LayoutTransform = Transform3D<f32, LayoutPixel, LayoutPixel>;
125pub type LayoutToWorldTransform = Transform3D<f32, LayoutPixel, WorldPixel>;
126pub type WorldToLayoutTransform = Transform3D<f32, WorldPixel, LayoutPixel>;
127
128pub type LayoutToPictureTransform = Transform3D<f32, LayoutPixel, PicturePixel>;
129pub type PictureToLayoutTransform = Transform3D<f32, PicturePixel, LayoutPixel>;
130
131pub type LayoutToRasterTransform = Transform3D<f32, LayoutPixel, RasterPixel>;
132pub type RasterToLayoutTransform = Transform3D<f32, RasterPixel, LayoutPixel>;
133
134pub type PictureToRasterTransform = Transform3D<f32, PicturePixel, RasterPixel>;
135pub type RasterToPictureTransform = Transform3D<f32, RasterPixel, PicturePixel>;
136
137/// Scaling ratio from picture pixels to raster pixels (e.g. if scaling a picture surface up/down).
138pub type RasterPixelScale = Scale<f32, PicturePixel, RasterPixel>;
139
140// Fixed position coordinates, to avoid float precision errors.
141pub type LayoutPointAu = Point2D<Au, LayoutPixel>;
142pub type LayoutRectAu = Box2D<Au, LayoutPixel>;
143pub type LayoutSizeAu = Size2D<Au, LayoutPixel>;
144pub type LayoutVector2DAu = Vector2D<Au, LayoutPixel>;
145pub type LayoutSideOffsetsAu = SideOffsets2D<Au, LayoutPixel>;
146
147pub type ImageDirtyRect = DirtyRect<i32, DevicePixel>;
148pub type BlobDirtyRect = DirtyRect<i32, LayoutPixel>;
149
150pub type BlobToDeviceTranslation = Translation2D<i32, LayoutPixel, DevicePixel>;
151
152/// Stores two coordinates in texel space. The coordinates
153/// are stored in texel coordinates because the texture atlas
154/// may grow. Storing them as texel coords and normalizing
155/// the UVs in the vertex shader means nothing needs to be
156/// updated on the CPU when the texture size changes.
157#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
158pub struct TexelRect {
159    pub uv0: DevicePoint,
160    pub uv1: DevicePoint,
161}
162
163impl TexelRect {
164    pub fn new(u0: f32, v0: f32, u1: f32, v1: f32) -> Self {
165        TexelRect {
166            uv0: DevicePoint::new(u0, v0),
167            uv1: DevicePoint::new(u1, v1),
168        }
169    }
170
171    pub fn invalid() -> Self {
172        TexelRect {
173            uv0: DevicePoint::new(-1.0, -1.0),
174            uv1: DevicePoint::new(-1.0, -1.0),
175        }
176    }
177}
178
179impl Into<TexelRect> for DeviceIntRect {
180    fn into(self) -> TexelRect {
181        TexelRect {
182            uv0: self.min.to_f32(),
183            uv1: self.max.to_f32(),
184        }
185    }
186}
187
188const MAX_AU_FLOAT: f32 = 1.0e6;
189
190pub trait AuHelpers<T> {
191    fn from_au(data: T) -> Self;
192    fn to_au(&self) -> T;
193}
194
195impl AuHelpers<LayoutSizeAu> for LayoutSize {
196    fn from_au(size: LayoutSizeAu) -> Self {
197        LayoutSize::new(
198            size.width.to_f32_px(),
199            size.height.to_f32_px(),
200        )
201    }
202
203    fn to_au(&self) -> LayoutSizeAu {
204        let width = self.width.min(2.0 * MAX_AU_FLOAT);
205        let height = self.height.min(2.0 * MAX_AU_FLOAT);
206
207        LayoutSizeAu::new(
208            Au::from_f32_px(width),
209            Au::from_f32_px(height),
210        )
211    }
212}
213
214impl AuHelpers<LayoutVector2DAu> for LayoutVector2D {
215    fn from_au(size: LayoutVector2DAu) -> Self {
216        LayoutVector2D::new(
217            size.x.to_f32_px(),
218            size.y.to_f32_px(),
219        )
220    }
221
222    fn to_au(&self) -> LayoutVector2DAu {
223        LayoutVector2DAu::new(
224            Au::from_f32_px(self.x),
225            Au::from_f32_px(self.y),
226        )
227    }
228}
229
230impl AuHelpers<LayoutPointAu> for LayoutPoint {
231    fn from_au(point: LayoutPointAu) -> Self {
232        LayoutPoint::new(
233            point.x.to_f32_px(),
234            point.y.to_f32_px(),
235        )
236    }
237
238    fn to_au(&self) -> LayoutPointAu {
239        let x = self.x.min(MAX_AU_FLOAT).max(-MAX_AU_FLOAT);
240        let y = self.y.min(MAX_AU_FLOAT).max(-MAX_AU_FLOAT);
241
242        LayoutPointAu::new(
243            Au::from_f32_px(x),
244            Au::from_f32_px(y),
245        )
246    }
247}
248
249impl AuHelpers<LayoutRectAu> for LayoutRect {
250    fn from_au(rect: LayoutRectAu) -> Self {
251        LayoutRect {
252            min: LayoutPoint::from_au(rect.min),
253            max: LayoutPoint::from_au(rect.max),
254        }
255    }
256
257    fn to_au(&self) -> LayoutRectAu {
258        LayoutRectAu {
259            min: self.min.to_au(),
260            max: self.max.to_au(),
261        }
262    }
263}
264
265impl AuHelpers<LayoutSideOffsetsAu> for LayoutSideOffsets {
266    fn from_au(offsets: LayoutSideOffsetsAu) -> Self {
267        LayoutSideOffsets::new(
268            offsets.top.to_f32_px(),
269            offsets.right.to_f32_px(),
270            offsets.bottom.to_f32_px(),
271            offsets.left.to_f32_px(),
272        )
273    }
274
275    fn to_au(&self) -> LayoutSideOffsetsAu {
276        LayoutSideOffsetsAu::new(
277            Au::from_f32_px(self.top),
278            Au::from_f32_px(self.right),
279            Au::from_f32_px(self.bottom),
280            Au::from_f32_px(self.left),
281        )
282    }
283}
284
285pub trait RectExt {
286    type Point;
287    fn top_left(&self) -> Self::Point;
288    fn top_right(&self) -> Self::Point;
289    fn bottom_left(&self) -> Self::Point;
290    fn bottom_right(&self) -> Self::Point;
291}
292
293impl<U> RectExt for Rect<f32, U> {
294    type Point = Point2D<f32, U>;
295    fn top_left(&self) -> Self::Point {
296        self.min()
297    }
298    fn top_right(&self) -> Self::Point {
299        Point2D::new(self.max_x(), self.min_y())
300    }
301    fn bottom_left(&self) -> Self::Point {
302        Point2D::new(self.min_x(), self.max_y())
303    }
304    fn bottom_right(&self) -> Self::Point {
305        self.max()
306    }
307}
308
309impl<U> RectExt for Box2D<f32, U> {
310    type Point = Point2D<f32, U>;
311    fn top_left(&self) -> Self::Point {
312        self.min
313    }
314    fn top_right(&self) -> Self::Point {
315        Point2D::new(self.max.x, self.min.y)
316    }
317    fn bottom_left(&self) -> Self::Point {
318        Point2D::new(self.min.x, self.max.y)
319    }
320    fn bottom_right(&self) -> Self::Point {
321        self.max
322    }
323}
324
325// A few helpers to convert to cast between coordinate spaces that are often equivalent.
326
327#[inline]
328pub fn layout_rect_as_picture_rect(layout_rect: &LayoutRect) -> PictureRect {
329    layout_rect.cast_unit()
330}
331
332#[inline]
333pub fn layout_vector_as_picture_vector(layout_vector: LayoutVector2D) -> PictureVector2D {
334    layout_vector.cast_unit()
335}
336
337#[inline]
338pub fn device_size_as_framebuffer_size(framebuffer_size: DeviceIntSize) -> FramebufferIntSize {
339    framebuffer_size.cast_unit()
340}
341
342#[inline]
343pub fn device_rect_as_framebuffer_rect(framebuffer_rect: &DeviceIntRect) -> FramebufferIntRect {
344    framebuffer_rect.cast_unit()
345}