layout/base/DisplayItemClip.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 "DisplayItemClip.h"
michael@0 7
michael@0 8 #include "gfxContext.h"
michael@0 9 #include "nsPresContext.h"
michael@0 10 #include "nsCSSRendering.h"
michael@0 11 #include "nsLayoutUtils.h"
michael@0 12 #include "nsRegion.h"
michael@0 13
michael@0 14 namespace mozilla {
michael@0 15
michael@0 16 void
michael@0 17 DisplayItemClip::SetTo(const nsRect& aRect)
michael@0 18 {
michael@0 19 mHaveClipRect = true;
michael@0 20 mClipRect = aRect;
michael@0 21 mRoundedClipRects.Clear();
michael@0 22 }
michael@0 23
michael@0 24 void
michael@0 25 DisplayItemClip::SetTo(const nsRect& aRect, const nscoord* aRadii)
michael@0 26 {
michael@0 27 mHaveClipRect = true;
michael@0 28 mClipRect = aRect;
michael@0 29 mRoundedClipRects.SetLength(1);
michael@0 30 mRoundedClipRects[0].mRect = aRect;
michael@0 31 memcpy(mRoundedClipRects[0].mRadii, aRadii, sizeof(nscoord)*8);
michael@0 32 }
michael@0 33
michael@0 34 bool
michael@0 35 DisplayItemClip::MayIntersect(const nsRect& aRect) const
michael@0 36 {
michael@0 37 if (!mHaveClipRect) {
michael@0 38 return !aRect.IsEmpty();
michael@0 39 }
michael@0 40 nsRect r = aRect.Intersect(mClipRect);
michael@0 41 if (r.IsEmpty()) {
michael@0 42 return false;
michael@0 43 }
michael@0 44 for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
michael@0 45 const RoundedRect& rr = mRoundedClipRects[i];
michael@0 46 if (!nsLayoutUtils::RoundedRectIntersectsRect(rr.mRect, rr.mRadii, r)) {
michael@0 47 return false;
michael@0 48 }
michael@0 49 }
michael@0 50 return true;
michael@0 51 }
michael@0 52
michael@0 53 void
michael@0 54 DisplayItemClip::IntersectWith(const DisplayItemClip& aOther)
michael@0 55 {
michael@0 56 if (!aOther.mHaveClipRect) {
michael@0 57 return;
michael@0 58 }
michael@0 59 if (!mHaveClipRect) {
michael@0 60 *this = aOther;
michael@0 61 return;
michael@0 62 }
michael@0 63 if (!mClipRect.IntersectRect(mClipRect, aOther.mClipRect)) {
michael@0 64 mRoundedClipRects.Clear();
michael@0 65 return;
michael@0 66 }
michael@0 67 mRoundedClipRects.AppendElements(aOther.mRoundedClipRects);
michael@0 68 }
michael@0 69
michael@0 70 void
michael@0 71 DisplayItemClip::ApplyTo(gfxContext* aContext,
michael@0 72 nsPresContext* aPresContext,
michael@0 73 uint32_t aBegin, uint32_t aEnd)
michael@0 74 {
michael@0 75 int32_t A2D = aPresContext->AppUnitsPerDevPixel();
michael@0 76 ApplyRectTo(aContext, A2D);
michael@0 77 ApplyRoundedRectsTo(aContext, A2D, aBegin, aEnd);
michael@0 78 }
michael@0 79
michael@0 80 void
michael@0 81 DisplayItemClip::ApplyRectTo(gfxContext* aContext, int32_t A2D) const
michael@0 82 {
michael@0 83 aContext->NewPath();
michael@0 84 gfxRect clip = nsLayoutUtils::RectToGfxRect(mClipRect, A2D);
michael@0 85 aContext->Rectangle(clip, true);
michael@0 86 aContext->Clip();
michael@0 87 }
michael@0 88
michael@0 89 void
michael@0 90 DisplayItemClip::ApplyRoundedRectsTo(gfxContext* aContext,
michael@0 91 int32_t A2D,
michael@0 92 uint32_t aBegin, uint32_t aEnd) const
michael@0 93 {
michael@0 94 aEnd = std::min<uint32_t>(aEnd, mRoundedClipRects.Length());
michael@0 95
michael@0 96 for (uint32_t i = aBegin; i < aEnd; ++i) {
michael@0 97 AddRoundedRectPathTo(aContext, A2D, mRoundedClipRects[i]);
michael@0 98 aContext->Clip();
michael@0 99 }
michael@0 100 }
michael@0 101
michael@0 102 void
michael@0 103 DisplayItemClip::DrawRoundedRectsTo(gfxContext* aContext,
michael@0 104 int32_t A2D,
michael@0 105 uint32_t aBegin, uint32_t aEnd) const
michael@0 106 {
michael@0 107 aEnd = std::min<uint32_t>(aEnd, mRoundedClipRects.Length());
michael@0 108
michael@0 109 if (aEnd - aBegin == 0)
michael@0 110 return;
michael@0 111
michael@0 112 // If there is just one rounded rect we can just fill it, if there are more then we
michael@0 113 // must clip the rest to get the intersection of clips
michael@0 114 ApplyRoundedRectsTo(aContext, A2D, aBegin, aEnd - 1);
michael@0 115 AddRoundedRectPathTo(aContext, A2D, mRoundedClipRects[aEnd - 1]);
michael@0 116 aContext->Fill();
michael@0 117 }
michael@0 118
michael@0 119 void
michael@0 120 DisplayItemClip::AddRoundedRectPathTo(gfxContext* aContext,
michael@0 121 int32_t A2D,
michael@0 122 const RoundedRect &aRoundRect) const
michael@0 123 {
michael@0 124 gfxCornerSizes pixelRadii;
michael@0 125 nsCSSRendering::ComputePixelRadii(aRoundRect.mRadii, A2D, &pixelRadii);
michael@0 126
michael@0 127 gfxRect clip = nsLayoutUtils::RectToGfxRect(aRoundRect.mRect, A2D);
michael@0 128 clip.Round();
michael@0 129 clip.Condition();
michael@0 130
michael@0 131 aContext->NewPath();
michael@0 132 aContext->RoundedRectangle(clip, pixelRadii);
michael@0 133 }
michael@0 134
michael@0 135 nsRect
michael@0 136 DisplayItemClip::ApproximateIntersectInward(const nsRect& aRect) const
michael@0 137 {
michael@0 138 nsRect r = aRect;
michael@0 139 if (mHaveClipRect) {
michael@0 140 r.IntersectRect(r, mClipRect);
michael@0 141 }
michael@0 142 for (uint32_t i = 0, iEnd = mRoundedClipRects.Length();
michael@0 143 i < iEnd; ++i) {
michael@0 144 const RoundedRect &rr = mRoundedClipRects[i];
michael@0 145 nsRegion rgn = nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, r);
michael@0 146 r = rgn.GetLargestRectangle();
michael@0 147 }
michael@0 148 return r;
michael@0 149 }
michael@0 150
michael@0 151 // Test if (aXPoint, aYPoint) is in the ellipse with center (aXCenter, aYCenter)
michael@0 152 // and radii aXRadius, aYRadius.
michael@0 153 bool IsInsideEllipse(nscoord aXRadius, nscoord aXCenter, nscoord aXPoint,
michael@0 154 nscoord aYRadius, nscoord aYCenter, nscoord aYPoint)
michael@0 155 {
michael@0 156 float scaledX = float(aXPoint - aXCenter) / float(aXRadius);
michael@0 157 float scaledY = float(aYPoint - aYCenter) / float(aYRadius);
michael@0 158 return scaledX * scaledX + scaledY * scaledY < 1.0f;
michael@0 159 }
michael@0 160
michael@0 161 bool
michael@0 162 DisplayItemClip::IsRectClippedByRoundedCorner(const nsRect& aRect) const
michael@0 163 {
michael@0 164 if (mRoundedClipRects.IsEmpty())
michael@0 165 return false;
michael@0 166
michael@0 167 nsRect rect;
michael@0 168 rect.IntersectRect(aRect, NonRoundedIntersection());
michael@0 169 for (uint32_t i = 0, iEnd = mRoundedClipRects.Length();
michael@0 170 i < iEnd; ++i) {
michael@0 171 const RoundedRect &rr = mRoundedClipRects[i];
michael@0 172 // top left
michael@0 173 if (rect.x < rr.mRect.x + rr.mRadii[NS_CORNER_TOP_LEFT_X] &&
michael@0 174 rect.y < rr.mRect.y + rr.mRadii[NS_CORNER_TOP_LEFT_Y]) {
michael@0 175 if (!IsInsideEllipse(rr.mRadii[NS_CORNER_TOP_LEFT_X],
michael@0 176 rr.mRect.x + rr.mRadii[NS_CORNER_TOP_LEFT_X],
michael@0 177 rect.x,
michael@0 178 rr.mRadii[NS_CORNER_TOP_LEFT_Y],
michael@0 179 rr.mRect.y + rr.mRadii[NS_CORNER_TOP_LEFT_Y],
michael@0 180 rect.y)) {
michael@0 181 return true;
michael@0 182 }
michael@0 183 }
michael@0 184 // top right
michael@0 185 if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[NS_CORNER_TOP_RIGHT_X] &&
michael@0 186 rect.y < rr.mRect.y + rr.mRadii[NS_CORNER_TOP_RIGHT_Y]) {
michael@0 187 if (!IsInsideEllipse(rr.mRadii[NS_CORNER_TOP_RIGHT_X],
michael@0 188 rr.mRect.XMost() - rr.mRadii[NS_CORNER_TOP_RIGHT_X],
michael@0 189 rect.XMost(),
michael@0 190 rr.mRadii[NS_CORNER_TOP_RIGHT_Y],
michael@0 191 rr.mRect.y + rr.mRadii[NS_CORNER_TOP_RIGHT_Y],
michael@0 192 rect.y)) {
michael@0 193 return true;
michael@0 194 }
michael@0 195 }
michael@0 196 // bottom left
michael@0 197 if (rect.x < rr.mRect.x + rr.mRadii[NS_CORNER_BOTTOM_LEFT_X] &&
michael@0 198 rect.YMost() > rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y]) {
michael@0 199 if (!IsInsideEllipse(rr.mRadii[NS_CORNER_BOTTOM_LEFT_X],
michael@0 200 rr.mRect.x + rr.mRadii[NS_CORNER_BOTTOM_LEFT_X],
michael@0 201 rect.x,
michael@0 202 rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y],
michael@0 203 rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_LEFT_Y],
michael@0 204 rect.YMost())) {
michael@0 205 return true;
michael@0 206 }
michael@0 207 }
michael@0 208 // bottom right
michael@0 209 if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X] &&
michael@0 210 rect.YMost() > rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y]) {
michael@0 211 if (!IsInsideEllipse(rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X],
michael@0 212 rr.mRect.XMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_X],
michael@0 213 rect.XMost(),
michael@0 214 rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y],
michael@0 215 rr.mRect.YMost() - rr.mRadii[NS_CORNER_BOTTOM_RIGHT_Y],
michael@0 216 rect.YMost())) {
michael@0 217 return true;
michael@0 218 }
michael@0 219 }
michael@0 220 }
michael@0 221 return false;
michael@0 222 }
michael@0 223
michael@0 224 nsRect
michael@0 225 DisplayItemClip::NonRoundedIntersection() const
michael@0 226 {
michael@0 227 NS_ASSERTION(mHaveClipRect, "Must have a clip rect!");
michael@0 228 nsRect result = mClipRect;
michael@0 229 for (uint32_t i = 0, iEnd = mRoundedClipRects.Length();
michael@0 230 i < iEnd; ++i) {
michael@0 231 result.IntersectRect(result, mRoundedClipRects[i].mRect);
michael@0 232 }
michael@0 233 return result;
michael@0 234 }
michael@0 235
michael@0 236 bool
michael@0 237 DisplayItemClip::IsRectAffectedByClip(const nsRect& aRect) const
michael@0 238 {
michael@0 239 if (mHaveClipRect && !mClipRect.Contains(aRect)) {
michael@0 240 return true;
michael@0 241 }
michael@0 242 for (uint32_t i = 0, iEnd = mRoundedClipRects.Length();
michael@0 243 i < iEnd; ++i) {
michael@0 244 const RoundedRect &rr = mRoundedClipRects[i];
michael@0 245 nsRegion rgn = nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, aRect);
michael@0 246 if (!rgn.Contains(aRect)) {
michael@0 247 return true;
michael@0 248 }
michael@0 249 }
michael@0 250 return false;
michael@0 251 }
michael@0 252
michael@0 253 nsRect
michael@0 254 DisplayItemClip::ApplyNonRoundedIntersection(const nsRect& aRect) const
michael@0 255 {
michael@0 256 if (!mHaveClipRect) {
michael@0 257 return aRect;
michael@0 258 }
michael@0 259
michael@0 260 nsRect result = aRect.Intersect(mClipRect);
michael@0 261 for (uint32_t i = 0, iEnd = mRoundedClipRects.Length();
michael@0 262 i < iEnd; ++i) {
michael@0 263 result.Intersect(mRoundedClipRects[i].mRect);
michael@0 264 }
michael@0 265 return result;
michael@0 266 }
michael@0 267
michael@0 268 void
michael@0 269 DisplayItemClip::RemoveRoundedCorners()
michael@0 270 {
michael@0 271 if (mRoundedClipRects.IsEmpty())
michael@0 272 return;
michael@0 273
michael@0 274 mClipRect = NonRoundedIntersection();
michael@0 275 mRoundedClipRects.Clear();
michael@0 276 }
michael@0 277
michael@0 278 static void
michael@0 279 AccumulateRectDifference(const nsRect& aR1, const nsRect& aR2, nsRegion* aOut)
michael@0 280 {
michael@0 281 if (aR1.IsEqualInterior(aR2))
michael@0 282 return;
michael@0 283 nsRegion r;
michael@0 284 r.Xor(aR1, aR2);
michael@0 285 aOut->Or(*aOut, r);
michael@0 286 }
michael@0 287
michael@0 288 void
michael@0 289 DisplayItemClip::AddOffsetAndComputeDifference(const nsPoint& aOffset,
michael@0 290 const nsRect& aBounds,
michael@0 291 const DisplayItemClip& aOther,
michael@0 292 const nsRect& aOtherBounds,
michael@0 293 nsRegion* aDifference)
michael@0 294 {
michael@0 295 if (mHaveClipRect != aOther.mHaveClipRect ||
michael@0 296 mRoundedClipRects.Length() != aOther.mRoundedClipRects.Length()) {
michael@0 297 aDifference->Or(*aDifference, aBounds);
michael@0 298 aDifference->Or(*aDifference, aOtherBounds);
michael@0 299 return;
michael@0 300 }
michael@0 301 if (mHaveClipRect) {
michael@0 302 AccumulateRectDifference((mClipRect + aOffset).Intersect(aBounds),
michael@0 303 aOther.mClipRect.Intersect(aOtherBounds),
michael@0 304 aDifference);
michael@0 305 }
michael@0 306 for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
michael@0 307 if (mRoundedClipRects[i] + aOffset != aOther.mRoundedClipRects[i]) {
michael@0 308 // The corners make it tricky so we'll just add both rects here.
michael@0 309 aDifference->Or(*aDifference, mRoundedClipRects[i].mRect.Intersect(aBounds));
michael@0 310 aDifference->Or(*aDifference, aOther.mRoundedClipRects[i].mRect.Intersect(aOtherBounds));
michael@0 311 }
michael@0 312 }
michael@0 313 }
michael@0 314
michael@0 315 uint32_t
michael@0 316 DisplayItemClip::GetCommonRoundedRectCount(const DisplayItemClip& aOther,
michael@0 317 uint32_t aMax) const
michael@0 318 {
michael@0 319 uint32_t end = std::min(std::min(mRoundedClipRects.Length(), aMax),
michael@0 320 aOther.mRoundedClipRects.Length());
michael@0 321 uint32_t clipCount = 0;
michael@0 322 for (; clipCount < end; ++clipCount) {
michael@0 323 if (mRoundedClipRects[clipCount] !=
michael@0 324 aOther.mRoundedClipRects[clipCount]) {
michael@0 325 return clipCount;
michael@0 326 }
michael@0 327 }
michael@0 328 return clipCount;
michael@0 329 }
michael@0 330
michael@0 331 void
michael@0 332 DisplayItemClip::AppendRoundedRects(nsTArray<RoundedRect>* aArray, uint32_t aCount) const
michael@0 333 {
michael@0 334 uint32_t count = std::min(mRoundedClipRects.Length(), aCount);
michael@0 335 for (uint32_t i = 0; i < count; ++i) {
michael@0 336 *aArray->AppendElement() = mRoundedClipRects[i];
michael@0 337 }
michael@0 338 }
michael@0 339
michael@0 340 bool
michael@0 341 DisplayItemClip::ComputeRegionInClips(DisplayItemClip* aOldClip,
michael@0 342 const nsPoint& aShift,
michael@0 343 nsRegion* aCombined) const
michael@0 344 {
michael@0 345 if (!mHaveClipRect || (aOldClip && !aOldClip->mHaveClipRect)) {
michael@0 346 return false;
michael@0 347 }
michael@0 348
michael@0 349 if (aOldClip) {
michael@0 350 *aCombined = aOldClip->NonRoundedIntersection();
michael@0 351 aCombined->MoveBy(aShift);
michael@0 352 aCombined->Or(*aCombined, NonRoundedIntersection());
michael@0 353 } else {
michael@0 354 *aCombined = NonRoundedIntersection();
michael@0 355 }
michael@0 356 return true;
michael@0 357 }
michael@0 358
michael@0 359 void
michael@0 360 DisplayItemClip::MoveBy(nsPoint aPoint)
michael@0 361 {
michael@0 362 if (!mHaveClipRect)
michael@0 363 return;
michael@0 364 mClipRect += aPoint;
michael@0 365 for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
michael@0 366 mRoundedClipRects[i].mRect += aPoint;
michael@0 367 }
michael@0 368 }
michael@0 369
michael@0 370 static DisplayItemClip* gNoClip;
michael@0 371
michael@0 372 const DisplayItemClip&
michael@0 373 DisplayItemClip::NoClip()
michael@0 374 {
michael@0 375 if (!gNoClip) {
michael@0 376 gNoClip = new DisplayItemClip();
michael@0 377 }
michael@0 378 return *gNoClip;
michael@0 379 }
michael@0 380
michael@0 381 void
michael@0 382 DisplayItemClip::Shutdown()
michael@0 383 {
michael@0 384 delete gNoClip;
michael@0 385 gNoClip = nullptr;
michael@0 386 }
michael@0 387
michael@0 388 #ifdef MOZ_DUMP_PAINTING
michael@0 389 nsCString
michael@0 390 DisplayItemClip::ToString() const
michael@0 391 {
michael@0 392 nsAutoCString str;
michael@0 393 if (mHaveClipRect) {
michael@0 394 str.AppendPrintf("%d,%d,%d,%d", mClipRect.x, mClipRect.y,
michael@0 395 mClipRect.width, mClipRect.height);
michael@0 396 for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
michael@0 397 const RoundedRect& r = mRoundedClipRects[i];
michael@0 398 str.AppendPrintf(" [%d,%d,%d,%d corners %d,%d,%d,%d,%d,%d,%d,%d]",
michael@0 399 r.mRect.x, r.mRect.y, r.mRect.width, r.mRect.height,
michael@0 400 r.mRadii[0], r.mRadii[1], r.mRadii[2], r.mRadii[3],
michael@0 401 r.mRadii[4], r.mRadii[5], r.mRadii[6], r.mRadii[7]);
michael@0 402 }
michael@0 403 }
michael@0 404 return str;
michael@0 405 }
michael@0 406 #endif
michael@0 407
michael@0 408 }

mercurial