Reverts "[Impeller] Use Skia software renderer to draw stroked text. (#53198)" (flutter/engine#53237)

Reverts: flutter/engine#53198
Initiated by: jonahwilliams
Reason for reverting: oops.
Original PR Author: jonahwilliams

Reviewed By: {chinmaygarde}

This change reverts the following previous change:
We need to match the rounding of glyphs to fix https://github.com/flutter/flutter/issues/138670 . We also don't have sufficient AA quality with 4x MSAA for high quality strokes.

Fixes https://github.com/flutter/flutter/issues/138670
Fixes https://github.com/flutter/flutter/issues/136688
This commit is contained in:
auto-submit[bot]
2024-06-06 01:03:29 +00:00
committed by GitHub
parent ee3a8d0775
commit 50421b59ad
23 changed files with 114 additions and 324 deletions

View File

@@ -18,6 +18,7 @@
#include "impeller/aiks/color_filter.h"
#include "impeller/aiks/image.h"
#include "impeller/aiks/image_filter.h"
#include "impeller/aiks/paint_pass_delegate.h"
#include "impeller/aiks/testing/context_spy.h"
#include "impeller/core/device_buffer.h"
#include "impeller/entity/contents/solid_color_contents.h"
@@ -631,7 +632,6 @@ TEST_P(AiksTest, CanRenderRoundedRectWithNonUniformRadii) {
}
struct TextRenderOptions {
bool stroke = false;
Scalar font_size = 50;
Color color = Color::Yellow();
Point position = Vector2(100, 200);
@@ -671,9 +671,6 @@ bool RenderTextInCanvasSkia(const std::shared_ptr<Context>& context,
Paint text_paint;
text_paint.color = options.color;
text_paint.mask_blur_descriptor = options.mask_blur_descriptor;
text_paint.stroke_width = 1;
text_paint.style =
options.stroke ? Paint::Style::kStroke : Paint::Style::kFill;
canvas.DrawTextFrame(frame, options.position, text_paint);
return true;
}
@@ -717,18 +714,6 @@ TEST_P(AiksTest, CanRenderTextFrame) {
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanRenderStrokedTextFrame) {
Canvas canvas;
canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)});
ASSERT_TRUE(RenderTextInCanvasSkia(
GetContext(), canvas, "the quick brown fox jumped over the lazy dog!.?",
"Roboto-Regular.ttf",
{
.stroke = true,
}));
ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
}
TEST_P(AiksTest, CanRenderTextFrameWithHalfScaling) {
Canvas canvas;
canvas.DrawPaint({.color = Color(0.1, 0.1, 0.1, 1.0)});

View File

@@ -889,16 +889,9 @@ void Canvas::DrawTextFrame(const std::shared_ptr<TextFrame>& text_frame,
auto text_contents = std::make_shared<TextContents>();
text_contents->SetTextFrame(text_frame);
text_contents->SetColor(paint.color);
text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value());
text_contents->SetOffset(position);
text_contents->SetColor(paint.color);
text_contents->SetTextProperties(paint.color, //
paint.style == Paint::Style::kStroke, //
paint.stroke_width, //
paint.stroke_cap, //
paint.stroke_join, //
paint.stroke_miter //
);
entity.SetTransform(GetCurrentTransform() *
Matrix::MakeTranslation(position));

View File

@@ -388,16 +388,9 @@ void ExperimentalCanvas::DrawTextFrame(
auto text_contents = std::make_shared<TextContents>();
text_contents->SetTextFrame(text_frame);
text_contents->SetColor(paint.color);
text_contents->SetForceTextColor(paint.mask_blur_descriptor.has_value());
text_contents->SetScale(GetCurrentTransform().GetMaxBasisLengthXY());
text_contents->SetColor(paint.color);
text_contents->SetTextProperties(paint.color, //
paint.style == Paint::Style::kStroke, //
paint.stroke_width, //
paint.stroke_cap, //
paint.stroke_join, //
paint.stroke_miter //
);
entity.SetTransform(GetCurrentTransform() *
Matrix::MakeTranslation(position));

View File

@@ -28,7 +28,6 @@
#include "impeller/geometry/path_builder.h"
#include "impeller/geometry/scalar.h"
#include "impeller/geometry/sigma.h"
#include "impeller/typographer/font_glyph_pair.h"
#if IMPELLER_ENABLE_3D
#include "impeller/entity/contents/scene_contents.h"
@@ -1267,22 +1266,8 @@ void TextFrameDispatcher::drawTextFrame(
const std::shared_ptr<impeller::TextFrame>& text_frame,
SkScalar x,
SkScalar y) {
GlyphProperties properties;
if (paint_.style == Paint::Style::kStroke) {
properties.stroke = true;
properties.stroke_cap = paint_.stroke_cap;
properties.stroke_join = paint_.stroke_join;
properties.stroke_miter = paint_.stroke_miter;
properties.stroke_width = paint_.stroke_width;
}
if (text_frame->HasColor()) {
properties.color = paint_.color;
}
renderer_.GetLazyGlyphAtlas()->AddTextFrame(*text_frame, //
matrix_.GetMaxBasisLengthXY(), //
Point(x, y), //
properties //
);
renderer_.GetLazyGlyphAtlas()->AddTextFrame(
*text_frame, matrix_.GetMaxBasisLengthXY(), Point(x, y));
}
void TextFrameDispatcher::drawDisplayList(
@@ -1295,59 +1280,4 @@ void TextFrameDispatcher::drawDisplayList(
FML_DCHECK(stack_depth == stack_.size());
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setDrawStyle(flutter::DlDrawStyle style) {
paint_.style = ToStyle(style);
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setColor(flutter::DlColor color) {
paint_.color = {
color.getRedF(),
color.getGreenF(),
color.getBlueF(),
color.getAlphaF(),
};
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setStrokeWidth(SkScalar width) {
paint_.stroke_width = width;
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setStrokeMiter(SkScalar limit) {
paint_.stroke_miter = limit;
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setStrokeCap(flutter::DlStrokeCap cap) {
switch (cap) {
case flutter::DlStrokeCap::kButt:
paint_.stroke_cap = Cap::kButt;
break;
case flutter::DlStrokeCap::kRound:
paint_.stroke_cap = Cap::kRound;
break;
case flutter::DlStrokeCap::kSquare:
paint_.stroke_cap = Cap::kSquare;
break;
}
}
// |flutter::DlOpReceiver|
void TextFrameDispatcher::setStrokeJoin(flutter::DlStrokeJoin join) {
switch (join) {
case flutter::DlStrokeJoin::kMiter:
paint_.stroke_join = Join::kMiter;
break;
case flutter::DlStrokeJoin::kRound:
paint_.stroke_join = Join::kRound;
break;
case flutter::DlStrokeJoin::kBevel:
paint_.stroke_join = Join::kBevel;
break;
}
}
} // namespace impeller

View File

@@ -368,29 +368,10 @@ class TextFrameDispatcher : public flutter::IgnoreAttributeDispatchHelper,
void drawDisplayList(const sk_sp<flutter::DisplayList> display_list,
SkScalar opacity) override;
// |flutter::DlOpReceiver|
void setDrawStyle(flutter::DlDrawStyle style) override;
// |flutter::DlOpReceiver|
void setColor(flutter::DlColor color) override;
// |flutter::DlOpReceiver|
void setStrokeWidth(SkScalar width) override;
// |flutter::DlOpReceiver|
void setStrokeMiter(SkScalar limit) override;
// |flutter::DlOpReceiver|
void setStrokeCap(flutter::DlStrokeCap cap) override;
// |flutter::DlOpReceiver|
void setStrokeJoin(flutter::DlStrokeJoin join) override;
private:
const ContentContext& renderer_;
Matrix matrix_;
std::vector<Matrix> stack_;
Paint paint_;
};
} // namespace impeller

View File

@@ -66,30 +66,10 @@ std::optional<Rect> TextContents::GetCoverage(const Entity& entity) const {
void TextContents::PopulateGlyphAtlas(
const std::shared_ptr<LazyGlyphAtlas>& lazy_glyph_atlas,
Scalar scale) {
lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_, properties_);
lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_);
scale_ = scale;
}
void TextContents::SetTextProperties(Color color,
bool stroke,
Scalar stroke_width,
Cap stroke_cap,
Join stroke_join,
Scalar stroke_miter) {
if (frame_->HasColor()) {
// Alpha is always applied when rendering, remove it here so
// we do not double-apply the alpha.
properties_.color = color.WithAlpha(1.0);
}
if (stroke) {
properties_.stroke = true;
properties_.stroke_width = stroke_width;
properties_.stroke_cap = stroke_cap;
properties_.stroke_join = stroke_join;
properties_.stroke_miter = stroke_miter;
}
}
bool TextContents::Render(const ContentContext& renderer,
const Entity& entity,
RenderPass& pass) const {
@@ -191,7 +171,7 @@ bool TextContents::Render(const ContentContext& renderer,
Scalar rounded_scale = TextFrame::RoundScaledFontSize(
scale_, font.GetMetrics().point_size);
const FontGlyphAtlas* font_atlas =
atlas->GetFontGlyphAtlas(font, rounded_scale);
atlas->GetFontGlyphAtlas(font, rounded_scale, frame_->GetColor());
if (!font_atlas) {
VALIDATION_LOG << "Could not find font in the atlas.";
continue;
@@ -223,7 +203,7 @@ bool TextContents::Render(const ContentContext& renderer,
glyph_position, font.GetAxisAlignment(), offset_, scale_);
std::optional<std::pair<Rect, Rect>> maybe_atlas_glyph_bounds =
font_atlas->FindGlyphBounds(
SubpixelGlyph{glyph_position.glyph, subpixel, properties_});
SubpixelGlyph{glyph_position.glyph, subpixel});
if (!maybe_atlas_glyph_bounds.has_value()) {
VALIDATION_LOG << "Could not find glyph position in the atlas.";
continue;

View File

@@ -9,7 +9,7 @@
#include "impeller/entity/contents/contents.h"
#include "impeller/geometry/color.h"
#include "impeller/typographer/font_glyph_pair.h"
#include "impeller/typographer/glyph_atlas.h"
#include "impeller/typographer/text_frame.h"
namespace impeller {
@@ -33,14 +33,6 @@ class TextContents final : public Contents {
/// This is used to ensure that mask blurs work correctly on emoji.
void SetForceTextColor(bool value);
/// Must be set after text frame.
void SetTextProperties(Color color,
bool stroke,
Scalar stroke_width,
Cap stroke_cap,
Join stroke_join,
Scalar stroke_miter);
Color GetColor() const;
// |Contents|
@@ -72,11 +64,10 @@ class TextContents final : public Contents {
private:
std::shared_ptr<TextFrame> frame_;
Scalar scale_ = 1.0;
Color color_;
Scalar inherited_opacity_ = 1.0;
Vector2 offset_;
bool force_text_color_ = false;
Color color_;
GlyphProperties properties_;
TextContents(const TextContents&) = delete;

View File

@@ -6,6 +6,7 @@
#include <vector>
#include "display_list/dl_color.h"
#include "flutter/fml/logging.h"
#include "impeller/typographer/backends/skia/typeface_skia.h"
#include "impeller/typographer/font.h"
@@ -38,6 +39,15 @@ static AxisAlignment ToAxisAligment(SkAxisAlignment aligment) {
FML_UNREACHABLE();
}
static Color ToColor(const flutter::DlColor& color) {
return {
static_cast<Scalar>(color.getRedF()), //
static_cast<Scalar>(color.getGreenF()), //
static_cast<Scalar>(color.getBlueF()), //
static_cast<Scalar>(color.getAlphaF()) //
};
}
static Font ToFont(const SkTextBlobRunIterator& run, AxisAlignment alignment) {
auto& font = run.font();
auto typeface = std::make_shared<TypefaceSkia>(font.refTypeface());
@@ -59,8 +69,10 @@ static Rect ToRect(const SkRect& rect) {
}
std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob) {
const sk_sp<SkTextBlob>& blob,
flutter::DlColor dl_color) {
bool has_color = false;
Color color = ToColor(dl_color);
std::vector<TextRun> runs;
for (SkTextBlobRunIterator run(blob.get()); !run.done(); run.next()) {
// TODO(jonahwilliams): ask Skia for a public API to look this up.
@@ -102,7 +114,8 @@ std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
continue;
}
}
return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color);
return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color,
has_color ? color : Color::Black());
}
} // namespace impeller

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TEXT_FRAME_SKIA_H_
#define FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_TEXT_FRAME_SKIA_H_
#include "display_list/dl_color.h"
#include "impeller/typographer/text_frame.h"
#include "third_party/skia/include/core/SkTextBlob.h"
@@ -12,7 +13,8 @@
namespace impeller {
std::shared_ptr<impeller::TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob);
const sk_sp<SkTextBlob>& blob,
flutter::DlColor color = flutter::DlColor::kBlack());
} // namespace impeller

View File

@@ -33,7 +33,6 @@
#include "impeller/typographer/typographer_context.h"
#include "include/core/SkColor.h"
#include "include/core/SkImageInfo.h"
#include "include/core/SkPaint.h"
#include "include/core/SkSize.h"
#include "third_party/skia/include/core/SkBitmap.h"
@@ -49,32 +48,6 @@ namespace impeller {
// https://github.com/flutter/flutter/issues/114563
constexpr auto kPadding = 2;
namespace {
SkPaint::Cap ToSkiaCap(Cap cap) {
switch (cap) {
case Cap::kButt:
return SkPaint::Cap::kButt_Cap;
case Cap::kRound:
return SkPaint::Cap::kRound_Cap;
case Cap::kSquare:
return SkPaint::Cap::kSquare_Cap;
}
FML_UNREACHABLE();
}
SkPaint::Join ToSkiaJoin(Join join) {
switch (join) {
case Join::kMiter:
return SkPaint::Join::kMiter_Join;
case Join::kRound:
return SkPaint::Join::kRound_Join;
case Join::kBevel:
return SkPaint::Join::kBevel_Join;
}
FML_UNREACHABLE();
}
} // namespace
std::shared_ptr<TypographerContext> TypographerContextSkia::Make() {
return std::make_shared<TypographerContextSkia>();
}
@@ -210,7 +183,6 @@ static void DrawGlyph(SkCanvas* canvas,
const ScaledFont& scaled_font,
const SubpixelGlyph& glyph,
const Rect& scaled_bounds,
const GlyphProperties& prop,
bool has_color) {
const auto& metrics = scaled_font.font.GetMetrics();
SkPoint position = SkPoint::Make(1, 1);
@@ -225,19 +197,11 @@ static void DrawGlyph(SkCanvas* canvas,
sk_font.setSubpixel(true);
sk_font.setSize(sk_font.getSize() * scaled_font.scale);
auto glyph_color =
has_color ? glyph.properties.color.ToARGB() : SK_ColorBLACK;
auto glyph_color = has_color ? scaled_font.color.ToARGB() : SK_ColorBLACK;
SkPaint glyph_paint;
glyph_paint.setColor(glyph_color);
glyph_paint.setBlendMode(SkBlendMode::kSrc);
if (prop.stroke) {
glyph_paint.setStroke(true);
glyph_paint.setStrokeWidth(prop.stroke_width * scaled_font.scale);
glyph_paint.setStrokeCap(ToSkiaCap(glyph.properties.stroke_cap));
glyph_paint.setStrokeJoin(ToSkiaJoin(glyph.properties.stroke_join));
glyph_paint.setStrokeMiter(prop.stroke_miter * scaled_font.scale);
}
canvas->translate(glyph.subpixel_offset.x, glyph.subpixel_offset.y);
canvas->drawGlyphs(1u, // count
&glyph_id, // glyphs
@@ -291,8 +255,7 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
return false;
}
DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds,
pair.glyph.properties, has_color);
DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, has_color);
// Writing to a malloc'd buffer and then copying to the staging buffers
// benchmarks as substantially faster on a number of Android devices.
@@ -319,19 +282,9 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
return blit_pass->ConvertTextureToShaderRead(texture);
}
static Rect ComputeGlyphSize(const SkFont& font,
const SubpixelGlyph& glyph,
Scalar scale) {
static Rect ComputeGlyphSize(const SkFont& font, const SubpixelGlyph& glyph) {
SkRect scaled_bounds;
SkPaint glyph_paint;
if (glyph.properties.stroke) {
glyph_paint.setStroke(true);
glyph_paint.setStrokeWidth(glyph.properties.stroke_width * scale);
glyph_paint.setStrokeCap(ToSkiaCap(glyph.properties.stroke_cap));
glyph_paint.setStrokeJoin(ToSkiaJoin(glyph.properties.stroke_join));
glyph_paint.setStrokeMiter(glyph.properties.stroke_miter * scale);
}
font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, &glyph_paint);
font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, nullptr);
// Expand the bounds of glyphs at subpixel offsets by 2 in the x direction.
Scalar adjustment = 0.0;
@@ -369,8 +322,8 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
std::vector<FontGlyphPair> new_glyphs;
for (const auto& font_value : font_glyph_map) {
const ScaledFont& scaled_font = font_value.first;
const FontGlyphAtlas* font_glyph_atlas =
last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
const FontGlyphAtlas* font_glyph_atlas = last_atlas->GetFontGlyphAtlas(
scaled_font.font, scaled_font.scale, scaled_font.color);
auto metrics = scaled_font.font.GetMetrics();
@@ -390,15 +343,13 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
for (const SubpixelGlyph& glyph : font_value.second) {
if (!font_glyph_atlas->FindGlyphBounds(glyph)) {
new_glyphs.emplace_back(scaled_font, glyph);
glyph_sizes.push_back(
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
glyph_sizes.push_back(ComputeGlyphSize(sk_font, glyph));
}
}
} else {
for (const SubpixelGlyph& glyph : font_value.second) {
new_glyphs.emplace_back(scaled_font, glyph);
glyph_sizes.push_back(
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
glyph_sizes.push_back(ComputeGlyphSize(sk_font, glyph));
}
}
}

View File

@@ -65,7 +65,7 @@ std::shared_ptr<TextFrame> MakeTextFrameSTB(
std::vector<TextRun> runs = {run};
return std::make_shared<TextFrame>(
runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false);
runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false, Color::Black());
}
} // namespace impeller

View File

@@ -428,8 +428,8 @@ std::shared_ptr<GlyphAtlas> TypographerContextSTB::CreateGlyphAtlas(
std::vector<Rect> new_sizes;
for (const auto& font_value : font_glyph_map) {
const ScaledFont& scaled_font = font_value.first;
const FontGlyphAtlas* font_glyph_atlas =
last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
const FontGlyphAtlas* font_glyph_atlas = last_atlas->GetFontGlyphAtlas(
scaled_font.font, scaled_font.scale, scaled_font.color);
if (font_glyph_atlas) {
for (const SubpixelGlyph& glyph : font_value.second) {
if (!font_glyph_atlas->FindGlyphBounds(glyph)) {

View File

@@ -9,22 +9,12 @@
#include <unordered_set>
#include "impeller/geometry/color.h"
#include "impeller/geometry/path.h"
#include "impeller/geometry/point.h"
#include "impeller/typographer/font.h"
#include "impeller/typographer/glyph.h"
namespace impeller {
struct GlyphProperties {
Color color = Color::Black();
Scalar stroke_width = 0.0;
Cap stroke_cap = Cap::kButt;
Join stroke_join = Join::kMiter;
Scalar stroke_miter = 4.0;
bool stroke = false;
};
//------------------------------------------------------------------------------
/// @brief A font and a scale. Used as a key that represents a typeface
/// within a glyph atlas.
@@ -32,6 +22,7 @@ struct GlyphProperties {
struct ScaledFont {
Font font;
Scalar scale;
Color color;
};
//------------------------------------------------------------------------------
@@ -40,14 +31,9 @@ struct ScaledFont {
struct SubpixelGlyph {
Glyph glyph;
Point subpixel_offset;
GlyphProperties properties;
SubpixelGlyph(Glyph p_glyph,
Point p_subpixel_offset,
GlyphProperties p_properties)
: glyph(p_glyph),
subpixel_offset(p_subpixel_offset),
properties(p_properties) {}
SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset)
: glyph(p_glyph), subpixel_offset(p_subpixel_offset) {}
};
using FontGlyphMap =
@@ -69,7 +55,7 @@ struct FontGlyphPair {
template <>
struct std::hash<impeller::ScaledFont> {
constexpr std::size_t operator()(const impeller::ScaledFont& sf) const {
return fml::HashCombine(sf.font.GetHash(), sf.scale);
return fml::HashCombine(sf.font.GetHash(), sf.scale, sf.color.ToARGB());
}
};
@@ -77,18 +63,16 @@ template <>
struct std::equal_to<impeller::ScaledFont> {
constexpr bool operator()(const impeller::ScaledFont& lhs,
const impeller::ScaledFont& rhs) const {
return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale &&
lhs.color == rhs.color;
}
};
template <>
struct std::hash<impeller::SubpixelGlyph> {
constexpr std::size_t operator()(const impeller::SubpixelGlyph& sg) const {
return fml::HashCombine(
sg.glyph.index, sg.subpixel_offset.x, sg.subpixel_offset.y,
sg.properties.color.ToARGB(), sg.properties.stroke,
sg.properties.stroke_cap, sg.properties.stroke_join,
sg.properties.stroke_miter, sg.properties.stroke_width);
return fml::HashCombine(sg.glyph.index, sg.subpixel_offset.x,
sg.subpixel_offset.y);
}
};
@@ -98,13 +82,7 @@ struct std::equal_to<impeller::SubpixelGlyph> {
const impeller::SubpixelGlyph& rhs) const {
return lhs.glyph.index == rhs.glyph.index &&
lhs.glyph.type == rhs.glyph.type &&
lhs.subpixel_offset == rhs.subpixel_offset &&
lhs.properties.color.ToARGB() == rhs.properties.color.ToARGB() &&
lhs.properties.stroke == rhs.properties.stroke &&
lhs.properties.stroke_cap == rhs.properties.stroke_cap &&
lhs.properties.stroke_join == rhs.properties.stroke_join &&
lhs.properties.stroke_miter == rhs.properties.stroke_miter &&
lhs.properties.stroke_width == rhs.properties.stroke_width;
lhs.subpixel_offset == rhs.subpixel_offset;
}
};

View File

@@ -80,8 +80,9 @@ std::optional<std::pair<Rect, Rect>> GlyphAtlas::FindFontGlyphBounds(
}
const FontGlyphAtlas* GlyphAtlas::GetFontGlyphAtlas(const Font& font,
Scalar scale) const {
const auto& found = font_atlas_map_.find(ScaledFont{font, scale});
Scalar scale,
Color color) const {
const auto& found = font_atlas_map_.find({font, scale, color});
if (found == font_atlas_map_.end()) {
return nullptr;
}

View File

@@ -130,7 +130,9 @@ class GlyphAtlas {
/// scale are not available in the atlas. The pointer is only
/// valid for the lifetime of the GlyphAtlas.
///
const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) const;
const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font,
Scalar scale,
Color color) const;
private:
const Type type_;

View File

@@ -32,15 +32,12 @@ LazyGlyphAtlas::~LazyGlyphAtlas() = default;
void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame,
Scalar scale,
Point offset,
const GlyphProperties& properties) {
Point offset) {
FML_DCHECK(alpha_atlas_ == nullptr && color_atlas_ == nullptr);
if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) {
frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset,
properties);
frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale, offset);
} else {
frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset,
properties);
frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale, offset);
}
}

View File

@@ -19,10 +19,7 @@ class LazyGlyphAtlas {
~LazyGlyphAtlas();
void AddTextFrame(const TextFrame& frame,
Scalar scale,
Point offset,
const GlyphProperties& properties);
void AddTextFrame(const TextFrame& frame, Scalar scale, Point offset);
void ResetTextFrames();

View File

@@ -10,8 +10,14 @@ namespace impeller {
TextFrame::TextFrame() = default;
TextFrame::TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color)
: runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {}
TextFrame::TextFrame(std::vector<TextRun>& runs,
Rect bounds,
bool has_color,
Color color)
: runs_(std::move(runs)),
bounds_(bounds),
has_color_(has_color),
color_(color) {}
TextFrame::~TextFrame() = default;
@@ -32,8 +38,8 @@ GlyphAtlas::Type TextFrame::GetAtlasType() const {
: GlyphAtlas::Type::kAlphaBitmap;
}
bool TextFrame::HasColor() const {
return has_color_;
Color TextFrame::GetColor() const {
return color_;
}
// static
@@ -84,21 +90,19 @@ Point TextFrame::ComputeSubpixelPosition(
}
}
void TextFrame::CollectUniqueFontGlyphPairs(
FontGlyphMap& glyph_map,
Scalar scale,
Point offset,
const GlyphProperties& properties) const {
void TextFrame::CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,
Scalar scale,
Point offset) const {
for (const TextRun& run : GetRuns()) {
const Font& font = run.GetFont();
auto rounded_scale =
RoundScaledFontSize(scale, font.GetMetrics().point_size);
auto& set = glyph_map[ScaledFont{font, rounded_scale}];
auto& set = glyph_map[ScaledFont{font, rounded_scale, color_}];
for (const TextRun::GlyphPosition& glyph_position :
run.GetGlyphPositions()) {
Point subpixel = ComputeSubpixelPosition(
glyph_position, font.GetAxisAlignment(), offset, scale);
set.emplace(glyph_position.glyph, subpixel, properties);
set.emplace(glyph_position.glyph, subpixel);
}
}
}

View File

@@ -20,14 +20,16 @@ class TextFrame {
public:
TextFrame();
TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color);
TextFrame(std::vector<TextRun>& runs,
Rect bounds,
bool has_color,
Color color);
~TextFrame();
void CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,
Scalar scale,
Point offset,
const GlyphProperties& properties) const;
Point offset) const;
static Point ComputeSubpixelPosition(
const TextRun::GlyphPosition& glyph_position,
@@ -66,7 +68,7 @@ class TextFrame {
/// COLR fonts can potentially use the paint color in the glyph
/// atlas, so this color must be considered as part of the cache
/// key.
bool HasColor() const;
Color GetColor() const;
//----------------------------------------------------------------------------
/// @brief The type of atlas this run should be emplaced in.
@@ -80,6 +82,7 @@ class TextFrame {
std::vector<TextRun> runs_;
Rect bounds_;
bool has_color_;
Color color_;
};
} // namespace impeller

View File

@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "display_list/dl_color.h"
#include "flutter/display_list/testing/dl_test_snippets.h"
#include "flutter/testing/testing.h"
#include "gtest/gtest.h"
@@ -38,7 +39,7 @@ static std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const TextFrame& frame) {
FontGlyphMap font_glyph_map;
frame.CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0}, {});
frame.CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0});
return typographer_context->CreateGlyphAtlas(context, type, host_buffer,
atlas_context, font_glyph_map);
}
@@ -50,13 +51,10 @@ static std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
GlyphAtlas::Type type,
Scalar scale,
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const std::vector<std::shared_ptr<TextFrame>>& frames,
const std::vector<GlyphProperties>& properties) {
const std::vector<std::shared_ptr<TextFrame>>& frames) {
FontGlyphMap font_glyph_map;
size_t offset = 0;
for (auto& frame : frames) {
frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0},
properties[offset++]);
frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0});
}
return typographer_context->CreateGlyphAtlas(context, type, host_buffer,
atlas_context, font_glyph_map);
@@ -137,14 +135,14 @@ TEST_P(TypographerTest, LazyAtlasTracksColor) {
LazyGlyphAtlas lazy_atlas(TypographerContextSkia::Make());
lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}, {});
lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0});
frame = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("😀 ", emoji_font));
ASSERT_TRUE(frame->GetAtlasType() == GlyphAtlas::Type::kColorBitmap);
lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0}, {});
lazy_atlas.AddTextFrame(*frame, 1.0f, {0, 0});
// Creates different atlases for color and red bitmap.
auto color_atlas = lazy_atlas.CreateOrGetGlyphAtlas(
@@ -224,7 +222,7 @@ TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) {
size_t size_count = 8;
for (size_t index = 0; index < size_count; index += 1) {
MakeTextFrameFromTextBlobSkia(blob)->CollectUniqueFontGlyphPairs(
font_glyph_map, 0.6 * index, {0, 0}, {});
font_glyph_map, 0.6 * index, {0, 0});
};
auto atlas =
context->CreateGlyphAtlas(*GetContext(), GlyphAtlas::Type::kAlphaBitmap,
@@ -305,18 +303,14 @@ TEST_P(TypographerTest, GlyphColorIsPartOfCacheKey) {
// Create two frames with the same character and a different color, expect
// that it adds a character.
auto frame = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("😂", emoji_font));
SkTextBlob::MakeFromString("😂", emoji_font), flutter::DlColor::kCyan());
auto frame_2 = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("😂", emoji_font));
auto properties = {
GlyphProperties{.color = Color::Red()},
GlyphProperties{.color = Color::Blue()},
};
SkTextBlob::MakeFromString("😂", emoji_font),
flutter::DlColor::kMagenta());
auto next_atlas =
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context,
{frame, frame_2}, properties);
auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f,
atlas_context, {frame, frame_2});
EXPECT_EQ(next_atlas->GetGlyphCount(), 2u);
}
@@ -334,19 +328,14 @@ TEST_P(TypographerTest, GlyphColorIsIgnoredForNonEmojiFonts) {
// Create two frames with the same character and a different color, but as a
// non-emoji font the text frame constructor will ignore it.
auto frame =
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font));
auto frame_2 =
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font));
auto properties = {
GlyphProperties{},
GlyphProperties{},
};
auto frame = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kCyan());
auto frame_2 = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kMagenta());
auto next_atlas =
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context,
{frame, frame_2}, properties);
auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f,
atlas_context, {frame, frame_2});
EXPECT_EQ(next_atlas->GetGlyphCount(), 1u);
}

