0
0
mirror of https://github.com/yse/easy_profiler.git synced 2024-12-28 01:04:41 +08:00
easy_profiler/profiler_gui/blocks_graphics_view.cpp

723 lines
21 KiB
C++
Raw Normal View History

2016-06-26 18:56:40 +03:00
/************************************************************************
* file name : blocks_graphics_view.cpp
* ----------------- :
* creation time : 2016/06/26
* copyright : (c) 2016 Victor Zarubkin
* author : Victor Zarubkin
* email : v.s.zarubkin@gmail.com
* ----------------- :
* description : The file contains implementation of GraphicsScene and GraphicsView and
* : it's auxiliary classes for displyaing easy_profiler blocks tree.
* ----------------- :
* change log : * 2016/06/26 Victor Zarubkin: Moved sources from graphics_view.h
2016-06-26 19:06:53 +03:00
* : and renamed classes from My* to Prof*.
* :
* : * 2016/06/27 Victor Zarubkin: Added text shifting relatively to it's parent item.
* : Disabled border lines painting because of vertical lines painting bug.
* : Changed height of blocks. Variable thread-block height.
* :
* : * 2016/06/29 Victor Zarubkin: Highly optimized painting performance and memory consumption.
* :
* : * 2016/06/30 Victor Zarubkin: Replaced doubles with floats (in ProfBlockItem) for less memory consumption.
* :
* : *
2016-06-26 18:56:40 +03:00
* ----------------- :
* license : TODO: add license text
************************************************************************/
2016-06-26 18:46:51 +03:00
#include <QWheelEvent>
#include <QMouseEvent>
#include <QSignalBlocker>
#include <QScrollBar>
#include <math.h>
2016-06-26 18:46:51 +03:00
#include "blocks_graphics_view.h"
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
const qreal GRAPHICS_ROW_SIZE = 16;
const qreal GRAPHICS_ROW_SIZE_FULL = GRAPHICS_ROW_SIZE + 1;
const qreal ROW_SPACING = 10;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef PROF_BLOCK_ITEM_OPT_MEMORY
void ProfBlockItem::setRect(preal _x, preal _y, preal _w, preal _h) {
x = _x; y = _y; w = _w; h = _h;
}
preal ProfBlockItem::left() const {
return x;
}
preal ProfBlockItem::top() const {
return y;
}
preal ProfBlockItem::width() const {
return w;
}
preal ProfBlockItem::height() const {
return h;
}
preal ProfBlockItem::right() const {
return x + w;
}
preal ProfBlockItem::bottom() const {
return y + h;
}
#else
void ProfBlockItem::setRect(preal _x, preal _y, preal _w, preal _h) {
rect.setRect(_x, _y, _w, _h);
}
preal ProfBlockItem::left() const {
return rect.left();
}
preal ProfBlockItem::top() const {
return rect.top();
}
preal ProfBlockItem::width() const {
return rect.width();
}
preal ProfBlockItem::height() const {
return rect.height();
}
preal ProfBlockItem::right() const {
return rect.right();
}
preal ProfBlockItem::bottom() const {
return rect.bottom();
}
#endif
//////////////////////////////////////////////////////////////////////////
ProfGraphicsItem::ProfGraphicsItem() : QGraphicsItem(nullptr), m_bTest(false)
2016-06-26 18:46:51 +03:00
{
}
ProfGraphicsItem::ProfGraphicsItem(bool _test) : QGraphicsItem(nullptr), m_bTest(_test)
{
}
ProfGraphicsItem::~ProfGraphicsItem()
{
}
QRectF ProfGraphicsItem::boundingRect() const
{
return m_rect;
}
void ProfGraphicsItem::paint(QPainter* _painter, const QStyleOptionGraphicsItem* _option, QWidget* _widget)
{
const auto nitems = m_items.size();
if (nitems == 0)
{
return;
}
const auto view = static_cast<ProfGraphicsView*>(scene()->parent());
const auto visibleSceneRect = view->visibleSceneRect(); // Current visible scene rect
const auto sceneLeft = visibleSceneRect.left() - x(), sceneRight = visibleSceneRect.right() - x();
if (m_rect.left() > sceneRight || m_rect.right() < sceneLeft)
{
// this frame is not visible at all
return;
}
const auto currentScale = view->currentScale(); // Current GraphicsView scale
const auto scaleRevert = 1.0 / currentScale; // Multiplier for reverting current GraphicsView scale
const auto screenWidth = m_rect.width() * currentScale; // Screen width of the top block
QRectF rect;
QBrush brush;
QPen pen = _painter->pen();
brush.setStyle(Qt::SolidPattern);
_painter->save();
_painter->setPen(Qt::NoPen);
if (screenWidth > 20)
{
// TODO: Test performance for drawText inside one loop with drawRect, but with _painter->save() and restore()
// and drawText in separate loop.
QRgb previousColor = 0;
for (auto& item : m_items)
{
item.draw = !(item.left() > sceneRight || item.right() < sceneLeft);
if (!item.draw)
{
// this item is fully out of scene visible rect
continue;
}
if (previousColor != item.color)
{
previousColor = item.color;
brush.setColor(previousColor);
_painter->setBrush(brush);
}
#ifdef PROF_BLOCK_ITEM_OPT_MEMORY
rect.setRect(item.x, item.y, item.w, item.h);
_painter->drawRect(rect);
#else
_painter->drawRect(item.rect);
#endif
}
_painter->setPen(Qt::SolidLine);
//_painter->setBrush(Qt::NoBrush);
_painter->setTransform(QTransform::fromScale(scaleRevert, 1), true);
if (m_bTest)
{
for (auto& item : m_items)
{
if (!item.draw)
continue;
auto w = item.width() * currentScale;
if (w < 20)
continue;
rect.setRect(item.left() * currentScale, item.top(), w, item.height());
_painter->drawText(rect, 0, "NOT VERY LONG TEST TEXT");
}
}
else
{
previousColor = 0;
for (auto& item : m_items)
{
if (!item.draw)
continue;
auto w = item.width() * currentScale;
if (w < 20)
continue;
rect.setRect(item.left() * currentScale, item.top(), w, item.height());
if (previousColor != item.color)
{
previousColor = item.color;
pen.setColor(0x00ffffff - previousColor);
_painter->setPen(pen);
}
//_painter->drawRect(rect);
_painter->drawText(rect, 0, item.block->node->getBlockName());
}
}
}
else
{
const auto& item = m_items.front();
brush.setColor(item.color);
_painter->setBrush(brush);
_painter->drawRect(m_rect);
//if (screenWidth > 10)
//{
// _painter->setTransform(QTransform::fromScale(scaleRevert, 1), true);
// //_painter->setBrush(Qt::NoBrush);
// rect.setRect(m_rect.left() * currentScale, m_rect.top(), screenWidth, m_rect.height());
// if (m_bTest)
// {
// _painter->setPen(Qt::SolidLine);
// _painter->drawText(rect, 0, "NOT VERY LONG TEST TEXT");
// }
// else
// {
// pen.setColor(0x00ffffff - item.color);
// _painter->setPen(pen);
// //_painter->drawRect(rect);
// _painter->drawText(rect, 0, item.block->node->getBlockName());
// }
//}
}
_painter->restore();
2016-06-26 18:46:51 +03:00
}
void ProfGraphicsItem::setBoundingRect(qreal x, qreal y, qreal w, qreal h)
{
m_rect.setRect(x, y, w, h);
}
void ProfGraphicsItem::setBoundingRect(const QRectF& _rect)
{
m_rect = _rect;
}
2016-06-26 18:46:51 +03:00
void ProfGraphicsItem::reserve(unsigned int _items)
2016-06-26 18:46:51 +03:00
{
m_items.reserve(_items);
2016-06-26 18:46:51 +03:00
}
const ProfGraphicsItem::Children& ProfGraphicsItem::items() const
2016-06-26 18:46:51 +03:00
{
return m_items;
2016-06-26 18:46:51 +03:00
}
const ProfBlockItem& ProfGraphicsItem::getItem(size_t _index) const
2016-06-26 18:46:51 +03:00
{
return m_items[_index];
2016-06-26 18:46:51 +03:00
}
ProfBlockItem& ProfGraphicsItem::getItem(size_t _index)
{
return m_items[_index];
}
size_t ProfGraphicsItem::addItem()
{
m_items.emplace_back();
return m_items.size() - 1;
}
size_t ProfGraphicsItem::addItem(const ProfBlockItem& _item)
{
m_items.emplace_back(_item);
return m_items.size() - 1;
}
size_t ProfGraphicsItem::addItem(ProfBlockItem&& _item)
{
m_items.emplace_back(::std::forward<ProfBlockItem&&>(_item));
return m_items.size() - 1;
}
2016-06-26 18:46:51 +03:00
// void ProfGraphicsItem::setLevels(unsigned int _levels)
// {
// m_levels.resize(_levels);
// }
//
// void ProfGraphicsItem::reserve(unsigned short _level, unsigned int _items)
// {
// m_levels[_level].reserve(_items);
// }
//
// const ProfGraphicsItem::Children& ProfGraphicsItem::items(unsigned short _level) const
// {
// return m_levels[_level];
// }
//
// const ProfBlockItem& ProfGraphicsItem::getItem(unsigned short _level, size_t _index) const
// {
// return m_levels[_level][_index];
// }
//
// ProfBlockItem& ProfGraphicsItem::getItem(unsigned short _level, size_t _index)
// {
// return m_levels[_level][_index];
// }
//
// size_t ProfGraphicsItem::addItem(unsigned short _level)
// {
// m_levels[_level].emplace_back();
// return m_levels[_level].size() - 1;
// }
//
// size_t ProfGraphicsItem::addItem(unsigned short _level, const ProfBlockItem& _item)
// {
// m_levels[_level].emplace_back(_item);
// return m_levels[_level].size() - 1;
// }
//
// size_t ProfGraphicsItem::addItem(unsigned short _level, ProfBlockItem&& _item)
// {
// m_levels[_level].emplace_back(::std::forward<ProfBlockItem&&>(_item));
// return m_levels[_level].size() - 1;
// }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProfGraphicsScene::ProfGraphicsScene(QGraphicsView* _parent, bool _test) : QGraphicsScene(_parent), m_beginTime(-1)
2016-06-26 18:46:51 +03:00
{
if (_test)
{
test(10000, 20000000, 5);
}
2016-06-26 18:46:51 +03:00
}
ProfGraphicsScene::ProfGraphicsScene(const thread_blocks_tree_t& _blocksTree, QGraphicsView* _parent) : QGraphicsScene(_parent), m_beginTime(-1)
2016-06-26 18:46:51 +03:00
{
setTree(_blocksTree);
2016-06-26 18:46:51 +03:00
}
2016-06-26 19:06:53 +03:00
ProfGraphicsScene::~ProfGraphicsScene()
2016-06-26 18:46:51 +03:00
{
}
void fillChildren(ProfGraphicsItem* _item, qreal _x, qreal _y, size_t _childrenNumber, size_t& _total_items)
{
size_t nchildren = _childrenNumber;
_childrenNumber >>= 1;
for (size_t i = 0; i < nchildren; ++i)
{
size_t j = _item->addItem();
if (_childrenNumber > 0)
{
fillChildren(_item, _x, _y + GRAPHICS_ROW_SIZE + 2, _childrenNumber, _total_items);
auto& last = _item->items().back();
auto& b = _item->getItem(j);
b.color = ((30 + rand() % 225) << 16) + ((30 + rand() % 225) << 8) + (30 + rand() % 225);
b.setRect(_x, _y, last.right() - _x, GRAPHICS_ROW_SIZE);
b.totalHeight = last.bottom() - _y;
_x = b.right();
}
else
{
auto& b = _item->getItem(j);
b.color = ((30 + rand() % 225) << 16) + ((30 + rand() % 225) << 8) + (30 + rand() % 225);
b.setRect(_x, _y, 10 + rand() % 40, GRAPHICS_ROW_SIZE);
b.totalHeight = GRAPHICS_ROW_SIZE;
_x = b.right();
}
++_total_items;
}
}
void ProfGraphicsScene::test(size_t _frames_number, size_t _total_items_number_estimate, int _depth)
2016-06-26 18:46:51 +03:00
{
const auto children_per_frame = _total_items_number_estimate / _frames_number;
const size_t first_level_children_count = sqrt(pow(2, _depth - 1)) * pow(children_per_frame, 1.0 / double(_depth)) + 0.5;
size_t total_items = 0;
qreal x = 0, y = 0, h = 0;
for (unsigned int i = 0; i < _frames_number; ++i)
2016-06-26 18:46:51 +03:00
{
auto item = new ProfGraphicsItem(true);
item->setPos(x, 0);
size_t j = item->addItem();
fillChildren(item, 0, y + GRAPHICS_ROW_SIZE + 2, first_level_children_count, total_items);
auto& last = item->items().back();
auto& b = item->getItem(j);
b.color = ((30 + rand() % 225) << 16) + ((30 + rand() % 225) << 8) + (30 + rand() % 225);
b.setRect(0, 0, last.right(), GRAPHICS_ROW_SIZE);
b.totalHeight = last.bottom() - y;
item->setBoundingRect(0, 0, b.width(), b.totalHeight);
x += b.width() + 500;
if (last.bottom() > h)
{
h = last.bottom();
}
item->addItem(b);
++total_items;
2016-06-26 18:46:51 +03:00
addItem(item);
}
setSceneRect(0, 0, x, h);
2016-06-26 18:46:51 +03:00
}
void ProfGraphicsScene::test2(size_t _frames_number, size_t _total_items_number_estimate, int _depth)
{
}
void ProfGraphicsScene::clearSilent()
{
const QSignalBlocker b(this); // block all scene signals (otherwise clear() would be extremely slow!)
clear(); // clear would be VERY SLOW if signals would not be blocked!
m_beginTime = -1;
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsScene::setTree(const thread_blocks_tree_t& _blocksTree)
2016-06-26 18:46:51 +03:00
{
// calculate scene size and fill it with items
::profiler::timestamp_t finish = 0;
qreal y = ROW_SPACING;
2016-06-26 18:46:51 +03:00
for (const auto& threadTree : _blocksTree)
{
const auto timestart = threadTree.second.children.front().node->block()->getBegin();
const auto timefinish = threadTree.second.children.back().node->block()->getEnd();
if (m_beginTime > timestart) m_beginTime = timestart;
2016-06-26 18:46:51 +03:00
if (finish < timefinish) finish = timefinish;
// fill scene with new items
qreal h = 0;
setTree(threadTree.second.children, h, y);
y += h + ROW_SPACING;
2016-06-26 18:46:51 +03:00
}
const qreal endX = time2position(finish + 1000000);
setSceneRect(QRectF(0, 0, endX, y + ROW_SPACING));
}
2016-06-26 18:46:51 +03:00
qreal ProfGraphicsScene::setTree(const BlocksTree::children_t& _children, qreal& _height, qreal _y)
{
//return 0;
qreal total_duration = 0, prev_end = 0, maxh = 0;
2016-06-26 18:46:51 +03:00
for (const auto& child : _children)
{
qreal xbegin = time2position(child.node->block()->getBegin());
2016-06-26 18:46:51 +03:00
qreal duration = time2position(child.node->block()->getEnd()) - xbegin;
const qreal dt = xbegin - prev_end;
if (dt < 0)
{
duration += dt;
xbegin -= dt;
}
2016-06-26 18:46:51 +03:00
if (duration < 0.1)
{
duration = 0.1;
}
auto item = new ProfGraphicsItem();
2016-06-26 18:46:51 +03:00
item->setPos(xbegin, _y);
item->reserve(child.total_children_number + 1);
auto i = item->addItem();
qreal h = 0;
const auto children_duration = setTree(item, child.children, h, _y + GRAPHICS_ROW_SIZE_FULL);
if (duration < children_duration)
{
duration = children_duration;
}
if (h > maxh)
{
maxh = h;
}
2016-06-26 18:46:51 +03:00
const auto color = child.node->block()->getColor();
auto& b = item->getItem(i);
b.block = &child;
b.color = (::profiler::colors::get_red(color) << 16) + (::profiler::colors::get_green(color) << 8) + ::profiler::colors::get_blue(color);
b.setRect(0, 0, duration, GRAPHICS_ROW_SIZE);
b.totalHeight = GRAPHICS_ROW_SIZE + h;
item->setBoundingRect(0, 0, duration, b.totalHeight);
addItem(item);
2016-06-26 18:46:51 +03:00
total_duration += duration;
2016-06-26 22:16:36 +03:00
prev_end = xbegin + duration;
}
_height += GRAPHICS_ROW_SIZE + maxh;
return total_duration;
}
qreal ProfGraphicsScene::setTree(ProfGraphicsItem* _item, const BlocksTree::children_t& _children, qreal& _height, qreal _y)
{
if (_children.empty())
{
return 0;
}
qreal total_duration = 0, prev_end = 0, maxh = 0;
for (const auto& child : _children)
{
qreal xbegin = time2position(child.node->block()->getBegin());
qreal duration = time2position(child.node->block()->getEnd()) - xbegin;
const qreal dt = xbegin - prev_end;
if (dt < 0)
{
duration += dt;
xbegin -= dt;
}
if (duration < 0.1)
{
duration = 0.1;
}
auto i = _item->addItem();
qreal h = 0;
const auto children_duration = setTree(_item, child.children, h, _y + GRAPHICS_ROW_SIZE_FULL);
if (duration < children_duration)
{
duration = children_duration;
}
if (h > maxh)
{
maxh = h;
}
const auto color = child.node->block()->getColor();
auto& b = _item->getItem(i);
b.block = &child;
b.color = (::profiler::colors::get_red(color) << 16) + (::profiler::colors::get_green(color) << 8) + ::profiler::colors::get_blue(color);
b.setRect(xbegin - _item->x(), _y - _item->y(), duration, GRAPHICS_ROW_SIZE);
b.totalHeight = GRAPHICS_ROW_SIZE + h;
total_duration += duration;
prev_end = xbegin + duration;
2016-06-26 18:46:51 +03:00
}
_height += GRAPHICS_ROW_SIZE_FULL + maxh;
return total_duration;
2016-06-26 18:46:51 +03:00
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProfGraphicsView::ProfGraphicsView(bool _test) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton), m_bUpdatingRect(false)
2016-06-26 18:46:51 +03:00
{
initMode();
setScene(new ProfGraphicsScene(this, _test));
2016-06-26 18:46:51 +03:00
centerOn(0, 0);
updateVisibleSceneRect();
2016-06-26 18:46:51 +03:00
}
ProfGraphicsView::ProfGraphicsView(const thread_blocks_tree_t& _blocksTree) : QGraphicsView(), m_scale(1), m_scaleCoeff(1.25), m_mouseButtons(Qt::NoButton), m_bUpdatingRect(false)
2016-06-26 18:46:51 +03:00
{
initMode();
2016-06-26 19:06:53 +03:00
setScene(new ProfGraphicsScene(_blocksTree, this));
2016-06-26 18:46:51 +03:00
centerOn(0, 0);
updateVisibleSceneRect();
2016-06-26 18:46:51 +03:00
}
2016-06-26 19:06:53 +03:00
ProfGraphicsView::~ProfGraphicsView()
2016-06-26 18:46:51 +03:00
{
}
2016-06-26 19:06:53 +03:00
void ProfGraphicsView::wheelEvent(QWheelEvent* _event)
2016-06-26 18:46:51 +03:00
{
qreal scaleCoeff;
2016-06-26 18:46:51 +03:00
if (_event->delta() > 0)
{
scaleCoeff = m_scaleCoeff;
2016-06-26 18:46:51 +03:00
}
else
{
scaleCoeff = 1. / m_scaleCoeff;
2016-06-26 18:46:51 +03:00
}
m_bUpdatingRect = true; // To be sure that updateVisibleSceneRect will not be called by scrollbar change
scale(scaleCoeff, 1); // Scale scene
m_scale *= scaleCoeff;
m_bUpdatingRect = false;
updateVisibleSceneRect(); // Update scene rect
_event->accept();
//QGraphicsView::wheelEvent(_event);
}
void ProfGraphicsView::mousePressEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
if (m_mouseButtons & Qt::LeftButton)
{
m_mousePressPos = _event->globalPos();
}
_event->accept();
//QGraphicsView::mousePressEvent(_event);
}
void ProfGraphicsView::mouseReleaseEvent(QMouseEvent* _event)
{
m_mouseButtons = _event->buttons();
_event->accept();
//QGraphicsView::mouseReleaseEvent(_event);
}
void ProfGraphicsView::mouseMoveEvent(QMouseEvent* _event)
{
if (m_mouseButtons & Qt::LeftButton)
{
const auto pos = _event->globalPos();
const auto delta = pos - m_mousePressPos;
m_mousePressPos = pos;
auto vbar = verticalScrollBar();
auto hbar = horizontalScrollBar();
m_bUpdatingRect = true; // Block scrollbars from updating scene rect to make it possible to do it only once
vbar->setValue(vbar->value() - delta.y()); hbar->setValue(hbar->value() - delta.x());
m_bUpdatingRect = false;
// Seems like an ugly stub, but QSignalBlocker is also a bad decision
// because if scrollbar does not emit valueChanged signal then viewport does not move
updateVisibleSceneRect(); // Update scene visible rect only once
}
//_event->accept();
QGraphicsView::mouseMoveEvent(_event); // This is to be able to use setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
}
void ProfGraphicsView::initMode()
{
// TODO: find mode with least number of bugs :)
// There are always some display bugs...
setCacheMode(QGraphicsView::CacheNone);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
connect(horizontalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange);
connect(verticalScrollBar(), &QScrollBar::valueChanged, this, &This::onScrollbarValueChange);
}
void ProfGraphicsView::updateVisibleSceneRect()
{
m_visibleSceneRect = mapToScene(rect()).boundingRect();
}
void ProfGraphicsView::setTree(const thread_blocks_tree_t& _blocksTree)
{
// clear scene
clearSilent();
// set new blocks tree
static_cast<ProfGraphicsScene*>(scene())->setTree(_blocksTree);
// center view on the beginning of the scene
centerOn(0, 0);
updateVisibleSceneRect();
}
void ProfGraphicsView::clearSilent()
{
// block all signals for faster scene recreation
const QSignalBlocker b(this);
// clear scene
static_cast<ProfGraphicsScene*>(scene())->clearSilent();
// scale back to initial 100% scale
scale(1. / m_scale, 1);
m_scale = 1;
}
void ProfGraphicsView::test(size_t _frames_number, size_t _total_items_number_estimate, int _depth)
{
static_cast<ProfGraphicsScene*>(scene())->test(_frames_number, _total_items_number_estimate, _depth);
updateVisibleSceneRect();
}
void ProfGraphicsView::onScrollbarValueChange(int)
{
if (!m_bUpdatingRect)
updateVisibleSceneRect();
2016-06-26 18:46:51 +03:00
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////