Bug 647560. Cache temporary backbuffer surfaces. r=karlt

This commit is contained in:
Robert O'Callahan
2011-05-18 23:24:52 +12:00
parent 57c00a6f42
commit 93c969e997
4 changed files with 92 additions and 67 deletions

View File

@@ -583,15 +583,16 @@ SetAntialiasingFlags(Layer* aLayer, gfxContext* aTarget)
aTarget->UserToDevice(gfxRect(bounds.x, bounds.y, bounds.width, bounds.height)))); aTarget->UserToDevice(gfxRect(bounds.x, bounds.y, bounds.width, bounds.height))));
} }
static PRBool already_AddRefed<gfxContext>
PushGroupForLayer(gfxContext* aContext, Layer* aLayer, const nsIntRegion& aRegion) BasicLayerManager::PushGroupForLayer(gfxContext* aContext, Layer* aLayer,
const nsIntRegion& aRegion,
PRBool* aNeedsClipToVisibleRegion)
{ {
// If we need to call PushGroup, we should clip to the smallest possible // If we need to call PushGroup, we should clip to the smallest possible
// area first to minimize the size of the temporary surface. // area first to minimize the size of the temporary surface.
PRBool didCompleteClip = ClipToContain(aContext, aRegion.GetBounds()); PRBool didCompleteClip = ClipToContain(aContext, aRegion.GetBounds());
gfxASurface::gfxContentType contentType = gfxASurface::CONTENT_COLOR_ALPHA; nsRefPtr<gfxContext> result;
PRBool needsClipToVisibleRegion = PR_FALSE;
if (aLayer->CanUseOpaqueSurface() && if (aLayer->CanUseOpaqueSurface() &&
((didCompleteClip && aRegion.GetNumRects() == 1) || ((didCompleteClip && aRegion.GetNumRects() == 1) ||
!aContext->CurrentMatrix().HasNonIntegerTranslation())) { !aContext->CurrentMatrix().HasNonIntegerTranslation())) {
@@ -599,11 +600,14 @@ PushGroupForLayer(gfxContext* aContext, Layer* aLayer, const nsIntRegion& aRegio
// group. We need to make sure that only pixels inside the layer's visible // group. We need to make sure that only pixels inside the layer's visible
// region are copied back to the destination. Remember if we've already // region are copied back to the destination. Remember if we've already
// clipped precisely to the visible region. // clipped precisely to the visible region.
needsClipToVisibleRegion = !didCompleteClip || aRegion.GetNumRects() > 1; *aNeedsClipToVisibleRegion = !didCompleteClip || aRegion.GetNumRects() > 1;
contentType = gfxASurface::CONTENT_COLOR; result = PushGroupWithCachedSurface(aContext, gfxASurface::CONTENT_COLOR);
} else {
*aNeedsClipToVisibleRegion = PR_FALSE;
result = aContext;
aContext->PushGroupAndCopyBackground(gfxASurface::CONTENT_COLOR_ALPHA);
} }
aContext->PushGroupAndCopyBackground(contentType); return result.forget();
return needsClipToVisibleRegion;
} }
void void
@@ -648,14 +652,21 @@ BasicThebesLayer::PaintThebes(gfxContext* aContext,
PRBool needsClipToVisibleRegion = PR_FALSE; PRBool needsClipToVisibleRegion = PR_FALSE;
PRBool needsGroup = PRBool needsGroup =
opacity != 1.0 || GetOperator() != gfxContext::OPERATOR_OVER; opacity != 1.0 || GetOperator() != gfxContext::OPERATOR_OVER;
nsRefPtr<gfxContext> groupContext;
if (needsGroup) { if (needsGroup) {
needsClipToVisibleRegion = PushGroupForLayer(aContext, this, toDraw) || groupContext =
GetOperator() != gfxContext::OPERATOR_OVER; BasicManager()->PushGroupForLayer(aContext, this, toDraw,
&needsClipToVisibleRegion);
if (GetOperator() != gfxContext::OPERATOR_OVER) {
needsClipToVisibleRegion = PR_TRUE;
} }
SetAntialiasingFlags(this, aContext); } else {
aCallback(this, aContext, toDraw, nsIntRegion(), aCallbackData); groupContext = aContext;
}
SetAntialiasingFlags(this, groupContext);
aCallback(this, groupContext, toDraw, nsIntRegion(), aCallbackData);
if (needsGroup) { if (needsGroup) {
aContext->PopGroupToSource(); BasicManager()->PopGroupToSourceWithCachedSurface(aContext, groupContext);
if (needsClipToVisibleRegion) { if (needsClipToVisibleRegion) {
gfxUtils::ClipToRegion(aContext, toDraw); gfxUtils::ClipToRegion(aContext, toDraw);
} }
@@ -1244,6 +1255,7 @@ BasicLayerManager::BasicLayerManager(nsIWidget* aWidget) :
, mYResolution(1.0) , mYResolution(1.0)
, mWidget(aWidget) , mWidget(aWidget)
, mDoubleBuffering(BUFFER_NONE), mUsingDefaultTarget(PR_FALSE) , mDoubleBuffering(BUFFER_NONE), mUsingDefaultTarget(PR_FALSE)
, mCachedSurfaceInUse(PR_FALSE)
, mTransactionIncomplete(false) , mTransactionIncomplete(false)
{ {
MOZ_COUNT_CTOR(BasicLayerManager); MOZ_COUNT_CTOR(BasicLayerManager);
@@ -1290,9 +1302,15 @@ BasicLayerManager::BeginTransaction()
already_AddRefed<gfxContext> already_AddRefed<gfxContext>
BasicLayerManager::PushGroupWithCachedSurface(gfxContext *aTarget, BasicLayerManager::PushGroupWithCachedSurface(gfxContext *aTarget,
gfxASurface::gfxContentType aContent, gfxASurface::gfxContentType aContent)
gfxPoint *aSavedOffset)
{ {
if (mCachedSurfaceInUse) {
aTarget->PushGroup(aContent);
nsRefPtr<gfxContext> result = aTarget;
return result.forget();
}
mCachedSurfaceInUse = PR_TRUE;
gfxContextMatrixAutoSaveRestore saveMatrix(aTarget); gfxContextMatrixAutoSaveRestore saveMatrix(aTarget);
aTarget->IdentityMatrix(); aTarget->IdentityMatrix();
@@ -1300,29 +1318,26 @@ BasicLayerManager::PushGroupWithCachedSurface(gfxContext *aTarget,
gfxRect clip = aTarget->GetClipExtents(); gfxRect clip = aTarget->GetClipExtents();
clip.RoundOut(); clip.RoundOut();
nsRefPtr<gfxContext> ctx = nsRefPtr<gfxContext> ctx = mCachedSurface.Get(aContent, clip, currentSurf);
mCachedSurface.Get(aContent,
gfxIntSize(clip.Width(), clip.Height()),
currentSurf);
/* Align our buffer for the original surface */ /* Align our buffer for the original surface */
ctx->Translate(-clip.TopLeft()); ctx->SetMatrix(saveMatrix.Matrix());
*aSavedOffset = clip.TopLeft();
ctx->Multiply(saveMatrix.Matrix());
return ctx.forget(); return ctx.forget();
} }
void void
BasicLayerManager::PopGroupWithCachedSurface(gfxContext *aTarget, BasicLayerManager::PopGroupToSourceWithCachedSurface(gfxContext *aTarget, gfxContext *aPushed)
const gfxPoint& aSavedOffset)
{ {
if (!mTarget) if (!aTarget)
return; return;
nsRefPtr<gfxASurface> current = aPushed->CurrentSurface();
if (mCachedSurface.IsSurface(current)) {
gfxContextMatrixAutoSaveRestore saveMatrix(aTarget); gfxContextMatrixAutoSaveRestore saveMatrix(aTarget);
aTarget->IdentityMatrix(); aTarget->IdentityMatrix();
aTarget->SetSource(current);
aTarget->SetSource(mTarget->OriginalSurface(), aSavedOffset); mCachedSurfaceInUse = PR_FALSE;
aTarget->Paint(); } else {
aTarget->PopGroupToSource();
}
} }
void void
@@ -1551,11 +1566,11 @@ BasicLayerManager::EndTransactionInternal(DrawThebesLayerCallback aCallback,
nsIntRegion region; nsIntRegion region;
MarkLayersHidden(mRoot, clipRect, clipRect, region, ALLOW_OPAQUE); MarkLayersHidden(mRoot, clipRect, clipRect, region, ALLOW_OPAQUE);
if (mUsingDefaultTarget && mDoubleBuffering != BUFFER_NONE) { if (mUsingDefaultTarget && mDoubleBuffering != BUFFER_NONE) {
ApplyDoubleBuffering(mRoot, deviceSpaceClipExtents); ApplyDoubleBuffering(mRoot, clipRect);
} }
} }
PaintLayer(mRoot, aCallback, aCallbackData, nsnull); PaintLayer(mTarget, mRoot, aCallback, aCallbackData, nsnull);
if (!mTransactionIncomplete) { if (!mTransactionIncomplete) {
// Clear out target if we have a complete transaction. // Clear out target if we have a complete transaction.
@@ -1608,7 +1623,8 @@ BasicLayerManager::SetRoot(Layer* aLayer)
} }
void void
BasicLayerManager::PaintLayer(Layer* aLayer, BasicLayerManager::PaintLayer(gfxContext* aTarget,
Layer* aLayer,
DrawThebesLayerCallback aCallback, DrawThebesLayerCallback aCallback,
void* aCallbackData, void* aCallbackData,
ReadbackProcessor* aReadback) ReadbackProcessor* aReadback)
@@ -1628,15 +1644,15 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
gfxMatrix savedMatrix; gfxMatrix savedMatrix;
if (needsSaveRestore) { if (needsSaveRestore) {
mTarget->Save(); aTarget->Save();
if (clipRect) { if (clipRect) {
mTarget->NewPath(); aTarget->NewPath();
mTarget->Rectangle(gfxRect(clipRect->x, clipRect->y, clipRect->width, clipRect->height), PR_TRUE); aTarget->Rectangle(gfxRect(clipRect->x, clipRect->y, clipRect->width, clipRect->height), PR_TRUE);
mTarget->Clip(); aTarget->Clip();
} }
} else { } else {
savedMatrix = mTarget->CurrentMatrix(); savedMatrix = aTarget->CurrentMatrix();
} }
gfxMatrix transform; gfxMatrix transform;
@@ -1645,11 +1661,11 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
NS_ASSERTION(effectiveTransform.Is2D(), NS_ASSERTION(effectiveTransform.Is2D(),
"Only 2D transforms supported currently"); "Only 2D transforms supported currently");
effectiveTransform.Is2D(&transform); effectiveTransform.Is2D(&transform);
mTarget->SetMatrix(transform); aTarget->SetMatrix(transform);
PRBool pushedTargetOpaqueRect = PR_FALSE; PRBool pushedTargetOpaqueRect = PR_FALSE;
const nsIntRegion& visibleRegion = aLayer->GetEffectiveVisibleRegion(); const nsIntRegion& visibleRegion = aLayer->GetEffectiveVisibleRegion();
nsRefPtr<gfxASurface> currentSurface = mTarget->CurrentSurface(); nsRefPtr<gfxASurface> currentSurface = aTarget->CurrentSurface();
const gfxRect& targetOpaqueRect = currentSurface->GetOpaqueRect(); const gfxRect& targetOpaqueRect = currentSurface->GetOpaqueRect();
// Try to annotate currentSurface with a region of pixels that have been // Try to annotate currentSurface with a region of pixels that have been
@@ -1659,14 +1675,17 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
!transform.HasNonAxisAlignedTransform()) { !transform.HasNonAxisAlignedTransform()) {
const nsIntRect& bounds = visibleRegion.GetBounds(); const nsIntRect& bounds = visibleRegion.GetBounds();
currentSurface->SetOpaqueRect( currentSurface->SetOpaqueRect(
mTarget->UserToDevice(gfxRect(bounds.x, bounds.y, bounds.width, bounds.height))); aTarget->UserToDevice(gfxRect(bounds.x, bounds.y, bounds.width, bounds.height)));
pushedTargetOpaqueRect = PR_TRUE; pushedTargetOpaqueRect = PR_TRUE;
} }
PRBool needsClipToVisibleRegion = PR_FALSE; PRBool needsClipToVisibleRegion = PR_FALSE;
nsRefPtr<gfxContext> groupTarget;
if (needsGroup) { if (needsGroup) {
needsClipToVisibleRegion = groupTarget = PushGroupForLayer(aTarget, aLayer, aLayer->GetEffectiveVisibleRegion(),
PushGroupForLayer(mTarget, aLayer, aLayer->GetEffectiveVisibleRegion()); &needsClipToVisibleRegion);
} else {
groupTarget = aTarget;
} }
/* Only paint ourself, or our children - This optimization relies on this! */ /* Only paint ourself, or our children - This optimization relies on this! */
@@ -1678,9 +1697,9 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
(void*)aLayer, data->IsHidden())); (void*)aLayer, data->IsHidden()));
#endif #endif
if (aLayer->AsThebesLayer()) { if (aLayer->AsThebesLayer()) {
data->PaintThebes(mTarget, aCallback, aCallbackData, aReadback); data->PaintThebes(groupTarget, aCallback, aCallbackData, aReadback);
} else { } else {
data->Paint(mTarget); data->Paint(groupTarget);
} }
} else { } else {
ReadbackProcessor readback; ReadbackProcessor readback;
@@ -1690,29 +1709,29 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
} }
for (; child; child = child->GetNextSibling()) { for (; child; child = child->GetNextSibling()) {
PaintLayer(child, aCallback, aCallbackData, &readback); PaintLayer(groupTarget, child, aCallback, aCallbackData, &readback);
if (mTransactionIncomplete) if (mTransactionIncomplete)
break; break;
} }
} }
if (needsGroup) { if (needsGroup) {
mTarget->PopGroupToSource(); PopGroupToSourceWithCachedSurface(aTarget, groupTarget);
// If we're doing our own double-buffering, we need to avoid drawing // If we're doing our own double-buffering, we need to avoid drawing
// the results of an incomplete transaction to the destination surface --- // the results of an incomplete transaction to the destination surface ---
// that could cause flicker. Double-buffering is implemented using a // that could cause flicker. Double-buffering is implemented using a
// temporary surface for one or more container layers, so we need to stop // temporary surface for one or more container layers, so we need to stop
// those temporary surfaces from being composited to mTarget. // those temporary surfaces from being composited to aTarget.
// ApplyDoubleBuffering guarantees that this container layer can't // ApplyDoubleBuffering guarantees that this container layer can't
// intersect any other leaf layers, so if the transaction is not yet marked // intersect any other leaf layers, so if the transaction is not yet marked
// incomplete, the contents of this container layer are the final contents // incomplete, the contents of this container layer are the final contents
// for the window. // for the window.
if (!mTransactionIncomplete) { if (!mTransactionIncomplete) {
if (needsClipToVisibleRegion) { if (needsClipToVisibleRegion) {
gfxUtils::ClipToRegion(mTarget, aLayer->GetEffectiveVisibleRegion()); gfxUtils::ClipToRegion(aTarget, aLayer->GetEffectiveVisibleRegion());
} }
AutoSetOperator setOperator(mTarget, container->GetOperator()); AutoSetOperator setOperator(aTarget, container->GetOperator());
mTarget->Paint(aLayer->GetEffectiveOpacity()); aTarget->Paint(aLayer->GetEffectiveOpacity());
} }
} }
@@ -1721,9 +1740,9 @@ BasicLayerManager::PaintLayer(Layer* aLayer,
} }
if (needsSaveRestore) { if (needsSaveRestore) {
mTarget->Restore(); aTarget->Restore();
} else { } else {
mTarget->SetMatrix(savedMatrix); aTarget->SetMatrix(savedMatrix);
} }
} }

