added graphics cache for drawn messages

This commit is contained in:
fourtf 2017-02-03 19:31:51 +01:00
parent a92c3dc2a4
commit 759e0aea50
7 changed files with 143 additions and 86 deletions

View file

@ -15,7 +15,7 @@ template <typename T>
class LimitedQueue class LimitedQueue
{ {
public: public:
LimitedQueue(int limit = 10, int buffer = 5) LimitedQueue(int limit = 100, int buffer = 25)
: vector(new std::vector<T>(limit + buffer)) : vector(new std::vector<T>(limit + buffer))
, vectorPtr(this->vector) , vectorPtr(this->vector)
, mutex() , mutex()

View file

@ -29,8 +29,8 @@ public:
T const &operator[](int index) const T const &operator[](int index) const
{ {
assert(index >= 0); // assert(index >= 0);
assert(index < length); // assert(index < length);
return vector->at(index + offset); return vector->at(index + offset);
} }

View file

@ -2,6 +2,8 @@
#include "emotes.h" #include "emotes.h"
#include "settings.h" #include "settings.h"
#include <QDebug>
#define MARGIN_LEFT 8 #define MARGIN_LEFT 8
#define MARGIN_RIGHT 8 #define MARGIN_RIGHT 8
#define MARGIN_TOP 8 #define MARGIN_TOP 8
@ -14,6 +16,7 @@ MessageRef::MessageRef(std::shared_ptr<Message> message)
: message(message.get()) : message(message.get())
, messagePtr(message) , messagePtr(message)
, wordParts() , wordParts()
, buffer()
{ {
} }
@ -22,14 +25,16 @@ MessageRef::layout(int width, bool enableEmoteMargins)
{ {
auto &settings = Settings::getInstance(); auto &settings = Settings::getInstance();
width = width - (width % 2); bool sizeChanged = width != this->currentLayoutWidth;
bool redraw = width != this->currentLayoutWidth;
int mediumTextLineHeight = Fonts::getFontMetrics(Fonts::Medium).height();
int spaceWidth = 4; int spaceWidth = 4;
bool redraw = width != this->currentLayoutWidth || this->relayoutRequested; {
int mediumTextLineHeight =
Fonts::getFontMetrics(Fonts::Medium).height();
bool recalculateImages = this->emoteGeneration != Emotes::getGeneration(); bool recalculateImages =
this->emoteGeneration != Emotes::getGeneration();
bool recalculateText = this->fontGeneration != Fonts::getGeneration(); bool recalculateText = this->fontGeneration != Fonts::getGeneration();
qreal emoteScale = settings.emoteScale.get(); qreal emoteScale = settings.emoteScale.get();
@ -50,7 +55,8 @@ MessageRef::layout(int width, bool enableEmoteMargins)
qreal h = image.getHeight(); qreal h = image.getHeight();
if (scaleEmotesByLineHeight) { if (scaleEmotesByLineHeight) {
word.setSize(w * mediumTextLineHeight / h * emoteScale, word.setSize(
w * mediumTextLineHeight / h * emoteScale,
mediumTextLineHeight * emoteScale); mediumTextLineHeight * emoteScale);
} else { } else {
word.setSize(w * image.getScale() * emoteScale, word.setSize(w * image.getScale() * emoteScale,
@ -66,11 +72,14 @@ MessageRef::layout(int width, bool enableEmoteMargins)
} }
} }
} }
}
if (!redraw) { if (!redraw) {
return false; return false;
} }
this->currentLayoutWidth = width;
int x = MARGIN_LEFT; int x = MARGIN_LEFT;
int y = MARGIN_TOP; int y = MARGIN_TOP;
@ -184,7 +193,16 @@ MessageRef::layout(int width, bool enableEmoteMargins)
this->alignWordParts(lineStart, lineHeight); this->alignWordParts(lineStart, lineHeight);
if (this->height != y + lineHeight) {
sizeChanged = true;
this->height = y + lineHeight; this->height = y + lineHeight;
}
if (sizeChanged) {
this->buffer = nullptr;
}
this->updateBuffer = true;
return true; return true;
} }

View file

