2013-08-25 07:19:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
#include "gfx2DGlue.h"
|
2013-08-25 07:19:43 +00:00
|
|
|
#include "gfxDrawable.h"
|
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "gfxUtils.h"
|
2014-08-22 20:12:38 +00:00
|
|
|
#include "ImageRegion.h"
|
|
|
|
#include "SVGImageContext.h"
|
2013-08-25 07:19:43 +00:00
|
|
|
|
|
|
|
#include "OrientedImage.h"
|
|
|
|
|
|
|
|
using std::swap;
|
|
|
|
|
|
|
|
namespace mozilla {
|
2014-07-10 15:00:31 +00:00
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
using layers::LayerManager;
|
|
|
|
using layers::ImageContainer;
|
|
|
|
|
2013-08-25 07:19:43 +00:00
|
|
|
namespace image {
|
|
|
|
|
2014-07-14 19:21:34 +00:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED0(OrientedImage, ImageWrapper)
|
2013-08-25 07:19:43 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OrientedImage::GetWidth(int32_t* aWidth)
|
|
|
|
{
|
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
|
|
|
return InnerImage()->GetHeight(aWidth);
|
|
|
|
} else {
|
|
|
|
return InnerImage()->GetWidth(aWidth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OrientedImage::GetHeight(int32_t* aHeight)
|
|
|
|
{
|
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
|
|
|
return InnerImage()->GetWidth(aHeight);
|
|
|
|
} else {
|
|
|
|
return InnerImage()->GetHeight(aHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OrientedImage::GetIntrinsicSize(nsSize* aSize)
|
|
|
|
{
|
|
|
|
nsresult rv = InnerImage()->GetIntrinsicSize(aSize);
|
|
|
|
|
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
|
|
|
swap(aSize->width, aSize->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OrientedImage::GetIntrinsicRatio(nsSize* aRatio)
|
|
|
|
{
|
|
|
|
nsresult rv = InnerImage()->GetIntrinsicRatio(aRatio);
|
|
|
|
|
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
|
|
|
swap(aRatio->width, aRatio->height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-06-17 14:00:52 +00:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
2013-08-25 07:19:43 +00:00
|
|
|
OrientedImage::GetFrame(uint32_t aWhichFrame,
|
2013-12-13 08:34:24 +00:00
|
|
|
uint32_t aFlags)
|
2013-08-25 07:19:43 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mOrientation.IsIdentity()) {
|
2013-12-13 08:34:24 +00:00
|
|
|
return InnerImage()->GetFrame(aWhichFrame, aFlags);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the underlying dimensions.
|
2015-09-23 18:49:05 +00:00
|
|
|
IntSize size;
|
2014-08-22 20:12:38 +00:00
|
|
|
rv = InnerImage()->GetWidth(&size.width);
|
2014-04-15 18:02:23 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2014-08-22 20:12:38 +00:00
|
|
|
rv = InnerImage()->GetHeight(&size.height);
|
2013-12-13 08:34:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2013-08-25 07:19:43 +00:00
|
|
|
|
|
|
|
// Determine an appropriate format for the surface.
|
|
|
|
gfx::SurfaceFormat surfaceFormat;
|
2014-11-25 07:42:43 +00:00
|
|
|
if (InnerImage()->IsOpaque()) {
|
2014-01-10 19:06:16 +00:00
|
|
|
surfaceFormat = gfx::SurfaceFormat::B8G8R8X8;
|
2013-08-25 07:19:43 +00:00
|
|
|
} else {
|
2014-01-10 19:06:16 +00:00
|
|
|
surfaceFormat = gfx::SurfaceFormat::B8G8R8A8;
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a surface to draw into.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<DrawTarget> target =
|
2014-04-15 18:02:23 +00:00
|
|
|
gfxPlatform::GetPlatform()->
|
2015-04-07 14:08:57 +00:00
|
|
|
CreateOffscreenContentDrawTarget(size, surfaceFormat);
|
2014-05-31 10:26:04 +00:00
|
|
|
if (!target) {
|
|
|
|
NS_ERROR("Could not create a DrawTarget");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-25 07:19:43 +00:00
|
|
|
|
|
|
|
// Create our drawable.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<SourceSurface> innerSurface =
|
2013-12-13 08:34:24 +00:00
|
|
|
InnerImage()->GetFrame(aWhichFrame, aFlags);
|
|
|
|
NS_ENSURE_TRUE(innerSurface, nullptr);
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<gfxDrawable> drawable =
|
2014-08-22 20:12:38 +00:00
|
|
|
new gfxSurfaceDrawable(innerSurface, size);
|
2013-08-25 07:19:43 +00:00
|
|
|
|
|
|
|
// Draw.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<gfxContext> ctx = new gfxContext(target);
|
2014-08-22 20:12:38 +00:00
|
|
|
ctx->Multiply(OrientationMatrix(size));
|
2015-10-06 00:12:46 +00:00
|
|
|
gfxUtils::DrawPixelSnapped(ctx, drawable, size, ImageRegion::Create(size),
|
|
|
|
surfaceFormat, Filter::LINEAR);
|
2014-12-05 23:58:00 +00:00
|
|
|
|
2014-04-15 18:02:23 +00:00
|
|
|
return target->Snapshot();
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2015-09-19 20:34:09 +00:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<SourceSurface>)
|
|
|
|
OrientedImage::GetFrameAtSize(const IntSize& aSize,
|
|
|
|
uint32_t aWhichFrame,
|
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
// XXX(seth): It'd be nice to support downscale-during-decode for this case,
|
|
|
|
// but right now we just fall back to the intrinsic size.
|
|
|
|
return GetFrame(aWhichFrame, aFlags);
|
|
|
|
}
|
|
|
|
|
2015-05-13 07:23:44 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
|
|
|
OrientedImage::IsImageContainerAvailable(LayerManager* aManager, uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (mOrientation.IsIdentity()) {
|
|
|
|
return InnerImage()->IsImageContainerAvailable(aManager, aFlags);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-18 02:40:16 +00:00
|
|
|
NS_IMETHODIMP_(already_AddRefed<ImageContainer>)
|
|
|
|
OrientedImage::GetImageContainer(LayerManager* aManager, uint32_t aFlags)
|
2013-08-25 07:19:43 +00:00
|
|
|
{
|
|
|
|
// XXX(seth): We currently don't have a way of orienting the result of
|
|
|
|
// GetImageContainer. We work around this by always returning null, but if it
|
|
|
|
// ever turns out that OrientedImage is widely used on codepaths that can
|
|
|
|
// actually benefit from GetImageContainer, it would be a good idea to fix
|
|
|
|
// that method for performance reasons.
|
|
|
|
|
|
|
|
if (mOrientation.IsIdentity()) {
|
2015-03-18 02:40:16 +00:00
|
|
|
return InnerImage()->GetImageContainer(aManager, aFlags);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-18 02:40:16 +00:00
|
|
|
return nullptr;
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
struct MatrixBuilder
|
2013-08-25 07:19:43 +00:00
|
|
|
{
|
2014-09-02 16:20:24 +00:00
|
|
|
explicit MatrixBuilder(bool aInvert) : mInvert(aInvert) { }
|
2014-08-22 20:12:38 +00:00
|
|
|
|
|
|
|
gfxMatrix Build() { return mMatrix; }
|
2013-08-25 07:19:43 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
void Scale(gfxFloat aX, gfxFloat aY)
|
|
|
|
{
|
2014-08-22 20:12:38 +00:00
|
|
|
if (mInvert) {
|
|
|
|
mMatrix *= gfxMatrix::Scaling(1.0 / aX, 1.0 / aY);
|
2013-08-25 07:19:43 +00:00
|
|
|
} else {
|
2014-08-22 20:12:38 +00:00
|
|
|
mMatrix.Scale(aX, aY);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
2014-08-22 20:12:38 +00:00
|
|
|
}
|
2013-08-25 07:19:43 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
void Rotate(gfxFloat aPhi)
|
|
|
|
{
|
2014-08-22 20:12:38 +00:00
|
|
|
if (mInvert) {
|
|
|
|
mMatrix *= gfxMatrix::Rotation(-aPhi);
|
|
|
|
} else {
|
|
|
|
mMatrix.Rotate(aPhi);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
2014-08-22 20:12:38 +00:00
|
|
|
}
|
2013-08-25 07:19:43 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
void Translate(gfxPoint aDelta)
|
|
|
|
{
|
2014-08-22 20:12:38 +00:00
|
|
|
if (mInvert) {
|
|
|
|
mMatrix *= gfxMatrix::Translation(-aDelta);
|
|
|
|
} else {
|
|
|
|
mMatrix.Translate(aDelta);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
2014-08-22 20:12:38 +00:00
|
|
|
}
|
2013-08-25 07:19:43 +00:00
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
private:
|
|
|
|
gfxMatrix mMatrix;
|
|
|
|
bool mInvert;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* OrientationMatrix() computes a matrix that applies the rotation and
|
|
|
|
* reflection specified by mOrientation, or that matrix's inverse if aInvert is
|
|
|
|
* true.
|
|
|
|
*
|
|
|
|
* @param aSize The scaled size of the inner image. (When outside code specifies
|
|
|
|
* the scaled size, as with imgIContainer::Draw and its aSize
|
|
|
|
* parameter, it's necessary to swap the width and height if
|
|
|
|
* mOrientation.SwapsWidthAndHeight() is true.)
|
|
|
|
* @param aInvert If true, compute the inverse of the orientation matrix. Prefer
|
|
|
|
* this approach to OrientationMatrix(..).Invert(), because it's
|
|
|
|
* more numerically accurate.
|
|
|
|
*/
|
|
|
|
gfxMatrix
|
|
|
|
OrientedImage::OrientationMatrix(const nsIntSize& aSize,
|
|
|
|
bool aInvert /* = false */)
|
|
|
|
{
|
|
|
|
MatrixBuilder builder(aInvert);
|
|
|
|
|
|
|
|
// Apply reflection, if present. (This logically happens second, but we
|
|
|
|
// apply it first because these transformations are all premultiplied.) A
|
|
|
|
// translation is necessary to place the image back in the first quadrant.
|
|
|
|
switch (mOrientation.flip) {
|
|
|
|
case Flip::Unflipped:
|
|
|
|
break;
|
|
|
|
case Flip::Horizontal:
|
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
|
|
|
builder.Translate(gfxPoint(aSize.height, 0));
|
|
|
|
} else {
|
|
|
|
builder.Translate(gfxPoint(aSize.width, 0));
|
|
|
|
}
|
|
|
|
builder.Scale(-1.0, 1.0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid flip value");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply rotation, if present. Again, a translation is used to place the
|
|
|
|
// image back in the first quadrant.
|
|
|
|
switch (mOrientation.rotation) {
|
|
|
|
case Angle::D0:
|
|
|
|
break;
|
|
|
|
case Angle::D90:
|
|
|
|
builder.Translate(gfxPoint(aSize.height, 0));
|
|
|
|
builder.Rotate(-1.5 * M_PI);
|
|
|
|
break;
|
|
|
|
case Angle::D180:
|
|
|
|
builder.Translate(gfxPoint(aSize.width, aSize.height));
|
|
|
|
builder.Rotate(-1.0 * M_PI);
|
|
|
|
break;
|
|
|
|
case Angle::D270:
|
|
|
|
builder.Translate(gfxPoint(0, aSize.width));
|
|
|
|
builder.Rotate(-0.5 * M_PI);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
MOZ_ASSERT(false, "Invalid rotation value");
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.Build();
|
|
|
|
}
|
|
|
|
|
|
|
|
static SVGImageContext
|
|
|
|
OrientViewport(const SVGImageContext& aOldContext,
|
|
|
|
const Orientation& aOrientation)
|
|
|
|
{
|
2015-04-03 19:48:12 +00:00
|
|
|
CSSIntSize viewportSize(aOldContext.GetViewportSize());
|
2014-08-22 20:12:38 +00:00
|
|
|
if (aOrientation.SwapsWidthAndHeight()) {
|
|
|
|
swap(viewportSize.width, viewportSize.height);
|
|
|
|
}
|
|
|
|
return SVGImageContext(viewportSize,
|
|
|
|
aOldContext.GetPreserveAspectRatio());
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 21:50:56 +00:00
|
|
|
NS_IMETHODIMP_(DrawResult)
|
2013-08-25 07:19:43 +00:00
|
|
|
OrientedImage::Draw(gfxContext* aContext,
|
2014-08-22 20:12:38 +00:00
|
|
|
const nsIntSize& aSize,
|
|
|
|
const ImageRegion& aRegion,
|
2013-08-25 07:19:43 +00:00
|
|
|
uint32_t aWhichFrame,
|
2015-10-06 00:18:10 +00:00
|
|
|
Filter aFilter,
|
2014-08-22 20:12:38 +00:00
|
|
|
const Maybe<SVGImageContext>& aSVGContext,
|
2013-08-25 07:19:43 +00:00
|
|
|
uint32_t aFlags)
|
|
|
|
{
|
|
|
|
if (mOrientation.IsIdentity()) {
|
2014-08-22 20:12:38 +00:00
|
|
|
return InnerImage()->Draw(aContext, aSize, aRegion,
|
|
|
|
aWhichFrame, aFilter, aSVGContext, aFlags);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
// Update the image size to match the image's coordinate system. (This could
|
|
|
|
// be done using TransformBounds but since it's only a size a swap is enough.)
|
|
|
|
nsIntSize size(aSize);
|
2013-08-25 07:19:43 +00:00
|
|
|
if (mOrientation.SwapsWidthAndHeight()) {
|
2014-08-22 20:12:38 +00:00
|
|
|
swap(size.width, size.height);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
// Update the matrix so that we transform the image into the orientation
|
|
|
|
// expected by the caller before drawing.
|
|
|
|
gfxMatrix matrix(OrientationMatrix(size));
|
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aContext);
|
|
|
|
aContext->Multiply(matrix);
|
|
|
|
|
|
|
|
// The region is already in the orientation expected by the caller, but we
|
|
|
|
// need it to be in the image's coordinate system, so we transform it using
|
|
|
|
// the inverse of the orientation matrix.
|
|
|
|
gfxMatrix inverseMatrix(OrientationMatrix(size, /* aInvert = */ true));
|
|
|
|
ImageRegion region(aRegion);
|
|
|
|
region.TransformBoundsBy(inverseMatrix);
|
|
|
|
|
|
|
|
return InnerImage()->Draw(aContext, size, region, aWhichFrame, aFilter,
|
|
|
|
aSVGContext.map(OrientViewport, mOrientation),
|
|
|
|
aFlags);
|
2013-08-25 07:19:43 +00:00
|
|
|
}
|
|
|
|
|
2014-07-28 21:27:40 +00:00
|
|
|
nsIntSize
|
2014-12-05 23:58:00 +00:00
|
|
|
OrientedImage::OptimalImageSizeForDest(const gfxSize& aDest,
|
|
|
|
uint32_t aWhichFrame,
|
2015-10-06 00:18:10 +00:00
|
|
|
Filter aFilter, uint32_t aFlags)
|
2014-07-28 21:27:40 +00:00
|
|
|
{
|
|
|
|
if (!mOrientation.SwapsWidthAndHeight()) {
|
2014-12-05 23:58:00 +00:00
|
|
|
return InnerImage()->OptimalImageSizeForDest(aDest, aWhichFrame, aFilter,
|
|
|
|
aFlags);
|
2014-07-28 21:27:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the size for the calculation, then swap it back for the caller.
|
|
|
|
gfxSize destSize(aDest.height, aDest.width);
|
|
|
|
nsIntSize innerImageSize(InnerImage()->OptimalImageSizeForDest(destSize,
|
|
|
|
aWhichFrame,
|
|
|
|
aFilter,
|
|
|
|
aFlags));
|
|
|
|
return nsIntSize(innerImageSize.height, innerImageSize.width);
|
|
|
|
}
|
|
|
|
|
2014-06-26 23:02:04 +00:00
|
|
|
NS_IMETHODIMP_(nsIntRect)
|
|
|
|
OrientedImage::GetImageSpaceInvalidationRect(const nsIntRect& aRect)
|
|
|
|
{
|
|
|
|
nsIntRect rect(InnerImage()->GetImageSpaceInvalidationRect(aRect));
|
|
|
|
|
|
|
|
if (mOrientation.IsIdentity()) {
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
nsIntSize innerSize;
|
|
|
|
nsresult rv = InnerImage()->GetWidth(&innerSize.width);
|
|
|
|
rv = NS_FAILED(rv) ? rv : InnerImage()->GetHeight(&innerSize.height);
|
2014-06-26 23:02:04 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// Fall back to identity if the width and height aren't available.
|
|
|
|
return rect;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transform the invalidation rect into the correct orientation.
|
2015-09-26 06:26:18 +00:00
|
|
|
gfxMatrix matrix(OrientationMatrix(innerSize));
|
2014-06-26 23:02:04 +00:00
|
|
|
gfxRect invalidRect(matrix.TransformBounds(gfxRect(rect.x, rect.y,
|
|
|
|
rect.width, rect.height)));
|
|
|
|
invalidRect.RoundOut();
|
|
|
|
|
|
|
|
return nsIntRect(invalidRect.x, invalidRect.y,
|
|
|
|
invalidRect.width, invalidRect.height);
|
|
|
|
}
|
|
|
|
|
2013-08-25 07:19:43 +00:00
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|