2010-07-21 18:06:33 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-03-03 21:37:04 +00:00
|
|
|
|
|
|
|
#include "ThebesLayerBuffer.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "gfxContext.h"
|
|
|
|
#include "gfxPlatform.h"
|
2010-09-03 02:31:42 +00:00
|
|
|
#include "gfxUtils.h"
|
2011-04-17 01:22:42 +00:00
|
|
|
#include "nsDeviceContext.h"
|
2012-04-16 18:36:33 +00:00
|
|
|
#include "sampler.h"
|
2010-03-03 21:37:04 +00: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 20:10:46 +00:00
|
|
|
XSide aXSide, YSide aYSide,
|
2012-02-07 22:27:21 +00:00
|
|
|
float aOpacity,
|
|
|
|
gfxASurface* aMask,
|
|
|
|
const gfxMatrix* aMaskTransform)
|
2010-03-03 21:37:04 +00: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 20:10:46 +00:00
|
|
|
aTarget->Rectangle(gfxRect(fillRect.x, fillRect.y,
|
|
|
|
fillRect.width, fillRect.height),
|
2011-10-17 14:59:28 +00:00
|
|
|
true);
|
2010-09-03 20:10:46 +00:00
|
|
|
|
|
|
|
gfxPoint quadrantTranslation(quadrantRect.x, quadrantRect.y);
|
|
|
|
nsRefPtr<gfxPattern> pattern = new gfxPattern(mBuffer);
|
|
|
|
|
2010-09-09 19:33:04 +00:00
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
|
|
gfxPattern::GraphicsFilter filter = gfxPattern::FILTER_NEAREST;
|
|
|
|
pattern->SetFilter(filter);
|
|
|
|
#endif
|
|
|
|
|
2010-09-23 23:06:27 +00:00
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aTarget);
|
|
|
|
|
2010-09-03 20:10:46 +00:00
|
|
|
// Transform from user -> buffer space.
|
|
|
|
gfxMatrix transform;
|
|
|
|
transform.Translate(-quadrantTranslation);
|
|
|
|
|
|
|
|
pattern->SetMatrix(transform);
|
|
|
|
aTarget->SetPattern(pattern);
|
|
|
|
|
2012-02-07 22:27:21 +00:00
|
|
|
if (aMask) {
|
|
|
|
if (aOpacity == 1.0) {
|
|
|
|
aTarget->SetMatrix(*aMaskTransform);
|
|
|
|
aTarget->Mask(aMask);
|
|
|
|
} else {
|
|
|
|
aTarget->PushGroup(gfxASurface::CONTENT_COLOR_ALPHA);
|
|
|
|
aTarget->Paint(aOpacity);
|
|
|
|
aTarget->PopGroupToSource();
|
|
|
|
aTarget->SetMatrix(*aMaskTransform);
|
|
|
|
aTarget->Mask(aMask);
|
|
|
|
}
|
2010-07-15 21:08:04 +00:00
|
|
|
} else {
|
2012-02-07 22:27:21 +00:00
|
|
|
if (aOpacity == 1.0) {
|
|
|
|
aTarget->Fill();
|
|
|
|
} else {
|
|
|
|
aTarget->Save();
|
|
|
|
aTarget->Clip();
|
|
|
|
aTarget->Paint(aOpacity);
|
|
|
|
aTarget->Restore();
|
|
|
|
}
|
2010-07-15 21:08:04 +00:00
|
|
|
}
|
2010-03-03 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-02-07 22:27:21 +00:00
|
|
|
ThebesLayerBuffer::DrawBufferWithRotation(gfxContext* aTarget, float aOpacity,
|
|
|
|
gfxASurface* aMask,
|
|
|
|
const gfxMatrix* aMaskTransform)
|
2010-03-03 21:37:04 +00:00
|
|
|
{
|
2012-04-16 18:36:33 +00:00
|
|
|
SAMPLE_LABEL("ThebesLayerBuffer", "DrawBufferWithRotation");
|
2010-03-03 21:37:04 +00:00
|
|
|
// Draw four quadrants. We could use REPEAT_, but it's probably better
|
|
|
|
// not to, to be performance-safe.
|
2012-02-07 22:27:21 +00:00
|
|
|
DrawBufferQuadrant(aTarget, LEFT, TOP, aOpacity, aMask, aMaskTransform);
|
|
|
|
DrawBufferQuadrant(aTarget, RIGHT, TOP, aOpacity, aMask, aMaskTransform);
|
|
|
|
DrawBufferQuadrant(aTarget, LEFT, BOTTOM, aOpacity, aMask, aMaskTransform);
|
|
|
|
DrawBufferQuadrant(aTarget, RIGHT, BOTTOM, aOpacity, aMask, aMaskTransform);
|
2010-03-03 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
2010-11-05 07:17:07 +00:00
|
|
|
already_AddRefed<gfxContext>
|
2011-06-22 12:11:28 +00:00
|
|
|
ThebesLayerBuffer::GetContextForQuadrantUpdate(const nsIntRect& aBounds)
|
2010-11-05 07:17:07 +00:00
|
|
|
{
|
|
|
|
nsRefPtr<gfxContext> ctx = new gfxContext(mBuffer);
|
|
|
|
|
|
|
|
// Figure out which quadrant to draw in
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t xBoundary = mBufferRect.XMost() - mBufferRotation.x;
|
|
|
|
int32_t yBoundary = mBufferRect.YMost() - mBufferRotation.y;
|
2010-11-05 07:17:07 +00:00
|
|
|
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 21:37:04 +00:00
|
|
|
static void
|
2012-08-22 15:56:38 +00:00
|
|
|
WrapRotationAxis(int32_t* aRotationPoint, int32_t aSize)
|
2010-03-03 21:37:04 +00:00
|
|
|
{
|
|
|
|
if (*aRotationPoint < 0) {
|
|
|
|
*aRotationPoint += aSize;
|
|
|
|
} else if (*aRotationPoint >= aSize) {
|
|
|
|
*aRotationPoint -= aSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ThebesLayerBuffer::PaintState
|
2010-09-03 20:10:46 +00:00
|
|
|
ThebesLayerBuffer::BeginPaint(ThebesLayer* aLayer, ContentType aContentType,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aFlags)
|
2010-03-03 21:37:04 +00:00
|
|
|
{
|
|
|
|
PaintState result;
|
2011-06-22 12:11:28 +00:00
|
|
|
// We need to disable rotation if we're going to be resampled when
|
2011-03-31 21:33:46 +00:00
|
|
|
// drawing, because we might sample across the rotation boundary.
|
2012-03-19 00:01:47 +00:00
|
|
|
bool canHaveRotation = !(aFlags & (PAINT_WILL_RESAMPLE | PAINT_NO_ROTATION));
|
2011-03-01 20:30:16 +00:00
|
|
|
|
|
|
|
nsIntRegion validRegion = aLayer->GetValidRegion();
|
|
|
|
|
|
|
|
ContentType contentType;
|
|
|
|
nsIntRegion neededRegion;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool canReuseBuffer;
|
2011-03-01 20:30:16 +00:00
|
|
|
nsIntRect destBufferRect;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
while (true) {
|
2011-03-01 20:30:16 +00:00
|
|
|
contentType = aContentType;
|
|
|
|
neededRegion = aLayer->GetVisibleRegion();
|
2011-06-22 12:11:28 +00:00
|
|
|
canReuseBuffer = mBuffer && BufferSizeOkFor(neededRegion.GetBounds().Size());
|
2011-03-01 20:30:16 +00:00
|
|
|
|
|
|
|
if (canReuseBuffer) {
|
|
|
|
if (mBufferRect.Contains(neededRegion.GetBounds())) {
|
|
|
|
// We don't need to adjust mBufferRect.
|
|
|
|
destBufferRect = mBufferRect;
|
2011-03-09 17:27:37 +00:00
|
|
|
} else if (neededRegion.GetBounds().Size() <= mBufferRect.Size()) {
|
2011-03-01 20:30:16 +00: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 17:27:37 +00:00
|
|
|
} else {
|
|
|
|
destBufferRect = neededRegion.GetBounds();
|
2011-03-01 20:30:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
destBufferRect = neededRegion.GetBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aFlags & PAINT_WILL_RESAMPLE) &&
|
2011-04-19 03:07:23 +00:00
|
|
|
(!neededRegion.GetBounds().IsEqualInterior(destBufferRect) ||
|
2011-03-01 20:30:16 +00: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 12:11:28 +00:00
|
|
|
if (mBuffer && contentType != mBuffer->GetContentType()) {
|
2011-03-01 20:30:16 +00: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 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
2011-03-09 17:27:37 +00:00
|
|
|
NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()),
|
|
|
|
"Destination rect doesn't contain what we need to paint");
|
|
|
|
|
2011-03-01 20:30:16 +00:00
|
|
|
result.mRegionToDraw.Sub(neededRegion, validRegion);
|
2010-03-03 21:37:04 +00:00
|
|
|
if (result.mRegionToDraw.IsEmpty())
|
|
|
|
return result;
|
|
|
|
|
2011-03-01 20:30:16 +00:00
|
|
|
nsIntRect drawBounds = result.mRegionToDraw.GetBounds();
|
2010-03-03 21:37:04 +00:00
|
|
|
nsRefPtr<gfxASurface> destBuffer;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t bufferFlags = canHaveRotation ? ALLOW_REPEAT : 0;
|
2011-03-01 20:30:16 +00:00
|
|
|
if (canReuseBuffer) {
|
2010-03-03 21:37:04 +00: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");
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t xBoundary = destBufferRect.XMost() - newRotation.x;
|
|
|
|
int32_t yBoundary = destBufferRect.YMost() - newRotation.y;
|
2010-03-03 21:37:04 +00:00
|
|
|
if ((drawBounds.x < xBoundary && xBoundary < drawBounds.XMost()) ||
|
2011-03-01 20:30:16 +00:00
|
|
|
(drawBounds.y < yBoundary && yBoundary < drawBounds.YMost()) ||
|
|
|
|
(newRotation != nsIntPoint(0,0) && !canHaveRotation)) {
|
2010-03-03 21:37:04 +00:00
|
|
|
// The stuff we need to redraw will wrap around an edge of the
|
2011-03-09 17:27:37 +00: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 12:11:28 +00:00
|
|
|
mBuffer->MovePixels(srcRect, dest);
|
2011-10-17 14:59:28 +00:00
|
|
|
result.mDidSelfCopy = true;
|
2011-03-09 17:27:37 +00:00
|
|
|
// Don't set destBuffer; we special-case self-copies, and
|
|
|
|
// just did the necessary work above.
|
|
|
|
mBufferRect = destBufferRect;
|
2010-03-03 21:37:04 +00: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 23:37:56 +00:00
|
|
|
destBufferRect = neededRegion.GetBounds();
|
2011-06-22 12:11:28 +00:00
|
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
2010-03-03 21:37:04 +00: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 12:11:28 +00:00
|
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
2010-03-03 21:37:04 +00:00
|
|
|
if (!destBuffer)
|
|
|
|
return result;
|
|
|
|
}
|
2011-03-01 20:30:16 +00:00
|
|
|
NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || destBufferRect == neededRegion.GetBounds(),
|
|
|
|
"If we're resampling, we need to validate the entire buffer");
|
2010-03-03 21:37:04 +00:00
|
|
|
|
2010-07-15 21:08:09 +00:00
|
|
|
// If we have no buffered data already, then destBuffer will be a fresh buffer
|
|
|
|
// and we do not need to clear it below.
|
2012-07-30 14:20:58 +00:00
|
|
|
bool isClear = mBuffer == nullptr;
|
2010-07-15 21:08:09 +00:00
|
|
|
|
2010-03-03 21:37:04 +00: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 12:11:28 +00:00
|
|
|
DrawBufferWithRotation(tmpCtx, 1.0);
|
2010-03-03 21:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mBuffer = destBuffer.forget();
|
|
|
|
mBufferRect = destBufferRect;
|
|
|
|
mBufferRotation = nsIntPoint(0,0);
|
|
|
|
}
|
2011-03-01 20:30:16 +00:00
|
|
|
NS_ASSERTION(canHaveRotation || mBufferRotation == nsIntPoint(0,0),
|
|
|
|
"Rotation disabled, but we have nonzero rotation?");
|
2010-03-03 21:37:04 +00:00
|
|
|
|
|
|
|
nsIntRegion invalidate;
|
|
|
|
invalidate.Sub(aLayer->GetValidRegion(), destBufferRect);
|
|
|
|
result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate);
|
|
|
|
|
2011-06-22 12:11:28 +00:00
|
|
|
result.mContext = GetContextForQuadrantUpdate(drawBounds);
|
2010-03-03 21:37:04 +00:00
|
|
|
|
2011-01-07 16:11:18 +00:00
|
|
|
gfxUtils::ClipToRegionSnapped(result.mContext, result.mRegionToDraw);
|
2011-03-01 20:30:16 +00:00
|
|
|
if (contentType == gfxASurface::CONTENT_COLOR_ALPHA && !isClear) {
|
2010-03-03 21:37:04 +00:00
|
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
|
|
result.mContext->Paint();
|
|
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|