Skip to content

Commit

Permalink
move de-Skia-ifying code to a DlColorFilter static factory, + formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
flar committed Feb 16, 2022
1 parent 41f7b15 commit 99d159a
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 61 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ FILE: ../../../flutter/display_list/display_list_canvas_dispatcher.h
FILE: ../../../flutter/display_list/display_list_canvas_recorder.cc
FILE: ../../../flutter/display_list/display_list_canvas_recorder.h
FILE: ../../../flutter/display_list/display_list_canvas_unittests.cc
FILE: ../../../flutter/display_list/display_list_color_filter.cc
FILE: ../../../flutter/display_list/display_list_color_filter.h
FILE: ../../../flutter/display_list/display_list_complexity.cc
FILE: ../../../flutter/display_list/display_list_complexity.h
FILE: ../../../flutter/display_list/display_list_dispatcher.cc
Expand Down
2 changes: 2 additions & 0 deletions display_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ source_set("display_list") {
"display_list_canvas_dispatcher.h",
"display_list_canvas_recorder.cc",
"display_list_canvas_recorder.h",
"display_list_color_filter.cc",
"display_list_color_filter.h",
"display_list_complexity.cc",
"display_list_complexity.h",
"display_list_dispatcher.cc",
Expand Down
30 changes: 2 additions & 28 deletions display_list/display_list_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,33 +135,6 @@ void DisplayListBuilder::onSetImageFilter(sk_sp<SkImageFilter> filter) {
? Push<SetImageFilterOp>(0, 0, std::move(filter))
: Push<ClearImageFilterOp>(0, 0);
}
void DisplayListBuilder::setColorFilter(sk_sp<SkColorFilter> filter) {
if (filter == nullptr) {
if (current_color_filter_->type() != DlColorFilter::kNone) {
onSetColorFilter(&DlNoColorFilter::instance);
}
return;
}
{
SkColor color;
SkBlendMode mode;
if (filter->asAColorMode(&color, &mode)) {
DlBlendColorFilter blend_filter(color, mode);
onSetColorFilter(&blend_filter);
return;
}
}
{
float matrix[20];
if (filter->asAColorMatrix(matrix)) {
DlMatrixColorFilter matrix_filter(matrix);
onSetColorFilter(&matrix_filter);
return;
}
}
DlUnknownColorFilter unknown_filter(std::move(filter));
onSetColorFilter(&unknown_filter);
}
void DisplayListBuilder::onSetColorFilter(const DlColorFilter* filter) {
if (filter == nullptr) {
filter = &DlNoColorFilter::instance;
Expand Down Expand Up @@ -278,7 +251,8 @@ void DisplayListBuilder::setAttributesFromPaint(
// we must clear it because it is a second potential color filter
// that is composed with the paint's color filter.
setInvertColors(false);
setColorFilter(sk_ref_sp(paint.getColorFilter()));
DlColorFilter filter = DlColorFilter::From(paint.getColorFilter());
setColorFilter(&filter);
}
if (flags.applies_image_filter()) {
setImageFilter(sk_ref_sp(paint.getImageFilter()));
Expand Down
1 change: 0 additions & 1 deletion display_list/display_list_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,6 @@ class DisplayListBuilder final : public virtual Dispatcher,
void onSetBlender(sk_sp<SkBlender> blender);
void onSetShader(sk_sp<SkShader> shader);
void onSetImageFilter(sk_sp<SkImageFilter> filter);
void setColorFilter(sk_sp<SkColorFilter> filter);
void onSetColorFilter(const DlColorFilter* filter);
void onSetPathEffect(sk_sp<SkPathEffect> effect);
void onSetMaskFilter(sk_sp<SkMaskFilter> filter);
Expand Down
53 changes: 39 additions & 14 deletions display_list/display_list_color_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,26 @@

namespace flutter {

DlColorFilter DlColorFilter::From(SkColorFilter* sk_filter) {
if (sk_filter == nullptr) {
return DlNoColorFilter::instance;
}
{
SkColor color;
SkBlendMode mode;
if (sk_filter->asAColorMode(&color, &mode)) {
return DlBlendColorFilter(color, mode);
}
}
{
float matrix[20];
if (sk_filter->asAColorMatrix(matrix)) {
return DlMatrixColorFilter(matrix);
}
}
return DlUnknownColorFilter(sk_ref_sp(sk_filter));
}

DlColorFilter::~DlColorFilter() {
if (type_ == kUnknown) {
delete static_cast<const DlUnknownColorFilter*>(this);
Expand Down Expand Up @@ -53,19 +73,16 @@ bool DlColorFilter::Equals(const DlColorFilter* a, const DlColorFilter* b) {
return true;
case kBlend:
return static_cast<const DlBlendColorFilter*>(a)->equals(
static_cast<const DlBlendColorFilter*>(b)
);
static_cast<const DlBlendColorFilter*>(b));
case kMatrix:
return static_cast<const DlMatrixColorFilter*>(a)->equals(
static_cast<const DlMatrixColorFilter*>(b)
);
static_cast<const DlMatrixColorFilter*>(b));
case kSrgbToLinearGamma:
case kLinearToSrgbGamma:
return true;
case kUnknown:
return static_cast<const DlUnknownColorFilter*>(a)->equals(
static_cast<const DlUnknownColorFilter*>(b)
);
static_cast<const DlUnknownColorFilter*>(b));
}
}

Expand All @@ -78,9 +95,11 @@ sk_sp<SkColorFilter> DlColorFilter::sk_filter() const {
case kMatrix:
return static_cast<const DlMatrixColorFilter*>(this)->sk_filter();
case kSrgbToLinearGamma:
return static_cast<const DlSrgbToLinearGammaColorFilter*>(this)->sk_filter();
return static_cast<const DlSrgbToLinearGammaColorFilter*>(this)
->sk_filter();
case kLinearToSrgbGamma:
return static_cast<const DlLinearToSrgbGammaColorFilter*>(this)->sk_filter();
return static_cast<const DlLinearToSrgbGammaColorFilter*>(this)
->sk_filter();
case kUnknown:
return static_cast<const DlUnknownColorFilter*>(this)->sk_filter();
}
Expand All @@ -91,15 +110,20 @@ std::shared_ptr<const DlColorFilter> DlColorFilter::shared() const {
case kNone:
return std::make_shared<DlNoColorFilter>();
case kBlend:
return std::make_shared<DlBlendColorFilter>(static_cast<const DlBlendColorFilter*>(this));
return std::make_shared<DlBlendColorFilter>(
static_cast<const DlBlendColorFilter*>(this));
case kMatrix:
return std::make_shared<DlMatrixColorFilter>(static_cast<const DlMatrixColorFilter*>(this));
return std::make_shared<DlMatrixColorFilter>(
static_cast<const DlMatrixColorFilter*>(this));
case kSrgbToLinearGamma:
return std::make_shared<DlSrgbToLinearGammaColorFilter>(static_cast<const DlSrgbToLinearGammaColorFilter*>(this));
return std::make_shared<DlSrgbToLinearGammaColorFilter>(
static_cast<const DlSrgbToLinearGammaColorFilter*>(this));
case kLinearToSrgbGamma:
return std::make_shared<DlLinearToSrgbGammaColorFilter>(static_cast<const DlLinearToSrgbGammaColorFilter*>(this));
return std::make_shared<DlLinearToSrgbGammaColorFilter>(
static_cast<const DlLinearToSrgbGammaColorFilter*>(this));
case kUnknown:
return std::make_shared<DlUnknownColorFilter>(static_cast<const DlUnknownColorFilter*>(this));
return std::make_shared<DlUnknownColorFilter>(
static_cast<const DlUnknownColorFilter*>(this));
}
}

Expand All @@ -115,7 +139,8 @@ bool DlColorFilter::modifies_transparent_black() const {
// Look at the matrix to make a faster determination?
// Basically, are the translation components all 0?
case kUnknown:
return sk_filter()->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT;
return sk_filter()->filterColor(SK_ColorTRANSPARENT) !=
SK_ColorTRANSPARENT;
}
}

Expand Down
22 changes: 9 additions & 13 deletions display_list/display_list_color_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class DlColorFilter {

Type type() const { return type_; }

static DlColorFilter From(SkColorFilter* sk_filter);

size_t size() const;
bool equals(const DlColorFilter* other) const;
static bool Equals(const DlColorFilter* a, const DlColorFilter* b);
Expand All @@ -37,15 +39,13 @@ class DlColorFilter {
bool modifies_transparent_black() const;

const DlBlendColorFilter* asABlendFilter() const {
return type_ == kBlend
? reinterpret_cast<const DlBlendColorFilter*>(this)
: nullptr;
return type_ == kBlend ? reinterpret_cast<const DlBlendColorFilter*>(this)
: nullptr;
}

const DlMatrixColorFilter* asAMatrixFilter() const {
return type_ == kMatrix
? reinterpret_cast<const DlMatrixColorFilter*>(this)
: nullptr;
return type_ == kMatrix ? reinterpret_cast<const DlMatrixColorFilter*>(this)
: nullptr;
}

const DlSrgbToLinearGammaColorFilter* asASrgbToLinearFilter() const {
Expand Down Expand Up @@ -88,8 +88,7 @@ class DlNoColorFilter final : public DlColorFilter {
class DlBlendColorFilter final : public DlColorFilter {
public:
DlBlendColorFilter(SkColor color, SkBlendMode mode)
: DlColorFilter(kBlend), color_(color), mode_(mode) {
}
: DlColorFilter(kBlend), color_(color), mode_(mode) {}
DlBlendColorFilter(const DlBlendColorFilter& filter)
: DlBlendColorFilter(filter.color_, filter.mode_) {}
DlBlendColorFilter(const DlBlendColorFilter* filter)
Expand All @@ -114,8 +113,7 @@ class DlBlendColorFilter final : public DlColorFilter {

class DlMatrixColorFilter final : public DlColorFilter {
public:
DlMatrixColorFilter(const float matrix[20])
: DlColorFilter(kMatrix) {
DlMatrixColorFilter(const float matrix[20]) : DlColorFilter(kMatrix) {
memcpy(matrix_, &matrix, sizeof(matrix_));
}
DlMatrixColorFilter(const DlMatrixColorFilter& filter)
Expand Down Expand Up @@ -156,9 +154,7 @@ class DlSrgbToLinearGammaColorFilter final : public DlColorFilter {
return true;
}

sk_sp<SkColorFilter> sk_filter() const {
return sk_filter_;
}
sk_sp<SkColorFilter> sk_filter() const { return sk_filter_; }

private:
static const sk_sp<SkColorFilter> sk_filter_;
Expand Down
3 changes: 1 addition & 2 deletions display_list/display_list_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ void SkPaintDispatchHelper::setMaskBlurFilter(SkBlurStyle style,
paint_.setMaskFilter(SkMaskFilter::MakeBlur(style, sigma));
}

sk_sp<SkColorFilter> SkPaintDispatchHelper::makeColorFilter()
const {
sk_sp<SkColorFilter> SkPaintDispatchHelper::makeColorFilter() const {
if (!invert_colors_) {
return color_filter_ ? color_filter_->sk_filter() : nullptr;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/painting/color_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#ifndef FLUTTER_LIB_UI_COLOR_FILTER_H_
#define FLUTTER_LIB_UI_COLOR_FILTER_H_

#include "flutter/display_list/display_list_color_filter.h"
#include "flutter/lib/ui/dart_wrapper.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/tonic/typed_data/typed_list.h"
#include "flutter/display_list/display_list_color_filter.h"

using tonic::DartPersistentValue;

Expand Down
3 changes: 1 addition & 2 deletions lib/ui/painting/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ const SkPaint* Paint::paint(SkPaint& paint) const {
}

if (uint_data[kInvertColorIndex]) {
sk_sp<SkColorFilter> invert_filter =
SkColorFilters::Matrix(invert_colors);
sk_sp<SkColorFilter> invert_filter = SkColorFilters::Matrix(invert_colors);
sk_sp<SkColorFilter> current_filter = paint.refColorFilter();
if (current_filter) {
invert_filter = invert_filter->makeComposed(current_filter);
Expand Down

0 comments on commit 99d159a

Please sign in to comment.