summaryrefslogtreecommitdiffstats
path: root/src/declarative/util/qmlpixmapcache.cpp
blob: c03b5df9eafc62a38470a39a5d24a084cff6a71d (plain)
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtDeclarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** 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, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qmlpixmapcache_p.h"
#include "qmlnetworkaccessmanagerfactory.h"

#include "qfxperf_p_p.h"

#include <qmlengine.h>
#include <private/qmlglobal_p.h>

#include <QCoreApplication>
#include <QImageReader>
#include <QHash>
#include <QNetworkReply>
#include <QPixmapCache>
#include <QFile>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QtCore/qdebug.h>
#include <private/qobject_p.h>
#include <QSslError>

#ifdef Q_OS_LINUX
#include <pthread.h>
#include <linux/sched.h>
#endif

// Maximum number of simultaneous image requests to send.
static const int maxImageRequestCount = 8;

QT_BEGIN_NAMESPACE

#if (QT_VERSION < QT_VERSION_CHECK(4, 7, 0))
inline uint qHash(const QUrl &uri)
{
    return qHash(uri.toEncoded(QUrl::FormattingOption(0x100)));
}
#endif

class QmlImageReaderEvent : public QEvent
{
public:
    enum ReadError { NoError, Loading, Decoding };

    QmlImageReaderEvent(QmlImageReaderEvent::ReadError err, const QString &errStr, QImage &img)
        : QEvent(QEvent::User), error(err), errorString(errStr), image(img) {}

    ReadError error;
    QString errorString;
    QImage image;
};

class QmlImageRequestHandler;
class QmlImageReader : public QThread
{
    Q_OBJECT
public:
    QmlImageReader(QmlEngine *eng);
    ~QmlImageReader();

    QmlPixmapReply *getImage(const QUrl &url);
    void cancel(QmlPixmapReply *rep);

    static QmlImageReader *instance(QmlEngine *engine);

protected:
    void run();

private:
    QList<QmlPixmapReply*> jobs;
    QList<QmlPixmapReply*> cancelled;
    QmlEngine *engine;
    QmlImageRequestHandler *handler;
    QMutex mutex;

    static QHash<QmlEngine *,QmlImageReader*> readers;
    static QMutex readerMutex;
    friend class QmlImageRequestHandler;
};

QHash<QmlEngine *,QmlImageReader*> QmlImageReader::readers;
QMutex QmlImageReader::readerMutex;


class QmlImageRequestHandler : public QObject
{
    Q_OBJECT
public:
    QmlImageRequestHandler(QmlImageReader *read, QmlEngine *eng)
        : QObject(), accessManager(0), engine(eng), reader(read)
    {
        QCoreApplication::postEvent(this, new QEvent(QEvent::User));
    }

    QmlPixmapReply *getImage(const QUrl &url);
    void cancel(QmlPixmapReply *reply);

protected:
    bool event(QEvent *event);

private slots:
    void networkRequestDone();

private:
    QNetworkAccessManager *networkAccessManager() {
        if (!accessManager) {
            if (engine && engine->networkAccessManagerFactory()) {
                accessManager = engine->networkAccessManagerFactory()->create(this);
            } else {
                accessManager = new QNetworkAccessManager(this);
            }
        }
        return accessManager;
    }

    QHash<QNetworkReply*,QmlPixmapReply*> replies;
    QNetworkAccessManager *accessManager;
    QmlEngine *engine;
    QmlImageReader *reader;
};

//===========================================================================