View File

@@ -180,6 +180,13 @@ public:
void SetTransactionIncomplete() { mTransactionIncomplete = true; } void SetTransactionIncomplete() { mTransactionIncomplete = true; }
already_AddRefed<gfxContext> PushGroupForLayer(gfxContext* aContext, Layer* aLayer,
const nsIntRegion& aRegion,
PRBool* aNeedsClipToVisibleRegion);
already_AddRefed<gfxContext> PushGroupWithCachedSurface(gfxContext *aTarget,
gfxASurface::gfxContentType aContent);
void PopGroupToSourceWithCachedSurface(gfxContext *aTarget, gfxContext *aPushed);
virtual PRBool IsCompositingCheap() { return PR_FALSE; } virtual PRBool IsCompositingCheap() { return PR_FALSE; }
virtual bool HasShadowManagerInternal() const { return false; } virtual bool HasShadowManagerInternal() const { return false; }
bool HasShadowManager() const { return HasShadowManagerInternal(); } bool HasShadowManager() const { return HasShadowManagerInternal(); }
@@ -193,7 +200,8 @@ protected:
#endif #endif
// Paints aLayer to mTarget. // Paints aLayer to mTarget.
void PaintLayer(Layer* aLayer, void PaintLayer(gfxContext* aTarget,
Layer* aLayer,
DrawThebesLayerCallback aCallback, DrawThebesLayerCallback aCallback,
void* aCallbackData, void* aCallbackData,
ReadbackProcessor* aReadback); ReadbackProcessor* aReadback);
@@ -201,12 +209,6 @@ protected:
// Clear the contents of a layer // Clear the contents of a layer
void ClearLayer(Layer* aLayer); void ClearLayer(Layer* aLayer);
already_AddRefed<gfxContext> PushGroupWithCachedSurface(gfxContext *aTarget,
gfxASurface::gfxContentType aContent,
gfxPoint *aSavedOffset);
void PopGroupWithCachedSurface(gfxContext *aTarget,
const gfxPoint& aSavedOffset);
bool EndTransactionInternal(DrawThebesLayerCallback aCallback, bool EndTransactionInternal(DrawThebesLayerCallback aCallback,
void* aCallbackData); void* aCallbackData);
@@ -227,6 +229,7 @@ protected:
BufferMode mDoubleBuffering; BufferMode mDoubleBuffering;
PRPackedBool mUsingDefaultTarget; PRPackedBool mUsingDefaultTarget;
PRPackedBool mCachedSurfaceInUse;
bool mTransactionIncomplete; bool mTransactionIncomplete;
}; };

