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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 strokePolygonCosmetic(const QPoint *pts, int pointCount, PolygonDrawMode mode);
200 void strokePolygonCosmetic(const QPointF *pt, int pointCount, PolygonDrawMode mode);
202 void clip(const QVectorPath &path, Qt::ClipOperation op);
203 void clip(const QRect &rect, Qt::ClipOperation op);
204 void clip(const QRegion ®ion, Qt::ClipOperation op);
206 void drawStaticTextItem(QStaticTextItem *textItem);
212 ClipType clipType() const;
213 QRect clipBoundingRect() const;
215 #ifdef Q_NO_USING_KEYWORD
216 inline void drawEllipse(const QRect &rect) { QPaintEngineEx::drawEllipse(rect); }
218 using QPaintEngineEx::drawPolygon;
219 using QPaintEngineEx::drawEllipse;
222 void releaseBuffer();
227 void saveBuffer(const QString &s) const;
231 void setCGContext(CGContextRef ref);
232 CGContextRef getCGContext() const;
238 void releaseDC(HDC hdc) const;
241 void alphaPenBlt(const void* src, int bpl, int depth, int rx,int ry,int w,int h);
243 Type type() const { return Raster; }
245 QPoint coordinateOffset() const;
247 #if defined(Q_WS_QWS) && !defined(QT_NO_RASTERCALLBACKS)
248 virtual void drawColorSpans(const QSpan *spans, int count, uint color);
249 virtual void drawBufferSpan(const uint *buffer, int bufsize,
250 int x, int y, int length, uint const_alpha);
254 QRasterPaintEngine(QRasterPaintEnginePrivate &d, QPaintDevice *);
256 friend struct QSpanData;
257 friend class QBlitterPaintEngine;
258 friend class QBlitterPaintEnginePrivate;
261 void fillRect(const QRectF &rect, QSpanData *data);
262 void drawBitmap(const QPointF &pos, const QImage &image, QSpanData *fill);
264 bool drawCachedGlyphs(int numGlyphs, const glyph_t *glyphs, const QFixedPoint *positions,
265 QFontEngine *fontEngine);
267 #if defined(Q_OS_SYMBIAN) && defined(QT_NO_FREETYPE)
268 void drawGlyphsS60(const QPointF &p, const QTextItemInt &ti);
269 #endif // Q_OS_SYMBIAN && QT_NO_FREETYPE
271 bool setClipRectInDeviceCoords(const QRect &r, Qt::ClipOperation op);
273 inline void ensureBrush(const QBrush &brush) {
274 if (!qbrush_fast_equals(state()->lastBrush, brush) || (brush.style() != Qt::NoBrush && state()->fillFlags))
277 inline void ensureBrush() { ensureBrush(state()->brush); }
279 inline void ensurePen(const QPen &pen) {
280 if (!qpen_fast_equals(state()->lastPen, pen) || (pen.style() != Qt::NoPen && state()->strokeFlags))
283 inline void ensurePen() { ensurePen(state()->pen); }
285 void updateOutlineMapper();
286 inline void ensureOutlineMapper();
289 inline void ensureState() {
296 /*******************************************************************************
297 * QRasterPaintEnginePrivate
303 QRasterPaintEnginePrivate : public QPaintEngineExPrivate
305 Q_DECLARE_PUBLIC(QRasterPaintEngine)
307 QRasterPaintEnginePrivate();
309 void rasterizeLine_dashed(QLineF line, qreal width,
310 int *dashIndex, qreal *dashOffset, bool *inDash);
311 void rasterize(QT_FT_Outline *outline, ProcessSpans callback, QSpanData *spanData, QRasterBuffer *rasterBuffer);
312 void rasterize(QT_FT_Outline *outline, ProcessSpans callback, void *userData, QRasterBuffer *rasterBuffer);
313 void updateMatrixData(QSpanData *spanData, const QBrush &brush, const QTransform &brushMatrix);
315 void systemStateChanged();
317 void drawImage(const QPointF &pt, const QImage &img, SrcOverBlendFunc func,
318 const QRect &clip, int alpha, const QRect &sr = QRect());
320 QTransform brushMatrix() const {
321 Q_Q(const QRasterPaintEngine);
322 const QRasterPaintEngineState *s = q->state();
323 QTransform m(s->matrix);
324 m.translate(s->brushOrigin.x(), s->brushOrigin.y());
328 bool isUnclipped_normalized(const QRect &rect) const;
329 bool isUnclipped(const QRect &rect, int penWidth) const;
330 bool isUnclipped(const QRectF &rect, int penWidth) const;
331 ProcessSpans getPenFunc(const QRect &rect, const QSpanData *data) const;
332 ProcessSpans getPenFunc(const QRectF &rect, const QSpanData *data) const;
333 ProcessSpans getBrushFunc(const QRect &rect, const QSpanData *data) const;
334 ProcessSpans getBrushFunc(const QRectF &rect, const QSpanData *data) const;
337 void prepare(QCustomRasterPaintDevice *);
340 inline const QClipData *clip() const;
342 void initializeRasterizer(QSpanData *data);
344 void recalculateFastImages();
345 bool canUseFastImageBlending(QPainter::CompositionMode mode, const QImage &image) const;
347 QPaintDevice *device;
348 QScopedPointer<QOutlineMapper> outlineMapper;
349 QScopedPointer<QRasterBuffer> rasterBuffer;
351 #if defined (Q_WS_WIN)
353 #elif defined(Q_WS_MAC)
354 CGContextRef cgContext;
359 QStroker basicStroker;
360 QScopedPointer<QDashStroker> dashStroker;
362 QScopedPointer<QT_FT_Raster> grayRaster;
364 QDataBuffer<QLineF> cachedLines;
365 QSpanData image_filler;
366 QSpanData image_filler_xform;
367 QSpanData solid_color_filler;
370 QFontEngineGlyphCache::Type glyphCacheType;
372 QScopedPointer<QClipData> baseClip;
376 uint mono_surface : 1;
377 uint outlinemapper_xform_dirty : 1;
380 uint isPlain45DegreeRotation : 1;
383 QScopedPointer<QRasterizer> rasterizer;
393 QClipData(int height);
404 inline ClipLine *clipLines() {
410 inline QSpan *spans() {
419 int xmin, xmax, ymin, ymax;
425 uint hasRectClip : 1;
426 uint hasRegionClip : 1;
428 void appendSpan(int x, int length, int y, int coverage);
429 void appendSpans(const QSpan *s, int num);
431 // ### Should optimize and actually kill the QSpans if the rect is
432 // ### a subset of The current region. Thus the "fast" clipspan
433 // ### callback can be used
434 void setClipRect(const QRect &rect);
435 void setClipRegion(const QRegion ®ion);
439 inline void QClipData::appendSpan(int x, int length, int y, int coverage)
441 Q_ASSERT(m_spans); // initialize() has to be called prior to adding spans..
443 if (count == allocated) {
445 m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
447 m_spans[count].x = x;
448 m_spans[count].len = length;
449 m_spans[count].y = y;
450 m_spans[count].coverage = coverage;
454 inline void QClipData::appendSpans(const QSpan *s, int num)
458 if (count + num > allocated) {
461 } while (count + num > allocated);
462 m_spans = (QSpan *)realloc(m_spans, allocated*sizeof(QSpan));
464 memcpy(m_spans+count, s, num*sizeof(QSpan));
469 class Q_GUI_EXPORT QCustomRasterPaintDevice : public QPaintDevice
472 QCustomRasterPaintDevice(QWidget *w) : widget(w) {}
474 int devType() const { return QInternal::CustomRaster; }
476 virtual int metric(PaintDeviceMetric m) const;
478 virtual void* memory() const { return 0; }
480 virtual QImage::Format format() const {
481 return QImage::Format_ARGB32_Premultiplied;
484 virtual int bytesPerLine() const;
486 virtual QSize size() const {
487 return static_cast<QRasterPaintEngine*>(paintEngine())->size();
495 /*******************************************************************************
505 QRasterBuffer() : m_width(0), m_height(0), m_buffer(0) { init(); }
511 QImage::Format prepare(QImage *image);
512 QImage::Format prepare(QPixmap *pix);
514 void prepare(QCustomRasterPaintDevice *device);
516 void prepare(int w, int h);
517 void prepareBuffer(int w, int h);
519 void resetBuffer(int val=0);
521 uchar *scanLine(int y) { Q_ASSERT(y>=0); Q_ASSERT(y<m_height); return m_buffer + y * bytes_per_line; }
524 QImage bufferImage() const;
527 void flushToARGBImage(QImage *image) const;
529 int width() const { return m_width; }
530 int height() const { return m_height; }
531 int bytesPerLine() const { return bytes_per_line; }
532 int bytesPerPixel() const { return bytes_per_pixel; }
534 uchar *buffer() const { return m_buffer; }
536 bool monoDestinationWithClut;
540 QPainter::CompositionMode compositionMode;
541 QImage::Format format;
542 DrawHelper *drawHelper;
543 QImage colorizeBitmap(const QImage &image, const QColor &color);
553 inline void QRasterPaintEngine::ensureOutlineMapper() {
554 if (d_func()->outlinemapper_xform_dirty)
555 updateOutlineMapper();
558 inline const QClipData *QRasterPaintEnginePrivate::clip() const {
559 Q_Q(const QRasterPaintEngine);
560 if (q->state() && q->state()->clip && q->state()->clip->enabled)
561 return q->state()->clip;
562 return baseClip.data();
567 #endif // QPAINTENGINE_RASTER_P_H