1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "OrientedImage.h"
10 #include "gfx2DGlue.h"
11 #include "gfxDrawable.h"
12 #include "gfxPlatform.h"
14 #include "ImageRegion.h"
15 #include "SVGImageContext.h"
22 using layers::ImageContainer
;
23 using layers::LayerManager
;
28 OrientedImage::GetWidth(int32_t* aWidth
) {
29 if (mOrientation
.SwapsWidthAndHeight()) {
30 return InnerImage()->GetHeight(aWidth
);
32 return InnerImage()->GetWidth(aWidth
);
37 OrientedImage::GetHeight(int32_t* aHeight
) {
38 if (mOrientation
.SwapsWidthAndHeight()) {
39 return InnerImage()->GetWidth(aHeight
);
41 return InnerImage()->GetHeight(aHeight
);
45 nsresult
OrientedImage::GetNativeSizes(nsTArray
<IntSize
>& aNativeSizes
) const {
46 nsresult rv
= InnerImage()->GetNativeSizes(aNativeSizes
);
48 if (mOrientation
.SwapsWidthAndHeight()) {
49 auto i
= aNativeSizes
.Length();
52 swap(aNativeSizes
[i
].width
, aNativeSizes
[i
].height
);
60 OrientedImage::GetIntrinsicSize(nsSize
* aSize
) {
61 nsresult rv
= InnerImage()->GetIntrinsicSize(aSize
);
63 if (mOrientation
.SwapsWidthAndHeight()) {
64 swap(aSize
->width
, aSize
->height
);
70 Maybe
<AspectRatio
> OrientedImage::GetIntrinsicRatio() {
71 Maybe
<AspectRatio
> ratio
= InnerImage()->GetIntrinsicRatio();
72 if (ratio
&& mOrientation
.SwapsWidthAndHeight()) {
73 ratio
= Some(ratio
->Inverted());
78 already_AddRefed
<SourceSurface
> OrientedImage::OrientSurface(
79 Orientation aOrientation
, SourceSurface
* aSurface
) {
82 // If the image does not require any re-orientation, return aSurface itself.
83 if (aOrientation
.IsIdentity()) {
84 return do_AddRef(aSurface
);
87 // Determine the size of the new surface.
88 nsIntSize originalSize
= aSurface
->GetSize();
89 nsIntSize targetSize
= originalSize
;
90 if (aOrientation
.SwapsWidthAndHeight()) {
91 swap(targetSize
.width
, targetSize
.height
);
94 // Create our drawable.
95 RefPtr
<gfxDrawable
> drawable
= new gfxSurfaceDrawable(aSurface
, originalSize
);
97 // Determine an appropriate format for the surface.
98 gfx::SurfaceFormat surfaceFormat
= IsOpaque(aSurface
->GetFormat())
99 ? gfx::SurfaceFormat::OS_RGBX
100 : gfx::SurfaceFormat::OS_RGBA
;
102 // Create the new surface to draw into.
103 RefPtr
<DrawTarget
> target
=
104 gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(
105 targetSize
, surfaceFormat
);
106 if (!target
|| !target
->IsValid()) {
107 NS_ERROR("Could not create a DrawTarget");
112 RefPtr
<gfxContext
> ctx
= gfxContext::CreateOrNull(target
);
113 MOZ_ASSERT(ctx
); // already checked the draw target above
114 ctx
->Multiply(OrientationMatrix(aOrientation
, originalSize
));
115 gfxUtils::DrawPixelSnapped(ctx
, drawable
, SizeDouble(originalSize
),
116 ImageRegion::Create(originalSize
), surfaceFormat
,
117 SamplingFilter::LINEAR
);
119 return target
->Snapshot();
122 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
123 OrientedImage::GetFrame(uint32_t aWhichFrame
, uint32_t aFlags
) {
124 // Get a SourceSurface for the inner image then orient it according to
126 RefPtr
<SourceSurface
> innerSurface
=
127 InnerImage()->GetFrame(aWhichFrame
, aFlags
);
128 NS_ENSURE_TRUE(innerSurface
, nullptr);
130 return OrientSurface(mOrientation
, innerSurface
);
133 NS_IMETHODIMP_(already_AddRefed
<SourceSurface
>)
134 OrientedImage::GetFrameAtSize(const IntSize
& aSize
, uint32_t aWhichFrame
,
136 // Get a SourceSurface for the inner image then orient it according to
138 IntSize innerSize
= aSize
;
139 if (mOrientation
.SwapsWidthAndHeight()) {
140 swap(innerSize
.width
, innerSize
.height
);
142 RefPtr
<SourceSurface
> innerSurface
=
143 InnerImage()->GetFrameAtSize(innerSize
, aWhichFrame
, aFlags
);
144 NS_ENSURE_TRUE(innerSurface
, nullptr);
146 return OrientSurface(mOrientation
, innerSurface
);
150 OrientedImage::IsImageContainerAvailable(LayerManager
* aManager
,
152 if (mOrientation
.IsIdentity()) {
153 return InnerImage()->IsImageContainerAvailable(aManager
, aFlags
);
158 NS_IMETHODIMP_(already_AddRefed
<ImageContainer
>)
159 OrientedImage::GetImageContainer(LayerManager
* aManager
, uint32_t aFlags
) {
160 // XXX(seth): We currently don't have a way of orienting the result of
161 // GetImageContainer. We work around this by always returning null, but if it
162 // ever turns out that OrientedImage is widely used on codepaths that can
163 // actually benefit from GetImageContainer, it would be a good idea to fix
164 // that method for performance reasons.
166 if (mOrientation
.IsIdentity()) {
167 return InnerImage()->GetImageContainer(aManager
, aFlags
);
174 OrientedImage::IsImageContainerAvailableAtSize(LayerManager
* aManager
,
175 const IntSize
& aSize
,
177 if (mOrientation
.IsIdentity()) {
178 return InnerImage()->IsImageContainerAvailableAtSize(aManager
, aSize
,
184 NS_IMETHODIMP_(ImgDrawResult
)
185 OrientedImage::GetImageContainerAtSize(
186 layers::LayerManager
* aManager
, const gfx::IntSize
& aSize
,
187 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
188 layers::ImageContainer
** aOutContainer
) {
189 // XXX(seth): We currently don't have a way of orienting the result of
190 // GetImageContainer. We work around this by always returning null, but if it
191 // ever turns out that OrientedImage is widely used on codepaths that can
192 // actually benefit from GetImageContainer, it would be a good idea to fix
193 // that method for performance reasons.
195 if (mOrientation
.IsIdentity()) {
196 return InnerImage()->GetImageContainerAtSize(aManager
, aSize
, aSVGContext
,
197 aFlags
, aOutContainer
);
200 return ImgDrawResult::NOT_SUPPORTED
;
203 struct MatrixBuilder
{
204 explicit MatrixBuilder(bool aInvert
) : mInvert(aInvert
) {}
206 gfxMatrix
Build() { return mMatrix
; }
208 void Scale(gfxFloat aX
, gfxFloat aY
) {
210 mMatrix
*= gfxMatrix::Scaling(1.0 / aX
, 1.0 / aY
);
212 mMatrix
.PreScale(aX
, aY
);
216 void Rotate(gfxFloat aPhi
) {
218 mMatrix
*= gfxMatrix::Rotation(-aPhi
);
220 mMatrix
.PreRotate(aPhi
);
224 void Translate(gfxPoint aDelta
) {
226 mMatrix
*= gfxMatrix::Translation(-aDelta
);
228 mMatrix
.PreTranslate(aDelta
);
237 gfxMatrix
OrientedImage::OrientationMatrix(Orientation aOrientation
,
238 const nsIntSize
& aSize
,
239 bool aInvert
/* = false */) {
240 MatrixBuilder
builder(aInvert
);
242 // Apply reflection, if present. (For a regular, non-flipFirst reflection,
243 // this logically happens second, but we apply it first because these
244 // transformations are all premultiplied.) A translation is necessary to place
245 // the image back in the first quadrant.
246 if (aOrientation
.flip
== Flip::Horizontal
&& !aOrientation
.flipFirst
) {
247 if (aOrientation
.SwapsWidthAndHeight()) {
248 builder
.Translate(gfxPoint(aSize
.height
, 0));
250 builder
.Translate(gfxPoint(aSize
.width
, 0));
252 builder
.Scale(-1.0, 1.0);
255 // Apply rotation, if present. Again, a translation is used to place the
256 // image back in the first quadrant.
257 switch (aOrientation
.rotation
) {
261 builder
.Translate(gfxPoint(aSize
.height
, 0));
262 builder
.Rotate(-1.5 * M_PI
);
265 builder
.Translate(gfxPoint(aSize
.width
, aSize
.height
));
266 builder
.Rotate(-1.0 * M_PI
);
269 builder
.Translate(gfxPoint(0, aSize
.width
));
270 builder
.Rotate(-0.5 * M_PI
);
273 MOZ_ASSERT(false, "Invalid rotation value");
276 // Apply a flipFirst reflection.
277 if (aOrientation
.flip
== Flip::Horizontal
&& aOrientation
.flipFirst
) {
278 builder
.Translate(gfxPoint(aSize
.width
, 0.0));
279 builder
.Scale(-1.0, 1.0);
282 return builder
.Build();
285 NS_IMETHODIMP_(ImgDrawResult
)
286 OrientedImage::Draw(gfxContext
* aContext
, const nsIntSize
& aSize
,
287 const ImageRegion
& aRegion
, uint32_t aWhichFrame
,
288 SamplingFilter aSamplingFilter
,
289 const Maybe
<SVGImageContext
>& aSVGContext
, uint32_t aFlags
,
291 if (mOrientation
.IsIdentity()) {
292 return InnerImage()->Draw(aContext
, aSize
, aRegion
, aWhichFrame
,
293 aSamplingFilter
, aSVGContext
, aFlags
, aOpacity
);
296 // Update the image size to match the image's coordinate system. (This could
297 // be done using TransformBounds but since it's only a size a swap is enough.)
298 nsIntSize
size(aSize
);
299 if (mOrientation
.SwapsWidthAndHeight()) {
300 swap(size
.width
, size
.height
);
303 // Update the matrix so that we transform the image into the orientation
304 // expected by the caller before drawing.
305 gfxMatrix
matrix(OrientationMatrix(size
));
306 gfxContextMatrixAutoSaveRestore
saveMatrix(aContext
);
307 aContext
->Multiply(matrix
);
309 // The region is already in the orientation expected by the caller, but we
310 // need it to be in the image's coordinate system, so we transform it using
311 // the inverse of the orientation matrix.
312 gfxMatrix
inverseMatrix(OrientationMatrix(size
, /* aInvert = */ true));
313 ImageRegion
region(aRegion
);
314 region
.TransformBoundsBy(inverseMatrix
);
316 auto orientViewport
= [&](const SVGImageContext
& aOldContext
) {
317 SVGImageContext
context(aOldContext
);
318 auto oldViewport
= aOldContext
.GetViewportSize();
319 if (oldViewport
&& mOrientation
.SwapsWidthAndHeight()) {
320 // Swap width and height:
321 CSSIntSize
newViewport(oldViewport
->height
, oldViewport
->width
);
322 context
.SetViewportSize(Some(newViewport
));
327 return InnerImage()->Draw(aContext
, size
, region
, aWhichFrame
,
328 aSamplingFilter
, aSVGContext
.map(orientViewport
),
332 nsIntSize
OrientedImage::OptimalImageSizeForDest(const gfxSize
& aDest
,
333 uint32_t aWhichFrame
,
334 SamplingFilter aSamplingFilter
,
336 if (!mOrientation
.SwapsWidthAndHeight()) {
337 return InnerImage()->OptimalImageSizeForDest(aDest
, aWhichFrame
,
338 aSamplingFilter
, aFlags
);
341 // Swap the size for the calculation, then swap it back for the caller.
342 gfxSize
destSize(aDest
.height
, aDest
.width
);
343 nsIntSize
innerImageSize(InnerImage()->OptimalImageSizeForDest(
344 destSize
, aWhichFrame
, aSamplingFilter
, aFlags
));
345 return nsIntSize(innerImageSize
.height
, innerImageSize
.width
);
348 NS_IMETHODIMP_(nsIntRect
)
349 OrientedImage::GetImageSpaceInvalidationRect(const nsIntRect
& aRect
) {
350 nsIntRect
rect(InnerImage()->GetImageSpaceInvalidationRect(aRect
));
352 if (mOrientation
.IsIdentity()) {
357 nsresult rv
= InnerImage()->GetWidth(&innerSize
.width
);
358 rv
= NS_FAILED(rv
) ? rv
: InnerImage()->GetHeight(&innerSize
.height
);
360 // Fall back to identity if the width and height aren't available.
364 // Transform the invalidation rect into the correct orientation.
365 gfxMatrix
matrix(OrientationMatrix(innerSize
));
366 gfxRect
invalidRect(matrix
.TransformBounds(
367 gfxRect(rect
.X(), rect
.Y(), rect
.Width(), rect
.Height())));
369 return IntRect::RoundOut(invalidRect
.X(), invalidRect
.Y(),
370 invalidRect
.Width(), invalidRect
.Height());
374 } // namespace mozilla