Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qpaintengine_raster_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** 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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QPAINTENGINE_RASTER_P_H
43 #define QPAINTENGINE_RASTER_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 for the convenience
50 // of other Qt classes.  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 "private/qpaintengineex_p.h"
57 #include "QtGui/qpainterpath.h"
58 #include "private/qdatabuffer_p.h"
59 #include "private/qdrawhelper_p.h"
60 #include "private/qpaintengine_p.h"
61 #include "private/qrasterizer_p.h"
62 #include "private/qstroker_p.h"
63 #include "private/qpainter_p.h"
64 #include "private/qtextureglyphcache_p.h"
65 #include "private/qoutlinemapper_p.h"
66
67 #include <stdlib.h>
68
69 QT_BEGIN_NAMESPACE
70
71 class QOutlineMapper;
72 class QRasterPaintEnginePrivate;
73 class QRasterBuffer;
74 class QClipData;
75 class QCustomRasterPaintDevice;
76
77 class QRasterPaintEngineState : public QPainterState
78 {
79 public:
80     QRasterPaintEngineState(QRasterPaintEngineState &other);
81     QRasterPaintEngineState();
82     ~QRasterPaintEngineState();
83
84
85     QPen lastPen;
86     QSpanData penData;
87     QStrokerOps *stroker;
88     uint strokeFlags;
89
90     QBrush lastBrush;
91     QSpanData brushData;
92     uint fillFlags;
93
94     uint pixmapFlags;
95     int intOpacity;
96
97     qreal txscale;
98
99     QClipData *clip;
100 //     QRect clipRect;
101 //     QRegion clipRegion;
102
103 //     QPainter::RenderHints hints;
104 //     QPainter::CompositionMode compositionMode;
105
106     uint dirty;
107
108     struct Flags {
109         uint has_clip_ownership : 1;        // should delete the clip member..
110         uint fast_pen : 1;                  // cosmetic 1-width pens, using midpoint drawlines
111         uint non_complex_pen : 1;           // can use rasterizer, rather than stroker
112         uint antialiased : 1;
113         uint bilinear : 1;
114         uint fast_text : 1;
115         uint int_xform : 1;
116         uint tx_noshear : 1;
117         uint fast_images : 1;
118     };
119
120     union {
121         Flags flags;
122         uint flag_bits;
123     };
124 };
125
126
127
128
129 /*******************************************************************************
130  * QRasterPaintEngine
131  */
132 class
133 #ifdef Q_WS_QWS
134 Q_GUI_EXPORT
135 #endif
136 QRasterPaintEngine : public QPaintEngineEx
137 {
138     Q_DECLARE_PRIVATE(QRasterPaintEngine)
139 public:
140
141     QRasterPaintEngine(QPaintDevice *device);
142     ~QRasterPaintEngine();
143     bool begin(QPaintDevice *device);
144     bool end();
145
146     void penChanged();
147     void brushChanged();
148     void brushOriginChanged();
149     void opacityChanged();
150     void compositionModeChanged();
151     void renderHintsChanged();
152     void transformChanged();
153     void clipEnabledChanged();
154
155     void setState(QPainterState *s);
156     QPainterState *createState(QPainterState *orig) const;
157     inline QRasterPaintEngineState *state() {
158         return static_cast<QRasterPaintEngineState *>(QPaintEngineEx::state());
159     }
160     inline const QRasterPaintEngineState *state() const {
161         return static_cast<const QRasterPaintEngineState *>(QPaintEngineEx::state());
162     }
163
164     void updateBrush(const QBrush &brush);
165     void updatePen(const QPen &pen);
166
167     void updateMatrix(const QTransform &matrix);
168
169     void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
170     void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode);
171     void fillPath(const QPainterPath &path, QSpanData *fillData);
172     void fillPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode);
173
174     void drawEllipse(const QRectF &rect);
175
176     void fillRect(const QRectF &rect, const QBrush &brush);
177     void fillRect(const QRectF &rect, const QColor &color);
178
179     void drawRects(const QRect  *rects, int rectCount);
180     void drawRects(const QRectF *rects, int rectCount);
181
182     void drawPixmap(const QPointF &p, const QPixmap &pm);
183     void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr);
184     void drawImage(const QPointF &p, const QImage &img);
185     void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr,
186                    Qt::ImageConversionFlags falgs = Qt::AutoColor);
187     void drawTiledPixmap(const QRectF &r, const QPixmap &pm, const QPointF &sr);
188     void drawTextItem(const QPointF &p, const QTextItem &textItem);
189
190     void drawLines(const QLine *line, int lineCount);
191     void drawLines(const QLineF *line, int lineCount);
192
193     void drawPoints(const QPointF *points, int pointCount);
194     void drawPoints(const QPoint *points, int pointCount);
195
196     void stroke(const QVectorPath &path, const QPen &pen);
197     void fill(const QVectorPath &path, const QBrush &brush);
198
199     void clip(const QVectorPath &path, Qt::ClipOperation op);
200     void clip(const QRect &rect, Qt::ClipOperation op);
201     void clip(const QRegion &region, Qt::ClipOperation op);
202
203     void drawStaticTextItem(QStaticTextItem *textItem);
204
205     enum ClipType {
206         RectClip,
207         ComplexClip
208     };
209     ClipType clipType() const;
210     QRect clipBoundingRect() const;
211
212 #ifdef Q_NO_USING_KEYWORD
213     inline void drawEllipse(const QRect &rect) { QPaintEngineEx::drawEllipse(rect); }
214 #else
215     using QPaintEngineEx::drawPolygon;
216     using QPaintEngineEx::drawEllipse;
217 #endif
218
219     void releaseBuffer();
220
221     QSize size() const;
222
223 #ifndef QT_NO_DEBUG
224     void saveBuffer(const QString &s) const;
225 #endif
226
227 #ifdef Q_WS_MAC
228     void setCGContext(CGContextRef ref);
229     CGContextRef getCGContext() const;
230 #endif
231
232 #ifdef Q_WS_WIN
233     void setDC(HDC hdc);
234     HDC getDC() const;
235     void releaseDC(HDC hdc) const;
236 #endif
237
238     void alphaPenBlt(const void* src, int bpl, int depth, int rx,int ry,int w,int h);
239
240     Type type() const { return Raster; }
241
242     QPoint coordinateOffset() const;
243
244 #if defined(Q_WS_QWS) && !defined(QT_NO_RASTERCALLBACKS)
245     virtual void drawColorSpans(const QSpan *spans, int count, uint color);
246     virtual void drawBufferSpan(const uint *buffer, int bufsize,
247                                 int x, int y, int length, uint const_alpha);
248 #endif
249     bool supportsTransformations(const QFontEngine *fontEngine) const;
250     bool supportsTransformations(qreal pixelSize, const QTransform &m) const;
251
252 protected:
253     QRasterPaintEngine(QRasterPaintEnginePrivate &d, QPaintDevice *);
254 private:
255     friend struct QSpanData;
256     friend class QBlitterPaintEngine;
257     friend class QBlitterPaintEnginePrivate;
258     void init();
259
260     void fillRect(const QRectF &rect, QSpanData *data);
261     void drawBitmap(const QPointF &pos, const QImage &image, QSpanData *fill);
262
263     bool drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, const QFixedPoint *positions,
264                           QFontEngine *fontEngine);
265
266 #if defined(Q_OS_SYMBIAN) && defined(QT_NO_FREETYPE)
267     void drawGlyphsS60(const QPointF &p, const QTextItemInt &ti);
268 #endif // Q_OS_SYMBIAN && QT_NO_FREETYPE
269
270     bool setClipRectInDeviceCoords(const QRect &r, Qt::ClipOperation op);
271
272     inline void ensureBrush(const QBrush &brush) {
273         if (!qbrush_fast_equals(state()->lastBrush, brush) || (brush.style() != Qt::NoBrush && state()->fillFlags))
274             updateBrush(brush);
275     }
276     inline void ensureBrush() { ensureBrush(state()->brush); }
277
278     inline void ensurePen(const QPen &pen) {
279         if (!qpen_fast_equals(state()->lastPen, pen) || (pen.style() != Qt::NoPen && state()->strokeFlags))
280             updatePen(pen);
281     }
282     inline void ensurePen() { ensurePen(state()->pen); }
283
284     void updateOutlineMapper();
285     inline void ensureOutlineMapper();
286
287     void updateState();
288     inline void ensureState() {
289         if (state()->dirty)
290             updateState();
291     }
292 };
293
294
295 /*******************************************************************************
296  * QRasterPaintEnginePrivate
297  */
298 class
299 #ifdef Q_WS_QWS
300 Q_GUI_EXPORT
301 #endif
302 QRasterPaintEnginePrivate : public QPaintEngineExPrivate
303 {
304     Q_DECLARE_PUBLIC(QRasterPaintEngine)
305 public:
306     QRasterPaintEnginePrivate();
307
308     void rasterizeLine_dashed(QLineF line, qreal width,
309                               int *dashIndex, qreal *dashOffset, bool *inDash);
310     void rasterize(QT_FT_Outline *outline, ProcessSpans callback, QSpanData *spanData, QRasterBuffer *rasterBuffer);
311     void rasterize(QT_FT_Outline *outline, ProcessSpans callback, void *userData, QRasterBuffer *rasterBuffer);
312     void updateMatrixData(QSpanData *spanData, const QBrush &brush, const QTransform &brushMatrix);
313
314     void systemStateChanged();
315
316     void drawImage(const QPointF &pt, const QImage &img, SrcOverBlendFunc func,
317                    const QRect &clip, int alpha, const QRect &sr = QRect());
318
319     QTransform brushMatrix() const {
320         Q_Q(const QRasterPaintEngine);
321         const QRasterPaintEngineState *s = q->state();
322         QTransform m(s->matrix);
323         m.translate(s->brushOrigin.x(), s->brushOrigin.y());
324         return m;
325     }
326
327     bool isUnclipped_normalized(const QRect &rect) const;
328     bool isUnclipped(const QRect &rect, int penWidth) const;
329     bool isUnclipped(const QRectF &rect, int penWidth) const;
330     ProcessSpans getPenFunc(const QRectF &rect, const QSpanData *data) const;
331     ProcessSpans getBrushFunc(const QRect &rect, const QSpanData *data) const;
332     ProcessSpans getBrushFunc(const QRectF &rect, const QSpanData *data) const;
333
334 #ifdef Q_WS_QWS
335     void prepare(QCustomRasterPaintDevice *);
336 #endif
337
338     inline const QClipData *clip() const;
339
340     void initializeRasterizer(QSpanData *data);
341
342     void recalculateFastImages();
343     bool canUseFastImageBlending(QPainter::CompositionMode mode, const QImage &image) const;
344
345     QPaintDevice *device;
346     QScopedPointer<QOutlineMapper> outlineMapper;
347     QScopedPointer<QRasterBuffer>  rasterBuffer;
348
349 #if defined (Q_WS_WIN)
350     HDC hdc;
351 #elif defined(Q_WS_MAC)
352     CGContextRef cgContext;
353 #endif
354
355     QRect deviceRect;
356
357     QStroker basicStroker;
358     QScopedPointer<QDashStroker> dashStroker;
359
360     QScopedPointer<QT_FT_Raster> grayRaster;
361
362     QDataBuffer<QLineF> cachedLines;
363     QSpanData image_filler;
364     QSpanData image_filler_xform;
365     QSpanData solid_color_filler;
366
367
368     QFontEngineGlyphCache::Type glyphCacheType;
369
370     QScopedPointer<QClipData> baseClip;
371
372     int deviceDepth;
373
374     uint mono_surface : 1;
375     uint outlinemapper_xform_dirty : 1;
376
377     QScopedPointer<QRasterizer> rasterizer;
378 };
379
380
381 class
382 #ifdef Q_WS_QWS
383 Q_GUI_EXPORT
384 #endif
385 QClipData {
386 public:
387     QClipData(int height);
388     ~QClipData();
389
390     int clipSpanHeight;
391     struct ClipLine {
392         int count;
393         QSpan *spans;
394     } *m_clipLines;
395
396     void initialize();
397
398     inline ClipLine *clipLines() {
399         if (!m_clipLines)
400             initialize();
401         return m_clipLines;
402     }
403
404     inline QSpan *spans() {
405         if (!m_spans)
406             initialize();
407         return m_spans;
408     }
409
410     int allocated;
411     int count;
412     QSpan *m_spans;
413     int xmin, xmax, ymin, ymax;
414
415     QRect clipRect;
416     QRegion clipRegion;
417
418     uint enabled : 1;
419     uint hasRectClip : 1;
420     uint hasRegionClip : 1;
421
422     void appendSpan(int x, int length, int y, int coverage);
423     void appendSpans(const QSpan *s, int num);
424
425     // ### Should optimize and actually kill the QSpans if the rect is
426     // ### a subset of The current region. Thus the "fast" clipspan
427     // ### callback can be used
428     void setClipRect(const QRect &rect);
429     void setClipRegion(const QRegion &region);
430     void fixup();
431 };
432
433 inline void QClipData::appendSpan(int x, int length, int y, int coverage)
434 {
435     Q_ASSERT(m_spans); // initialize() has to be called prior to adding spans..
436
437     if (count == allocated) {
438         allocated *= 2;
439         m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
440     }
441     m_spans[count].x = x;
442     m_spans[count].len = length;
443     m_spans[count].y = y;
444     m_spans[count].coverage = coverage;
445     ++count;
446 }
447
448 inline void QClipData::appendSpans(const QSpan *s, int num)
449 {
450     Q_ASSERT(m_spans);
451
452     if (count + num > allocated) {
453         do {
454             allocated *= 2;
455         } while (count + num > allocated);
456         m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
457     }
458     memcpy(m_spans+count, s, num*sizeof(QSpan));
459     count += num;
460 }
461
462 #ifdef Q_WS_QWS
463 class Q_GUI_EXPORT QCustomRasterPaintDevice : public QPaintDevice
464 {
465 public:
466     QCustomRasterPaintDevice(QWidget *w) : widget(w) {}
467
468     int devType() const { return QInternal::CustomRaster; }
469
470     virtual int metric(PaintDeviceMetric m) const;
471
472     virtual void* memory() const { return 0; }
473
474     virtual QImage::Format format() const {
475         return QImage::Format_ARGB32_Premultiplied;
476     }
477
478     virtual int bytesPerLine() const;
479
480     virtual QSize size() const {
481         return static_cast<QRasterPaintEngine*>(paintEngine())->size();
482     }
483
484 private:
485     QWidget *widget;
486 };
487 #endif // Q_WS_QWS
488
489 /*******************************************************************************
490  * QRasterBuffer
491  */
492 class
493 #ifdef Q_WS_QWS
494 Q_GUI_EXPORT
495 #endif
496 QRasterBuffer
497 {
498 public:
499     QRasterBuffer() : m_width(0), m_height(0), m_buffer(0) { init(); }
500
501     ~QRasterBuffer();
502
503     void init();
504
505     QImage::Format prepare(QImage *image);
506     QImage::Format prepare(QPixmap *pix);
507 #ifdef Q_WS_QWS
508     void prepare(QCustomRasterPaintDevice *device);
509 #endif
510     void prepare(int w, int h);
511     void prepareBuffer(int w, int h);
512
513     void resetBuffer(int val=0);
514
515     uchar *scanLine(int y) { Q_ASSERT(y>=0); Q_ASSERT(y<m_height); return m_buffer + y * bytes_per_line; }
516
517 #ifndef QT_NO_DEBUG
518     QImage bufferImage() const;
519 #endif
520
521     void flushToARGBImage(QImage *image) const;
522
523     int width() const { return m_width; }
524     int height() const { return m_height; }
525     int bytesPerLine() const { return bytes_per_line; }
526     int bytesPerPixel() const { return bytes_per_pixel; }
527
528     uchar *buffer() const { return m_buffer; }
529
530     bool monoDestinationWithClut;
531     QRgb destColor0;
532     QRgb destColor1;
533
534     QPainter::CompositionMode compositionMode;
535     QImage::Format format;
536     DrawHelper *drawHelper;
537     QImage colorizeBitmap(const QImage &image, const QColor &color);
538
539 private:
540     int m_width;
541     int m_height;
542     int bytes_per_line;
543     int bytes_per_pixel;
544     uchar *m_buffer;
545 };
546
547 inline void QRasterPaintEngine::ensureOutlineMapper() {
548     if (d_func()->outlinemapper_xform_dirty)
549         updateOutlineMapper();
550 }
551
552 inline const QClipData *QRasterPaintEnginePrivate::clip() const {
553     Q_Q(const QRasterPaintEngine);
554     if (q->state() && q->state()->clip && q->state()->clip->enabled)
555         return q->state()->clip;
556     return baseClip.data();
557 }
558
559
560 QT_END_NAMESPACE
561 #endif // QPAINTENGINE_RASTER_P_H