Flutter Impeller
impeller::TypographerContextSkia Class Reference

#include <typographer_context_skia.h>

Inheritance diagram for impeller::TypographerContextSkia:
impeller::TypographerContext

Public Member Functions

 TypographerContextSkia ()
 
 ~TypographerContextSkia () override
 
std::shared_ptr< GlyphAtlasContextCreateGlyphAtlasContext (GlyphAtlas::Type type) const override
 
std::shared_ptr< GlyphAtlasCreateGlyphAtlas (Context &context, GlyphAtlas::Type type, HostBuffer &host_buffer, const std::shared_ptr< GlyphAtlasContext > &atlas_context, const std::vector< RenderableText > &renderable_texts) const override
 
- Public Member Functions inherited from impeller::TypographerContext
virtual ~TypographerContext ()
 
virtual bool IsValid () const
 

Static Public Member Functions

static std::shared_ptr< TypographerContextMake ()
 

Additional Inherited Members

- Protected Member Functions inherited from impeller::TypographerContext
 TypographerContext ()
 Create a new context to render text that talks to an underlying graphics context. More...
 

Detailed Description

Definition at line 12 of file typographer_context_skia.h.

Constructor & Destructor Documentation

◆ TypographerContextSkia()

impeller::TypographerContextSkia::TypographerContextSkia ( )
default

◆ ~TypographerContextSkia()

impeller::TypographerContextSkia::~TypographerContextSkia ( )
overridedefault

Member Function Documentation

◆ CreateGlyphAtlas()

std::shared_ptr< GlyphAtlas > impeller::TypographerContextSkia::CreateGlyphAtlas ( Context context,
GlyphAtlas::Type  type,
HostBuffer host_buffer,
const std::shared_ptr< GlyphAtlasContext > &  atlas_context,
const std::vector< RenderableText > &  renderable_texts 
) const
overridevirtual

Implements impeller::TypographerContext.

Definition at line 482 of file typographer_context_skia.cc.

