From 04de8977809053a55347984eb05fa0a100b740ef Mon Sep 17 00:00:00 2001 From: Mark Watkins Date: Wed, 31 Aug 2011 21:44:53 +1000 Subject: [PATCH] Initial draw multithreading.. (still some glitches) --- Graphs/gFlagsLine.cpp | 31 ++++++++++---- Graphs/gFlagsLine.h | 4 ++ Graphs/gFooBar.cpp | 43 +++++++------------ Graphs/gFooBar.h | 2 + Graphs/gGraphView.cpp | 97 ++++++++++++++++++++++++++++++++++--------- Graphs/gGraphView.h | 32 ++++++++++++-- 6 files changed, 147 insertions(+), 62 deletions(-) diff --git a/Graphs/gFlagsLine.cpp b/Graphs/gFlagsLine.cpp index dd96934c..87806894 100644 --- a/Graphs/gFlagsLine.cpp +++ b/Graphs/gFlagsLine.cpp @@ -12,6 +12,16 @@ gFlagsGroup::gFlagsGroup() { + static QColor col1=QColor(0xd0,0xff,0xd0,0xff); + static QColor col2=QColor(0xff,0xff,0xff,0xff); + static QColor col=Qt::black; + + addGLBuf(quad1=new GLBuffer(col1,2048,GL_QUADS)); + addGLBuf(quad2=new GLBuffer(col2,2048,GL_QUADS)); + addGLBuf(lines=new GLBuffer(col,2048,GL_LINES)); + quad1->setAntiAlias(true); + quad2->setAntiAlias(true); + lines->setAntiAlias(false); } gFlagsGroup::~gFlagsGroup() { @@ -49,26 +59,30 @@ void gFlagsGroup::paint(gGraph &w, int left, int top, int width, int height) float barh=float(height)/float(vis); float linetop=top; - static QColor col1=QColor(0xd0,0xff,0xd0,0xff); - static QColor col2=QColor(0xff,0xff,0xff,0xff); - for (int i=0;iadd(left,linetop,left,linetop+barh); + quad1->add(left+width-1,linetop+barh,left+width-1,linetop); + } else { + quad2->add(left,linetop,left,linetop+barh); + quad2->add(left+width-1,linetop+barh,left+width-1,linetop); + } + + // barcol=&col1; //int qo=0; //if (evil_intel_graphics_card) qo=1; // Draw the bars with filled quads - glBegin(GL_QUADS); + /*glBegin(GL_QUADS); w.qglColor(*barcol); glVertex2f(left, linetop); glVertex2f(left, linetop+barh); glVertex2f(left+width-1, linetop+barh); glVertex2f(left+width-1, linetop); - glEnd(); + glEnd(); */ // Paint the actual flags lvisible[i]->paint(w,left,linetop,width,barh); @@ -87,7 +101,6 @@ void gFlagsGroup::paint(gGraph &w, int left, int top, int width, int height) } - gFlagsLine::gFlagsLine(ChannelID code,QColor flag_color,QString label,bool always_visible,FlagType flt) :Layer(code),m_label(label),m_always_visible(always_visible),m_flt(flt),m_flag_color(flag_color) { diff --git a/Graphs/gFlagsLine.h b/Graphs/gFlagsLine.h index e5e37c56..21d63f00 100644 --- a/Graphs/gFlagsLine.h +++ b/Graphs/gFlagsLine.h @@ -44,6 +44,10 @@ public: virtual void paint(gGraph & w,int left, int top, int width, int height); virtual qint64 Minx(); virtual qint64 Maxx(); + +protected: + GLBuffer *quad1, *quad2, *lines; + }; #endif // GFLAGSLINE_H diff --git a/Graphs/gFooBar.cpp b/Graphs/gFooBar.cpp index d8d01181..2019503f 100644 --- a/Graphs/gFooBar.cpp +++ b/Graphs/gFooBar.cpp @@ -9,6 +9,12 @@ gShadowArea::gShadowArea(QColor shadow_color) :Layer(EmptyChannel),m_shadow_color(shadow_color) { + QColor col=Qt::blue; + addGLBuf(quads=new GLBuffer(shadow_color,20,GL_QUADS)); + addGLBuf(lines=new GLBuffer(col,20,GL_LINES)); + quads->forceAntiAlias(true); + lines->setAntiAlias(true); + lines->setSize(2); } gShadowArea::~gShadowArea() { @@ -30,36 +36,15 @@ void gShadowArea::paint(gGraph & w,int left, int top, int width, int height) double px=((1/rmx)*(w.min_x-w.rmin_x))*width; double py=((1/rmx)*(w.max_x-w.rmin_x))*width; - glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); + quads->add(start_px,top,start_px,top+height); + quads->add(start_px+px, top+height, start_px+px, top); + quads->add(start_px+py, top, start_px+py, top+height); + quads->add(end_px, top+height, end_px, top); - glEnable(GL_BLEND); - glBegin(GL_QUADS); - w.qglColor(m_shadow_color); - - glVertex2f(start_px, top); - glVertex2f(start_px, top+height); - glVertex2f(start_px+px, top+height); - glVertex2f(start_px+px, top); - - glVertex2f(start_px+py, top); - glVertex2f(start_px+py, top+height); - glVertex2f(end_px, top+height); - glVertex2f(end_px, top); - glEnd(); - glDisable(GL_BLEND); - - glLineWidth(2); - w.qglColor(Qt::blue); - glBegin(GL_LINES); - glVertex2f(start_px+px, top); - glVertex2f(start_px+py, top); - glVertex2f(start_px+px, top+height+1); - glVertex2f(start_px+py, top+height+1); - glEnd(); - - glLineWidth(1); - - //glFlush(); + lines->add(start_px+px, top); + lines->add(start_px+py, top); + lines->add(start_px+px, top+height+1); + lines->add(start_px+py, top+height+1); } gFooBar::gFooBar(int offset,QColor handle_color,QColor line_color) diff --git a/Graphs/gFooBar.h b/Graphs/gFooBar.h index 419b5df2..d4528b4e 100644 --- a/Graphs/gFooBar.h +++ b/Graphs/gFooBar.h @@ -17,6 +17,8 @@ class gShadowArea:public Layer virtual void paint(gGraph & w,int left, int top, int width, int height); protected: QColor m_shadow_color; + GLBuffer *quads; + GLBuffer *lines; }; class gFooBar:public Layer diff --git a/Graphs/gGraphView.cpp b/Graphs/gGraphView.cpp index c5f2d8e3..1c146b46 100644 --- a/Graphs/gGraphView.cpp +++ b/Graphs/gGraphView.cpp @@ -49,6 +49,8 @@ GLBuffer::GLBuffer(QColor color,int max,int type) :m_color(color), m_max(max), m_type(type) { m_scissor=false; + m_antialias=true; + m_forceantialias=false; buffer=new GLshort [max+8]; m_cnt=0; m_size=1; @@ -89,7 +91,7 @@ void GLBuffer::add(GLshort x1, GLshort y1, GLshort x2, GLshort y2) void GLBuffer::draw() { if (m_cnt>0) { - bool antialias=pref["UseAntiAliasing"].toBool() && m_antialias; + bool antialias=m_forceantialias || (pref["UseAntiAliasing"].toBool() && m_antialias); float size=m_size; if (antialias) { glEnable(GL_BLEND); @@ -203,6 +205,7 @@ bool LayerGroup::isEmpty() } void LayerGroup::drawGLBuf() { + Layer::drawGLBuf(); for (int i=0;idrawGLBuf(); } @@ -285,6 +288,23 @@ EventDataType LayerGroup::Maxy() const double zoom_hard_limit=500.0; +gThread::gThread(gGraph *g) +{ + graph=g; +} +void gThread::run() +{ + int originX=m_lastbounds.x(); + int originY=m_lastbounds.y(); + int width=m_lastbounds.width(); + int height=m_lastbounds.height(); + graph->paint(originX,originY,width,height); + graph->threadDone(); +} +void gThread::paint(int originX, int originY, int width, int height) +{ + m_lastbounds=QRect(originX,originY,width,height); +} gGraph::gGraph(gGraphView *graphview,QString title,int height,short group) : m_graphview(graphview), @@ -306,9 +326,14 @@ gGraph::gGraph(gGraphView *graphview,QString title,int height,short group) : m_marginright=10; m_selecting_area=m_blockzoom=false; m_lastx23=0; + m_thread=new gThread(this); + quad=new GLBuffer(QColor(128,128,255,128),8,GL_QUADS); + quad->forceAntiAlias(true); } gGraph::~gGraph() { + delete m_thread; + delete quad; } bool gGraph::isEmpty() { @@ -321,11 +346,18 @@ bool gGraph::isEmpty() } return empty; } +void gGraph::threadDone() +{ + + m_graphview->masterlock->release(1); +} + void gGraph::drawGLBuf() { for (int i=0;idrawGLBuf(); } + quad->draw(); } void gGraph::setDay(Day * day) { @@ -372,20 +404,24 @@ void gGraph::repaint() } } */ + void gGraph::qglColor(QColor col) { m_graphview->qglColor(col); } + void gGraph::renderText(QString text, int x,int y, float angle, QColor color, QFont *font) { m_graphview->AddTextQue(text,x,y,angle,color,font); } - void gGraph::paint(int originX, int originY, int width, int height) { m_lastbounds=QRect(originX,originY,width,height); - +/* int originX=m_lastbounds.x(); + int originY=m_lastbounds.y(); + int width=m_lastbounds.width(); + int height=m_lastbounds.height(); */ /*glEnable(GL_BLEND); glBegin(GL_QUADS); glColor4f(1,1,1,0.4); // Gradient End @@ -451,19 +487,15 @@ void gGraph::paint(int originX, int originY, int width, int height) } if (m_selection.width()>0 && m_selecting_area) { - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glEnable(GL_BLEND); - glBegin(GL_QUADS); - glColor4ub(128,128,128,128); - glVertex2i(originX+m_selection.x(),originY+top); - glVertex2i(originX+m_selection.x()+m_selection.width(),originY+top); - glColor4ub(128,128,255,128); - glVertex2i(originX+m_selection.x()+m_selection.width(),originY+height-top-bottom); - glVertex2i(originX+m_selection.x(),originY+height-top-bottom); - glEnd(); - glDisable(GL_BLEND); + quad->add(originX+m_selection.x(),originY+top, originX+m_selection.x()+m_selection.width(),originY+top); + quad->add(originX+m_selection.x()+m_selection.width(),originY+height-top-bottom, originX+m_selection.x(),originY+height-top-bottom); } + + /*m_graphview->gl_mutex.lock(); + drawGLBuf(); + m_graphview->gl_mutex.unlock();*/ + } void gGraph::AddLayer(Layer * l,LayerPosition position, short width, short height, short order, bool movable, short x, short y) @@ -899,12 +931,17 @@ gGraphView::gGraphView(QWidget *parent, gGraphView * shared) : this->setMouseTracking(true); m_emptytext=QObject::tr("No Data"); InitGraphs(); + m_idealthreads=QThread::idealThreadCount(); + if (m_idealthreads<=0) m_idealthreads=1; + + masterlock=new QSemaphore(m_idealthreads); } gGraphView::~gGraphView() { for (int i=0;idisconnect(SIGNAL(sliderMoved(int)),this); @@ -961,19 +998,18 @@ void gGraphView::DrawTextQue() void gGraphView::AddTextQue(QString & text, short x, short y, float angle, QColor & color, QFont * font) { - text_mutex.lock(); + //text_mutex.lock(); if (m_textque_items>=textque_max) { DrawTextQue(); } - TextQue & q=m_textque[m_textque_items]; + TextQue & q=m_textque[m_textque_items++]; + //text_mutex.unlock(); q.text=text; q.x=x; q.y=y; q.angle=angle; q.color=color; q.font=font; - m_textque_items++; - text_mutex.unlock(); } void gGraphView::DrawStaticText(QStaticText & text, short x, short y) { @@ -1123,6 +1159,10 @@ void gGraphView::resizeGL(int w, int h) glLoadIdentity(); } +void gGraphView::threadDone() +{ + qDebug() << "Thread Done" << m_threadsrunning; +} void gGraphView::paintGL() { @@ -1156,7 +1196,12 @@ void gGraphView::paintGL() float h,w; //ax=px;//-m_offsetX; + bool threaded; + if (m_idealthreads>1) { + threaded=true; + } else threaded=false; + //threaded=true; for (int i=0;iisEmpty() || !m_graphs[i]->visible()) continue; numgraphs++; @@ -1170,8 +1215,15 @@ void gGraphView::paintGL() if ((py + h + graphSpacer) >= 0) { w=width(); - m_graphs[i]->paint(px,py,width()-titleWidth,h); - glColor4f(0,0,0,1); + if (threaded) { + masterlock->acquire(1); + m_graphs[i]->thread()->paint(px,py,width()-titleWidth,h); + m_graphs[i]->thread()->start(QThread::HighestPriority); + m_graphs[i]->thread()->wait(); + } else { + m_graphs[i]->paint(px,py,width()-titleWidth,h); + } + //glColor4f(0,0,0,1); //if (iacquire(m_idealthreads); + masterlock->release(m_idealthreads); + } + for (int i=0;idrawGLBuf(); } diff --git a/Graphs/gGraphView.h b/Graphs/gGraphView.h index db80b7c8..c9a61e7b 100644 --- a/Graphs/gGraphView.h +++ b/Graphs/gGraphView.h @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include @@ -44,6 +46,7 @@ public: bool full() { return m_cnt>=m_max; } void setSize(float f) { m_size=f; } void setAntiAlias(bool b) { m_antialias=b; } + void forceAntiAlias(bool b) { m_forceantialias=b; } protected: QColor m_color; GLshort * buffer; @@ -54,6 +57,7 @@ protected: int s1,s2,s3,s4; bool m_scissor; bool m_antialias; + bool m_forceantialias; }; struct TextQue @@ -152,10 +156,23 @@ protected: QVector layers; }; +class gGraph; +class gThread:public QThread +{ +public: + gThread(gGraph *g); + void run(); + void paint(int originX, int originY, int width, int height); +protected: + gGraph * graph; + QRect m_lastbounds; +}; + class gGraph { - friend class gGraphView; public: + friend class gGraphView; + gGraph(gGraphView * graphview=NULL, QString title="",int height=100,short group=0); virtual ~gGraph(); @@ -210,8 +227,10 @@ public: void DrawTextQue(); void DrawStaticText(QStaticText & text, short x, short y); void setDay(Day * day); -protected: + gThread * thread() { return m_thread; } virtual void paint(int originX, int originY, int width, int height); + void threadDone(); +protected: //void invalidate(); virtual void wheelEvent(QWheelEvent * event); @@ -223,6 +242,7 @@ protected: void ZoomX(double mult,int origin_px); + gThread * m_thread; gGraphView * m_graphview; QString m_title; QVector m_layers; @@ -242,6 +262,7 @@ protected: short m_group; short m_lastx23; Day * m_day; + GLBuffer * quad; }; class gGraphView : public QGLWidget @@ -285,9 +306,12 @@ public: void updateScale(); // update scale & Scrollbar void setEmptyText(QString s) { m_emptytext=s; } QMutex text_mutex; + QMutex gl_mutex; void setDay(Day * day); + void threadDone(); + QSemaphore * masterlock; protected: - + int m_idealthreads; Day * m_day; float totalHeight(); float scaleHeight(); @@ -331,6 +355,7 @@ protected: TextQue m_textque[textque_max]; int m_textque_items; int m_lastxpos,m_lastypos; + volatile int m_threadsrunning; QString m_emptytext; signals: @@ -338,7 +363,6 @@ signals: public slots: void scrollbarValueChanged(int val); - }; #endif // GGRAPHVIEW_H