Improve DisplayList bounds calculations (flutter/engine#28656)

This commit is contained in:
Jim Graham
2021-09-17 13:23:02 -07:00
committed by GitHub
parent 69ba5fd75e
commit df4ad31448
8 changed files with 1557 additions and 472 deletions

View File

@@ -797,33 +797,33 @@ struct DrawTextBlobOp final : DLOp {
};
// 4 byte header + 28 byte payload packs evenly into 32 bytes
#define DEFINE_DRAW_SHADOW_OP(name, occludes) \
struct Draw##name##Op final : DLOp { \
static const auto kType = DisplayListOpType::kDraw##name; \
\
Draw##name##Op(const SkPath& path, \
SkColor color, \
SkScalar elevation, \
SkScalar dpr) \
: color(color), elevation(elevation), dpr(dpr), path(path) {} \
\
const SkColor color; \
const SkScalar elevation; \
const SkScalar dpr; \
const SkPath path; \
\
void dispatch(Dispatcher& dispatcher) const { \
dispatcher.drawShadow(path, color, elevation, occludes, dpr); \
} \
#define DEFINE_DRAW_SHADOW_OP(name, nonoccluding) \
struct Draw##name##Op final : DLOp { \
static const auto kType = DisplayListOpType::kDraw##name; \
\
Draw##name##Op(const SkPath& path, \
SkColor color, \
SkScalar elevation, \
SkScalar dpr) \
: color(color), elevation(elevation), dpr(dpr), path(path) {} \
\
const SkColor color; \
const SkScalar elevation; \
const SkScalar dpr; \
const SkPath path; \
\
void dispatch(Dispatcher& dispatcher) const { \
dispatcher.drawShadow(path, color, elevation, nonoccluding, dpr); \
} \
};
DEFINE_DRAW_SHADOW_OP(Shadow, false)
DEFINE_DRAW_SHADOW_OP(ShadowOccludes, true)
DEFINE_DRAW_SHADOW_OP(NonOccludingShadow, true)
#undef DEFINE_DRAW_SHADOW_OP
#pragma pack(pop, DLOp_Alignment)
void DisplayList::ComputeBounds() {
DisplayListBoundsCalculator calculator(bounds_cull_);
DisplayListBoundsCalculator calculator(&bounds_cull_);
Dispatch(calculator);
bounds_ = calculator.getBounds();
}
@@ -1365,10 +1365,10 @@ void DisplayListBuilder::drawTextBlob(const sk_sp<SkTextBlob> blob,
void DisplayListBuilder::drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) {
occludes //
? Push<DrawShadowOccludesOp>(0, 1, path, color, elevation, dpr)
transparentOccluder //
? Push<DrawNonOccludingShadowOp>(0, 1, path, color, elevation, dpr)
: Push<DrawShadowOp>(0, 1, path, color, elevation, dpr);
}

View File

@@ -148,7 +148,7 @@ namespace flutter {
V(DrawTextBlob) \
\
V(DrawShadow) \
V(DrawShadowOccludes)
V(DrawNonOccludingShadow)
#define DL_OP_TO_ENUM_VALUE(name) k##name,
enum class DisplayListOpType { FOR_EACH_DISPLAY_LIST_OP(DL_OP_TO_ENUM_VALUE) };
@@ -245,8 +245,8 @@ class Dispatcher {
virtual void setMaskBlurFilter(SkBlurStyle style, SkScalar sigma) = 0;
virtual void save() = 0;
virtual void restore() = 0;
virtual void saveLayer(const SkRect* bounds, bool restoreWithPaint) = 0;
virtual void restore() = 0;
virtual void translate(SkScalar tx, SkScalar ty) = 0;
virtual void scale(SkScalar sx, SkScalar sy) = 0;
@@ -325,7 +325,7 @@ class Dispatcher {
virtual void drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) = 0;
};
@@ -358,8 +358,8 @@ class DisplayListBuilder final : public virtual Dispatcher, public SkRefCnt {
void setMaskBlurFilter(SkBlurStyle style, SkScalar sigma) override;
void save() override;
void restore() override;
void saveLayer(const SkRect* bounds, bool restoreWithPaint) override;
void restore() override;
void translate(SkScalar tx, SkScalar ty) override;
void scale(SkScalar sx, SkScalar sy) override;
@@ -440,7 +440,7 @@ class DisplayListBuilder final : public virtual Dispatcher, public SkRefCnt {
void drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) override;
sk_sp<DisplayList> Build();

View File

@@ -176,10 +176,10 @@ void DisplayListCanvasDispatcher::drawTextBlob(const sk_sp<SkTextBlob> blob,
void DisplayListCanvasDispatcher::drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) {
flutter::PhysicalShapeLayer::DrawShadow(canvas_, path, color, elevation,
occludes, dpr);
transparentOccluder, dpr);
}
DisplayListCanvasRecorder::DisplayListCanvasRecorder(const SkRect& bounds)

View File

@@ -110,7 +110,7 @@ class DisplayListCanvasDispatcher : public virtual Dispatcher,
void drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) override;
private:

File diff suppressed because it is too large Load Diff

View File

