Bug 1359842 - Replace use of NSRectToRect in WebRender-related code with proper LayoutDevicePixel types. r=nical
authorKartikaya Gupta <kgupta@mozilla.com>
Thu, 27 Apr 2017 12:04:12 -0400
changeset 569618 1e8212d501798bd2661cabe667e495a4488c5cbc
parent 569617 3d7c750e62c7f376b10c27c07c21512d3158de24
child 569619 c53b024ad6be2912038afee1f7613824722df71e
push id56240
push usermozilla@buttercookie.de
push dateThu, 27 Apr 2017 18:44:39 +0000
reviewersnical
bugs1359842
milestone55.0a1
Bug 1359842 - Replace use of NSRectToRect in WebRender-related code with proper LayoutDevicePixel types. r=nical MozReview-Commit-ID: 7MXFubPtLoD
gfx/layers/wr/WebRenderDisplayItemLayer.cpp
layout/base/UnitTransforms.h
layout/generic/nsBulletFrame.cpp
layout/painting/nsDisplayList.cpp
layout/painting/nsImageRenderer.cpp
--- a/gfx/layers/wr/WebRenderDisplayItemLayer.cpp
+++ b/gfx/layers/wr/WebRenderDisplayItemLayer.cpp
@@ -7,16 +7,17 @@
 
 #include "LayersLogging.h"
 #include "mozilla/webrender/webrender_ffi.h"
 #include "mozilla/webrender/WebRenderTypes.h"
 #include "mozilla/layers/WebRenderBridgeChild.h"
 #include "nsDisplayList.h"
 #include "mozilla/gfx/Matrix.h"
 #include "mozilla/layers/UpdateImageHelper.h"
