1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QQUICKPIXMAPCACHE_H
5#define QQUICKPIXMAPCACHE_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/qcoreapplication.h>
19#include <QtCore/qstring.h>
20#include <QtGui/qpixmap.h>
21#include <QtCore/qurl.h>
22#include <private/qtquickglobal_p.h>
23#include <QtQuick/qquickimageprovider.h>
24
25#include <private/qintrusivelist_p.h>
26
27QT_BEGIN_NAMESPACE
28
29class QQmlEngine;
30class QQuickPixmapData;
31class QQuickTextureFactory;
32class QQuickImageProviderOptionsPrivate;
33
34class QQuickDefaultTextureFactory : public QQuickTextureFactory
35{
36 Q_OBJECT
37public:
38 QQuickDefaultTextureFactory(const QImage &i);
39 QSGTexture *createTexture(QQuickWindow *window) const override;
40 QSize textureSize() const override { return size; }
41 int textureByteCount() const override { return size.width() * size.height() * 4; }
42 QImage image() const override { return im; }
43
44private:
45 QImage im;
46 QSize size;
47};
48
49class QQuickImageProviderPrivate
50{
51public:
52 QQuickImageProvider::ImageType type;
53 QQuickImageProvider::Flags flags;
54 bool isProviderWithOptions;
55};
56
57// ### Qt 6: Make public moving to qquickimageprovider.h
58class Q_QUICK_PRIVATE_EXPORT QQuickImageProviderOptions
59{
60public:
61 enum AutoTransform {
62 UsePluginDefaultTransform = -1,
63 ApplyTransform = 0,
64 DoNotApplyTransform = 1
65 };
66
67 QQuickImageProviderOptions();
68 ~QQuickImageProviderOptions();
69
70 QQuickImageProviderOptions(const QQuickImageProviderOptions&);
71 QQuickImageProviderOptions& operator=(const QQuickImageProviderOptions&);
72
73 bool operator==(const QQuickImageProviderOptions&) const;
74
75 AutoTransform autoTransform() const;
76 void setAutoTransform(AutoTransform autoTransform);
77
78 bool preserveAspectRatioCrop() const;
79 void setPreserveAspectRatioCrop(bool preserveAspectRatioCrop);
80
81 bool preserveAspectRatioFit() const;
82 void setPreserveAspectRatioFit(bool preserveAspectRatioFit);
83
84 QColorSpace targetColorSpace() const;
85 void setTargetColorSpace(const QColorSpace &colorSpace);
86
87private:
88 QSharedDataPointer<QQuickImageProviderOptionsPrivate> d;
89};
90
91/*! \internal
92 A class that encapsulates the action of fetching a pixmap, as well as the
93 pixmap itself (indirectly via QQuickPixmapData::textureFactory) and the
94 responsibility of canceling outstanding requests. Rather than relying on
95 QPixmapCache which doesn't cache all the information Qt Quick needs,
96 QQuickPixmap implements its own cache, that correctly degrades over time.
97 (QQuickPixmapData::release() marks it as being not-currently-used, and
98 QQuickPixmapStore::shrinkCache() sweeps away the least-recently-released
99 instances until the remaining bytes are less than cache_limit.)
100*/
101class Q_QUICK_PRIVATE_EXPORT QQuickPixmap
102{
103 Q_DECLARE_TR_FUNCTIONS(QQuickPixmap)
104public:
105 QQuickPixmap();
106 QQuickPixmap(QQmlEngine *, const QUrl &);
107 QQuickPixmap(QQmlEngine *, const QUrl &, const QRect &region, const QSize &);
108 QQuickPixmap(const QUrl &, const QImage &image);
109 ~QQuickPixmap();
110
111 enum Status { Null, Ready, Error, Loading };
112
113 enum Option {
114 Asynchronous = 0x00000001,
115 Cache = 0x00000002
116 };
117 Q_DECLARE_FLAGS(Options, Option)
118
119 bool isNull() const;
120 bool isReady() const;
121 bool isError() const;
122 bool isLoading() const;
123
124 Status status() const;
125 QString error() const;
126 const QUrl &url() const;
127 const QSize &implicitSize() const;
128 const QRect &requestRegion() const;
129 const QSize &requestSize() const;
130 QQuickImageProviderOptions::AutoTransform autoTransform() const;
131 int frameCount() const;
132 QImage image() const;
133 void setImage(const QImage &);
134 void setPixmap(const QQuickPixmap &other);
135
136 QColorSpace colorSpace() const;
137
138 QQuickTextureFactory *textureFactory() const;
139
140 QRect rect() const;
141 int width() const;
142 int height() const;
143
144 void load(QQmlEngine *, const QUrl &);
145 void load(QQmlEngine *, const QUrl &, QQuickPixmap::Options options);
146 void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize);
147 void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize, QQuickPixmap::Options options);
148 void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize,
149 QQuickPixmap::Options options, const QQuickImageProviderOptions &providerOptions, int frame = 0, int frameCount = 1,
150 qreal devicePixelRatio = 1.0);
151 void loadImageFromDevice(QQmlEngine *engine, QIODevice *device, const QUrl &url,
152 const QRect &requestRegion, const QSize &requestSize,
153 const QQuickImageProviderOptions &providerOptions, int frame = 0, int frameCount = 1);
154
155 void clear();
156 void clear(QObject *);
157
158 bool connectFinished(QObject *, const char *);
159 bool connectFinished(QObject *, int);
160 bool connectDownloadProgress(QObject *, const char *);
161 bool connectDownloadProgress(QObject *, int);
162
163 static void purgeCache();
164 static bool isCached(const QUrl &url, const QRect &requestRegion, const QSize &requestSize,
165 const int frame, const QQuickImageProviderOptions &options);
166
167 static const QLatin1String itemGrabberScheme;
168
169private:
170 Q_DISABLE_COPY(QQuickPixmap)
171 QQuickPixmapData *d;
172 QIntrusiveListNode dataListNode;
173 friend class QQuickPixmapData;
174};
175
176Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickPixmap::Options)
177
178// ### Qt 6: This should be made public in Qt 6. It's functionality can't be merged into
179// QQuickImageProvider without breaking source compatibility.
180class Q_QUICK_PRIVATE_EXPORT QQuickImageProviderWithOptions : public QQuickAsyncImageProvider
181{
182public:
183 QQuickImageProviderWithOptions(ImageType type, Flags flags = Flags());
184
185 QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize) override;
186 QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize) override;
187 QQuickTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize) override;
188 QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override;
189
190 virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options);
191 virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options);
192 virtual QQuickTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options);
193 virtual QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize, const QQuickImageProviderOptions &options);
194
195 static QSize loadSize(const QSize &originalSize, const QSize &requestedSize, const QByteArray &format, const QQuickImageProviderOptions &options,
196 qreal devicePixelRatio = 1.0);
197 static QQuickImageProviderWithOptions *checkedCast(QQuickImageProvider *provider);
198};
199
200QT_END_NAMESPACE
201
202#endif // QQUICKPIXMAPCACHE_H
203

source code of qtdeclarative/src/quick/util/qquickpixmapcache_p.h