michael@0: /* -*- Mode: C++; tab-width: 20; 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 "DisplayItemClip.h" michael@0: michael@0: #include "gfxContext.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsCSSRendering.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsRegion.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: void michael@0: DisplayItemClip::SetTo(const nsRect& aRect) michael@0: { michael@0: mHaveClipRect = true; michael@0: mClipRect = aRect; michael@0: mRoundedClipRects.Clear(); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::SetTo(const nsRect& aRect, const nscoord* aRadii) michael@0: { michael@0: mHaveClipRect = true; michael@0: mClipRect = aRect; michael@0: mRoundedClipRects.SetLength(1); michael@0: mRoundedClipRects[0].mRect = aRect; michael@0: memcpy(mRoundedClipRects[0].mRadii, aRadii, sizeof(nscoord)*8); michael@0: } michael@0: michael@0: bool michael@0: DisplayItemClip::MayIntersect(const nsRect& aRect) const michael@0: { michael@0: if (!mHaveClipRect) { michael@0: return !aRect.IsEmpty(); michael@0: } michael@0: nsRect r = aRect.Intersect(mClipRect); michael@0: if (r.IsEmpty()) { michael@0: return false; michael@0: } michael@0: for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) { michael@0: const RoundedRect& rr = mRoundedClipRects[i]; michael@0: if (!nsLayoutUtils::RoundedRectIntersectsRect(rr.mRect, rr.mRadii, r)) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::IntersectWith(const DisplayItemClip& aOther) michael@0: { michael@0: if (!aOther.mHaveClipRect) { michael@0: return; michael@0: } michael@0: if (!mHaveClipRect) { michael@0: *this = aOther; michael@0: return; michael@0: } michael@0: if (!mClipRect.IntersectRect(mClipRect, aOther.mClipRect)) { michael@0: mRoundedClipRects.Clear(); michael@0: return; michael@0: } michael@0: mRoundedClipRects.AppendElements(aOther.mRoundedClipRects); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::ApplyTo(gfxContext* aContext, michael@0: nsPresContext* aPresContext, michael@0: uint32_t aBegin, uint32_t aEnd) michael@0: { michael@0: int32_t A2D = aPresContext->AppUnitsPerDevPixel(); michael@0: ApplyRectTo(aContext, A2D); michael@0: ApplyRoundedRectsTo(aContext, A2D, aBegin, aEnd); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::ApplyRectTo(gfxContext* aContext, int32_t A2D) const michael@0: { michael@0: aContext->NewPath(); michael@0: gfxRect clip = nsLayoutUtils::RectToGfxRect(mClipRect, A2D); michael@0: aContext->Rectangle(clip, true); michael@0: aContext->Clip(); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::ApplyRoundedRectsTo(gfxContext* aContext, michael@0: int32_t A2D, michael@0: uint32_t aBegin, uint32_t aEnd) const michael@0: { michael@0: aEnd = std::min(aEnd, mRoundedClipRects.Length()); michael@0: michael@0: for (uint32_t i = aBegin; i < aEnd; ++i) { michael@0: AddRoundedRectPathTo(aContext, A2D, mRoundedClipRects[i]); michael@0: aContext->Clip(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::DrawRoundedRectsTo(gfxContext* aContext, michael@0: int32_t A2D, michael@0: uint32_t aBegin, uint32_t aEnd) const michael@0: { michael@0: aEnd = std::min(aEnd, mRoundedClipRects.Length()); michael@0: michael@0: if (aEnd - aBegin == 0) michael@0: return; michael@0: michael@0: // If there is just one rounded rect we can just fill it, if there are more then we michael@0: // must clip the rest to get the intersection of clips michael@0: ApplyRoundedRectsTo(aContext, A2D, aBegin, aEnd - 1); michael@0: AddRoundedRectPathTo(aContext, A2D, mRoundedClipRects[aEnd - 1]); michael@0: aContext->Fill(); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::AddRoundedRectPathTo(gfxContext* aContext, michael@0: int32_t A2D, michael@0: const RoundedRect &aRoundRect) const michael@0: { michael@0: gfxCornerSizes pixelRadii; michael@0: nsCSSRendering::ComputePixelRadii(aRoundRect.mRadii, A2D, &pixelRadii); michael@0: michael@0: gfxRect clip = nsLayoutUtils::RectToGfxRect(aRoundRect.mRect, A2D); michael@0: clip.Round(); michael@0: clip.Condition(); michael@0: michael@0: aContext->NewPath(); michael@0: aContext->RoundedRectangle(clip, pixelRadii); michael@0: } michael@0: michael@0: nsRect michael@0: DisplayItemClip::ApproximateIntersectInward(const nsRect& aRect) const michael@0: { michael@0: nsRect r = aRect; michael@0: if (mHaveClipRect) { michael@0: r.IntersectRect(r, mClipRect); michael@0: } michael@0: for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); michael@0: i < iEnd; ++i) { michael@0: const RoundedRect &rr = mRoundedClipRects[i]; michael@0: nsRegion rgn = nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, r); michael@0: r = rgn.GetLargestRectangle(); michael@0: } michael@0: return r; michael@0: } michael@0: michael@0: // Test if (aXPoint, aYPoint) is in the ellipse with center (aXCenter, aYCenter) michael@0: // and radii aXRadius, aYRadius. michael@0: bool IsInsideEllipse(nscoord aXRadius, nscoord aXCenter, nscoord aXPoint, michael@0: nscoord aYRadius, nscoord aYCenter, nscoord aYPoint) michael@0: { michael@0: float scaledX = float(aXPoint - aXCenter) / float(aXRadius); michael@0: float scaledY = float(aYPoint - aYCenter) / float(aYRadius); michael@0: return scaledX * scaledX + scaledY * scaledY < 1.0f; michael@0: } michael@0: michael@0: bool michael@0: DisplayItemClip::IsRectClippedByRoundedCorner(const nsRect& aRect) const michael@0: { michael@0: if (mRoundedClipRects.IsEmpty()) michael@0: return false; michael@0: michael@0: nsRect rect; michael@0: rect.IntersectRect(aRect, NonRoundedIntersection()); michael@0: for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); michael@0: i < iEnd; ++i) { michael@0: const RoundedRect &rr = mRoundedClipRects[i]; michael@0: // top left michael@0: if (rect.x < rr.mRect.x + rr.mRadii[NS_CORNER_TOP_LEFT_X] && michael@0: rect.y < rr.mRect.y + rr.mRadii[NS_CORNER_TOP_LEFT_Y]) { michael@0: if (!IsInsideEllipse(rr.mRadii[NS_CORNER_TOP_LEFT_X], michael@0: rr.mRect.x + rr.mRadii[NS_CORNER_TOP_LEFT_X], michael@0: rect.x, michael@0: rr.mRadii[NS_CORNER_TOP_LEFT_Y], michael@0: rr.mRect.y + rr.mRadii[NS_CORNER_TOP_LEFT_Y], michael@0: rect.y)) { michael@0: return true; michael@0: } michael@0: } michael@0: // top right michael@0: if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[NS_CORNER_TOP_RIGHT_X] && michael@0: rect.y < rr.mRect.y + rr.mRadii[NS_CORNER_TOP_RIGHT_Y]) { michael@0: if (!IsInsideEllipse(rr.mRadii[NS_CORNER_TOP_RIGHT_X], michael@0: rr.mRect.XMost() - rr.mRadii[NS_CORNER_TOP_RIGHT_X], michael@0: rect.XMost(), michael@0: rr.mRadii[NS_CORNER_TOP_RIGHT_Y], michael@0: rr.mRect.y + rr.mRadii[NS_CORNER_TOP_RIGHT_Y], michael@0: rect.y)) { michael@0: return true; michael@0: } michael@0: } michael@0: // bottom left michael@0: if (rect.x < rr.mRect.x + rr.mRadii[NS_CORNER_BOTTOM_LEFT_X] && michael@0: rect.YMost() > rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y]) { michael@0: if (!IsInsideEllipse(rr.mRadii[NS_CORNER_BOTTOM_LEFT_X], michael@0: rr.mRect.x + rr.mRadii[NS_CORNER_BOTTOM_LEFT_X], michael@0: rect.x, michael@0: rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y], michael@0: rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y], michael@0: rect.YMost())) { michael@0: return true; michael@0: } michael@0: } michael@0: // bottom right michael@0: if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X] && michael@0: rect.YMost() > rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y]) { michael@0: if (!IsInsideEllipse(rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X], michael@0: rr.mRect.XMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X], michael@0: rect.XMost(), michael@0: rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y], michael@0: rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y], michael@0: rect.YMost())) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: nsRect michael@0: DisplayItemClip::NonRoundedIntersection() const michael@0: { michael@0: NS_ASSERTION(mHaveClipRect, "Must have a clip rect!"); michael@0: nsRect result = mClipRect; michael@0: for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); michael@0: i < iEnd; ++i) { michael@0: result.IntersectRect(result, mRoundedClipRects[i].mRect); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: bool michael@0: DisplayItemClip::IsRectAffectedByClip(const nsRect& aRect) const michael@0: { michael@0: if (mHaveClipRect && !mClipRect.Contains(aRect)) { michael@0: return true; michael@0: } michael@0: for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); michael@0: i < iEnd; ++i) { michael@0: const RoundedRect &rr = mRoundedClipRects[i]; michael@0: nsRegion rgn = nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, aRect); michael@0: if (!rgn.Contains(aRect)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: nsRect michael@0: DisplayItemClip::ApplyNonRoundedIntersection(const nsRect& aRect) const michael@0: { michael@0: if (!mHaveClipRect) { michael@0: return aRect; michael@0: } michael@0: michael@0: nsRect result = aRect.Intersect(mClipRect); michael@0: for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); michael@0: i < iEnd; ++i) { michael@0: result.Intersect(mRoundedClipRects[i].mRect); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::RemoveRoundedCorners() michael@0: { michael@0: if (mRoundedClipRects.IsEmpty()) michael@0: return; michael@0: michael@0: mClipRect = NonRoundedIntersection(); michael@0: mRoundedClipRects.Clear(); michael@0: } michael@0: michael@0: static void michael@0: AccumulateRectDifference(const nsRect& aR1, const nsRect& aR2, nsRegion* aOut) michael@0: { michael@0: if (aR1.IsEqualInterior(aR2)) michael@0: return; michael@0: nsRegion r; michael@0: r.Xor(aR1, aR2); michael@0: aOut->Or(*aOut, r); michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::AddOffsetAndComputeDifference(const nsPoint& aOffset, michael@0: const nsRect& aBounds, michael@0: const DisplayItemClip& aOther, michael@0: const nsRect& aOtherBounds, michael@0: nsRegion* aDifference) michael@0: { michael@0: if (mHaveClipRect != aOther.mHaveClipRect || michael@0: mRoundedClipRects.Length() != aOther.mRoundedClipRects.Length()) { michael@0: aDifference->Or(*aDifference, aBounds); michael@0: aDifference->Or(*aDifference, aOtherBounds); michael@0: return; michael@0: } michael@0: if (mHaveClipRect) { michael@0: AccumulateRectDifference((mClipRect + aOffset).Intersect(aBounds), michael@0: aOther.mClipRect.Intersect(aOtherBounds), michael@0: aDifference); michael@0: } michael@0: for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) { michael@0: if (mRoundedClipRects[i] + aOffset != aOther.mRoundedClipRects[i]) { michael@0: // The corners make it tricky so we'll just add both rects here. michael@0: aDifference->Or(*aDifference, mRoundedClipRects[i].mRect.Intersect(aBounds)); michael@0: aDifference->Or(*aDifference, aOther.mRoundedClipRects[i].mRect.Intersect(aOtherBounds)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: uint32_t michael@0: DisplayItemClip::GetCommonRoundedRectCount(const DisplayItemClip& aOther, michael@0: uint32_t aMax) const michael@0: { michael@0: uint32_t end = std::min(std::min(mRoundedClipRects.Length(), aMax), michael@0: aOther.mRoundedClipRects.Length()); michael@0: uint32_t clipCount = 0; michael@0: for (; clipCount < end; ++clipCount) { michael@0: if (mRoundedClipRects[clipCount] != michael@0: aOther.mRoundedClipRects[clipCount]) { michael@0: return clipCount; michael@0: } michael@0: } michael@0: return clipCount; michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::AppendRoundedRects(nsTArray* aArray, uint32_t aCount) const michael@0: { michael@0: uint32_t count = std::min(mRoundedClipRects.Length(), aCount); michael@0: for (uint32_t i = 0; i < count; ++i) { michael@0: *aArray->AppendElement() = mRoundedClipRects[i]; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: DisplayItemClip::ComputeRegionInClips(DisplayItemClip* aOldClip, michael@0: const nsPoint& aShift, michael@0: nsRegion* aCombined) const michael@0: { michael@0: if (!mHaveClipRect || (aOldClip && !aOldClip->mHaveClipRect)) { michael@0: return false; michael@0: } michael@0: michael@0: if (aOldClip) { michael@0: *aCombined = aOldClip->NonRoundedIntersection(); michael@0: aCombined->MoveBy(aShift); michael@0: aCombined->Or(*aCombined, NonRoundedIntersection()); michael@0: } else { michael@0: *aCombined = NonRoundedIntersection(); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::MoveBy(nsPoint aPoint) michael@0: { michael@0: if (!mHaveClipRect) michael@0: return; michael@0: mClipRect += aPoint; michael@0: for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) { michael@0: mRoundedClipRects[i].mRect += aPoint; michael@0: } michael@0: } michael@0: michael@0: static DisplayItemClip* gNoClip; michael@0: michael@0: const DisplayItemClip& michael@0: DisplayItemClip::NoClip() michael@0: { michael@0: if (!gNoClip) { michael@0: gNoClip = new DisplayItemClip(); michael@0: } michael@0: return *gNoClip; michael@0: } michael@0: michael@0: void michael@0: DisplayItemClip::Shutdown() michael@0: { michael@0: delete gNoClip; michael@0: gNoClip = nullptr; michael@0: } michael@0: michael@0: #ifdef MOZ_DUMP_PAINTING michael@0: nsCString michael@0: DisplayItemClip::ToString() const michael@0: { michael@0: nsAutoCString str; michael@0: if (mHaveClipRect) { michael@0: str.AppendPrintf("%d,%d,%d,%d", mClipRect.x, mClipRect.y, michael@0: mClipRect.width, mClipRect.height); michael@0: for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) { michael@0: const RoundedRect& r = mRoundedClipRects[i]; michael@0: str.AppendPrintf(" [%d,%d,%d,%d corners %d,%d,%d,%d,%d,%d,%d,%d]", michael@0: r.mRect.x, r.mRect.y, r.mRect.width, r.mRect.height, michael@0: r.mRadii[0], r.mRadii[1], r.mRadii[2], r.mRadii[3], michael@0: r.mRadii[4], r.mRadii[5], r.mRadii[6], r.mRadii[7]); michael@0: } michael@0: } michael@0: return str; michael@0: } michael@0: #endif michael@0: michael@0: }