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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
/*
* Copyright (C) 2024, 2025 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#if USE(COORDINATED_GRAPHICS)
#include "FloatRect.h"
#include "LayoutSize.h"
#include "Region.h"
#include <wtf/BitVector.h>
#include <wtf/ForbidHeapAllocation.h>
#include <wtf/text/TextStream.h>
// A helper class to store damage rectangles in a few approximated ways
// to trade-off the CPU cost of the data structure and the resolution
// it brings (i.e. how good approximation reflects the reality).
// The simplest way to store the damage is to maintain a minimum
// bounding rectangle (bounding box) of all incoming damage rectangles.
// This way the amount of memory used is minimal (just a single rect)
// and the add() operations are cheap as it's always about unite().
// While this method works well in many scenarios, it fails to model
// the small rectangles that are very far apart.
// The more sophisticated method to store the damage is to store a
// limited vector of rectangles. Unless the limit of rectangles is hit
// each rectangle is stored as-is.
// Once the new rectangle cannot be added without extending the vector
// past the limit, the unification mechanism starts.
// Unification mechanism - once enabled - uses an artificial grid
// to map incoming rects into cells that can store up to 1 rectangle
// each. If more than one rect gets mapped to the same cell, such
// rectangles are unified using a minimum bounding rectangle.
// This way the amount of memory used is limited as the vector of
// rectangles cannot grow past the limit. At the same time, the
// CPU utilization is also limited as the rect addition cost
// is O(1) excluding the vector addition complexity.
// And since the vector size is limited, the cost of adding to vector
// cannot get out of hand either.
// This method is more expensive than simple "bonding box", however,
// it yields surprisingly good approximation results.
// Moreover, the approximation resolution can be controlled by tweaking
// the artificial grid size - the more rows/cols the better the
// resolution at the expense of higher memory/CPU utilization.
namespace WebCore {
class Damage {
WTF_FORBID_HEAP_ALLOCATION;
public:
using Rects = Vector<IntRect, 1>;
enum class Mode : uint8_t {
Rectangles, // Tracks dirty regions as rectangles, only unifying when maximum is reached.
BoundingBox, // Dirty region is always the minimum bounding box of all added rectangles.
Full, // All area is always dirty.
};
static constexpr uint32_t NoMaxRectangles = 0;
explicit Damage(const IntRect& rect, Mode mode = Mode::Rectangles, uint32_t maxRectangles = NoMaxRectangles)
: m_mode(mode)
, m_rect(rect)
{
initialize(maxRectangles);
}
explicit Damage(const IntSize& size, Mode mode = Mode::Rectangles, uint32_t maxRectangles = NoMaxRectangles)
: Damage({ { }, size }, mode, maxRectangles)
{
}
explicit Damage(const FloatSize& size, Mode mode = Mode::Rectangles, uint32_t maxRectangles = NoMaxRectangles)
: Damage(ceiledIntSize(LayoutSize(size)), mode, maxRectangles)
{
}
Damage(Damage&&) = default;
Damage(const Damage&) = default;
Damage& operator=(const Damage&) = default;
Damage& operator=(Damage&&) = default;
ALWAYS_INLINE Mode mode() const { return m_mode; }
ALWAYS_INLINE const IntRect& at(size_t index) const LIFETIME_BOUND
{
switch (m_mode) {
case Mode::Rectangles:
if (!m_rects.indices.isEmpty()) {
size_t i = 0;
for (const auto& rect : *this) {
if (i++ == index)
return rect;
}
}
return m_rects.rects.at(index);
case Mode::BoundingBox:
ASSERT(!index);
return m_minimumBoundingRectangle;
case Mode::Full:
ASSERT(!index);
return m_rect;
}
RELEASE_ASSERT_NOT_REACHED();
}
const IntRect& operator[](size_t index) const LIFETIME_BOUND { return at(index); }
ALWAYS_INLINE size_t size() const
{
// FIXME: we should not allow to create a Damage for an empty rect.
if (m_rect.isEmpty())
return 0;
switch (m_mode) {
case Mode::Rectangles:
if (m_rects.indices.isEmpty())
return m_rects.rects.size();
return m_rects.indices.bitCount();
case Mode::BoundingBox:
return m_minimumBoundingRectangle.isEmpty() ? 0 : 1;
case Mode::Full:
return 1;
}
RELEASE_ASSERT_NOT_REACHED();
}
ALWAYS_INLINE bool isEmpty() const
{
// FIXME: we should not allow to create a Damage for an empty rect.
if (m_rect.isEmpty())
return true;
switch (m_mode) {
case Mode::Rectangles:
return m_rects.indices.isEmpty() && m_rects.rects.isEmpty();
case Mode::BoundingBox:
return m_minimumBoundingRectangle.isEmpty();
case Mode::Full:
return false;
}
RELEASE_ASSERT_NOT_REACHED();
}
ALWAYS_INLINE const IntRect& bounds() const
{
switch (m_mode) {
case Mode::Rectangles:
case Mode::BoundingBox:
return m_minimumBoundingRectangle;
case Mode::Full:
return m_rect;
}
RELEASE_ASSERT_NOT_REACHED();
}
class iterator {
WTF_DEPRECATED_MAKE_FAST_ALLOCATED(iterator);
public:
iterator() = default;
iterator(const Damage& damage, size_t index)
: m_damage(&damage)
, m_index(index)
{
}
iterator(const Damage& damage, BitVector::iterator iter)
: m_damage(&damage)
, m_iter(iter)
, m_index(*iter)
{
}
const IntRect& operator*()
{
switch (m_damage->m_mode) {
case Mode::Rectangles:
return m_damage->m_rects.rects.at(m_index);
case Mode::BoundingBox:
ASSERT(!m_index);
return m_damage->m_minimumBoundingRectangle;
case Mode::Full:
ASSERT(!m_index);
return m_damage->m_rect;
}
RELEASE_ASSERT_NOT_REACHED();
}
iterator& operator++()
{
if (m_iter) {
++m_iter.value();
m_index = *m_iter.value();
} else
++m_index;
return *this;
}
bool operator==(const iterator& other) const
{
return m_index == other.m_index;
}
private:
const Damage* m_damage { nullptr };
std::optional<BitVector::iterator> m_iter;
size_t m_index { 0 };
};
iterator begin() const LIFETIME_BOUND
{
switch (m_mode) {
case Mode::Rectangles:
if (!m_rects.indices.isEmpty())
return iterator(*this, m_rects.indices.begin());
[[fallthrough]];
case Mode::BoundingBox:
case Mode::Full:
return iterator(*this, 0);
}
RELEASE_ASSERT_NOT_REACHED();
}
iterator end() const LIFETIME_BOUND
{
switch (m_mode) {
case Mode::Rectangles:
if (!m_rects.indices.isEmpty())
return iterator(*this, m_rects.indices.end());
[[fallthrough]];
case Mode::BoundingBox:
case Mode::Full:
return iterator(*this, size());
}
RELEASE_ASSERT_NOT_REACHED();
}
ALWAYS_INLINE const Rects& rectsForTesting() const
{
return m_rects.rects;
}
Region regionForTesting() const
{
Region region;
// FIXME: we should not allow to create a Damage for an empty rect.
if (m_rect.isEmpty())
return region;
for (const auto& rect : *this)
region.unite(Region { rect });
return region;
}
// May return overlapping rects.
ALWAYS_INLINE Rects rects() const
{
// FIXME: we should not allow to create a Damage for an empty rect.
if (m_rect.isEmpty())
return { };
Rects result;
result.reserveInitialCapacity(size());
for (const auto& rect : *this)
result.append(rect);
return result;
}
void makeFull()
{
if (m_mode == Mode::Full)
return;
m_mode = Mode::Full;
initialize(NoMaxRectangles);
}
bool add(const IntRect& rect)
{
if (rect.isEmpty() || !shouldAdd())
return false;
if (rect.contains(m_rect)) {
makeFull();
return true;
}
const auto rectsCount = size();
if (!rectsCount || rect.contains(m_minimumBoundingRectangle)) {
if (m_mode == Mode::Rectangles) {
if (rectsCount) {
m_rects.rects.clear();
m_rects.indices = { };
m_rects.shouldUnite = m_rects.gridCells.width() == 1 && m_rects.gridCells.height() == 1;
}
m_rects.rects.append(rect);
}
m_minimumBoundingRectangle = rect;
return true;
}
if (rectsCount == 1 && m_minimumBoundingRectangle.contains(rect))
return false;
m_minimumBoundingRectangle.unite(rect);
if (m_mode == Mode::BoundingBox) {
ASSERT(rectsCount == 1);
return true;
}
ASSERT(m_mode == Mode::Rectangles);
if (m_rects.shouldUnite) {
unite(rect);
return true;
}
if (rectsCount == m_rects.gridCells.unclampedArea()) {
m_rects.shouldUnite = true;
uniteExistingRects();
unite(rect);
return true;
}
m_rects.rects.append(rect);
return true;
}
ALWAYS_INLINE bool add(const FloatRect& rect)
{
if (rect.isEmpty() || !shouldAdd())
return false;
return add(enclosingIntRect(rect));
}
ALWAYS_INLINE bool add(const Rects& rects)
{
if (rects.isEmpty() || !shouldAdd())
return false;
// When adding rects to an empty Damage and we know we will need to unite,
// we can unite the rects directly.
if (m_mode == Mode::Rectangles && isEmpty()) {
auto rectsCount = rects.size();
auto gridArea = m_rects.gridCells.unclampedArea();
if (rectsCount > gridArea) {
m_rects.rects.grow(gridArea);
if (rectsCount > 1)
m_rects.indices = BitVector(rectsCount);
for (const auto& rect : rects) {
if (rect.isEmpty())
continue;
if (rect.contains(m_rect)) {
makeFull();
return true;
}
m_minimumBoundingRectangle.unite(rect);
unite(rect);
}
if (m_minimumBoundingRectangle.isEmpty()) {
// All rectangles were empty.
m_rects.rects.clear();
m_rects.indices = { };
return false;
}
m_rects.shouldUnite = true;
if (m_rects.rects.size() == 1)
m_rects.indices = { };
return true;
}
}
bool returnValue = false;
for (const auto& rect : rects)
returnValue |= add(rect);
return returnValue;
}
ALWAYS_INLINE bool add(const Damage& other)
{
if (other.isEmpty() || !shouldAdd())
return false;
if (other.m_mode == Mode::Full && m_rect == other.m_rect) {
makeFull();
return true;
}
if (m_mode == Mode::Rectangles) {
// When both Damage are already united and have the same rect and grid, we can just iterate the rects and unite them.
if (m_rects.shouldUnite && m_mode == other.m_mode && m_rect == other.m_rect && m_rects.gridCells == other.m_rects.gridCells && other.m_rects.shouldUnite && m_rects.rects.size() == other.m_rects.rects.size()) {
for (unsigned i = 0; i < m_rects.rects.size(); ++i)
m_rects.rects[i].unite(other.m_rects.rects[i]);
return true;
}
}
switch (other.m_mode) {
case Mode::Rectangles:
return add(other.m_rects.rects);
case Mode::BoundingBox:
return add(other.m_minimumBoundingRectangle);
case Mode::Full:
return add(other.m_rect);
}
RELEASE_ASSERT_NOT_REACHED();
}
private:
IntSize gridSize(int maxRectangles) const
{
const float widthToHeightRatio = static_cast<float>(m_rect.width()) / m_rect.height();
if (widthToHeightRatio >= 1) {
int gridHeight = std::max<int>(1, std::floor(std::sqrt(static_cast<float>(maxRectangles) / widthToHeightRatio)));
while (gridHeight > 1 && maxRectangles % gridHeight)
gridHeight--;
return { maxRectangles / gridHeight, gridHeight };
}
int gridWidth = std::max<int>(1, std::floor(std::sqrt(static_cast<float>(maxRectangles) * widthToHeightRatio)));
while (gridWidth > 1 && maxRectangles % gridWidth)
gridWidth--;
return { gridWidth, maxRectangles / gridWidth };
}
void initialize(uint32_t maxRectangles)
{
switch (m_mode) {
case Mode::Rectangles:
if (maxRectangles != NoMaxRectangles) {
m_rects.gridCells = gridSize(maxRectangles);
m_rects.cellSize = IntSize(std::ceil(static_cast<float>(m_rect.width()) / m_rects.gridCells.width()), std::ceil(static_cast<float>(m_rect.height()) / m_rects.gridCells.height()));
} else {
static constexpr int defaultCellSize { 256 };
m_rects.cellSize = { defaultCellSize, defaultCellSize };
m_rects.gridCells = IntSize(std::ceil(static_cast<float>(m_rect.width()) / m_rects.cellSize.width()), std::ceil(static_cast<float>(m_rect.height()) / m_rects.cellSize.height())).expandedTo({ 1, 1 });
}
m_rects.shouldUnite = m_rects.gridCells.width() == 1 && m_rects.gridCells.height() == 1;
break;
case Mode::BoundingBox:
case Mode::Full:
m_minimumBoundingRectangle = { };
m_rects = { };
break;
}
}
ALWAYS_INLINE bool shouldAdd() const
{
// FIXME: we should not allow to create a Damage for an empty rect.
return !m_rect.isEmpty() && m_mode != Mode::Full;
}
void uniteExistingRects()
{
auto rectsCount = m_rects.rects.size();
Rects rectsCopy(rectsCount);
m_rects.rects.swap(rectsCopy);
if (rectsCount > 1)
m_rects.indices = BitVector(rectsCount);
for (const auto& rect : rectsCopy)
unite(rect);
}
ALWAYS_INLINE size_t cellIndexForRect(const IntRect& rect) const
{
ASSERT(m_rects.rects.size() > 1);
const auto rectCenter = IntPoint(rect.center() - m_rect.location());
const auto rectCell = flooredIntPoint(FloatPoint { static_cast<float>(rectCenter.x()) / m_rects.cellSize.width(), static_cast<float>(rectCenter.y()) / m_rects.cellSize.height() });
return std::clamp(rectCell.x(), 0, m_rects.gridCells.width() - 1) + std::clamp(rectCell.y(), 0, m_rects.gridCells.height() - 1) * m_rects.gridCells.width();
}
void unite(const IntRect& rect)
{
// When merging cannot be avoided, we use m_rects to store minimal bounding rectangles
// and perform merging while trying to keep minimal bounding rectangles small and
// separated from each other.
if (m_rects.rects.size() == 1) {
m_rects.rects[0] = m_minimumBoundingRectangle;
return;
}
const auto index = cellIndexForRect(rect);
ASSERT(index < m_rects.rects.size());
m_rects.rects[index].unite(rect);
m_rects.indices.set(index);
}
Mode m_mode { Mode::Rectangles };
IntRect m_rect;
IntRect m_minimumBoundingRectangle;
struct {
Rects rects;
BitVector indices;
bool shouldUnite { false };
IntSize cellSize;
IntSize gridCells;
} m_rects;
};
static inline WTF::TextStream& operator<<(WTF::TextStream& ts, const Damage& damage)
{
return streamSizedContainer(ts, damage);
}
} // namespace WebCore
#endif // USE(COORDINATED_GRAPHICS)
|