Skip to content

Commit

Permalink
TextDrawer: Improve performance for text wrapping (by measuring witho…
Browse files Browse the repository at this point in the history
…ut wrapping first)
  • Loading branch information
hrydgard committed Oct 16, 2024
1 parent 49e86f5 commit ce980af
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions Common/Render/Text/draw_text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Common/Render/Text/draw_text.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion UI/BackgroundAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ class SampleLoadTask : public Task {
TaskPriority Priority() const override {
return TaskPriority::NORMAL;
}
virtual void Run() {
virtual void Run() override {
mixer_->LoadSamplesOnThread();
}
private:
Expand Down

0 comments on commit ce980af

Please sign in to comment.