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"
|
2014-11-01 13:45:10 +03:00
|
|
|
|
|
|
|
#include "gfx2DGlue.h"
|
2013-10-08 03:15:59 +04:00
|
|
|
#include "gfxContext.h"
|
2013-10-02 08:20:47 +04:00
|
|
|
#include "gfxPlatform.h"
|
|
|
|
#include "mozilla/gfx/2D.h"
|
2014-11-01 13:45:10 +03:00
|
|
|
#include "mozilla/gfx/Blur.h"
|
|
|
|
#include "mozilla/gfx/PathHelpers.h"
|
2016-11-21 21:16:59 +03:00
|
|
|
#include "mozilla/Maybe.h"
|
2017-06-15 00:24:28 +03:00
|
|
|
#include "mozilla/SystemGroup.h"
|
2014-06-11 01:51:24 +04:00
|
|
|
#include "nsExpirationTracker.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2015-05-13 22:19:27 +03:00
|
|
|
#include "gfxUtils.h"
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
using namespace mozilla;
|
2011-11-18 13:19:24 +04:00
|
|
|
using namespace mozilla::gfx;
|
2008-09-25 23:53:09 +04:00
|
|
|
|
|
|
|
gfxAlphaBoxBlur::gfxAlphaBoxBlur()
|
2016-11-21 21:17:09 +03:00
|
|
|
: mData(nullptr),
|
|
|
|
mAccelerated(false)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxAlphaBoxBlur::~gfxAlphaBoxBlur()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
already_AddRefed<gfxContext>
|
2016-11-21 21:17:09 +03:00
|
|
|
gfxAlphaBoxBlur::Init(gfxContext* aDestinationCtx,
|
|
|
|
const gfxRect& aRect,
|
2015-06-01 11:26:19 +03:00
|
|
|
const IntSize& aSpreadRadius,
|
|
|
|
const IntSize& aBlurRadius,
|
2010-04-18 07:13:10 +04:00
|
|
|
const gfxRect* aDirtyRect,
|
2017-07-24 23:48:14 +03:00
|
|
|
const gfxRect* aSkipRect,
|
|
|
|
bool aUseHardwareAccel)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
2016-11-21 21:17:09 +03:00
|
|
|
DrawTarget* refDT = aDestinationCtx->GetDrawTarget();
|
2016-11-21 21:16:59 +03:00
|
|
|
Maybe<Rect> dirtyRect = aDirtyRect ? Some(ToRect(*aDirtyRect)) : Nothing();
|
|
|
|
Maybe<Rect> skipRect = aSkipRect ? Some(ToRect(*aSkipRect)) : Nothing();
|
|
|
|
RefPtr<DrawTarget> dt =
|
2016-11-21 21:17:09 +03:00
|
|
|
InitDrawTarget(refDT, ToRect(aRect), aSpreadRadius, aBlurRadius,
|
2017-07-24 23:48:14 +03:00
|
|
|
dirtyRect.ptrOr(nullptr), skipRect.ptrOr(nullptr),
|
|
|
|
aUseHardwareAccel);
|
2016-11-21 21:16:59 +03:00
|
|
|
if (!dt) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2011-11-18 13:19:24 +04:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
|
|
|
|
MOZ_ASSERT(context); // already checked for target above
|
2017-11-11 05:14:09 +03:00
|
|
|
context->SetMatrix(Matrix::Translation(-mBlur.GetRect().TopLeft()));
|
2016-11-21 21:16:59 +03:00
|
|
|
return context.forget();
|
|
|
|
}
|
2016-11-18 00:04:05 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
already_AddRefed<DrawTarget>
|
2016-11-21 21:17:09 +03:00
|
|
|
gfxAlphaBoxBlur::InitDrawTarget(const DrawTarget* aReferenceDT,
|
|
|
|
const Rect& aRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
const IntSize& aSpreadRadius,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
const Rect* aDirtyRect,
|
2017-07-24 23:48:14 +03:00
|
|
|
const Rect* aSkipRect,
|
|
|
|
bool aUseHardwareAccel)
|
2016-11-21 21:16:59 +03:00
|
|
|
{
|
|
|
|
mBlur.Init(aRect, aSpreadRadius, aBlurRadius, aDirtyRect, aSkipRect);
|
|
|
|
size_t blurDataSize = mBlur.GetSurfaceAllocationSize();
|
|
|
|
if (blurDataSize == 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-11-18 02:30:41 +03:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
BackendType backend = aReferenceDT->GetBackendType();
|
|
|
|
|
|
|
|
// Check if the backend has an accelerated DrawSurfaceWithShadow.
|
|
|
|
// Currently, only D2D1.1 supports this.
|
|
|
|
// Otherwise, DrawSurfaceWithShadow only supports square blurs without spread.
|
2017-05-09 19:59:08 +03:00
|
|
|
// When blurring small draw targets such as short spans text, the cost of
|
|
|
|
// creating and flushing an accelerated draw target may exceed the speedup
|
2017-07-24 23:48:14 +03:00
|
|
|
// gained from the faster blur. It's up to the users of this blur
|
|
|
|
// to determine whether they want to use hardware acceleration.
|
2016-11-21 21:17:09 +03:00
|
|
|
if (aBlurRadius.IsSquare() && aSpreadRadius.IsEmpty() &&
|
2017-07-24 23:48:14 +03:00
|
|
|
aUseHardwareAccel &&
|
2016-11-21 21:17:09 +03:00
|
|
|
backend == BackendType::DIRECT2D1_1) {
|
|
|
|
mAccelerated = true;
|
2017-10-31 22:02:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aReferenceDT->IsCaptureDT()) {
|
|
|
|
if (mAccelerated) {
|
|
|
|
mDrawTarget = Factory::CreateCaptureDrawTarget(backend, mBlur.GetSize(), SurfaceFormat::A8);
|
|
|
|
} else {
|
|
|
|
mDrawTarget =
|
|
|
|
Factory::CreateCaptureDrawTargetForData(backend, mBlur.GetSize(), SurfaceFormat::A8,
|
|
|
|
mBlur.GetStride(), blurDataSize);
|
|
|
|
}
|
|
|
|
} else if (mAccelerated) {
|
|
|
|
// Note: CreateShadowDrawTarget is only implemented for Cairo, so we don't
|
|
|
|
// care about mimicking this in the DrawTargetCapture case.
|
2016-11-21 21:17:09 +03:00
|
|
|
mDrawTarget =
|
|
|
|
aReferenceDT->CreateShadowDrawTarget(mBlur.GetSize(),
|
|
|
|
SurfaceFormat::A8,
|
|
|
|
AlphaBoxBlur::CalculateBlurSigma(aBlurRadius.width));
|
|
|
|
} else {
|
|
|
|
// Make an alpha-only surface to draw on. We will play with the data after
|
|
|
|
// everything is drawn to create a blur effect.
|
2017-07-11 18:05:53 +03:00
|
|
|
// This will be freed when the DrawTarget dies
|
2016-11-21 21:17:09 +03:00
|
|
|
mData = static_cast<uint8_t*>(calloc(1, blurDataSize));
|
|
|
|
if (!mData) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
mDrawTarget =
|
|
|
|
Factory::DoesBackendSupportDataDrawtarget(backend) ?
|
|
|
|
Factory::CreateDrawTargetForData(backend,
|
|
|
|
mData,
|
2016-11-21 21:16:59 +03:00
|
|
|
mBlur.GetSize(),
|
|
|
|
mBlur.GetStride(),
|
2016-11-21 21:17:09 +03:00
|
|
|
SurfaceFormat::A8) :
|
|
|
|
gfxPlatform::CreateDrawTargetForData(mData,
|
|
|
|
mBlur.GetSize(),
|
|
|
|
mBlur.GetStride(),
|
|
|
|
SurfaceFormat::A8);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mDrawTarget || !mDrawTarget->IsValid()) {
|
2017-07-11 18:05:53 +03:00
|
|
|
if (mData) {
|
|
|
|
free(mData);
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
2017-07-11 18:05:53 +03:00
|
|
|
|
|
|
|
if (mData) {
|
|
|
|
mDrawTarget->AddUserData(reinterpret_cast<UserDataKey*>(mDrawTarget.get()),
|
|
|
|
mData,
|
|
|
|
free);
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
mDrawTarget->SetTransform(Matrix::Translation(-mBlur.GetRect().TopLeft()));
|
|
|
|
return do_AddRef(mDrawTarget);
|
2008-09-25 23:53:09 +04:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
|
|
|
gfxAlphaBoxBlur::DoBlur(const Color* aShadowColor, IntPoint* aOutTopLeft)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
2016-11-21 21:17:09 +03:00
|
|
|
if (aOutTopLeft) {
|
|
|
|
*aOutTopLeft = mBlur.GetRect().TopLeft();
|
|
|
|
}
|
2016-11-18 02:29:02 +03:00
|
|
|
|
2017-10-31 22:02:30 +03:00
|
|
|
RefPtr<SourceSurface> blurMask;
|
|
|
|
if (mData) {
|
|
|
|
mBlur.Blur(mData);
|
|
|
|
blurMask = mDrawTarget->Snapshot();
|
|
|
|
} else if (mAccelerated) {
|
|
|
|
blurMask = mDrawTarget->Snapshot();
|
2016-11-21 21:17:09 +03:00
|
|
|
RefPtr<DrawTarget> blurDT =
|
2017-10-31 22:02:30 +03:00
|
|
|
mDrawTarget->CreateSimilarDrawTarget(blurMask->GetSize(), SurfaceFormat::A8);
|
2016-11-21 21:17:09 +03:00
|
|
|
if (!blurDT) {
|
|
|
|
return nullptr;
|
2016-11-18 02:29:02 +03:00
|
|
|
}
|
2016-11-21 21:17:09 +03:00
|
|
|
blurDT->DrawSurfaceWithShadow(blurMask, Point(0, 0), Color(1, 1, 1), Point(0, 0),
|
|
|
|
AlphaBoxBlur::CalculateBlurSigma(mBlur.GetBlurRadius().width),
|
|
|
|
CompositionOp::OP_OVER);
|
|
|
|
blurMask = blurDT->Snapshot();
|
2017-10-31 22:02:30 +03:00
|
|
|
} else if (mDrawTarget->IsCaptureDT()) {
|
|
|
|
mDrawTarget->Blur(mBlur);
|
|
|
|
blurMask = mDrawTarget->Snapshot();
|
2016-11-21 21:17:09 +03:00
|
|
|
}
|
2008-09-25 23:53:09 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
if (!aShadowColor) {
|
|
|
|
return blurMask.forget();
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
RefPtr<DrawTarget> shadowDT =
|
2017-10-31 22:02:30 +03:00
|
|
|
mDrawTarget->CreateSimilarDrawTarget(blurMask->GetSize(), SurfaceFormat::B8G8R8A8);
|
2016-11-21 21:17:09 +03:00
|
|
|
if (!shadowDT) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
ColorPattern shadowColor(ToDeviceColor(*aShadowColor));
|
|
|
|
shadowDT->MaskSurface(shadowColor, blurMask, Point(0, 0));
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
return shadowDT->Snapshot();
|
2014-06-11 01:51:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxAlphaBoxBlur::Paint(gfxContext* aDestinationCtx)
|
|
|
|
{
|
2017-10-31 22:02:30 +03:00
|
|
|
if ((mDrawTarget && !mDrawTarget->IsCaptureDT()) && !mAccelerated && !mData) {
|
2016-11-21 21:17:09 +03:00
|
|
|
return;
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
DrawTarget *dest = aDestinationCtx->GetDrawTarget();
|
|
|
|
if (!dest) {
|
|
|
|
NS_WARNING("Blurring not supported for Thebes contexts!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
RefPtr<gfxPattern> thebesPat = aDestinationCtx->GetPattern();
|
|
|
|
Pattern* pat = thebesPat->GetPattern(dest, nullptr);
|
|
|
|
if (!pat) {
|
|
|
|
NS_WARNING("Failed to get pattern for blur!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
IntPoint topLeft;
|
|
|
|
RefPtr<SourceSurface> mask = DoBlur(nullptr, &topLeft);
|
|
|
|
if (!mask) {
|
|
|
|
NS_ERROR("Failed to create mask!");
|
|
|
|
return;
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
// Avoid a semi-expensive clip operation if we can, otherwise
|
|
|
|
// clip to the dirty rect
|
|
|
|
Rect* dirtyRect = mBlur.GetDirtyRect();
|
|
|
|
if (dirtyRect) {
|
|
|
|
dest->PushClipRect(*dirtyRect);
|
|
|
|
}
|
|
|
|
|
|
|
|
Matrix oldTransform = dest->GetTransform();
|
|
|
|
Matrix newTransform = oldTransform;
|
|
|
|
newTransform.PreTranslate(topLeft);
|
|
|
|
dest->SetTransform(newTransform);
|
|
|
|
|
|
|
|
dest->MaskSurface(*pat, mask, Point(0, 0));
|
|
|
|
|
|
|
|
dest->SetTransform(oldTransform);
|
|
|
|
|
|
|
|
if (dirtyRect) {
|
|
|
|
dest->PopClip();
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
}
|
|
|
|
|
2015-06-01 11:26:19 +03:00
|
|
|
IntSize gfxAlphaBoxBlur::CalculateBlurRadius(const gfxPoint& aStd)
|
2008-09-25 23:53:09 +04:00
|
|
|
{
|
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);
|
2015-06-01 11:26:19 +03:00
|
|
|
return IntSize(size.width, size.height);
|
2008-09-25 23:53:09 +04:00
|
|
|
}
|
2013-11-26 03:08:29 +04:00
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
struct BlurCacheKey : public PLDHashEntryHdr {
|
|
|
|
typedef const BlurCacheKey& KeyType;
|
|
|
|
typedef const BlurCacheKey* KeyTypePointer;
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
2015-06-25 22:04:21 +03:00
|
|
|
IntSize mMinSize;
|
2015-06-01 11:26:19 +03:00
|
|
|
IntSize mBlurRadius;
|
2015-09-24 10:43:32 +03:00
|
|
|
Color mShadowColor;
|
2014-06-11 01:51:24 +04:00
|
|
|
BackendType mBackend;
|
2015-06-25 22:04:21 +03:00
|
|
|
RectCornerRadii mCornerRadii;
|
2015-09-24 19:50:29 +03:00
|
|
|
bool mIsInset;
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
// Only used for inset blurs
|
|
|
|
IntSize mInnerMinSize;
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
BlurCacheKey(const IntSize& aMinSize, const IntSize& aBlurRadius,
|
|
|
|
const RectCornerRadii* aCornerRadii, const Color& aShadowColor,
|
2015-09-24 19:50:29 +03:00
|
|
|
BackendType aBackendType)
|
|
|
|
: BlurCacheKey(aMinSize, IntSize(0, 0),
|
2016-03-29 18:24:21 +03:00
|
|
|
aBlurRadius, aCornerRadii,
|
|
|
|
aShadowColor, false,
|
2016-11-21 21:16:59 +03:00
|
|
|
aBackendType)
|
2015-09-24 19:50:29 +03:00
|
|
|
{}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2014-08-08 05:17:30 +04:00
|
|
|
explicit BlurCacheKey(const BlurCacheKey* aOther)
|
2015-06-25 22:04:21 +03:00
|
|
|
: mMinSize(aOther->mMinSize)
|
2014-06-11 01:51:24 +04:00
|
|
|
, mBlurRadius(aOther->mBlurRadius)
|
2015-06-25 22:04:21 +03:00
|
|
|
, mShadowColor(aOther->mShadowColor)
|
2014-06-11 01:51:24 +04:00
|
|
|
, mBackend(aOther->mBackend)
|
2015-06-25 22:04:21 +03:00
|
|
|
, mCornerRadii(aOther->mCornerRadii)
|
2015-09-24 19:50:29 +03:00
|
|
|
, mIsInset(aOther->mIsInset)
|
|
|
|
, mInnerMinSize(aOther->mInnerMinSize)
|
|
|
|
{ }
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
explicit BlurCacheKey(const IntSize& aOuterMinSize, const IntSize& aInnerMinSize,
|
|
|
|
const IntSize& aBlurRadius,
|
2015-09-24 10:43:32 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
|
|
|
const Color& aShadowColor, bool aIsInset,
|
2016-11-21 21:16:59 +03:00
|
|
|
BackendType aBackendType)
|
2015-09-24 19:50:29 +03:00
|
|
|
: mMinSize(aOuterMinSize)
|
|
|
|
, mBlurRadius(aBlurRadius)
|
|
|
|
, mShadowColor(aShadowColor)
|
|
|
|
, mBackend(aBackendType)
|
|
|
|
, mCornerRadii(aCornerRadii ? *aCornerRadii : RectCornerRadii())
|
|
|
|
, mIsInset(aIsInset)
|
|
|
|
, mInnerMinSize(aInnerMinSize)
|
2014-06-11 01:51:24 +04:00
|
|
|
{ }
|
|
|
|
|
2017-11-11 00:43:54 +03:00
|
|
|
BlurCacheKey(BlurCacheKey&&) = default;
|
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
static PLDHashNumber
|
|
|
|
HashKey(const KeyTypePointer aKey)
|
|
|
|
{
|
2015-06-25 22:04:21 +03:00
|
|
|
PLDHashNumber hash = 0;
|
|
|
|
hash = AddToHash(hash, aKey->mMinSize.width, aKey->mMinSize.height);
|
2014-06-11 01:51:24 +04:00
|
|
|
hash = AddToHash(hash, aKey->mBlurRadius.width, aKey->mBlurRadius.height);
|
2015-06-25 22:04:21 +03:00
|
|
|
|
2015-09-24 10:43:32 +03:00
|
|
|
hash = AddToHash(hash, HashBytes(&aKey->mShadowColor.r,
|
|
|
|
sizeof(aKey->mShadowColor.r)));
|
|
|
|
hash = AddToHash(hash, HashBytes(&aKey->mShadowColor.g,
|
|
|
|
sizeof(aKey->mShadowColor.g)));
|
|
|
|
hash = AddToHash(hash, HashBytes(&aKey->mShadowColor.b,
|
|
|
|
sizeof(aKey->mShadowColor.b)));
|
|
|
|
hash = AddToHash(hash, HashBytes(&aKey->mShadowColor.a,
|
|
|
|
sizeof(aKey->mShadowColor.a)));
|
2015-06-25 22:04:21 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < 4; i++) {
|
2015-09-24 19:50:29 +03:00
|
|
|
hash = AddToHash(hash, aKey->mCornerRadii[i].width, aKey->mCornerRadii[i].height);
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
hash = AddToHash(hash, (uint32_t)aKey->mBackend);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
|
|
|
if (aKey->mIsInset) {
|
|
|
|
hash = AddToHash(hash, aKey->mInnerMinSize.width, aKey->mInnerMinSize.height);
|
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
bool
|
|
|
|
KeyEquals(KeyTypePointer aKey) const
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
2015-06-25 22:04:21 +03:00
|
|
|
if (aKey->mMinSize == mMinSize &&
|
2014-06-11 01:51:24 +04:00
|
|
|
aKey->mBlurRadius == mBlurRadius &&
|
2015-06-25 22:04:21 +03:00
|
|
|
aKey->mCornerRadii == mCornerRadii &&
|
|
|
|
aKey->mShadowColor == mShadowColor &&
|
2014-06-11 01:51:24 +04:00
|
|
|
aKey->mBackend == mBackend) {
|
2015-09-24 19:50:29 +03:00
|
|
|
|
|
|
|
if (mIsInset) {
|
2016-11-21 21:16:59 +03:00
|
|
|
return (mInnerMinSize == aKey->mInnerMinSize);
|
2015-09-24 19:50:29 +03:00
|
|
|
}
|
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
return true;
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-06-11 01:51:24 +04:00
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
|
|
|
|
static KeyTypePointer
|
|
|
|
KeyToPointer(KeyType aKey)
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
|
|
|
return &aKey;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is what is cached. It need to be allocated in an object separated
|
|
|
|
* to the cache entry to be able to be tracked by the nsExpirationTracker.
|
|
|
|
* */
|
|
|
|
struct BlurCacheData {
|
2017-11-11 00:43:54 +03:00
|
|
|
BlurCacheData(SourceSurface* aBlur, const IntMargin& aBlurMargin, BlurCacheKey&& aKey)
|
2014-06-11 01:51:24 +04:00
|
|
|
: mBlur(aBlur)
|
2016-11-21 21:16:59 +03:00
|
|
|
, mBlurMargin(aBlurMargin)
|
2017-11-11 00:43:54 +03:00
|
|
|
, mKey(Move(aKey))
|
2014-06-11 01:51:24 +04:00
|
|
|
{}
|
|
|
|
|
2017-11-11 00:43:54 +03:00
|
|
|
BlurCacheData(BlurCacheData&& aOther) = default;
|
2014-06-11 01:51:24 +04:00
|
|
|
|
|
|
|
nsExpirationState *GetExpirationState() {
|
|
|
|
return &mExpirationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsExpirationState mExpirationState;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SourceSurface> mBlur;
|
2016-11-21 21:16:59 +03:00
|
|
|
IntMargin mBlurMargin;
|
2014-06-11 01:51:24 +04:00
|
|
|
BlurCacheKey mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class implements a cache with no maximum size, that retains the
|
|
|
|
* SourceSurfaces used to draw the blurs.
|
|
|
|
*
|
|
|
|
* An entry stays in the cache as long as it is used often.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class BlurCache final : public nsExpirationTracker<BlurCacheData,4>
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
BlurCache()
|
2017-06-15 00:24:28 +03:00
|
|
|
: nsExpirationTracker<BlurCacheData, 4>(GENERATION_MS, "BlurCache",
|
|
|
|
SystemGroup::EventTargetFor(TaskCategory::Other))
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual void NotifyExpired(BlurCacheData* aObject) override
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
|
|
|
RemoveObject(aObject);
|
|
|
|
mHashEntries.Remove(aObject->mKey);
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
BlurCacheData* Lookup(const IntSize& aMinSize,
|
2015-09-23 21:49:05 +03:00
|
|
|
const IntSize& aBlurRadius,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2015-06-25 22:04:21 +03:00
|
|
|
BackendType aBackendType)
|
2014-06-11 01:51:24 +04:00
|
|
|
{
|
|
|
|
BlurCacheData* blur =
|
2015-06-25 22:04:21 +03:00
|
|
|
mHashEntries.Get(BlurCacheKey(aMinSize, aBlurRadius,
|
|
|
|
aCornerRadii, aShadowColor,
|
|
|
|
aBackendType));
|
2014-06-11 01:51:24 +04:00
|
|
|
if (blur) {
|
|
|
|
MarkUsed(blur);
|
|
|
|
}
|
|
|
|
|
|
|
|
return blur;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
BlurCacheData* LookupInsetBoxShadow(const IntSize& aOuterMinSize,
|
|
|
|
const IntSize& aInnerMinSize,
|
2015-09-23 21:49:05 +03:00
|
|
|
const IntSize& aBlurRadius,
|
2015-09-24 19:50:29 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2015-09-24 19:50:29 +03:00
|
|
|
BackendType aBackendType)
|
|
|
|
{
|
2016-03-29 18:24:21 +03:00
|
|
|
bool insetBoxShadow = true;
|
2015-09-24 19:50:29 +03:00
|
|
|
BlurCacheKey key(aOuterMinSize, aInnerMinSize,
|
2016-03-29 18:24:21 +03:00
|
|
|
aBlurRadius, aCornerRadii,
|
|
|
|
aShadowColor, insetBoxShadow,
|
2016-11-21 21:16:59 +03:00
|
|
|
aBackendType);
|
2015-09-24 19:50:29 +03:00
|
|
|
BlurCacheData* blur = mHashEntries.Get(key);
|
|
|
|
if (blur) {
|
|
|
|
MarkUsed(blur);
|
|
|
|
}
|
|
|
|
|
|
|
|
return blur;
|
|
|
|
}
|
|
|
|
|
2014-06-11 01:51:24 +04:00
|
|
|
// Returns true if we successfully register the blur in the cache, false
|
|
|
|
// otherwise.
|
|
|
|
bool RegisterEntry(BlurCacheData* aValue)
|
|
|
|
{
|
|
|
|
nsresult rv = AddObject(aValue);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We are OOM, and we cannot track this object. We don't want stall
|
|
|
|
// entries in the hash table (since the expiration tracker is responsible
|
|
|
|
// for removing the cache entries), so we avoid putting that entry in the
|
|
|
|
// table, which is a good things considering we are short on memory
|
|
|
|
// anyway, we probably don't want to retain things.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
mHashEntries.Put(aValue->mKey, aValue);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static const uint32_t GENERATION_MS = 1000;
|
|
|
|
/**
|
|
|
|
* FIXME use nsTHashtable to avoid duplicating the BlurCacheKey.
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=761393#c47
|
|
|
|
*/
|
|
|
|
nsClassHashtable<BlurCacheKey, BlurCacheData> mHashEntries;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BlurCache* gBlurCache = nullptr;
|
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
static IntSize
|
2016-11-21 21:16:59 +03:00
|
|
|
ComputeMinSizeForShadowShape(const RectCornerRadii* aCornerRadii,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
IntMargin& aOutSlice,
|
2015-06-25 22:04:21 +03:00
|
|
|
const IntSize& aRectSize)
|
2015-05-13 22:19:27 +03:00
|
|
|
{
|
2016-11-21 21:16:59 +03:00
|
|
|
Size cornerSize(0, 0);
|
2015-05-13 22:19:27 +03:00
|
|
|
if (aCornerRadii) {
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii& corners = *aCornerRadii;
|
2017-01-05 12:07:07 +03:00
|
|
|
NS_FOR_CSS_FULL_CORNERS(i) {
|
2016-11-21 21:16:59 +03:00
|
|
|
cornerSize.width = std::max(cornerSize.width, corners[i].width);
|
|
|
|
cornerSize.height = std::max(cornerSize.height, corners[i].height);
|
2015-05-13 22:19:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
IntSize margin = IntSize::Ceil(cornerSize) + aBlurRadius;
|
|
|
|
aOutSlice = IntMargin(margin.height, margin.width,
|
|
|
|
margin.height, margin.width);
|
2015-05-13 22:19:27 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
IntSize minSize(aOutSlice.LeftRight() + 1,
|
|
|
|
aOutSlice.TopBottom() + 1);
|
2015-06-25 22:04:21 +03:00
|
|
|
|
|
|
|
// If aRectSize is smaller than minSize, the border-image approach won't
|
|
|
|
// work; there's no way to squeeze parts of the min box-shadow source
|
|
|
|
// image such that the result looks correct. So we need to adjust minSize
|
|
|
|
// in such a way that we can later draw it without stretching in the affected
|
|
|
|
// dimension. We also need to adjust "slice" to ensure that we're not trying
|
|
|
|
// to slice away more than we have.
|
|
|
|
if (aRectSize.width < minSize.width) {
|
|
|
|
minSize.width = aRectSize.width;
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutSlice.left = 0;
|
|
|
|
aOutSlice.right = 0;
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
if (aRectSize.height < minSize.height) {
|
|
|
|
minSize.height = aRectSize.height;
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutSlice.top = 0;
|
|
|
|
aOutSlice.bottom = 0;
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
MOZ_ASSERT(aOutSlice.LeftRight() <= minSize.width);
|
|
|
|
MOZ_ASSERT(aOutSlice.TopBottom() <= minSize.height);
|
2015-06-25 22:04:21 +03:00
|
|
|
return minSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2016-11-21 21:16:59 +03:00
|
|
|
CacheBlur(DrawTarget* aDT,
|
2015-06-25 22:04:21 +03:00
|
|
|
const IntSize& aMinSize,
|
2015-09-23 21:49:05 +03:00
|
|
|
const IntSize& aBlurRadius,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2016-11-21 21:16:59 +03:00
|
|
|
const IntMargin& aBlurMargin,
|
2015-06-25 22:04:21 +03:00
|
|
|
SourceSurface* aBoxShadow)
|
|
|
|
{
|
2016-11-21 21:16:59 +03:00
|
|
|
BlurCacheKey key(aMinSize, aBlurRadius, aCornerRadii, aShadowColor, aDT->GetBackendType());
|
2017-11-11 00:43:54 +03:00
|
|
|
BlurCacheData* data = new BlurCacheData(aBoxShadow, aBlurMargin, Move(key));
|
2015-06-25 22:04:21 +03:00
|
|
|
if (!gBlurCache->RegisterEntry(data)) {
|
|
|
|
delete data;
|
|
|
|
}
|
2015-05-13 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
// Blurs a small surface and creates the colored box shadow.
|
2015-06-17 17:00:52 +03:00
|
|
|
static already_AddRefed<SourceSurface>
|
2016-11-21 21:17:09 +03:00
|
|
|
CreateBoxShadow(DrawTarget* aDestDrawTarget,
|
|
|
|
const IntSize& aMinSize,
|
|
|
|
const RectCornerRadii* aCornerRadii,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
const Color& aShadowColor,
|
|
|
|
bool aMirrorCorners,
|
|
|
|
IntMargin& aOutBlurMargin)
|
2015-05-13 22:19:27 +03:00
|
|
|
{
|
|
|
|
gfxAlphaBoxBlur blur;
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect minRect(Point(0, 0), Size(aMinSize));
|
|
|
|
Rect blurRect(minRect);
|
|
|
|
// If mirroring corners, we only need to draw the top-left quadrant.
|
|
|
|
// Use ceil to preserve the remaining 1x1 middle area for minimized box
|
|
|
|
// shadows.
|
|
|
|
if (aMirrorCorners) {
|
2017-12-19 23:48:39 +03:00
|
|
|
blurRect.SizeTo(ceil(blurRect.Width() * 0.5f), ceil(blurRect.Height() * 0.5f));
|
2016-11-21 21:16:59 +03:00
|
|
|
}
|
|
|
|
IntSize zeroSpread(0, 0);
|
|
|
|
RefPtr<DrawTarget> blurDT =
|
2016-11-21 21:17:09 +03:00
|
|
|
blur.InitDrawTarget(aDestDrawTarget, blurRect, zeroSpread, aBlurRadius);
|
2016-11-21 21:16:59 +03:00
|
|
|
if (!blurDT) {
|
2015-05-13 22:19:27 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ColorPattern black(Color(0.f, 0.f, 0.f, 1.f));
|
|
|
|
|
|
|
|
if (aCornerRadii) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Path> roundedRect =
|
2016-11-21 21:16:59 +03:00
|
|
|
MakePathForRoundedRect(*blurDT, minRect, *aCornerRadii);
|
2015-05-13 22:19:27 +03:00
|
|
|
blurDT->Fill(roundedRect, black);
|
|
|
|
} else {
|
2016-11-21 21:16:59 +03:00
|
|
|
blurDT->FillRect(minRect, black);
|
2015-05-13 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IntPoint topLeft;
|
2016-11-21 21:17:09 +03:00
|
|
|
RefPtr<SourceSurface> result = blur.DoBlur(&aShadowColor, &topLeft);
|
2015-08-19 06:43:24 +03:00
|
|
|
if (!result) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-05-13 22:19:27 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
// Since blurRect is at (0, 0), we can find the inflated margin by
|
|
|
|
// negating the new rect origin, which would have been negative if
|
|
|
|
// the rect was inflated.
|
|
|
|
aOutBlurMargin = IntMargin(-topLeft.y, -topLeft.x, -topLeft.y, -topLeft.x);
|
2015-05-13 22:19:27 +03:00
|
|
|
|
|
|
|
return result.forget();
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:29:52 +03:00
|
|
|
static already_AddRefed<SourceSurface>
|
2016-01-11 01:05:26 +03:00
|
|
|
GetBlur(gfxContext* aDestinationCtx,
|
2015-06-25 22:04:21 +03:00
|
|
|
const IntSize& aRectSize,
|
2015-09-23 21:49:05 +03:00
|
|
|
const IntSize& aBlurRadius,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2016-11-21 21:16:59 +03:00
|
|
|
bool aMirrorCorners,
|
|
|
|
IntMargin& aOutBlurMargin,
|
|
|
|
IntMargin& aOutSlice,
|
|
|
|
IntSize& aOutMinSize)
|
2015-06-25 22:04:21 +03:00
|
|
|
{
|
|
|
|
if (!gBlurCache) {
|
|
|
|
gBlurCache = new BlurCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
IntSize minSize =
|
2016-11-21 21:16:59 +03:00
|
|
|
ComputeMinSizeForShadowShape(aCornerRadii, aBlurRadius, aOutSlice, aRectSize);
|
2015-06-25 22:04:21 +03:00
|
|
|
|
2015-11-05 18:29:52 +03:00
|
|
|
// We can get seams using the min size rect when drawing to the destination rect
|
|
|
|
// if we have a non-pixel aligned destination transformation. In those cases,
|
|
|
|
// fallback to just rendering the destination rect.
|
2017-05-11 19:56:15 +03:00
|
|
|
// During printing, we record all the Moz 2d commands and replay them on the parent side
|
|
|
|
// with Cairo. Cairo printing uses StretchDIBits to stretch the surface. However,
|
|
|
|
// since our source image is only 1px for some parts, we make thousands of calls.
|
|
|
|
// Instead just render the blur ourself here as one image and send it over for printing.
|
|
|
|
// TODO: May need to change this with the blob renderer in WR since it also records.
|
2017-11-11 05:14:09 +03:00
|
|
|
Matrix destMatrix = aDestinationCtx->CurrentMatrix();
|
2017-05-11 19:56:15 +03:00
|
|
|
bool useDestRect = !destMatrix.IsRectilinear() || destMatrix.HasNonIntegerTranslation() ||
|
|
|
|
aDestinationCtx->GetDrawTarget()->IsRecording();
|
2015-11-05 18:29:52 +03:00
|
|
|
if (useDestRect) {
|
|
|
|
minSize = aRectSize;
|
|
|
|
}
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutMinSize = minSize;
|
|
|
|
|
|
|
|
DrawTarget* destDT = aDestinationCtx->GetDrawTarget();
|
|
|
|
|
|
|
|
if (!useDestRect) {
|
|
|
|
BlurCacheData* cached = gBlurCache->Lookup(minSize, aBlurRadius,
|
|
|
|
aCornerRadii, aShadowColor,
|
|
|
|
destDT->GetBackendType());
|
|
|
|
if (cached) {
|
2016-11-21 21:17:09 +03:00
|
|
|
// See CreateBoxShadow() for these values
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutBlurMargin = cached->mBlurMargin;
|
|
|
|
RefPtr<SourceSurface> blur = cached->mBlur;
|
|
|
|
return blur.forget();
|
|
|
|
}
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
RefPtr<SourceSurface> boxShadow =
|
|
|
|
CreateBoxShadow(destDT, minSize, aCornerRadii, aBlurRadius,
|
|
|
|
aShadowColor, aMirrorCorners, aOutBlurMargin);
|
2015-07-06 18:58:22 +03:00
|
|
|
if (!boxShadow) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-16 23:50:39 +03:00
|
|
|
if (RefPtr<SourceSurface> opt = destDT->OptimizeSourceSurface(boxShadow)) {
|
|
|
|
boxShadow = opt;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
if (!useDestRect) {
|
2016-01-11 01:05:26 +03:00
|
|
|
CacheBlur(destDT, minSize, aBlurRadius, aCornerRadii, aShadowColor,
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutBlurMargin, boxShadow);
|
2015-11-05 18:29:52 +03:00
|
|
|
}
|
|
|
|
return boxShadow.forget();
|
2015-06-25 22:04:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gfxAlphaBoxBlur::ShutdownBlurCache()
|
|
|
|
{
|
|
|
|
delete gBlurCache;
|
|
|
|
gBlurCache = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
static Rect
|
|
|
|
RectWithEdgesTRBL(Float aTop, Float aRight, Float aBottom, Float aLeft)
|
|
|
|
{
|
|
|
|
return Rect(aLeft, aTop, aRight - aLeft, aBottom - aTop);
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
static bool
|
|
|
|
ShouldStretchSurface(DrawTarget* aDT, SourceSurface* aSurface)
|
|
|
|
{
|
|
|
|
// Use stretching if possible, since it leads to less seams when the
|
|
|
|
// destination is transformed. However, don't do this if we're using cairo,
|
|
|
|
// because if cairo is using pixman it won't render anything for large
|
|
|
|
// stretch factors because pixman's internal fixed point precision is not
|
|
|
|
// high enough to handle those scale factors.
|
|
|
|
// Calling FillRect on a D2D backend with a repeating pattern is much slower
|
|
|
|
// than DrawSurface, so special case the D2D backend here.
|
|
|
|
return (!aDT->GetTransform().IsRectilinear() &&
|
|
|
|
aDT->GetBackendType() != BackendType::CAIRO) ||
|
|
|
|
(aDT->GetBackendType() == BackendType::DIRECT2D1_1);
|
|
|
|
}
|
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
static void
|
2016-11-21 21:16:59 +03:00
|
|
|
RepeatOrStretchSurface(DrawTarget* aDT, SourceSurface* aSurface,
|
|
|
|
const Rect& aDest, const Rect& aSrc, const Rect& aSkipRect)
|
2015-05-13 22:19:27 +03:00
|
|
|
{
|
|
|
|
if (aSkipRect.Contains(aDest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
if (ShouldStretchSurface(aDT, aSurface)) {
|
|
|
|
aDT->DrawSurface(aSurface, aDest, aSrc);
|
2015-05-13 22:19:27 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfacePattern pattern(aSurface, ExtendMode::REPEAT,
|
|
|
|
Matrix::Translation(aDest.TopLeft() - aSrc.TopLeft()),
|
2016-05-25 19:01:18 +03:00
|
|
|
SamplingFilter::GOOD, RoundedToInt(aSrc));
|
2016-11-21 21:16:59 +03:00
|
|
|
aDT->FillRect(aDest, pattern);
|
2015-05-13 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2016-11-21 21:16:59 +03:00
|
|
|
DrawCorner(DrawTarget* aDT, SourceSurface* aSurface,
|
|
|
|
const Rect& aDest, const Rect& aSrc, const Rect& aSkipRect)
|
2015-05-13 22:19:27 +03:00
|
|
|
{
|
|
|
|
if (aSkipRect.Contains(aDest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
aDT->DrawSurface(aSurface, aDest, aSrc);
|
2015-05-13 22:19:27 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
static void
|
2016-11-21 21:16:59 +03:00
|
|
|
DrawMinBoxShadow(DrawTarget* aDestDrawTarget, SourceSurface* aSourceBlur,
|
|
|
|
const Rect& aDstOuter, const Rect& aDstInner,
|
|
|
|
const Rect& aSrcOuter, const Rect& aSrcInner,
|
|
|
|
const Rect& aSkipRect, bool aMiddle = false)
|
2015-09-24 19:50:29 +03:00
|
|
|
{
|
|
|
|
// Corners: top left, top right, bottom left, bottom right
|
|
|
|
DrawCorner(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstOuter.Y(), aDstInner.X(),
|
|
|
|
aDstInner.Y(), aDstOuter.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcOuter.Y(), aSrcInner.X(),
|
|
|
|
aSrcInner.Y(), aSrcOuter.X()),
|
2016-03-29 18:24:21 +03:00
|
|
|
aSkipRect);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
|
|
|
DrawCorner(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstOuter.Y(), aDstOuter.XMost(),
|
|
|
|
aDstInner.Y(), aDstInner.XMost()),
|
|
|
|
RectWithEdgesTRBL(aSrcOuter.Y(), aSrcOuter.XMost(),
|
|
|
|
aSrcInner.Y(), aSrcInner.XMost()),
|
|
|
|
aSkipRect);
|
|
|
|
|
|
|
|
DrawCorner(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.YMost(), aDstInner.X(),
|
|
|
|
aDstOuter.YMost(), aDstOuter.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.YMost(), aSrcInner.X(),
|
|
|
|
aSrcOuter.YMost(), aSrcOuter.X()),
|
|
|
|
aSkipRect);
|
|
|
|
|
|
|
|
DrawCorner(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.YMost(), aDstOuter.XMost(),
|
|
|
|
aDstOuter.YMost(), aDstInner.XMost()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.YMost(), aSrcOuter.XMost(),
|
|
|
|
aSrcOuter.YMost(), aSrcInner.XMost()),
|
|
|
|
aSkipRect);
|
|
|
|
|
|
|
|
// Edges: top, left, right, bottom
|
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstOuter.Y(), aDstInner.XMost(),
|
|
|
|
aDstInner.Y(), aDstInner.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcOuter.Y(), aSrcInner.XMost(),
|
|
|
|
aSrcInner.Y(), aSrcInner.X()),
|
|
|
|
aSkipRect);
|
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.Y(), aDstInner.X(),
|
|
|
|
aDstInner.YMost(), aDstOuter.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.Y(), aSrcInner.X(),
|
|
|
|
aSrcInner.YMost(), aSrcOuter.X()),
|
|
|
|
aSkipRect);
|
2016-03-29 18:24:21 +03:00
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.Y(), aDstOuter.XMost(),
|
|
|
|
aDstInner.YMost(), aDstInner.XMost()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.Y(), aSrcOuter.XMost(),
|
|
|
|
aSrcInner.YMost(), aSrcInner.XMost()),
|
|
|
|
aSkipRect);
|
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.YMost(), aDstInner.XMost(),
|
|
|
|
aDstOuter.YMost(), aDstInner.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.YMost(), aSrcInner.XMost(),
|
|
|
|
aSrcOuter.YMost(), aSrcInner.X()),
|
|
|
|
aSkipRect);
|
2016-11-21 21:16:59 +03:00
|
|
|
|
|
|
|
// Middle part
|
|
|
|
if (aMiddle) {
|
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.Y(), aDstInner.XMost(),
|
|
|
|
aDstInner.YMost(), aDstInner.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.Y(), aSrcInner.XMost(),
|
|
|
|
aSrcInner.YMost(), aSrcInner.X()),
|
|
|
|
aSkipRect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DrawMirroredRect(DrawTarget* aDT,
|
|
|
|
SourceSurface* aSurface,
|
|
|
|
const Rect& aDest, const Point& aSrc,
|
|
|
|
Float aScaleX, Float aScaleY)
|
|
|
|
{
|
|
|
|
SurfacePattern pattern(aSurface, ExtendMode::CLAMP,
|
|
|
|
Matrix::Scaling(aScaleX, aScaleY)
|
|
|
|
.PreTranslate(-aSrc)
|
|
|
|
.PostTranslate(
|
2017-12-19 23:48:39 +03:00
|
|
|
aScaleX < 0 ? aDest.XMost() : aDest.X(),
|
|
|
|
aScaleY < 0 ? aDest.YMost() : aDest.Y()));
|
2016-11-21 21:16:59 +03:00
|
|
|
aDT->FillRect(aDest, pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DrawMirroredBoxShadow(DrawTarget* aDT,
|
|
|
|
SourceSurface* aSurface,
|
|
|
|
const Rect& aDestRect)
|
|
|
|
{
|
2017-12-19 23:48:39 +03:00
|
|
|
Point center(ceil(aDestRect.X() + aDestRect.Width() / 2),
|
|
|
|
ceil(aDestRect.Y() + aDestRect.Height() / 2));
|
|
|
|
Rect topLeft(aDestRect.X(), aDestRect.Y(),
|
|
|
|
center.x - aDestRect.X(),
|
|
|
|
center.y - aDestRect.Y());
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect bottomRight(topLeft.BottomRight(), aDestRect.Size() - topLeft.Size());
|
2017-12-19 23:48:39 +03:00
|
|
|
Rect topRight(bottomRight.X(), topLeft.Y(), bottomRight.Width(), topLeft.Height());
|
|
|
|
Rect bottomLeft(topLeft.X(), bottomRight.Y(), topLeft.Width(), bottomRight.Height());
|
2016-11-21 21:16:59 +03:00
|
|
|
DrawMirroredRect(aDT, aSurface, topLeft, Point(), 1, 1);
|
|
|
|
DrawMirroredRect(aDT, aSurface, topRight, Point(), -1, 1);
|
|
|
|
DrawMirroredRect(aDT, aSurface, bottomLeft, Point(), 1, -1);
|
|
|
|
DrawMirroredRect(aDT, aSurface, bottomRight, Point(), -1, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DrawMirroredCorner(DrawTarget* aDT, SourceSurface* aSurface,
|
|
|
|
const Rect& aDest, const Point& aSrc,
|
|
|
|
const Rect& aSkipRect, Float aScaleX, Float aScaleY)
|
|
|
|
{
|
|
|
|
if (aSkipRect.Contains(aDest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawMirroredRect(aDT, aSurface, aDest, aSrc, aScaleX, aScaleY);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RepeatOrStretchMirroredSurface(DrawTarget* aDT, SourceSurface* aSurface,
|
|
|
|
const Rect& aDest, const Rect& aSrc,
|
|
|
|
const Rect& aSkipRect, Float aScaleX, Float aScaleY)
|
|
|
|
{
|
|
|
|
if (aSkipRect.Contains(aDest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ShouldStretchSurface(aDT, aSurface)) {
|
2017-12-19 23:48:39 +03:00
|
|
|
aScaleX *= aDest.Width() / aSrc.Width();
|
|
|
|
aScaleY *= aDest.Height() / aSrc.Height();
|
2016-11-21 21:16:59 +03:00
|
|
|
DrawMirroredRect(aDT, aSurface, aDest, aSrc.TopLeft(), aScaleX, aScaleY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SurfacePattern pattern(aSurface, ExtendMode::REPEAT,
|
|
|
|
Matrix::Scaling(aScaleX, aScaleY)
|
|
|
|
.PreTranslate(-aSrc.TopLeft())
|
|
|
|
.PostTranslate(
|
2017-12-19 23:48:39 +03:00
|
|
|
aScaleX < 0 ? aDest.XMost() : aDest.X(),
|
|
|
|
aScaleY < 0 ? aDest.YMost() : aDest.Y()),
|
2016-11-21 21:16:59 +03:00
|
|
|
SamplingFilter::GOOD, RoundedToInt(aSrc));
|
|
|
|
aDT->FillRect(aDest, pattern);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
DrawMirroredMinBoxShadow(DrawTarget* aDestDrawTarget, SourceSurface* aSourceBlur,
|
|
|
|
const Rect& aDstOuter, const Rect& aDstInner,
|
|
|
|
const Rect& aSrcOuter, const Rect& aSrcInner,
|
|
|
|
const Rect& aSkipRect, bool aMiddle = false)
|
|
|
|
{
|
|
|
|
// Corners: top left, top right, bottom left, bottom right
|
|
|
|
// Compute quadrant bounds and then clip them to corners along
|
|
|
|
// dimensions where we need to stretch from min size.
|
2017-12-19 23:48:39 +03:00
|
|
|
Point center(ceil(aDstOuter.X() + aDstOuter.Width() / 2),
|
|
|
|
ceil(aDstOuter.Y() + aDstOuter.Height() / 2));
|
|
|
|
Rect topLeft(aDstOuter.X(), aDstOuter.Y(),
|
|
|
|
center.x - aDstOuter.X(),
|
|
|
|
center.y - aDstOuter.Y());
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect bottomRight(topLeft.BottomRight(), aDstOuter.Size() - topLeft.Size());
|
2017-12-19 23:48:39 +03:00
|
|
|
Rect topRight(bottomRight.X(), topLeft.Y(), bottomRight.Width(), topLeft.Height());
|
|
|
|
Rect bottomLeft(topLeft.X(), bottomRight.Y(), topLeft.Width(), bottomRight.Height());
|
2016-11-21 21:16:59 +03:00
|
|
|
|
|
|
|
// Check if the middle part has been minimized along each dimension.
|
|
|
|
// If so, those will be strecthed/drawn separately and need to be clipped out.
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aSrcInner.Width() == 1) {
|
|
|
|
topLeft.SetRightEdge(aDstInner.X());
|
2016-11-21 21:16:59 +03:00
|
|
|
topRight.SetLeftEdge(aDstInner.XMost());
|
2017-12-19 23:48:39 +03:00
|
|
|
bottomLeft.SetRightEdge(aDstInner.X());
|
2016-11-21 21:16:59 +03:00
|
|
|
bottomRight.SetLeftEdge(aDstInner.XMost());
|
|
|
|
}
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aSrcInner.Height() == 1) {
|
|
|
|
topLeft.SetBottomEdge(aDstInner.Y());
|
|
|
|
topRight.SetBottomEdge(aDstInner.Y());
|
2016-11-21 21:16:59 +03:00
|
|
|
bottomLeft.SetTopEdge(aDstInner.YMost());
|
|
|
|
bottomRight.SetTopEdge(aDstInner.YMost());
|
|
|
|
}
|
|
|
|
|
|
|
|
DrawMirroredCorner(aDestDrawTarget, aSourceBlur, topLeft,
|
|
|
|
aSrcOuter.TopLeft(), aSkipRect, 1, 1);
|
|
|
|
DrawMirroredCorner(aDestDrawTarget, aSourceBlur, topRight,
|
|
|
|
aSrcOuter.TopLeft(), aSkipRect, -1, 1);
|
|
|
|
DrawMirroredCorner(aDestDrawTarget, aSourceBlur, bottomLeft,
|
|
|
|
aSrcOuter.TopLeft(), aSkipRect, 1, -1);
|
|
|
|
DrawMirroredCorner(aDestDrawTarget, aSourceBlur, bottomRight,
|
|
|
|
aSrcOuter.TopLeft(), aSkipRect, -1, -1);
|
|
|
|
|
|
|
|
// Edges: top, bottom, left, right
|
|
|
|
// Draw middle edges where they need to be stretched. The top and left
|
|
|
|
// sections that are part of the top-left quadrant will be mirrored to
|
|
|
|
// the bottom and right sections, respectively.
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aSrcInner.Width() == 1) {
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect dstTop = RectWithEdgesTRBL(aDstOuter.Y(), aDstInner.XMost(),
|
|
|
|
aDstInner.Y(), aDstInner.X());
|
|
|
|
Rect srcTop = RectWithEdgesTRBL(aSrcOuter.Y(), aSrcInner.XMost(),
|
|
|
|
aSrcInner.Y(), aSrcInner.X());
|
|
|
|
Rect dstBottom = RectWithEdgesTRBL(aDstInner.YMost(), aDstInner.XMost(),
|
|
|
|
aDstOuter.YMost(), aDstInner.X());
|
|
|
|
Rect srcBottom = RectWithEdgesTRBL(aSrcOuter.Y(), aSrcInner.XMost(),
|
|
|
|
aSrcInner.Y(), aSrcInner.X());
|
|
|
|
// If we only need to stretch along the X axis and we're drawing
|
|
|
|
// the middle section, just sample all the way to the center of the
|
|
|
|
// source on the Y axis to avoid extra draw calls.
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aMiddle && aSrcInner.Height() != 1) {
|
2016-11-21 21:16:59 +03:00
|
|
|
dstTop.SetBottomEdge(center.y);
|
2017-12-19 23:48:39 +03:00
|
|
|
srcTop.SetHeight(dstTop.Height());
|
2016-11-21 21:16:59 +03:00
|
|
|
dstBottom.SetTopEdge(dstTop.YMost());
|
2017-12-19 23:48:39 +03:00
|
|
|
srcBottom.SetHeight(dstBottom.Height());
|
2016-11-21 21:16:59 +03:00
|
|
|
}
|
|
|
|
RepeatOrStretchMirroredSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
dstTop, srcTop, aSkipRect, 1, 1);
|
|
|
|
RepeatOrStretchMirroredSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
dstBottom, srcBottom, aSkipRect, 1, -1);
|
|
|
|
}
|
|
|
|
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aSrcInner.Height() == 1) {
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect dstLeft = RectWithEdgesTRBL(aDstInner.Y(), aDstInner.X(),
|
|
|
|
aDstInner.YMost(), aDstOuter.X());
|
|
|
|
Rect srcLeft = RectWithEdgesTRBL(aSrcInner.Y(), aSrcInner.X(),
|
|
|
|
aSrcInner.YMost(), aSrcOuter.X());
|
|
|
|
Rect dstRight = RectWithEdgesTRBL(aDstInner.Y(), aDstOuter.XMost(),
|
|
|
|
aDstInner.YMost(), aDstInner.XMost());
|
|
|
|
Rect srcRight = RectWithEdgesTRBL(aSrcInner.Y(), aSrcInner.X(),
|
|
|
|
aSrcInner.YMost(), aSrcOuter.X());
|
|
|
|
// Only stretching on Y axis, so sample source to the center of the X axis.
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aMiddle && aSrcInner.Width() != 1) {
|
2016-11-21 21:16:59 +03:00
|
|
|
dstLeft.SetRightEdge(center.x);
|
2017-12-19 23:48:39 +03:00
|
|
|
srcLeft.SetWidth(dstLeft.Width());
|
2016-11-21 21:16:59 +03:00
|
|
|
dstRight.SetLeftEdge(dstLeft.XMost());
|
2017-12-19 23:48:39 +03:00
|
|
|
srcRight.SetWidth(dstRight.Width());
|
2016-11-21 21:16:59 +03:00
|
|
|
}
|
|
|
|
RepeatOrStretchMirroredSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
dstLeft, srcLeft, aSkipRect, 1, 1);
|
|
|
|
RepeatOrStretchMirroredSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
dstRight, srcRight, aSkipRect, -1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we need to stretch along both dimensions, then the middle part
|
|
|
|
// must be drawn separately.
|
2017-12-19 23:48:39 +03:00
|
|
|
if (aMiddle && aSrcInner.Width() == 1 && aSrcInner.Height() == 1) {
|
2016-11-21 21:16:59 +03:00
|
|
|
RepeatOrStretchSurface(aDestDrawTarget, aSourceBlur,
|
|
|
|
RectWithEdgesTRBL(aDstInner.Y(), aDstInner.XMost(),
|
|
|
|
aDstInner.YMost(), aDstInner.X()),
|
|
|
|
RectWithEdgesTRBL(aSrcInner.Y(), aSrcInner.XMost(),
|
|
|
|
aSrcInner.YMost(), aSrcInner.X()),
|
|
|
|
aSkipRect);
|
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
}
|
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
/***
|
|
|
|
* We draw a blurred a rectangle by only blurring a smaller rectangle and
|
|
|
|
* splitting the rectangle into 9 parts.
|
|
|
|
* First, a small minimum source rect is calculated and used to create a blur
|
|
|
|
* mask since the actual blurring itself is expensive. Next, we use the mask
|
|
|
|
* with the given shadow color to create a minimally-sized box shadow of the
|
|
|
|
* right color. Finally, we cut out the 9 parts from the box-shadow source and
|
|
|
|
* paint each part in the right place, stretching the non-corner parts to fill
|
|
|
|
* the space between the corners.
|
|
|
|
*/
|
|
|
|
|
2013-11-26 03:08:29 +04:00
|
|
|
/* static */ void
|
2015-06-25 22:04:21 +03:00
|
|
|
gfxAlphaBoxBlur::BlurRectangle(gfxContext* aDestinationCtx,
|
2013-11-26 03:08:29 +04:00
|
|
|
const gfxRect& aRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2013-11-26 03:09:04 +04:00
|
|
|
const gfxPoint& aBlurStdDev,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2013-11-26 03:08:29 +04:00
|
|
|
const gfxRect& aDirtyRect,
|
|
|
|
const gfxRect& aSkipRect)
|
|
|
|
{
|
2015-06-01 11:26:19 +03:00
|
|
|
IntSize blurRadius = CalculateBlurRadius(aBlurStdDev);
|
2016-11-21 21:16:59 +03:00
|
|
|
bool mirrorCorners = !aCornerRadii || aCornerRadii->AreRadiiSame();
|
2013-11-26 03:08:29 +04:00
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
IntRect rect = RoundedToInt(ToRect(aRect));
|
2016-11-21 21:16:59 +03:00
|
|
|
IntMargin blurMargin;
|
2015-05-13 22:19:27 +03:00
|
|
|
IntMargin slice;
|
2016-11-21 21:16:59 +03:00
|
|
|
IntSize minSize;
|
2016-01-11 01:05:26 +03:00
|
|
|
RefPtr<SourceSurface> boxShadow = GetBlur(aDestinationCtx,
|
2015-06-25 22:04:21 +03:00
|
|
|
rect.Size(), blurRadius,
|
2016-11-21 21:16:59 +03:00
|
|
|
aCornerRadii, aShadowColor, mirrorCorners,
|
|
|
|
blurMargin, slice, minSize);
|
2015-06-25 22:04:21 +03:00
|
|
|
if (!boxShadow) {
|
2015-05-13 22:19:27 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-05-18 18:30:41 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
DrawTarget* destDrawTarget = aDestinationCtx->GetDrawTarget();
|
|
|
|
destDrawTarget->PushClipRect(ToRect(aDirtyRect));
|
2015-05-13 22:19:27 +03:00
|
|
|
|
|
|
|
// Copy the right parts from boxShadow into destDrawTarget. The middle parts
|
|
|
|
// will be stretched, border-image style.
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
Rect srcOuter(Point(blurMargin.left, blurMargin.top), Size(minSize));
|
|
|
|
Rect srcInner(srcOuter);
|
|
|
|
srcOuter.Inflate(Margin(blurMargin));
|
2015-05-13 22:19:27 +03:00
|
|
|
srcInner.Deflate(Margin(slice));
|
|
|
|
|
|
|
|
Rect dstOuter(rect);
|
|
|
|
Rect dstInner(rect);
|
2016-11-21 21:16:59 +03:00
|
|
|
dstOuter.Inflate(Margin(blurMargin));
|
2015-05-13 22:19:27 +03:00
|
|
|
dstInner.Deflate(Margin(slice));
|
|
|
|
|
|
|
|
Rect skipRect = ToRect(aSkipRect);
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
if (minSize == rect.Size()) {
|
2015-05-13 22:19:27 +03:00
|
|
|
// The target rect is smaller than the minimal size so just draw the surface
|
2016-11-21 21:16:59 +03:00
|
|
|
if (mirrorCorners) {
|
|
|
|
DrawMirroredBoxShadow(destDrawTarget, boxShadow, dstOuter);
|
|
|
|
} else {
|
|
|
|
destDrawTarget->DrawSurface(boxShadow, dstOuter, srcOuter);
|
|
|
|
}
|
2015-05-13 22:19:27 +03:00
|
|
|
} else {
|
2016-11-21 21:16:59 +03:00
|
|
|
if (mirrorCorners) {
|
|
|
|
DrawMirroredMinBoxShadow(destDrawTarget, boxShadow, dstOuter, dstInner,
|
|
|
|
srcOuter, srcInner, skipRect, true);
|
|
|
|
} else {
|
|
|
|
DrawMinBoxShadow(destDrawTarget, boxShadow, dstOuter, dstInner,
|
|
|
|
srcOuter, srcInner, skipRect, true);
|
|
|
|
}
|
2013-11-26 03:08:29 +04:00
|
|
|
}
|
|
|
|
|
2015-05-13 22:19:27 +03:00
|
|
|
// A note about anti-aliasing and seems between adjacent parts:
|
|
|
|
// We don't explicitly disable anti-aliasing in the DrawSurface calls above,
|
|
|
|
// so if there's a transform on destDrawTarget that is not pixel-aligned,
|
|
|
|
// there will be seams between adjacent parts of the box-shadow. It's hard to
|
|
|
|
// avoid those without the use of an intermediate surface.
|
|
|
|
// You might think that we could avoid those by just turning of AA, but there
|
|
|
|
// is a problem with that: Box-shadow rendering needs to clip out the
|
|
|
|
// element's border box, and we'd like that clip to have anti-aliasing -
|
|
|
|
// especially if the element has rounded corners! So we can't do that unless
|
|
|
|
// we have a way to say "Please anti-alias the clip, but don't antialias the
|
|
|
|
// destination rect of the DrawSurface call".
|
|
|
|
// On OS X there is an additional problem with turning off AA: CoreGraphics
|
|
|
|
// will not just fill the pixels that have their pixel center inside the
|
|
|
|
// filled shape. Instead, it will fill all the pixels which are partially
|
|
|
|
// covered by the shape. So for pixels on the edge between two adjacent parts,
|
|
|
|
// all those pixels will be painted to by both parts, which looks very bad.
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
destDrawTarget->PopClip();
|
2013-11-26 03:08:29 +04:00
|
|
|
}
|
2014-06-11 01:51:24 +04:00
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
static already_AddRefed<Path>
|
|
|
|
GetBoxShadowInsetPath(DrawTarget* aDrawTarget,
|
|
|
|
const Rect aOuterRect, const Rect aInnerRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aInnerClipRadii)
|
2015-09-24 19:50:29 +03:00
|
|
|
{
|
|
|
|
/***
|
|
|
|
* We create an inset path by having two rects.
|
|
|
|
*
|
|
|
|
* -----------------------
|
|
|
|
* | ________________ |
|
|
|
|
* | | | |
|
|
|
|
* | | | |
|
|
|
|
* | ------------------ |
|
|
|
|
* |_____________________|
|
|
|
|
*
|
|
|
|
* The outer rect and the inside rect. The path
|
|
|
|
* creates a frame around the content where we draw the inset shadow.
|
|
|
|
*/
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PathBuilder> builder =
|
2015-09-24 19:50:29 +03:00
|
|
|
aDrawTarget->CreatePathBuilder(FillRule::FILL_EVEN_ODD);
|
|
|
|
AppendRectToPath(builder, aOuterRect, true);
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
if (aInnerClipRadii) {
|
|
|
|
AppendRoundedRectToPath(builder, aInnerRect, *aInnerClipRadii, false);
|
2015-09-24 19:50:29 +03:00
|
|
|
} else {
|
|
|
|
AppendRectToPath(builder, aInnerRect, false);
|
|
|
|
}
|
|
|
|
return builder->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
FillDestinationPath(gfxContext* aDestinationCtx,
|
2016-11-21 21:16:59 +03:00
|
|
|
const Rect& aDestinationRect,
|
|
|
|
const Rect& aShadowClipRect,
|
2015-09-24 19:50:29 +03:00
|
|
|
const Color& aShadowColor,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aInnerClipRadii = nullptr)
|
2015-09-24 19:50:29 +03:00
|
|
|
{
|
|
|
|
// When there is no blur radius, fill the path onto the destination
|
|
|
|
// surface.
|
2015-09-24 10:43:32 +03:00
|
|
|
aDestinationCtx->SetColor(aShadowColor);
|
2015-09-24 19:50:29 +03:00
|
|
|
DrawTarget* destDrawTarget = aDestinationCtx->GetDrawTarget();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Path> shadowPath = GetBoxShadowInsetPath(destDrawTarget, aDestinationRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
aShadowClipRect, aInnerClipRadii);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
|
|
|
aDestinationCtx->SetPath(shadowPath);
|
|
|
|
aDestinationCtx->Fill();
|
|
|
|
}
|
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
static void
|
2016-11-21 21:16:59 +03:00
|
|
|
CacheInsetBlur(const IntSize& aMinOuterSize,
|
|
|
|
const IntSize& aMinInnerSize,
|
2015-09-23 21:49:05 +03:00
|
|
|
const IntSize& aBlurRadius,
|
2015-09-24 19:50:29 +03:00
|
|
|
const RectCornerRadii* aCornerRadii,
|
2015-09-24 10:43:32 +03:00
|
|
|
const Color& aShadowColor,
|
2015-09-24 19:50:29 +03:00
|
|
|
BackendType aBackendType,
|
|
|
|
SourceSurface* aBoxShadow)
|
|
|
|
{
|
2016-03-29 18:24:21 +03:00
|
|
|
bool isInsetBlur = true;
|
2015-09-24 19:50:29 +03:00
|
|
|
BlurCacheKey key(aMinOuterSize, aMinInnerSize,
|
2016-03-29 18:24:21 +03:00
|
|
|
aBlurRadius, aCornerRadii,
|
|
|
|
aShadowColor, isInsetBlur,
|
2016-11-21 21:16:59 +03:00
|
|
|
aBackendType);
|
|
|
|
IntMargin blurMargin(0, 0, 0, 0);
|
2017-11-11 00:43:54 +03:00
|
|
|
BlurCacheData* data = new BlurCacheData(aBoxShadow, blurMargin, Move(key));
|
2015-09-24 19:50:29 +03:00
|
|
|
if (!gBlurCache->RegisterEntry(data)) {
|
|
|
|
delete data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
already_AddRefed<SourceSurface>
|
|
|
|
gfxAlphaBoxBlur::GetInsetBlur(const Rect& aOuterRect,
|
|
|
|
const Rect& aWhitespaceRect,
|
|
|
|
bool aIsDestRect,
|
|
|
|
const Color& aShadowColor,
|
|
|
|
const IntSize& aBlurRadius,
|
|
|
|
const RectCornerRadii* aInnerClipRadii,
|
|
|
|
DrawTarget* aDestDrawTarget,
|
|
|
|
bool aMirrorCorners)
|
2015-09-24 19:50:29 +03:00
|
|
|
{
|
|
|
|
if (!gBlurCache) {
|
|
|
|
gBlurCache = new BlurCache();
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
IntSize outerSize = IntSize::Truncate(aOuterRect.Size());
|
|
|
|
IntSize whitespaceSize = IntSize::Truncate(aWhitespaceRect.Size());
|
|
|
|
if (!aIsDestRect) {
|
|
|
|
BlurCacheData* cached =
|
2016-03-29 18:24:21 +03:00
|
|
|
gBlurCache->LookupInsetBoxShadow(outerSize, whitespaceSize,
|
2016-11-21 21:16:59 +03:00
|
|
|
aBlurRadius, aInnerClipRadii,
|
|
|
|
aShadowColor, aDestDrawTarget->GetBackendType());
|
|
|
|
if (cached) {
|
|
|
|
// So we don't forget the actual cached blur
|
|
|
|
RefPtr<SourceSurface> cachedBlur = cached->mBlur;
|
|
|
|
return cachedBlur.forget();
|
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
}
|
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
// If we can do a min rect, the whitespace rect will be expanded in Init to
|
|
|
|
// aOuterRect.
|
|
|
|
Rect blurRect = aIsDestRect ? aOuterRect : aWhitespaceRect;
|
2016-11-21 21:16:59 +03:00
|
|
|
// If mirroring corners, we only need to draw the top-left quadrant.
|
|
|
|
// Use ceil to preserve the remaining 1x1 middle area for minimized box
|
|
|
|
// shadows.
|
|
|
|
if (aMirrorCorners) {
|
2017-12-19 23:48:39 +03:00
|
|
|
blurRect.SizeTo(ceil(blurRect.Width() * 0.5f), ceil(blurRect.Height() * 0.5f));
|
2016-11-21 21:16:59 +03:00
|
|
|
}
|
2015-09-23 21:49:05 +03:00
|
|
|
IntSize zeroSpread(0, 0);
|
2016-11-21 21:16:59 +03:00
|
|
|
RefPtr<DrawTarget> minDrawTarget =
|
2016-11-21 21:17:09 +03:00
|
|
|
InitDrawTarget(aDestDrawTarget, blurRect, zeroSpread, aBlurRadius);
|
2016-11-21 21:16:59 +03:00
|
|
|
if (!minDrawTarget) {
|
2015-09-24 19:50:29 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
// This is really annoying. When we create the AlphaBoxBlur, the DrawTarget
|
2016-03-29 18:24:21 +03:00
|
|
|
// has a translation applied to it that is the topLeft point. This is actually
|
|
|
|
// the rect we gave it plus the blur radius. The rects we give this for the outer
|
|
|
|
// and whitespace rects are based at (0, 0). We could either translate those rects
|
|
|
|
// when we don't have a destination rect or ignore the translation when using
|
|
|
|
// the dest rect. The dest rects layout gives us expect this translation.
|
|
|
|
if (!aIsDestRect) {
|
2016-11-21 21:16:59 +03:00
|
|
|
minDrawTarget->SetTransform(Matrix());
|
2016-03-29 18:24:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in the path between the inside white space / outer rects
|
|
|
|
// NOT the inner frame
|
2015-11-12 01:23:14 +03:00
|
|
|
RefPtr<Path> maskPath =
|
2016-03-29 18:24:21 +03:00
|
|
|
GetBoxShadowInsetPath(minDrawTarget, aOuterRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
aWhitespaceRect, aInnerClipRadii);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
ColorPattern black(Color(0.f, 0.f, 0.f, 1.f));
|
|
|
|
minDrawTarget->Fill(maskPath, black);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
2016-11-21 21:17:09 +03:00
|
|
|
// Blur and fill in with the color we actually wanted
|
|
|
|
RefPtr<SourceSurface> minInsetBlur = DoBlur(&aShadowColor);
|
2015-09-24 19:50:29 +03:00
|
|
|
if (!minInsetBlur) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-06-16 23:50:39 +03:00
|
|
|
if (RefPtr<SourceSurface> opt = aDestDrawTarget->OptimizeSourceSurface(minInsetBlur)) {
|
|
|
|
minInsetBlur = opt;
|
|
|
|
}
|
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
if (!aIsDestRect) {
|
|
|
|
CacheInsetBlur(outerSize, whitespaceSize,
|
2016-11-21 21:16:59 +03:00
|
|
|
aBlurRadius, aInnerClipRadii,
|
|
|
|
aShadowColor, aDestDrawTarget->GetBackendType(),
|
2016-03-29 18:24:21 +03:00
|
|
|
minInsetBlur);
|
2015-12-04 22:05:47 +03:00
|
|
|
}
|
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
return minInsetBlur.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
2016-03-29 18:24:21 +03:00
|
|
|
* We create our minimal rect with 2 rects.
|
|
|
|
* The first is the inside whitespace rect, that is "cut out"
|
|
|
|
* from the box. This is (1). This must be the size
|
|
|
|
* of the blur radius + corner radius so we can have a big enough
|
|
|
|
* inside cut.
|
|
|
|
*
|
|
|
|
* The second (2) is one blur radius surrounding the inner
|
|
|
|
* frame of (1). This is the amount of blur space required
|
|
|
|
* to get a proper blend.
|
2015-09-24 19:50:29 +03:00
|
|
|
*
|
2016-03-29 18:24:21 +03:00
|
|
|
* B = one blur size
|
|
|
|
* W = one blur + corner radii - known as inner margin
|
|
|
|
* ___________________________________
|
|
|
|
* | |
|
|
|
|
* | | | |
|
|
|
|
* | (2) | (1) | (2) |
|
|
|
|
* | B | W | B |
|
|
|
|
* | | | |
|
|
|
|
* | | | |
|
|
|
|
* | | |
|
|
|
|
* |________________________________|
|
2015-09-24 19:50:29 +03:00
|
|
|
*/
|
2016-11-21 21:16:59 +03:00
|
|
|
static void GetBlurMargins(const RectCornerRadii* aInnerClipRadii,
|
|
|
|
const IntSize& aBlurRadius,
|
2016-03-29 18:24:21 +03:00
|
|
|
Margin& aOutBlurMargin,
|
|
|
|
Margin& aOutInnerMargin)
|
|
|
|
{
|
2016-11-21 21:16:59 +03:00
|
|
|
Size cornerSize(0, 0);
|
|
|
|
if (aInnerClipRadii) {
|
|
|
|
const RectCornerRadii& corners = *aInnerClipRadii;
|
2017-01-05 12:07:07 +03:00
|
|
|
NS_FOR_CSS_FULL_CORNERS(i) {
|
2016-11-21 21:16:59 +03:00
|
|
|
cornerSize.width = std::max(cornerSize.width, corners[i].width);
|
|
|
|
cornerSize.height = std::max(cornerSize.height, corners[i].height);
|
2016-03-29 18:24:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only the inside whitespace size cares about the border radius size.
|
|
|
|
// Outer sizes only care about blur.
|
2016-11-21 21:16:59 +03:00
|
|
|
IntSize margin = IntSize::Ceil(cornerSize) + aBlurRadius;
|
2016-03-29 18:24:21 +03:00
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
aOutInnerMargin.SizeTo(margin.height, margin.width,
|
|
|
|
margin.height, margin.width);
|
2016-03-29 18:24:21 +03:00
|
|
|
aOutBlurMargin.SizeTo(aBlurRadius.height, aBlurRadius.width,
|
|
|
|
aBlurRadius.height, aBlurRadius.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2016-11-21 21:16:59 +03:00
|
|
|
GetInsetBoxShadowRects(const Margin& aBlurMargin,
|
|
|
|
const Margin& aInnerMargin,
|
|
|
|
const Rect& aShadowClipRect,
|
|
|
|
const Rect& aDestinationRect,
|
2016-03-29 18:24:21 +03:00
|
|
|
Rect& aOutWhitespaceRect,
|
|
|
|
Rect& aOutOuterRect)
|
|
|
|
{
|
|
|
|
// We always copy (2 * blur radius) + corner radius worth of data to the destination rect
|
|
|
|
// This covers the blend of the path + the actual blur
|
|
|
|
// Need +1 so that we copy the edges correctly as we'll copy
|
|
|
|
// over the min box shadow corners then the +1 for the edges between
|
|
|
|
// Note, the (x,y) coordinates are from the blur margin
|
|
|
|
// since the frame outside the whitespace rect is 1 blur radius extra space.
|
|
|
|
Rect insideWhiteSpace(aBlurMargin.left,
|
|
|
|
aBlurMargin.top,
|
|
|
|
aInnerMargin.LeftRight() + 1,
|
|
|
|
aInnerMargin.TopBottom() + 1);
|
|
|
|
|
|
|
|
// If the inner white space rect is larger than the shadow clip rect
|
|
|
|
// our approach does not work as we'll just copy one corner
|
|
|
|
// and cover the destination. In those cases, fallback to the destination rect
|
2017-12-19 23:48:39 +03:00
|
|
|
bool useDestRect = (aShadowClipRect.Width() <= aInnerMargin.LeftRight()) ||
|
|
|
|
(aShadowClipRect.Height() <= aInnerMargin.TopBottom());
|
2016-03-29 18:24:21 +03:00
|
|
|
|
|
|
|
if (useDestRect) {
|
|
|
|
aOutWhitespaceRect = aShadowClipRect;
|
|
|
|
aOutOuterRect = aDestinationRect;
|
|
|
|
} else {
|
|
|
|
aOutWhitespaceRect = insideWhiteSpace;
|
|
|
|
aOutOuterRect = aOutWhitespaceRect;
|
|
|
|
aOutOuterRect.Inflate(aBlurMargin);
|
|
|
|
}
|
|
|
|
|
|
|
|
return useDestRect;
|
|
|
|
}
|
|
|
|
|
2015-09-24 19:50:29 +03:00
|
|
|
void
|
|
|
|
gfxAlphaBoxBlur::BlurInsetBox(gfxContext* aDestinationCtx,
|
2016-11-21 21:16:59 +03:00
|
|
|
const Rect& aDestinationRect,
|
|
|
|
const Rect& aShadowClipRect,
|
|
|
|
const IntSize& aBlurRadius,
|
2015-09-24 19:50:29 +03:00
|
|
|
const Color& aShadowColor,
|
2016-11-21 21:16:59 +03:00
|
|
|
const RectCornerRadii* aInnerClipRadii,
|
|
|
|
const Rect& aSkipRect,
|
|
|
|
const Point& aShadowOffset)
|
2015-09-24 19:50:29 +03:00
|
|
|
{
|
2016-03-29 18:24:21 +03:00
|
|
|
if ((aBlurRadius.width == 0 && aBlurRadius.height == 0)
|
|
|
|
|| aShadowClipRect.IsEmpty()) {
|
2015-09-24 19:50:29 +03:00
|
|
|
FillDestinationPath(aDestinationCtx, aDestinationRect, aShadowClipRect,
|
2016-11-21 21:16:59 +03:00
|
|
|
aShadowColor, aInnerClipRadii);
|
2015-09-24 19:50:29 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
DrawTarget* destDrawTarget = aDestinationCtx->GetDrawTarget();
|
2015-09-24 19:50:29 +03:00
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
Margin innerMargin;
|
|
|
|
Margin blurMargin;
|
2016-11-21 21:16:59 +03:00
|
|
|
GetBlurMargins(aInnerClipRadii, aBlurRadius, blurMargin, innerMargin);
|
2015-09-24 19:50:29 +03:00
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
Rect whitespaceRect;
|
|
|
|
Rect outerRect;
|
2016-11-21 21:16:59 +03:00
|
|
|
bool useDestRect =
|
|
|
|
GetInsetBoxShadowRects(blurMargin, innerMargin, aShadowClipRect,
|
|
|
|
aDestinationRect, whitespaceRect, outerRect);
|
|
|
|
|
2017-01-25 22:53:38 +03:00
|
|
|
// Check that the inset margin between the outer and whitespace rects is symmetric,
|
|
|
|
// and that all corner radii are the same, in which case the blur can be mirrored.
|
|
|
|
Margin checkMargin = outerRect - whitespaceRect;
|
|
|
|
bool mirrorCorners =
|
|
|
|
checkMargin.left == checkMargin.right &&
|
|
|
|
checkMargin.top == checkMargin.bottom &&
|
|
|
|
(!aInnerClipRadii || aInnerClipRadii->AreRadiiSame());
|
2016-11-21 21:16:59 +03:00
|
|
|
RefPtr<SourceSurface> minBlur =
|
|
|
|
GetInsetBlur(outerRect, whitespaceRect, useDestRect, aShadowColor,
|
|
|
|
aBlurRadius, aInnerClipRadii, destDrawTarget, mirrorCorners);
|
2016-03-29 18:24:21 +03:00
|
|
|
if (!minBlur) {
|
|
|
|
return;
|
2015-12-04 22:05:47 +03:00
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
|
2016-03-29 18:24:21 +03:00
|
|
|
if (useDestRect) {
|
|
|
|
Rect destBlur = aDestinationRect;
|
|
|
|
destBlur.Inflate(blurMargin);
|
2016-11-21 21:16:59 +03:00
|
|
|
if (mirrorCorners) {
|
|
|
|
DrawMirroredBoxShadow(destDrawTarget, minBlur.get(), destBlur);
|
|
|
|
} else {
|
|
|
|
Rect srcBlur(Point(0, 0), Size(minBlur->GetSize()));
|
|
|
|
MOZ_ASSERT(srcBlur.Size() == destBlur.Size());
|
|
|
|
destDrawTarget->DrawSurface(minBlur, destBlur, srcBlur);
|
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
} else {
|
2016-03-29 18:24:21 +03:00
|
|
|
Rect srcOuter(outerRect);
|
|
|
|
Rect srcInner(srcOuter);
|
|
|
|
srcInner.Deflate(blurMargin); // The outer color fill
|
|
|
|
srcInner.Deflate(innerMargin); // The inner whitespace
|
|
|
|
|
|
|
|
// The shadow clip rect already takes into account the spread radius
|
|
|
|
Rect outerFillRect(aShadowClipRect);
|
|
|
|
outerFillRect.Inflate(blurMargin);
|
2016-11-21 21:16:59 +03:00
|
|
|
FillDestinationPath(aDestinationCtx, aDestinationRect, outerFillRect, aShadowColor);
|
2016-03-29 18:24:21 +03:00
|
|
|
|
|
|
|
// Inflate once for the frame around the whitespace
|
|
|
|
Rect destRect(aShadowClipRect);
|
|
|
|
destRect.Inflate(blurMargin);
|
|
|
|
|
|
|
|
// Deflate for the blurred in white space
|
|
|
|
Rect destInnerRect(aShadowClipRect);
|
|
|
|
destInnerRect.Deflate(innerMargin);
|
|
|
|
|
2016-11-21 21:16:59 +03:00
|
|
|
if (mirrorCorners) {
|
|
|
|
DrawMirroredMinBoxShadow(destDrawTarget, minBlur,
|
|
|
|
destRect, destInnerRect,
|
|
|
|
srcOuter, srcInner,
|
|
|
|
aSkipRect);
|
|
|
|
} else {
|
|
|
|
DrawMinBoxShadow(destDrawTarget, minBlur,
|
|
|
|
destRect, destInnerRect,
|
|
|
|
srcOuter, srcInner,
|
|
|
|
aSkipRect);
|
|
|
|
}
|
2015-10-16 18:27:51 +03:00
|
|
|
}
|
2015-09-24 19:50:29 +03:00
|
|
|
}
|