source: trunk/src/gui/painting/qpaintbuffer_p.h@ 617

Last change on this file since 617 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 13.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
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 [email protected].
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
62QT_BEGIN_NAMESPACE
63
64class QPaintBufferPrivate;
65class QPaintBufferPlayback;
66
67class Q_GUI_EXPORT QPaintBuffer : public QPaintDevice
68{
69 Q_DECLARE_PRIVATE(QPaintBuffer)
70public:
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 void setBoundingRect(const QRectF &rect);
82 QRectF boundingRect() const;
83
84 virtual QPaintEngine *paintEngine() const;
85 virtual int metric(PaintDeviceMetric m) const;
86 virtual int devType() const;
87
88 QPaintBuffer &operator=(const QPaintBuffer &other);
89
90private:
91 friend class QPainterReplayer;
92 friend class QOpenGLReplayer;
93
94 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
95 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
96
97 QPaintBufferPrivate *d_ptr;
98};
99
100Q_GUI_EXPORT QDataStream &operator<<(QDataStream &stream, const QPaintBuffer &buffer);
101Q_GUI_EXPORT QDataStream &operator>>(QDataStream &stream, QPaintBuffer &buffer);
102
103class QPaintBufferEngine;
104
105class QTextItemIntCopy
106{
107public:
108 QTextItemIntCopy(const QTextItem &item);
109 ~QTextItemIntCopy();
110 QTextItemInt &operator () () {return m_item;}
111private:
112 QTextItemInt m_item;
113 QFont m_font;
114};
115
116struct QPaintBufferCommand
117{
118 uint id : 8;
119 uint size : 24;
120
121 int offset;
122 int offset2;
123 int extra;
124};
125
126QDataStream &operator<<(QDataStream &stream, const QPaintBufferCommand &command);
127QDataStream &operator>>(QDataStream &stream, QPaintBufferCommand &command);
128
129Q_DECLARE_TYPEINFO(QPaintBufferCommand, Q_MOVABLE_TYPE);
130
131class QPaintBufferPrivate
132{
133public:
134 enum Command {
135 Cmd_Save,
136 Cmd_Restore,
137
138 Cmd_SetBrush,
139 Cmd_SetBrushOrigin,
140 Cmd_SetClipEnabled,
141 Cmd_SetCompositionMode,
142 Cmd_SetOpacity,
143 Cmd_SetPen,
144 Cmd_SetRenderHints,
145 Cmd_SetTransform,
146 Cmd_SetBackgroundMode,
147
148 Cmd_ClipPath,
149 Cmd_ClipRect,
150 Cmd_ClipRegion,
151 Cmd_ClipVectorPath,
152
153 Cmd_DrawVectorPath,
154 Cmd_FillVectorPath,
155 Cmd_StrokeVectorPath,
156
157 Cmd_DrawConvexPolygonF,
158 Cmd_DrawConvexPolygonI,
159 Cmd_DrawEllipseF,
160 Cmd_DrawEllipseI,
161 Cmd_DrawLineF,
162 Cmd_DrawLineI,
163 Cmd_DrawPath,
164 Cmd_DrawPointsF,
165 Cmd_DrawPointsI,
166 Cmd_DrawPolygonF,
167 Cmd_DrawPolygonI,
168 Cmd_DrawPolylineF,
169 Cmd_DrawPolylineI,
170 Cmd_DrawRectF,
171 Cmd_DrawRectI,
172
173 Cmd_FillRectBrush,
174 Cmd_FillRectColor,
175
176 Cmd_DrawText,
177 Cmd_DrawTextItem,
178
179 Cmd_DrawImagePos,
180 Cmd_DrawImageRect,
181 Cmd_DrawPixmapPos,
182 Cmd_DrawPixmapRect,
183 Cmd_DrawTiledPixmap,
184
185 Cmd_SystemStateChanged,
186
187 Cmd_LastCommand
188 };
189
190 QPaintBufferPrivate();
191 ~QPaintBufferPrivate();
192
193 int addData(const int *data, int count) {
194 if (count <= 0)
195 return 0;
196 int pos = ints.size();
197 ints.resize(pos + count);
198 memcpy(ints.data() + pos, data, count * sizeof(int));
199 return pos;
200 }
201
202 int addData(const qreal *data, int count) {
203 if (count <= 0)
204 return 0;
205 int pos = floats.size();
206 floats.resize(pos + count);
207 memcpy(floats.data() + pos, data, count * sizeof(qreal));
208 return pos;
209 }
210
211 int addData(const QVariant &var) {
212 variants << var;
213 return variants.size() - 1;
214 }
215
216 QPaintBufferCommand *addCommand(Command command) {
217 QPaintBufferCommand cmd;
218 cmd.id = command;
219 cmd.size = cmd.offset = cmd.offset2 = cmd.extra = 0;
220 commands << cmd;
221 return &commands.last();
222 }
223
224 QPaintBufferCommand *addCommand(Command command, const QVariant &var) {
225 QPaintBufferCommand cmd;
226 cmd.id = command;
227 cmd.offset = addData(var);
228 cmd.size = cmd.offset2 = cmd.extra = 0;
229 commands << cmd;
230 return &commands.last();
231 }
232
233 QPaintBufferCommand *addCommand(Command command, const QVectorPath &path) {
234 QPaintBufferCommand cmd;
235 cmd.id = command;
236 cmd.offset = addData(path.points(), path.elementCount() * 2);
237 cmd.offset2 = ints.size();
238 ints << path.hints();
239 // The absence of path elements is indicated by setting the highest bit in 'cmd.offset2'.
240 if (path.elements())
241 addData((const int *) path.elements(), path.elementCount());
242 else
243 cmd.offset2 |= 0x80000000;
244 cmd.size = path.elementCount();
245 cmd.extra = 0;
246 commands << cmd;
247 return &commands.last();
248 }
249
250 QPaintBufferCommand *addCommand(Command command , const qreal *pts, int arrayLength, int elementCount) {
251 QPaintBufferCommand cmd;
252 cmd.id = command;
253 cmd.offset = addData(pts, arrayLength);
254 cmd.size = elementCount;
255 cmd.offset2 = cmd.extra = 0;
256 commands << cmd;
257 return &commands.last();
258 }
259
260 QPaintBufferCommand *addCommand(Command command , const int *pts, int arrayLength, int elementCount) {
261 QPaintBufferCommand cmd;
262 cmd.id = command;
263 cmd.offset = addData(pts, arrayLength);
264 cmd.size = elementCount;
265 cmd.offset2 = cmd.extra = 0;
266 commands << cmd;
267 return &commands.last();
268 }
269
270 inline void updateBoundingRect(const QRectF &rect);
271
272 QAtomicInt ref;
273
274 QVector<int> ints;
275 QVector<qreal> floats;
276 QVector<QVariant> variants;
277
278 QVector<QPaintBufferCommand> commands;
279 QList<int> frames;
280
281 QPaintBufferEngine *engine;
282 QRectF boundingRect;
283 qreal penWidthAdjustment;
284 uint calculateBoundingRect : 1;
285
286 void *cache;
287};
288
289
290struct QVectorPathCmd
291{
292 // The absence of path elements is indicated by setting the highest bit in 'cmd.offset2'.
293 QVectorPathCmd(QPaintBufferPrivate *d, const QPaintBufferCommand &cmd)
294 : vectorPath(d->floats.constData() + cmd.offset,
295 cmd.size,
296 cmd.offset2 & 0x80000000
297 ? 0
298 : (const QPainterPath::ElementType *) (d->ints.constData() + cmd.offset2 + 1),
299 *(d->ints.constData() + (cmd.offset2 & 0x7fffffff))) {}
300
301 inline const QVectorPath &operator()() const { return vectorPath; }
302
303 QVectorPath vectorPath;
304};
305
306
307class Q_GUI_EXPORT QPainterReplayer
308{
309public:
310 QPainterReplayer() { }
311
312 virtual ~QPainterReplayer() { }
313
314 void setupTransform(QPainter *painter);
315 virtual void process(const QPaintBufferCommand &cmd);
316 void draw(const QPaintBuffer &buffer, QPainter *painter, int frame);
317
318protected:
319 QPaintBufferPrivate *d;
320 QTransform m_world_matrix;
321
322 QPainter *painter;
323};
324
325class Q_GUI_EXPORT QPaintEngineExReplayer : public QPainterReplayer
326{
327public:
328 QPaintEngineExReplayer() { }
329
330 virtual void process(const QPaintBufferCommand &cmd);
331};
332
333class QPaintBufferEnginePrivate;
334
335class QPaintBufferEngine : public QPaintEngineEx
336{
337 Q_DECLARE_PRIVATE(QPaintBufferEngine)
338public:
339 QPaintBufferEngine(QPaintBufferPrivate *buffer);
340
341 virtual bool begin(QPaintDevice *device);
342 virtual bool end();
343
344 virtual Type type() const { return QPaintEngine::PaintBuffer; }
345
346 virtual QPainterState *createState(QPainterState *orig) const;
347
348 virtual void draw(const QVectorPath &path);
349 virtual void fill(const QVectorPath &path, const QBrush &brush);
350 virtual void stroke(const QVectorPath &path, const QPen &pen);
351
352 virtual void clip(const QVectorPath &path, Qt::ClipOperation op);
353 virtual void clip(const QRect &rect, Qt::ClipOperation op);
354 virtual void clip(const QRegion &region, Qt::ClipOperation op);
355 virtual void clip(const QPainterPath &path, Qt::ClipOperation op);
356
357 virtual void clipEnabledChanged();
358 virtual void penChanged();
359 virtual void brushChanged();
360 virtual void brushOriginChanged();
361 virtual void opacityChanged();
362 virtual void compositionModeChanged();
363 virtual void renderHintsChanged();
364 virtual void transformChanged();
365 virtual void backgroundModeChanged();
366
367 virtual void fillRect(const QRectF &rect, const QBrush &brush);
368 virtual void fillRect(const QRectF &rect, const QColor &color);
369
370 virtual void drawRects(const QRect *rects, int rectCount);
371 virtual void drawRects(const QRectF *rects, int rectCount);
372
373 virtual void drawLines(const QLine *lines, int lineCount);
374 virtual void drawLines(const QLineF *lines, int lineCount);
375
376 virtual void drawEllipse(const QRectF &r);
377 virtual void drawEllipse(const QRect &r);
378
379 virtual void drawPath(const QPainterPath &path);
380
381 virtual void drawPoints(const QPointF *points, int pointCount);
382 virtual void drawPoints(const QPoint *points, int pointCount);
383
384 virtual void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
385 virtual void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode);
386
387 virtual void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
388 virtual void drawPixmap(const QPointF &pos, const QPixmap &pm);
389
390 virtual void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
391 Qt::ImageConversionFlags flags = Qt::AutoColor);
392 virtual void drawImage(const QPointF &pos, const QImage &image);
393
394 virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s);
395
396 virtual void drawTextItem(const QPointF &pos, const QTextItem &ti);
397
398 virtual void setState(QPainterState *s);
399 virtual uint flags() const {return QPaintEngineEx::DoNotEmulate;}
400
401 QPaintBufferPrivate *buffer;
402
403 mutable int m_begin_detected : 1;
404 mutable int m_save_detected : 1;
405 mutable int m_stream_raw_text_items : 1;
406 mutable int m_unused : 29;
407
408 mutable QPainterState *m_created_state;
409};
410
411class Q_GUI_EXPORT QPaintBufferSignalProxy : public QObject
412{
413 Q_OBJECT
414public:
415 QPaintBufferSignalProxy() : QObject() {}
416 void emitAboutToDestroy(const QPaintBufferPrivate *buffer) {
417 emit aboutToDestroy(buffer);
418 }
419 static QPaintBufferSignalProxy *instance();
420Q_SIGNALS:
421 void aboutToDestroy(const QPaintBufferPrivate *buffer);
422};
423
424// One resource per paint buffer and vice versa.
425class Q_GUI_EXPORT QPaintBufferResource : public QObject
426{
427 Q_OBJECT
428public:
429 typedef void (*FreeFunc)(void *);
430
431 QPaintBufferResource(FreeFunc f, QObject *parent = 0);
432 ~QPaintBufferResource();
433 // Set resource 'value' for 'key'.
434 void insert(const QPaintBufferPrivate *key, void *value);
435 // Return resource for 'key'.
436 void *value(const QPaintBufferPrivate *key);
437public slots:
438 // Remove entry 'key' from cache and delete resource.
439 void remove(const QPaintBufferPrivate *key);
440private:
441 typedef QHash<const QPaintBufferPrivate *, void *> Cache;
442 Cache m_cache;
443 FreeFunc free;
444};
445
446QT_END_NAMESPACE
447
448#endif // QPAINTBUFFER_P_H
Note: See TracBrowser for help on using the repository browser.