Skip to content

Commit

Permalink
Fixed the harfbuzz multi-threaded crash.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fjie committed Aug 15, 2023
1 parent 56f7fb6 commit f361647
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/rendering/utils/shaper/TextShaperHarfbuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,27 @@ static std::shared_ptr<hb_font_t> CreateHBFont(const std::shared_ptr<tgfx::Typef
return hbFont;
}

static std::mutex _mutex{};

static hb_buffer_t* HarfbuzzBufferCreate() {
// hb_buffer_create会创建并依赖一些全局的静态属性,在异步调用hb_buffer_destroy时会被释放,引发空指针,所以需要加锁
std::lock_guard<std::mutex> autoLock(_mutex);
return hb_buffer_create();
}

static void HarfbuzzBufferDestroy(hb_buffer_t* buffer) {
std::lock_guard<std::mutex> autoLock(_mutex);
hb_buffer_destroy(buffer);
}

static std::vector<std::tuple<uint32_t, uint32_t, uint32_t>> Shape(
const std::string& text, const std::shared_ptr<tgfx::Typeface>& typeface) {
auto hbFont = CreateHBFont(typeface);
if (hbFont == nullptr) {
return {};
}

auto hbBuffer = std::shared_ptr<hb_buffer_t>(hb_buffer_create(), hb_buffer_destroy);
auto hbBuffer = std::shared_ptr<hb_buffer_t>(HarfbuzzBufferCreate(), HarfbuzzBufferDestroy);
if (!hb_buffer_allocation_successful(hbBuffer.get())) {
LOGI("TextShaperHarfbuzz::shape text = %s, alloc harfbuzz(%p) failure", text.c_str(),
hbBuffer.get());
Expand Down

0 comments on commit f361647

Please sign in to comment.