2012-08-29 09:47:18 +04:00
|
|
|
/*-*- Mode: C++; tab-width: 2; 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 "LayerTreeInvalidation.h"
|
|
|
|
#include "Layers.h"
|
|
|
|
#include "ImageLayers.h"
|
|
|
|
#include "gfxUtils.h"
|
2012-11-21 06:23:57 +04:00
|
|
|
#include "nsDataHashtable.h"
|
2012-08-29 09:47:18 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
|
|
|
struct LayerPropertiesBase;
|
|
|
|
LayerPropertiesBase* CloneLayerTreePropertiesInternal(Layer* aRoot);
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
static nsIntRect
|
2012-08-29 09:47:18 +04:00
|
|
|
TransformRect(const nsIntRect& aRect, const gfx3DMatrix& aTransform)
|
|
|
|
{
|
|
|
|
if (aRect.IsEmpty()) {
|
|
|
|
return nsIntRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
gfxRect rect(aRect.x, aRect.y, aRect.width, aRect.height);
|
|
|
|
rect = aTransform.TransformBounds(rect);
|
|
|
|
rect.RoundOut();
|
|
|
|
|
|
|
|
nsIntRect intRect;
|
|
|
|
if (!gfxUtils::GfxRectToIntRect(rect, &intRect)) {
|
|
|
|
return nsIntRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
return intRect;
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
static void
|
|
|
|
AddTransformedRegion(nsIntRegion& aDest, const nsIntRegion& aSource, const gfx3DMatrix& aTransform)
|
|
|
|
{
|
|
|
|
nsIntRegionRectIterator iter(aSource);
|
|
|
|
const nsIntRect *r;
|
|
|
|
while ((r = iter.Next())) {
|
|
|
|
aDest.Or(aDest, TransformRect(*r, aTransform));
|
|
|
|
}
|
|
|
|
aDest.SimplifyOutward(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
AddRegion(nsIntRegion& aDest, const nsIntRegion& aSource)
|
|
|
|
{
|
|
|
|
aDest.Or(aDest, aSource);
|
|
|
|
aDest.SimplifyOutward(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsIntRegion
|
|
|
|
TransformRegion(const nsIntRegion& aRegion, const gfx3DMatrix& aTransform)
|
|
|
|
{
|
|
|
|
nsIntRegion result;
|
|
|
|
AddTransformedRegion(result, aRegion, aTransform);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-29 09:47:18 +04:00
|
|
|
/**
|
|
|
|
* Walks over this layer, and all descendant layers.
|
|
|
|
* If any of these are a ContainerLayer that reports invalidations to a PresShell,
|
|
|
|
* then report that the entire bounds have changed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
NotifySubdocumentInvalidationRecursive(Layer* aLayer, NotifySubDocInvalidationFunc aCallback)
|
|
|
|
{
|
|
|
|
aLayer->ClearInvalidRect();
|
|
|
|
ContainerLayer* container = aLayer->AsContainerLayer();
|
|
|
|
|
|
|
|
if (aLayer->GetMaskLayer()) {
|
|
|
|
NotifySubdocumentInvalidationRecursive(aLayer->GetMaskLayer(), aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Layer* child = container->GetFirstChild(); child; child = child->GetNextSibling()) {
|
|
|
|
NotifySubdocumentInvalidationRecursive(child, aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
aCallback(container, container->GetVisibleRegion());
|
|
|
|
}
|
|
|
|
|
|
|
|
struct LayerPropertiesBase : public LayerProperties
|
|
|
|
{
|
|
|
|
LayerPropertiesBase(Layer* aLayer)
|
|
|
|
: mLayer(aLayer)
|
|
|
|
, mMaskLayer(nullptr)
|
2012-10-01 07:38:46 +04:00
|
|
|
, mVisibleRegion(aLayer->GetVisibleRegion())
|
2012-08-29 09:47:18 +04:00
|
|
|
, mTransform(aLayer->GetTransform())
|
|
|
|
, mOpacity(aLayer->GetOpacity())
|
|
|
|
, mUseClipRect(!!aLayer->GetClipRect())
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(LayerPropertiesBase);
|
|
|
|
if (aLayer->GetMaskLayer()) {
|
|
|
|
mMaskLayer = CloneLayerTreePropertiesInternal(aLayer->GetMaskLayer());
|
|
|
|
}
|
|
|
|
if (mUseClipRect) {
|
|
|
|
mClipRect = *aLayer->GetClipRect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LayerPropertiesBase()
|
|
|
|
: mLayer(nullptr)
|
|
|
|
, mMaskLayer(nullptr)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(LayerPropertiesBase);
|
|
|
|
}
|
|
|
|
~LayerPropertiesBase()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(LayerPropertiesBase);
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
virtual nsIntRegion ComputeDifferences(Layer* aRoot,
|
|
|
|
NotifySubDocInvalidationFunc aCallback);
|
2012-08-29 09:47:18 +04:00
|
|
|
|
2012-09-17 02:57:22 +04:00
|
|
|
virtual void MoveBy(const nsIntPoint& aOffset);
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
nsIntRegion ComputeChange(NotifySubDocInvalidationFunc aCallback)
|
2012-08-29 09:47:18 +04:00
|
|
|
{
|
2012-10-17 12:00:00 +04:00
|
|
|
bool transformChanged = !mTransform.FuzzyEqual(mLayer->GetTransform());
|
2012-08-29 09:47:18 +04:00
|
|
|
Layer* otherMask = mLayer->GetMaskLayer();
|
|
|
|
const nsIntRect* otherClip = mLayer->GetClipRect();
|
2012-11-12 22:31:15 +04:00
|
|
|
nsIntRegion result;
|
2012-08-29 09:47:18 +04:00
|
|
|
if ((mMaskLayer ? mMaskLayer->mLayer : nullptr) != otherMask ||
|
|
|
|
(mUseClipRect != !!otherClip) ||
|
|
|
|
mLayer->GetOpacity() != mOpacity ||
|
|
|
|
transformChanged)
|
|
|
|
{
|
|
|
|
result = OldTransformedBounds();
|
|
|
|
if (transformChanged) {
|
2012-11-12 22:31:15 +04:00
|
|
|
AddRegion(result, NewTransformedBounds());
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have to generate invalidations separately for child
|
|
|
|
// layers then we can just stop here since we've already invalidated the entire
|
|
|
|
// old and new bounds.
|
|
|
|
if (!aCallback) {
|
|
|
|
ClearInvalidations(mLayer);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-01 07:38:46 +04:00
|
|
|
nsIntRegion visible;
|
|
|
|
visible.Xor(mVisibleRegion, mLayer->GetVisibleRegion());
|
2012-11-12 22:31:15 +04:00
|
|
|
AddTransformedRegion(result, visible, mTransform);
|
2012-10-01 07:38:46 +04:00
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
AddRegion(result, ComputeChangeInternal(aCallback));
|
|
|
|
AddTransformedRegion(result, mLayer->GetInvalidRegion().GetBounds(), mTransform);
|
2012-08-29 09:47:18 +04:00
|
|
|
|
|
|
|
if (mMaskLayer && otherMask) {
|
2012-11-12 22:31:15 +04:00
|
|
|
AddTransformedRegion(result, mMaskLayer->ComputeChange(aCallback), mTransform);
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mUseClipRect && otherClip) {
|
|
|
|
if (!mClipRect.IsEqualInterior(*otherClip)) {
|
|
|
|
nsIntRegion tmp;
|
|
|
|
tmp.Xor(mClipRect, *otherClip);
|
2012-11-12 22:31:15 +04:00
|
|
|
AddRegion(result, tmp);
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mLayer->ClearInvalidRect();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect NewTransformedBounds()
|
|
|
|
{
|
|
|
|
return TransformRect(mLayer->GetVisibleRegion().GetBounds(), mLayer->GetTransform());
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRect OldTransformedBounds()
|
|
|
|
{
|
2012-10-01 07:38:46 +04:00
|
|
|
return TransformRect(mVisibleRegion.GetBounds(), mTransform);
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
virtual nsIntRegion ComputeChangeInternal(NotifySubDocInvalidationFunc aCallback) { return nsIntRect(); }
|
2012-08-29 09:47:18 +04:00
|
|
|
|
|
|
|
nsRefPtr<Layer> mLayer;
|
|
|
|
nsAutoPtr<LayerPropertiesBase> mMaskLayer;
|
2012-10-01 07:38:46 +04:00
|
|
|
nsIntRegion mVisibleRegion;
|
2012-08-29 09:47:18 +04:00
|
|
|
gfx3DMatrix mTransform;
|
|
|
|
float mOpacity;
|
|
|
|
nsIntRect mClipRect;
|
|
|
|
bool mUseClipRect;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ContainerLayerProperties : public LayerPropertiesBase
|
|
|
|
{
|
|
|
|
ContainerLayerProperties(ContainerLayer* aLayer)
|
|
|
|
: LayerPropertiesBase(aLayer)
|
|
|
|
{
|
|
|
|
for (Layer* child = aLayer->GetFirstChild(); child; child = child->GetNextSibling()) {
|
|
|
|
mChildren.AppendElement(CloneLayerTreePropertiesInternal(child));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
virtual nsIntRegion ComputeChangeInternal(NotifySubDocInvalidationFunc aCallback)
|
2012-08-29 09:47:18 +04:00
|
|
|
{
|
|
|
|
ContainerLayer* container = mLayer->AsContainerLayer();
|
|
|
|
nsIntRegion result;
|
|
|
|
|
2012-11-21 06:23:57 +04:00
|
|
|
// A low frame rate is especially visible to users when scrolling, so we
|
|
|
|
// particularly want to avoid unnecessary invalidation at that time. For us
|
|
|
|
// here, that means avoiding unnecessary invalidation of child items when
|
|
|
|
// other children are added to or removed from our container layer, since
|
|
|
|
// that may be caused by children being scrolled in or out of view. We are
|
|
|
|
// less concerned with children changing order.
|
|
|
|
// TODO: Consider how we could avoid unnecessary invalidation when children
|
|
|
|
// change order, and whether the overhead would be worth it.
|
|
|
|
|
|
|
|
nsDataHashtable<nsPtrHashKey<Layer>, uint32_t> oldIndexMap;
|
|
|
|
oldIndexMap.Init(mChildren.Length());
|
|
|
|
for (uint32_t i = 0; i < mChildren.Length(); ++i) {
|
|
|
|
oldIndexMap.Put(mChildren[i]->mLayer, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t i = 0; // cursor into the old child list mChildren
|
2012-08-29 09:47:18 +04:00
|
|
|
for (Layer* child = container->GetFirstChild(); child; child = child->GetNextSibling()) {
|
2012-11-21 06:23:57 +04:00
|
|
|
bool invalidateChildsCurrentArea = false;
|
|
|
|
if (i < mChildren.Length()) {
|
|
|
|
uint32_t childsOldIndex;
|
|
|
|
if (oldIndexMap.Get(child, &childsOldIndex)) {
|
|
|
|
if (childsOldIndex >= i) {
|
|
|
|
// Invalidate the old areas of layers that used to be between the
|
|
|
|
// current |child| and the previous |child| that was also in the
|
|
|
|
// old list mChildren (if any of those children have been reordered
|
|
|
|
// rather than removed, we will invalidate their new area when we
|
|
|
|
// encounter them in the new list):
|
|
|
|
for (uint32_t j = i; j < childsOldIndex; ++j) {
|
|
|
|
AddRegion(result, mChildren[j]->OldTransformedBounds());
|
|
|
|
}
|
|
|
|
// Invalidate any regions of the child that have changed:
|
|
|
|
AddRegion(result, mChildren[childsOldIndex]->ComputeChange(aCallback));
|
|
|
|
i = childsOldIndex + 1;
|
|
|
|
} else {
|
|
|
|
// We've already seen this child in mChildren (which means it must
|
|
|
|
// have been reordered) and invalidated its old area. We need to
|
|
|
|
// invalidate its new area too:
|
|
|
|
invalidateChildsCurrentArea = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// |child| is new
|
|
|
|
invalidateChildsCurrentArea = true;
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
2012-11-21 06:23:57 +04:00
|
|
|
} else {
|
|
|
|
// |child| is new, or was reordered to a higher index
|
|
|
|
invalidateChildsCurrentArea = true;
|
|
|
|
}
|
|
|
|
if (invalidateChildsCurrentArea) {
|
|
|
|
AddTransformedRegion(result, child->GetVisibleRegion(), child->GetTransform());
|
2012-08-29 09:47:18 +04:00
|
|
|
if (aCallback) {
|
|
|
|
NotifySubdocumentInvalidationRecursive(child, aCallback);
|
|
|
|
} else {
|
|
|
|
ClearInvalidations(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process remaining removed children.
|
|
|
|
while (i < mChildren.Length()) {
|
2012-11-12 22:31:15 +04:00
|
|
|
AddRegion(result, mChildren[i]->OldTransformedBounds());
|
2012-08-29 09:47:18 +04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aCallback) {
|
|
|
|
aCallback(container, result);
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
return TransformRegion(result, mLayer->GetTransform());
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
|
2012-11-21 06:23:57 +04:00
|
|
|
// The old list of children:
|
2012-08-29 09:47:18 +04:00
|
|
|
nsAutoTArray<nsAutoPtr<LayerPropertiesBase>,1> mChildren;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ColorLayerProperties : public LayerPropertiesBase
|
|
|
|
{
|
|
|
|
ColorLayerProperties(ColorLayer *aLayer)
|
|
|
|
: LayerPropertiesBase(aLayer)
|
|
|
|
, mColor(aLayer->GetColor())
|
|
|
|
{ }
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
virtual nsIntRegion ComputeChangeInternal(NotifySubDocInvalidationFunc aCallback)
|
2012-08-29 09:47:18 +04:00
|
|
|
{
|
|
|
|
ColorLayer* color = static_cast<ColorLayer*>(mLayer.get());
|
|
|
|
|
|
|
|
if (mColor != color->GetColor()) {
|
|
|
|
return NewTransformedBounds();
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
return nsIntRegion();
|
2012-08-29 09:47:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
gfxRGBA mColor;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ImageLayerProperties : public LayerPropertiesBase
|
|
|
|
{
|
|
|
|
ImageLayerProperties(ImageLayer* aImage)
|
|
|
|
: LayerPropertiesBase(aImage)
|
|
|
|
, mVisibleRegion(aImage->GetVisibleRegion())
|
|
|
|
, mContainer(aImage->GetContainer())
|
|
|
|
, mFilter(aImage->GetFilter())
|
|
|
|
, mScaleToSize(aImage->GetScaleToSize())
|
|
|
|
, mScaleMode(aImage->GetScaleMode())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
virtual nsIntRegion ComputeChangeInternal(NotifySubDocInvalidationFunc aCallback)
|
2012-08-29 09:47:18 +04:00
|
|
|
{
|
|
|
|
ImageLayer* imageLayer = static_cast<ImageLayer*>(mLayer.get());
|
|
|
|
|
|
|
|
if (!imageLayer->GetVisibleRegion().IsEqual(mVisibleRegion)) {
|
|
|
|
nsIntRect result = NewTransformedBounds();
|
|
|
|
result = result.Union(OldTransformedBounds());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mContainer != imageLayer->GetContainer() ||
|
|
|
|
mFilter != imageLayer->GetFilter() ||
|
|
|
|
mScaleToSize != imageLayer->GetScaleToSize() ||
|
2012-09-28 17:12:59 +04:00
|
|
|
mScaleMode != imageLayer->GetScaleMode()) {
|
2012-08-29 09:47:18 +04:00
|
|
|
return NewTransformedBounds();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsIntRect();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIntRegion mVisibleRegion;
|
|
|
|
nsRefPtr<ImageContainer> mContainer;
|
|
|
|
gfxPattern::GraphicsFilter mFilter;
|
|
|
|
gfxIntSize mScaleToSize;
|
|
|
|
ImageLayer::ScaleMode mScaleMode;
|
|
|
|
};
|
|
|
|
|
|
|
|
LayerPropertiesBase*
|
|
|
|
CloneLayerTreePropertiesInternal(Layer* aRoot)
|
|
|
|
{
|
|
|
|
if (!aRoot) {
|
|
|
|
return new LayerPropertiesBase();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (aRoot->GetType()) {
|
|
|
|
case Layer::TYPE_CONTAINER: return new ContainerLayerProperties(aRoot->AsContainerLayer());
|
|
|
|
case Layer::TYPE_COLOR: return new ColorLayerProperties(static_cast<ColorLayer*>(aRoot));
|
|
|
|
case Layer::TYPE_IMAGE: return new ImageLayerProperties(static_cast<ImageLayer*>(aRoot));
|
|
|
|
default: return new LayerPropertiesBase(aRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ LayerProperties*
|
|
|
|
LayerProperties::CloneFrom(Layer* aRoot)
|
|
|
|
{
|
|
|
|
return CloneLayerTreePropertiesInternal(aRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
LayerProperties::ClearInvalidations(Layer *aLayer)
|
|
|
|
{
|
|
|
|
aLayer->ClearInvalidRect();
|
|
|
|
if (aLayer->GetMaskLayer()) {
|
|
|
|
ClearInvalidations(aLayer->GetMaskLayer());
|
|
|
|
}
|
|
|
|
|
|
|
|
ContainerLayer* container = aLayer->AsContainerLayer();
|
|
|
|
if (!container) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Layer* child = container->GetFirstChild(); child; child = child->GetNextSibling()) {
|
|
|
|
ClearInvalidations(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-12 22:31:15 +04:00
|
|
|
nsIntRegion
|
2012-08-29 09:47:18 +04:00
|
|
|
LayerPropertiesBase::ComputeDifferences(Layer* aRoot, NotifySubDocInvalidationFunc aCallback)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aRoot, "Must have a layer tree to compare against!");
|
|
|
|
if (mLayer != aRoot) {
|
|
|
|
if (aCallback) {
|
|
|
|
NotifySubdocumentInvalidationRecursive(aRoot, aCallback);
|
|
|
|
} else {
|
|
|
|
ClearInvalidations(aRoot);
|
|
|
|
}
|
|
|
|
nsIntRect result = TransformRect(aRoot->GetVisibleRegion().GetBounds(), aRoot->GetTransform());
|
|
|
|
result = result.Union(OldTransformedBounds());
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return ComputeChange(aCallback);
|
|
|
|
}
|
|
|
|
}
|
2012-09-17 02:57:22 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
LayerPropertiesBase::MoveBy(const nsIntPoint& aOffset)
|
|
|
|
{
|
|
|
|
mTransform.TranslatePost(gfxPoint3D(aOffset.x, aOffset.y, 0));
|
|
|
|
}
|
2012-08-29 09:47:18 +04:00
|
|
|
|
|
|
|
} // namespace layers
|
|
|
|
} // namespace mozilla
|