Skip to content

Commit

Permalink
Bug 1720152 - Recurse into replay for dependencies, rather than using…
Browse files Browse the repository at this point in the history
… a temp surface. r=jrmuizel,bobowen

Differential Revision: https://phabricator.services.mozilla.com/D120050
  • Loading branch information
mattwoodrow committed Jul 29, 2021
1 parent 94e4d60 commit 63077e7
Show file tree
Hide file tree
Showing 15 changed files with 108 additions and 116 deletions.
5 changes: 1 addition & 4 deletions gfx/2d/2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -1157,10 +1157,7 @@ class DrawTarget : public external::AtomicRefCounted<DrawTarget> {
* This is considered fallible, and replaying this without making the surface
* available to the replay will just skip the draw.
*/
virtual void DrawDependentSurface(
uint64_t aId, const Rect& aDest,
const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
const DrawOptions& aOptions = DrawOptions()) {
virtual void DrawDependentSurface(uint64_t aId, const Rect& aDest) {
MOZ_CRASH("GFX: DrawDependentSurface");
}

Expand Down
8 changes: 3 additions & 5 deletions gfx/2d/DrawTargetRecording.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,10 @@ void DrawTargetRecording::DrawSurface(SourceSurface* aSurface,
aSurfOptions, aOptions));
}

