2010-07-21 11:06:33 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2010-03-03 13:37:04 -08:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Corporation code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2010
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Robert O'Callahan <robert@ocallahan.org>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "ThebesLayerBuffer.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
2010-09-02 19:31:42 -07:00
|
|
|
#include "gfxUtils.h"
|
2011-04-16 18:22:42 -07:00
|
|
|
#include "nsDeviceContext.h"
|
2010-03-03 13:37:04 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
nsIntRect
|
|
|
|
ThebesLayerBuffer::GetQuadrantRectangle(XSide aXSide, YSide aYSide)
|
|
|
|
{
|
|
|
|
// quadrantTranslation is the amount we translate the top-left
|
|
|
|
// of the quadrant by to get coordinates relative to the layer
|
|
|
|
nsIntPoint quadrantTranslation = -mBufferRotation;
|
|
|
|
quadrantTranslation.x += aXSide == LEFT ? mBufferRect.width : 0;
|
|
|
|
quadrantTranslation.y += aYSide == TOP ? mBufferRect.height : 0;
|
|
|
|
return mBufferRect + quadrantTranslation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param aXSide LEFT means we draw from the left side of the buffer (which
|
|
|
|
* is drawn on the right side of mBufferRect). RIGHT means we draw from
|
|
|
|
* the right side of the buffer (which is drawn on the left side of
|
|
|
|
* mBufferRect).
|
|
|
|
* @param aYSide TOP means we draw from the top side of the buffer (which
|
|
|
|
* is drawn on the bottom side of mBufferRect). BOTTOM means we draw from
|
|
|
|
* the bottom side of the buffer (which is drawn on the top side of
|
|
|
|
* mBufferRect).
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ThebesLayerBuffer::DrawBufferQuadrant(gfxContext* aTarget,
|
2010-09-03 13:10:46 -07:00
|
|
|
XSide aXSide, YSide aYSide,
|
2011-06-22 05:11:28 -07:00
|
|
|
float aOpacity)
|
2010-03-03 13:37:04 -08:00
|
|
|
{
|
|
|
|
// The rectangle that we're going to fill. Basically we're going to
|
|
|
|
// render the buffer at mBufferRect + quadrantTranslation to get the
|
|
|
|
// pixels in the right place, but we're only going to paint within
|
|
|
|
// mBufferRect
|
|
|
|
nsIntRect quadrantRect = GetQuadrantRectangle(aXSide, aYSide);
|
|
|
|
nsIntRect fillRect;
|
|
|
|
if (!fillRect.IntersectRect(mBufferRect, quadrantRect))
|
|
|
|
return;
|
|
|
|
|
|
|
|
aTarget->NewPath();
|
2010-09-03 13:10:46 -07:00
|
|
|
aTarget->Rectangle(gfxRect(fillRect.x, fillRect.y,
|
|
|
|
fillRect.width, fillRect.height),
|
2011-10-17 07:59:28 -07:00
|
|
|
true);
|
2010-09-03 13:10:46 -07:00
|
|
|
|
|
|
|
gfxPoint quadrantTranslation(quadrantRect.x, quadrantRect.y);
|
|
|
|
nsRefPtr<gfxPattern> pattern = new gfxPattern(mBuffer);
|
|
|
|
|
2010-09-09 12:33:04 -07:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
gfxPattern::GraphicsFilter filter = gfxPattern::FILTER_NEAREST;
|
|
|
|
pattern->SetFilter(filter);
|
|
|
|
#endif
|
|
|
|
|
2010-09-23 16:06:27 -07:00
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aTarget);
|
|
|
|
|
2010-09-03 13:10:46 -07:00
|
|
|
// Transform from user -> buffer space.
|
|
|
|
gfxMatrix transform;
|
|
|
|
transform.Translate(-quadrantTranslation);
|
|
|
|
|
|
|
|
pattern->SetMatrix(transform);
|
|
|
|
aTarget->SetPattern(pattern);
|
|
|
|
|
2010-07-15 14:08:04 -07:00
|
|
|
if (aOpacity != 1.0) {
|
|
|
|
aTarget->Save();
|
|
|
|
aTarget->Clip();
|
|
|
|
aTarget->Paint(aOpacity);
|
|
|
|
aTarget->Restore();
|
|
|
|
} else {
|
|
|
|
aTarget->Fill();
|
|
|
|
}
|
2010-03-03 13:37:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-22 05:11:28 -07:00
|
|
|
ThebesLayerBuffer::DrawBufferWithRotation(gfxContext* aTarget, float aOpacity)
|
2010-03-03 13:37:04 -08:00
|
|
|
{
|
|
|
|
// Draw four quadrants. We could use REPEAT_, but it's probably better
|
|
|
|
// not to, to be performance-safe.
|
2011-06-22 05:11:28 -07:00
|
|
|
DrawBufferQuadrant(aTarget, LEFT, TOP, aOpacity);
|
|
|
|
DrawBufferQuadrant(aTarget, RIGHT, TOP, aOpacity);
|
|
|
|
DrawBufferQuadrant(aTarget, LEFT, BOTTOM, aOpacity);
|
|
|
|
DrawBufferQuadrant(aTarget, RIGHT, BOTTOM, aOpacity);
|
2010-03-03 13:37:04 -08:00
|
|
|
}
|
|
|
|
|
2010-11-05 00:17:07 -07:00
|
|
|
already_AddRefed<gfxContext>
|
2011-06-22 05:11:28 -07:00
|
|
|
ThebesLayerBuffer::GetContextForQuadrantUpdate(const nsIntRect& aBounds)
|
2010-11-05 00:17:07 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(mBuffer);
|
|
|
|
|
|
|
|
// Figure out which quadrant to draw in
|
|
|
|
PRInt32 xBoundary = mBufferRect.XMost() - mBufferRotation.x;
|
|
|
|
PRInt32 yBoundary = mBufferRect.YMost() - mBufferRotation.y;
|
|
|
|
XSide sideX = aBounds.XMost() <= xBoundary ? RIGHT : LEFT;
|
|
|
|
YSide sideY = aBounds.YMost() <= yBoundary ? BOTTOM : TOP;
|
|
|
|
nsIntRect quadrantRect = GetQuadrantRectangle(sideX, sideY);
|
|
|
|
NS_ASSERTION(quadrantRect.Contains(aBounds), "Messed up quadrants");
|
|
|
|
ctx->Translate(-gfxPoint(quadrantRect.x, quadrantRect.y));
|
|
|
|
|
|
|
|
return ctx.forget();
|
|
|
|
}
|
|
|
|
|
2010-03-03 13:37:04 -08:00
|
|
|
static void
|
|
|
|
WrapRotationAxis(PRInt32* aRotationPoint, PRInt32 aSize)
|
|
|
|
{
|
|
|
|
if (*aRotationPoint < 0) {
|
|
|
|
*aRotationPoint += aSize;
|
|
|
|
} else if (*aRotationPoint >= aSize) {
|
|
|
|
*aRotationPoint -= aSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThebesLayerBuffer::PaintState
|
2010-09-03 13:10:46 -07:00
|
|
|
ThebesLayerBuffer::BeginPaint(ThebesLayer* aLayer, ContentType aContentType,
|
2011-03-01 12:30:16 -08:00
|
|
|
PRUint32 aFlags)
|
2010-03-03 13:37:04 -08:00
|
|
|
{
|
|
|
|
PaintState result;
|
2011-06-22 05:11:28 -07:00
|
|
|
// We need to disable rotation if we're going to be resampled when
|
2011-03-31 14:33:46 -07:00
|
|
|
// drawing, because we might sample across the rotation boundary.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canHaveRotation = !(aFlags & PAINT_WILL_RESAMPLE);
|
2011-03-01 12:30:16 -08:00
|
|
|
|
|
|
|
nsIntRegion validRegion = aLayer->GetValidRegion();
|
|
|
|
|
|
|
|
ContentType contentType;
|
|
|
|
nsIntRegion neededRegion;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool canReuseBuffer;
|
2011-03-01 12:30:16 -08:00
|
|
|
nsIntRect destBufferRect;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
while (true) {
|
2011-03-01 12:30:16 -08:00
|
|
|
contentType = aContentType;
|
|
|
|
neededRegion = aLayer->GetVisibleRegion();
|
2011-06-22 05:11:28 -07:00
|
|
|
canReuseBuffer = mBuffer && BufferSizeOkFor(neededRegion.GetBounds().Size());
|
2011-03-01 12:30:16 -08:00
|
|
|
|
|
|
|
if (canReuseBuffer) {
|
|
|
|
if (mBufferRect.Contains(neededRegion.GetBounds())) {
|
|
|
|
// We don't need to adjust mBufferRect.
|
|
|
|
destBufferRect = mBufferRect;
|
2011-03-09 09:27:37 -08:00
|
|
|
} else if (neededRegion.GetBounds().Size() <= mBufferRect.Size()) {
|
2011-03-01 12:30:16 -08:00
|
|
|
// The buffer's big enough but doesn't contain everything that's
|
|
|
|
// going to be visible. We'll move it.
|
|
|
|
destBufferRect = nsIntRect(neededRegion.GetBounds().TopLeft(), mBufferRect.Size());
|
2011-03-09 09:27:37 -08:00
|
|
|
} else {
|
|
|
|
destBufferRect = neededRegion.GetBounds();
|
2011-03-01 12:30:16 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destBufferRect = neededRegion.GetBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aFlags & PAINT_WILL_RESAMPLE) &&
|
2011-04-18 20:07:23 -07:00
|
|
|
(!neededRegion.GetBounds().IsEqualInterior(destBufferRect) ||
|
2011-03-01 12:30:16 -08:00
|
|
|
neededRegion.GetNumRects() > 1)) {
|
|
|
|
// The area we add to neededRegion might not be painted opaquely
|
|
|
|
contentType = gfxASurface::CONTENT_COLOR_ALPHA;
|
|
|
|
|
|
|
|
// We need to validate the entire buffer, to make sure that only valid
|
|
|
|
// pixels are sampled
|
|
|
|
neededRegion = destBufferRect;
|
|
|
|
}
|
|
|
|
|
2011-06-22 05:11:28 -07:00
|
|
|
if (mBuffer && contentType != mBuffer->GetContentType()) {
|
2011-03-01 12:30:16 -08:00
|
|
|
// We're effectively clearing the valid region, so we need to draw
|
|
|
|
// the entire needed region now.
|
|
|
|
result.mRegionToInvalidate = aLayer->GetValidRegion();
|
|
|
|
validRegion.SetEmpty();
|
|
|
|
Clear();
|
|
|
|
// Restart decision process with the cleared buffer. We can only go
|
|
|
|
// around the loop one more iteration, since mBuffer is null now.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2010-03-03 13:37:04 -08:00
|
|
|
}
|
|
|
|
|
2011-03-09 09:27:37 -08:00
|
|
|
NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()),
|
|
|
|
"Destination rect doesn't contain what we need to paint");
|
|
|
|
|
2011-03-01 12:30:16 -08:00
|
|
|
result.mRegionToDraw.Sub(neededRegion, validRegion);
|
2010-03-03 13:37:04 -08:00
|
|
|
if (result.mRegionToDraw.IsEmpty())
|
|
|
|
return result;
|
|
|
|
|
2011-03-01 12:30:16 -08:00
|
|
|
nsIntRect drawBounds = result.mRegionToDraw.GetBounds();
|
2010-03-03 13:37:04 -08:00
|
|
|
nsRefPtr<gfxASurface> destBuffer;
|
2011-03-31 14:33:46 -07:00
|
|
|
PRUint32 bufferFlags = canHaveRotation ? ALLOW_REPEAT : 0;
|
2011-03-01 12:30:16 -08:00
|
|
|
if (canReuseBuffer) {
|
2010-03-03 13:37:04 -08:00
|
|
|
nsIntRect keepArea;
|
|
|
|
if (keepArea.IntersectRect(destBufferRect, mBufferRect)) {
|
|
|
|
// Set mBufferRotation so that the pixels currently in mBuffer
|
|
|
|
// will still be rendered in the right place when mBufferRect
|
|
|
|
// changes to destBufferRect.
|
|
|
|
nsIntPoint newRotation = mBufferRotation +
|
|
|
|
(destBufferRect.TopLeft() - mBufferRect.TopLeft());
|
|
|
|
WrapRotationAxis(&newRotation.x, mBufferRect.width);
|
|
|
|
WrapRotationAxis(&newRotation.y, mBufferRect.height);
|
|
|
|
NS_ASSERTION(nsIntRect(nsIntPoint(0,0), mBufferRect.Size()).Contains(newRotation),
|
|
|
|
"newRotation out of bounds");
|
|
|
|
PRInt32 xBoundary = destBufferRect.XMost() - newRotation.x;
|
|
|
|
PRInt32 yBoundary = destBufferRect.YMost() - newRotation.y;
|
|
|
|
if ((drawBounds.x < xBoundary && xBoundary < drawBounds.XMost()) ||
|
2011-03-01 12:30:16 -08:00
|
|
|
(drawBounds.y < yBoundary && yBoundary < drawBounds.YMost()) ||
|
|
|
|
(newRotation != nsIntPoint(0,0) && !canHaveRotation)) {
|
2010-03-03 13:37:04 -08:00
|
|
|
// The stuff we need to redraw will wrap around an edge of the
|
2011-03-09 09:27:37 -08:00
|
|
|
// buffer, so move the pixels we can keep into a position that
|
|
|
|
// lets us redraw in just one quadrant.
|
|
|
|
if (mBufferRotation == nsIntPoint(0,0)) {
|
|
|
|
nsIntRect srcRect(nsIntPoint(0, 0), mBufferRect.Size());
|
|
|
|
nsIntPoint dest = mBufferRect.TopLeft() - destBufferRect.TopLeft();
|
2011-06-22 05:11:28 -07:00
|
|
|
mBuffer->MovePixels(srcRect, dest);
|
2011-10-17 07:59:28 -07:00
|
|
|
result.mDidSelfCopy = true;
|
2011-03-09 09:27:37 -08:00
|
|
|
// Don't set destBuffer; we special-case self-copies, and
|
|
|
|
// just did the necessary work above.
|
|
|
|
mBufferRect = destBufferRect;
|
2010-03-03 13:37:04 -08:00
|
|
|
} else {
|
|
|
|
// We can't do a real self-copy because the buffer is rotated.
|
|
|
|
// So allocate a new buffer for the destination.
|
2011-04-06 16:37:56 -07:00
|
|
|
destBufferRect = neededRegion.GetBounds();
|
2011-06-22 05:11:28 -07:00
|
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
2010-03-03 13:37:04 -08:00
|
|
|
if (!destBuffer)
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mBufferRect = destBufferRect;
|
|
|
|
mBufferRotation = newRotation;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// No pixels are going to be kept. The whole visible region
|
|
|
|
// will be redrawn, so we don't need to copy anything, so we don't
|
|
|
|
// set destBuffer.
|
|
|
|
mBufferRect = destBufferRect;
|
|
|
|
mBufferRotation = nsIntPoint(0,0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The buffer's not big enough, so allocate a new one
|
2011-06-22 05:11:28 -07:00
|
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
2010-03-03 13:37:04 -08:00
|
|
|
if (!destBuffer)
|
|
|
|
return result;
|
|
|
|
}
|
2011-03-01 12:30:16 -08:00
|
|
|
NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || destBufferRect == neededRegion.GetBounds(),
|
|
|
|
"If we're resampling, we need to validate the entire buffer");
|
2010-03-03 13:37:04 -08:00
|
|
|
|
2010-07-15 14:08:09 -07:00
|
|
|
// If we have no buffered data already, then destBuffer will be a fresh buffer
|
|
|
|
// and we do not need to clear it below.
|
2011-09-28 23:19:26 -07:00
|
|
|
bool isClear = mBuffer == nsnull;
|
2010-07-15 14:08:09 -07:00
|
|
|
|
2010-03-03 13:37:04 -08:00
|
|
|
if (destBuffer) {
|
|
|
|
if (mBuffer) {
|
|
|
|
// Copy the bits
|
|
|
|
nsRefPtr<gfxContext> tmpCtx = new gfxContext(destBuffer);
|
|
|
|
nsIntPoint offset = -destBufferRect.TopLeft();
|
|
|
|
tmpCtx->SetOperator(gfxContext::OPERATOR_SOURCE);
|
|
|
|
tmpCtx->Translate(gfxPoint(offset.x, offset.y));
|
2011-06-22 05:11:28 -07:00
|
|
|
DrawBufferWithRotation(tmpCtx, 1.0);
|
2010-03-03 13:37:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer = destBuffer.forget();
|
|
|
|
mBufferRect = destBufferRect;
|
|
|
|
mBufferRotation = nsIntPoint(0,0);
|
|
|
|
}
|
2011-03-01 12:30:16 -08:00
|
|
|
NS_ASSERTION(canHaveRotation || mBufferRotation == nsIntPoint(0,0),
|
|
|
|
"Rotation disabled, but we have nonzero rotation?");
|
2010-03-03 13:37:04 -08:00
|
|
|
|
|
|
|
nsIntRegion invalidate;
|
|
|
|
invalidate.Sub(aLayer->GetValidRegion(), destBufferRect);
|
|
|
|
result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate);
|
|
|
|
|
2011-06-22 05:11:28 -07:00
|
|
|
result.mContext = GetContextForQuadrantUpdate(drawBounds);
|
2010-03-03 13:37:04 -08:00
|
|
|
|
2011-01-07 08:11:18 -08:00
|
|
|
gfxUtils::ClipToRegionSnapped(result.mContext, result.mRegionToDraw);
|
2011-03-01 12:30:16 -08:00
|
|
|
if (contentType == gfxASurface::CONTENT_COLOR_ALPHA && !isClear) {
|
2010-03-03 13:37:04 -08:00
|
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
|
|
result.mContext->Paint();
|
|
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|