bool QmlImageRequestHandler::event(QEvent *event)
{
    if (event->type() == QEvent::User) {
        static int replyDownloadProgress = -1;
        static int replyFinished = -1;
        static int downloadProgress = -1;
        static int thisNetworkRequestDone = -1;

        if (replyDownloadProgress == -1) {
            replyDownloadProgress = QNetworkReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
            replyFinished = QNetworkReply::staticMetaObject.indexOfSignal("finished()");
            downloadProgress = QmlPixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)");
            thisNetworkRequestDone = QmlImageRequestHandler::staticMetaObject.indexOfSlot("networkRequestDone()");
        }

        while (1) {
            reader->mutex.lock();

            if (reader->cancelled.count()) {
                for (int i = 0; i < reader->cancelled.count(); ++i) {
                    QmlPixmapReply *job = reader->cancelled.at(i);
                    QNetworkReply *reply = replies.key(job, 0);
                    if (reply && reply->isRunning()) {
                        replies.remove(reply);
                        reply->close();
                        job->release(true);
                    }
                }
                reader->cancelled.clear();
            }

            if (!reader->jobs.count() || replies.count() > maxImageRequestCount) {
                reader->mutex.unlock();
                break;
            }

            QmlPixmapReply *runningJob = reader->jobs.takeFirst();
            runningJob->addRef();
            runningJob->setLoading();
            QUrl url = runningJob->url();
            reader->mutex.unlock();

            // fetch
            QNetworkRequest req(url);
            req.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true);
            QNetworkReply *reply = networkAccessManager()->get(req);

            QMetaObject::connect(reply, replyDownloadProgress, runningJob, downloadProgress);
            QMetaObject::connect(reply, replyFinished, this, thisNetworkRequestDone);

            replies.insert(reply, runningJob);
        }
        return true;
    }

    return QObject::event(event);
}

void QmlImageRequestHandler::networkRequestDone()
{
    QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
    QmlPixmapReply *job = replies.take(reply);
    if (job) {
        QImage image;
        QmlImageReaderEvent::ReadError error;
        QString errorString;
        if (reply->error()) {
            error = QmlImageReaderEvent::Loading;
            errorString = reply->errorString();
        } else {
            QImageReader imgio(reply);
            if (imgio.read(&image)) {
                error = QmlImageReaderEvent::NoError;
            } else {
                errorString = QLatin1String("Error decoding: ") + reply->url().toString()
                              + QLatin1String(" \"") + imgio.errorString() + QLatin1String("\"");
                error = QmlImageReaderEvent::Decoding;
            }
        }
        // send completion event to the QmlPixmapReply
        QCoreApplication::postEvent(job, new QmlImageReaderEvent(error, errorString, image));
    }
    // kick off event loop again if we have dropped below max request count
    if (replies.count() == maxImageRequestCount)
        QCoreApplication::postEvent(this, new QEvent(QEvent::User));
    reply->deleteLater();
}

//===========================================================================

QmlImageReader::QmlImageReader(QmlEngine *eng)
    : QThread(eng), engine(eng), handler(0)
{
    start(QThread::LowPriority);
}

QmlImageReader::~QmlImageReader()
{
    quit();
    wait();
    readerMutex.lock();
    readers.remove(engine);
    readerMutex.unlock();
    delete handler;
}

QmlImageReader *QmlImageReader::instance(QmlEngine *engine)
{
    readerMutex.lock();
    QmlImageReader *reader = readers.value(engine);
    if (!reader) {
        reader = new QmlImageReader(engine);
        readers.insert(engine, reader);
    }
    readerMutex.unlock();

    return reader;
}

QmlPixmapReply *QmlImageReader::getImage(const QUrl &url)
{
    mutex.lock();
    QmlPixmapReply *reply = new QmlPixmapReply(this, url);
    jobs.append(reply);
    if (jobs.count() == 1 && handler)
        QCoreApplication::postEvent(handler, new QEvent(QEvent::User));
    mutex.unlock();
    return reply;
}

void QmlImageReader::cancel(QmlPixmapReply *reply)
{
    mutex.lock();
    if (reply->isLoading()) {
        // Already requested.  Add to cancel list to be cancelled in reader thread.
        cancelled.append(reply);
        if (cancelled.count() == 1 && handler)
            QCoreApplication::postEvent(handler, new QEvent(QEvent::User));
    } else {
        // Not yet processed - just remove from waiting list
        QList<QmlPixmapReply*>::iterator it = jobs.begin();
        while (it != jobs.end()) {
            QmlPixmapReply *job = *it;
            if (job == reply) {
                jobs.erase(it);
                break;
            }
            ++it;
        }
    }
    mutex.unlock();
}

void QmlImageReader::run()
{
#if defined(Q_OS_LINUX) && defined(SCHED_IDLE)
    struct sched_param param;
    int policy;

    pthread_getschedparam(pthread_self(), &policy, &param);
    pthread_setschedparam(pthread_self(), SCHED_IDLE, &param);
#endif

    handler = new QmlImageRequestHandler(this, engine);

    exec();
}

