I'v managed to drop the CPU usage quite a lot with those patches:
001 and 002 shouldn't matter.

to get maximum CPU gain: go to prefs, animation speed = 0, it will remove
the animations from the profile when you switch dives, and also the tooltip
animation.

Tomaz
From 3c899500ed45d03f559167938aaeca58591b9428 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Thu, 15 Jan 2015 18:19:53 -0200
Subject: [PATCH 3/5] Force the ToolTip to have a refresh rate of 30 fps

We don't need more than 30fps and we were doing it more than
150fps sometimes. this brings the CPU doen to ~7% ( where 25% means
100% ) in my machine. I'm still looking in ways to improve the speed.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetooltipitem.cpp | 6 ++++++
 qt-ui/profile/divetooltipitem.h   | 3 ++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp
index 790c222..d05336d 100644
--- a/qt-ui/profile/divetooltipitem.cpp
+++ b/qt-ui/profile/divetooltipitem.cpp
@@ -171,6 +171,7 @@ ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent),
 
 	setBrush(QBrush(Qt::white));
 	setPen(QPen(Qt::black, 0.5));
+	refreshTimer.start();
 }
 
 ToolTipItem::~ToolTipItem()
@@ -248,6 +249,11 @@ void ToolTipItem::refresh(const QPointF &pos)
 	static QPainter painter(&tissues);
 	static struct membuffer mb = { 0 };
 
+	if (refreshTimer.elapsed() < 30) {
+		return;
+	}
+	refreshTimer.restart();
+
 	int time = timeAxis->valueAt(pos);
 	if (time == lastTime)
 		return;
diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h
index ca5bc89..694fb0d 100644
--- a/qt-ui/profile/divetooltipitem.h
+++ b/qt-ui/profile/divetooltipitem.h
@@ -6,6 +6,7 @@
 #include <QPair>
 #include <QRectF>
 #include <QIcon>
+#include <QTime>
 #include "display.h"
 
 class DiveCartesianAxis;
@@ -60,7 +61,7 @@ private:
 	DiveCartesianAxis *timeAxis;
 	plot_info pInfo;
 	int lastTime;
-
+	QTime refreshTimer;
 	QList<QGraphicsItem*> oldSelection;
 };
 
-- 
2.2.2

From 4ab0be15916d0fb774eb40073cecabbfd247fdef Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Thu, 15 Jan 2015 19:08:24 -0200
Subject: [PATCH 4/5] Miscellaneous fixes for speed.

- Do not use QGraphicsPathItem, use QGraphicsRectItem instread
- Remove the calcualtions for the title and separator bars, they
 are just uneeded.
- QImage instead of QPixmap for painting.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetooltipitem.cpp | 122 +++++++++++---------------------------
 qt-ui/profile/divetooltipitem.h   |  15 ++---
 2 files changed, 38 insertions(+), 99 deletions(-)

diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp
index d05336d..cc9239d 100644
--- a/qt-ui/profile/divetooltipitem.cpp
+++ b/qt-ui/profile/divetooltipitem.cpp
@@ -13,6 +13,7 @@
 #include <QSettings>
 #include <QGraphicsView>
 #include <QDebug>
+#include <QStyleOptionGraphicsItem>
 
 #define PORT_IN_PROGRESS 1
 #ifdef PORT_IN_PROGRESS
@@ -24,7 +25,7 @@ void ToolTipItem::addToolTip(const QString &toolTip, const QIcon &icon, const QP
 	const IconMetrics& iconMetrics = defaultIconMetrics();
 
 	QGraphicsPixmapItem *iconItem = 0;
-	double yValue = title->boundingRect().height() + iconMetrics.spacing;
+	double yValue = iconMetrics.spacing;
 	Q_FOREACH (ToolTip t, toolTips) {
 		yValue += t.second->boundingRect().height();
 	}
@@ -55,27 +56,6 @@ void ToolTipItem::clear()
 	toolTips.clear();
 }
 
