Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qpaintbuffer_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QPAINTBUFFER_P_H
43 #define QPAINTBUFFER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include <qpaintdevice.h>
57
58 #include <private/qpaintengineex_p.h>
59 #include <private/qtextengine_p.h>
60 #include <QDebug>
61
62 QT_BEGIN_NAMESPACE
63
64 class QPaintBufferPrivate;
65 class QPaintBufferPlayback;
66
67 class Q_GUI_EXPORT QPaintBuffer : public QPaintDevice
68 {
69     Q_DECLARE_PRIVATE(QPaintBuffer)
70 public:
71     QPaintBuffer();
72     QPaintBuffer(const QPaintBuffer &other);
73     ~QPaintBuffer();
74
75     bool isEmpty() const;
76
77     void beginNewFrame();
78     int numFrames() const;
79
80     void draw(QPainter *painter, int frame = 0) const;
81
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;
87 #endif
88
89     void setBoundingRect(const QRectF &rect);
90     QRectF boundingRect() const;
91
92     virtual QPaintEngine *paintEngine() const;
93     virtual int metric(PaintDeviceMetric m) const;
94     virtual int devType() const;
95
96     QPaintBuffer &operator=(const QPaintBuffer &other);
97
98 private:
99     friend class QPainterReplayer;
100     friend class QOpenGLReplayer;
101
102     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
103     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
104
105     QPaintBufferPrivate *d_ptr;
106 };
107
108 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
109 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
110
111 class QPaintBufferEngine;
112
113 class QTextItemIntCopy
114 {
115 public:
116     QTextItemIntCopy(const QTextItem &item);
117     ~QTextItemIntCopy();
118     QTextItemInt &operator () () {return m_item;}
119 private:
120     QTextItemInt m_item;
121     QFont m_font;
122 };
123
124 struct QPaintBufferCommand
125 {
126     uint id : 8;
127     uint size : 24;
128
129     int offset;
130     int offset2;
131     int extra;
132 };
133
134 QDataStream &operator<<(QDataStream &stream, const QPaintBufferCommand &command);
135 QDataStream &operator>>(QDataStream &stream, QPaintBufferCommand &command);
136
137 Q_DECLARE_TYPEINFO(QPaintBufferCommand, Q_MOVABLE_TYPE);
138
139 class QPaintBufferPrivate
140 {
141 public:
142     enum Command {
143         Cmd_Save,
144         Cmd_Restore,
145
146         Cmd_SetBrush,
147         Cmd_SetBrushOrigin,
148         Cmd_SetClipEnabled,
149         Cmd_SetCompositionMode,
150         Cmd_SetOpacity,
151         Cmd_SetPen,
152         Cmd_SetRenderHints,
153         Cmd_SetTransform,
154         Cmd_SetBackgroundMode,
155
156         Cmd_ClipPath,
157         Cmd_ClipRect,
158         Cmd_ClipRegion,
159         Cmd_ClipVectorPath,
160
161         Cmd_DrawVectorPath,
162         Cmd_FillVectorPath,
163         Cmd_StrokeVectorPath,
164
165         Cmd_DrawConvexPolygonF,
166         Cmd_DrawConvexPolygonI,
167         Cmd_DrawEllipseF,
168         Cmd_DrawEllipseI,
169         Cmd_DrawLineF,
170         Cmd_DrawLineI,
171         Cmd_DrawPath,
172         Cmd_DrawPointsF,
173         Cmd_DrawPointsI,
174         Cmd_DrawPolygonF,
175         Cmd_DrawPolygonI,
176         Cmd_DrawPolylineF,
177         Cmd_DrawPolylineI,
178         Cmd_DrawRectF,
179         Cmd_DrawRectI,
180
181         Cmd_FillRectBrush,
182         Cmd_FillRectColor,
183
184         Cmd_DrawText,
185         Cmd_DrawTextItem,
186
187         Cmd_DrawImagePos,
188         Cmd_DrawImageRect,
189         Cmd_DrawPixmapPos,
190         Cmd_DrawPixmapRect,
191         Cmd_DrawTiledPixmap,
192
193         Cmd_SystemStateChanged,
194         Cmd_Translate,
195         Cmd_DrawStaticText,
196
197         // new commands must be added above this line
198
199         Cmd_LastCommand
200     };
201
202     QPaintBufferPrivate();
203     ~QPaintBufferPrivate();
204
205     int addData(const int *data, int count) {
206         if (count <= 0)
207             return 0;
208         int pos = ints.size();
209         ints.resize(pos + count);
210         memcpy(ints.data() + pos, data, count * sizeof(int));
211         return pos;
212     }
213
214     int addData(const qreal *data, int count) {
215         if (count <= 0)
216             return 0;
217         int pos = floats.size();
218         floats.resize(pos + count);
219         memcpy(floats.data() + pos, data, count * sizeof(qreal));
220         return pos;
221     }
222
223     int addData(const QVariant &var) {
224         variants << var;
225         return variants.size() - 1;
226     }
227
228     QPaintBufferCommand *addCommand(Command command) {
229         QPaintBufferCommand cmd;
230         cmd.id = command;
231         cmd.size = cmd.offset = cmd.offset2 = cmd.extra = 0;
232         commands << cmd;
233         return &commands.last();
234     }
235
236     QPaintBufferCommand *addCommand(Command command, const QVariant &var) {
237         QPaintBufferCommand cmd;
238         cmd.id = command;
239         cmd.offset = addData(var);
240         cmd.size = cmd.offset2 = cmd.extra = 0;
241         commands << cmd;
242         return &commands.last();
243     }
244
245     QPaintBufferCommand *addCommand(Command command, const QVectorPath &path) {
246         QPaintBufferCommand cmd;
247         cmd.id = command;
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'.
252         if (path.elements())
253             addData((const int *) path.elements(), path.elementCount());
254         else
255             cmd.offset2 |= 0x80000000;
256         cmd.size = path.elementCount();
257         cmd.extra = 0;
258         commands << cmd;
259         return &commands.last();
260     }
261
262     QPaintBufferCommand *addCommand(Command command , const qreal *pts, int arrayLength, int elementCount) {
263         QPaintBufferCommand cmd;
264         cmd.id = command;
265         cmd.offset = addData(pts, arrayLength);
266         cmd.size = elementCount;
267         cmd.offset2 = cmd.extra = 0;
268         commands << cmd;
269         return &commands.last();
270     }
271
272     QPaintBufferCommand *addCommand(Command command , const int *pts, int arrayLength, int elementCount) {
273         QPaintBufferCommand cmd;
274         cmd.id = command;
275         cmd.offset = addData(pts, arrayLength);
276         cmd.size = elementCount;
277         cmd.offset2 = cmd.extra = 0;
278         commands << cmd;
279         return &commands.last();
280     }
281
282     inline void updateBoundingRect(const QRectF &rect);
283
284     QAtomicInt ref;
285
286     QVector<int> ints;
287     QVector<qreal> floats;
288     QVector<QVariant> variants;
289
290     QVector<QPaintBufferCommand> commands;
291     QList<int> frames;
292
293     QPaintBufferEngine *engine;
294     QRectF boundingRect;
295     qreal penWidthAdjustment;
296     uint calculateBoundingRect : 1;
297
298     void *cache;
299 };
300
301
302 struct QVectorPathCmd
303 {
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,
307                      cmd.size,
308                      cmd.offset2 & 0x80000000
309                      ? 0
310                      : (const QPainterPath::ElementType *) (d->ints.constData() + cmd.offset2 + 1),
311                      *(d->ints.constData() + (cmd.offset2 & 0x7fffffff))) {}
312
313     inline const QVectorPath &operator()() const { return vectorPath; }
314
315     QVectorPath vectorPath;
316 };
317
318
319 class Q_GUI_EXPORT QPainterReplayer
320 {
321 public:
322     QPainterReplayer() { }
323
324     virtual ~QPainterReplayer() { }
325
326     void setupTransform(QPainter *painter);
327     virtual void process(const QPaintBufferCommand &cmd);
328     void processCommands(const QPaintBuffer &buffer, QPainter *painter, int begin, int end);
329
330 protected:
331     QPaintBufferPrivate *d;
332     QTransform m_world_matrix;
333
334     QPainter *painter;
335 };
336
337 class Q_GUI_EXPORT QPaintEngineExReplayer : public QPainterReplayer
338 {
339 public:
340     QPaintEngineExReplayer() { }
341
342     virtual void process(const QPaintBufferCommand &cmd);
343 };
344
345 class QPaintBufferEnginePrivate;
346
347 class QPaintBufferEngine : public QPaintEngineEx
348 {
349     Q_DECLARE_PRIVATE(QPaintBufferEngine)
350 public:
351     QPaintBufferEngine(QPaintBufferPrivate *buffer);
352
353     virtual bool begin(QPaintDevice *device);
354     virtual bool end();
355
356     virtual Type type() const { return QPaintEngine::PaintBuffer; }
357
358     virtual QPainterState *createState(QPainterState *orig) const;
359
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);
363
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 &region, Qt::ClipOperation op);
367     virtual void clip(const QPainterPath &path, Qt::ClipOperation op);
368
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();
378
379     virtual void fillRect(const QRectF &rect, const QBrush &brush);
380     virtual void fillRect(const QRectF &rect, const QColor &color);
381
382     virtual void drawRects(const QRect *rects, int rectCount);
383     virtual void drawRects(const QRectF *rects, int rectCount);
384
385     virtual void drawLines(const QLine *lines, int lineCount);
386     virtual void drawLines(const QLineF *lines, int lineCount);
387
388     virtual void drawEllipse(const QRectF &r);
389     virtual void drawEllipse(const QRect &r);
390
391     virtual void drawPath(const QPainterPath &path);
392
393     virtual void drawPoints(const QPointF *points, int pointCount);
394     virtual void drawPoints(const QPoint *points, int pointCount);
395
396     virtual void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
397     virtual void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode);
398
399     virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
400     virtual void drawPixmap(const QPointF &pos, const QPixmap &pm);
401
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);
405
406     virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s);
407
408     virtual void drawTextItem(const QPointF &pos, const QTextItem &ti);
409     virtual void drawStaticTextItem(QStaticTextItem *staticTextItem);
410
411     virtual void setState(QPainterState *s);
412     virtual uint flags() const {return QPaintEngineEx::DoNotEmulate;}
413
414     QPaintBufferPrivate *buffer;
415
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;
420
421     mutable QPainterState *m_created_state;
422 };
423
424 QT_END_NAMESPACE
425
426 #endif // QPAINTBUFFER_P_H