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 "BasicThebesLayer.h" michael@0: #include // for uint32_t michael@0: #include "GeckoProfiler.h" // for PROFILER_LABEL michael@0: #include "ReadbackLayer.h" // for ReadbackLayer, ReadbackSink michael@0: #include "ReadbackProcessor.h" // for ReadbackProcessor::Update, etc michael@0: #include "RenderTrace.h" // for RenderTraceInvalidateEnd, etc michael@0: #include "BasicLayersImpl.h" // for AutoMaskData, etc michael@0: #include "gfxContext.h" // for gfxContext, etc michael@0: #include "gfxRect.h" // for gfxRect michael@0: #include "gfxUtils.h" // for gfxUtils michael@0: #include "mozilla/gfx/2D.h" // for DrawTarget 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 Rect, IntRect michael@0: #include "mozilla/gfx/Types.h" // for Float, etc michael@0: #include "mozilla/layers/LayersTypes.h" michael@0: #include "nsAutoPtr.h" // for nsRefPtr michael@0: #include "nsCOMPtr.h" // for already_AddRefed 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 "nsTArray.h" // for nsTArray, nsTArray_Impl michael@0: #include "AutoMaskData.h" michael@0: #include "gfx2DGlue.h" michael@0: michael@0: using namespace mozilla::gfx; michael@0: michael@0: namespace mozilla { michael@0: namespace layers { michael@0: michael@0: static nsIntRegion michael@0: IntersectWithClip(const nsIntRegion& aRegion, gfxContext* aContext) michael@0: { michael@0: gfxRect clip = aContext->GetClipExtents(); michael@0: clip.RoundOut(); michael@0: nsIntRect r(clip.X(), clip.Y(), clip.Width(), clip.Height()); michael@0: nsIntRegion result; michael@0: result.And(aRegion, r); michael@0: return result; michael@0: } michael@0: michael@0: void michael@0: BasicThebesLayer::PaintThebes(gfxContext* aContext, michael@0: Layer* aMaskLayer, michael@0: LayerManager::DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData, michael@0: ReadbackProcessor* aReadback) michael@0: { michael@0: PROFILER_LABEL("BasicThebesLayer", "PaintThebes"); michael@0: NS_ASSERTION(BasicManager()->InDrawing(), michael@0: "Can only draw in drawing phase"); michael@0: michael@0: nsTArray readbackUpdates; michael@0: if (aReadback && UsedForReadback()) { michael@0: aReadback->GetThebesLayerUpdates(this, &readbackUpdates); michael@0: } michael@0: michael@0: float opacity = GetEffectiveOpacity(); michael@0: CompositionOp effectiveOperator = GetEffectiveOperator(this); michael@0: michael@0: if (!BasicManager()->IsRetained()) { michael@0: NS_ASSERTION(readbackUpdates.IsEmpty(), "Can't do readback for non-retained layer"); michael@0: michael@0: mValidRegion.SetEmpty(); michael@0: mContentClient->Clear(); michael@0: michael@0: nsIntRegion toDraw = IntersectWithClip(GetEffectiveVisibleRegion(), aContext); michael@0: michael@0: RenderTraceInvalidateStart(this, "FFFF00", toDraw.GetBounds()); michael@0: michael@0: if (!toDraw.IsEmpty() && !IsHidden()) { michael@0: if (!aCallback) { michael@0: BasicManager()->SetTransactionIncomplete(); michael@0: return; michael@0: } michael@0: michael@0: aContext->Save(); michael@0: michael@0: bool needsClipToVisibleRegion = GetClipToVisibleRegion(); michael@0: bool needsGroup = opacity != 1.0 || michael@0: effectiveOperator != CompositionOp::OP_OVER || michael@0: aMaskLayer; michael@0: nsRefPtr groupContext; michael@0: if (needsGroup) { michael@0: groupContext = michael@0: BasicManager()->PushGroupForLayer(aContext, this, toDraw, michael@0: &needsClipToVisibleRegion); michael@0: if (effectiveOperator != CompositionOp::OP_OVER) { michael@0: needsClipToVisibleRegion = true; michael@0: } michael@0: } else { michael@0: groupContext = aContext; michael@0: } michael@0: SetAntialiasingFlags(this, groupContext); michael@0: aCallback(this, groupContext, toDraw, DrawRegionClip::CLIP_NONE, nsIntRegion(), aCallbackData); michael@0: if (needsGroup) { michael@0: BasicManager()->PopGroupToSourceWithCachedSurface(aContext, groupContext); michael@0: if (needsClipToVisibleRegion) { michael@0: gfxUtils::ClipToRegion(aContext, toDraw); michael@0: } michael@0: AutoSetOperator setOptimizedOperator(aContext, ThebesOp(effectiveOperator)); michael@0: PaintWithMask(aContext, opacity, aMaskLayer); michael@0: } michael@0: michael@0: aContext->Restore(); michael@0: } michael@0: michael@0: RenderTraceInvalidateEnd(this, "FFFF00"); michael@0: return; michael@0: } michael@0: michael@0: if (BasicManager()->IsTransactionIncomplete()) michael@0: return; michael@0: michael@0: gfxRect clipExtents; michael@0: clipExtents = aContext->GetClipExtents(); michael@0: michael@0: // Pull out the mask surface and transform here, because the mask michael@0: // is internal to basic layers michael@0: AutoMoz2DMaskData mask; michael@0: SourceSurface* maskSurface = nullptr; michael@0: Matrix maskTransform; michael@0: if (GetMaskData(aMaskLayer, Point(), &mask)) { michael@0: maskSurface = mask.GetSurface(); michael@0: maskTransform = mask.GetTransform(); michael@0: } michael@0: michael@0: if (!IsHidden() && !clipExtents.IsEmpty()) { michael@0: mContentClient->DrawTo(this, aContext->GetDrawTarget(), opacity, michael@0: GetOperator(), michael@0: maskSurface, &maskTransform); michael@0: } michael@0: michael@0: for (uint32_t i = 0; i < readbackUpdates.Length(); ++i) { michael@0: ReadbackProcessor::Update& update = readbackUpdates[i]; michael@0: nsIntPoint offset = update.mLayer->GetBackgroundLayerOffset(); michael@0: nsRefPtr ctx = michael@0: update.mLayer->GetSink()->BeginUpdate(update.mUpdateRect + offset, michael@0: update.mSequenceCounter); michael@0: if (ctx) { michael@0: NS_ASSERTION(opacity == 1.0, "Should only read back opaque layers"); michael@0: ctx->Translate(gfxPoint(offset.x, offset.y)); michael@0: mContentClient->DrawTo(this, ctx->GetDrawTarget(), 1.0, michael@0: CompositionOpForOp(ctx->CurrentOperator()), michael@0: maskSurface, &maskTransform); michael@0: update.mLayer->GetSink()->EndUpdate(ctx, update.mUpdateRect + offset); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: BasicThebesLayer::Validate(LayerManager::DrawThebesLayerCallback aCallback, michael@0: void* aCallbackData) michael@0: { michael@0: if (!mContentClient) { michael@0: // This client will have a null Forwarder, which means it will not have michael@0: // a ContentHost on the other side. michael@0: mContentClient = new ContentClientBasic(); michael@0: } michael@0: michael@0: if (!BasicManager()->IsRetained()) { michael@0: return; michael@0: } michael@0: michael@0: uint32_t flags = 0; michael@0: #ifndef MOZ_WIDGET_ANDROID michael@0: if (BasicManager()->CompositorMightResample()) { michael@0: flags |= RotatedContentBuffer::PAINT_WILL_RESAMPLE; michael@0: } michael@0: if (!(flags & RotatedContentBuffer::PAINT_WILL_RESAMPLE)) { michael@0: if (MayResample()) { michael@0: flags |= RotatedContentBuffer::PAINT_WILL_RESAMPLE; michael@0: } michael@0: } michael@0: #endif michael@0: if (mDrawAtomically) { michael@0: flags |= RotatedContentBuffer::PAINT_NO_ROTATION; michael@0: } michael@0: PaintState state = michael@0: mContentClient->BeginPaintBuffer(this, flags); michael@0: mValidRegion.Sub(mValidRegion, state.mRegionToInvalidate); michael@0: michael@0: if (DrawTarget* target = mContentClient->BorrowDrawTargetForPainting(state)) { michael@0: // The area that became invalid and is visible needs to be repainted michael@0: // (this could be the whole visible area if our buffer switched michael@0: // from RGB to RGBA, because we might need to repaint with michael@0: // subpixel AA) michael@0: state.mRegionToInvalidate.And(state.mRegionToInvalidate, michael@0: GetEffectiveVisibleRegion()); michael@0: SetAntialiasingFlags(this, target); michael@0: michael@0: RenderTraceInvalidateStart(this, "FFFF00", state.mRegionToDraw.GetBounds()); michael@0: michael@0: nsRefPtr ctx = gfxContext::ContextForDrawTarget(target); michael@0: PaintBuffer(ctx, michael@0: state.mRegionToDraw, state.mRegionToDraw, state.mRegionToInvalidate, michael@0: state.mDidSelfCopy, michael@0: state.mClip, michael@0: aCallback, aCallbackData); michael@0: MOZ_LAYERS_LOG_IF_SHADOWABLE(this, ("Layer::Mutated(%p) PaintThebes", this)); michael@0: Mutated(); michael@0: ctx = nullptr; michael@0: mContentClient->ReturnDrawTargetToBuffer(target); michael@0: michael@0: RenderTraceInvalidateEnd(this, "FFFF00"); michael@0: } else { michael@0: // It's possible that state.mRegionToInvalidate is nonempty here, michael@0: // if we are shrinking the valid region to nothing. So use mRegionToDraw michael@0: // instead. michael@0: NS_WARN_IF_FALSE(state.mRegionToDraw.IsEmpty(), michael@0: "No context when we have something to draw, resource exhaustion?"); michael@0: } michael@0: } michael@0: michael@0: already_AddRefed michael@0: BasicLayerManager::CreateThebesLayer() michael@0: { michael@0: NS_ASSERTION(InConstruction(), "Only allowed in construction phase"); michael@0: nsRefPtr layer = new BasicThebesLayer(this); michael@0: return layer.forget(); michael@0: } michael@0: michael@0: } michael@0: }