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

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:
Jonah Williams
2024-06-05 16:54:48 -07:00
committed by GitHub
parent b5bc4a3868
commit ee3a8d0775
23 changed files with 324 additions and 114 deletions

View File

@@ -18,7 +18,6 @@
#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"
@@ -632,6 +631,7 @@ TEST_P(AiksTest, CanRenderRoundedRectWithNonUniformRadii) {
}
struct TextRenderOptions {
bool stroke = false;
Scalar font_size = 50;
Color color = Color::Yellow();
Point position = Vector2(100, 200);
@@ -671,6 +671,9 @@ 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;
}
@@ -714,6 +717,18 @@ 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,9 +889,16 @@ 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,9 +388,16 @@ 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,6 +28,7 @@
#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"
@@ -1266,8 +1267,22 @@ void TextFrameDispatcher::drawTextFrame(
const std::shared_ptr<impeller::TextFrame>& text_frame,
SkScalar x,
SkScalar y) {
renderer_.GetLazyGlyphAtlas()->AddTextFrame(
*text_frame, matrix_.GetMaxBasisLengthXY(), Point(x, 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 //
);
}
void TextFrameDispatcher::drawDisplayList(
@@ -1280,4 +1295,59 @@ 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,10 +368,29 @@ 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,10 +66,30 @@ 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_);
lazy_glyph_atlas->AddTextFrame(*frame_, scale, offset_, properties_);
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 {
@@ -171,7 +191,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, frame_->GetColor());
atlas->GetFontGlyphAtlas(font, rounded_scale);
if (!font_atlas) {
VALIDATION_LOG << "Could not find font in the atlas.";
continue;
@@ -203,7 +223,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});
SubpixelGlyph{glyph_position.glyph, subpixel, properties_});
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/glyph_atlas.h"
#include "impeller/typographer/font_glyph_pair.h"
#include "impeller/typographer/text_frame.h"
namespace impeller {
@@ -33,6 +33,14 @@ 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|
@@ -64,10 +72,11 @@ 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,7 +6,6 @@
#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"
@@ -39,15 +38,6 @@ 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());
@@ -69,10 +59,8 @@ static Rect ToRect(const SkRect& rect) {
}
std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob,
flutter::DlColor dl_color) {
const sk_sp<SkTextBlob>& blob) {
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.
@@ -114,8 +102,7 @@ std::shared_ptr<TextFrame> MakeTextFrameFromTextBlobSkia(
continue;
}
}
return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color,
has_color ? color : Color::Black());
return std::make_shared<TextFrame>(runs, ToRect(blob->bounds()), has_color);
}
} // namespace impeller

View File

@@ -5,7 +5,6 @@
#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"
@@ -13,8 +12,7 @@
namespace impeller {
std::shared_ptr<impeller::TextFrame> MakeTextFrameFromTextBlobSkia(
const sk_sp<SkTextBlob>& blob,
flutter::DlColor color = flutter::DlColor::kBlack());
const sk_sp<SkTextBlob>& blob);
} // namespace impeller

View File

@@ -33,6 +33,7 @@
#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"
@@ -48,6 +49,32 @@ 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>();
}
@@ -183,6 +210,7 @@ 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);
@@ -197,11 +225,19 @@ static void DrawGlyph(SkCanvas* canvas,
sk_font.setSubpixel(true);
sk_font.setSize(sk_font.getSize() * scaled_font.scale);
auto glyph_color = has_color ? scaled_font.color.ToARGB() : SK_ColorBLACK;
auto glyph_color =
has_color ? glyph.properties.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
@@ -255,7 +291,8 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
return false;
}
DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds, has_color);
DrawGlyph(canvas, pair.scaled_font, pair.glyph, bounds,
pair.glyph.properties, 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.
@@ -282,9 +319,19 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
return blit_pass->ConvertTextureToShaderRead(texture);
}
static Rect ComputeGlyphSize(const SkFont& font, const SubpixelGlyph& glyph) {
static Rect ComputeGlyphSize(const SkFont& font,
const SubpixelGlyph& glyph,
Scalar scale) {
SkRect scaled_bounds;
font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, nullptr);
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);
// Expand the bounds of glyphs at subpixel offsets by 2 in the x direction.
Scalar adjustment = 0.0;
@@ -322,8 +369,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, scaled_font.color);
const FontGlyphAtlas* font_glyph_atlas =
last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
auto metrics = scaled_font.font.GetMetrics();
@@ -343,13 +390,15 @@ 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));
glyph_sizes.push_back(
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
}
}
} else {
for (const SubpixelGlyph& glyph : font_value.second) {
new_glyphs.emplace_back(scaled_font, glyph);
glyph_sizes.push_back(ComputeGlyphSize(sk_font, glyph));
glyph_sizes.push_back(
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
}
}
}

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, Color::Black());
runs, result.value_or(Rect::MakeLTRB(0, 0, 0, 0)), false);
}
} // 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, scaled_font.color);
const FontGlyphAtlas* font_glyph_atlas =
last_atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
if (font_glyph_atlas) {
for (const SubpixelGlyph& glyph : font_value.second) {
if (!font_glyph_atlas->FindGlyphBounds(glyph)) {

View File

@@ -9,12 +9,22 @@
#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.
@@ -22,7 +32,6 @@ namespace impeller {
struct ScaledFont {
Font font;
Scalar scale;
Color color;
};
//------------------------------------------------------------------------------
@@ -31,9 +40,14 @@ struct ScaledFont {
struct SubpixelGlyph {
Glyph glyph;
Point subpixel_offset;
GlyphProperties properties;
SubpixelGlyph(Glyph p_glyph, Point p_subpixel_offset)
: glyph(p_glyph), subpixel_offset(p_subpixel_offset) {}
SubpixelGlyph(Glyph p_glyph,
Point p_subpixel_offset,
GlyphProperties p_properties)
: glyph(p_glyph),
subpixel_offset(p_subpixel_offset),
properties(p_properties) {}
};
using FontGlyphMap =
@@ -55,7 +69,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, sf.color.ToARGB());
return fml::HashCombine(sf.font.GetHash(), sf.scale);
}
};
@@ -63,16 +77,18 @@ 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 &&
lhs.color == rhs.color;
return lhs.font.IsEqual(rhs.font) && lhs.scale == rhs.scale;
}
};
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);
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);
}
};
@@ -82,7 +98,13 @@ 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.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;
}
};

