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

Commit a254c5e

Browse files
author
auto-submit[bot]
committed
Revert "[Impeller] hash even less stuff per frame. (#55092)"
This reverts commit d283bc8.
1 parent 3dfb462 commit a254c5e

17 files changed

+205
-402
lines changed

impeller/display_list/dl_dispatcher.cc

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,13 +1458,10 @@ void TextFrameDispatcher::drawTextFrame(
14581458
}
14591459
auto scale =
14601460
(matrix_ * Matrix::MakeTranslation(Point(x, y))).GetMaxBasisLengthXY();
1461-
renderer_.GetLazyGlyphAtlas()->AddTextFrame(
1462-
text_frame, //
1463-
scale, //
1464-
Point(x, y), //
1465-
(properties.stroke || text_frame->HasColor()) //
1466-
? std::optional<GlyphProperties>(properties) //
1467-
: std::nullopt //
1461+
renderer_.GetLazyGlyphAtlas()->AddTextFrame(*text_frame, //
1462+
scale, //
1463+
Point(x, y), //
1464+
properties //
14681465
);
14691466
}
14701467

impeller/entity/contents/text_contents.cc

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ bool TextContents::Render(const ContentContext& renderer,
9090
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
9191
return false;
9292
}
93-
if (!frame_->IsFrameComplete()) {
94-
VALIDATION_LOG << "Failed to find font glyph bounds.";
95-
return false;
96-
}
9793

