Bug 647560. Cache temporary backbuffer surfaces. r=karlt

This commit is contained in:
Robert O'Callahan 2011-05-30 15:48:29 +12:00
parent 89fdba6cb9
commit a537a15852
4 changed files with 91 additions and 66 deletions

View File

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

View File

@ -180,6 +180,13 @@ public:
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 bool HasShadowManagerInternal() const { return false; }
bool HasShadowManager() const { return HasShadowManagerInternal(); }
@ -193,7 +200,8 @@ protected:
#endif
// Paints aLayer to mTarget.
void PaintLayer(Layer* aLayer,
void PaintLayer(gfxContext* aTarget,
Layer* aLayer,
DrawThebesLayerCallback aCallback,
void* aCallbackData,
ReadbackProcessor* aReadback);
@ -201,12 +209,6 @@ protected:
// Clear the contents of a layer
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,
void* aCallbackData);
@ -227,6 +229,7 @@ protected:
BufferMode mDoubleBuffering;
PRPackedBool mUsingDefaultTarget;
PRPackedBool mCachedSurfaceInUse;
bool mTransactionIncomplete;
};

View File

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

View File

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