From 578e769483759562ba7f5908e61d2880f1f56e70 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Fri, 10 May 2024 14:39:47 -0700 Subject: [PATCH] [Impeller] treat glyph atlas texture as source of truth, remove copy of SkBitmap. (flutter/engine#52567) Work towards part of https://github.com/flutter/flutter/issues/138798 Allow updating single glyphs in the glyph atlas, without replacing the entire bitmap. Required to efficiently append/update to large atlases. --- .../ci/licenses_golden/licenses_flutter | 4 - .../impeller/entity/contents/text_contents.cc | 2 +- .../backend/gles/blit_command_gles.cc | 1 + .../renderer/backend/gles/proc_table_gles.h | 1 + .../renderer/backend/gles/texture_gles.cc | 1 + .../backend/metal/blit_command_mtl.mm | 4 +- .../flutter/impeller/renderer/blit_pass.cc | 4 +- .../typographer/backends/skia/BUILD.gn | 2 - .../backends/skia/glyph_atlas_context_skia.cc | 23 -- .../backends/skia/glyph_atlas_context_skia.h | 42 --- .../backends/skia/typographer_context_skia.cc | 350 ++++++++---------- .../backends/skia/typographer_context_skia.h | 4 +- .../backends/stb/glyph_atlas_context_stb.cc | 3 +- .../backends/stb/glyph_atlas_context_stb.h | 2 +- .../backends/stb/typographer_context_stb.cc | 5 +- .../backends/stb/typographer_context_stb.h | 5 +- .../impeller/typographer/glyph_atlas.cc | 5 +- .../impeller/typographer/glyph_atlas.h | 6 +- .../impeller/typographer/lazy_glyph_atlas.cc | 9 +- .../impeller/typographer/lazy_glyph_atlas.h | 1 + .../typographer/typographer_context.h | 8 +- .../typographer/typographer_unittests.cc | 129 +++---- engine/src/flutter/lib/gpu/shader_library.cc | 1 + engine/src/flutter/shell/common/shell.h | 1 + 24 files changed, 260 insertions(+), 353 deletions(-) delete mode 100644 engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc delete mode 100644 engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index b8c15596ed..c0b161943c 100644 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -40915,8 +40915,6 @@ ORIGIN: ../../../flutter/impeller/toolkit/egl/surface.cc + ../../../flutter/LICE ORIGIN: ../../../flutter/impeller/toolkit/egl/surface.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/toolkit/gles/gles.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/toolkit/gles/texture.h + ../../../flutter/LICENSE -ORIGIN: ../../../flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc + ../../../flutter/LICENSE -ORIGIN: ../../../flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/typographer/backends/skia/text_frame_skia.cc + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/typographer/backends/skia/text_frame_skia.h + ../../../flutter/LICENSE ORIGIN: ../../../flutter/impeller/typographer/backends/skia/typeface_skia.cc + ../../../flutter/LICENSE @@ -43801,8 +43799,6 @@ FILE: ../../../flutter/impeller/toolkit/gles/gles.h FILE: ../../../flutter/impeller/toolkit/gles/texture.cc FILE: ../../../flutter/impeller/toolkit/gles/texture.h FILE: ../../../flutter/impeller/tools/malioc.json -FILE: ../../../flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc -FILE: ../../../flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h FILE: ../../../flutter/impeller/typographer/backends/skia/text_frame_skia.cc FILE: ../../../flutter/impeller/typographer/backends/skia/text_frame_skia.h FILE: ../../../flutter/impeller/typographer/backends/skia/typeface_skia.cc diff --git a/engine/src/flutter/impeller/entity/contents/text_contents.cc b/engine/src/flutter/impeller/entity/contents/text_contents.cc index 96cbfd7935..645d0c46ac 100644 --- a/engine/src/flutter/impeller/entity/contents/text_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/text_contents.cc @@ -73,7 +73,7 @@ bool TextContents::Render(const ContentContext& renderer, auto type = frame_->GetAtlasType(); const std::shared_ptr& atlas = renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas( - *renderer.GetContext(), type); + *renderer.GetContext(), renderer.GetTransientsBuffer(), type); if (!atlas || !atlas->IsValid()) { VALIDATION_LOG << "Cannot render glyphs without prepared atlas."; diff --git a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc index 2d104449a0..c7a5de96fa 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/blit_command_gles.cc @@ -284,6 +284,7 @@ bool BlitCopyBufferToTextureCommandGLES::Encode( { TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes", std::to_string(data.buffer_view.range.length).c_str()); + gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1); gl.TexSubImage2D(texture_target, // target 0u, // LOD level destination_region.GetX(), // xoffset diff --git a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h index 0ea7ba2a12..44587ae96a 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h +++ b/engine/src/flutter/impeller/renderer/backend/gles/proc_table_gles.h @@ -162,6 +162,7 @@ struct GLProc { PROC(IsShader); \ PROC(IsTexture); \ PROC(LinkProgram); \ + PROC(PixelStorei); \ PROC(RenderbufferStorage); \ PROC(Scissor); \ PROC(ShaderBinary); \ diff --git a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc index 96939b0676..7df092cee3 100644 --- a/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc +++ b/engine/src/flutter/impeller/renderer/backend/gles/texture_gles.cc @@ -291,6 +291,7 @@ bool TextureGLES::OnSetContents(std::shared_ptr mapping, { TRACE_EVENT1("impeller", "TexImage2DUpload", "Bytes", std::to_string(data->data->GetSize()).c_str()); + gl.PixelStorei(GL_UNPACK_ALIGNMENT, 1); gl.TexImage2D(texture_target, // target 0u, // LOD level data->internal_format, // internal format diff --git a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm index ca73897cdb..16ebf4441d 100644 --- a/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm +++ b/engine/src/flutter/impeller/renderer/backend/metal/blit_command_mtl.mm @@ -120,12 +120,12 @@ bool BlitCopyBufferToTextureCommandMTL::Encode( auto destination_bytes_per_pixel = BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format); - auto destination_bytes_per_row = + auto source_bytes_per_row = destination_region.GetWidth() * destination_bytes_per_pixel; [encoder copyFromBuffer:source_mtl sourceOffset:source.range.offset - sourceBytesPerRow:destination_bytes_per_row + sourceBytesPerRow:source_bytes_per_row sourceBytesPerImage: 0 // 0 for 2D textures according to // https://developer.apple.com/documentation/metal/mtlblitcommandencoder/1400752-copyfrombuffer diff --git a/engine/src/flutter/impeller/renderer/blit_pass.cc b/engine/src/flutter/impeller/renderer/blit_pass.cc index cd9511d56a..e215b05e8d 100644 --- a/engine/src/flutter/impeller/renderer/blit_pass.cc +++ b/engine/src/flutter/impeller/renderer/blit_pass.cc @@ -142,9 +142,9 @@ bool BlitPass::AddCopy(BufferView source, auto bytes_per_pixel = BytesPerPixelForPixelFormat(destination->GetTextureDescriptor().format); - auto bytes_per_image = destination_region_value.Area() * bytes_per_pixel; + auto bytes_per_region = destination_region_value.Area() * bytes_per_pixel; - if (source.range.length != bytes_per_image) { + if (source.range.length != bytes_per_region) { VALIDATION_LOG << "Attempted to add a texture blit with out of bounds access."; return false; diff --git a/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn b/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn index 778df39e99..cf344bff3d 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn +++ b/engine/src/flutter/impeller/typographer/backends/skia/BUILD.gn @@ -6,8 +6,6 @@ import("//flutter/impeller/tools/impeller.gni") impeller_component("typographer_skia_backend") { sources = [ - "glyph_atlas_context_skia.cc", - "glyph_atlas_context_skia.h", "text_frame_skia.cc", "text_frame_skia.h", "typeface_skia.cc", diff --git a/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc deleted file mode 100644 index 78826736d8..0000000000 --- a/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.cc +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "impeller/typographer/backends/skia/glyph_atlas_context_skia.h" - -#include "third_party/skia/include/core/SkBitmap.h" - -namespace impeller { - -GlyphAtlasContextSkia::GlyphAtlasContextSkia() = default; - -GlyphAtlasContextSkia::~GlyphAtlasContextSkia() = default; - -std::shared_ptr GlyphAtlasContextSkia::GetBitmap() const { - return bitmap_; -} - -void GlyphAtlasContextSkia::UpdateBitmap(std::shared_ptr bitmap) { - bitmap_ = std::move(bitmap); -} - -} // namespace impeller diff --git a/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h b/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h deleted file mode 100644 index 7f59eca618..0000000000 --- a/engine/src/flutter/impeller/typographer/backends/skia/glyph_atlas_context_skia.h +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright 2013 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_GLYPH_ATLAS_CONTEXT_SKIA_H_ -#define FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_GLYPH_ATLAS_CONTEXT_SKIA_H_ - -#include "impeller/base/backend_cast.h" -#include "impeller/typographer/glyph_atlas.h" - -class SkBitmap; - -namespace impeller { - -//------------------------------------------------------------------------------ -/// @brief A container for caching a glyph atlas across frames. -/// -class GlyphAtlasContextSkia - : public GlyphAtlasContext, - public BackendCast { - public: - GlyphAtlasContextSkia(); - - ~GlyphAtlasContextSkia() override; - - //---------------------------------------------------------------------------- - /// @brief Retrieve the previous (if any) SkBitmap instance. - std::shared_ptr GetBitmap() const; - - void UpdateBitmap(std::shared_ptr bitmap); - - private: - std::shared_ptr bitmap_; - - GlyphAtlasContextSkia(const GlyphAtlasContextSkia&) = delete; - - GlyphAtlasContextSkia& operator=(const GlyphAtlasContextSkia&) = delete; -}; - -} // namespace impeller - -#endif // FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_GLYPH_ATLAS_CONTEXT_SKIA_H_ diff --git a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc index 36c6af3076..25f36c73bc 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc +++ b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.cc @@ -4,23 +4,33 @@ #include "impeller/typographer/backends/skia/typographer_context_skia.h" +#include #include #include #include "flutter/fml/logging.h" #include "flutter/fml/trace_event.h" #include "fml/closure.h" -#include "fml/mapping.h" + #include "impeller/base/allocation.h" #include "impeller/core/allocator.h" +#include "impeller/core/buffer_view.h" +#include "impeller/core/formats.h" +#include "impeller/core/host_buffer.h" +#include "impeller/core/platform.h" +#include "impeller/core/texture_descriptor.h" #include "impeller/renderer/command_buffer.h" -#include "impeller/typographer/backends/skia/glyph_atlas_context_skia.h" +#include "impeller/renderer/render_pass.h" +#include "impeller/renderer/render_target.h" #include "impeller/typographer/backends/skia/typeface_skia.h" #include "impeller/typographer/glyph_atlas.h" #include "impeller/typographer/rectangle_packer.h" #include "impeller/typographer/typographer_context.h" #include "include/core/SkColor.h" +#include "include/core/SkImageInfo.h" +#include "include/core/SkPixelRef.h" #include "include/core/SkSize.h" + #include "third_party/skia/include/core/SkBitmap.h" #include "third_party/skia/include/core/SkCanvas.h" #include "third_party/skia/include/core/SkFont.h" @@ -33,6 +43,56 @@ namespace impeller { // https://github.com/flutter/flutter/issues/114563 constexpr auto kPadding = 2; +namespace { + +class HostBufferAllocator : public SkBitmap::Allocator { + public: + explicit HostBufferAllocator(HostBuffer& host_buffer) + : host_buffer_(host_buffer) {} + + [[nodiscard]] BufferView TakeBufferView() { + buffer_view_.buffer->Flush(); + return std::move(buffer_view_); + } + + // |SkBitmap::Allocator| + bool allocPixelRef(SkBitmap* bitmap) override { + if (!bitmap) { + return false; + } + const SkImageInfo& info = bitmap->info(); + if (kUnknown_SkColorType == info.colorType() || info.width() < 0 || + info.height() < 0 || !info.validRowBytes(bitmap->rowBytes())) { + return false; + } + + size_t required_bytes = bitmap->rowBytes() * bitmap->height(); + BufferView buffer_view = host_buffer_.Emplace(nullptr, required_bytes, + DefaultUniformAlignment()); + + // The impeller host buffer is not cleared between frames and may contain + // stale data. The Skia software canvas does not write to pixels without + // any contents, which causes this data to leak through. + ::memset(buffer_view.buffer->OnGetContents() + buffer_view.range.offset, 0, + required_bytes); + + auto pixel_ref = sk_sp(new SkPixelRef( + info.width(), info.height(), + buffer_view.buffer->OnGetContents() + buffer_view.range.offset, + bitmap->rowBytes())); + + bitmap->setPixelRef(std::move(pixel_ref), 0, 0); + buffer_view_ = std::move(buffer_view); + return true; + } + + private: + BufferView buffer_view_; + HostBuffer& host_buffer_; +}; + +} // namespace + std::shared_ptr TypographerContextSkia::Make() { return std::make_shared(); } @@ -42,8 +102,19 @@ TypographerContextSkia::TypographerContextSkia() = default; TypographerContextSkia::~TypographerContextSkia() = default; std::shared_ptr -TypographerContextSkia::CreateGlyphAtlasContext() const { - return std::make_shared(); +TypographerContextSkia::CreateGlyphAtlasContext(GlyphAtlas::Type type) const { + return std::make_shared(type); +} + +static SkImageInfo GetImageInfo(const GlyphAtlas& atlas, Size size) { + switch (atlas.GetType()) { + case GlyphAtlas::Type::kAlphaBitmap: + return SkImageInfo::MakeA8(SkISize{static_cast(size.width), + static_cast(size.height)}); + case GlyphAtlas::Type::kColorBitmap: + return SkImageInfo::MakeN32Premul(size.width, size.height); + } + FML_UNREACHABLE(); } static size_t PairsFitInAtlasOfSize( @@ -161,11 +232,9 @@ static ISize OptimumAtlasSizeForFontGlyphPairs( static void DrawGlyph(SkCanvas* canvas, const ScaledFont& scaled_font, const Glyph& glyph, - const Rect& location, bool has_color) { const auto& metrics = scaled_font.font.GetMetrics(); - const auto position = SkPoint::Make(location.GetX() / scaled_font.scale, - location.GetY() / scaled_font.scale); + const auto position = SkPoint::Make(0, 0); SkGlyphID glyph_id = glyph.index; SkFont sk_font( @@ -181,30 +250,23 @@ static void DrawGlyph(SkCanvas* canvas, glyph_paint.setColor(glyph_color); canvas->resetMatrix(); canvas->scale(scaled_font.scale, scaled_font.scale); - canvas->drawGlyphs(1u, // count - &glyph_id, // glyphs - &position, // positions - SkPoint::Make(-glyph.bounds.GetLeft(), - -glyph.bounds.GetTop()), // origin - sk_font, // font - glyph_paint // paint + + canvas->drawGlyphs( + 1u, // count + &glyph_id, // glyphs + &position, // positions + SkPoint::Make(-glyph.bounds.GetLeft(), -glyph.bounds.GetTop()), // origin + sk_font, // font + glyph_paint // paint ); } static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, - const std::shared_ptr& bitmap, + std::shared_ptr& blit_pass, + HostBuffer& host_buffer, + const std::shared_ptr& texture, const std::vector& new_pairs) { TRACE_EVENT0("impeller", __FUNCTION__); - FML_DCHECK(bitmap != nullptr); - - auto surface = SkSurfaces::WrapPixels(bitmap->pixmap()); - if (!surface) { - return false; - } - auto canvas = surface->getCanvas(); - if (!canvas) { - return false; - } bool has_color = atlas.GetType() == GlyphAtlas::Type::kColorBitmap; @@ -213,134 +275,49 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas, if (!pos.has_value()) { continue; } - DrawGlyph(canvas, pair.scaled_font, pair.glyph, pos.value(), has_color); + Size size = pos->GetSize(); + if (size.IsEmpty()) { + continue; + } + + SkBitmap bitmap; + HostBufferAllocator allocator(host_buffer); + bitmap.setInfo(GetImageInfo(atlas, size)); + if (!bitmap.tryAllocPixels(&allocator)) { + return false; + } + auto surface = SkSurfaces::WrapPixels(bitmap.pixmap()); + if (!surface) { + return false; + } + auto canvas = surface->getCanvas(); + if (!canvas) { + return false; + } + + DrawGlyph(canvas, pair.scaled_font, pair.glyph, has_color); + + if (!blit_pass->AddCopy(allocator.TakeBufferView(), texture, + IRect::MakeXYWH(pos->GetLeft(), pos->GetTop(), + size.width, size.height))) { + return false; + } } return true; } -static std::shared_ptr CreateAtlasBitmap(const GlyphAtlas& atlas, - const ISize& atlas_size) { - TRACE_EVENT0("impeller", __FUNCTION__); - auto bitmap = std::make_shared(); - SkImageInfo image_info; - - switch (atlas.GetType()) { - case GlyphAtlas::Type::kAlphaBitmap: - image_info = - SkImageInfo::MakeA8(SkISize{static_cast(atlas_size.width), - static_cast(atlas_size.height)}); - break; - case GlyphAtlas::Type::kColorBitmap: - image_info = - SkImageInfo::MakeN32Premul(atlas_size.width, atlas_size.height); - break; - } - - if (!bitmap->tryAllocPixels(image_info)) { - return nullptr; - } - - auto surface = SkSurfaces::WrapPixels(bitmap->pixmap()); - if (!surface) { - return nullptr; - } - auto canvas = surface->getCanvas(); - if (!canvas) { - return nullptr; - } - - bool has_color = atlas.GetType() == GlyphAtlas::Type::kColorBitmap; - - atlas.IterateGlyphs([canvas, has_color](const ScaledFont& scaled_font, - const Glyph& glyph, - const Rect& location) -> bool { - DrawGlyph(canvas, scaled_font, glyph, location, has_color); - return true; - }); - - return bitmap; -} - -static bool UpdateGlyphTextureAtlas( - std::shared_ptr bitmap, - const std::shared_ptr& allocator, - const std::shared_ptr& texture, - const std::shared_ptr& blit_pass) { - TRACE_EVENT0("impeller", __FUNCTION__); - - FML_DCHECK(bitmap != nullptr); - auto texture_descriptor = texture->GetTextureDescriptor(); - - auto mapping = std::make_shared( - reinterpret_cast(bitmap->getAddr(0, 0)), // data - texture_descriptor.GetByteSizeOfBaseMipLevel(), // size - [bitmap](auto, auto) mutable { bitmap.reset(); } // proc - ); - - std::shared_ptr device_buffer = - allocator->CreateBufferWithCopy(*mapping); - blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)), - texture); - return blit_pass->EncodeCommands(allocator); -} - -static std::shared_ptr UploadGlyphTextureAtlas( - const std::shared_ptr& allocator, - std::shared_ptr bitmap, - const std::shared_ptr& blit_pass, - const ISize& atlas_size, - PixelFormat format) { - TRACE_EVENT0("impeller", __FUNCTION__); - if (!allocator) { - return nullptr; - } - - FML_DCHECK(bitmap != nullptr); - const auto& pixmap = bitmap->pixmap(); - - TextureDescriptor texture_descriptor; - texture_descriptor.storage_mode = StorageMode::kDevicePrivate; - texture_descriptor.format = format; - texture_descriptor.size = atlas_size; - - if (pixmap.rowBytes() * pixmap.height() != - texture_descriptor.GetByteSizeOfBaseMipLevel()) { - return nullptr; - } - - std::shared_ptr texture = - allocator->CreateTexture(texture_descriptor); - if (!texture || !texture->IsValid()) { - return nullptr; - } - texture->SetLabel("GlyphAtlas"); - - std::shared_ptr mapping = - std::make_shared( - reinterpret_cast(bitmap->getAddr(0, 0)), // data - texture_descriptor.GetByteSizeOfBaseMipLevel(), // size - [bitmap](auto, auto) mutable { bitmap.reset(); } // proc - ); - std::shared_ptr device_buffer = - allocator->CreateBufferWithCopy(*mapping); - blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)), - texture); - blit_pass->EncodeCommands(allocator); - - return texture; -} - std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( Context& context, GlyphAtlas::Type type, + HostBuffer& host_buffer, const std::shared_ptr& atlas_context, const FontGlyphMap& font_glyph_map) const { TRACE_EVENT0("impeller", __FUNCTION__); if (!IsValid()) { return nullptr; } - auto& atlas_context_skia = GlyphAtlasContextSkia::Cast(*atlas_context); std::shared_ptr last_atlas = atlas_context->GetGlyphAtlas(); + FML_DCHECK(last_atlas->GetType() == type); if (font_glyph_map.empty()) { return last_atlas; @@ -348,7 +325,8 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( std::shared_ptr cmd_buffer = context.CreateCommandBuffer(); std::shared_ptr blit_pass = cmd_buffer->CreateBlitPass(); - fml::ScopedCleanupClosure closure([&cmd_buffer, &context]() { + fml::ScopedCleanupClosure closure([&]() { + blit_pass->EncodeCommands(context.GetResourceAllocator()); context.GetCommandQueue()->Submit({std::move(cmd_buffer)}); }); @@ -373,7 +351,7 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( } } } - if (last_atlas->GetType() == type && new_glyphs.size() == 0) { + if (new_glyphs.size() == 0) { return last_atlas; } @@ -383,8 +361,7 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( // the type is identical. // --------------------------------------------------------------------------- std::vector glyph_positions; - if (last_atlas->GetType() == type && - CanAppendToExistingAtlas(last_atlas, new_glyphs, glyph_positions, + if (CanAppendToExistingAtlas(last_atlas, new_glyphs, glyph_positions, atlas_context->GetAtlasSize(), atlas_context->GetRectPacker())) { // The old bitmap will be reused and only the additional glyphs will be @@ -399,20 +376,14 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( } // --------------------------------------------------------------------------- - // Step 4a: Draw new font-glyph pairs into the existing bitmap. + // Step 4a: Draw new font-glyph pairs into the a host buffer and encode + // the uploads into the blit pass. // --------------------------------------------------------------------------- - auto bitmap = atlas_context_skia.GetBitmap(); - if (!UpdateAtlasBitmap(*last_atlas, bitmap, new_glyphs)) { + if (!UpdateAtlasBitmap(*last_atlas, blit_pass, host_buffer, + last_atlas->GetTexture(), new_glyphs)) { return nullptr; } - // --------------------------------------------------------------------------- - // Step 5a: Update the existing texture with the updated bitmap. - // --------------------------------------------------------------------------- - if (!UpdateGlyphTextureAtlas(bitmap, context.GetResourceAllocator(), - last_atlas->GetTexture(), blit_pass)) { - return nullptr; - } return last_atlas; } // A new glyph atlas must be created. @@ -464,52 +435,57 @@ std::shared_ptr TypographerContextSkia::CreateGlyphAtlas( } } - // --------------------------------------------------------------------------- - // Step 6b: Draw font-glyph pairs in the correct spot in the atlas. - // --------------------------------------------------------------------------- - auto bitmap = CreateAtlasBitmap(*glyph_atlas, atlas_size); - if (!bitmap) { - return nullptr; - } - atlas_context_skia.UpdateBitmap(bitmap); - // If the new atlas size is the same size as the previous texture, reuse the // texture and treat this as an updated that replaces all glyphs. - if (last_atlas && last_atlas->GetTexture()) { - std::shared_ptr last_texture = last_atlas->GetTexture(); - if (atlas_size == last_texture->GetSize()) { - if (!UpdateGlyphTextureAtlas(bitmap, context.GetResourceAllocator(), - last_texture, blit_pass)) { - return nullptr; - } - - glyph_atlas->SetTexture(last_texture); - return glyph_atlas; + std::shared_ptr new_texture; + if (last_atlas && last_atlas->GetTexture() && + last_atlas->GetTexture()->GetSize() == atlas_size) { + new_texture = last_atlas->GetTexture(); + } else { + // Otherwise, create a new texture. + TextureDescriptor descriptor; + switch (type) { + case GlyphAtlas::Type::kAlphaBitmap: + descriptor.format = + context.GetCapabilities()->GetDefaultGlyphAtlasFormat(); + break; + case GlyphAtlas::Type::kColorBitmap: + descriptor.format = PixelFormat::kR8G8B8A8UNormInt; + break; } + descriptor.size = atlas_size; + descriptor.storage_mode = StorageMode::kDevicePrivate; + new_texture = context.GetResourceAllocator()->CreateTexture(descriptor); } - // --------------------------------------------------------------------------- - // Step 7b: Upload the atlas as a texture. - // --------------------------------------------------------------------------- - PixelFormat format; - switch (type) { - case GlyphAtlas::Type::kAlphaBitmap: - format = context.GetCapabilities()->GetDefaultGlyphAtlasFormat(); - break; - case GlyphAtlas::Type::kColorBitmap: - format = PixelFormat::kR8G8B8A8UNormInt; - break; + if (!new_texture) { + return nullptr; } - std::shared_ptr texture = UploadGlyphTextureAtlas( - context.GetResourceAllocator(), bitmap, blit_pass, atlas_size, format); - if (!texture) { + // The texture needs to be cleared to transparent black so that linearly + // samplex rotated/skewed glyphs do not grab uninitialized data. We could + // instead use a render pass to clear to transparent black, but there are + // more restrictions on what kinds of textures can be bound on GLES. + { + auto bytes = + new_texture->GetTextureDescriptor().GetByteSizeOfBaseMipLevel(); + BufferView buffer_view = + host_buffer.Emplace(nullptr, bytes, DefaultUniformAlignment()); + + ::memset(buffer_view.buffer->OnGetContents() + buffer_view.range.offset, 0, + bytes); + blit_pass->AddCopy(buffer_view, new_texture); + } + + new_texture->SetLabel("GlyphAtlas"); + if (!UpdateAtlasBitmap(*glyph_atlas, blit_pass, host_buffer, new_texture, + font_glyph_pairs)) { return nullptr; } // --------------------------------------------------------------------------- // Step 8b: Record the texture in the glyph atlas. // --------------------------------------------------------------------------- - glyph_atlas->SetTexture(std::move(texture)); + glyph_atlas->SetTexture(std::move(new_texture)); return glyph_atlas; } diff --git a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.h b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.h index bc215f205f..c250087406 100644 --- a/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.h +++ b/engine/src/flutter/impeller/typographer/backends/skia/typographer_context_skia.h @@ -18,12 +18,14 @@ class TypographerContextSkia : public TypographerContext { ~TypographerContextSkia() override; // |TypographerContext| - std::shared_ptr CreateGlyphAtlasContext() const override; + std::shared_ptr CreateGlyphAtlasContext( + GlyphAtlas::Type type) const override; // |TypographerContext| std::shared_ptr CreateGlyphAtlas( Context& context, GlyphAtlas::Type type, + HostBuffer& host_buffer, const std::shared_ptr& atlas_context, const FontGlyphMap& font_glyph_map) const override; diff --git a/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.cc b/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.cc index fb6c105afe..e2b119b01b 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.cc +++ b/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.cc @@ -43,7 +43,8 @@ size_t BitmapSTB::GetSize() const { return width_ * height_ * bytes_per_pixel_; } -GlyphAtlasContextSTB::GlyphAtlasContextSTB() = default; +GlyphAtlasContextSTB::GlyphAtlasContextSTB(GlyphAtlas::Type type) + : GlyphAtlasContext(type) {} GlyphAtlasContextSTB::~GlyphAtlasContextSTB() = default; diff --git a/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.h b/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.h index 58962570d9..982b93a305 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.h +++ b/engine/src/flutter/impeller/typographer/backends/stb/glyph_atlas_context_stb.h @@ -41,7 +41,7 @@ class GlyphAtlasContextSTB : public GlyphAtlasContext, public BackendCast { public: - GlyphAtlasContextSTB(); + explicit GlyphAtlasContextSTB(GlyphAtlas::Type type); ~GlyphAtlasContextSTB() override; diff --git a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc index d7deda3f3a..b4e5bd58b0 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc +++ b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.cc @@ -38,8 +38,8 @@ TypographerContextSTB::TypographerContextSTB() : TypographerContext() {} TypographerContextSTB::~TypographerContextSTB() = default; std::shared_ptr -TypographerContextSTB::CreateGlyphAtlasContext() const { - return std::make_shared(); +TypographerContextSTB::CreateGlyphAtlasContext(GlyphAtlas::Type type) const { + return std::make_shared(type); } // Function returns the count of "remaining pairs" not packed into rect of given @@ -384,6 +384,7 @@ static std::shared_ptr UploadGlyphTextureAtlas( std::shared_ptr TypographerContextSTB::CreateGlyphAtlas( Context& context, GlyphAtlas::Type type, + HostBuffer& host_buffer, const std::shared_ptr& atlas_context, const FontGlyphMap& font_glyph_map) const { TRACE_EVENT0("impeller", __FUNCTION__); diff --git a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.h b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.h index dcc1cce181..87eedbe92a 100644 --- a/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.h +++ b/engine/src/flutter/impeller/typographer/backends/stb/typographer_context_stb.h @@ -5,6 +5,7 @@ #ifndef FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_STB_TYPOGRAPHER_CONTEXT_STB_H_ #define FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_STB_TYPOGRAPHER_CONTEXT_STB_H_ +#include "impeller/typographer/glyph_atlas.h" #include "impeller/typographer/typographer_context.h" #include @@ -20,12 +21,14 @@ class TypographerContextSTB : public TypographerContext { ~TypographerContextSTB() override; // |TypographerContext| - std::shared_ptr CreateGlyphAtlasContext() const override; + std::shared_ptr CreateGlyphAtlasContext( + GlyphAtlas::Type type) const override; // |TypographerContext| std::shared_ptr CreateGlyphAtlas( Context& context, GlyphAtlas::Type type, + HostBuffer& host_buffer, const std::shared_ptr& atlas_context, const FontGlyphMap& font_glyph_map) const override; diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.cc b/engine/src/flutter/impeller/typographer/glyph_atlas.cc index 1f9bfaff8d..f14944b2ae 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.cc @@ -9,9 +9,8 @@ namespace impeller { -GlyphAtlasContext::GlyphAtlasContext() - : atlas_(std::make_shared(GlyphAtlas::Type::kAlphaBitmap)), - atlas_size_(ISize(0, 0)) {} +GlyphAtlasContext::GlyphAtlasContext(GlyphAtlas::Type type) + : atlas_(std::make_shared(type)), atlas_size_(ISize(0, 0)) {} GlyphAtlasContext::~GlyphAtlasContext() {} diff --git a/engine/src/flutter/impeller/typographer/glyph_atlas.h b/engine/src/flutter/impeller/typographer/glyph_atlas.h index 9df7eecd4b..e758a8f454 100644 --- a/engine/src/flutter/impeller/typographer/glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/glyph_atlas.h @@ -12,7 +12,6 @@ #include "impeller/core/texture.h" #include "impeller/geometry/rect.h" -#include "impeller/renderer/pipeline.h" #include "impeller/typographer/font_glyph_pair.h" #include "impeller/typographer/rectangle_packer.h" @@ -145,6 +144,8 @@ class GlyphAtlas { /// class GlyphAtlasContext { public: + explicit GlyphAtlasContext(GlyphAtlas::Type type); + virtual ~GlyphAtlasContext(); //---------------------------------------------------------------------------- @@ -165,9 +166,6 @@ class GlyphAtlasContext { void UpdateRectPacker(std::shared_ptr rect_packer); - protected: - GlyphAtlasContext(); - private: std::shared_ptr atlas_; ISize atlas_size_; diff --git a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc index 121e6186c9..839625be47 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.cc @@ -20,10 +20,12 @@ LazyGlyphAtlas::LazyGlyphAtlas( std::shared_ptr typographer_context) : typographer_context_(std::move(typographer_context)), alpha_context_(typographer_context_ - ? typographer_context_->CreateGlyphAtlasContext() + ? typographer_context_->CreateGlyphAtlasContext( + GlyphAtlas::Type::kAlphaBitmap) : nullptr), color_context_(typographer_context_ - ? typographer_context_->CreateGlyphAtlasContext() + ? typographer_context_->CreateGlyphAtlasContext( + GlyphAtlas::Type::kColorBitmap) : nullptr) {} LazyGlyphAtlas::~LazyGlyphAtlas() = default; @@ -46,6 +48,7 @@ void LazyGlyphAtlas::ResetTextFrames() { const std::shared_ptr& LazyGlyphAtlas::CreateOrGetGlyphAtlas( Context& context, + HostBuffer& host_buffer, GlyphAtlas::Type type) const { { if (type == GlyphAtlas::Type::kAlphaBitmap && alpha_atlas_) { @@ -72,7 +75,7 @@ const std::shared_ptr& LazyGlyphAtlas::CreateOrGetGlyphAtlas( const std::shared_ptr& atlas_context = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_; std::shared_ptr atlas = typographer_context_->CreateGlyphAtlas( - context, type, atlas_context, glyph_map); + context, type, host_buffer, atlas_context, glyph_map); if (!atlas || !atlas->IsValid()) { VALIDATION_LOG << "Could not create valid atlas."; return kNullGlyphAtlas; diff --git a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h index 87820dba18..921e9141c1 100644 --- a/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h +++ b/engine/src/flutter/impeller/typographer/lazy_glyph_atlas.h @@ -25,6 +25,7 @@ class LazyGlyphAtlas { const std::shared_ptr& CreateOrGetGlyphAtlas( Context& context, + HostBuffer& host_buffer, GlyphAtlas::Type type) const; private: diff --git a/engine/src/flutter/impeller/typographer/typographer_context.h b/engine/src/flutter/impeller/typographer/typographer_context.h index efd10bdffa..2c45c419dc 100644 --- a/engine/src/flutter/impeller/typographer/typographer_context.h +++ b/engine/src/flutter/impeller/typographer/typographer_context.h @@ -25,15 +25,13 @@ class TypographerContext { virtual bool IsValid() const; - virtual std::shared_ptr CreateGlyphAtlasContext() - const = 0; - - // TODO(dnfield): Callers should not need to know which type of atlas to - // create. https://github.com/flutter/flutter/issues/111640 + virtual std::shared_ptr CreateGlyphAtlasContext( + GlyphAtlas::Type type) const = 0; virtual std::shared_ptr CreateGlyphAtlas( Context& context, GlyphAtlas::Type type, + HostBuffer& host_buffer, const std::shared_ptr& atlas_context, const FontGlyphMap& font_glyph_map) const = 0; diff --git a/engine/src/flutter/impeller/typographer/typographer_unittests.cc b/engine/src/flutter/impeller/typographer/typographer_unittests.cc index 4f7b120999..305062b970 100644 --- a/engine/src/flutter/impeller/typographer/typographer_unittests.cc +++ b/engine/src/flutter/impeller/typographer/typographer_unittests.cc @@ -4,6 +4,9 @@ #include "flutter/display_list/testing/dl_test_snippets.h" #include "flutter/testing/testing.h" +#include "gtest/gtest.h" +#include "impeller/core/host_buffer.h" +#include "impeller/playground/playground.h" #include "impeller/playground/playground_test.h" #include "impeller/typographer/backends/skia/text_frame_skia.h" #include "impeller/typographer/backends/skia/typographer_context_skia.h" @@ -28,14 +31,15 @@ INSTANTIATE_PLAYGROUND_SUITE(TypographerTest); static std::shared_ptr CreateGlyphAtlas( Context& context, const TypographerContext* typographer_context, + HostBuffer& host_buffer, GlyphAtlas::Type type, Scalar scale, const std::shared_ptr& atlas_context, const TextFrame& frame) { FontGlyphMap font_glyph_map; frame.CollectUniqueFontGlyphPairs(font_glyph_map, scale); - return typographer_context->CreateGlyphAtlas(context, type, atlas_context, - font_glyph_map); + return typographer_context->CreateGlyphAtlas(context, type, host_buffer, + atlas_context, font_glyph_map); } TEST_P(TypographerTest, CanConvertTextBlob) { @@ -58,14 +62,17 @@ TEST_P(TypographerTest, CanCreateRenderContext) { TEST_P(TypographerTest, CanCreateGlyphAtlas) { auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap); + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); ASSERT_TRUE(context && context->IsValid()); SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString("hello", sk_font); ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); ASSERT_NE(atlas, nullptr); ASSERT_NE(atlas->GetTexture(), nullptr); ASSERT_EQ(atlas->GetType(), GlyphAtlas::Type::kAlphaBitmap); @@ -90,6 +97,7 @@ TEST_P(TypographerTest, CanCreateGlyphAtlas) { } TEST_P(TypographerTest, LazyAtlasTracksColor) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); #if FML_OS_MACOSX auto mapping = flutter::testing::OpenFixtureAsSkData("Apple Color Emoji.ttc"); #else @@ -119,24 +127,27 @@ TEST_P(TypographerTest, LazyAtlasTracksColor) { // Creates different atlases for color and red bitmap. auto color_atlas = lazy_atlas.CreateOrGetGlyphAtlas( - *GetContext(), GlyphAtlas::Type::kColorBitmap); + *GetContext(), *host_buffer, GlyphAtlas::Type::kColorBitmap); auto bitmap_atlas = lazy_atlas.CreateOrGetGlyphAtlas( - *GetContext(), GlyphAtlas::Type::kAlphaBitmap); + *GetContext(), *host_buffer, GlyphAtlas::Type::kAlphaBitmap); ASSERT_FALSE(color_atlas == bitmap_atlas); } TEST_P(TypographerTest, GlyphAtlasWithOddUniqueGlyphSize) { auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap); + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); ASSERT_TRUE(context && context->IsValid()); SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString("AGH", sk_font); ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); ASSERT_NE(atlas, nullptr); ASSERT_NE(atlas->GetTexture(), nullptr); @@ -146,30 +157,36 @@ TEST_P(TypographerTest, GlyphAtlasWithOddUniqueGlyphSize) { TEST_P(TypographerTest, GlyphAtlasIsRecycledIfUnchanged) { auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap); + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); ASSERT_TRUE(context && context->IsValid()); SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString("spooky skellingtons", sk_font); ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); ASSERT_NE(atlas, nullptr); ASSERT_NE(atlas->GetTexture(), nullptr); ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas()); // now attempt to re-create an atlas with the same text blob. - auto next_atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto next_atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); ASSERT_EQ(atlas, next_atlas); ASSERT_EQ(atlas_context->GetGlyphAtlas(), atlas); } TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap); ASSERT_TRUE(context && context->IsValid()); const char* test_string = @@ -190,7 +207,7 @@ TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) { }; auto atlas = context->CreateGlyphAtlas(*GetContext(), GlyphAtlas::Type::kAlphaBitmap, - atlas_context, font_glyph_map); + *host_buffer, atlas_context, font_glyph_map); ASSERT_NE(atlas, nullptr); ASSERT_NE(atlas->GetTexture(), nullptr); @@ -212,15 +229,18 @@ TEST_P(TypographerTest, GlyphAtlasWithLotsOfdUniqueGlyphSize) { } TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledIfUnchanged) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap); ASSERT_TRUE(context && context->IsValid()); SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString("spooky 1", sk_font); ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); auto old_packer = atlas_context->GetRectPacker(); ASSERT_NE(atlas, nullptr); @@ -232,9 +252,10 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledIfUnchanged) { // Now create a new glyph atlas with a nearly identical blob. auto blob2 = SkTextBlob::MakeFromString("spooky 2", sk_font); - auto next_atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob2)); + auto next_atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob2)); ASSERT_EQ(atlas, next_atlas); auto* second_texture = next_atlas->GetTexture().get(); @@ -244,40 +265,6 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledIfUnchanged) { ASSERT_EQ(old_packer, new_packer); } -TEST_P(TypographerTest, GlyphAtlasTextureIsRecreatedIfTypeChanges) { - auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); - ASSERT_TRUE(context && context->IsValid()); - SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); - auto blob = SkTextBlob::MakeFromString("spooky 1", sk_font); - ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kAlphaBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); - auto old_packer = atlas_context->GetRectPacker(); - - ASSERT_NE(atlas, nullptr); - ASSERT_NE(atlas->GetTexture(), nullptr); - ASSERT_EQ(atlas, atlas_context->GetGlyphAtlas()); - - auto* first_texture = atlas->GetTexture().get(); - - // now create a new glyph atlas with an identical blob, - // but change the type. - - auto blob2 = SkTextBlob::MakeFromString("spooky 1", sk_font); - auto next_atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kColorBitmap, 1.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob2)); - ASSERT_NE(atlas, next_atlas); - auto* second_texture = next_atlas->GetTexture().get(); - - auto new_packer = atlas_context->GetRectPacker(); - - ASSERT_NE(second_texture, first_texture); - ASSERT_NE(old_packer, new_packer); -} - TEST_P(TypographerTest, MaybeHasOverlapping) { sk_sp font_mgr = txt::GetDefaultFontManager(); sk_sp typeface = @@ -339,16 +326,19 @@ TEST_P(TypographerTest, RectanglePackerAddsNonoverlapingRectangles) { } TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledWhenContentsAreRecreated) { + auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator()); auto context = TypographerContextSkia::Make(); - auto atlas_context = context->CreateGlyphAtlasContext(); + auto atlas_context = + context->CreateGlyphAtlasContext(GlyphAtlas::Type::kColorBitmap); ASSERT_TRUE(context && context->IsValid()); SkFont sk_font = flutter::testing::CreateTestFontOfSize(12); auto blob = SkTextBlob::MakeFromString("ABCDEFGHIJKLMNOPQRSTUVQXYZ123456789", sk_font); ASSERT_TRUE(blob); - auto atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kColorBitmap, 32.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob)); + auto atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 32.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob)); auto old_packer = atlas_context->GetRectPacker(); ASSERT_NE(atlas, nullptr); @@ -362,9 +352,10 @@ TEST_P(TypographerTest, GlyphAtlasTextureIsRecycledWhenContentsAreRecreated) { auto blob2 = SkTextBlob::MakeFromString("abcdefghijklmnopqrstuvwxyz123456789", sk_font); - auto next_atlas = CreateGlyphAtlas( - *GetContext(), context.get(), GlyphAtlas::Type::kColorBitmap, 32.0f, - atlas_context, *MakeTextFrameFromTextBlobSkia(blob2)); + auto next_atlas = + CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer, + GlyphAtlas::Type::kColorBitmap, 32.0f, atlas_context, + *MakeTextFrameFromTextBlobSkia(blob2)); ASSERT_NE(atlas, next_atlas); auto* second_texture = next_atlas->GetTexture().get(); diff --git a/engine/src/flutter/lib/gpu/shader_library.cc b/engine/src/flutter/lib/gpu/shader_library.cc index 8ef631bc71..565c2f938f 100644 --- a/engine/src/flutter/lib/gpu/shader_library.cc +++ b/engine/src/flutter/lib/gpu/shader_library.cc @@ -14,6 +14,7 @@ #include "flutter/lib/ui/window/platform_configuration.h" #include "fml/mapping.h" #include "fml/memory/ref_ptr.h" +#include "impeller/base/validation.h" #include "impeller/core/shader_types.h" #include "impeller/renderer/vertex_descriptor.h" #include "impeller/shader_bundle/shader_bundle_flatbuffers.h" diff --git a/engine/src/flutter/shell/common/shell.h b/engine/src/flutter/shell/common/shell.h index b724fa0160..f57c87491d 100644 --- a/engine/src/flutter/shell/common/shell.h +++ b/engine/src/flutter/shell/common/shell.h @@ -40,6 +40,7 @@ #include "flutter/shell/common/rasterizer.h" #include "flutter/shell/common/resource_cache_limit_calculator.h" #include "flutter/shell/common/shell_io_manager.h" +#include "impeller/renderer/context.h" #include "impeller/runtime_stage/runtime_stage.h" namespace flutter {