source: trunk/src/gui/painting/qpaintbuffer.cpp@ 651

Last change on this file since 651 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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