gfx/layers/basic/BasicThebesLayer.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "BasicThebesLayer.h"
michael@0 7 #include <stdint.h> // for uint32_t
michael@0 8 #include "GeckoProfiler.h" // for PROFILER_LABEL
michael@0 9 #include "ReadbackLayer.h" // for ReadbackLayer, ReadbackSink
michael@0 10 #include "ReadbackProcessor.h" // for ReadbackProcessor::Update, etc
michael@0 11 #include "RenderTrace.h" // for RenderTraceInvalidateEnd, etc
michael@0 12 #include "BasicLayersImpl.h" // for AutoMaskData, etc
michael@0 13 #include "gfxContext.h" // for gfxContext, etc
michael@0 14 #include "gfxRect.h" // for gfxRect
michael@0 15 #include "gfxUtils.h" // for gfxUtils
michael@0 16 #include "mozilla/gfx/2D.h" // for DrawTarget
michael@0 17 #include "mozilla/gfx/BaseRect.h" // for BaseRect
michael@0 18 #include "mozilla/gfx/Matrix.h" // for Matrix
michael@0 19 #include "mozilla/gfx/Rect.h" // for Rect, IntRect
michael@0 20 #include "mozilla/gfx/Types.h" // for Float, etc
michael@0 21 #include "mozilla/layers/LayersTypes.h"
michael@0 22 #include "nsAutoPtr.h" // for nsRefPtr
michael@0 23 #include "nsCOMPtr.h" // for already_AddRefed
michael@0 24 #include "nsISupportsImpl.h" // for gfxContext::Release, etc
michael@0 25 #include "nsPoint.h" // for nsIntPoint
michael@0 26 #include "nsRect.h" // for nsIntRect
michael@0 27 #include "nsTArray.h" // for nsTArray, nsTArray_Impl
michael@0 28 #include "AutoMaskData.h"
michael@0 29 #include "gfx2DGlue.h"
michael@0 30
michael@0 31 using namespace mozilla::gfx;
michael@0 32
michael@0 33 namespace mozilla {
michael@0 34 namespace layers {
michael@0 35
michael@0 36 static nsIntRegion
michael@0 37 IntersectWithClip(const nsIntRegion& aRegion, gfxContext* aContext)
michael@0 38 {
michael@0 39 gfxRect clip = aContext->GetClipExtents();
michael@0 40 clip.RoundOut();
michael@0 41 nsIntRect r(clip.X(), clip.Y(), clip.Width(), clip.Height());
michael@0 42 nsIntRegion result;
michael@0 43 result.And(aRegion, r);
michael@0 44 return result;
michael@0 45 }
michael@0 46
michael@0 47 void
michael@0 48 BasicThebesLayer::PaintThebes(gfxContext* aContext,
michael@0 49 Layer* aMaskLayer,
michael@0 50 LayerManager::DrawThebesLayerCallback aCallback,
michael@0 51 void* aCallbackData,
michael@0 52 ReadbackProcessor* aReadback)
michael@0 53 {
michael@0 54 PROFILER_LABEL("BasicThebesLayer", "PaintThebes");
michael@0 55 NS_ASSERTION(BasicManager()->InDrawing(),
michael@0 56 "Can only draw in drawing phase");
michael@0 57
michael@0 58 nsTArray<ReadbackProcessor::Update> readbackUpdates;
michael@0 59 if (aReadback && UsedForReadback()) {
michael@0 60 aReadback->GetThebesLayerUpdates(this, &readbackUpdates);
michael@0 61 }
michael@0 62
michael@0 63 float opacity = GetEffectiveOpacity();
michael@0 64 CompositionOp effectiveOperator = GetEffectiveOperator(this);
michael@0 65
michael@0 66 if (!BasicManager()->IsRetained()) {
michael@0 67 NS_ASSERTION(readbackUpdates.IsEmpty(), "Can't do readback for non-retained layer");
michael@0 68
michael@0 69 mValidRegion.SetEmpty();
michael@0 70 mContentClient->Clear();
michael@0 71
michael@0 72 nsIntRegion toDraw = IntersectWithClip(GetEffectiveVisibleRegion(), aContext);
michael@0 73
michael@0 74 RenderTraceInvalidateStart(this, "FFFF00", toDraw.GetBounds());
michael@0 75
michael@0 76 if (!toDraw.IsEmpty() && !IsHidden()) {
michael@0 77 if (!aCallback) {
michael@0 78 BasicManager()->SetTransactionIncomplete();
michael@0 79 return;
michael@0 80 }
michael@0 81
michael@0 82 aContext->Save();
michael@0 83
michael@0 84 bool needsClipToVisibleRegion = GetClipToVisibleRegion();
michael@0 85 bool needsGroup = opacity != 1.0 ||
michael@0 86 effectiveOperator != CompositionOp::OP_OVER ||
michael@0 87 aMaskLayer;
michael@0 88 nsRefPtr<gfxContext> groupContext;
michael@0 89 if (needsGroup) {
michael@0 90 groupContext =
michael@0 91 BasicManager()->PushGroupForLayer(aContext, this, toDraw,
michael@0 92 &needsClipToVisibleRegion);
michael@0 93 if (effectiveOperator != CompositionOp::OP_OVER) {
michael@0 94 needsClipToVisibleRegion = true;
michael@0 95 }
michael@0 96 } else {
michael@0 97 groupContext = aContext;
michael@0 98 }
michael@0 99 SetAntialiasingFlags(this, groupContext);
michael@0 100 aCallback(this, groupContext, toDraw, DrawRegionClip::CLIP_NONE, nsIntRegion(), aCallbackData);
michael@0 101 if (needsGroup) {
michael@0 102 BasicManager()->PopGroupToSourceWithCachedSurface(aContext, groupContext);
michael@0 103 if (needsClipToVisibleRegion) {
michael@0 104 gfxUtils::ClipToRegion(aContext, toDraw);
michael@0 105 }
michael@0 106 AutoSetOperator setOptimizedOperator(aContext, ThebesOp(effectiveOperator));
michael@0 107 PaintWithMask(aContext, opacity, aMaskLayer);
michael@0 108 }
michael@0 109
michael@0 110 aContext->Restore();
michael@0 111 }
michael@0 112
michael@0 113 RenderTraceInvalidateEnd(this, "FFFF00");
michael@0 114 return;
michael@0 115 }
michael@0 116
michael@0 117 if (BasicManager()->IsTransactionIncomplete())
michael@0 118 return;
michael@0 119
michael@0 120 gfxRect clipExtents;
michael@0 121 clipExtents = aContext->GetClipExtents();
michael@0 122
michael@0 123 // Pull out the mask surface and transform here, because the mask
michael@0 124 // is internal to basic layers
michael@0 125 AutoMoz2DMaskData mask;
michael@0 126 SourceSurface* maskSurface = nullptr;
michael@0 127 Matrix maskTransform;
michael@0 128 if (GetMaskData(aMaskLayer, Point(), &mask)) {
michael@0 129 maskSurface = mask.GetSurface();
michael@0 130 maskTransform = mask.GetTransform();
michael@0 131 }
michael@0 132
michael@0 133 if (!IsHidden() && !clipExtents.IsEmpty()) {
michael@0 134 mContentClient->DrawTo(this, aContext->GetDrawTarget(), opacity,
michael@0 135 GetOperator(),
michael@0 136 maskSurface, &maskTransform);
michael@0 137 }
michael@0 138
michael@0 139 for (uint32_t i = 0; i < readbackUpdates.Length(); ++i) {
michael@0 140 ReadbackProcessor::Update& update = readbackUpdates[i];
michael@0 141 nsIntPoint offset = update.mLayer->GetBackgroundLayerOffset();
michael@0 142 nsRefPtr<gfxContext> ctx =
michael@0 143 update.mLayer->GetSink()->BeginUpdate(update.mUpdateRect + offset,
michael@0 144 update.mSequenceCounter);
michael@0 145 if (ctx) {
michael@0 146 NS_ASSERTION(opacity == 1.0, "Should only read back opaque layers");
michael@0 147 ctx->Translate(gfxPoint(offset.x, offset.y));
michael@0 148 mContentClient->DrawTo(this, ctx->GetDrawTarget(), 1.0,
michael@0 149 CompositionOpForOp(ctx->CurrentOperator()),
michael@0 150 maskSurface, &maskTransform);
michael@0 151 update.mLayer->GetSink()->EndUpdate(ctx, update.mUpdateRect + offset);
michael@0 152 }
michael@0 153 }
michael@0 154 }
michael@0 155
michael@0 156 void
michael@0 157 BasicThebesLayer::Validate(LayerManager::DrawThebesLayerCallback aCallback,
michael@0 158 void* aCallbackData)
michael@0 159 {
michael@0 160 if (!mContentClient) {
michael@0 161 // This client will have a null Forwarder, which means it will not have
michael@0 162 // a ContentHost on the other side.
michael@0 163 mContentClient = new ContentClientBasic();
michael@0 164 }
michael@0 165
michael@0 166 if (!BasicManager()->IsRetained()) {
michael@0 167 return;
michael@0 168 }
michael@0 169
michael@0 170 uint32_t flags = 0;
michael@0 171 #ifndef MOZ_WIDGET_ANDROID
michael@0 172 if (BasicManager()->CompositorMightResample()) {
michael@0 173 flags |= RotatedContentBuffer::PAINT_WILL_RESAMPLE;
michael@0 174 }
michael@0 175 if (!(flags & RotatedContentBuffer::PAINT_WILL_RESAMPLE)) {
michael@0 176 if (MayResample()) {
michael@0 177 flags |= RotatedContentBuffer::PAINT_WILL_RESAMPLE;
michael@0 178 }
michael@0 179 }
michael@0 180 #endif
michael@0 181 if (mDrawAtomically) {
michael@0 182 flags |= RotatedContentBuffer::PAINT_NO_ROTATION;
michael@0 183 }
michael@0 184 PaintState state =
michael@0 185 mContentClient->BeginPaintBuffer(this, flags);
michael@0 186 mValidRegion.Sub(mValidRegion, state.mRegionToInvalidate);
michael@0 187
michael@0 188 if (DrawTarget* target = mContentClient->BorrowDrawTargetForPainting(state)) {
michael@0 189 // The area that became invalid and is visible needs to be repainted
michael@0 190 // (this could be the whole visible area if our buffer switched
michael@0 191 // from RGB to RGBA, because we might need to repaint with
michael@0 192 // subpixel AA)
michael@0 193 state.mRegionToInvalidate.And(state.mRegionToInvalidate,
michael@0 194 GetEffectiveVisibleRegion());
michael@0 195 SetAntialiasingFlags(this, target);
michael@0 196
michael@0 197 RenderTraceInvalidateStart(this, "FFFF00", state.mRegionToDraw.GetBounds());
michael@0 198
michael@0 199 nsRefPtr<gfxContext> ctx = gfxContext::ContextForDrawTarget(target);
michael@0 200 PaintBuffer(ctx,
michael@0 201 state.mRegionToDraw, state.mRegionToDraw, state.mRegionToInvalidate,
michael@0 202 state.mDidSelfCopy,
michael@0 203 state.mClip,
michael@0 204 aCallback, aCallbackData);
michael@0 205 MOZ_LAYERS_LOG_IF_SHADOWABLE(this, ("Layer::Mutated(%p) PaintThebes", this));
michael@0 206 Mutated();
michael@0 207 ctx = nullptr;
michael@0 208 mContentClient->ReturnDrawTargetToBuffer(target);
michael@0 209
michael@0 210 RenderTraceInvalidateEnd(this, "FFFF00");
michael@0 211 } else {
michael@0 212 // It's possible that state.mRegionToInvalidate is nonempty here,
michael@0 213 // if we are shrinking the valid region to nothing. So use mRegionToDraw
michael@0 214 // instead.
michael@0 215 NS_WARN_IF_FALSE(state.mRegionToDraw.IsEmpty(),
michael@0 216 "No context when we have something to draw, resource exhaustion?");
michael@0 217 }
michael@0 218 }
michael@0 219
michael@0 220 already_AddRefed<ThebesLayer>
michael@0 221 BasicLayerManager::CreateThebesLayer()
michael@0 222 {
michael@0 223 NS_ASSERTION(InConstruction(), "Only allowed in construction phase");
michael@0 224 nsRefPtr<ThebesLayer> layer = new BasicThebesLayer(this);
michael@0 225 return layer.forget();
michael@0 226 }
michael@0 227
michael@0 228 }
michael@0 229 }

mercurial