//===========================================================================

static bool readImage(QIODevice *dev, QPixmap *pixmap, QString &errorString)
{
    QImageReader imgio(dev);

//#define QT_TEST_SCALED_SIZE
#ifdef QT_TEST_SCALED_SIZE
    /*
    Some mechanism is needed for loading images at a limited size, especially
    for remote images. Loading only thumbnails of remote progressive JPEG
    images can be efficient. (Qt jpeg handler does not do so currently)
    */

    QSize limit(60,60);
    QSize sz = imgio.size();
    if (sz.width() > limit.width() || sz.height() > limit.height()) {
        sz.scale(limit,Qt::KeepAspectRatio);
        imgio.setScaledSize(sz);
    }
#endif

    QImage img;
    if (imgio.read(&img)) {
#ifdef QT_TEST_SCALED_SIZE
        if (!sz.isValid())
            img = img.scaled(limit,Qt::KeepAspectRatio);
#endif
        *pixmap = QPixmap::fromImage(img);
        return true;
    } else {
        errorString = imgio.errorString();
        return false;
    }
}

/*!
    \internal
    \class QmlPixmapCache
    \brief Enacapsultes a pixmap for QmlGraphics items.

    This class is NOT reentrant.
 */

static QString toLocalFileOrQrc(const QUrl& url)
{
    QString r = url.toLocalFile();
    if (r.isEmpty() && url.scheme() == QLatin1String("qrc"))
        r = QLatin1Char(':') + url.path();
    return r;
}

typedef QHash<QUrl, QmlPixmapReply *> QmlPixmapReplyHash;
Q_GLOBAL_STATIC(QmlPixmapReplyHash, qmlActivePixmapReplies);

class QmlPixmapReplyPrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QmlPixmapReply)

public:
    QmlPixmapReplyPrivate(QmlImageReader *r, const QUrl &u)
        : QObjectPrivate(), refCount(1), url(u), status(QmlPixmapReply::Loading), loading(false), reader(r) {
    }

    int refCount;
    QUrl url;
    QPixmap pixmap; // ensure reference to pixmap so QPixmapCache does not discard
    QmlPixmapReply::Status status;
    bool loading;
    QmlImageReader *reader;
};


QmlPixmapReply::QmlPixmapReply(QmlImageReader *reader, const QUrl &url)
  : QObject(*new QmlPixmapReplyPrivate(reader, url), 0)
{
}

QmlPixmapReply::~QmlPixmapReply()
{
}

const QUrl &QmlPixmapReply::url() const
{
    Q_D(const QmlPixmapReply);
    return d->url;
}

bool QmlPixmapReply::event(QEvent *event)
{
    Q_D(QmlPixmapReply);
    if (event->type() == QEvent::User) {
        d->loading = false;
        if (!release(true)) {
            QmlImageReaderEvent *de = static_cast<QmlImageReaderEvent*>(event);
            d->status = (de->error == QmlImageReaderEvent::NoError) ? Ready : Error;
            if (d->status == Ready)
                d->pixmap = QPixmap::fromImage(de->image);
            else
                qWarning() << de->errorString;
            QByteArray key = d->url.toEncoded(QUrl::FormattingOption(0x100));
            QString strKey = QString::fromLatin1(key.constData(), key.count());
            QPixmapCache::insert(strKey, d->pixmap); // note: may fail (returns false)
            emit finished();
        }
        return true;
    }

    return QObject::event(event);
}

QmlPixmapReply::Status QmlPixmapReply::status() const
{
    Q_D(const QmlPixmapReply);
    return d->status;
}

bool QmlPixmapReply::isLoading() const
{
    Q_D(const QmlPixmapReply);
    return d->loading;
}

void QmlPixmapReply::setLoading()
{
    Q_D(QmlPixmapReply);
    d->loading = true;
}

void QmlPixmapReply::addRef()
{
    Q_D(QmlPixmapReply);
    ++d->refCount;
}

