Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: add signal to invalidate buffers without doing layout #5123

Merged
merged 3 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
- Dev: Fix `NotebookTab` emitting updates for every message. (#5068)
- Dev: Added benchmark for parsing and building recent messages. (#5071)
- Dev: Boost is depended on as a header-only library when using conan. (#5107)
- Dev: Added signal to invalidate paint buffers of channel views without forcing a relayout. (#5123)

## 2.4.6

Expand Down
8 changes: 7 additions & 1 deletion src/messages/layouts/MessageLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ int MessageLayout::getWidth() const

// Layout
// return true if redraw is required
bool MessageLayout::layout(int width, float scale, MessageElementFlags flags)
bool MessageLayout::layout(int width, float scale, MessageElementFlags flags,
bool shouldInvalidateBuffer)
{
// BenchmarkGuard benchmark("MessageLayout::layout()");

Expand Down Expand Up @@ -108,6 +109,11 @@ bool MessageLayout::layout(int width, float scale, MessageElementFlags flags)

if (!layoutRequired)
{
if (shouldInvalidateBuffer)
{
this->invalidateBuffer();
return true;
}
return false;
}

Expand Down
3 changes: 2 additions & 1 deletion src/messages/layouts/MessageLayout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class MessageLayout

MessageLayoutFlags flags;

bool layout(int width, float scale_, MessageElementFlags flags);
bool layout(int width, float scale_, MessageElementFlags flags,
bool shouldInvalidateBuffer);

// Painting
MessagePaintResult paint(const MessagePaintContext &ctx);
Expand Down
9 changes: 7 additions & 2 deletions src/singletons/WindowManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ void WindowManager::forceLayoutChannelViews()
this->layoutChannelViews(nullptr);
}

void WindowManager::invalidateChannelViewBuffers(Channel *channel)
{
this->invalidateBuffersRequested.invoke(channel);
}

void WindowManager::repaintVisibleChatWidgets(Channel *channel)
{
this->layoutRequested.invoke(channel);
Expand Down Expand Up @@ -407,10 +412,10 @@ void WindowManager::initialize(Settings &settings, const Paths &paths)
this->forceLayoutChannelViews();
});
settings.alternateMessages.connect([this](auto, auto) {
this->forceLayoutChannelViews();
this->invalidateChannelViewBuffers();
});
settings.separateMessages.connect([this](auto, auto) {
this->forceLayoutChannelViews();
this->invalidateChannelViewBuffers();
});
settings.collpseMessagesMinLines.connect([this](auto, auto) {
this->forceLayoutChannelViews();
Expand Down
7 changes: 7 additions & 0 deletions src/singletons/WindowManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ class WindowManager final : public Singleton
// This is called, for example, when the emote scale or timestamp format has
// changed
void forceLayoutChannelViews();

// Tell a channel (or all channels if channel is nullptr) to invalidate all paint buffers
void invalidateChannelViewBuffers(Channel *channel = nullptr);

void repaintVisibleChatWidgets(Channel *channel = nullptr);
void repaintGifEmotes();

Expand Down Expand Up @@ -124,6 +128,9 @@ class WindowManager final : public Singleton
// This signal fires whenever views rendering a channel, or all views if the
// channel is a nullptr, need to redo their layout
pajlada::Signals::Signal<Channel *> layoutRequested;
// This signal fires whenever views rendering a channel, or all views if the
// channel is a nullptr, need to invalidate their paint buffers
pajlada::Signals::Signal<Channel *> invalidateBuffersRequested;

pajlada::Signals::NoArgSignal wordFlagsChanged;

Expand Down
29 changes: 24 additions & 5 deletions src/widgets/helper/ChannelView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ void ChannelView::initializeSignals()
}
});

this->signalHolder_.managedConnect(
getIApp()->getWindows()->invalidateBuffersRequested,
[this](Channel *channel) {
if (this->isVisible() &&
(channel == nullptr || this->channel_.get() == channel))
{
this->invalidateBuffers();
}
});

this->signalHolder_.managedConnect(getIApp()->getFonts()->fontChanged,
[this] {
this->queueLayout();
Expand Down Expand Up @@ -590,6 +600,12 @@ void ChannelView::queueUpdate(const QRect &area)
this->update(area);
}

void ChannelView::invalidateBuffers()
{
this->bufferInvalidationQueued_ = true;
this->queueLayout();
}

void ChannelView::queueLayout()
{
if (this->isVisible())
Expand Down Expand Up @@ -651,12 +667,13 @@ void ChannelView::layoutVisibleMessages(
{
const auto &message = messages[i];

redrawRequired |=
message->layout(layoutWidth, this->scale(), flags);
redrawRequired |= message->layout(layoutWidth, this->scale(), flags,
this->bufferInvalidationQueued_);

y += message->getHeight();
}
}
this->bufferInvalidationQueued_ = false;

if (redrawRequired)
{
Expand Down Expand Up @@ -685,7 +702,7 @@ void ChannelView::updateScrollbar(
{
auto *message = messages[i].get();

message->layout(layoutWidth, this->scale(), flags);
message->layout(layoutWidth, this->scale(), flags, false);

h -= message->getHeight();

Expand Down Expand Up @@ -1656,7 +1673,8 @@ void ChannelView::wheelEvent(QWheelEvent *event)
else
{
snapshot[i - 1]->layout(this->getLayoutWidth(),
this->scale(), this->getFlags());
this->scale(), this->getFlags(),
false);
scrollFactor = 1;
currentScrollLeft = snapshot[i - 1]->getHeight();
}
Expand Down Expand Up @@ -1690,7 +1708,8 @@ void ChannelView::wheelEvent(QWheelEvent *event)
else
{
snapshot[i + 1]->layout(this->getLayoutWidth(),
this->scale(), this->getFlags());
this->scale(), this->getFlags(),
false);

scrollFactor = 1;
currentScrollLeft = snapshot[i + 1]->getHeight();
Expand Down
3 changes: 3 additions & 0 deletions src/widgets/helper/ChannelView.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class ChannelView final : public BaseWidget
bool hasSourceChannel() const;

LimitedQueueSnapshot<MessageLayoutPtr> &getMessagesSnapshot();

void queueLayout();
void invalidateBuffers();

void clearMessages();

Expand Down Expand Up @@ -270,6 +272,7 @@ class ChannelView final : public BaseWidget
bool canReplyToMessages() const;

bool layoutQueued_ = false;
bool bufferInvalidationQueued_ = false;

bool lastMessageHasAlternateBackground_ = false;
bool lastMessageHasAlternateBackgroundReverse_ = true;
Expand Down
Loading