1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QPAINTBUFFER_P_H
43 #define QPAINTBUFFER_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <qpaintdevice.h>
58 #include <private/qpaintengineex_p.h>
59 #include <private/qtextengine_p.h>
64 class QPaintBufferPrivate;
65 class QPaintBufferPlayback;
67 class Q_GUI_EXPORT QPaintBuffer : public QPaintDevice
69 Q_DECLARE_PRIVATE(QPaintBuffer)
72 QPaintBuffer(const QPaintBuffer &other);
78 int numFrames() const;
80 void draw(QPainter *painter, int frame = 0) const;
82 int frameStartIndex(int frame) const;
83 int frameEndIndex(int frame) const;
84 int processCommands(QPainter *painter, int begin, int end) const;
85 #ifndef QT_NO_DEBUG_STREAM
86 QString commandDescription(int command) const;
89 void setBoundingRect(const QRectF &rect);
90 QRectF boundingRect() const;
92 virtual QPaintEngine *paintEngine() const;
93 virtual int metric(PaintDeviceMetric m) const;
94 virtual int devType() const;
96 QPaintBuffer &operator=(const QPaintBuffer &other);
99 friend class QPainterReplayer;
100 friend class QOpenGLReplayer;
102 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
103 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
105 QPaintBufferPrivate *d_ptr;
108 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
109 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
111 class QPaintBufferEngine;
113 class QTextItemIntCopy
116 QTextItemIntCopy(const QTextItem &item);
118 QTextItemInt &operator () () {return m_item;}
124 struct QPaintBufferCommand
134 QDataStream &operator<<(QDataStream &stream, const QPaintBufferCommand &command);
135 QDataStream &operator>>(QDataStream &stream, QPaintBufferCommand &command);
137 Q_DECLARE_TYPEINFO(QPaintBufferCommand, Q_MOVABLE_TYPE);
139 class QPaintBufferPrivate
149 Cmd_SetCompositionMode,
154 Cmd_SetBackgroundMode,
163 Cmd_StrokeVectorPath,
165 Cmd_DrawConvexPolygonF,
166 Cmd_DrawConvexPolygonI,
193 Cmd_SystemStateChanged,
197 // new commands must be added above this line
202 QPaintBufferPrivate();
203 ~QPaintBufferPrivate();
205 int addData(const int *data, int count) {
208 int pos = ints.size();
209 ints.resize(pos + count);
210 memcpy(ints.data() + pos, data, count * sizeof(int));
214 int addData(const qreal *data, int count) {
217 int pos = floats.size();
218 floats.resize(pos + count);
219 memcpy(floats.data() + pos, data, count * sizeof(qreal));
223 int addData(const QVariant &var) {
225 return variants.size() - 1;
228 QPaintBufferCommand *addCommand(Command command) {
229 QPaintBufferCommand cmd;
231 cmd.size = cmd.offset = cmd.offset2 = cmd.extra = 0;
233 return &commands.last();
236 QPaintBufferCommand *addCommand(Command command, const QVariant &var) {
237 QPaintBufferCommand cmd;
239 cmd.offset = addData(var);
240 cmd.size = cmd.offset2 = cmd.extra = 0;
242 return &commands.last();
245 QPaintBufferCommand *addCommand(Command command, const QVectorPath &path) {
246 QPaintBufferCommand cmd;
248 cmd.offset = addData(path.points(), path.elementCount() * 2);
249 cmd.offset2 = ints.size();
250 ints << path.hints();
251 // The absence of path elements is indicated by setting the highest bit in 'cmd.offset2'.
253 addData((const int *) path.elements(), path.elementCount());
255 cmd.offset2 |= 0x80000000;
256 cmd.size = path.elementCount();
259 return &commands.last();
262 QPaintBufferCommand *addCommand(Command command , const qreal *pts, int arrayLength, int elementCount) {
263 QPaintBufferCommand cmd;
265 cmd.offset = addData(pts, arrayLength);
266 cmd.size = elementCount;
267 cmd.offset2 = cmd.extra = 0;
269 return &commands.last();
272 QPaintBufferCommand *addCommand(Command command , const int *pts, int arrayLength, int elementCount) {
273 QPaintBufferCommand cmd;
275 cmd.offset = addData(pts, arrayLength);
276 cmd.size = elementCount;
277 cmd.offset2 = cmd.extra = 0;
279 return &commands.last();
282 inline void updateBoundingRect(const QRectF &rect);
287 QVector<qreal> floats;
288 QVector<QVariant> variants;
290 QVector<QPaintBufferCommand> commands;
293 QPaintBufferEngine *engine;
295 qreal penWidthAdjustment;
296 uint calculateBoundingRect : 1;
302 struct QVectorPathCmd
304 // The absence of path elements is indicated by setting the highest bit in 'cmd.offset2'.
305 QVectorPathCmd(QPaintBufferPrivate *d, const QPaintBufferCommand &cmd)
306 : vectorPath(d->floats.constData() + cmd.offset,
308 cmd.offset2 & 0x80000000
310 : (const QPainterPath::ElementType *) (d->ints.constData() + cmd.offset2 + 1),
311 *(d->ints.constData() + (cmd.offset2 & 0x7fffffff))) {}
313 inline const QVectorPath &operator()() const { return vectorPath; }
315 QVectorPath vectorPath;
319 class Q_GUI_EXPORT QPainterReplayer
322 QPainterReplayer() { }
324 virtual ~QPainterReplayer() { }
326 void setupTransform(QPainter *painter);
327 virtual void process(const QPaintBufferCommand &cmd);
328 void processCommands(const QPaintBuffer &buffer, QPainter *painter, int begin, int end);
331 QPaintBufferPrivate *d;
332 QTransform m_world_matrix;
337 class Q_GUI_EXPORT QPaintEngineExReplayer : public QPainterReplayer
340 QPaintEngineExReplayer() { }
342 virtual void process(const QPaintBufferCommand &cmd);
345 class QPaintBufferEnginePrivate;
347 class QPaintBufferEngine : public QPaintEngineEx
349 Q_DECLARE_PRIVATE(QPaintBufferEngine)
351 QPaintBufferEngine(QPaintBufferPrivate *buffer);
353 virtual bool begin(QPaintDevice *device);
356 virtual Type type() const { return QPaintEngine::PaintBuffer; }
358 virtual QPainterState *createState(QPainterState *orig) const;
360 virtual void draw(const QVectorPath &path);
361 virtual void fill(const QVectorPath &path, const QBrush &brush);
362 virtual void stroke(const QVectorPath &path, const QPen &pen);
364 virtual void clip(const QVectorPath &path, Qt::ClipOperation op);
365 virtual void clip(const QRect &rect, Qt::ClipOperation op);
366 virtual void clip(const QRegion ®ion, Qt::ClipOperation op);
367 virtual void clip(const QPainterPath &path, Qt::ClipOperation op);
369 virtual void clipEnabledChanged();
370 virtual void penChanged();
371 virtual void brushChanged();
372 virtual void brushOriginChanged();
373 virtual void opacityChanged();
374 virtual void compositionModeChanged();
375 virtual void renderHintsChanged();
376 virtual void transformChanged();
377 virtual void backgroundModeChanged();
379 virtual void fillRect(const QRectF &rect, const QBrush &brush);
380 virtual void fillRect(const QRectF &rect, const QColor &color);
382 virtual void drawRects(const QRect *rects, int rectCount);
383 virtual void drawRects(const QRectF *rects, int rectCount);
385 virtual void drawLines(const QLine *lines, int lineCount);
386 virtual void drawLines(const QLineF *lines, int lineCount);
388 virtual void drawEllipse(const QRectF &r);
389 virtual void drawEllipse(const QRect &r);
391 virtual void drawPath(const QPainterPath &path);
393 virtual void drawPoints(const QPointF *points, int pointCount);
394 virtual void drawPoints(const QPoint *points, int pointCount);
396 virtual void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
397 virtual void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode);
399 virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
400 virtual void drawPixmap(const QPointF &pos, const QPixmap &pm);
402 virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
403 Qt::ImageConversionFlags flags = Qt::AutoColor);
404 virtual void drawImage(const QPointF &pos, const QImage &image);
406 virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s);
408 virtual void drawTextItem(const QPointF &pos, const QTextItem &ti);
409 virtual void drawStaticTextItem(QStaticTextItem *staticTextItem);
411 virtual void setState(QPainterState *s);
412 virtual uint flags() const {return QPaintEngineEx::DoNotEmulate;}
414 QPaintBufferPrivate *buffer;
416 mutable int m_begin_detected : 1;
417 mutable int m_save_detected : 1;
418 mutable int m_stream_raw_text_items : 1;
419 mutable int m_unused : 29;
421 mutable QPainterState *m_created_state;
424 class Q_GUI_EXPORT QPaintBufferSignalProxy : public QObject
428 QPaintBufferSignalProxy() : QObject() {}
429 void emitAboutToDestroy(const QPaintBufferPrivate *buffer) {
430 emit aboutToDestroy(buffer);
432 static QPaintBufferSignalProxy *instance();
434 void aboutToDestroy(const QPaintBufferPrivate *buffer);
437 // One resource per paint buffer and vice versa.
438 class Q_GUI_EXPORT QPaintBufferResource : public QObject
442 typedef void (*FreeFunc)(void *);
444 QPaintBufferResource(FreeFunc f, QObject *parent = 0);
445 ~QPaintBufferResource();
446 // Set resource 'value' for 'key'.
447 void insert(const QPaintBufferPrivate *key, void *value);
448 // Return resource for 'key'.
449 void *value(const QPaintBufferPrivate *key);
451 // Remove entry 'key' from cache and delete resource.
452 void remove(const QPaintBufferPrivate *key);
454 typedef QHash<const QPaintBufferPrivate *, void *> Cache;
461 #endif // QPAINTBUFFER_P_H