2008-09-25 23:53:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 15:12:37 +04: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 23:53:09 +04:00
|
|
|
|
|
|
|
#include "gfxBlur.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxImageSurface.h"
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
#include "mozilla/gfx/Blur.h"
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
using namespace mozilla::gfx;
|
2008-09-25 23:53:09 +04:00
|
|
|
|
|
|
|
gfxAlphaBoxBlur::gfxAlphaBoxBlur()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mBlur(nullptr)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxAlphaBoxBlur::~gfxAlphaBoxBlur()
|
|
|
|
{
|
2012-09-26 13:27:40 +04:00
|
|
|
mContext = nullptr;
|
|
|
|
mImageSurface = nullptr;
|
2011-11-18 13:19:24 +04:00
|
|
|
delete mBlur;
|
2008-09-25 23:53:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxContext*
|
|
|
|
gfxAlphaBoxBlur::Init(const gfxRect& aRect,
|
2010-08-23 13:30:07 +04:00
|
|
|
const gfxIntSize& aSpreadRadius,
|
2008-12-06 21:54:36 +03:00
|
|
|
const gfxIntSize& aBlurRadius,
|
2010-04-18 07:13:10 +04:00
|
|
|
const gfxRect* aDirtyRect,
|
|
|
|
const gfxRect* aSkipRect)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
2012-09-28 21:21:40 +04:00
|
|
|
mozilla::gfx::Rect rect(Float(aRect.x), Float(aRect.y),
|
|
|
|
Float(aRect.width), Float(aRect.height));
|
2011-11-18 13:19:24 +04:00
|
|
|
IntSize spreadRadius(aSpreadRadius.width, aSpreadRadius.height);
|
|
|
|
IntSize blurRadius(aBlurRadius.width, aBlurRadius.height);
|
2012-02-01 06:18:30 +04:00
|
|
|
nsAutoPtr<mozilla::gfx::Rect> dirtyRect;
|
2008-12-06 21:54:36 +03:00
|
|
|
if (aDirtyRect) {
|
2012-09-28 21:21:40 +04:00
|
|
|
dirtyRect = new mozilla::gfx::Rect(Float(aDirtyRect->x),
|
|
|
|
Float(aDirtyRect->y),
|
|
|
|
Float(aDirtyRect->width),
|
|
|
|
Float(aDirtyRect->height));
|
2010-04-18 07:13:10 +04:00
|
|
|
}
|
2012-02-01 06:18:30 +04:00
|
|
|
nsAutoPtr<mozilla::gfx::Rect> skipRect;
|
2011-11-17 22:01:11 +04:00
|
|
|
if (aSkipRect) {
|
2012-09-28 21:21:40 +04:00
|
|
|
skipRect = new mozilla::gfx::Rect(Float(aSkipRect->x),
|
|
|
|
Float(aSkipRect->y),
|
|
|
|
Float(aSkipRect->width),
|
|
|
|
Float(aSkipRect->height));
|
2011-11-17 22:01:11 +04:00
|
|
|
}
|
2011-11-14 08:29:28 +04:00
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
mBlur = new AlphaBoxBlur(rect, spreadRadius, blurRadius, dirtyRect, skipRect);
|
2013-04-19 14:13:18 +04:00
|
|
|
int32_t blurDataSize = mBlur->GetSurfaceAllocationSize();
|
|
|
|
if (blurDataSize <= 0)
|
|
|
|
return nullptr;
|
2011-11-18 13:19:24 +04:00
|
|
|
|
|
|
|
IntSize size = mBlur->GetSize();
|
2013-04-19 14:13:18 +04:00
|
|
|
|
2008-09-25 23:53:09 +04: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 14:13:18 +04:00
|
|
|
mImageSurface = new gfxImageSurface(gfxIntSize(size.width, size.height),
|
2013-09-25 00:45:13 +04:00
|
|
|
gfxImageFormatA8,
|
2011-11-18 13:19:24 +04:00
|
|
|
mBlur->GetStride(),
|
2013-04-19 14:13:18 +04:00
|
|
|
blurDataSize,
|
|
|
|
true);
|
2011-11-18 13:19:24 +04:00
|
|
|
if (mImageSurface->CairoStatus())
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
IntRect irect = mBlur->GetRect();
|
|
|
|
gfxPoint topleft(irect.TopLeft().x, irect.TopLeft().y);
|
|
|
|
|
2008-09-25 23:53:09 +04: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 13:19:24 +04:00
|
|
|
mImageSurface->SetDeviceOffset(-topleft);
|
2008-09-25 23:53:09 +04:00
|
|
|
|
|
|
|
mContext = new gfxContext(mImageSurface);
|
|
|
|
|
|
|
|
return mContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-26 03:05:36 +04:00
|
|
|
gfxAlphaBoxBlur::Paint(gfxContext* aDestinationCtx)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
|
|
|
if (!mContext)
|
|
|
|
return;
|
|
|
|
|
2013-04-19 14:13:18 +04:00
|
|
|
mBlur->Blur(mImageSurface->Data());
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2012-02-01 06:18:30 +04:00
|
|
|
mozilla::gfx::Rect* dirtyrect = mBlur->GetDirtyRect();
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2008-12-06 21:54:36 +03:00
|
|
|
// Avoid a semi-expensive clip operation if we can, otherwise
|
|
|
|
// clip to the dirty rect
|
2011-11-18 13:19:24 +04:00
|
|
|
if (dirtyrect) {
|
2010-08-23 13:30:07 +04:00
|
|
|
aDestinationCtx->Save();
|
|
|
|
aDestinationCtx->NewPath();
|
2011-11-18 13:19:24 +04:00
|
|
|
gfxRect dirty(dirtyrect->x, dirtyrect->y, dirtyrect->width, dirtyrect->height);
|
2013-11-26 03:05:36 +04:00
|
|
|
gfxRect imageRect(-mImageSurface->GetDeviceOffset(), mImageSurface->GetSize());
|
2012-05-12 21:01:20 +04:00
|
|
|
dirty.IntersectRect(dirty, imageRect);
|
2011-11-18 13:19:24 +04:00
|
|
|
aDestinationCtx->Rectangle(dirty);
|
2010-08-23 13:30:07 +04:00
|
|
|
aDestinationCtx->Clip();
|
2013-11-26 03:05:36 +04:00
|
|
|
aDestinationCtx->Mask(mImageSurface, gfxPoint());
|
2010-08-23 13:30:07 +04:00
|
|
|
aDestinationCtx->Restore();
|
2008-12-06 21:54:36 +03:00
|
|
|
} else {
|
2013-11-26 03:05:36 +04:00
|
|
|
aDestinationCtx->Mask(mImageSurface, gfxPoint());
|
2008-12-06 21:54:36 +03:00
|
|
|
}
|
2008-09-25 23:53:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxIntSize gfxAlphaBoxBlur::CalculateBlurRadius(const gfxPoint& aStd)
|
|
|
|
{
|
2012-09-28 21:21:40 +04:00
|
|
|
mozilla::gfx::Point std(Float(aStd.x), Float(aStd.y));
|
2011-11-18 13:19:24 +04:00
|
|
|
IntSize size = AlphaBoxBlur::CalculateBlurRadius(std);
|
|
|
|
return gfxIntSize(size.width, size.height);
|
2008-09-25 23:53:09 +04:00
|
|
|
}
|