View File

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

View File

@@ -130,9 +130,7 @@ 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,
Color color) const;
const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) const;
private:
const Type type_;

View File

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

View File

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

View File

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

View File

@@ -20,16 +20,14 @@ class TextFrame {
public:
TextFrame();
TextFrame(std::vector<TextRun>& runs,
Rect bounds,
bool has_color,
Color color);
TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color);
~TextFrame();
void CollectUniqueFontGlyphPairs(FontGlyphMap& glyph_map,
Scalar scale,
Point offset) const;
Point offset,
const GlyphProperties& properties) const;
static Point ComputeSubpixelPosition(
const TextRun::GlyphPosition& glyph_position,
@@ -68,7 +66,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.
Color GetColor() const;
bool HasColor() const;
//----------------------------------------------------------------------------
/// @brief The type of atlas this run should be emplaced in.
@@ -82,7 +80,6 @@ class TextFrame {
std::vector<TextRun> runs_;
Rect bounds_;
bool has_color_;
Color color_;
};
} // namespace impeller

View File

@@ -2,7 +2,6 @@
// 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"
@@ -39,7 +38,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);
}
@@ -51,10 +50,13 @@ 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<std::shared_ptr<TextFrame>>& frames,
const std::vector<GlyphProperties>& properties) {
FontGlyphMap font_glyph_map;
size_t offset = 0;
for (auto& frame : frames) {
frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0});
frame->CollectUniqueFontGlyphPairs(font_glyph_map, scale, {0, 0},
properties[offset++]);
}
return typographer_context->CreateGlyphAtlas(context, type, host_buffer,
atlas_context, font_glyph_map);
@@ -135,14 +137,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(
@@ -222,7 +224,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,
@@ -303,14 +305,18 @@ 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), flutter::DlColor::kCyan());
SkTextBlob::MakeFromString("😂", emoji_font));
auto frame_2 = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("😂", emoji_font),
flutter::DlColor::kMagenta());
SkTextBlob::MakeFromString("😂", emoji_font));
auto properties = {
GlyphProperties{.color = Color::Red()},
GlyphProperties{.color = Color::Blue()},
};
auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f,
atlas_context, {frame, frame_2});
auto next_atlas =
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context,
{frame, frame_2}, properties);
EXPECT_EQ(next_atlas->GetGlyphCount(), 2u);
}
@@ -328,14 +334,19 @@ 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), flutter::DlColor::kCyan());
auto frame_2 = MakeTextFrameFromTextBlobSkia(
SkTextBlob::MakeFromString("A", sk_font), flutter::DlColor::kMagenta());
auto frame =
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font));
auto frame_2 =
MakeTextFrameFromTextBlobSkia(SkTextBlob::MakeFromString("A", sk_font));
auto properties = {
GlyphProperties{},
GlyphProperties{},
};
auto next_atlas = CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f,
atlas_context, {frame, frame_2});
auto next_atlas =
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
GlyphAtlas::Type::kColorBitmap, 1.0f, atlas_context,
{frame, frame_2}, properties);
EXPECT_EQ(next_atlas->GetGlyphCount(), 1u);
}

View File

@@ -406,6 +406,9 @@ 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,9 +102,8 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
builder_->DrawPath(transformed, dl_paints_[paint_id]);
return;
}
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(
blob, dl_paints_[paint_id].getColor()),
x, y, dl_paints_[paint_id]);
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x,
y, dl_paints_[paint_id]);
return;
}
#endif // IMPELLER_SUPPORTS_RENDERING
@@ -126,9 +125,8 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
paint.setMaskFilter(&filter);
}
if (impeller_enabled_) {
builder_->DrawTextFrame(
impeller::MakeTextFrameFromTextBlobSkia(blob, paint.getColor()), x, y,
paint);
builder_->DrawTextFrame(impeller::MakeTextFrameFromTextBlobSkia(blob), x,
y, paint);
return;
}
builder_->DrawTextBlob(blob, x, y, paint);
@@ -219,12 +217,11 @@ class DisplayListParagraphPainter : public skt::ParagraphPainter {
bool ShouldRenderAsPath(const DlPaint& paint) const {
FML_DCHECK(impeller_enabled_);
// 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);
// 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());
}
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(), 0);
EXPECT_EQ(recorder.textFrameCount(), 1);
EXPECT_EQ(recorder.blobCount(), 0);
EXPECT_EQ(recorder.pathCount(), 1);
EXPECT_EQ(recorder.pathCount(), 0);
}
TEST_F(PainterTest, DrawTextWithGradientImpeller) {