Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; 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 "RotatedBuffer.h" |
michael@0 | 7 | #include <sys/types.h> // for int32_t |
michael@0 | 8 | #include <algorithm> // for max |
michael@0 | 9 | #include "BasicImplData.h" // for BasicImplData |
michael@0 | 10 | #include "BasicLayersImpl.h" // for ToData |
michael@0 | 11 | #include "BufferUnrotate.h" // for BufferUnrotate |
michael@0 | 12 | #include "GeckoProfiler.h" // for PROFILER_LABEL |
michael@0 | 13 | #include "Layers.h" // for ThebesLayer, Layer, etc |
michael@0 | 14 | #include "gfxPlatform.h" // for gfxPlatform |
michael@0 | 15 | #include "gfxPrefs.h" // for gfxPrefs |
michael@0 | 16 | #include "gfxUtils.h" // for gfxUtils |
michael@0 | 17 | #include "mozilla/ArrayUtils.h" // for ArrayLength |
michael@0 | 18 | #include "mozilla/gfx/BasePoint.h" // for BasePoint |
michael@0 | 19 | #include "mozilla/gfx/BaseRect.h" // for BaseRect |
michael@0 | 20 | #include "mozilla/gfx/BaseSize.h" // for BaseSize |
michael@0 | 21 | #include "mozilla/gfx/Matrix.h" // for Matrix |
michael@0 | 22 | #include "mozilla/gfx/Point.h" // for Point, IntPoint |
michael@0 | 23 | #include "mozilla/gfx/Rect.h" // for Rect, IntRect |
michael@0 | 24 | #include "mozilla/gfx/Types.h" // for ExtendMode::ExtendMode::CLAMP, etc |
michael@0 | 25 | #include "mozilla/layers/ShadowLayers.h" // for ShadowableLayer |
michael@0 | 26 | #include "mozilla/layers/TextureClient.h" // for TextureClient |
michael@0 | 27 | #include "nsSize.h" // for nsIntSize |
michael@0 | 28 | #include "gfx2DGlue.h" |
michael@0 | 29 | |
michael@0 | 30 | namespace mozilla { |
michael@0 | 31 | |
michael@0 | 32 | using namespace gfx; |
michael@0 | 33 | |
michael@0 | 34 | namespace layers { |
michael@0 | 35 | |
michael@0 | 36 | nsIntRect |
michael@0 | 37 | RotatedBuffer::GetQuadrantRectangle(XSide aXSide, YSide aYSide) const |
michael@0 | 38 | { |
michael@0 | 39 | // quadrantTranslation is the amount we translate the top-left |
michael@0 | 40 | // of the quadrant by to get coordinates relative to the layer |
michael@0 | 41 | nsIntPoint quadrantTranslation = -mBufferRotation; |
michael@0 | 42 | quadrantTranslation.x += aXSide == LEFT ? mBufferRect.width : 0; |
michael@0 | 43 | quadrantTranslation.y += aYSide == TOP ? mBufferRect.height : 0; |
michael@0 | 44 | return mBufferRect + quadrantTranslation; |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | Rect |
michael@0 | 48 | RotatedBuffer::GetSourceRectangle(XSide aXSide, YSide aYSide) const |
michael@0 | 49 | { |
michael@0 | 50 | Rect result; |
michael@0 | 51 | if (aXSide == LEFT) { |
michael@0 | 52 | result.x = 0; |
michael@0 | 53 | result.width = mBufferRotation.x; |
michael@0 | 54 | } else { |
michael@0 | 55 | result.x = mBufferRotation.x; |
michael@0 | 56 | result.width = mBufferRect.width - mBufferRotation.x; |
michael@0 | 57 | } |
michael@0 | 58 | if (aYSide == TOP) { |
michael@0 | 59 | result.y = 0; |
michael@0 | 60 | result.height = mBufferRotation.y; |
michael@0 | 61 | } else { |
michael@0 | 62 | result.y = mBufferRotation.y; |
michael@0 | 63 | result.height = mBufferRect.height - mBufferRotation.y; |
michael@0 | 64 | } |
michael@0 | 65 | return result; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | /** |
michael@0 | 69 | * @param aXSide LEFT means we draw from the left side of the buffer (which |
michael@0 | 70 | * is drawn on the right side of mBufferRect). RIGHT means we draw from |
michael@0 | 71 | * the right side of the buffer (which is drawn on the left side of |
michael@0 | 72 | * mBufferRect). |
michael@0 | 73 | * @param aYSide TOP means we draw from the top side of the buffer (which |
michael@0 | 74 | * is drawn on the bottom side of mBufferRect). BOTTOM means we draw from |
michael@0 | 75 | * the bottom side of the buffer (which is drawn on the top side of |
michael@0 | 76 | * mBufferRect). |
michael@0 | 77 | */ |
michael@0 | 78 | void |
michael@0 | 79 | RotatedBuffer::DrawBufferQuadrant(gfx::DrawTarget* aTarget, |
michael@0 | 80 | XSide aXSide, YSide aYSide, |
michael@0 | 81 | ContextSource aSource, |
michael@0 | 82 | float aOpacity, |
michael@0 | 83 | gfx::CompositionOp aOperator, |
michael@0 | 84 | gfx::SourceSurface* aMask, |
michael@0 | 85 | const gfx::Matrix* aMaskTransform) const |
michael@0 | 86 | { |
michael@0 | 87 | // The rectangle that we're going to fill. Basically we're going to |
michael@0 | 88 | // render the buffer at mBufferRect + quadrantTranslation to get the |
michael@0 | 89 | // pixels in the right place, but we're only going to paint within |
michael@0 | 90 | // mBufferRect |
michael@0 | 91 | nsIntRect quadrantRect = GetQuadrantRectangle(aXSide, aYSide); |
michael@0 | 92 | nsIntRect fillRect; |
michael@0 | 93 | if (!fillRect.IntersectRect(mBufferRect, quadrantRect)) |
michael@0 | 94 | return; |
michael@0 | 95 | |
michael@0 | 96 | gfx::Point quadrantTranslation(quadrantRect.x, quadrantRect.y); |
michael@0 | 97 | |
michael@0 | 98 | MOZ_ASSERT(aOperator == CompositionOp::OP_OVER || aOperator == CompositionOp::OP_SOURCE); |
michael@0 | 99 | // direct2d is much slower when using OP_SOURCE so use OP_OVER and |
michael@0 | 100 | // (maybe) a clear instead. Normally we need to draw in a single operation |
michael@0 | 101 | // (to avoid flickering) but direct2d is ok since it defers rendering. |
michael@0 | 102 | // We should try abstract this logic in a helper when we have other use |
michael@0 | 103 | // cases. |
michael@0 | 104 | if (aTarget->GetType() == BackendType::DIRECT2D && aOperator == CompositionOp::OP_SOURCE) { |
michael@0 | 105 | aOperator = CompositionOp::OP_OVER; |
michael@0 | 106 | if (mDTBuffer->GetFormat() == SurfaceFormat::B8G8R8A8) { |
michael@0 | 107 | aTarget->ClearRect(ToRect(fillRect)); |
michael@0 | 108 | } |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | RefPtr<gfx::SourceSurface> snapshot; |
michael@0 | 112 | if (aSource == BUFFER_BLACK) { |
michael@0 | 113 | snapshot = mDTBuffer->Snapshot(); |
michael@0 | 114 | } else { |
michael@0 | 115 | MOZ_ASSERT(aSource == BUFFER_WHITE); |
michael@0 | 116 | snapshot = mDTBufferOnWhite->Snapshot(); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | if (aOperator == CompositionOp::OP_SOURCE) { |
michael@0 | 120 | // OP_SOURCE is unbounded in Azure, and we really don't want that behaviour here. |
michael@0 | 121 | // We also can't do a ClearRect+FillRect since we need the drawing to happen |
michael@0 | 122 | // as an atomic operation (to prevent flickering). |
michael@0 | 123 | aTarget->PushClipRect(gfx::Rect(fillRect.x, fillRect.y, |
michael@0 | 124 | fillRect.width, fillRect.height)); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | if (aMask) { |
michael@0 | 128 | Matrix oldTransform = aTarget->GetTransform(); |
michael@0 | 129 | |
michael@0 | 130 | // Transform from user -> buffer space. |
michael@0 | 131 | Matrix transform; |
michael@0 | 132 | transform.Translate(quadrantTranslation.x, quadrantTranslation.y); |
michael@0 | 133 | |
michael@0 | 134 | Matrix inverseMask = *aMaskTransform; |
michael@0 | 135 | inverseMask.Invert(); |
michael@0 | 136 | |
michael@0 | 137 | transform *= oldTransform; |
michael@0 | 138 | transform *= inverseMask; |
michael@0 | 139 | |
michael@0 | 140 | #ifdef MOZ_GFX_OPTIMIZE_MOBILE |
michael@0 | 141 | SurfacePattern source(snapshot, ExtendMode::CLAMP, transform, Filter::POINT); |
michael@0 | 142 | #else |
michael@0 | 143 | SurfacePattern source(snapshot, ExtendMode::CLAMP, transform); |
michael@0 | 144 | #endif |
michael@0 | 145 | |
michael@0 | 146 | aTarget->SetTransform(*aMaskTransform); |
michael@0 | 147 | aTarget->MaskSurface(source, aMask, Point(0, 0), DrawOptions(aOpacity, aOperator)); |
michael@0 | 148 | aTarget->SetTransform(oldTransform); |
michael@0 | 149 | } else { |
michael@0 | 150 | #ifdef MOZ_GFX_OPTIMIZE_MOBILE |
michael@0 | 151 | DrawSurfaceOptions options(Filter::POINT); |
michael@0 | 152 | #else |
michael@0 | 153 | DrawSurfaceOptions options; |
michael@0 | 154 | #endif |
michael@0 | 155 | aTarget->DrawSurface(snapshot, ToRect(fillRect), |
michael@0 | 156 | GetSourceRectangle(aXSide, aYSide), |
michael@0 | 157 | options, |
michael@0 | 158 | DrawOptions(aOpacity, aOperator)); |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | if (aOperator == CompositionOp::OP_SOURCE) { |
michael@0 | 162 | aTarget->PopClip(); |
michael@0 | 163 | } |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | void |
michael@0 | 167 | RotatedBuffer::DrawBufferWithRotation(gfx::DrawTarget *aTarget, ContextSource aSource, |
michael@0 | 168 | float aOpacity, |
michael@0 | 169 | gfx::CompositionOp aOperator, |
michael@0 | 170 | gfx::SourceSurface* aMask, |
michael@0 | 171 | const gfx::Matrix* aMaskTransform) const |
michael@0 | 172 | { |
michael@0 | 173 | PROFILER_LABEL("RotatedBuffer", "DrawBufferWithRotation"); |
michael@0 | 174 | // See above, in Azure Repeat should always be a safe, even faster choice |
michael@0 | 175 | // though! Particularly on D2D Repeat should be a lot faster, need to look |
michael@0 | 176 | // into that. TODO[Bas] |
michael@0 | 177 | DrawBufferQuadrant(aTarget, LEFT, TOP, aSource, aOpacity, aOperator, aMask, aMaskTransform); |
michael@0 | 178 | DrawBufferQuadrant(aTarget, RIGHT, TOP, aSource, aOpacity, aOperator, aMask, aMaskTransform); |
michael@0 | 179 | DrawBufferQuadrant(aTarget, LEFT, BOTTOM, aSource, aOpacity, aOperator, aMask, aMaskTransform); |
michael@0 | 180 | DrawBufferQuadrant(aTarget, RIGHT, BOTTOM, aSource, aOpacity, aOperator,aMask, aMaskTransform); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | /* static */ bool |
michael@0 | 184 | RotatedContentBuffer::IsClippingCheap(DrawTarget* aTarget, const nsIntRegion& aRegion) |
michael@0 | 185 | { |
michael@0 | 186 | // Assume clipping is cheap if the draw target just has an integer |
michael@0 | 187 | // translation, and the visible region is simple. |
michael@0 | 188 | return !aTarget->GetTransform().HasNonIntegerTranslation() && |
michael@0 | 189 | aRegion.GetNumRects() <= 1; |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | void |
michael@0 | 193 | RotatedContentBuffer::DrawTo(ThebesLayer* aLayer, |
michael@0 | 194 | DrawTarget* aTarget, |
michael@0 | 195 | float aOpacity, |
michael@0 | 196 | CompositionOp aOp, |
michael@0 | 197 | SourceSurface* aMask, |
michael@0 | 198 | const Matrix* aMaskTransform) |
michael@0 | 199 | { |
michael@0 | 200 | if (!EnsureBuffer()) { |
michael@0 | 201 | return; |
michael@0 | 202 | } |
michael@0 | 203 | |
michael@0 | 204 | bool clipped = false; |
michael@0 | 205 | |
michael@0 | 206 | // If the entire buffer is valid, we can just draw the whole thing, |
michael@0 | 207 | // no need to clip. But we'll still clip if clipping is cheap --- |
michael@0 | 208 | // that might let us copy a smaller region of the buffer. |
michael@0 | 209 | // Also clip to the visible region if we're told to. |
michael@0 | 210 | if (!aLayer->GetValidRegion().Contains(BufferRect()) || |
michael@0 | 211 | (ToData(aLayer)->GetClipToVisibleRegion() && |
michael@0 | 212 | !aLayer->GetVisibleRegion().Contains(BufferRect())) || |
michael@0 | 213 | IsClippingCheap(aTarget, aLayer->GetEffectiveVisibleRegion())) { |
michael@0 | 214 | // We don't want to draw invalid stuff, so we need to clip. Might as |
michael@0 | 215 | // well clip to the smallest area possible --- the visible region. |
michael@0 | 216 | // Bug 599189 if there is a non-integer-translation transform in aTarget, |
michael@0 | 217 | // we might sample pixels outside GetEffectiveVisibleRegion(), which is wrong |
michael@0 | 218 | // and may cause gray lines. |
michael@0 | 219 | gfxUtils::ClipToRegionSnapped(aTarget, aLayer->GetEffectiveVisibleRegion()); |
michael@0 | 220 | clipped = true; |
michael@0 | 221 | } |
michael@0 | 222 | |
michael@0 | 223 | DrawBufferWithRotation(aTarget, BUFFER_BLACK, aOpacity, aOp, aMask, aMaskTransform); |
michael@0 | 224 | if (clipped) { |
michael@0 | 225 | aTarget->PopClip(); |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | DrawTarget* |
michael@0 | 230 | RotatedContentBuffer::BorrowDrawTargetForQuadrantUpdate(const nsIntRect& aBounds, |
michael@0 | 231 | ContextSource aSource, |
michael@0 | 232 | DrawIterator* aIter) |
michael@0 | 233 | { |
michael@0 | 234 | nsIntRect bounds = aBounds; |
michael@0 | 235 | if (aIter) { |
michael@0 | 236 | // If an iterator was provided, then BeginPaint must have been run with |
michael@0 | 237 | // PAINT_CAN_DRAW_ROTATED, and the draw region might cover multiple quadrants. |
michael@0 | 238 | // Iterate over each of them, and return an appropriate buffer each time we find |
michael@0 | 239 | // one that intersects the draw region. The iterator mCount value tracks which |
michael@0 | 240 | // quadrants we have considered across multiple calls to this function. |
michael@0 | 241 | aIter->mDrawRegion.SetEmpty(); |
michael@0 | 242 | while (aIter->mCount < 4) { |
michael@0 | 243 | nsIntRect quadrant = GetQuadrantRectangle((aIter->mCount & 1) ? LEFT : RIGHT, |
michael@0 | 244 | (aIter->mCount & 2) ? TOP : BOTTOM); |
michael@0 | 245 | aIter->mDrawRegion.And(aBounds, quadrant); |
michael@0 | 246 | aIter->mCount++; |
michael@0 | 247 | if (!aIter->mDrawRegion.IsEmpty()) { |
michael@0 | 248 | break; |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | if (aIter->mDrawRegion.IsEmpty()) { |
michael@0 | 252 | return nullptr; |
michael@0 | 253 | } |
michael@0 | 254 | bounds = aIter->mDrawRegion.GetBounds(); |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | if (!EnsureBuffer()) { |
michael@0 | 258 | return nullptr; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | MOZ_ASSERT(!mLoanedDrawTarget, "draw target has been borrowed and not returned"); |
michael@0 | 262 | if (aSource == BUFFER_BOTH && HaveBufferOnWhite()) { |
michael@0 | 263 | if (!EnsureBufferOnWhite()) { |
michael@0 | 264 | return nullptr; |
michael@0 | 265 | } |
michael@0 | 266 | MOZ_ASSERT(mDTBuffer && mDTBufferOnWhite); |
michael@0 | 267 | mLoanedDrawTarget = Factory::CreateDualDrawTarget(mDTBuffer, mDTBufferOnWhite); |
michael@0 | 268 | } else if (aSource == BUFFER_WHITE) { |
michael@0 | 269 | if (!EnsureBufferOnWhite()) { |
michael@0 | 270 | return nullptr; |
michael@0 | 271 | } |
michael@0 | 272 | mLoanedDrawTarget = mDTBufferOnWhite; |
michael@0 | 273 | } else { |
michael@0 | 274 | // BUFFER_BLACK, or BUFFER_BOTH with a single buffer. |
michael@0 | 275 | mLoanedDrawTarget = mDTBuffer; |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | // Figure out which quadrant to draw in |
michael@0 | 279 | int32_t xBoundary = mBufferRect.XMost() - mBufferRotation.x; |
michael@0 | 280 | int32_t yBoundary = mBufferRect.YMost() - mBufferRotation.y; |
michael@0 | 281 | XSide sideX = bounds.XMost() <= xBoundary ? RIGHT : LEFT; |
michael@0 | 282 | YSide sideY = bounds.YMost() <= yBoundary ? BOTTOM : TOP; |
michael@0 | 283 | nsIntRect quadrantRect = GetQuadrantRectangle(sideX, sideY); |
michael@0 | 284 | NS_ASSERTION(quadrantRect.Contains(bounds), "Messed up quadrants"); |
michael@0 | 285 | |
michael@0 | 286 | mLoanedTransform = mLoanedDrawTarget->GetTransform(); |
michael@0 | 287 | mLoanedTransform.Translate(-quadrantRect.x, -quadrantRect.y); |
michael@0 | 288 | mLoanedDrawTarget->SetTransform(mLoanedTransform); |
michael@0 | 289 | mLoanedTransform.Translate(quadrantRect.x, quadrantRect.y); |
michael@0 | 290 | |
michael@0 | 291 | return mLoanedDrawTarget; |
michael@0 | 292 | } |
michael@0 | 293 | |
michael@0 | 294 | void |
michael@0 | 295 | BorrowDrawTarget::ReturnDrawTarget(gfx::DrawTarget*& aReturned) |
michael@0 | 296 | { |
michael@0 | 297 | MOZ_ASSERT(aReturned == mLoanedDrawTarget); |
michael@0 | 298 | mLoanedDrawTarget->SetTransform(mLoanedTransform); |
michael@0 | 299 | mLoanedDrawTarget = nullptr; |
michael@0 | 300 | aReturned = nullptr; |
michael@0 | 301 | } |
michael@0 | 302 | |
michael@0 | 303 | gfxContentType |
michael@0 | 304 | RotatedContentBuffer::BufferContentType() |
michael@0 | 305 | { |
michael@0 | 306 | if (mBufferProvider || mDTBuffer) { |
michael@0 | 307 | SurfaceFormat format; |
michael@0 | 308 | |
michael@0 | 309 | if (mBufferProvider) { |
michael@0 | 310 | format = mBufferProvider->GetFormat(); |
michael@0 | 311 | } else if (mDTBuffer) { |
michael@0 | 312 | format = mDTBuffer->GetFormat(); |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | return ContentForFormat(format); |
michael@0 | 316 | } |
michael@0 | 317 | return gfxContentType::SENTINEL; |
michael@0 | 318 | } |
michael@0 | 319 | |
michael@0 | 320 | bool |
michael@0 | 321 | RotatedContentBuffer::BufferSizeOkFor(const nsIntSize& aSize) |
michael@0 | 322 | { |
michael@0 | 323 | return (aSize == mBufferRect.Size() || |
michael@0 | 324 | (SizedToVisibleBounds != mBufferSizePolicy && |
michael@0 | 325 | aSize < mBufferRect.Size())); |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | bool |
michael@0 | 329 | RotatedContentBuffer::EnsureBuffer() |
michael@0 | 330 | { |
michael@0 | 331 | NS_ASSERTION(!mLoanedDrawTarget, "Loaned draw target must be returned"); |
michael@0 | 332 | if (!mDTBuffer) { |
michael@0 | 333 | if (mBufferProvider) { |
michael@0 | 334 | mDTBuffer = mBufferProvider->GetAsDrawTarget(); |
michael@0 | 335 | } |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | NS_WARN_IF_FALSE(mDTBuffer, "no buffer"); |
michael@0 | 339 | return !!mDTBuffer; |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | bool |
michael@0 | 343 | RotatedContentBuffer::EnsureBufferOnWhite() |
michael@0 | 344 | { |
michael@0 | 345 | NS_ASSERTION(!mLoanedDrawTarget, "Loaned draw target must be returned"); |
michael@0 | 346 | if (!mDTBufferOnWhite) { |
michael@0 | 347 | if (mBufferProviderOnWhite) { |
michael@0 | 348 | mDTBufferOnWhite = |
michael@0 | 349 | mBufferProviderOnWhite->GetAsDrawTarget(); |
michael@0 | 350 | } |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | NS_WARN_IF_FALSE(mDTBufferOnWhite, "no buffer"); |
michael@0 | 354 | return mDTBufferOnWhite; |
michael@0 | 355 | } |
michael@0 | 356 | |
michael@0 | 357 | bool |
michael@0 | 358 | RotatedContentBuffer::HaveBuffer() const |
michael@0 | 359 | { |
michael@0 | 360 | return mDTBuffer || mBufferProvider; |
michael@0 | 361 | } |
michael@0 | 362 | |
michael@0 | 363 | bool |
michael@0 | 364 | RotatedContentBuffer::HaveBufferOnWhite() const |
michael@0 | 365 | { |
michael@0 | 366 | return mDTBufferOnWhite || mBufferProviderOnWhite; |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | static void |
michael@0 | 370 | WrapRotationAxis(int32_t* aRotationPoint, int32_t aSize) |
michael@0 | 371 | { |
michael@0 | 372 | if (*aRotationPoint < 0) { |
michael@0 | 373 | *aRotationPoint += aSize; |
michael@0 | 374 | } else if (*aRotationPoint >= aSize) { |
michael@0 | 375 | *aRotationPoint -= aSize; |
michael@0 | 376 | } |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | static nsIntRect |
michael@0 | 380 | ComputeBufferRect(const nsIntRect& aRequestedRect) |
michael@0 | 381 | { |
michael@0 | 382 | nsIntRect rect(aRequestedRect); |
michael@0 | 383 | // Set a minimum width to guarantee a minimum size of buffers we |
michael@0 | 384 | // allocate (and work around problems on some platforms with smaller |
michael@0 | 385 | // dimensions). 64 is the magic number needed to work around the |
michael@0 | 386 | // rendering glitch, and guarantees image rows can be SIMD'd for |
michael@0 | 387 | // even r5g6b5 surfaces pretty much everywhere. |
michael@0 | 388 | rect.width = std::max(aRequestedRect.width, 64); |
michael@0 | 389 | #ifdef MOZ_WIDGET_GONK |
michael@0 | 390 | // Set a minumum height to guarantee a minumum height of buffers we |
michael@0 | 391 | // allocate. Some GL implementations fail to render gralloc textures |
michael@0 | 392 | // with a height 9px-16px. It happens on Adreno 200. Adreno 320 does not |
michael@0 | 393 | // have this problem. 32 is choosed as alignment of gralloc buffers. |
michael@0 | 394 | // See Bug 873937. |
michael@0 | 395 | // Increase the height only when the requested height is more than 0. |
michael@0 | 396 | // See Bug 895976. |
michael@0 | 397 | // XXX it might be better to disable it on the gpu that does not have |
michael@0 | 398 | // the height problem. |
michael@0 | 399 | if (rect.height > 0) { |
michael@0 | 400 | rect.height = std::max(aRequestedRect.height, 32); |
michael@0 | 401 | } |
michael@0 | 402 | #endif |
michael@0 | 403 | return rect; |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | void |
michael@0 | 407 | RotatedContentBuffer::FlushBuffers() |
michael@0 | 408 | { |
michael@0 | 409 | if (mDTBuffer) { |
michael@0 | 410 | mDTBuffer->Flush(); |
michael@0 | 411 | } |
michael@0 | 412 | if (mDTBufferOnWhite) { |
michael@0 | 413 | mDTBufferOnWhite->Flush(); |
michael@0 | 414 | } |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | RotatedContentBuffer::PaintState |
michael@0 | 418 | RotatedContentBuffer::BeginPaint(ThebesLayer* aLayer, |
michael@0 | 419 | uint32_t aFlags) |
michael@0 | 420 | { |
michael@0 | 421 | PaintState result; |
michael@0 | 422 | // We need to disable rotation if we're going to be resampled when |
michael@0 | 423 | // drawing, because we might sample across the rotation boundary. |
michael@0 | 424 | bool canHaveRotation = gfxPlatform::BufferRotationEnabled() && |
michael@0 | 425 | !(aFlags & (PAINT_WILL_RESAMPLE | PAINT_NO_ROTATION)); |
michael@0 | 426 | |
michael@0 | 427 | nsIntRegion validRegion = aLayer->GetValidRegion(); |
michael@0 | 428 | |
michael@0 | 429 | bool canUseOpaqueSurface = aLayer->CanUseOpaqueSurface(); |
michael@0 | 430 | ContentType layerContentType = |
michael@0 | 431 | canUseOpaqueSurface ? gfxContentType::COLOR : |
michael@0 | 432 | gfxContentType::COLOR_ALPHA; |
michael@0 | 433 | |
michael@0 | 434 | SurfaceMode mode; |
michael@0 | 435 | nsIntRegion neededRegion; |
michael@0 | 436 | bool canReuseBuffer; |
michael@0 | 437 | nsIntRect destBufferRect; |
michael@0 | 438 | |
michael@0 | 439 | while (true) { |
michael@0 | 440 | mode = aLayer->GetSurfaceMode(); |
michael@0 | 441 | neededRegion = aLayer->GetVisibleRegion(); |
michael@0 | 442 | canReuseBuffer = HaveBuffer() && BufferSizeOkFor(neededRegion.GetBounds().Size()); |
michael@0 | 443 | result.mContentType = layerContentType; |
michael@0 | 444 | |
michael@0 | 445 | if (canReuseBuffer) { |
michael@0 | 446 | if (mBufferRect.Contains(neededRegion.GetBounds())) { |
michael@0 | 447 | // We don't need to adjust mBufferRect. |
michael@0 | 448 | destBufferRect = mBufferRect; |
michael@0 | 449 | } else if (neededRegion.GetBounds().Size() <= mBufferRect.Size()) { |
michael@0 | 450 | // The buffer's big enough but doesn't contain everything that's |
michael@0 | 451 | // going to be visible. We'll move it. |
michael@0 | 452 | destBufferRect = nsIntRect(neededRegion.GetBounds().TopLeft(), mBufferRect.Size()); |
michael@0 | 453 | } else { |
michael@0 | 454 | destBufferRect = neededRegion.GetBounds(); |
michael@0 | 455 | } |
michael@0 | 456 | } else { |
michael@0 | 457 | // We won't be reusing the buffer. Compute a new rect. |
michael@0 | 458 | destBufferRect = ComputeBufferRect(neededRegion.GetBounds()); |
michael@0 | 459 | } |
michael@0 | 460 | |
michael@0 | 461 | if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 462 | #if defined(MOZ_GFX_OPTIMIZE_MOBILE) || defined(MOZ_WIDGET_GONK) |
michael@0 | 463 | mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; |
michael@0 | 464 | #else |
michael@0 | 465 | if (!aLayer->GetParent() || |
michael@0 | 466 | !aLayer->GetParent()->SupportsComponentAlphaChildren() || |
michael@0 | 467 | !aLayer->Manager()->IsCompositingCheap() || |
michael@0 | 468 | !aLayer->AsShadowableLayer() || |
michael@0 | 469 | !aLayer->AsShadowableLayer()->HasShadow() || |
michael@0 | 470 | !gfxPrefs::ComponentAlphaEnabled()) { |
michael@0 | 471 | mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; |
michael@0 | 472 | } else { |
michael@0 | 473 | result.mContentType = gfxContentType::COLOR; |
michael@0 | 474 | } |
michael@0 | 475 | #endif |
michael@0 | 476 | } |
michael@0 | 477 | |
michael@0 | 478 | if ((aFlags & PAINT_WILL_RESAMPLE) && |
michael@0 | 479 | (!neededRegion.GetBounds().IsEqualInterior(destBufferRect) || |
michael@0 | 480 | neededRegion.GetNumRects() > 1)) { |
michael@0 | 481 | // The area we add to neededRegion might not be painted opaquely |
michael@0 | 482 | if (mode == SurfaceMode::SURFACE_OPAQUE) { |
michael@0 | 483 | result.mContentType = gfxContentType::COLOR_ALPHA; |
michael@0 | 484 | mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | // We need to validate the entire buffer, to make sure that only valid |
michael@0 | 488 | // pixels are sampled |
michael@0 | 489 | neededRegion = destBufferRect; |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | // If we have an existing buffer, but the content type has changed or we |
michael@0 | 493 | // have transitioned into/out of component alpha, then we need to recreate it. |
michael@0 | 494 | if (HaveBuffer() && |
michael@0 | 495 | (result.mContentType != BufferContentType() || |
michael@0 | 496 | (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) != HaveBufferOnWhite())) { |
michael@0 | 497 | |
michael@0 | 498 | // We're effectively clearing the valid region, so we need to draw |
michael@0 | 499 | // the entire needed region now. |
michael@0 | 500 | result.mRegionToInvalidate = aLayer->GetValidRegion(); |
michael@0 | 501 | validRegion.SetEmpty(); |
michael@0 | 502 | Clear(); |
michael@0 | 503 | // Restart decision process with the cleared buffer. We can only go |
michael@0 | 504 | // around the loop one more iteration, since mDTBuffer is null now. |
michael@0 | 505 | continue; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | break; |
michael@0 | 509 | } |
michael@0 | 510 | |
michael@0 | 511 | NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()), |
michael@0 | 512 | "Destination rect doesn't contain what we need to paint"); |
michael@0 | 513 | |
michael@0 | 514 | result.mRegionToDraw.Sub(neededRegion, validRegion); |
michael@0 | 515 | |
michael@0 | 516 | if (result.mRegionToDraw.IsEmpty()) |
michael@0 | 517 | return result; |
michael@0 | 518 | |
michael@0 | 519 | // Do not modify result.mRegionToDraw or result.mContentType after this call. |
michael@0 | 520 | // Do not modify mBufferRect, mBufferRotation, or mDidSelfCopy, |
michael@0 | 521 | // or call CreateBuffer before this call. |
michael@0 | 522 | FinalizeFrame(result.mRegionToDraw); |
michael@0 | 523 | |
michael@0 | 524 | nsIntRect drawBounds = result.mRegionToDraw.GetBounds(); |
michael@0 | 525 | RefPtr<DrawTarget> destDTBuffer; |
michael@0 | 526 | RefPtr<DrawTarget> destDTBufferOnWhite; |
michael@0 | 527 | uint32_t bufferFlags = canHaveRotation ? ALLOW_REPEAT : 0; |
michael@0 | 528 | if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 529 | bufferFlags |= BUFFER_COMPONENT_ALPHA; |
michael@0 | 530 | } |
michael@0 | 531 | if (canReuseBuffer) { |
michael@0 | 532 | if (!EnsureBuffer()) { |
michael@0 | 533 | return result; |
michael@0 | 534 | } |
michael@0 | 535 | nsIntRect keepArea; |
michael@0 | 536 | if (keepArea.IntersectRect(destBufferRect, mBufferRect)) { |
michael@0 | 537 | // Set mBufferRotation so that the pixels currently in mDTBuffer |
michael@0 | 538 | // will still be rendered in the right place when mBufferRect |
michael@0 | 539 | // changes to destBufferRect. |
michael@0 | 540 | nsIntPoint newRotation = mBufferRotation + |
michael@0 | 541 | (destBufferRect.TopLeft() - mBufferRect.TopLeft()); |
michael@0 | 542 | WrapRotationAxis(&newRotation.x, mBufferRect.width); |
michael@0 | 543 | WrapRotationAxis(&newRotation.y, mBufferRect.height); |
michael@0 | 544 | NS_ASSERTION(nsIntRect(nsIntPoint(0,0), mBufferRect.Size()).Contains(newRotation), |
michael@0 | 545 | "newRotation out of bounds"); |
michael@0 | 546 | int32_t xBoundary = destBufferRect.XMost() - newRotation.x; |
michael@0 | 547 | int32_t yBoundary = destBufferRect.YMost() - newRotation.y; |
michael@0 | 548 | bool drawWrapsBuffer = (drawBounds.x < xBoundary && xBoundary < drawBounds.XMost()) || |
michael@0 | 549 | (drawBounds.y < yBoundary && yBoundary < drawBounds.YMost()); |
michael@0 | 550 | if ((drawWrapsBuffer && !(aFlags & PAINT_CAN_DRAW_ROTATED)) || |
michael@0 | 551 | (newRotation != nsIntPoint(0,0) && !canHaveRotation)) { |
michael@0 | 552 | // The stuff we need to redraw will wrap around an edge of the |
michael@0 | 553 | // buffer (and the caller doesn't know how to support that), so |
michael@0 | 554 | // move the pixels we can keep into a position that lets us |
michael@0 | 555 | // redraw in just one quadrant. |
michael@0 | 556 | if (mBufferRotation == nsIntPoint(0,0)) { |
michael@0 | 557 | nsIntRect srcRect(nsIntPoint(0, 0), mBufferRect.Size()); |
michael@0 | 558 | nsIntPoint dest = mBufferRect.TopLeft() - destBufferRect.TopLeft(); |
michael@0 | 559 | MOZ_ASSERT(mDTBuffer); |
michael@0 | 560 | mDTBuffer->CopyRect(IntRect(srcRect.x, srcRect.y, srcRect.width, srcRect.height), |
michael@0 | 561 | IntPoint(dest.x, dest.y)); |
michael@0 | 562 | if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 563 | if (!EnsureBufferOnWhite()) { |
michael@0 | 564 | return result; |
michael@0 | 565 | } |
michael@0 | 566 | MOZ_ASSERT(mDTBufferOnWhite); |
michael@0 | 567 | mDTBufferOnWhite->CopyRect(IntRect(srcRect.x, srcRect.y, srcRect.width, srcRect.height), |
michael@0 | 568 | IntPoint(dest.x, dest.y)); |
michael@0 | 569 | } |
michael@0 | 570 | result.mDidSelfCopy = true; |
michael@0 | 571 | mDidSelfCopy = true; |
michael@0 | 572 | // Don't set destBuffer; we special-case self-copies, and |
michael@0 | 573 | // just did the necessary work above. |
michael@0 | 574 | mBufferRect = destBufferRect; |
michael@0 | 575 | } else { |
michael@0 | 576 | // With azure and a data surface perform an buffer unrotate |
michael@0 | 577 | // (SelfCopy). |
michael@0 | 578 | unsigned char* data; |
michael@0 | 579 | IntSize size; |
michael@0 | 580 | int32_t stride; |
michael@0 | 581 | SurfaceFormat format; |
michael@0 | 582 | |
michael@0 | 583 | if (mDTBuffer->LockBits(&data, &size, &stride, &format)) { |
michael@0 | 584 | uint8_t bytesPerPixel = BytesPerPixel(format); |
michael@0 | 585 | BufferUnrotate(data, |
michael@0 | 586 | size.width * bytesPerPixel, |
michael@0 | 587 | size.height, stride, |
michael@0 | 588 | newRotation.x * bytesPerPixel, newRotation.y); |
michael@0 | 589 | mDTBuffer->ReleaseBits(data); |
michael@0 | 590 | |
michael@0 | 591 | if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 592 | if (!EnsureBufferOnWhite()) { |
michael@0 | 593 | return result; |
michael@0 | 594 | } |
michael@0 | 595 | MOZ_ASSERT(mDTBufferOnWhite); |
michael@0 | 596 | mDTBufferOnWhite->LockBits(&data, &size, &stride, &format); |
michael@0 | 597 | uint8_t bytesPerPixel = BytesPerPixel(format); |
michael@0 | 598 | BufferUnrotate(data, |
michael@0 | 599 | size.width * bytesPerPixel, |
michael@0 | 600 | size.height, stride, |
michael@0 | 601 | newRotation.x * bytesPerPixel, newRotation.y); |
michael@0 | 602 | mDTBufferOnWhite->ReleaseBits(data); |
michael@0 | 603 | } |
michael@0 | 604 | |
michael@0 | 605 | // Buffer unrotate moves all the pixels, note that |
michael@0 | 606 | // we self copied for SyncBackToFrontBuffer |
michael@0 | 607 | result.mDidSelfCopy = true; |
michael@0 | 608 | mDidSelfCopy = true; |
michael@0 | 609 | mBufferRect = destBufferRect; |
michael@0 | 610 | mBufferRotation = nsIntPoint(0, 0); |
michael@0 | 611 | } |
michael@0 | 612 | |
michael@0 | 613 | if (!result.mDidSelfCopy) { |
michael@0 | 614 | destBufferRect = ComputeBufferRect(neededRegion.GetBounds()); |
michael@0 | 615 | CreateBuffer(result.mContentType, destBufferRect, bufferFlags, |
michael@0 | 616 | &destDTBuffer, &destDTBufferOnWhite); |
michael@0 | 617 | if (!destDTBuffer) { |
michael@0 | 618 | return result; |
michael@0 | 619 | } |
michael@0 | 620 | } |
michael@0 | 621 | } |
michael@0 | 622 | } else { |
michael@0 | 623 | mBufferRect = destBufferRect; |
michael@0 | 624 | mBufferRotation = newRotation; |
michael@0 | 625 | } |
michael@0 | 626 | } else { |
michael@0 | 627 | // No pixels are going to be kept. The whole visible region |
michael@0 | 628 | // will be redrawn, so we don't need to copy anything, so we don't |
michael@0 | 629 | // set destBuffer. |
michael@0 | 630 | mBufferRect = destBufferRect; |
michael@0 | 631 | mBufferRotation = nsIntPoint(0,0); |
michael@0 | 632 | } |
michael@0 | 633 | } else { |
michael@0 | 634 | // The buffer's not big enough, so allocate a new one |
michael@0 | 635 | CreateBuffer(result.mContentType, destBufferRect, bufferFlags, |
michael@0 | 636 | &destDTBuffer, &destDTBufferOnWhite); |
michael@0 | 637 | if (!destDTBuffer) { |
michael@0 | 638 | return result; |
michael@0 | 639 | } |
michael@0 | 640 | } |
michael@0 | 641 | |
michael@0 | 642 | NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || destBufferRect == neededRegion.GetBounds(), |
michael@0 | 643 | "If we're resampling, we need to validate the entire buffer"); |
michael@0 | 644 | |
michael@0 | 645 | // If we have no buffered data already, then destBuffer will be a fresh buffer |
michael@0 | 646 | // and we do not need to clear it below. |
michael@0 | 647 | bool isClear = !HaveBuffer(); |
michael@0 | 648 | |
michael@0 | 649 | if (destDTBuffer) { |
michael@0 | 650 | if (!isClear && (mode != SurfaceMode::SURFACE_COMPONENT_ALPHA || HaveBufferOnWhite())) { |
michael@0 | 651 | // Copy the bits |
michael@0 | 652 | nsIntPoint offset = -destBufferRect.TopLeft(); |
michael@0 | 653 | Matrix mat; |
michael@0 | 654 | mat.Translate(offset.x, offset.y); |
michael@0 | 655 | destDTBuffer->SetTransform(mat); |
michael@0 | 656 | if (!EnsureBuffer()) { |
michael@0 | 657 | return result; |
michael@0 | 658 | } |
michael@0 | 659 | MOZ_ASSERT(mDTBuffer, "Have we got a Thebes buffer for some reason?"); |
michael@0 | 660 | DrawBufferWithRotation(destDTBuffer, BUFFER_BLACK, 1.0, CompositionOp::OP_SOURCE); |
michael@0 | 661 | destDTBuffer->SetTransform(Matrix()); |
michael@0 | 662 | |
michael@0 | 663 | if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 664 | NS_ASSERTION(destDTBufferOnWhite, "Must have a white buffer!"); |
michael@0 | 665 | destDTBufferOnWhite->SetTransform(mat); |
michael@0 | 666 | if (!EnsureBufferOnWhite()) { |
michael@0 | 667 | return result; |
michael@0 | 668 | } |
michael@0 | 669 | MOZ_ASSERT(mDTBufferOnWhite, "Have we got a Thebes buffer for some reason?"); |
michael@0 | 670 | DrawBufferWithRotation(destDTBufferOnWhite, BUFFER_WHITE, 1.0, CompositionOp::OP_SOURCE); |
michael@0 | 671 | destDTBufferOnWhite->SetTransform(Matrix()); |
michael@0 | 672 | } |
michael@0 | 673 | } |
michael@0 | 674 | |
michael@0 | 675 | mDTBuffer = destDTBuffer.forget(); |
michael@0 | 676 | mDTBufferOnWhite = destDTBufferOnWhite.forget(); |
michael@0 | 677 | mBufferRect = destBufferRect; |
michael@0 | 678 | mBufferRotation = nsIntPoint(0,0); |
michael@0 | 679 | } |
michael@0 | 680 | NS_ASSERTION(canHaveRotation || mBufferRotation == nsIntPoint(0,0), |
michael@0 | 681 | "Rotation disabled, but we have nonzero rotation?"); |
michael@0 | 682 | |
michael@0 | 683 | nsIntRegion invalidate; |
michael@0 | 684 | invalidate.Sub(aLayer->GetValidRegion(), destBufferRect); |
michael@0 | 685 | result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate); |
michael@0 | 686 | result.mClip = DrawRegionClip::DRAW_SNAPPED; |
michael@0 | 687 | result.mMode = mode; |
michael@0 | 688 | |
michael@0 | 689 | return result; |
michael@0 | 690 | } |
michael@0 | 691 | |
michael@0 | 692 | DrawTarget* |
michael@0 | 693 | RotatedContentBuffer::BorrowDrawTargetForPainting(const PaintState& aPaintState, |
michael@0 | 694 | DrawIterator* aIter /* = nullptr */) |
michael@0 | 695 | { |
michael@0 | 696 | if (aPaintState.mMode == SurfaceMode::SURFACE_NONE) { |
michael@0 | 697 | return nullptr; |
michael@0 | 698 | } |
michael@0 | 699 | |
michael@0 | 700 | DrawTarget* result = BorrowDrawTargetForQuadrantUpdate(aPaintState.mRegionToDraw.GetBounds(), |
michael@0 | 701 | BUFFER_BOTH, aIter); |
michael@0 | 702 | if (!result) { |
michael@0 | 703 | return nullptr; |
michael@0 | 704 | } |
michael@0 | 705 | const nsIntRegion* drawPtr = &aPaintState.mRegionToDraw; |
michael@0 | 706 | if (aIter) { |
michael@0 | 707 | // The iterators draw region currently only contains the bounds of the region, |
michael@0 | 708 | // this makes it the precise region. |
michael@0 | 709 | aIter->mDrawRegion.And(aIter->mDrawRegion, aPaintState.mRegionToDraw); |
michael@0 | 710 | drawPtr = &aIter->mDrawRegion; |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | if (aPaintState.mMode == SurfaceMode::SURFACE_COMPONENT_ALPHA) { |
michael@0 | 714 | MOZ_ASSERT(mDTBuffer && mDTBufferOnWhite); |
michael@0 | 715 | nsIntRegionRectIterator iter(*drawPtr); |
michael@0 | 716 | const nsIntRect *iterRect; |
michael@0 | 717 | while ((iterRect = iter.Next())) { |
michael@0 | 718 | mDTBuffer->FillRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height), |
michael@0 | 719 | ColorPattern(Color(0.0, 0.0, 0.0, 1.0))); |
michael@0 | 720 | mDTBufferOnWhite->FillRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height), |
michael@0 | 721 | ColorPattern(Color(1.0, 1.0, 1.0, 1.0))); |
michael@0 | 722 | } |
michael@0 | 723 | } else if (aPaintState.mContentType == gfxContentType::COLOR_ALPHA && HaveBuffer()) { |
michael@0 | 724 | // HaveBuffer() => we have an existing buffer that we must clear |
michael@0 | 725 | nsIntRegionRectIterator iter(*drawPtr); |
michael@0 | 726 | const nsIntRect *iterRect; |
michael@0 | 727 | while ((iterRect = iter.Next())) { |
michael@0 | 728 | result->ClearRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height)); |
michael@0 | 729 | } |
michael@0 | 730 | } |
michael@0 | 731 | |
michael@0 | 732 | return result; |
michael@0 | 733 | } |
michael@0 | 734 | |
michael@0 | 735 | } |
michael@0 | 736 | } |
michael@0 | 737 |