1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 
43 #include <QtTest/QtTest>
44 #include <QtGui/private/qpixmapdata_p.h>
45 #include <QtGui/private/qnativeimagehandleprovider_p.h>
46 #include <QScopedPointer>
47 #include <QPixmap>
48 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_OPENVG)
49 #include <fbs.h>
50 #include <bitdev.h>
51 #include <QtOpenVG/private/qpixmapdata_vg_p.h>
52 #endif
53 
pixmapFromNativeImageHandleProvider(QNativeImageHandleProvider * source)54 QPixmap pixmapFromNativeImageHandleProvider(QNativeImageHandleProvider *source)
55 {
56 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_OPENVG)
57     if (!source)
58         return QPixmap();
59     QScopedPointer<QPixmapData> pd(QPixmapData::create(0, 0, QPixmapData::PixmapType));
60     pd->fromNativeType(source, QPixmapData::NativeImageHandleProvider);
61     return QPixmap(pd.take());
62 #else
63     Q_UNUSED(source);
64     return QPixmap();
65 #endif
66 }
67 
68 class DummyProvider : public QNativeImageHandleProvider
69 {
70 public:
71     void get(void **handle, QString *type);
72     void release(void *handle, const QString &type);
73 };
74 
get(void ** handle,QString * type)75 void DummyProvider::get(void **handle, QString *type)
76 {
77     *handle = (void *) 0x12345678;
78     *type = "some dummy type";
79 }
80 
release(void * handle,const QString & type)81 void DummyProvider::release(void *handle, const QString &type)
82 {
83     Q_UNUSED(handle);
84     Q_UNUSED(type);
85 }
86 
87 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_OPENVG)
88 class BitmapProvider : public QNativeImageHandleProvider
89 {
90 public:
BitmapProvider()91     BitmapProvider() : bmp(0), refCount(0), w(50), h(60) { }
92     void get(void **handle, QString *type);
93     void release(void *handle, const QString &type);
94 
95     CFbsBitmap *bmp;
96     int refCount, w, h;
97     void *returnedHandle;
98     QString returnedType;
99 };
100 
get(void ** handle,QString * type)101 void BitmapProvider::get(void **handle, QString *type)
102 {
103     // There may not be a release() if the get() fails so don't bother with
104     // refcounting in such cases.
105     if (bmp)
106         ++refCount;
107     returnedType = QLatin1String("CFbsBitmap");
108     returnedHandle = bmp;
109     *handle = returnedHandle;
110     *type = returnedType;
111 }
112 
release(void * handle,const QString & type)113 void BitmapProvider::release(void *handle, const QString &type)
114 {
115     if (handle == returnedHandle && type == returnedType && returnedHandle) {
116         --refCount;
117     }
118 }
119 #endif // symbian & openvg
120 
121 class tst_NativeImageHandleProvider : public QObject
122 {
123     Q_OBJECT
124 
125 public:
tst_NativeImageHandleProvider()126     tst_NativeImageHandleProvider() { }
127 
128 private slots:
129     void create();
130     void bitmap();
131     void hibernate();
132 };
133 
create()134 void tst_NativeImageHandleProvider::create()
135 {
136     QPixmap pm = pixmapFromNativeImageHandleProvider(0);
137     QVERIFY(pm.isNull());
138     QPixmap tmp(10, 20);
139     if (tmp.pixmapData()->classId() == QPixmapData::OpenVGClass) {
140         // Verify that null pixmap is properly returned when get() provides bogus results.
141         DummyProvider prov;
142         pm = pixmapFromNativeImageHandleProvider(&prov);
143         QVERIFY(pm.isNull());
144         pm = QPixmap();
145     } else {
146         QSKIP("Not openvg, skipping non-trivial tests", SkipSingle);
147     }
148 }
149 
bitmap()150 void tst_NativeImageHandleProvider::bitmap()
151 {
152 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_OPENVG)
153     QPixmap tmp(10, 20);
154     if (tmp.pixmapData()->classId() == QPixmapData::OpenVGClass) {
155         BitmapProvider prov;
156 
157         // This should fail because of null ptr.
158         QPixmap pm = pixmapFromNativeImageHandleProvider(&prov);
159         QVERIFY(pm.isNull());
160         pm = QPixmap();
161         QCOMPARE(prov.refCount, 0);
162 
163         prov.bmp = new CFbsBitmap;
164         QCOMPARE(prov.bmp->Create(TSize(prov.w, prov.h), EColor16MAP), KErrNone);
165         CFbsBitmapDevice *bitmapDevice = CFbsBitmapDevice::NewL(prov.bmp);
166         CBitmapContext *bitmapContext = 0;
167         QCOMPARE(bitmapDevice->CreateBitmapContext(bitmapContext), KErrNone);
168         TRgb symbianColor = TRgb(255, 200, 100);
169         bitmapContext->SetBrushColor(symbianColor);
170         bitmapContext->Clear();
171         delete bitmapContext;
172         delete bitmapDevice;
173 
174         pm = pixmapFromNativeImageHandleProvider(&prov);
175         QVERIFY(!pm.isNull());
176         QCOMPARE(pm.width(), prov.w);
177         QCOMPARE(pm.height(), prov.h);
178         QVERIFY(prov.refCount == 1);
179         QImage img = pm.toImage();
180         QVERIFY(prov.refCount == 1);
181         QRgb pix = img.pixel(QPoint(1, 2));
182         QCOMPARE(qRed(pix), symbianColor.Red());
183         QCOMPARE(qGreen(pix), symbianColor.Green());
184         QCOMPARE(qBlue(pix), symbianColor.Blue());
185 
186         pm = QPixmap(); // should result in calling release
187         QCOMPARE(prov.refCount, 0);
188         delete prov.bmp;
189     } else {
190          QSKIP("Not openvg", SkipSingle);
191     }
192 #else
193     QSKIP("Not applicable", SkipSingle);
194 #endif
195 }
196 
hibernate()197 void tst_NativeImageHandleProvider::hibernate()
198 {
199 #if defined(Q_OS_SYMBIAN) && !defined(QT_NO_OPENVG)
200     QPixmap tmp(10, 20);
201     if (tmp.pixmapData()->classId() == QPixmapData::OpenVGClass) {
202         BitmapProvider prov;
203         prov.bmp = new CFbsBitmap;
204         QCOMPARE(prov.bmp->Create(TSize(prov.w, prov.h), EColor16MAP), KErrNone);
205 
206         QPixmap pm = pixmapFromNativeImageHandleProvider(&prov);
207         QCOMPARE(prov.refCount, 1);
208 
209         QVGPixmapData *vgpd = static_cast<QVGPixmapData *>(pm.pixmapData());
210         vgpd->hibernate();
211         QCOMPARE(prov.refCount, 0);
212 
213         // Calling toVGImage() may cause some warnings as we don't have a gui initialized,
214         // but the only thing we care about here is get() being called.
215         vgpd->toVGImage();
216         QCOMPARE(prov.refCount, 1);
217 
218         pm = QPixmap();
219         QCOMPARE(prov.refCount, 0);
220         delete prov.bmp;
221     } else {
222          QSKIP("Not openvg", SkipSingle);
223     }
224 #else
225     QSKIP("Not applicable", SkipSingle);
226 #endif
227 }
228 
main(int argc,char * argv[])229 int main(int argc, char *argv[])
230 {
231     QApplication::setGraphicsSystem("openvg");
232     QApplication app(argc, argv);
233     tst_NativeImageHandleProvider tc;
234     return QTest::qExec(&tc, argc, argv);
235 }
236 
237 #include "tst_nativeimagehandleprovider.moc"
238