Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dl_bounds_accumulator.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/display_list/utils/dl_bounds_accumulator.h"
6
7namespace flutter {
8
9void RectBoundsAccumulator::accumulate(const SkRect& r, int index) {
10 if (r.fLeft < r.fRight && r.fTop < r.fBottom) {
11 rect_.accumulate(r.fLeft, r.fTop);
12 rect_.accumulate(r.fRight, r.fBottom);
13 }
14}
15
17 saved_rects_.emplace_back(rect_);
18 rect_ = AccumulationRect();
19}
21 if (!saved_rects_.empty()) {
22 SkRect layer_bounds = rect_.bounds();
23 pop_and_accumulate(layer_bounds, nullptr);
24 }
25}
27 std::function<bool(const SkRect&, SkRect&)> mapper,
28 const SkRect* clip) {
29 bool success = true;
30 if (!saved_rects_.empty()) {
31 SkRect layer_bounds = rect_.bounds();
32 success = mapper(layer_bounds, layer_bounds);
33 pop_and_accumulate(layer_bounds, clip);
34 }
35 return success;
36}
37void RectBoundsAccumulator::pop_and_accumulate(SkRect& layer_bounds,
38 const SkRect* clip) {
39 FML_DCHECK(!saved_rects_.empty());
40
41 rect_ = saved_rects_.back();
42 saved_rects_.pop_back();
43
44 if (clip == nullptr || layer_bounds.intersect(*clip)) {
45 accumulate(layer_bounds, -1);
46 }
47}
48
49RectBoundsAccumulator::AccumulationRect::AccumulationRect() {
50 min_x_ = std::numeric_limits<SkScalar>::infinity();
51 min_y_ = std::numeric_limits<SkScalar>::infinity();
52 max_x_ = -std::numeric_limits<SkScalar>::infinity();
53 max_y_ = -std::numeric_limits<SkScalar>::infinity();
54}
55void RectBoundsAccumulator::AccumulationRect::accumulate(SkScalar x,
56 SkScalar y) {
57 if (min_x_ > x) {
58 min_x_ = x;
59 }
60 if (min_y_ > y) {
61 min_y_ = y;
62 }
63 if (max_x_ < x) {
64 max_x_ = x;
65 }
66 if (max_y_ < y) {
67 max_y_ = y;
68 }
69}
70SkRect RectBoundsAccumulator::AccumulationRect::bounds() const {
71 return (max_x_ >= min_x_ && max_y_ >= min_y_)
72 ? SkRect::MakeLTRB(min_x_, min_y_, max_x_, max_y_)
73 : SkRect::MakeEmpty();
74}
75
76void RTreeBoundsAccumulator::accumulate(const SkRect& r, int index) {
77 if (r.fLeft < r.fRight && r.fTop < r.fBottom) {
78 rects_.push_back(r);
79 rect_indices_.push_back(index);
80 }
81}
83 saved_offsets_.push_back(rects_.size());
84}
86 if (saved_offsets_.empty()) {
87 return;
88 }
89
90 saved_offsets_.pop_back();
91}
93 std::function<bool(const SkRect& original, SkRect& modified)> map,
94 const SkRect* clip) {
95 if (saved_offsets_.empty()) {
96 return true;
97 }
98
99 size_t previous_size = saved_offsets_.back();
100 saved_offsets_.pop_back();
101
102 bool success = true;
103 for (size_t i = previous_size; i < rects_.size(); i++) {
104 SkRect original = rects_[i];
105 if (!map(original, original)) {
106 success = false;
107 }
108 if (clip == nullptr || original.intersect(*clip)) {
109 rect_indices_[previous_size] = rect_indices_[i];
110 rects_[previous_size] = original;
111 previous_size++;
112 }
113 }
114 rects_.resize(previous_size);
115 rect_indices_.resize(previous_size);
116 return success;
117}
118
120 FML_DCHECK(saved_offsets_.empty());
121 RectBoundsAccumulator accumulator;
122 for (auto& rect : rects_) {
123 accumulator.accumulate(rect, 0);
124 }
125 return accumulator.bounds();
126}
127
129 FML_DCHECK(saved_offsets_.empty());
130 return sk_make_sp<DlRTree>(rects_.data(), rects_.size(), rect_indices_.data(),
131 [](int id) { return id >= 0; });
132}
133
134} // namespace flutter
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
void accumulate(const SkRect &r, int index) override
sk_sp< DlRTree > rtree() const override
void accumulate(SkScalar x, SkScalar y)
float SkScalar
Definition extension.cpp:12
#define FML_DCHECK(condition)
Definition logging.h:103
double y
double x
SkScalar fBottom
larger y-axis bounds
Definition extension.cpp:17
bool intersect(const SkRect &r)
Definition SkRect.cpp:114
SkScalar fLeft
smaller x-axis bounds
Definition extension.cpp:14
SkScalar fRight
larger x-axis bounds
Definition extension.cpp:16
static constexpr SkRect MakeLTRB(float l, float t, float r, float b)
Definition SkRect.h:646
SkScalar fTop
smaller y-axis bounds
Definition extension.cpp:15