/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef MOZ_UNIT_TRANSFORMS_H_ #define MOZ_UNIT_TRANSFORMS_H_ #include "Units.h" #include "mozilla/gfx/Matrix.h" #include "mozilla/Maybe.h" #include "nsRegion.h" namespace mozilla { // Convenience functions for converting an entity from one strongly-typed // coordinate system to another without changing the values it stores (this // can be thought of as a cast). // To use these functions, you must provide a justification for each use! // Feel free to add more justifications to PixelCastJustification, along with // a comment that explains under what circumstances it is appropriate to use. enum class PixelCastJustification : uint8_t { // For the root layer, Screen Pixel = Parent Layer Pixel. ScreenIsParentLayerForRoot, // On the layout side, Screen Pixel = LayoutDevice at the outer-window level. LayoutDeviceIsScreenForBounds, // For the root layer, Render Target Pixel = Parent Layer Pixel. RenderTargetIsParentLayerForRoot, // For the root composition size we want to view it as layer pixels in any // layer ParentLayerToLayerForRootComposition, // The Layer coordinate space for one layer is the ParentLayer coordinate // space for its children MovingDownToChildren, // The transform that is usually used to convert between two coordinate // systems is not available (for example, because the object that stores it // is being destroyed), so fall back to the identity. TransformNotAvailable, // When an OS event is initially constructed, its reference point is // technically in screen pixels, as it has not yet accounted for any // asynchronous transforms. This justification is for viewing the initial // reference point as a screen point. The reverse is useful when synthetically // created WidgetEvents need to be converted back to InputData. LayoutDeviceIsScreenForUntransformedEvent, // Similar to LayoutDeviceIsScreenForUntransformedEvent, PBrowser handles // some widget/tab dimension information as the OS does -- in screen units. LayoutDeviceIsScreenForTabDims, // A combination of LayoutDeviceIsScreenForBounds and // ScreenIsParentLayerForRoot, which is how we're using it. LayoutDeviceIsParentLayerForRCDRSF, // Used to treat the product of AsyncTransformComponentMatrix objects // as an AsyncTransformMatrix. See the definitions of these matrices in // LayersTypes.h for details. MultipleAsyncTransforms, // We have reason to believe a layer doesn't have a local transform. // Should only be used if we've already checked or asserted this. NoTransformOnLayer, // LayerPixels are ImagePixels LayerIsImage, // External pixels are the same scale as screen pixels ExternalIsScreen, // LayerToScreenMatrix is used as LayoutDeviceToLayoutDevice, because // out-of-process iframes uses LayoutDevicePixels as the type system-visible // type of their top-level event coordinate space even if technically // inaccurate. ContentProcessIsLayerInUiProcess, // Propagating TransformToAncestorScale to a child process. PropagatingToChildProcess, // A quantity represents a proportion of a page length, e.g. "0.5 pages". // The proportion does not need to be scaled when converting between // units (the page length that it's mutlipled by will be scaled instead). DeltaIsPageProportion, // Used to cast between CSS and OuterCSS pixels when moving between code // that deals with content outside a scroll frame generically (which would // use CSS pixels) and code related to the scroll frame in APZ (which wants // such quantities in OuterCSS pixels). CSSPixelsOfSurroundingContent, }; template gfx::CoordTyped ViewAs(const gfx::CoordTyped& aCoord, PixelCastJustification) { return gfx::CoordTyped(aCoord.value); } template gfx::IntCoordTyped ViewAs( const gfx::IntCoordTyped& aCoord, PixelCastJustification) { return gfx::IntCoordTyped(aCoord.value); } template gfx::SizeTyped ViewAs(const gfx::SizeTyped& aSize, PixelCastJustification) { return gfx::SizeTyped(aSize.width, aSize.height); } template gfx::IntSizeTyped ViewAs( const gfx::IntSizeTyped& aSize, PixelCastJustification) { return gfx::IntSizeTyped(aSize.width, aSize.height); } template gfx::PointTyped ViewAs(const gfx::PointTyped& aPoint, PixelCastJustification) { return gfx::PointTyped(aPoint.x, aPoint.y); } template gfx::IntPointTyped ViewAs( const gfx::IntPointTyped& aPoint, PixelCastJustification) { return gfx::IntPointTyped(aPoint.x, aPoint.y); } template gfx::RectTyped ViewAs(const gfx::RectTyped& aRect, PixelCastJustification) { return gfx::RectTyped(aRect.x, aRect.y, aRect.Width(), aRect.Height()); } template gfx::IntRectTyped ViewAs( const gfx::IntRectTyped& aRect, PixelCastJustification) { return gfx::IntRectTyped(aRect.x, aRect.y, aRect.Width(), aRect.Height()); } template gfx::MarginTyped ViewAs( const gfx::MarginTyped& aMargin, PixelCastJustification) { return gfx::MarginTyped(aMargin.top.value, aMargin.right.value, aMargin.bottom.value, aMargin.left.value); } template gfx::IntMarginTyped ViewAs( const gfx::IntMarginTyped& aMargin, PixelCastJustification) { return gfx::IntMarginTyped(aMargin.top, aMargin.right, aMargin.bottom, aMargin.left); } template gfx::IntRegionTyped ViewAs( const gfx::IntRegionTyped& aRegion, PixelCastJustification) { return gfx::IntRegionTyped::FromUnknownRegion( aRegion.ToUnknownRegion()); } template gfx::ScaleFactor ViewTargetAs( const gfx::ScaleFactor& aScaleFactor, PixelCastJustification) { return gfx::ScaleFactor(aScaleFactor.scale); } template gfx::ScaleFactors2D ViewTargetAs( const gfx::ScaleFactors2D& aScaleFactors, PixelCastJustification) { return gfx::ScaleFactors2D(aScaleFactors.xScale, aScaleFactors.yScale); } template Maybe> ViewAs( const Maybe>& aRect, PixelCastJustification aJustification) { if (aRect.isSome()) { return Some(ViewAs(aRect.value(), aJustification)); } return Nothing(); } // Unlike the other functions in this category, these functions take the // target matrix or scale type, rather than its source and target unit types, as // the explicit template argument, so an example invocation is: // ViewAs(otherTypedMatrix, justification) // The reason is that if it took the source and target unit types as two // template arguments, there may be some confusion as to which is the // source and which is the target. template TargetMatrix ViewAs(const gfx::Matrix4x4Typed& aMatrix, PixelCastJustification) { return aMatrix.template Cast(); } template Maybe ViewAs( const Maybe>& aMatrix, PixelCastJustification) { if (aMatrix.isSome()) { return Some(aMatrix->template Cast()); } return Nothing(); } template TargetScale ViewAs(const gfx::ScaleFactor& aScale, PixelCastJustification) { return TargetScale{aScale.scale}; } // A non-member overload of ToUnknownMatrix() for use on a Maybe. // We can't make this a member because we can't inject a member into Maybe. template Maybe ToUnknownMatrix( const Maybe>& aMatrix) { if (aMatrix.isSome()) { return Some(aMatrix->ToUnknownMatrix()); } return Nothing(); } // Convenience functions for casting untyped entities to typed entities. // Using these functions does not require a justification, but once we convert // all code to use strongly typed units they should not be needed any longer. template gfx::CoordTyped ViewAs(const gfx::Coord& aCoord) { return gfx::CoordTyped(aCoord.value); } template gfx::PointTyped ViewAs(const gfxPoint& aPoint) { return gfx::PointTyped(aPoint.x, aPoint.y); } template gfx::PointTyped ViewAs(const gfx::Point& aPoint) { return gfx::PointTyped(aPoint.x, aPoint.y); } template gfx::RectTyped ViewAs(const gfx::Rect& aRect) { return gfx::RectTyped(aRect.x, aRect.y, aRect.Width(), aRect.Height()); } template gfx::IntSizeTyped ViewAs(const nsIntSize& aSize) { return gfx::IntSizeTyped(aSize.width, aSize.height); } template gfx::IntPointTyped ViewAs(const nsIntPoint& aPoint) { return gfx::IntPointTyped(aPoint.x, aPoint.y); } template gfx::IntRectTyped ViewAs(const nsIntRect& aRect) { return gfx::IntRectTyped(aRect.x, aRect.y, aRect.Width(), aRect.Height()); } template gfx::IntRegionTyped ViewAs(const nsIntRegion& aRegion) { return gfx::IntRegionTyped::FromUnknownRegion(aRegion); } // Unlike the other functions in this category, these functions take the // target matrix or scale type, rather than its source and target unit // types, as the template argument, so an example invocation is: // ViewAs(untypedMatrix) // The reason is that if it took the source and target unit types as two // template arguments, there may be some confusion as to which is the // source and which is the target. template TypedScale ViewAs(const Scale2D& aScale) { return TypedScale(aScale.xScale, aScale.yScale); } template TypedMatrix ViewAs(const gfx::Matrix4x4& aMatrix) { return TypedMatrix::FromUnknownMatrix(aMatrix); } // Convenience functions for transforming an entity from one strongly-typed // coordinate system to another using the provided transformation matrix. template static gfx::PointTyped TransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::PointTyped& aPoint) { return aTransform.TransformPoint(aPoint); } template static gfx::IntPointTyped TransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::IntPointTyped& aPoint) { return RoundedToInt( TransformBy(aTransform, gfx::PointTyped(aPoint))); } template static gfx::RectTyped TransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::RectTyped& aRect) { return aTransform.TransformBounds(aRect); } template static gfx::IntRectTyped TransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::IntRectTyped& aRect) { return RoundedToInt( TransformBy(aTransform, gfx::RectTyped(aRect))); } template static gfx::IntRegionTyped TransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::IntRegionTyped& aRegion) { return ViewAs( aRegion.ToUnknownRegion().Transform(aTransform.ToUnknownMatrix())); } // Transform |aVector|, which is anchored at |aAnchor|, by the given transform // matrix, yielding a point in |TargetUnits|. // The anchor is necessary because with 3D tranforms, the location of the // vector can affect the result of the transform. template static gfx::PointTyped TransformVector( const gfx::Matrix4x4Typed& aTransform, const gfx::PointTyped& aVector, const gfx::PointTyped& aAnchor) { gfx::PointTyped transformedStart = TransformBy(aTransform, aAnchor); gfx::PointTyped transformedEnd = TransformBy(aTransform, aAnchor + aVector); return transformedEnd - transformedStart; } // UntransformBy() and UntransformVector() are like TransformBy() and // TransformVector(), respectively, but are intended for cases where // the transformation matrix is the inverse of a 3D projection. When // using such transforms, the resulting Point4D is only meaningful // if it has a positive w-coordinate. To handle this, these functions // return a Maybe object which contains a value if and only if the // result is meaningful template static Maybe> UntransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::PointTyped& aPoint) { gfx::Point4DTyped point = aTransform.ProjectPoint(aPoint); if (!point.HasPositiveWCoord()) { return Nothing(); } return Some(point.As2DPoint()); } template static Maybe> UntransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::IntPointTyped& aPoint) { gfx::PointTyped p = aPoint; gfx::Point4DTyped point = aTransform.ProjectPoint(p); if (!point.HasPositiveWCoord()) { return Nothing(); } return Some(RoundedToInt(point.As2DPoint())); } // The versions of UntransformBy() that take a rectangle also take a clip, // which represents the bounds within which the target must fall. The // result of the transform is intersected with this clip, and is considered // meaningful if the intersection is not empty. template static Maybe> UntransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::RectTyped& aRect, const gfx::RectTyped& aClip) { gfx::RectTyped rect = aTransform.ProjectRectBounds(aRect, aClip); if (rect.IsEmpty()) { return Nothing(); } return Some(rect); } template static Maybe> UntransformBy( const gfx::Matrix4x4Typed& aTransform, const gfx::IntRectTyped& aRect, const gfx::IntRectTyped& aClip) { gfx::RectTyped rect = aTransform.ProjectRectBounds(aRect, aClip); if (rect.IsEmpty()) { return Nothing(); } return Some(RoundedToInt(rect)); } template static Maybe> UntransformVector( const gfx::Matrix4x4Typed& aTransform, const gfx::PointTyped& aVector, const gfx::PointTyped& aAnchor) { gfx::Point4DTyped projectedAnchor = aTransform.ProjectPoint(aAnchor); gfx::Point4DTyped projectedTarget = aTransform.ProjectPoint(aAnchor + aVector); if (!projectedAnchor.HasPositiveWCoord() || !projectedTarget.HasPositiveWCoord()) { return Nothing(); } return Some(projectedTarget.As2DPoint() - projectedAnchor.As2DPoint()); } } // namespace mozilla #endif