1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DisplayItemClip.h"
#include "gfxContext.h"
#include "gfxUtils.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/gfx/PathHelpers.h"
#include "mozilla/layers/StackingContextHelper.h"
#include "mozilla/webrender/WebRenderTypes.h"
#include "nsCSSRendering.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsRegion.h"
using namespace mozilla::gfx;
namespace mozilla {
void DisplayItemClip::SetTo(const nsRect& aRect) { SetTo(aRect, nullptr); }
void DisplayItemClip::SetTo(const nsRect& aRect,
const nsRectCornerRadii* aRadii) {
mHaveClipRect = true;
mClipRect = aRect;
if (aRadii) {
mRoundedClipRects.Clear();
mRoundedClipRects.AppendElement(RoundedRect{aRect, *aRadii});
} else {
mRoundedClipRects.Clear();
}
}
void DisplayItemClip::SetTo(const nsRect& aRect, const nsRect& aRoundedRect,
const nsRectCornerRadii* aRadii) {
mHaveClipRect = true;
mClipRect = aRect;
mRoundedClipRects.Clear();
mRoundedClipRects.AppendElement(RoundedRect{aRoundedRect, *aRadii});
}
bool DisplayItemClip::MayIntersect(const nsRect& aRect) const {
if (!mHaveClipRect) {
return !aRect.IsEmpty();
}
nsRect r = aRect.Intersect(mClipRect);
if (r.IsEmpty()) {
return false;
}
for (const RoundedRect& rr : mRoundedClipRects) {
if (!nsLayoutUtils::RoundedRectIntersectsRect(rr.mRect, rr.mRadii, r)) {
return false;
}
}
return true;
}
void DisplayItemClip::IntersectWith(const DisplayItemClip& aOther) {
if (!aOther.mHaveClipRect) {
return;
}
if (!mHaveClipRect) {
*this = aOther;
return;
}
if (!mClipRect.IntersectRect(mClipRect, aOther.mClipRect)) {
mRoundedClipRects.Clear();
return;
}
mRoundedClipRects.AppendElements(aOther.mRoundedClipRects);
}
void DisplayItemClip::ApplyTo(gfxContext* aContext, int32_t A2D) const {
ApplyRectTo(aContext, A2D);
ApplyRoundedRectClipsTo(aContext, A2D, 0, mRoundedClipRects.Length());
}
void DisplayItemClip::ApplyRectTo(gfxContext* aContext, int32_t A2D) const {
aContext->NewPath();
gfxRect clip = nsLayoutUtils::RectToGfxRect(mClipRect, A2D);
aContext->SnappedRectangle(clip);
aContext->Clip();
}
void DisplayItemClip::ApplyRoundedRectClipsTo(gfxContext* aContext, int32_t A2D,
uint32_t aBegin,
uint32_t aEnd) const {
DrawTarget& aDrawTarget = *aContext->GetDrawTarget();
aEnd = std::min<uint32_t>(aEnd, mRoundedClipRects.Length());
for (uint32_t i = aBegin; i < aEnd; ++i) {
RefPtr<Path> roundedRect =
MakeRoundedRectPath(aDrawTarget, A2D, mRoundedClipRects[i]);
aContext->Clip(roundedRect);
}
}
void DisplayItemClip::FillIntersectionOfRoundedRectClips(
gfxContext* aContext, const DeviceColor& aColor,
int32_t aAppUnitsPerDevPixel) const {
DrawTarget& aDrawTarget = *aContext->GetDrawTarget();
uint32_t end = mRoundedClipRects.Length();
if (!end) {
return;
}
// Push clips for any rects that come BEFORE the rect at |aEnd - 1|, if any:
ApplyRoundedRectClipsTo(aContext, aAppUnitsPerDevPixel, 0, end - 1);
// Now fill the rect at |aEnd - 1|:
RefPtr<Path> roundedRect = MakeRoundedRectPath(
aDrawTarget, aAppUnitsPerDevPixel, mRoundedClipRects[end - 1]);
aDrawTarget.Fill(roundedRect, ColorPattern(aColor));
// Finally, pop any clips that we may have pushed:
for (uint32_t i = 0; i < end - 1; ++i) {
aContext->PopClip();
}
}
already_AddRefed<Path> DisplayItemClip::MakeRoundedRectPath(
DrawTarget& aDrawTarget, int32_t A2D, const RoundedRect& aRoundRect) const {
RectCornerRadii pixelRadii;
nsCSSRendering::ComputePixelRadii(aRoundRect.mRadii, A2D, &pixelRadii);
Rect rect = NSRectToSnappedRect(aRoundRect.mRect, A2D, aDrawTarget);
return MakePathForRoundedRect(aDrawTarget, rect, pixelRadii);
}
nsRect DisplayItemClip::ApproximateIntersectInward(const nsRect& aRect) const {
nsRect r = aRect;
if (mHaveClipRect) {
r.IntersectRect(r, mClipRect);
}
for (const RoundedRect& rr : mRoundedClipRects) {
nsRegion rgn =
nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, r);
r = rgn.GetLargestRectangle();
}
return r;
}
// Test if (aXPoint, aYPoint) is in the ellipse with center (aXCenter, aYCenter)
// and radii aXRadius, aYRadius.
static bool IsInsideEllipse(nscoord aXRadius, nscoord aXCenter, nscoord aXPoint,
nscoord aYRadius, nscoord aYCenter,
nscoord aYPoint) {
float scaledX = float(aXPoint - aXCenter) / float(aXRadius);
float scaledY = float(aYPoint - aYCenter) / float(aYRadius);
return scaledX * scaledX + scaledY * scaledY < 1.0f;
}
bool DisplayItemClip::IsRectClippedByRoundedCorner(const nsRect& aRect) const {
if (mRoundedClipRects.IsEmpty()) {
return false;
}
nsRect rect;
rect.IntersectRect(aRect, NonRoundedIntersection());
for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); i < iEnd; ++i) {
const RoundedRect& rr = mRoundedClipRects[i];
// top left
if (rect.x < rr.mRect.x + rr.mRadii[eCornerTopLeftX] &&
rect.y < rr.mRect.y + rr.mRadii[eCornerTopLeftY]) {
if (!IsInsideEllipse(rr.mRadii[eCornerTopLeftX],
rr.mRect.x + rr.mRadii[eCornerTopLeftX], rect.x,
rr.mRadii[eCornerTopLeftY],
rr.mRect.y + rr.mRadii[eCornerTopLeftY], rect.y)) {
return true;
}
}
// top right
if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[eCornerTopRightX] &&
rect.y < rr.mRect.y + rr.mRadii[eCornerTopRightY]) {
if (!IsInsideEllipse(rr.mRadii[eCornerTopRightX],
rr.mRect.XMost() - rr.mRadii[eCornerTopRightX],
rect.XMost(), rr.mRadii[eCornerTopRightY],
rr.mRect.y + rr.mRadii[eCornerTopRightY], rect.y)) {
return true;
}
}
// bottom left
if (rect.x < rr.mRect.x + rr.mRadii[eCornerBottomLeftX] &&
rect.YMost() > rr.mRect.YMost() - rr.mRadii[eCornerBottomLeftY]) {
if (!IsInsideEllipse(rr.mRadii[eCornerBottomLeftX],
rr.mRect.x + rr.mRadii[eCornerBottomLeftX], rect.x,
rr.mRadii[eCornerBottomLeftY],
rr.mRect.YMost() - rr.mRadii[eCornerBottomLeftY],
rect.YMost())) {
return true;
}
}
// bottom right
if (rect.XMost() > rr.mRect.XMost() - rr.mRadii[eCornerBottomRightX] &&
rect.YMost() > rr.mRect.YMost() - rr.mRadii[eCornerBottomRightY]) {
if (!IsInsideEllipse(rr.mRadii[eCornerBottomRightX],
rr.mRect.XMost() - rr.mRadii[eCornerBottomRightX],
rect.XMost(), rr.mRadii[eCornerBottomRightY],
rr.mRect.YMost() - rr.mRadii[eCornerBottomRightY],
rect.YMost())) {
return true;
}
}
}
return false;
}
nsRect DisplayItemClip::NonRoundedIntersection() const {
NS_ASSERTION(mHaveClipRect, "Must have a clip rect!");
nsRect result = mClipRect;
for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); i < iEnd; ++i) {
result.IntersectRect(result, mRoundedClipRects[i].mRect);
}
return result;
}
bool DisplayItemClip::IsRectAffectedByClip(const nsRect& aRect) const {
if (mHaveClipRect && !mClipRect.Contains(aRect)) {
return true;
}
for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); i < iEnd; ++i) {
const RoundedRect& rr = mRoundedClipRects[i];
nsRegion rgn =
nsLayoutUtils::RoundedRectIntersectRect(rr.mRect, rr.mRadii, aRect);
if (!rgn.Contains(aRect)) {
return true;
}
}
return false;
}
bool DisplayItemClip::IsRectAffectedByClip(const nsIntRect& aRect,
float aXScale, float aYScale,
int32_t A2D) const {
if (mHaveClipRect) {
nsIntRect pixelClipRect =
mClipRect.ScaleToNearestPixels(aXScale, aYScale, A2D);
if (!pixelClipRect.Contains(aRect)) {
return true;
}
}
// Rounded rect clipping only snaps to user-space pixels, not device space.
nsIntRect unscaled = aRect;
unscaled.Scale(1 / aXScale, 1 / aYScale);
for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); i < iEnd; ++i) {
const RoundedRect& rr = mRoundedClipRects[i];
nsIntRect pixelRect = rr.mRect.ToNearestPixels(A2D);
RectCornerRadii pixelRadii;
nsCSSRendering::ComputePixelRadii(rr.mRadii, A2D, &pixelRadii);
nsIntRegion rgn = nsLayoutUtils::RoundedRectIntersectIntRect(
pixelRect, pixelRadii, unscaled);
if (!rgn.Contains(unscaled)) {
return true;
}
}
return false;
}
nsRect DisplayItemClip::ApplyNonRoundedIntersection(const nsRect& aRect) const {
if (!mHaveClipRect) {
return aRect;
}
nsRect result = aRect.Intersect(mClipRect);
for (uint32_t i = 0, iEnd = mRoundedClipRects.Length(); i < iEnd; ++i) {
result = result.Intersect(mRoundedClipRects[i].mRect);
}
return result;
}
void DisplayItemClip::RemoveRoundedCorners() {
if (mRoundedClipRects.IsEmpty()) {
return;
}
mClipRect = NonRoundedIntersection();
mRoundedClipRects.Clear();
}
// Computes the difference between aR1 and aR2, limited to aBounds.
static void AccumulateRectDifference(const nsRect& aR1, const nsRect& aR2,
const nsRect& aBounds, nsRegion* aOut) {
if (aR1.IsEqualInterior(aR2)) {
return;
}
nsRegion r;
r.Xor(aR1, aR2);
r.And(r, aBounds);
aOut->Or(*aOut, r);
}
static void AccumulateRoundedRectDifference(
const DisplayItemClip::RoundedRect& aR1,
const DisplayItemClip::RoundedRect& aR2, const nsRect& aBounds,
const nsRect& aOtherBounds, nsRegion* aOut) {
const nsRect& rect1 = aR1.mRect;
const nsRect& rect2 = aR2.mRect;
// If the two rectangles are totally disjoint, just add them both - otherwise
// we'd end up adding one big enclosing rect
if (!rect1.Intersects(rect2) || aR1.mRadii != aR2.mRadii) {
aOut->Or(*aOut, rect1.Intersect(aBounds));
aOut->Or(*aOut, rect2.Intersect(aOtherBounds));
return;
}
nscoord lowestBottom = std::max(rect1.YMost(), rect2.YMost());
nscoord highestTop = std::min(rect1.Y(), rect2.Y());
nscoord maxRight = std::max(rect1.XMost(), rect2.XMost());
nscoord minLeft = std::min(rect1.X(), rect2.X());
// At this point, we know that the radii haven't changed, and that the bounds
// are different in some way. To explain how this works, consider the case
// where the rounded rect has just been translated along the X direction.
// | ______________________ _ _ _ _ _ _ |
// | / / \ \ |
// | | | |
// | | aR1 | | aR2 | |
// | | | |
// | \ __________\___________ / _ _ _ _ _ / |
// | |
// The invalidation region will be as if we lopped off the left rounded part
// of aR2, and the right rounded part of aR1, and XOR'd them:
// | ______________________ _ _ _ _ _ _ |
// | -/-----------/- -\-----------\- |
// | |-------------- --|------------ |
// | |-----aR1---|-- --|-----aR2---| |
// | |-------------- --|------------ |
// | -\ __________\-__________-/ _ _ _ _ _ /- |
// | |
// The logic below just implements this idea, but generalized to both the
// X and Y dimensions. The "(...)Adjusted(...)" values represent the lopped
// off sides.
nscoord highestAdjustedBottom = std::min(
rect1.YMost() - aR1.mRadii[eCornerBottomLeftY],
std::min(rect1.YMost() - aR1.mRadii[eCornerBottomRightY],
std::min(rect2.YMost() - aR2.mRadii[eCornerBottomLeftY],
rect2.YMost() - aR2.mRadii[eCornerBottomRightY])));
nscoord lowestAdjustedTop =
std::max(rect1.Y() + aR1.mRadii[eCornerTopLeftY],
std::max(rect1.Y() + aR1.mRadii[eCornerTopRightY],
std::max(rect2.Y() + aR2.mRadii[eCornerTopLeftY],
rect2.Y() + aR2.mRadii[eCornerTopRightY])));
nscoord minAdjustedRight = std::min(
rect1.XMost() - aR1.mRadii[eCornerTopRightX],
std::min(rect1.XMost() - aR1.mRadii[eCornerBottomRightX],
std::min(rect2.XMost() - aR2.mRadii[eCornerTopRightX],
rect2.XMost() - aR2.mRadii[eCornerBottomRightX])));
nscoord maxAdjustedLeft =
std::max(rect1.X() + aR1.mRadii[eCornerTopLeftX],
std::max(rect1.X() + aR1.mRadii[eCornerBottomLeftX],
std::max(rect2.X() + aR2.mRadii[eCornerTopLeftX],
rect2.X() + aR2.mRadii[eCornerBottomLeftX])));
// We only want to add an invalidation rect if the bounds have changed. If we
// always added all of the 4 rects below, we would always be invalidating a
// border around the rects, even in cases where we just translated along the X
// or Y axis.
nsRegion r;
// First, or with the Y delta rects, wide along the X axis
if (rect1.Y() != rect2.Y()) {
r.Or(r, nsRect(minLeft, highestTop, maxRight - minLeft,
lowestAdjustedTop - highestTop));
}
if (rect1.YMost() != rect2.YMost()) {
r.Or(r, nsRect(minLeft, highestAdjustedBottom, maxRight - minLeft,
lowestBottom - highestAdjustedBottom));
}
// Then, or with the X delta rects, wide along the Y axis
if (rect1.X() != rect2.X()) {
r.Or(r, nsRect(minLeft, highestTop, maxAdjustedLeft - minLeft,
lowestBottom - highestTop));
}
if (rect1.XMost() != rect2.XMost()) {
r.Or(r, nsRect(minAdjustedRight, highestTop, maxRight - minAdjustedRight,
lowestBottom - highestTop));
}
r.And(r, aBounds.Union(aOtherBounds));
aOut->Or(*aOut, r);
}
void DisplayItemClip::AddOffsetAndComputeDifference(
const nsPoint& aOffset, const nsRect& aBounds,
const DisplayItemClip& aOther, const nsRect& aOtherBounds,
nsRegion* aDifference) {
if (mHaveClipRect != aOther.mHaveClipRect ||
mRoundedClipRects.Length() != aOther.mRoundedClipRects.Length()) {
aDifference->Or(*aDifference, aBounds);
aDifference->Or(*aDifference, aOtherBounds);
return;
}
if (mHaveClipRect) {
AccumulateRectDifference(mClipRect + aOffset, aOther.mClipRect,
aBounds.Union(aOtherBounds), aDifference);
}
for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
if (mRoundedClipRects[i] + aOffset != aOther.mRoundedClipRects[i]) {
AccumulateRoundedRectDifference(mRoundedClipRects[i] + aOffset,
aOther.mRoundedClipRects[i], aBounds,
aOtherBounds, aDifference);
}
}
}
void DisplayItemClip::AppendRoundedRects(nsTArray<RoundedRect>* aArray) const {
aArray->AppendElements(mRoundedClipRects.Elements(),
mRoundedClipRects.Length());
}
bool DisplayItemClip::ComputeRegionInClips(const DisplayItemClip* aOldClip,
const nsPoint& aShift,
nsRegion* aCombined) const {
if (!mHaveClipRect || (aOldClip && !aOldClip->mHaveClipRect)) {
return false;
}
if (aOldClip) {
*aCombined = aOldClip->NonRoundedIntersection();
aCombined->MoveBy(aShift);
aCombined->Or(*aCombined, NonRoundedIntersection());
} else {
*aCombined = NonRoundedIntersection();
}
return true;
}
void DisplayItemClip::MoveBy(const nsPoint& aPoint) {
if (!mHaveClipRect) {
return;
}
mClipRect += aPoint;
for (uint32_t i = 0; i < mRoundedClipRects.Length(); ++i) {
mRoundedClipRects[i].mRect += aPoint;
}
}
static StaticAutoPtr<DisplayItemClip> gNoClip;
const DisplayItemClip& DisplayItemClip::NoClip() {
if (!gNoClip) {
gNoClip = new DisplayItemClip();
}
return *gNoClip;
}
void DisplayItemClip::Shutdown() { gNoClip = nullptr; }
nsCString DisplayItemClip::ToString() const {
nsAutoCString str;
if (mHaveClipRect) {
str.AppendPrintf("%d,%d,%d,%d", mClipRect.x, mClipRect.y, mClipRect.width,
mClipRect.height);
for (const RoundedRect& r : mRoundedClipRects) {
str.AppendPrintf(
" [%d,%d,%d,%d corners %d,%d,%d,%d,%d,%d,%d,%d]", r.mRect.x,
r.mRect.y, r.mRect.width, r.mRect.height, r.mRadii.TopLeft().width,
r.mRadii.TopLeft().height, r.mRadii.TopRight().width,
r.mRadii.TopRight().height, r.mRadii.BottomLeft().width,
r.mRadii.BottomLeft().height, r.mRadii.BottomRight().width,
r.mRadii.BottomRight().height);
}
}
return std::move(str);
}
void DisplayItemClip::ToComplexClipRegions(
int32_t aAppUnitsPerDevPixel,
nsTArray<wr::ComplexClipRegion>& aOutArray) const {
for (const auto& clipRect : mRoundedClipRects) {
aOutArray.AppendElement(wr::ToComplexClipRegion(
clipRect.mRect, clipRect.mRadii, aAppUnitsPerDevPixel));
}
}
} // namespace mozilla
|