/* * Copyright (c) 2003-2005 Max Howell * Copyright (c) 2005-2013 Mark Kretschmann * Copyright 2017 Malte Veerman * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License or (at your option) version 3 or any later version * accepted by the membership of KDE e.V. (or its successor approved * by the membership of KDE e.V.), which shall act as a proxy * defined in Section 14 of version 3 of the license. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ #ifndef BLOCKRENDERER_H #define BLOCKRENDERER_H #include "BlockAnalyzer.h" #include "BlockWorker.h" #include #include #include #include #include #include #include class BlockRenderer : public QQuickFramebufferObject::Renderer { public: static const int BLOCK_HEIGHT = BlockAnalyzer::BLOCK_HEIGHT; BlockRenderer() : m_worker( nullptr ) {} protected: QOpenGLFramebufferObject* createFramebufferObject(const QSize &size) override { QOpenGLFramebufferObject* fo = new QOpenGLFramebufferObject(size); fo->setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); return fo; } void render() override { // Synchronize worker data if (!m_worker) return; m_worker->m_mutex.lock(); const QVector store = m_worker->m_store; const QVector > fadebars = m_worker->m_fadebars; m_worker->m_mutex.unlock(); QOpenGLPaintDevice d; d.setSize(framebufferObject()->size()); QPainter p(&d); p.scale( QGuiApplication::primaryScreen()->devicePixelRatio(), QGuiApplication::primaryScreen()->devicePixelRatio() ); // Draw the background p.drawPixmap(QRect(QPoint(0, 0), framebufferObject()->size()), m_backgroundPixmap); for(uint x = 0; x < (uint)store.size(); ++x) { // Draw fade bars for(const auto &fadebar : std::as_const(fadebars.at(x))) { if(fadebar.intensity > 0) { const uint offset = fadebar.intensity; const int fadeHeight = fadebar.y * (BLOCK_HEIGHT + 1); if(fadeHeight > 0) p.drawPixmap(x * (m_columnWidth + 1), 0, m_fadeBarsPixmaps.value(offset), 0, 0, m_columnWidth, fadeHeight); } } // Draw bars const int height = store.at(x) * (BLOCK_HEIGHT + 1); if (height > 0) p.drawPixmap(x * (m_columnWidth + 1), 0, m_barPixmap, 0, 0, m_columnWidth, height); // Draw top bar p.drawPixmap(x * (m_columnWidth + 1), height + BLOCK_HEIGHT - 1, m_topBarPixmap); } } void synchronize(QQuickFramebufferObject *item) override { auto analyzer = qobject_cast(item); if (!analyzer) return; m_rows = analyzer->m_rows; m_columnWidth = analyzer->m_columnWidth; if (!m_worker) m_worker = qobject_cast(analyzer->worker()); if (analyzer->m_pixmapsChanged) { m_barPixmap = analyzer->m_barPixmap; m_topBarPixmap = analyzer->m_topBarPixmap; m_backgroundPixmap = analyzer->m_backgroundPixmap; m_fadeBarsPixmaps = analyzer->m_fadeBarsPixmaps; analyzer->m_pixmapsChanged = false; } } private: QPointer m_worker; int m_rows; int m_columnWidth; QPixmap m_barPixmap; QPixmap m_topBarPixmap; QPixmap m_backgroundPixmap; QVector m_fadeBarsPixmaps; }; #endif //BLOCKRENDERER_H