void DrawTargetRecording::DrawDependentSurface(
uint64_t aId, const Rect& aDest, const DrawSurfaceOptions& aSurfOptions,
const DrawOptions& aOptions) {
void DrawTargetRecording::DrawDependentSurface(uint64_t aId,
const Rect& aDest) {
mRecorder->AddDependentSurface(aId);
mRecorder->RecordEvent(
RecordedDrawDependentSurface(this, aId, aDest, aSurfOptions, aOptions));
mRecorder->RecordEvent(RecordedDrawDependentSurface(this, aId, aDest));
}

void DrawTargetRecording::DrawSurfaceWithShadow(
Expand Down
5 changes: 1 addition & 4 deletions gfx/2d/DrawTargetRecording.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ class DrawTargetRecording : public DrawTarget {
const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
const DrawOptions& aOptions = DrawOptions()) override;

virtual void DrawDependentSurface(
uint64_t aId, const Rect& aDest,
const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
const DrawOptions& aOptions = DrawOptions()) override;
virtual void DrawDependentSurface(uint64_t aId, const Rect& aDest) override;

virtual void DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
const Point& aDestPoint,
Expand Down
8 changes: 3 additions & 5 deletions gfx/2d/DrawTargetWrapAndRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,10 @@ void DrawTargetWrapAndRecord::DrawSurface(
aSurfOptions, aOptions);
}

void DrawTargetWrapAndRecord::DrawDependentSurface(
uint64_t aId, const Rect& aDest, const DrawSurfaceOptions& aSurfOptions,
const DrawOptions& aOptions) {
void DrawTargetWrapAndRecord::DrawDependentSurface(uint64_t aId,
const Rect& aDest) {
mRecorder->AddDependentSurface(aId);
mRecorder->RecordEvent(
RecordedDrawDependentSurface(this, aId, aDest, aSurfOptions, aOptions));
mRecorder->RecordEvent(RecordedDrawDependentSurface(this, aId, aDest));
}

void DrawTargetWrapAndRecord::DrawSurfaceWithShadow(
Expand Down
5 changes: 1 addition & 4 deletions gfx/2d/DrawTargetWrapAndRecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ class DrawTargetWrapAndRecord : public DrawTarget {
const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
const DrawOptions& aOptions = DrawOptions()) override;

virtual void DrawDependentSurface(
uint64_t aId, const Rect& aDest,
const DrawSurfaceOptions& aSurfOptions = DrawSurfaceOptions(),
const DrawOptions& aOptions = DrawOptions()) override;
virtual void DrawDependentSurface(uint64_t aId, const Rect& aDest) override;

virtual void DrawFilter(FilterNode* aNode, const Rect& aSourceRect,
const Point& aDestPoint,
Expand Down
30 changes: 3 additions & 27 deletions gfx/2d/InlineTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,35 +111,11 @@ already_AddRefed<DrawTarget> InlineTranslator::CreateDrawTarget(

already_AddRefed<SourceSurface> InlineTranslator::LookupExternalSurface(
uint64_t aKey) {
if (mExternalSurfaces) {
RefPtr<SourceSurface> surface = mExternalSurfaces->Get(aKey);
if (surface) {
return surface.forget();
}
}

if (!mDependentSurfaces) {
return nullptr;
}

RefPtr<RecordedDependentSurface> recordedSurface =
mDependentSurfaces->Get(aKey);
if (!recordedSurface) {
if (!mExternalSurfaces) {
return nullptr;
}

RefPtr<DrawTarget> newDT = GetReferenceDrawTarget()->CreateSimilarDrawTarget(
recordedSurface->mSize, SurfaceFormat::B8G8R8A8);

InlineTranslator translator(newDT, nullptr);
translator.SetDependentSurfaces(mDependentSurfaces);
if (!translator.TranslateRecording((char*)recordedSurface->mRecording.mData,
recordedSurface->mRecording.mLen)) {
return nullptr;
}

RefPtr<SourceSurface> snapshot = newDT->Snapshot();
return snapshot.forget();
RefPtr<SourceSurface> surface = mExternalSurfaces->Get(aKey);
return surface.forget();
}

} // namespace mozilla::gfx
11 changes: 4 additions & 7 deletions gfx/2d/InlineTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Filters.h"
#include "mozilla/gfx/RecordedEvent.h"
#include "nsRefPtrHashtable.h"

namespace mozilla {
namespace gfx {
Expand All @@ -40,10 +39,8 @@ class InlineTranslator : public Translator {
nsRefPtrHashtable<nsUint64HashKey, SourceSurface>* aExternalSurfaces) {
mExternalSurfaces = aExternalSurfaces;
}
void SetDependentSurfaces(
nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>*
aDependentSurfaces) {
mDependentSurfaces = aDependentSurfaces;
void SetReferenceDrawTargetTransform(const Matrix& aTransform) {
mBaseDTTransform = aTransform;
}

DrawTarget* LookupDrawTarget(ReferencePtr aRefPtr) final {
Expand Down Expand Up @@ -163,12 +160,14 @@ class InlineTranslator : public Translator {
MOZ_ASSERT(mBaseDT, "mBaseDT has not been initialized.");
return mBaseDT;
}
Matrix GetReferenceDrawTargetTransform() final { return mBaseDTTransform; }

void* GetFontContext() final { return mFontContext; }
std::string GetError() { return mError; }

protected:
RefPtr<DrawTarget> mBaseDT;
Matrix mBaseDTTransform;
nsRefPtrHashtable<nsPtrHashKey<void>, DrawTarget> mDrawTargets;

private:
Expand All @@ -184,8 +183,6 @@ class InlineTranslator : public Translator {
nsRefPtrHashtable<nsUint64HashKey, NativeFontResource> mNativeFontResources;
nsRefPtrHashtable<nsUint64HashKey, SourceSurface>* mExternalSurfaces =
nullptr;
nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>*
mDependentSurfaces = nullptr;
};

} // namespace gfx
Expand Down
35 changes: 35 additions & 0 deletions gfx/2d/RecordedEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Logging.h"
#include "ScaledFontBase.h"
#include "SFNTData.h"
#include "InlineTranslator.h"

namespace mozilla {
namespace gfx {
Expand Down Expand Up @@ -168,5 +169,39 @@ already_AddRefed<DrawTarget> Translator::CreateDrawTarget(
return newDT.forget();
}

void Translator::DrawDependentSurface(ReferencePtr aDrawTarget, uint64_t aKey,
const Rect& aRect) {
if (!mDependentSurfaces) {
return;
}

DrawTarget* dt = LookupDrawTarget(aDrawTarget);
if (!dt) {
return;
}

RefPtr<RecordedDependentSurface> recordedSurface =
mDependentSurfaces->Get(aKey);
if (!recordedSurface) {
return;
}

dt->PushClipRect(aRect);

// Construct a new translator, so we can recurse into translating this
// sub-recording into the same DT. Set an initial transform for the
// translator, so that all commands get moved into the rect we want to draw.
Matrix transform = dt->GetTransform();
transform.PreTranslate(aRect.TopLeft());
InlineTranslator translator(dt, nullptr);
translator.SetReferenceDrawTargetTransform(transform);

translator.SetDependentSurfaces(mDependentSurfaces);
translator.TranslateRecording((char*)recordedSurface->mRecording.mData,
recordedSurface->mRecording.mLen);

dt->PopClip();
}

} // namespace gfx
} // namespace mozilla
13 changes: 13 additions & 0 deletions gfx/2d/RecordedEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mozilla/gfx/Point.h"
#include "mozilla/gfx/Types.h"
#include "mozilla/ipc/ByteBuf.h"
#include "nsRefPtrHashtable.h"

namespace mozilla {
namespace gfx {
Expand Down Expand Up @@ -101,6 +102,8 @@ class Translator {
virtual already_AddRefed<SourceSurface> LookupExternalSurface(uint64_t aKey) {
return nullptr;
}
void DrawDependentSurface(ReferencePtr aDrawTarget, uint64_t aKey,
const Rect& aRect);
virtual void AddDrawTarget(ReferencePtr aRefPtr, DrawTarget* aDT) = 0;
virtual void RemoveDrawTarget(ReferencePtr aRefPtr) = 0;
virtual void AddPath(ReferencePtr aRefPtr, Path* aPath) = 0;
Expand Down Expand Up @@ -137,7 +140,17 @@ class Translator {
const IntSize& aSize,
SurfaceFormat aFormat);
virtual DrawTarget* GetReferenceDrawTarget() = 0;
virtual Matrix GetReferenceDrawTargetTransform() { return Matrix(); }
virtual void* GetFontContext() { return nullptr; }

void SetDependentSurfaces(
nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>*
aDependentSurfaces) {
mDependentSurfaces = aDependentSurfaces;
}

nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>*
mDependentSurfaces = nullptr;
};

struct ColorPatternStorage {
Expand Down
40 changes: 13 additions & 27 deletions gfx/2d/RecordedEventImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -706,14 +706,10 @@ class RecordedDrawSurface : public RecordedDrawingEvent<RecordedDrawSurface> {
class RecordedDrawDependentSurface
: public RecordedDrawingEvent<RecordedDrawDependentSurface> {
public:
RecordedDrawDependentSurface(DrawTarget* aDT, uint64_t aId, const Rect& aDest,
const DrawSurfaceOptions& aDSOptions,
const DrawOptions& aOptions)
RecordedDrawDependentSurface(DrawTarget* aDT, uint64_t aId, const Rect& aDest)
: RecordedDrawingEvent(DRAWDEPENDENTSURFACE, aDT),
mId(aId),
mDest(aDest),
mDSOptions(aDSOptions),
mOptions(aOptions) {}
mDest(aDest) {}

bool PlayEvent(Translator* aTranslator) const override;

Expand All @@ -731,8 +727,6 @@ class RecordedDrawDependentSurface

uint64_t mId;
Rect mDest;
DrawSurfaceOptions mDSOptions;
DrawOptions mOptions;
};

class RecordedDrawSurfaceWithShadow
Expand Down Expand Up @@ -2756,7 +2750,16 @@ inline bool RecordedSetTransform::PlayEvent(Translator* aTranslator) const {
return false;
}

dt->SetTransform(mTransform);
// If we're drawing to the reference DT, then we need to manually apply
// its initial transform, otherwise we'll just clobber it with only the
// the transform that was visible to the code doing the recording.
if (dt == aTranslator->GetReferenceDrawTarget()) {
dt->SetTransform(mTransform *
aTranslator->GetReferenceDrawTargetTransform());
} else {
dt->SetTransform(mTransform);
}

return true;
}

Expand Down Expand Up @@ -2822,20 +2825,7 @@ inline void RecordedDrawSurface::OutputSimpleEventInfo(

inline bool RecordedDrawDependentSurface::PlayEvent(
Translator* aTranslator) const {
DrawTarget* dt = aTranslator->LookupDrawTarget(mDT);
if (!dt) {
return false;
}

// We still return true even if this fails, since dependent surfaces are
// used for cross-origin iframe drawing and can fail.
RefPtr<SourceSurface> surface = aTranslator->LookupExternalSurface(mId);
if (!surface) {
return true;
}

dt->DrawSurface(surface, mDest, Rect(Point(), Size(surface->GetSize())),
mDSOptions, mOptions);
aTranslator->DrawDependentSurface(mDT, mId, mDest);
return true;
}

Expand All @@ -2844,17 +2834,13 @@ void RecordedDrawDependentSurface::Record(S& aStream) const {
RecordedDrawingEvent::Record(aStream);
WriteElement(aStream, mId);
WriteElement(aStream, mDest);
WriteElement(aStream, mDSOptions);
WriteElement(aStream, mOptions);
}

template <class S>
RecordedDrawDependentSurface::RecordedDrawDependentSurface(S& aStream)
: RecordedDrawingEvent(DRAWDEPENDENTSURFACE, aStream) {
ReadElement(aStream, mId);
ReadElement(aStream, mDest);
ReadDrawSurfaceOptions(aStream, mDSOptions);
ReadDrawOptions(aStream, mOptions);
}

inline void RecordedDrawDependentSurface::OutputSimpleEventInfo(
Expand Down
21 changes: 0 additions & 21 deletions layout/printing/PrintTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,26 +82,5 @@ already_AddRefed<DrawTarget> PrintTranslator::CreateDrawTarget(
return drawTarget.forget();
}

already_AddRefed<SourceSurface> PrintTranslator::LookupExternalSurface(
uint64_t aKey) {
RefPtr<RecordedDependentSurface> surface = mDependentSurfaces.Get(aKey);
if (!surface) {
return nullptr;
}

RefPtr<DrawTarget> newDT = GetReferenceDrawTarget()->CreateSimilarDrawTarget(
surface->mSize, SurfaceFormat::B8G8R8A8);

InlineTranslator translator(newDT, nullptr);
translator.SetDependentSurfaces(&mDependentSurfaces);
if (!translator.TranslateRecording((char*)surface->mRecording.mData,
surface->mRecording.mLen)) {
return nullptr;
}

RefPtr<SourceSurface> snapshot = newDT->Snapshot();
return snapshot.forget();
}

} // namespace layout
} // namespace mozilla
11 changes: 0 additions & 11 deletions layout/printing/PrintTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/Filters.h"
#include "mozilla/gfx/RecordedEvent.h"
#include "nsRefPtrHashtable.h"

class nsDeviceContext;

Expand All @@ -38,12 +37,6 @@ class PrintTranslator final : public Translator {

bool TranslateRecording(PRFileDescStream& aRecording);

void SetDependentSurfaces(
nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>&&
aDependentSurfaces) {
mDependentSurfaces = std::move(aDependentSurfaces);
}

DrawTarget* LookupDrawTarget(ReferencePtr aRefPtr) final {
DrawTarget* result = mDrawTargets.GetWeak(aRefPtr);
MOZ_ASSERT(result);
Expand Down Expand Up @@ -91,8 +84,6 @@ class PrintTranslator final : public Translator {
return result;
}

already_AddRefed<SourceSurface> LookupExternalSurface(uint64_t aKey) final;

void AddDrawTarget(ReferencePtr aRefPtr, DrawTarget* aDT) final {
mDrawTargets.InsertOrUpdate(aRefPtr, RefPtr{aDT});
}
Expand Down Expand Up @@ -171,8 +162,6 @@ class PrintTranslator final : public Translator {
nsRefPtrHashtable<nsPtrHashKey<void>, ScaledFont> mScaledFonts;
nsRefPtrHashtable<nsPtrHashKey<void>, UnscaledFont> mUnscaledFonts;
nsRefPtrHashtable<nsUint64HashKey, NativeFontResource> mNativeFontResources;
nsRefPtrHashtable<nsUint64HashKey, RecordedDependentSurface>
mDependentSurfaces;
};

} // namespace layout
Expand Down
4 changes: 3 additions & 1 deletion layout/printing/ipc/RemotePrintJobParent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,13 @@ nsresult RemotePrintJobParent::PrintPage(
return rv;
}
if (aFragments) {
mPrintTranslator->SetDependentSurfaces(std::move(*aFragments));
mPrintTranslator->SetDependentSurfaces(aFragments);
}
if (!mPrintTranslator->TranslateRecording(aRecording)) {
mPrintTranslator->SetDependentSurfaces(nullptr);
return NS_ERROR_FAILURE;
}
mPrintTranslator->SetDependentSurfaces(nullptr);

rv = mPrintDeviceContext->EndPage();
if (NS_WARN_IF(NS_FAILED(rv))) {
Expand Down
Loading

0 comments on commit 63077e7

Please sign in to comment.