1e5a00e8359fcae48702e794e0949f380d0bda27
[profile/ivi/qtbase.git] / src / gui / painting / qpaintbuffer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtGui module 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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 #include <qmath.h>
42 #include <private/qpainterpath_p.h>
43 #include <private/qpaintbuffer_p.h>
44 //#include <private/qtextengine_p.h>
45 #include <private/qfontengine_p.h>
46 #include <private/qemulationpaintengine_p.h>
47 #include <private/qimage_p.h>
48 #include <qstatictext.h>
49 #include <private/qstatictext_p.h>
50 #include <private/qrawfont_p.h>
51
52 #include <QDebug>
53
54 // #define QPAINTBUFFER_DEBUG_DRAW
55
56 QT_BEGIN_NAMESPACE
57
58 extern void qt_format_text(const QFont &font,
59                            const QRectF &_r, int tf, const QTextOption *option, const QString& str, QRectF *brect,
60                            int tabstops, int* tabarray, int tabarraylen,
61                            QPainter *painter);
62
63 QTextItemIntCopy::QTextItemIntCopy(const QTextItem &item)
64     : m_item(static_cast<const QTextItemInt &>(item))
65 {
66     QChar *chars = new QChar[m_item.num_chars];
67     unsigned short *logClusters = new unsigned short[m_item.num_chars];
68     memcpy(chars, m_item.chars, m_item.num_chars * sizeof(QChar));
69     memcpy(logClusters, m_item.logClusters, m_item.num_chars * sizeof(unsigned short));
70     m_item.chars = chars;
71     m_item.logClusters = logClusters;
72
73     const int size = QGlyphLayout::spaceNeededForGlyphLayout(m_item.glyphs.numGlyphs);
74     char *glyphLayoutData = new char[size];
75     QGlyphLayout glyphs(glyphLayoutData, m_item.glyphs.numGlyphs);
76     memcpy(glyphs.offsets, m_item.glyphs.offsets, m_item.glyphs.numGlyphs * sizeof(QFixedPoint));
77     memcpy(glyphs.glyphs, m_item.glyphs.glyphs, m_item.glyphs.numGlyphs * sizeof(HB_Glyph));
78     memcpy(glyphs.advances_x, m_item.glyphs.advances_x, m_item.glyphs.numGlyphs * sizeof(QFixed));
79     memcpy(glyphs.advances_y, m_item.glyphs.advances_y, m_item.glyphs.numGlyphs * sizeof(QFixed));
80     memcpy(glyphs.justifications, m_item.glyphs.justifications, m_item.glyphs.numGlyphs * sizeof(QGlyphJustification));
81     memcpy(glyphs.attributes, m_item.glyphs.attributes, m_item.glyphs.numGlyphs * sizeof(HB_GlyphAttributes));
82     m_item.glyphs = glyphs;
83
84     m_font = *m_item.f;
85     m_item.f = &m_font;
86
87     m_item.fontEngine->ref.ref(); // Increment reference count.
88 }
89
90 QTextItemIntCopy::~QTextItemIntCopy()
91 {
92     delete m_item.chars;
93     delete m_item.logClusters;
94     delete m_item.glyphs.data();
95     if (!m_item.fontEngine->ref.deref())
96         delete m_item.fontEngine;
97 }
98
99 /************************************************************************
100  *
101  * QPaintBufferPrivate
102  *
103  ************************************************************************/
104
105 QPaintBufferPrivate::QPaintBufferPrivate()
106     : ref(1), engine(0), penWidthAdjustment(0)
107     , calculateBoundingRect(true)
108     , cache(0)
109 {
110 }
111
112 QPaintBufferPrivate::~QPaintBufferPrivate()
113 {
114     for (int i = 0; i < commands.size(); ++i) {
115         const QPaintBufferCommand &cmd = commands.at(i);
116         if (cmd.id == QPaintBufferPrivate::Cmd_DrawTextItem)
117             delete reinterpret_cast<QTextItemIntCopy *>(qvariant_cast<void *>(variants.at(cmd.offset)));
118     }
119 }
120
121
122 inline void QPaintBufferPrivate::updateBoundingRect(const QRectF &br)
123 {
124     // transform to device coords and adjust for pen width
125     Q_ASSERT(engine && engine->painter());
126     QPainter *painter = engine->painter();
127     const QTransform transform = painter->transform();
128     QRectF devRect = transform.mapRect(br);
129     if (penWidthAdjustment > 0) {
130         devRect = devRect.adjusted(-penWidthAdjustment, -penWidthAdjustment,
131                                    penWidthAdjustment, penWidthAdjustment);
132     }
133
134     if (boundingRect.isEmpty()) {
135         boundingRect = devRect;
136     } else {
137         qreal min_x = qMin(devRect.left(), boundingRect.left());
138         qreal min_y = qMin(devRect.top(), boundingRect.top());
139         qreal max_x = qMax(devRect.right(), boundingRect.right());
140         qreal max_y = qMax(devRect.bottom(), boundingRect.bottom());
141         boundingRect = QRectF(min_x, min_y, max_x - min_x, max_y - min_y);
142     }
143     if (painter->hasClipping())
144         boundingRect &= transform.mapRect(painter->clipRegion().boundingRect());
145 }
146
147
148 /************************************************************************
149  *
150  * QPaintBuffer
151  *
152  ************************************************************************/
153
154
155
156 QPaintBuffer::QPaintBuffer()
157     : d_ptr(new QPaintBufferPrivate)
158 {
159 }
160
161 QPaintBuffer::~QPaintBuffer()
162 {
163     if (!d_ptr->ref.deref())
164         delete d_ptr;
165 }
166
167 QPaintBuffer::QPaintBuffer(const QPaintBuffer &other)
168     : QPaintDevice(), d_ptr(other.d_ptr)
169 {
170     d_ptr->ref.ref();
171 }
172
173 QPaintEngine *QPaintBuffer::paintEngine() const
174 {
175     QPaintBufferPrivate *d = const_cast<QPaintBuffer *>(this)->d_ptr;
176     if (!d->engine)
177         d->engine = new QPaintBufferEngine(d);
178     return d->engine;
179 }
180
181
182 int QPaintBuffer::metric(PaintDeviceMetric metric) const
183 {
184     int val = 0;
185     switch (metric) {
186     case PdmWidth:
187         val = qCeil(d_ptr->boundingRect.width());
188         break;
189     case PdmHeight:
190         val = qCeil(d_ptr->boundingRect.height());
191         break;
192     case PdmDpiX:
193     case PdmPhysicalDpiX:
194         val = qt_defaultDpiX();
195         break;
196     case PdmDpiY:
197     case PdmPhysicalDpiY:
198         val = qt_defaultDpiY();
199         break;
200     default:
201         val = QPaintDevice::metric(metric);
202     }
203
204     return val;
205 }
206
207 int QPaintBuffer::devType() const
208 {
209     return QInternal::PaintBuffer;
210 }
211
212 QPaintBuffer &QPaintBuffer::operator=(const QPaintBuffer &other)
213 {
214     if (other.d_ptr != d_ptr) {
215         QPaintBufferPrivate *data = other.d_ptr;
216         data->ref.ref();
217         if (d_ptr->ref.deref())
218             delete d_ptr;
219         d_ptr = data;
220     }
221     return *this;
222 }
223
224 bool QPaintBuffer::isEmpty() const
225 {
226     return d_ptr->commands.isEmpty();
227 }
228
229
230
231 void QPaintBuffer::draw(QPainter *painter, int frame) const
232 {
233 #ifdef QPAINTBUFFER_DEBUG_DRAW
234     qDebug() << "QPaintBuffer::draw() --------------------------------";
235
236     Q_D(const QPaintBuffer);
237     printf("Float buffer:");
238     for (int i=0; i<d->floats.size(); i++) {
239         if ((i % 10) == 0) {
240             printf("\n%4d-%4d: ", i, i+9);
241         }
242         printf("%4.2f  ", d->floats[i]);
243     }
244     printf("\n");
245
246     printf("Int Buffer:");
247     for (int i=0; i<d->ints.size(); i++) {
248         if ((i % 10) == 0) {
249             printf("\n%4d-%4d: ", i, i+10);
250         }
251         printf("%5d", d->ints[i]);
252     }
253     printf("\n");
254 #endif
255
256     processCommands(painter, frameStartIndex(frame), frameEndIndex(frame));
257
258 #ifdef QPAINTBUFFER_DEBUG_DRAW
259     qDebug() << "QPaintBuffer::draw() -------------------------------- DONE!";
260 #endif
261 }
262
263 int QPaintBuffer::frameStartIndex(int frame) const
264 {
265     return (frame == 0) ? 0 : d_ptr->frames.at(frame - 1);
266 }
267
268 int QPaintBuffer::frameEndIndex(int frame) const
269 {
270     return (frame == d_ptr->frames.size()) ? d_ptr->commands.size() : d_ptr->frames.at(frame);
271 }
272
273 int QPaintBuffer::processCommands(QPainter *painter, int begin, int end) const
274 {
275     if (!painter || !painter->isActive())
276         return 0;
277
278     QPaintEngineEx *xengine = painter->paintEngine()->isExtended()
279                               ? (QPaintEngineEx *) painter->paintEngine() : 0;
280     if (xengine) {
281         QPaintEngineExReplayer player;
282         player.processCommands(*this, painter, begin, end);
283     } else {
284         QPainterReplayer player;
285         player.processCommands(*this, painter, begin, end);
286     }
287
288     int depth = 0;
289     for (int i = begin; i < end; ++i) {
290         const QPaintBufferCommand &cmd = d_ptr->commands.at(i);
291         if (cmd.id == QPaintBufferPrivate::Cmd_Save)
292             ++depth;
293         else if (cmd.id == QPaintBufferPrivate::Cmd_Restore)
294             --depth;
295     }
296     return depth;
297 }
298
299 #ifndef QT_NO_DEBUG_STREAM
300 QString QPaintBuffer::commandDescription(int command) const
301 {
302     QString desc;
303     QDebug debug(&desc);
304
305     const QPaintBufferCommand &cmd = d_ptr->commands.at(command);
306
307     switch (cmd.id) {
308     case QPaintBufferPrivate::Cmd_Save: {
309         debug << "Cmd_Save";
310         break; }
311
312     case QPaintBufferPrivate::Cmd_Restore: {
313         debug << "Cmd_Restore";
314         break; }
315
316     case QPaintBufferPrivate::Cmd_SetBrush: {
317         QBrush brush = qvariant_cast<QBrush>(d_ptr->variants.at(cmd.offset));
318         debug << "Cmd_SetBrush: " << brush;
319         break; }
320
321     case QPaintBufferPrivate::Cmd_SetBrushOrigin: {
322         debug << "Cmd_SetBrushOrigin: " << d_ptr->variants.at(cmd.offset).toPointF();
323         break; }
324
325     case QPaintBufferPrivate::Cmd_SetCompositionMode: {
326         QPainter::CompositionMode mode = (QPainter::CompositionMode) cmd.extra;
327         debug << "ExCmd_SetCompositionMode, mode: " << mode;
328         break; }
329
330     case QPaintBufferPrivate::Cmd_SetOpacity: {
331         debug << "ExCmd_SetOpacity: " << d_ptr->variants.at(cmd.offset).toDouble();
332         break; }
333
334     case QPaintBufferPrivate::Cmd_DrawVectorPath: {
335         debug << "ExCmd_DrawVectorPath: size: " << cmd.size
336 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
337                  << "pts/elms:" << cmd.offset << cmd.offset2;
338         break; }
339
340     case QPaintBufferPrivate::Cmd_StrokeVectorPath: {
341         QPen pen = qvariant_cast<QPen>(d_ptr->variants.at(cmd.extra));
342         debug << "ExCmd_StrokeVectorPath: size: " << cmd.size
343 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
344                  << "pts/elms:" << cmd.offset << cmd.offset2 << pen;
345         break; }
346
347     case QPaintBufferPrivate::Cmd_FillVectorPath: {
348         QBrush brush = qvariant_cast<QBrush>(d_ptr->variants.at(cmd.extra));
349         debug << "ExCmd_FillVectorPath: size: " << cmd.size
350 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
351                  << "pts/elms:" << cmd.offset << cmd.offset2 << brush;
352         break; }
353
354     case QPaintBufferPrivate::Cmd_FillRectBrush: {
355         QBrush brush = qvariant_cast<QBrush>(d_ptr->variants.at(cmd.extra));
356         QRectF *rect = (QRectF *)(d_ptr->floats.constData() + cmd.offset);
357         debug << "ExCmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " brush: " << brush;
358         break; }
359
360     case QPaintBufferPrivate::Cmd_FillRectColor: {
361         QColor color = qvariant_cast<QColor>(d_ptr->variants.at(cmd.extra));
362         QRectF *rect = (QRectF *)(d_ptr->floats.constData() + cmd.offset);
363         debug << "ExCmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " color: " << color;
364         break; }
365
366     case QPaintBufferPrivate::Cmd_DrawPolygonF: {
367         debug << "ExCmd_DrawPolygonF, offset: " << cmd.offset << " size: " << cmd.size
368                  << " mode: " << cmd.extra
369                  << d_ptr->floats.at(cmd.offset)
370                  << d_ptr->floats.at(cmd.offset+1);
371         break; }
372
373     case QPaintBufferPrivate::Cmd_DrawPolygonI: {
374         debug << "ExCmd_DrawPolygonI, offset: " << cmd.offset << " size: " << cmd.size
375                  << " mode: " << cmd.extra
376                  << d_ptr->ints.at(cmd.offset)
377                  << d_ptr->ints.at(cmd.offset+1);
378         break; }
379
380     case QPaintBufferPrivate::Cmd_DrawEllipseF: {
381         debug << "ExCmd_DrawEllipseF, offset: " << cmd.offset;
382         break; }
383
384     case QPaintBufferPrivate::Cmd_DrawLineF: {
385         debug << "ExCmd_DrawLineF, offset: " << cmd.offset << " size: " << cmd.size;
386         break; }
387
388     case QPaintBufferPrivate::Cmd_DrawLineI: {
389         debug << "ExCmd_DrawLineI, offset: " << cmd.offset << " size: " << cmd.size;
390         break; }
391
392     case QPaintBufferPrivate::Cmd_DrawPointsF: {
393         debug << "ExCmd_DrawPointsF, offset: " << cmd.offset << " size: " << cmd.size;
394         break; }
395
396     case QPaintBufferPrivate::Cmd_DrawPointsI: {
397         debug << "ExCmd_DrawPointsI, offset: " << cmd.offset << " size: " << cmd.size;
398         break; }
399
400     case QPaintBufferPrivate::Cmd_DrawPolylineF: {
401         debug << "ExCmd_DrawPolylineF, offset: " << cmd.offset << " size: " << cmd.size;
402         break; }
403
404     case QPaintBufferPrivate::Cmd_DrawPolylineI: {
405         debug << "ExCmd_DrawPolylineI, offset: " << cmd.offset << " size: " << cmd.size;
406         break; }
407
408     case QPaintBufferPrivate::Cmd_DrawRectF: {
409         debug << "ExCmd_DrawRectF, offset: " << cmd.offset << " size: " << cmd.size;
410         break; }
411
412     case QPaintBufferPrivate::Cmd_DrawRectI: {
413         debug << "ExCmd_DrawRectI, offset: " << cmd.offset << " size: " << cmd.size;
414         break; }
415
416     case QPaintBufferPrivate::Cmd_SetClipEnabled: {
417         bool clipEnabled = d_ptr->variants.at(cmd.offset).toBool();
418         debug << "ExCmd_SetClipEnabled:" << clipEnabled;
419         break; }
420
421     case QPaintBufferPrivate::Cmd_ClipVectorPath: {
422         QVectorPathCmd path(d_ptr, cmd);
423         debug << "ExCmd_ClipVectorPath:" << path().elementCount();
424         break; }
425
426     case QPaintBufferPrivate::Cmd_ClipRect: {
427         QRect rect(QPoint(d_ptr->ints.at(cmd.offset), d_ptr->ints.at(cmd.offset + 1)),
428                    QPoint(d_ptr->ints.at(cmd.offset + 2), d_ptr->ints.at(cmd.offset + 3)));
429         debug << "ExCmd_ClipRect:" << rect << cmd.extra;
430         break; }
431
432     case QPaintBufferPrivate::Cmd_ClipRegion: {
433         QRegion region(d_ptr->variants.at(cmd.offset).value<QRegion>());
434         debug << "ExCmd_ClipRegion:" << region.boundingRect() << cmd.extra;
435         break; }
436
437     case QPaintBufferPrivate::Cmd_SetPen: {
438         QPen pen = qvariant_cast<QPen>(d_ptr->variants.at(cmd.offset));
439         debug << "Cmd_SetPen: " << pen;
440         break; }
441
442     case QPaintBufferPrivate::Cmd_SetTransform: {
443         QTransform xform = qvariant_cast<QTransform>(d_ptr->variants.at(cmd.offset));
444         debug << "Cmd_SetTransform, offset: " << cmd.offset << xform;
445         break; }
446
447     case QPaintBufferPrivate::Cmd_SetRenderHints: {
448         debug << "Cmd_SetRenderHints, hints: " << cmd.extra;
449         break; }
450
451     case QPaintBufferPrivate::Cmd_SetBackgroundMode: {
452         debug << "Cmd_SetBackgroundMode: " << cmd.extra;
453         break; }
454
455     case QPaintBufferPrivate::Cmd_DrawConvexPolygonF: {
456         debug << "Cmd_DrawConvexPolygonF, offset: " << cmd.offset << " size: " << cmd.size;
457         break; }
458
459     case QPaintBufferPrivate::Cmd_DrawConvexPolygonI: {
460         debug << "Cmd_DrawConvexPolygonI, offset: " << cmd.offset << " size: " << cmd.size;
461         break; }
462
463     case QPaintBufferPrivate::Cmd_DrawEllipseI: {
464         debug << "Cmd_DrawEllipseI, offset: " << cmd.offset;
465         break; }
466
467     case QPaintBufferPrivate::Cmd_DrawPixmapRect: {
468         QPixmap pm(d_ptr->variants.at(cmd.offset).value<QPixmap>());
469         QRectF r(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1),
470                  d_ptr->floats.at(cmd.extra+2), d_ptr->floats.at(cmd.extra+3));
471
472         QRectF sr(d_ptr->floats.at(cmd.extra+4), d_ptr->floats.at(cmd.extra+5),
473                   d_ptr->floats.at(cmd.extra+6), d_ptr->floats.at(cmd.extra+7));
474         debug << "Cmd_DrawPixmapRect:" << r << sr << pm.size();
475         break; }
476
477     case QPaintBufferPrivate::Cmd_DrawPixmapPos: {
478         QPixmap pm(d_ptr->variants.at(cmd.offset).value<QPixmap>());
479         QPointF pos(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1));
480         debug << "Cmd_DrawPixmapPos:" << pos << pm.size();
481         break; }
482
483     case QPaintBufferPrivate::Cmd_DrawTiledPixmap: {
484         QPixmap pm(d_ptr->variants.at(cmd.offset).value<QPixmap>());
485         QRectF r(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1),
486                  d_ptr->floats.at(cmd.extra+2), d_ptr->floats.at(cmd.extra+3));
487
488         QPointF offset(d_ptr->floats.at(cmd.extra+4), d_ptr->floats.at(cmd.extra+5));
489         debug << "Cmd_DrawTiledPixmap:" << r << offset << pm.size();
490         break; }
491
492     case QPaintBufferPrivate::Cmd_DrawImageRect: {
493         QImage image(d_ptr->variants.at(cmd.offset).value<QImage>());
494         QRectF r(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1),
495                  d_ptr->floats.at(cmd.extra+2), d_ptr->floats.at(cmd.extra+3));
496         QRectF sr(d_ptr->floats.at(cmd.extra+4), d_ptr->floats.at(cmd.extra+5),
497                   d_ptr->floats.at(cmd.extra+6), d_ptr->floats.at(cmd.extra+7));
498         debug << "Cmd_DrawImageRect:" << r << sr << image.size();
499         break; }
500
501     case QPaintBufferPrivate::Cmd_DrawImagePos: {
502         QImage image(d_ptr->variants.at(cmd.offset).value<QImage>());
503         QPointF pos(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1));
504         debug << "Cmd_DrawImagePos:" << pos << image.size();
505         break; }
506
507     case QPaintBufferPrivate::Cmd_DrawText: {
508         QPointF pos(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1));
509         QList<QVariant> variants(d_ptr->variants.at(cmd.offset).value<QList<QVariant> >());
510
511         QFont font(variants.at(0).value<QFont>());
512         QString text(variants.at(1).value<QString>());
513
514         debug << "Cmd_DrawText:" << pos << text << font.family();
515         break; }
516
517     case QPaintBufferPrivate::Cmd_DrawTextItem: {
518         QPointF pos(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1));
519         QTextItemIntCopy *tiCopy = reinterpret_cast<QTextItemIntCopy *>(qvariant_cast<void *>(d_ptr->variants.at(cmd.offset)));
520         QTextItemInt &ti = (*tiCopy)();
521         QString text(ti.text());
522
523         debug << "Cmd_DrawTextItem:" << pos << " " << text;
524         break; }
525     case QPaintBufferPrivate::Cmd_SystemStateChanged: {
526         QRegion systemClip(d_ptr->variants.at(cmd.offset).value<QRegion>());
527
528         debug << "Cmd_SystemStateChanged:" << systemClip;
529         break; }
530     case QPaintBufferPrivate::Cmd_Translate: {
531         QPointF delta(d_ptr->floats.at(cmd.extra), d_ptr->floats.at(cmd.extra+1));
532         debug << "Cmd_Translate:" << delta;
533         break; }
534     case QPaintBufferPrivate::Cmd_DrawStaticText: {
535         debug << "Cmd_DrawStaticText";
536         break; }
537     }
538
539     return desc;
540 }
541 #endif
542
543 QRectF QPaintBuffer::boundingRect() const
544 {
545     return d_ptr->boundingRect;
546 }
547
548 void QPaintBuffer::setBoundingRect(const QRectF &rect)
549 {
550     d_ptr->boundingRect = rect;
551     d_ptr->calculateBoundingRect = false;
552 }
553
554
555 class QPaintBufferEnginePrivate : public QPaintEngineExPrivate
556 {
557     Q_DECLARE_PUBLIC(QPaintBufferEngine)
558 public:
559     void systemStateChanged() {
560         Q_Q(QPaintBufferEngine);
561         q->buffer->addCommand(QPaintBufferPrivate::Cmd_SystemStateChanged, QVariant(systemClip));
562     }
563
564     QTransform last;
565 };
566
567
568 /************************************************************************
569  *
570  * QPaintBufferEngine
571  *
572  ************************************************************************/
573
574 QPaintBufferEngine::QPaintBufferEngine(QPaintBufferPrivate *b)
575     : QPaintEngineEx(*(new QPaintBufferEnginePrivate))
576     , buffer(b)
577     , m_begin_detected(false)
578     , m_save_detected(false)
579     , m_stream_raw_text_items(false)
580 {
581 }
582
583 bool QPaintBufferEngine::begin(QPaintDevice *)
584 {
585     Q_D(QPaintBufferEngine);
586     painter()->save();
587     d->systemStateChanged();
588     return true;
589 }
590
591 bool QPaintBufferEngine::end()
592 {
593     painter()->restore();
594     m_created_state = 0;
595     return true;
596 }
597
598 QPainterState *QPaintBufferEngine::createState(QPainterState *orig) const
599 {
600 #ifdef QPAINTBUFFER_DEBUG_DRAW
601     qDebug() << "QPaintBufferEngine: createState, orig=" << orig << ", current=" << state();
602 #endif
603
604     Q_ASSERT(!m_begin_detected);
605     Q_ASSERT(!m_save_detected);
606
607     if (orig == 0) {
608         m_begin_detected = true;
609         return new QPainterState();
610     } else {
611         m_save_detected = true;
612         return new QPainterState(orig);
613     }
614 }
615
616 void QPaintBufferEngine::clip(const QVectorPath &path, Qt::ClipOperation op)
617 {
618 #ifdef QPAINTBUFFER_DEBUG_DRAW
619     qDebug() << "QPaintBufferEngine: clip vpath:" << path.elementCount() << "op:" << op;
620 #endif
621     QPaintBufferCommand *cmd =
622         buffer->addCommand(QPaintBufferPrivate::Cmd_ClipVectorPath, path);
623     cmd->extra = op;
624 }
625
626 void QPaintBufferEngine::clip(const QRect &rect, Qt::ClipOperation op)
627 {
628 #ifdef QPAINTBUFFER_DEBUG_DRAW
629     qDebug() << "QPaintBufferEngine: clip rect:" << rect << "op:" << op;
630 #endif
631     QPaintBufferCommand *cmd =
632         buffer->addCommand(QPaintBufferPrivate::Cmd_ClipRect, (int *) &rect, 4, 1);
633     cmd->extra = op;
634 }
635
636 void QPaintBufferEngine::clip(const QRegion &region, Qt::ClipOperation op)
637 {
638 #ifdef QPAINTBUFFER_DEBUG_DRAW
639     qDebug() << "QPaintBufferEngine: clip region br:" << region.boundingRect() << "op:" << op;
640 #endif
641     QPaintBufferCommand *cmd =
642         buffer->addCommand(QPaintBufferPrivate::Cmd_ClipRegion, QVariant(region));
643     cmd->extra = op;
644 }
645
646 void QPaintBufferEngine::clip(const QPainterPath &path, Qt::ClipOperation op)
647 {
648     // ### TODO
649 //     QPaintBufferCommand *cmd =
650 //         buffer->addCommand(QPaintBufferPrivate::Cmd_ClipPath, QVariant(path));
651 //     cmd->extra = op;
652     QPaintEngineEx::clip(path, op);
653 }
654
655 void QPaintBufferEngine::clipEnabledChanged()
656 {
657 #ifdef QPAINTBUFFER_DEBUG_DRAW
658     qDebug() << "QPaintBufferEngine: clip enable change" << state()->clipEnabled;
659 #endif
660
661     buffer->addCommand(QPaintBufferPrivate::Cmd_SetClipEnabled, state()->clipEnabled);
662 }
663
664 void QPaintBufferEngine::penChanged()
665 {
666     const QPen &pen = state()->pen;
667
668     if (!buffer->commands.isEmpty()
669         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetPen) {
670 #ifdef QPAINTBUFFER_DEBUG_DRAW
671     qDebug() << "QPaintBufferEngine: penChanged (compressed)" << state()->pen;
672 #endif
673         buffer->variants[buffer->commands.last().offset] = pen;
674         return;
675     }
676
677     if (buffer->calculateBoundingRect) {
678         if (pen.style() == Qt::NoPen) {
679             buffer->penWidthAdjustment = 0;
680         } else {
681             qreal penWidth = (pen.widthF() == 0) ? 1 : pen.widthF();
682             QPointF transformedWidth(penWidth, penWidth);
683             if (!pen.isCosmetic())
684                 transformedWidth = painter()->transform().map(transformedWidth);
685             buffer->penWidthAdjustment = transformedWidth.x() / 2.0;
686         }
687     }
688 #ifdef QPAINTBUFFER_DEBUG_DRAW
689     qDebug() << "QPaintBufferEngine: penChanged" << state()->pen;
690 #endif
691     buffer->addCommand(QPaintBufferPrivate::Cmd_SetPen, pen);
692 }
693
694 void QPaintBufferEngine::brushChanged()
695 {
696     const QBrush &brush = state()->brush;
697
698     if (!buffer->commands.isEmpty()
699         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetBrush) {
700 #ifdef QPAINTBUFFER_DEBUG_DRAW
701         qDebug() << "QPaintBufferEngine: brushChanged (compressed)" << state()->brush;
702 #endif
703         buffer->variants[buffer->commands.last().offset] = brush;
704         return;
705     }
706
707 #ifdef QPAINTBUFFER_DEBUG_DRAW
708     qDebug() << "QPaintBufferEngine: brushChanged" << state()->brush;
709 #endif
710     buffer->addCommand(QPaintBufferPrivate::Cmd_SetBrush, brush);
711 }
712
713 void QPaintBufferEngine::brushOriginChanged()
714 {
715 #ifdef QPAINTBUFFER_DEBUG_DRAW
716     qDebug() << "QPaintBufferEngine: brush origin changed" << state()->brushOrigin;
717 #endif
718     buffer->addCommand(QPaintBufferPrivate::Cmd_SetBrushOrigin, state()->brushOrigin);
719 }
720
721 void QPaintBufferEngine::opacityChanged()
722 {
723 #ifdef QPAINTBUFFER_DEBUG_DRAW
724     qDebug() << "QPaintBufferEngine: opacity changed" << state()->opacity;
725 #endif
726     buffer->addCommand(QPaintBufferPrivate::Cmd_SetOpacity, state()->opacity);
727 }
728
729 void QPaintBufferEngine::compositionModeChanged()
730 {
731 #ifdef QPAINTBUFFER_DEBUG_DRAW
732     qDebug() << "QPaintBufferEngine: composition mode" << state()->composition_mode;
733 #endif
734     QPaintBufferCommand *cmd =
735         buffer->addCommand(QPaintBufferPrivate::Cmd_SetCompositionMode);
736     cmd->extra = state()->composition_mode;
737 }
738
739 void QPaintBufferEngine::renderHintsChanged()
740 {
741 #ifdef QPAINTBUFFER_DEBUG_DRAW
742     qDebug() << "QPaintBufferEngine: render hints changed" << state()->renderHints;
743 #endif
744     QPaintBufferCommand *cmd =
745         buffer->addCommand(QPaintBufferPrivate::Cmd_SetRenderHints);
746     cmd->extra = state()->renderHints;
747 }
748
749 void QPaintBufferEngine::transformChanged()
750 {
751     Q_D(QPaintBufferEngine);
752     const QTransform &transform = state()->matrix;
753
754     QTransform delta;
755
756     bool invertible = false;
757     if (transform.type() <= QTransform::TxScale && transform.type() == d->last.type())
758         delta = transform * d->last.inverted(&invertible);
759
760     d->last = transform;
761
762     if (invertible && delta.type() == QTransform::TxNone)
763         return;
764
765     if (invertible && delta.type() == QTransform::TxTranslate) {
766 #ifdef QPAINTBUFFER_DEBUG_DRAW
767         qDebug() << "QPaintBufferEngine: transformChanged (translate only) " << state()->matrix;
768 #endif
769         QPaintBufferCommand *cmd =
770             buffer->addCommand(QPaintBufferPrivate::Cmd_Translate);
771
772         qreal data[] = { delta.dx(), delta.dy() };
773         cmd->extra = buffer->addData((qreal *) data, 2);
774         return;
775     }
776
777     // ### accumulate, like in QBrush case...
778     if (!buffer->commands.isEmpty()
779         && buffer->commands.last().id == QPaintBufferPrivate::Cmd_SetTransform) {
780 #ifdef QPAINTBUFFER_DEBUG_DRAW
781         qDebug() << "QPaintBufferEngine: transformChanged (compressing) " << state()->matrix;
782 #endif
783         buffer->variants[buffer->commands.last().offset] = state()->matrix;
784         return;
785     }
786
787 #ifdef QPAINTBUFFER_DEBUG_DRAW
788         qDebug() << "QPaintBufferEngine: transformChanged:" << state()->matrix;
789 #endif
790     buffer->addCommand(QPaintBufferPrivate::Cmd_SetTransform, state()->matrix);
791 }
792
793 void QPaintBufferEngine::backgroundModeChanged()
794 {
795 #ifdef QPAINTBUFFER_DEBUG_DRAW
796     qDebug() << "QPaintEngineBuffer: background mode changed" << state()->bgMode;
797 #endif
798     QPaintBufferCommand *cmd = buffer->addCommand(QPaintBufferPrivate::Cmd_SetBackgroundMode);
799     cmd->extra = state()->bgMode;
800 }
801
802 void QPaintBufferEngine::draw(const QVectorPath &path)
803 {
804 #ifdef QPAINTBUFFER_DEBUG_DRAW
805     qDebug() << "QPaintBufferEngine: draw vpath:" << path.elementCount();
806 #endif
807
808     bool hasBrush = qbrush_style(state()->brush) != Qt::NoBrush;
809     bool hasPen = qpen_style(state()->pen) != Qt::NoPen
810                   && qbrush_style(qpen_brush(state()->pen)) != Qt::NoBrush;
811
812     if (hasPen || hasBrush)
813         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawVectorPath, path);
814 #ifdef QPAINTBUFFER_DEBUG_DRAW
815     else
816         qDebug() << " - no pen or brush active, discarded...\n";
817 #endif
818
819 //     if (buffer->calculateBoundingRect) {
820 //         QRealRect r = path.controlPointRect();
821 //         buffer->updateBoundingRect(QRectF(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1));
822 //     }
823 }
824
825 void QPaintBufferEngine::fill(const QVectorPath &path, const QBrush &brush)
826 {
827 #ifdef QPAINTBUFFER_DEBUG_DRAW
828     qDebug() << "QPaintBufferEngine: fill vpath:" << path.elementCount() << brush;
829 #endif
830     QPaintBufferCommand *cmd =
831         buffer->addCommand(QPaintBufferPrivate::Cmd_FillVectorPath, path);
832     cmd->extra = buffer->addData(QVariant(brush));
833 //     if (buffer->calculateBoundingRect) {
834 //         QRealRect r = path.controlPointRect();
835 //         buffer->updateBoundingRect(QRectF(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1));
836 //     }
837 }
838
839 void QPaintBufferEngine::stroke(const QVectorPath &path, const QPen &pen)
840 {
841 #ifdef QPAINTBUFFER_DEBUG_DRAW
842     qDebug() << "QPaintBufferEngine: stroke vpath:" << path.elementCount() << pen;
843 #endif
844     QPaintBufferCommand *cmd =
845         buffer->addCommand(QPaintBufferPrivate::Cmd_StrokeVectorPath, path);
846     cmd->extra = buffer->addData(QVariant(pen));
847 //     if (buffer->calculateBoundingRect) {
848 //         QRealRect r = path.controlPointRect();
849 //         buffer->updateBoundingRect(QRectF(r.x1, r.y1, r.x2 - r.x1, r.y2 - r.y1));
850 //     }
851 }
852
853 void QPaintBufferEngine::fillRect(const QRectF &rect, const QBrush &brush)
854 {
855 #ifdef QPAINTBUFFER_DEBUG_DRAW
856     qDebug() << "QPaintBufferEngine: fillRect brush:" << rect << brush;
857 #endif
858     QPaintBufferCommand *cmd =
859         buffer->addCommand(QPaintBufferPrivate::Cmd_FillRectBrush, (qreal *) &rect, 4, 1);
860     cmd->extra = buffer->addData(brush);
861     if (buffer->calculateBoundingRect)
862         buffer->updateBoundingRect(rect);
863 }
864
865 void QPaintBufferEngine::fillRect(const QRectF &rect, const QColor &color)
866 {
867 #ifdef QPAINTBUFFER_DEBUG_DRAW
868     qDebug() << "QPaintBufferEngine: fillRect color:" << rect << color;
869 #endif
870     QPaintBufferCommand *cmd =
871         buffer->addCommand(QPaintBufferPrivate::Cmd_FillRectColor, (qreal *) &rect, 4, 1);
872     cmd->extra = buffer->addData(color);
873     if (buffer->calculateBoundingRect)
874         buffer->updateBoundingRect(rect);
875 }
876
877 void QPaintBufferEngine::drawRects(const QRect *rects, int rectCount)
878 {
879 #ifdef QPAINTBUFFER_DEBUG_DRAW
880     qDebug() << "QPaintBufferEngine: drawRectsI:" << rectCount;
881 #endif
882     QPaintBufferCommand *cmd =
883         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawRectI, (int *) rects, 4 * rectCount, rectCount);
884     cmd->extra = rectCount;
885
886     if (buffer->calculateBoundingRect) {
887         if (rectCount == 1) {
888             buffer->updateBoundingRect(rects[0]);
889         } else {
890             int min_x = rects[0].left();
891             int min_y = rects[0].top();
892             int max_x = rects[0].left() + rects[0].width();
893             int max_y = rects[0].top() + rects[0].height();
894             for (int i=1; i< rectCount; ++i) {
895                 if (rects[i].left() < min_x)
896                     min_x = rects[i].left();
897                 if (rects[i].top() < min_y)
898                     min_y = rects[i].top();
899                 if (rects[i].right() > max_x)
900                     max_x = rects[i].left() + rects[i].width();
901                 if (rects[i].bottom() > max_y)
902                     max_y = rects[i].top() + rects[i].height();
903
904             }
905             buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
906         }
907     }
908 }
909
910 void QPaintBufferEngine::drawRects(const QRectF *rects, int rectCount)
911 {
912 #ifdef QPAINTBUFFER_DEBUG_DRAW
913     qDebug() << "QPaintBufferEngine: drawRectsF:" << rectCount;
914 #endif
915     QPaintBufferCommand *cmd =
916         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawRectF, (qreal *) rects, 4 * rectCount, rectCount);
917     cmd->extra = rectCount;
918
919     if (buffer->calculateBoundingRect) {
920         if (rectCount == 1) {
921             buffer->updateBoundingRect(rects[0]);
922         } else {
923             qreal min_x = rects[0].left();
924             qreal min_y = rects[0].top();
925             qreal max_x = rects[0].right();
926             qreal max_y = rects[0].bottom();
927             for (int i=1; i< rectCount; ++i) {
928                 if (rects[i].left() < min_x)
929                     min_x = rects[i].left();
930                 if (rects[i].top() < min_y)
931                     min_y = rects[i].top();
932                 if (rects[i].right() > max_x)
933                     max_x = rects[i].right();
934                 if (rects[i].bottom() > max_y)
935                     max_y = rects[i].bottom();
936
937             }
938             buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
939         }
940     }
941 }
942
943 void QPaintBufferEngine::drawLines(const QLine *lines, int lineCount)
944 {
945 #ifdef QPAINTBUFFER_DEBUG_DRAW
946     qDebug() << "QPaintBufferEngine: drawLinesI:" << lineCount;
947 #endif
948     QPaintBufferCommand *cmd =
949         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawLineI, (int *) lines, 4 * lineCount, lineCount);
950     cmd->extra = lineCount;
951
952     if (buffer->calculateBoundingRect) {
953         int min_x = lines[0].p1().x();
954         int min_y = lines[0].p1().y();
955         int max_x = lines[0].p2().x();
956         int max_y = lines[0].p2().y();
957         if (min_x > max_x)
958             qSwap(min_x, max_x);
959         if (min_y > max_y)
960             qSwap(min_y, max_y);
961         for (int i=1; i < lineCount; ++i) {
962             int p1_x = lines[i].p1().x();
963             int p1_y = lines[i].p1().y();
964             int p2_x = lines[i].p2().x();
965             int p2_y = lines[i].p2().y();
966             if (p1_x > p2_x) {
967                 min_x = qMin(p2_x, min_x);
968                 max_x = qMax(p1_x, max_x);
969             } else {
970                 min_x = qMin(p1_x, min_x);
971                 max_x = qMax(p2_x, max_x);
972             }
973             if (p1_y > p2_y) {
974                 min_y = qMin(p2_y, min_y);
975                 max_y = qMax(p1_y, max_y);
976             } else {
977                 min_y = qMin(p1_y, min_y);
978                 max_y = qMax(p2_y, max_y);
979             }
980         }
981         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
982     }
983 }
984
985 void QPaintBufferEngine::drawLines(const QLineF *lines, int lineCount)
986 {
987 #ifdef QPAINTBUFFER_DEBUG_DRAW
988     qDebug() << "QPaintBufferEngine: drawLinesF:" << lineCount;
989 #endif
990     QPaintBufferCommand *cmd =
991         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawLineF, (qreal *) lines, 4 * lineCount, lineCount);
992     cmd->extra = lineCount;
993
994     if (buffer->calculateBoundingRect) {
995         qreal min_x = lines[0].p1().x();
996         qreal min_y = lines[0].p1().y();
997         qreal max_x = lines[0].p2().x();
998         qreal max_y = lines[0].p2().y();
999         if (min_x > max_x)
1000             qSwap(min_x, max_x);
1001         if (min_y > max_y)
1002             qSwap(min_y, max_y);
1003         for (int i=1; i < lineCount; ++i) {
1004             qreal p1_x = lines[i].p1().x();
1005             qreal p1_y = lines[i].p1().y();
1006             qreal p2_x = lines[i].p2().x();
1007             qreal p2_y = lines[i].p2().y();
1008             if (p1_x > p2_x) {
1009                 min_x = qMin(p2_x, min_x);
1010                 max_x = qMax(p1_x, max_x);
1011             } else {
1012                 min_x = qMin(p1_x, min_x);
1013                 max_x = qMax(p2_x, max_x);
1014             }
1015             if (p1_y > p2_y) {
1016                 min_y = qMin(p2_y, min_y);
1017                 max_y = qMax(p1_y, max_y);
1018             } else {
1019                 min_y = qMin(p1_y, min_y);
1020                 max_y = qMax(p2_y, max_y);
1021             }
1022         }
1023         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
1024     }
1025 }
1026
1027 void QPaintBufferEngine::drawEllipse(const QRectF &r)
1028 {
1029 #ifdef QPAINTBUFFER_DEBUG_DRAW
1030     qDebug() << "QPaintBufferEngine: drawEllipseF:" << r;
1031 #endif
1032     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawEllipseF, (qreal *) &r, 4, 1);
1033     if (buffer->calculateBoundingRect)
1034         buffer->updateBoundingRect(r);
1035 }
1036
1037 void QPaintBufferEngine::drawEllipse(const QRect &r)
1038 {
1039 #ifdef QPAINTBUFFER_DEBUG_DRAW
1040     qDebug() << "QPaintBufferEngine: drawEllipseI:" << r;
1041 #endif
1042     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawEllipseI, (int *) &r, 4, 1);
1043     if (buffer->calculateBoundingRect)
1044         buffer->updateBoundingRect(r);
1045 }
1046
1047 void QPaintBufferEngine::drawPath(const QPainterPath &path)
1048 {
1049 // #ifdef QPAINTBUFFER_DEBUG_DRAW
1050 //     qDebug() << "QPaintBufferEngine: drawPath: element count:" << path.elementCount();
1051 // #endif
1052 //     // ### Path -> QVariant
1053 //     // buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPath, QVariant(path));
1054     QPaintEngineEx::drawPath(path);
1055
1056 //     if (buffer->calculateBoundingRect)
1057 //         buffer->updateBoundingRect(path.boundingRect());
1058 }
1059
1060 void QPaintBufferEngine::drawPoints(const QPoint *points, int pointCount)
1061 {
1062 #ifdef QPAINTBUFFER_DEBUG_DRAW
1063     qDebug() << "QPaintBufferEngine: drawPointsI: " << pointCount;
1064 #endif
1065     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPointsI, (int *) points, 2 * pointCount, pointCount);
1066
1067     if (buffer->calculateBoundingRect) {
1068         int min_x = points[0].x();
1069         int min_y = points[0].y();
1070         int max_x = points[0].x()+1;
1071         int max_y = points[0].y()+1;
1072         for (int i=1; i<pointCount; ++i) {
1073             int x = points[i].x();
1074             int y = points[i].y();
1075             min_x = qMin(min_x, x);
1076             min_y = qMin(min_y, y);
1077             max_x = qMax(max_x, x+1);
1078             max_y = qMax(max_y, y+1);
1079         }
1080         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
1081     }
1082 }
1083
1084 void QPaintBufferEngine::drawPoints(const QPointF *points, int pointCount)
1085 {
1086 #ifdef QPAINTBUFFER_DEBUG_DRAW
1087     qDebug() << "QPaintBufferEngine: drawPointsF: " << pointCount;
1088 #endif
1089     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPointsF, (qreal *) points, 2 * pointCount, pointCount);
1090
1091     if (buffer->calculateBoundingRect) {
1092         qreal min_x = points[0].x();
1093         qreal min_y = points[0].y();
1094         qreal max_x = points[0].x()+1;
1095         qreal max_y = points[0].y()+1;
1096         for (int i=1; i<pointCount; ++i) {
1097             qreal x = points[i].x();
1098             qreal y = points[i].y();
1099             min_x = qMin(min_x, x);
1100             min_y = qMin(min_y, y);
1101             max_x = qMax(max_x, x+1);
1102             max_y = qMax(max_y, y+1);
1103         }
1104         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
1105     }
1106 }
1107
1108 void QPaintBufferEngine::drawPolygon(const QPoint *pts, int count, PolygonDrawMode mode)
1109 {
1110 #ifdef QPAINTBUFFER_DEBUG_DRAW
1111     qDebug() << "QPaintBufferEngine: drawPolygonI: size:" << count << ", mode:" << mode;
1112 #endif
1113     if (mode == QPaintEngine::OddEvenMode || mode == QPaintEngine::WindingMode) {
1114         QPaintBufferCommand *cmd = buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPolygonI,
1115                                                       (int *) pts, 2 * count, count);
1116         cmd->extra = mode;
1117     } else if (mode == QPaintEngine::PolylineMode) {
1118         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPolylineI, (int *) pts, 2 * count, count);
1119     } else {
1120         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawConvexPolygonI, (int *) pts, 2 * count, count);
1121     }
1122
1123     if (buffer->calculateBoundingRect) {
1124         int min_x = pts[0].x();
1125         int min_y = pts[0].y();
1126         int max_x = pts[0].x();
1127         int max_y = pts[0].y();
1128         for (int i=1; i<count; ++i) {
1129             int x = pts[i].x();
1130             int y = pts[i].y();
1131             min_x = qMin(min_x, x);
1132             min_y = qMin(min_y, y);
1133             max_x = qMax(max_x, x);
1134             max_y = qMax(max_y, y);
1135         }
1136         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
1137     }
1138 }
1139
1140 void QPaintBufferEngine::drawPolygon(const QPointF *pts, int count, PolygonDrawMode mode)
1141 {
1142 #ifdef QPAINTBUFFER_DEBUG_DRAW
1143     qDebug() << "QPaintBufferEngine: drawPolygonF: size:" << count << ", mode:" << mode;
1144 #endif
1145     if (mode == QPaintEngine::OddEvenMode || mode == QPaintEngine::WindingMode) {
1146         QPaintBufferCommand *cmd = buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPolygonF,
1147                                                       (qreal *) pts, 2 * count, count);
1148         cmd->extra = mode;
1149     } else if (mode == QPaintEngine::PolylineMode) {
1150         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPolylineF, (qreal *) pts, 2 * count, count);
1151     } else {
1152         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawConvexPolygonF, (qreal *) pts, 2 * count, count);
1153     }
1154
1155     if (buffer->calculateBoundingRect) {
1156         qreal min_x = pts[0].x();
1157         qreal min_y = pts[0].y();
1158         qreal max_x = pts[0].x();
1159         qreal max_y = pts[0].y();
1160         for (int i=1; i<count; ++i) {
1161             qreal x = pts[i].x();
1162             qreal y = pts[i].y();
1163             min_x = qMin(min_x, x);
1164             min_y = qMin(min_y, y);
1165             max_x = qMax(max_x, x);
1166             max_y = qMax(max_y, y);
1167         }
1168         buffer->updateBoundingRect(QRectF(min_x, min_y, max_x - min_x, max_y - min_y));
1169     }
1170 }
1171
1172 void QPaintBufferEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
1173 {
1174 #ifdef QPAINTBUFFER_DEBUG_DRAW
1175     qDebug() << "QPaintBufferEngine: drawPixmap: src/dest rects " << r << sr;
1176 #endif
1177     QPaintBufferCommand *cmd =
1178         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPixmapRect, QVariant(pm));
1179     cmd->extra = buffer->addData((qreal *) &r, 4);
1180     buffer->addData((qreal *) &sr, 4);
1181     if (buffer->calculateBoundingRect)
1182         buffer->updateBoundingRect(r);
1183 }
1184
1185 void QPaintBufferEngine::drawPixmap(const QPointF &pos, const QPixmap &pm)
1186 {
1187 #ifdef QPAINTBUFFER_DEBUG_DRAW
1188     qDebug() << "QPaintBufferEngine: drawPixmap: pos:" << pos;
1189 #endif
1190     QPaintBufferCommand *cmd =
1191         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawPixmapPos, QVariant(pm));
1192     cmd->extra = buffer->addData((qreal *) &pos, 2);
1193     if (buffer->calculateBoundingRect)
1194         buffer->updateBoundingRect(QRectF(pos, pm.size()));
1195 }
1196
1197 static inline QImage qpaintbuffer_storable_image(const QImage &src)
1198 {
1199     QImageData *d = const_cast<QImage &>(src).data_ptr();
1200     return d->own_data ? src : src.copy();
1201 }
1202
1203 void QPaintBufferEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr,
1204                                    Qt::ImageConversionFlags /*flags */)
1205 {
1206 #ifdef QPAINTBUFFER_DEBUG_DRAW
1207     qDebug() << "QPaintBufferEngine: drawImage: src/dest rects " << r << sr;
1208 #endif
1209     QPaintBufferCommand *cmd =
1210         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawImageRect,
1211                            QVariant(qpaintbuffer_storable_image(image)));
1212     cmd->extra = buffer->addData((qreal *) &r, 4);
1213     buffer->addData((qreal *) &sr, 4);
1214     // ### flags...
1215     if (buffer->calculateBoundingRect)
1216         buffer->updateBoundingRect(r);
1217 }
1218
1219 void QPaintBufferEngine::drawImage(const QPointF &pos, const QImage &image)
1220 {
1221 #ifdef QPAINTBUFFER_DEBUG_DRAW
1222     qDebug() << "QPaintBufferEngine: drawImage: pos:" << pos;
1223 #endif
1224     QPaintBufferCommand *cmd =
1225         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawImagePos,
1226                            QVariant(qpaintbuffer_storable_image(image)));
1227     cmd->extra = buffer->addData((qreal *) &pos, 2);
1228     if (buffer->calculateBoundingRect)
1229         buffer->updateBoundingRect(QRectF(pos, image.size()));
1230 }
1231
1232 void QPaintBufferEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pm, const QPointF &s)
1233 {
1234 #ifdef QPAINTBUFFER_DEBUG_DRAW
1235     qDebug() << "QPaintBufferEngine: drawTiledPixmap: src rect/offset:" << r << s;
1236 #endif
1237     QPaintBufferCommand *cmd =
1238         buffer->addCommand(QPaintBufferPrivate::Cmd_DrawTiledPixmap, QVariant(pm));
1239     cmd->extra = buffer->addData((qreal *) &r, 4);
1240     buffer->addData((qreal *) &s, 2);
1241     if (buffer->calculateBoundingRect)
1242         buffer->updateBoundingRect(r);
1243 }
1244
1245 void QPaintBufferEngine::drawStaticTextItem(QStaticTextItem *staticTextItem)
1246 {
1247     QVariantList variants;
1248
1249     variants << QVariant(staticTextItem->font);
1250     for (int i=0; i<staticTextItem->numGlyphs; ++i) {
1251         variants.append(staticTextItem->glyphs[i]);
1252         variants.append(staticTextItem->glyphPositions[i].toPointF());
1253     }
1254
1255     buffer->addCommand(QPaintBufferPrivate::Cmd_DrawStaticText, QVariant(variants));
1256 }
1257
1258 void QPaintBufferEngine::drawTextItem(const QPointF &pos, const QTextItem &ti)
1259 {
1260 #ifdef QPAINTBUFFER_DEBUG_DRAW
1261     qDebug() << "QPaintBufferEngine: drawTextItem: pos:" << pos << ti.text();
1262 #endif
1263     if (m_stream_raw_text_items) {
1264         QPaintBufferCommand *cmd = buffer->addCommand(QPaintBufferPrivate::Cmd_DrawTextItem, QVariant::fromValue<void *>(new QTextItemIntCopy(ti)));
1265
1266         QFont font(ti.font());
1267         font.setUnderline(false);
1268         font.setStrikeOut(false);
1269         font.setOverline(false);
1270
1271         const QTextItemInt &si = static_cast<const QTextItemInt &>(ti);
1272         qreal justificationWidth = 0;
1273         if (si.justified)
1274             justificationWidth = si.width.toReal();
1275         int renderFlags = ti.renderFlags();
1276         qreal scaleFactor = font.d->dpi/qreal(qt_defaultDpiY());
1277
1278         buffer->addData(QVariant(font));
1279         cmd->extra = buffer->addData((qreal *) &pos, 2);
1280         buffer->addData((qreal *) &justificationWidth, 1);
1281         buffer->addData((qreal *) &scaleFactor, 1);
1282         cmd->offset2 = buffer->addData((int *) &renderFlags, 1);
1283     } else {
1284         QList<QVariant> variants;
1285         variants << QVariant(ti.font()) << QVariant(ti.text());
1286         QPaintBufferCommand *cmd = buffer->addCommand(QPaintBufferPrivate::Cmd_DrawText, QVariant(variants));
1287         cmd->extra = buffer->addData((qreal *) &pos, 2);
1288     }
1289
1290     if (buffer->calculateBoundingRect)
1291         buffer->updateBoundingRect(QRectF(pos, QSize(ti.width(), ti.ascent() + ti.descent() + 1)));
1292 }
1293
1294
1295 void QPaintBufferEngine::setState(QPainterState *s)
1296 {
1297     Q_D(QPaintBufferEngine);
1298     if (m_begin_detected) {
1299 #ifdef QPAINTBUFFER_DEBUG_DRAW
1300             qDebug() << "QPaintBufferEngine: setState: begin, ignoring.";
1301 #endif
1302         m_begin_detected = false;
1303     } else if (m_save_detected) {
1304 #ifdef QPAINTBUFFER_DEBUG_DRAW
1305         qDebug() << "QPaintBufferEngine: setState: save.";
1306 #endif
1307         m_save_detected = false;
1308         buffer->addCommand(QPaintBufferPrivate::Cmd_Save);
1309     } else {
1310 #ifdef QPAINTBUFFER_DEBUG_DRAW
1311         qDebug() << "QPaintBufferEngine: setState: restore.";
1312 #endif
1313         buffer->addCommand(QPaintBufferPrivate::Cmd_Restore);
1314     }
1315
1316     d->last = s->matrix;
1317
1318     QPaintEngineEx::setState(s);
1319 }
1320
1321
1322 /***********************************************************************
1323  *
1324  * class QPaintBufferPlayback_Painter
1325  *
1326  */
1327
1328 // QFakeDevice is used to create fonts with a custom DPI
1329 //
1330 class QFakeDevice : public QPaintDevice
1331 {
1332 public:
1333     QFakeDevice() { dpi_x = qt_defaultDpiX(); dpi_y = qt_defaultDpiY(); }
1334     void setDpiX(int dpi) { dpi_x = dpi; }
1335     void setDpiY(int dpi) { dpi_y = dpi; }
1336     QPaintEngine *paintEngine() const { return 0; }
1337     int metric(PaintDeviceMetric m) const
1338     {
1339         switch(m) {
1340             case PdmPhysicalDpiX:
1341             case PdmDpiX:
1342                 return dpi_x;
1343             case PdmPhysicalDpiY:
1344             case PdmDpiY:
1345                 return dpi_y;
1346             default:
1347                 return QPaintDevice::metric(m);
1348         }
1349     }
1350
1351 private:
1352     int dpi_x;
1353     int dpi_y;
1354 };
1355
1356
1357 void QPainterReplayer::setupTransform(QPainter *_painter)
1358 {
1359     painter = _painter;
1360     m_world_matrix = painter->transform();
1361     m_world_matrix.scale(qreal(painter->device()->logicalDpiX()) / qreal(qt_defaultDpiX()),
1362                          qreal(painter->device()->logicalDpiY()) / qreal(qt_defaultDpiY()));
1363     painter->setTransform(m_world_matrix);
1364 }
1365
1366 void QPainterReplayer::processCommands(const QPaintBuffer &buffer, QPainter *p, int begin, int end)
1367 {
1368     d = buffer.d_ptr;
1369     painter = p;
1370
1371     for (int cmdIndex = begin; cmdIndex < end; ++cmdIndex) {
1372         const QPaintBufferCommand &cmd = d->commands.at(cmdIndex);
1373         process(cmd);
1374     }
1375 }
1376
1377 void QPaintBuffer::beginNewFrame()
1378 {
1379     if (!d_ptr->commands.isEmpty())
1380         d_ptr->frames << d_ptr->commands.size();
1381 }
1382
1383 int QPaintBuffer::numFrames() const
1384 {
1385     return d_ptr->frames.size() + 1;
1386 }
1387
1388 void QPainterReplayer::process(const QPaintBufferCommand &cmd)
1389 {
1390     switch (cmd.id) {
1391     case QPaintBufferPrivate::Cmd_Save: {
1392 #ifdef QPAINTBUFFER_DEBUG_DRAW
1393         qDebug() << " -> Cmd_Save";
1394 #endif
1395         painter->save();
1396         break; }
1397
1398     case QPaintBufferPrivate::Cmd_Restore: {
1399 #ifdef QPAINTBUFFER_DEBUG_DRAW
1400         qDebug() << " -> Cmd_Restore";
1401 #endif
1402         painter->restore();
1403         break; }
1404
1405     case QPaintBufferPrivate::Cmd_SetPen: {
1406         QPen pen = qvariant_cast<QPen>(d->variants.at(cmd.offset));
1407 #ifdef QPAINTBUFFER_DEBUG_DRAW
1408         qDebug() << " -> Cmd_SetPen: " << pen;
1409 #endif
1410         painter->setPen(pen);
1411         break; }
1412
1413     case QPaintBufferPrivate::Cmd_SetBrush: {
1414         QBrush brush = qvariant_cast<QBrush>(d->variants.at(cmd.offset));
1415 #ifdef QPAINTBUFFER_DEBUG_DRAW
1416         qDebug() << " -> Cmd_SetBrush: " << brush;
1417 #endif
1418         painter->setBrush(brush);
1419         break; }
1420
1421     case QPaintBufferPrivate::Cmd_SetBrushOrigin: {
1422 #ifdef QPAINTBUFFER_DEBUG_DRAW
1423         qDebug() << " -> Cmd_SetBrushOrigin: " << d->variants.at(cmd.offset).toPointF();
1424 #endif
1425         painter->setBrushOrigin(d->variants.at(cmd.offset).toPointF());
1426         break; }
1427
1428     case QPaintBufferPrivate::Cmd_SetTransform: {
1429         QTransform xform = qvariant_cast<QTransform>(d->variants.at(cmd.offset));
1430 #ifdef QPAINTBUFFER_DEBUG_DRAW
1431         qDebug() << " -> Cmd_SetTransform, offset: " << cmd.offset << xform;
1432 #endif
1433         painter->setTransform(xform * m_world_matrix);
1434         break; }
1435
1436     case QPaintBufferPrivate::Cmd_Translate: {
1437         QPointF delta(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
1438 #ifdef QPAINTBUFFER_DEBUG_DRAW
1439         qDebug() << " -> Cmd_Translate, offset: " << cmd.offset << delta;
1440 #endif
1441         painter->translate(delta.x(), delta.y());
1442         return;
1443     }
1444
1445     case QPaintBufferPrivate::Cmd_SetCompositionMode: {
1446         QPainter::CompositionMode mode = (QPainter::CompositionMode) cmd.extra;
1447 #ifdef QPAINTBUFFER_DEBUG_DRAW
1448         qDebug() << " -> Cmd_SetCompositionMode, mode: " << mode;
1449 #endif
1450         painter->setCompositionMode(mode);
1451         break; }
1452
1453     case QPaintBufferPrivate::Cmd_SetRenderHints: {
1454 #ifdef QPAINTBUFFER_DEBUG_DRAW
1455         qDebug() << " -> Cmd_SetRenderHints, hints: " << cmd.extra;
1456 #endif
1457         QPainter::RenderHints ph = painter->renderHints();
1458         QPainter::RenderHints nh = (QPainter::RenderHints) cmd.extra;
1459         QPainter::RenderHints xored = ph ^ nh;
1460         if (xored & QPainter::Antialiasing)
1461             painter->setRenderHint(QPainter::Antialiasing, nh & QPainter::Antialiasing);
1462         if (xored & QPainter::HighQualityAntialiasing)
1463             painter->setRenderHint(QPainter::HighQualityAntialiasing, nh & QPainter::HighQualityAntialiasing);
1464         if (xored & QPainter::TextAntialiasing)
1465             painter->setRenderHint(QPainter::TextAntialiasing, nh & QPainter::TextAntialiasing);
1466         if (xored & QPainter::SmoothPixmapTransform)
1467             painter->setRenderHint(QPainter::SmoothPixmapTransform, nh & QPainter::SmoothPixmapTransform);
1468         if (xored & QPainter::NonCosmeticDefaultPen)
1469             painter->setRenderHint(QPainter::NonCosmeticDefaultPen, nh & QPainter::NonCosmeticDefaultPen);
1470         if (xored & QPainter::Qt4CompatiblePainting)
1471             painter->setRenderHint(QPainter::Qt4CompatiblePainting, nh & QPainter::Qt4CompatiblePainting);
1472         break; }
1473
1474     case QPaintBufferPrivate::Cmd_SetOpacity: {
1475 #ifdef QPAINTBUFFER_DEBUG_DRAW
1476         qDebug() << " -> Cmd_SetOpacity: " << d->variants.at(cmd.offset).toDouble();
1477 #endif
1478         painter->setOpacity(d->variants.at(cmd.offset).toDouble());
1479         break; }
1480
1481     case QPaintBufferPrivate::Cmd_SetBackgroundMode: {
1482 #ifdef QPAINTBUFFER_DEBUG_DRAW
1483         qDebug() << " -> Cmd_SetBackgroundMode: " << cmd.extra;
1484 #endif
1485         painter->setBackgroundMode((Qt::BGMode)cmd.extra);
1486         break; }
1487
1488     case QPaintBufferPrivate::Cmd_DrawVectorPath: {
1489 #ifdef QPAINTBUFFER_DEBUG_DRAW
1490         qDebug() << " -> Cmd_DrawVectorPath: size: " << cmd.size
1491 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1492                  << "pts/elms:" << cmd.offset << cmd.offset2;
1493 #endif
1494         QVectorPathCmd path(d, cmd);
1495         painter->drawPath(path().convertToPainterPath());
1496         break; }
1497
1498     case QPaintBufferPrivate::Cmd_StrokeVectorPath: {
1499         QPen pen = qvariant_cast<QPen>(d->variants.at(cmd.extra));
1500 #ifdef QPAINTBUFFER_DEBUG_DRAW
1501         qDebug() << " -> Cmd_StrokeVectorPath: size: " << cmd.size
1502 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1503                  << "pts/elms:" << cmd.offset << cmd.offset2;
1504 #endif
1505         QVectorPathCmd path(d, cmd);
1506         painter->strokePath(path().convertToPainterPath(), pen);
1507         break; }
1508
1509     case QPaintBufferPrivate::Cmd_FillVectorPath: {
1510         QBrush brush = qvariant_cast<QBrush>(d->variants.at(cmd.extra));
1511 #ifdef QPAINTBUFFER_DEBUG_DRAW
1512         qDebug() << " -> Cmd_FillVectorPath: size: " << cmd.size
1513 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1514                  << "pts/elms:" << cmd.offset << cmd.offset2 << brush;
1515 #endif
1516         QVectorPathCmd path(d, cmd);
1517         painter->fillPath(path().convertToPainterPath(), brush);
1518         break; }
1519
1520     case QPaintBufferPrivate::Cmd_DrawPolygonF: {
1521 #ifdef QPAINTBUFFER_DEBUG_DRAW
1522         qDebug() << " -> Cmd_DrawPolygonF, offset: " << cmd.offset << " size: " << cmd.size
1523                  << " mode: " << cmd.extra
1524                  << d->floats.at(cmd.offset)
1525                  << d->floats.at(cmd.offset+1);
1526 #endif
1527         Qt::FillRule fill = (QPaintEngine::PolygonDrawMode) cmd.extra == QPaintEngine::OddEvenMode
1528                             ? Qt::OddEvenFill : Qt::WindingFill;
1529         painter->drawPolygon((QPointF *) (d->floats.constData() + cmd.offset), cmd.size, fill);
1530         break; }
1531
1532     case QPaintBufferPrivate::Cmd_DrawPolygonI: {
1533 #ifdef QPAINTBUFFER_DEBUG_DRAW
1534         qDebug() << " -> Cmd_DrawPolygonI, offset: " << cmd.offset << " size: " << cmd.size
1535                  << " mode: " << cmd.extra
1536                  << d->ints.at(cmd.offset)
1537                  << d->ints.at(cmd.offset+1);
1538 #endif
1539         Qt::FillRule fill = (QPaintEngine::PolygonDrawMode) cmd.extra == QPaintEngine::OddEvenMode
1540                             ? Qt::OddEvenFill : Qt::WindingFill;
1541         painter->drawPolygon((QPoint *) (d->ints.constData() + cmd.offset), cmd.size, fill);
1542         break; }
1543
1544     case QPaintBufferPrivate::Cmd_DrawPolylineF: {
1545 #ifdef QPAINTBUFFER_DEBUG_DRAW
1546         qDebug() << " -> Cmd_DrawPolylineF, offset: " << cmd.offset << " size: " << cmd.size;
1547 #endif
1548         painter->drawPolyline((QPointF *) (d->floats.constData() + cmd.offset), cmd.size);
1549         break; }
1550
1551     case QPaintBufferPrivate::Cmd_DrawPolylineI: {
1552 #ifdef QPAINTBUFFER_DEBUG_DRAW
1553         qDebug() << " -> Cmd_DrawPolylineI, offset: " << cmd.offset << " size: " << cmd.size;
1554 #endif
1555         painter->drawPolyline((QPoint *) (d->ints.constData() + cmd.offset), cmd.size);
1556         break; }
1557
1558     case QPaintBufferPrivate::Cmd_DrawConvexPolygonF: {
1559 #ifdef QPAINTBUFFER_DEBUG_DRAW
1560         qDebug() << " -> Cmd_DrawConvexPolygonF, offset: " << cmd.offset << " size: " << cmd.size;
1561 #endif
1562         painter->drawConvexPolygon((QPointF *) (d->floats.constData() + cmd.offset), cmd.size);
1563         break; }
1564
1565     case QPaintBufferPrivate::Cmd_DrawConvexPolygonI: {
1566 #ifdef QPAINTBUFFER_DEBUG_DRAW
1567         qDebug() << " -> Cmd_DrawConvexPolygonI, offset: " << cmd.offset << " size: " << cmd.size;
1568 #endif
1569         painter->drawConvexPolygon((QPoint *) (d->ints.constData() + cmd.offset), cmd.size);
1570         break; }
1571
1572     case QPaintBufferPrivate::Cmd_DrawEllipseF: {
1573 #ifdef QPAINTBUFFER_DEBUG_DRAW
1574         qDebug() << " -> Cmd_DrawEllipseF, offset: " << cmd.offset;
1575 #endif
1576         painter->drawEllipse(*(QRectF *)(d->floats.constData() + cmd.offset));
1577         break; }
1578
1579     case QPaintBufferPrivate::Cmd_DrawEllipseI: {
1580 #ifdef QPAINTBUFFER_DEBUG_DRAW
1581         qDebug() << " -> Cmd_DrawEllipseI, offset: " << cmd.offset;
1582 #endif
1583         painter->drawEllipse(*(QRect *)(d->ints.constData() + cmd.offset));
1584         break; }
1585
1586     case QPaintBufferPrivate::Cmd_DrawLineF: {
1587 #ifdef QPAINTBUFFER_DEBUG_DRAW
1588         qDebug() << " -> Cmd_DrawLineF, offset: " << cmd.offset << " size: " << cmd.size;
1589 #endif
1590         painter->drawLines((QLineF *)(d->floats.constData() + cmd.offset), cmd.size);
1591         break; }
1592
1593     case QPaintBufferPrivate::Cmd_DrawLineI: {
1594 #ifdef QPAINTBUFFER_DEBUG_DRAW
1595         qDebug() << " -> Cmd_DrawLineI, offset: " << cmd.offset << " size: " << cmd.size;
1596 #endif
1597         painter->drawLines((QLine *)(d->ints.constData() + cmd.offset), cmd.size);
1598         break; }
1599
1600     case QPaintBufferPrivate::Cmd_DrawPointsF: {
1601 #ifdef QPAINTBUFFER_DEBUG_DRAW
1602         qDebug() << " -> Cmd_DrawPointsF, offset: " << cmd.offset << " size: " << cmd.size;
1603 #endif
1604         painter->drawPoints((QPointF *)(d->floats.constData() + cmd.offset), cmd.size);
1605         break; }
1606
1607     case QPaintBufferPrivate::Cmd_DrawPointsI: {
1608 #ifdef QPAINTBUFFER_DEBUG_DRAW
1609         qDebug() << " -> Cmd_DrawPointsI, offset: " << cmd.offset << " size: " << cmd.size;
1610 #endif
1611         painter->drawPoints((QPoint *)(d->ints.constData() + cmd.offset), cmd.size);
1612         break; }
1613
1614     case QPaintBufferPrivate::Cmd_DrawPixmapRect: {
1615         QPixmap pm(d->variants.at(cmd.offset).value<QPixmap>());
1616         QRectF r(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1),
1617                  d->floats.at(cmd.extra+2), d->floats.at(cmd.extra+3));
1618
1619         QRectF sr(d->floats.at(cmd.extra+4), d->floats.at(cmd.extra+5),
1620                   d->floats.at(cmd.extra+6), d->floats.at(cmd.extra+7));
1621 #ifdef QPAINTBUFFER_DEBUG_DRAW
1622         qDebug() << " -> Cmd_DrawPixmapRect:" << r << sr;
1623 #endif
1624         painter->drawPixmap(r, pm, sr);
1625         break; }
1626
1627     case QPaintBufferPrivate::Cmd_DrawPixmapPos: {
1628         QPixmap pm(d->variants.at(cmd.offset).value<QPixmap>());
1629         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
1630 #ifdef QPAINTBUFFER_DEBUG_DRAW
1631         qDebug() << " -> Cmd_DrawPixmapPos:" << pos;
1632 #endif
1633         painter->drawPixmap(pos, pm);
1634         break; }
1635
1636     case QPaintBufferPrivate::Cmd_DrawTiledPixmap: {
1637         QPixmap pm(d->variants.at(cmd.offset).value<QPixmap>());
1638         QRectF r(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1),
1639                  d->floats.at(cmd.extra+2), d->floats.at(cmd.extra+3));
1640
1641         QPointF offset(d->floats.at(cmd.extra+4), d->floats.at(cmd.extra+5));
1642 #ifdef QPAINTBUFFER_DEBUG_DRAW
1643         qDebug() << " -> Cmd_DrawTiledPixmap:" << r << offset;
1644 #endif
1645         painter->drawTiledPixmap(r, pm, offset);
1646         break; }
1647
1648     case QPaintBufferPrivate::Cmd_DrawImageRect: {
1649         QImage image(d->variants.at(cmd.offset).value<QImage>());
1650         QRectF r(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1),
1651                  d->floats.at(cmd.extra+2), d->floats.at(cmd.extra+3));
1652         QRectF sr(d->floats.at(cmd.extra+4), d->floats.at(cmd.extra+5),
1653                   d->floats.at(cmd.extra+6), d->floats.at(cmd.extra+7));
1654 #ifdef QPAINTBUFFER_DEBUG_DRAW
1655         qDebug() << " -> Cmd_DrawImageRect:" << r << sr;
1656 #endif
1657         painter->drawImage(r, image, sr);
1658         break; }
1659
1660     case QPaintBufferPrivate::Cmd_DrawImagePos: {
1661         QImage image(d->variants.at(cmd.offset).value<QImage>());
1662         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
1663 #ifdef QPAINTBUFFER_DEBUG_DRAW
1664         qDebug() << " -> Cmd_DrawImagePos:" << pos;
1665 #endif
1666         painter->drawImage(pos, image);
1667         break; }
1668
1669     case QPaintBufferPrivate::Cmd_DrawRectF: {
1670 #ifdef QPAINTBUFFER_DEBUG_DRAW
1671         qDebug() << " -> Cmd_DrawRectF, offset: " << cmd.offset;
1672 #endif
1673         painter->drawRects((QRectF *)(d->floats.constData() + cmd.offset), cmd.size);
1674         break; }
1675
1676     case QPaintBufferPrivate::Cmd_DrawRectI: {
1677 #ifdef QPAINTBUFFER_DEBUG_DRAW
1678         qDebug() << " -> Cmd_DrawRectI, offset: " << cmd.offset;
1679 #endif
1680         painter->drawRects((QRect *)(d->ints.constData() + cmd.offset), cmd.size);
1681         break; }
1682
1683     case QPaintBufferPrivate::Cmd_FillRectBrush: {
1684         QBrush brush = qvariant_cast<QBrush>(d->variants.at(cmd.extra));
1685         QRectF *rect = (QRectF *)(d->floats.constData() + cmd.offset);
1686 #ifdef QPAINTBUFFER_DEBUG_DRAW
1687         qDebug() << " -> Cmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " brush: " << brush;
1688 #endif
1689         painter->fillRect(*rect, brush);
1690         break; }
1691
1692     case QPaintBufferPrivate::Cmd_FillRectColor: {
1693         QColor color = qvariant_cast<QColor>(d->variants.at(cmd.extra));
1694         QRectF *rect = (QRectF *)(d->floats.constData() + cmd.offset);
1695 #ifdef QPAINTBUFFER_DEBUG_DRAW
1696         qDebug() << " -> Cmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " color: " << color;
1697 #endif
1698         painter->fillRect(*rect, color);
1699         break; }
1700
1701     case QPaintBufferPrivate::Cmd_SetClipEnabled: {
1702         bool clipEnabled = d->variants.at(cmd.offset).toBool();
1703 #ifdef QPAINTBUFFER_DEBUG_DRAW
1704         qDebug() << " -> Cmd_SetClipEnabled:" << clipEnabled;
1705 #endif
1706         painter->setClipping(clipEnabled);
1707         break; }
1708
1709     case QPaintBufferPrivate::Cmd_ClipVectorPath: {
1710         QVectorPathCmd path(d, cmd);
1711 #ifdef QPAINTBUFFER_DEBUG_DRAW
1712         qDebug() << " -> Cmd_ClipVectorPath:" << path().elementCount();
1713 #endif
1714         painter->setClipPath(path().convertToPainterPath(), Qt::ClipOperation(cmd.extra));
1715         break; }
1716
1717
1718     case QPaintBufferPrivate::Cmd_ClipRect: {
1719         QRect rect(QPoint(d->ints.at(cmd.offset), d->ints.at(cmd.offset + 1)),
1720                    QPoint(d->ints.at(cmd.offset + 2), d->ints.at(cmd.offset + 3)));
1721 #ifdef QPAINTBUFFER_DEBUG_DRAW
1722         qDebug() << " -> Cmd_ClipRect:" << rect << cmd.extra;
1723 #endif
1724         painter->setClipRect(rect, Qt::ClipOperation(cmd.extra));
1725         break; }
1726
1727     case QPaintBufferPrivate::Cmd_ClipRegion: {
1728         QRegion region(d->variants.at(cmd.offset).value<QRegion>());
1729 #ifdef QPAINTBUFFER_DEBUG_DRAW
1730         qDebug() << " -> Cmd_ClipRegion:" << region.boundingRect() << cmd.extra;
1731 #endif
1732         painter->setClipRegion(region, Qt::ClipOperation(cmd.extra));
1733         break; }
1734         
1735 #if !defined(QT_NO_RAWFONT)
1736     case QPaintBufferPrivate::Cmd_DrawStaticText: {
1737             
1738             QVariantList variants(d->variants.at(cmd.offset).value<QVariantList>());
1739             
1740             QFont font = variants.at(0).value<QFont>();
1741
1742             QVector<quint32> glyphIndexes;
1743             QVector<QPointF> positions;
1744
1745             for (int i=0; i<(variants.size() - 1) / 2; ++i) {
1746                 glyphIndexes.append(variants.at(i*2 + 1).toUInt());
1747                 positions.append(variants.at(i*2 + 2).toPointF());
1748             }
1749
1750             painter->setFont(font);
1751
1752             QRawFont rawFont;
1753             QRawFontPrivate *rawFontD = QRawFontPrivate::get(rawFont);
1754             QFontPrivate *fontD = QFontPrivate::get(font);
1755             rawFontD->fontEngine = fontD->engineForScript(QUnicodeTables::Common);
1756             rawFontD->fontEngine->ref.ref();
1757
1758             QGlyphRun glyphs;
1759             glyphs.setRawFont(rawFont);
1760             glyphs.setGlyphIndexes(glyphIndexes);
1761             glyphs.setPositions(positions);
1762
1763             painter->drawGlyphRun(QPointF(), glyphs);
1764             break;
1765     }
1766 #endif
1767
1768     case QPaintBufferPrivate::Cmd_DrawText: {
1769         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
1770         QList<QVariant> variants(d->variants.at(cmd.offset).value<QList<QVariant> >());
1771
1772         QFont font(variants.at(0).value<QFont>());
1773         QString text(variants.at(1).value<QString>());
1774
1775         painter->setFont(font);
1776         painter->drawText(pos, text);
1777         break; }
1778
1779     case QPaintBufferPrivate::Cmd_DrawTextItem: {
1780         QPointF pos(d->floats.at(cmd.extra), d->floats.at(cmd.extra+1));
1781         QTextItemIntCopy *tiCopy = reinterpret_cast<QTextItemIntCopy *>(qvariant_cast<void *>(d->variants.at(cmd.offset)));
1782         QTextItemInt &ti = (*tiCopy)();
1783         QString text(ti.text());
1784
1785         QFont font(ti.font());
1786         font.setUnderline(false);
1787         font.setStrikeOut(false);
1788         font.setOverline(false);
1789
1790         const QTextItemInt &si = static_cast<const QTextItemInt &>(ti);
1791         qreal justificationWidth = 0;
1792         if (si.justified)
1793             justificationWidth = si.width.toReal();
1794         qreal scaleFactor = font.d->dpi/qreal(qt_defaultDpiY());
1795
1796 #ifdef QPAINTBUFFER_DEBUG_DRAW
1797         qDebug() << " -> Cmd_DrawTextItem:" << pos << " " << text << " " << scaleFactor;
1798 #endif
1799
1800         if (scaleFactor != 1.0) {
1801             QFont fnt(font);
1802             QFakeDevice fake;
1803             fake.setDpiX(qRound(scaleFactor*qt_defaultDpiX()));
1804             fake.setDpiY(qRound(scaleFactor*qt_defaultDpiY()));
1805             font = QFont(fnt, &fake);
1806         }
1807
1808         int flags = Qt::TextSingleLine | Qt::TextDontClip | Qt::TextForceLeftToRight;
1809         QSizeF size(1, 1);
1810         if (justificationWidth > 0) {
1811             size.setWidth(justificationWidth);
1812             flags |= Qt::TextJustificationForced;
1813             flags |= Qt::AlignJustify;
1814         }
1815
1816         QFontMetrics fm(font);
1817         QPointF pt(pos.x(), pos.y() - fm.ascent());
1818         qt_format_text(font, QRectF(pt, size), flags, /*opt*/0,
1819                        text, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
1820         break; }
1821     case QPaintBufferPrivate::Cmd_SystemStateChanged: {
1822         QRegion systemClip(d->variants.at(cmd.offset).value<QRegion>());
1823
1824 #ifdef QPAINTBUFFER_DEBUG_DRAW
1825         qDebug() << " -> Cmd_SystemStateChanged:" << systemClip;
1826 #endif
1827
1828         painter->paintEngine()->setSystemClip(systemClip);
1829         painter->paintEngine()->d_ptr->systemStateChanged();
1830         break; }
1831     }
1832 }
1833
1834 void QPaintEngineExReplayer::process(const QPaintBufferCommand &cmd)
1835 {
1836     Q_ASSERT(painter->paintEngine()->isExtended());
1837     QPaintEngineEx *xengine = static_cast<QPaintEngineEx *>(painter->paintEngine());
1838
1839     switch (cmd.id) {
1840     case QPaintBufferPrivate::Cmd_SetBrushOrigin: {
1841 #ifdef QPAINTBUFFER_DEBUG_DRAW
1842         qDebug() << " -> ExCmd_SetBrushOrigin: " << d->variants.at(cmd.offset).toPointF();
1843 #endif
1844         xengine->state()->brushOrigin = d->variants.at(cmd.offset).toPointF();
1845         xengine->brushOriginChanged();
1846         break; }
1847
1848     case QPaintBufferPrivate::Cmd_SetCompositionMode: {
1849         QPainter::CompositionMode mode = (QPainter::CompositionMode) cmd.extra;
1850 #ifdef QPAINTBUFFER_DEBUG_DRAW
1851         qDebug() << " -> ExCmd_SetCompositionMode, mode: " << mode;
1852 #endif
1853         xengine->state()->composition_mode = mode;
1854         xengine->compositionModeChanged();
1855         break; }
1856
1857     case QPaintBufferPrivate::Cmd_SetOpacity: {
1858 #ifdef QPAINTBUFFER_DEBUG_DRAW
1859         qDebug() << " -> ExCmd_SetOpacity: " << d->variants.at(cmd.offset).toDouble();
1860 #endif
1861         xengine->state()->opacity = d->variants.at(cmd.offset).toDouble();
1862         xengine->opacityChanged();
1863         break; }
1864
1865     case QPaintBufferPrivate::Cmd_DrawVectorPath: {
1866 #ifdef QPAINTBUFFER_DEBUG_DRAW
1867         qDebug() << " -> ExCmd_DrawVectorPath: size: " << cmd.size
1868 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1869                  << "pts/elms:" << cmd.offset << cmd.offset2;
1870 #endif
1871         QVectorPathCmd path(d, cmd);
1872         xengine->draw(path());
1873         break; }
1874
1875     case QPaintBufferPrivate::Cmd_StrokeVectorPath: {
1876         QPen pen = qvariant_cast<QPen>(d->variants.at(cmd.extra));
1877 #ifdef QPAINTBUFFER_DEBUG_DRAW
1878         qDebug() << " -> ExCmd_StrokeVectorPath: size: " << cmd.size
1879 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1880                  << "pts/elms:" << cmd.offset << cmd.offset2;
1881 #endif
1882         QVectorPathCmd path(d, cmd);
1883         xengine->stroke(path(), pen);
1884         break; }
1885
1886     case QPaintBufferPrivate::Cmd_FillVectorPath: {
1887         QBrush brush = qvariant_cast<QBrush>(d->variants.at(cmd.extra));
1888 #ifdef QPAINTBUFFER_DEBUG_DRAW
1889         qDebug() << " -> ExCmd_FillVectorPath: size: " << cmd.size
1890 //                 << ", hints:" << d->ints[cmd.offset2+cmd.size]
1891                  << "pts/elms:" << cmd.offset << cmd.offset2 << brush;
1892 #endif
1893         QVectorPathCmd path(d, cmd);
1894         xengine->fill(path(), brush);
1895         break; }
1896
1897     case QPaintBufferPrivate::Cmd_FillRectBrush: {
1898         QBrush brush = qvariant_cast<QBrush>(d->variants.at(cmd.extra));
1899         QRectF *rect = (QRectF *)(d->floats.constData() + cmd.offset);
1900 #ifdef QPAINTBUFFER_DEBUG_DRAW
1901         qDebug() << " -> ExCmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " brush: " << brush;
1902 #endif
1903         xengine->fillRect(*rect, brush);
1904         break; }
1905
1906     case QPaintBufferPrivate::Cmd_FillRectColor: {
1907         QColor color = qvariant_cast<QColor>(d->variants.at(cmd.extra));
1908         QRectF *rect = (QRectF *)(d->floats.constData() + cmd.offset);
1909 #ifdef QPAINTBUFFER_DEBUG_DRAW
1910         qDebug() << " -> ExCmd_FillRectBrush, offset: " << cmd.offset << " rect: " << *rect << " color: " << color;
1911 #endif
1912         xengine->fillRect(*rect, color);
1913         break; }
1914
1915     case QPaintBufferPrivate::Cmd_DrawPolygonF: {
1916 #ifdef QPAINTBUFFER_DEBUG_DRAW
1917         qDebug() << " -> ExCmd_DrawPolygonF, offset: " << cmd.offset << " size: " << cmd.size
1918                  << " mode: " << cmd.extra
1919                  << d->floats.at(cmd.offset)
1920                  << d->floats.at(cmd.offset+1);
1921 #endif
1922         xengine->drawPolygon((QPointF *) (d->floats.constData() + cmd.offset), cmd.size,
1923                              (QPaintEngine::PolygonDrawMode) cmd.extra);
1924         break; }
1925
1926     case QPaintBufferPrivate::Cmd_DrawPolygonI: {
1927 #ifdef QPAINTBUFFER_DEBUG_DRAW
1928         qDebug() << " -> ExCmd_DrawPolygonI, offset: " << cmd.offset << " size: " << cmd.size
1929                  << " mode: " << cmd.extra
1930                  << d->ints.at(cmd.offset)
1931                  << d->ints.at(cmd.offset+1);
1932 #endif
1933         xengine->drawPolygon((QPoint *) (d->ints.constData() + cmd.offset), cmd.size,
1934                              (QPaintEngine::PolygonDrawMode) cmd.extra);
1935         break; }
1936
1937     case QPaintBufferPrivate::Cmd_DrawEllipseF: {
1938 #ifdef QPAINTBUFFER_DEBUG_DRAW
1939         qDebug() << " -> ExCmd_DrawEllipseF, offset: " << cmd.offset;
1940 #endif
1941         xengine->drawEllipse(*(QRectF *)(d->floats.constData() + cmd.offset));
1942         break; }
1943
1944     case QPaintBufferPrivate::Cmd_DrawEllipseI: {
1945 #ifdef QPAINTBUFFER_DEBUG_DRAW
1946         qDebug() << " -> ExCmd_DrawEllipseI, offset: " << cmd.offset;
1947 #endif
1948         xengine->drawEllipse(*(QRect *)(d->ints.constData() + cmd.offset));
1949         break; }
1950
1951     case QPaintBufferPrivate::Cmd_DrawLineF: {
1952 #ifdef QPAINTBUFFER_DEBUG_DRAW
1953         qDebug() << " -> ExCmd_DrawLineF, offset: " << cmd.offset << " size: " << cmd.size;
1954 #endif
1955         xengine->drawLines((QLineF *)(d->floats.constData() + cmd.offset), cmd.size);
1956         break; }
1957
1958     case QPaintBufferPrivate::Cmd_DrawLineI: {
1959 #ifdef QPAINTBUFFER_DEBUG_DRAW
1960         qDebug() << " -> ExCmd_DrawLineI, offset: " << cmd.offset << " size: " << cmd.size;
1961 #endif
1962         xengine->drawLines((QLine *)(d->ints.constData() + cmd.offset), cmd.size);
1963         break; }
1964
1965     case QPaintBufferPrivate::Cmd_DrawPointsF: {
1966 #ifdef QPAINTBUFFER_DEBUG_DRAW
1967         qDebug() << " -> ExCmd_DrawPointsF, offset: " << cmd.offset << " size: " << cmd.size;
1968 #endif
1969         xengine->drawPoints((QPointF *)(d->floats.constData() + cmd.offset), cmd.size);
1970         break; }
1971
1972     case QPaintBufferPrivate::Cmd_DrawPointsI: {
1973 #ifdef QPAINTBUFFER_DEBUG_DRAW
1974         qDebug() << " -> ExCmd_DrawPointsI, offset: " << cmd.offset << " size: " << cmd.size;
1975 #endif
1976         xengine->drawPoints((QPoint *)(d->ints.constData() + cmd.offset), cmd.size);
1977         break; }
1978
1979     case QPaintBufferPrivate::Cmd_DrawPolylineF: {
1980 #ifdef QPAINTBUFFER_DEBUG_DRAW
1981         qDebug() << " -> ExCmd_DrawPolylineF, offset: " << cmd.offset << " size: " << cmd.size;
1982 #endif
1983         xengine->drawPolygon((QPointF *) (d->floats.constData() + cmd.offset), cmd.size, QPaintEngine::PolylineMode);
1984         break; }
1985
1986     case QPaintBufferPrivate::Cmd_DrawPolylineI: {
1987 #ifdef QPAINTBUFFER_DEBUG_DRAW
1988         qDebug() << " -> ExCmd_DrawPolylineI, offset: " << cmd.offset << " size: " << cmd.size;
1989 #endif
1990         xengine->drawPolygon((QPoint *) (d->ints.constData() + cmd.offset), cmd.size, QPaintEngine::PolylineMode);
1991         break; }
1992
1993     case QPaintBufferPrivate::Cmd_DrawRectF: {
1994 #ifdef QPAINTBUFFER_DEBUG_DRAW
1995         qDebug() << " -> ExCmd_DrawRectF, offset: " << cmd.offset << " size: " << cmd.size;
1996 #endif
1997         xengine->drawRects((QRectF *) (d->floats.constData() + cmd.offset), cmd.size);
1998         break; }
1999
2000     case QPaintBufferPrivate::Cmd_DrawRectI: {
2001 #ifdef QPAINTBUFFER_DEBUG_DRAW
2002         qDebug() << " -> ExCmd_DrawRectI, offset: " << cmd.offset << " size: " << cmd.size;
2003 #endif
2004         xengine->drawRects((QRect *) (d->ints.constData() + cmd.offset), cmd.size);
2005         break; }
2006
2007     case QPaintBufferPrivate::Cmd_SetClipEnabled: {
2008         bool clipEnabled = d->variants.at(cmd.offset).toBool();
2009 #ifdef QPAINTBUFFER_DEBUG_DRAW
2010         qDebug() << " -> ExCmd_SetClipEnabled:" << clipEnabled;
2011 #endif
2012         xengine->state()->clipEnabled = clipEnabled;
2013         xengine->clipEnabledChanged();
2014         break; }
2015
2016     case QPaintBufferPrivate::Cmd_ClipVectorPath: {
2017         QVectorPathCmd path(d, cmd);
2018 #ifdef QPAINTBUFFER_DEBUG_DRAW
2019         qDebug() << " -> ExCmd_ClipVectorPath:" << path().elementCount();
2020 #endif
2021         xengine->clip(path(), Qt::ClipOperation(cmd.extra));
2022         break; }
2023
2024
2025     case QPaintBufferPrivate::Cmd_ClipRect: {
2026         QRect rect(QPoint(d->ints.at(cmd.offset), d->ints.at(cmd.offset + 1)),
2027                    QPoint(d->ints.at(cmd.offset + 2), d->ints.at(cmd.offset + 3)));
2028 #ifdef QPAINTBUFFER_DEBUG_DRAW
2029         qDebug() << " -> ExCmd_ClipRect:" << rect << cmd.extra;
2030 #endif
2031         xengine->clip(rect, Qt::ClipOperation(cmd.extra));
2032         break; }
2033
2034     case QPaintBufferPrivate::Cmd_ClipRegion: {
2035         QRegion region(d->variants.at(cmd.offset).value<QRegion>());
2036 #ifdef QPAINTBUFFER_DEBUG_DRAW
2037         qDebug() << " -> ExCmd_ClipRegion:" << region.boundingRect() << cmd.extra;
2038 #endif
2039         xengine->clip(region, Qt::ClipOperation(cmd.extra));
2040         break; }
2041
2042     default:
2043         QPainterReplayer::process(cmd);
2044         break;
2045     }
2046 }
2047
2048 QDataStream &operator<<(QDataStream &stream, const QPaintBufferCommand &command)
2049 {
2050     quint32 id = command.id;
2051     quint32 size = command.size;
2052     stream << id << size;
2053     stream << command.offset << command.offset2 << command.extra;
2054     return stream;
2055 }
2056
2057 QDataStream &operator>>(QDataStream &stream, QPaintBufferCommand &command)
2058 {
2059     quint32 id;
2060     quint32 size;
2061     stream >> id >> size;
2062     stream >> command.offset >> command.offset2 >> command.extra;
2063     command.id = id;
2064     command.size = size;
2065     return stream;
2066 }
2067
2068 struct QPaintBufferCacheEntry
2069 {
2070     QVariant::Type type;
2071     quint64 cacheKey;
2072 };
2073
2074 struct QPaintBufferCacheEntryV2
2075 {
2076     enum Type {
2077         ImageKey,
2078         PixmapKey
2079     };
2080
2081     struct Flags {
2082         uint type : 8;
2083         uint key : 24;
2084     };
2085
2086     union {
2087         Flags flags;
2088         uint bits;
2089     };
2090 };
2091
2092 QT_END_NAMESPACE
2093 Q_DECLARE_METATYPE(QPaintBufferCacheEntry)
2094 Q_DECLARE_METATYPE(QPaintBufferCacheEntryV2)
2095 QT_BEGIN_NAMESPACE
2096
2097 QDataStream &operator<<(QDataStream &stream, const QPaintBufferCacheEntry &entry)
2098 {
2099     return stream << entry.type << entry.cacheKey;
2100 }
2101
2102 QDataStream &operator>>(QDataStream &stream, QPaintBufferCacheEntry &entry)
2103 {
2104     return stream >> entry.type >> entry.cacheKey;
2105 }
2106
2107 QDataStream &operator<<(QDataStream &stream, const QPaintBufferCacheEntryV2 &entry)
2108 {
2109     return stream << entry.bits;
2110 }
2111
2112 QDataStream &operator>>(QDataStream &stream, QPaintBufferCacheEntryV2 &entry)
2113 {
2114     return stream >> entry.bits;
2115 }
2116
2117 static void qRegisterPaintBufferMetaTypes()
2118 {
2119     qRegisterMetaTypeStreamOperators<QPaintBufferCacheEntry>();
2120     qRegisterMetaTypeStreamOperators<QPaintBufferCacheEntryV2>();
2121 }
2122
2123 Q_CONSTRUCTOR_FUNCTION(qRegisterPaintBufferMetaTypes)
2124
2125 QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer)
2126 {
2127     QHash<qint64, uint> pixmapKeys;
2128     QHash<qint64, uint> imageKeys;
2129
2130     QHash<qint64, QPixmap> pixmaps;
2131     QHash<qint64, QImage> images;
2132
2133     QVector<QVariant> variants = buffer.d_ptr->variants;
2134     for (int i = 0; i < variants.size(); ++i) {
2135         const QVariant &v = variants.at(i);
2136         if (v.type() == QVariant::Image) {
2137             const QImage image(v.value<QImage>());
2138
2139             QPaintBufferCacheEntryV2 entry;
2140             entry.flags.type = QPaintBufferCacheEntryV2::ImageKey;
2141
2142             QHash<qint64, uint>::iterator it = imageKeys.find(image.cacheKey());
2143             if (it != imageKeys.end()) {
2144                 entry.flags.key = *it;
2145             } else {
2146                 imageKeys[image.cacheKey()] = entry.flags.key = images.size();
2147                 images[images.size()] = image;
2148             }
2149
2150             variants[i] = QVariant::fromValue(entry);
2151         } else if (v.type() == QVariant::Pixmap) {
2152             const QPixmap pixmap(v.value<QPixmap>());
2153
2154             QPaintBufferCacheEntryV2 entry;
2155             entry.flags.type = QPaintBufferCacheEntryV2::PixmapKey;
2156
2157             QHash<qint64, uint>::iterator it = pixmapKeys.find(pixmap.cacheKey());
2158             if (it != pixmapKeys.end()) {
2159                 entry.flags.key = *it;
2160             } else {
2161                 pixmapKeys[pixmap.cacheKey()] = entry.flags.key = pixmaps.size();
2162                 pixmaps[pixmaps.size()] = pixmap;
2163             }
2164
2165             variants[i] = QVariant::fromValue(entry);
2166         }
2167     }
2168
2169     stream << pixmaps;
2170     stream << images;
2171
2172     stream << buffer.d_ptr->ints;
2173     stream << buffer.d_ptr->floats;
2174     stream << variants;
2175     stream << buffer.d_ptr->commands;
2176     stream << buffer.d_ptr->boundingRect;
2177     stream << buffer.d_ptr->frames;
2178
2179     return stream;
2180 }
2181
2182 QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer)
2183 {
2184     QHash<qint64, QPixmap> pixmaps;
2185     QHash<qint64, QImage> images;
2186
2187     stream >> pixmaps;
2188     stream >> images;
2189
2190     stream >> buffer.d_ptr->ints;
2191     stream >> buffer.d_ptr->floats;
2192     stream >> buffer.d_ptr->variants;
2193     stream >> buffer.d_ptr->commands;
2194     stream >> buffer.d_ptr->boundingRect;
2195     stream >> buffer.d_ptr->frames;
2196
2197     QVector<QVariant> &variants = buffer.d_ptr->variants;
2198     for (int i = 0; i < variants.size(); ++i) {
2199         const QVariant &v = variants.at(i);
2200         if (v.canConvert<QPaintBufferCacheEntry>()) {
2201             QPaintBufferCacheEntry entry = v.value<QPaintBufferCacheEntry>();
2202             if (entry.type == QVariant::Image)
2203                 variants[i] = QVariant(images.value(entry.cacheKey));
2204             else
2205                 variants[i] = QVariant(pixmaps.value(entry.cacheKey));
2206         } else if (v.canConvert<QPaintBufferCacheEntryV2>()) {
2207             QPaintBufferCacheEntryV2 entry = v.value<QPaintBufferCacheEntryV2>();
2208
2209             if (entry.flags.type == QPaintBufferCacheEntryV2::ImageKey)
2210                 variants[i] = QVariant(images.value(entry.flags.key));
2211             else if (entry.flags.type == QPaintBufferCacheEntryV2::PixmapKey)
2212                 variants[i] = QVariant(pixmaps.value(entry.flags.key));
2213             else
2214                 qWarning() << "operator<<(QDataStream &stream, QPaintBuffer &buffer): unrecognized cache entry type:" << entry.flags.type;
2215         }
2216     }
2217
2218     return stream;
2219 }
2220
2221 QT_END_NAMESPACE