bool QmlPixmapReply::release(bool defer)
{
    Q_D(QmlPixmapReply);
    Q_ASSERT(d->refCount > 0);
    --d->refCount;
    if (d->refCount == 0) {
        qmlActivePixmapReplies()->remove(d->url);
        if (d->status == Loading && !d->loading)
            d->reader->cancel(this);
        if (defer)
            deleteLater();
        else
            delete this;
        return true;
    } else if (d->refCount == 1 && d->loading) {
        // The only reference left is the reader thread.
        qmlActivePixmapReplies()->remove(d->url);
        d->reader->cancel(this);
    }

    return false;
}

/*!
    Finds the cached pixmap corresponding to \a url.
    If the image is a network resource and has not yet
    been retrieved and cached, request() must be called.

    Returns Ready, or Error if the image has been retrieved,
    otherwise the current retrieval status.
*/
QmlPixmapReply::Status QmlPixmapCache::get(const QUrl& url, QPixmap *pixmap)
{
    QmlPixmapReply::Status status = QmlPixmapReply::Unrequested;

#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
    QString lf = toLocalFileOrQrc(url);
    if (!lf.isEmpty()) {
        status = QmlPixmapReply::Ready;
        if (!QPixmapCache::find(lf,pixmap)) {
            QFile f(lf);
            if (f.open(QIODevice::ReadOnly)) {
                QString errorString;
                if (!readImage(&f, pixmap, errorString)) {
                    errorString = QLatin1String("Error decoding: ") + url.toString()
                                  + QLatin1String(" \"") + errorString + QLatin1String("\"");
                    qWarning() << errorString;
                    *pixmap = QPixmap();
                    status = QmlPixmapReply::Error;
                }
            } else {
                qWarning() << "Cannot open" << url;
                *pixmap = QPixmap();
                status = QmlPixmapReply::Error;
            }
            if (status == QmlPixmapReply::Ready)
                QPixmapCache::insert(lf, *pixmap);
        }
        return status;
    }
#endif

    QByteArray key = url.toEncoded(QUrl::FormattingOption(0x100));
    QString strKey = QString::fromLatin1(key.constData(), key.count());
    QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
    if (iter != qmlActivePixmapReplies()->end() && (*iter)->status() == QmlPixmapReply::Ready) {
        // Must check this, since QPixmapCache::insert may have failed.
        *pixmap = (*iter)->d_func()->pixmap;
        status = (*iter)->status();
        (*iter)->release();
    } else if (QPixmapCache::find(strKey, pixmap)) {
        if (iter != qmlActivePixmapReplies()->end()) {
            status = (*iter)->status();
            (*iter)->release();
        } else {
            status = pixmap->isNull() ? QmlPixmapReply::Error : QmlPixmapReply::Ready;
        }
    } else if (iter != qmlActivePixmapReplies()->end()) {
        status = QmlPixmapReply::Loading;
    }

    return status;
}

/*!
    Starts a network request to load \a url.

    Returns a QmlPixmapReply.  Caller should connect to QmlPixmapReply::finished()
    and call get() when the image is available.

    The returned QmlPixmapReply will be deleted when all request() calls are
    matched by a corresponding get() call.
*/
QmlPixmapReply *QmlPixmapCache::request(QmlEngine *engine, const QUrl &url)
{
    QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
    if (iter == qmlActivePixmapReplies()->end()) {
        QmlImageReader *reader = QmlImageReader::instance(engine);
        QmlPixmapReply *item = reader->getImage(url);
        iter = qmlActivePixmapReplies()->insert(url, item);
    } else {
        (*iter)->addRef();
    }

    return (*iter);
}

/*!
    Cancels a previous call to request().

    May also cancel loading (eg. if no other pending request).

    Any connections from the QmlPixmapReply returned by request() to \a obj will be
    disconnected.
*/
void QmlPixmapCache::cancel(const QUrl& url, QObject *obj)
{
    QmlPixmapReplyHash::Iterator iter = qmlActivePixmapReplies()->find(url);
    if (iter == qmlActivePixmapReplies()->end())
        return;

    QmlPixmapReply *reply = *iter;
    if (obj)
        QObject::disconnect(reply, 0, obj, 0);
    reply->release();
}

/*!
    This function is mainly for test verification. It returns the number of
    requests that are still unfinished.
*/
int QmlPixmapCache::pendingRequests()
{
    return qmlActivePixmapReplies()->count();
}

#include <qmlpixmapcache.moc>

QT_END_NAMESPACE