+#include "UnitTransforms.h"
 
 namespace mozilla {
 namespace layers {
 
 WebRenderDisplayItemLayer::~WebRenderDisplayItemLayer()
 {
   MOZ_COUNT_DTOR(WebRenderDisplayItemLayer);
   if (mKey.isSome()) {
@@ -129,44 +130,50 @@ WebRenderDisplayItemLayer::PushItemAsIma
     mImageClient->Connect();
   }
 
   if (mExternalImageId.isNothing()) {
     MOZ_ASSERT(mImageClient);
     mExternalImageId = Some(WrBridge()->AllocExternalImageIdForCompositable(mImageClient));
   }
 
+  const int32_t appUnitsPerDevPixel = mItem->Frame()->PresContext()->AppUnitsPerDevPixel();
+
   bool snap;
-  gfx::Rect bounds = mozilla::NSRectToRect(mItem->GetBounds(mBuilder, &snap),
-                                      mItem->Frame()->PresContext()->AppUnitsPerDevPixel());
-  gfx::IntSize imageSize = RoundedToInt(bounds.Size());
+  LayerRect bounds = ViewAs<LayerPixel>(
+      LayoutDeviceRect::FromAppUnits(mItem->GetBounds(mBuilder, &snap), appUnitsPerDevPixel),
+      PixelCastJustification::WebRenderHasUnitResolution);
+  LayerIntSize imageSize = RoundedToInt(bounds.Size());
+  LayerRect imageRect;
+  imageRect.SizeTo(LayerSize(imageSize));
 
-  UpdateImageHelper helper(mImageContainer, mImageClient, imageSize);
-  const int32_t appUnitsPerDevPixel = mItem->Frame()->PresContext()->AppUnitsPerDevPixel();
-  gfx::Point offset = NSPointToPoint(mItem->ToReferenceFrame(), appUnitsPerDevPixel);
+  UpdateImageHelper helper(mImageContainer, mImageClient, imageSize.ToUnknownSize());
+  LayerPoint offset = ViewAs<LayerPixel>(
+      LayoutDevicePoint::FromAppUnits(mItem->ToReferenceFrame(), appUnitsPerDevPixel),
+      PixelCastJustification::WebRenderHasUnitResolution);
 
   {
     RefPtr<gfx::DrawTarget> target = helper.GetDrawTarget();
     if (!target) {
       return false;
     }
 
-    target->ClearRect(gfx::Rect(0, 0, imageSize.width, imageSize.height));
-    RefPtr<gfxContext> context = gfxContext::CreateOrNull(target, offset);
+    target->ClearRect(imageRect.ToUnknownRect());
+    RefPtr<gfxContext> context = gfxContext::CreateOrNull(target, offset.ToUnknownPoint());
     MOZ_ASSERT(context);
 
     nsRenderingContext ctx(context);
     mItem->Paint(mBuilder, &ctx);
   }
 
   if (!helper.UpdateImage()) {
     return false;
   }
 
-  gfx::Rect dest = RelativeToParent(gfx::Rect(0, 0, imageSize.width, imageSize.height)) + offset;
+  gfx::Rect dest = RelativeToParent(imageRect.ToUnknownRect()) + offset.ToUnknownPoint();
   WrClipRegion clipRegion = aBuilder.BuildClipRegion(wr::ToWrRect(dest));
   WrImageKey key = GetImageKey();
   aParentCommands.AppendElement(layers::OpAddExternalImage(
                                 mExternalImageId.value(),
                                 key));
   aBuilder.PushImage(wr::ToWrRect(dest),
                      clipRegion,
                      WrImageRendering::Auto,
--- a/layout/base/UnitTransforms.h
+++ b/layout/base/UnitTransforms.h
@@ -50,17 +50,22 @@ enum class PixelCastJustification : uint
   // 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
+  NoTransformOnLayer,
+  // When building non-rasterized WebRender layers (e.g.
+  // WebRenderDisplayItemLayer, or anything else that doesn't deal in textures),
+  // there is no "resolution" and so the LayoutDevicePixel space is equal to the
+  // LayerPixel space.
+  WebRenderHasUnitResolution
 };
 
 template <class TargetUnits, class SourceUnits>
 gfx::CoordTyped<TargetUnits> ViewAs(const gfx::CoordTyped<SourceUnits>& aCoord, PixelCastJustification) {
   return gfx::CoordTyped<TargetUnits>(aCoord.value);
 }
 template <class TargetUnits, class SourceUnits>
 gfx::SizeTyped<TargetUnits> ViewAs(const gfx::SizeTyped<SourceUnits>& aSize, PixelCastJustification) {
--- a/layout/generic/nsBulletFrame.cpp
+++ b/layout/generic/nsBulletFrame.cpp
@@ -458,18 +458,17 @@ BulletRenderer::CreateWebRenderCommandsF
   }
 
   Maybe<wr::ImageKey> key = layer->SendImageContainer(container, aParentCommands);
   if (key.isNothing()) {
     return;
   }
 
   const int32_t appUnitsPerDevPixel = aItem->Frame()->PresContext()->AppUnitsPerDevPixel();
-  Rect destRect =
-    NSRectToRect(mDest, appUnitsPerDevPixel);
+  Rect destRect = LayoutDeviceRect::FromAppUnits(mDest, appUnitsPerDevPixel).ToUnknownRect();
   Rect destRectTransformed = aLayer->RelativeToParent(destRect);
   IntRect dest = RoundedToInt(destRectTransformed);
 
   WrClipRegion clipRegion = aBuilder.BuildClipRegion(wr::ToWrRect(dest));
 
   aBuilder.PushImage(wr::ToWrRect(dest),
                      clipRegion,
                      WrImageRendering::Auto,
@@ -498,18 +497,18 @@ BulletRenderer::CreateWebRenderCommandsF
   MOZ_ASSERT(IsTextType());
   MOZ_ASSERT(mFont);
   MOZ_ASSERT(!mGlyphs.IsEmpty());
 
   layers::WebRenderDisplayItemLayer* layer = static_cast<layers::WebRenderDisplayItemLayer*>(aLayer);
   nsDisplayListBuilder* builder = layer->GetDisplayListBuilder();
   const int32_t appUnitsPerDevPixel = aItem->Frame()->PresContext()->AppUnitsPerDevPixel();
   bool dummy;
-  Rect destRect =
-    NSRectToRect(aItem->GetBounds(builder, &dummy), appUnitsPerDevPixel);
+  Rect destRect = LayoutDeviceRect::FromAppUnits(
+      aItem->GetBounds(builder, &dummy), appUnitsPerDevPixel).ToUnknownRect();
   Rect destRectTransformed = aLayer->RelativeToParent(destRect);
 
   layer->WrBridge()->PushGlyphs(aBuilder, mGlyphs, mFont, aLayer->GetOffsetToParent(),
                                 destRectTransformed, destRectTransformed);
 }
 
 class nsDisplayBullet final : public nsDisplayItem {
 public:
--- a/layout/painting/nsDisplayList.cpp
+++ b/layout/painting/nsDisplayList.cpp
@@ -4225,17 +4225,18 @@ nsDisplayOutline::CreateWebRenderCommand
                                           nsTArray<WebRenderParentCommand>& aParentCommands,
                                           WebRenderDisplayItemLayer* aLayer)
 {
   MOZ_ASSERT(mBorderRenderer.isSome());
 
   gfx::Rect clip(0, 0, 0, 0);
   if (GetClip().HasClip()) {
     int32_t appUnitsPerDevPixel = mFrame->PresContext()->AppUnitsPerDevPixel();
-    clip = NSRectToRect(GetClip().GetClipRect(), appUnitsPerDevPixel);
+    clip = LayoutDeviceRect::FromAppUnits(
+        GetClip().GetClipRect(), appUnitsPerDevPixel).ToUnknownRect();
   }
   mBorderRenderer->CreateWebRenderCommands(aBuilder, aLayer, clip);
 }
 
 bool
 nsDisplayOutline::IsInvisibleInRect(const nsRect& aRect)
 {
   const nsStyleOutline* outline = mFrame->StyleOutline();
@@ -4505,20 +4506,20 @@ nsDisplayCaret::CreateWebRenderCommands(
 
   int32_t appUnitsPerDevPixel = frame->PresContext()->AppUnitsPerDevPixel();
 
   nsRect caretRect;
   nsRect hookRect;
   mCaret->ComputeCaretRects(frame, contentOffset, &caretRect, &hookRect);
 
   gfx::Color color = ToDeviceColor(frame->GetCaretColorAt(contentOffset));
-  Rect devCaretRect =
-    NSRectToRect(caretRect + ToReferenceFrame(), appUnitsPerDevPixel);
-  Rect devHookRect =
-    NSRectToRect(hookRect + ToReferenceFrame(), appUnitsPerDevPixel);
+  Rect devCaretRect = LayoutDeviceRect::FromAppUnits(
+    caretRect + ToReferenceFrame(), appUnitsPerDevPixel).ToUnknownRect();
+  Rect devHookRect = LayoutDeviceRect::FromAppUnits(
+    hookRect + ToReferenceFrame(), appUnitsPerDevPixel).ToUnknownRect();
 
   Rect caretTransformedRect = aLayer->RelativeToParent(devCaretRect);
   Rect hookTransformedRect = aLayer->RelativeToParent(devHookRect);
 
   IntRect caret = RoundedToInt(caretTransformedRect);
   IntRect hook = RoundedToInt(hookTransformedRect);
 
   // Note, WR will pixel snap anything that is layout aligned.
@@ -4763,25 +4764,25 @@ nsDisplayBorder::CreateBorderImageWebRen
   float outset[4];
   const int32_t appUnitsPerDevPixel = mFrame->PresContext()->AppUnitsPerDevPixel();
   NS_FOR_CSS_SIDES(i) {
     slice[i] = (float)(mBorderImageRenderer->mSlice.Side(i)) / appUnitsPerDevPixel;
     widths[i] = (float)(mBorderImageRenderer->mWidths.Side(i)) / appUnitsPerDevPixel;
     outset[i] = (float)(mBorderImageRenderer->mImageOutset.Side(i)) / appUnitsPerDevPixel;
   }
 
-  Rect destRect =
-    NSRectToRect(mBorderImageRenderer->mArea, appUnitsPerDevPixel);
+  Rect destRect = LayoutDeviceRect::FromAppUnits(
+    mBorderImageRenderer->mArea, appUnitsPerDevPixel).ToUnknownRect();
   Rect destRectTransformed = aLayer->RelativeToParent(destRect);
   IntRect dest = RoundedToInt(destRectTransformed);
 
   IntRect clip = dest;
   if (!mBorderImageRenderer->mClip.IsEmpty()) {
-    Rect clipRect =
-      NSRectToRect(mBorderImageRenderer->mClip, appUnitsPerDevPixel);
+    Rect clipRect = LayoutDeviceRect::FromAppUnits(
+      mBorderImageRenderer->mClip, appUnitsPerDevPixel).ToUnknownRect();
     Rect clipRectTransformed = aLayer->RelativeToParent(clipRect);
     clip = RoundedToInt(clipRectTransformed);
   }
 
   switch (mBorderImageRenderer->mImageRenderer.GetType()) {
     case eStyleImageType_Image:
     {
       nsDisplayListBuilder* builder = aLayer->GetDisplayListBuilder();
@@ -4866,17 +4867,18 @@ nsDisplayBorder::CreateWebRenderCommands
   MOZ_ASSERT(mBorderImageRenderer || mBorderRenderer);
 
   if (mBorderImageRenderer) {
     CreateBorderImageWebRenderCommands(aBuilder, aParentCommands, aLayer);
   } else if (mBorderRenderer) {
     gfx::Rect clip(0, 0, 0, 0);
     if (GetClip().HasClip()) {
       int32_t appUnitsPerDevPixel = mFrame->PresContext()->AppUnitsPerDevPixel();
-      clip = NSRectToRect(GetClip().GetClipRect(), appUnitsPerDevPixel);
+      clip = LayoutDeviceRect::FromAppUnits(
+          GetClip().GetClipRect(), appUnitsPerDevPixel).ToUnknownRect();
     }
 
     mBorderRenderer->CreateWebRenderCommands(aBuilder, aLayer, clip);
   }
 }
 
 void
 nsDisplayBorder::Paint(nsDisplayListBuilder* aBuilder,
@@ -5148,17 +5150,18 @@ nsDisplayBoxShadowOuter::CreateWebRender
                                                      borderRect,
                                                      mFrame,
                                                      borderRadii);
     MOZ_ASSERT(borderRadii.AreRadiiSame());
   }
 
   // Everything here is in app units, change to device units.
   for (uint32_t i = 0; i < rects.Length(); ++i) {
-    Rect clipRect = NSRectToRect(rects[i], appUnitsPerDevPixel);
+    Rect clipRect = LayoutDeviceRect::FromAppUnits(
+        rects[i], appUnitsPerDevPixel).ToUnknownRect();
     nsCSSShadowArray* shadows = mFrame->StyleEffects()->mBoxShadow;
     MOZ_ASSERT(shadows);
 
     for (uint32_t j = shadows->Length(); j  > 0; j--) {
       nsCSSShadowItem* shadow = shadows->ShadowAt(j - 1);
       float blurRadius = float(shadow->mRadius) / float(appUnitsPerDevPixel);
       gfx::Color shadowColor = nsCSSRendering::GetShadowColor(shadow,
                                                               mFrame,
@@ -5166,17 +5169,18 @@ nsDisplayBoxShadowOuter::CreateWebRender
 
       // We don't move the shadow rect here since WR does it for us
       // Now translate everything to device pixels.
       nsRect shadowRect = frameRect;
       Point shadowOffset;
       shadowOffset.x = (shadow->mXOffset / appUnitsPerDevPixel);
       shadowOffset.y = (shadow->mYOffset / appUnitsPerDevPixel);
 
-      Rect deviceBoxRect = NSRectToRect(shadowRect, appUnitsPerDevPixel);
+      Rect deviceBoxRect = LayoutDeviceRect::FromAppUnits(
+          shadowRect, appUnitsPerDevPixel).ToUnknownRect();
       deviceBoxRect = aLayer->RelativeToParent(deviceBoxRect);
       deviceBoxRect.Round();
       Rect deviceClipRect = aLayer->RelativeToParent(clipRect);
 
       // TODO: support non-uniform border radius.
       float borderRadius = hasBorderRadius ? borderRadii.TopLeft().width
                                            : 0.0;
       float spreadRadius = float(shadow->mSpread) / float(appUnitsPerDevPixel);
@@ -5338,31 +5342,33 @@ nsDisplayBoxShadowInner::CreateInsetBoxS
 
   AutoTArray<nsRect,10> rects;
   nsRegion visible = aLayer->GetVisibleRegion().ToAppUnits(appUnitsPerDevPixel);
   ComputeDisjointRectangles(visible, &rects);
 
   nsCSSShadowArray* shadows = aFrame->StyleEffects()->mBoxShadow;
 
   for (uint32_t i = 0; i < rects.Length(); ++i) {
-    Rect clipRect = NSRectToRect(rects[i], appUnitsPerDevPixel);
+    Rect clipRect = LayoutDeviceRect::FromAppUnits(
+        rects[i], appUnitsPerDevPixel).ToUnknownRect();
 
     for (uint32_t i = shadows->Length(); i > 0; --i) {
       nsCSSShadowItem* shadowItem = shadows->ShadowAt(i - 1);
       if (!shadowItem->mInset) {
         continue;
       }
 
       nsRect shadowRect =
         nsCSSRendering::GetBoxShadowInnerPaddingRect(aFrame, aBorderRect);
       RectCornerRadii innerRadii;
       nsCSSRendering::GetShadowInnerRadii(aFrame, aBorderRect, innerRadii);
 
       // Now translate everything to device pixels.
-      Rect deviceBoxRect = NSRectToRect(shadowRect, appUnitsPerDevPixel);
+      Rect deviceBoxRect = LayoutDeviceRect::FromAppUnits(
+          shadowRect, appUnitsPerDevPixel).ToUnknownRect();
       Rect deviceClipRect = aLayer->RelativeToParent(clipRect);
       Color shadowColor = nsCSSRendering::GetShadowColor(shadowItem, aFrame, 1.0);
 
       Point shadowOffset;
       shadowOffset.x = (shadowItem->mXOffset / appUnitsPerDevPixel);
       shadowOffset.y = (shadowItem->mYOffset / appUnitsPerDevPixel);
 
       float blurRadius = float(shadowItem->mRadius) / float(appUnitsPerDevPixel);
--- a/layout/painting/nsImageRenderer.cpp
+++ b/layout/painting/nsImageRenderer.cpp
@@ -616,20 +616,22 @@ nsImageRenderer::BuildWebRenderDisplayIt
         return DrawResult::BAD_IMAGE;
       }
       Maybe<wr::ImageKey> key = aLayer->SendImageContainer(container, aParentCommands);
       if (key.isNothing()) {
         return DrawResult::BAD_IMAGE;
       }
 
       const int32_t appUnitsPerDevPixel = mForFrame->PresContext()->AppUnitsPerDevPixel();
-      Rect destRect = NSRectToRect(aDest, appUnitsPerDevPixel);
+      Rect destRect = LayoutDeviceRect::FromAppUnits(
+          aDest, appUnitsPerDevPixel).ToUnknownRect();
       Rect dest = aLayer->RelativeToParent(destRect);
 
-      Rect fillRect = NSRectToRect(aFill, appUnitsPerDevPixel);
+      Rect fillRect = LayoutDeviceRect::FromAppUnits(
+          aFill, appUnitsPerDevPixel).ToUnknownRect();
       Rect fill = aLayer->RelativeToParent(fillRect);
 
       Rect clip = fill;
       Size gapSize((aRepeatSize.width - aDest.width) / appUnitsPerDevPixel,
                    (aRepeatSize.height - aDest.height) / appUnitsPerDevPixel);
       aBuilder.PushImage(wr::ToWrRect(fill), aBuilder.BuildClipRegion(wr::ToWrRect(clip)),
                          wr::ToWrSize(dest.Size()), wr::ToWrSize(gapSize),
                          wr::ImageRendering::Auto, key.value());