2008-09-25 19:53:09 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* 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/. */
|
2008-09-25 19:53:09 +00:00
|
|
|
|
|
|
|
#include "gfxBlur.h"
|
|
|
|
|
2011-11-18 09:19:24 +00:00
|
|
|
#include "mozilla/gfx/Blur.h"
|
2008-09-25 19:53:09 +00:00
|
|
|
|
2011-11-18 09:19:24 +00:00
|
|
|
using namespace mozilla::gfx;
|
2008-09-25 19:53:09 +00:00
|
|
|
|
|
|
|
gfxAlphaBoxBlur::gfxAlphaBoxBlur()
|
2012-07-30 14:20:58 +00:00
|
|
|
: mBlur(nullptr)
|
2008-09-25 19:53:09 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxAlphaBoxBlur::~gfxAlphaBoxBlur()
|
|
|
|
{
|
2012-09-26 09:27:40 +00:00
|
|
|
mContext = nullptr;
|
|
|
|
mImageSurface = nullptr;
|
2011-11-18 09:19:24 +00:00
|
|
|
delete mBlur;
|
2008-09-25 19:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext*
|
|
|
|
gfxAlphaBoxBlur::Init(const gfxRect& aRect,
|
2010-08-23 09:30:07 +00:00
|
|
|
const gfxIntSize& aSpreadRadius,
|
2008-12-06 18:54:36 +00:00
|
|
|
const gfxIntSize& aBlurRadius,
|
2010-04-18 03:13:10 +00:00
|
|
|
const gfxRect* aDirtyRect,
|
|
|
|
const gfxRect* aSkipRect)
|
2008-09-25 19:53:09 +00:00
|
|
|
{
|
2012-09-28 17:21:40 +00:00
|
|
|
mozilla::gfx::Rect rect(Float(aRect.x), Float(aRect.y),
|
|
|
|
Float(aRect.width), Float(aRect.height));
|
2011-11-18 09:19:24 +00:00
|
|
|
IntSize spreadRadius(aSpreadRadius.width, aSpreadRadius.height);
|
|
|
|
IntSize blurRadius(aBlurRadius.width, aBlurRadius.height);
|
2012-02-01 02:18:30 +00:00
|
|
|
nsAutoPtr<mozilla::gfx::Rect> dirtyRect;
|
2008-12-06 18:54:36 +00:00
|
|
|
if (aDirtyRect) {
|
2012-09-28 17:21:40 +00:00
|
|
|
dirtyRect = new mozilla::gfx::Rect(Float(aDirtyRect->x),
|
|
|
|
Float(aDirtyRect->y),
|
|
|
|
Float(aDirtyRect->width),
|
|
|
|
Float(aDirtyRect->height));
|
2010-04-18 03:13:10 +00:00
|
|
|
}
|
2012-02-01 02:18:30 +00:00
|
|
|
nsAutoPtr<mozilla::gfx::Rect> skipRect;
|
2011-11-17 18:01:11 +00:00
|
|
|
if (aSkipRect) {
|
2012-09-28 17:21:40 +00:00
|
|
|
skipRect = new mozilla::gfx::Rect(Float(aSkipRect->x),
|
|
|
|
Float(aSkipRect->y),
|
|
|
|
Float(aSkipRect->width),
|
|
|
|
Float(aSkipRect->height));
|
2011-11-17 18:01:11 +00:00
|
|
|
}
|
2011-11-14 04:29:28 +00:00
|
|
|
|
2011-11-18 09:19:24 +00:00
|
|
|
mBlur = new AlphaBoxBlur(rect, spreadRadius, blurRadius, dirtyRect, skipRect);
|
2013-04-19 10:13:18 +00:00
|
|
|
int32_t blurDataSize = mBlur->GetSurfaceAllocationSize();
|
|
|
|
if (blurDataSize <= 0)
|
|
|
|
return nullptr;
|
2011-11-18 09:19:24 +00:00
|
|
|
|
|
|
|
IntSize size = mBlur->GetSize();
|
2013-04-19 10:13:18 +00:00
|
|
|
|
2008-09-25 19:53:09 +00:00
|
|
|
// Make an alpha-only surface to draw on. We will play with the data after
|
|
|
|
// everything is drawn to create a blur effect.
|
2013-04-19 10:13:18 +00:00
|
|
|
mImageSurface = new gfxImageSurface(gfxIntSize(size.width, size.height),
|
2013-09-24 20:45:13 +00:00
|
|
|
gfxImageFormatA8,
|
2011-11-18 09:19:24 +00:00
|
|
|
mBlur->GetStride(),
|
2013-04-19 10:13:18 +00:00
|
|
|
blurDataSize,
|
|
|
|
true);
|
2011-11-18 09:19:24 +00:00
|
|
|
if (mImageSurface->CairoStatus())
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2008-09-25 19:53:09 +00:00
|
|
|
|
2011-11-18 09:19:24 +00:00
|
|
|
IntRect irect = mBlur->GetRect();
|
|
|
|
gfxPoint topleft(irect.TopLeft().x, irect.TopLeft().y);
|
|
|
|
|
2008-09-25 19:53:09 +00:00
|
|
|
// Use a device offset so callers don't need to worry about translating
|
|
|
|
// coordinates, they can draw as if this was part of the destination context
|
|
|
|
// at the coordinates of rect.
|
2011-11-18 09:19:24 +00:00
|
|
|
mImageSurface->SetDeviceOffset(-topleft);
|
2008-09-25 19:53:09 +00:00
|
|
|
|
|
|
|
mContext = new gfxContext(mImageSurface);
|
|
|
|
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxAlphaBoxBlur::Paint(gfxContext* aDestinationCtx, const gfxPoint& offset)
|
|
|
|
{
|
|
|
|
if (!mContext)
|
|
|
|
return;
|
|
|
|
|
2013-04-19 10:13:18 +00:00
|
|
|
mBlur->Blur(mImageSurface->Data());
|
2008-09-25 19:53:09 +00:00
|
|
|
|
2012-02-01 02:18:30 +00:00
|
|
|
mozilla::gfx::Rect* dirtyrect = mBlur->GetDirtyRect();
|
2008-09-25 19:53:09 +00:00
|
|
|
|
2008-12-06 18:54:36 +00:00
|
|
|
// Avoid a semi-expensive clip operation if we can, otherwise
|
|
|
|
// clip to the dirty rect
|
2011-11-18 09:19:24 +00:00
|
|
|
if (dirtyrect) {
|
2010-08-23 09:30:07 +00:00
|
|
|
aDestinationCtx->Save();
|
|
|
|
aDestinationCtx->NewPath();
|
2011-11-18 09:19:24 +00:00
|
|
|
gfxRect dirty(dirtyrect->x, dirtyrect->y, dirtyrect->width, dirtyrect->height);
|
2012-05-12 17:01:20 +00:00
|
|
|
gfxRect imageRect(offset - mImageSurface->GetDeviceOffset(), mImageSurface->GetSize());
|
|
|
|
dirty.IntersectRect(dirty, imageRect);
|
2011-11-18 09:19:24 +00:00
|
|
|
aDestinationCtx->Rectangle(dirty);
|
2010-08-23 09:30:07 +00:00
|
|
|
aDestinationCtx->Clip();
|
|
|
|
aDestinationCtx->Mask(mImageSurface, offset);
|
|
|
|
aDestinationCtx->Restore();
|
2008-12-06 18:54:36 +00:00
|
|
|
} else {
|
2010-08-23 09:30:07 +00:00
|
|
|
aDestinationCtx->Mask(mImageSurface, offset);
|
2008-12-06 18:54:36 +00:00
|
|
|
}
|
2008-09-25 19:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxIntSize gfxAlphaBoxBlur::CalculateBlurRadius(const gfxPoint& aStd)
|
|
|
|
{
|
2012-09-28 17:21:40 +00:00
|
|
|
mozilla::gfx::Point std(Float(aStd.x), Float(aStd.y));
|
2011-11-18 09:19:24 +00:00
|
|
|
IntSize size = AlphaBoxBlur::CalculateBlurRadius(std);
|
|
|
|
return gfxIntSize(size.width, size.height);
|
2008-09-25 19:53:09 +00:00
|
|
|
}
|