diff -r 000000000000 -r 6474c204b198 layout/base/UnitTransforms.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/layout/base/UnitTransforms.h Wed Dec 31 06:09:35 2014 +0100 @@ -0,0 +1,91 @@ +/* -*- 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" + +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. + +MOZ_BEGIN_ENUM_CLASS(PixelCastJustification, uint8_t) + // For the root layer, Screen Pixel = Parent Layer Pixel. + ScreenToParentLayerForRoot, + // For the root composition size we want to view it as layer pixels in any layer + ParentLayerToLayerForRootComposition +MOZ_END_ENUM_CLASS(PixelCastJustification) + +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); +} + +// 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::RectTyped ViewAs(const gfxRect& 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 casting typed entities to untyped 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 +gfxPoint ViewAsUntyped(const gfx::PointTyped& aPoint) { + return gfxPoint(aPoint.x, aPoint.y); +} +template +gfxRect ViewAsUntyped(const gfx::RectTyped& aRect) { + return gfxRect(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 gfx3DMatrix& aTransform, + const gfx::PointTyped& aPoint) +{ + return ViewAs(aTransform.Transform(ViewAsUntyped(aPoint))); +} +template +static gfx::RectTyped TransformTo(const gfx3DMatrix& aTransform, + const gfx::RectTyped& aRect) +{ + return ViewAs(aTransform.TransformBounds(ViewAsUntyped(aRect))); +} + + +} + +#endif