@ -1,9 +1,11 @@
#ifndef MESSAGEREF_H #ifndef MESSAGEREF_H
#define MESSAGEREF_H #define MESSAGEREF_H
#include <memory>
#include "messages/message.h" #include "messages/message.h"
#include <QPixmap>
#include <memory>
namespace chatterino { namespace chatterino {
namespace messages { namespace messages {
@ -26,18 +28,15 @@ public:
bool layout(int width, bool enableEmoteMargins = true); bool layout(int width, bool enableEmoteMargins = true);
void
requestRelayout()
{
relayoutRequested = true;
}
const std::vector<WordPart> & const std::vector<WordPart> &
getWordParts() const getWordParts() const
{ {
return wordParts; return wordParts;
} }
std::shared_ptr<QPixmap> buffer = nullptr;
bool updateBuffer = false;
private: private:
Message *message; Message *message;
std::shared_ptr<Message> messagePtr; std::shared_ptr<Message> messagePtr;
@ -47,7 +46,6 @@ private:
int height = 0; int height = 0;
int currentLayoutWidth = -1; int currentLayoutWidth = -1;
bool relayoutRequested = true;
int fontGeneration = -1; int fontGeneration = -1;
int emoteGeneration = -1; int emoteGeneration = -1;

View file

@ -46,6 +46,7 @@ ChatWidgetHeader::ChatWidgetHeader(ChatWidget *parent)
this->leftMenu.addAction("Close split", this, SLOT(menuCloseSplit()), this->leftMenu.addAction("Close split", this, SLOT(menuCloseSplit()),
QKeySequence(tr("Ctrl+W"))); QKeySequence(tr("Ctrl+W")));
this->leftMenu.addAction("Move split", this, SLOT(menuMoveSplit())); this->leftMenu.addAction("Move split", this, SLOT(menuMoveSplit()));
this->leftMenu.addAction("Popup", this, SLOT(menuPopup()));
this->leftMenu.addSeparator(); this->leftMenu.addSeparator();
this->leftMenu.addAction("Change channel", this, SLOT(menuChangeChannel()), this->leftMenu.addAction("Change channel", this, SLOT(menuChangeChannel()),
QKeySequence(tr("Ctrl+R"))); QKeySequence(tr("Ctrl+R")));
@ -182,6 +183,13 @@ ChatWidgetHeader::menuMoveSplit()
{ {
} }
void void
ChatWidgetHeader::menuPopup()
{
auto widget = new ChatWidget();
widget->setChannelName(this->chatWidget->getChannelName());
widget->show();
}
void
ChatWidgetHeader::menuChangeChannel() ChatWidgetHeader::menuChangeChannel()
{ {
this->chatWidget->showChangeChannelPopup(); this->chatWidget->showChangeChannelPopup();

View file

@ -61,6 +61,7 @@ private slots:
void menuAddSplit(); void menuAddSplit();
void menuCloseSplit(); void menuCloseSplit();
void menuMoveSplit(); void menuMoveSplit();
void menuPopup();
void menuChangeChannel(); void menuChangeChannel();
void menuClearChat(); void menuClearChat();
void menuOpenChannel(); void menuOpenChannel();

View file

@ -37,17 +37,33 @@ ChatWidgetView::~ChatWidgetView()
bool bool
ChatWidgetView::layoutMessages() ChatWidgetView::layoutMessages()
{ {
bool showScrollbar = false;
auto messages = chatWidget->getMessagesSnapshot(); auto messages = chatWidget->getMessagesSnapshot();
bool redraw = false; if (messages.getLength() == 0) {
this->scrollbar.setVisible(false);
// for (std::shared_ptr<messages::Message> &message : messages) { return false;
// redraw |= message.get()->layout(this->width(), true); }
// }
redraw = true; bool showScrollbar = false, redraw = false;
int start = this->scrollbar.getCurrentValue();
int y = -(messages[start].get()->getHeight() *
(fmod(this->scrollbar.getCurrentValue(), 1)));
for (int i = start; i < messages.getLength(); ++i) {
auto messagePtr = messages[i];
auto message = messagePtr.get();
redraw |= message->layout(this->width(), true);
y += message->getHeight();
if (y >= height()) {
break;
}
}
int h = this->height() - 8; int h = this->height() - 8;
@ -87,18 +103,15 @@ ChatWidgetView::resizeEvent(QResizeEvent *)
void void
ChatWidgetView::paintEvent(QPaintEvent *) ChatWidgetView::paintEvent(QPaintEvent *)
{ {
QPainter painter(this); QPainter _painter(this);
painter.setRenderHint(QPainter::SmoothPixmapTransform); _painter.setRenderHint(QPainter::SmoothPixmapTransform);
// auto c = this->chatWidget->getChannel();
QColor color;
ColorScheme &scheme = ColorScheme::getInstance(); ColorScheme &scheme = ColorScheme::getInstance();
// code for tesing colors // code for tesing colors
/* /*
QColor color;
static ConcurrentMap<qreal, QImage *> imgCache; static ConcurrentMap<qreal, QImage *> imgCache;
std::function<QImage *(qreal)> getImg = [&scheme](qreal light) { std::function<QImage *(qreal)> getImg = [&scheme](qreal light) {
@ -129,11 +142,6 @@ ChatWidgetView::paintEvent(QPaintEvent *)
painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/ painter.fillRect(QRect(0, 9, 500, 2), QColor(0, 0, 0));*/
// if (c == NULL)
// return;
// auto messages = c->getMessageSnapshot();
auto messages = chatWidget->getMessagesSnapshot(); auto messages = chatWidget->getMessagesSnapshot();
int start = this->scrollbar.getCurrentValue(); int start = this->scrollbar.getCurrentValue();
@ -148,20 +156,37 @@ ChatWidgetView::paintEvent(QPaintEvent *)
for (int i = start; i < messages.getLength(); ++i) { for (int i = start; i < messages.getLength(); ++i) {
messages::MessageRef *messageRef = messages[i].get(); messages::MessageRef *messageRef = messages[i].get();
for (messages::WordPart const &wordPart : messageRef->getWordParts()) { std::shared_ptr<QPixmap> bufferPtr = messageRef->buffer;
QPixmap *buffer = bufferPtr.get();
bool updateBuffer = messageRef->updateBuffer;
if (buffer == nullptr) {
buffer = new QPixmap(this->width(), messageRef->getHeight());
bufferPtr = std::shared_ptr<QPixmap>(buffer);
updateBuffer = true;
}
if (updateBuffer) {
QPainter painter(buffer);
painter.fillRect(buffer->rect(), scheme.ChatBackground);
for (messages::WordPart const &wordPart :
messageRef->getWordParts()) {
painter.setPen(QColor(255, 0, 0)); painter.setPen(QColor(255, 0, 0));
painter.drawRect(wordPart.getX(), wordPart.getY() + y, painter.drawRect(wordPart.getX(), wordPart.getY(),
wordPart.getWidth(), wordPart.getHeight()); wordPart.getWidth(), wordPart.getHeight());
// image // image
if (wordPart.getWord().isImage()) { if (wordPart.getWord().isImage()) {
messages::LazyLoadedImage &lli = wordPart.getWord().getImage(); messages::LazyLoadedImage &lli =
wordPart.getWord().getImage();
const QPixmap *image = lli.getPixmap(); const QPixmap *image = lli.getPixmap();
if (image != NULL) { if (image != NULL) {
painter.drawPixmap( painter.drawPixmap(
QRect(wordPart.getX(), wordPart.getY() + y, QRect(wordPart.getX(), wordPart.getY(),
wordPart.getWidth(), wordPart.getHeight()), wordPart.getWidth(), wordPart.getHeight()),
*image); *image);
} }
@ -176,12 +201,19 @@ ChatWidgetView::paintEvent(QPaintEvent *)
painter.setFont(wordPart.getWord().getFont()); painter.setFont(wordPart.getWord().getFont());
painter.drawText( painter.drawText(
QRectF(wordPart.getX(), wordPart.getY() + y, 10000, 10000), QRectF(wordPart.getX(), wordPart.getY(), 10000, 10000),
wordPart.getText(), wordPart.getText(),
QTextOption(Qt::AlignLeft | Qt::AlignTop)); QTextOption(Qt::AlignLeft | Qt::AlignTop));
} }
} }
messageRef->updateBuffer = false;
}
messageRef->buffer = bufferPtr;
_painter.drawPixmap(0, y, *buffer);
y += messageRef->getHeight(); y += messageRef->getHeight();
if (y > height()) { if (y > height()) {