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"
|
2013-10-02 08:20:47 +04:00
|
|
|
#include "gfxPlatform.h"
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2011-11-18 13:19:24 +04:00
|
|
|
#include "mozilla/gfx/Blur.h"
|
2013-10-02 08:20:47 +04:00
|
|
|
#include "mozilla/gfx/2D.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;
|
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-10-02 08:20:47 +04:00
|
|
|
mData = new unsigned char[blurDataSize];
|
|
|
|
memset(mData, 0, blurDataSize);
|
|
|
|
|
2013-12-01 06:21:03 +04:00
|
|
|
mozilla::RefPtr<DrawTarget> dt =
|
2013-10-02 08:20:47 +04:00
|
|
|
gfxPlatform::GetPlatform()->CreateDrawTargetForData(mData, size,
|
|
|
|
mBlur->GetStride(),
|
|
|
|
FORMAT_A8);
|
|
|
|
if (!dt) {
|
|
|
|
nsRefPtr<gfxImageSurface> image =
|
|
|
|
new gfxImageSurface(mData,
|
|
|
|
gfxIntSize(size.width, size.height),
|
|
|
|
mBlur->GetStride(),
|
|
|
|
gfxImageFormatA8);
|
|
|
|
dt = Factory::CreateDrawTargetForCairoSurface(image->CairoSurface(), size);
|
|
|
|
if (!dt) {
|
|
|
|
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);
|
|
|
|
|
2013-10-02 08:20:47 +04:00
|
|
|
mContext = new gfxContext(dt);
|
|
|
|
mContext->Translate(-topleft);
|
2008-09-25 23:53:09 +04:00
|
|
|
|
|
|
|
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-10-02 08:20:47 +04:00
|
|
|
mBlur->Blur(mData);
|
|
|
|
|
|
|
|
mozilla::gfx::Rect* dirtyRect = mBlur->GetDirtyRect();
|
|
|
|
|
|
|
|
DrawTarget *dest = aDestinationCtx->GetDrawTarget();
|
|
|
|
if (!dest) {
|
|
|
|
NS_ERROR("Blurring not supported for Thebes contexts!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-01 06:21:03 +04:00
|
|
|
mozilla::RefPtr<SourceSurface> mask
|
|
|
|
= dest->CreateSourceSurfaceFromData(mData,
|
|
|
|
mBlur->GetSize(),
|
|
|
|
mBlur->GetStride(),
|
|
|
|
FORMAT_A8);
|
2013-10-02 08:20:47 +04:00
|
|
|
if (!mask) {
|
|
|
|
NS_ERROR("Failed to create mask!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<gfxPattern> thebesPat = aDestinationCtx->GetPattern();
|
|
|
|
Pattern* pat = thebesPat->GetPattern(dest, nullptr);
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2013-10-02 08:20:47 +04:00
|
|
|
Matrix oldTransform = dest->GetTransform();
|
|
|
|
Matrix newTransform = oldTransform;
|
|
|
|
newTransform.Translate(mBlur->GetRect().x, mBlur->GetRect().y);
|
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
|
2013-10-02 08:20:47 +04:00
|
|
|
if (dirtyRect) {
|
|
|
|
dest->PushClipRect(*dirtyRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
dest->SetTransform(newTransform);
|
|
|
|
dest->MaskSurface(*pat, mask, Point(0, 0));
|
|
|
|
dest->SetTransform(oldTransform);
|
|
|
|
|
|
|
|
if (dirtyRect) {
|
|
|
|
dest->PopClip();
|
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
|
|
|
}
|
2013-11-26 03:08:29 +04:00
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
gfxAlphaBoxBlur::BlurRectangle(gfxContext *aDestinationCtx,
|
|
|
|
const gfxRect& aRect,
|
|
|
|
gfxCornerSizes* aCornerRadii,
|
2013-11-26 03:09:04 +04:00
|
|
|
const gfxPoint& aBlurStdDev,
|
2013-11-26 03:08:29 +04:00
|
|
|
const gfxRGBA& aShadowColor,
|
|
|
|
const gfxRect& aDirtyRect,
|
|
|
|
const gfxRect& aSkipRect)
|
|
|
|
{
|
2013-11-26 03:09:04 +04:00
|
|
|
gfxIntSize blurRadius = CalculateBlurRadius(aBlurStdDev);
|
|
|
|
|
2013-11-26 03:08:29 +04:00
|
|
|
// Create the temporary surface for blurring
|
|
|
|
gfxAlphaBoxBlur blur;
|
2013-11-26 03:09:04 +04:00
|
|
|
gfxContext *dest = blur.Init(aRect, gfxIntSize(), blurRadius, &aDirtyRect, &aSkipRect);
|
2013-11-26 03:08:29 +04:00
|
|
|
|
|
|
|
if (!dest) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect shadowGfxRect = aRect;
|
|
|
|
shadowGfxRect.Round();
|
|
|
|
|
|
|
|
dest->NewPath();
|
|
|
|
if (aCornerRadii) {
|
|
|
|
dest->RoundedRectangle(shadowGfxRect, *aCornerRadii);
|
|
|
|
} else {
|
|
|
|
dest->Rectangle(shadowGfxRect);
|
|
|
|
}
|
|
|
|
dest->Fill();
|
|
|
|
|
|
|
|
aDestinationCtx->SetColor(aShadowColor);
|
|
|
|
blur.Paint(aDestinationCtx);
|
|
|
|
}
|