Bug 1697344 p3: Don't hold the GradientStops object on CanvasGradient. r=jrmuizel

In the DrawTargetRecording case we create new GradientStopsRecording each time
and holding onto them in the content process can mean they take a very large
amount of memory in the GPU process, if a script deliberately creates lots of
unique stops.
In the non-recording case then the GradientStops are cached in the content
process anyway.

Differential Revision: https://phabricator.services.mozilla.com/D109792
This commit is contained in:
Bob Owen 2021-03-29 12:12:21 +00:00
parent 7fb6f51d74
commit 87882aa75a
8 changed files with 28 additions and 39 deletions

View File

@ -26,16 +26,10 @@ class CanvasGradient : public nsWrapperCache {
Type GetType() { return mType; }
mozilla::gfx::GradientStops* GetGradientStopsForTarget(
already_AddRefed<mozilla::gfx::GradientStops> GetGradientStopsForTarget(
mozilla::gfx::DrawTarget* aRT) {
if (mStops && mStops->GetBackendType() == aRT->GetBackendType()) {
return mStops;
}
mStops = gfx::gfxGradientCache::GetOrCreateGradientStops(
return gfx::gfxGradientCache::GetOrCreateGradientStops(
aRT, mRawStops, gfx::ExtendMode::CLAMP);
return mStops;
}
// WebIDL
@ -57,7 +51,6 @@ class CanvasGradient : public nsWrapperCache {
RefPtr<CanvasRenderingContext2D> mContext;
nsTArray<mozilla::gfx::GradientStop> mRawStops;
RefPtr<mozilla::gfx::GradientStops> mStops;
Type mType;
};

View File

@ -751,8 +751,6 @@ void CanvasGradient::AddColorStop(float aOffset, const nsACString& aColorstr,
return aRv.ThrowSyntaxError("Invalid color");
}
mStops = nullptr;
GradientStop newStop;
newStop.offset = aOffset;

View File

@ -242,7 +242,8 @@ class LinearGradientPattern : public Pattern {
public:
/// For constructor parameter description, see member data documentation.
LinearGradientPattern(const Point& aBegin, const Point& aEnd,
GradientStops* aStops, const Matrix& aMatrix = Matrix())
already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix())
: mBegin(aBegin), mEnd(aEnd), mStops(aStops), mMatrix(aMatrix) {}
PatternType GetType() const override { return PatternType::LINEAR_GRADIENT; }
@ -268,7 +269,8 @@ class RadialGradientPattern : public Pattern {
public:
/// For constructor parameter description, see member data documentation.
RadialGradientPattern(const Point& aCenter1, const Point& aCenter2,
Float aRadius1, Float aRadius2, GradientStops* aStops,
Float aRadius1, Float aRadius2,
already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix())
: mCenter1(aCenter1),
mCenter2(aCenter2),
@ -299,7 +301,7 @@ class ConicGradientPattern : public Pattern {
public:
/// For constructor parameter description, see member data documentation.
ConicGradientPattern(const Point& aCenter, Float aAngle, Float aStartOffset,
Float aEndOffset, GradientStops* aStops,
Float aEndOffset, already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix())
: mCenter(aCenter),
mAngle(aAngle),

View File

@ -133,12 +133,13 @@ static SourceSurface* GetSourceSurface(SourceSurface* aSurface) {
return static_cast<SourceSurfaceWrapAndRecord*>(aSurface)->mFinalSurface;
}
static GradientStops* GetGradientStops(GradientStops* aStops) {
static already_AddRefed<GradientStops> GetGradientStops(GradientStops* aStops) {
if (aStops->GetBackendType() != BackendType::RECORDING) {
return aStops;
return do_AddRef(aStops);
}
return static_cast<GradientStopsWrapAndRecord*>(aStops)->mFinalGradientStops;
return do_AddRef(
static_cast<GradientStopsWrapAndRecord*>(aStops)->mFinalGradientStops);
}
class FilterNodeWrapAndRecord : public FilterNode {

View File

@ -70,17 +70,9 @@ class InlineTranslator : public Translator {
return result;
}
GradientStops* LookupGradientStops(ReferencePtr aRefPtr) final {
DebugOnly<bool> found;
GradientStops* result = mGradientStops.GetWeak(aRefPtr
#if defined(DEBUG)
,
&found
#endif
);
// GradientStops can be null in some circumstances.
MOZ_ASSERT(found);
return result;
already_AddRefed<GradientStops> LookupGradientStops(
ReferencePtr aRefPtr) final {
return mGradientStops.Get(aRefPtr);
}
ScaledFont* LookupScaledFont(ReferencePtr aRefPtr) final {

View File

@ -73,29 +73,32 @@ class GeneralPattern final {
}
LinearGradientPattern* InitLinearGradientPattern(
const Point& aBegin, const Point& aEnd, GradientStops* aStops,
const Point& aBegin, const Point& aEnd,
already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix()) {
MOZ_ASSERT(!mPattern);
mPattern = new (mLinearGradientPattern.addr())
LinearGradientPattern(aBegin, aEnd, aStops, aMatrix);
LinearGradientPattern(aBegin, aEnd, std::move(aStops), aMatrix);
return mLinearGradientPattern.addr();
}
RadialGradientPattern* InitRadialGradientPattern(
const Point& aCenter1, const Point& aCenter2, Float aRadius1,
Float aRadius2, GradientStops* aStops, const Matrix& aMatrix = Matrix()) {
Float aRadius2, already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix()) {
MOZ_ASSERT(!mPattern);
mPattern = new (mRadialGradientPattern.addr()) RadialGradientPattern(
aCenter1, aCenter2, aRadius1, aRadius2, aStops, aMatrix);
aCenter1, aCenter2, aRadius1, aRadius2, std::move(aStops), aMatrix);
return mRadialGradientPattern.addr();
}
ConicGradientPattern* InitConicGradientPattern(
const Point& aCenter, Float aAngle, Float aStartOffset, Float aEndOffset,
GradientStops* aStops, const Matrix& aMatrix = Matrix()) {
already_AddRefed<GradientStops> aStops,
const Matrix& aMatrix = Matrix()) {
MOZ_ASSERT(!mPattern);
mPattern = new (mConicGradientPattern.addr()) ConicGradientPattern(
aCenter, aAngle, aStartOffset, aEndOffset, aStops, aMatrix);
aCenter, aAngle, aStartOffset, aEndOffset, std::move(aStops), aMatrix);
return mConicGradientPattern.addr();
}

View File

@ -93,7 +93,8 @@ class Translator {
virtual Path* LookupPath(ReferencePtr aRefPtr) = 0;
virtual SourceSurface* LookupSourceSurface(ReferencePtr aRefPtr) = 0;
virtual FilterNode* LookupFilterNode(ReferencePtr aRefPtr) = 0;
virtual GradientStops* LookupGradientStops(ReferencePtr aRefPtr) = 0;
virtual already_AddRefed<GradientStops> LookupGradientStops(
ReferencePtr aRefPtr) = 0;
virtual ScaledFont* LookupScaledFont(ReferencePtr aRefPtr) = 0;
virtual UnscaledFont* LookupUnscaledFont(ReferencePtr aRefPtr) = 0;
virtual NativeFontResource* LookupNativeFontResource(uint64_t aKey) = 0;

View File

@ -68,10 +68,9 @@ class PrintTranslator final : public Translator {
return result;
}
GradientStops* LookupGradientStops(ReferencePtr aRefPtr) final {
GradientStops* result = mGradientStops.GetWeak(aRefPtr);
MOZ_ASSERT(result);
return result;
already_AddRefed<GradientStops> LookupGradientStops(
ReferencePtr aRefPtr) final {
return mGradientStops.Get(aRefPtr);
}
ScaledFont* LookupScaledFont(ReferencePtr aRefPtr) final {