summaryrefslogtreecommitdiffstats
path: root/src/gui/image/qvolatileimage.cpp
blob: 350d70ff42a69f9552c163f76c9c589c9b678de4 (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
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qvolatileimage_p.h"
#include "qvolatileimagedata_p.h"
#include <QtGui/private/qpaintengine_raster_p.h>
#include <QtGui/private/qpixmapdata_p.h>

QT_BEGIN_NAMESPACE

class QVolatileImagePaintEnginePrivate : public QRasterPaintEnginePrivate
{
public:
    QVolatileImagePaintEnginePrivate() { }
    QVolatileImage *img;
};

class QVolatileImagePaintEngine : public QRasterPaintEngine
{
    Q_DECLARE_PRIVATE(QVolatileImagePaintEngine)

public:
    QVolatileImagePaintEngine(QPaintDevice *device, QVolatileImage *img);
    bool begin(QPaintDevice *device);
    bool end();
    void drawPixmap(const QPointF &p, const QPixmap &pm);
    void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
};

QVolatileImage::QVolatileImage()
    : d(new QVolatileImageData)
{
}

QVolatileImage::QVolatileImage(int w, int h, QImage::Format format)
    : d(new QVolatileImageData(w, h, format))
{
}

QVolatileImage::QVolatileImage(const QImage &sourceImage)
    : d(new QVolatileImageData(sourceImage))
{
}

QVolatileImage::QVolatileImage(void *nativeImage, void *nativeMask)
    : d(new QVolatileImageData(nativeImage, nativeMask))
{
}

// Need non-inline, non-autogenerated copy ctor, dtor, op= to keep the
// fwd declared QSharedData working.

QVolatileImage::QVolatileImage(const QVolatileImage &other)
    : d(other.d)
{
}

QVolatileImage::~QVolatileImage()
{
}

QVolatileImage &QVolatileImage::operator=(const QVolatileImage &rhs)
{
    d = rhs.d;
    return *this;
}

bool QVolatileImage::paintingActive() const
{
    return d->pengine && d->pengine->isActive();
}

bool QVolatileImage::isNull() const
{
    return d->image.isNull();
}

QImage::Format QVolatileImage::format() const
{
    return d->image.format();
}

int QVolatileImage::width() const
{
    return d->image.width();
}

int QVolatileImage::height() const
{
    return d->image.height();
}

int QVolatileImage::bytesPerLine() const
{
    return d->image.bytesPerLine();
}

int QVolatileImage::byteCount() const
{
    return d->image.byteCount();
}

int QVolatileImage::depth() const
{
    return d->image.depth();
}

bool QVolatileImage::hasAlphaChannel() const
{
    return d->image.hasAlphaChannel();
}

void QVolatileImage::beginDataAccess() const
{
    d->beginDataAccess();
}

void QVolatileImage::endDataAccess(bool readOnly) const
{
    d->endDataAccess(readOnly);
}

/*!
  Access to pixel data via bits() or constBits() should be guarded by
  begin/endDataAccess().
 */
uchar *QVolatileImage::bits()
{
    return d->image.bits();
}

const uchar *QVolatileImage::constBits() const
{
    return d->image.constBits();
}

bool QVolatileImage::ensureFormat(QImage::Format format)
{
    return d->ensureFormat(format);
}

/*!
  This will always perform a copy of the pixel data.
 */
QImage QVolatileImage::toImage() const
{
    d->beginDataAccess();
    QImage newImage = d->image.copy(); // no sharing allowed
    d->endDataAccess(true);
    return newImage;
}

/*!
  Returns a reference to the image that is potentially using some native
  buffer internally. Access to the image's pixel data should be guarded by
  begin/endDataAccess(). Use it when there is a need for QImage APIs not provided
  by this class. The returned QImage must never be shared or assigned to.
  */
QImage &QVolatileImage::imageRef() // non-const, in order to cause a detach
{
    d->ensureImage();
    return d->image;
}

/*!
  Non-detaching version, for read-only access only.
  Must be guarded by begin/endDataAccess().
 */
const QImage &QVolatileImage::constImageRef() const
{
    const_cast<QVolatileImageData *>(d.data())->ensureImage();
    return d->image;
}

void *QVolatileImage::duplicateNativeImage() const
{
    return d->duplicateNativeImage();
}

void QVolatileImage::setAlphaChannel(const QPixmap &alphaChannel)
{
    ensureFormat(QImage::Format_ARGB32_Premultiplied);
    beginDataAccess();
    imageRef().setAlphaChannel(alphaChannel.toImage());
    endDataAccess();
    d->ensureImage();
}

void QVolatileImage::fill(uint pixelValue)
{
    beginDataAccess();
    imageRef().fill(pixelValue);
    endDataAccess();
    d->ensureImage();
}

void QVolatileImage::copyFrom(QVolatileImage *source, const QRect &rect)
{
    if (source->isNull()) {
        return;
    }
    QRect r = rect;
    if (rect.isNull()) {
        r = QRect(0, 0, source->width(), source->height());
    }
    source->beginDataAccess();
    QImage &srcImgRef(source->imageRef());
    int srcbpl = srcImgRef.bytesPerLine();
    int srcbpp = srcImgRef.depth() / 8;
    const uchar *sptr = srcImgRef.constBits() + r.y() * srcbpl;
    beginDataAccess();
    QImage &dstImgRef(imageRef());
    if (!dstImgRef.isNull()) {
        int dstbpl = dstImgRef.bytesPerLine();
        uchar *dptr = dstImgRef.bits();
        for (int y = 0; y < r.height(); ++y) {
            qMemCopy(dptr, sptr + r.x() * srcbpp, r.width() * srcbpp);
            sptr += srcbpl;
            dptr += dstbpl;
        }
    }
    endDataAccess();
    source->endDataAccess(true);
}

/*!
    To be called from the PixmapData's paintEngine().
  */
QPaintEngine *QVolatileImage::paintEngine()
{
    if (!d->pengine) {
        d->pengine = new QVolatileImagePaintEngine(&imageRef(), this);
    }
    return d->pengine;
}

QVolatileImagePaintEngine::QVolatileImagePaintEngine(QPaintDevice *device,
                                                             QVolatileImage *img)
    : QRasterPaintEngine(*(new QVolatileImagePaintEnginePrivate), device)
{
    Q_D(QVolatileImagePaintEngine);
    d->img = img;
}

bool QVolatileImagePaintEngine::begin(QPaintDevice *device)
{
    Q_D(QVolatileImagePaintEngine);
    d->img->beginDataAccess();
    return QRasterPaintEngine::begin(device);
}

bool QVolatileImagePaintEngine::end()
{
    Q_D(QVolatileImagePaintEngine);
    bool ret = QRasterPaintEngine::end();
    d->img->endDataAccess();
    return ret;
}

// For non-RasterClass pixmaps drawPixmap() would call toImage() which is slow in
// our case. Therefore drawPixmap() is rerouted to drawImage().

void QVolatileImagePaintEngine::drawPixmap(const QPointF &p, const QPixmap &pm)
{
#ifdef Q_OS_SYMBIAN
    QVolatileImage img = pm.pixmapData()->toVolatileImage();
    if (!img.isNull()) {
        img.beginDataAccess();
        // imageRef() would detach and since we received the QVolatileImage from
        // toVolatileImage() by value, it would cause a copy which would ruin
        // our goal. So use constImageRef() instead.
        QRasterPaintEngine::drawImage(p, img.constImageRef());
        img.endDataAccess(true);
    } else {
        QRasterPaintEngine::drawPixmap(p, pm);
    }
#else
    QRasterPaintEngine::drawPixmap(p, pm);
#endif
}

void QVolatileImagePaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
{
#ifdef Q_OS_SYMBIAN
    QVolatileImage img = pm.pixmapData()->toVolatileImage();
    if (!img.isNull()) {
        img.beginDataAccess();
        QRasterPaintEngine::drawImage(r, img.constImageRef(), sr);
        img.endDataAccess(true);
    } else {
        QRasterPaintEngine::drawPixmap(r, pm, sr);
    }
#else
    QRasterPaintEngine::drawPixmap(r, pm, sr);
#endif
}

QT_END_NAMESPACE