зеркало из https://github.com/mozilla/gecko-dev.git
306 строки
11 KiB
C++
306 строки
11 KiB
C++
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
* 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/. */
|
|
|
|
#include "ThebesLayerBuffer.h"
|
|
#include "Layers.h"
|
|
#include "gfxContext.h"
|
|
#include "gfxPlatform.h"
|
|
#include "gfxUtils.h"
|
|
#include "nsDeviceContext.h"
|
|
#include "sampler.h"
|
|
|
|
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,
|
|
XSide aXSide, YSide aYSide,
|
|
float aOpacity,
|
|
gfxASurface* aMask,
|
|
const gfxMatrix* aMaskTransform)
|
|
{
|
|
// 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();
|
|
aTarget->Rectangle(gfxRect(fillRect.x, fillRect.y,
|
|
fillRect.width, fillRect.height),
|
|
true);
|
|
|
|
gfxPoint quadrantTranslation(quadrantRect.x, quadrantRect.y);
|
|
nsRefPtr<gfxPattern> pattern = new gfxPattern(mBuffer);
|
|
|
|
#ifdef MOZ_GFX_OPTIMIZE_MOBILE
|
|
gfxPattern::GraphicsFilter filter = gfxPattern::FILTER_NEAREST;
|
|
pattern->SetFilter(filter);
|
|
#endif
|
|
|
|
gfxContextMatrixAutoSaveRestore saveMatrix(aTarget);
|
|
|
|
// Transform from user -> buffer space.
|
|
gfxMatrix transform;
|
|
transform.Translate(-quadrantTranslation);
|
|
|
|
pattern->SetMatrix(transform);
|
|
aTarget->SetPattern(pattern);
|
|
|
|
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);
|
|
}
|
|
} else {
|
|
if (aOpacity == 1.0) {
|
|
aTarget->Fill();
|
|
} else {
|
|
aTarget->Save();
|
|
aTarget->Clip();
|
|
aTarget->Paint(aOpacity);
|
|
aTarget->Restore();
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
ThebesLayerBuffer::DrawBufferWithRotation(gfxContext* aTarget, float aOpacity,
|
|
gfxASurface* aMask,
|
|
const gfxMatrix* aMaskTransform)
|
|
{
|
|
SAMPLE_LABEL("ThebesLayerBuffer", "DrawBufferWithRotation");
|
|
// Draw four quadrants. We could use REPEAT_, but it's probably better
|
|
// not to, to be performance-safe.
|
|
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);
|
|
}
|
|
|
|
already_AddRefed<gfxContext>
|
|
ThebesLayerBuffer::GetContextForQuadrantUpdate(const nsIntRect& aBounds)
|
|
{
|
|
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();
|
|
}
|
|
|
|
static void
|
|
WrapRotationAxis(PRInt32* aRotationPoint, PRInt32 aSize)
|
|
{
|
|
if (*aRotationPoint < 0) {
|
|
*aRotationPoint += aSize;
|
|
} else if (*aRotationPoint >= aSize) {
|
|
*aRotationPoint -= aSize;
|
|
}
|
|
}
|
|
|
|
ThebesLayerBuffer::PaintState
|
|
ThebesLayerBuffer::BeginPaint(ThebesLayer* aLayer, ContentType aContentType,
|
|
PRUint32 aFlags)
|
|
{
|
|
PaintState result;
|
|
// We need to disable rotation if we're going to be resampled when
|
|
// drawing, because we might sample across the rotation boundary.
|
|
bool canHaveRotation = !(aFlags & (PAINT_WILL_RESAMPLE | PAINT_NO_ROTATION));
|
|
|
|
nsIntRegion validRegion = aLayer->GetValidRegion();
|
|
|
|
ContentType contentType;
|
|
nsIntRegion neededRegion;
|
|
bool canReuseBuffer;
|
|
nsIntRect destBufferRect;
|
|
|
|
while (true) {
|
|
contentType = aContentType;
|
|
neededRegion = aLayer->GetVisibleRegion();
|
|
canReuseBuffer = mBuffer && BufferSizeOkFor(neededRegion.GetBounds().Size());
|
|
|
|
if (canReuseBuffer) {
|
|
if (mBufferRect.Contains(neededRegion.GetBounds())) {
|
|
// We don't need to adjust mBufferRect.
|
|
destBufferRect = mBufferRect;
|
|
} else if (neededRegion.GetBounds().Size() <= mBufferRect.Size()) {
|
|
// 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());
|
|
} else {
|
|
destBufferRect = neededRegion.GetBounds();
|
|
}
|
|
} else {
|
|
destBufferRect = neededRegion.GetBounds();
|
|
}
|
|
|
|
if ((aFlags & PAINT_WILL_RESAMPLE) &&
|
|
(!neededRegion.GetBounds().IsEqualInterior(destBufferRect) ||
|
|
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;
|
|
}
|
|
|
|
if (mBuffer && contentType != mBuffer->GetContentType()) {
|
|
// 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;
|
|
}
|
|
|
|
NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()),
|
|
"Destination rect doesn't contain what we need to paint");
|
|
|
|
result.mRegionToDraw.Sub(neededRegion, validRegion);
|
|
if (result.mRegionToDraw.IsEmpty())
|
|
return result;
|
|
|
|
nsIntRect drawBounds = result.mRegionToDraw.GetBounds();
|
|
nsRefPtr<gfxASurface> destBuffer;
|
|
PRUint32 bufferFlags = canHaveRotation ? ALLOW_REPEAT : 0;
|
|
if (canReuseBuffer) {
|
|
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()) ||
|
|
(drawBounds.y < yBoundary && yBoundary < drawBounds.YMost()) ||
|
|
(newRotation != nsIntPoint(0,0) && !canHaveRotation)) {
|
|
// The stuff we need to redraw will wrap around an edge of the
|
|
// 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();
|
|
mBuffer->MovePixels(srcRect, dest);
|
|
result.mDidSelfCopy = true;
|
|
// Don't set destBuffer; we special-case self-copies, and
|
|
// just did the necessary work above.
|
|
mBufferRect = destBufferRect;
|
|
} else {
|
|
// We can't do a real self-copy because the buffer is rotated.
|
|
// So allocate a new buffer for the destination.
|
|
destBufferRect = neededRegion.GetBounds();
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
|
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
|
|
destBuffer = CreateBuffer(contentType, destBufferRect.Size(), bufferFlags);
|
|
if (!destBuffer)
|
|
return result;
|
|
}
|
|
NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || destBufferRect == neededRegion.GetBounds(),
|
|
"If we're resampling, we need to validate the entire buffer");
|
|
|
|
// If we have no buffered data already, then destBuffer will be a fresh buffer
|
|
// and we do not need to clear it below.
|
|
bool isClear = mBuffer == nsnull;
|
|
|
|
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));
|
|
DrawBufferWithRotation(tmpCtx, 1.0);
|
|
}
|
|
|
|
mBuffer = destBuffer.forget();
|
|
mBufferRect = destBufferRect;
|
|
mBufferRotation = nsIntPoint(0,0);
|
|
}
|
|
NS_ASSERTION(canHaveRotation || mBufferRotation == nsIntPoint(0,0),
|
|
"Rotation disabled, but we have nonzero rotation?");
|
|
|
|
nsIntRegion invalidate;
|
|
invalidate.Sub(aLayer->GetValidRegion(), destBufferRect);
|
|
result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate);
|
|
|
|
result.mContext = GetContextForQuadrantUpdate(drawBounds);
|
|
|
|
gfxUtils::ClipToRegionSnapped(result.mContext, result.mRegionToDraw);
|
|
if (contentType == gfxASurface::CONTENT_COLOR_ALPHA && !isClear) {
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_CLEAR);
|
|
result.mContext->Paint();
|
|
result.mContext->SetOperator(gfxContext::OPERATOR_OVER);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
}
|
|
}
|
|
|