@@ -888,6 +888,7 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
SkColorFilters::Matrix(alpha_matrix);
{
// No tricky stuff, just verifying drawing a rect produces rect bounds
DisplayListBuilder builder(build_bounds);
builder.saveLayer(&save_bounds, true);
builder.drawRect(rect);
@@ -897,6 +898,7 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
}
{
// Now checking that a normal color filter still produces rect bounds
DisplayListBuilder builder(build_bounds);
builder.setColorFilter(base_color_filter);
builder.saveLayer(&save_bounds, true);
@@ -908,6 +910,26 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
}
{
// Now checking how SkPictureRecorder deals with a color filter
// that modifies alpha channels (save layer bounds are meaningless
// under those circumstances)
SkPictureRecorder recorder;
SkCanvas* canvas = recorder.beginRecording(build_bounds);
SkPaint p1;
p1.setColorFilter(alpha_color_filter);
canvas->saveLayer(save_bounds, &p1);
SkPaint p2;
canvas->drawRect(rect, p2);
canvas->restore();
sk_sp<SkPicture> picture = recorder.finishRecordingAsPicture();
ASSERT_EQ(picture->cullRect(), build_bounds);
}
{
// Now checking that DisplayList has the same behavior that we
// saw in the SkPictureRecorder example above - returning the
// cull rect of the DisplayListBuilder when it encounters a
// save layer that modifies an unbounded region
DisplayListBuilder builder(build_bounds);
builder.setColorFilter(alpha_color_filter);
builder.saveLayer(&save_bounds, true);
@@ -915,10 +937,12 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
builder.drawRect(rect);
builder.restore();
sk_sp<DisplayList> display_list = builder.Build();
ASSERT_EQ(display_list->bounds(), save_bounds);
ASSERT_EQ(display_list->bounds(), build_bounds);
}
{
// Verifying that the save layer bounds are not relevant
// to the behavior in the previous example
DisplayListBuilder builder(build_bounds);
builder.setColorFilter(alpha_color_filter);
builder.saveLayer(nullptr, true);
@@ -930,6 +954,8 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
}
{
// Making sure hiding a ColorFilter as an ImageFilter will
// generate the same behavior as setting it as a ColorFilter
DisplayListBuilder builder(build_bounds);
builder.setImageFilter(
SkImageFilters::ColorFilter(base_color_filter, nullptr));
@@ -942,6 +968,8 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
}
{
// Making sure hiding a problematic ColorFilter as an ImageFilter
// will generate the same behavior as setting it as a ColorFilter
DisplayListBuilder builder(build_bounds);
builder.setImageFilter(
SkImageFilters::ColorFilter(alpha_color_filter, nullptr));
@@ -950,10 +978,11 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
builder.drawRect(rect);
builder.restore();
sk_sp<DisplayList> display_list = builder.Build();
ASSERT_EQ(display_list->bounds(), save_bounds);
ASSERT_EQ(display_list->bounds(), build_bounds);
}
{
// Same as above (ImageFilter hiding ColorFilter) with no save bounds
DisplayListBuilder builder(build_bounds);
builder.setImageFilter(
SkImageFilters::ColorFilter(alpha_color_filter, nullptr));
@@ -966,6 +995,7 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
}
{
// Testing behavior with an unboundable blend mode
DisplayListBuilder builder(build_bounds);
builder.setBlendMode(SkBlendMode::kClear);
builder.saveLayer(&save_bounds, true);
@@ -973,10 +1003,11 @@ TEST(DisplayList, DisplayListSaveLayerBoundsWithAlphaFilter) {
builder.drawRect(rect);
builder.restore();
sk_sp<DisplayList> display_list = builder.Build();
ASSERT_EQ(display_list->bounds(), save_bounds);
ASSERT_EQ(display_list->bounds(), build_bounds);
}
{
// Same as previous with no save bounds
DisplayListBuilder builder(build_bounds);
builder.setBlendMode(SkBlendMode::kClear);
builder.saveLayer(nullptr, true);

View File

@@ -137,101 +137,210 @@ void SkMatrixDispatchHelper::reset() {
}
void ClipBoundsDispatchHelper::clipRect(const SkRect& rect,
bool isAA,
bool is_aa,
SkClipOp clip_op) {
if (clip_op == SkClipOp::kIntersect) {
intersect(rect);
intersect(rect, is_aa);
}
}
void ClipBoundsDispatchHelper::clipRRect(const SkRRect& rrect,
bool isAA,
bool is_aa,
SkClipOp clip_op) {
if (clip_op == SkClipOp::kIntersect) {
intersect(rrect.getBounds());
intersect(rrect.getBounds(), is_aa);
}
}
void ClipBoundsDispatchHelper::clipPath(const SkPath& path,
bool isAA,
bool is_aa,
SkClipOp clip_op) {
if (clip_op == SkClipOp::kIntersect) {
intersect(path.getBounds());
intersect(path.getBounds(), is_aa);
}
}
void ClipBoundsDispatchHelper::intersect(const SkRect& rect) {
void ClipBoundsDispatchHelper::intersect(const SkRect& rect, bool is_aa) {
SkRect devClipBounds = matrix().mapRect(rect);
if (!bounds_.intersect(devClipBounds)) {
bounds_.setEmpty();
if (is_aa) {
devClipBounds.roundOut(&devClipBounds);
}
if (has_clip_) {
if (!bounds_.intersect(devClipBounds)) {
bounds_.setEmpty();
}
} else {
has_clip_ = true;
if (devClipBounds.isEmpty()) {
bounds_.setEmpty();
} else {
bounds_ = devClipBounds;
}
}
}
void ClipBoundsDispatchHelper::save() {
saved_.push_back(bounds_);
if (!has_clip_) {
saved_.push_back(SkRect::MakeLTRB(0, 0, -1, -1));
} else if (bounds_.isEmpty()) {
saved_.push_back(SkRect::MakeEmpty());
} else {
saved_.push_back(bounds_);
}
}
void ClipBoundsDispatchHelper::restore() {
bounds_ = saved_.back();
saved_.pop_back();
has_clip_ = (bounds_.fLeft <= bounds_.fRight && //
bounds_.fTop <= bounds_.fBottom);
if (!has_clip_) {
bounds_.setEmpty();
}
}
void ClipBoundsDispatchHelper::reset(const SkRect* cull_rect) {
if ((has_clip_ = ((bool)cull_rect)) && !cull_rect->isEmpty()) {
bounds_ = *cull_rect;
} else {
bounds_.setEmpty();
}
}
void DisplayListBoundsCalculator::saveLayer(const SkRect* bounds,
bool with_paint) {
SkMatrixDispatchHelper::save();
ClipBoundsDispatchHelper::save();
saved_infos_.emplace_back(
with_paint ? std::make_unique<SaveLayerWithPaintInfo>(
this, accumulator_, matrix(), bounds, paint())
: std::make_unique<SaveLayerInfo>(accumulator_, matrix()));
accumulator_ = saved_infos_.back()->save();
SkMatrixDispatchHelper::reset();
DisplayListBoundsCalculator::DisplayListBoundsCalculator(
const SkRect* cull_rect)
: ClipBoundsDispatchHelper(cull_rect) {
layer_infos_.emplace_back(std::make_unique<RootLayerData>());
accumulator_ = layer_infos_.back()->accumulatorForLayer();
}
void DisplayListBoundsCalculator::setCaps(SkPaint::Cap cap) {
cap_is_square_ = (cap == SkPaint::kSquare_Cap);
}
void DisplayListBoundsCalculator::setJoins(SkPaint::Join join) {
join_is_miter_ = (join == SkPaint::kMiter_Join);
}
void DisplayListBoundsCalculator::setDrawStyle(SkPaint::Style style) {
style_flag_ = (style == SkPaint::kFill_Style) ? kIsFilledGeometry //
: kIsStrokedGeometry;
}
void DisplayListBoundsCalculator::setStrokeWidth(SkScalar width) {
half_stroke_width_ = std::max(width * 0.5f, kMinStrokeWidth);
}
void DisplayListBoundsCalculator::setMiterLimit(SkScalar limit) {
miter_limit_ = std::max(limit, 1.0f);
}
void DisplayListBoundsCalculator::setBlendMode(SkBlendMode mode) {
blend_mode_ = mode;
}
void DisplayListBoundsCalculator::setBlender(sk_sp<SkBlender> blender) {
SkPaint paint;
paint.setBlender(std::move(blender));
blend_mode_ = paint.asBlendMode();
}
void DisplayListBoundsCalculator::setImageFilter(sk_sp<SkImageFilter> filter) {
image_filter_ = std::move(filter);
}
void DisplayListBoundsCalculator::setColorFilter(sk_sp<SkColorFilter> filter) {
color_filter_ = std::move(filter);
}
void DisplayListBoundsCalculator::setPathEffect(sk_sp<SkPathEffect> effect) {
path_effect_ = std::move(effect);
}
void DisplayListBoundsCalculator::setMaskFilter(sk_sp<SkMaskFilter> filter) {
mask_filter_ = std::move(filter);
mask_sigma_pad_ = 0.0f;
}
void DisplayListBoundsCalculator::setMaskBlurFilter(SkBlurStyle style,
SkScalar sigma) {
mask_sigma_pad_ = std::max(3.0f * sigma, 0.0f);
mask_filter_ = nullptr;
}
void DisplayListBoundsCalculator::save() {
SkMatrixDispatchHelper::save();
ClipBoundsDispatchHelper::save();
saved_infos_.emplace_back(std::make_unique<SaveInfo>(accumulator_));
accumulator_ = saved_infos_.back()->save();
layer_infos_.emplace_back(std::make_unique<SaveData>(accumulator_));
accumulator_ = layer_infos_.back()->accumulatorForLayer();
}
void DisplayListBoundsCalculator::saveLayer(const SkRect* bounds,
bool with_paint) {
SkMatrixDispatchHelper::save();
ClipBoundsDispatchHelper::save();
if (with_paint) {
layer_infos_.emplace_back(std::make_unique<SaveLayerData>(
accumulator_, image_filter_, paintNopsOnTransparenBlack()));
} else {
layer_infos_.emplace_back(
std::make_unique<SaveLayerData>(accumulator_, nullptr, true));
}
accumulator_ = layer_infos_.back()->accumulatorForLayer();
// Accumulate the layer in its own coordinate system and then
// filter and transform its bounds on restore.
SkMatrixDispatchHelper::reset();
ClipBoundsDispatchHelper::reset(bounds);
}
void DisplayListBoundsCalculator::restore() {
if (!saved_infos_.empty()) {
if (layer_infos_.size() > 1) {
SkMatrixDispatchHelper::restore();
ClipBoundsDispatchHelper::restore();
accumulator_ = saved_infos_.back()->restore();
saved_infos_.pop_back();
accumulator_ = layer_infos_.back()->accumulatorForRestore();
SkRect layer_bounds = layer_infos_.back()->getLayerBounds();
// Must read unbounded state after layer_bounds
bool layer_unbounded = layer_infos_.back()->is_unbounded();
layer_infos_.pop_back();
// We accumulate the bounds even if the layer was unbounded because
// the unbounded state may be contained at a higher level, so we at
// least accumulate our best estimate about what we have.
if (!layer_bounds.isEmpty()) {
// kUnfiltered because the layer already applied all bounds
// modifications based on the attributes that were in place
// when it was instantiated. Modifying it further base on the
// current attributes would mix attribute states.
accumulateRect(layer_bounds, kIsUnfiltered);
}
if (layer_unbounded) {
accumulateUnbounded();
}
}
}
void DisplayListBoundsCalculator::drawPaint() {
if (!bounds_cull_.isEmpty()) {
root_accumulator_.accumulate(bounds_cull_);
}
accumulateUnbounded();
}
void DisplayListBoundsCalculator::drawColor(SkColor color, SkBlendMode mode) {
if (!bounds_cull_.isEmpty()) {
root_accumulator_.accumulate(bounds_cull_);
}
accumulateUnbounded();
}
void DisplayListBoundsCalculator::drawLine(const SkPoint& p0,
const SkPoint& p1) {
SkRect bounds = SkRect::MakeLTRB(p0.fX, p0.fY, p1.fX, p1.fY).makeSorted();
accumulateRect(bounds, true);
int cap_flag = kIsStrokedGeometry;
if (bounds.width() > 0.0f && bounds.height() > 0.0f) {
cap_flag |= kGeometryMayHaveDiagonalEndCaps;
}
accumulateRect(bounds, cap_flag);
}
void DisplayListBoundsCalculator::drawRect(const SkRect& rect) {
accumulateRect(rect);
accumulateRect(rect, kIsDrawnGeometry);
}
void DisplayListBoundsCalculator::drawOval(const SkRect& bounds) {
accumulateRect(bounds);
accumulateRect(bounds, kIsDrawnGeometry);
}
void DisplayListBoundsCalculator::drawCircle(const SkPoint& center,
SkScalar radius) {
accumulateRect(SkRect::MakeLTRB(center.fX - radius, center.fY - radius,
center.fX + radius, center.fY + radius));
center.fX + radius, center.fY + radius),
kIsDrawnGeometry);
}
void DisplayListBoundsCalculator::drawRRect(const SkRRect& rrect) {
accumulateRect(rrect.getBounds());
accumulateRect(rrect.getBounds(), kIsDrawnGeometry);
}
void DisplayListBoundsCalculator::drawDRRect(const SkRRect& outer,
const SkRRect& inner) {
accumulateRect(outer.getBounds());
accumulateRect(outer.getBounds(), kIsDrawnGeometry);
}
void DisplayListBoundsCalculator::drawPath(const SkPath& path) {
accumulateRect(path.getBounds());
if (path.isInverseFillType()) {
accumulateUnbounded();
} else {
accumulateRect(path.getBounds(), //
(kIsDrawnGeometry | //
kGeometryMayHaveDiagonalEndCaps | //
kGeometryMayHaveProblematicJoins));
}
}
void DisplayListBoundsCalculator::drawArc(const SkRect& bounds,
SkScalar start,
@@ -240,7 +349,7 @@ void DisplayListBoundsCalculator::drawArc(const SkRect& bounds,
// This could be tighter if we compute where the start and end
// angles are and then also consider the quadrants swept and
// the center if specified.
accumulateRect(bounds);
accumulateRect(bounds, kIsDrawnGeometry | kGeometryMayHaveDiagonalEndCaps);
}
void DisplayListBoundsCalculator::drawPoints(SkCanvas::PointMode mode,
uint32_t count,
@@ -250,19 +359,24 @@ void DisplayListBoundsCalculator::drawPoints(SkCanvas::PointMode mode,
for (size_t i = 0; i < count; i++) {
ptBounds.accumulate(pts[i]);
}
accumulateRect(ptBounds.getBounds(), true);
int flags = kIsStrokedGeometry;
if (mode != SkCanvas::kPoints_PointMode) {
flags |= kGeometryMayHaveDiagonalEndCaps;
// Even Polygon mode just draws (count-1) separate lines, no joins
}
accumulateRect(ptBounds.getBounds(), flags);
}
}
void DisplayListBoundsCalculator::drawVertices(const sk_sp<SkVertices> vertices,
SkBlendMode mode) {
accumulateRect(vertices->bounds());
accumulateRect(vertices->bounds(), kIsNonGeometric);
}
void DisplayListBoundsCalculator::drawImage(const sk_sp<SkImage> image,
const SkPoint point,
const SkSamplingOptions& sampling) {
SkRect bounds = SkRect::Make(image->bounds());
bounds.offset(point);
accumulateRect(bounds);
SkRect bounds = SkRect::MakeXYWH(point.fX, point.fY, //
image->width(), image->height());
accumulateRect(bounds, kIsNonGeometric | kApplyMaskFilter);
}
void DisplayListBoundsCalculator::drawImageRect(
const sk_sp<SkImage> image,
@@ -270,13 +384,13 @@ void DisplayListBoundsCalculator::drawImageRect(
const SkRect& dst,
const SkSamplingOptions& sampling,
SkCanvas::SrcRectConstraint constraint) {
accumulateRect(dst);
accumulateRect(dst, kIsNonGeometric | kApplyMaskFilter);
}
void DisplayListBoundsCalculator::drawImageNine(const sk_sp<SkImage> image,
const SkIRect& center,
const SkRect& dst,
SkFilterMode filter) {
accumulateRect(dst);
accumulateRect(dst, kIsNonGeometric);
}
void DisplayListBoundsCalculator::drawImageLattice(
const sk_sp<SkImage> image,
@@ -284,7 +398,7 @@ void DisplayListBoundsCalculator::drawImageLattice(
const SkRect& dst,
SkFilterMode filter,
bool with_paint) {
accumulateRect(dst);
accumulateRect(dst, kIsNonGeometric | kApplyMaskFilter);
}
void DisplayListBoundsCalculator::drawAtlas(const sk_sp<SkImage> atlas,
const SkRSXform xform[],
@@ -304,7 +418,7 @@ void DisplayListBoundsCalculator::drawAtlas(const sk_sp<SkImage> atlas,
}
}
if (atlasBounds.isNotEmpty()) {
accumulateRect(atlasBounds.getBounds());
accumulateRect(atlasBounds.getBounds(), kIsNonGeometric);
}
}
void DisplayListBoundsCalculator::drawPicture(const sk_sp<SkPicture> picture,
@@ -317,118 +431,150 @@ void DisplayListBoundsCalculator::drawPicture(const sk_sp<SkPicture> picture,
if (pic_matrix) {
pic_matrix->mapRect(&bounds);
}
if (with_save_layer) {
accumulateRect(bounds);
} else {
matrix().mapRect(&bounds);
accumulator_->accumulate(bounds);
}
accumulateRect(bounds, with_save_layer ? kIsNonGeometric : kIsUnfiltered);
}
void DisplayListBoundsCalculator::drawDisplayList(
const sk_sp<DisplayList> display_list) {
accumulateRect(display_list->bounds());
accumulateRect(display_list->bounds(), kIsUnfiltered);
}
void DisplayListBoundsCalculator::drawTextBlob(const sk_sp<SkTextBlob> blob,
SkScalar x,
SkScalar y) {
accumulateRect(blob->bounds().makeOffset(x, y));
accumulateRect(blob->bounds().makeOffset(x, y), kIsFilledGeometry);
}
void DisplayListBoundsCalculator::drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) {
accumulateRect(
PhysicalShapeLayer::ComputeShadowBounds(path, elevation, dpr, matrix()));
SkRect shadow_bounds =
PhysicalShapeLayer::ComputeShadowBounds(path, elevation, dpr, matrix());
accumulateRect(shadow_bounds, kIsUnfiltered);
}
void DisplayListBoundsCalculator::accumulateRect(const SkRect& rect,
bool forceStroke) {
SkRect dstRect = rect;
const SkPaint& p = paint();
if (forceStroke) {
if (p.getStyle() == SkPaint::kFill_Style) {
setDrawStyle(SkPaint::kStroke_Style);
bool DisplayListBoundsCalculator::getFilteredBounds(SkRect& bounds,
SkImageFilter* filter) {
if (filter) {
if (!filter->canComputeFastBounds()) {
return false;
}
bounds = filter->computeFastBounds(bounds);
}
return true;
}
bool DisplayListBoundsCalculator::adjustBoundsForPaint(SkRect& bounds,
int flags) {
if ((flags & kIsUnfiltered) != 0) {
FML_DCHECK(flags == kIsUnfiltered);
return true;
}
if ((flags & kIsAnyGeometryMask) != 0) {
if ((flags & kIsDrawnGeometry) != 0) {
FML_DCHECK((flags & (kIsFilledGeometry | kIsStrokedGeometry)) == 0);
flags |= style_flag_;
}
// Path effect occurs before stroking...
if (path_effect_) {
SkPathEffect::DashInfo info;
if (path_effect_->asADash(&info) == SkPathEffect::kDash_DashType) {
// A dash effect has a very simple impact. It cannot introduce any
// miter joins that weren't already present in the original path
// and it does not grow the bounds of the path, but it can add
// end caps to areas that might not have had them before so all
// we need to do is to indicate the potential for diagonal
// end caps and move on.
flags |= kGeometryMayHaveDiagonalEndCaps;
} else {
SkPaint p;
p.setPathEffect(path_effect_);
if (!p.canComputeFastBounds()) {
return false;
}
bounds = p.computeFastBounds(bounds, &bounds);
flags |= (kGeometryMayHaveDiagonalEndCaps |
kGeometryMayHaveProblematicJoins);
}
}
if ((flags & kIsStrokedGeometry) != 0) {
FML_DCHECK((flags & kIsFilledGeometry) == 0);
// Determine the max multiplier to the stroke width first.
SkScalar pad = 1.0f;
if (join_is_miter_ && (flags & kGeometryMayHaveProblematicJoins) != 0) {
pad = std::max(pad, miter_limit_);
}
if (cap_is_square_ && (flags & kGeometryMayHaveDiagonalEndCaps) != 0) {
pad = std::max(pad, SK_ScalarSqrt2);
}
pad *= half_stroke_width_;
bounds.outset(pad, pad);
} else {
forceStroke = false;
FML_DCHECK((flags & kIsStrokedGeometry) == 0);
}
flags |= kApplyMaskFilter;
} else {
FML_DCHECK((flags & (kGeometryMayHaveDiagonalEndCaps |
kGeometryMayHaveProblematicJoins)) == 0);
}
if ((flags & kApplyMaskFilter) != 0) {
if (mask_filter_) {
SkPaint p;
p.setMaskFilter(mask_filter_);
if (!p.canComputeFastBounds()) {
return false;
}
bounds = p.computeFastBounds(bounds, &bounds);
}
if (mask_sigma_pad_ > 0.0f) {
bounds.outset(mask_sigma_pad_, mask_sigma_pad_);
}
}
if (p.canComputeFastBounds()) {
dstRect = p.computeFastBounds(rect, &dstRect);
matrix().mapRect(&dstRect);
accumulator_->accumulate(dstRect);
return getFilteredBounds(bounds, image_filter_.get());
}
void DisplayListBoundsCalculator::accumulateUnbounded() {
if (has_clip()) {
accumulator_->accumulate(getClipBounds());
} else {
root_accumulator_.accumulate(bounds_cull_);
layer_infos_.back()->set_unbounded();
}
if (forceStroke) {
setDrawStyle(SkPaint::kFill_Style);
}
void DisplayListBoundsCalculator::accumulateRect(SkRect& rect, int flags) {
if (adjustBoundsForPaint(rect, flags)) {
matrix().mapRect(&rect);
if (!has_clip() || rect.intersect(getClipBounds())) {
accumulator_->accumulate(rect);
}
} else {
accumulateUnbounded();
}
}
DisplayListBoundsCalculator::SaveInfo::SaveInfo(BoundsAccumulator* accumulator)
: saved_accumulator_(accumulator) {}
BoundsAccumulator* DisplayListBoundsCalculator::SaveInfo::save() {
// No need to swap out the accumulator for a normal save
return saved_accumulator_;
}
BoundsAccumulator* DisplayListBoundsCalculator::SaveInfo::restore() {
return saved_accumulator_;
}
DisplayListBoundsCalculator::SaveLayerInfo::SaveLayerInfo(
BoundsAccumulator* accumulator,
const SkMatrix& matrix)
: SaveInfo(accumulator), matrix_(matrix) {}
BoundsAccumulator* DisplayListBoundsCalculator::SaveLayerInfo::save() {
// Use the local layerAccumulator until restore is called and
// then transform (and adjust with paint if necessary) on restore()
return &layer_accumulator_;
}
BoundsAccumulator* DisplayListBoundsCalculator::SaveLayerInfo::restore() {
SkRect layer_bounds = layer_accumulator_.getBounds();
layer_bounds.roundOut(&layer_bounds);
matrix_.mapRect(&layer_bounds);
saved_accumulator_->accumulate(layer_bounds);
return saved_accumulator_;
}
DisplayListBoundsCalculator::SaveLayerWithPaintInfo::SaveLayerWithPaintInfo(
DisplayListBoundsCalculator* calculator,
BoundsAccumulator* accumulator,
const SkMatrix& saveMatrix,
const SkRect* saveBounds,
const SkPaint& savePaint)
: SaveLayerInfo(accumulator, saveMatrix),
calculator_(calculator),
paint_(savePaint) {
if (saveBounds) {
bounds_.emplace(*saveBounds);
}
}
static bool PaintNopsOnTransparenBlack(const SkPaint& paint) {
SkImageFilter* image_filter = paint.getImageFilter();
bool DisplayListBoundsCalculator::paintNopsOnTransparenBlack() {
// SkImageFilter::canComputeFastBounds tests for transparency behavior
// This test assumes that the blend mode checked down below will
// NOP on transparent black.
if (image_filter && !image_filter->canComputeFastBounds()) {
if (image_filter_ && !image_filter_->canComputeFastBounds()) {
return false;
}
SkColorFilter* color_filter = paint.getColorFilter();
// We filter the transparent black that is used for the background of a
// saveLayer and make sure it returns transparent black. If it does, then
// the color filter will leave all area surrounding the contents of the
// save layer untouched out to the edge of the output surface.
// This test assumes that the blend mode checked down below will
// NOP on transparent black.
if (color_filter &&
color_filter->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT) {
if (color_filter_ &&
color_filter_->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT) {
return false;
}
const auto blend_mode = paint.asBlendMode();
if (!blend_mode) {
if (!blend_mode_) {
return false; // can we query other blenders for this?
}
// Unusual blendmodes require us to process a saved layer
@@ -436,7 +582,7 @@ static bool PaintNopsOnTransparenBlack(const SkPaint& paint) {
// For example, DstIn is used by masking layers.
// https://code.google.com/p/skia/issues/detail?id=1291
// https://crbug.com/401593
switch (blend_mode.value()) {
switch (blend_mode_.value()) {
// For each of the following transfer modes, if the source
// alpha is zero (our transparent black), the resulting
// blended pixel is not necessarily equal to the original
@@ -485,30 +631,4 @@ static bool PaintNopsOnTransparenBlack(const SkPaint& paint) {
}
}
BoundsAccumulator*
DisplayListBoundsCalculator::SaveLayerWithPaintInfo::restore() {
SkRect layer_bounds;
if (paint_.canComputeFastBounds() && PaintNopsOnTransparenBlack(paint_)) {
// The ideal situation. The paint can compute the bounds AND the
// surrounding transparent pixels will not affect the destination.
layer_bounds = layer_accumulator_.getBounds();
layer_bounds = paint_.computeFastBounds(layer_bounds, &layer_bounds);
} else if (bounds_.has_value()) {
// Bounds were provided by the save layer, the operation will affect
// all of those bounds.
layer_bounds = bounds_.value();
} else {
// Bounds were not provided for the save layer. We will fill to the
// cull bounds provided to the original DisplayList.
calculator_->root_accumulator_.accumulate(calculator_->bounds_cull_);
// There is no need to process the layer bounds further as we just
// expanded bounds to the cull rect of the DisplayList.
return saved_accumulator_;
}
layer_bounds.roundOut(&layer_bounds);
matrix_.mapRect(&layer_bounds);
saved_accumulator_->accumulate(layer_bounds);
return saved_accumulator_;
}
} // namespace flutter

View File

@@ -190,20 +190,32 @@ class SkMatrixDispatchHelper : public virtual Dispatcher,
class ClipBoundsDispatchHelper : public virtual Dispatcher,
private virtual SkMatrixSource {
public:
void clipRect(const SkRect& rect, bool isAA, SkClipOp clip_op) override;
void clipRRect(const SkRRect& rrect, bool isAA, SkClipOp clip_op) override;
void clipPath(const SkPath& path, bool isAA, SkClipOp clip_op) override;
ClipBoundsDispatchHelper() : ClipBoundsDispatchHelper(nullptr) {}
ClipBoundsDispatchHelper(const SkRect* cull_rect)
: has_clip_(cull_rect),
bounds_(cull_rect && !cull_rect->isEmpty() ? *cull_rect
: SkRect::MakeEmpty()) {}
void clipRect(const SkRect& rect, bool is_aa, SkClipOp clip_op) override;
void clipRRect(const SkRRect& rrect, bool is_aa, SkClipOp clip_op) override;
void clipPath(const SkPath& path, bool is_aa, SkClipOp clip_op) override;
void save() override;
void restore() override;
const SkRect& getCullingBounds() const { return bounds_; }
bool has_clip() const { return has_clip_; }
const SkRect& getClipBounds() const { return bounds_; }
protected:
void reset(const SkRect* cull_rect);
private:
bool has_clip_;
SkRect bounds_;
std::vector<SkRect> saved_;
void intersect(const SkRect& clipBounds);
void intersect(const SkRect& clipBounds, bool is_aa);
};
class BoundsAccumulator {
@@ -246,19 +258,36 @@ class BoundsAccumulator {
// bounds of the rendering operations.
class DisplayListBoundsCalculator final
: public virtual Dispatcher,
public virtual SkPaintDispatchHelper,
public virtual IgnoreAttributeDispatchHelper,
public virtual SkMatrixDispatchHelper,
public virtual ClipBoundsDispatchHelper {
public:
// Construct a Calculator to determine the bounds of a list of
// DisplayList dispatcher method calls. Since 2 of the method calls
// have no intrinsic size because they render to the entire available,
// the |cullRect| provides a bounds for them to include.
DisplayListBoundsCalculator(const SkRect& cull_rect = SkRect::MakeEmpty())
: accumulator_(&root_accumulator_), bounds_cull_(cull_rect) {}
// have no intrinsic size because they flood the entire clip/surface,
// the |cull_rect| provides a bounds for them to include. If cull_rect
// is not specified or is null, then the unbounded calls will not
// affect the resulting bounds, but will set a flag that can be
// queried using |isUnbounded| if an alternate plan is available
// for such cases.
// The flag should never be set if a cull_rect is provided.
DisplayListBoundsCalculator(const SkRect* cull_rect = nullptr);
void setCaps(SkPaint::Cap cap) override;
void setJoins(SkPaint::Join join) override;
void setDrawStyle(SkPaint::Style style) override;
void setStrokeWidth(SkScalar width) override;
void setMiterLimit(SkScalar limit) override;
void setBlendMode(SkBlendMode mode) override;
void setBlender(sk_sp<SkBlender> blender) override;
void setImageFilter(sk_sp<SkImageFilter> filter) override;
void setColorFilter(sk_sp<SkColorFilter> filter) override;
void setPathEffect(sk_sp<SkPathEffect> effect) override;
void setMaskFilter(sk_sp<SkMaskFilter> filter) override;
void setMaskBlurFilter(SkBlurStyle style, SkScalar sigma) override;
void saveLayer(const SkRect* bounds, bool with_paint) override;
void save() override;
void saveLayer(const SkRect* bounds, bool with_paint) override;
void restore() override;
void drawPaint() override;
@@ -314,80 +343,264 @@ class DisplayListBoundsCalculator final
void drawShadow(const SkPath& path,
const SkColor color,
const SkScalar elevation,
bool occludes,
bool transparentOccluder,
SkScalar dpr) override;
SkRect getBounds() {
FML_DCHECK(accumulator_ == &root_accumulator_);
return root_accumulator_.getBounds();
// The DisplayList had an unbounded call with no cull rect or clip
// to contain it. Should only be called after the stream is fully
// dispatched.
// Unbounded operations are calls like |drawColor| which are defined
// to flood the entire surface, or calls that relied on a rendering
// attribute which is unable to compute bounds (should be rare).
// In those cases the bounds will represent only the accumulation
// of the bounded calls and this flag will be set to indicate that
// condition.
bool isUnbounded() const {
FML_DCHECK(layer_infos_.size() == 1);
return layer_infos_.front()->is_unbounded();
}
SkRect getBounds() const {
FML_DCHECK(layer_infos_.size() == 1);
if (isUnbounded()) {
FML_LOG(INFO) << "returning partial bounds for unbounded DisplayList";
}
return accumulator_->getBounds();
}
private:
// current accumulator based on saveLayer history
BoundsAccumulator* accumulator_;
// Only used for drawColor and drawPaint and paint objects that
// cannot support fast bounds.
SkRect bounds_cull_;
BoundsAccumulator root_accumulator_;
class SaveInfo {
// A class that abstracts the information kept for a single
// |save| or |saveLayer|, including the root information that
// is kept as a base set of information for the DisplayList
// at the initial conditions outside of any saveLayer.
// See |RootLayerData|, |SaveData| and |SaveLayerData|.
class LayerData {
public:
SaveInfo(BoundsAccumulator* accumulator);
virtual ~SaveInfo() = default;
// Construct a LayerData to push on the save stack for a |save|
// or |saveLayer| call.
// There does not tend to be an actual layer in the case of
// a |save| call, but in order to homogenize the handling
// of |restore| it adds a trivial implementation of this
// class to the stack of saves.
// The |outer| parameter is the |BoundsAccumulator| that was
// in use by the stream before this layer was pushed on the
// stack and should be returned when this layer is popped off
// the stack.
// Some layers may substitute their own accumulator to compute
// their own local bounds while they are on the stack.
LayerData(BoundsAccumulator* outer) : outer_(outer), is_unbounded_(false) {}
virtual ~LayerData() = default;
virtual BoundsAccumulator* save();
virtual BoundsAccumulator* restore();
// The accumulator to use while this layer is put in play by
// a |save| or |saveLayer|
virtual BoundsAccumulator* accumulatorForLayer() { return outer_; }
protected:
BoundsAccumulator* saved_accumulator_;
// The accumulator to use after this layer is removed from play
// via |restore|
virtual BoundsAccumulator* accumulatorForRestore() { return outer_; }
// The bounds of this layer. May be empty for cases like
// a non-layer |save| call which uses the |outer_| accumulator
// to accumulate draw calls inside of it
virtual SkRect getLayerBounds() = 0;
// is_unbounded should be set to true if we ever encounter an operation
// on a layer that either is unrestricted (|drawColor| or |drawPaint|)
// or cannot compute its bounds (some effects and filters) and there
// was no outstanding clip op at the time.
// When the layer is restored, the outer layer may then process this
// unbounded state by accumulating its own clip or transferring the
// unbounded state to its own outer layer.
// Typically the DisplayList will have been constructed with a cull
// rect which will act as a default clip for the outermost layer and
// the unbounded state of all sub layers will eventually be caught by
// that cull rect so that the overall unbounded state of the entire
// DisplayList will never be true.
//
// SkPicture treats these same conditions as a Nop (they accumulate
// the SkPicture cull rect, but if it was not specified then it is an
// empty Rect and so has no effect on the bounds).
// If the Calculator object accumulates this flag into the root layer,
// then at least we can make the caller aware of that exceptional
// condition via the |DisplayListBoundsCalculator::isUnbounded| call.
//
// Flutter is unlikely to ever run into this as the Dart mechanisms
// all supply a non-null cull rect for all Dart Picture objects,
// even if that cull rect is kGiantRect.
void set_unbounded() { is_unbounded_ = true; }
// |is_unbounded| should be called after |getLayerBounds| in case
// a problem was found during the computation of those bounds,
// the layer will have one last chance to flag an unbounded state.
bool is_unbounded() const { return is_unbounded_; }
private:
FML_DISALLOW_COPY_AND_ASSIGN(SaveInfo);
BoundsAccumulator* outer_;
bool is_unbounded_;
FML_DISALLOW_COPY_AND_ASSIGN(LayerData);
};
class SaveLayerInfo : public SaveInfo {
// An intermediate implementation class that handles keeping
// a local accumulator for the layer, used by both |RootLayerData|
// and |SaveLayerData|.
class AccumulatorLayerData : public LayerData {
public:
SaveLayerInfo(BoundsAccumulator* accumulator, const SkMatrix& matrix);
virtual ~SaveLayerInfo() = default;
BoundsAccumulator* accumulatorForLayer() override {
return &layer_accumulator_;
}
BoundsAccumulator* save() override;
BoundsAccumulator* restore() override;
SkRect getLayerBounds() override {
// Even though this layer might be unbounded, we still
// accumulate what bounds we have as the unbounded condition
// may be contained at a higher level and we at least want to
// account for the bounds that we do have.
return layer_accumulator_.getBounds();
}
protected:
using LayerData::LayerData;
~AccumulatorLayerData() = default;
private:
BoundsAccumulator layer_accumulator_;
const SkMatrix matrix_;
private:
FML_DISALLOW_COPY_AND_ASSIGN(SaveLayerInfo);
};
class SaveLayerWithPaintInfo : public SaveLayerInfo {
// Used as the initial default layer info for the Calculator.
class RootLayerData final : public AccumulatorLayerData {
public:
SaveLayerWithPaintInfo(DisplayListBoundsCalculator* calculator,
BoundsAccumulator* accumulator,
const SkMatrix& save_matrix,
const SkRect* bounds,
const SkPaint& save_paint);
virtual ~SaveLayerWithPaintInfo() = default;
BoundsAccumulator* restore() override;
protected:
DisplayListBoundsCalculator* calculator_;
std::optional<SkRect> bounds_;
SkPaint paint_;
RootLayerData() : AccumulatorLayerData(nullptr) {}
~RootLayerData() = default;
private:
static constexpr SkRect kMissingBounds = SkRect::MakeWH(-1, -1);
FML_DISALLOW_COPY_AND_ASSIGN(SaveLayerWithPaintInfo);
FML_DISALLOW_COPY_AND_ASSIGN(RootLayerData);
};
std::vector<std::unique_ptr<SaveInfo>> saved_infos_;
// Used for |save|
class SaveData final : public LayerData {
public:
using LayerData::LayerData;
~SaveData() = default;
void accumulateRect(const SkRect& rect, bool force_stroke = false);
SkRect getLayerBounds() override { return SkRect::MakeEmpty(); }
private:
FML_DISALLOW_COPY_AND_ASSIGN(SaveData);
};
// Used for |saveLayer|
class SaveLayerData final : public AccumulatorLayerData {
public:
SaveLayerData(BoundsAccumulator* outer,
sk_sp<SkImageFilter> filter,
bool paint_nops_on_transparency)
: AccumulatorLayerData(outer), layer_filter_(std::move(filter)) {
if (!paint_nops_on_transparency) {
set_unbounded();
}
}
~SaveLayerData() = default;
SkRect getLayerBounds() override {
SkRect bounds = AccumulatorLayerData::getLayerBounds();
if (!getFilteredBounds(bounds, layer_filter_.get())) {
set_unbounded();
}
return bounds;
}
private:
sk_sp<SkImageFilter> layer_filter_;
FML_DISALLOW_COPY_AND_ASSIGN(SaveLayerData);
};
std::vector<std::unique_ptr<LayerData>> layer_infos_;
// A drawing operation that is not geometric in nature (but which
// may still apply a MaskFilter - see |kApplyMaskFilter| below).
static constexpr int kIsNonGeometric = 0x00;
// A geometric operation that is defined as a fill operation
// regardless of what the current paint Style is set to.
// This flag will automatically assume |kApplyMaskFilter|.
static constexpr int kIsFilledGeometry = 0x01;
// A geometric operation that is defined as a stroke operation
// regardless of what the current paint Style is set to.
// This flag will automatically assume |kApplyMaskFilter|.
static constexpr int kIsStrokedGeometry = 0x02;
// A geometric operation that may be a stroke or fill operation
// depending on the current state of the paint Style attribute.
// This flag will automatically assume |kApplyMaskFilter|.
static constexpr int kIsDrawnGeometry = 0x04;
static constexpr int kIsAnyGeometryMask = //
kIsFilledGeometry | //
kIsStrokedGeometry | //
kIsDrawnGeometry;
// A geometric operation which has a path that might have
// end caps that are not rectilinear which means that square
// end caps might project further than half the stroke width
// from the geometry bounds.
// A rectilinear path such as |drawRect| will not have
// diagonal end caps. |drawLine| might have diagonal end
// caps depending on the angle of the line, and more likely
// |drawPath| will often have such end caps.
static constexpr int kGeometryMayHaveDiagonalEndCaps = 0x08;
// A geometric operation which has joined vertices that are
// not guaranteed to be smooth (angles of incoming and outgoing)
// segments at some joins may not have the same angle) or
// rectilinear (squares have right angles at the corners, but
// those corners will never extend past the bounding box of
// the geometry pre-transform).
// |drawRect|, |drawOval| and |drawRRect| all have well
// behaved joins, but |drawPath| might have joins that cause
// mitered extensions outside the pre-transformed bounding box.
static constexpr int kGeometryMayHaveProblematicJoins = 0x10;
// Some operations are inherently non-geometric and yet have the
// mask filter applied anyway.
// |drawImage| variants behave this way.
static constexpr int kApplyMaskFilter = 0x20;
// In very rare circumstances the ImageFilter is ignored.
// This is one of the few flags that turns off a step in
// estimating the bounds, rather than turning on any steps.
static constexpr int kIsUnfiltered = 0x40;
static constexpr SkScalar kMinStrokeWidth = 0.01;
skstd::optional<SkBlendMode> blend_mode_ = SkBlendMode::kSrcOver;
sk_sp<SkColorFilter> color_filter_;
SkScalar half_stroke_width_ = kMinStrokeWidth;
SkScalar miter_limit_ = 4.0;
int style_flag_ = kIsFilledGeometry;
bool join_is_miter_ = true;
bool cap_is_square_ = false;
sk_sp<SkImageFilter> image_filter_;
sk_sp<SkPathEffect> path_effect_;
sk_sp<SkMaskFilter> mask_filter_;
SkScalar mask_sigma_pad_ = 0.0;
bool paintNopsOnTransparenBlack();
static bool getFilteredBounds(SkRect& rect, SkImageFilter* filter);
bool adjustBoundsForPaint(SkRect& bounds, int flags);
void accumulateUnbounded();
void accumulateRect(const SkRect& rect, int flags) {
SkRect bounds = rect;
accumulateRect(bounds, flags);
}
void accumulateRect(SkRect& rect, int flags);
};
} // namespace flutter