| 
									
										
										
										
											2022-02-28 12:13:23 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |  * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org> | 
					
						
							| 
									
										
										
										
											2022-02-28 12:13:23 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Painting/PaintContext.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 17:33:50 +02:00
										 |  |  | static u64 s_next_paint_generation_id = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-23 18:40:10 +02:00
										 |  |  | PaintContext::PaintContext(Painting::DisplayListRecorder& display_list_recorder, Palette const& palette, double device_pixels_per_css_pixel) | 
					
						
							|  |  |  |     : m_display_list_recorder(display_list_recorder) | 
					
						
							| 
									
										
										
										
											2022-02-28 12:13:23 +01:00
										 |  |  |     , m_palette(palette) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     , m_device_pixels_per_css_pixel(device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2024-04-29 17:33:50 +02:00
										 |  |  |     , m_paint_generation_id(s_next_paint_generation_id++) | 
					
						
							| 
									
										
										
										
											2022-02-28 12:13:23 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-27 14:37:05 +01:00
										 |  |  | CSSPixelRect PaintContext::css_viewport_rect() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         m_device_viewport_rect.x().value() / m_device_pixels_per_css_pixel, | 
					
						
							|  |  |  |         m_device_viewport_rect.y().value() / m_device_pixels_per_css_pixel, | 
					
						
							|  |  |  |         m_device_viewport_rect.width().value() / m_device_pixels_per_css_pixel, | 
					
						
							|  |  |  |         m_device_viewport_rect.height().value() / m_device_pixels_per_css_pixel | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | DevicePixels PaintContext::rounded_device_pixels(CSSPixels css_pixels) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |     return round(css_pixels.to_double() * m_device_pixels_per_css_pixel); | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixels PaintContext::enclosing_device_pixels(CSSPixels css_pixels) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |     return ceil(css_pixels.to_double() * m_device_pixels_per_css_pixel); | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixels PaintContext::floored_device_pixels(CSSPixels css_pixels) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |     return floor(css_pixels.to_double() * m_device_pixels_per_css_pixel); | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixelPoint PaintContext::rounded_device_point(CSSPixelPoint point) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         round(point.x().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         round(point.y().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-10 12:34:39 +01:00
										 |  |  | DevicePixelPoint PaintContext::floored_device_point(CSSPixelPoint point) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         floor(point.x().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         floor(point.y().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2023-04-10 12:34:39 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | DevicePixelRect PaintContext::enclosing_device_rect(CSSPixelRect rect) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         floor(rect.x().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         floor(rect.y().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         ceil(rect.width().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         ceil(rect.height().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixelRect PaintContext::rounded_device_rect(CSSPixelRect rect) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         round(rect.x().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         round(rect.y().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         round(rect.width().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         round(rect.height().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixelSize PaintContext::enclosing_device_size(CSSPixelSize size) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         ceil(size.width().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         ceil(size.height().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DevicePixelSize PaintContext::rounded_device_size(CSSPixelSize size) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							| 
									
										
										
										
											2023-07-30 15:17:23 +01:00
										 |  |  |         round(size.width().to_double() * m_device_pixels_per_css_pixel), | 
					
						
							|  |  |  |         round(size.height().to_double() * m_device_pixels_per_css_pixel) | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixels PaintContext::scale_to_css_pixels(DevicePixels device_pixels) const | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-08-26 15:57:31 +01:00
										 |  |  |     return CSSPixels::nearest_value_for(device_pixels.value() / m_device_pixels_per_css_pixel); | 
					
						
							| 
									
										
										
										
											2022-10-26 20:53:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixelPoint PaintContext::scale_to_css_point(DevicePixelPoint point) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         scale_to_css_pixels(point.x()), | 
					
						
							|  |  |  |         scale_to_css_pixels(point.y()) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 19:01:49 -04:00
										 |  |  | CSSPixelSize PaintContext::scale_to_css_size(DevicePixelSize size) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         scale_to_css_pixels(size.width()), | 
					
						
							|  |  |  |         scale_to_css_pixels(size.height()) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | CSSPixelRect PaintContext::scale_to_css_rect(DevicePixelRect rect) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |         scale_to_css_point(rect.location()), | 
					
						
							|  |  |  |         scale_to_css_size(rect.size()) | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 12:13:23 +01:00
										 |  |  | } |