Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 6264763

Browse files
author
Jonah Williams
authored
[Impeller] write glyphs to malloc buffer. (#52937)
Writing to a malloc buffer and then copying is faster on a variety of Pixel devices. Since this change was supposed to be a performance optimization, just back it out.
1 parent dd2b6f4 commit 6264763

File tree

1 file changed

+11
-54
lines changed

1 file changed

+11
-54
lines changed

impeller/typographer/backends/skia/typographer_context_skia.cc

Lines changed: 11 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -46,56 +46,6 @@ namespace impeller {
4646
// https://github.com/flutter/flutter/issues/114563
4747
constexpr auto kPadding = 2;
4848

49-
namespace {
50-
51-
class HostBufferAllocator : public SkBitmap::Allocator {
52-
public:
53-
explicit HostBufferAllocator(HostBuffer& host_buffer)
54-
: host_buffer_(host_buffer) {}
55-
56-
[[nodiscard]] BufferView TakeBufferView() {
57-
buffer_view_.buffer->Flush();
58-
return std::move(buffer_view_);
59-
}
60-
61-
// |SkBitmap::Allocator|
62-
bool allocPixelRef(SkBitmap* bitmap) override {
63-
if (!bitmap) {
64-
return false;
65-
}
66-
const SkImageInfo& info = bitmap->info();
67-
if (kUnknown_SkColorType == info.colorType() || info.width() < 0 ||
68-
info.height() < 0 || !info.validRowBytes(bitmap->rowBytes())) {
69-
return false;
70-
}
71-
72-
size_t required_bytes = bitmap->rowBytes() * bitmap->height();
73-
BufferView buffer_view = host_buffer_.Emplace(nullptr, required_bytes,
74-
DefaultUniformAlignment());
75-
76-
// The impeller host buffer is not cleared between frames and may contain
77-
// stale data. The Skia software canvas does not write to pixels without
78-
// any contents, which causes this data to leak through.
79-
::memset(buffer_view.buffer->OnGetContents() + buffer_view.range.offset, 0,
80-
required_bytes);
81-
82-
auto pixel_ref = sk_sp<SkPixelRef>(new SkPixelRef(
83-
info.width(), info.height(),
84-
buffer_view.buffer->OnGetContents() + buffer_view.range.offset,
85-
bitmap->rowBytes()));
86-
87-
bitmap->setPixelRef(std::move(pixel_ref), 0, 0);
88-
buffer_view_ = std::move(buffer_view);
89-
return true;
90-
}
91-
92-
private:
93-
BufferView buffer_view_;
94-
HostBuffer& host_buffer_;
95-
};
96-
97-
} // namespace
98-
9949
std::shared_ptr<TypographerContext> TypographerContextSkia::Make() {
10050
return std::make_shared<TypographerContextSkia>();
10151
}
@@ -282,9 +232,8 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
282232
}
283233

284234
SkBitmap bitmap;
285-
HostBufferAllocator allocator(host_buffer);
286235
bitmap.setInfo(GetImageInfo(atlas, size));
287-
if (!bitmap.tryAllocPixels(&allocator)) {
236+
if (!bitmap.tryAllocPixels()) {
288237
return false;
289238
}
290239
auto surface = SkSurfaces::WrapPixels(bitmap.pixmap());
@@ -298,11 +247,19 @@ static bool UpdateAtlasBitmap(const GlyphAtlas& atlas,
298247

299248
DrawGlyph(canvas, pair.scaled_font, pair.glyph, has_color);
300249

250+
// Writing to a malloc'd buffer and then copying to the staging buffers
251+
// benchmarks as substantially faster on a number of Android devices.
252+
BufferView buffer_view = host_buffer.Emplace(
253+
bitmap.getAddr(0, 0),
254+
size.Area() * BytesPerPixelForPixelFormat(
255+
atlas.GetTexture()->GetTextureDescriptor().format),
256+
DefaultUniformAlignment());
257+
301258
// convert_to_read is set to false so that the texture remains in a transfer
302259
// dst layout until we finish writing to it below. This only has an impact
303260
// on Vulkan where we are responsible for managing image layouts.
304-
if (!blit_pass->AddCopy(allocator.TakeBufferView(), //
305-
texture, //
261+
if (!blit_pass->AddCopy(std::move(buffer_view), //
262+
texture, //
306263
IRect::MakeXYWH(pos->GetLeft(), pos->GetTop(),
307264
size.width, size.height), //
308265
/*label=*/"", //

0 commit comments

Comments
 (0)