Bug 968872 - Remove TEXTURE_DEALLOCATE_DEFERRED flag. r=nical
[gecko.git] / gfx / layers / RotatedBuffer.cpp
blob8476c16b2ad0d6ab2cb2fd3474a38ac942ca7c1f
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "RotatedBuffer.h"
7 #include <sys/types.h> // for int32_t
8 #include <algorithm> // for max
9 #include "BasicImplData.h" // for BasicImplData
10 #include "BasicLayersImpl.h" // for ToData
11 #include "BufferUnrotate.h" // for BufferUnrotate
12 #include "GeckoProfiler.h" // for PROFILER_LABEL
13 #include "Layers.h" // for ThebesLayer, Layer, etc
14 #include "gfxPlatform.h" // for gfxPlatform
15 #include "gfxUtils.h" // for gfxUtils
16 #include "mozilla/ArrayUtils.h" // for ArrayLength
17 #include "mozilla/gfx/BasePoint.h" // for BasePoint
18 #include "mozilla/gfx/BaseRect.h" // for BaseRect
19 #include "mozilla/gfx/BaseSize.h" // for BaseSize
20 #include "mozilla/gfx/Matrix.h" // for Matrix
21 #include "mozilla/gfx/Point.h" // for Point, IntPoint
22 #include "mozilla/gfx/Rect.h" // for Rect, IntRect
23 #include "mozilla/gfx/Types.h" // for ExtendMode::ExtendMode::CLAMP, etc
24 #include "mozilla/layers/ShadowLayers.h" // for ShadowableLayer
25 #include "mozilla/layers/TextureClient.h" // for DeprecatedTextureClient
26 #include "nsSize.h" // for nsIntSize
27 #include "gfx2DGlue.h"
29 namespace mozilla {
31 using namespace gfx;
33 namespace layers {
35 nsIntRect
36 RotatedBuffer::GetQuadrantRectangle(XSide aXSide, YSide aYSide) const
38 // quadrantTranslation is the amount we translate the top-left
39 // of the quadrant by to get coordinates relative to the layer
40 nsIntPoint quadrantTranslation = -mBufferRotation;
41 quadrantTranslation.x += aXSide == LEFT ? mBufferRect.width : 0;
42 quadrantTranslation.y += aYSide == TOP ? mBufferRect.height : 0;
43 return mBufferRect + quadrantTranslation;
46 Rect
47 RotatedBuffer::GetSourceRectangle(XSide aXSide, YSide aYSide) const
49 Rect result;
50 if (aXSide == LEFT) {
51 result.x = 0;
52 result.width = mBufferRotation.x;
53 } else {
54 result.x = mBufferRotation.x;
55 result.width = mBufferRect.width - mBufferRotation.x;
57 if (aYSide == TOP) {
58 result.y = 0;
59 result.height = mBufferRotation.y;
60 } else {
61 result.y = mBufferRotation.y;
62 result.height = mBufferRect.height - mBufferRotation.y;
64 return result;
67 /**
68 * @param aXSide LEFT means we draw from the left side of the buffer (which
69 * is drawn on the right side of mBufferRect). RIGHT means we draw from
70 * the right side of the buffer (which is drawn on the left side of
71 * mBufferRect).
72 * @param aYSide TOP means we draw from the top side of the buffer (which
73 * is drawn on the bottom side of mBufferRect). BOTTOM means we draw from
74 * the bottom side of the buffer (which is drawn on the top side of
75 * mBufferRect).
77 void
78 RotatedBuffer::DrawBufferQuadrant(gfx::DrawTarget* aTarget,
79 XSide aXSide, YSide aYSide,
80 ContextSource aSource,
81 float aOpacity,
82 gfx::CompositionOp aOperator,
83 gfx::SourceSurface* aMask,
84 const gfx::Matrix* aMaskTransform) const
86 // The rectangle that we're going to fill. Basically we're going to
87 // render the buffer at mBufferRect + quadrantTranslation to get the
88 // pixels in the right place, but we're only going to paint within
89 // mBufferRect
90 nsIntRect quadrantRect = GetQuadrantRectangle(aXSide, aYSide);
91 nsIntRect fillRect;
92 if (!fillRect.IntersectRect(mBufferRect, quadrantRect))
93 return;
95 gfx::Point quadrantTranslation(quadrantRect.x, quadrantRect.y);
97 MOZ_ASSERT(aOperator == CompositionOp::OP_OVER || aOperator == CompositionOp::OP_SOURCE);
98 // direct2d is much slower when using OP_SOURCE so use OP_OVER and
99 // (maybe) a clear instead. Normally we need to draw in a single operation
100 // (to avoid flickering) but direct2d is ok since it defers rendering.
101 // We should try abstract this logic in a helper when we have other use
102 // cases.
103 if (aTarget->GetType() == BackendType::DIRECT2D && aOperator == CompositionOp::OP_SOURCE) {
104 aOperator = CompositionOp::OP_OVER;
105 if (mDTBuffer->GetFormat() == SurfaceFormat::B8G8R8A8) {
106 aTarget->ClearRect(ToRect(fillRect));
110 RefPtr<gfx::SourceSurface> snapshot;
111 if (aSource == BUFFER_BLACK) {
112 snapshot = mDTBuffer->Snapshot();
113 } else {
114 MOZ_ASSERT(aSource == BUFFER_WHITE);
115 snapshot = mDTBufferOnWhite->Snapshot();
118 if (aOperator == CompositionOp::OP_SOURCE) {
119 // OP_SOURCE is unbounded in Azure, and we really don't want that behaviour here.
120 // We also can't do a ClearRect+FillRect since we need the drawing to happen
121 // as an atomic operation (to prevent flickering).
122 aTarget->PushClipRect(gfx::Rect(fillRect.x, fillRect.y,
123 fillRect.width, fillRect.height));
126 if (aMask) {
127 // Transform from user -> buffer space.
128 Matrix transform;
129 transform.Translate(quadrantTranslation.x, quadrantTranslation.y);
131 #ifdef MOZ_GFX_OPTIMIZE_MOBILE
132 SurfacePattern source(snapshot, ExtendMode::CLAMP, transform, Filter::POINT);
133 #else
134 SurfacePattern source(snapshot, ExtendMode::CLAMP, transform);
135 #endif
137 Matrix oldTransform = aTarget->GetTransform();
138 aTarget->SetTransform(*aMaskTransform);
139 aTarget->MaskSurface(source, aMask, Point(0, 0), DrawOptions(aOpacity, aOperator));
140 aTarget->SetTransform(oldTransform);
141 } else {
142 #ifdef MOZ_GFX_OPTIMIZE_MOBILE
143 DrawSurfaceOptions options(Filter::POINT);
144 #else
145 DrawSurfaceOptions options;
146 #endif
147 aTarget->DrawSurface(snapshot, ToRect(fillRect),
148 GetSourceRectangle(aXSide, aYSide),
149 options,
150 DrawOptions(aOpacity, aOperator));
153 if (aOperator == CompositionOp::OP_SOURCE) {
154 aTarget->PopClip();
158 void
159 RotatedBuffer::DrawBufferWithRotation(gfx::DrawTarget *aTarget, ContextSource aSource,
160 float aOpacity,
161 gfx::CompositionOp aOperator,
162 gfx::SourceSurface* aMask,
163 const gfx::Matrix* aMaskTransform) const
165 PROFILER_LABEL("RotatedBuffer", "DrawBufferWithRotation");
166 // See above, in Azure Repeat should always be a safe, even faster choice
167 // though! Particularly on D2D Repeat should be a lot faster, need to look
168 // into that. TODO[Bas]
169 DrawBufferQuadrant(aTarget, LEFT, TOP, aSource, aOpacity, aOperator, aMask, aMaskTransform);
170 DrawBufferQuadrant(aTarget, RIGHT, TOP, aSource, aOpacity, aOperator, aMask, aMaskTransform);
171 DrawBufferQuadrant(aTarget, LEFT, BOTTOM, aSource, aOpacity, aOperator, aMask, aMaskTransform);
172 DrawBufferQuadrant(aTarget, RIGHT, BOTTOM, aSource, aOpacity, aOperator,aMask, aMaskTransform);
175 /* static */ bool
176 RotatedContentBuffer::IsClippingCheap(DrawTarget* aTarget, const nsIntRegion& aRegion)
178 // Assume clipping is cheap if the draw target just has an integer
179 // translation, and the visible region is simple.
180 return !aTarget->GetTransform().HasNonIntegerTranslation() &&
181 aRegion.GetNumRects() <= 1;
184 void
185 RotatedContentBuffer::DrawTo(ThebesLayer* aLayer,
186 DrawTarget* aTarget,
187 float aOpacity,
188 CompositionOp aOp,
189 gfxASurface* aMask,
190 const Matrix* aMaskTransform)
192 if (!EnsureBuffer()) {
193 return;
196 bool clipped = false;
198 // If the entire buffer is valid, we can just draw the whole thing,
199 // no need to clip. But we'll still clip if clipping is cheap ---
200 // that might let us copy a smaller region of the buffer.
201 // Also clip to the visible region if we're told to.
202 if (!aLayer->GetValidRegion().Contains(BufferRect()) ||
203 (ToData(aLayer)->GetClipToVisibleRegion() &&
204 !aLayer->GetVisibleRegion().Contains(BufferRect())) ||
205 IsClippingCheap(aTarget, aLayer->GetEffectiveVisibleRegion())) {
206 // We don't want to draw invalid stuff, so we need to clip. Might as
207 // well clip to the smallest area possible --- the visible region.
208 // Bug 599189 if there is a non-integer-translation transform in aTarget,
209 // we might sample pixels outside GetEffectiveVisibleRegion(), which is wrong
210 // and may cause gray lines.
211 gfxUtils::ClipToRegionSnapped(aTarget, aLayer->GetEffectiveVisibleRegion());
212 clipped = true;
215 RefPtr<gfx::SourceSurface> mask;
216 if (aMask) {
217 mask = gfxPlatform::GetPlatform()->GetSourceSurfaceForSurface(aTarget, aMask);
220 Matrix maskTransform;
221 if (aMaskTransform) {
222 maskTransform = *aMaskTransform;
225 DrawBufferWithRotation(aTarget, BUFFER_BLACK, aOpacity, aOp, mask, &maskTransform);
226 if (clipped) {
227 aTarget->PopClip();
231 DrawTarget*
232 RotatedContentBuffer::BorrowDrawTargetForQuadrantUpdate(const nsIntRect& aBounds,
233 ContextSource aSource)
235 if (!EnsureBuffer()) {
236 return nullptr;
239 MOZ_ASSERT(!mLoanedDrawTarget, "draw target has been borrowed and not returned");
240 if (aSource == BUFFER_BOTH && HaveBufferOnWhite()) {
241 if (!EnsureBufferOnWhite()) {
242 return nullptr;
244 MOZ_ASSERT(mDTBuffer && mDTBufferOnWhite);
245 mLoanedDrawTarget = Factory::CreateDualDrawTarget(mDTBuffer, mDTBufferOnWhite);
246 } else if (aSource == BUFFER_WHITE) {
247 if (!EnsureBufferOnWhite()) {
248 return nullptr;
250 mLoanedDrawTarget = mDTBufferOnWhite;
251 } else {
252 // BUFFER_BLACK, or BUFFER_BOTH with a single buffer.
253 mLoanedDrawTarget = mDTBuffer;
256 // Figure out which quadrant to draw in
257 int32_t xBoundary = mBufferRect.XMost() - mBufferRotation.x;
258 int32_t yBoundary = mBufferRect.YMost() - mBufferRotation.y;
259 XSide sideX = aBounds.XMost() <= xBoundary ? RIGHT : LEFT;
260 YSide sideY = aBounds.YMost() <= yBoundary ? BOTTOM : TOP;
261 nsIntRect quadrantRect = GetQuadrantRectangle(sideX, sideY);
262 NS_ASSERTION(quadrantRect.Contains(aBounds), "Messed up quadrants");
264 mLoanedTransform = mLoanedDrawTarget->GetTransform();
265 mLoanedTransform.Translate(-quadrantRect.x, -quadrantRect.y);
266 mLoanedDrawTarget->SetTransform(mLoanedTransform);
267 mLoanedTransform.Translate(quadrantRect.x, quadrantRect.y);
269 return mLoanedDrawTarget;
272 void
273 BorrowDrawTarget::ReturnDrawTarget(gfx::DrawTarget*& aReturned)
275 MOZ_ASSERT(aReturned == mLoanedDrawTarget);
276 mLoanedDrawTarget->SetTransform(mLoanedTransform);
277 mLoanedDrawTarget = nullptr;
278 aReturned = nullptr;
281 gfxContentType
282 RotatedContentBuffer::BufferContentType()
284 if (mDeprecatedBufferProvider) {
285 return mDeprecatedBufferProvider->GetContentType();
287 if (mBufferProvider || mDTBuffer) {
288 SurfaceFormat format;
290 if (mBufferProvider) {
291 format = mBufferProvider->AsTextureClientDrawTarget()->GetFormat();
292 } else if (mDTBuffer) {
293 format = mDTBuffer->GetFormat();
296 return ContentForFormat(format);
298 return gfxContentType::SENTINEL;
301 bool
302 RotatedContentBuffer::BufferSizeOkFor(const nsIntSize& aSize)
304 return (aSize == mBufferRect.Size() ||
305 (SizedToVisibleBounds != mBufferSizePolicy &&
306 aSize < mBufferRect.Size()));
309 bool
310 RotatedContentBuffer::EnsureBuffer()
312 NS_ASSERTION(!mLoanedDrawTarget, "Loaned draw target must be returned");
313 if (!mDTBuffer) {
314 if (mDeprecatedBufferProvider) {
315 mDTBuffer = mDeprecatedBufferProvider->LockDrawTarget();
316 } else if (mBufferProvider) {
317 mDTBuffer = mBufferProvider->AsTextureClientDrawTarget()->GetAsDrawTarget();
321 NS_WARN_IF_FALSE(mDTBuffer, "no buffer");
322 return !!mDTBuffer;
325 bool
326 RotatedContentBuffer::EnsureBufferOnWhite()
328 NS_ASSERTION(!mLoanedDrawTarget, "Loaned draw target must be returned");
329 if (!mDTBufferOnWhite) {
330 if (mDeprecatedBufferProviderOnWhite) {
331 mDTBufferOnWhite = mDeprecatedBufferProviderOnWhite->LockDrawTarget();
332 } else if (mBufferProviderOnWhite) {
333 mDTBufferOnWhite =
334 mBufferProviderOnWhite->AsTextureClientDrawTarget()->GetAsDrawTarget();
338 NS_WARN_IF_FALSE(mDTBufferOnWhite, "no buffer");
339 return mDTBufferOnWhite;
342 bool
343 RotatedContentBuffer::HaveBuffer() const
345 return mDTBuffer || mDeprecatedBufferProvider || mBufferProvider;
348 bool
349 RotatedContentBuffer::HaveBufferOnWhite() const
351 return mDTBufferOnWhite || mDeprecatedBufferProviderOnWhite || mBufferProviderOnWhite;
354 static void
355 WrapRotationAxis(int32_t* aRotationPoint, int32_t aSize)
357 if (*aRotationPoint < 0) {
358 *aRotationPoint += aSize;
359 } else if (*aRotationPoint >= aSize) {
360 *aRotationPoint -= aSize;
364 static nsIntRect
365 ComputeBufferRect(const nsIntRect& aRequestedRect)
367 nsIntRect rect(aRequestedRect);
368 // Set a minimum width to guarantee a minimum size of buffers we
369 // allocate (and work around problems on some platforms with smaller
370 // dimensions). 64 is the magic number needed to work around the
371 // rendering glitch, and guarantees image rows can be SIMD'd for
372 // even r5g6b5 surfaces pretty much everywhere.
373 rect.width = std::max(aRequestedRect.width, 64);
374 #ifdef MOZ_WIDGET_GONK
375 // Set a minumum height to guarantee a minumum height of buffers we
376 // allocate. Some GL implementations fail to render gralloc textures
377 // with a height 9px-16px. It happens on Adreno 200. Adreno 320 does not
378 // have this problem. 32 is choosed as alignment of gralloc buffers.
379 // See Bug 873937.
380 // Increase the height only when the requested height is more than 0.
381 // See Bug 895976.
382 // XXX it might be better to disable it on the gpu that does not have
383 // the height problem.
384 if (rect.height > 0) {
385 rect.height = std::max(aRequestedRect.height, 32);
387 #endif
388 return rect;
391 void
392 RotatedContentBuffer::FlushBuffers()
394 if (mDTBuffer) {
395 mDTBuffer->Flush();
397 if (mDTBufferOnWhite) {
398 mDTBufferOnWhite->Flush();
402 RotatedContentBuffer::PaintState
403 RotatedContentBuffer::BeginPaint(ThebesLayer* aLayer,
404 uint32_t aFlags)
406 PaintState result;
407 // We need to disable rotation if we're going to be resampled when
408 // drawing, because we might sample across the rotation boundary.
409 bool canHaveRotation = gfxPlatform::BufferRotationEnabled() &&
410 !(aFlags & (PAINT_WILL_RESAMPLE | PAINT_NO_ROTATION));
412 nsIntRegion validRegion = aLayer->GetValidRegion();
414 bool canUseOpaqueSurface = aLayer->CanUseOpaqueSurface();
415 ContentType layerContentType =
416 canUseOpaqueSurface ? gfxContentType::COLOR :
417 gfxContentType::COLOR_ALPHA;
419 SurfaceMode mode;
420 nsIntRegion neededRegion;
421 bool canReuseBuffer;
422 nsIntRect destBufferRect;
424 while (true) {
425 mode = aLayer->GetSurfaceMode();
426 neededRegion = aLayer->GetVisibleRegion();
427 canReuseBuffer = HaveBuffer() && BufferSizeOkFor(neededRegion.GetBounds().Size());
428 result.mContentType = layerContentType;
430 if (canReuseBuffer) {
431 if (mBufferRect.Contains(neededRegion.GetBounds())) {
432 // We don't need to adjust mBufferRect.
433 destBufferRect = mBufferRect;
434 } else if (neededRegion.GetBounds().Size() <= mBufferRect.Size()) {
435 // The buffer's big enough but doesn't contain everything that's
436 // going to be visible. We'll move it.
437 destBufferRect = nsIntRect(neededRegion.GetBounds().TopLeft(), mBufferRect.Size());
438 } else {
439 destBufferRect = neededRegion.GetBounds();
441 } else {
442 // We won't be reusing the buffer. Compute a new rect.
443 destBufferRect = ComputeBufferRect(neededRegion.GetBounds());
446 if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
447 #if defined(MOZ_GFX_OPTIMIZE_MOBILE) || defined(MOZ_WIDGET_GONK)
448 mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA;
449 #else
450 if (!aLayer->GetParent() ||
451 !aLayer->GetParent()->SupportsComponentAlphaChildren() ||
452 !aLayer->Manager()->IsCompositingCheap() ||
453 !aLayer->AsShadowableLayer() ||
454 !aLayer->AsShadowableLayer()->HasShadow() ||
455 !gfxPlatform::ComponentAlphaEnabled()) {
456 mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA;
457 } else {
458 result.mContentType = gfxContentType::COLOR;
460 #endif
463 if ((aFlags & PAINT_WILL_RESAMPLE) &&
464 (!neededRegion.GetBounds().IsEqualInterior(destBufferRect) ||
465 neededRegion.GetNumRects() > 1)) {
466 // The area we add to neededRegion might not be painted opaquely
467 if (mode == SurfaceMode::SURFACE_OPAQUE) {
468 result.mContentType = gfxContentType::COLOR_ALPHA;
469 mode = SurfaceMode::SURFACE_SINGLE_CHANNEL_ALPHA;
472 // We need to validate the entire buffer, to make sure that only valid
473 // pixels are sampled
474 neededRegion = destBufferRect;
477 // If we have an existing buffer, but the content type has changed or we
478 // have transitioned into/out of component alpha, then we need to recreate it.
479 if (HaveBuffer() &&
480 (result.mContentType != BufferContentType() ||
481 (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) != HaveBufferOnWhite())) {
483 // We're effectively clearing the valid region, so we need to draw
484 // the entire needed region now.
485 result.mRegionToInvalidate = aLayer->GetValidRegion();
486 validRegion.SetEmpty();
487 Clear();
488 // Restart decision process with the cleared buffer. We can only go
489 // around the loop one more iteration, since mDTBuffer is null now.
490 continue;
493 break;
496 NS_ASSERTION(destBufferRect.Contains(neededRegion.GetBounds()),
497 "Destination rect doesn't contain what we need to paint");
499 result.mRegionToDraw.Sub(neededRegion, validRegion);
501 // Do not modify result.mRegionToDraw or result.mContentType after this call.
502 // Do not modify mBufferRect, mBufferRotation, or mDidSelfCopy,
503 // or call CreateBuffer before this call.
504 FinalizeFrame(result.mRegionToDraw);
506 if (result.mRegionToDraw.IsEmpty())
507 return result;
509 nsIntRect drawBounds = result.mRegionToDraw.GetBounds();
510 RefPtr<DrawTarget> destDTBuffer;
511 RefPtr<DrawTarget> destDTBufferOnWhite;
512 uint32_t bufferFlags = canHaveRotation ? ALLOW_REPEAT : 0;
513 if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
514 bufferFlags |= BUFFER_COMPONENT_ALPHA;
516 if (canReuseBuffer) {
517 if (!EnsureBuffer()) {
518 return result;
520 nsIntRect keepArea;
521 if (keepArea.IntersectRect(destBufferRect, mBufferRect)) {
522 // Set mBufferRotation so that the pixels currently in mDTBuffer
523 // will still be rendered in the right place when mBufferRect
524 // changes to destBufferRect.
525 nsIntPoint newRotation = mBufferRotation +
526 (destBufferRect.TopLeft() - mBufferRect.TopLeft());
527 WrapRotationAxis(&newRotation.x, mBufferRect.width);
528 WrapRotationAxis(&newRotation.y, mBufferRect.height);
529 NS_ASSERTION(nsIntRect(nsIntPoint(0,0), mBufferRect.Size()).Contains(newRotation),
530 "newRotation out of bounds");
531 int32_t xBoundary = destBufferRect.XMost() - newRotation.x;
532 int32_t yBoundary = destBufferRect.YMost() - newRotation.y;
533 if ((drawBounds.x < xBoundary && xBoundary < drawBounds.XMost()) ||
534 (drawBounds.y < yBoundary && yBoundary < drawBounds.YMost()) ||
535 (newRotation != nsIntPoint(0,0) && !canHaveRotation)) {
536 // The stuff we need to redraw will wrap around an edge of the
537 // buffer, so move the pixels we can keep into a position that
538 // lets us redraw in just one quadrant.
539 if (mBufferRotation == nsIntPoint(0,0)) {
540 nsIntRect srcRect(nsIntPoint(0, 0), mBufferRect.Size());
541 nsIntPoint dest = mBufferRect.TopLeft() - destBufferRect.TopLeft();
542 MOZ_ASSERT(mDTBuffer);
543 mDTBuffer->CopyRect(IntRect(srcRect.x, srcRect.y, srcRect.width, srcRect.height),
544 IntPoint(dest.x, dest.y));
545 if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
546 if (!EnsureBufferOnWhite()) {
547 return result;
549 MOZ_ASSERT(mDTBufferOnWhite);
550 mDTBufferOnWhite->CopyRect(IntRect(srcRect.x, srcRect.y, srcRect.width, srcRect.height),
551 IntPoint(dest.x, dest.y));
553 result.mDidSelfCopy = true;
554 mDidSelfCopy = true;
555 // Don't set destBuffer; we special-case self-copies, and
556 // just did the necessary work above.
557 mBufferRect = destBufferRect;
558 } else {
559 // With azure and a data surface perform an buffer unrotate
560 // (SelfCopy).
561 unsigned char* data;
562 IntSize size;
563 int32_t stride;
564 SurfaceFormat format;
566 if (mDTBuffer->LockBits(&data, &size, &stride, &format)) {
567 uint8_t bytesPerPixel = BytesPerPixel(format);
568 BufferUnrotate(data,
569 size.width * bytesPerPixel,
570 size.height, stride,
571 newRotation.x * bytesPerPixel, newRotation.y);
572 mDTBuffer->ReleaseBits(data);
574 if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
575 if (!EnsureBufferOnWhite()) {
576 return result;
578 MOZ_ASSERT(mDTBufferOnWhite);
579 mDTBufferOnWhite->LockBits(&data, &size, &stride, &format);
580 uint8_t bytesPerPixel = BytesPerPixel(format);
581 BufferUnrotate(data,
582 size.width * bytesPerPixel,
583 size.height, stride,
584 newRotation.x * bytesPerPixel, newRotation.y);
585 mDTBufferOnWhite->ReleaseBits(data);
588 // Buffer unrotate moves all the pixels, note that
589 // we self copied for SyncBackToFrontBuffer
590 result.mDidSelfCopy = true;
591 mDidSelfCopy = true;
592 mBufferRect = destBufferRect;
593 mBufferRotation = nsIntPoint(0, 0);
596 if (!result.mDidSelfCopy) {
597 destBufferRect = ComputeBufferRect(neededRegion.GetBounds());
598 CreateBuffer(result.mContentType, destBufferRect, bufferFlags,
599 &destDTBuffer, &destDTBufferOnWhite);
600 if (!destDTBuffer) {
601 return result;
605 } else {
606 mBufferRect = destBufferRect;
607 mBufferRotation = newRotation;
609 } else {
610 // No pixels are going to be kept. The whole visible region
611 // will be redrawn, so we don't need to copy anything, so we don't
612 // set destBuffer.
613 mBufferRect = destBufferRect;
614 mBufferRotation = nsIntPoint(0,0);
616 } else {
617 // The buffer's not big enough, so allocate a new one
618 CreateBuffer(result.mContentType, destBufferRect, bufferFlags,
619 &destDTBuffer, &destDTBufferOnWhite);
620 if (!destDTBuffer) {
621 return result;
625 NS_ASSERTION(!(aFlags & PAINT_WILL_RESAMPLE) || destBufferRect == neededRegion.GetBounds(),
626 "If we're resampling, we need to validate the entire buffer");
628 // If we have no buffered data already, then destBuffer will be a fresh buffer
629 // and we do not need to clear it below.
630 bool isClear = !HaveBuffer();
632 if (destDTBuffer) {
633 if (!isClear && (mode != SurfaceMode::SURFACE_COMPONENT_ALPHA || HaveBufferOnWhite())) {
634 // Copy the bits
635 nsIntPoint offset = -destBufferRect.TopLeft();
636 Matrix mat;
637 mat.Translate(offset.x, offset.y);
638 destDTBuffer->SetTransform(mat);
639 if (!EnsureBuffer()) {
640 return result;
642 MOZ_ASSERT(mDTBuffer, "Have we got a Thebes buffer for some reason?");
643 DrawBufferWithRotation(destDTBuffer, BUFFER_BLACK, 1.0, CompositionOp::OP_SOURCE);
644 destDTBuffer->SetTransform(Matrix());
646 if (mode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
647 NS_ASSERTION(destDTBufferOnWhite, "Must have a white buffer!");
648 destDTBufferOnWhite->SetTransform(mat);
649 if (!EnsureBufferOnWhite()) {
650 return result;
652 MOZ_ASSERT(mDTBufferOnWhite, "Have we got a Thebes buffer for some reason?");
653 DrawBufferWithRotation(destDTBufferOnWhite, BUFFER_WHITE, 1.0, CompositionOp::OP_SOURCE);
654 destDTBufferOnWhite->SetTransform(Matrix());
658 mDTBuffer = destDTBuffer.forget();
659 mDTBufferOnWhite = destDTBufferOnWhite.forget();
660 mBufferRect = destBufferRect;
661 mBufferRotation = nsIntPoint(0,0);
663 NS_ASSERTION(canHaveRotation || mBufferRotation == nsIntPoint(0,0),
664 "Rotation disabled, but we have nonzero rotation?");
666 nsIntRegion invalidate;
667 invalidate.Sub(aLayer->GetValidRegion(), destBufferRect);
668 result.mRegionToInvalidate.Or(result.mRegionToInvalidate, invalidate);
669 result.mClip = DrawRegionClip::DRAW_SNAPPED;
670 result.mMode = mode;
672 return result;
675 DrawTarget*
676 RotatedContentBuffer::BorrowDrawTargetForPainting(ThebesLayer* aLayer,
677 const PaintState& aPaintState)
679 if (aPaintState.mMode == SurfaceMode::SURFACE_NONE) {
680 return nullptr;
683 DrawTarget* result = BorrowDrawTargetForQuadrantUpdate(aPaintState.mRegionToDraw.GetBounds(),
684 BUFFER_BOTH);
686 if (aPaintState.mMode == SurfaceMode::SURFACE_COMPONENT_ALPHA) {
687 MOZ_ASSERT(mDTBuffer && mDTBufferOnWhite);
688 nsIntRegionRectIterator iter(aPaintState.mRegionToDraw);
689 const nsIntRect *iterRect;
690 while ((iterRect = iter.Next())) {
691 mDTBuffer->FillRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height),
692 ColorPattern(Color(0.0, 0.0, 0.0, 1.0)));
693 mDTBufferOnWhite->FillRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height),
694 ColorPattern(Color(1.0, 1.0, 1.0, 1.0)));
696 } else if (aPaintState.mContentType == gfxContentType::COLOR_ALPHA && HaveBuffer()) {
697 // HaveBuffer() => we have an existing buffer that we must clear
698 nsIntRegionRectIterator iter(aPaintState.mRegionToDraw);
699 const nsIntRect *iterRect;
700 while ((iterRect = iter.Next())) {
701 result->ClearRect(Rect(iterRect->x, iterRect->y, iterRect->width, iterRect->height));
705 return result;