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/. */
6 
7 #include "LayersHelpers.h"
8 
9 namespace mozilla {
10 namespace layers {
11 
12 using namespace gfx;
13 
ComputeBackdropCopyRect(const gfx::Rect & aRect,const gfx::IntRect & aClipRect,const gfx::Matrix4x4 & aTransform,const gfx::IntRect & aRenderTargetRect,gfx::Matrix4x4 * aOutTransform,gfx::Rect * aOutLayerQuad)14 gfx::IntRect ComputeBackdropCopyRect(const gfx::Rect& aRect,
15                                      const gfx::IntRect& aClipRect,
16                                      const gfx::Matrix4x4& aTransform,
17                                      const gfx::IntRect& aRenderTargetRect,
18                                      gfx::Matrix4x4* aOutTransform,
19                                      gfx::Rect* aOutLayerQuad) {
20   // Compute the clip.
21   IntPoint rtOffset = aRenderTargetRect.TopLeft();
22   IntSize rtSize = aRenderTargetRect.Size();
23 
24   gfx::IntRect renderBounds(0, 0, rtSize.width, rtSize.height);
25   renderBounds.IntersectRect(renderBounds, aClipRect);
26   renderBounds.MoveBy(rtOffset);
27 
28   // Apply the layer transform.
29   RectDouble dest = aTransform.TransformAndClipBounds(
30       RectDouble(aRect.X(), aRect.Y(), aRect.Width(), aRect.Height()),
31       RectDouble(renderBounds.X(), renderBounds.Y(), renderBounds.Width(),
32                  renderBounds.Height()));
33   dest -= rtOffset;
34 
35   // Ensure we don't round out to -1, which trips up Direct3D.
36   dest.IntersectRect(dest, RectDouble(0, 0, rtSize.width, rtSize.height));
37 
38   if (aOutLayerQuad) {
39     *aOutLayerQuad = Rect(dest.X(), dest.Y(), dest.Width(), dest.Height());
40   }
41 
42   // Round out to integer.
43   IntRect result;
44   dest.RoundOut();
45   dest.ToIntRect(&result);
46 
47   // Create a transform from adjusted clip space to render target space,
48   // translate it for the backdrop rect, then transform it into the backdrop's
49   // uv-space.
50   Matrix4x4 transform;
51   transform.PostScale(rtSize.width, rtSize.height, 1.0);
52   transform.PostTranslate(-result.X(), -result.Y(), 0.0);
53   transform.PostScale(1 / float(result.Width()), 1 / float(result.Height()),
54                       1.0);
55   *aOutTransform = transform;
56   return result;
57 }
58 
59 }  // namespace layers
60 }  // namespace mozilla
61