Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Refactor ColorSource resolution to use explicit factory types #37656

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,8 @@ FILE: ../../../flutter/impeller/aiks/aiks_playground.h
FILE: ../../../flutter/impeller/aiks/aiks_unittests.cc
FILE: ../../../flutter/impeller/aiks/canvas.cc
FILE: ../../../flutter/impeller/aiks/canvas.h
FILE: ../../../flutter/impeller/aiks/color_source_factory.cc
FILE: ../../../flutter/impeller/aiks/color_source_factory.h
FILE: ../../../flutter/impeller/aiks/image.cc
FILE: ../../../flutter/impeller/aiks/image.h
FILE: ../../../flutter/impeller/aiks/paint.cc
Expand Down Expand Up @@ -1129,6 +1131,10 @@ FILE: ../../../flutter/impeller/compiler/types.cc
FILE: ../../../flutter/impeller/compiler/types.h
FILE: ../../../flutter/impeller/compiler/utilities.cc
FILE: ../../../flutter/impeller/compiler/utilities.h
FILE: ../../../flutter/impeller/display_list/conversion_utilities.cc
FILE: ../../../flutter/impeller/display_list/conversion_utilities.h
FILE: ../../../flutter/impeller/display_list/display_list_color_source_factory.cc
FILE: ../../../flutter/impeller/display_list/display_list_color_source_factory.h
FILE: ../../../flutter/impeller/display_list/display_list_dispatcher.cc
FILE: ../../../flutter/impeller/display_list/display_list_dispatcher.h
FILE: ../../../flutter/impeller/display_list/display_list_image_impeller.cc
Expand Down
2 changes: 2 additions & 0 deletions impeller/aiks/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ impeller_component("aiks") {
"aiks_context.h",
"canvas.cc",
"canvas.h",
"color_source_factory.cc",
"color_source_factory.h",
"image.cc",
"image.h",
"paint.cc",
Expand Down
425 changes: 234 additions & 191 deletions impeller/aiks/aiks_unittests.cc

Large diffs are not rendered by default.

11 changes: 4 additions & 7 deletions impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ void Canvas::DrawPaint(const Paint& paint) {
bool Canvas::AttemptDrawBlurredRRect(const Rect& rect,
Scalar corner_radius,
const Paint& paint) {
if (paint.color_source == nullptr ||
paint.color_source_type != Paint::ColorSourceType::kColor ||
paint.style != Paint::Style::kFill) {
if (!paint.color_source || paint.style != Paint::Style::kFill) {
return false;
}

Expand Down Expand Up @@ -375,17 +373,16 @@ void Canvas::DrawTextFrame(const TextFrame& text_frame,

void Canvas::DrawVertices(const Vertices& vertices,
BlendMode blend_mode,
Paint paint) {
const Paint& paint) {
auto geometry = Geometry::MakeVertices(vertices);

Entity entity;
entity.SetTransformation(GetCurrentTransformation());
entity.SetStencilDepth(GetStencilDepth());
entity.SetBlendMode(paint.blend_mode);

if (paint.color_source.has_value()) {
auto& source = paint.color_source.value();
auto contents = source();
if (paint.color_source) {
auto contents = paint.color_source->MakeContents();
contents->SetGeometry(std::move(geometry));
contents->SetAlpha(paint.color.alpha);
entity.SetContents(paint.WithFilters(std::move(contents), true));
Expand Down
2 changes: 1 addition & 1 deletion impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Canvas {

void DrawVertices(const Vertices& vertices,
BlendMode blend_mode,
Paint paint);
const Paint& paint);

void DrawAtlas(const std::shared_ptr<Image>& atlas,
std::vector<Matrix> transforms,
Expand Down
11 changes: 11 additions & 0 deletions impeller/aiks/color_source_factory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// 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 "impeller/aiks/color_source_factory.h"

namespace impeller {

ColorSourceFactory::~ColorSourceFactory() = default;

} // namespace impeller
32 changes: 32 additions & 0 deletions impeller/aiks/color_source_factory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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.

#pragma once

#include <memory>

#include "impeller/entity/contents/color_source_contents.h"

namespace impeller {

class ColorSourceFactory {
public:
enum class ColorSourceType {
kColor,
kImage,
kLinearGradient,
kRadialGradient,
kConicalGradient,
kSweepGradient,
kRuntimeEffect,
};

virtual ~ColorSourceFactory();

virtual std::shared_ptr<ColorSourceContents> MakeContents() = 0;

virtual ColorSourceType GetType() = 0;
};

} // namespace impeller
5 changes: 2 additions & 3 deletions impeller/aiks/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@ std::shared_ptr<Contents> Paint::CreateContentsForEntity(const Path& path,

std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
std::unique_ptr<Geometry> geometry) const {
if (color_source.has_value()) {
auto& source = color_source.value();
auto contents = source();
if (color_source) {
auto contents = color_source->MakeContents();
contents->SetGeometry(std::move(geometry));
contents->SetAlpha(color.alpha);
return contents;
Expand Down
17 changes: 3 additions & 14 deletions impeller/aiks/paint.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <memory>

#include "flutter/fml/macros.h"
#include "impeller/aiks/color_source_factory.h"
#include "impeller/entity/contents/contents.h"
#include "impeller/entity/contents/filters/color_filter_contents.h"
#include "impeller/entity/contents/filters/filter_contents.h"
Expand All @@ -29,23 +30,12 @@ struct Paint {
FilterInput::Ref,
bool is_solid_color,
const Matrix& effect_transform)>;
using ColorSourceProc = std::function<std::shared_ptr<ColorSourceContents>()>;

enum class Style {
kFill,
kStroke,
};

enum class ColorSourceType {
kColor,
kImage,
kLinearGradient,
kRadialGradient,
kConicalGradient,
kSweepGradient,
kRuntimeEffect,
};

struct MaskBlurDescriptor {
FilterContents::BlurStyle style;
Sigma sigma;
Expand All @@ -56,10 +46,9 @@ struct Paint {
const Matrix& effect_matrix) const;
};

Color color = Color::Black();
std::optional<ColorSourceProc> color_source;
ColorSourceType color_source_type = ColorSourceType::kColor;
std::shared_ptr<ColorSourceFactory> color_source;

Color color = Color::Black();
Scalar stroke_width = 0.0;
Cap stroke_cap = Cap::kButt;
Join stroke_join = Join::kMiter;
Expand Down
4 changes: 4 additions & 0 deletions impeller/display_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import("//flutter/impeller/tools/impeller.gni")

impeller_component("display_list") {
sources = [
"conversion_utilities.cc",
"conversion_utilities.h",
"display_list_color_source_factory.cc",
"display_list_color_source_factory.h",
"display_list_dispatcher.cc",
"display_list_dispatcher.h",
"display_list_image_impeller.cc",
Expand Down
179 changes: 179 additions & 0 deletions impeller/display_list/conversion_utilities.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// 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 "impeller/display_list/conversion_utilities.h"

namespace impeller {

BlendMode ToBlendMode(flutter::DlBlendMode mode) {
switch (mode) {
case flutter::DlBlendMode::kClear:
return BlendMode::kClear;
case flutter::DlBlendMode::kSrc:
return BlendMode::kSource;
case flutter::DlBlendMode::kDst:
return BlendMode::kDestination;
case flutter::DlBlendMode::kSrcOver:
return BlendMode::kSourceOver;
case flutter::DlBlendMode::kDstOver:
return BlendMode::kDestinationOver;
case flutter::DlBlendMode::kSrcIn:
return BlendMode::kSourceIn;
case flutter::DlBlendMode::kDstIn:
return BlendMode::kDestinationIn;
case flutter::DlBlendMode::kSrcOut:
return BlendMode::kSourceOut;
case flutter::DlBlendMode::kDstOut:
return BlendMode::kDestinationOut;
case flutter::DlBlendMode::kSrcATop:
return BlendMode::kSourceATop;
case flutter::DlBlendMode::kDstATop:
return BlendMode::kDestinationATop;
case flutter::DlBlendMode::kXor:
return BlendMode::kXor;
case flutter::DlBlendMode::kPlus:
return BlendMode::kPlus;
case flutter::DlBlendMode::kModulate:
return BlendMode::kModulate;
case flutter::DlBlendMode::kScreen:
return BlendMode::kScreen;
case flutter::DlBlendMode::kOverlay:
return BlendMode::kOverlay;
case flutter::DlBlendMode::kDarken:
return BlendMode::kDarken;
case flutter::DlBlendMode::kLighten:
return BlendMode::kLighten;
case flutter::DlBlendMode::kColorDodge:
return BlendMode::kColorDodge;
case flutter::DlBlendMode::kColorBurn:
return BlendMode::kColorBurn;
case flutter::DlBlendMode::kHardLight:
return BlendMode::kHardLight;
case flutter::DlBlendMode::kSoftLight:
return BlendMode::kSoftLight;
case flutter::DlBlendMode::kDifference:
return BlendMode::kDifference;
case flutter::DlBlendMode::kExclusion:
return BlendMode::kExclusion;
case flutter::DlBlendMode::kMultiply:
return BlendMode::kMultiply;
case flutter::DlBlendMode::kHue:
return BlendMode::kHue;
case flutter::DlBlendMode::kSaturation:
return BlendMode::kSaturation;
case flutter::DlBlendMode::kColor:
return BlendMode::kColor;
case flutter::DlBlendMode::kLuminosity:
return BlendMode::kLuminosity;
}
FML_UNREACHABLE();
}

Entity::TileMode ToTileMode(flutter::DlTileMode tile_mode) {
switch (tile_mode) {
case flutter::DlTileMode::kClamp:
return Entity::TileMode::kClamp;
case flutter::DlTileMode::kRepeat:
return Entity::TileMode::kRepeat;
case flutter::DlTileMode::kMirror:
return Entity::TileMode::kMirror;
case flutter::DlTileMode::kDecal:
return Entity::TileMode::kDecal;
}
}

impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlImageSampling options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlImageSampling::kNearestNeighbor:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlImageSampling::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
case flutter::DlImageSampling::kMipmapLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.mip_filter = impeller::MipFilter::kLinear;
desc.label = "Mipmap Linear Sampler";
break;
default:
break;
}
return desc;
}

impeller::SamplerDescriptor ToSamplerDescriptor(
const flutter::DlFilterMode options) {
impeller::SamplerDescriptor desc;
switch (options) {
case flutter::DlFilterMode::kNearest:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kNearest;
desc.label = "Nearest Sampler";
break;
case flutter::DlFilterMode::kLinear:
desc.min_filter = desc.mag_filter = impeller::MinMagFilter::kLinear;
desc.label = "Linear Sampler";
break;
default:
break;
}
return desc;
}

Matrix ToMatrix(const SkMatrix& m) {
return Matrix{
// clang-format off
m[0], m[3], 0, m[6],
m[1], m[4], 0, m[7],
0, 0, 1, 0,
m[2], m[5], 0, m[8],
// clang-format on
};
}

Point ToPoint(const SkPoint& point) {
return Point::MakeXY(point.fX, point.fY);
}

Color ToColor(const SkColor& color) {
return {
static_cast<Scalar>(SkColorGetR(color) / 255.0), //
static_cast<Scalar>(SkColorGetG(color) / 255.0), //
static_cast<Scalar>(SkColorGetB(color) / 255.0), //
static_cast<Scalar>(SkColorGetA(color) / 255.0) //
};
}

std::vector<Color> ToColors(const flutter::DlColor colors[], int count) {
auto result = std::vector<Color>();
if (colors == nullptr) {
return result;
}
for (int i = 0; i < count; i++) {
result.push_back(ToColor(colors[i]));
}
return result;
}

std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count) {
auto result = std::vector<Matrix>();
for (int i = 0; i < count; i++) {
auto form = xform[i];
// clang-format off
auto matrix = Matrix{
form.fSCos, form.fSSin, 0, 0,
-form.fSSin, form.fSCos, 0, 0,
0, 0, 1, 0,
form.fTx, form.fTy, 0, 1
};
// clang-format on
result.push_back(matrix);
}
return result;
}

} // namespace impeller
Loading