michael@0: /* -*- Mode: C++; tab-width: 2; 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 // for uint32_t michael@0: #include // for rand, RAND_MAX michael@0: #include // for int32_t michael@0: #include "BasicContainerLayer.h" // for BasicContainerLayer michael@0: #include "BasicLayersImpl.h" // for ToData, BasicReadbackLayer, etc michael@0: #include "GeckoProfiler.h" // for PROFILER_LABEL michael@0: #include "ImageContainer.h" // for ImageFactory michael@0: #include "Layers.h" // for Layer, ContainerLayer, etc michael@0: #include "ReadbackLayer.h" // for ReadbackLayer michael@0: #include "ReadbackProcessor.h" // for ReadbackProcessor michael@0: #include "RenderTrace.h" // for RenderTraceLayers, etc michael@0: #include "basic/BasicImplData.h" // for BasicImplData michael@0: #include "basic/BasicLayers.h" // for BasicLayerManager, etc michael@0: #include "gfx3DMatrix.h" // for gfx3DMatrix michael@0: #include "gfxASurface.h" // for gfxASurface, etc michael@0: #include "gfxCachedTempSurface.h" // for gfxCachedTempSurface michael@0: #include "gfxColor.h" // for gfxRGBA michael@0: #include "gfxContext.h" // for gfxContext, etc michael@0: #include "gfxImageSurface.h" // for gfxImageSurface michael@0: #include "gfxMatrix.h" // for gfxMatrix michael@0: #include "gfxPlatform.h" // for gfxPlatform michael@0: #include "gfxPrefs.h" // for gfxPrefs michael@0: #include "gfxPoint.h" // for gfxIntSize, gfxPoint michael@0: #include "gfxRect.h" // for gfxRect michael@0: #include "gfxUtils.h" // for gfxUtils michael@0: #include "gfx2DGlue.h" // for thebes --> moz2d transition michael@0: #include "mozilla/Assertions.h" // for MOZ_ASSERT, etc michael@0: #include "mozilla/WidgetUtils.h" // for ScreenRotation michael@0: #include "mozilla/gfx/2D.h" // for DrawTarget michael@0: #include "mozilla/gfx/BasePoint.h" // for BasePoint michael@0: #include "mozilla/gfx/BaseRect.h" // for BaseRect michael@0: #include "mozilla/gfx/Matrix.h" // for Matrix michael@0: #include "mozilla/gfx/Rect.h" // for IntRect, Rect michael@0: #include "mozilla/layers/LayersTypes.h" // for BufferMode::BUFFER_NONE, etc michael@0: #include "mozilla/mozalloc.h" // for operator new michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsCOMPtr.h" // for already_AddRefed michael@0: #include "nsDebug.h" // for NS_ASSERTION, etc michael@0: #include "nsISupportsImpl.h" // for gfxContext::Release, etc michael@0: #include "nsPoint.h" // for nsIntPoint michael@0: #include "nsRect.h" // for nsIntRect michael@0: #include "nsRegion.h" // for nsIntRegion, etc michael@0: #include "nsTArray.h" // for nsAutoTArray michael@0: #define PIXMAN_DONT_DEFINE_STDINT michael@0: #include "pixman.h" // for pixman_f_transform, etc michael@0: michael@0: class nsIWidget; michael@0: michael@0: using namespace mozilla::dom; michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: /** michael@0: * Clips to the smallest device-pixel-aligned rectangle containing aRect michael@0: * in user space. michael@0: * Returns true if the clip is "perfect", i.e. we actually clipped exactly to michael@0: * aRect. michael@0: */ michael@0: static bool michael@0: ClipToContain(gfxContext* aContext, const nsIntRect& aRect) michael@0: { michael@0: gfxRect userRect(aRect.x, aRect.y, aRect.width, aRect.height); michael@0: gfxRect deviceRect = aContext->UserToDevice(userRect); michael@0: deviceRect.RoundOut(); michael@0: michael@0: gfxMatrix currentMatrix = aContext->CurrentMatrix(); michael@0: aContext->IdentityMatrix(); michael@0: aContext->NewPath(); michael@0: aContext->Rectangle(deviceRect); michael@0: aContext->Clip(); michael@0: aContext->SetMatrix(currentMatrix); michael@0: michael@0: return aContext->DeviceToUser(deviceRect).IsEqualInterior(userRect); michael@0: } michael@0: michael@0: already_AddRefed michael@0: BasicLayerManager::PushGroupForLayer(gfxContext* aContext, Layer* aLayer, michael@0: const nsIntRegion& aRegion, michael@0: bool* aNeedsClipToVisibleRegion) michael@0: { michael@0: // If we need to call PushGroup, we should clip to the smallest possible michael@0: // area first to minimize the size of the temporary surface. michael@0: bool didCompleteClip = ClipToContain(aContext, aRegion.GetBounds()); michael@0: michael@0: nsRefPtr result; michael@0: if (aLayer->CanUseOpaqueSurface() && michael@0: ((didCompleteClip && aRegion.GetNumRects() == 1) || michael@0: !aContext->CurrentMatrix().HasNonIntegerTranslation())) { michael@0: // If the layer is opaque in its visible region we can push a gfxContentType::COLOR michael@0: // group. We need to make sure that only pixels inside the layer's visible michael@0: // region are copied back to the destination. Remember if we've already michael@0: // clipped precisely to the visible region. michael@0: *aNeedsClipToVisibleRegion = !didCompleteClip || aRegion.GetNumRects() > 1; michael@0: MOZ_ASSERT(!aContext->IsCairo()); michael@0: result = PushGroupWithCachedSurface(aContext, gfxContentType::COLOR); michael@0: } else { michael@0: *aNeedsClipToVisibleRegion = false; michael@0: result = aContext; michael@0: if (aLayer->GetContentFlags() & Layer::CONTENT_COMPONENT_ALPHA) { michael@0: aContext->PushGroupAndCopyBackground(gfxContentType::COLOR_ALPHA); michael@0: } else { michael@0: aContext->PushGroup(gfxContentType::COLOR_ALPHA); michael@0: } michael@0: } michael@0: return result.forget(); michael@0: } michael@0: michael@0: static nsIntRect michael@0: ToOutsideIntRect(const gfxRect &aRect) michael@0: { michael@0: gfxRect r = aRect; michael@0: r.RoundOut(); michael@0: return nsIntRect(r.X(), r.Y(), r.Width(), r.Height()); michael@0: } michael@0: michael@0: static nsIntRect michael@0: ToInsideIntRect(const gfxRect& aRect) michael@0: { michael@0: gfxRect r = aRect; michael@0: r.RoundIn(); michael@0: return nsIntRect(r.X(), r.Y(), r.Width(), r.Height()); michael@0: } michael@0: michael@0: // A context helper for BasicLayerManager::PaintLayer() that holds all the michael@0: // painting context together in a data structure so it can be easily passed michael@0: // around. It also uses ensures that the Transform and Opaque rect are restored michael@0: // to their former state on destruction. michael@0: michael@0: class PaintLayerContext { michael@0: public: michael@0: PaintLayerContext(gfxContext* aTarget, Layer* aLayer, michael@0: LayerManager::DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData, ReadbackProcessor* aReadback) michael@0: : mTarget(aTarget) michael@0: , mTargetMatrixSR(aTarget) michael@0: , mLayer(aLayer) michael@0: , mCallback(aCallback) michael@0: , mCallbackData(aCallbackData) michael@0: , mReadback(aReadback) michael@0: , mPushedOpaqueRect(false) michael@0: {} michael@0: michael@0: ~PaintLayerContext() michael@0: { michael@0: // Matrix is restored by mTargetMatrixSR michael@0: if (mPushedOpaqueRect) michael@0: { michael@0: ClearOpaqueRect(); michael@0: } michael@0: } michael@0: michael@0: // Gets the effective transform and returns true if it is a 2D michael@0: // transform. michael@0: bool Setup2DTransform() michael@0: { michael@0: // Will return an identity matrix for 3d transforms. michael@0: return mLayer->GetEffectiveTransform().CanDraw2D(&mTransform); michael@0: } michael@0: michael@0: // Applies the effective transform if it's 2D. If it's a 3D transform then michael@0: // it applies an identity. michael@0: void Apply2DTransform() michael@0: { michael@0: mTarget->SetMatrix(ThebesMatrix(mTransform)); michael@0: } michael@0: michael@0: // Set the opaque rect to match the bounds of the visible region. michael@0: void AnnotateOpaqueRect() michael@0: { michael@0: const nsIntRegion& visibleRegion = mLayer->GetEffectiveVisibleRegion(); michael@0: const nsIntRect& bounds = visibleRegion.GetBounds(); michael@0: michael@0: if (mTarget->IsCairo()) { michael@0: nsRefPtr currentSurface = mTarget->CurrentSurface(); michael@0: const gfxRect& targetOpaqueRect = currentSurface->GetOpaqueRect(); michael@0: michael@0: // Try to annotate currentSurface with a region of pixels that have been michael@0: // (or will be) painted opaque, if no such region is currently set. michael@0: if (targetOpaqueRect.IsEmpty() && visibleRegion.GetNumRects() == 1 && michael@0: (mLayer->GetContentFlags() & Layer::CONTENT_OPAQUE) && michael@0: !mTransform.HasNonAxisAlignedTransform()) { michael@0: currentSurface->SetOpaqueRect( michael@0: mTarget->UserToDevice(gfxRect(bounds.x, bounds.y, bounds.width, bounds.height))); michael@0: mPushedOpaqueRect = true; michael@0: } michael@0: } else { michael@0: DrawTarget *dt = mTarget->GetDrawTarget(); michael@0: const IntRect& targetOpaqueRect = dt->GetOpaqueRect(); michael@0: michael@0: // Try to annotate currentSurface with a region of pixels that have been michael@0: // (or will be) painted opaque, if no such region is currently set. michael@0: if (targetOpaqueRect.IsEmpty() && visibleRegion.GetNumRects() == 1 && michael@0: (mLayer->GetContentFlags() & Layer::CONTENT_OPAQUE) && michael@0: !mTransform.HasNonAxisAlignedTransform()) { michael@0: michael@0: gfx::Rect opaqueRect = dt->GetTransform().TransformBounds( michael@0: gfx::Rect(bounds.x, bounds.y, bounds.width, bounds.height)); michael@0: opaqueRect.RoundIn(); michael@0: IntRect intOpaqueRect; michael@0: if (opaqueRect.ToIntRect(&intOpaqueRect)) { michael@0: mTarget->GetDrawTarget()->SetOpaqueRect(intOpaqueRect); michael@0: mPushedOpaqueRect = true; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Clear the Opaque rect. Although this doesn't really restore it to it's michael@0: // previous state it will happen on the exit path of the PaintLayer() so when michael@0: // painting is complete the opaque rect qill be clear. michael@0: void ClearOpaqueRect() { michael@0: if (mTarget->IsCairo()) { michael@0: nsRefPtr currentSurface = mTarget->CurrentSurface(); michael@0: currentSurface->SetOpaqueRect(gfxRect()); michael@0: } else { michael@0: mTarget->GetDrawTarget()->SetOpaqueRect(IntRect()); michael@0: } michael@0: } michael@0: michael@0: gfxContext* mTarget; michael@0: gfxContextMatrixAutoSaveRestore mTargetMatrixSR; michael@0: Layer* mLayer; michael@0: LayerManager::DrawThebesLayerCallback mCallback; michael@0: void* mCallbackData; michael@0: ReadbackProcessor* mReadback; michael@0: Matrix mTransform; michael@0: bool mPushedOpaqueRect; michael@0: }; michael@0: michael@0: BasicLayerManager::BasicLayerManager(nsIWidget* aWidget) : michael@0: mPhase(PHASE_NONE), michael@0: mWidget(aWidget) michael@0: , mDoubleBuffering(BufferMode::BUFFER_NONE), mUsingDefaultTarget(false) michael@0: , mCachedSurfaceInUse(false) michael@0: , mTransactionIncomplete(false) michael@0: , mCompositorMightResample(false) michael@0: { michael@0: MOZ_COUNT_CTOR(BasicLayerManager); michael@0: NS_ASSERTION(aWidget, "Must provide a widget"); michael@0: } michael@0: michael@0: BasicLayerManager::BasicLayerManager() : michael@0: mPhase(PHASE_NONE), michael@0: mWidget(nullptr) michael@0: , mDoubleBuffering(BufferMode::BUFFER_NONE), mUsingDefaultTarget(false) michael@0: , mCachedSurfaceInUse(false) michael@0: , mTransactionIncomplete(false) michael@0: { michael@0: MOZ_COUNT_CTOR(BasicLayerManager); michael@0: } michael@0: michael@0: BasicLayerManager::~BasicLayerManager() michael@0: { michael@0: NS_ASSERTION(!InTransaction(), "Died during transaction?"); michael@0: michael@0: ClearCachedResources(); michael@0: michael@0: mRoot = nullptr; michael@0: michael@0: MOZ_COUNT_DTOR(BasicLayerManager); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::SetDefaultTarget(gfxContext* aContext) michael@0: { michael@0: NS_ASSERTION(!InTransaction(), michael@0: "Must set default target outside transaction"); michael@0: mDefaultTarget = aContext; michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::SetDefaultTargetConfiguration(BufferMode aDoubleBuffering, ScreenRotation aRotation) michael@0: { michael@0: mDoubleBuffering = aDoubleBuffering; michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::BeginTransaction() michael@0: { michael@0: mInTransaction = true; michael@0: mUsingDefaultTarget = true; michael@0: BeginTransactionWithTarget(mDefaultTarget); michael@0: } michael@0: michael@0: already_AddRefed michael@0: BasicLayerManager::PushGroupWithCachedSurface(gfxContext *aTarget, michael@0: gfxContentType aContent) michael@0: { michael@0: nsRefPtr ctx; michael@0: // We can't cache Azure DrawTargets at this point. michael@0: if (!mCachedSurfaceInUse && aTarget->IsCairo()) { michael@0: gfxContextMatrixAutoSaveRestore saveMatrix(aTarget); michael@0: aTarget->IdentityMatrix(); michael@0: michael@0: nsRefPtr currentSurf = aTarget->CurrentSurface(); michael@0: gfxRect clip = aTarget->GetClipExtents(); michael@0: clip.RoundOut(); michael@0: michael@0: ctx = mCachedSurface.Get(aContent, clip, currentSurf); michael@0: michael@0: if (ctx) { michael@0: mCachedSurfaceInUse = true; michael@0: /* Align our buffer for the original surface */ michael@0: ctx->SetMatrix(saveMatrix.Matrix()); michael@0: return ctx.forget(); michael@0: } michael@0: } michael@0: michael@0: ctx = aTarget; michael@0: ctx->PushGroup(aContent); michael@0: return ctx.forget(); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::PopGroupToSourceWithCachedSurface(gfxContext *aTarget, gfxContext *aPushed) michael@0: { michael@0: if (!aTarget) michael@0: return; michael@0: if (aTarget->IsCairo()) { michael@0: nsRefPtr current = aPushed->CurrentSurface(); michael@0: if (mCachedSurface.IsSurface(current)) { michael@0: gfxContextMatrixAutoSaveRestore saveMatrix(aTarget); michael@0: aTarget->IdentityMatrix(); michael@0: aTarget->SetSource(current); michael@0: mCachedSurfaceInUse = false; michael@0: return; michael@0: } michael@0: } michael@0: aTarget->PopGroupToSource(); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::BeginTransactionWithTarget(gfxContext* aTarget) michael@0: { michael@0: mInTransaction = true; michael@0: michael@0: #ifdef MOZ_LAYERS_HAVE_LOG michael@0: MOZ_LAYERS_LOG(("[----- BeginTransaction")); michael@0: Log(); michael@0: #endif michael@0: michael@0: NS_ASSERTION(!InTransaction(), "Nested transactions not allowed"); michael@0: mPhase = PHASE_CONSTRUCTION; michael@0: mTarget = aTarget; michael@0: } michael@0: michael@0: static void michael@0: TransformIntRect(nsIntRect& aRect, const Matrix& aMatrix, michael@0: nsIntRect (*aRoundMethod)(const gfxRect&)) michael@0: { michael@0: Rect gr = Rect(aRect.x, aRect.y, aRect.width, aRect.height); michael@0: gr = aMatrix.TransformBounds(gr); michael@0: aRect = (*aRoundMethod)(ThebesRect(gr)); michael@0: } michael@0: michael@0: /** michael@0: * This function assumes that GetEffectiveTransform transforms michael@0: * all layers to the same coordinate system (the "root coordinate system"). michael@0: * It can't be used as is by accelerated layers because of intermediate surfaces. michael@0: * This must set the hidden flag to true or false on *all* layers in the subtree. michael@0: * It also sets the operator for all layers to "OVER", and call michael@0: * SetDrawAtomically(false). michael@0: * It clears mClipToVisibleRegion on all layers. michael@0: * @param aClipRect the cliprect, in the root coordinate system. We assume michael@0: * that any layer drawing is clipped to this rect. It is therefore not michael@0: * allowed to add to the opaque region outside that rect. michael@0: * @param aDirtyRect the dirty rect that will be painted, in the root michael@0: * coordinate system. Layers outside this rect should be hidden. michael@0: * @param aOpaqueRegion the opaque region covering aLayer, in the michael@0: * root coordinate system. michael@0: */ michael@0: enum { michael@0: ALLOW_OPAQUE = 0x01, michael@0: }; michael@0: static void michael@0: MarkLayersHidden(Layer* aLayer, const nsIntRect& aClipRect, michael@0: const nsIntRect& aDirtyRect, michael@0: nsIntRegion& aOpaqueRegion, michael@0: uint32_t aFlags) michael@0: { michael@0: nsIntRect newClipRect(aClipRect); michael@0: uint32_t newFlags = aFlags; michael@0: michael@0: // Allow aLayer or aLayer's descendants to cover underlying layers michael@0: // only if it's opaque. michael@0: if (aLayer->GetOpacity() != 1.0f) { michael@0: newFlags &= ~ALLOW_OPAQUE; michael@0: } michael@0: michael@0: { michael@0: const nsIntRect* clipRect = aLayer->GetEffectiveClipRect(); michael@0: if (clipRect) { michael@0: nsIntRect cr = *clipRect; michael@0: // clipRect is in the container's coordinate system. Get it into the michael@0: // global coordinate system. michael@0: if (aLayer->GetParent()) { michael@0: Matrix tr; michael@0: if (aLayer->GetParent()->GetEffectiveTransform().CanDraw2D(&tr)) { michael@0: // Clip rect is applied after aLayer's transform, i.e., in the coordinate michael@0: // system of aLayer's parent. michael@0: TransformIntRect(cr, tr, ToInsideIntRect); michael@0: } else { michael@0: cr.SetRect(0, 0, 0, 0); michael@0: } michael@0: } michael@0: newClipRect.IntersectRect(newClipRect, cr); michael@0: } michael@0: } michael@0: michael@0: BasicImplData* data = ToData(aLayer); michael@0: data->SetOperator(CompositionOp::OP_OVER); michael@0: data->SetClipToVisibleRegion(false); michael@0: data->SetDrawAtomically(false); michael@0: michael@0: if (!aLayer->AsContainerLayer()) { michael@0: Matrix transform; michael@0: if (!aLayer->GetEffectiveTransform().CanDraw2D(&transform)) { michael@0: data->SetHidden(false); michael@0: return; michael@0: } michael@0: michael@0: nsIntRegion region = aLayer->GetEffectiveVisibleRegion(); michael@0: nsIntRect r = region.GetBounds(); michael@0: TransformIntRect(r, transform, ToOutsideIntRect); michael@0: r.IntersectRect(r, aDirtyRect); michael@0: data->SetHidden(aOpaqueRegion.Contains(r)); michael@0: michael@0: // Allow aLayer to cover underlying layers only if aLayer's michael@0: // content is opaque michael@0: if ((aLayer->GetContentFlags() & Layer::CONTENT_OPAQUE) && michael@0: (newFlags & ALLOW_OPAQUE)) { michael@0: nsIntRegionRectIterator it(region); michael@0: while (const nsIntRect* sr = it.Next()) { michael@0: r = *sr; michael@0: TransformIntRect(r, transform, ToInsideIntRect); michael@0: michael@0: r.IntersectRect(r, newClipRect); michael@0: aOpaqueRegion.Or(aOpaqueRegion, r); michael@0: } michael@0: } michael@0: } else { michael@0: Layer* child = aLayer->GetLastChild(); michael@0: bool allHidden = true; michael@0: for (; child; child = child->GetPrevSibling()) { michael@0: MarkLayersHidden(child, newClipRect, aDirtyRect, aOpaqueRegion, newFlags); michael@0: if (!ToData(child)->IsHidden()) { michael@0: allHidden = false; michael@0: } michael@0: } michael@0: data->SetHidden(allHidden); michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * This function assumes that GetEffectiveTransform transforms michael@0: * all layers to the same coordinate system (the "root coordinate system"). michael@0: * MarkLayersHidden must be called before calling this. michael@0: * @param aVisibleRect the rectangle of aLayer that is visible (i.e. not michael@0: * clipped and in the dirty rect), in the root coordinate system. michael@0: */ michael@0: static void michael@0: ApplyDoubleBuffering(Layer* aLayer, const nsIntRect& aVisibleRect) michael@0: { michael@0: BasicImplData* data = ToData(aLayer); michael@0: if (data->IsHidden()) michael@0: return; michael@0: michael@0: nsIntRect newVisibleRect(aVisibleRect); michael@0: michael@0: { michael@0: const nsIntRect* clipRect = aLayer->GetEffectiveClipRect(); michael@0: if (clipRect) { michael@0: nsIntRect cr = *clipRect; michael@0: // clipRect is in the container's coordinate system. Get it into the michael@0: // global coordinate system. michael@0: if (aLayer->GetParent()) { michael@0: Matrix tr; michael@0: if (aLayer->GetParent()->GetEffectiveTransform().CanDraw2D(&tr)) { michael@0: NS_ASSERTION(!ThebesMatrix(tr).HasNonIntegerTranslation(), michael@0: "Parent can only have an integer translation"); michael@0: cr += nsIntPoint(int32_t(tr._31), int32_t(tr._32)); michael@0: } else { michael@0: NS_ERROR("Parent can only have an integer translation"); michael@0: } michael@0: } michael@0: newVisibleRect.IntersectRect(newVisibleRect, cr); michael@0: } michael@0: } michael@0: michael@0: BasicContainerLayer* container = michael@0: static_cast(aLayer->AsContainerLayer()); michael@0: // Layers that act as their own backbuffers should be drawn to the destination michael@0: // using OPERATOR_SOURCE to ensure that alpha values in a transparent window michael@0: // are cleared. This can also be faster than OPERATOR_OVER. michael@0: if (!container) { michael@0: data->SetOperator(CompositionOp::OP_SOURCE); michael@0: data->SetDrawAtomically(true); michael@0: } else { michael@0: if (container->UseIntermediateSurface() || michael@0: !container->ChildrenPartitionVisibleRegion(newVisibleRect)) { michael@0: // We need to double-buffer this container. michael@0: data->SetOperator(CompositionOp::OP_SOURCE); michael@0: container->ForceIntermediateSurface(); michael@0: } else { michael@0: // Tell the children to clip to their visible regions so our assumption michael@0: // that they don't paint outside their visible regions is valid! michael@0: for (Layer* child = aLayer->GetFirstChild(); child; michael@0: child = child->GetNextSibling()) { michael@0: ToData(child)->SetClipToVisibleRegion(true); michael@0: ApplyDoubleBuffering(child, newVisibleRect); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::EndTransaction(DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData, michael@0: EndTransactionFlags aFlags) michael@0: { michael@0: mInTransaction = false; michael@0: michael@0: EndTransactionInternal(aCallback, aCallbackData, aFlags); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::AbortTransaction() michael@0: { michael@0: NS_ASSERTION(InConstruction(), "Should be in construction phase"); michael@0: mPhase = PHASE_NONE; michael@0: mUsingDefaultTarget = false; michael@0: mInTransaction = false; michael@0: } michael@0: michael@0: static uint16_t sFrameCount = 0; michael@0: void michael@0: BasicLayerManager::RenderDebugOverlay() michael@0: { michael@0: if (!gfxPrefs::DrawFrameCounter()) { michael@0: return; michael@0: } michael@0: michael@0: profiler_set_frame_number(sFrameCount); michael@0: michael@0: uint16_t frameNumber = sFrameCount; michael@0: const uint16_t bitWidth = 3; michael@0: for (size_t i = 0; i < 16; i++) { michael@0: michael@0: gfxRGBA bitColor; michael@0: if ((frameNumber >> i) & 0x1) { michael@0: bitColor = gfxRGBA(0, 0, 0, 1.0); michael@0: } else { michael@0: bitColor = gfxRGBA(1.0, 1.0, 1.0, 1.0); michael@0: } michael@0: mTarget->NewPath(); michael@0: mTarget->SetColor(bitColor); michael@0: mTarget->Rectangle(gfxRect(bitWidth*i, 0, bitWidth, bitWidth)); michael@0: mTarget->Fill(); michael@0: } michael@0: // We intentionally overflow at 2^16. michael@0: sFrameCount++; michael@0: } michael@0: michael@0: bool michael@0: BasicLayerManager::EndTransactionInternal(DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData, michael@0: EndTransactionFlags aFlags) michael@0: { michael@0: PROFILER_LABEL("BasicLayerManager", "EndTransactionInternal"); michael@0: #ifdef MOZ_LAYERS_HAVE_LOG michael@0: MOZ_LAYERS_LOG((" ----- (beginning paint)")); michael@0: Log(); michael@0: #endif michael@0: michael@0: NS_ASSERTION(InConstruction(), "Should be in construction phase"); michael@0: mPhase = PHASE_DRAWING; michael@0: michael@0: RenderTraceLayers(mRoot, "FF00"); michael@0: michael@0: mTransactionIncomplete = false; michael@0: michael@0: if (mRoot) { michael@0: // Need to do this before we call ApplyDoubleBuffering, michael@0: // which depends on correct effective transforms michael@0: mSnapEffectiveTransforms = michael@0: mTarget ? !(mTarget->GetFlags() & gfxContext::FLAG_DISABLE_SNAPPING) : true; michael@0: mRoot->ComputeEffectiveTransforms(mTarget ? Matrix4x4::From2D(ToMatrix(mTarget->CurrentMatrix())) : Matrix4x4()); michael@0: michael@0: ToData(mRoot)->Validate(aCallback, aCallbackData); michael@0: if (mRoot->GetMaskLayer()) { michael@0: ToData(mRoot->GetMaskLayer())->Validate(aCallback, aCallbackData); michael@0: } michael@0: michael@0: if (aFlags & END_NO_COMPOSITE) { michael@0: // Apply pending tree updates before recomputing effective michael@0: // properties. michael@0: mRoot->ApplyPendingUpdatesToSubtree(); michael@0: } michael@0: } michael@0: michael@0: if (mTarget && mRoot && michael@0: !(aFlags & END_NO_IMMEDIATE_REDRAW) && michael@0: !(aFlags & END_NO_COMPOSITE)) { michael@0: nsIntRect clipRect; michael@0: michael@0: { michael@0: gfxContextMatrixAutoSaveRestore save(mTarget); michael@0: mTarget->SetMatrix(gfxMatrix()); michael@0: clipRect = ToOutsideIntRect(mTarget->GetClipExtents()); michael@0: } michael@0: michael@0: if (IsRetained()) { michael@0: nsIntRegion region; michael@0: MarkLayersHidden(mRoot, clipRect, clipRect, region, ALLOW_OPAQUE); michael@0: if (mUsingDefaultTarget && mDoubleBuffering != BufferMode::BUFFER_NONE) { michael@0: ApplyDoubleBuffering(mRoot, clipRect); michael@0: } michael@0: } michael@0: michael@0: PaintLayer(mTarget, mRoot, aCallback, aCallbackData, nullptr); michael@0: if (!mRegionToClear.IsEmpty()) { michael@0: AutoSetOperator op(mTarget, gfxContext::OPERATOR_CLEAR); michael@0: nsIntRegionRectIterator iter(mRegionToClear); michael@0: const nsIntRect *r; michael@0: while ((r = iter.Next())) { michael@0: mTarget->NewPath(); michael@0: mTarget->Rectangle(gfxRect(r->x, r->y, r->width, r->height)); michael@0: mTarget->Fill(); michael@0: } michael@0: } michael@0: if (mWidget) { michael@0: FlashWidgetUpdateArea(mTarget); michael@0: } michael@0: RenderDebugOverlay(); michael@0: RecordFrame(); michael@0: PostPresent(); michael@0: michael@0: if (!mTransactionIncomplete) { michael@0: // Clear out target if we have a complete transaction. michael@0: mTarget = nullptr; michael@0: } michael@0: } michael@0: michael@0: #ifdef MOZ_LAYERS_HAVE_LOG michael@0: Log(); michael@0: MOZ_LAYERS_LOG(("]----- EndTransaction")); michael@0: #endif michael@0: michael@0: // Go back to the construction phase if the transaction isn't complete. michael@0: // Layout will update the layer tree and call EndTransaction(). michael@0: mPhase = mTransactionIncomplete ? PHASE_CONSTRUCTION : PHASE_NONE; michael@0: michael@0: if (!mTransactionIncomplete) { michael@0: // This is still valid if the transaction was incomplete. michael@0: mUsingDefaultTarget = false; michael@0: } michael@0: michael@0: NS_ASSERTION(!aCallback || !mTransactionIncomplete, michael@0: "If callback is not null, transaction must be complete"); michael@0: michael@0: // XXX - We should probably assert here that for an incomplete transaction michael@0: // out target is the default target. michael@0: michael@0: return !mTransactionIncomplete; michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::FlashWidgetUpdateArea(gfxContext *aContext) michael@0: { michael@0: if (gfxPrefs::WidgetUpdateFlashing()) { michael@0: float r = float(rand()) / RAND_MAX; michael@0: float g = float(rand()) / RAND_MAX; michael@0: float b = float(rand()) / RAND_MAX; michael@0: aContext->SetColor(gfxRGBA(r, g, b, 0.2)); michael@0: aContext->Paint(); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: BasicLayerManager::EndEmptyTransaction(EndTransactionFlags aFlags) michael@0: { michael@0: mInTransaction = false; michael@0: michael@0: if (!mRoot) { michael@0: return false; michael@0: } michael@0: michael@0: return EndTransactionInternal(nullptr, nullptr, aFlags); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::SetRoot(Layer* aLayer) michael@0: { michael@0: NS_ASSERTION(aLayer, "Root can't be null"); michael@0: NS_ASSERTION(aLayer->Manager() == this, "Wrong manager"); michael@0: NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); michael@0: mRoot = aLayer; michael@0: } michael@0: michael@0: static pixman_transform michael@0: BasicLayerManager_Matrix3DToPixman(const gfx3DMatrix& aMatrix) michael@0: { michael@0: pixman_f_transform transform; michael@0: michael@0: transform.m[0][0] = aMatrix._11; michael@0: transform.m[0][1] = aMatrix._21; michael@0: transform.m[0][2] = aMatrix._41; michael@0: transform.m[1][0] = aMatrix._12; michael@0: transform.m[1][1] = aMatrix._22; michael@0: transform.m[1][2] = aMatrix._42; michael@0: transform.m[2][0] = aMatrix._14; michael@0: transform.m[2][1] = aMatrix._24; michael@0: transform.m[2][2] = aMatrix._44; michael@0: michael@0: pixman_transform result; michael@0: pixman_transform_from_pixman_f_transform(&result, &transform); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static void michael@0: PixmanTransform(const gfxImageSurface* aDest, michael@0: RefPtr aSrc, michael@0: const gfx3DMatrix& aTransform, michael@0: gfxPoint aDestOffset) michael@0: { michael@0: IntSize destSize = ToIntSize(aDest->GetSize()); michael@0: pixman_image_t* dest = pixman_image_create_bits(aDest->Format() == gfxImageFormat::ARGB32 ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8, michael@0: destSize.width, michael@0: destSize.height, michael@0: (uint32_t*)aDest->Data(), michael@0: aDest->Stride()); michael@0: michael@0: IntSize srcSize = aSrc->GetSize(); michael@0: pixman_image_t* src = pixman_image_create_bits(aSrc->GetFormat() == SurfaceFormat::B8G8R8A8 ? PIXMAN_a8r8g8b8 : PIXMAN_x8r8g8b8, michael@0: srcSize.width, michael@0: srcSize.height, michael@0: (uint32_t*)aSrc->GetData(), michael@0: aSrc->Stride()); michael@0: michael@0: NS_ABORT_IF_FALSE(src && dest, "Failed to create pixman images?"); michael@0: michael@0: pixman_transform pixTransform = BasicLayerManager_Matrix3DToPixman(aTransform); michael@0: pixman_transform pixTransformInverted; michael@0: michael@0: // If the transform is singular then nothing would be drawn anyway, return here michael@0: if (!pixman_transform_invert(&pixTransformInverted, &pixTransform)) { michael@0: return; michael@0: } michael@0: pixman_image_set_transform(src, &pixTransformInverted); michael@0: michael@0: pixman_image_composite32(PIXMAN_OP_SRC, michael@0: src, michael@0: nullptr, michael@0: dest, michael@0: aDestOffset.x, michael@0: aDestOffset.y, michael@0: 0, michael@0: 0, michael@0: 0, michael@0: 0, michael@0: destSize.width, michael@0: destSize.height); michael@0: michael@0: pixman_image_unref(dest); michael@0: pixman_image_unref(src); michael@0: } michael@0: michael@0: /** michael@0: * Transform a surface using a gfx3DMatrix and blit to the destination if michael@0: * it is efficient to do so. michael@0: * michael@0: * @param aSource Source surface. michael@0: * @param aDest Desintation context. michael@0: * @param aBounds Area represented by aSource. michael@0: * @param aTransform Transformation matrix. michael@0: * @param aDestRect Output: rectangle in which to draw returned surface on aDest michael@0: * (same size as aDest). Only filled in if this returns michael@0: * a surface. michael@0: * @return Transformed surface michael@0: */ michael@0: static already_AddRefed michael@0: Transform3D(RefPtr aSource, michael@0: gfxContext* aDest, michael@0: const gfxRect& aBounds, michael@0: const gfx3DMatrix& aTransform, michael@0: gfxRect& aDestRect) michael@0: { michael@0: // Find the transformed rectangle of our layer. michael@0: gfxRect offsetRect = aTransform.TransformBounds(aBounds); michael@0: michael@0: // Intersect the transformed layer with the destination rectangle. michael@0: // This is in device space since we have an identity transform set on aTarget. michael@0: aDestRect = aDest->GetClipExtents(); michael@0: aDestRect.IntersectRect(aDestRect, offsetRect); michael@0: aDestRect.RoundOut(); michael@0: michael@0: // Create a surface the size of the transformed object. michael@0: nsRefPtr dest = aDest->CurrentSurface(); michael@0: nsRefPtr destImage = new gfxImageSurface(gfxIntSize(aDestRect.width, michael@0: aDestRect.height), michael@0: gfxImageFormat::ARGB32); michael@0: gfxPoint offset = aDestRect.TopLeft(); michael@0: michael@0: // Include a translation to the correct origin. michael@0: gfx3DMatrix translation = gfx3DMatrix::Translation(aBounds.x, aBounds.y, 0); michael@0: michael@0: // Transform the content and offset it such that the content begins at the origin. michael@0: PixmanTransform(destImage, aSource->GetDataSurface(), translation * aTransform, offset); michael@0: michael@0: // If we haven't actually drawn to aDest then return our temporary image so michael@0: // that the caller can do this. michael@0: return destImage.forget(); michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::PaintSelfOrChildren(PaintLayerContext& aPaintContext, michael@0: gfxContext* aGroupTarget) michael@0: { michael@0: BasicImplData* data = ToData(aPaintContext.mLayer); michael@0: michael@0: /* Only paint ourself, or our children - This optimization relies on this! */ michael@0: Layer* child = aPaintContext.mLayer->GetFirstChild(); michael@0: if (!child) { michael@0: if (aPaintContext.mLayer->AsThebesLayer()) { michael@0: data->PaintThebes(aGroupTarget, aPaintContext.mLayer->GetMaskLayer(), michael@0: aPaintContext.mCallback, aPaintContext.mCallbackData, michael@0: aPaintContext.mReadback); michael@0: } else { michael@0: data->Paint(aGroupTarget->GetDrawTarget(), michael@0: aGroupTarget->GetDeviceOffset(), michael@0: aPaintContext.mLayer->GetMaskLayer()); michael@0: } michael@0: } else { michael@0: ReadbackProcessor readback; michael@0: ContainerLayer* container = michael@0: static_cast(aPaintContext.mLayer); michael@0: if (IsRetained()) { michael@0: readback.BuildUpdates(container); michael@0: } michael@0: nsAutoTArray children; michael@0: container->SortChildrenBy3DZOrder(children); michael@0: for (uint32_t i = 0; i < children.Length(); i++) { michael@0: PaintLayer(aGroupTarget, children.ElementAt(i), aPaintContext.mCallback, michael@0: aPaintContext.mCallbackData, &readback); michael@0: if (mTransactionIncomplete) michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::FlushGroup(PaintLayerContext& aPaintContext, bool aNeedsClipToVisibleRegion) michael@0: { michael@0: // If we're doing our own double-buffering, we need to avoid drawing michael@0: // the results of an incomplete transaction to the destination surface --- michael@0: // that could cause flicker. Double-buffering is implemented using a michael@0: // temporary surface for one or more container layers, so we need to stop michael@0: // those temporary surfaces from being composited to aTarget. michael@0: // ApplyDoubleBuffering guarantees that this container layer can't michael@0: // intersect any other leaf layers, so if the transaction is not yet marked michael@0: // incomplete, the contents of this container layer are the final contents michael@0: // for the window. michael@0: if (!mTransactionIncomplete) { michael@0: if (aNeedsClipToVisibleRegion) { michael@0: gfxUtils::ClipToRegion(aPaintContext.mTarget, michael@0: aPaintContext.mLayer->GetEffectiveVisibleRegion()); michael@0: } michael@0: michael@0: CompositionOp op = GetEffectiveOperator(aPaintContext.mLayer); michael@0: AutoSetOperator setOperator(aPaintContext.mTarget, ThebesOp(op)); michael@0: michael@0: PaintWithMask(aPaintContext.mTarget, aPaintContext.mLayer->GetEffectiveOpacity(), michael@0: aPaintContext.mLayer->GetMaskLayer()); michael@0: } michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::PaintLayer(gfxContext* aTarget, michael@0: Layer* aLayer, michael@0: DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData, michael@0: ReadbackProcessor* aReadback) michael@0: { michael@0: PROFILER_LABEL("BasicLayerManager", "PaintLayer"); michael@0: PaintLayerContext paintLayerContext(aTarget, aLayer, aCallback, aCallbackData, aReadback); michael@0: michael@0: // Don't attempt to paint layers with a singular transform, cairo will michael@0: // just throw an error. michael@0: if (aLayer->GetEffectiveTransform().IsSingular()) { michael@0: return; michael@0: } michael@0: michael@0: RenderTraceScope trace("BasicLayerManager::PaintLayer", "707070"); michael@0: michael@0: const nsIntRect* clipRect = aLayer->GetEffectiveClipRect(); michael@0: BasicContainerLayer* container = michael@0: static_cast(aLayer->AsContainerLayer()); michael@0: bool needsGroup = container && michael@0: container->UseIntermediateSurface(); michael@0: BasicImplData* data = ToData(aLayer); michael@0: bool needsClipToVisibleRegion = michael@0: data->GetClipToVisibleRegion() && !aLayer->AsThebesLayer(); michael@0: NS_ASSERTION(needsGroup || !container || michael@0: container->GetOperator() == CompositionOp::OP_OVER, michael@0: "non-OVER operator should have forced UseIntermediateSurface"); michael@0: NS_ASSERTION(!container || !aLayer->GetMaskLayer() || michael@0: container->UseIntermediateSurface(), michael@0: "ContainerLayer with mask layer should force UseIntermediateSurface"); michael@0: michael@0: gfxContextAutoSaveRestore contextSR; michael@0: gfxMatrix transform; michael@0: // Will return an identity matrix for 3d transforms, and is handled separately below. michael@0: bool is2D = paintLayerContext.Setup2DTransform(); michael@0: NS_ABORT_IF_FALSE(is2D || needsGroup || !aLayer->GetFirstChild(), "Must PushGroup for 3d transforms!"); michael@0: michael@0: bool needsSaveRestore = michael@0: needsGroup || clipRect || needsClipToVisibleRegion || !is2D; michael@0: if (needsSaveRestore) { michael@0: contextSR.SetContext(aTarget); michael@0: michael@0: if (clipRect) { michael@0: aTarget->NewPath(); michael@0: aTarget->SnappedRectangle(gfxRect(clipRect->x, clipRect->y, clipRect->width, clipRect->height)); michael@0: aTarget->Clip(); michael@0: } michael@0: } michael@0: michael@0: paintLayerContext.Apply2DTransform(); michael@0: michael@0: const nsIntRegion& visibleRegion = aLayer->GetEffectiveVisibleRegion(); michael@0: // If needsGroup is true, we'll clip to the visible region after we've popped the group michael@0: if (needsClipToVisibleRegion && !needsGroup) { michael@0: gfxUtils::ClipToRegion(aTarget, visibleRegion); michael@0: // Don't need to clip to visible region again michael@0: needsClipToVisibleRegion = false; michael@0: } michael@0: michael@0: if (is2D) { michael@0: paintLayerContext.AnnotateOpaqueRect(); michael@0: } michael@0: michael@0: bool clipIsEmpty = !aTarget || aTarget->GetClipExtents().IsEmpty(); michael@0: if (clipIsEmpty) { michael@0: PaintSelfOrChildren(paintLayerContext, aTarget); michael@0: return; michael@0: } michael@0: michael@0: if (is2D) { michael@0: if (needsGroup) { michael@0: nsRefPtr groupTarget = PushGroupForLayer(aTarget, aLayer, aLayer->GetEffectiveVisibleRegion(), michael@0: &needsClipToVisibleRegion); michael@0: PaintSelfOrChildren(paintLayerContext, groupTarget); michael@0: PopGroupToSourceWithCachedSurface(aTarget, groupTarget); michael@0: FlushGroup(paintLayerContext, needsClipToVisibleRegion); michael@0: } else { michael@0: PaintSelfOrChildren(paintLayerContext, aTarget); michael@0: } michael@0: } else { michael@0: const nsIntRect& bounds = visibleRegion.GetBounds(); michael@0: RefPtr untransformedDT = michael@0: gfxPlatform::GetPlatform()->CreateOffscreenContentDrawTarget(IntSize(bounds.width, bounds.height), michael@0: SurfaceFormat::B8G8R8A8); michael@0: if (!untransformedDT) { michael@0: return; michael@0: } michael@0: michael@0: nsRefPtr groupTarget = new gfxContext(untransformedDT, michael@0: Point(bounds.x, bounds.y)); michael@0: michael@0: PaintSelfOrChildren(paintLayerContext, groupTarget); michael@0: michael@0: // Temporary fast fix for bug 725886 michael@0: // Revert these changes when 725886 is ready michael@0: NS_ABORT_IF_FALSE(untransformedDT, michael@0: "We should always allocate an untransformed surface with 3d transforms!"); michael@0: gfxRect destRect; michael@0: #ifdef DEBUG michael@0: if (aLayer->GetDebugColorIndex() != 0) { michael@0: gfxRGBA color((aLayer->GetDebugColorIndex() & 1) ? 1.0 : 0.0, michael@0: (aLayer->GetDebugColorIndex() & 2) ? 1.0 : 0.0, michael@0: (aLayer->GetDebugColorIndex() & 4) ? 1.0 : 0.0, michael@0: 1.0); michael@0: michael@0: nsRefPtr temp = new gfxContext(untransformedDT, Point(bounds.x, bounds.y)); michael@0: temp->SetColor(color); michael@0: temp->Paint(); michael@0: } michael@0: #endif michael@0: gfx3DMatrix effectiveTransform; michael@0: gfx::To3DMatrix(aLayer->GetEffectiveTransform(), effectiveTransform); michael@0: nsRefPtr result = michael@0: Transform3D(untransformedDT->Snapshot(), aTarget, bounds, michael@0: effectiveTransform, destRect); michael@0: michael@0: if (result) { michael@0: aTarget->SetSource(result, destRect.TopLeft()); michael@0: // Azure doesn't support EXTEND_NONE, so to avoid extending the edges michael@0: // of the source surface out to the current clip region, clip to michael@0: // the rectangle of the result surface now. michael@0: aTarget->NewPath(); michael@0: aTarget->SnappedRectangle(destRect); michael@0: aTarget->Clip(); michael@0: FlushGroup(paintLayerContext, needsClipToVisibleRegion); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: BasicLayerManager::ClearCachedResources(Layer* aSubtree) michael@0: { michael@0: MOZ_ASSERT(!aSubtree || aSubtree->Manager() == this); michael@0: if (aSubtree) { michael@0: ClearLayer(aSubtree); michael@0: } else if (mRoot) { michael@0: ClearLayer(mRoot); michael@0: } michael@0: mCachedSurface.Expire(); michael@0: } michael@0: void michael@0: BasicLayerManager::ClearLayer(Layer* aLayer) michael@0: { michael@0: ToData(aLayer)->ClearCachedResources(); michael@0: for (Layer* child = aLayer->GetFirstChild(); child; michael@0: child = child->GetNextSibling()) { michael@0: ClearLayer(child); michael@0: } michael@0: } michael@0: michael@0: already_AddRefed michael@0: BasicLayerManager::CreateReadbackLayer() michael@0: { michael@0: NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); michael@0: nsRefPtr layer = new BasicReadbackLayer(this); michael@0: return layer.forget(); michael@0: } michael@0: michael@0: } michael@0: }