View File

@@ -406,9 +406,6 @@ impeller_Play_AiksTest_CanRenderStrokePathThatEndsAtSharpTurn_Vulkan.png
impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_Metal.png
impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_OpenGLES.png
impeller_Play_AiksTest_CanRenderStrokePathWithCubicLine_Vulkan.png
impeller_Play_AiksTest_CanRenderStrokedTextFrame_Metal.png
impeller_Play_AiksTest_CanRenderStrokedTextFrame_OpenGLES.png
impeller_Play_AiksTest_CanRenderStrokedTextFrame_Vulkan.png
impeller_Play_AiksTest_CanRenderStrokes_Metal.png
impeller_Play_AiksTest_CanRenderStrokes_OpenGLES.png
impeller_Play_AiksTest_CanRenderStrokes_Vulkan.png

View File

@@ -102,8 +102,9 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
builder_->DrawPath(transformed, dl_paints_[paint_id]);
return;
}
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x,
y, dl_paints_[paint_id]);
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(
blob, dl_paints_[paint_id].getColor()),
x, y, dl_paints_[paint_id]);
return;
}
#endif // IMPELLER_SUPPORTS_RENDERING
@@ -125,8 +126,9 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
paint.setMaskFilter(&filter);
}
if (impeller_enabled_) {
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x,
y, paint);
builder_->DrawTextFrame(
impeller::MakeTextFrameFromTextBlobSkia(blob, paint.getColor()), x, y,
paint);
return;
}
builder_->DrawTextBlob(blob, x, y, paint);
@@ -217,11 +219,12 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
bool ShouldRenderAsPath(const DlPaint& paint) const {
FML_DCHECK(impeller_enabled_);
// Text with non-trivial color sources should be rendered as a path when
// running on Impeller for correctness. These filters rely on having the
// glyph coverage, whereas regular text is drawn as rectangular texture
// samples.
return (paint.getColorSource() && !paint.getColorSource()->asColor());
// Text with non-trivial color sources or stroke paint mode should be
// rendered as a path when running on Impeller for correctness. These
// filters rely on having the glyph coverage, whereas regular text is
// drawn as rectangular texture samples.
return ((paint.getColorSource() && !paint.getColorSource()->asColor()) ||
paint.getDrawStyle() != DlDrawStyle::kFill);
}
DlPaint toDlPaint(const DecorationStyle& decor_style,

View File

@@ -253,9 +253,9 @@ TEST_F(PainterTest, DrawStrokedTextImpeller) {
auto recorder = DlOpRecorder();
draw(style)->Dispatch(recorder);
EXPECT_EQ(recorder.textFrameCount(), 1);
EXPECT_EQ(recorder.textFrameCount(), 0);
EXPECT_EQ(recorder.blobCount(), 0);
EXPECT_EQ(recorder.pathCount(), 0);
EXPECT_EQ(recorder.pathCount(), 1);
}
TEST_F(PainterTest, DrawTextWithGradientImpeller) {