9894
// Information shared by all glyph draw calls.
9995
pass.SetCommandLabel("TextFrame");
@@ -173,12 +169,16 @@ bool TextContents::Render(const ContentContext& renderer,
173169
VS::PerVertexData* vtx_contents =
174170
reinterpret_cast<VS::PerVertexData*>(contents);
175171
size_t i = 0u;
176-
size_t bounds_offset = 0u;
177172
for (const TextRun& run : frame_->GetRuns()) {
178173
const Font& font = run.GetFont();
179174
Scalar rounded_scale = TextFrame::RoundScaledFontSize(
180175
scale_, font.GetMetrics().point_size);
181-
FontGlyphAtlas* font_atlas = nullptr;
176+
const FontGlyphAtlas* font_atlas =
177+
atlas->GetFontGlyphAtlas(font, rounded_scale);
178+
if (!font_atlas) {
179+
VALIDATION_LOG << "Could not find font in the atlas.";
180+
continue;
181+
}
182182

183183
// Adjust glyph position based on the subpixel rounding
184184
// used by the font.
@@ -201,45 +201,22 @@ bool TextContents::Render(const ContentContext& renderer,
201201
Point screen_offset = (entity_transform * Point(0, 0));
202202
for (const TextRun::GlyphPosition& glyph_position :
203203
run.GetGlyphPositions()) {
204-
const FrameBounds& frame_bounds =
205-
frame_->GetFrameBounds(bounds_offset);
206-
bounds_offset++;
207-
auto atlas_glyph_bounds = frame_bounds.atlas_bounds;
208-
auto glyph_bounds = frame_bounds.glyph_bounds;
209-
210-
// If frame_bounds.is_placeholder is true, this is the first frame
211-
// the glyph has been rendered and so its atlas position was not
212-
// known when the glyph was recorded. Perform a slow lookup into the
213-
// glyph atlas hash table.
214-
if (frame_bounds.is_placeholder) {
215-
// Note: uses unrounded scale for more accurate subpixel position.
216-
if (!font_atlas) {
217-
font_atlas = atlas->GetOrCreateFontGlyphAtlas(
218-
ScaledFont{font, rounded_scale});
219-
}
220-
221-
if (!font_atlas) {
222-
VALIDATION_LOG << "Could not find font in the atlas.";
223-
continue;
224-
}
225-
// Note: uses unrounded scale for more accurate subpixel position.
226-
Point subpixel = TextFrame::ComputeSubpixelPosition(
227-
glyph_position, font.GetAxisAlignment(), offset_, scale_);
228-
229-
std::optional<FrameBounds> maybe_atlas_glyph_bounds =
230-
font_atlas->FindGlyphBounds(SubpixelGlyph{
231-
glyph_position.glyph, //
232-
subpixel, //
233-
GetGlyphProperties() //
234-
});
235-
if (!maybe_atlas_glyph_bounds.has_value()) {
236-
VALIDATION_LOG << "Could not find glyph position in the atlas.";
237-
continue;
238-
}
239-
atlas_glyph_bounds =
240-
maybe_atlas_glyph_bounds.value().atlas_bounds;
204+
// Note: uses unrounded scale for more accurate subpixel position.
205+
Point subpixel = TextFrame::ComputeSubpixelPosition(
206+
glyph_position, font.GetAxisAlignment(), offset_, scale_);
207+
std::optional<std::pair<Rect, Rect>> maybe_atlas_glyph_bounds =
208+
font_atlas->FindGlyphBounds(SubpixelGlyph{
209+
glyph_position.glyph, subpixel,
210+
(properties_.stroke || frame_->HasColor())
211+
? std::optional<GlyphProperties>(properties_)
212+
: std::nullopt});
213+
if (!maybe_atlas_glyph_bounds.has_value()) {
214+
VALIDATION_LOG << "Could not find glyph position in the atlas.";
215+
continue;
241216
}
242-
217+
const Rect& atlas_glyph_bounds =
218+
maybe_atlas_glyph_bounds.value().first;
219+
Rect glyph_bounds = maybe_atlas_glyph_bounds.value().second;
243220
Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
244221
// For each glyph, we compute two rectangles. One for the vertex
245222
// positions and one for the texture coordinates (UVs). The atlas
@@ -289,10 +266,4 @@ bool TextContents::Render(const ContentContext& renderer,
289266
return pass.Draw().ok();
290267
}
291268

292-
std::optional<GlyphProperties> TextContents::GetGlyphProperties() const {
293-
return (properties_.stroke || frame_->HasColor())
294-
? std::optional<GlyphProperties>(properties_)
295-
: std::nullopt;
296-
}
297-
298269
} // namespace impeller

impeller/entity/contents/text_contents.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ class TextContents final : public Contents {
6262
RenderPass& pass) const override;
6363

6464
private:
65-
std::optional<GlyphProperties> GetGlyphProperties() const;
66-
6765
std::shared_ptr<TextFrame> frame_;
6866
Scalar scale_ = 1.0;
6967
Scalar inherited_opacity_ = 1.0;

impeller/typographer/backends/skia/typographer_context_skia.cc

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,7 @@ static bool BulkUpdateAtlasBitmap(const GlyphAtlas& atlas,
282282
if (!data.has_value()) {
283283
continue;
284284
}
285-
auto [pos, bounds, placeholder] = data.value();
286-
FML_DCHECK(!placeholder);
285+
auto [pos, bounds] = data.value();
287286
Size size = pos.GetSize();
288287
if (size.IsEmpty()) {
289288
continue;
@@ -326,9 +325,7 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
326325
if (!data.has_value()) {
327326
continue;
328327
}
329-
auto [pos, bounds, placeholder] = data.value();
330-
FML_DCHECK(!placeholder);
331-
328+
auto [pos, bounds] = data.value();
332329
Size size = pos.GetSize();
333330
if (size.IsEmpty()) {
334331
continue;
@@ -405,88 +402,61 @@ static Rect ComputeGlyphSize(const SkFont& font,
405402
scaled_bounds.fBottom);
406403
};
407404

408-
std::pair<std::vector<FontGlyphPair>, std::vector<Rect>>
409-
TypographerContextSkia::CollectNewGlyphs(
410-
const std::shared_ptr<GlyphAtlas>& atlas,
411-
const std::vector<std::shared_ptr<TextFrame>>& text_frames) {
412-
std::vector<FontGlyphPair> new_glyphs;
413-
std::vector<Rect> glyph_sizes;
414-
for (const auto& frame : text_frames) {
415-
// TODO(jonahwilliams): unless we destroy the atlas (which we know about),
416-
// we could probably guarantee that a text frame that is complete does not
417-
// need to be processed unless the scale or properties changed. I'm leaving
418-
// this as a future optimization.
419-
frame->ClearFrameBounds();
420-
421-
for (const auto& run : frame->GetRuns()) {
422-
auto metrics = run.GetFont().GetMetrics();
423-
424-
auto rounded_scale =
425-
TextFrame::RoundScaledFontSize(frame->GetScale(), metrics.point_size);
426-
ScaledFont scaled_font{.font = run.GetFont(), .scale = rounded_scale};
427-
428-
FontGlyphAtlas* font_glyph_atlas =
429-
atlas->GetOrCreateFontGlyphAtlas(scaled_font);
430-
FML_DCHECK(!!font_glyph_atlas);
431-
432-
SkFont sk_font(
433-
TypefaceSkia::Cast(*scaled_font.font.GetTypeface()).GetSkiaTypeface(),
434-
metrics.point_size, metrics.scaleX, metrics.skewX);
435-
sk_font.setEdging(SkFont::Edging::kAntiAlias);
436-
sk_font.setHinting(SkFontHinting::kSlight);
437-
sk_font.setEmbolden(metrics.embolden);
438-
// Rather than computing the bounds at the requested point size and
439-
// scaling up the bounds, we scale up the font size and request the
440-
// bounds. This seems to give more accurate bounds information.
441-
sk_font.setSize(sk_font.getSize() * scaled_font.scale);
442-
sk_font.setSubpixel(true);
443-
444-
for (const auto& glyph_position : run.GetGlyphPositions()) {
445-
Point subpixel = TextFrame::ComputeSubpixelPosition(
446-
glyph_position, scaled_font.font.GetAxisAlignment(),
447-
frame->GetOffset(), rounded_scale);
448-
SubpixelGlyph subpixel_glyph(glyph_position.glyph, subpixel,
449-
frame->GetProperties());
450-
const auto& font_glyph_bounds =
451-
font_glyph_atlas->FindGlyphBounds(subpixel_glyph);
452-
453-
if (!font_glyph_bounds.has_value()) {
454-
new_glyphs.push_back(FontGlyphPair{scaled_font, subpixel_glyph});
455-
auto glyph_bounds =
456-
ComputeGlyphSize(sk_font, subpixel_glyph, scaled_font.scale);
457-
glyph_sizes.push_back(glyph_bounds);
458-
459-
auto frame_bounds = FrameBounds{
460-
Rect::MakeLTRB(0, 0, 0, 0), //
461-
glyph_bounds, //
462-
/*placeholder=*/true //
463-
};
464-
465-
frame->AppendFrameBounds(frame_bounds);
466-
font_glyph_atlas->AppendGlyph(subpixel_glyph, frame_bounds);
467-
} else {
468-
frame->AppendFrameBounds(font_glyph_bounds.value());
405+
static void CollectNewGlyphs(const std::shared_ptr<GlyphAtlas>& atlas,
406+
const FontGlyphMap& font_glyph_map,
407+
std::vector<FontGlyphPair>& new_glyphs,
408+
std::vector<Rect>& glyph_sizes) {
409+
for (const auto& font_value : font_glyph_map) {
410+
const ScaledFont& scaled_font = font_value.first;
411+
const FontGlyphAtlas* font_glyph_atlas =
412+
atlas->GetFontGlyphAtlas(scaled_font.font, scaled_font.scale);
413+
414+
auto metrics = scaled_font.font.GetMetrics();
415+
416+
SkFont sk_font(
417+
TypefaceSkia::Cast(*scaled_font.font.GetTypeface()).GetSkiaTypeface(),
418+
metrics.point_size, metrics.scaleX, metrics.skewX);
419+
sk_font.setEdging(SkFont::Edging::kAntiAlias);
420+
sk_font.setHinting(SkFontHinting::kSlight);
421+
sk_font.setEmbolden(metrics.embolden);
422+
// Rather than computing the bounds at the requested point size and scaling
423+
// up the bounds, we scale up the font size and request the bounds. This
424+
// seems to give more accurate bounds information.
425+
sk_font.setSize(sk_font.getSize() * scaled_font.scale);
426+
sk_font.setSubpixel(true);
427+
428+
if (font_glyph_atlas) {
429+
for (const SubpixelGlyph& glyph : font_value.second) {
430+
if (!font_glyph_atlas->FindGlyphBounds(glyph)) {
431+
new_glyphs.emplace_back(scaled_font, glyph);
432+
glyph_sizes.push_back(
433+
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
469434
}
470435
}
436+
} else {
437+
for (const SubpixelGlyph& glyph : font_value.second) {
438+
new_glyphs.emplace_back(scaled_font, glyph);
439+
glyph_sizes.push_back(
440+
ComputeGlyphSize(sk_font, glyph, scaled_font.scale));
441+
}
471442
}
472443
}
473-
return {std::move(new_glyphs), std::move(glyph_sizes)};
474444
}
475445

476446
std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
477447
Context& context,
478448
GlyphAtlas::Type type,
479449
HostBuffer& host_buffer,
480450
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
481-
const std::vector<std::shared_ptr<TextFrame>>& text_frames) const {
451+
const FontGlyphMap& font_glyph_map) const {
482452
TRACE_EVENT0("impeller", __FUNCTION__);
483453
if (!IsValid()) {
484454
return nullptr;
485455
}
486456
std::shared_ptr<GlyphAtlas> last_atlas = atlas_context->GetGlyphAtlas();
487457
FML_DCHECK(last_atlas->GetType() == type);
488458

489-
if (text_frames.empty()) {
459+
if (font_glyph_map.empty()) {
490460
return last_atlas;
491461
}
492462

@@ -495,7 +465,9 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
495465
// with the current atlas and reuse if possible. For each new font and
496466
// glyph pair, compute the glyph size at scale.
497467
// ---------------------------------------------------------------------------
498-
auto [new_glyphs, glyph_sizes] = CollectNewGlyphs(last_atlas, text_frames);
468+
std::vector<FontGlyphPair> new_glyphs;
469+
std::vector<Rect> glyph_sizes;
470+
CollectNewGlyphs(last_atlas, font_glyph_map, new_glyphs, glyph_sizes);
499471
if (new_glyphs.size() == 0) {
500472
return last_atlas;
501473
}
@@ -564,11 +536,9 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
564536
blit_old_atlas = false;
565537
new_atlas = std::make_shared<GlyphAtlas>(type);
566538

567-
auto [update_glyphs, update_sizes] =
568-
CollectNewGlyphs(new_atlas, text_frames);
569-
new_glyphs = std::move(update_glyphs);
570-
glyph_sizes = std::move(update_sizes);
571-
539+
new_glyphs.clear();
540+
glyph_sizes.clear();
541+
CollectNewGlyphs(new_atlas, font_glyph_map, new_glyphs, glyph_sizes);
572542
glyph_positions.clear();
573543
glyph_positions.reserve(new_glyphs.size());
574544
first_missing_index = 0;

impeller/typographer/backends/skia/typographer_context_skia.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ class TypographerContextSkia : public TypographerContext {
2727
GlyphAtlas::Type type,
2828
HostBuffer& host_buffer,
2929
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
30-
const std::vector<std::shared_ptr<TextFrame>>& text_frames)
31-
const override;
30+
const FontGlyphMap& font_glyph_map) const override;
3231

3332
private:
34-
static std::pair<std::vector<FontGlyphPair>, std::vector<Rect>>
35-
CollectNewGlyphs(const std::shared_ptr<GlyphAtlas>& atlas,
36-
const std::vector<std::shared_ptr<TextFrame>>& text_frames);
37-
3833
TypographerContextSkia(const TypographerContextSkia&) = delete;
3934

4035
TypographerContextSkia& operator=(const TypographerContextSkia&) = delete;

0 commit comments

Comments
 (0)