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 QPAINTENGINE_RASTER_P_H
43 #define QPAINTENGINE_RASTER_P_H
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.
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"
72 class QRasterPaintEnginePrivate;
75 class QCustomRasterPaintDevice;
77 class QRasterPaintEngineState : public QPainterState
80 QRasterPaintEngineState(QRasterPaintEngineState &other);
81 QRasterPaintEngineState();
82 ~QRasterPaintEngineState();
101 // QRegion clipRegion;
103 // QPainter::RenderHints hints;
104 // QPainter::CompositionMode compositionMode;
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;
117 uint fast_images : 1;
129 /*******************************************************************************
136 QRasterPaintEngine : public QPaintEngineEx
138 Q_DECLARE_PRIVATE(QRasterPaintEngine)
141 QRasterPaintEngine(QPaintDevice *device);
142 ~QRasterPaintEngine();
143 bool begin(QPaintDevice *device);
148 void brushOriginChanged();
149 void opacityChanged();
150 void compositionModeChanged();
151 void renderHintsChanged();
152 void transformChanged();
153 void clipEnabledChanged();
155 void setState(QPainterState *s);
156 QPainterState *createState(QPainterState *orig) const;
157 inline QRasterPaintEngineState *state() {
158 return static_cast<QRasterPaintEngineState *>(QPaintEngineEx::state());
160 inline const QRasterPaintEngineState *state() const {
161 return static_cast<const QRasterPaintEngineState *>(QPaintEngineEx::state());
164 void updateBrush(const QBrush &brush);
165 void updatePen(const QPen &pen);
167 void updateMatrix(const QTransform &matrix);
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);
174 void drawEllipse(const QRectF &rect);
176 void fillRect(const QRectF &rect, const QBrush &brush);
177 void fillRect(const QRectF &rect, const QColor &color);
179 void drawRects(const QRect *rects, int rectCount);
180 void drawRects(const QRectF *rects, int rectCount);
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);
190 void drawLines(const QLine *line, int lineCount);
191 void drawLines(const QLineF *line, int lineCount);
193 void drawPoints(const QPointF *points, int pointCount);
194 void drawPoints(const QPoint *points, int pointCount);
196 void stroke(const QVectorPath &path, const QPen &pen);
197 void fill(const QVectorPath &path, const QBrush &brush);
199 void clip(const QVectorPath &path, Qt::ClipOperation op);
200 void clip(const QRect &rect, Qt::ClipOperation op);
201 void clip(const QRegion ®ion, Qt::ClipOperation op);
203 void drawStaticTextItem(QStaticTextItem *textItem);
209 ClipType clipType() const;
210 QRect clipBoundingRect() const;
212 #ifdef Q_NO_USING_KEYWORD
213 inline void drawEllipse(const QRect &rect) { QPaintEngineEx::drawEllipse(rect); }
215 using QPaintEngineEx::drawPolygon;
216 using QPaintEngineEx::drawEllipse;
219 void releaseBuffer();
224 void saveBuffer(const QString &s) const;
228 void setCGContext(CGContextRef ref);
229 CGContextRef getCGContext() const;
235 void releaseDC(HDC hdc) const;
238 void alphaPenBlt(const void* src, int bpl, int depth, int rx,int ry,int w,int h);
240 Type type() const { return Raster; }
242 QPoint coordinateOffset() const;
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);
249 bool supportsTransformations(const QFontEngine *fontEngine) const;
250 bool supportsTransformations(qreal pixelSize, const QTransform &m) const;
253 QRasterPaintEngine(QRasterPaintEnginePrivate &d, QPaintDevice *);
255 friend struct QSpanData;
256 friend class QBlitterPaintEngine;
257 friend class QBlitterPaintEnginePrivate;
260 void fillRect(const QRectF &rect, QSpanData *data);
261 void drawBitmap(const QPointF &pos, const QImage &image, QSpanData *fill);
263 bool drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, const QFixedPoint *positions,
264 QFontEngine *fontEngine);
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
270 bool setClipRectInDeviceCoords(const QRect &r, Qt::ClipOperation op);
272 inline void ensureBrush(const QBrush &brush) {
273 if (!qbrush_fast_equals(state()->lastBrush, brush) || (brush.style() != Qt::NoBrush && state()->fillFlags))
276 inline void ensureBrush() { ensureBrush(state()->brush); }
278 inline void ensurePen(const QPen &pen) {
279 if (!qpen_fast_equals(state()->lastPen, pen) || (pen.style() != Qt::NoPen && state()->strokeFlags))
282 inline void ensurePen() { ensurePen(state()->pen); }
284 void updateOutlineMapper();
285 inline void ensureOutlineMapper();
288 inline void ensureState() {
295 /*******************************************************************************
296 * QRasterPaintEnginePrivate
302 QRasterPaintEnginePrivate : public QPaintEngineExPrivate
304 Q_DECLARE_PUBLIC(QRasterPaintEngine)
306 QRasterPaintEnginePrivate();
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);
314 void systemStateChanged();
316 void drawImage(const QPointF &pt, const QImage &img, SrcOverBlendFunc func,
317 const QRect &clip, int alpha, const QRect &sr = QRect());
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());
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;
335 void prepare(QCustomRasterPaintDevice *);
338 inline const QClipData *clip() const;
340 void initializeRasterizer(QSpanData *data);
342 void recalculateFastImages();
343 bool canUseFastImageBlending(QPainter::CompositionMode mode, const QImage &image) const;
345 QPaintDevice *device;
346 QScopedPointer<QOutlineMapper> outlineMapper;
347 QScopedPointer<QRasterBuffer> rasterBuffer;
349 #if defined (Q_WS_WIN)
351 #elif defined(Q_WS_MAC)
352 CGContextRef cgContext;
357 QStroker basicStroker;
358 QScopedPointer<QDashStroker> dashStroker;
360 QScopedPointer<QT_FT_Raster> grayRaster;
362 QDataBuffer<QLineF> cachedLines;
363 QSpanData image_filler;
364 QSpanData image_filler_xform;
365 QSpanData solid_color_filler;
368 QFontEngineGlyphCache::Type glyphCacheType;
370 QScopedPointer<QClipData> baseClip;
374 uint mono_surface : 1;
375 uint outlinemapper_xform_dirty : 1;
377 QScopedPointer<QRasterizer> rasterizer;
387 QClipData(int height);
398 inline ClipLine *clipLines() {
404 inline QSpan *spans() {
413 int xmin, xmax, ymin, ymax;
419 uint hasRectClip : 1;
420 uint hasRegionClip : 1;
422 void appendSpan(int x, int length, int y, int coverage);
423 void appendSpans(const QSpan *s, int num);
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 ®ion);
433 inline void QClipData::appendSpan(int x, int length, int y, int coverage)
435 Q_ASSERT(m_spans); // initialize() has to be called prior to adding spans..
437 if (count == allocated) {
439 m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
441 m_spans[count].x = x;
442 m_spans[count].len = length;
443 m_spans[count].y = y;
444 m_spans[count].coverage = coverage;
448 inline void QClipData::appendSpans(const QSpan *s, int num)
452 if (count + num > allocated) {
455 } while (count + num > allocated);
456 m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
458 memcpy(m_spans+count, s, num*sizeof(QSpan));
463 class Q_GUI_EXPORT QCustomRasterPaintDevice : public QPaintDevice
466 QCustomRasterPaintDevice(QWidget *w) : widget(w) {}
468 int devType() const { return QInternal::CustomRaster; }
470 virtual int metric(PaintDeviceMetric m) const;
472 virtual void* memory() const { return 0; }
474 virtual QImage::Format format() const {
475 return QImage::Format_ARGB32_Premultiplied;
478 virtual int bytesPerLine() const;
480 virtual QSize size() const {
481 return static_cast<QRasterPaintEngine*>(paintEngine())->size();
489 /*******************************************************************************
499 QRasterBuffer() : m_width(0), m_height(0), m_buffer(0) { init(); }
505 QImage::Format prepare(QImage *image);
506 QImage::Format prepare(QPixmap *pix);
508 void prepare(QCustomRasterPaintDevice *device);
510 void prepare(int w, int h);
511 void prepareBuffer(int w, int h);
513 void resetBuffer(int val=0);
515 uchar *scanLine(int y) { Q_ASSERT(y>=0); Q_ASSERT(y<m_height); return m_buffer + y * bytes_per_line; }
518 QImage bufferImage() const;
521 void flushToARGBImage(QImage *image) const;
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; }
528 uchar *buffer() const { return m_buffer; }
530 bool monoDestinationWithClut;
534 QPainter::CompositionMode compositionMode;
535 QImage::Format format;
536 DrawHelper *drawHelper;
537 QImage colorizeBitmap(const QImage &image, const QColor &color);
547 inline void QRasterPaintEngine::ensureOutlineMapper() {
548 if (d_func()->outlinemapper_xform_dirty)
549 updateOutlineMapper();
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();
561 #endif // QPAINTENGINE_RASTER_P_H