Bug 1865597 - Add error checking when initializing parallel marking and disable on...
[gecko.git] / gfx / src / nsPoint.h
blob86688d214240b864771902532b4e7af3b98c3020
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef NSPOINT_H
8 #define NSPOINT_H
10 #include <cstdint>
11 #include "nsCoord.h"
12 #include "mozilla/gfx/BasePoint.h"
13 #include "mozilla/gfx/Point.h"
15 // nsIntPoint represents a point in one of the types of pixels.
16 // Uses of nsIntPoint should eventually be converted to CSSIntPoint,
17 // LayoutDeviceIntPoint, etc. (see layout/base/Units.h).
18 typedef mozilla::gfx::IntPoint nsIntPoint;
20 // nsPoint represents a point in app units.
22 struct nsPoint : public mozilla::gfx::BasePoint<nscoord, nsPoint> {
23 typedef mozilla::gfx::BasePoint<nscoord, nsPoint> Super;
25 nsPoint() = default;
26 nsPoint(const nsPoint& aPoint) = default;
27 nsPoint(nscoord aX, nscoord aY) : Super(aX, aY) {}
29 inline nsIntPoint ScaleToNearestPixels(float aXScale, float aYScale,
30 nscoord aAppUnitsPerPixel) const;
31 inline nsIntPoint ToNearestPixels(nscoord aAppUnitsPerPixel) const;
33 /**
34 * Return this point scaled to a different appunits per pixel (APP) ratio.
35 * @param aFromAPP the APP to scale from
36 * @param aToAPP the APP to scale to
38 [[nodiscard]] inline nsPoint ScaleToOtherAppUnits(int32_t aFromAPP,
39 int32_t aToAPP) const;
41 [[nodiscard]] inline nsPoint RemoveResolution(const float resolution) const;
42 [[nodiscard]] inline nsPoint ApplyResolution(const float resolution) const;
45 inline nsPoint ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel);
47 inline nsIntPoint nsPoint::ScaleToNearestPixels(
48 float aXScale, float aYScale, nscoord aAppUnitsPerPixel) const {
49 return nsIntPoint(
50 NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale),
51 NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale));
54 inline nsIntPoint nsPoint::ToNearestPixels(nscoord aAppUnitsPerPixel) const {
55 return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
58 inline nsPoint nsPoint::ScaleToOtherAppUnits(int32_t aFromAPP,
59 int32_t aToAPP) const {
60 if (aFromAPP != aToAPP) {
61 nsPoint point;
62 point.x = NSToCoordRound(NSCoordScale(x, aFromAPP, aToAPP));
63 point.y = NSToCoordRound(NSCoordScale(y, aFromAPP, aToAPP));
64 return point;
66 return *this;
69 inline nsPoint nsPoint::RemoveResolution(const float resolution) const {
70 if (resolution != 1.0f) {
71 nsPoint point;
72 point.x = NSToCoordRound(NSCoordToFloat(x) / resolution);
73 point.y = NSToCoordRound(NSCoordToFloat(y) / resolution);
74 return point;
76 return *this;
79 inline nsPoint nsPoint::ApplyResolution(const float resolution) const {
80 if (resolution != 1.0f) {
81 nsPoint point;
82 point.x = NSToCoordRound(NSCoordToFloat(x) * resolution);
83 point.y = NSToCoordRound(NSCoordToFloat(y) * resolution);
84 return point;
86 return *this;
89 // app units are integer multiples of pixels, so no rounding needed
90 inline nsPoint ToAppUnits(const nsIntPoint& aPoint, nscoord aAppUnitsPerPixel) {
91 return nsPoint(NSIntPixelsToAppUnits(aPoint.x, aAppUnitsPerPixel),
92 NSIntPixelsToAppUnits(aPoint.y, aAppUnitsPerPixel));
95 #endif /* NSPOINT_H */