-void ToolTipItem::setRect(const QRectF &r)
-{
-	if( r == rectangle ) {
-		return;
-	}
-
-	rectangle = r;
-
-	// Creates a 2pixels border
-	QPainterPath border;
-	border.addRoundedRect(-4, -4, rectangle.width() + 8, rectangle.height() + 10, 3, 3);
-	border.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
-	setPath(border);
-
-	QPainterPath bg;
-	bg.addRoundedRect(-1, -1, rectangle.width() + 3, rectangle.height() + 4, 3, 3);
-
-	background->setPath(bg);
-	updateTitlePosition();
-}
-
 void ToolTipItem::collapse()
 {
 	int dim = defaultIconMetrics().sz_small;
@@ -92,12 +72,8 @@ void ToolTipItem::collapse()
 
 void ToolTipItem::expand()
 {
-	if (!title)
-		return;
-
 	const IconMetrics& iconMetrics = defaultIconMetrics();
-
-	double width = 0, height = title->boundingRect().height() + iconMetrics.spacing;
+	double width = 0, height = iconMetrics.spacing;
 	Q_FOREACH (const ToolTip& t, toolTips) {
 		if (t.second->boundingRect().width() > width)
 			width = t.second->boundingRect().width();
@@ -113,19 +89,16 @@ void ToolTipItem::expand()
 	/*       Left padding, Icon Size,   space, right padding */
 	width += iconMetrics.spacing + iconMetrics.sz_small + iconMetrics.spacing + iconMetrics.spacing;
 
-	if (width < title->boundingRect().width() + iconMetrics.spacing * 2)
-		width = title->boundingRect().width() + iconMetrics.spacing * 2;
-
 	if (height < iconMetrics.sz_small)
 		height = iconMetrics.sz_small;
 
 	nextRectangle.setWidth(width);
 	nextRectangle.setHeight(height);
 
-	if (nextRectangle != rectangle) {
+	if (nextRectangle != rect()) {
 		QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this);
 		animation->setDuration(100);
-		animation->setStartValue(rectangle);
+		animation->setStartValue(rect());
 		animation->setEndValue(nextRectangle);
 		animation->start(QAbstractAnimation::DeleteWhenStopped);
 	}
@@ -133,44 +106,32 @@ void ToolTipItem::expand()
 	status = EXPANDED;
 }
 
-ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsPathItem(parent),
-	background(0),
-	separator(new QGraphicsLineItem(this)),
-	title(new QGraphicsSimpleTextItem(tr("Information"), this)),
+ToolTipItem::ToolTipItem(QGraphicsItem *parent) : QGraphicsRectItem(parent),
 	status(COLLAPSED),
 	timeAxis(0),
-	lastTime(-1)
+	lastTime(-1),
+	tissues(16,60,QImage::Format_ARGB32_Premultiplied)
 {
 	memset(&pInfo, 0, sizeof(pInfo));
 	entryToolTip.first = NULL;
 	entryToolTip.second = NULL;
 	setFlags(ItemIgnoresTransformations | ItemIsMovable | ItemClipsChildrenToShape);
-
-	QColor c = QColor(Qt::black);
-	c.setAlpha(155);
-	background = new QGraphicsPathItem(this);
-	background->setFlag(ItemStacksBehindParent);
-	background->setFlag(ItemIgnoresTransformations);
-	background->setBrush(c);
-	background->setPen(QPen(QBrush(Qt::transparent), 0));
-	background->setZValue(-10);
-
-	updateTitlePosition();
 	setZValue(99);
 
 	addToolTip(QString(), QIcon(), QPixmap(16,60));
 	entryToolTip = toolTips.first();
 	toolTips.clear();
 
-	separator->setFlag(ItemIgnoresTransformations);
-	separator->setPen(QPen(Qt::white));
-
-	title->setFlag(ItemIgnoresTransformations);
-	title->setPen(QPen(Qt::white, 1));
-	title->setBrush(Qt::white);
+	QPainter painter(&tissues);
+	painter.setPen(QColor(0, 0, 0, 0));
+	painter.setBrush(QColor(LIMENADE1));
+	painter.drawRect(0, 10 + (100 - AMB_PERCENTAGE) / 2, 16, AMB_PERCENTAGE / 2);
+	painter.setBrush(QColor(SPRINGWOOD1));
+	painter.drawRect(0, 10, 16, (100 - AMB_PERCENTAGE) / 2);
+	painter.setBrush(QColor(Qt::red));
+	painter.drawRect(0,0,16,10);
+	painter.end();
 
-	setBrush(QBrush(Qt::white));
-	setPen(QPen(Qt::black, 0.5));
 	refreshTimer.start();
 }
 
@@ -179,26 +140,6 @@ ToolTipItem::~ToolTipItem()
 	clear();
 }
 
-void ToolTipItem::updateTitlePosition()
-{
-	const IconMetrics& iconMetrics = defaultIconMetrics();
-	if (rectangle.width() < title->boundingRect().width() + iconMetrics.spacing * 4) {
-		QRectF newRect = rectangle;
-		newRect.setWidth(title->boundingRect().width() + iconMetrics.spacing * 4);
-		newRect.setHeight((newRect.height() && isExpanded()) ? newRect.height() : iconMetrics.sz_small);
-		setRect(newRect);
-	}
-
-	title->setPos(boundingRect().width() / 2 - title->boundingRect().width() / 2 - 1, 0);
-
-	double x1 = 3;
-	double y1 = title->pos().y() + iconMetrics.spacing / 2 + title->boundingRect().height();
-	double x2 = boundingRect().width() - 10;
-	double y2 = y1;
-
-	separator->setLine(x1, y1, x2, y2);
-}
-
 bool ToolTipItem::isExpanded() const
 {
 	return status == EXPANDED;
@@ -207,7 +148,7 @@ bool ToolTipItem::isExpanded() const
 void ToolTipItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
 {
 	persistPos();
-	QGraphicsPathItem::mouseReleaseEvent(event);
+	QGraphicsRectItem::mouseReleaseEvent(event);
 	Q_FOREACH (QGraphicsItem *item, oldSelection) {
 		item->setSelected(true);
 	}
@@ -245,11 +186,9 @@ void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis)
 void ToolTipItem::refresh(const QPointF &pos)
 {
 	struct plot_data *entry;
-	static QPixmap tissues(16,60);
-	static QPainter painter(&tissues);
 	static struct membuffer mb = { 0 };
 
-	if (refreshTimer.elapsed() < 30) {
+	if (refreshTimer.elapsed() < 40) {
 		return;
 	}
 	refreshTimer.restart();
@@ -264,14 +203,8 @@ void ToolTipItem::refresh(const QPointF &pos)
 	mb.len = 0;
 	entry = get_plot_details_new(&pInfo, time, &mb);
 	if (entry) {
-		tissues.fill();
-		painter.setPen(QColor(0, 0, 0, 0));
-		painter.setBrush(QColor(LIMENADE1));
-		painter.drawRect(0, 10 + (100 - AMB_PERCENTAGE) / 2, 16, AMB_PERCENTAGE / 2);
-		painter.setBrush(QColor(SPRINGWOOD1));
-		painter.drawRect(0, 10, 16, (100 - AMB_PERCENTAGE) / 2);
-		painter.setBrush(QColor(Qt::red));
-		painter.drawRect(0,0,16,10);
+		QImage pix = tissues;
+		QPainter painter(&pix);
 		painter.setPen(QColor(0, 0, 0, 255));
 		painter.drawLine(0, 60 - entry->gfline / 2, 16, 60 - entry->gfline / 2);
 		painter.drawLine(0, 60 - AMB_PERCENTAGE * (entry->pressures.n2 + entry->pressures.he) / entry->ambpressure / 2,
@@ -280,7 +213,7 @@ void ToolTipItem::refresh(const QPointF &pos)
 		for (int i=0; i<16; i++) {
 			painter.drawLine(i, 60, i, 60 - entry->percentages[i] / 2);
 		}
-		entryToolTip.first->setPixmap(tissues);
+		entryToolTip.first->setPixmap(QPixmap::fromImage(pix));
 		entryToolTip.second->setText(QString::fromUtf8(mb.buffer, mb.len));
 	}
 
@@ -298,3 +231,14 @@ void ToolTipItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
 	scene()->clearSelection();
 	QGraphicsItem::mousePressEvent(event);
 }
+
+void ToolTipItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
+{
+    Q_UNUSED(widget);
+    painter->save();
+    painter->setClipRect(option->exposedRect);
+    painter->setPen(QPen(QBrush(Qt::white), 1));
+    painter->setBrush(QBrush(QColor(0,0,0,155)));
+    painter->drawRect(rect());
+    painter->restore();
+}
diff --git a/qt-ui/profile/divetooltipitem.h b/qt-ui/profile/divetooltipitem.h
index 694fb0d..614feb6 100644
--- a/qt-ui/profile/divetooltipitem.h
+++ b/qt-ui/profile/divetooltipitem.h
@@ -7,6 +7,7 @@
 #include <QRectF>
 #include <QIcon>
 #include <QTime>
+#include <QImage>
 #include "display.h"
 
 class DiveCartesianAxis;
@@ -18,10 +19,9 @@ struct graphics_context;
 /* To use a tooltip, simply ->setToolTip on the QGraphicsItem that you want
  * or, if it's a "global" tooltip, set it on the mouseMoveEvent of the ProfileGraphicsView.
  */
-class ToolTipItem : public QObject, public QGraphicsPathItem {
+class ToolTipItem : public QObject, public QGraphicsRectItem {
 	Q_OBJECT
-	void updateTitlePosition();
-	Q_PROPERTY(QRectF rect READ boundingRect WRITE setRect)
+	Q_PROPERTY(QRectF rect READ rect WRITE setRect)
 
 public:
 	enum Status {
@@ -44,25 +44,20 @@ public:
 	void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
 	void setTimeAxis(DiveCartesianAxis *axis);
 	void setPlotInfo(const plot_info &plot);
-public
-slots:
-	void setRect(const QRectF &rect);
+	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
 
 private:
 	typedef QPair<QGraphicsPixmapItem *, QGraphicsSimpleTextItem *> ToolTip;
 	QVector<ToolTip> toolTips;
 	ToolTip entryToolTip;
-	QGraphicsPathItem *background;
-	QGraphicsLineItem *separator;
-	QGraphicsSimpleTextItem *title;
 	Status status;
-	QRectF rectangle;
 	QRectF nextRectangle;
 	DiveCartesianAxis *timeAxis;
 	plot_info pInfo;
 	int lastTime;
 	QTime refreshTimer;
 	QList<QGraphicsItem*> oldSelection;
+	QImage tissues;
 };
 
 #endif // DIVETOOLTIPITEM_H
-- 
2.2.2

From 8106f84a35523bfa51130e1b11a76afa2b323f42 Mon Sep 17 00:00:00 2001
From: Tomaz Canabrava <[email protected]>
Date: Thu, 15 Jan 2015 19:13:15 -0200
Subject: [PATCH 5/5] Honor Animation Speed on the ToolTip

If animation_speed == 0, do not try to animate.
this saves tons of cycles and it's specially good on older
machines.

Signed-off-by: Tomaz Canabrava <[email protected]>
---
 qt-ui/profile/divetooltipitem.cpp | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/qt-ui/profile/divetooltipitem.cpp b/qt-ui/profile/divetooltipitem.cpp
index cc9239d..a4de262 100644
--- a/qt-ui/profile/divetooltipitem.cpp
+++ b/qt-ui/profile/divetooltipitem.cpp
@@ -60,11 +60,15 @@ void ToolTipItem::collapse()
 {
 	int dim = defaultIconMetrics().sz_small;
 
-	QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
-	animation->setDuration(100);
-	animation->setStartValue(nextRectangle);
-	animation->setEndValue(QRect(0, 0, dim, dim));
-	animation->start(QAbstractAnimation::DeleteWhenStopped);
+	if (prefs.animation_speed) {
+		QPropertyAnimation *animation = new QPropertyAnimation(this, "rect");
+		animation->setDuration(prefs.animation_speed);
+		animation->setStartValue(nextRectangle);
+		animation->setEndValue(QRect(0, 0, dim, dim));
+		animation->start(QAbstractAnimation::DeleteWhenStopped);
+	} else {
+		setRect(nextRectangle);
+	}
 	clear();
 
 	status = COLLAPSED;
@@ -96,11 +100,15 @@ void ToolTipItem::expand()
 	nextRectangle.setHeight(height);
 
 	if (nextRectangle != rect()) {
-		QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this);
-		animation->setDuration(100);
-		animation->setStartValue(rect());
-		animation->setEndValue(nextRectangle);
-		animation->start(QAbstractAnimation::DeleteWhenStopped);
+		if (prefs.animation_speed != 0) {
+			QPropertyAnimation *animation = new QPropertyAnimation(this, "rect", this);
+			animation->setDuration(prefs.animation_speed);
+			animation->setStartValue(rect());
+			animation->setEndValue(nextRectangle);
+			animation->start(QAbstractAnimation::DeleteWhenStopped);
+		} else {
+			setRect(nextRectangle);
+		}
 	}
 
 	status = EXPANDED;
-- 
2.2.2

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to