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 
15 use app_units::Au;
16 use euclid::{Length, TypedRect, TypedScale, TypedSize2D, TypedTransform3D};
17 use euclid::{TypedPoint2D, TypedPoint3D, TypedVector2D, TypedVector3D};
18 
19 /// Geometry in the coordinate system of the render target (screen or intermediate
20 /// surface) in physical pixels.
21 #[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
22 pub struct DevicePixel;
23 
24 pub type DeviceIntRect = TypedRect<i32, DevicePixel>;
25 pub type DeviceIntPoint = TypedPoint2D<i32, DevicePixel>;
26 pub type DeviceIntSize = TypedSize2D<i32, DevicePixel>;
27 pub type DeviceIntLength = Length<i32, DevicePixel>;
28 
29 pub type DeviceUintRect = TypedRect<u32, DevicePixel>;
30 pub type DeviceUintPoint = TypedPoint2D<u32, DevicePixel>;
31 pub type DeviceUintSize = TypedSize2D<u32, DevicePixel>;
32 
33 pub type DeviceRect = TypedRect<f32, DevicePixel>;
34 pub type DevicePoint = TypedPoint2D<f32, DevicePixel>;
35 pub type DeviceVector2D = TypedVector2D<f32, DevicePixel>;
36 pub type DeviceSize = TypedSize2D<f32, DevicePixel>;
37 
38 /// Geometry in a stacking context's local coordinate space (logical pixels).
39 ///
40 /// For now layout pixels are equivalent to layer pixels, but it may change.
41 pub type LayoutPixel = LayerPixel;
42 
43 pub type LayoutRect = LayerRect;
44 pub type LayoutPoint = LayerPoint;
45 pub type LayoutVector2D = LayerVector2D;
46 pub type LayoutVector3D = LayerVector3D;
47 pub type LayoutSize = LayerSize;
48 
49 /// Geometry in a layer's local coordinate space (logical pixels).
50 #[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Deserialize, Serialize)]
51 pub struct LayerPixel;
52 
53 pub type LayerRect = TypedRect<f32, LayerPixel>;
54 pub type LayerPoint = TypedPoint2D<f32, LayerPixel>;
55 pub type LayerPoint3D = TypedPoint3D<f32, LayerPixel>;
56 pub type LayerVector2D = TypedVector2D<f32, LayerPixel>;
57 pub type LayerVector3D = TypedVector3D<f32, LayerPixel>;
58 pub type LayerSize = TypedSize2D<f32, LayerPixel>;
59 
60 /// Geometry in a layer's scrollable parent coordinate space (logical pixels).
61 ///
62 /// Some layers are scrollable while some are not. There is a distinction between
63 /// a layer's parent layer and a layer's scrollable parent layer (its closest parent
64 /// that is scrollable, but not necessarily its immediate parent). Most of the internal
65 /// transforms are expressed in terms of the scrollable parent and not the immediate
66 /// parent.
67 #[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
68 pub struct ScrollLayerPixel;
69 
70 pub type ScrollLayerRect = TypedRect<f32, ScrollLayerPixel>;
71 pub type ScrollLayerPoint = TypedPoint2D<f32, ScrollLayerPixel>;
72 pub type ScrollLayerVector2D = TypedVector2D<f32, ScrollLayerPixel>;
73 pub type ScrollLayerSize = TypedSize2D<f32, ScrollLayerPixel>;
74 
75 /// Geometry in the document's coordinate space (logical pixels).
76 #[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
77 pub struct WorldPixel;
78 
79 pub type WorldRect = TypedRect<f32, WorldPixel>;
80 pub type WorldPoint = TypedPoint2D<f32, WorldPixel>;
81 pub type WorldSize = TypedSize2D<f32, WorldPixel>;
82 pub type WorldPoint3D = TypedPoint3D<f32, WorldPixel>;
83 pub type WorldVector2D = TypedVector2D<f32, WorldPixel>;
84 pub type WorldVector3D = TypedVector3D<f32, WorldPixel>;
85 
86 /// Offset in number of tiles.
87 #[derive(Hash, Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
88 pub struct Tiles;
89 pub type TileOffset = TypedPoint2D<u16, Tiles>;
90 
91 /// Scaling ratio from world pixels to device pixels.
92 pub type DevicePixelScale = TypedScale<f32, WorldPixel, DevicePixel>;
93 /// Scaling ratio from layer to world. Used for cases where we know the layer
94 /// is in world space, or specifically want to treat it this way.
95 pub type LayerToWorldScale = TypedScale<f32, LayerPixel, WorldPixel>;
96 
97 pub type LayoutTransform = TypedTransform3D<f32, LayoutPixel, LayoutPixel>;
98 pub type LayerTransform = TypedTransform3D<f32, LayerPixel, LayerPixel>;
99 pub type LayerToScrollTransform = TypedTransform3D<f32, LayerPixel, ScrollLayerPixel>;
100 pub type ScrollToLayerTransform = TypedTransform3D<f32, ScrollLayerPixel, LayerPixel>;
101 pub type LayerToWorldTransform = TypedTransform3D<f32, LayerPixel, WorldPixel>;
102 pub type WorldToLayerTransform = TypedTransform3D<f32, WorldPixel, LayerPixel>;
103 pub type ScrollToWorldTransform = TypedTransform3D<f32, ScrollLayerPixel, WorldPixel>;
104 
105 // Fixed position coordinates, to avoid float precision errors.
106 pub type LayerPointAu = TypedPoint2D<Au, LayerPixel>;
107 pub type LayerRectAu = TypedRect<Au, LayerPixel>;
108 pub type LayerSizeAu = TypedSize2D<Au, LayerPixel>;
109 
as_scroll_parent_rect(rect: &LayerRect) -> ScrollLayerRect110 pub fn as_scroll_parent_rect(rect: &LayerRect) -> ScrollLayerRect {
111     ScrollLayerRect::from_untyped(&rect.to_untyped())
112 }
113 
as_scroll_parent_vector(vector: &LayerVector2D) -> ScrollLayerVector2D114 pub fn as_scroll_parent_vector(vector: &LayerVector2D) -> ScrollLayerVector2D {
115     ScrollLayerVector2D::from_untyped(&vector.to_untyped())
116 }
117 
118 /// Stores two coordinates in texel space. The coordinates
119 /// are stored in texel coordinates because the texture atlas
120 /// may grow. Storing them as texel coords and normalizing
121 /// the UVs in the vertex shader means nothing needs to be
122 /// updated on the CPU when the texture size changes.
123 #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
124 pub struct TexelRect {
125     pub uv0: DevicePoint,
126     pub uv1: DevicePoint,
127 }
128 
129 impl TexelRect {
new(u0: f32, v0: f32, u1: f32, v1: f32) -> Self130     pub fn new(u0: f32, v0: f32, u1: f32, v1: f32) -> Self {
131         TexelRect {
132             uv0: DevicePoint::new(u0, v0),
133             uv1: DevicePoint::new(u1, v1),
134         }
135     }
136 
invalid() -> Self137     pub fn invalid() -> Self {
138         TexelRect {
139             uv0: DevicePoint::new(-1.0, -1.0),
140             uv1: DevicePoint::new(-1.0, -1.0),
141         }
142     }
143 }
144