Skip to content

Commit

Permalink
Bug 1815404 - Remove refcounting from gfxContext. r=gfx-reviewers,lsa…
Browse files Browse the repository at this point in the history
…lzman

Depends on D170367

Differential Revision: https://phabricator.services.mozilla.com/D170369
  • Loading branch information
jfkthame committed Feb 21, 2023
1 parent a21b83f commit ffe28a6
Show file tree
Hide file tree
Showing 57 changed files with 188 additions and 188 deletions.
12 changes: 6 additions & 6 deletions dom/canvas/CanvasRenderingContext2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3857,14 +3857,14 @@ struct MOZ_STACK_CLASS CanvasBidiProcessor final
return;
}

RefPtr<gfxContext> thebes =
UniquePtr<gfxContext> thebes =
gfxContext::CreatePreservingTransformOrNull(target);
if (!thebes) {
// If CreatePreservingTransformOrNull returns null, it will also have
// issued a gfxCriticalNote already, so here we'll just bail out.
return;
}
gfxTextRun::DrawParams params(thebes);
gfxTextRun::DrawParams params(thebes.get());

params.allowGDI = false;

Expand Down Expand Up @@ -5027,7 +5027,7 @@ void CanvasRenderingContext2D::DrawDirectlyToCanvas(
// the matrix even though this is a temp gfxContext.
AutoRestoreTransform autoRestoreTransform(mTarget);

RefPtr<gfxContext> context = gfxContext::CreateOrNull(tempTarget);
UniquePtr<gfxContext> context = gfxContext::CreateOrNull(tempTarget);
if (!context) {
gfxDevCrash(LogReason::InvalidContext) << "Canvas context problem";
return;
Expand All @@ -5047,7 +5047,7 @@ void CanvasRenderingContext2D::DrawDirectlyToCanvas(
SVGImageContext svgContext(Some(sz));

auto result = aImage.mImgContainer->Draw(
context, scaledImageSize,
context.get(), scaledImageSize,
ImageRegion::Create(gfxRect(aSrc.x, aSrc.y, aSrc.width, aSrc.height)),
aImage.mWhichFrame, SamplingFilter::GOOD, svgContext, modifiedFlags,
CurrentState().globalAlpha);
Expand Down Expand Up @@ -5225,7 +5225,7 @@ void CanvasRenderingContext2D::DrawWindow(nsGlobalWindowInner& aWindow,
return;
}

RefPtr<gfxContext> thebes;
UniquePtr<gfxContext> thebes;
RefPtr<DrawTarget> drawDT;
// Rendering directly is faster and can be done if mTarget supports Azure
// and does not need alpha blending.
Expand Down Expand Up @@ -5275,7 +5275,7 @@ void CanvasRenderingContext2D::DrawWindow(nsGlobalWindowInner& aWindow,
RefPtr<PresShell> presShell = presContext->PresShell();

Unused << presShell->RenderDocument(r, renderDocFlags, backgroundColor,
thebes);
thebes.get());
// If this canvas was contained in the drawn window, the pre-transaction
// callback may have returned its DT. If so, we must reacquire it here.
EnsureTarget(discardContent ? &drawRect : nullptr);
Expand Down
4 changes: 2 additions & 2 deletions gfx/ipc/CrossProcessPaint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ PaintFragment PaintFragment::Record(dom::BrowsingContext* aBc,
dt->AddUserData(&sDisablePixelSnapping, (void*)0x1, nullptr);
}

RefPtr<gfxContext> thebes = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> thebes = gfxContext::CreateOrNull(dt);
thebes->SetMatrix(Matrix::Scaling(aScale, aScale));
thebes->SetCrossProcessPaintScale(aScale);
RefPtr<PresShell> presShell = presContext->PresShell();
Unused << presShell->RenderDocument(r, renderDocFlags, aBackgroundColor,
thebes);
thebes.get());
}

if (!recorder->mOutputStream.mValid) {
Expand Down
12 changes: 6 additions & 6 deletions gfx/layers/wr/WebRenderCommandBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ struct DIGroup {
RefPtr<gfx::DrawTarget> dt = gfx::Factory::CreateRecordingDrawTarget(
recorder, dummyDt, mLayerBounds.ToUnknownRect());
// Setup the gfxContext
RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> context = gfxContext::CreateOrNull(dt);
context->SetMatrix(Matrix::Scaling(mScale).PostTranslate(
mResidualOffset.x, mResidualOffset.y));

Expand All @@ -673,7 +673,7 @@ struct DIGroup {
return;
}

PaintItemRange(aGrouper, aStartItem, aEndItem, context, recorder,
PaintItemRange(aGrouper, aStartItem, aEndItem, context.get(), recorder,
rootManager, aResources);

// XXX: set this correctly perhaps using
Expand Down Expand Up @@ -2295,7 +2295,7 @@ static void PaintItemByDrawTarget(nsDisplayItem* aItem, gfx::DrawTarget* aDT,

// XXX Why is this ClearRect() needed?
aDT->ClearRect(Rect(visibleRect));
RefPtr<gfxContext> context = gfxContext::CreateOrNull(aDT);
UniquePtr<gfxContext> context = gfxContext::CreateOrNull(aDT);
MOZ_ASSERT(context);

switch (aItem->GetType()) {
Expand All @@ -2315,7 +2315,7 @@ static void PaintItemByDrawTarget(nsDisplayItem* aItem, gfx::DrawTarget* aDT,
if (aDisplayListBuilder->IsPaintingToWindow()) {
aItem->Frame()->AddStateBits(NS_FRAME_PAINTED_THEBES);
}
aItem->AsPaintedDisplayItem()->Paint(aDisplayListBuilder, context);
aItem->AsPaintedDisplayItem()->Paint(aDisplayListBuilder, context.get());
break;
}

Expand Down Expand Up @@ -2782,7 +2782,7 @@ Maybe<wr::ImageMask> WebRenderCommandBuilder::BuildWrMaskImage(
RefPtr<DrawTarget> dt = Factory::CreateRecordingDrawTarget(
recorder, dummyDt, IntRect(IntPoint(0, 0), size));

RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> context = gfxContext::CreateOrNull(dt);
MOZ_ASSERT(context);

context->SetMatrix(context->CurrentMatrix()
Expand All @@ -2791,7 +2791,7 @@ Maybe<wr::ImageMask> WebRenderCommandBuilder::BuildWrMaskImage(

bool maskPainted = false;
bool maskIsComplete = aMaskItem->PaintMask(
aDisplayListBuilder, context, shouldHandleOpacity, &maskPainted);
aDisplayListBuilder, context.get(), shouldHandleOpacity, &maskPainted);
if (!maskPainted) {
return Nothing();
}
Expand Down
2 changes: 1 addition & 1 deletion gfx/layers/wr/WebRenderLayerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class WebRenderLayerManager final : public WindowRenderer {
// we send a message to our remote side to capture the actual pixels
// being drawn to the default target, and then copy those pixels
// back to mTarget.
RefPtr<gfxContext> mTarget;
gfxContext* mTarget;

// See equivalent field in ClientLayerManager
uint32_t mPaintSequenceNumber;
Expand Down
11 changes: 5 additions & 6 deletions gfx/src/nsDeviceContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,16 +116,15 @@ void nsDeviceContext::Init(nsIWidget* aWidget) {
}

// XXX This is only for printing. We should make that obvious in the name.
already_AddRefed<gfxContext> nsDeviceContext::CreateRenderingContext() {
UniquePtr<gfxContext> nsDeviceContext::CreateRenderingContext() {
return CreateRenderingContextCommon(/* not a reference context */ false);
}

already_AddRefed<gfxContext>
nsDeviceContext::CreateReferenceRenderingContext() {
UniquePtr<gfxContext> nsDeviceContext::CreateReferenceRenderingContext() {
return CreateRenderingContextCommon(/* a reference context */ true);
}

already_AddRefed<gfxContext> nsDeviceContext::CreateRenderingContextCommon(
UniquePtr<gfxContext> nsDeviceContext::CreateRenderingContextCommon(
bool aWantReferenceContext) {
MOZ_ASSERT(IsPrinterContext());
MOZ_ASSERT(mWidth > 0 && mHeight > 0);
Expand Down Expand Up @@ -154,14 +153,14 @@ already_AddRefed<gfxContext> nsDeviceContext::CreateRenderingContextCommon(

dt->AddUserData(&sDisablePixelSnapping, (void*)0x1, nullptr);

RefPtr<gfxContext> pContext = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> pContext = gfxContext::CreateOrNull(dt);
MOZ_ASSERT(pContext); // already checked draw target above

gfxMatrix transform;
transform.PreTranslate(mPrintingTranslate);
transform.PreScale(mPrintingScale, mPrintingScale);
pContext->SetMatrixDouble(transform);
return pContext.forget();
return pContext;
}

uint32_t nsDeviceContext::GetDepth() {
Expand Down
6 changes: 3 additions & 3 deletions gfx/src/nsDeviceContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ class nsDeviceContext final {
*
* @return the new rendering context (guaranteed to be non-null)
*/
already_AddRefed<gfxContext> CreateRenderingContext();
mozilla::UniquePtr<gfxContext> CreateRenderingContext();

/**
* Create a reference rendering context and initialize it. Only call this
* method on device contexts that were initialized for printing.
*
* @return the new rendering context.
*/
already_AddRefed<gfxContext> CreateReferenceRenderingContext();
mozilla::UniquePtr<gfxContext> CreateReferenceRenderingContext();

/**
* Gets the number of app units in one device pixel; this number
Expand Down Expand Up @@ -270,7 +270,7 @@ class nsDeviceContext final {
* Implementation shared by CreateRenderingContext and
* CreateReferenceRenderingContext.
*/
already_AddRefed<gfxContext> CreateRenderingContextCommon(
mozilla::UniquePtr<gfxContext> CreateRenderingContextCommon(
bool aWantReferenceContext);

void SetDPI();
Expand Down
18 changes: 9 additions & 9 deletions gfx/thebes/gfxBlur.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ using namespace mozilla::gfx;

gfxAlphaBoxBlur::~gfxAlphaBoxBlur() = default;

already_AddRefed<gfxContext> gfxAlphaBoxBlur::Init(gfxContext* aDestinationCtx,
const gfxRect& aRect,
const IntSize& aSpreadRadius,
const IntSize& aBlurRadius,
const gfxRect* aDirtyRect,
const gfxRect* aSkipRect,
bool aUseHardwareAccel) {
UniquePtr<gfxContext> gfxAlphaBoxBlur::Init(gfxContext* aDestinationCtx,
const gfxRect& aRect,
const IntSize& aSpreadRadius,
const IntSize& aBlurRadius,
const gfxRect* aDirtyRect,
const gfxRect* aSkipRect,
bool aUseHardwareAccel) {
DrawTarget* refDT = aDestinationCtx->GetDrawTarget();
Maybe<Rect> dirtyRect = aDirtyRect ? Some(ToRect(*aDirtyRect)) : Nothing();
Maybe<Rect> skipRect = aSkipRect ? Some(ToRect(*aSkipRect)) : Nothing();
Expand All @@ -40,10 +40,10 @@ already_AddRefed<gfxContext> gfxAlphaBoxBlur::Init(gfxContext* aDestinationCtx,
return nullptr;
}

RefPtr<gfxContext> context = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> context = gfxContext::CreateOrNull(dt);
MOZ_ASSERT(context); // already checked for target above
context->SetMatrix(Matrix::Translation(-mBlur.GetRect().TopLeft()));
return context.forget();
return context;
}

already_AddRefed<DrawTarget> gfxAlphaBoxBlur::InitDrawTarget(
Expand Down
12 changes: 5 additions & 7 deletions gfx/thebes/gfxBlur.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,11 @@ class gfxAlphaBoxBlur final {
* @param aUseHardwareAccel Flag to state whether or not we can use hardware
* acceleration to speed up this blur.
*/
already_AddRefed<gfxContext> Init(gfxContext* aDestinationCtx,
const gfxRect& aRect,
const mozilla::gfx::IntSize& aSpreadRadius,
const mozilla::gfx::IntSize& aBlurRadius,
const gfxRect* aDirtyRect,
const gfxRect* aSkipRect,
bool aUseHardwareAccel = true);
mozilla::UniquePtr<gfxContext> Init(
gfxContext* aDestinationCtx, const gfxRect& aRect,
const mozilla::gfx::IntSize& aSpreadRadius,
const mozilla::gfx::IntSize& aBlurRadius, const gfxRect* aDirtyRect,
const gfxRect* aSkipRect, bool aUseHardwareAccel = true);

already_AddRefed<DrawTarget> InitDrawTarget(
const mozilla::gfx::DrawTarget* aReferenceDT,
Expand Down
14 changes: 7 additions & 7 deletions gfx/thebes/gfxContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,19 @@ gfxContext::gfxContext(DrawTarget* aTarget, const Point& aDeviceOffset)
}

/* static */
already_AddRefed<gfxContext> gfxContext::CreateOrNull(
UniquePtr<gfxContext> gfxContext::CreateOrNull(
DrawTarget* aTarget, const mozilla::gfx::Point& aDeviceOffset) {
if (!aTarget || !aTarget->IsValid()) {
gfxCriticalNote << "Invalid target in gfxContext::CreateOrNull "
<< hexa(aTarget);
return nullptr;
}

RefPtr<gfxContext> result = new gfxContext(aTarget, aDeviceOffset);
return result.forget();
return MakeUnique<gfxContext>(aTarget, aDeviceOffset);
}

/* static */
already_AddRefed<gfxContext> gfxContext::CreatePreservingTransformOrNull(
UniquePtr<gfxContext> gfxContext::CreatePreservingTransformOrNull(
DrawTarget* aTarget) {
if (!aTarget || !aTarget->IsValid()) {
gfxCriticalNote
Expand All @@ -84,10 +83,11 @@ already_AddRefed<gfxContext> gfxContext::CreatePreservingTransformOrNull(
return nullptr;
}

Matrix transform = aTarget->GetTransform();
RefPtr<gfxContext> result = new gfxContext(aTarget);
auto transform = aTarget->GetTransform();
auto result = MakeUnique<gfxContext>(aTarget);
result->SetMatrix(transform);
return result.forget();

return result;
}

gfxContext::~gfxContext() {
Expand Down
33 changes: 16 additions & 17 deletions gfx/thebes/gfxContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,28 @@ class gfxContext final {
typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
typedef mozilla::gfx::Size Size;

NS_INLINE_DECL_REFCOUNTING(gfxContext)

public:
/**
* Initialize this context from a DrawTarget.
* Strips any transform from aTarget.
* aTarget will be flushed in the gfxContext's destructor. Use the static
* ContextForDrawTargetNoTransform() when you want this behavior, as that
* version deals with null DrawTarget better.
*/
explicit gfxContext(
mozilla::gfx::DrawTarget* aTarget,
const mozilla::gfx::Point& aDeviceOffset = mozilla::gfx::Point());

~gfxContext();

/**
* Initialize this context from a DrawTarget.
* Strips any transform from aTarget.
* aTarget will be flushed in the gfxContext's destructor.
* If aTarget is null or invalid, nullptr is returned. The caller
* is responsible for handling this scenario as appropriate.
*/
static already_AddRefed<gfxContext> CreateOrNull(
static mozilla::UniquePtr<gfxContext> CreateOrNull(
mozilla::gfx::DrawTarget* aTarget,
const mozilla::gfx::Point& aDeviceOffset = mozilla::gfx::Point());

Expand All @@ -78,7 +89,7 @@ class gfxContext final {
* If aTarget is null or invalid, nullptr is returned. The caller
* is responsible for handling this scenario as appropriate.
*/
static already_AddRefed<gfxContext> CreatePreservingTransformOrNull(
static mozilla::UniquePtr<gfxContext> CreatePreservingTransformOrNull(
mozilla::gfx::DrawTarget* aTarget);

mozilla::gfx::DrawTarget* GetDrawTarget() { return mDT; }
Expand Down Expand Up @@ -438,18 +449,6 @@ class gfxContext final {
#endif

private:
/**
* Initialize this context from a DrawTarget.
* Strips any transform from aTarget.
* aTarget will be flushed in the gfxContext's destructor. Use the static
* ContextForDrawTargetNoTransform() when you want this behavior, as that
* version deals with null DrawTarget better.
*/
explicit gfxContext(
mozilla::gfx::DrawTarget* aTarget,
const mozilla::gfx::Point& aDeviceOffset = mozilla::gfx::Point());
~gfxContext();

friend class PatternFromState;
friend class GlyphBufferAzure;

Expand Down Expand Up @@ -543,7 +542,7 @@ class gfxContextAutoSaveRestore {
~gfxContextAutoSaveRestore() { Restore(); }

void SetContext(gfxContext* aContext) {
NS_ASSERTION(!mContext, "Not going to call Restore() on some context!!!");
MOZ_ASSERT(!mContext, "no context?");
mContext = aContext;
mContext->Save();
}
Expand Down
4 changes: 2 additions & 2 deletions gfx/thebes/gfxDrawable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ already_AddRefed<gfxSurfaceDrawable> gfxCallbackDrawable::MakeSurfaceDrawable(

if (!dt || !dt->IsValid()) return nullptr;

RefPtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
UniquePtr<gfxContext> ctx = gfxContext::CreateOrNull(dt);
MOZ_ASSERT(ctx); // already checked for target above
Draw(ctx, gfxRect(0, 0, mSize.width, mSize.height), ExtendMode::CLAMP,
Draw(ctx.get(), gfxRect(0, 0, mSize.width, mSize.height), ExtendMode::CLAMP,
aSamplingFilter);

RefPtr<SourceSurface> surface = dt->Snapshot();
Expand Down
Loading

0 comments on commit ffe28a6

Please sign in to comment.