487  {
488  TRACE_EVENT0("impeller", __FUNCTION__);
489  if (!IsValid()) {
490  return nullptr;
491  }
492  std::shared_ptr<GlyphAtlas> last_atlas = atlas_context->GetGlyphAtlas();
493  FML_DCHECK(last_atlas->GetType() == type);
494 
495  if (renderable_texts.empty()) {
496  return last_atlas;
497  }
498 
499  // ---------------------------------------------------------------------------
500  // Step 1: Determine if the atlas type and font glyph pairs are compatible
501  // with the current atlas and reuse if possible. For each new font and
502  // glyph pair, compute the glyph size at scale.
503  // ---------------------------------------------------------------------------
504  auto [new_glyphs, glyph_sizes] =
505  CollectNewGlyphs(last_atlas, renderable_texts);
506  if (new_glyphs.size() == 0) {
507  return last_atlas;
508  }
509 
510  // ---------------------------------------------------------------------------
511  // Step 2: Determine if the additional missing glyphs can be appended to the
512  // existing bitmap without recreating the atlas.
513  // ---------------------------------------------------------------------------
514  std::vector<Rect> glyph_positions;
515  glyph_positions.reserve(new_glyphs.size());
516  size_t first_missing_index = 0;
517 
518  if (last_atlas->GetTexture()) {
519  // Append all glyphs that fit into the current atlas.
520  first_missing_index = AppendToExistingAtlas(
521  last_atlas, new_glyphs, glyph_positions, glyph_sizes,
522  atlas_context->GetAtlasSize(), atlas_context->GetHeightAdjustment(),
523  atlas_context->GetRectPacker());
524 
525  // ---------------------------------------------------------------------------
526  // Step 3a: Record the positions in the glyph atlas of the newly added
527  // glyphs.
528  // ---------------------------------------------------------------------------
529  for (size_t i = 0; i < first_missing_index; i++) {
530  last_atlas->AddTypefaceGlyphPositionAndBounds(
531  new_glyphs[i], glyph_positions[i], glyph_sizes[i]);
532  }
533 
534  std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
535  std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
536 
537  fml::ScopedCleanupClosure closure([&]() {
538  blit_pass->EncodeCommands();
539  if (!context.EnqueueCommandBuffer(std::move(cmd_buffer))) {
540  VALIDATION_LOG << "Failed to submit glyph atlas command buffer";
541  }
542  });
543 
544  // ---------------------------------------------------------------------------
545  // Step 4a: Draw new font-glyph pairs into the a host buffer and encode
546  // the uploads into the blit pass.
547  // ---------------------------------------------------------------------------
548  if (!UpdateAtlasBitmap(*last_atlas, blit_pass, data_host_buffer,
549  last_atlas->GetTexture(), new_glyphs, 0,
550  first_missing_index)) {
551  return nullptr;
552  }
553 
554  // If all glyphs fit, just return the old atlas.
555  if (first_missing_index == new_glyphs.size()) {
556  return last_atlas;
557  }
558  }
559 
560  int64_t height_adjustment = atlas_context->GetAtlasSize().height;
561  const int64_t max_texture_height =
562  context.GetResourceAllocator()->GetMaxTextureSizeSupported().height;
563 
564  // IF the current atlas size is as big as it can get, then "GC" and create an
565  // atlas with only the required glyphs. OpenGLES cannot reliably perform the
566  // blit required here, as 1) it requires attaching textures as read and write
567  // framebuffers which has substantially smaller size limits that max textures
568  // and 2) is missing a GLES 2.0 implementation and cap check.
569  bool blit_old_atlas = true;
570  std::shared_ptr<GlyphAtlas> new_atlas = last_atlas;
571  if (atlas_context->GetAtlasSize().height >= max_texture_height ||
572  context.GetBackendType() == Context::BackendType::kOpenGLES) {
573  blit_old_atlas = false;
574  new_atlas = std::make_shared<GlyphAtlas>(
575  type, /*initial_generation=*/last_atlas->GetAtlasGeneration() + 1);
576 
577  auto [update_glyphs, update_sizes] =
578  CollectNewGlyphs(new_atlas, renderable_texts);
579  new_glyphs = std::move(update_glyphs);
580  glyph_sizes = std::move(update_sizes);
581 
582  glyph_positions.clear();
583  glyph_positions.reserve(new_glyphs.size());
584  first_missing_index = 0;
585 
586  height_adjustment = 0;
587  atlas_context->UpdateRectPacker(nullptr);
588  atlas_context->UpdateGlyphAtlas(new_atlas, {0, 0}, 0);
589  }
590 
591  // A new glyph atlas must be created.
592  ISize atlas_size = ComputeNextAtlasSize(atlas_context, //
593  new_glyphs, //
594  glyph_positions, //
595  glyph_sizes, //
596  first_missing_index, //
597  max_texture_height //
598  );
599 
600  atlas_context->UpdateGlyphAtlas(new_atlas, atlas_size, height_adjustment);
601  if (atlas_size.IsEmpty()) {
602  return nullptr;
603  }
604  FML_DCHECK(new_glyphs.size() == glyph_positions.size());
605 
606  TextureDescriptor descriptor;
607  switch (type) {
609  descriptor.format =
610  context.GetCapabilities()->GetDefaultGlyphAtlasFormat();
611  break;
613  descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
614  break;
615  }
616  descriptor.size = atlas_size;
617  descriptor.storage_mode = StorageMode::kDevicePrivate;
618  descriptor.usage = TextureUsage::kShaderRead;
619  std::shared_ptr<Texture> new_texture =
620  context.GetResourceAllocator()->CreateTexture(descriptor);
621  if (!new_texture) {
622  return nullptr;
623  }
624 
625  new_texture->SetLabel("GlyphAtlas");
626 
627  std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
628  std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
629 
630  fml::ScopedCleanupClosure closure([&]() {
631  blit_pass->EncodeCommands();
632  if (!context.EnqueueCommandBuffer(std::move(cmd_buffer))) {
633  VALIDATION_LOG << "Failed to submit glyph atlas command buffer";
634  }
635  });
636 
637  // Now append all remaining glyphs. This should never have any missing data...
638  auto old_texture = new_atlas->GetTexture();
639  new_atlas->SetTexture(std::move(new_texture));
640 
641  // ---------------------------------------------------------------------------
642  // Step 3a: Record the positions in the glyph atlas of the newly added
643  // glyphs.
644  // ---------------------------------------------------------------------------
645  for (size_t i = first_missing_index; i < glyph_positions.size(); i++) {
646  new_atlas->AddTypefaceGlyphPositionAndBounds(
647  new_glyphs[i], glyph_positions[i], glyph_sizes[i]);
648  }
649 
650  // ---------------------------------------------------------------------------
651  // Step 4a: Draw new font-glyph pairs into the a host buffer and encode
652  // the uploads into the blit pass.
653  // ---------------------------------------------------------------------------
654  if (!BulkUpdateAtlasBitmap(*new_atlas, blit_pass, data_host_buffer,
655  new_atlas->GetTexture(), new_glyphs,
656  first_missing_index, new_glyphs.size())) {
657  return nullptr;
658  }
659 
660  // Blit the old texture to the top left of the new atlas.
661  if (blit_old_atlas && old_texture) {
662  blit_pass->AddCopy(old_texture, new_atlas->GetTexture(),
663  IRect::MakeSize(new_atlas->GetTexture()->GetSize()),
664  {0, 0});
665  }
666 
667  // ---------------------------------------------------------------------------
668  // Step 8b: Record the texture in the glyph atlas.
669  // ---------------------------------------------------------------------------
670 
671  return new_atlas;
672 }
static bool UpdateAtlasBitmap(const GlyphAtlas &atlas, std::shared_ptr< BlitPass > &blit_pass, HostBuffer &data_host_buffer, const std::shared_ptr< Texture > &texture, const std::vector< FontGlyphPair > &new_pairs, size_t start_index, size_t end_index)
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)
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)
static bool BulkUpdateAtlasBitmap(const GlyphAtlas &atlas, std::shared_ptr< BlitPass > &blit_pass, HostBuffer &data_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.
ISize64 ISize
Definition: size.h:162
constexpr static TRect MakeSize(const TSize< U > &size)
Definition: rect.h:150

References impeller::AppendToExistingAtlas(), impeller::BulkUpdateAtlasBitmap(), impeller::ComputeNextAtlasSize(), impeller::Context::CreateCommandBuffer(), impeller::Context::EnqueueCommandBuffer(), impeller::TextureDescriptor::format, impeller::Context::GetBackendType(), impeller::Context::GetCapabilities(), impeller::Context::GetResourceAllocator(), impeller::TSize< T >::IsEmpty(), impeller::TypographerContext::IsValid(), impeller::GlyphAtlas::kAlphaBitmap, impeller::GlyphAtlas::kColorBitmap, impeller::kDevicePrivate, impeller::Context::kOpenGLES, impeller::kR8G8B8A8UNormInt, impeller::kShaderRead, impeller::TRect< T >::MakeSize(), impeller::TextureDescriptor::size, impeller::TextureDescriptor::storage_mode, impeller::UpdateAtlasBitmap(), and impeller::TextureDescriptor::usage.

◆ CreateGlyphAtlasContext()

std::shared_ptr< GlyphAtlasContext > impeller::TypographerContextSkia::CreateGlyphAtlasContext ( GlyphAtlas::Type  type) const
overridevirtual

Implements impeller::TypographerContext.

Definition at line 85 of file typographer_context_skia.cc.

85  {
86  return std::make_shared<GlyphAtlasContext>(type);
87 }

◆ Make()

std::shared_ptr< TypographerContext > impeller::TypographerContextSkia::Make ( )
static

Definition at line 76 of file typographer_context_skia.cc.

76  {
77  return std::make_shared<TypographerContextSkia>();
78 }

Referenced by impeller::DlPlayground::OpenPlaygroundHere(), and impeller::testing::TEST_P().


The documentation for this class was generated from the following files: