[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.
This commit is contained in:
Jonah Williams
2024-05-10 14:39:47 -07:00
committed by GitHub
parent 4dba894abc
commit 578e769483
24 changed files with 260 additions and 353 deletions

View File

@@ -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

View File

@@ -73,7 +73,7 @@ bool TextContents::Render(const ContentContext& renderer,
auto type = frame_->GetAtlasType();
const std::shared_ptr<GlyphAtlas>& atlas =
renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
*renderer.GetContext(), type);
*renderer.GetContext(), renderer.GetTransientsBuffer(), type);
if (!atlas || !atlas->IsValid()) {
VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";

View File

@@ -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

View File

@@ -162,6 +162,7 @@ struct GLProc {
PROC(IsShader); \
PROC(IsTexture); \
PROC(LinkProgram); \
PROC(PixelStorei); \
PROC(RenderbufferStorage); \
PROC(Scissor); \
PROC(ShaderBinary); \

View File

@@ -291,6 +291,7 @@ bool TextureGLES::OnSetContents(std::shared_ptr<const fml::Mapping> 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

View File

@@ -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

View File

@@ -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;

View File

@@ -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",

View File

@@ -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<SkBitmap> GlyphAtlasContextSkia::GetBitmap() const {
return bitmap_;
}
void GlyphAtlasContextSkia::UpdateBitmap(std::shared_ptr<SkBitmap> bitmap) {
bitmap_ = std::move(bitmap);
}
} // namespace impeller

View File

@@ -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<GlyphAtlasContextSkia, GlyphAtlasContext> {
public:
GlyphAtlasContextSkia();
~GlyphAtlasContextSkia() override;
//----------------------------------------------------------------------------
/// @brief Retrieve the previous (if any) SkBitmap instance.
std::shared_ptr<SkBitmap> GetBitmap() const;
void UpdateBitmap(std::shared_ptr<SkBitmap> bitmap);
private:
std::shared_ptr<SkBitmap> bitmap_;
GlyphAtlasContextSkia(const GlyphAtlasContextSkia&) = delete;
GlyphAtlasContextSkia& operator=(const GlyphAtlasContextSkia&) = delete;
};
} // namespace impeller
#endif // FLUTTER_IMPELLER_TYPOGRAPHER_BACKENDS_SKIA_GLYPH_ATLAS_CONTEXT_SKIA_H_

View File

@@ -4,23 +4,33 @@
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
#include <cstddef>
#include <numeric>
#include <utility>
#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<SkPixelRef>(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<TypographerContext> TypographerContextSkia::Make() {
return std::make_shared<TypographerContextSkia>();
}
@@ -42,8 +102,19 @@ TypographerContextSkia::TypographerContextSkia() = default;
TypographerContextSkia::~TypographerContextSkia() = default;
std::shared_ptr<GlyphAtlasContext>
TypographerContextSkia::CreateGlyphAtlasContext() const {
return std::make_shared<GlyphAtlasContextSkia>();
TypographerContextSkia::CreateGlyphAtlasContext(GlyphAtlas::Type type) const {
return std::make_shared<GlyphAtlasContext>(type);
}
static SkImageInfo GetImageInfo(const GlyphAtlas& atlas, Size size) {
switch (atlas.GetType()) {
case GlyphAtlas::Type::kAlphaBitmap:
return SkImageInfo::MakeA8(SkISize{static_cast<int32_t>(size.width),
static_cast<int32_t>(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<SkBitmap>& bitmap,
std::shared_ptr<BlitPass>& blit_pass,
HostBuffer& host_buffer,
const std::shared_ptr<Texture>& texture,
const std::vector<FontGlyphPair>& 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<SkBitmap> CreateAtlasBitmap(const GlyphAtlas& atlas,
const ISize& atlas_size) {
TRACE_EVENT0("impeller", __FUNCTION__);
auto bitmap = std::make_shared<SkBitmap>();
SkImageInfo image_info;
switch (atlas.GetType()) {
case GlyphAtlas::Type::kAlphaBitmap:
image_info =
SkImageInfo::MakeA8(SkISize{static_cast<int32_t>(atlas_size.width),
static_cast<int32_t>(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<SkBitmap> bitmap,
const std::shared_ptr<Allocator>& allocator,
const std::shared_ptr<Texture>& texture,
const std::shared_ptr<BlitPass>& blit_pass) {
TRACE_EVENT0("impeller", __FUNCTION__);
FML_DCHECK(bitmap != nullptr);
auto texture_descriptor = texture->GetTextureDescriptor();
auto mapping = std::make_shared<fml::NonOwnedMapping>(
reinterpret_cast<const uint8_t*>(bitmap->getAddr(0, 0)), // data
texture_descriptor.GetByteSizeOfBaseMipLevel(), // size
[bitmap](auto, auto) mutable { bitmap.reset(); } // proc
);
std::shared_ptr<DeviceBuffer> device_buffer =
allocator->CreateBufferWithCopy(*mapping);
blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)),
texture);
return blit_pass->EncodeCommands(allocator);
}
static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
const std::shared_ptr<Allocator>& allocator,
std::shared_ptr<SkBitmap> bitmap,
const std::shared_ptr<BlitPass>& 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> texture =
allocator->CreateTexture(texture_descriptor);
if (!texture || !texture->IsValid()) {
return nullptr;
}
texture->SetLabel("GlyphAtlas");
std::shared_ptr<fml::NonOwnedMapping> mapping =
std::make_shared<fml::NonOwnedMapping>(
reinterpret_cast<const uint8_t*>(bitmap->getAddr(0, 0)), // data
texture_descriptor.GetByteSizeOfBaseMipLevel(), // size
[bitmap](auto, auto) mutable { bitmap.reset(); } // proc
);
std::shared_ptr<DeviceBuffer> device_buffer =
allocator->CreateBufferWithCopy(*mapping);
blit_pass->AddCopy(DeviceBuffer::AsBufferView(std::move(device_buffer)),
texture);
blit_pass->EncodeCommands(allocator);
return texture;
}
std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
Context& context,
GlyphAtlas::Type type,
HostBuffer& host_buffer,
const std::shared_ptr<GlyphAtlasContext>& 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<GlyphAtlas> 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<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
std::shared_ptr<BlitPass> 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<GlyphAtlas> 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<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
// the type is identical.
// ---------------------------------------------------------------------------
std::vector<Rect> 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<GlyphAtlas> 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<GlyphAtlas> 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<Texture> 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<Texture> 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> 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;
}

View File

@@ -18,12 +18,14 @@ class TypographerContextSkia : public TypographerContext {
~TypographerContextSkia() override;
// |TypographerContext|
std::shared_ptr<GlyphAtlasContext> CreateGlyphAtlasContext() const override;
std::shared_ptr<GlyphAtlasContext> CreateGlyphAtlasContext(
GlyphAtlas::Type type) const override;
// |TypographerContext|
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
Context& context,
GlyphAtlas::Type type,
HostBuffer& host_buffer,
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const FontGlyphMap& font_glyph_map) const override;

View File

@@ -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;

View File

@@ -41,7 +41,7 @@ class GlyphAtlasContextSTB
: public GlyphAtlasContext,
public BackendCast<GlyphAtlasContextSTB, GlyphAtlasContext> {
public:
GlyphAtlasContextSTB();
explicit GlyphAtlasContextSTB(GlyphAtlas::Type type);
~GlyphAtlasContextSTB() override;

View File

@@ -38,8 +38,8 @@ TypographerContextSTB::TypographerContextSTB() : TypographerContext() {}
TypographerContextSTB::~TypographerContextSTB() = default;
std::shared_ptr<GlyphAtlasContext>
TypographerContextSTB::CreateGlyphAtlasContext() const {
return std::make_shared<GlyphAtlasContextSTB>();
TypographerContextSTB::CreateGlyphAtlasContext(GlyphAtlas::Type type) const {
return std::make_shared<GlyphAtlasContextSTB>(type);
}
// Function returns the count of "remaining pairs" not packed into rect of given
@@ -384,6 +384,7 @@ static std::shared_ptr<Texture> UploadGlyphTextureAtlas(
std::shared_ptr<GlyphAtlas> TypographerContextSTB::CreateGlyphAtlas(
Context& context,
GlyphAtlas::Type type,
HostBuffer& host_buffer,
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const FontGlyphMap& font_glyph_map) const {
TRACE_EVENT0("impeller", __FUNCTION__);

View File

@@ -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 <memory>
@@ -20,12 +21,14 @@ class TypographerContextSTB : public TypographerContext {
~TypographerContextSTB() override;
// |TypographerContext|
std::shared_ptr<GlyphAtlasContext> CreateGlyphAtlasContext() const override;
std::shared_ptr<GlyphAtlasContext> CreateGlyphAtlasContext(
GlyphAtlas::Type type) const override;
// |TypographerContext|
std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
Context& context,
GlyphAtlas::Type type,
HostBuffer& host_buffer,
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const FontGlyphMap& font_glyph_map) const override;

View File

@@ -9,9 +9,8 @@
namespace impeller {
GlyphAtlasContext::GlyphAtlasContext()
: atlas_(std::make_shared<GlyphAtlas>(GlyphAtlas::Type::kAlphaBitmap)),
atlas_size_(ISize(0, 0)) {}
GlyphAtlasContext::GlyphAtlasContext(GlyphAtlas::Type type)
: atlas_(std::make_shared<GlyphAtlas>(type)), atlas_size_(ISize(0, 0)) {}
GlyphAtlasContext::~GlyphAtlasContext() {}

View File

@@ -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<RectanglePacker> rect_packer);
protected:
GlyphAtlasContext();
private:
std::shared_ptr<GlyphAtlas> atlas_;
ISize atlas_size_;

View File

@@ -20,10 +20,12 @@ LazyGlyphAtlas::LazyGlyphAtlas(
std::shared_ptr<TypographerContext> 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<GlyphAtlas>& 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<GlyphAtlas>& LazyGlyphAtlas::CreateOrGetGlyphAtlas(
const std::shared_ptr<GlyphAtlasContext>& atlas_context =
type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
std::shared_ptr<GlyphAtlas> 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;

View File

@@ -25,6 +25,7 @@ class LazyGlyphAtlas {
const std::shared_ptr<GlyphAtlas>& CreateOrGetGlyphAtlas(
Context& context,
HostBuffer& host_buffer,
GlyphAtlas::Type type) const;
private:

View File

@@ -25,15 +25,13 @@ class TypographerContext {
virtual bool IsValid() const;
virtual std::shared_ptr<GlyphAtlasContext> 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<GlyphAtlasContext> CreateGlyphAtlasContext(
GlyphAtlas::Type type) const = 0;
virtual std::shared_ptr<GlyphAtlas> CreateGlyphAtlas(
Context& context,
GlyphAtlas::Type type,
HostBuffer& host_buffer,
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
const FontGlyphMap& font_glyph_map) const = 0;

View File

@@ -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<GlyphAtlas> CreateGlyphAtlas(
Context& context,
const TypographerContext* typographer_context,
HostBuffer& host_buffer,
GlyphAtlas::Type type,
Scalar scale,
const std::shared_ptr<GlyphAtlasContext>& 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<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
sk_sp<SkTypeface> 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();

View File

@@ -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"

View File

@@ -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 {