nextpnr/gui/fpgaviewwidget.h

197 lines
5.2 KiB
C
Raw Normal View History

/*
* nextpnr -- Next Generation Place and Route
*
* Copyright (C) 2018 Serge Bazanski <q3k@symbioticeda.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
2018-06-05 21:03:06 +02:00
#ifndef MAPGLWIDGET_H
#define MAPGLWIDGET_H
2018-06-10 20:48:52 +02:00
#include <QMainWindow>
2018-07-17 20:16:26 +02:00
#include <QMutex>
#include <QOpenGLBuffer>
2018-06-05 21:03:06 +02:00
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
2018-06-22 21:16:49 +02:00
#include <QOpenGLVertexArrayObject>
2018-06-06 22:53:52 +02:00
#include <QOpenGLWidget>
2018-06-05 21:03:06 +02:00
#include <QPainter>
2018-07-17 20:16:26 +02:00
#include <QThread>
2018-07-20 14:19:45 +02:00
#include <QTimer>
2018-07-17 20:16:26 +02:00
#include <QWaitCondition>
#include "nextpnr.h"
2018-07-26 17:20:58 +02:00
#include "quadtree.h"
#include "lineshader.h"
2018-06-05 21:03:06 +02:00
NEXTPNR_NAMESPACE_BEGIN
2018-07-20 11:58:30 +02:00
class PeriodicRunner : public QThread
{
Q_OBJECT
2018-07-20 14:19:45 +02:00
private:
2018-07-20 11:58:30 +02:00
QMutex mutex_;
QWaitCondition condition_;
bool abort_;
std::function<void()> target_;
QTimer timer_;
2018-07-20 14:19:45 +02:00
public:
explicit PeriodicRunner(QObject *parent, std::function<void()> target)
: QThread(parent), abort_(false), target_(target), timer_(this)
2018-07-20 11:58:30 +02:00
{
connect(&timer_, &QTimer::timeout, this, &PeriodicRunner::poke);
}
void run(void) override
{
for (;;) {
mutex_.lock();
condition_.wait(&mutex_);
if (abort_) {
mutex_.unlock();
return;
}
target_();
mutex_.unlock();
}
}
void startTimer(int msecs) { timer_.start(msecs); }
2018-07-20 11:58:30 +02:00
~PeriodicRunner()
{
mutex_.lock();
abort_ = true;
condition_.wakeOne();
mutex_.unlock();
wait();
}
2018-07-20 14:19:45 +02:00
void poke(void) { condition_.wakeOne(); }
2018-07-20 11:58:30 +02:00
};
2018-06-05 21:03:06 +02:00
class FPGAViewWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
Q_OBJECT
2018-06-06 22:53:52 +02:00
public:
2018-06-05 21:03:06 +02:00
FPGAViewWidget(QWidget *parent = 0);
~FPGAViewWidget();
2018-06-06 22:53:52 +02:00
protected:
// Qt callbacks.
2018-06-05 21:03:06 +02:00
void initializeGL() Q_DECL_OVERRIDE;
void paintGL() Q_DECL_OVERRIDE;
void resizeGL(int width, int height) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void wheelEvent(QWheelEvent *event) Q_DECL_OVERRIDE;
QSize minimumSizeHint() const override;
QSize sizeHint() const override;
2018-07-26 17:26:05 +02:00
2018-06-26 15:47:22 +02:00
public Q_SLOTS:
void newContext(Context *ctx);
2018-07-14 19:44:37 +02:00
void onSelectedArchItem(std::vector<DecalXY> decals);
2018-07-15 17:50:58 +02:00
void onHighlightGroupChanged(std::vector<DecalXY> decals, int group);
2018-07-17 20:16:26 +02:00
void pokeRenderer(void);
2018-07-26 17:26:05 +02:00
void zoomIn();
void zoomOut();
void zoomSelected();
void zoomOutbound();
2018-07-26 17:20:58 +02:00
Q_SIGNALS:
void clickedBel(BelId bel);
2018-06-06 22:53:52 +02:00
private:
const float zoomNear_ = 1.0f; // do not zoom closer than this
2018-07-12 21:22:53 +02:00
const float zoomFar_ = 10000.0f; // do not zoom further than this
const float zoomLvl1_ = 100.0f;
const float zoomLvl2_ = 50.0f;
2018-06-22 21:16:49 +02:00
Context *ctx_;
2018-07-20 11:58:30 +02:00
QTimer paintTimer_;
std::unique_ptr<PeriodicRunner> renderRunner_;
2018-07-17 20:16:26 +02:00
2018-07-26 17:20:58 +02:00
using QuadTreeBels = QuadTree<float, BelId>;
template <typename T>
void commitToQuadtree(T *tree, const DecalXY &decal, BelId bel)
{
float offsetX = decal.x;
float offsetY = decal.y;
for (auto &el : ctx_->getDecalGraphics(decal.decal)) {
if (el.type == GraphicElement::TYPE_BOX) {
2018-07-26 17:20:58 +02:00
tree->insert(typename T::BoundingBox(offsetX + el.x1, offsetY + el.y1, offsetX + el.x2, offsetY + el.y2), bel);
}
}
}
QPoint lastDragPos_;
LineShader lineShader_;
QMatrix4x4 viewMove_;
float zoom_;
2018-07-20 14:19:45 +02:00
struct
{
2018-07-17 20:16:26 +02:00
QColor background;
QColor grid;
QColor frame;
QColor hidden;
QColor inactive;
QColor active;
QColor selected;
QColor highlight[8];
} colors_;
2018-07-20 14:19:45 +02:00
struct RendererData
{
2018-07-26 18:26:26 +02:00
LineShaderData gfxByStyle[GraphicElement::STYLE_MAX];
LineShaderData gfxSelected;
LineShaderData gfxHighlighted[8];
2018-07-26 17:20:58 +02:00
std::unique_ptr<QuadTreeBels> qtBels;
2018-07-17 20:16:26 +02:00
};
std::unique_ptr<RendererData> rendererData_;
QMutex rendererDataLock_;
2018-07-20 14:19:45 +02:00
struct RendererArgs
{
2018-07-26 18:26:26 +02:00
std::vector<DecalXY> selectedDecals;
std::vector<DecalXY> highlightedDecals[8];
2018-07-17 20:16:26 +02:00
bool highlightedOrSelectedChanged;
};
std::unique_ptr<RendererArgs> rendererArgs_;
QMutex rendererArgsLock_;
void zoom(int level);
void renderLines(void);
void drawGraphicElement(LineShaderData &out, const GraphicElement &el, float x, float y);
void drawDecal(LineShaderData &out, const DecalXY &decal);
void drawArchDecal(LineShaderData out[GraphicElement::STYLE_MAX], const DecalXY &decal);
QVector4D mouseToWorldCoordinates(int x, int y);
QVector4D mouseToWorldDimensions(int x, int y);
QMatrix4x4 getProjection(void);
2018-06-05 21:03:06 +02:00
};
NEXTPNR_NAMESPACE_END
2018-06-05 21:03:06 +02:00
#endif