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

[Impeller] Use new SkParagraph APIs for stroked text. #41735

Merged
merged 5 commits into from
Jul 12, 2023
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
1 change: 1 addition & 0 deletions impeller/display_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ impeller_component("skia_conversions") {
"../geometry",
"//flutter/fml",
"//third_party/skia",
"//third_party/skia/modules/skparagraph",
]
}

Expand Down
20 changes: 17 additions & 3 deletions impeller/display_list/dl_dispatcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,23 @@ void DlDispatcher::drawDisplayList(
void DlDispatcher::drawTextBlob(const sk_sp<SkTextBlob> blob,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's strictly necessary, but adding a playground test in display_list_unittests.cc would be nice as a jumping off point for debugging in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea, will do.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

SkScalar x,
SkScalar y) {
canvas_.DrawTextFrame(TextFrameFromTextBlob(blob), //
impeller::Point{x, y}, //
paint_ //
const auto text_frame = TextFrameFromTextBlob(blob);
if (paint_.style == Paint::Style::kStroke) {
auto path = skia_conversions::PathDataFromTextBlob(blob);
auto bounds = text_frame.GetBounds();
if (!bounds.has_value()) {
return;
}
canvas_.Save();
canvas_.Translate({x + bounds->origin.x, y + bounds->origin.y, 0.0});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the frame bounds origin actually the right thing to be adding here? It would seem weird for Skia's API to circularly depend on the text frame bounds origin when coming up with path coords to output. Needing to add in something like the baseline offset would make sense, though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest I don't really know. This is similar to the approach I used when drawing with different color sources on top of the text: https://github.com/flutter/engine/blob/main/impeller/aiks/canvas.cc#L529-L534

canvas_.DrawPath(path, paint_);
canvas_.Restore();
return;
}

canvas_.DrawTextFrame(text_frame, //
impeller::Point{x, y}, //
paint_ //
);
}

Expand Down
13 changes: 13 additions & 0 deletions impeller/display_list/dl_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,19 @@ TEST_P(DisplayListTest, CanDrawWithMaskBlur) {
ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}

TEST_P(DisplayListTest, CanDrawStrokedText) {
flutter::DisplayListBuilder builder;
flutter::DlPaint paint;

paint.setDrawStyle(flutter::DlDrawStyle::kStroke);
paint.setColor(flutter::DlColor::kRed());
builder.DrawTextBlob(
SkTextBlob::MakeFromString("stoked about stroked text", CreateTestFont()),
250, 250, paint);

ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
}

TEST_P(DisplayListTest, IgnoreMaskFilterWhenSavingLayer) {
auto texture = CreateTextureForFixture("embarcadero.jpg");
flutter::DisplayListBuilder builder;
Expand Down
9 changes: 9 additions & 0 deletions impeller/display_list/skia_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// found in the LICENSE file.

#include "impeller/display_list/skia_conversions.h"
#include "third_party/skia/modules/skparagraph/include/Paragraph.h"

namespace impeller {
namespace skia_conversions {
Expand Down Expand Up @@ -159,6 +160,14 @@ std::vector<Matrix> ToRSXForms(const SkRSXform xform[], int count) {
return result;
}

Path PathDataFromTextBlob(const sk_sp<SkTextBlob>& blob) {
if (!blob) {
return {};
}

return ToPath(skia::textlayout::Paragraph::GetPath(blob.get()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I'll be damned. That's a lot easier than I was expecting!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I had to wait 3 months for the API to be added so IMO not that easy 😆

}

std::optional<impeller::PixelFormat> ToPixelFormat(SkColorType type) {
switch (type) {
case kRGBA_8888_SkColorType:
Expand Down
3 changes: 3 additions & 0 deletions impeller/display_list/skia_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "third_party/skia/include/core/SkPoint.h"
#include "third_party/skia/include/core/SkRRect.h"
#include "third_party/skia/include/core/SkRSXform.h"
#include "third_party/skia/include/core/SkTextBlob.h"

namespace impeller {
namespace skia_conversions {
Expand All @@ -40,6 +41,8 @@ Path ToPath(const SkPath& path);

Path ToPath(const SkRRect& rrect);

Path PathDataFromTextBlob(const sk_sp<SkTextBlob>& blob);

std::optional<impeller::PixelFormat> ToPixelFormat(SkColorType type);

} // namespace skia_conversions
Expand Down