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: #ifndef DISPLAYITEMCLIP_H_ michael@0: #define DISPLAYITEMCLIP_H_ michael@0: michael@0: #include "nsRect.h" michael@0: #include "nsTArray.h" michael@0: #include "nsStyleConsts.h" michael@0: michael@0: class gfxContext; michael@0: class nsDisplayItem; michael@0: class nsPresContext; michael@0: class nsRegion; michael@0: michael@0: namespace mozilla { michael@0: michael@0: /** michael@0: * An DisplayItemClip represents the intersection of an optional rectangle michael@0: * with a list of rounded rectangles (which is often empty), all in appunits. michael@0: * It can represent everything CSS clipping can do to an element (except for michael@0: * SVG clip-path), including no clipping at all. michael@0: */ michael@0: class DisplayItemClip { michael@0: public: michael@0: struct RoundedRect { michael@0: nsRect mRect; michael@0: // Indices into mRadii are the NS_CORNER_* constants in nsStyleConsts.h michael@0: nscoord mRadii[8]; michael@0: michael@0: RoundedRect operator+(const nsPoint& aOffset) const { michael@0: RoundedRect r = *this; michael@0: r.mRect += aOffset; michael@0: return r; michael@0: } michael@0: bool operator==(const RoundedRect& aOther) const { michael@0: if (!mRect.IsEqualInterior(aOther.mRect)) { michael@0: return false; michael@0: } michael@0: michael@0: NS_FOR_CSS_HALF_CORNERS(corner) { michael@0: if (mRadii[corner] != aOther.mRadii[corner]) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: bool operator!=(const RoundedRect& aOther) const { michael@0: return !(*this == aOther); michael@0: } michael@0: }; michael@0: michael@0: // Constructs a DisplayItemClip that does no clipping at all. michael@0: DisplayItemClip() : mHaveClipRect(false) {} michael@0: michael@0: void SetTo(const nsRect& aRect); michael@0: void SetTo(const nsRect& aRect, const nscoord* aRadii); michael@0: void IntersectWith(const DisplayItemClip& aOther); michael@0: michael@0: // Apply this |DisplayItemClip| to the given gfxContext. Any saving of state michael@0: // or clearing of other clips must be done by the caller. michael@0: // See aBegin/aEnd note on ApplyRoundedRectsTo. michael@0: void ApplyTo(gfxContext* aContext, nsPresContext* aPresContext, michael@0: uint32_t aBegin = 0, uint32_t aEnd = UINT32_MAX); michael@0: michael@0: void ApplyRectTo(gfxContext* aContext, int32_t A2D) const; michael@0: // Applies the rounded rects in this Clip to aContext michael@0: // Will only apply rounded rects from aBegin (inclusive) to aEnd michael@0: // (exclusive) or the number of rounded rects, whichever is smaller. michael@0: void ApplyRoundedRectsTo(gfxContext* aContext, int32_t A2DPRInt32, michael@0: uint32_t aBegin, uint32_t aEnd) const; michael@0: michael@0: // Draw (fill) the rounded rects in this clip to aContext michael@0: void DrawRoundedRectsTo(gfxContext* aContext, int32_t A2D, michael@0: uint32_t aBegin, uint32_t aEnd) const; michael@0: // 'Draw' (create as a path, does not stroke or fill) aRoundRect to aContext michael@0: void AddRoundedRectPathTo(gfxContext* aContext, int32_t A2D, michael@0: const RoundedRect &aRoundRect) const; michael@0: michael@0: // Returns true if the intersection of aRect and this clip region is michael@0: // non-empty. This is precise for DisplayItemClips with at most one michael@0: // rounded rectangle. When multiple rounded rectangles are present, we just michael@0: // check that the rectangle intersects all of them (but possibly in different michael@0: // places). So it may return true when the correct answer is false. michael@0: bool MayIntersect(const nsRect& aRect) const; michael@0: // Return a rectangle contained in the intersection of aRect with this michael@0: // clip region. Tries to return the largest possible rectangle, but may michael@0: // not succeed. michael@0: nsRect ApproximateIntersectInward(const nsRect& aRect) const; michael@0: michael@0: /* michael@0: * Computes a region which contains the clipped area of this DisplayItemClip, michael@0: * or if aOldClip is non-null, the union of the clipped area of this michael@0: * DisplayItemClip with the clipped area of aOldClip translated by aShift. michael@0: * The result is stored in aCombined. If the result would be infinite michael@0: * (because one or both of the clips does no clipping), returns false. michael@0: */ michael@0: bool ComputeRegionInClips(DisplayItemClip* aOldClip, michael@0: const nsPoint& aShift, michael@0: nsRegion* aCombined) const; michael@0: michael@0: // Returns false if aRect is definitely not clipped by a rounded corner in michael@0: // this clip. Returns true if aRect is clipped by a rounded corner in this michael@0: // clip or it can not be quickly determined that it is not clipped by a michael@0: // rounded corner in this clip. michael@0: bool IsRectClippedByRoundedCorner(const nsRect& aRect) const; michael@0: michael@0: // Returns false if aRect is definitely not clipped by anything in this clip. michael@0: // Fast but not necessarily accurate. michael@0: bool IsRectAffectedByClip(const nsRect& aRect) const; michael@0: michael@0: // Intersection of all rects in this clip ignoring any rounded corners. michael@0: nsRect NonRoundedIntersection() const; michael@0: michael@0: // Intersect the given rects with all rects in this clip, ignoring any michael@0: // rounded corners. michael@0: nsRect ApplyNonRoundedIntersection(const nsRect& aRect) const; michael@0: michael@0: // Gets rid of any rounded corners in this clip. michael@0: void RemoveRoundedCorners(); michael@0: michael@0: // Adds the difference between Intersect(*this + aPoint, aBounds) and michael@0: // Intersect(aOther, aOtherBounds) to aDifference (or a bounding-box thereof). michael@0: void AddOffsetAndComputeDifference(const nsPoint& aPoint, const nsRect& aBounds, michael@0: const DisplayItemClip& aOther, const nsRect& aOtherBounds, michael@0: nsRegion* aDifference); michael@0: michael@0: bool operator==(const DisplayItemClip& aOther) const { michael@0: return mHaveClipRect == aOther.mHaveClipRect && michael@0: (!mHaveClipRect || mClipRect.IsEqualInterior(aOther.mClipRect)) && michael@0: mRoundedClipRects == aOther.mRoundedClipRects; michael@0: } michael@0: bool operator!=(const DisplayItemClip& aOther) const { michael@0: return !(*this == aOther); michael@0: } michael@0: michael@0: bool HasClip() const { return mHaveClipRect; } michael@0: const nsRect& GetClipRect() const michael@0: { michael@0: NS_ASSERTION(HasClip(), "No clip rect!"); michael@0: return mClipRect; michael@0: } michael@0: michael@0: void MoveBy(nsPoint aPoint); michael@0: michael@0: #ifdef MOZ_DUMP_PAINTING michael@0: nsCString ToString() const; michael@0: #endif michael@0: michael@0: /** michael@0: * Find the largest N such that the first N rounded rects in 'this' are michael@0: * equal to the first N rounded rects in aOther, and N <= aMax. michael@0: */ michael@0: uint32_t GetCommonRoundedRectCount(const DisplayItemClip& aOther, michael@0: uint32_t aMax) const; michael@0: uint32_t GetRoundedRectCount() const { return mRoundedClipRects.Length(); } michael@0: void AppendRoundedRects(nsTArray* aArray, uint32_t aCount) const; michael@0: michael@0: static const DisplayItemClip& NoClip(); michael@0: michael@0: static void Shutdown(); michael@0: michael@0: private: michael@0: nsRect mClipRect; michael@0: nsTArray mRoundedClipRects; michael@0: // If mHaveClipRect is false then this object represents no clipping at all michael@0: // and mRoundedClipRects must be empty. michael@0: bool mHaveClipRect; michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif /* DISPLAYITEMCLIP_H_ */