src/gui/painting/qpaintengine_x11.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** 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
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "qplatformdefs.h"
       
    43 
       
    44 #include "private/qpixmap_x11_p.h"
       
    45 
       
    46 #include "qapplication.h"
       
    47 #include "qdebug.h"
       
    48 #include "qfont.h"
       
    49 #include "qwidget.h"
       
    50 #include "qbitmap.h"
       
    51 #include "qpixmapcache.h"
       
    52 #include "qtextcodec.h"
       
    53 #include "qcoreevent.h"
       
    54 #include "qiodevice.h"
       
    55 #include <qmath.h>
       
    56 
       
    57 #include "qpainter_p.h"
       
    58 #include <qtextlayout.h>
       
    59 #include <qvarlengtharray.h>
       
    60 #include <private/qfont_p.h>
       
    61 #include <private/qtextengine_p.h>
       
    62 #include <private/qpaintengine_x11_p.h>
       
    63 #include <private/qfontengine_x11_p.h>
       
    64 #include <private/qwidget_p.h>
       
    65 #include <private/qpainterpath_p.h>
       
    66 
       
    67 #include "qpen.h"
       
    68 #include "qcolor.h"
       
    69 #include "qcolormap.h"
       
    70 
       
    71 #include <private/qpaintengine_p.h>
       
    72 #include "qpaintengine_x11_p.h"
       
    73 
       
    74 #include <private/qt_x11_p.h>
       
    75 #include <private/qnumeric_p.h>
       
    76 #include <limits.h>
       
    77 
       
    78 #ifndef QT_NO_XRENDER
       
    79 #include <private/qtessellator_p.h>
       
    80 #endif
       
    81 
       
    82 QT_BEGIN_NAMESPACE
       
    83 
       
    84 extern Drawable qt_x11Handle(const QPaintDevice *pd);
       
    85 extern const QX11Info *qt_x11Info(const QPaintDevice *pd);
       
    86 extern QPixmap qt_pixmapForBrush(int brushStyle, bool invert); //in qbrush.cpp
       
    87 extern QPixmap qt_toX11Pixmap(const QPixmap &pixmap);
       
    88 
       
    89 // use the same rounding as in qrasterizer.cpp (6 bit fixed point)
       
    90 static const qreal aliasedCoordinateDelta = 0.5 - 0.015625;
       
    91 
       
    92 #undef X11 // defined in qt_x11_p.h
       
    93 /*!
       
    94     Returns the X11 specific pen GC for the painter \a p. Note that
       
    95     QPainter::begin() must be called before this function returns a
       
    96     valid GC.
       
    97 */
       
    98 Q_GUI_EXPORT GC qt_x11_get_pen_gc(QPainter *p)
       
    99 {
       
   100     if (p && p->paintEngine()
       
   101         && p->paintEngine()->isActive()
       
   102         && p->paintEngine()->type() == QPaintEngine::X11) {
       
   103         return static_cast<QX11PaintEngine *>(p->paintEngine())->d_func()->gc;
       
   104     }
       
   105     return 0;
       
   106 }
       
   107 
       
   108 /*!
       
   109     Returns the X11 specific brush GC for the painter \a p. Note that
       
   110     QPainter::begin() must be called before this function returns a
       
   111     valid GC.
       
   112 */
       
   113 Q_GUI_EXPORT GC qt_x11_get_brush_gc(QPainter *p)
       
   114 {
       
   115     if (p && p->paintEngine()
       
   116         && p->paintEngine()->isActive()
       
   117         && p->paintEngine()->type() == QPaintEngine::X11) {
       
   118         return static_cast<QX11PaintEngine *>(p->paintEngine())->d_func()->gc_brush;
       
   119     }
       
   120     return 0;
       
   121 }
       
   122 #define X11 qt_x11Data
       
   123 
       
   124 #ifndef QT_NO_XRENDER
       
   125 static const int compositionModeToRenderOp[QPainter::CompositionMode_Xor + 1] = {
       
   126     PictOpOver, //CompositionMode_SourceOver,
       
   127     PictOpOverReverse, //CompositionMode_DestinationOver,
       
   128     PictOpClear, //CompositionMode_Clear,
       
   129     PictOpSrc, //CompositionMode_Source,
       
   130     PictOpDst, //CompositionMode_Destination,
       
   131     PictOpIn, //CompositionMode_SourceIn,
       
   132     PictOpInReverse, //CompositionMode_DestinationIn,
       
   133     PictOpOut, //CompositionMode_SourceOut,
       
   134     PictOpOutReverse, //CompositionMode_DestinationOut,
       
   135     PictOpAtop, //CompositionMode_SourceAtop,
       
   136     PictOpAtopReverse, //CompositionMode_DestinationAtop,
       
   137     PictOpXor //CompositionMode_Xor
       
   138 };
       
   139 
       
   140 static inline int qpainterOpToXrender(QPainter::CompositionMode mode)
       
   141 {
       
   142     Q_ASSERT(mode <= QPainter::CompositionMode_Xor);
       
   143     return compositionModeToRenderOp[mode];
       
   144 }
       
   145 #endif
       
   146 
       
   147 // hack, so we don't have to make QRegion::clipRectangles() public or include
       
   148 // X11 headers in qregion.h
       
   149 Q_AUTOTEST_EXPORT void *qt_getClipRects(const QRegion &r, int &num)
       
   150 {
       
   151     return r.clipRectangles(num);
       
   152 }
       
   153 
       
   154 static inline void x11SetClipRegion(Display *dpy, GC gc, GC gc2,
       
   155 #ifndef QT_NO_XRENDER
       
   156                                     Picture picture,
       
   157 #else
       
   158                                     Qt::HANDLE picture,
       
   159 #endif
       
   160                                     const QRegion &r)
       
   161 {
       
   162     int num;
       
   163     XRectangle *rects = (XRectangle *)qt_getClipRects(r, num);
       
   164 
       
   165     if (gc)
       
   166         XSetClipRectangles( dpy, gc, 0, 0, rects, num, YXBanded );
       
   167     if (gc2)
       
   168         XSetClipRectangles( dpy, gc2, 0, 0, rects, num, YXBanded );
       
   169 
       
   170 #ifndef QT_NO_XRENDER
       
   171     if (picture)
       
   172         XRenderSetPictureClipRectangles(dpy, picture, 0, 0, rects, num);
       
   173 #else
       
   174     Q_UNUSED(picture);
       
   175 #endif // QT_NO_XRENDER
       
   176 }
       
   177 
       
   178 
       
   179 static inline void x11ClearClipRegion(Display *dpy, GC gc, GC gc2,
       
   180 #ifndef QT_NO_XRENDER
       
   181                                     Picture picture
       
   182 #else
       
   183                                     Qt::HANDLE picture
       
   184 #endif
       
   185                                       )
       
   186 {
       
   187     if (gc)
       
   188         XSetClipMask(dpy, gc, XNone);
       
   189     if (gc2)
       
   190         XSetClipMask(dpy, gc2, XNone);
       
   191 
       
   192 #ifndef QT_NO_XRENDER
       
   193     if (picture) {
       
   194         XRenderPictureAttributes attrs;
       
   195         attrs.clip_mask = XNone;
       
   196         XRenderChangePicture (dpy, picture, CPClipMask, &attrs);
       
   197     }
       
   198 #else
       
   199     Q_UNUSED(picture);
       
   200 #endif // QT_NO_XRENDER
       
   201 }
       
   202 
       
   203 
       
   204 #define DITHER_SIZE 16
       
   205 static const uchar base_dither_matrix[DITHER_SIZE][DITHER_SIZE] = {
       
   206   {   0,192, 48,240, 12,204, 60,252,  3,195, 51,243, 15,207, 63,255 },
       
   207   { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
       
   208   {  32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
       
   209   { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
       
   210   {   8,200, 56,248,  4,196, 52,244, 11,203, 59,251,  7,199, 55,247 },
       
   211   { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
       
   212   {  40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
       
   213   { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
       
   214   {   2,194, 50,242, 14,206, 62,254,  1,193, 49,241, 13,205, 61,253 },
       
   215   { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
       
   216   {  34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
       
   217   { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
       
   218   {  10,202, 58,250,  6,198, 54,246,  9,201, 57,249,  5,197, 53,245 },
       
   219   { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
       
   220   {  42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
       
   221   { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
       
   222 };
       
   223 
       
   224 static QPixmap qt_patternForAlpha(uchar alpha, int screen)
       
   225 {
       
   226     QPixmap pm;
       
   227     QString key = QLatin1String("$qt-alpha-brush$") + QString::number(alpha) + QString::number(screen);
       
   228     if (!QPixmapCache::find(key, pm)) {
       
   229         // #### why not use a mono image here????
       
   230         QImage pattern(DITHER_SIZE, DITHER_SIZE, QImage::Format_ARGB32);
       
   231         pattern.fill(0xffffffff);
       
   232         for (int y = 0; y < DITHER_SIZE; ++y) {
       
   233             for (int x = 0; x < DITHER_SIZE; ++x) {
       
   234                 if (base_dither_matrix[x][y] <= alpha)
       
   235                     pattern.setPixel(x, y, 0x00000000);
       
   236             }
       
   237         }
       
   238         pm = QBitmap::fromImage(pattern);
       
   239         pm.x11SetScreen(screen);
       
   240         QPixmapCache::insert(key, pm);
       
   241     }
       
   242     return pm;
       
   243 }
       
   244 
       
   245 #if !defined(QT_NO_XRENDER)
       
   246 
       
   247 class QXRenderTessellator : public QTessellator
       
   248 {
       
   249 public:
       
   250     QXRenderTessellator() : traps(0), allocated(0), size(0) {}
       
   251     ~QXRenderTessellator() { free(traps); }
       
   252     XTrapezoid *traps;
       
   253     int allocated;
       
   254     int size;
       
   255     void addTrap(const Trapezoid &trap);
       
   256     QRect tessellate(const QPointF *points, int nPoints, bool winding) {
       
   257         size = 0;
       
   258         setWinding(winding);
       
   259         return QTessellator::tessellate(points, nPoints).toRect();
       
   260     }
       
   261     void done() {
       
   262         if (allocated > 64) {
       
   263             free(traps);
       
   264             traps = 0;
       
   265             allocated = 0;
       
   266         }
       
   267     }
       
   268 };
       
   269 
       
   270 void QXRenderTessellator::addTrap(const Trapezoid &trap)
       
   271 {
       
   272     if (size == allocated) {
       
   273         allocated = qMax(2*allocated, 64);
       
   274         traps = q_check_ptr((XTrapezoid *)realloc(traps, allocated * sizeof(XTrapezoid)));
       
   275     }
       
   276     traps[size].top = Q27Dot5ToXFixed(trap.top);
       
   277     traps[size].bottom = Q27Dot5ToXFixed(trap.bottom);
       
   278     traps[size].left.p1.x = Q27Dot5ToXFixed(trap.topLeft->x);
       
   279     traps[size].left.p1.y = Q27Dot5ToXFixed(trap.topLeft->y);
       
   280     traps[size].left.p2.x = Q27Dot5ToXFixed(trap.bottomLeft->x);
       
   281     traps[size].left.p2.y = Q27Dot5ToXFixed(trap.bottomLeft->y);
       
   282     traps[size].right.p1.x = Q27Dot5ToXFixed(trap.topRight->x);
       
   283     traps[size].right.p1.y = Q27Dot5ToXFixed(trap.topRight->y);
       
   284     traps[size].right.p2.x = Q27Dot5ToXFixed(trap.bottomRight->x);
       
   285     traps[size].right.p2.y = Q27Dot5ToXFixed(trap.bottomRight->y);
       
   286     ++size;
       
   287 }
       
   288 
       
   289 #endif // !defined(QT_NO_XRENDER)
       
   290 
       
   291 
       
   292 #ifndef QT_NO_XRENDER
       
   293 static Picture getPatternFill(int screen, const QBrush &b)
       
   294 {
       
   295     if (!X11->use_xrender)
       
   296         return XNone;
       
   297 
       
   298     XRenderColor color = X11->preMultiply(b.color());
       
   299     XRenderColor bg_color;
       
   300 
       
   301     bg_color = X11->preMultiply(QColor(0, 0, 0, 0));
       
   302 
       
   303     for (int i = 0; i < X11->pattern_fill_count; ++i) {
       
   304         if (X11->pattern_fills[i].screen == screen
       
   305             && X11->pattern_fills[i].opaque == false
       
   306             && X11->pattern_fills[i].style == b.style()
       
   307             && X11->pattern_fills[i].color.alpha == color.alpha
       
   308             && X11->pattern_fills[i].color.red == color.red
       
   309             && X11->pattern_fills[i].color.green == color.green
       
   310             && X11->pattern_fills[i].color.blue == color.blue
       
   311             && X11->pattern_fills[i].bg_color.alpha == bg_color.alpha
       
   312             && X11->pattern_fills[i].bg_color.red == bg_color.red
       
   313             && X11->pattern_fills[i].bg_color.green == bg_color.green
       
   314             && X11->pattern_fills[i].bg_color.blue == bg_color.blue)
       
   315             return X11->pattern_fills[i].picture;
       
   316     }
       
   317     // none found, replace one
       
   318     int i = rand() % 16;
       
   319 
       
   320     if (X11->pattern_fills[i].screen != screen && X11->pattern_fills[i].picture) {
       
   321 	XRenderFreePicture (X11->display, X11->pattern_fills[i].picture);
       
   322 	X11->pattern_fills[i].picture = 0;
       
   323     }
       
   324 
       
   325     if (!X11->pattern_fills[i].picture) {
       
   326         Pixmap pixmap = XCreatePixmap (X11->display, RootWindow (X11->display, screen), 8, 8, 32);
       
   327         XRenderPictureAttributes attrs;
       
   328         attrs.repeat = True;
       
   329         X11->pattern_fills[i].picture = XRenderCreatePicture (X11->display, pixmap,
       
   330                                                               XRenderFindStandardFormat(X11->display, PictStandardARGB32),
       
   331                                                               CPRepeat, &attrs);
       
   332         XFreePixmap (X11->display, pixmap);
       
   333     }
       
   334 
       
   335     X11->pattern_fills[i].screen = screen;
       
   336     X11->pattern_fills[i].color = color;
       
   337     X11->pattern_fills[i].bg_color = bg_color;
       
   338     X11->pattern_fills[i].opaque = false;
       
   339     X11->pattern_fills[i].style = b.style();
       
   340 
       
   341     XRenderFillRectangle(X11->display, PictOpSrc, X11->pattern_fills[i].picture, &bg_color, 0, 0, 8, 8);
       
   342 
       
   343     QPixmap pattern(qt_pixmapForBrush(b.style(), true));
       
   344     XRenderPictureAttributes attrs;
       
   345     attrs.repeat = true;
       
   346     XRenderChangePicture(X11->display, pattern.x11PictureHandle(), CPRepeat, &attrs);
       
   347 
       
   348     Picture fill_fg = X11->getSolidFill(screen, b.color());
       
   349     XRenderComposite(X11->display, PictOpOver, fill_fg, pattern.x11PictureHandle(),
       
   350                      X11->pattern_fills[i].picture,
       
   351                      0, 0, 0, 0, 0, 0, 8, 8);
       
   352 
       
   353     return X11->pattern_fills[i].picture;
       
   354 }
       
   355 
       
   356 static void qt_render_bitmap(Display *dpy, int scrn, Picture src, Picture dst,
       
   357                       int sx, int sy, int x, int y, int sw, int sh,
       
   358                       const QPen &pen)
       
   359 {
       
   360     Picture fill_fg = X11->getSolidFill(scrn, pen.color());
       
   361     XRenderComposite(dpy, PictOpOver,
       
   362                      fill_fg, src, dst, sx, sy, sx, sy, x, y, sw, sh);
       
   363 }
       
   364 #endif
       
   365 
       
   366 void QX11PaintEnginePrivate::init()
       
   367 {
       
   368     dpy = 0;
       
   369     scrn = 0;
       
   370     hd = 0;
       
   371     picture = 0;
       
   372     xinfo = 0;
       
   373 #ifndef QT_NO_XRENDER
       
   374     current_brush = 0;
       
   375     composition_mode = PictOpOver;
       
   376     tessellator = new QXRenderTessellator;
       
   377 #endif
       
   378 }
       
   379 
       
   380 void QX11PaintEnginePrivate::setupAdaptedOrigin(const QPoint &p)
       
   381 {
       
   382     if (adapted_pen_origin)
       
   383         XSetTSOrigin(dpy, gc, p.x(), p.y());
       
   384     if (adapted_brush_origin)
       
   385         XSetTSOrigin(dpy, gc_brush, p.x(), p.y());
       
   386 }
       
   387 
       
   388 void QX11PaintEnginePrivate::resetAdaptedOrigin()
       
   389 {
       
   390     if (adapted_pen_origin)
       
   391         XSetTSOrigin(dpy, gc, 0, 0);
       
   392     if (adapted_brush_origin)
       
   393         XSetTSOrigin(dpy, gc_brush, 0, 0);
       
   394 }
       
   395 
       
   396 void QX11PaintEnginePrivate::clipPolygon_dev(const QPolygonF &poly, QPolygonF *clipped_poly)
       
   397 {
       
   398     int clipped_count = 0;
       
   399     qt_float_point *clipped_points = 0;
       
   400     polygonClipper.clipPolygon((qt_float_point *) poly.data(), poly.size(),
       
   401                                &clipped_points, &clipped_count);
       
   402     clipped_poly->resize(clipped_count);
       
   403     for (int i=0; i<clipped_count; ++i)
       
   404         (*clipped_poly)[i] = *((QPointF *)(&clipped_points[i]));
       
   405 }
       
   406 
       
   407 void QX11PaintEnginePrivate::systemStateChanged()
       
   408 {
       
   409     Q_Q(QX11PaintEngine);
       
   410     QPainter *painter = q->state ? static_cast<QPainterState *>(q->state)->painter : 0;
       
   411     if (painter && painter->hasClipping()) {
       
   412         if (q->testDirty(QPaintEngine::DirtyTransform))
       
   413             q->updateMatrix(q->state->transform());
       
   414         QPolygonF clip_poly_dev(matrix.map(painter->clipPath().toFillPolygon()));
       
   415         QPolygonF clipped_poly_dev;
       
   416         clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
       
   417         q->updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), Qt::ReplaceClip);
       
   418     } else {
       
   419         q->updateClipRegion_dev(QRegion(), Qt::NoClip);
       
   420     }
       
   421 }
       
   422 
       
   423 static QPaintEngine::PaintEngineFeatures qt_decide_features()
       
   424 {
       
   425     QPaintEngine::PaintEngineFeatures features =
       
   426         QPaintEngine::PrimitiveTransform
       
   427         | QPaintEngine::PatternBrush
       
   428         | QPaintEngine::AlphaBlend
       
   429         | QPaintEngine::PainterPaths
       
   430         | QPaintEngine::RasterOpModes;
       
   431 
       
   432     if (X11->use_xrender) {
       
   433         features |= QPaintEngine::Antialiasing;
       
   434         features |= QPaintEngine::PorterDuff;
       
   435         features |= QPaintEngine::MaskedBrush;
       
   436 #if 0
       
   437         if (X11->xrender_version > 10) {
       
   438             features |= QPaintEngine::LinearGradientFill;
       
   439             // ###
       
   440         }
       
   441 #endif
       
   442     }
       
   443 
       
   444     return features;
       
   445 }
       
   446 
       
   447 /*
       
   448  * QX11PaintEngine members
       
   449  */
       
   450 
       
   451 QX11PaintEngine::QX11PaintEngine()
       
   452     : QPaintEngine(*(new QX11PaintEnginePrivate), qt_decide_features())
       
   453 {
       
   454     d_func()->init();
       
   455 }
       
   456 
       
   457 QX11PaintEngine::QX11PaintEngine(QX11PaintEnginePrivate &dptr)
       
   458     : QPaintEngine(dptr, qt_decide_features())
       
   459 {
       
   460     d_func()->init();
       
   461 }
       
   462 
       
   463 QX11PaintEngine::~QX11PaintEngine()
       
   464 {
       
   465 #ifndef QT_NO_XRENDER
       
   466     Q_D(QX11PaintEngine);
       
   467     delete d->tessellator;
       
   468 #endif
       
   469 }
       
   470 
       
   471 bool QX11PaintEngine::begin(QPaintDevice *pdev)
       
   472 {
       
   473     Q_D(QX11PaintEngine);
       
   474     d->xinfo = qt_x11Info(pdev);
       
   475     QWidget *w = d->pdev->devType() == QInternal::Widget ? static_cast<QWidget *>(d->pdev) : 0;
       
   476     const bool isAlienWidget = w && !w->internalWinId() && w->testAttribute(Qt::WA_WState_Created);
       
   477 #ifndef QT_NO_XRENDER
       
   478     if (w) {
       
   479         if (isAlienWidget)
       
   480             d->picture = (::Picture)w->nativeParentWidget()->x11PictureHandle();
       
   481         else
       
   482             d->picture = (::Picture)w->x11PictureHandle();
       
   483     } else if (pdev->devType() == QInternal::Pixmap) {
       
   484         const QPixmap *pm = static_cast<const QPixmap *>(pdev);
       
   485         QX11PixmapData *data = static_cast<QX11PixmapData*>(pm->data.data());
       
   486         if (X11->use_xrender && data->depth() != 32 && data->x11_mask)
       
   487             data->convertToARGB32();
       
   488         d->picture = (::Picture)static_cast<const QPixmap *>(pdev)->x11PictureHandle();
       
   489     }
       
   490 #else
       
   491     d->picture = 0;
       
   492 #endif
       
   493     d->hd = !isAlienWidget ? qt_x11Handle(pdev) : qt_x11Handle(w->nativeParentWidget());
       
   494 
       
   495     Q_ASSERT(d->xinfo != 0);
       
   496     d->dpy = d->xinfo->display(); // get display variable
       
   497     d->scrn = d->xinfo->screen(); // get screen variable
       
   498 
       
   499     d->crgn = QRegion();
       
   500     d->gc = XCreateGC(d->dpy, d->hd, 0, 0);
       
   501     d->gc_brush = XCreateGC(d->dpy, d->hd, 0, 0);
       
   502     d->has_alpha_brush = false;
       
   503     d->has_alpha_pen = false;
       
   504     d->has_clipping = false;
       
   505     d->has_complex_xform = false;
       
   506     d->has_scaling_xform = false;
       
   507     d->has_non_scaling_xform = true;
       
   508     d->xform_scale = 1;
       
   509     d->has_custom_pen = false;
       
   510     d->matrix = QTransform();
       
   511     d->pdev_depth = d->pdev->depth();
       
   512     d->render_hints = 0;
       
   513     d->txop = QTransform::TxNone;
       
   514     d->use_path_fallback = false;
       
   515 #if !defined(QT_NO_XRENDER)
       
   516     d->composition_mode = PictOpOver;
       
   517 #endif
       
   518     d->xlibMaxLinePoints = 32762; // a safe number used to avoid, call to XMaxRequestSize(d->dpy) - 3;
       
   519     d->opacity = 1;
       
   520 
       
   521     // Set up the polygon clipper. Note: This will only work in
       
   522     // polyline mode as long as we have a buffer zone, since a
       
   523     // polyline may be clipped into several non-connected polylines.
       
   524     const int BUFFERZONE = 1000;
       
   525     QRect devClipRect(-BUFFERZONE, -BUFFERZONE,
       
   526                       pdev->width() + 2*BUFFERZONE, pdev->height() + 2*BUFFERZONE);
       
   527     d->polygonClipper.setBoundingRect(devClipRect);
       
   528 
       
   529     if (isAlienWidget) {
       
   530         // Set system clip for alien widgets painting outside the paint event.
       
   531         // This is not a problem with native windows since the windowing system
       
   532         // will handle the clip.
       
   533         QWidgetPrivate *wd = w->d_func();
       
   534         QRegion widgetClip(wd->clipRect());
       
   535         wd->clipToEffectiveMask(widgetClip);
       
   536         wd->subtractOpaqueSiblings(widgetClip);
       
   537         widgetClip.translate(w->mapTo(w->nativeParentWidget(), QPoint()));
       
   538         setSystemClip(widgetClip);
       
   539     }
       
   540 
       
   541     QPixmap::x11SetDefaultScreen(d->xinfo->screen());
       
   542 
       
   543     if (w && w->testAttribute(Qt::WA_PaintUnclipped)) {  // paint direct on device
       
   544  	updatePen(QPen(Qt::black));
       
   545  	updateBrush(QBrush(Qt::white), QPoint());
       
   546         XSetSubwindowMode(d->dpy, d->gc, IncludeInferiors);
       
   547         XSetSubwindowMode(d->dpy, d->gc_brush, IncludeInferiors);
       
   548 #ifndef QT_NO_XRENDER
       
   549         XRenderPictureAttributes attrs;
       
   550         attrs.subwindow_mode = IncludeInferiors;
       
   551         XRenderChangePicture(d->dpy, d->picture, CPSubwindowMode, &attrs);
       
   552 #endif
       
   553     }
       
   554 
       
   555     setDirty(QPaintEngine::DirtyClipRegion);
       
   556     setDirty(QPaintEngine::DirtyPen);
       
   557     setDirty(QPaintEngine::DirtyBrush);
       
   558     setDirty(QPaintEngine::DirtyBackground);
       
   559 
       
   560     return true;
       
   561 }
       
   562 
       
   563 bool QX11PaintEngine::end()
       
   564 {
       
   565     Q_D(QX11PaintEngine);
       
   566 
       
   567 #if !defined(QT_NO_XRENDER)
       
   568     if (d->picture) {
       
   569         // reset clipping/subwindow mode on our render picture
       
   570 	XRenderPictureAttributes attrs;
       
   571 	attrs.subwindow_mode = ClipByChildren;
       
   572         attrs.clip_mask = XNone;
       
   573 	XRenderChangePicture(d->dpy, d->picture, CPClipMask|CPSubwindowMode, &attrs);
       
   574     }
       
   575 #endif
       
   576 
       
   577     if (d->gc_brush && d->pdev->painters < 2) {
       
   578         XFreeGC(d->dpy, d->gc_brush);
       
   579         d->gc_brush = 0;
       
   580     }
       
   581 
       
   582     if (d->gc && d->pdev->painters < 2) {
       
   583         XFreeGC(d->dpy, d->gc);
       
   584         d->gc = 0;
       
   585     }
       
   586 
       
   587     // Restore system clip for alien widgets painting outside the paint event.
       
   588     if (d->pdev->devType() == QInternal::Widget && !static_cast<QWidget *>(d->pdev)->internalWinId())
       
   589         setSystemClip(QRegion());
       
   590 
       
   591     return true;
       
   592 }
       
   593 
       
   594 static bool clipLine(QLineF *line, const QRect &rect)
       
   595 {
       
   596     qreal x1 = line->x1();
       
   597     qreal x2 = line->x2();
       
   598     qreal y1 = line->y1();
       
   599     qreal y2 = line->y2();
       
   600 
       
   601     qreal left = rect.x();
       
   602     qreal right = rect.x() + rect.width() - 1;
       
   603     qreal top = rect.y();
       
   604     qreal bottom = rect.y() + rect.height() - 1;
       
   605 
       
   606     enum { Left, Right, Top, Bottom };
       
   607     // clip the lines, after cohen-sutherland, see e.g. http://www.nondot.org/~sabre/graphpro/line6.html
       
   608     int p1 = ((x1 < left) << Left)
       
   609              | ((x1 > right) << Right)
       
   610              | ((y1 < top) << Top)
       
   611              | ((y1 > bottom) << Bottom);
       
   612     int p2 = ((x2 < left) << Left)
       
   613              | ((x2 > right) << Right)
       
   614              | ((y2 < top) << Top)
       
   615              | ((y2 > bottom) << Bottom);
       
   616 
       
   617     if (p1 & p2)
       
   618         // completely outside
       
   619         return false;
       
   620 
       
   621     if (p1 | p2) {
       
   622         qreal dx = x2 - x1;
       
   623         qreal dy = y2 - y1;
       
   624 
       
   625         // clip x coordinates
       
   626         if (x1 < left) {
       
   627             y1 += dy/dx * (left - x1);
       
   628             x1 = left;
       
   629         } else if (x1 > right) {
       
   630             y1 -= dy/dx * (x1 - right);
       
   631             x1 = right;
       
   632         }
       
   633         if (x2 < left) {
       
   634             y2 += dy/dx * (left - x2);
       
   635             x2 = left;
       
   636         } else if (x2 > right) {
       
   637             y2 -= dy/dx * (x2 - right);
       
   638             x2 = right;
       
   639         }
       
   640         p1 = ((y1 < top) << Top)
       
   641              | ((y1 > bottom) << Bottom);
       
   642         p2 = ((y2 < top) << Top)
       
   643              | ((y2 > bottom) << Bottom);
       
   644         if (p1 & p2)
       
   645             return false;
       
   646         // clip y coordinates
       
   647         if (y1 < top) {
       
   648             x1 += dx/dy * (top - y1);
       
   649             y1 = top;
       
   650         } else if (y1 > bottom) {
       
   651             x1 -= dx/dy * (y1 - bottom);
       
   652             y1 = bottom;
       
   653         }
       
   654         if (y2 < top) {
       
   655             x2 += dx/dy * (top - y2);
       
   656             y2 = top;
       
   657         } else if (y2 > bottom) {
       
   658             x2 -= dx/dy * (y2 - bottom);
       
   659             y2 = bottom;
       
   660         }
       
   661         *line = QLineF(QPointF(x1, y1), QPointF(x2, y2));
       
   662     }
       
   663     return true;
       
   664 }
       
   665 
       
   666 void QX11PaintEngine::drawLines(const QLine *lines, int lineCount)
       
   667 {
       
   668     Q_ASSERT(lines);
       
   669     Q_ASSERT(lineCount);
       
   670     Q_D(QX11PaintEngine);
       
   671     if (d->has_alpha_brush
       
   672         || d->has_alpha_pen
       
   673         || d->has_custom_pen
       
   674         || (d->cpen.widthF() > 0 && d->has_complex_xform
       
   675             && !d->has_non_scaling_xform)
       
   676         || (d->render_hints & QPainter::Antialiasing)) {
       
   677         for (int i = 0; i < lineCount; ++i) {
       
   678             QPainterPath path(lines[i].p1());
       
   679             path.lineTo(lines[i].p2());
       
   680             drawPath(path);
       
   681         }
       
   682         return;
       
   683     }
       
   684 
       
   685     if (d->has_pen) {
       
   686         for (int i = 0; i < lineCount; ++i) {
       
   687             QLineF linef;
       
   688             if (d->txop == QTransform::TxNone) {
       
   689                 linef = lines[i];
       
   690             } else {
       
   691                 linef = d->matrix.map(QLineF(lines[i]));
       
   692             }
       
   693             if (clipLine(&linef, d->polygonClipper.boundingRect())) {
       
   694                 int x1 = qRound(linef.x1() + aliasedCoordinateDelta);
       
   695                 int y1 = qRound(linef.y1() + aliasedCoordinateDelta);
       
   696                 int x2 = qRound(linef.x2() + aliasedCoordinateDelta);
       
   697                 int y2 = qRound(linef.y2() + aliasedCoordinateDelta);
       
   698 
       
   699                 XDrawLine(d->dpy, d->hd, d->gc, x1, y1, x2, y2);
       
   700             }
       
   701         }
       
   702     }
       
   703 }
       
   704 
       
   705 void QX11PaintEngine::drawLines(const QLineF *lines, int lineCount)
       
   706 {
       
   707     Q_ASSERT(lines);
       
   708     Q_ASSERT(lineCount);
       
   709     Q_D(QX11PaintEngine);
       
   710     if (d->has_alpha_brush
       
   711         || d->has_alpha_pen
       
   712         || d->has_custom_pen
       
   713         || (d->cpen.widthF() > 0 && d->has_complex_xform
       
   714             && !d->has_non_scaling_xform)
       
   715         || (d->render_hints & QPainter::Antialiasing)) {
       
   716         for (int i = 0; i < lineCount; ++i) {
       
   717             QPainterPath path(lines[i].p1());
       
   718             path.lineTo(lines[i].p2());
       
   719             drawPath(path);
       
   720         }
       
   721         return;
       
   722     }
       
   723 
       
   724     if (d->has_pen) {
       
   725         for (int i = 0; i < lineCount; ++i) {
       
   726             QLineF linef = d->matrix.map(lines[i]);
       
   727             if (clipLine(&linef, d->polygonClipper.boundingRect())) {
       
   728                 int x1 = qRound(linef.x1() + aliasedCoordinateDelta);
       
   729                 int y1 = qRound(linef.y1() + aliasedCoordinateDelta);
       
   730                 int x2 = qRound(linef.x2() + aliasedCoordinateDelta);
       
   731                 int y2 = qRound(linef.y2() + aliasedCoordinateDelta);
       
   732 
       
   733                 XDrawLine(d->dpy, d->hd, d->gc, x1, y1, x2, y2);
       
   734             }
       
   735         }
       
   736     }
       
   737 }
       
   738 
       
   739 static inline QLine clipStraightLine(const QRect &clip, const QLine &l)
       
   740 {
       
   741     if (l.p1().x() == l.p2().x()) {
       
   742         int x = qBound(clip.left(), l.p1().x(), clip.right());
       
   743         int y1 = qBound(clip.top(), l.p1().y(), clip.bottom());
       
   744         int y2 = qBound(clip.top(), l.p2().y(), clip.bottom());
       
   745 
       
   746         return QLine(x, y1, x, y2);
       
   747     } else {
       
   748         Q_ASSERT(l.p1().y() == l.p2().y());
       
   749 
       
   750         int x1 = qBound(clip.left(), l.p1().x(), clip.right());
       
   751         int x2 = qBound(clip.left(), l.p2().x(), clip.right());
       
   752         int y = qBound(clip.top(), l.p1().y(), clip.bottom());
       
   753 
       
   754         return QLine(x1, y, x2, y);
       
   755     }
       
   756 }
       
   757 
       
   758 void QX11PaintEngine::drawRects(const QRectF *rects, int rectCount)
       
   759 {
       
   760     Q_D(QX11PaintEngine);
       
   761     Q_ASSERT(rects);
       
   762     Q_ASSERT(rectCount);
       
   763 
       
   764     if (rectCount != 1
       
   765         || d->has_pen
       
   766         || d->has_alpha_brush
       
   767         || d->has_complex_xform
       
   768         || d->has_custom_pen
       
   769         || d->cbrush.style() != Qt::SolidPattern)
       
   770     {
       
   771         QPaintEngine::drawRects(rects, rectCount);
       
   772         return;
       
   773     }
       
   774 
       
   775     QPoint alignedOffset;
       
   776     if (d->txop == QTransform::TxTranslate) {
       
   777         QPointF offset(d->matrix.dx(), d->matrix.dy());
       
   778         alignedOffset = offset.toPoint();
       
   779         if (offset != QPointF(alignedOffset)) {
       
   780             QPaintEngine::drawRects(rects, rectCount);
       
   781             return;
       
   782         }
       
   783     }
       
   784 
       
   785     const QRectF& r = rects[0];
       
   786     QRect alignedRect = r.toAlignedRect();
       
   787     if (r != QRectF(alignedRect)) {
       
   788         QPaintEngine::drawRects(rects, rectCount);
       
   789         return;
       
   790     }
       
   791     alignedRect.translate(alignedOffset);
       
   792 
       
   793     QRect clip(d->polygonClipper.boundingRect());
       
   794     alignedRect = alignedRect.intersected(clip);
       
   795     if (alignedRect.isEmpty())
       
   796         return;
       
   797 
       
   798     // simple-case:
       
   799     //   the rectangle is pixel-aligned
       
   800     //   the fill brush is just a solid non-alpha color
       
   801     //   the painter transform is only integer translation
       
   802     // ignore: antialiasing and just XFillRectangles directly
       
   803     XRectangle xrect;
       
   804     xrect.x = short(alignedRect.x());
       
   805     xrect.y = short(alignedRect.y());
       
   806     xrect.width = ushort(alignedRect.width());
       
   807     xrect.height = ushort(alignedRect.height());
       
   808     XFillRectangles(d->dpy, d->hd, d->gc_brush, &xrect, 1);
       
   809 }
       
   810 
       
   811 void QX11PaintEngine::drawRects(const QRect *rects, int rectCount)
       
   812 {
       
   813     Q_D(QX11PaintEngine);
       
   814     Q_ASSERT(rects);
       
   815     Q_ASSERT(rectCount);
       
   816 
       
   817     if (d->has_alpha_pen
       
   818         || d->has_complex_xform
       
   819         || d->has_custom_pen
       
   820         || (d->render_hints & QPainter::Antialiasing))
       
   821     {
       
   822         for (int i = 0; i < rectCount; ++i) {
       
   823             QPainterPath path;
       
   824             path.addRect(rects[i]);
       
   825             drawPath(path);
       
   826         }
       
   827         return;
       
   828     }
       
   829 
       
   830     QRect clip(d->polygonClipper.boundingRect());
       
   831     QPoint offset(qRound(d->matrix.dx()), qRound(d->matrix.dy()));
       
   832 #if !defined(QT_NO_XRENDER)
       
   833     ::Picture pict = d->picture;
       
   834 
       
   835     if (X11->use_xrender && pict && d->has_brush && d->pdev_depth != 1
       
   836         && (d->has_texture || d->has_alpha_brush))
       
   837     {
       
   838         XRenderColor xc;
       
   839         if (!d->has_texture && !d->has_pattern)
       
   840             xc = X11->preMultiply(d->cbrush.color());
       
   841 
       
   842         for (int i = 0; i < rectCount; ++i) {
       
   843             QRect r(rects[i]);
       
   844             if (d->txop == QTransform::TxTranslate)
       
   845                 r.translate(offset);
       
   846 
       
   847             if (r.width() == 0 || r.height() == 0) {
       
   848                 if (d->has_pen) {
       
   849                     const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
       
   850                     XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
       
   851                 }
       
   852                 continue;
       
   853             }
       
   854 
       
   855             r = r.intersected(clip);
       
   856             if (r.isEmpty())
       
   857                 continue;
       
   858             if (d->has_texture || d->has_pattern) {
       
   859                 XRenderComposite(d->dpy, d->composition_mode, d->current_brush, 0, pict,
       
   860                                  qRound(r.x() - d->bg_origin.x()), qRound(r.y() - d->bg_origin.y()),
       
   861                                  0, 0, r.x(), r.y(), r.width(), r.height());
       
   862             } else {
       
   863                 XRenderFillRectangle(d->dpy, d->composition_mode, pict, &xc, r.x(), r.y(), r.width(), r.height());
       
   864             }
       
   865             if (d->has_pen)
       
   866                 XDrawRectangle(d->dpy, d->hd, d->gc, r.x(), r.y(), r.width(), r.height());
       
   867         }
       
   868     } else
       
   869 #endif // !QT_NO_XRENDER
       
   870     {
       
   871         if (d->has_brush && d->has_pen) {
       
   872             for (int i = 0; i < rectCount; ++i) {
       
   873                 QRect r(rects[i]);
       
   874                 if (d->txop == QTransform::TxTranslate)
       
   875                     r.translate(offset);
       
   876 
       
   877                 if (r.width() == 0 || r.height() == 0) {
       
   878                     const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
       
   879                     XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
       
   880                     continue;
       
   881                 }
       
   882 
       
   883                 r = r.intersected(clip);
       
   884                 if (r.isEmpty())
       
   885                     continue;
       
   886                 d->setupAdaptedOrigin(r.topLeft());
       
   887                 XFillRectangle(d->dpy, d->hd, d->gc_brush, r.x(), r.y(), r.width(), r.height());
       
   888                 XDrawRectangle(d->dpy, d->hd, d->gc, r.x(), r.y(), r.width(), r.height());
       
   889             }
       
   890             d->resetAdaptedOrigin();
       
   891         } else {
       
   892             QVarLengthArray<XRectangle> xrects(rectCount);
       
   893             int numClipped = rectCount;
       
   894             for (int i = 0; i < rectCount; ++i) {
       
   895                 QRect r(rects[i]);
       
   896                 if (d->txop == QTransform::TxTranslate)
       
   897                     r.translate(offset);
       
   898 
       
   899                 if (r.width() == 0 || r.height() == 0) {
       
   900                     --numClipped;
       
   901                     if (d->has_pen) {
       
   902                         const QLine l = clipStraightLine(clip, QLine(r.left(), r.top(), r.left() + r.width(), r.top() + r.height()));
       
   903                         XDrawLine(d->dpy, d->hd, d->gc, l.p1().x(), l.p1().y(), l.p2().x(), l.p2().y());
       
   904                     }
       
   905                     continue;
       
   906                 }
       
   907 
       
   908                 r = r.intersected(clip);
       
   909                 if (r.isEmpty()) {
       
   910                     --numClipped;
       
   911                     continue;
       
   912                 }
       
   913                 xrects[i].x = short(r.x());
       
   914                 xrects[i].y = short(r.y());
       
   915                 xrects[i].width = ushort(r.width());
       
   916                 xrects[i].height = ushort(r.height());
       
   917             }
       
   918             if (numClipped) {
       
   919                 d->setupAdaptedOrigin(rects[0].topLeft());
       
   920                 if (d->has_brush)
       
   921                     XFillRectangles(d->dpy, d->hd, d->gc_brush, xrects.data(), numClipped);
       
   922                 else if (d->has_pen)
       
   923                     XDrawRectangles(d->dpy, d->hd, d->gc, xrects.data(), numClipped);
       
   924                 d->resetAdaptedOrigin();
       
   925             }
       
   926         }
       
   927     }
       
   928 }
       
   929 
       
   930 static inline void setCapStyle(int cap_style, GC gc)
       
   931 {
       
   932     ulong mask = GCCapStyle;
       
   933     XGCValues vals;
       
   934     vals.cap_style = cap_style;
       
   935     XChangeGC(X11->display, gc, mask, &vals);
       
   936 }
       
   937 
       
   938 void QX11PaintEngine::drawPoints(const QPoint *points, int pointCount)
       
   939 {
       
   940     Q_ASSERT(points);
       
   941     Q_ASSERT(pointCount);
       
   942     Q_D(QX11PaintEngine);
       
   943 
       
   944     if (!d->has_pen)
       
   945         return;
       
   946 
       
   947     // use the same test here as in drawPath to ensure that we don't use the path fallback
       
   948     // and end up in XDrawLines for pens with width <= 1
       
   949     if (d->cpen.widthF() > 1.0f
       
   950         || (X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
       
   951         || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate))
       
   952     {
       
   953         Qt::PenCapStyle capStyle = d->cpen.capStyle();
       
   954         if (capStyle == Qt::FlatCap) {
       
   955             setCapStyle(CapProjecting, d->gc);
       
   956             d->cpen.setCapStyle(Qt::SquareCap);
       
   957         }
       
   958         const QPoint *end = points + pointCount;
       
   959         while (points < end) {
       
   960             QPainterPath path;
       
   961             path.moveTo(*points);
       
   962             path.lineTo(points->x()+.005, points->y());
       
   963             drawPath(path);
       
   964             ++points;
       
   965         }
       
   966 
       
   967         if (capStyle == Qt::FlatCap) {
       
   968             setCapStyle(CapButt, d->gc);
       
   969             d->cpen.setCapStyle(capStyle);
       
   970         }
       
   971         return;
       
   972     }
       
   973 
       
   974     static const int BUF_SIZE = 1024;
       
   975     XPoint xPoints[BUF_SIZE];
       
   976     int i = 0, j = 0;
       
   977     while (i < pointCount) {
       
   978         while (i < pointCount && j < BUF_SIZE) {
       
   979             const QPoint &xformed = d->matrix.map(points[i]);
       
   980             int x = xformed.x();
       
   981             int y = xformed.y();
       
   982             if (x >= SHRT_MIN && y >= SHRT_MIN && x < SHRT_MAX && y < SHRT_MAX) {
       
   983                 xPoints[j].x = x;
       
   984                 xPoints[j].y = y;
       
   985                 ++j;
       
   986             }
       
   987             ++i;
       
   988         }
       
   989         if (j)
       
   990             XDrawPoints(d->dpy, d->hd, d->gc, xPoints, j, CoordModeOrigin);
       
   991 
       
   992         j = 0;
       
   993     }
       
   994 }
       
   995 
       
   996 void QX11PaintEngine::drawPoints(const QPointF *points, int pointCount)
       
   997 {
       
   998     Q_ASSERT(points);
       
   999     Q_ASSERT(pointCount);
       
  1000     Q_D(QX11PaintEngine);
       
  1001 
       
  1002     if (!d->has_pen)
       
  1003         return;
       
  1004 
       
  1005     // use the same test here as in drawPath to ensure that we don't use the path fallback
       
  1006     // and end up in XDrawLines for pens with width <= 1
       
  1007     if (d->cpen.widthF() > 1.0f
       
  1008         || (X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
       
  1009         || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate))
       
  1010     {
       
  1011         Qt::PenCapStyle capStyle = d->cpen.capStyle();
       
  1012         if (capStyle == Qt::FlatCap) {
       
  1013             setCapStyle(CapProjecting, d->gc);
       
  1014             d->cpen.setCapStyle(Qt::SquareCap);
       
  1015         }
       
  1016 
       
  1017         const QPointF *end = points + pointCount;
       
  1018         while (points < end) {
       
  1019             QPainterPath path;
       
  1020             path.moveTo(*points);
       
  1021             path.lineTo(points->x() + 0.005, points->y());
       
  1022             drawPath(path);
       
  1023             ++points;
       
  1024         }
       
  1025         if (capStyle == Qt::FlatCap) {
       
  1026             setCapStyle(CapButt, d->gc);
       
  1027             d->cpen.setCapStyle(capStyle);
       
  1028         }
       
  1029         return;
       
  1030     }
       
  1031 
       
  1032     static const int BUF_SIZE = 1024;
       
  1033     XPoint xPoints[BUF_SIZE];
       
  1034     int i = 0, j = 0;
       
  1035     while (i < pointCount) {
       
  1036         while (i < pointCount && j < BUF_SIZE) {
       
  1037             const QPointF &xformed = d->matrix.map(points[i]);
       
  1038             int x = qFloor(xformed.x());
       
  1039             int y = qFloor(xformed.y());
       
  1040 
       
  1041             if (x >= SHRT_MIN && y >= SHRT_MIN && x < SHRT_MAX && y < SHRT_MAX) {
       
  1042                 xPoints[j].x = x;
       
  1043                 xPoints[j].y = y;
       
  1044                 ++j;
       
  1045             }
       
  1046             ++i;
       
  1047         }
       
  1048         if (j)
       
  1049             XDrawPoints(d->dpy, d->hd, d->gc, xPoints, j, CoordModeOrigin);
       
  1050 
       
  1051         j = 0;
       
  1052     }
       
  1053 }
       
  1054 
       
  1055 QPainter::RenderHints QX11PaintEngine::supportedRenderHints() const
       
  1056 {
       
  1057 #if !defined(QT_NO_XRENDER)
       
  1058     if (X11->use_xrender)
       
  1059         return QPainter::Antialiasing;
       
  1060 #endif
       
  1061     return QFlag(0);
       
  1062 }
       
  1063 
       
  1064 void QX11PaintEngine::updateState(const QPaintEngineState &state)
       
  1065 {
       
  1066     Q_D(QX11PaintEngine);
       
  1067     QPaintEngine::DirtyFlags flags = state.state();
       
  1068 
       
  1069 
       
  1070     if (flags & DirtyOpacity) {
       
  1071         d->opacity = state.opacity();
       
  1072         // Force update pen/brush as to get proper alpha colors propagated
       
  1073         flags |= DirtyPen;
       
  1074         flags |= DirtyBrush;
       
  1075     }
       
  1076 
       
  1077     if (flags & DirtyTransform) updateMatrix(state.transform());
       
  1078     if (flags & DirtyPen) updatePen(state.pen());
       
  1079     if (flags & (DirtyBrush | DirtyBrushOrigin)) updateBrush(state.brush(), state.brushOrigin());
       
  1080     if (flags & DirtyFont) updateFont(state.font());
       
  1081 
       
  1082     if (state.state() & DirtyClipEnabled) {
       
  1083         if (state.isClipEnabled()) {
       
  1084             QPolygonF clip_poly_dev(d->matrix.map(painter()->clipPath().toFillPolygon()));
       
  1085             QPolygonF clipped_poly_dev;
       
  1086             d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
       
  1087             updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), Qt::ReplaceClip);
       
  1088         } else {
       
  1089             updateClipRegion_dev(QRegion(), Qt::NoClip);
       
  1090         }
       
  1091     }
       
  1092 
       
  1093     if (flags & DirtyClipPath) {
       
  1094         QPolygonF clip_poly_dev(d->matrix.map(state.clipPath().toFillPolygon()));
       
  1095         QPolygonF clipped_poly_dev;
       
  1096         d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
       
  1097         updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon(), state.clipPath().fillRule()),
       
  1098                              state.clipOperation());
       
  1099     } else if (flags & DirtyClipRegion) {
       
  1100         extern QPainterPath qt_regionToPath(const QRegion &region);
       
  1101         QPainterPath clip_path = qt_regionToPath(state.clipRegion());
       
  1102         QPolygonF clip_poly_dev(d->matrix.map(clip_path.toFillPolygon()));
       
  1103         QPolygonF clipped_poly_dev;
       
  1104         d->clipPolygon_dev(clip_poly_dev, &clipped_poly_dev);
       
  1105         updateClipRegion_dev(QRegion(clipped_poly_dev.toPolygon()), state.clipOperation());
       
  1106     }
       
  1107     if (flags & DirtyHints) updateRenderHints(state.renderHints());
       
  1108     if (flags & DirtyCompositionMode) {
       
  1109         int function = GXcopy;
       
  1110         if (state.compositionMode() >= QPainter::RasterOp_SourceOrDestination) {
       
  1111             switch (state.compositionMode()) {
       
  1112             case QPainter::RasterOp_SourceOrDestination:
       
  1113                 function = GXor;
       
  1114                 break;
       
  1115             case QPainter::RasterOp_SourceAndDestination:
       
  1116                 function = GXand;
       
  1117                 break;
       
  1118             case QPainter::RasterOp_SourceXorDestination:
       
  1119                 function = GXxor;
       
  1120                 break;
       
  1121             case QPainter::RasterOp_NotSourceAndNotDestination:
       
  1122                 function = GXnor;
       
  1123                 break;
       
  1124             case QPainter::RasterOp_NotSourceOrNotDestination:
       
  1125                 function = GXnand;
       
  1126                 break;
       
  1127             case QPainter::RasterOp_NotSourceXorDestination:
       
  1128                 function = GXequiv;
       
  1129                 break;
       
  1130             case QPainter::RasterOp_NotSource:
       
  1131                 function = GXcopyInverted;
       
  1132                 break;
       
  1133             case QPainter::RasterOp_SourceAndNotDestination:
       
  1134                 function = GXandReverse;
       
  1135                 break;
       
  1136             case QPainter::RasterOp_NotSourceAndDestination:
       
  1137                 function = GXandInverted;
       
  1138                 break;
       
  1139             default:
       
  1140                 function = GXcopy;
       
  1141             }
       
  1142         }
       
  1143 #if !defined(QT_NO_XRENDER)
       
  1144         else {
       
  1145             d->composition_mode =
       
  1146             qpainterOpToXrender(state.compositionMode());
       
  1147         }
       
  1148 #endif
       
  1149         XSetFunction(X11->display, d->gc, function);
       
  1150         XSetFunction(X11->display, d->gc_brush, function);
       
  1151     }
       
  1152     d->decidePathFallback();
       
  1153     d->decideCoordAdjust();
       
  1154 }
       
  1155 
       
  1156 void QX11PaintEngine::updateRenderHints(QPainter::RenderHints hints)
       
  1157 {
       
  1158     Q_D(QX11PaintEngine);
       
  1159     d->render_hints = hints;
       
  1160 
       
  1161 #if !defined(QT_NO_XRENDER)
       
  1162     if (X11->use_xrender && d->picture) {
       
  1163         XRenderPictureAttributes attrs;
       
  1164         attrs.poly_edge = (hints & QPainter::Antialiasing) ? PolyEdgeSmooth : PolyEdgeSharp;
       
  1165         XRenderChangePicture(d->dpy, d->picture, CPPolyEdge, &attrs);
       
  1166     }
       
  1167 #endif
       
  1168 }
       
  1169 
       
  1170 void QX11PaintEngine::updatePen(const QPen &pen)
       
  1171 {
       
  1172     Q_D(QX11PaintEngine);
       
  1173     d->cpen = pen;
       
  1174     int cp = CapButt;
       
  1175     int jn = JoinMiter;
       
  1176     int ps = pen.style();
       
  1177 
       
  1178     if (d->opacity < 1.0) {
       
  1179         QColor c = d->cpen.color();
       
  1180         c.setAlpha(qRound(c.alpha()*d->opacity));
       
  1181         d->cpen.setColor(c);
       
  1182     }
       
  1183 
       
  1184     d->has_pen = (ps != Qt::NoPen);
       
  1185     d->has_alpha_pen = (pen.color().alpha() != 255);
       
  1186 
       
  1187     switch (pen.capStyle()) {
       
  1188     case Qt::SquareCap:
       
  1189         cp = CapProjecting;
       
  1190         break;
       
  1191     case Qt::RoundCap:
       
  1192         cp = CapRound;
       
  1193         break;
       
  1194     case Qt::FlatCap:
       
  1195     default:
       
  1196         cp = CapButt;
       
  1197         break;
       
  1198     }
       
  1199     switch (pen.joinStyle()) {
       
  1200     case Qt::BevelJoin:
       
  1201         jn = JoinBevel;
       
  1202         break;
       
  1203     case Qt::RoundJoin:
       
  1204         jn = JoinRound;
       
  1205         break;
       
  1206     case Qt::MiterJoin:
       
  1207     default:
       
  1208         jn = JoinMiter;
       
  1209         break;
       
  1210     }
       
  1211 
       
  1212     d->adapted_pen_origin = false;
       
  1213 
       
  1214     char dashes[10];                            // custom pen dashes
       
  1215     int dash_len = 0;                           // length of dash list
       
  1216     int xStyle = LineSolid;
       
  1217 
       
  1218     /*
       
  1219       We are emulating Windows here.  Windows treats cpen.width() == 1
       
  1220       (or 0) as a very special case.  The fudge variable unifies this
       
  1221       case with the general case.
       
  1222     */
       
  1223     qreal pen_width = pen.widthF();
       
  1224     int scale =  qRound(pen_width < 1 ? 1 : pen_width);
       
  1225     int space = (pen_width < 1 && pen_width > 0 ? 1 : (2 * scale));
       
  1226     int dot = 1 * scale;
       
  1227     int dash = 4 * scale;
       
  1228 
       
  1229     d->has_custom_pen = false;
       
  1230 
       
  1231     switch (ps) {
       
  1232     case Qt::NoPen:
       
  1233     case Qt::SolidLine:
       
  1234         xStyle = LineSolid;
       
  1235 	break;
       
  1236     case Qt::DashLine:
       
  1237 	dashes[0] = dash;
       
  1238 	dashes[1] = space;
       
  1239 	dash_len = 2;
       
  1240         xStyle = LineOnOffDash;
       
  1241 	break;
       
  1242     case Qt::DotLine:
       
  1243 	dashes[0] = dot;
       
  1244 	dashes[1] = space;
       
  1245 	dash_len = 2;
       
  1246         xStyle = LineOnOffDash;
       
  1247 	break;
       
  1248     case Qt::DashDotLine:
       
  1249 	dashes[0] = dash;
       
  1250 	dashes[1] = space;
       
  1251 	dashes[2] = dot;
       
  1252 	dashes[3] = space;
       
  1253 	dash_len = 4;
       
  1254         xStyle = LineOnOffDash;
       
  1255 	break;
       
  1256     case Qt::DashDotDotLine:
       
  1257 	dashes[0] = dash;
       
  1258 	dashes[1] = space;
       
  1259 	dashes[2] = dot;
       
  1260 	dashes[3] = space;
       
  1261 	dashes[4] = dot;
       
  1262 	dashes[5] = space;
       
  1263 	dash_len = 6;
       
  1264         xStyle = LineOnOffDash;
       
  1265         break;
       
  1266     case Qt::CustomDashLine:
       
  1267         d->has_custom_pen = true;
       
  1268         break;
       
  1269     }
       
  1270 
       
  1271     ulong mask = GCForeground | GCBackground | GCGraphicsExposures | GCLineWidth
       
  1272                  | GCCapStyle | GCJoinStyle | GCLineStyle;
       
  1273     XGCValues vals;
       
  1274     vals.graphics_exposures = false;
       
  1275     if (d->pdev_depth == 1) {
       
  1276         vals.foreground = qGray(pen.color().rgb()) > 127 ? 0 : 1;
       
  1277         vals.background = qGray(QColor(Qt::transparent).rgb()) > 127 ? 0 : 1;
       
  1278     } else if (d->pdev->devType() == QInternal::Pixmap && d->pdev_depth == 32
       
  1279         && X11->use_xrender) {
       
  1280         vals.foreground = pen.color().rgba();
       
  1281         vals.background = QColor(Qt::transparent).rgba();
       
  1282     } else {
       
  1283         QColormap cmap = QColormap::instance(d->scrn);
       
  1284         vals.foreground = cmap.pixel(pen.color());
       
  1285         vals.background = cmap.pixel(QColor(Qt::transparent));
       
  1286     }
       
  1287 
       
  1288 
       
  1289     vals.line_width = qRound(pen.widthF());
       
  1290     vals.cap_style = cp;
       
  1291     vals.join_style = jn;
       
  1292     vals.line_style = xStyle;
       
  1293 
       
  1294     XChangeGC(d->dpy, d->gc, mask, &vals);
       
  1295 
       
  1296     if (dash_len) { // make dash list
       
  1297         XSetDashes(d->dpy, d->gc, 0, dashes, dash_len);
       
  1298     }
       
  1299 
       
  1300     if (!d->has_clipping) { // if clipping is set the paintevent clip region is merged with the clip region
       
  1301         QRegion sysClip = systemClip();
       
  1302         if (!sysClip.isEmpty())
       
  1303             x11SetClipRegion(d->dpy, d->gc, 0, d->picture, sysClip);
       
  1304         else
       
  1305             x11ClearClipRegion(d->dpy, d->gc, 0, d->picture);
       
  1306     }
       
  1307 }
       
  1308 
       
  1309 void QX11PaintEngine::updateBrush(const QBrush &brush, const QPointF &origin)
       
  1310 {
       
  1311     Q_D(QX11PaintEngine);
       
  1312     d->cbrush = brush;
       
  1313     d->bg_origin = origin;
       
  1314     d->adapted_brush_origin = false;
       
  1315 #if !defined(QT_NO_XRENDER)
       
  1316     d->current_brush = 0;
       
  1317 #endif
       
  1318     if (d->opacity < 1.0) {
       
  1319         QColor c = d->cbrush.color();
       
  1320         c.setAlpha(qRound(c.alpha()*d->opacity));
       
  1321         d->cbrush.setColor(c);
       
  1322     }
       
  1323 
       
  1324     int s  = FillSolid;
       
  1325     int  bs = d->cbrush.style();
       
  1326     d->has_brush = (bs != Qt::NoBrush);
       
  1327     d->has_pattern = bs >= Qt::Dense1Pattern && bs <= Qt::DiagCrossPattern;
       
  1328     d->has_texture = bs == Qt::TexturePattern;
       
  1329     d->has_alpha_brush = brush.color().alpha() != 255;
       
  1330     d->has_alpha_texture = d->has_texture && d->cbrush.texture().hasAlphaChannel();
       
  1331 
       
  1332     ulong mask = GCForeground | GCBackground | GCGraphicsExposures
       
  1333                  | GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle;
       
  1334     XGCValues vals;
       
  1335     vals.graphics_exposures = false;
       
  1336     if (d->pdev_depth == 1) {
       
  1337         vals.foreground = qGray(d->cbrush.color().rgb()) > 127 ? 0 : 1;
       
  1338         vals.background = qGray(QColor(Qt::transparent).rgb()) > 127 ? 0 : 1;
       
  1339     } else if (X11->use_xrender && d->pdev->devType() == QInternal::Pixmap
       
  1340                && d->pdev_depth == 32) {
       
  1341         vals.foreground = d->cbrush.color().rgba();
       
  1342         vals.background = QColor(Qt::transparent).rgba();
       
  1343     } else {
       
  1344         QColormap cmap = QColormap::instance(d->scrn);
       
  1345         vals.foreground = cmap.pixel(d->cbrush.color());
       
  1346         vals.background = cmap.pixel(QColor(Qt::transparent));
       
  1347 
       
  1348         if (!X11->use_xrender && d->has_brush && !d->has_pattern && !brush.isOpaque()) {
       
  1349             QPixmap pattern = qt_patternForAlpha(brush.color().alpha(), d->scrn);
       
  1350             mask |= GCStipple;
       
  1351             vals.stipple = pattern.handle();
       
  1352             s = FillStippled;
       
  1353             d->adapted_brush_origin = true;
       
  1354         }
       
  1355     }
       
  1356     vals.cap_style = CapButt;
       
  1357     vals.join_style = JoinMiter;
       
  1358     vals.line_style = LineSolid;
       
  1359 
       
  1360     if (d->has_pattern || d->has_texture) {
       
  1361         if (bs == Qt::TexturePattern) {
       
  1362             d->brush_pm = qt_toX11Pixmap(d->cbrush.texture());
       
  1363 #if !defined(QT_NO_XRENDER)
       
  1364             if (X11->use_xrender) {
       
  1365                 XRenderPictureAttributes attrs;
       
  1366                 attrs.repeat = true;
       
  1367                 XRenderChangePicture(d->dpy, d->brush_pm.x11PictureHandle(), CPRepeat, &attrs);
       
  1368                 QX11PixmapData *data = static_cast<QX11PixmapData*>(d->brush_pm.data.data());
       
  1369                 if (data->mask_picture)
       
  1370                     XRenderChangePicture(d->dpy, data->mask_picture, CPRepeat, &attrs);
       
  1371             }
       
  1372 #endif
       
  1373         } else {
       
  1374             d->brush_pm = qt_toX11Pixmap(qt_pixmapForBrush(bs, true));
       
  1375         }
       
  1376         d->brush_pm.x11SetScreen(d->scrn);
       
  1377         if (d->brush_pm.depth() == 1) {
       
  1378             mask |= GCStipple;
       
  1379             vals.stipple = d->brush_pm.handle();
       
  1380             s = FillStippled;
       
  1381 #if !defined(QT_NO_XRENDER)
       
  1382             if (X11->use_xrender) {
       
  1383                 d->bitmap_texture = QPixmap(d->brush_pm.size());
       
  1384                 d->bitmap_texture.fill(Qt::transparent);
       
  1385                 d->bitmap_texture = qt_toX11Pixmap(d->bitmap_texture);
       
  1386                 d->bitmap_texture.x11SetScreen(d->scrn);
       
  1387 
       
  1388                 ::Picture src  = X11->getSolidFill(d->scrn, d->cbrush.color());
       
  1389                 XRenderComposite(d->dpy, PictOpSrc, src, d->brush_pm.x11PictureHandle(),
       
  1390                                  d->bitmap_texture.x11PictureHandle(),
       
  1391                                  0, 0, d->brush_pm.width(), d->brush_pm.height(),
       
  1392                                  0, 0, d->brush_pm.width(), d->brush_pm.height());
       
  1393 
       
  1394                 XRenderPictureAttributes attrs;
       
  1395                 attrs.repeat = true;
       
  1396                 XRenderChangePicture(d->dpy, d->bitmap_texture.x11PictureHandle(), CPRepeat, &attrs);
       
  1397 
       
  1398                 d->current_brush = d->bitmap_texture.x11PictureHandle();
       
  1399             }
       
  1400 #endif
       
  1401         } else {
       
  1402             mask |= GCTile;
       
  1403 #ifndef QT_NO_XRENDER
       
  1404             if (d->pdev_depth == 32 && d->brush_pm.depth() != 32) {
       
  1405                 d->brush_pm.detach();
       
  1406                 QX11PixmapData *brushData = static_cast<QX11PixmapData*>(d->brush_pm.data.data());
       
  1407                 brushData->convertToARGB32();
       
  1408             }
       
  1409 #endif
       
  1410             vals.tile = (d->brush_pm.depth() == d->pdev_depth
       
  1411                          ? d->brush_pm.handle()
       
  1412                          : static_cast<QX11PixmapData*>(d->brush_pm.data.data())->x11ConvertToDefaultDepth());
       
  1413             s = FillTiled;
       
  1414 #if !defined(QT_NO_XRENDER)
       
  1415             d->current_brush = d->cbrush.texture().x11PictureHandle();
       
  1416 #endif
       
  1417         }
       
  1418 
       
  1419         mask |= GCTileStipXOrigin | GCTileStipYOrigin;
       
  1420         vals.ts_x_origin = qRound(origin.x());
       
  1421         vals.ts_y_origin = qRound(origin.y());
       
  1422     }
       
  1423 #if !defined(QT_NO_XRENDER)
       
  1424     else if (d->has_alpha_brush) {
       
  1425         d->current_brush = X11->getSolidFill(d->scrn, d->cbrush.color());
       
  1426     }
       
  1427 #endif
       
  1428 
       
  1429     vals.fill_style = s;
       
  1430     XChangeGC(d->dpy, d->gc_brush, mask, &vals);
       
  1431     if (!d->has_clipping) {
       
  1432         QRegion sysClip = systemClip();
       
  1433         if (!sysClip.isEmpty())
       
  1434             x11SetClipRegion(d->dpy, d->gc_brush, 0, d->picture, sysClip);
       
  1435         else
       
  1436             x11ClearClipRegion(d->dpy, d->gc_brush, 0, d->picture);
       
  1437     }
       
  1438 }
       
  1439 
       
  1440 void QX11PaintEngine::drawEllipse(const QRectF &rect)
       
  1441 {
       
  1442     QRect aligned = rect.toAlignedRect();
       
  1443     if (aligned == rect)
       
  1444         drawEllipse(aligned);
       
  1445     else
       
  1446         QPaintEngine::drawEllipse(rect);
       
  1447 }
       
  1448 
       
  1449 void QX11PaintEngine::drawEllipse(const QRect &rect)
       
  1450 {
       
  1451     Q_D(QX11PaintEngine);
       
  1452     QRect devclip(SHRT_MIN, SHRT_MIN, SHRT_MAX*2 - 1, SHRT_MAX*2 - 1);
       
  1453     QRect r(rect);
       
  1454     if (d->txop < QTransform::TxRotate) {
       
  1455         r = d->matrix.mapRect(rect);
       
  1456     } else if (d->txop == QTransform::TxRotate && rect.width() == rect.height()) {
       
  1457         QPainterPath path;
       
  1458         path.addEllipse(rect);
       
  1459         r = d->matrix.map(path).boundingRect().toRect();
       
  1460     }
       
  1461 
       
  1462     if (d->has_alpha_brush || d->has_alpha_pen || d->has_custom_pen || (d->render_hints & QPainter::Antialiasing)
       
  1463         || d->has_alpha_texture || devclip.intersected(r) != r
       
  1464         || (d->has_complex_xform
       
  1465             && !(d->has_non_scaling_xform && rect.width() == rect.height())))
       
  1466     {
       
  1467         QPainterPath path;
       
  1468         path.addEllipse(rect);
       
  1469         drawPath(path);
       
  1470         return;
       
  1471     }
       
  1472 
       
  1473     int x = r.x();
       
  1474     int y = r.y();
       
  1475     int w = r.width();
       
  1476     int h = r.height();
       
  1477     if (w < 1 || h < 1)
       
  1478         return;
       
  1479     if (w == 1 && h == 1) {
       
  1480         XDrawPoint(d->dpy, d->hd, d->has_pen ? d->gc : d->gc_brush, x, y);
       
  1481         return;
       
  1482     }
       
  1483     d->setupAdaptedOrigin(rect.topLeft());
       
  1484     if (d->has_brush) {             // draw filled ellipse
       
  1485         XFillArc(d->dpy, d->hd, d->gc_brush, x, y, w, h, 0, 360*64);
       
  1486         if (!d->has_pen)            // make smoother outline
       
  1487             XDrawArc(d->dpy, d->hd, d->gc_brush, x, y, w-1, h-1, 0, 360*64);
       
  1488     }
       
  1489     if (d->has_pen)                // draw outline
       
  1490         XDrawArc(d->dpy, d->hd, d->gc, x, y, w, h, 0, 360*64);
       
  1491     d->resetAdaptedOrigin();
       
  1492 }
       
  1493 
       
  1494 
       
  1495 
       
  1496 void QX11PaintEnginePrivate::fillPolygon_translated(const QPointF *polygonPoints, int pointCount,
       
  1497                                                     QX11PaintEnginePrivate::GCMode gcMode,
       
  1498                                                     QPaintEngine::PolygonDrawMode mode)
       
  1499 {
       
  1500 
       
  1501     QVarLengthArray<QPointF> translated_points(pointCount);
       
  1502     QPointF offset(matrix.dx(), matrix.dy());
       
  1503 
       
  1504     qreal offs = adjust_coords ? aliasedCoordinateDelta : 0.0;
       
  1505     if (!X11->use_xrender || !(render_hints & QPainter::Antialiasing))
       
  1506         offset += QPointF(aliasedCoordinateDelta, aliasedCoordinateDelta);
       
  1507 
       
  1508     for (int i = 0; i < pointCount; ++i) {
       
  1509         translated_points[i] = polygonPoints[i] + offset;
       
  1510 
       
  1511         translated_points[i].rx() = qRound(translated_points[i].x()) + offs;
       
  1512         translated_points[i].ry() = qRound(translated_points[i].y()) + offs;
       
  1513     }
       
  1514 
       
  1515     fillPolygon_dev(translated_points.data(), pointCount, gcMode, mode);
       
  1516 }
       
  1517 
       
  1518 #ifndef QT_NO_XRENDER
       
  1519 static void qt_XRenderCompositeTrapezoids(Display *dpy,
       
  1520                                           int op,
       
  1521                                           Picture src,
       
  1522                                           Picture dst,
       
  1523                                           _Xconst XRenderPictFormat *maskFormat,
       
  1524                                           int xSrc,
       
  1525                                           int ySrc,
       
  1526                                           const XTrapezoid *traps, int size)
       
  1527 {
       
  1528     const int MAX_TRAPS = 50000;
       
  1529     while (size) {
       
  1530         int to_draw = size;
       
  1531         if (to_draw > MAX_TRAPS)
       
  1532             to_draw = MAX_TRAPS;
       
  1533         XRenderCompositeTrapezoids(dpy, op, src, dst,
       
  1534                                    maskFormat,
       
  1535                                    xSrc, ySrc,
       
  1536                                    traps, to_draw);
       
  1537         size -= to_draw;
       
  1538         traps += to_draw;
       
  1539     }
       
  1540 }
       
  1541 #endif
       
  1542 
       
  1543 void QX11PaintEnginePrivate::fillPolygon_dev(const QPointF *polygonPoints, int pointCount,
       
  1544                                              QX11PaintEnginePrivate::GCMode gcMode,
       
  1545                                              QPaintEngine::PolygonDrawMode mode)
       
  1546 {
       
  1547     Q_Q(QX11PaintEngine);
       
  1548 
       
  1549     int clippedCount = 0;
       
  1550     qt_float_point *clippedPoints = 0;
       
  1551 
       
  1552 #ifndef QT_NO_XRENDER
       
  1553     //can change if we switch to pen if gcMode != BrushGC
       
  1554     bool has_fill_texture = has_texture;
       
  1555     bool has_fill_pattern = has_pattern;
       
  1556     ::Picture src;
       
  1557 #endif
       
  1558     QBrush fill;
       
  1559     GC fill_gc;
       
  1560     if (gcMode == BrushGC) {
       
  1561         fill = cbrush;
       
  1562         fill_gc = gc_brush;
       
  1563 #ifndef QT_NO_XRENDER
       
  1564         if (current_brush)
       
  1565             src = current_brush;
       
  1566         else
       
  1567             src = X11->getSolidFill(scrn, fill.color());
       
  1568 #endif
       
  1569     } else {
       
  1570         fill = QBrush(cpen.brush());
       
  1571         fill_gc = gc;
       
  1572 #ifndef QT_NO_XRENDER
       
  1573         //we use the pens brush
       
  1574         has_fill_texture = (fill.style() == Qt::TexturePattern);
       
  1575         has_fill_pattern = (fill.style() >= Qt::Dense1Pattern && fill.style() <= Qt::DiagCrossPattern);
       
  1576         if (has_fill_texture)
       
  1577             src = fill.texture().x11PictureHandle();
       
  1578         else if (has_fill_pattern)
       
  1579             src = getPatternFill(scrn, fill);
       
  1580         else
       
  1581             src = X11->getSolidFill(scrn, fill.color());
       
  1582 #endif
       
  1583     }
       
  1584 
       
  1585     polygonClipper.clipPolygon((qt_float_point *) polygonPoints, pointCount,
       
  1586                                &clippedPoints, &clippedCount);
       
  1587 
       
  1588 #ifndef QT_NO_XRENDER
       
  1589     bool solid_fill = fill.color().alpha() == 255;
       
  1590     if (has_fill_texture && fill.texture().depth() == 1 && solid_fill) {
       
  1591         has_fill_texture = false;
       
  1592         has_fill_pattern = true;
       
  1593     }
       
  1594 
       
  1595     bool antialias = render_hints & QPainter::Antialiasing;
       
  1596 
       
  1597     if (X11->use_xrender
       
  1598         && picture
       
  1599         && !has_fill_pattern
       
  1600         && (clippedCount > 0)
       
  1601         && (fill.style() != Qt::NoBrush)
       
  1602         && ((has_fill_texture && fill.texture().hasAlpha()) || antialias || !solid_fill || has_alpha_pen != has_alpha_brush))
       
  1603     {
       
  1604         QRect br = tessellator->tessellate((QPointF *)clippedPoints, clippedCount,
       
  1605                                               mode == QPaintEngine::WindingMode);
       
  1606         if (tessellator->size > 0) {
       
  1607             XRenderPictureAttributes attrs;
       
  1608             attrs.poly_edge = antialias ? PolyEdgeSmooth : PolyEdgeSharp;
       
  1609             XRenderChangePicture(dpy, picture, CPPolyEdge, &attrs);
       
  1610             int x_offset = int(XFixedToDouble(tessellator->traps[0].left.p1.x) - bg_origin.x());
       
  1611             int y_offset = int(XFixedToDouble(tessellator->traps[0].left.p1.y) - bg_origin.y());
       
  1612             qt_XRenderCompositeTrapezoids(dpy, composition_mode, src, picture,
       
  1613                                           antialias
       
  1614                                           ? XRenderFindStandardFormat(dpy, PictStandardA8)
       
  1615                                           : XRenderFindStandardFormat(dpy, PictStandardA1),
       
  1616                                           x_offset, y_offset,
       
  1617                                           tessellator->traps, tessellator->size);
       
  1618             tessellator->done();
       
  1619         }
       
  1620     } else
       
  1621 #endif
       
  1622         if (fill.style() != Qt::NoBrush) {
       
  1623             if (clippedCount > 200000) {
       
  1624                 QPolygon poly;
       
  1625                 for (int i = 0; i < clippedCount; ++i)
       
  1626                     poly << QPoint(qFloor(clippedPoints[i].x), qFloor(clippedPoints[i].y));
       
  1627 
       
  1628                 const QRect bounds = poly.boundingRect();
       
  1629                 const QRect aligned = bounds
       
  1630                     & QRect(QPoint(), QSize(pdev->width(), pdev->height()));
       
  1631 
       
  1632                 QImage img(aligned.size(), QImage::Format_ARGB32_Premultiplied);
       
  1633                 img.fill(0);
       
  1634 
       
  1635                 QPainter painter(&img);
       
  1636                 painter.translate(-aligned.x(), -aligned.y());
       
  1637                 painter.setPen(Qt::NoPen);
       
  1638                 painter.setBrush(fill);
       
  1639                 if (gcMode == BrushGC)
       
  1640                     painter.setBrushOrigin(q->painter()->brushOrigin());
       
  1641                 painter.drawPolygon(poly);
       
  1642                 painter.end();
       
  1643 
       
  1644                 q->drawImage(aligned, img, img.rect(), Qt::AutoColor);
       
  1645             } else if (clippedCount > 0) {
       
  1646                 QVarLengthArray<XPoint> xpoints(clippedCount);
       
  1647                 for (int i = 0; i < clippedCount; ++i) {
       
  1648                     xpoints[i].x = qFloor(clippedPoints[i].x);
       
  1649                     xpoints[i].y = qFloor(clippedPoints[i].y);
       
  1650                 }
       
  1651                 if (mode == QPaintEngine::WindingMode)
       
  1652                     XSetFillRule(dpy, fill_gc, WindingRule);
       
  1653                 setupAdaptedOrigin(QPoint(xpoints[0].x, xpoints[0].y));
       
  1654                 XFillPolygon(dpy, hd, fill_gc,
       
  1655                              xpoints.data(), clippedCount,
       
  1656                              mode == QPaintEngine::ConvexMode ? Convex : Complex, CoordModeOrigin);
       
  1657                 resetAdaptedOrigin();
       
  1658                 if (mode == QPaintEngine::WindingMode)
       
  1659                     XSetFillRule(dpy, fill_gc, EvenOddRule);
       
  1660             }
       
  1661         }
       
  1662 }
       
  1663 
       
  1664 void QX11PaintEnginePrivate::strokePolygon_translated(const QPointF *polygonPoints, int pointCount, bool close)
       
  1665 {
       
  1666     QVarLengthArray<QPointF> translated_points(pointCount);
       
  1667     QPointF offset(matrix.dx(), matrix.dy());
       
  1668     for (int i = 0; i < pointCount; ++i)
       
  1669         translated_points[i] = polygonPoints[i] + offset;
       
  1670     strokePolygon_dev(translated_points.data(), pointCount, close);
       
  1671 }
       
  1672 
       
  1673 void QX11PaintEnginePrivate::strokePolygon_dev(const QPointF *polygonPoints, int pointCount, bool close)
       
  1674 {
       
  1675     int clippedCount = 0;
       
  1676     qt_float_point *clippedPoints = 0;
       
  1677     polygonClipper.clipPolygon((qt_float_point *) polygonPoints, pointCount,
       
  1678                                &clippedPoints, &clippedCount, close);
       
  1679 
       
  1680     if (clippedCount > 0) {
       
  1681         QVarLengthArray<XPoint> xpoints(clippedCount);
       
  1682         for (int i = 0; i < clippedCount; ++i) {
       
  1683             xpoints[i].x = qRound(clippedPoints[i].x + aliasedCoordinateDelta);
       
  1684             xpoints[i].y = qRound(clippedPoints[i].y + aliasedCoordinateDelta);
       
  1685         }
       
  1686         uint numberPoints = qMin(clippedCount, xlibMaxLinePoints);
       
  1687         XPoint *pts = xpoints.data();
       
  1688         XDrawLines(dpy, hd, gc, pts, numberPoints, CoordModeOrigin);
       
  1689         pts += numberPoints;
       
  1690         clippedCount -= numberPoints;
       
  1691         numberPoints = qMin(clippedCount, xlibMaxLinePoints-1);
       
  1692         while (clippedCount) {
       
  1693             XDrawLines(dpy, hd, gc, pts-1, numberPoints+1, CoordModeOrigin);
       
  1694             pts += numberPoints;
       
  1695             clippedCount -= numberPoints;
       
  1696             numberPoints = qMin(clippedCount, xlibMaxLinePoints-1);
       
  1697         }
       
  1698     }
       
  1699 }
       
  1700 
       
  1701 void QX11PaintEngine::drawPolygon(const QPointF *polygonPoints, int pointCount, PolygonDrawMode mode)
       
  1702 {
       
  1703     Q_D(QX11PaintEngine);
       
  1704     if (d->use_path_fallback) {
       
  1705         QPainterPath path(polygonPoints[0]);
       
  1706         for (int i = 1; i < pointCount; ++i)
       
  1707             path.lineTo(polygonPoints[i]);
       
  1708         if (mode == PolylineMode) {
       
  1709             QBrush oldBrush = d->cbrush;
       
  1710             d->cbrush = QBrush(Qt::NoBrush);
       
  1711             path.setFillRule(Qt::WindingFill);
       
  1712             drawPath(path);
       
  1713             d->cbrush = oldBrush;
       
  1714         } else {
       
  1715             path.setFillRule(mode == OddEvenMode ? Qt::OddEvenFill : Qt::WindingFill);
       
  1716             path.closeSubpath();
       
  1717             drawPath(path);
       
  1718         }
       
  1719         return;
       
  1720     }
       
  1721     if (mode != PolylineMode && d->has_brush)
       
  1722         d->fillPolygon_translated(polygonPoints, pointCount, QX11PaintEnginePrivate::BrushGC, mode);
       
  1723 
       
  1724     if (d->has_pen)
       
  1725         d->strokePolygon_translated(polygonPoints, pointCount, mode != PolylineMode);
       
  1726 }
       
  1727 
       
  1728 
       
  1729 void QX11PaintEnginePrivate::fillPath(const QPainterPath &path, QX11PaintEnginePrivate::GCMode gc_mode, bool transform)
       
  1730 {
       
  1731     qreal offs = adjust_coords ? aliasedCoordinateDelta : 0.0;
       
  1732 
       
  1733     QPainterPath clippedPath;
       
  1734     QPainterPath clipPath;
       
  1735     clipPath.addRect(polygonClipper.boundingRect());
       
  1736 
       
  1737     if (transform)
       
  1738          clippedPath = (path*matrix).intersected(clipPath);
       
  1739     else
       
  1740         clippedPath = path.intersected(clipPath);
       
  1741 
       
  1742     QList<QPolygonF> polys = clippedPath.toFillPolygons();
       
  1743     for (int i = 0; i < polys.size(); ++i) {
       
  1744         QVarLengthArray<QPointF> translated_points(polys.at(i).size());
       
  1745 
       
  1746         for (int j = 0; j < polys.at(i).size(); ++j) {
       
  1747             translated_points[j] = polys.at(i).at(j);
       
  1748             if (!X11->use_xrender || !(render_hints & QPainter::Antialiasing)) {
       
  1749                 translated_points[j].rx() = qRound(translated_points[j].rx() + aliasedCoordinateDelta) + offs;
       
  1750                 translated_points[j].ry() = qRound(translated_points[j].ry() + aliasedCoordinateDelta) + offs;
       
  1751             }
       
  1752         }
       
  1753 
       
  1754         fillPolygon_dev(translated_points.data(), polys.at(i).size(), gc_mode,
       
  1755                         path.fillRule() == Qt::OddEvenFill ? QPaintEngine::OddEvenMode : QPaintEngine::WindingMode);
       
  1756     }
       
  1757 }
       
  1758 
       
  1759 void QX11PaintEngine::drawPath(const QPainterPath &path)
       
  1760 {
       
  1761     Q_D(QX11PaintEngine);
       
  1762     if (path.isEmpty())
       
  1763         return;
       
  1764     QTransform old_matrix = d->matrix;
       
  1765 
       
  1766     if (d->has_brush)
       
  1767         d->fillPath(path, QX11PaintEnginePrivate::BrushGC, true);
       
  1768     if (d->has_pen
       
  1769         && ((X11->use_xrender && (d->has_alpha_pen || (d->render_hints & QPainter::Antialiasing)))
       
  1770             || (!d->cpen.isCosmetic() && d->txop > QTransform::TxTranslate
       
  1771                 && !d->has_non_scaling_xform)
       
  1772             || (d->cpen.style() == Qt::CustomDashLine))) {
       
  1773         QPainterPathStroker stroker;
       
  1774         if (d->cpen.style() == Qt::CustomDashLine) {
       
  1775             stroker.setDashPattern(d->cpen.dashPattern());
       
  1776             stroker.setDashOffset(d->cpen.dashOffset());
       
  1777         } else {
       
  1778             stroker.setDashPattern(d->cpen.style());
       
  1779         }
       
  1780         stroker.setCapStyle(d->cpen.capStyle());
       
  1781         stroker.setJoinStyle(d->cpen.joinStyle());
       
  1782         QPainterPath stroke;
       
  1783         qreal width = d->cpen.widthF();
       
  1784         QPolygonF poly;
       
  1785         QRectF deviceRect(0, 0, d->pdev->width(), d->pdev->height());
       
  1786         // necessary to get aliased alphablended primitives to be drawn correctly
       
  1787         if (d->cpen.isCosmetic() || d->has_scaling_xform) {
       
  1788             if (d->cpen.isCosmetic())
       
  1789                 stroker.setWidth(width == 0 ? 1 : width);
       
  1790             else
       
  1791                 stroker.setWidth(width * d->xform_scale);
       
  1792             stroker.d_ptr->stroker.setClipRect(deviceRect);
       
  1793             stroke = stroker.createStroke(path * d->matrix);
       
  1794             if (stroke.isEmpty())
       
  1795                 return;
       
  1796             stroke.setFillRule(Qt::WindingFill);
       
  1797             d->fillPath(stroke, QX11PaintEnginePrivate::PenGC, false);
       
  1798         } else {
       
  1799             stroker.setWidth(width);
       
  1800             stroker.d_ptr->stroker.setClipRect(d->matrix.inverted().mapRect(deviceRect));
       
  1801             stroke = stroker.createStroke(path);
       
  1802             if (stroke.isEmpty())
       
  1803                 return;
       
  1804             stroke.setFillRule(Qt::WindingFill);
       
  1805             d->fillPath(stroke, QX11PaintEnginePrivate::PenGC, true);
       
  1806         }
       
  1807     } else if (d->has_pen) {
       
  1808         // if we have a cosmetic pen - use XDrawLine() for speed
       
  1809         QList<QPolygonF> polys = path.toSubpathPolygons(d->matrix);
       
  1810         for (int i = 0; i < polys.size(); ++i)
       
  1811             d->strokePolygon_dev(polys.at(i).data(), polys.at(i).size(), false);
       
  1812     }
       
  1813 }
       
  1814 
       
  1815 Q_GUI_EXPORT void qt_x11_drawImage(const QRect &rect, const QPoint &pos, const QImage &image,
       
  1816                                    Drawable hd, GC gc, Display *dpy, Visual *visual, int depth)
       
  1817 {
       
  1818     Q_ASSERT(image.format() == QImage::Format_RGB32);
       
  1819     Q_ASSERT(image.depth() == 32);
       
  1820 
       
  1821     XImage *xi;
       
  1822     // Note: this code assumes either RGB or BGR, 8 bpc server layouts
       
  1823     const uint red_mask = (uint) visual->red_mask;
       
  1824     bool bgr_layout = (red_mask == 0xff);
       
  1825 
       
  1826     const int w = rect.width();
       
  1827     const int h = rect.height();
       
  1828 
       
  1829     QImage im;
       
  1830     int image_byte_order = ImageByteOrder(X11->display);
       
  1831     if ((QSysInfo::ByteOrder == QSysInfo::BigEndian && ((image_byte_order == LSBFirst) || bgr_layout))
       
  1832         || (image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian)
       
  1833         || (image_byte_order == LSBFirst && bgr_layout))
       
  1834     {
       
  1835         im = image.copy(rect);
       
  1836         const int iw = im.bytesPerLine() / 4;
       
  1837         uint *data = (uint *)im.bits();
       
  1838         for (int i=0; i < h; i++) {
       
  1839             uint *p = data;
       
  1840             uint *end = p + w;
       
  1841             if (bgr_layout && image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian) {
       
  1842                 while (p < end) {
       
  1843                     *p = ((*p << 8) & 0xffffff00) | ((*p >> 24) & 0x000000ff);
       
  1844                     p++;
       
  1845                 }
       
  1846             } else if ((image_byte_order == LSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian)
       
  1847                     || (image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::LittleEndian)) {
       
  1848                 while (p < end) {
       
  1849                     *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
       
  1850                         | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
       
  1851                     p++;
       
  1852                 }
       
  1853             } else if ((image_byte_order == MSBFirst && QSysInfo::ByteOrder == QSysInfo::BigEndian)
       
  1854                        || (image_byte_order == LSBFirst && bgr_layout))
       
  1855             {
       
  1856                 while (p < end) {
       
  1857                     *p = ((*p << 16) & 0x00ff0000) | ((*p >> 16) & 0x000000ff)
       
  1858                         | ((*p ) & 0xff00ff00);
       
  1859                     p++;
       
  1860                 }
       
  1861             }
       
  1862             data += iw;
       
  1863         }
       
  1864         xi = XCreateImage(dpy, visual, depth, ZPixmap,
       
  1865                           0, (char *) im.bits(), w, h, 32, im.bytesPerLine());
       
  1866     } else {
       
  1867         xi = XCreateImage(dpy, visual, depth, ZPixmap,
       
  1868                           0, (char *) image.scanLine(rect.y())+rect.x()*sizeof(uint), w, h, 32, image.bytesPerLine());
       
  1869     }
       
  1870     XPutImage(dpy, hd, gc, xi, 0, 0, pos.x(), pos.y(), w, h);
       
  1871     xi->data = 0; // QImage owns these bits
       
  1872     XDestroyImage(xi);
       
  1873 }
       
  1874 
       
  1875 void QX11PaintEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags flags)
       
  1876 {
       
  1877     Q_D(QX11PaintEngine);
       
  1878 
       
  1879     if (image.format() == QImage::Format_RGB32
       
  1880         && d->pdev_depth >= 24 && image.depth() == 32
       
  1881         && r.size() == sr.size())
       
  1882     {
       
  1883         int sx = qRound(sr.x());
       
  1884         int sy = qRound(sr.y());
       
  1885         int x = qRound(r.x());
       
  1886         int y = qRound(r.y());
       
  1887         int w = qRound(r.width());
       
  1888         int h = qRound(r.height());
       
  1889 
       
  1890         qt_x11_drawImage(QRect(sx, sy, w, h), QPoint(x, y), image, d->hd, d->gc, d->dpy,
       
  1891                          (Visual *)d->xinfo->visual(), d->pdev_depth);
       
  1892     } else {
       
  1893         QPaintEngine::drawImage(r, image, sr, flags);
       
  1894     }
       
  1895 }
       
  1896 
       
  1897 void QX11PaintEngine::drawPixmap(const QRectF &r, const QPixmap &px, const QRectF &_sr)
       
  1898 {
       
  1899     Q_D(QX11PaintEngine);
       
  1900     QRectF sr = _sr;
       
  1901     int x = qRound(r.x());
       
  1902     int y = qRound(r.y());
       
  1903     int sx = qRound(sr.x());
       
  1904     int sy = qRound(sr.y());
       
  1905     int sw = qRound(sr.width());
       
  1906     int sh = qRound(sr.height());
       
  1907 
       
  1908     QPixmap pixmap = qt_toX11Pixmap(px);
       
  1909 
       
  1910     if ((d->xinfo && d->xinfo->screen() != pixmap.x11Info().screen())
       
  1911         || (pixmap.x11Info().screen() != DefaultScreen(X11->display))) {
       
  1912         QPixmap* p = const_cast<QPixmap *>(&pixmap);
       
  1913         p->x11SetScreen(d->xinfo ? d->xinfo->screen() : DefaultScreen(X11->display));
       
  1914     }
       
  1915 
       
  1916     QPixmap::x11SetDefaultScreen(pixmap.x11Info().screen());
       
  1917 
       
  1918 #ifndef QT_NO_XRENDER
       
  1919     ::Picture src_pict = static_cast<QX11PixmapData*>(pixmap.data.data())->picture;
       
  1920     if (src_pict && d->picture) {
       
  1921         const int pDepth = pixmap.depth();
       
  1922         if (pDepth == 1 && (d->has_alpha_pen)) {
       
  1923             qt_render_bitmap(d->dpy, d->scrn, src_pict, d->picture,
       
  1924                              sx, sy, x, y, sw, sh, d->cpen);
       
  1925             return;
       
  1926         } else if (pDepth != 1 && (pDepth == 32 || pDepth != d->pdev_depth)) {
       
  1927             XRenderComposite(d->dpy, d->composition_mode,
       
  1928                              src_pict, 0, d->picture, sx, sy, 0, 0, x, y, sw, sh);
       
  1929             return;
       
  1930         }
       
  1931     }
       
  1932 #endif
       
  1933 
       
  1934     bool mono_src = pixmap.depth() == 1;
       
  1935     bool mono_dst = d->pdev_depth == 1;
       
  1936     bool restore_clip = false;
       
  1937 
       
  1938     if (static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask) { // pixmap has a mask
       
  1939         QBitmap comb(sw, sh);
       
  1940         GC cgc = XCreateGC(d->dpy, comb.handle(), 0, 0);
       
  1941         XSetForeground(d->dpy, cgc, 0);
       
  1942         XFillRectangle(d->dpy, comb.handle(), cgc, 0, 0, sw, sh);
       
  1943         XSetBackground(d->dpy, cgc, 0);
       
  1944         XSetForeground(d->dpy, cgc, 1);
       
  1945         if (!d->crgn.isEmpty()) {
       
  1946             int num;
       
  1947             XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
       
  1948             XSetClipRectangles(d->dpy, cgc, -x, -y, rects, num, Unsorted);
       
  1949         } else if (d->has_clipping) {
       
  1950             XSetClipRectangles(d->dpy, cgc, 0, 0, 0, 0, Unsorted);
       
  1951         }
       
  1952         XSetFillStyle(d->dpy, cgc, FillOpaqueStippled);
       
  1953         XSetTSOrigin(d->dpy, cgc, -sx, -sy);
       
  1954         XSetStipple(d->dpy, cgc,
       
  1955                     static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask);
       
  1956         XFillRectangle(d->dpy, comb.handle(), cgc, 0, 0, sw, sh);
       
  1957         XFreeGC(d->dpy, cgc);
       
  1958 
       
  1959         XSetClipOrigin(d->dpy, d->gc, x, y);
       
  1960         XSetClipMask(d->dpy, d->gc, comb.handle());
       
  1961         restore_clip = true;
       
  1962     }
       
  1963 
       
  1964     if (mono_src) {
       
  1965         if (!d->crgn.isEmpty()) {
       
  1966             Pixmap comb = XCreatePixmap(d->dpy, d->hd, sw, sh, 1);
       
  1967             GC cgc = XCreateGC(d->dpy, comb, 0, 0);
       
  1968             XSetForeground(d->dpy, cgc, 0);
       
  1969             XFillRectangle(d->dpy, comb, cgc, 0, 0, sw, sh);
       
  1970             int num;
       
  1971             XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
       
  1972             XSetClipRectangles(d->dpy, cgc, -x, -y, rects, num, Unsorted);
       
  1973             XCopyArea(d->dpy, pixmap.handle(), comb, cgc, sx, sy, sw, sh, 0, 0);
       
  1974             XFreeGC(d->dpy, cgc);
       
  1975 
       
  1976             XSetClipMask(d->dpy, d->gc, comb);
       
  1977             XSetClipOrigin(d->dpy, d->gc, x, y);
       
  1978             XFreePixmap(d->dpy, comb);
       
  1979         } else {
       
  1980             XSetClipMask(d->dpy, d->gc, pixmap.handle());
       
  1981             XSetClipOrigin(d->dpy, d->gc, x - sx, y - sy);
       
  1982         }
       
  1983 
       
  1984         if (mono_dst) {
       
  1985             XSetForeground(d->dpy, d->gc, qGray(d->cpen.color().rgb()) > 127 ? 0 : 1);
       
  1986         } else {
       
  1987             QColormap cmap = QColormap::instance(d->scrn);
       
  1988             XSetForeground(d->dpy, d->gc, cmap.pixel(d->cpen.color()));
       
  1989         }
       
  1990         XFillRectangle(d->dpy, d->hd, d->gc, x, y, sw, sh);
       
  1991         restore_clip = true;
       
  1992     } else {
       
  1993         XCopyArea(d->dpy, pixmap.handle(), d->hd, d->gc, sx, sy, sw, sh, x, y);
       
  1994     }
       
  1995 
       
  1996     if (d->pdev->devType() == QInternal::Pixmap) {
       
  1997         const QPixmap *px = static_cast<const QPixmap*>(d->pdev);
       
  1998         Pixmap src_mask = static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask;
       
  1999         Pixmap dst_mask = static_cast<QX11PixmapData*>(px->data.data())->x11_mask;
       
  2000         if (dst_mask) {
       
  2001             GC cgc = XCreateGC(d->dpy, dst_mask, 0, 0);
       
  2002             if (src_mask) { // copy src mask into dst mask
       
  2003                 XCopyArea(d->dpy, src_mask, dst_mask, cgc, sx, sy, sw, sh, x, y);
       
  2004             } else { // no src mask, but make sure the area copied is opaque in dest
       
  2005                 XSetBackground(d->dpy, cgc, 0);
       
  2006                 XSetForeground(d->dpy, cgc, 1);
       
  2007                 XFillRectangle(d->dpy, dst_mask, cgc, x, y, sw, sh);
       
  2008             }
       
  2009             XFreeGC(d->dpy, cgc);
       
  2010         }
       
  2011     }
       
  2012 
       
  2013     if (restore_clip) {
       
  2014         XSetClipOrigin(d->dpy, d->gc, 0, 0);
       
  2015         int num;
       
  2016         XRectangle *rects = (XRectangle *)qt_getClipRects(d->crgn, num);
       
  2017         if (num == 0)
       
  2018             XSetClipMask(d->dpy, d->gc, XNone);
       
  2019         else
       
  2020             XSetClipRectangles(d->dpy, d->gc, 0, 0, rects, num, Unsorted);
       
  2021     }
       
  2022 }
       
  2023 
       
  2024 void QX11PaintEngine::updateMatrix(const QTransform &mtx)
       
  2025 {
       
  2026     Q_D(QX11PaintEngine);
       
  2027     d->txop = mtx.type();
       
  2028     d->matrix = mtx;
       
  2029 
       
  2030     d->has_complex_xform = (d->txop > QTransform::TxTranslate);
       
  2031 
       
  2032     extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale);
       
  2033     bool scaling = qt_scaleForTransform(d->matrix, &d->xform_scale);
       
  2034     d->has_scaling_xform = scaling && d->xform_scale != 1.0;
       
  2035     d->has_non_scaling_xform = scaling && d->xform_scale == 1.0;
       
  2036 }
       
  2037 
       
  2038 /*
       
  2039    NB! the clip region is expected to be in dev coordinates
       
  2040 */
       
  2041 void QX11PaintEngine::updateClipRegion_dev(const QRegion &clipRegion, Qt::ClipOperation op)
       
  2042 {
       
  2043     Q_D(QX11PaintEngine);
       
  2044     QRegion sysClip = systemClip();
       
  2045     if (op == Qt::NoClip) {
       
  2046         d->has_clipping = false;
       
  2047         d->crgn = sysClip;
       
  2048         if (!sysClip.isEmpty()) {
       
  2049             x11SetClipRegion(d->dpy, d->gc, d->gc_brush, d->picture, sysClip);
       
  2050         } else {
       
  2051             x11ClearClipRegion(d->dpy, d->gc, d->gc_brush, d->picture);
       
  2052         }
       
  2053         return;
       
  2054     }
       
  2055 
       
  2056     switch (op) {
       
  2057     case Qt::IntersectClip:
       
  2058         if (d->has_clipping) {
       
  2059             d->crgn &= clipRegion;
       
  2060             break;
       
  2061         }
       
  2062         // fall through
       
  2063     case Qt::ReplaceClip:
       
  2064         if (!sysClip.isEmpty())
       
  2065             d->crgn = clipRegion.intersected(sysClip);
       
  2066         else
       
  2067             d->crgn = clipRegion;
       
  2068         break;
       
  2069     case Qt::UniteClip:
       
  2070         d->crgn |= clipRegion;
       
  2071         if (!sysClip.isEmpty())
       
  2072             d->crgn = d->crgn.intersected(sysClip);
       
  2073         break;
       
  2074     default:
       
  2075         break;
       
  2076     }
       
  2077     d->has_clipping = true;
       
  2078     x11SetClipRegion(d->dpy, d->gc, d->gc_brush, d->picture, d->crgn);
       
  2079 }
       
  2080 
       
  2081 void QX11PaintEngine::updateFont(const QFont &)
       
  2082 {
       
  2083 }
       
  2084 
       
  2085 Qt::HANDLE QX11PaintEngine::handle() const
       
  2086 {
       
  2087     Q_D(const QX11PaintEngine);
       
  2088     Q_ASSERT(isActive());
       
  2089     Q_ASSERT(d->hd);
       
  2090     return d->hd;
       
  2091 }
       
  2092 
       
  2093 extern void qt_draw_tile(QPaintEngine *, qreal, qreal, qreal, qreal, const QPixmap &,
       
  2094                          qreal, qreal);
       
  2095 
       
  2096 void QX11PaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &p)
       
  2097 {
       
  2098     int x = qRound(r.x());
       
  2099     int y = qRound(r.y());
       
  2100     int w = qRound(r.width());
       
  2101     int h = qRound(r.height());
       
  2102     int sx = qRound(p.x());
       
  2103     int sy = qRound(p.y());
       
  2104 
       
  2105     bool mono_src = pixmap.depth() == 1;
       
  2106     Q_D(QX11PaintEngine);
       
  2107 
       
  2108     if ((d->xinfo && d->xinfo->screen() != pixmap.x11Info().screen())
       
  2109         || (pixmap.x11Info().screen() != DefaultScreen(X11->display))) {
       
  2110         QPixmap* p = const_cast<QPixmap *>(&pixmap);
       
  2111         p->x11SetScreen(d->xinfo ? d->xinfo->screen() : DefaultScreen(X11->display));
       
  2112     }
       
  2113 
       
  2114     QPixmap::x11SetDefaultScreen(pixmap.x11Info().screen());
       
  2115 
       
  2116 #ifndef QT_NO_XRENDER
       
  2117     if (X11->use_xrender && d->picture && pixmap.x11PictureHandle()) {
       
  2118 #if 0
       
  2119         // ### Qt 5: enable this
       
  2120         XRenderPictureAttributes attrs;
       
  2121         attrs.repeat = true;
       
  2122         XRenderChangePicture(d->dpy, pixmap.x11PictureHandle(), CPRepeat, &attrs);
       
  2123 
       
  2124         if (mono_src) {
       
  2125             qt_render_bitmap(d->dpy, d->scrn, pixmap.x11PictureHandle(), d->picture,
       
  2126                              sx, sy, x, y, w, h, d->cpen);
       
  2127         } else {
       
  2128             XRenderComposite(d->dpy, d->composition_mode,
       
  2129                              pixmap.x11PictureHandle(), XNone, d->picture,
       
  2130                              sx, sy, 0, 0, x, y, w, h);
       
  2131         }
       
  2132 #else
       
  2133         const int numTiles = (w / pixmap.width()) * (h / pixmap.height());
       
  2134         if (numTiles < 100) {
       
  2135             // this is essentially qt_draw_tile(), inlined for
       
  2136             // the XRenderComposite call
       
  2137             int yPos, xPos, drawH, drawW, yOff, xOff;
       
  2138             yPos = y;
       
  2139             yOff = sy;
       
  2140             while(yPos < y + h) {
       
  2141                 drawH = pixmap.height() - yOff;    // Cropping first row
       
  2142                 if (yPos + drawH > y + h)        // Cropping last row
       
  2143                     drawH = y + h - yPos;
       
  2144                 xPos = x;
       
  2145                 xOff = sx;
       
  2146                 while(xPos < x + w) {
       
  2147                     drawW = pixmap.width() - xOff; // Cropping first column
       
  2148                     if (xPos + drawW > x + w)    // Cropping last column
       
  2149                         drawW = x + w - xPos;
       
  2150                     if (mono_src) {
       
  2151                         qt_render_bitmap(d->dpy, d->scrn, pixmap.x11PictureHandle(), d->picture,
       
  2152                                          xOff, yOff, xPos, yPos, drawW, drawH, d->cpen);
       
  2153                     } else {
       
  2154                         XRenderComposite(d->dpy, d->composition_mode,
       
  2155                                          pixmap.x11PictureHandle(), XNone, d->picture,
       
  2156                                          xOff, yOff, 0, 0, xPos, yPos, drawW, drawH);
       
  2157                     }
       
  2158                     xPos += drawW;
       
  2159                     xOff = 0;
       
  2160                 }
       
  2161                 yPos += drawH;
       
  2162                 yOff = 0;
       
  2163             }
       
  2164         } else {
       
  2165             w = qMin(w, d->pdev->width() - x);
       
  2166             h = qMin(h, d->pdev->height() - y);
       
  2167             if (w <= 0 || h <= 0)
       
  2168                 return;
       
  2169 
       
  2170             const int pw = w + sx;
       
  2171             const int ph = h + sy;
       
  2172             QPixmap pm(pw, ph);
       
  2173             if (pixmap.hasAlpha() || mono_src)
       
  2174                 pm.fill(Qt::transparent);
       
  2175 
       
  2176             const int mode = pixmap.hasAlpha() ? PictOpOver : PictOpSrc;
       
  2177             const ::Picture pmPicture = pm.x11PictureHandle();
       
  2178 
       
  2179             // first tile
       
  2180             XRenderComposite(d->dpy, mode,
       
  2181                              pixmap.x11PictureHandle(), XNone, pmPicture,
       
  2182                              0, 0, 0, 0, 0, 0, qMin(pw, pixmap.width()), qMin(ph, pixmap.height()));
       
  2183 
       
  2184             // first row of tiles
       
  2185             int xPos = pixmap.width();
       
  2186             const int sh = qMin(ph, pixmap.height());
       
  2187             while (xPos < pw) {
       
  2188                 const int sw = qMin(xPos, pw - xPos);
       
  2189                 XRenderComposite(d->dpy, mode,
       
  2190                                  pmPicture, XNone, pmPicture,
       
  2191                                  0, 0, 0, 0, xPos, 0, sw, sh);
       
  2192                 xPos *= 2;
       
  2193             }
       
  2194 
       
  2195             // remaining rows
       
  2196             int yPos = pixmap.height();
       
  2197             const int sw = pw;
       
  2198             while (yPos < ph) {
       
  2199                 const int sh = qMin(yPos, ph - yPos);
       
  2200                 XRenderComposite(d->dpy, mode,
       
  2201                                  pmPicture, XNone, pmPicture,
       
  2202                                  0, 0, 0, 0, 0, yPos, sw, sh);
       
  2203                 yPos *= 2;
       
  2204             }
       
  2205 
       
  2206             // composite
       
  2207             if (mono_src)
       
  2208                 qt_render_bitmap(d->dpy, d->scrn, pmPicture, d->picture,
       
  2209                                  sx, sy, x, y, w, h, d->cpen);
       
  2210             else
       
  2211                 XRenderComposite(d->dpy, d->composition_mode,
       
  2212                                  pmPicture, XNone, d->picture,
       
  2213                                  sx, sy, 0, 0, x, y, w, h);
       
  2214         }
       
  2215 #endif
       
  2216     } else
       
  2217 #endif // !QT_NO_XRENDER
       
  2218         if (pixmap.depth() > 1 && !static_cast<QX11PixmapData*>(pixmap.data.data())->x11_mask) {
       
  2219             XSetTile(d->dpy, d->gc, pixmap.handle());
       
  2220             XSetFillStyle(d->dpy, d->gc, FillTiled);
       
  2221             XSetTSOrigin(d->dpy, d->gc, x-sx, y-sy);
       
  2222             XFillRectangle(d->dpy, d->hd, d->gc, x, y, w, h);
       
  2223             XSetTSOrigin(d->dpy, d->gc, 0, 0);
       
  2224             XSetFillStyle(d->dpy, d->gc, FillSolid);
       
  2225         } else {
       
  2226             qt_draw_tile(this, x, y, w, h, pixmap, sx, sy);
       
  2227         }
       
  2228 }
       
  2229 
       
  2230 void QX11PaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
       
  2231 {
       
  2232     const QTextItemInt &ti = static_cast<const QTextItemInt &>(textItem);
       
  2233 
       
  2234     switch(ti.fontEngine->type()) {
       
  2235     case QFontEngine::TestFontEngine:
       
  2236     case QFontEngine::Box:
       
  2237         d_func()->drawBoxTextItem(p, ti);
       
  2238         break;
       
  2239     case QFontEngine::XLFD:
       
  2240         drawXLFD(p, ti);
       
  2241         break;
       
  2242 #ifndef QT_NO_FONTCONFIG
       
  2243     case QFontEngine::Freetype:
       
  2244         drawFreetype(p, ti);
       
  2245         break;
       
  2246 #endif
       
  2247     default:
       
  2248         Q_ASSERT(false);
       
  2249     }
       
  2250 }
       
  2251 
       
  2252 void QX11PaintEngine::drawXLFD(const QPointF &p, const QTextItemInt &ti)
       
  2253 {
       
  2254     Q_D(QX11PaintEngine);
       
  2255 
       
  2256     if (d->txop > QTransform::TxTranslate) {
       
  2257         // XServer or font don't support server side transformations, need to do it by hand
       
  2258         QPaintEngine::drawTextItem(p, ti);
       
  2259         return;
       
  2260     }
       
  2261 
       
  2262     if (!ti.glyphs.numGlyphs)
       
  2263         return;
       
  2264 
       
  2265     QVarLengthArray<QFixedPoint> positions;
       
  2266     QVarLengthArray<glyph_t> glyphs;
       
  2267     QTransform matrix = d->matrix;
       
  2268     matrix.translate(p.x(), p.y());
       
  2269     ti.fontEngine->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
       
  2270     if (glyphs.size() == 0)
       
  2271         return;
       
  2272 
       
  2273     QFontEngineXLFD *xlfd = static_cast<QFontEngineXLFD *>(ti.fontEngine);
       
  2274     Qt::HANDLE font_id = xlfd->fontStruct()->fid;
       
  2275 
       
  2276     XSetFont(d->dpy, d->gc, font_id);
       
  2277 
       
  2278     const QFixed offs = QFixed::fromReal(aliasedCoordinateDelta);
       
  2279     for (int i = 0; i < glyphs.size(); i++) {
       
  2280         int xp = qRound(positions[i].x + offs);
       
  2281         int yp = qRound(positions[i].y + offs);
       
  2282         if (xp < SHRT_MAX && xp > SHRT_MIN &&  yp > SHRT_MIN && yp < SHRT_MAX) {
       
  2283             XChar2b ch;
       
  2284             ch.byte1 = glyphs[i] >> 8;
       
  2285             ch.byte2 = glyphs[i] & 0xff;
       
  2286             XDrawString16(d->dpy, d->hd, d->gc, xp, yp, &ch, 1);
       
  2287         }
       
  2288     }
       
  2289 }
       
  2290 
       
  2291 #ifndef QT_NO_FONTCONFIG
       
  2292 static QPainterPath path_for_glyphs(const QVarLengthArray<glyph_t> &glyphs,
       
  2293                                     const QVarLengthArray<QFixedPoint> &positions,
       
  2294                                     const QFontEngineFT *ft)
       
  2295 {
       
  2296     QPainterPath path;
       
  2297     path.setFillRule(Qt::WindingFill);
       
  2298     ft->lockFace();
       
  2299     int i = 0;
       
  2300     while (i < glyphs.size()) {
       
  2301         QFontEngineFT::Glyph *glyph = ft->loadGlyph(glyphs[i], QFontEngineFT::Format_Mono);
       
  2302         // #### fix case where we don't get a glyph
       
  2303         if (!glyph)
       
  2304             break;
       
  2305 
       
  2306         Q_ASSERT(glyph->format == QFontEngineFT::Format_Mono);
       
  2307         int n = 0;
       
  2308         int h = glyph->height;
       
  2309         int xp = qRound(positions[i].x);
       
  2310         int yp = qRound(positions[i].y);
       
  2311 
       
  2312         xp += glyph->x;
       
  2313         yp += -glyph->y + glyph->height;
       
  2314         int pitch = ((glyph->width + 31) & ~31) >> 3;
       
  2315 
       
  2316         uchar *src = glyph->data;
       
  2317         while (h--) {
       
  2318             for (int x = 0; x < glyph->width; ++x) {
       
  2319                 bool set = src[x >> 3] & (0x80 >> (x & 7));
       
  2320                 if (set) {
       
  2321                     QRect r(xp + x, yp - h, 1, 1);
       
  2322                     while (x < glyph->width-1 && src[(x+1) >> 3] & (0x80 >> ((x+1) & 7))) {
       
  2323                         ++x;
       
  2324                         r.setRight(r.right()+1);
       
  2325                     }
       
  2326 
       
  2327                     path.addRect(r);
       
  2328                     ++n;
       
  2329                 }
       
  2330             }
       
  2331             src += pitch;
       
  2332         }
       
  2333         ++i;
       
  2334     }
       
  2335     ft->unlockFace();
       
  2336     return path;
       
  2337 }
       
  2338 
       
  2339 void QX11PaintEngine::drawFreetype(const QPointF &p, const QTextItemInt &ti)
       
  2340 {
       
  2341     Q_D(QX11PaintEngine);
       
  2342     if (!ti.glyphs.numGlyphs)
       
  2343         return;
       
  2344 
       
  2345     QFontEngineX11FT *ft = static_cast<QFontEngineX11FT *>(ti.fontEngine);
       
  2346 
       
  2347     if (!d->cpen.isSolid()) {
       
  2348         QPaintEngine::drawTextItem(p, ti);
       
  2349         return;
       
  2350     }
       
  2351 
       
  2352     const bool xrenderPath = (X11->use_xrender
       
  2353                               && !(d->pdev->devType() == QInternal::Pixmap
       
  2354                                    && static_cast<const QPixmap *>(d->pdev)->data->pixelType() == QPixmapData::BitmapType));
       
  2355 
       
  2356     QVarLengthArray<QFixedPoint> positions;
       
  2357     QVarLengthArray<glyph_t> glyphs;
       
  2358     QTransform matrix;
       
  2359 
       
  2360     if (xrenderPath)
       
  2361         matrix = d->matrix;
       
  2362     matrix.translate(p.x(), p.y());
       
  2363     ft->getGlyphPositions(ti.glyphs, matrix, ti.flags, glyphs, positions);
       
  2364     if (glyphs.count() == 0)
       
  2365         return;
       
  2366 
       
  2367 #ifndef QT_NO_XRENDER
       
  2368     QFontEngineFT::QGlyphSet *set = ft->defaultGlyphs();
       
  2369     if (d->txop >= QTransform::TxScale && xrenderPath)
       
  2370         set = ft->loadTransformedGlyphSet(d->matrix);
       
  2371 
       
  2372     if (!set || set->outline_drawing
       
  2373         || !ft->loadGlyphs(set, glyphs.data(), glyphs.size(), QFontEngineFT::Format_Render))
       
  2374     {
       
  2375         QPaintEngine::drawTextItem(p, ti);
       
  2376         return;
       
  2377     }
       
  2378 
       
  2379     if (xrenderPath) {
       
  2380         GlyphSet glyphSet = set->id;
       
  2381         const QColor &pen = d->cpen.color();
       
  2382         ::Picture src = X11->getSolidFill(d->scrn, pen);
       
  2383         XRenderPictFormat *maskFormat = 0;
       
  2384         if (ft->xglyph_format != PictStandardA1)
       
  2385             maskFormat = XRenderFindStandardFormat(X11->display, ft->xglyph_format);
       
  2386 
       
  2387         enum { t_min = SHRT_MIN, t_max = SHRT_MAX };
       
  2388 
       
  2389         int i = 0;
       
  2390         for (; i < glyphs.size()
       
  2391                  && (positions[i].x < t_min || positions[i].x > t_max
       
  2392                      || positions[i].y < t_min || positions[i].y > t_max);
       
  2393              ++i)
       
  2394             ;
       
  2395 
       
  2396         if (i >= glyphs.size())
       
  2397             return;
       
  2398         ++i;
       
  2399 
       
  2400         QFixed xp = positions[i - 1].x;
       
  2401         QFixed yp = positions[i - 1].y;
       
  2402         QFixed offs = QFixed::fromReal(aliasedCoordinateDelta);
       
  2403 
       
  2404         XGlyphElt32 elt;
       
  2405         elt.glyphset = glyphSet;
       
  2406         elt.chars = &glyphs[i - 1];
       
  2407         elt.nchars = 1;
       
  2408         elt.xOff = qRound(xp + offs);
       
  2409         elt.yOff = qRound(yp + offs);
       
  2410         for (; i < glyphs.size(); ++i) {
       
  2411             if (positions[i].x < t_min || positions[i].x > t_max
       
  2412                 || positions[i].y < t_min || positions[i].y > t_max) {
       
  2413                 break;
       
  2414             }
       
  2415             QFontEngineFT::Glyph *g = ft->cachedGlyph(glyphs[i - 1]);
       
  2416             if (g
       
  2417                 && positions[i].x == xp + g->advance
       
  2418                 && positions[i].y == yp
       
  2419                 && elt.nchars < 253 // don't draw more than 253 characters as some X servers
       
  2420                                     // hang with it
       
  2421                 ) {
       
  2422                 elt.nchars++;
       
  2423                 xp += g->advance;
       
  2424             } else {
       
  2425                 xp = positions[i].x;
       
  2426                 yp = positions[i].y;
       
  2427 
       
  2428                 XRenderCompositeText32(X11->display, PictOpOver, src, d->picture,
       
  2429                                        maskFormat, 0, 0, 0, 0,
       
  2430                                        &elt, 1);
       
  2431                 elt.chars = &glyphs[i];
       
  2432                 elt.nchars = 1;
       
  2433                 elt.xOff = qRound(xp + offs);
       
  2434                 elt.yOff = qRound(yp + offs);
       
  2435             }
       
  2436         }
       
  2437         XRenderCompositeText32(X11->display, PictOpOver, src, d->picture,
       
  2438                                maskFormat, 0, 0, 0, 0,
       
  2439                                &elt, 1);
       
  2440 
       
  2441         return;
       
  2442 
       
  2443     }
       
  2444 #endif
       
  2445 
       
  2446     QPainterPath path = path_for_glyphs(glyphs, positions, ft);
       
  2447     if (path.elementCount() <= 1)
       
  2448         return;
       
  2449     Q_ASSERT((path.elementCount() % 5) == 0);
       
  2450     if (d->txop >= QTransform::TxScale) {
       
  2451         painter()->save();
       
  2452         painter()->setBrush(d->cpen.brush());
       
  2453         painter()->setPen(Qt::NoPen);
       
  2454         painter()->drawPath(path);
       
  2455         painter()->restore();
       
  2456         return;
       
  2457     }
       
  2458 
       
  2459     const int rectcount = 256;
       
  2460     XRectangle rects[rectcount];
       
  2461     int num_rects = 0;
       
  2462 
       
  2463     QPoint delta(qRound(d->matrix.dx()), qRound(d->matrix.dy()));
       
  2464     QRect clip(d->polygonClipper.boundingRect());
       
  2465     for (int i=0; i < path.elementCount(); i+=5) {
       
  2466         int x = qRound(path.elementAt(i).x);
       
  2467         int y = qRound(path.elementAt(i).y);
       
  2468         int w = qRound(path.elementAt(i+1).x) - x;
       
  2469         int h = qRound(path.elementAt(i+2).y) - y;
       
  2470 
       
  2471         QRect rect = QRect(x + delta.x(), y + delta.y(), w, h);
       
  2472         rect = rect.intersected(clip);
       
  2473         if (rect.isEmpty())
       
  2474             continue;
       
  2475 
       
  2476         rects[num_rects].x = short(rect.x());
       
  2477         rects[num_rects].y = short(rect.y());
       
  2478         rects[num_rects].width = ushort(rect.width());
       
  2479         rects[num_rects].height = ushort(rect.height());
       
  2480         ++num_rects;
       
  2481         if (num_rects == rectcount) {
       
  2482             XFillRectangles(d->dpy, d->hd, d->gc, rects, num_rects);
       
  2483             num_rects = 0;
       
  2484         }
       
  2485     }
       
  2486     if (num_rects > 0)
       
  2487         XFillRectangles(d->dpy, d->hd, d->gc, rects, num_rects);
       
  2488 
       
  2489 }
       
  2490 #endif // !QT_NO_XRENDER
       
  2491 
       
  2492 QT_END_NAMESPACE