/* -*- 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" 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, // 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. LayoutDeviceToScreenForUntransformedEvent }; 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::ScaleFactor ViewTargetAs( const gfx::ScaleFactor& aScaleFactor, PixelCastJustification) { return gfx::ScaleFactor(aScaleFactor.scale); } // 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::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); } // Convenience functions for transforming an entity from one strongly-typed // coordinate system to another using the provided transformation matrix. template static gfx::PointTyped TransformTo(const gfx::Matrix4x4& aTransform, const gfx::PointTyped& aPoint) { return ViewAs(aTransform * aPoint.ToUnknownPoint()); } template static gfx::IntPointTyped TransformTo(const gfx::Matrix4x4& aTransform, const gfx::IntPointTyped& aPoint) { return RoundedToInt(TransformTo(aTransform, gfx::PointTyped(aPoint))); } template static gfx::RectTyped TransformTo(const gfx::Matrix4x4& aTransform, const gfx::RectTyped& aRect) { return ViewAs(aTransform.TransformBounds(aRect.ToUnknownRect())); } template static gfx::IntRectTyped TransformTo(const gfx::Matrix4x4& aTransform, const gfx::IntRectTyped& aRect) { gfx::Rect rect(aRect.ToUnknownRect()); return RoundedToInt(ViewAs(aTransform.TransformBounds(rect))); } // 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::Matrix4x4& aTransform, const gfx::PointTyped& aVector, const gfx::PointTyped& aAnchor) { gfx::PointTyped transformedStart = TransformTo(aTransform, aAnchor); gfx::PointTyped transformedEnd = TransformTo(aTransform, aAnchor + aVector); return transformedEnd - transformedStart; } } #endif