Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display list R-Tree culling #38429

Merged
merged 14 commits into from
Dec 22, 2022
Merged
1 change: 1 addition & 0 deletions ci/licenses_golden/excluded_files
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
../../../flutter/display_list/display_list_matrix_clip_tracker_unittests.cc
../../../flutter/display_list/display_list_paint_unittests.cc
../../../flutter/display_list/display_list_path_effect_unittests.cc
../../../flutter/display_list/display_list_rtree_unittests.cc
../../../flutter/display_list/display_list_unittests.cc
../../../flutter/display_list/display_list_utils_unittests.cc
../../../flutter/display_list/display_list_vertices_unittests.cc
Expand Down
1 change: 1 addition & 0 deletions display_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ if (enable_unittests) {
"display_list_matrix_clip_tracker_unittests.cc",
"display_list_paint_unittests.cc",
"display_list_path_effect_unittests.cc",
"display_list_rtree_unittests.cc",
"display_list_unittests.cc",
"display_list_utils_unittests.cc",
"display_list_vertices_unittests.cc",
Expand Down
114 changes: 105 additions & 9 deletions display_list/display_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,105 @@ DisplayList::~DisplayList() {
DisposeOps(ptr, ptr + byte_count_);
}

class Culler {
public:
virtual bool init(DispatchContext& context) = 0;
virtual void update(DispatchContext& context) = 0;
};
class NopCuller : public Culler {
public:
static NopCuller instance;

bool init(DispatchContext& context) override {
context.next_render_index = 0;
flar marked this conversation as resolved.
Show resolved Hide resolved
return true;
}
void update(DispatchContext& context) override {}
};
NopCuller NopCuller::instance = NopCuller();
class VectorCuller : public Culler {
public:
VectorCuller(const DlRTree* rtree, const std::vector<int>& rect_indices)
: rtree_(rtree), cur_(rect_indices.begin()), end_(rect_indices.end()) {}

bool init(DispatchContext& context) override {
if (cur_ < end_) {
context.next_render_index = rtree_->id(*cur_++);
return true;
} else {
context.next_render_index = std::numeric_limits<int>::max();
return false;
}
}
void update(DispatchContext& context) override {
if (++context.cur_index > context.next_render_index) {
while (cur_ < end_) {
context.next_render_index = rtree_->id(*cur_++);
if (context.next_render_index >= context.cur_index) {
// It should be rare that we have duplicate indices
// but if we do, then having a while loop is a cheap
// insurance for those cases.
return;
}
}
context.next_render_index = std::numeric_limits<int>::max();
}
}

private:
const DlRTree* rtree_;
std::vector<int>::const_iterator cur_;
std::vector<int>::const_iterator end_;
};

void DisplayList::Dispatch(Dispatcher& ctx) const {
uint8_t* ptr = storage_.get();
Dispatch(ctx, ptr, ptr + byte_count_, NopCuller::instance);
}
void DisplayList::Dispatch(Dispatcher& ctx, const SkRect& cull_rect) {
if (cull_rect.isEmpty()) {
return;
}
if (cull_rect.contains(bounds())) {
Dispatch(ctx);
return;
}
const DlRTree* rtree = this->rtree().get();
FML_DCHECK(rtree != nullptr);
if (rtree == nullptr) {
FML_LOG(ERROR) << "dispatched with culling rect on DL with no rtree";
Dispatch(ctx);
return;
}
uint8_t* ptr = storage_.get();
std::vector<int> rect_indices;
rtree->search(cull_rect, &rect_indices);
VectorCuller culler(rtree, rect_indices);
Dispatch(ctx, ptr, ptr + byte_count_, culler);
}

void DisplayList::Dispatch(Dispatcher& dispatcher,
uint8_t* ptr,
uint8_t* end) const {
uint8_t* end,
Culler& culler) const {
DispatchContext context = {
.dispatcher = dispatcher,

.cur_index = 0,

.next_restore_index = std::numeric_limits<int>::max(),
};
if (!culler.init(context)) {
return;
}
while (ptr < end) {
auto op = reinterpret_cast<const DLOp*>(ptr);
ptr += op->size;
FML_DCHECK(ptr <= end);
switch (op->type) {
#define DL_OP_DISPATCH(name) \
case DisplayListOpType::k##name: \
static_cast<const name##Op*>(op)->dispatch(dispatcher); \
#define DL_OP_DISPATCH(name) \
case DisplayListOpType::k##name: \
static_cast<const name##Op*>(op)->dispatch(context); \
break;

FOR_EACH_DISPLAY_LIST_OP(DL_OP_DISPATCH)
Expand All @@ -73,6 +161,7 @@ void DisplayList::Dispatch(Dispatcher& dispatcher,
FML_DCHECK(false);
return;
}
culler.update(context);
}
}

Expand Down Expand Up @@ -166,18 +255,25 @@ static bool CompareOps(uint8_t* ptrA,
return true;
}

void DisplayList::RenderTo(DisplayListBuilder* builder,
SkScalar opacity) const {
void DisplayList::RenderTo(DisplayListBuilder* builder, SkScalar opacity) {
// TODO(100983): Opacity is not respected and attributes are not reset.
if (!builder) {
return;
}
Dispatch(*builder);
if (has_rtree()) {
Dispatch(*builder, builder->getLocalClipBounds());
} else {
Dispatch(*builder);
}
}

void DisplayList::RenderTo(SkCanvas* canvas, SkScalar opacity) const {
flar marked this conversation as resolved.
Show resolved Hide resolved
void DisplayList::RenderTo(SkCanvas* canvas, SkScalar opacity) {
DisplayListCanvasDispatcher dispatcher(canvas, opacity);
Dispatch(dispatcher);
if (has_rtree()) {
Dispatch(dispatcher, canvas->getLocalClipBounds());
} else {
Dispatch(dispatcher);
}
}

bool DisplayList::Equals(const DisplayList* other) const {
Expand Down
19 changes: 11 additions & 8 deletions display_list/display_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ class DisplayListStorage {
std::unique_ptr<uint8_t, FreeDeleter> ptr_;
};

class Culler;

// The base class that contains a sequence of rendering operations
// for dispatch to a Dispatcher. These objects must be instantiated
// through an instance of DisplayListBuilder::build().
Expand All @@ -244,15 +246,12 @@ class DisplayList : public SkRefCnt {

~DisplayList();

void Dispatch(Dispatcher& ctx) const {
uint8_t* ptr = storage_.get();
Dispatch(ctx, ptr, ptr + byte_count_);
}
void Dispatch(Dispatcher& ctx) const;
void Dispatch(Dispatcher& ctx, const SkRect& cull_rect);

void RenderTo(DisplayListBuilder* builder,
SkScalar opacity = SK_Scalar1) const;
void RenderTo(DisplayListBuilder* builder, SkScalar opacity = SK_Scalar1);

void RenderTo(SkCanvas* canvas, SkScalar opacity = SK_Scalar1) const;
void RenderTo(SkCanvas* canvas, SkScalar opacity = SK_Scalar1);

// SkPicture always includes nested bytes, but nested ops are
// only included if requested. The defaults used here for these
Expand All @@ -270,6 +269,7 @@ class DisplayList : public SkRefCnt {

const SkRect& bounds() { return bounds_; }

bool has_rtree() { return rtree_ != nullptr; }
sk_sp<const DlRTree> rtree() { return rtree_; }

bool Equals(const DisplayList* other) const;
Expand Down Expand Up @@ -305,7 +305,10 @@ class DisplayList : public SkRefCnt {
bool can_apply_group_opacity_;
sk_sp<const DlRTree> rtree_;

void Dispatch(Dispatcher& ctx, uint8_t* ptr, uint8_t* end) const;
void Dispatch(Dispatcher& ctx,
uint8_t* ptr,
uint8_t* end,
Culler& culler) const;

friend class DisplayListBuilder;
};
Expand Down
Loading