| Flutter Impeller |

typographer_context_skia.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/typographer/backends/skia/typographer_context_skia.h"
6
7 #include <cstddef>
8 #include <cstdint>
9 #include <memory>
10 #include <numeric>
11 #include <utility>
12 #include <vector>
13
14 #include "flutter/fml/logging.h"
15 #include "flutter/fml/trace_event.h"
16 #include "fml/closure.h"
17
18 #include "impeller/base/validation.h"
19 #include "impeller/core/allocator.h"
20 #include "impeller/core/buffer_view.h"
21 #include "impeller/core/formats.h"
22 #include "impeller/core/host_buffer.h"
23 #include "impeller/core/texture_descriptor.h"
24 #include "impeller/geometry/rect.h"
25 #include "impeller/geometry/size.h"
26 #include "impeller/renderer/command_buffer.h"
27 #include "impeller/renderer/render_pass.h"
28 #include "impeller/renderer/render_target.h"
29 #include "impeller/typographer/backends/skia/typeface_skia.h"
30 #include "impeller/typographer/font_glyph_pair.h"
31 #include "impeller/typographer/glyph.h"
32 #include "impeller/typographer/glyph_atlas.h"
33 #include "impeller/typographer/rectangle_packer.h"
34 #include "impeller/typographer/typographer_context.h"
35 #include "include/core/SkColor.h"
36 #include "include/core/SkImageInfo.h"
37 #include "include/core/SkPaint.h"
38 #include "include/core/SkSize.h"
39
40 #include "third_party/skia/include/core/SkBitmap.h"
41 #include "third_party/skia/include/core/SkBlendMode.h"
42 #include "third_party/skia/include/core/SkCanvas.h"
43 #include "third_party/skia/include/core/SkFont.h"
44 #include "third_party/skia/include/core/SkSurface.h"
45
46 namespace impeller {
47
48 constexpr auto kPadding = 2;
49
50 namespace {
51 SkPaint::Cap ToSkiaCap(Cap cap) {
52 switch (cap) {
53 case Cap::kButt:
54 return SkPaint::Cap::kButt_Cap;
55 case Cap::kRound:
56 return SkPaint::Cap::kRound_Cap;
57 case Cap::kSquare:
58 return SkPaint::Cap::kSquare_Cap;
59 }
60 FML_UNREACHABLE();
61 }
62
63 SkPaint::Join ToSkiaJoin(Join join) {
64 switch (join) {
65 case Join::kMiter:
66 return SkPaint::Join::kMiter_Join;
67 case Join::kRound:
68 return SkPaint::Join::kRound_Join;
69 case Join::kBevel:
70 return SkPaint::Join::kBevel_Join;
71 }
72 FML_UNREACHABLE();
73 }
74 } // namespace
75
76 std::shared_ptr<TypographerContext> TypographerContextSkia::Make() {
77 return std::make_shared<TypographerContextSkia>();
78 }
79
80 TypographerContextSkia::TypographerContextSkia() = default;
81
82 TypographerContextSkia::~TypographerContextSkia() = default;
83
84 std::shared_ptr<GlyphAtlasContext>
85 TypographerContextSkia::CreateGlyphAtlasContext(GlyphAtlas::Type type) const {
86 return std::make_shared<GlyphAtlasContext>(type);
87 }
88
89 static SkImageInfo GetImageInfo(const GlyphAtlas& atlas, Size size) {
90 switch (atlas.GetType()) {
91 case GlyphAtlas::Type::kAlphaBitmap:
92 return SkImageInfo::MakeA8(SkISize{static_cast<int32_t>(size.width),
93 static_cast<int32_t>(size.height)});
94 case GlyphAtlas::Type::kColorBitmap:
95 return SkImageInfo::MakeN32Premul(size.width, size.height);
96 }
97 FML_UNREACHABLE();
98 }
99
100 /// Append as many glyphs to the texture as will fit, and return the first index
101 /// of [extra_pairs] that did not fit.
102 static size_t AppendToExistingAtlas(
103 const std::shared_ptr<GlyphAtlas>& atlas,
104 const std::vector<FontGlyphPair>& extra_pairs,
105 std::vector<Rect>& glyph_positions,
106 const std::vector<Rect>& glyph_sizes,
107 ISize atlas_size,
108 int64_t height_adjustment,
109 const std::shared_ptr<RectanglePacker>& rect_packer) {
110 TRACE_EVENT0("impeller", __FUNCTION__);
111 if (!rect_packer || atlas_size.IsEmpty()) {
112 return 0;
113 }
114
115 for (size_t i = 0; i < extra_pairs.size(); i++) {
116 ISize glyph_size = ISize::Ceil(glyph_sizes[i].GetSize());
117 IPoint16 location_in_atlas;
118 if (!rect_packer->AddRect(glyph_size.width + kPadding, //
119 glyph_size.height + kPadding, //
120 &location_in_atlas //
121 )) {
122 return i;
123 }
124 // Position the glyph in the center of the 1px padding.
125 glyph_positions.push_back(Rect::MakeXYWH(
126 location_in_atlas.x() + 1, //
127 location_in_atlas.y() + height_adjustment + 1, //
128 glyph_size.width, //
129 glyph_size.height //
130 ));
131 }
132
133 return extra_pairs.size();
134 }
135
136 static size_t PairsFitInAtlasOfSize(
137 const std::vector<FontGlyphPair>& pairs,
138 const ISize& atlas_size,
139 std::vector<Rect>& glyph_positions,
140 const std::vector<Rect>& glyph_sizes,
141 int64_t height_adjustment,
142 const std::shared_ptr<RectanglePacker>& rect_packer,
143 size_t start_index) {
144 FML_DCHECK(!atlas_size.IsEmpty());
145
146 for (size_t i = start_index; i < pairs.size(); i++) {
147 ISize glyph_size = ISize::Ceil(glyph_sizes[i].GetSize());
148 IPoint16 location_in_atlas;
149 if (!rect_packer->AddRect(glyph_size.width + kPadding, //
150 glyph_size.height + kPadding, //
151 &location_in_atlas //
152 )) {
153 return i;
154 }
155 glyph_positions.push_back(Rect::MakeXYWH(
156 location_in_atlas.x() + 1, //
157 location_in_atlas.y() + height_adjustment + 1, //
158 glyph_size.width, //
159 glyph_size.height //
160 ));
161 }
162
163 return pairs.size();
164 }
165
166 static ISize ComputeNextAtlasSize(
167 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
168 const std::vector<FontGlyphPair>& extra_pairs,
169 std::vector<Rect>& glyph_positions,
170 const std::vector<Rect>& glyph_sizes,
171 size_t glyph_index_start,
172 int64_t max_texture_height) {
173 // Because we can't grow the skyline packer horizontally, pick a reasonable
174 // large width for all atlases.
175 static constexpr int64_t kAtlasWidth = 4096;
176 static constexpr int64_t kMinAtlasHeight = 1024;
177
178 ISize current_size = ISize(kAtlasWidth, kMinAtlasHeight);
179 if (atlas_context->GetAtlasSize().height > current_size.height) {
180 current_size.height = atlas_context->GetAtlasSize().height * 2;
181 }
182
183 auto height_adjustment = atlas_context->GetAtlasSize().height;
184 while (current_size.height <= max_texture_height) {
185 std::shared_ptr<RectanglePacker> rect_packer;
186 if (atlas_context->GetRectPacker() || glyph_index_start) {
187 rect_packer = RectanglePacker::Factory(
188 kAtlasWidth,
189 current_size.height - atlas_context->GetAtlasSize().height);
190 } else {
191 rect_packer = RectanglePacker::Factory(kAtlasWidth, current_size.height);
192 }
193 glyph_positions.erase(glyph_positions.begin() + glyph_index_start,
194 glyph_positions.end());
195 atlas_context->UpdateRectPacker(rect_packer);
196 auto next_index = PairsFitInAtlasOfSize(
197 extra_pairs, current_size, glyph_positions, glyph_sizes,
198 height_adjustment, rect_packer, glyph_index_start);
199 if (next_index == extra_pairs.size()) {
200 return current_size;
201 }
202 current_size = ISize(current_size.width, current_size.height * 2);
203 }
204 return {};
205 }
206
207 static Point SubpixelPositionToPoint(SubpixelPosition pos) {
208 return Point((pos & 0xff) / 4.f, (pos >> 2 & 0xff) / 4.f);
209 }
210
211 static void DrawGlyph(SkCanvas* canvas,
212 const SkPoint position,
213 const ScaledFont& scaled_font,
214 const SubpixelGlyph& glyph,
215 const Rect& scaled_bounds,
216 const std::optional<GlyphProperties>& prop,
217 bool has_color) {
218 const auto& metrics = scaled_font.font.GetMetrics();
219 SkGlyphID glyph_id = glyph.glyph.index;
220
221 SkFont sk_font(
222 TypefaceSkia::Cast(*scaled_font.font.GetTypeface()).GetSkiaTypeface(),
223 metrics.point_size, metrics.scaleX, metrics.skewX);
224 sk_font.setEdging(SkFont::Edging::kAntiAlias);
225 sk_font.setHinting(SkFontHinting::kSlight);
226 sk_font.setEmbolden(metrics.embolden);
227 sk_font.setSubpixel(true);
228 sk_font.setSize(sk_font.getSize() * static_cast<Scalar>(scaled_font.scale));
229
230 auto glyph_color = prop.has_value() ? prop->color.ToARGB() : SK_ColorBLACK;
231
232 SkPaint glyph_paint;
233 glyph_paint.setColor(glyph_color);
234 glyph_paint.setBlendMode(SkBlendMode::kSrc);
235 if (prop.has_value()) {
236 auto stroke = prop->stroke;
237 if (stroke.has_value()) {
238 glyph_paint.setStroke(true);
239 glyph_paint.setStrokeWidth(stroke->width *
240 static_cast<Scalar>(scaled_font.scale));
241 glyph_paint.setStrokeCap(ToSkiaCap(stroke->cap));
242 glyph_paint.setStrokeJoin(ToSkiaJoin(stroke->join));
243 glyph_paint.setStrokeMiter(stroke->miter_limit);
244 } else {
245 glyph_paint.setStroke(false);
246 }
247 }
248 canvas->save();
249 Point subpixel_offset = SubpixelPositionToPoint(glyph.subpixel_offset);
250 canvas->translate(subpixel_offset.x, subpixel_offset.y);
251 canvas->drawGlyphs(1u, // count
252 &glyph_id, // glyphs
253 &position, // positions
254 SkPoint::Make(-scaled_bounds.GetLeft(),
255 -scaled_bounds.GetTop()), // origin
256 sk_font, // font
257 glyph_paint // paint
258 );
259 canvas->restore();
260 }
261
262 /// @brief Batch render to a single surface.
263 ///
264 /// This is only safe for use when updating a fresh texture.
265 static bool BulkUpdateAtlasBitmap(const GlyphAtlas& atlas,
266 std::shared_ptr<BlitPass>& blit_pass,
267 HostBuffer& host_buffer,
268 const std::shared_ptr<Texture>& texture,
269 const std::vector<FontGlyphPair>& new_pairs,
270 size_t start_index,
271 size_t end_index) {
272 TRACE_EVENT0("impeller", __FUNCTION__);
273
274 bool has_color = atlas.GetType() == GlyphAtlas::Type::kColorBitmap;
275
276 SkBitmap bitmap;
277 bitmap.setInfo(GetImageInfo(atlas, Size(texture->GetSize())));
278 if (!bitmap.tryAllocPixels()) {
279 return false;
280 }
281
282 auto surface = SkSurfaces::WrapPixels(bitmap.pixmap());
283 if (!surface) {
284 return false;
285 }
286 auto canvas = surface->getCanvas();
287 if (!canvas) {
288 return false;
289 }
290
291 for (size_t i = start_index; i < end_index; i++) {
292 const FontGlyphPair& pair = new_pairs[i];
293 auto data = atlas.FindFontGlyphBounds(pair);
294 if (!data.has_value()) {
295 continue;
296 }
297 auto [pos, bounds, placeholder] = data.value();
298 FML_DCHECK(!placeholder);
299 Size size = pos.GetSize();
300 if (size.IsEmpty()) {
301 continue;
302 }
303
304 DrawGlyph(canvas, SkPoint::Make(pos.GetLeft(), pos.GetTop()),
305 pair.scaled_font, pair.glyph, bounds, pair.glyph.properties,
306 has_color);
307 }
308
309 // Writing to a malloc'd buffer and then copying to the staging buffers
310 // benchmarks as substantially faster on a number of Android devices.
311 BufferView buffer_view = host_buffer.Emplace(
312 bitmap.getAddr(0, 0),
313 texture->GetSize().Area() *
314 BytesPerPixelForPixelFormat(
315 atlas.GetTexture()->GetTextureDescriptor().format),
316 host_buffer.GetMinimumUniformAlignment());
317
318 return blit_pass->AddCopy(std::move(buffer_view), //
319 texture, //
320 IRect::MakeXYWH(0, 0, texture->GetSize().width,
321 texture->GetSize().height));
322 }
323
324 static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
325 std::shared_ptr<BlitPass>& blit_pass,
326 HostBuffer& host_buffer,
327 const std::shared_ptr<Texture>& texture,
328 const std::vector<FontGlyphPair>& new_pairs,
329 size_t start_index,
330 size_t end_index) {
331 TRACE_EVENT0("impeller", __FUNCTION__);
332
333 bool has_color = atlas.GetType() == GlyphAtlas::Type::kColorBitmap;
334
335 for (size_t i = start_index; i < end_index; i++) {
336 const FontGlyphPair& pair = new_pairs[i];
337 auto data = atlas.FindFontGlyphBounds(pair);
338 if (!data.has_value()) {
339 continue;
340 }
341 auto [pos, bounds, placeholder] = data.value();
342 FML_DCHECK(!placeholder);
343
344 Size size = pos.GetSize();
345 if (size.IsEmpty()) {
346 continue;
347 }
348 // The uploaded bitmap is expanded by 1px of padding
349 // on each side.
350 size.width += 2;
351 size.height += 2;
352
353 SkBitmap bitmap;
354 bitmap.setInfo(GetImageInfo(atlas, size));
355 if (!bitmap.tryAllocPixels()) {
356 return false;
357 }
358
359 auto surface = SkSurfaces::WrapPixels(bitmap.pixmap());
360 if (!surface) {
361 return false;
362 }
363 auto canvas = surface->getCanvas();
364 if (!canvas) {
365 return false;
366 }
367
368 DrawGlyph(canvas, SkPoint::Make(1, 1), pair.scaled_font, pair.glyph, bounds,
369 pair.glyph.properties, has_color);
370
371 // Writing to a malloc'd buffer and then copying to the staging buffers
372 // benchmarks as substantially faster on a number of Android devices.
373 BufferView buffer_view = host_buffer.Emplace(
374 bitmap.getAddr(0, 0),
375 size.Area() * BytesPerPixelForPixelFormat(
376 atlas.GetTexture()->GetTextureDescriptor().format),
377 host_buffer.GetMinimumUniformAlignment());
378
379 // convert_to_read is set to false so that the texture remains in a transfer
380 // dst layout until we finish writing to it below. This only has an impact
381 // on Vulkan where we are responsible for managing image layouts.
382 if (!blit_pass->AddCopy(std::move(buffer_view), //
383 texture, //
384 IRect::MakeXYWH(pos.GetLeft() - 1, pos.GetTop() - 1,
385 size.width, size.height), //
386 /*label=*/"", //
387 /*mip_level=*/0, //
388 /*slice=*/0, //
389 /*convert_to_read=*/false //
390 )) {
391 return false;
392 }
393 }
394 return blit_pass->ConvertTextureToShaderRead(texture);
395 }
396
397 static Rect ComputeGlyphSize(const SkFont& font,
398 const SubpixelGlyph& glyph,
399 Scalar scale) {
400 SkRect scaled_bounds;
401 SkPaint glyph_paint;
402 if (glyph.properties.has_value() && glyph.properties->stroke) {
403 glyph_paint.setStroke(true);
404 glyph_paint.setStrokeWidth(glyph.properties->stroke->width * scale);
405 glyph_paint.setStrokeCap(ToSkiaCap(glyph.properties->stroke->cap));
406 glyph_paint.setStrokeJoin(ToSkiaJoin(glyph.properties->stroke->join));
407 glyph_paint.setStrokeMiter(glyph.properties->stroke->miter_limit);
408 }
409 font.getBounds(&glyph.glyph.index, 1, &scaled_bounds, &glyph_paint);
410
411 // Expand the bounds of glyphs at subpixel offsets by 2 in the x direction.
412 Scalar adjustment = 0.0;
413 if (glyph.subpixel_offset != SubpixelPosition::kSubpixel00) {
414 adjustment = 1.0;
415 }
416 return Rect::MakeLTRB(scaled_bounds.fLeft - adjustment, scaled_bounds.fTop,
417 scaled_bounds.fRight + adjustment,
418 scaled_bounds.fBottom);
419 };
420
421 std::pair<std::vector<FontGlyphPair>, std::vector<Rect>>
422 TypographerContextSkia::CollectNewGlyphs(
423 const std::shared_ptr<GlyphAtlas>& atlas,
424 const std::vector<std::shared_ptr<TextFrame>>& text_frames) {
425 std::vector<FontGlyphPair> new_glyphs;
426 std::vector<Rect> glyph_sizes;
427 size_t generation_id = atlas->GetAtlasGeneration();
428 intptr_t atlas_id = reinterpret_cast<intptr_t>(atlas.get());
429 for (const auto& frame : text_frames) {
430 // TODO(jonahwilliams): determine how to re-enable this. See
431 // https://github.com/flutter/flutter/issues/163730 for example. This can
432 // happen when the Aiks/Typographer context are re-created, but the last
433 // DisplayList is re-used. The "atlas_id" check is not reliable, perhaps
434 // because it may end up with the same memory?
435 #if false
436 auto [frame_generation_id, frame_atlas_id] =
437 frame->GetAtlasGenerationAndID();
438 if (atlas->IsValid() && frame->IsFrameComplete() &&
439 frame_generation_id == generation_id && frame_atlas_id == atlas_id &&
440 !frame->GetFrameBounds(0).is_placeholder) {
441 continue;
442 }
443 #endif // false
444 frame->ClearFrameBounds();
445 frame->SetAtlasGeneration(generation_id, atlas_id);
446
447 for (const auto& run : frame->GetRuns()) {
448 auto metrics = run.GetFont().GetMetrics();
449
450 auto rounded_scale = TextFrame::RoundScaledFontSize(frame->GetScale());
451 ScaledFont scaled_font{.font = run.GetFont(), .scale = rounded_scale};
452
453 FontGlyphAtlas* font_glyph_atlas =
454 atlas->GetOrCreateFontGlyphAtlas(scaled_font);
455 FML_DCHECK(!!font_glyph_atlas);
456
457 SkFont sk_font(
458 TypefaceSkia::Cast(*scaled_font.font.GetTypeface()).GetSkiaTypeface(),
459 metrics.point_size, metrics.scaleX, metrics.skewX);
460 sk_font.setEdging(SkFont::Edging::kAntiAlias);
461 sk_font.setHinting(SkFontHinting::kSlight);
462 sk_font.setEmbolden(metrics.embolden);
463 // Rather than computing the bounds at the requested point size and
464 // scaling up the bounds, we scale up the font size and request the
465 // bounds. This seems to give more accurate bounds information.
466 sk_font.setSize(sk_font.getSize() *
467 static_cast<Scalar>(scaled_font.scale));
468 sk_font.setSubpixel(true);
469
470 for (const auto& glyph_position : run.GetGlyphPositions()) {
471 SubpixelPosition subpixel = TextFrame::ComputeSubpixelPosition(
472 glyph_position, scaled_font.font.GetAxisAlignment(),
473 frame->GetOffsetTransform());
474 SubpixelGlyph subpixel_glyph(glyph_position.glyph, subpixel,
475 frame->GetProperties());
476 const auto& font_glyph_bounds =
477 font_glyph_atlas->FindGlyphBounds(subpixel_glyph);
478
479 if (!font_glyph_bounds.has_value()) {
480 new_glyphs.push_back(FontGlyphPair{scaled_font, subpixel_glyph});
481 auto glyph_bounds = ComputeGlyphSize(
482 sk_font, subpixel_glyph, static_cast<Scalar>(scaled_font.scale));
483 glyph_sizes.push_back(glyph_bounds);
484
485 auto frame_bounds = FrameBounds{
486 Rect::MakeLTRB(0, 0, 0, 0), //
487 glyph_bounds, //
488 /*placeholder=*/true //
489 };
490
491 frame->AppendFrameBounds(frame_bounds);
492 font_glyph_atlas->AppendGlyph(subpixel_glyph, frame_bounds);
493 } else {
494 frame->AppendFrameBounds(font_glyph_bounds.value());
495 }
496 }
497 }
498 }
499 return {std::move(new_glyphs), std::move(glyph_sizes)};
500 }
501
502 std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
503 Context& context,
504 GlyphAtlas::Type type,
505 HostBuffer& host_buffer,
506 const std::shared_ptr<GlyphAtlasContext>& atlas_context,
507 const std::vector<std::shared_ptr<TextFrame>>& text_frames) const {
508 TRACE_EVENT0("impeller", __FUNCTION__);
509 if (!IsValid()) {
510 return nullptr;
511 }
512 std::shared_ptr<GlyphAtlas> last_atlas = atlas_context->GetGlyphAtlas();
513 FML_DCHECK(last_atlas->GetType() == type);
514
515 if (text_frames.empty()) {
516 return last_atlas;
517 }
518
519 // ---------------------------------------------------------------------------
520 // Step 1: Determine if the atlas type and font glyph pairs are compatible
521 // with the current atlas and reuse if possible. For each new font and
522 // glyph pair, compute the glyph size at scale.
523 // ---------------------------------------------------------------------------
524 auto [new_glyphs, glyph_sizes] = CollectNewGlyphs(last_atlas, text_frames);
525 if (new_glyphs.size() == 0) {
526 return last_atlas;
527 }
528
529 // ---------------------------------------------------------------------------
530 // Step 2: Determine if the additional missing glyphs can be appended to the
531 // existing bitmap without recreating the atlas.
532 // ---------------------------------------------------------------------------
533 std::vector<Rect> glyph_positions;
534 glyph_positions.reserve(new_glyphs.size());
535 size_t first_missing_index = 0;
536
537 if (last_atlas->GetTexture()) {
538 // Append all glyphs that fit into the current atlas.
539 first_missing_index = AppendToExistingAtlas(
540 last_atlas, new_glyphs, glyph_positions, glyph_sizes,
541 atlas_context->GetAtlasSize(), atlas_context->GetHeightAdjustment(),
542 atlas_context->GetRectPacker());
543
544 // ---------------------------------------------------------------------------
545 // Step 3a: Record the positions in the glyph atlas of the newly added
546 // glyphs.
547 // ---------------------------------------------------------------------------
548 for (size_t i = 0; i < first_missing_index; i++) {
549 last_atlas->AddTypefaceGlyphPositionAndBounds(
550 new_glyphs[i], glyph_positions[i], glyph_sizes[i]);
551 }
552
553 std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
554 std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
555
556 fml::ScopedCleanupClosure closure([&]() {
557 blit_pass->EncodeCommands();
558 if (!context.EnqueueCommandBuffer(std::move(cmd_buffer))) {
559 VALIDATION_LOG << "Failed to submit glyph atlas command buffer";
560 }
561 });
562
563 // ---------------------------------------------------------------------------
564 // Step 4a: Draw new font-glyph pairs into the a host buffer and encode
565 // the uploads into the blit pass.
566 // ---------------------------------------------------------------------------
567 if (!UpdateAtlasBitmap(*last_atlas, blit_pass, host_buffer,
568 last_atlas->GetTexture(), new_glyphs, 0,
569 first_missing_index)) {
570 return nullptr;
571 }
572
573 // If all glyphs fit, just return the old atlas.
574 if (first_missing_index == new_glyphs.size()) {
575 return last_atlas;
576 }
577 }
578
579 int64_t height_adjustment = atlas_context->GetAtlasSize().height;
580 const int64_t max_texture_height =
581 context.GetResourceAllocator()->GetMaxTextureSizeSupported().height;
582
583 // IF the current atlas size is as big as it can get, then "GC" and create an
584 // atlas with only the required glyphs. OpenGLES cannot reliably perform the
585 // blit required here, as 1) it requires attaching textures as read and write
586 // framebuffers which has substantially smaller size limits that max textures
587 // and 2) is missing a GLES 2.0 implementation and cap check.
588 bool blit_old_atlas = true;
589 std::shared_ptr<GlyphAtlas> new_atlas = last_atlas;
590 if (atlas_context->GetAtlasSize().height >= max_texture_height ||
591 context.GetBackendType() == Context::BackendType::kOpenGLES) {
592 blit_old_atlas = false;
593 new_atlas = std::make_shared<GlyphAtlas>(
594 type, /*initial_generation=*/last_atlas->GetAtlasGeneration() + 1);
595
596 auto [update_glyphs, update_sizes] =
597 CollectNewGlyphs(new_atlas, text_frames);
598 new_glyphs = std::move(update_glyphs);
599 glyph_sizes = std::move(update_sizes);
600
601 glyph_positions.clear();
602 glyph_positions.reserve(new_glyphs.size());
603 first_missing_index = 0;
604
605 height_adjustment = 0;
606 atlas_context->UpdateRectPacker(nullptr);
607 atlas_context->UpdateGlyphAtlas(new_atlas, {0, 0}, 0);
608 }
609
610 // A new glyph atlas must be created.
611 ISize atlas_size = ComputeNextAtlasSize(atlas_context, //
612 new_glyphs, //
613 glyph_positions, //
614 glyph_sizes, //
615 first_missing_index, //
616 max_texture_height //
617 );
618
619 atlas_context->UpdateGlyphAtlas(new_atlas, atlas_size, height_adjustment);
620 if (atlas_size.IsEmpty()) {
621 return nullptr;
622 }
623 FML_DCHECK(new_glyphs.size() == glyph_positions.size());
624
625 TextureDescriptor descriptor;
626 switch (type) {
627 case GlyphAtlas::Type::kAlphaBitmap:
628 descriptor.format =
629 context.GetCapabilities()->GetDefaultGlyphAtlasFormat();
630 break;
631 case GlyphAtlas::Type::kColorBitmap:
632 descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
633 break;
634 }
635 descriptor.size = atlas_size;
636 descriptor.storage_mode = StorageMode::kDevicePrivate;
637 descriptor.usage = TextureUsage::kShaderRead;
638 std::shared_ptr<Texture> new_texture =
639 context.GetResourceAllocator()->CreateTexture(descriptor);
640 if (!new_texture) {
641 return nullptr;
642 }
643
644 new_texture->SetLabel("GlyphAtlas");
645
646 std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
647 std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
648
649 fml::ScopedCleanupClosure closure([&]() {
650 blit_pass->EncodeCommands();
651 if (!context.EnqueueCommandBuffer(std::move(cmd_buffer))) {
652 VALIDATION_LOG << "Failed to submit glyph atlas command buffer";
653 }
654 });
655
656 // Now append all remaining glyphs. This should never have any missing data...
657 auto old_texture = new_atlas->GetTexture();
658 new_atlas->SetTexture(std::move(new_texture));
659
660 // ---------------------------------------------------------------------------
661 // Step 3a: Record the positions in the glyph atlas of the newly added
662 // glyphs.
663 // ---------------------------------------------------------------------------
664 for (size_t i = first_missing_index; i < glyph_positions.size(); i++) {
665 new_atlas->AddTypefaceGlyphPositionAndBounds(
666 new_glyphs[i], glyph_positions[i], glyph_sizes[i]);
667 }
668
669 // ---------------------------------------------------------------------------
670 // Step 4a: Draw new font-glyph pairs into the a host buffer and encode
671 // the uploads into the blit pass.
672 // ---------------------------------------------------------------------------
673 if (!BulkUpdateAtlasBitmap(*new_atlas, blit_pass, host_buffer,
674 new_atlas->GetTexture(), new_glyphs,
675 first_missing_index, new_glyphs.size())) {
676 return nullptr;
677 }
678
679 // Blit the old texture to the top left of the new atlas.
680 if (blit_old_atlas && old_texture) {
681 blit_pass->AddCopy(old_texture, new_atlas->GetTexture(),
682 IRect::MakeSize(new_atlas->GetTexture()->GetSize()),
683 {0, 0});
684 }
685
686 // ---------------------------------------------------------------------------
687 // Step 8b: Record the texture in the glyph atlas.
688 // ---------------------------------------------------------------------------
689
690 return new_atlas;
691 }
692
693 } // namespace impeller
allocator.h
type
GLenum type
Definition: blit_command_gles.cc:153
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:154
buffer_view.h
impeller::BackendCast< TypefaceSkia, Typeface >::Cast
static TypefaceSkia & Cast(Typeface &base)
Definition: backend_cast.h:13
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
impeller::Context::CreateCommandBuffer
virtual std::shared_ptr< CommandBuffer > CreateCommandBuffer() const =0
Create a new command buffer. Command buffers can be used to encode graphics, blit,...
impeller::Context::GetCapabilities
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
impeller::Context::GetBackendType
virtual BackendType GetBackendType() const =0
Get the graphics backend of an Impeller context.
impeller::Context::GetResourceAllocator
virtual std::shared_ptr< Allocator > GetResourceAllocator() const =0
Returns the allocator used to create textures and buffers on the device.
impeller::Context::BackendType::kOpenGLES
@ kOpenGLES
impeller::Context::EnqueueCommandBuffer
virtual bool EnqueueCommandBuffer(std::shared_ptr< CommandBuffer > command_buffer)
Enqueue command_buffer for submission by the end of the frame.
Definition: context.cc:33
impeller::Font::GetTypeface
const std::shared_ptr< Typeface > & GetTypeface() const
The typeface whose intrinsic properties this font modifies.
Definition: font.cc:27
impeller::Font::GetMetrics
const Metrics & GetMetrics() const
Definition: font.cc:45
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:37
impeller::GlyphAtlas::FindFontGlyphBounds
std::optional< FrameBounds > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
Definition: glyph_atlas.cc:87
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:41
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::GlyphAtlas::GetType
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:58
impeller::GlyphAtlas::GetTexture
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
impeller::HostBuffer
Definition: host_buffer.h:26
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:92
impeller::HostBuffer::GetMinimumUniformAlignment
size_t GetMinimumUniformAlignment() const
Retrieve the minimum uniform buffer alignment in bytes.
Definition: host_buffer.cc:241
impeller::RectanglePacker::Factory
static std::shared_ptr< RectanglePacker > Factory(int width, int height)
Return an empty packer with area specified by width and height.
Definition: rectangle_packer.cc:175
impeller::TextFrame::RoundScaledFontSize
static Rational RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:55
impeller::TextFrame::ComputeSubpixelPosition
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:94
impeller::TypefaceSkia::GetSkiaTypeface
const sk_sp< SkTypeface > & GetSkiaTypeface() const
Definition: typeface_skia.cc:31
impeller::TypographerContext::IsValid
virtual bool IsValid() const
Definition: typographer_context.cc:17
impeller::TypographerContextSkia::TypographerContextSkia
TypographerContextSkia()
impeller::TypographerContextSkia::~TypographerContextSkia
~TypographerContextSkia() override
impeller::TypographerContextSkia::CreateGlyphAtlasContext
std::shared_ptr< GlyphAtlasContext > CreateGlyphAtlasContext(GlyphAtlas::Type type) const override
Definition: typographer_context_skia.cc:85
impeller::TypographerContextSkia::Make
static std::shared_ptr< TypographerContext > Make()
Definition: typographer_context_skia.cc:76
impeller::TypographerContextSkia::CreateGlyphAtlas
std::shared_ptr< GlyphAtlas > CreateGlyphAtlas(Context &context, GlyphAtlas::Type type, HostBuffer &host_buffer, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::vector< std::shared_ptr< TextFrame >> &text_frames) const override
Definition: typographer_context_skia.cc:502
command_buffer.h
formats.h
font_glyph_pair.h
glyph.h
glyph_atlas.h
host_buffer.h
impeller
Definition: allocation.cc:12
impeller::BytesPerPixelForPixelFormat
constexpr size_t BytesPerPixelForPixelFormat(PixelFormat format)
Definition: formats.h:466
impeller::Join
Join
An enum that describes ways to join two segments of a path.
Definition: stroke_parameters.h:20
impeller::Join::kRound
@ kRound
impeller::Join::kMiter
@ kMiter
impeller::Join::kBevel
@ kBevel
impeller::ComputeGlyphSize
static Rect ComputeGlyphSize(const SkFont &font, const SubpixelGlyph &glyph, Scalar scale)
Definition: typographer_context_skia.cc:397
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::PairsFitInAtlasOfSize
static size_t PairsFitInAtlasOfSize(const std::vector< FontGlyphPair > &pairs, const ISize &atlas_size, std::vector< Rect > &glyph_positions, const std::vector< Rect > &glyph_sizes, int64_t height_adjustment, const std::shared_ptr< RectanglePacker > &rect_packer, size_t start_index)
Definition: typographer_context_skia.cc:136
impeller::DrawGlyph
static void DrawGlyph(SkCanvas *canvas, const SkPoint position, const ScaledFont &scaled_font, const SubpixelGlyph &glyph, const Rect &scaled_bounds, const std::optional< GlyphProperties > &prop, bool has_color)
Definition: typographer_context_skia.cc:211
impeller::SubpixelPositionToPoint
static Point SubpixelPositionToPoint(SubpixelPosition pos)
Definition: typographer_context_skia.cc:207
impeller::GetImageInfo
static SkImageInfo GetImageInfo(const GlyphAtlas &atlas, Size size)
Definition: typographer_context_skia.cc:89
impeller::BulkUpdateAtlasBitmap
static bool BulkUpdateAtlasBitmap(const GlyphAtlas &atlas, std::shared_ptr< BlitPass > &blit_pass, HostBuffer &host_buffer, const std::shared_ptr< Texture > &texture, const std::vector< FontGlyphPair > &new_pairs, size_t start_index, size_t end_index)
Batch render to a single surface.
Definition: typographer_context_skia.cc:265
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::StorageMode::kDevicePrivate
@ kDevicePrivate
impeller::Cap
Cap
An enum that describes ways to decorate the end of a path contour.
Definition: stroke_parameters.h:13
impeller::Cap::kRound
@ kRound
impeller::Cap::kButt
@ kButt
impeller::Cap::kSquare
@ kSquare
impeller::AppendToExistingAtlas
static size_t AppendToExistingAtlas(const std::shared_ptr< GlyphAtlas > &atlas, const std::vector< FontGlyphPair > &extra_pairs, std::vector< Rect > &glyph_positions, const std::vector< Rect > &glyph_sizes, ISize atlas_size, int64_t height_adjustment, const std::shared_ptr< RectanglePacker > &rect_packer)
Definition: typographer_context_skia.cc:102
impeller::ComputeNextAtlasSize
static ISize ComputeNextAtlasSize(const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::vector< FontGlyphPair > &extra_pairs, std::vector< Rect > &glyph_positions, const std::vector< Rect > &glyph_sizes, size_t glyph_index_start, int64_t max_texture_height)
Definition: typographer_context_skia.cc:166
impeller::PixelFormat::kR8G8B8A8UNormInt
@ kR8G8B8A8UNormInt
impeller::Size
TSize< Scalar > Size
Definition: size.h:159
impeller::SubpixelPosition
SubpixelPosition
Definition: font_glyph_pair.h:56
impeller::kSubpixel00
@ kSubpixel00
Definition: font_glyph_pair.h:58
impeller::ISize
ISize64 ISize
Definition: size.h:162
impeller::UpdateAtlasBitmap
static bool UpdateAtlasBitmap(const GlyphAtlas &atlas, std::shared_ptr< BlitPass > &blit_pass, HostBuffer &host_buffer, const std::shared_ptr< Texture > &texture, const std::vector< FontGlyphPair > &new_pairs, size_t start_index, size_t end_index)
Definition: typographer_context_skia.cc:324
impeller::kPadding
constexpr auto kPadding
Definition: typographer_context_skia.cc:48
impeller::TextureUsage::kShaderRead
@ kShaderRead
rect.h
rectangle_packer.h
render_pass.h
render_target.h
size.h
impeller::BufferView
Definition: buffer_view.h:19
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:137
impeller::FontGlyphPair::glyph
SubpixelGlyph glyph
Definition: font_glyph_pair.h:141
impeller::FontGlyphPair::scaled_font
ScaledFont scaled_font
Definition: font_glyph_pair.h:140
impeller::Glyph::index
uint16_t index
Definition: glyph.h:22
impeller::IPoint16
Definition: rectangle_packer.h:16
impeller::IPoint16::y
int16_t y() const
Definition: rectangle_packer.h:18
impeller::IPoint16::x
int16_t x() const
Definition: rectangle_packer.h:17
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:36
impeller::ScaledFont::scale
Rational scale
Definition: font_glyph_pair.h:38
impeller::ScaledFont::font
Font font
Definition: font_glyph_pair.h:37
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:85
impeller::SubpixelGlyph::properties
std::optional< GlyphProperties > properties
Definition: font_glyph_pair.h:88
impeller::SubpixelGlyph::subpixel_offset
SubpixelPosition subpixel_offset
Definition: font_glyph_pair.h:87
impeller::SubpixelGlyph::glyph
Glyph glyph
Definition: font_glyph_pair.h:86
impeller::TPoint< Scalar >
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::TRect< Scalar >
impeller::TRect::GetTop
constexpr auto GetTop() const
Definition: rect.h:357
impeller::TRect::GetLeft
constexpr auto GetLeft() const
Definition: rect.h:355
impeller::TRect< Scalar >::MakeXYWH
constexpr static TRect MakeXYWH(Type x, Type y, Type width, Type height)
Definition: rect.h:136
impeller::TRect::MakeSize
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150
impeller::TRect< Scalar >::MakeLTRB
constexpr static TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
Definition: rect.h:129
impeller::TSize< Scalar >
impeller::TSize::Area
constexpr Type Area() const
Definition: size.h:120
impeller::TSize::height
Type height
Definition: size.h:29
impeller::TSize::width
Type width
Definition: size.h:28
impeller::TSize::Ceil
constexpr TSize Ceil() const
Definition: size.h:114
impeller::TSize::IsEmpty
constexpr bool IsEmpty() const
Returns true if either of the width or height are 0, negative, or NaN.
Definition: size.h:123
impeller::TextureDescriptor
A lightweight object that describes the attributes of a texture that can then used an allocator to cr...
Definition: texture_descriptor.h:38
impeller::TextureDescriptor::usage
TextureUsageMask usage
Definition: texture_descriptor.h:44
impeller::TextureDescriptor::size
ISize size
Definition: texture_descriptor.h:42
impeller::TextureDescriptor::storage_mode
StorageMode storage_mode
Definition: texture_descriptor.h:39
impeller::TextureDescriptor::format
PixelFormat format
Definition: texture_descriptor.h:41
texture_descriptor.h
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
typeface_skia.h
typographer_context.h
typographer_context_skia.h
validation.h