From 165f0cffe5c8124797f858b5d2039d1ba19d960e Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Fri, 22 Apr 2022 15:54:03 -0700 Subject: [PATCH] add DlPaint object for optional use with DisplayListBuilder (flutter/engine#32860) --- .../ci/licenses_golden/licenses_flutter | 3 + engine/src/flutter/display_list/BUILD.gn | 3 + .../display_list/display_list_attributes.h | 4 +- .../display_list/display_list_blend_mode.h | 1 + .../display_list/display_list_builder.cc | 185 +++++++++++++++ .../display_list/display_list_builder.h | 57 +++++ .../display_list_enum_unittests.cc | 39 ++++ .../display_list/display_list_paint.cc | 37 +++ .../flutter/display_list/display_list_paint.h | 218 ++++++++++++++++++ .../display_list_paint_unittests.cc | 106 +++++++++ .../flow/layers/opacity_layer_unittests.cc | 10 +- 11 files changed, 655 insertions(+), 8 deletions(-) create mode 100644 engine/src/flutter/display_list/display_list_paint.cc create mode 100644 engine/src/flutter/display_list/display_list_paint.h create mode 100644 engine/src/flutter/display_list/display_list_paint_unittests.cc diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index 02d16d077b..f77e5485d2 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -88,6 +88,9 @@ FILE: ../../../flutter/display_list/display_list_mask_filter.h FILE: ../../../flutter/display_list/display_list_mask_filter_unittests.cc FILE: ../../../flutter/display_list/display_list_ops.cc FILE: ../../../flutter/display_list/display_list_ops.h +FILE: ../../../flutter/display_list/display_list_paint.cc +FILE: ../../../flutter/display_list/display_list_paint.h +FILE: ../../../flutter/display_list/display_list_paint_unittests.cc FILE: ../../../flutter/display_list/display_list_test_utils.cc FILE: ../../../flutter/display_list/display_list_test_utils.h FILE: ../../../flutter/display_list/display_list_tile_mode.h diff --git a/engine/src/flutter/display_list/BUILD.gn b/engine/src/flutter/display_list/BUILD.gn index 7f00626b93..678631c4dd 100644 --- a/engine/src/flutter/display_list/BUILD.gn +++ b/engine/src/flutter/display_list/BUILD.gn @@ -41,6 +41,8 @@ source_set("display_list") { "display_list_mask_filter.h", "display_list_ops.cc", "display_list_ops.h", + "display_list_paint.cc", + "display_list_paint.h", "display_list_tile_mode.h", "display_list_utils.cc", "display_list_utils.h", @@ -69,6 +71,7 @@ source_set("unittests") { "display_list_enum_unittests.cc", "display_list_image_filter_unittests.cc", "display_list_mask_filter_unittests.cc", + "display_list_paint_unittests.cc", "display_list_test_utils.cc", "display_list_test_utils.h", "display_list_unittests.cc", diff --git a/engine/src/flutter/display_list/display_list_attributes.h b/engine/src/flutter/display_list/display_list_attributes.h index 76b2891c44..051e4efd65 100644 --- a/engine/src/flutter/display_list/display_list_attributes.h +++ b/engine/src/flutter/display_list/display_list_attributes.h @@ -101,11 +101,11 @@ class DlAttribute { // Return an equivalent sk_sp version of this object. virtual sk_sp skia_object() const = 0; - // Perform a content aware |==| comparison of the ColorFilter. + // Perform a content aware |==| comparison of the Attribute. bool operator==(D const& other) const { return type() == other.type() && equals_(other); } - // Perform a content aware |!=| comparison of the ColorFilter. + // Perform a content aware |!=| comparison of the Attribute. bool operator!=(D const& other) const { return !(*this == other); } virtual ~DlAttribute() = default; diff --git a/engine/src/flutter/display_list/display_list_blend_mode.h b/engine/src/flutter/display_list/display_list_blend_mode.h index 32753daa09..6a84e08879 100644 --- a/engine/src/flutter/display_list/display_list_blend_mode.h +++ b/engine/src/flutter/display_list/display_list_blend_mode.h @@ -63,6 +63,7 @@ enum class DlBlendMode { kLastSeparableMode = kMultiply, //!< last blend mode operating separately on components kLastMode = kLuminosity, //!< last valid value + kDefaultMode = kSrcOver, }; inline DlBlendMode ToDl(SkBlendMode mode) { diff --git a/engine/src/flutter/display_list/display_list_builder.cc b/engine/src/flutter/display_list/display_list_builder.cc index d84f26778a..1d0574dd8c 100644 --- a/engine/src/flutter/display_list/display_list_builder.cc +++ b/engine/src/flutter/display_list/display_list_builder.cc @@ -291,6 +291,49 @@ void DisplayListBuilder::onSetMaskFilter(const DlMaskFilter* filter) { } } +void DisplayListBuilder::setAttributesFromDlPaint( + const DlPaint& paint, + const DisplayListAttributeFlags flags) { + if (flags.applies_anti_alias()) { + setAntiAlias(paint.isAntiAlias()); + } + if (flags.applies_dither()) { + setDither(paint.isDither()); + } + if (flags.applies_alpha_or_color()) { + setColor(paint.getColor().argb); + } + if (flags.applies_blend()) { + setBlendMode(paint.getBlendMode()); + } + if (flags.applies_style()) { + setStyle(ToSk(paint.getDrawStyle())); + } + if (flags.is_stroked(ToSk(paint.getDrawStyle()))) { + setStrokeWidth(paint.getStrokeWidth()); + setStrokeMiter(paint.getStrokeMiter()); + setStrokeCap(ToSk(paint.getStrokeCap())); + setStrokeJoin(ToSk(paint.getStrokeJoin())); + } + if (flags.applies_shader()) { + setColorSource(paint.getColorSource().get()); + } + if (flags.applies_color_filter()) { + setInvertColors(paint.isInvertColors()); + setColorFilter(paint.getColorFilter().get()); + } + if (flags.applies_image_filter()) { + setImageFilter(paint.getImageFilter().get()); + } + // Waiting for https://github.com/flutter/engine/pull/32159 + // if (flags.applies_path_effect()) { + // setPathEffect(sk_ref_sp(paint.getPathEffect())); + // } + if (flags.applies_mask_filter()) { + setMaskFilter(paint.getMaskFilter().get()); + } +} + void DisplayListBuilder::setAttributesFromPaint( const SkPaint& paint, const DisplayListAttributeFlags flags) { @@ -406,6 +449,15 @@ void DisplayListBuilder::saveLayer(const SkRect* bounds, } } } +void DisplayListBuilder::saveLayer(const SkRect* bounds, const DlPaint* paint) { + if (paint != nullptr) { + setAttributesFromDlPaint(*paint, + DisplayListOpFlags::kSaveLayerWithPaintFlags); + saveLayer(bounds, true); + } else { + saveLayer(bounds, false); + } +} void DisplayListBuilder::translate(SkScalar tx, SkScalar ty) { if (SkScalarIsFinite(tx) && SkScalarIsFinite(ty) && @@ -536,6 +588,10 @@ void DisplayListBuilder::drawPaint() { Push(0, 1); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawPaint(const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawPaintFlags); + drawPaint(); +} void DisplayListBuilder::drawColor(SkColor color, DlBlendMode mode) { Push(0, 1, color, mode); CheckLayerOpacityCompatibility(mode); @@ -544,18 +600,38 @@ void DisplayListBuilder::drawLine(const SkPoint& p0, const SkPoint& p1) { Push(0, 1, p0, p1); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawLine(const SkPoint& p0, + const SkPoint& p1, + const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawLineFlags); + drawLine(p0, p1); +} void DisplayListBuilder::drawRect(const SkRect& rect) { Push(0, 1, rect); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawRect(const SkRect& rect, const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawRectFlags); + drawRect(rect); +} void DisplayListBuilder::drawOval(const SkRect& bounds) { Push(0, 1, bounds); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawOval(const SkRect& bounds, const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawOvalFlags); + drawOval(bounds); +} void DisplayListBuilder::drawCircle(const SkPoint& center, SkScalar radius) { Push(0, 1, center, radius); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawCircle(const SkPoint& center, + SkScalar radius, + const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawCircleFlags); + drawCircle(center, radius); +} void DisplayListBuilder::drawRRect(const SkRRect& rrect) { if (rrect.isRect()) { drawRect(rrect.rect()); @@ -566,15 +642,29 @@ void DisplayListBuilder::drawRRect(const SkRRect& rrect) { CheckLayerOpacityCompatibility(); } } +void DisplayListBuilder::drawRRect(const SkRRect& rrect, const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawRRectFlags); + drawRRect(rrect); +} void DisplayListBuilder::drawDRRect(const SkRRect& outer, const SkRRect& inner) { Push(0, 1, outer, inner); CheckLayerOpacityCompatibility(); } +void DisplayListBuilder::drawDRRect(const SkRRect& outer, + const SkRRect& inner, + const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawDRRectFlags); + drawDRRect(outer, inner); +} void DisplayListBuilder::drawPath(const SkPath& path) { Push(0, 1, path); CheckLayerOpacityHairlineCompatibility(); } +void DisplayListBuilder::drawPath(const SkPath& path, const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawPathFlags); + drawPath(path); +} void DisplayListBuilder::drawArc(const SkRect& bounds, SkScalar start, @@ -587,6 +677,15 @@ void DisplayListBuilder::drawArc(const SkRect& bounds, CheckLayerOpacityCompatibility(); } } +void DisplayListBuilder::drawArc(const SkRect& bounds, + SkScalar start, + SkScalar sweep, + bool useCenter, + const DlPaint& paint) { + setAttributesFromDlPaint( + paint, useCenter ? kDrawArcWithCenterFlags : kDrawArcNoCenterFlags); + drawArc(bounds, start, sweep, useCenter); +} void DisplayListBuilder::drawPoints(SkCanvas::PointMode mode, uint32_t count, const SkPoint pts[]) { @@ -615,6 +714,28 @@ void DisplayListBuilder::drawPoints(SkCanvas::PointMode mode, // See: https://fiddle.skia.org/c/228459001d2de8db117ce25ef5cedb0c UpdateLayerOpacityCompatibility(false); } +void DisplayListBuilder::drawPoints(SkCanvas::PointMode mode, + uint32_t count, + const SkPoint pts[], + const DlPaint& paint) { + const DisplayListAttributeFlags* flags; + switch (mode) { + case SkCanvas::PointMode::kPoints_PointMode: + flags = &DisplayListOpFlags::kDrawPointsAsPointsFlags; + break; + case SkCanvas::PointMode::kLines_PointMode: + flags = &DisplayListOpFlags::kDrawPointsAsLinesFlags; + break; + case SkCanvas::PointMode::kPolygon_PointMode: + flags = &DisplayListOpFlags::kDrawPointsAsPolygonFlags; + break; + default: + FML_DCHECK(false); + return; + } + setAttributesFromDlPaint(paint, *flags); + drawPoints(mode, count, pts); +} void DisplayListBuilder::drawSkVertices(const sk_sp vertices, SkBlendMode mode) { Push(0, 1, std::move(vertices), mode); @@ -634,6 +755,12 @@ void DisplayListBuilder::drawVertices(const DlVertices* vertices, // cases. UpdateLayerOpacityCompatibility(false); } +void DisplayListBuilder::drawVertices(const DlVertices* vertices, + DlBlendMode mode, + const DlPaint& paint) { + setAttributesFromDlPaint(paint, DisplayListOpFlags::kDrawVerticesFlags); + drawVertices(vertices, mode); +} void DisplayListBuilder::drawImage(const sk_sp image, const SkPoint point, @@ -644,6 +771,18 @@ void DisplayListBuilder::drawImage(const sk_sp image, : Push(0, 1, std::move(image), point, sampling); CheckLayerOpacityCompatibility(render_with_attributes); } +void DisplayListBuilder::drawImage(const sk_sp image, + const SkPoint point, + const SkSamplingOptions& sampling, + const DlPaint* paint) { + if (paint != nullptr) { + setAttributesFromDlPaint(*paint, + DisplayListOpFlags::kDrawImageWithPaintFlags); + drawImage(image, point, sampling, true); + } else { + drawImage(image, point, sampling, false); + } +} void DisplayListBuilder::drawImageRect(const sk_sp image, const SkRect& src, const SkRect& dst, @@ -654,6 +793,20 @@ void DisplayListBuilder::drawImageRect(const sk_sp image, render_with_attributes, constraint); CheckLayerOpacityCompatibility(render_with_attributes); } +void DisplayListBuilder::drawImageRect(const sk_sp image, + const SkRect& src, + const SkRect& dst, + const SkSamplingOptions& sampling, + const DlPaint* paint, + SkCanvas::SrcRectConstraint constraint) { + if (paint != nullptr) { + setAttributesFromDlPaint(*paint, + DisplayListOpFlags::kDrawImageRectWithPaintFlags); + drawImageRect(image, src, dst, sampling, true, constraint); + } else { + drawImageRect(image, src, dst, sampling, false, constraint); + } +} void DisplayListBuilder::drawImageNine(const sk_sp image, const SkIRect& center, const SkRect& dst, @@ -665,6 +818,19 @@ void DisplayListBuilder::drawImageNine(const sk_sp image, : Push(0, 1, std::move(image), center, dst, filter); CheckLayerOpacityCompatibility(render_with_attributes); } +void DisplayListBuilder::drawImageNine(const sk_sp image, + const SkIRect& center, + const SkRect& dst, + SkFilterMode filter, + const DlPaint* paint) { + if (paint != nullptr) { + setAttributesFromDlPaint(*paint, + DisplayListOpFlags::kDrawImageNineWithPaintFlags); + drawImageNine(image, center, dst, filter, true); + } else { + drawImageNine(image, center, dst, filter, false); + } +} void DisplayListBuilder::drawImageLattice(const sk_sp image, const SkCanvas::Lattice& lattice, const SkRect& dst, @@ -725,6 +891,25 @@ void DisplayListBuilder::drawAtlas(const sk_sp atlas, // of the transforms and texture rectangles. UpdateLayerOpacityCompatibility(false); } +void DisplayListBuilder::drawAtlas(const sk_sp atlas, + const SkRSXform xform[], + const SkRect tex[], + const SkColor colors[], + int count, + DlBlendMode mode, + const SkSamplingOptions& sampling, + const SkRect* cull_rect, + const DlPaint* paint) { + if (paint != nullptr) { + setAttributesFromDlPaint(*paint, + DisplayListOpFlags::kDrawAtlasWithPaintFlags); + drawAtlas(atlas, xform, tex, colors, count, mode, sampling, cull_rect, + true); + } else { + drawAtlas(atlas, xform, tex, colors, count, mode, sampling, cull_rect, + false); + } +} void DisplayListBuilder::drawPicture(const sk_sp picture, const SkMatrix* matrix, diff --git a/engine/src/flutter/display_list/display_list_builder.h b/engine/src/flutter/display_list/display_list_builder.h index e1437e9913..80ac26ee72 100644 --- a/engine/src/flutter/display_list/display_list_builder.h +++ b/engine/src/flutter/display_list/display_list_builder.h @@ -11,6 +11,7 @@ #include "flutter/display_list/display_list_dispatcher.h" #include "flutter/display_list/display_list_flags.h" #include "flutter/display_list/display_list_image.h" +#include "flutter/display_list/display_list_paint.h" #include "flutter/display_list/types.h" #include "flutter/fml/macros.h" @@ -158,6 +159,7 @@ class DisplayListBuilder final : public virtual Dispatcher, ? SaveLayerOptions::kWithAttributes : SaveLayerOptions::kNoAttributes); } + void saveLayer(const SkRect* bounds, const DlPaint* paint); void restore() override; int getSaveCount() { return layer_stack_.size(); } @@ -192,21 +194,40 @@ class DisplayListBuilder final : public virtual Dispatcher, void clipPath(const SkPath& path, SkClipOp clip_op, bool is_aa) override; void drawPaint() override; + void drawPaint(const DlPaint& paint); void drawColor(SkColor color, DlBlendMode mode) override; void drawLine(const SkPoint& p0, const SkPoint& p1) override; + void drawLine(const SkPoint& p0, const SkPoint& p1, const DlPaint& paint); void drawRect(const SkRect& rect) override; + void drawRect(const SkRect& rect, const DlPaint& paint); void drawOval(const SkRect& bounds) override; + void drawOval(const SkRect& bounds, const DlPaint& paint); void drawCircle(const SkPoint& center, SkScalar radius) override; + void drawCircle(const SkPoint& center, SkScalar radius, const DlPaint& paint); void drawRRect(const SkRRect& rrect) override; + void drawRRect(const SkRRect& rrect, const DlPaint& paint); void drawDRRect(const SkRRect& outer, const SkRRect& inner) override; + void drawDRRect(const SkRRect& outer, + const SkRRect& inner, + const DlPaint& paint); void drawPath(const SkPath& path) override; + void drawPath(const SkPath& path, const DlPaint& paint); void drawArc(const SkRect& bounds, SkScalar start, SkScalar sweep, bool useCenter) override; + void drawArc(const SkRect& bounds, + SkScalar start, + SkScalar sweep, + bool useCenter, + const DlPaint& paint); void drawPoints(SkCanvas::PointMode mode, uint32_t count, const SkPoint pts[]) override; + void drawPoints(SkCanvas::PointMode mode, + uint32_t count, + const SkPoint pts[], + const DlPaint& paint); void drawSkVertices(const sk_sp vertices, SkBlendMode mode) override; void drawVertices(const DlVertices* vertices, DlBlendMode mode) override; @@ -214,10 +235,22 @@ class DisplayListBuilder final : public virtual Dispatcher, DlBlendMode mode) { drawVertices(vertices.get(), mode); } + void drawVertices(const DlVertices* vertices, + DlBlendMode mode, + const DlPaint& paint); + void drawVertices(const std::shared_ptr vertices, + DlBlendMode mode, + const DlPaint& paint) { + drawVertices(vertices.get(), mode, paint); + } void drawImage(const sk_sp image, const SkPoint point, const SkSamplingOptions& sampling, bool render_with_attributes) override; + void drawImage(const sk_sp image, + const SkPoint point, + const SkSamplingOptions& sampling, + const DlPaint* paint = nullptr); void drawImageRect( const sk_sp image, const SkRect& src, @@ -226,11 +259,23 @@ class DisplayListBuilder final : public virtual Dispatcher, bool render_with_attributes, SkCanvas::SrcRectConstraint constraint = SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint) override; + void drawImageRect(const sk_sp image, + const SkRect& src, + const SkRect& dst, + const SkSamplingOptions& sampling, + const DlPaint* paint = nullptr, + SkCanvas::SrcRectConstraint constraint = + SkCanvas::SrcRectConstraint::kFast_SrcRectConstraint); void drawImageNine(const sk_sp image, const SkIRect& center, const SkRect& dst, SkFilterMode filter, bool render_with_attributes) override; + void drawImageNine(const sk_sp image, + const SkIRect& center, + const SkRect& dst, + SkFilterMode filter, + const DlPaint* paint = nullptr); void drawImageLattice(const sk_sp image, const SkCanvas::Lattice& lattice, const SkRect& dst, @@ -245,6 +290,15 @@ class DisplayListBuilder final : public virtual Dispatcher, const SkSamplingOptions& sampling, const SkRect* cullRect, bool render_with_attributes) override; + void drawAtlas(const sk_sp atlas, + const SkRSXform xform[], + const SkRect tex[], + const SkColor colors[], + int count, + DlBlendMode mode, + const SkSamplingOptions& sampling, + const SkRect* cullRect, + const DlPaint* paint = nullptr); void drawPicture(const sk_sp picture, const SkMatrix* matrix, bool render_with_attributes) override; @@ -277,6 +331,9 @@ class DisplayListBuilder final : public virtual Dispatcher, template void* Push(size_t extra, int op_inc, Args&&... args); + void setAttributesFromDlPaint(const DlPaint& paint, + const DisplayListAttributeFlags flags); + // kInvalidSigma is used to indicate that no MaskBlur is currently set. static constexpr SkScalar kInvalidSigma = 0.0; static bool mask_sigma_valid(SkScalar sigma) { diff --git a/engine/src/flutter/display_list/display_list_enum_unittests.cc b/engine/src/flutter/display_list/display_list_enum_unittests.cc index dc319e5a9c..6a664fe6d3 100644 --- a/engine/src/flutter/display_list/display_list_enum_unittests.cc +++ b/engine/src/flutter/display_list/display_list_enum_unittests.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include "flutter/display_list/display_list_blend_mode.h" +#include "flutter/display_list/display_list_paint.h" #include "flutter/display_list/display_list_tile_mode.h" #include "flutter/display_list/display_list_vertices.h" #include "flutter/display_list/types.h" @@ -25,6 +26,44 @@ TEST(DisplayListEnum, ToSkTileMode) { ASSERT_EQ(ToSk(DlTileMode::kDecal), SkTileMode::kDecal); } +TEST(DisplayListEnum, ToDlDrawStyle) { + ASSERT_EQ(ToDl(SkPaint::Style::kFill_Style), DlDrawStyle::kFill); + ASSERT_EQ(ToDl(SkPaint::Style::kStroke_Style), DlDrawStyle::kStroke); + ASSERT_EQ(ToDl(SkPaint::Style::kStrokeAndFill_Style), + DlDrawStyle::kStrokeAndFill); +} + +TEST(DisplayListEnum, ToSkDrawStyle) { + ASSERT_EQ(ToSk(DlDrawStyle::kFill), SkPaint::Style::kFill_Style); + ASSERT_EQ(ToSk(DlDrawStyle::kStroke), SkPaint::Style::kStroke_Style); + ASSERT_EQ(ToSk(DlDrawStyle::kStrokeAndFill), + SkPaint::Style::kStrokeAndFill_Style); +} + +TEST(DisplayListEnum, ToDlStrokeCap) { + ASSERT_EQ(ToDl(SkPaint::Cap::kButt_Cap), DlStrokeCap::kButt); + ASSERT_EQ(ToDl(SkPaint::Cap::kRound_Cap), DlStrokeCap::kRound); + ASSERT_EQ(ToDl(SkPaint::Cap::kSquare_Cap), DlStrokeCap::kSquare); +} + +TEST(DisplayListEnum, ToSkStrokeCap) { + ASSERT_EQ(ToSk(DlStrokeCap::kButt), SkPaint::Cap::kButt_Cap); + ASSERT_EQ(ToSk(DlStrokeCap::kRound), SkPaint::Cap::kRound_Cap); + ASSERT_EQ(ToSk(DlStrokeCap::kSquare), SkPaint::Cap::kSquare_Cap); +} + +TEST(DisplayListEnum, ToDlStrokeJoin) { + ASSERT_EQ(ToDl(SkPaint::Join::kMiter_Join), DlStrokeJoin::kMiter); + ASSERT_EQ(ToDl(SkPaint::Join::kRound_Join), DlStrokeJoin::kRound); + ASSERT_EQ(ToDl(SkPaint::Join::kBevel_Join), DlStrokeJoin::kBevel); +} + +TEST(DisplayListEnum, ToSkStrokeJoin) { + ASSERT_EQ(ToSk(DlStrokeJoin::kMiter), SkPaint::Join::kMiter_Join); + ASSERT_EQ(ToSk(DlStrokeJoin::kRound), SkPaint::Join::kRound_Join); + ASSERT_EQ(ToSk(DlStrokeJoin::kBevel), SkPaint::Join::kBevel_Join); +} + TEST(DisplayListEnum, ToDlVertexMode) { ASSERT_EQ(ToDl(SkVertices::VertexMode::kTriangles_VertexMode), DlVertexMode::kTriangles); diff --git a/engine/src/flutter/display_list/display_list_paint.cc b/engine/src/flutter/display_list/display_list_paint.cc new file mode 100644 index 0000000000..8e532e0bbd --- /dev/null +++ b/engine/src/flutter/display_list/display_list_paint.cc @@ -0,0 +1,37 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/display_list/display_list_paint.h" + +namespace flutter { + +DlPaint::DlPaint() + : blendMode_(static_cast(DlBlendMode::kDefaultMode)), + drawStyle_(static_cast(DlDrawStyle::kDefaultStyle)), + strokeCap_(static_cast(DlStrokeCap::kDefaultCap)), + strokeJoin_(static_cast(DlStrokeJoin::kDefaultJoin)), + isAntiAlias_(false), + isDither_(false), + isInvertColors_(false), + strokeWidth_(0.0), + strokeMiter_(4.0) {} + +bool DlPaint::operator==(DlPaint const& other) const { + return blendMode_ == other.blendMode_ && // + drawStyle_ == other.drawStyle_ && // + strokeCap_ == other.strokeCap_ && // + strokeJoin_ == other.strokeJoin_ && // + isAntiAlias_ == other.isAntiAlias_ && // + isDither_ == other.isDither_ && // + isInvertColors_ == other.isInvertColors_ && // + color_ == other.color_ && // + strokeWidth_ == other.strokeWidth_ && // + strokeMiter_ == other.strokeMiter_ && // + Equals(colorSource_, other.colorSource_) && // + Equals(colorFilter_, other.colorFilter_) && // + Equals(imageFilter_, other.imageFilter_) && // + Equals(maskFilter_, other.maskFilter_); +} + +} // namespace flutter diff --git a/engine/src/flutter/display_list/display_list_paint.h b/engine/src/flutter/display_list/display_list_paint.h new file mode 100644 index 0000000000..c23d94a198 --- /dev/null +++ b/engine/src/flutter/display_list/display_list_paint.h @@ -0,0 +1,218 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PAINT_H_ +#define FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PAINT_H_ + +#include "flutter/display_list/display_list_blend_mode.h" +#include "flutter/display_list/display_list_color_filter.h" +#include "flutter/display_list/display_list_color_source.h" +#include "flutter/display_list/display_list_image_filter.h" +#include "flutter/display_list/display_list_mask_filter.h" + +namespace flutter { + +class DlColor { + public: + DlColor() : argb(0xFF000000) {} + DlColor(uint32_t argb) : argb(argb) {} + + uint32_t argb; + + bool operator==(DlColor const& other) const { return argb == other.argb; } + bool operator!=(DlColor const& other) const { return argb != other.argb; } +}; + +enum class DlDrawStyle { + kFill, //!< fills interior of shapes + kStroke, //!< strokes boundary of shapes + kStrokeAndFill, //!< both strokes and fills shapes + + kLastStyle = kStrokeAndFill, + kDefaultStyle = kFill, +}; + +inline DlDrawStyle ToDl(SkPaint::Style style) { + return static_cast(style); +} + +inline SkPaint::Style ToSk(DlDrawStyle style) { + return static_cast(style); +} + +enum class DlStrokeCap { + kButt, //!< no stroke extension + kRound, //!< adds circle + kSquare, //!< adds square + + kLastCap = kSquare, + kDefaultCap = kButt, +}; + +inline DlStrokeCap ToDl(SkPaint::Cap cap) { + return static_cast(cap); +} + +inline SkPaint::Cap ToSk(DlStrokeCap cap) { + return static_cast(cap); +} + +enum class DlStrokeJoin { + kMiter, //!< extends to miter limit + kRound, //!< adds circle + kBevel, //!< connects outside edges + + kLastJoin = kBevel, + kDefaultJoin = kMiter, +}; + +inline DlStrokeJoin ToDl(SkPaint::Join join) { + return static_cast(join); +} + +inline SkPaint::Join ToSk(DlStrokeJoin join) { + return static_cast(join); +} + +class DlPaint { + public: + DlPaint(); + + bool isAntiAlias() const { return isAntiAlias_; } + DlPaint& setAntiAlias(bool isAntiAlias) { + isAntiAlias_ = isAntiAlias; + return *this; + } + + bool isDither() const { return isDither_; } + DlPaint& setDither(bool isDither) { + isDither_ = isDither; + return *this; + } + + bool isInvertColors() const { return isInvertColors_; } + DlPaint& setInvertColors(bool isInvertColors) { + isInvertColors_ = isInvertColors; + return *this; + } + + DlColor getColor() const { return color_; } + DlPaint& setColor(DlColor color) { + color_.argb = color.argb; + return *this; + } + + DlBlendMode getBlendMode() const { + return static_cast(blendMode_); + } + DlPaint& setBlendMode(DlBlendMode mode) { + blendMode_ = static_cast(mode); + return *this; + } + + DlDrawStyle getDrawStyle() const { + return static_cast(drawStyle_); + } + DlPaint& setDrawStyle(DlDrawStyle style) { + drawStyle_ = static_cast(style); + return *this; + } + + DlStrokeCap getStrokeCap() const { + return static_cast(strokeCap_); + } + DlPaint& setStrokeCap(DlStrokeCap cap) { + strokeCap_ = static_cast(cap); + return *this; + } + + DlStrokeJoin getStrokeJoin() const { + return static_cast(strokeJoin_); + } + DlPaint& setStrokeJoin(DlStrokeJoin join) { + strokeJoin_ = static_cast(join); + return *this; + } + + float getStrokeWidth() const { return strokeWidth_; } + DlPaint& setStrokeWidth(float width) { + strokeWidth_ = width; + return *this; + } + + float getStrokeMiter() const { return strokeMiter_; } + DlPaint& setStrokeMiter(float miter) { + strokeMiter_ = miter; + return *this; + } + + std::shared_ptr getColorSource() const { return colorSource_; } + DlPaint& setColorSource(std::shared_ptr source) { + colorSource_ = source; + return *this; + } + + std::shared_ptr getColorFilter() const { return colorFilter_; } + DlPaint& setColorFilter(std::shared_ptr filter) { + colorFilter_ = filter; + return *this; + } + + std::shared_ptr getImageFilter() const { return imageFilter_; } + DlPaint& setImageFilter(std::shared_ptr filter) { + imageFilter_ = filter; + return *this; + } + + std::shared_ptr getMaskFilter() const { return maskFilter_; } + DlPaint& setMaskFilter(std::shared_ptr filter) { + maskFilter_ = filter; + return *this; + } + + bool operator==(DlPaint const& other) const; + bool operator!=(DlPaint const& other) const { return !(*this == other); } + + private: +#define ASSERT_ENUM_FITS(last_enum, num_bits) \ + static_assert(static_cast(last_enum) < (1 << num_bits) && \ + static_cast(last_enum) * 2 >= (1 << num_bits)) + + static constexpr int kBlendModeBits = 5; + static constexpr int kDrawStyleBits = 2; + static constexpr int kStrokeCapBits = 2; + static constexpr int kStrokeJoinBits = 2; + ASSERT_ENUM_FITS(DlBlendMode::kLastMode, kBlendModeBits); + ASSERT_ENUM_FITS(DlDrawStyle::kLastStyle, kDrawStyleBits); + ASSERT_ENUM_FITS(DlStrokeCap::kLastCap, kStrokeCapBits); + ASSERT_ENUM_FITS(DlStrokeJoin::kLastJoin, kStrokeJoinBits); + + union { + struct { + unsigned blendMode_ : kBlendModeBits; + unsigned drawStyle_ : kDrawStyleBits; + unsigned strokeCap_ : kStrokeCapBits; + unsigned strokeJoin_ : kStrokeJoinBits; + unsigned isAntiAlias_ : 1; + unsigned isDither_ : 1; + unsigned isInvertColors_ : 1; + }; + }; + + DlColor color_; + float strokeWidth_; + float strokeMiter_; + + std::shared_ptr colorSource_; + std::shared_ptr colorFilter_; + std::shared_ptr imageFilter_; + std::shared_ptr maskFilter_; + // missing (as compared to SkPaint): + // DlPathEffect - waiting for https://github.com/flutter/engine/pull/32159 + // DlBlender - not planning on using that object in a pure DisplayList world +}; + +} // namespace flutter + +#endif // FLUTTER_DISPLAY_LIST_DISPLAY_LIST_PAINT_H_ diff --git a/engine/src/flutter/display_list/display_list_paint_unittests.cc b/engine/src/flutter/display_list/display_list_paint_unittests.cc new file mode 100644 index 0000000000..fcea32e455 --- /dev/null +++ b/engine/src/flutter/display_list/display_list_paint_unittests.cc @@ -0,0 +1,106 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/display_list/display_list_paint.h" + +#include "flutter/display_list/display_list_comparable.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(DisplayListPaint, ConstructorDefaults) { + DlPaint paint; + EXPECT_FALSE(paint.isAntiAlias()); + EXPECT_FALSE(paint.isDither()); + EXPECT_FALSE(paint.isInvertColors()); + EXPECT_EQ(paint.getColor(), DlColor{0xFF000000}); + EXPECT_EQ(paint.getBlendMode(), DlBlendMode::kDefaultMode); + EXPECT_EQ(paint.getDrawStyle(), DlDrawStyle::kDefaultStyle); + EXPECT_EQ(paint.getStrokeCap(), DlStrokeCap::kDefaultCap); + EXPECT_EQ(paint.getStrokeJoin(), DlStrokeJoin::kDefaultJoin); + EXPECT_EQ(paint.getStrokeWidth(), 0.0); + EXPECT_EQ(paint.getStrokeMiter(), 4.0); + EXPECT_EQ(paint.getColorSource(), nullptr); + EXPECT_EQ(paint.getColorFilter(), nullptr); + EXPECT_EQ(paint.getImageFilter(), nullptr); + EXPECT_EQ(paint.getMaskFilter(), nullptr); + + EXPECT_EQ(DlBlendMode::kDefaultMode, DlBlendMode::kSrcOver); + EXPECT_EQ(DlDrawStyle::kDefaultStyle, DlDrawStyle::kFill); + EXPECT_EQ(DlStrokeCap::kDefaultCap, DlStrokeCap::kButt); + EXPECT_EQ(DlStrokeJoin::kDefaultJoin, DlStrokeJoin::kMiter); + + EXPECT_EQ(paint, DlPaint()); + + EXPECT_NE(paint, DlPaint().setAntiAlias(true)); + EXPECT_NE(paint, DlPaint().setDither(true)); + EXPECT_NE(paint, DlPaint().setInvertColors(true)); + EXPECT_NE(paint, DlPaint().setColor(DlColor(0xFF00FF00))); + EXPECT_NE(paint, DlPaint().setBlendMode(DlBlendMode::kDstIn)); + EXPECT_NE(paint, DlPaint().setDrawStyle(DlDrawStyle::kStrokeAndFill)); + EXPECT_NE(paint, DlPaint().setStrokeCap(DlStrokeCap::kRound)); + EXPECT_NE(paint, DlPaint().setStrokeJoin(DlStrokeJoin::kRound)); + EXPECT_NE(paint, DlPaint().setStrokeWidth(6)); + EXPECT_NE(paint, DlPaint().setStrokeMiter(7)); + + DlColorColorSource colorSource(0xFFFF00FF); + EXPECT_NE(paint, DlPaint().setColorSource(colorSource.shared())); + + DlBlendColorFilter colorFilter(0xFFFFFF00, SkBlendMode::kDstIn); + EXPECT_NE(paint, DlPaint().setColorFilter(colorFilter.shared())); + + DlBlurImageFilter imageFilter(1.3, 4.7, DlTileMode::kClamp); + EXPECT_NE(paint, DlPaint().setImageFilter(imageFilter.shared())); + + DlBlurMaskFilter maskFilter(SkBlurStyle::kInner_SkBlurStyle, 3.14); + EXPECT_NE(paint, DlPaint().setMaskFilter(maskFilter.shared())); +} + +TEST(DisplayListPaint, ChainingConstructor) { + DlPaint paint = + DlPaint() // + .setAntiAlias(true) // + .setDither(true) // + .setInvertColors(true) // + .setColor({0xFF00FF00}) // + .setBlendMode(DlBlendMode::kLuminosity) // + .setDrawStyle(DlDrawStyle::kStrokeAndFill) // + .setStrokeCap(DlStrokeCap::kSquare) // + .setStrokeJoin(DlStrokeJoin::kBevel) // + .setStrokeWidth(42) // + .setStrokeMiter(1.5) // + .setColorSource(DlColorColorSource(0xFFFF00FF).shared()) // + .setColorFilter( + DlBlendColorFilter(0xFFFFFF00, SkBlendMode::kDstIn).shared()) + .setImageFilter( + DlBlurImageFilter(1.3, 4.7, DlTileMode::kClamp).shared()) + .setMaskFilter( + DlBlurMaskFilter(SkBlurStyle::kInner_SkBlurStyle, 3.14).shared()); + EXPECT_TRUE(paint.isAntiAlias()); + EXPECT_TRUE(paint.isDither()); + EXPECT_TRUE(paint.isInvertColors()); + EXPECT_EQ(paint.getColor(), DlColor{0xFF00FF00}); + EXPECT_EQ(paint.getBlendMode(), DlBlendMode::kLuminosity); + EXPECT_EQ(paint.getDrawStyle(), DlDrawStyle::kStrokeAndFill); + EXPECT_EQ(paint.getStrokeCap(), DlStrokeCap::kSquare); + EXPECT_EQ(paint.getStrokeJoin(), DlStrokeJoin::kBevel); + EXPECT_EQ(paint.getStrokeWidth(), 42); + EXPECT_EQ(paint.getStrokeMiter(), 1.5); + EXPECT_TRUE( + Equals(paint.getColorSource(), DlColorColorSource(0xFFFF00FF).shared())); + EXPECT_TRUE( + Equals(paint.getColorFilter(), + DlBlendColorFilter(0xFFFFFF00, SkBlendMode::kDstIn).shared())); + EXPECT_TRUE(Equals(paint.getImageFilter(), + DlBlurImageFilter(1.3, 4.7, DlTileMode::kClamp).shared())); + EXPECT_TRUE( + Equals(paint.getMaskFilter(), + DlBlurMaskFilter(SkBlurStyle::kInner_SkBlurStyle, 3.14).shared())); + + EXPECT_NE(paint, DlPaint()); +} + +} // namespace testing +} // namespace flutter diff --git a/engine/src/flutter/flow/layers/opacity_layer_unittests.cc b/engine/src/flutter/flow/layers/opacity_layer_unittests.cc index 2581dbff58..320f60c5b0 100644 --- a/engine/src/flutter/flow/layers/opacity_layer_unittests.cc +++ b/engine/src/flutter/flow/layers/opacity_layer_unittests.cc @@ -298,11 +298,11 @@ TEST_F(OpacityLayerTest, HalfTransparent) { EXPECT_EQ(mock_layer->parent_mutators(), std::vector({Mutator(layer_transform), Mutator(alpha_half)})); - SkPaint opacity_paint; - opacity_paint.setAlphaf(alpha_half * (1.0 / SK_AlphaOPAQUE)); SkRect opacity_bounds; expected_layer_bounds.makeOffset(-layer_offset.fX, -layer_offset.fY) .roundOut(&opacity_bounds); + DlPaint save_paint = DlPaint().setColor(DlColor(alpha_half << 24)); + DlPaint child_dl_paint = DlPaint().setColor(DlColor(SK_ColorGREEN)); auto expected_builder = DisplayListBuilder(); expected_builder.save(); @@ -311,10 +311,8 @@ TEST_F(OpacityLayerTest, HalfTransparent) { expected_builder.transformReset(); expected_builder.transform(SkM44(integral_layer_transform)); #endif - expected_builder.setColor(alpha_half << 24); - expected_builder.saveLayer(&opacity_bounds, true); - expected_builder.setColor(SkColors::kGreen.toSkColor()); - expected_builder.drawPath(child_path); + expected_builder.saveLayer(&opacity_bounds, &save_paint); + expected_builder.drawPath(child_path, child_dl_paint); expected_builder.restore(); expected_builder.restore(); sk_sp expected_display_list = expected_builder.Build();