View File

@@ -98,12 +98,12 @@ gfxCachedTempSurface::~gfxCachedTempSurface()
already_AddRefed<gfxContext> already_AddRefed<gfxContext>
gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType, gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType,
const gfxIntSize& aSize, const gfxRect& aRect,
gfxASurface* aSimilarTo) gfxASurface* aSimilarTo)
{ {
if (mSurface) { if (mSurface) {
/* Verify the current buffer is valid for this purpose */ /* Verify the current buffer is valid for this purpose */
if (mSize.width < aSize.width || mSize.height < aSize.height if (mSize.width < aRect.width || mSize.height < aRect.height
|| mSurface->GetContentType() != aContentType) { || mSurface->GetContentType() != aContentType) {
mSurface = nsnull; mSurface = nsnull;
} else { } else {
@@ -114,8 +114,8 @@ gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType,
PRBool cleared = PR_FALSE; PRBool cleared = PR_FALSE;
if (!mSurface) { if (!mSurface) {
mSize = aSize; mSize = gfxIntSize(PRInt32(NS_ceil(aRect.width)), PRInt32(NS_ceil(aRect.height)));
mSurface = aSimilarTo->CreateSimilarSurface(aContentType, aSize); mSurface = aSimilarTo->CreateSimilarSurface(aContentType, mSize);
if (!mSurface) if (!mSurface)
return nsnull; return nsnull;
@@ -124,9 +124,10 @@ gfxCachedTempSurface::Get(gfxASurface::gfxContentType aContentType,
mType = aSimilarTo->GetType(); mType = aSimilarTo->GetType();
#endif #endif
} }
mSurface->SetDeviceOffset(-aRect.TopLeft());
nsRefPtr<gfxContext> ctx = new gfxContext(mSurface); nsRefPtr<gfxContext> ctx = new gfxContext(mSurface);
ctx->Rectangle(gfxRect(0, 0, aSize.width, aSize.height)); ctx->Rectangle(aRect);
ctx->Clip(); ctx->Clip();
if (!cleared && aContentType != gfxASurface::CONTENT_COLOR) { if (!cleared && aContentType != gfxASurface::CONTENT_COLOR) {
ctx->SetOperator(gfxContext::OPERATOR_CLEAR); ctx->SetOperator(gfxContext::OPERATOR_CLEAR);

View File

@@ -71,13 +71,15 @@ public:
* different format. * different format.
*/ */
already_AddRefed<gfxContext> Get(gfxASurface::gfxContentType aContentType, already_AddRefed<gfxContext> Get(gfxASurface::gfxContentType aContentType,
const gfxIntSize& aSize, const gfxRect& aRect,
gfxASurface* aSimilarTo); gfxASurface* aSimilarTo);
void Expire() { mSurface = nsnull; } void Expire() { mSurface = nsnull; }
nsExpirationState* GetExpirationState() { return &mExpirationState; } nsExpirationState* GetExpirationState() { return &mExpirationState; }
~gfxCachedTempSurface(); ~gfxCachedTempSurface();
PRBool IsSurface(gfxASurface* aSurface) { return mSurface == aSurface; }
private: private:
nsRefPtr<gfxASurface> mSurface; nsRefPtr<gfxASurface> mSurface;
gfxIntSize mSize; gfxIntSize mSize;