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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
|
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include <qtest.h>
#include <QtQuick>
#include <private/qopenglcontext_p.h>
#include <QtQml>
class tst_SceneGraph : public QObject
{
Q_OBJECT
private slots:
void manyWindows_data();
void manyWindows();
void render_data();
void render();
void hideWithOtherContext();
};
template <typename T> class ScopedList : public QList<T> {
public:
~ScopedList() { qDeleteAll(*this); }
};
QQuickView *createView(const QString &file, QWindow *parent = 0, int x = -1, int y = -1, int w = -1, int h = -1)
{
QQuickView *view = new QQuickView(parent);
view->setSource(QUrl::fromLocalFile("data/" + file));
if (x >= 0 && y >= 0) view->setPosition(x, y);
if (w >= 0 && h >= 0) view->resize(w, h);
view->show();
return view;
}
QImage showAndGrab(const QString &file, int w, int h)
{
QQuickView view;
view.setSource(QUrl::fromLocalFile(QStringLiteral("data/") + file));
if (w >= 0 && h >= 0)
view.resize(w, h);
view.create();
return view.grabWindow();
}
// Assumes the images are opaque white...
bool containsSomethingOtherThanWhite(const QImage &image)
{
Q_ASSERT(image.format() == QImage::Format_ARGB32_Premultiplied
|| image.format() == QImage::Format_RGB32);
int w = image.width();
int h = image.height();
for (int y=0; y<h; ++y) {
const uint *pixels = (const uint *) image.constScanLine(y);
for (int x=0; x<w; ++x)
if (pixels[x] != 0xffffffff)
return true;
}
return false;
}
// When running on native Nvidia graphics cards on linux, the
// distance field glyph pixels have a measurable, but not visible
// pixel error. Use a custom compare function to avoid
//
// This was GT-216 with the ubuntu "nvidia-319" driver package.
// llvmpipe does not show the same issue.
//
bool compareImages(const QImage &ia, const QImage &ib)
{
if (ia.size() != ib.size())
qDebug() << "images are of different size" << ia.size() << ib.size();
Q_ASSERT(ia.size() == ib.size());
Q_ASSERT(ia.format() == ib.format());
int w = ia.width();
int h = ia.height();
const int tolerance = 5;
for (int y=0; y<h; ++y) {
const uint *as= (const uint *) ia.constScanLine(y);
const uint *bs= (const uint *) ib.constScanLine(y);
for (int x=0; x<w; ++x) {
uint a = as[x];
uint b = bs[x];
// No tolerance for error in the alpha.
if ((a & 0xff000000) != (b & 0xff000000))
return false;
if (qAbs(qRed(a) - qRed(b)) > tolerance)
return false;
if (qAbs(qRed(a) - qRed(b)) > tolerance)
return false;
if (qAbs(qRed(a) - qRed(b)) > tolerance)
return false;
}
}
return true;
}
void tst_SceneGraph::manyWindows_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<bool>("toplevel");
QTest::addColumn<bool>("shared");
QTest::newRow("image,toplevel") << QStringLiteral("manyWindows_image.qml") << true << false;
QTest::newRow("image,subwindow") << QStringLiteral("manyWindows_image.qml") << false << false;
QTest::newRow("dftext,toplevel") << QStringLiteral("manyWindows_dftext.qml") << true << false;
QTest::newRow("dftext,subwindow") << QStringLiteral("manyWindows_dftext.qml") << false << false;
QTest::newRow("ntext,toplevel") << QStringLiteral("manyWindows_ntext.qml") << true << false;
QTest::newRow("ntext,subwindow") << QStringLiteral("manyWindows_ntext.qml") << false << false;
QTest::newRow("rects,toplevel") << QStringLiteral("manyWindows_rects.qml") << true << false;
QTest::newRow("rects,subwindow") << QStringLiteral("manyWindows_rects.qml") << false << false;
QTest::newRow("image,toplevel,sharing") << QStringLiteral("manyWindows_image.qml") << true << true;
QTest::newRow("image,subwindow,sharing") << QStringLiteral("manyWindows_image.qml") << false << true;
QTest::newRow("dftext,toplevel,sharing") << QStringLiteral("manyWindows_dftext.qml") << true << true;
QTest::newRow("dftext,subwindow,sharing") << QStringLiteral("manyWindows_dftext.qml") << false << true;
QTest::newRow("ntext,toplevel,sharing") << QStringLiteral("manyWindows_ntext.qml") << true << true;
QTest::newRow("ntext,subwindow,sharing") << QStringLiteral("manyWindows_ntext.qml") << false << true;
QTest::newRow("rects,toplevel,sharing") << QStringLiteral("manyWindows_rects.qml") << true << true;
QTest::newRow("rects,subwindow,sharing") << QStringLiteral("manyWindows_rects.qml") << false << true;
}
struct ShareContextResetter {
public:
~ShareContextResetter() { QOpenGLContextPrivate::setGlobalShareContext(0); }
};
void tst_SceneGraph::manyWindows()
{
QFETCH(QString, file);
QFETCH(bool, toplevel);
QFETCH(bool, shared);
QOpenGLContext sharedGLContext;
ShareContextResetter cleanup; // To avoid dangling pointer in case of test-failure.
if (shared) {
sharedGLContext.create();
QOpenGLContextPrivate::setGlobalShareContext(&sharedGLContext);
}
QScopedPointer<QWindow> parent;
if (!toplevel) {
parent.reset(new QWindow());
parent->resize(200, 200);
parent->show();
}
ScopedList <QQuickView *> views;
const int COUNT = 4;
QImage baseLine;
for (int i=0; i<COUNT; ++i) {
views << createView(file, parent.data(), (i % 2) * 100, (i / 2) * 100, 100, 100);
}
for (int i=0; i<COUNT; ++i) {
QQuickView *view = views.at(i);
QTest::qWaitForWindowExposed(view);
QImage content = view->grabWindow();
if (i == 0) {
baseLine = content;
QVERIFY(containsSomethingOtherThanWhite(baseLine));
} else {
QVERIFY(compareImages(content, baseLine));
}
}
// Wipe and recreate one (scope pointer delets it...)
delete views.takeLast();
QQuickView *last = createView(file, parent.data(), 100, 100, 100, 100);
QTest::qWaitForWindowExposed(last);
views << last;
QVERIFY(compareImages(baseLine, last->grabWindow()));
// Wipe and recreate all
qDeleteAll(views);
views.clear();
for (int i=0; i<COUNT; ++i) {
views << createView(file, parent.data(), (i % 2) * 100, (i / 2) * 100, 100, 100);
}
for (int i=0; i<COUNT; ++i) {
QQuickView *view = views.at(i);
QTest::qWaitForWindowExposed(view);
QImage content = view->grabWindow();
QVERIFY(compareImages(content, baseLine));
}
}
struct Sample {
Sample(int xx, int yy, qreal rr, qreal gg, qreal bb, qreal errorMargin = 0.05)
: x(xx)
, y(yy)
, r(rr)
, g(gg)
, b(bb)
, tolerance(errorMargin)
{
}
Sample(const Sample &o) : x(o.x), y(o.y), r(o.r), g(o.g), b(o.b), tolerance(o.tolerance) { }
Sample() : x(0), y(0), r(0), g(0), b(0), tolerance(0) { }
QString toString(const QImage &image) const {
QColor color(image.pixel(x,y));
return QString::fromLatin1("pixel(%1,%2), rgb(%3,%4,%5), tolerance=%6 -- image(%7,%8,%9)")
.arg(x).arg(y)
.arg(r).arg(g).arg(b)
.arg(tolerance)
.arg(color.redF()).arg(color.greenF()).arg(color.blueF());
}
bool check(const QImage &image) {
QColor color(image.pixel(x, y));
return qAbs(color.redF() - r) <= tolerance
&& qAbs(color.greenF() - g) <= tolerance
&& qAbs(color.blueF() - b) <= tolerance;
}
int x, y;
qreal r, g, b;
qreal tolerance;
};
static Sample sample_from_regexp(QRegExp *re) {
return Sample(re->cap(1).toInt(),
re->cap(2).toInt(),
re->cap(3).toFloat(),
re->cap(4).toFloat(),
re->cap(5).toFloat(),
re->cap(6).toFloat()
);
}
Q_DECLARE_METATYPE(Sample);
/*
The render() test implements a small test framework for itself with
the purpose of testing odds and ends of the scene graph
rendering. Each .qml file can consist of one or two stages. The
first stage is when the file is first displayed. The content is
grabbed and matched against 'base' samples defined in the .qml file
itself. The samples contain a pixel position, and RGB value and a
margin of error. The samples are defined in the .qml file so it is
easy to make the connection between colors and positions on the screen.
If the base stage samples all succeed, the test emits
'enterFinalStage' on the root item and waits for the .qml file to
update the value of 'finalStageComplete' The test can set this
directly or run an animation and set it later. Once the
'finalStageComplete' variable is true, we grab and match against the
second set of samples 'final'
The samples in the .qml file are defined in comments on the format:
#base: x y r g b error-tolerance
#final: x y r g b error-tolerance
- x and y are integers
- r g b are floats in the range of 0.0-1.0
- error-tolerance is a float in the range of 0.0-1.0
We also include a
#samples: count
to sanity check that all base/final samples were matched correctly
as the matching regexp is a bit crude.
To add new tests, add them to the 'files' list and put #base,
#final, #samples tags into the .qml file
*/
void tst_SceneGraph::render_data()
{
QTest::addColumn<QString>("file");
QTest::addColumn<QList<Sample> >("baseStage");
QTest::addColumn<QList<Sample> >("finalStage");
QList<QString> files;
files << "data/render_DrawSets.qml"
<< "data/render_Overlap.qml"
<< "data/render_MovingOverlap.qml"
<< "data/render_BreakOpacityBatch.qml"
<< "data/render_OutOfFloatRange.qml"
<< "data/render_StackingOrder.qml"
<< "data/render_Mipmap.qml"
<< "data/render_ImageFiltering.qml"
<< "data/render_bug37422.qml"
<< "data/render_OpacityThroughBatchRoot.qml"
;
QRegExp sampleCount("#samples: *(\\d+)");
// X:int Y:int R:float G:float B:float Error:float
QRegExp baseSamples("#base: *(\\d+) *(\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+)");
QRegExp finalSamples("#final: *(\\d+) *(\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+) *(\\d\\.\\d+)");
foreach (QString fileName, files) {
QFile file(fileName);
if (!file.open(QFile::ReadOnly)) {
qFatal("render_data: QFile::open failed! file=%s, error=%s",
qPrintable(fileName), qPrintable(file.errorString()));
}
QStringList contents = QString::fromLatin1(file.readAll()).split(QLatin1Char('\n'));
int samples = -1;
foreach (QString line, contents) {
if (sampleCount.indexIn(line) >= 0) {
samples = sampleCount.cap(1).toInt();
break;
}
}
if (samples == -1)
qFatal("render_data: failed to find string '#samples: [count], file=%s", qPrintable(fileName));
QList<Sample> baseStage, finalStage;
foreach (QString line, contents) {
if (baseSamples.indexIn(line) >= 0)
baseStage << sample_from_regexp(&baseSamples);
else if (finalSamples.indexIn(line) >= 0)
finalStage << sample_from_regexp(&finalSamples);
}
if (baseStage.size() + finalStage.size() != samples)
qFatal("render_data: #samples does not add up to number of counted samples, file=%s", qPrintable(fileName));
QTest::newRow(qPrintable(fileName)) << fileName << baseStage << finalStage;
}
}
void tst_SceneGraph::render()
{
QFETCH(QString, file);
QFETCH(QList<Sample>, baseStage);
QFETCH(QList<Sample>, finalStage);
QObject suite;
suite.setObjectName("The Suite");
QQuickView view;
view.rootContext()->setContextProperty("suite", &suite);
view.setSource(QUrl::fromLocalFile(file));
view.setResizeMode(QQuickView::SizeViewToRootObject);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
// Grab the window and check all our base stage samples
QImage content = view.grabWindow();
for (int i=0; i<baseStage.size(); ++i) {
Sample sample = baseStage.at(i);
QVERIFY2(sample.check(content), qPrintable(sample.toString(content)));
}
// Put the qml file into the final stage and wait for it to
// complete it.
QQuickItem *rootItem = view.rootObject();
QMetaObject::invokeMethod(rootItem, "enterFinalStage");
QTRY_VERIFY(rootItem->property("finalStageComplete").toBool());
// The grab the results and verify the samples in the end state.
content = view.grabWindow();
for (int i=0; i<finalStage.size(); ++i) {
Sample sample = finalStage.at(i);
QVERIFY2(sample.check(content), qPrintable(sample.toString(content)));
}
}
// Testcase for QTBUG-34898. We make another context current on another surface
// in the GUI thread and hide the QQuickWindow while the other context is
// current on the other window.
void tst_SceneGraph::hideWithOtherContext()
{
QWindow window;
window.setSurfaceType(QWindow::OpenGLSurface);
window.resize(100, 100);
window.create();
QOpenGLContext context;
context.create();
bool renderingOnMainThread = false;
{
QQuickView view;
view.setSource(QUrl::fromLocalFile("data/simple.qml"));
view.setResizeMode(QQuickView::SizeViewToRootObject);
view.show();
QVERIFY(QTest::qWaitForWindowExposed(&view));
renderingOnMainThread = view.openglContext()->thread() == QGuiApplication::instance()->thread();
// Make the local context current on the local window...
context.makeCurrent(&window);
}
// The local context should no longer be the current one. It is not
// rebound because all well behaving Qt/OpenGL applications are
// required to makeCurrent their context again before making any
// GL calls to a new frame (see QOpenGLContext docs).
QVERIFY(!renderingOnMainThread || QOpenGLContext::currentContext() != &context);
}
#include "tst_scenegraph.moc"
QTEST_MAIN(tst_SceneGraph)
|