TextDrawer: Improve performance for text wrapping (by measuring without wrapping first)

This commit is contained in:
Henrik Rydgård 2024-10-16 13:35:32 +02:00
parent 49e86f5e76
commit ce980af01e
3 changed files with 13 additions and 8 deletions

View File

@ -18,8 +18,6 @@ TextDrawer::TextDrawer(Draw::DrawContext *draw) : draw_(draw) {
// These probably shouldn't be state.
dpiScale_ = CalculateDPIScale();
}
TextDrawer::~TextDrawer() {
}
float TextDrawerWordWrapper::MeasureWidth(std::string_view str) {
float w, h;
@ -124,12 +122,19 @@ void TextDrawer::DrawString(DrawBuffer &target, std::string_view str, float x, f
}
void TextDrawer::MeasureStringRect(std::string_view str, const Bounds &bounds, float *w, float *h, int align) {
std::string toMeasure = std::string(str);
int wrap = align & (FLAG_WRAP_TEXT | FLAG_ELLIPSIZE_TEXT);
if (wrap) {
float plainW, plainH;
MeasureString(str, &plainW, &plainH);
if (wrap && plainW > bounds.w) {
std::string toMeasure = std::string(str);
WrapString(toMeasure, toMeasure.c_str(), bounds.w, wrap);
MeasureString(toMeasure, w, h);
} else {
*w = plainW;
*h = plainH;
}
MeasureString(toMeasure, w, h);
}
void TextDrawer::DrawStringRect(DrawBuffer &target, std::string_view str, const Bounds &bounds, uint32_t color, int align) {
@ -186,7 +191,7 @@ void TextDrawer::OncePerFrame() {
}
// Drop old strings. Use a prime number to reduce clashing with other rhythms
if (frameCount_ % 23 == 0) {
if (frameCount_ % 63 == 0) {
for (auto iter = cache_.begin(); iter != cache_.end();) {
if (frameCount_ - iter->second->lastUsedFrame > 100) {
if (iter->second->texture)

View File

@ -42,7 +42,7 @@ struct TextMeasureEntry {
class TextDrawer {
public:
virtual ~TextDrawer();
virtual ~TextDrawer() = default;
virtual bool IsReady() const { return true; }
virtual uint32_t SetFont(const char *fontName, int size, int flags) = 0;

View File

@ -571,7 +571,7 @@ public:
TaskPriority Priority() const override {
return TaskPriority::NORMAL;
}
virtual void Run() {
virtual void Run() override {
mixer_->LoadSamplesOnThread();
}
private: