Skip to content

Commit

Permalink
[deploy] pref: auto enable text pre render mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Nomango committed Sep 13, 2023
1 parent 6f880db commit 946bada
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
24 changes: 16 additions & 8 deletions src/kiwano/2d/TextActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,16 +222,20 @@ void TextActor::SetTextLayout(TextLayoutPtr layout)

void TextActor::SetPreRenderEnabled(bool enable)
{
if (enable)
const bool enabled = texture_cached_ != nullptr;
if (enabled != enable)
{
texture_cached_ = MakePtr<Texture>();
}
else
{
texture_cached_ = nullptr;
if (enable)
{
texture_cached_ = MakePtr<Texture>();
}
else
{
texture_cached_ = nullptr;
}
render_ctx_ = nullptr;
is_cache_dirty_ = true;
}
render_ctx_ = nullptr;
is_cache_dirty_ = true;
}

void TextActor::Update(Duration dt)
Expand Down Expand Up @@ -273,6 +277,10 @@ void TextActor::ForceUpdateLayout()

void TextActor::UpdateCachedTexture()
{
// 有文字描边或其他额外渲染时,需要开启预渲染以提升性能
this->SetPreRenderEnabled(outline_brush_ || style_.show_strikethrough || style_.show_underline
|| (layout_ && layout_->GetContentLength() > 30));

if (!texture_cached_)
{
return;
Expand Down
8 changes: 4 additions & 4 deletions src/kiwano/2d/TextActor.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ class KGE_API TextActor : public Actor
/// @brief 设置文本布局
void SetTextLayout(TextLayoutPtr layout);

/// \~chinese
/// @brief 设置预渲染模式,在描边等情况下会有更好的性能
void SetPreRenderEnabled(bool enable);

/// \~chinese
/// @brief 更新脏文字布局
/// @details 仅当文字布局脏时更新
Expand All @@ -165,6 +161,10 @@ class KGE_API TextActor : public Actor

void UpdateCachedTexture();

/// \~chinese
/// @brief 设置预渲染模式,在描边等情况下会有更好的性能
void SetPreRenderEnabled(bool enable);

private:
bool is_cache_dirty_;
String content_;
Expand Down
9 changes: 9 additions & 0 deletions src/kiwano/render/TextLayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class KGE_API TextLayout : public NativeObject
/// @brief 获取文本行数
uint32_t GetLineCount() const;

/// \~chinese
/// @brief 获取文本长度
uint32_t GetContentLength() const;

/// \~chinese
/// @brief 设置字体
/// @param font 字体
Expand Down Expand Up @@ -136,6 +140,11 @@ inline void TextLayout::Clear()
ResetNativePointer();
}

inline uint32_t TextLayout::GetContentLength() const
{
return content_length_;
}

inline TextLayout::DirtyFlag TextLayout::GetDirtyFlag() const
{
return dirty_flag_;
Expand Down

0 comments on commit 946bada

Please sign in to comment.