michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "ReadbackProcessor.h" michael@0: #include // for int32_t michael@0: #include "Layers.h" // for Layer, ThebesLayer, etc michael@0: #include "ReadbackLayer.h" // for ReadbackLayer, ReadbackSink michael@0: #include "gfx3DMatrix.h" // for gfx3DMatrix michael@0: #include "gfxColor.h" // for gfxRGBA michael@0: #include "gfxContext.h" // for gfxContext michael@0: #include "gfxRect.h" // for gfxRect michael@0: #include "mozilla/gfx/BasePoint.h" // for BasePoint michael@0: #include "mozilla/gfx/BaseRect.h" // for BaseRect michael@0: #include "nsAutoPtr.h" // for nsRefPtr, nsAutoPtr michael@0: #include "nsDebug.h" // for NS_ASSERTION michael@0: #include "nsISupportsImpl.h" // for gfxContext::Release, etc michael@0: #include "nsPoint.h" // for nsIntPoint michael@0: #include "nsRegion.h" // for nsIntRegion michael@0: #include "nsSize.h" // for nsIntSize michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: void michael@0: ReadbackProcessor::BuildUpdates(ContainerLayer* aContainer) michael@0: { michael@0: NS_ASSERTION(mAllUpdates.IsEmpty(), "Some updates not processed?"); michael@0: michael@0: if (!aContainer->mMayHaveReadbackChild) michael@0: return; michael@0: michael@0: aContainer->mMayHaveReadbackChild = false; michael@0: // go backwards so the updates read from earlier layers are later in the michael@0: // array. michael@0: for (Layer* l = aContainer->GetLastChild(); l; l = l->GetPrevSibling()) { michael@0: if (l->GetType() == Layer::TYPE_READBACK) { michael@0: aContainer->mMayHaveReadbackChild = true; michael@0: BuildUpdatesForLayer(static_cast(l)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static Layer* michael@0: FindBackgroundLayer(ReadbackLayer* aLayer, nsIntPoint* aOffset) michael@0: { michael@0: gfx::Matrix transform; michael@0: if (!aLayer->GetTransform().Is2D(&transform) || michael@0: transform.HasNonIntegerTranslation()) michael@0: return nullptr; michael@0: nsIntPoint transformOffset(int32_t(transform._31), int32_t(transform._32)); michael@0: michael@0: for (Layer* l = aLayer->GetPrevSibling(); l; l = l->GetPrevSibling()) { michael@0: gfx::Matrix backgroundTransform; michael@0: if (!l->GetTransform().Is2D(&backgroundTransform) || michael@0: gfx::ThebesMatrix(backgroundTransform).HasNonIntegerTranslation()) michael@0: return nullptr; michael@0: michael@0: nsIntPoint backgroundOffset(int32_t(backgroundTransform._31), int32_t(backgroundTransform._32)); michael@0: nsIntRect rectInBackground(transformOffset - backgroundOffset, aLayer->GetSize()); michael@0: const nsIntRegion& visibleRegion = l->GetEffectiveVisibleRegion(); michael@0: if (!visibleRegion.Intersects(rectInBackground)) michael@0: continue; michael@0: // Since l is present in the background, from here on we either choose l michael@0: // or nothing. michael@0: if (!visibleRegion.Contains(rectInBackground)) michael@0: return nullptr; michael@0: michael@0: if (l->GetEffectiveOpacity() != 1.0 || michael@0: !(l->GetContentFlags() & Layer::CONTENT_OPAQUE)) michael@0: return nullptr; michael@0: michael@0: // cliprects are post-transform michael@0: const nsIntRect* clipRect = l->GetEffectiveClipRect(); michael@0: if (clipRect && !clipRect->Contains(nsIntRect(transformOffset, aLayer->GetSize()))) michael@0: return nullptr; michael@0: michael@0: Layer::LayerType type = l->GetType(); michael@0: if (type != Layer::TYPE_COLOR && type != Layer::TYPE_THEBES) michael@0: return nullptr; michael@0: michael@0: *aOffset = backgroundOffset - transformOffset; michael@0: return l; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: ReadbackProcessor::BuildUpdatesForLayer(ReadbackLayer* aLayer) michael@0: { michael@0: if (!aLayer->mSink) michael@0: return; michael@0: michael@0: nsIntPoint offset; michael@0: Layer* newBackground = FindBackgroundLayer(aLayer, &offset); michael@0: if (!newBackground) { michael@0: aLayer->SetUnknown(); michael@0: return; michael@0: } michael@0: michael@0: if (newBackground->GetType() == Layer::TYPE_COLOR) { michael@0: ColorLayer* colorLayer = static_cast(newBackground); michael@0: if (aLayer->mBackgroundColor != colorLayer->GetColor()) { michael@0: aLayer->mBackgroundLayer = nullptr; michael@0: aLayer->mBackgroundColor = colorLayer->GetColor(); michael@0: NS_ASSERTION(aLayer->mBackgroundColor.a == 1.0, michael@0: "Color layer said it was opaque!"); michael@0: nsRefPtr ctx = michael@0: aLayer->mSink->BeginUpdate(aLayer->GetRect(), michael@0: aLayer->AllocateSequenceNumber()); michael@0: if (ctx) { michael@0: ctx->SetColor(aLayer->mBackgroundColor); michael@0: nsIntSize size = aLayer->GetSize(); michael@0: ctx->Rectangle(gfxRect(0, 0, size.width, size.height)); michael@0: ctx->Fill(); michael@0: aLayer->mSink->EndUpdate(ctx, aLayer->GetRect()); michael@0: } michael@0: } michael@0: } else { michael@0: NS_ASSERTION(newBackground->AsThebesLayer(), "Must be ThebesLayer"); michael@0: ThebesLayer* thebesLayer = static_cast(newBackground); michael@0: // updateRect is relative to the ThebesLayer michael@0: nsIntRect updateRect = aLayer->GetRect() - offset; michael@0: if (thebesLayer != aLayer->mBackgroundLayer || michael@0: offset != aLayer->mBackgroundLayerOffset) { michael@0: aLayer->mBackgroundLayer = thebesLayer; michael@0: aLayer->mBackgroundLayerOffset = offset; michael@0: aLayer->mBackgroundColor = gfxRGBA(0,0,0,0); michael@0: thebesLayer->SetUsedForReadback(true); michael@0: } else { michael@0: nsIntRegion invalid; michael@0: invalid.Sub(updateRect, thebesLayer->GetValidRegion()); michael@0: updateRect = invalid.GetBounds(); michael@0: } michael@0: michael@0: Update update = { aLayer, updateRect, aLayer->AllocateSequenceNumber() }; michael@0: mAllUpdates.AppendElement(update); michael@0: } michael@0: } michael@0: michael@0: void michael@0: ReadbackProcessor::GetThebesLayerUpdates(ThebesLayer* aLayer, michael@0: nsTArray* aUpdates, michael@0: nsIntRegion* aUpdateRegion) michael@0: { michael@0: // All ThebesLayers used for readback are in mAllUpdates (some possibly michael@0: // with an empty update rect). michael@0: aLayer->SetUsedForReadback(false); michael@0: if (aUpdateRegion) { michael@0: aUpdateRegion->SetEmpty(); michael@0: } michael@0: for (uint32_t i = mAllUpdates.Length(); i > 0; --i) { michael@0: const Update& update = mAllUpdates[i - 1]; michael@0: if (update.mLayer->mBackgroundLayer == aLayer) { michael@0: aLayer->SetUsedForReadback(true); michael@0: // Don't bother asking for updates if we have an empty update rect. michael@0: if (!update.mUpdateRect.IsEmpty()) { michael@0: aUpdates->AppendElement(update); michael@0: if (aUpdateRegion) { michael@0: aUpdateRegion->Or(*aUpdateRegion, update.mUpdateRect); michael@0: } michael@0: } michael@0: mAllUpdates.RemoveElementAt(i - 1); michael@0: } michael@0: } michael@0: } michael@0: michael@0: ReadbackProcessor::~ReadbackProcessor() michael@0: { michael@0: for (uint32_t i = mAllUpdates.Length(); i > 0; --i) { michael@0: const Update& update = mAllUpdates[i - 1]; michael@0: // Unprocessed update. Notify the readback sink that this content is michael@0: // unknown. michael@0: update.mLayer->SetUnknown(); michael@0: } michael@0: } michael@0: michael@0: } michael@0: }