[Impeller] grow glyph atlas instead of resizing when rect packer is full. (flutter/engine#52849)
Fixes https://github.com/flutter/flutter/issues/138798 As far as I can tell, this gets us about as good as Skia, modulo the actual cost of rasterizing one of these glyphs is much more expensive for us. The basic strategy is: 1. If the existing atlas texture has room (defined by the rect packer addRect call succeeding) append as many glyphs as we can to the old texture. 2. Check if there were any remaining glyphs. If not return. 3. Otherwise, double the size of the texture and create a new rect packer that represents only the increased area. So for example if you doubled a 100x100 texture to 100x200, then you'd create another 100x100 rect packer, assuming the old one is full. 4. Grow this size until all remaining glyphs fit. Store the new rect packer and the "height adjustment", which basically tells you where along the y-axis the rect packer offset starts. 5. Allocate this new texture, clear it, and then blit the contents of the old texture into the top left corner. Append all additional glyphs. The allows us to recycle all previously rendered glyphs and keep them in the texture, reducing the amount of software rasterization per frame. 6. Note; if the max atlas size would be exceeded, throw it away and start again.
This commit is contained in:
@@ -5,24 +5,27 @@
|
||||
#include "impeller/typographer/backends/skia/typographer_context_skia.h"
|
||||
|
||||
#include <cstddef>
|
||||
#include <memory>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "flutter/fml/logging.h"
|
||||
#include "flutter/fml/trace_event.h"
|
||||
#include "fml/closure.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/geometry/size.h"
|
||||
#include "impeller/renderer/command_buffer.h"
|
||||
#include "impeller/renderer/render_pass.h"
|
||||
#include "impeller/renderer/render_target.h"
|
||||
#include "impeller/typographer/backends/skia/typeface_skia.h"
|
||||
#include "impeller/typographer/font_glyph_pair.h"
|
||||
#include "impeller/typographer/glyph_atlas.h"
|
||||
#include "impeller/typographer/rectangle_packer.h"
|
||||
#include "impeller/typographer/typographer_context.h"
|
||||
@@ -117,60 +120,22 @@ static SkImageInfo GetImageInfo(const GlyphAtlas& atlas, Size size) {
|
||||
FML_UNREACHABLE();
|
||||
}
|
||||
|
||||
static size_t PairsFitInAtlasOfSize(
|
||||
const std::vector<FontGlyphPair>& pairs,
|
||||
const ISize& atlas_size,
|
||||
std::vector<Rect>& glyph_positions,
|
||||
const std::shared_ptr<RectanglePacker>& rect_packer) {
|
||||
if (atlas_size.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
glyph_positions.clear();
|
||||
glyph_positions.reserve(pairs.size());
|
||||
|
||||
size_t i = 0;
|
||||
for (auto it = pairs.begin(); it != pairs.end(); ++i, ++it) {
|
||||
const auto& pair = *it;
|
||||
|
||||
const auto glyph_size =
|
||||
ISize::Ceil(pair.glyph.bounds.GetSize() * pair.scaled_font.scale);
|
||||
IPoint16 location_in_atlas;
|
||||
if (!rect_packer->AddRect(glyph_size.width + kPadding, //
|
||||
glyph_size.height + kPadding, //
|
||||
&location_in_atlas //
|
||||
)) {
|
||||
return pairs.size() - i;
|
||||
}
|
||||
glyph_positions.emplace_back(Rect::MakeXYWH(location_in_atlas.x(), //
|
||||
location_in_atlas.y(), //
|
||||
glyph_size.width, //
|
||||
glyph_size.height //
|
||||
));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool CanAppendToExistingAtlas(
|
||||
/// Append as many glyphs to the texture as will fit, and return the first index
|
||||
/// of [extra_pairs] that did not fit.
|
||||
static size_t AppendToExistingAtlas(
|
||||
const std::shared_ptr<GlyphAtlas>& atlas,
|
||||
const std::vector<FontGlyphPair>& extra_pairs,
|
||||
std::vector<Rect>& glyph_positions,
|
||||
ISize atlas_size,
|
||||
int64_t height_adjustment,
|
||||
const std::shared_ptr<RectanglePacker>& rect_packer) {
|
||||
TRACE_EVENT0("impeller", __FUNCTION__);
|
||||
if (!rect_packer || atlas_size.IsEmpty()) {
|
||||
return false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// We assume that all existing glyphs will fit. After all, they fit before.
|
||||
// The glyph_positions only contains the values for the additional glyphs
|
||||
// from extra_pairs.
|
||||
FML_DCHECK(glyph_positions.size() == 0);
|
||||
glyph_positions.reserve(extra_pairs.size());
|
||||
for (size_t i = 0; i < extra_pairs.size(); i++) {
|
||||
const FontGlyphPair& pair = extra_pairs[i];
|
||||
|
||||
const auto glyph_size =
|
||||
ISize::Ceil(pair.glyph.bounds.GetSize() * pair.scaled_font.scale);
|
||||
IPoint16 location_in_atlas;
|
||||
@@ -178,51 +143,88 @@ static bool CanAppendToExistingAtlas(
|
||||
glyph_size.height + kPadding, //
|
||||
&location_in_atlas //
|
||||
)) {
|
||||
return false;
|
||||
return i;
|
||||
}
|
||||
glyph_positions.emplace_back(Rect::MakeXYWH(location_in_atlas.x(), //
|
||||
location_in_atlas.y(), //
|
||||
glyph_size.width, //
|
||||
glyph_size.height //
|
||||
));
|
||||
glyph_positions.push_back(Rect::MakeXYWH(
|
||||
location_in_atlas.x(), //
|
||||
location_in_atlas.y() + height_adjustment, //
|
||||
glyph_size.width, //
|
||||
glyph_size.height //
|
||||
));
|
||||
}
|
||||
|
||||
return true;
|
||||
return extra_pairs.size();
|
||||
}
|
||||
|
||||
static ISize OptimumAtlasSizeForFontGlyphPairs(
|
||||
static size_t PairsFitInAtlasOfSize(
|
||||
const std::vector<FontGlyphPair>& pairs,
|
||||
const ISize& atlas_size,
|
||||
std::vector<Rect>& glyph_positions,
|
||||
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
|
||||
const ISize& max_texture_size) {
|
||||
static constexpr ISize kMinAtlasSize = ISize{4096, 1024};
|
||||
int64_t height_adjustment,
|
||||
const std::shared_ptr<RectanglePacker>& rect_packer,
|
||||
size_t start_index) {
|
||||
FML_DCHECK(!atlas_size.IsEmpty());
|
||||
|
||||
TRACE_EVENT0("impeller", __FUNCTION__);
|
||||
|
||||
ISize current_size = kMinAtlasSize;
|
||||
size_t total_pairs = pairs.size() + 1;
|
||||
do {
|
||||
auto rect_packer = std::shared_ptr<RectanglePacker>(
|
||||
RectanglePacker::Factory(current_size.width, current_size.height));
|
||||
|
||||
auto remaining_pairs = PairsFitInAtlasOfSize(pairs, current_size,
|
||||
glyph_positions, rect_packer);
|
||||
if (remaining_pairs == 0) {
|
||||
atlas_context->UpdateRectPacker(rect_packer);
|
||||
return current_size;
|
||||
} else if (remaining_pairs < std::ceil(total_pairs / 2)) {
|
||||
current_size = ISize::MakeWH(
|
||||
std::max(current_size.width, current_size.height),
|
||||
Allocation::NextPowerOfTwoSize(
|
||||
std::min(current_size.width, current_size.height) + 1));
|
||||
} else {
|
||||
current_size = ISize::MakeWH(
|
||||
Allocation::NextPowerOfTwoSize(current_size.width + 1),
|
||||
Allocation::NextPowerOfTwoSize(current_size.height + 1));
|
||||
for (size_t i = start_index; i < pairs.size(); i++) {
|
||||
const auto& pair = pairs[i];
|
||||
const auto glyph_size =
|
||||
ISize::Ceil(pair.glyph.bounds.GetSize() * pair.scaled_font.scale);
|
||||
IPoint16 location_in_atlas;
|
||||
if (!rect_packer->AddRect(glyph_size.width + kPadding, //
|
||||
glyph_size.height + kPadding, //
|
||||
&location_in_atlas //
|
||||
)) {
|
||||
return i;
|
||||
}
|
||||
} while (current_size.width <= max_texture_size.width &&
|
||||
current_size.height <= max_texture_size.height);
|
||||
return ISize{0, 0};
|
||||
glyph_positions.push_back(Rect::MakeXYWH(
|
||||
location_in_atlas.x(), //
|
||||
location_in_atlas.y() + height_adjustment, //
|
||||
glyph_size.width, //
|
||||
glyph_size.height //
|
||||
));
|
||||
}
|
||||
|
||||
return pairs.size();
|
||||
}
|
||||
|
||||
static ISize ComputeNextAtlasSize(
|
||||
const std::shared_ptr<GlyphAtlasContext>& atlas_context,
|
||||
const std::vector<FontGlyphPair>& extra_pairs,
|
||||
std::vector<Rect>& glyph_positions,
|
||||
size_t glyph_index_start,
|
||||
int64_t max_texture_height) {
|
||||
// Because we can't grow the skyline packer horizontally, pick a reasonable
|
||||
// large width for all atlases.
|
||||
static constexpr int64_t kAtlasWidth = 4096;
|
||||
static constexpr int64_t kMinAtlasHeight = 1024;
|
||||
|
||||
ISize current_size = ISize(kAtlasWidth, kMinAtlasHeight);
|
||||
if (atlas_context->GetAtlasSize().height > current_size.height) {
|
||||
current_size.height = atlas_context->GetAtlasSize().height * 2;
|
||||
}
|
||||
|
||||
auto height_adjustment = atlas_context->GetAtlasSize().height;
|
||||
while (current_size.height <= max_texture_height) {
|
||||
std::shared_ptr<RectanglePacker> rect_packer;
|
||||
if (atlas_context->GetRectPacker() || glyph_index_start) {
|
||||
rect_packer = RectanglePacker::Factory(
|
||||
kAtlasWidth,
|
||||
current_size.height - atlas_context->GetAtlasSize().height);
|
||||
} else {
|
||||
rect_packer = RectanglePacker::Factory(kAtlasWidth, current_size.height);
|
||||
}
|
||||
glyph_positions.erase(glyph_positions.begin() + glyph_index_start,
|
||||
glyph_positions.end());
|
||||
atlas_context->UpdateRectPacker(rect_packer);
|
||||
auto next_index = PairsFitInAtlasOfSize(extra_pairs, current_size,
|
||||
glyph_positions, height_adjustment,
|
||||
rect_packer, glyph_index_start);
|
||||
if (next_index == extra_pairs.size()) {
|
||||
return current_size;
|
||||
}
|
||||
current_size = ISize(current_size.width, current_size.height * 2);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
static void DrawGlyph(SkCanvas* canvas,
|
||||
@@ -261,12 +263,15 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
|
||||
std::shared_ptr<BlitPass>& blit_pass,
|
||||
HostBuffer& host_buffer,
|
||||
const std::shared_ptr<Texture>& texture,
|
||||
const std::vector<FontGlyphPair>& new_pairs) {
|
||||
const std::vector<FontGlyphPair>& new_pairs,
|
||||
size_t start_index,
|
||||
size_t end_index) {
|
||||
TRACE_EVENT0("impeller", __FUNCTION__);
|
||||
|
||||
bool has_color = atlas.GetType() == GlyphAtlas::Type::kColorBitmap;
|
||||
|
||||
for (const FontGlyphPair& pair : new_pairs) {
|
||||
for (size_t i = start_index; i < end_index; i++) {
|
||||
const FontGlyphPair& pair = new_pairs[i];
|
||||
auto pos = atlas.FindFontGlyphBounds(pair);
|
||||
if (!pos.has_value()) {
|
||||
continue;
|
||||
@@ -349,17 +354,20 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
|
||||
// existing bitmap without recreating the atlas.
|
||||
// ---------------------------------------------------------------------------
|
||||
std::vector<Rect> 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
|
||||
// added.
|
||||
glyph_positions.reserve(new_glyphs.size());
|
||||
size_t first_missing_index = 0;
|
||||
|
||||
if (last_atlas->GetTexture()) {
|
||||
// Append all glyphs that fit into the current atlas.
|
||||
first_missing_index = AppendToExistingAtlas(
|
||||
last_atlas, new_glyphs, glyph_positions, atlas_context->GetAtlasSize(),
|
||||
atlas_context->GetHeightAdjustment(), atlas_context->GetRectPacker());
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 3a: Record the positions in the glyph atlas of the newly added
|
||||
// glyphs.
|
||||
// ---------------------------------------------------------------------------
|
||||
for (size_t i = 0, count = glyph_positions.size(); i < count; i++) {
|
||||
for (size_t i = 0; i < first_missing_index; i++) {
|
||||
last_atlas->AddTypefaceGlyphPosition(new_glyphs[i], glyph_positions[i]);
|
||||
}
|
||||
|
||||
@@ -376,84 +384,68 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
|
||||
// the uploads into the blit pass.
|
||||
// ---------------------------------------------------------------------------
|
||||
if (!UpdateAtlasBitmap(*last_atlas, blit_pass, host_buffer,
|
||||
last_atlas->GetTexture(), new_glyphs)) {
|
||||
last_atlas->GetTexture(), new_glyphs, 0,
|
||||
first_missing_index)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return last_atlas;
|
||||
}
|
||||
// A new glyph atlas must be created.
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 3b: Get the optimum size of the texture atlas.
|
||||
// ---------------------------------------------------------------------------
|
||||
std::vector<FontGlyphPair> font_glyph_pairs;
|
||||
font_glyph_pairs.reserve(std::accumulate(
|
||||
font_glyph_map.begin(), font_glyph_map.end(), 0,
|
||||
[](const int a, const auto& b) { return a + b.second.size(); }));
|
||||
for (const auto& font_value : font_glyph_map) {
|
||||
const ScaledFont& scaled_font = font_value.first;
|
||||
for (const Glyph& glyph : font_value.second) {
|
||||
font_glyph_pairs.push_back({scaled_font, glyph});
|
||||
// If all glyphs fit, just return the old atlas.
|
||||
if (first_missing_index == new_glyphs.size()) {
|
||||
return last_atlas;
|
||||
}
|
||||
}
|
||||
std::shared_ptr<GlyphAtlas> glyph_atlas = std::make_shared<GlyphAtlas>(type);
|
||||
ISize atlas_size = OptimumAtlasSizeForFontGlyphPairs(
|
||||
font_glyph_pairs, //
|
||||
glyph_positions, //
|
||||
atlas_context, //
|
||||
context.GetResourceAllocator()->GetMaxTextureSizeSupported() //
|
||||
|
||||
int64_t height_adjustment = atlas_context->GetAtlasSize().height;
|
||||
const int64_t max_texture_height =
|
||||
context.GetResourceAllocator()->GetMaxTextureSizeSupported().height;
|
||||
|
||||
// IF the current atlas size is as big as it can get, then "GC" and create an
|
||||
// atlas with only the required glyphs. OpenGLES cannot reliably perform the
|
||||
// blit required here, as 1) it requires attaching textures as read and write
|
||||
// framebuffers which has substantially smaller size limits that max textures
|
||||
// and 2) is missing a GLES 2.0 implementation and cap check.
|
||||
bool blit_old_atlas = true;
|
||||
std::shared_ptr<GlyphAtlas> new_atlas = last_atlas;
|
||||
if (atlas_context->GetAtlasSize().height >= max_texture_height ||
|
||||
context.GetBackendType() == Context::BackendType::kOpenGLES) {
|
||||
blit_old_atlas = false;
|
||||
first_missing_index = 0;
|
||||
glyph_positions.clear();
|
||||
height_adjustment = 0;
|
||||
new_atlas = std::make_shared<GlyphAtlas>(type);
|
||||
atlas_context->UpdateRectPacker(nullptr);
|
||||
atlas_context->UpdateGlyphAtlas(new_atlas, {0, 0}, 0);
|
||||
}
|
||||
|
||||
// A new glyph atlas must be created.
|
||||
ISize atlas_size = ComputeNextAtlasSize(atlas_context, //
|
||||
new_glyphs, //
|
||||
glyph_positions, //
|
||||
first_missing_index, //
|
||||
max_texture_height //
|
||||
);
|
||||
|
||||
atlas_context->UpdateGlyphAtlas(glyph_atlas, atlas_size);
|
||||
atlas_context->UpdateGlyphAtlas(new_atlas, atlas_size, height_adjustment);
|
||||
if (atlas_size.IsEmpty()) {
|
||||
return nullptr;
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 4b: Find location of font-glyph pairs in the atlas. We have this from
|
||||
// the last step. So no need to do create another rect packer. But just do a
|
||||
// sanity check of counts. This could also be just an assertion as only a
|
||||
// construction issue would cause such a failure.
|
||||
// ---------------------------------------------------------------------------
|
||||
if (glyph_positions.size() != font_glyph_pairs.size()) {
|
||||
return nullptr;
|
||||
}
|
||||
FML_DCHECK(new_glyphs.size() == glyph_positions.size());
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 5b: Record the positions in the glyph atlas.
|
||||
// ---------------------------------------------------------------------------
|
||||
{
|
||||
size_t i = 0;
|
||||
for (auto it = font_glyph_pairs.begin(); it != font_glyph_pairs.end();
|
||||
++i, ++it) {
|
||||
glyph_atlas->AddTypefaceGlyphPosition(*it, glyph_positions[i]);
|
||||
}
|
||||
TextureDescriptor descriptor;
|
||||
switch (type) {
|
||||
case GlyphAtlas::Type::kAlphaBitmap:
|
||||
descriptor.format =
|
||||
context.GetCapabilities()->GetDefaultGlyphAtlasFormat();
|
||||
break;
|
||||
case GlyphAtlas::Type::kColorBitmap:
|
||||
descriptor.format = PixelFormat::kR8G8B8A8UNormInt;
|
||||
break;
|
||||
}
|
||||
|
||||
// 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.
|
||||
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;
|
||||
descriptor.usage = TextureUsage::kShaderRead | TextureUsage::kRenderTarget;
|
||||
new_texture = context.GetResourceAllocator()->CreateTexture(descriptor);
|
||||
}
|
||||
|
||||
descriptor.size = atlas_size;
|
||||
descriptor.storage_mode = StorageMode::kDevicePrivate;
|
||||
descriptor.usage = TextureUsage::kShaderRead;
|
||||
std::shared_ptr<Texture> new_texture =
|
||||
context.GetResourceAllocator()->CreateTexture(descriptor);
|
||||
if (!new_texture) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -461,12 +453,13 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
|
||||
new_texture->SetLabel("GlyphAtlas");
|
||||
|
||||
std::shared_ptr<CommandBuffer> cmd_buffer = context.CreateCommandBuffer();
|
||||
std::shared_ptr<BlitPass> blit_pass;
|
||||
std::shared_ptr<BlitPass> blit_pass = cmd_buffer->CreateBlitPass();
|
||||
|
||||
// The R8/A8 textures used for certain glyphs is not supported as color
|
||||
// attachments in most graphics drivers. To be safe, just do a CPU clear
|
||||
// for these.
|
||||
if (type == GlyphAtlas::Type::kAlphaBitmap) {
|
||||
// attachments in most graphics drivers. For other textures, most framebuffer
|
||||
// attachments have a much smaller size limit than the max texture size.
|
||||
{
|
||||
TRACE_EVENT0("flutter", "ClearGlyphAtlas");
|
||||
size_t byte_size =
|
||||
new_texture->GetTextureDescriptor().GetByteSizeOfBaseMipLevel();
|
||||
BufferView buffer_view =
|
||||
@@ -475,42 +468,46 @@ std::shared_ptr<GlyphAtlas> TypographerContextSkia::CreateGlyphAtlas(
|
||||
::memset(buffer_view.buffer->OnGetContents() + buffer_view.range.offset, 0,
|
||||
byte_size);
|
||||
buffer_view.buffer->Flush();
|
||||
blit_pass = cmd_buffer->CreateBlitPass();
|
||||
blit_pass->AddCopy(buffer_view, new_texture);
|
||||
} else {
|
||||
// In all other cases, we can use a render pass to clear to a transparent
|
||||
// color.
|
||||
ColorAttachment attachment;
|
||||
attachment.clear_color = Color::BlackTransparent();
|
||||
attachment.load_action = LoadAction::kClear;
|
||||
attachment.store_action = StoreAction::kStore;
|
||||
attachment.texture = new_texture;
|
||||
|
||||
RenderTarget render_target;
|
||||
render_target.SetColorAttachment(attachment, 0u);
|
||||
|
||||
auto render_pass = cmd_buffer->CreateRenderPass(render_target);
|
||||
render_pass->EncodeCommands();
|
||||
blit_pass = cmd_buffer->CreateBlitPass();
|
||||
}
|
||||
FML_DCHECK(!!blit_pass);
|
||||
|
||||
fml::ScopedCleanupClosure closure([&]() {
|
||||
blit_pass->EncodeCommands(context.GetResourceAllocator());
|
||||
context.GetCommandQueue()->Submit({std::move(cmd_buffer)});
|
||||
});
|
||||
|
||||
if (!UpdateAtlasBitmap(*glyph_atlas, blit_pass, host_buffer, new_texture,
|
||||
font_glyph_pairs)) {
|
||||
return nullptr;
|
||||
// Blit the old texture to the top left of the new atlas.
|
||||
if (new_atlas->GetTexture() && blit_old_atlas) {
|
||||
blit_pass->AddCopy(new_atlas->GetTexture(), new_texture,
|
||||
IRect::MakeSize(new_atlas->GetTexture()->GetSize()),
|
||||
{0, 0});
|
||||
}
|
||||
|
||||
// Now append all remaining glyphs. This should never have any missing data...
|
||||
new_atlas->SetTexture(std::move(new_texture));
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 3a: Record the positions in the glyph atlas of the newly added
|
||||
// glyphs.
|
||||
// ---------------------------------------------------------------------------
|
||||
for (size_t i = first_missing_index; i < glyph_positions.size(); i++) {
|
||||
new_atlas->AddTypefaceGlyphPosition(new_glyphs[i], glyph_positions[i]);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 4a: Draw new font-glyph pairs into the a host buffer and encode
|
||||
// the uploads into the blit pass.
|
||||
// ---------------------------------------------------------------------------
|
||||
if (!UpdateAtlasBitmap(*new_atlas, blit_pass, host_buffer,
|
||||
new_atlas->GetTexture(), new_glyphs,
|
||||
first_missing_index, new_glyphs.size())) {
|
||||
return nullptr;
|
||||
}
|
||||
// ---------------------------------------------------------------------------
|
||||
// Step 8b: Record the texture in the glyph atlas.
|
||||
// ---------------------------------------------------------------------------
|
||||
glyph_atlas->SetTexture(std::move(new_texture));
|
||||
|
||||
return glyph_atlas;
|
||||
return new_atlas;
|
||||
}
|
||||
|
||||
} // namespace impeller
|
||||
|
||||
@@ -493,7 +493,7 @@ std::shared_ptr<GlyphAtlas> TypographerContextSTB::CreateGlyphAtlas(
|
||||
context.GetResourceAllocator()->GetMaxTextureSizeSupported() //
|
||||
);
|
||||
|
||||
atlas_context->UpdateGlyphAtlas(glyph_atlas, atlas_size);
|
||||
atlas_context->UpdateGlyphAtlas(glyph_atlas, atlas_size, 0);
|
||||
if (atlas_size.IsEmpty()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -22,14 +22,20 @@ const ISize& GlyphAtlasContext::GetAtlasSize() const {
|
||||
return atlas_size_;
|
||||
}
|
||||
|
||||
int64_t GlyphAtlasContext::GetHeightAdjustment() const {
|
||||
return height_adjustment_;
|
||||
}
|
||||
|
||||
std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
|
||||
return rect_packer_;
|
||||
}
|
||||
|
||||
void GlyphAtlasContext::UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
|
||||
ISize size) {
|
||||
ISize size,
|
||||
int64_t height_adjustment) {
|
||||
atlas_ = std::move(atlas);
|
||||
atlas_size_ = size;
|
||||
height_adjustment_ = height_adjustment;
|
||||
}
|
||||
|
||||
void GlyphAtlasContext::UpdateRectPacker(
|
||||
|
||||
@@ -160,9 +160,21 @@ class GlyphAtlasContext {
|
||||
/// @brief Retrieve the previous (if any) rect packer.
|
||||
std::shared_ptr<RectanglePacker> GetRectPacker() const;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief A y-coordinate shift that must be applied to glyphs appended
|
||||
/// to
|
||||
/// the atlas.
|
||||
///
|
||||
/// The rectangle packer is only initialized for unfilled regions
|
||||
/// of the atlas. The area the rectangle packer covers is offset
|
||||
/// from the origin by this height adjustment.
|
||||
int64_t GetHeightAdjustment() const;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Update the context with a newly constructed glyph atlas.
|
||||
void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas, ISize size);
|
||||
void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
|
||||
ISize size,
|
||||
int64_t height_adjustment_);
|
||||
|
||||
void UpdateRectPacker(std::shared_ptr<RectanglePacker> rect_packer);
|
||||
|
||||
@@ -170,6 +182,7 @@ class GlyphAtlasContext {
|
||||
std::shared_ptr<GlyphAtlas> atlas_;
|
||||
ISize atlas_size_;
|
||||
std::shared_ptr<RectanglePacker> rect_packer_;
|
||||
int64_t height_adjustment_;
|
||||
|
||||
GlyphAtlasContext(const GlyphAtlasContext&) = delete;
|
||||
|
||||
|
||||
@@ -33,8 +33,6 @@ class SkylineRectanglePacker final : public RectanglePacker {
|
||||
return area_so_far_ / ((float)width() * height());
|
||||
}
|
||||
|
||||
std::unique_ptr<RectanglePacker> Clone(uint32_t scale) final;
|
||||
|
||||
private:
|
||||
struct SkylineSegment {
|
||||
int x_;
|
||||
@@ -173,17 +171,6 @@ void SkylineRectanglePacker::AddSkylineLevel(size_t skyline_index,
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<RectanglePacker> SkylineRectanglePacker::Clone(uint32_t scale) {
|
||||
FML_DCHECK(scale != 0);
|
||||
auto packer =
|
||||
std::make_unique<SkylineRectanglePacker>(width(), height() * scale);
|
||||
for (SkylineSegment segment : skyline_) {
|
||||
packer->skyline_.push_back(segment);
|
||||
}
|
||||
packer->area_so_far_ = area_so_far_;
|
||||
return packer;
|
||||
}
|
||||
|
||||
std::shared_ptr<RectanglePacker> RectanglePacker::Factory(int width,
|
||||
int height) {
|
||||
return std::make_shared<SkylineRectanglePacker>(width, height);
|
||||
|
||||
@@ -51,19 +51,6 @@ class RectanglePacker {
|
||||
///
|
||||
virtual Scalar PercentFull() const = 0;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Create a new rectangle packer with a larger scaled height
|
||||
/// scaled and initialize its contents to the current packer.
|
||||
///
|
||||
/// @param[in] scale The scaling factor to be applied to the new height.
|
||||
///
|
||||
/// @return A new rectangle packer.
|
||||
///
|
||||
/// This method is used for growing the glyph atlas while keeping
|
||||
/// existing rects in place. The width of the rectangle packer
|
||||
/// cannot be increased.
|
||||
virtual std::unique_ptr<RectanglePacker> Clone(uint32_t scale) = 0;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
/// @brief Empty out all previously added rectangles.
|
||||
///
|
||||
|
||||
@@ -325,58 +325,6 @@ TEST_P(TypographerTest, RectanglePackerAddsNonoverlapingRectangles) {
|
||||
ASSERT_EQ(packer->PercentFull(), 0);
|
||||
}
|
||||
|
||||
TEST(TypographerTest, CanCloneRectanglePackerEmpty) {
|
||||
auto skyline = RectanglePacker::Factory(256, 256);
|
||||
|
||||
EXPECT_EQ(skyline->PercentFull(), 0);
|
||||
|
||||
auto skyline_2 = skyline->Clone(/*scale=*/2);
|
||||
|
||||
EXPECT_EQ(skyline->PercentFull(), 0);
|
||||
}
|
||||
|
||||
TEST(TypographerTest, CanCloneRectanglePackerAndPreservePositions) {
|
||||
auto skyline = RectanglePacker::Factory(256, 256);
|
||||
IPoint16 loc;
|
||||
EXPECT_TRUE(skyline->AddRect(100, 100, &loc));
|
||||
|
||||
EXPECT_EQ(loc.x(), 0);
|
||||
EXPECT_EQ(loc.y(), 0);
|
||||
auto percent = skyline->PercentFull();
|
||||
|
||||
auto skyline_2 = skyline->Clone(/*scale=*/2);
|
||||
|
||||
EXPECT_LT(skyline_2->PercentFull(), percent);
|
||||
}
|
||||
|
||||
TEST(TypographerTest, CanCloneRectanglePackerWhileFull) {
|
||||
auto skyline = RectanglePacker::Factory(256, 256);
|
||||
IPoint16 loc;
|
||||
// Add a rectangle the size of the entire area.
|
||||
EXPECT_TRUE(skyline->AddRect(256, 256, &loc));
|
||||
// Packer is now full.
|
||||
EXPECT_FALSE(skyline->AddRect(256, 256, &loc));
|
||||
|
||||
auto skyline_2 = skyline->Clone(/*scale=*/2);
|
||||
|
||||
// Can now fit one more
|
||||
EXPECT_TRUE(skyline_2->AddRect(256, 256, &loc));
|
||||
}
|
||||
|
||||
TEST(TypographerTest, CloneToSameSizePreservesContents) {
|
||||
auto skyline = RectanglePacker::Factory(256, 256);
|
||||
IPoint16 loc;
|
||||
// Add a rectangle the size of the entire area.
|
||||
EXPECT_TRUE(skyline->AddRect(256, 256, &loc));
|
||||
// Packer is now full.
|
||||
EXPECT_FALSE(skyline->AddRect(256, 256, &loc));
|
||||
|
||||
auto skyline_2 = skyline->Clone(/*scale=*/1);
|
||||
|
||||
// Packer is still full.
|
||||
EXPECT_FALSE(skyline->AddRect(256, 256, &loc));
|
||||
}
|
||||
|
||||
TEST(TypographerTest, RectanglePackerFillsRows) {
|
||||
auto skyline = RectanglePacker::Factory(257, 256);
|
||||
|
||||
@@ -398,6 +346,57 @@ TEST(TypographerTest, RectanglePackerFillsRows) {
|
||||
EXPECT_EQ(loc.y(), 16);
|
||||
}
|
||||
|
||||
TEST_P(TypographerTest, GlyphAtlasTextureWillGrowTilMaxTextureSize) {
|
||||
if (GetBackend() == PlaygroundBackend::kOpenGLES) {
|
||||
GTEST_SKIP() << "Atlas growth isn't supported for OpenGLES currently.";
|
||||
}
|
||||
|
||||
auto host_buffer = HostBuffer::Create(GetContext()->GetResourceAllocator());
|
||||
auto context = TypographerContextSkia::Make();
|
||||
auto atlas_context =
|
||||
context->CreateGlyphAtlasContext(GlyphAtlas::Type::kAlphaBitmap);
|
||||
ASSERT_TRUE(context && context->IsValid());
|
||||
SkFont sk_font = flutter::testing::CreateTestFontOfSize(12);
|
||||
auto blob = SkTextBlob::MakeFromString("A", sk_font);
|
||||
ASSERT_TRUE(blob);
|
||||
auto atlas =
|
||||
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
|
||||
GlyphAtlas::Type::kAlphaBitmap, 1.0f, atlas_context,
|
||||
*MakeTextFrameFromTextBlobSkia(blob));
|
||||
// Continually append new glyphs until the glyph size grows to the maximum.
|
||||
// Note that the sizes here are more or less experimentally determined, but
|
||||
// the important expectation is that the atlas size will shrink again after
|
||||
// growing to the maximum size.
|
||||
constexpr ISize expected_sizes[13] = {
|
||||
{4096, 4096}, //
|
||||
{4096, 4096}, //
|
||||
{4096, 8192}, //
|
||||
{4096, 8192}, //
|
||||
{4096, 8192}, //
|
||||
{4096, 8192}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 16384}, //
|
||||
{4096, 4096} // Shrinks!
|
||||
};
|
||||
|
||||
for (int i = 0; i < 13; i++) {
|
||||
SkFont sk_font = flutter::testing::CreateTestFontOfSize(50 + i);
|
||||
auto blob = SkTextBlob::MakeFromString("A", sk_font);
|
||||
|
||||
atlas =
|
||||
CreateGlyphAtlas(*GetContext(), context.get(), *host_buffer,
|
||||
GlyphAtlas::Type::kAlphaBitmap, 50 + i, atlas_context,
|
||||
*MakeTextFrameFromTextBlobSkia(blob));
|
||||
ASSERT_TRUE(!!atlas);
|
||||
EXPECT_EQ(atlas->GetTexture()->GetTextureDescriptor().size,
|
||||
expected_sizes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
} // namespace impeller
|
||||
|
||||
|
||||
Reference in New Issue
Block a user