tests/auto/qpaintervideosurface/tst_qpaintervideosurface.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <qpaintervideosurface_p.h>
       
    44 #include <QtTest/QtTest>
       
    45 
       
    46 #include <QtGui/qapplication.h>
       
    47 #include <qvideosurfaceformat.h>
       
    48 
       
    49 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
       
    50 #include <QtOpenGL/qgl.h>
       
    51 #include <QtOpenGL/qglframebufferobject.h>
       
    52 #endif
       
    53 
       
    54 QT_USE_NAMESPACE
       
    55 class tst_QPainterVideoSurface : public QObject
       
    56 {
       
    57     Q_OBJECT
       
    58 private slots:
       
    59     void cleanup() {}
       
    60     void cleanupTestCase() {}
       
    61 
       
    62     void colors();
       
    63 
       
    64     void supportedFormat_data();
       
    65     void supportedFormat();
       
    66 
       
    67     void present_data();
       
    68     void present();
       
    69     void presentOpaqueFrame();
       
    70 
       
    71 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
       
    72 
       
    73     void shaderType();
       
    74 
       
    75     void shaderTypeStarted_data();
       
    76     void shaderTypeStarted();
       
    77 
       
    78     void shaderSupportedFormat_data();
       
    79     void shaderSupportedFormat();
       
    80 
       
    81     void shaderPresent_data();
       
    82     void shaderPresent();
       
    83     void shaderPresentOpaqueFrame_data();
       
    84     void shaderPresentOpaqueFrame();
       
    85     void shaderPresentGLFrame_data();
       
    86     void shaderPresentGLFrame();
       
    87 #endif
       
    88 };
       
    89 
       
    90 Q_DECLARE_METATYPE(const uchar *)
       
    91 
       
    92 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
       
    93 Q_DECLARE_METATYPE(QPainterVideoSurface::ShaderType);
       
    94 
       
    95 class QtTestGLVideoBuffer : public QAbstractVideoBuffer
       
    96 {
       
    97 public:
       
    98     QtTestGLVideoBuffer()
       
    99         : QAbstractVideoBuffer(GLTextureHandle)
       
   100         , m_textureId(0)
       
   101     {
       
   102         glGenTextures(1, &m_textureId);
       
   103     }
       
   104     
       
   105     ~QtTestGLVideoBuffer()
       
   106     {
       
   107         glDeleteTextures(1, &m_textureId);
       
   108     }
       
   109 
       
   110     GLuint textureId() const { return m_textureId; }
       
   111 
       
   112     QVariant handle() const { return m_textureId; }
       
   113 
       
   114     uchar *map(MapMode, int *, int *) { return 0; }
       
   115     void unmap() {}
       
   116     MapMode	mapMode() const { return NotMapped; }
       
   117 
       
   118 private:
       
   119     GLuint m_textureId;
       
   120 };
       
   121 
       
   122 #endif
       
   123 
       
   124 class QtTestOpaqueVideoBuffer : public QAbstractVideoBuffer
       
   125 {
       
   126 public:
       
   127     QtTestOpaqueVideoBuffer()
       
   128         : QAbstractVideoBuffer(UserHandle)
       
   129     {}
       
   130 
       
   131     uchar *map(MapMode, int *, int *) { return 0; }
       
   132     void unmap() {}
       
   133     MapMode	mapMode() const { return NotMapped; }
       
   134 };
       
   135 
       
   136 void tst_QPainterVideoSurface::colors()
       
   137 {
       
   138     QPainterVideoSurface surface;
       
   139 
       
   140     QCOMPARE(surface.brightness(), 0);
       
   141     QCOMPARE(surface.contrast(), 0);
       
   142     QCOMPARE(surface.hue(), 0);
       
   143     QCOMPARE(surface.saturation(), 0);
       
   144 
       
   145     surface.setBrightness(56);
       
   146     QCOMPARE(surface.brightness(), 56);
       
   147 
       
   148     surface.setContrast(43);
       
   149     QCOMPARE(surface.contrast(), 43);
       
   150 
       
   151     surface.setHue(-84);
       
   152     QCOMPARE(surface.hue(), -84);
       
   153 
       
   154     surface.setSaturation(100);
       
   155     QCOMPARE(surface.saturation(), 100);
       
   156 }
       
   157 
       
   158 static const uchar rgb32ImageData[] =
       
   159 {
       
   160     0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0x00,
       
   161     0x00, 0xff, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00
       
   162 };
       
   163 
       
   164 static const uchar argb32ImageData[] =
       
   165 {
       
   166     0x00, 0xff, 0x00, 0x00, 0xcc, 0x00, 0xff, 0xcc,
       
   167     0x77, 0x00, 0x00, 0x77, 0x00, 0xff, 0xff, 0x00
       
   168 };
       
   169 
       
   170 static const uchar rgb24ImageData[] =
       
   171 {
       
   172     0x00, 0xff, 0x00, 0x00, 0xcc, 0x00, 0x00, 0x00,
       
   173     0xcc, 0x00, 0xcc, 0x77, 0xff, 0x77, 0x00, 0x00
       
   174 };
       
   175 
       
   176 static const uchar rgb565ImageData[] =
       
   177 {
       
   178     0xff, 0xff, 0xff, 0xff,
       
   179     0x00, 0x00, 0x00, 0x00
       
   180 };
       
   181 
       
   182 static const uchar yuvPlanarImageData[] =
       
   183 {
       
   184     0x00, 0x00, 0x0f, 0xff, 0xff, 0x0f, 0x00, 0x00,
       
   185     0x00, 0x0f, 0xff, 0x0f, 0x0f, 0xff, 0x0f, 0x00,
       
   186     0x00, 0xff, 0x0f, 0x00, 0x00, 0x0f, 0xff, 0x00,
       
   187     0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff,
       
   188     0xff, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff,
       
   189     0x0f, 0xff, 0x0f, 0x00, 0x00, 0x0f, 0xff, 0x0f,
       
   190     0x00, 0x0f, 0xff, 0x0f, 0x0f, 0xff, 0x0f, 0x00,
       
   191     0x00, 0x00, 0x0f, 0xff, 0xff, 0x0f, 0x00, 0x00,
       
   192     0x00, 0x0f, 0x0f, 0x00,
       
   193     0x0f, 0x00, 0x00, 0x0f,
       
   194     0x0f, 0x00, 0x00, 0x0f,
       
   195     0x00, 0x0f, 0x0f, 0x00,
       
   196     0x00, 0x0f, 0x0f, 0x00,
       
   197     0x0f, 0x00, 0x00, 0x0f,
       
   198     0x0f, 0x00, 0x00, 0x0f,
       
   199     0x00, 0x0f, 0x0f, 0x00,
       
   200 };
       
   201 
       
   202 void tst_QPainterVideoSurface::supportedFormat_data()
       
   203 {
       
   204     QTest::addColumn<QAbstractVideoBuffer::HandleType>("handleType");
       
   205     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormat");
       
   206     QTest::addColumn<QSize>("frameSize");
       
   207     QTest::addColumn<bool>("supportedPixelFormat");
       
   208     QTest::addColumn<bool>("supportedFormat");
       
   209 
       
   210     QTest::newRow("rgb32 640x480")
       
   211             << QAbstractVideoBuffer::NoHandle
       
   212             << QVideoFrame::Format_RGB32
       
   213             << QSize(640, 480)
       
   214             << true
       
   215             << true;
       
   216     QTest::newRow("rgb32 -640x480")
       
   217             << QAbstractVideoBuffer::NoHandle
       
   218             << QVideoFrame::Format_RGB32
       
   219             << QSize(-640, 480)
       
   220             << true
       
   221             << false;
       
   222     QTest::newRow("rgb24 1024x768")
       
   223             << QAbstractVideoBuffer::NoHandle
       
   224             << QVideoFrame::Format_RGB24
       
   225             << QSize(1024, 768)
       
   226             << true
       
   227             << true;
       
   228     QTest::newRow("rgb24 -1024x-768")
       
   229             << QAbstractVideoBuffer::NoHandle
       
   230             << QVideoFrame::Format_RGB24
       
   231             << QSize(-1024, -768)
       
   232             << true
       
   233             << false;
       
   234     QTest::newRow("rgb565 0x0")
       
   235             << QAbstractVideoBuffer::NoHandle
       
   236             << QVideoFrame::Format_RGB565
       
   237             << QSize(0, 0)
       
   238             << true
       
   239             << false;
       
   240     QTest::newRow("YUV420P 640x480")
       
   241             << QAbstractVideoBuffer::NoHandle
       
   242             << QVideoFrame::Format_YUV420P
       
   243             << QSize(640, 480)
       
   244             << false
       
   245             << false;
       
   246     QTest::newRow("YUV420P 640x-480")
       
   247             << QAbstractVideoBuffer::NoHandle
       
   248             << QVideoFrame::Format_YUV420P
       
   249             << QSize(640, -480)
       
   250             << false
       
   251             << false;
       
   252     QTest::newRow("Y8 640x480")
       
   253             << QAbstractVideoBuffer::NoHandle
       
   254             << QVideoFrame::Format_Y8
       
   255             << QSize(640, 480)
       
   256             << false
       
   257             << false;
       
   258     QTest::newRow("Texture: rgb32 640x480")
       
   259             << QAbstractVideoBuffer::GLTextureHandle
       
   260             << QVideoFrame::Format_RGB32
       
   261             << QSize(640, 480)
       
   262             << false
       
   263             << false;
       
   264     QTest::newRow("Texture: rgb32 -640x480")
       
   265             << QAbstractVideoBuffer::GLTextureHandle
       
   266             << QVideoFrame::Format_RGB32
       
   267             << QSize(-640, 480)
       
   268             << false
       
   269             << false;
       
   270     QTest::newRow("rgb565 32x32")
       
   271             << QAbstractVideoBuffer::NoHandle
       
   272             << QVideoFrame::Format_RGB565
       
   273             << QSize(32, 32)
       
   274             << true
       
   275             << true;
       
   276     QTest::newRow("rgb565 0x0")
       
   277             << QAbstractVideoBuffer::NoHandle
       
   278             << QVideoFrame::Format_RGB565
       
   279             << QSize(0, 0)
       
   280             << true
       
   281             << false;
       
   282     QTest::newRow("argb32 256x256")
       
   283             << QAbstractVideoBuffer::NoHandle
       
   284             << QVideoFrame::Format_ARGB32
       
   285             << QSize(256, 256)
       
   286             << true
       
   287             << true;
       
   288     QTest::newRow("Texture: rgb24 1024x768")
       
   289             << QAbstractVideoBuffer::GLTextureHandle
       
   290             << QVideoFrame::Format_RGB24
       
   291             << QSize(1024, 768)
       
   292             << false
       
   293             << false;
       
   294     QTest::newRow("Texture: rgb24 -1024x-768")
       
   295             << QAbstractVideoBuffer::GLTextureHandle
       
   296             << QVideoFrame::Format_RGB24
       
   297             << QSize(-1024, -768)
       
   298             << false
       
   299             << false;
       
   300     QTest::newRow("Texture: YUV420P 640x480")
       
   301             << QAbstractVideoBuffer::GLTextureHandle
       
   302             << QVideoFrame::Format_YUV420P
       
   303             << QSize(640, 480)
       
   304             << false
       
   305             << false;
       
   306     QTest::newRow("Texture: YUV420P 640x-480")
       
   307             << QAbstractVideoBuffer::GLTextureHandle
       
   308             << QVideoFrame::Format_YUV420P
       
   309             << QSize(640, -480)
       
   310             << false
       
   311             << false;
       
   312     QTest::newRow("User Buffer: rgb32 256x256")
       
   313             << QAbstractVideoBuffer::UserHandle
       
   314             << QVideoFrame::Format_RGB32
       
   315             << QSize(256, 256)
       
   316             << false
       
   317             << false;
       
   318 }
       
   319 
       
   320 void tst_QPainterVideoSurface::supportedFormat()
       
   321 {
       
   322     QFETCH(QAbstractVideoBuffer::HandleType, handleType);
       
   323     QFETCH(QVideoFrame::PixelFormat, pixelFormat);
       
   324     QFETCH(QSize, frameSize);
       
   325     QFETCH(bool, supportedPixelFormat);
       
   326     QFETCH(bool, supportedFormat);
       
   327 
       
   328     QPainterVideoSurface surface;
       
   329 
       
   330     const QList<QVideoFrame::PixelFormat> pixelFormats = surface.supportedPixelFormats(handleType);
       
   331 
       
   332     QCOMPARE(pixelFormats.contains(pixelFormat), QBool(supportedPixelFormat));
       
   333 
       
   334     QVideoSurfaceFormat format(frameSize, pixelFormat, handleType);
       
   335 
       
   336     QCOMPARE(surface.isFormatSupported(format), supportedFormat);
       
   337     QCOMPARE(surface.start(format), supportedFormat);
       
   338 }
       
   339 
       
   340 void tst_QPainterVideoSurface::present_data()
       
   341 {
       
   342     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormatA");
       
   343     QTest::addColumn<QSize>("frameSizeA");
       
   344     QTest::addColumn<const uchar *>("frameDataA");
       
   345     QTest::addColumn<int>("bytesA");
       
   346     QTest::addColumn<int>("bytesPerLineA");
       
   347     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormatB");
       
   348     QTest::addColumn<QSize>("frameSizeB");
       
   349     QTest::addColumn<const uchar *>("frameDataB");
       
   350     QTest::addColumn<int>("bytesB");
       
   351     QTest::addColumn<int>("bytesPerLineB");
       
   352 
       
   353     QTest::newRow("rgb32 -> argb32")
       
   354             << QVideoFrame::Format_RGB32
       
   355             << QSize(2, 2)
       
   356             << static_cast<const uchar *>(rgb32ImageData)
       
   357             << int(sizeof(rgb32ImageData))
       
   358             << 8
       
   359             << QVideoFrame::Format_ARGB32
       
   360             << QSize(2, 2)
       
   361             << static_cast<const uchar *>(argb32ImageData)
       
   362             << int(sizeof(argb32ImageData))
       
   363             << 8;
       
   364 
       
   365     QTest::newRow("rgb32 -> rgb24")
       
   366             << QVideoFrame::Format_RGB32
       
   367             << QSize(2, 2)
       
   368             << static_cast<const uchar *>(rgb32ImageData)
       
   369             << int(sizeof(rgb32ImageData))
       
   370             << 8
       
   371             << QVideoFrame::Format_RGB24
       
   372             << QSize(2, 2)
       
   373             << static_cast<const uchar *>(rgb24ImageData)
       
   374             << int(sizeof(rgb24ImageData))
       
   375             << 8;
       
   376 
       
   377     QTest::newRow("rgb32 -> rgb565")
       
   378             << QVideoFrame::Format_RGB32
       
   379             << QSize(2, 2)
       
   380             << static_cast<const uchar *>(rgb32ImageData)
       
   381             << int(sizeof(rgb32ImageData))
       
   382             << 8
       
   383             << QVideoFrame::Format_RGB565
       
   384             << QSize(2, 2)
       
   385             << static_cast<const uchar *>(rgb565ImageData)
       
   386             << int(sizeof(rgb565ImageData))
       
   387             << 4;
       
   388 
       
   389     QTest::newRow("rgb24 -> rgb565")
       
   390             << QVideoFrame::Format_RGB24
       
   391             << QSize(2, 2)
       
   392             << static_cast<const uchar *>(rgb24ImageData)
       
   393             << int(sizeof(rgb24ImageData))
       
   394             << 8
       
   395             << QVideoFrame::Format_RGB565
       
   396             << QSize(2, 2)
       
   397             << static_cast<const uchar *>(rgb565ImageData)
       
   398             << int(sizeof(rgb565ImageData))
       
   399             << 4;
       
   400 }
       
   401 
       
   402 void tst_QPainterVideoSurface::present()
       
   403 {
       
   404     QFETCH(QVideoFrame::PixelFormat, pixelFormatA);
       
   405     QFETCH(QSize, frameSizeA);
       
   406     QFETCH(const uchar *, frameDataA);
       
   407     QFETCH(int, bytesA);
       
   408     QFETCH(int, bytesPerLineA);
       
   409     QFETCH(QVideoFrame::PixelFormat, pixelFormatB);
       
   410     QFETCH(QSize, frameSizeB);
       
   411     QFETCH(const uchar *, frameDataB);
       
   412     QFETCH(int, bytesB);
       
   413     QFETCH(int, bytesPerLineB);
       
   414 
       
   415     QPainterVideoSurface surface;
       
   416 
       
   417     QImage image(320, 240, QImage::Format_RGB32);
       
   418 
       
   419     QSignalSpy frameSpy(&surface, SIGNAL(frameChanged()));
       
   420 
       
   421     const QList<QVideoFrame::PixelFormat> pixelFormats = surface.supportedPixelFormats();
       
   422 
       
   423     {   // Test painting before started.
       
   424         QPainter painter(&image);
       
   425         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   426     }
       
   427 
       
   428     QVideoSurfaceFormat formatA(frameSizeA, pixelFormatA);
       
   429 
       
   430     QVERIFY(surface.start(formatA));
       
   431     QCOMPARE(surface.isActive(), true);
       
   432     QCOMPARE(surface.isReady(), true);
       
   433 
       
   434     {   // Test painting before receiving a frame.
       
   435         QPainter painter(&image);
       
   436         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   437     }
       
   438     QCOMPARE(surface.error(), QAbstractVideoSurface::NoError);
       
   439 
       
   440     QVideoFrame frameA(bytesA, frameSizeA, bytesPerLineA, pixelFormatA);
       
   441 
       
   442     frameA.map(QAbstractVideoBuffer::WriteOnly);
       
   443     memcpy(frameA.bits(), frameDataA, frameA.mappedBytes());
       
   444     frameA.unmap();
       
   445 
       
   446     QVERIFY(surface.present(frameA));
       
   447     QCOMPARE(surface.isReady(), false);
       
   448     QCOMPARE(frameSpy.count(), 1);
       
   449 
       
   450     {
       
   451         QPainter painter(&image);
       
   452         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   453     }
       
   454     QCOMPARE(surface.isActive(), true);
       
   455     QCOMPARE(surface.isReady(), false);
       
   456 
       
   457     {   // Test repainting before receiving another frame.
       
   458         QPainter painter(&image);
       
   459         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   460     }
       
   461     QCOMPARE(surface.isActive(), true);
       
   462     QCOMPARE(surface.isReady(), false);
       
   463 
       
   464     // Not ready.
       
   465     QVERIFY(!surface.present(frameA));
       
   466     QCOMPARE(frameSpy.count(), 1);
       
   467 
       
   468     surface.setReady(true);
       
   469     QCOMPARE(surface.isReady(), true);
       
   470     QVERIFY(surface.present(frameA));
       
   471     QCOMPARE(frameSpy.count(), 2);
       
   472 
       
   473     // Try switching to a different format after starting.
       
   474     QVideoSurfaceFormat formatB(frameSizeB, pixelFormatB);
       
   475 
       
   476     QVERIFY(surface.start(formatB));
       
   477     QCOMPARE(surface.isActive(), true);
       
   478     QCOMPARE(surface.isReady(), true);
       
   479 
       
   480     QVideoFrame frameB(bytesB, frameSizeB, bytesPerLineB, pixelFormatB);
       
   481 
       
   482     frameB.map(QAbstractVideoBuffer::WriteOnly);
       
   483     memcpy(frameB.bits(), frameDataB, frameB.mappedBytes());
       
   484     frameB.unmap();
       
   485 
       
   486     QVERIFY(surface.present(frameB));
       
   487     QCOMPARE(surface.isReady(), false);
       
   488     QCOMPARE(frameSpy.count(), 3);
       
   489 
       
   490     {
       
   491         QPainter painter(&image);
       
   492         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   493     }
       
   494     QVERIFY(surface.isActive());
       
   495 
       
   496     surface.stop();
       
   497 
       
   498     QCOMPARE(surface.isActive(), false);
       
   499     QCOMPARE(surface.isReady(), false);
       
   500 
       
   501     // Try presenting a frame while stopped.
       
   502     QVERIFY(!surface.present(frameB));
       
   503     QCOMPARE(surface.isReady(), false);
       
   504     QCOMPARE(surface.error(), QAbstractVideoSurface::StoppedError);
       
   505 
       
   506     // Try presenting a frame with a different format.
       
   507     QVERIFY(surface.start(formatB));
       
   508     QVERIFY(!surface.present(frameA));
       
   509     QCOMPARE(surface.isActive(), false);
       
   510     QCOMPARE(surface.isReady(), false);
       
   511     QCOMPARE(surface.error(), QAbstractVideoSurface::IncorrectFormatError);
       
   512 }
       
   513 
       
   514 void tst_QPainterVideoSurface::presentOpaqueFrame()
       
   515 {
       
   516     QPainterVideoSurface surface;
       
   517 
       
   518     QImage image(320, 240, QImage::Format_RGB32);
       
   519 
       
   520     QVideoSurfaceFormat format(QSize(64, 64), QVideoFrame::Format_RGB32);
       
   521 
       
   522     QVERIFY(surface.start(format));
       
   523     QCOMPARE(surface.isActive(), true);
       
   524     QCOMPARE(surface.isReady(), true);
       
   525 
       
   526     QVideoFrame frame(new QtTestOpaqueVideoBuffer, QSize(64, 64), QVideoFrame::Format_RGB32);
       
   527 
       
   528     if (surface.present(frame)) {
       
   529         QPainter painter(&image);
       
   530         surface.paint(&painter, QRect(0, 0, 320, 240));
       
   531     }
       
   532 
       
   533     QCOMPARE(surface.isActive(), false);
       
   534     QCOMPARE(surface.isReady(), false);
       
   535     QCOMPARE(surface.error(), QAbstractVideoSurface::IncorrectFormatError);
       
   536 }
       
   537 
       
   538 #if !defined(QT_NO_OPENGL) && !defined(QT_OPENGL_ES_1_CL) && !defined(QT_OPENGL_ES_1)
       
   539 
       
   540 void tst_QPainterVideoSurface::shaderType()
       
   541 {
       
   542     QPainterVideoSurface surface;
       
   543     QGLWidget widget;
       
   544     widget.makeCurrent();
       
   545 
       
   546     QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   547     QCOMPARE(surface.supportedShaderTypes(), QPainterVideoSurface::NoShaders);
       
   548 
       
   549     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
   550     QCOMPARE(surface.glContext(), widget.context());
       
   551 
       
   552     {
       
   553         QSignalSpy spy(&surface, SIGNAL(supportedFormatsChanged()));
       
   554 
       
   555         surface.setShaderType(QPainterVideoSurface::NoShaders);
       
   556         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   557         QCOMPARE(spy.count(), 0);
       
   558     }
       
   559 
       
   560 #ifndef QT_OPENGL_ES
       
   561     if (surface.supportedShaderTypes() & QPainterVideoSurface::FragmentProgramShader) {
       
   562         QSignalSpy spy(&surface, SIGNAL(supportedFormatsChanged()));
       
   563 
       
   564         surface.setShaderType(QPainterVideoSurface::FragmentProgramShader);
       
   565         QCOMPARE(surface.shaderType(), QPainterVideoSurface::FragmentProgramShader);
       
   566         QCOMPARE(spy.count(), 1);
       
   567 
       
   568         surface.setShaderType(QPainterVideoSurface::FragmentProgramShader);
       
   569         QCOMPARE(surface.shaderType(), QPainterVideoSurface::FragmentProgramShader);
       
   570         QCOMPARE(spy.count(), 1);
       
   571     }
       
   572 #endif
       
   573 
       
   574     if (surface.supportedShaderTypes() & QPainterVideoSurface::GlslShader) {
       
   575         QSignalSpy spy(&surface, SIGNAL(supportedFormatsChanged()));
       
   576 
       
   577         surface.setShaderType(QPainterVideoSurface::GlslShader);
       
   578         QCOMPARE(surface.shaderType(), QPainterVideoSurface::GlslShader);
       
   579         QCOMPARE(spy.count(), 1);
       
   580 
       
   581         surface.setShaderType(QPainterVideoSurface::GlslShader);
       
   582         QCOMPARE(surface.shaderType(), QPainterVideoSurface::GlslShader);
       
   583         QCOMPARE(spy.count(), 1);
       
   584     }
       
   585 
       
   586     {
       
   587         QSignalSpy spy(&surface, SIGNAL(supportedFormatsChanged()));
       
   588 
       
   589         surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
   590         QCOMPARE(surface.glContext(), widget.context());
       
   591         QCOMPARE(spy.count(), 0);
       
   592     }
       
   593 
       
   594     surface.setGLContext(0);
       
   595     QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   596     QCOMPARE(surface.supportedShaderTypes(), QPainterVideoSurface::NoShaders);
       
   597 
       
   598     {
       
   599         QSignalSpy spy(&surface, SIGNAL(supportedFormatsChanged()));
       
   600 
       
   601         surface.setShaderType(QPainterVideoSurface::NoShaders);
       
   602         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   603         QCOMPARE(spy.count(), 0);
       
   604 
       
   605 #ifndef QT_OPENGL_ES
       
   606         surface.setShaderType(QPainterVideoSurface::FragmentProgramShader);
       
   607         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   608         QCOMPARE(spy.count(), 0);
       
   609 #endif
       
   610 
       
   611         surface.setShaderType(QPainterVideoSurface::GlslShader);
       
   612         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   613         QCOMPARE(spy.count(), 0);
       
   614     }
       
   615 }
       
   616 
       
   617 void tst_QPainterVideoSurface::shaderTypeStarted_data()
       
   618 {
       
   619     QTest::addColumn<QPainterVideoSurface::ShaderType>("shaderType");
       
   620 
       
   621 #ifndef QT_OPENGL_ES
       
   622     QTest::newRow("ARBfp")
       
   623             << QPainterVideoSurface::FragmentProgramShader;
       
   624 #endif
       
   625     QTest::newRow("GLSL")
       
   626             << QPainterVideoSurface::GlslShader;
       
   627 }
       
   628 
       
   629 void tst_QPainterVideoSurface::shaderTypeStarted()
       
   630 {
       
   631     QFETCH(QPainterVideoSurface::ShaderType, shaderType);
       
   632 
       
   633     QGLWidget widget;
       
   634     widget.makeCurrent();
       
   635 
       
   636     QPainterVideoSurface surface;
       
   637 
       
   638     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
   639 
       
   640     if (!(surface.supportedShaderTypes() & shaderType))
       
   641         QSKIP("Shader type unsupported on this platform", SkipSingle);
       
   642 
       
   643     surface.setShaderType(shaderType);
       
   644     QCOMPARE(surface.shaderType(), shaderType);
       
   645 
       
   646     QVERIFY(surface.start(QVideoSurfaceFormat(QSize(640, 480), QVideoFrame::Format_RGB32)));
       
   647     {
       
   648         QSignalSpy spy(&surface, SIGNAL(activeChanged(bool)));
       
   649 
       
   650         surface.setShaderType(QPainterVideoSurface::NoShaders);
       
   651         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   652         QCOMPARE(surface.isActive(), false);
       
   653         QCOMPARE(spy.count(), 1);
       
   654         QCOMPARE(spy.last().value(0).toBool(), false);
       
   655     }
       
   656 
       
   657     QVERIFY(surface.start(QVideoSurfaceFormat(QSize(640, 480), QVideoFrame::Format_RGB32)));
       
   658     {
       
   659         QSignalSpy spy(&surface, SIGNAL(activeChanged(bool)));
       
   660 
       
   661         surface.setShaderType(QPainterVideoSurface::NoShaders);
       
   662         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   663         QCOMPARE(surface.isActive(), true);
       
   664         QCOMPARE(spy.count(), 0);
       
   665 
       
   666         surface.setShaderType(shaderType);
       
   667         QCOMPARE(surface.shaderType(), shaderType);
       
   668         QCOMPARE(surface.isActive(), false);
       
   669         QCOMPARE(spy.count(), 1);
       
   670         QCOMPARE(spy.last().value(0).toBool(), false);
       
   671     }
       
   672 
       
   673     QVERIFY(surface.start(QVideoSurfaceFormat(QSize(640, 480), QVideoFrame::Format_RGB32)));
       
   674     {
       
   675         QSignalSpy spy(&surface, SIGNAL(activeChanged(bool)));
       
   676 
       
   677         surface.setShaderType(shaderType);
       
   678         QCOMPARE(surface.shaderType(), shaderType);
       
   679         QCOMPARE(surface.isActive(), true);
       
   680         QCOMPARE(spy.count(), 0);
       
   681 
       
   682         surface.setGLContext(0);
       
   683         QCOMPARE(surface.shaderType(), QPainterVideoSurface::NoShaders);
       
   684         QCOMPARE(surface.isActive(), false);
       
   685         QCOMPARE(spy.count(), 1);
       
   686         QCOMPARE(spy.last().value(0).toBool(), false);
       
   687     }
       
   688 }
       
   689 
       
   690 void tst_QPainterVideoSurface::shaderSupportedFormat_data()
       
   691 {
       
   692     QTest::addColumn<QPainterVideoSurface::ShaderType>("shaderType");
       
   693     QTest::addColumn<QAbstractVideoBuffer::HandleType>("handleType");
       
   694     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormat");
       
   695     QTest::addColumn<QSize>("frameSize");
       
   696     QTest::addColumn<bool>("supportedPixelFormat");
       
   697     QTest::addColumn<bool>("supportedFormat");
       
   698 
       
   699     QList<QPair<QPainterVideoSurface::ShaderType, QByteArray> > types;
       
   700 
       
   701 
       
   702 #ifndef QT_OPENGL_ES
       
   703     types << qMakePair(QPainterVideoSurface::FragmentProgramShader, QByteArray("ARBfp: "));
       
   704 #endif
       
   705     types << qMakePair(QPainterVideoSurface::GlslShader, QByteArray("GLSL: "));
       
   706 
       
   707     QPair<QPainterVideoSurface::ShaderType, QByteArray> type;
       
   708     foreach (type, types) {
       
   709         QTest::newRow((type.second + "rgb32 640x480").constData())
       
   710                 << type.first
       
   711                 << QAbstractVideoBuffer::NoHandle
       
   712                 << QVideoFrame::Format_RGB32
       
   713                 << QSize(640, 480)
       
   714                 << true
       
   715                 << true;
       
   716         QTest::newRow((type.second + "rgb32 -640x480").constData())
       
   717                 << type.first
       
   718                 << QAbstractVideoBuffer::NoHandle
       
   719                 << QVideoFrame::Format_RGB32
       
   720                 << QSize(-640, 480)
       
   721                 << true
       
   722                 << false;
       
   723         QTest::newRow((type.second + "rgb565 32x32").constData())
       
   724                 << type.first
       
   725                 << QAbstractVideoBuffer::NoHandle
       
   726                 << QVideoFrame::Format_RGB565
       
   727                 << QSize(32, 32)
       
   728                 << true
       
   729                 << true;
       
   730         QTest::newRow((type.second + "rgb565 0x0").constData())
       
   731                 << type.first
       
   732                 << QAbstractVideoBuffer::NoHandle
       
   733                 << QVideoFrame::Format_RGB565
       
   734                 << QSize(0, 0)
       
   735                 << true
       
   736                 << false;
       
   737         QTest::newRow((type.second + "argb32 256x256").constData())
       
   738                 << type.first
       
   739                 << QAbstractVideoBuffer::NoHandle
       
   740                 << QVideoFrame::Format_ARGB32
       
   741                 << QSize(256, 256)
       
   742                 << true
       
   743                 << true;
       
   744         QTest::newRow((type.second + "rgb24 1024x768").constData())
       
   745                 << type.first
       
   746                 << QAbstractVideoBuffer::NoHandle
       
   747                 << QVideoFrame::Format_RGB24
       
   748                 << QSize(1024, 768)
       
   749                 << true
       
   750                 << true;
       
   751         QTest::newRow((type.second + "rgb24 -1024x-768").constData())
       
   752                 << type.first
       
   753                 << QAbstractVideoBuffer::NoHandle
       
   754                 << QVideoFrame::Format_RGB24
       
   755                 << QSize(-1024, -768)
       
   756                 << true
       
   757                 << false;
       
   758 #ifndef Q_WS_MAC
       
   759         QTest::newRow((type.second + "YUV420P 640x480").constData())
       
   760                 << type.first
       
   761                 << QAbstractVideoBuffer::NoHandle
       
   762                 << QVideoFrame::Format_YUV420P
       
   763                 << QSize(640, 480)
       
   764                 << true
       
   765                 << true;
       
   766         QTest::newRow((type.second + "YUV420P 640x-480").constData())
       
   767                 << type.first
       
   768                 << QAbstractVideoBuffer::NoHandle
       
   769                 << QVideoFrame::Format_YUV420P
       
   770                 << QSize(640, -480)
       
   771                 << true
       
   772                 << false;
       
   773 #endif
       
   774         QTest::newRow((type.second + "Y8 640x480").constData())
       
   775                 << type.first
       
   776                 << QAbstractVideoBuffer::NoHandle
       
   777                 << QVideoFrame::Format_Y8
       
   778                 << QSize(640, 480)
       
   779                 << false
       
   780                 << false;
       
   781         QTest::newRow((type.second + "Texture: rgb32 640x480").constData())
       
   782                 << type.first
       
   783                 << QAbstractVideoBuffer::GLTextureHandle
       
   784                 << QVideoFrame::Format_RGB32
       
   785                 << QSize(640, 480)
       
   786                 << true
       
   787                 << true;
       
   788         QTest::newRow((type.second + "Texture: rgb32 -640x480").constData())
       
   789                 << type.first
       
   790                 << QAbstractVideoBuffer::GLTextureHandle
       
   791                 << QVideoFrame::Format_RGB32
       
   792                 << QSize(-640, 480)
       
   793                 << true
       
   794                 << false;
       
   795 #ifndef Q_WS_MAC
       
   796         QTest::newRow((type.second + "Texture: rgb565 32x32").constData())
       
   797                 << type.first
       
   798                 << QAbstractVideoBuffer::GLTextureHandle
       
   799                 << QVideoFrame::Format_RGB565
       
   800                 << QSize(32, 32)
       
   801                 << false
       
   802                 << false;
       
   803         QTest::newRow((type.second + "Texture: rgb565 0x0").constData())
       
   804                 << type.first
       
   805                 << QAbstractVideoBuffer::GLTextureHandle
       
   806                 << QVideoFrame::Format_RGB565
       
   807                 << QSize(0, 0)
       
   808                 << false
       
   809                 << false;
       
   810 #endif
       
   811         QTest::newRow((type.second + "Texture argb32 256x256").constData())
       
   812                 << type.first
       
   813                 << QAbstractVideoBuffer::GLTextureHandle
       
   814                 << QVideoFrame::Format_ARGB32
       
   815                 << QSize(256, 256)
       
   816                 << true
       
   817                 << true;
       
   818 #ifndef Q_WS_MAC
       
   819         QTest::newRow((type.second + "Texture: rgb24 1024x768").constData())
       
   820                 << type.first
       
   821                 << QAbstractVideoBuffer::GLTextureHandle
       
   822                 << QVideoFrame::Format_RGB24
       
   823                 << QSize(1024, 768)
       
   824                 << false
       
   825                 << false;
       
   826         QTest::newRow((type.second + "Texture: rgb24 -1024x-768").constData())
       
   827                 << type.first
       
   828                 << QAbstractVideoBuffer::GLTextureHandle
       
   829                 << QVideoFrame::Format_RGB24
       
   830                 << QSize(-1024, -768)
       
   831                 << false
       
   832                 << false;
       
   833         QTest::newRow((type.second + "Texture: YUV420P 640x480").constData())
       
   834                 << type.first
       
   835                 << QAbstractVideoBuffer::GLTextureHandle
       
   836                 << QVideoFrame::Format_YUV420P
       
   837                 << QSize(640, 480)
       
   838                 << false
       
   839                 << false;
       
   840         QTest::newRow((type.second + "Texture: YUV420P 640x-480").constData())
       
   841                 << type.first
       
   842                 << QAbstractVideoBuffer::GLTextureHandle
       
   843                 << QVideoFrame::Format_YUV420P
       
   844                 << QSize(640, -480)
       
   845                 << false
       
   846                 << false;
       
   847 #endif
       
   848         QTest::newRow(type.second + "User Buffer: rgb32 256x256")
       
   849                 << type.first
       
   850                 << QAbstractVideoBuffer::UserHandle
       
   851                 << QVideoFrame::Format_RGB32
       
   852                 << QSize(256, 256)
       
   853                 << false
       
   854                 << false;
       
   855     }
       
   856 }
       
   857 
       
   858 void tst_QPainterVideoSurface::shaderSupportedFormat()
       
   859 {
       
   860     QFETCH(QPainterVideoSurface::ShaderType, shaderType);
       
   861     QFETCH(QAbstractVideoBuffer::HandleType, handleType);
       
   862     QFETCH(QVideoFrame::PixelFormat, pixelFormat);
       
   863     QFETCH(QSize, frameSize);
       
   864     QFETCH(bool, supportedPixelFormat);
       
   865     QFETCH(bool, supportedFormat);
       
   866 
       
   867     QGLWidget widget;
       
   868     widget.makeCurrent();
       
   869 
       
   870     QPainterVideoSurface surface;
       
   871     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
   872 
       
   873 
       
   874     if (!(surface.supportedShaderTypes() & shaderType))
       
   875         QSKIP("Shader type not supported on this platform", SkipSingle);
       
   876 
       
   877     surface.setShaderType(shaderType);
       
   878     if (surface.shaderType() != shaderType)
       
   879         QSKIP("Shader type couldn't be set", SkipSingle);
       
   880 
       
   881     const QList<QVideoFrame::PixelFormat> pixelFormats = surface.supportedPixelFormats(handleType);
       
   882 
       
   883     QCOMPARE(pixelFormats.contains(pixelFormat), QBool(supportedPixelFormat));
       
   884 
       
   885     QVideoSurfaceFormat format(frameSize, pixelFormat, handleType);
       
   886 
       
   887     QCOMPARE(surface.isFormatSupported(format), supportedFormat);
       
   888     QCOMPARE(surface.start(format), supportedFormat);
       
   889 }
       
   890 
       
   891 void tst_QPainterVideoSurface::shaderPresent_data()
       
   892 {
       
   893     QTest::addColumn<QPainterVideoSurface::ShaderType>("shaderType");
       
   894     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormatA");
       
   895     QTest::addColumn<QSize>("frameSizeA");
       
   896     QTest::addColumn<const uchar *>("frameDataA");
       
   897     QTest::addColumn<int>("bytesA");
       
   898     QTest::addColumn<int>("bytesPerLineA");
       
   899     QTest::addColumn<QVideoFrame::PixelFormat>("pixelFormatB");
       
   900     QTest::addColumn<QSize>("frameSizeB");
       
   901     QTest::addColumn<const uchar *>("frameDataB");
       
   902     QTest::addColumn<int>("bytesB");
       
   903     QTest::addColumn<int>("bytesPerLineB");
       
   904 
       
   905     QList<QPair<QPainterVideoSurface::ShaderType, QByteArray> > types;
       
   906 #ifndef QT_OPENGL_ES
       
   907     types << qMakePair(QPainterVideoSurface::FragmentProgramShader, QByteArray("ARBfp: "));
       
   908 #endif
       
   909     types << qMakePair(QPainterVideoSurface::GlslShader, QByteArray("GLSL: "));
       
   910 
       
   911     QPair<QPainterVideoSurface::ShaderType, QByteArray> type;
       
   912     foreach (type, types) {
       
   913         QTest::newRow((type.second + "rgb32 -> argb32").constData())
       
   914                 << type.first
       
   915                 << QVideoFrame::Format_RGB32
       
   916                 << QSize(2, 2)
       
   917                 << static_cast<const uchar *>(rgb32ImageData)
       
   918                 << int(sizeof(rgb32ImageData))
       
   919                 << 8
       
   920                 << QVideoFrame::Format_ARGB32
       
   921                 << QSize(2, 2)
       
   922                 << static_cast<const uchar *>(argb32ImageData)
       
   923                 << int(sizeof(argb32ImageData))
       
   924                 << 8;
       
   925 
       
   926         QTest::newRow((type.second + "rgb32 -> rgb565").constData())
       
   927                 << type.first
       
   928                 << QVideoFrame::Format_RGB32
       
   929                 << QSize(2, 2)
       
   930                 << static_cast<const uchar *>(rgb32ImageData)
       
   931                 << int(sizeof(rgb32ImageData))
       
   932                 << 8
       
   933                 << QVideoFrame::Format_RGB565
       
   934                 << QSize(2, 2)
       
   935                 << static_cast<const uchar *>(rgb565ImageData)
       
   936                 << int(sizeof(rgb565ImageData))
       
   937                 << 4;
       
   938 #ifndef Q_WS_MAC
       
   939         QTest::newRow((type.second + "rgb32 -> yuv420p").constData())
       
   940                 << type.first
       
   941                 << QVideoFrame::Format_RGB32
       
   942                 << QSize(2, 2)
       
   943                 << static_cast<const uchar *>(rgb32ImageData)
       
   944                 << int(sizeof(rgb32ImageData))
       
   945                 << 8
       
   946                 << QVideoFrame::Format_YUV420P
       
   947                 << QSize(8, 8)
       
   948                 << static_cast<const uchar *>(yuvPlanarImageData)
       
   949                 << int(sizeof(yuvPlanarImageData))
       
   950                 << 8;
       
   951 
       
   952         QTest::newRow((type.second + "yv12 -> rgb32").constData())
       
   953                 << type.first
       
   954                 << QVideoFrame::Format_YV12
       
   955                 << QSize(8, 8)
       
   956                 << static_cast<const uchar *>(yuvPlanarImageData)
       
   957                 << int(sizeof(yuvPlanarImageData))
       
   958                 << 8
       
   959                 << QVideoFrame::Format_RGB32
       
   960                 << QSize(2, 2)
       
   961                 << static_cast<const uchar *>(rgb32ImageData)
       
   962                 << int(sizeof(rgb32ImageData))
       
   963                 << 8;
       
   964 #endif
       
   965     }
       
   966 }
       
   967 
       
   968 void tst_QPainterVideoSurface::shaderPresent()
       
   969 {
       
   970     QFETCH(QPainterVideoSurface::ShaderType, shaderType);
       
   971     QFETCH(QVideoFrame::PixelFormat, pixelFormatA);
       
   972     QFETCH(QSize, frameSizeA);
       
   973     QFETCH(const uchar *, frameDataA);
       
   974     QFETCH(int, bytesA);
       
   975     QFETCH(int, bytesPerLineA);
       
   976     QFETCH(QVideoFrame::PixelFormat, pixelFormatB);
       
   977     QFETCH(QSize, frameSizeB);
       
   978     QFETCH(const uchar *, frameDataB);
       
   979     QFETCH(int, bytesB);
       
   980     QFETCH(int, bytesPerLineB);
       
   981 
       
   982     QGLWidget widget;
       
   983     widget.makeCurrent();
       
   984 
       
   985     QPainterVideoSurface surface;
       
   986     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
   987 
       
   988     if (!(surface.supportedShaderTypes() & shaderType))
       
   989         QSKIP("Shader type unsupported on this platform", SkipSingle);
       
   990 
       
   991     surface.setShaderType(shaderType);
       
   992     if (surface.shaderType() != shaderType)
       
   993         QSKIP("Shader type couldn't be set", SkipSingle);
       
   994 
       
   995     QSignalSpy frameSpy(&surface, SIGNAL(frameChanged()));
       
   996 
       
   997     {   // Test painting before starting the surface.
       
   998         QPainter painter(&widget);
       
   999         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1000     }
       
  1001     QCOMPARE(surface.error(), QAbstractVideoSurface::NoError);
       
  1002 
       
  1003     QVideoSurfaceFormat formatA(frameSizeA, pixelFormatA);
       
  1004 
       
  1005     QVERIFY(surface.start(formatA));
       
  1006     QCOMPARE(surface.isActive(), true);
       
  1007     QCOMPARE(surface.isReady(), true);
       
  1008 
       
  1009     // Test painting before receiving a frame.
       
  1010     {
       
  1011         QPainter painter(&widget);
       
  1012         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1013     }
       
  1014     QCOMPARE(surface.isActive(), true);
       
  1015     QCOMPARE(surface.isReady(), true);
       
  1016 
       
  1017     QVideoFrame frameA(bytesA, frameSizeA, bytesPerLineA, pixelFormatA);
       
  1018 
       
  1019     frameA.map(QAbstractVideoBuffer::WriteOnly);
       
  1020     memcpy(frameA.bits(), frameDataA, frameA.mappedBytes());
       
  1021     frameA.unmap();
       
  1022 
       
  1023     QVERIFY(surface.present(frameA));
       
  1024     QCOMPARE(surface.isReady(), false);
       
  1025     QCOMPARE(frameSpy.count(), 1);
       
  1026 
       
  1027     {
       
  1028         QPainter painter(&widget);
       
  1029         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1030     }
       
  1031     QCOMPARE(surface.isActive(), true);
       
  1032     QCOMPARE(surface.isReady(), false);
       
  1033 
       
  1034     {   // Test repainting before receiving another frame.
       
  1035         QPainter painter(&widget);
       
  1036         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1037     }
       
  1038     QCOMPARE(surface.isActive(), true);
       
  1039     QCOMPARE(surface.isReady(), false);
       
  1040 
       
  1041     // Not ready.
       
  1042     QVERIFY(!surface.present(frameA));
       
  1043     QCOMPARE(frameSpy.count(), 1);
       
  1044 
       
  1045     surface.setReady(true);
       
  1046     QCOMPARE(surface.isReady(), true);
       
  1047     QVERIFY(surface.present(frameA));
       
  1048     QCOMPARE(frameSpy.count(), 2);
       
  1049 
       
  1050     // Try switching to a different format after starting.
       
  1051     QVideoSurfaceFormat formatB(frameSizeB, pixelFormatB);
       
  1052 
       
  1053     QVERIFY(surface.start(formatB));
       
  1054     QCOMPARE(surface.isActive(), true);
       
  1055     QCOMPARE(surface.isReady(), true);
       
  1056 
       
  1057     QVideoFrame frameB(bytesB, frameSizeB, bytesPerLineB, pixelFormatB);
       
  1058 
       
  1059     frameB.map(QAbstractVideoBuffer::WriteOnly);
       
  1060     memcpy(frameB.bits(), frameDataB, frameB.mappedBytes());
       
  1061     frameB.unmap();
       
  1062 
       
  1063     QVERIFY(surface.present(frameB));
       
  1064     QCOMPARE(surface.isReady(), false);
       
  1065     QCOMPARE(frameSpy.count(), 3);
       
  1066 
       
  1067     {
       
  1068         QPainter painter(&widget);
       
  1069         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1070     }
       
  1071     QCOMPARE(surface.isActive(), true);
       
  1072 
       
  1073     surface.stop();
       
  1074     QCOMPARE(surface.isActive(), false);
       
  1075     QCOMPARE(surface.isReady(), false);
       
  1076 
       
  1077     // Try presenting a frame while stopped.
       
  1078     QVERIFY(!surface.present(frameB));
       
  1079     QCOMPARE(surface.isReady(), false);
       
  1080     QCOMPARE(surface.error(), QAbstractVideoSurface::StoppedError);
       
  1081 
       
  1082     // Try stopping while already stopped.
       
  1083     surface.stop();
       
  1084     QCOMPARE(surface.isActive(), false);
       
  1085     QCOMPARE(surface.isReady(), false);
       
  1086 
       
  1087     // Try presenting a frame with a different format.
       
  1088     QVERIFY(surface.start(formatB));
       
  1089     QVERIFY(!surface.present(frameA));
       
  1090     QCOMPARE(surface.isActive(), false);
       
  1091     QCOMPARE(surface.isReady(), false);
       
  1092     QCOMPARE(surface.error(), QAbstractVideoSurface::IncorrectFormatError);
       
  1093 }
       
  1094 
       
  1095 void tst_QPainterVideoSurface::shaderPresentOpaqueFrame_data()
       
  1096 {
       
  1097     QTest::addColumn<QPainterVideoSurface::ShaderType>("shaderType");
       
  1098 
       
  1099 #ifndef QT_OPENGL_ES
       
  1100     QTest::newRow("ARBfp")
       
  1101             << QPainterVideoSurface::FragmentProgramShader;
       
  1102 #endif
       
  1103     QTest::newRow("GLSL")
       
  1104             << QPainterVideoSurface::GlslShader;
       
  1105 }
       
  1106 
       
  1107 void tst_QPainterVideoSurface::shaderPresentOpaqueFrame()
       
  1108 {
       
  1109     QFETCH(QPainterVideoSurface::ShaderType, shaderType);
       
  1110 
       
  1111     QGLWidget widget;
       
  1112     widget.makeCurrent();
       
  1113 
       
  1114     QPainterVideoSurface surface;
       
  1115     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
  1116 
       
  1117     if (!(surface.supportedShaderTypes() & shaderType))
       
  1118         QSKIP("Shader type unsupported on this platform", SkipSingle);
       
  1119 
       
  1120     surface.setShaderType(shaderType);
       
  1121     if (surface.shaderType() != shaderType)
       
  1122         QSKIP("Shader type couldn't be set", SkipSingle);
       
  1123 
       
  1124     QVideoSurfaceFormat format(QSize(64, 64), QVideoFrame::Format_RGB32);
       
  1125 
       
  1126     QVERIFY(surface.start(format));
       
  1127     QCOMPARE(surface.isActive(), true);
       
  1128     QCOMPARE(surface.isReady(), true);
       
  1129 
       
  1130     QVideoFrame frame(new QtTestOpaqueVideoBuffer, QSize(64, 64), QVideoFrame::Format_RGB32);
       
  1131 
       
  1132     if (surface.present(frame)) {
       
  1133         QPainter painter(&widget);
       
  1134         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1135     }
       
  1136 
       
  1137     QCOMPARE(surface.isActive(), false);
       
  1138     QCOMPARE(surface.isReady(), false);
       
  1139     QCOMPARE(surface.error(), QAbstractVideoSurface::IncorrectFormatError);
       
  1140 }
       
  1141 
       
  1142 void tst_QPainterVideoSurface::shaderPresentGLFrame_data()
       
  1143 {
       
  1144     QTest::addColumn<QPainterVideoSurface::ShaderType>("shaderType");
       
  1145 
       
  1146 #ifndef QT_OPENGL_ES
       
  1147     QTest::newRow("ARBfp")
       
  1148             << QPainterVideoSurface::FragmentProgramShader;
       
  1149 #endif
       
  1150     QTest::newRow("GLSL")
       
  1151             << QPainterVideoSurface::GlslShader;
       
  1152 }
       
  1153 
       
  1154 void tst_QPainterVideoSurface::shaderPresentGLFrame()
       
  1155 {
       
  1156     QFETCH(QPainterVideoSurface::ShaderType, shaderType);
       
  1157 
       
  1158     QGLWidget widget;
       
  1159     widget.makeCurrent();
       
  1160 
       
  1161     QPainterVideoSurface surface;
       
  1162     surface.setGLContext(const_cast<QGLContext *>(widget.context()));
       
  1163 
       
  1164     if (!(surface.supportedShaderTypes() & shaderType))
       
  1165         QSKIP("Shader type unsupported on this platform", SkipSingle);
       
  1166 
       
  1167     surface.setShaderType(shaderType);
       
  1168     if (surface.shaderType() != shaderType)
       
  1169         QSKIP("Shader type couldn't be set", SkipSingle);
       
  1170 
       
  1171     QVideoSurfaceFormat format(
       
  1172             QSize(2, 2), QVideoFrame::Format_RGB32, QAbstractVideoBuffer::GLTextureHandle);
       
  1173 
       
  1174     QVERIFY(surface.start(format));
       
  1175     QCOMPARE(surface.isActive(), true);
       
  1176     QCOMPARE(surface.isReady(), true);
       
  1177 
       
  1178     QtTestGLVideoBuffer *buffer = new QtTestGLVideoBuffer;
       
  1179 
       
  1180     glBindTexture(GL_TEXTURE_2D, buffer->textureId());
       
  1181     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, rgb32ImageData);
       
  1182     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
  1183     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
  1184     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
       
  1185     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
       
  1186 
       
  1187     QVideoFrame frame(buffer, QSize(2, 2), QVideoFrame::Format_RGB32);
       
  1188 
       
  1189     QVERIFY(surface.present(frame));
       
  1190 
       
  1191     {
       
  1192         QPainter painter(&widget);
       
  1193         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1194     }
       
  1195 
       
  1196     QCOMPARE(surface.isActive(), true);
       
  1197     QCOMPARE(surface.isReady(), false);
       
  1198 
       
  1199     {
       
  1200         QPainter painter(&widget);
       
  1201         surface.paint(&painter, QRect(0, 0, 320, 240));
       
  1202     }
       
  1203 
       
  1204     QCOMPARE(surface.isActive(), true);
       
  1205     QCOMPARE(surface.isReady(), false);
       
  1206 }
       
  1207 
       
  1208 #endif
       
  1209 
       
  1210 QTEST_MAIN(tst_QPainterVideoSurface)
       
  1211 
       
  1212 #include "tst_qpaintervideosurface.moc"