1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
/***************************************************************************
* *
* This file is part of the Fotowall project, *
* http://www.enricoros.com/opensource/fotowall *
* *
* Copyright (C) 2009 by Enrico Ros <enrico.ros@gmail.com> *
* *
* 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) any later version. *
* *
***************************************************************************/
#include "Hardware3DTest.h"
#include "Canvas/Canvas.h"
#include "Canvas/PictureContent.h"
#include "Shared/RenderOpts.h"
#include <QApplication>
#include <QDebug>
#include <QLabel>
#include <QGraphicsView>
#include <QPaintEvent>
#include <QPushButton>
#include <QTimer>
#include <QVBoxLayout>
#define FIXEDSIZE QSize(320, 240)
#define TESTPOWER 5
#define TESTSIZE (1 << TESTPOWER)
Hardware3DTest::Hardware3DTest(QWidget * parent, Qt::WindowFlags flags)
: QDialog(parent, flags)
, m_canvas(new Canvas(physicalDpiX(), physicalDpiY(), this))
, m_view(0)
, m_resultIdx(0)
, m_retCode(Canceled)
, m_state(Off)
, m_statePhase(0)
{
// customize dialog
setWindowTitle(tr("OpenGL Test"));
setWindowModality(Qt::ApplicationModal);
setLayout(new QVBoxLayout);
layout()->setAlignment(Qt::AlignCenter);
layout()->setMargin(0);
// create the timed SceneView
m_view = new TimedSceneView(this);
connect(m_view, SIGNAL(repainted(int)), this, SLOT(slotViewRepainted(int)), Qt::QueuedConnection);
layout()->addWidget(m_view);
// draw a fake contents pixmap
QPixmap picturePix = QPixmap(500, 500);
picturePix.fill(Qt::transparent);
QPainter pixPainter(&picturePix);
QRadialGradient gradient(200, 200, 300, 0, 0);
gradient.setColorAt(0.0, QColor(255, 255, 255, 200));
gradient.setColorAt(1.0, QColor(0, 0, 0, 100));
pixPainter.fillRect(picturePix.rect(), gradient);
pixPainter.end();
// populate scene with PictureContents
m_canvas->resize(FIXEDSIZE);
m_pictures.resize(TESTSIZE);
m_results.resize((TESTPOWER + 1) * 2);
for (int i = 0; i < TESTSIZE; i++) {
PictureContent * p = new PictureContent(m_canvas);
p->loadPixmap(picturePix);
p->setRotation(-60 + (qrand() % 120));
#if QT_VERSION >= 0x040500
p->setOpacity((qreal)(qrand() % 100) / 99.0);
#endif
m_canvas->addManualContent(p, QPoint(qrand() % FIXEDSIZE.width(), qrand() % FIXEDSIZE.height()));
p->hide();
m_pictures[i] = p;
}
m_view->flushPaints();
}
Hardware3DTest::ExitState Hardware3DTest::run()
{
// ensure we're layouted before applying the canvas
show();
m_view->setScene(m_canvas);
// start testing
nextStep();
// run with the events
if (exec() == QDialog::Rejected)
return Canceled;
return m_retCode;
}
void Hardware3DTest::showResults()
{
// delete previous contents
delete m_canvas;
m_canvas = 0;
delete m_view;
m_view = 0;
// drawing consts
static const int margin = 25;
static const int radius = 4;
static const int colWidth = 50;
static const int cols = TESTPOWER + 1;
static const int colsWidth = colWidth * (cols - 1);
static const int rowsHeight = 150;
static const int graphWidth = colsWidth + 2 * (radius + margin);
static const int graphHeight = rowsHeight + 2 * (radius + margin);
// find out min/max of the series
int max = 0;
int min = 0;
for (int i = 0; i < m_results.size(); ++i) {
if (m_results[i] > max || !max)
max = m_results[i];
// commented to let the minimum be zero
//if (m_results[i] < min || !min)
// min = m_results[i];
}
if (max <= min)
max = min + 1;
// draw the graph
#define DATAPOINT(x, y) \
QPointF(x * colWidth + margin + radius, graphHeight - radius - margin - (y - min) * rowsHeight / (max - min))
QPixmap pix(graphWidth, graphHeight);
pix.fill(Qt::transparent);
QPainter p(&pix);
p.setRenderHint(QPainter::Antialiasing, true);
QLinearGradient lg(0, 0, 0, graphHeight);
lg.setColorAt(0.0, QColor(250, 250, 250));
lg.setColorAt(1.0, Qt::transparent);
p.fillRect(pix.rect(), lg);
int seriesSum[2];
for (int s = 0; s < 2; s++) {
seriesSum[s] = 0;
int dIdx = s * cols;
int val = m_results[dIdx + 0];
QColor col = s ? Qt::darkGreen : Qt::blue;
// draw lines
QPointF from = DATAPOINT(0, val);
p.setPen(col);
p.setBrush(Qt::NoBrush);
for (int c = 1; c < cols; c++) {
val = m_results[dIdx + c];
QPointF to = DATAPOINT(c, val);
p.drawLine(from, to);
from = to;
}
// draw circles
p.setPen(Qt::black);
p.setBrush(col);
for (int c = 0; c < cols; c++) {
val = m_results[dIdx + c];
QPointF to = DATAPOINT(c, val);
p.drawEllipse(to, radius, radius);
seriesSum[s] += val;
}
}
p.setBrush(Qt::NoBrush);
p.setPen(Qt::blue);
p.drawText(margin + radius, margin - 15, colsWidth, 20, 0, tr("Non-OpenGL"));
p.setPen(Qt::darkGreen);
p.drawText(margin + radius, margin - 15 + 20, colsWidth, 20, 0, tr("OpenGL"));
p.setPen(Qt::darkGray);
p.drawText(margin + radius, margin + radius + rowsHeight + radius, colsWidth, 20, Qt::AlignCenter, tr("samples (%1...%2)").arg(1).arg(TESTSIZE));
p.translate(margin, margin + radius + rowsHeight);
p.rotate(-90);
p.drawText(0, -20, rowsHeight, 20, Qt::AlignCenter, tr("duration (%1...%2ms)").arg(min).arg(max));
p.end();
// display the new widgets
QLabel * results = new QLabel(this);
layout()->addWidget(results);
results->setFixedSize(pix.size());
results->setPixmap(pix);
results->show();
QPushButton * b1 = new QPushButton(tr("Use OpenGL"), this);
connect(b1, SIGNAL(clicked()), this, SLOT(slotUseOpenGL()));
layout()->addWidget(b1);
QPushButton * b2 = new QPushButton(tr("Don't use OpenGL"), this);
connect(b2, SIGNAL(clicked()), this, SLOT(slotUseSoftware()));
layout()->addWidget(b2);
QFont boldFont;
boldFont.setBold(true);
boldFont.setPointSize(boldFont.pointSize() + 2);
if (seriesSum[0] > seriesSum[1]) {
setWindowTitle(tr("OpenGL Won"));
b1->setFont(boldFont);
b1->setFocus();
} else {
setWindowTitle(tr("OpenGL Lost"));
b2->setFont(boldFont);
b2->setFocus();
}
adjustSize();
}
void Hardware3DTest::nextStep()
{
// advance state
switch (m_state) {
case Off:
// go to Software test
if (true) {
m_state = TestingSoftware;
m_statePhase = 0;
m_view->setOpenGL(false);
}
break;
case TestingSoftware:
// eventually go to Hardware test
if (++m_statePhase > TESTPOWER) {
m_state = TestingOpenGL;
m_statePhase = 0;
m_view->setOpenGL(true);
}
break;
case TestingOpenGL:
// eventually go to Final state
if (++m_statePhase > TESTPOWER) {
showResults();
m_state = Finished;
m_statePhase = 0;
return;
}
break;
case Finished:
return;
}
// apply state configuration
int visibleCount = 1 << m_statePhase;
for (int i = 0; i < TESTSIZE; i++)
m_pictures[i]->setVisible(i <= visibleCount);
// go measure
m_view->measureNextRepaint();
}
void Hardware3DTest::slotViewRepainted(int durationMs)
{
// got the measure
m_results[m_resultIdx++] = durationMs;
nextStep();
}
void Hardware3DTest::slotUseSoftware()
{
m_retCode = UseSoftware;
accept();
}
void Hardware3DTest::slotUseOpenGL()
{
m_retCode = UseOpenGL;
accept();
}
TimedSceneView::TimedSceneView(QWidget * parent)
: SceneView(parent)
, m_measureRepaint(false)
{
// non interactive, fixed sizes
setInteractive(false);
setRenderHint(QPainter::Antialiasing, true);
setRenderHint(QPainter::SmoothPixmapTransform, false);
setFixedSize(FIXEDSIZE);
}
void TimedSceneView::measureNextRepaint()
{
flushPaints();
m_measureRepaint = true;
viewport()->update();
}
void TimedSceneView::flushPaints()
{
// flush out pending painting events
viewport()->update();
QApplication::processEvents();
QApplication::processEvents();
}
void TimedSceneView::paintEvent(QPaintEvent * event)
{
// start time measurement if needed
QTime * time = 0;
if (m_measureRepaint && event->rect().size() == FIXEDSIZE) {
time = new QTime;
time->start();
}
// do painting
QGraphicsView::paintEvent(event);
// get measurement results
if (time) {
int elapsed = time->elapsed();
delete time;
m_measureRepaint = false;
emit repainted(elapsed);
}
}
|