summaryrefslogtreecommitdiffstats
path: root/src/gui/painting/qwindowsurface_qws.cpp
blob: 3789a331101114bee05708015c6beebcb29ae409 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtGui module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwindowsurface_qws_p.h"
#include <qwidget.h>
#include <qscreen_qws.h>
#include <qwsmanager_qws.h>
#include <qapplication.h>
#include <qwsdisplay_qws.h>
#include <qrgb.h>
#include <qpaintengine.h>
#include <qdesktopwidget.h>
#include <private/qapplication_p.h>
#include <private/qwsdisplay_qws_p.h>
#include <private/qwidget_p.h>
#include <private/qwsmanager_p.h>
#include <private/qwslock_p.h>
#include <private/qbackingstore_p.h>
#include <stdio.h>

QT_BEGIN_NAMESPACE

#ifdef Q_BACKINGSTORE_SUBSURFACES

typedef QMap<int, QWSWindowSurface*> SurfaceMap;
Q_GLOBAL_STATIC(SurfaceMap, winIdToSurfaceMap);

QWSWindowSurface* qt_findWindowSurface(int winId)
{
    return winIdToSurfaceMap()->value(winId);
}

static void qt_insertWindowSurface(int winId, QWSWindowSurface *surface)
{
    if (!surface)
        winIdToSurfaceMap()->remove(winId);
    else
        winIdToSurfaceMap()->insert(winId, surface);
}

#endif // Q_BACKINGSTORE_SUBSURFACES

inline bool isWidgetOpaque(const QWidget *w)
{
    return w->d_func()->isOpaque && !w->testAttribute(Qt::WA_TranslucentBackground);
}

static inline QScreen *getScreen(const QWidget *w)
{
    const QList<QScreen*> subScreens = qt_screen->subScreens();
    if (subScreens.isEmpty())
        return qt_screen;

    const int screen = QApplication::desktop()->screenNumber(w);

    return qt_screen->subScreens().at(screen < 0 ? 0 : screen);
}

static int bytesPerPixel(QImage::Format format)
{
    switch (format) {
    case QImage::Format_Invalid:
        return 0;
#ifndef QT_NO_DEBUG
    case QImage::Format_Mono:
    case QImage::Format_MonoLSB:
        qFatal("QWSWindowSurface: Invalid backingstore format: %i",
               int(format));
#endif
    case QImage::Format_Indexed8:
        return 1;
    case QImage::Format_RGB32:
    case QImage::Format_ARGB32:
    case QImage::Format_ARGB32_Premultiplied:
        return 4;
    case QImage::Format_RGB16:
    case QImage::Format_RGB555:
    case QImage::Format_RGB444:
    case QImage::Format_ARGB4444_Premultiplied:
        return 2;
    case QImage::Format_ARGB8565_Premultiplied:
    case QImage::Format_ARGB8555_Premultiplied:
    case QImage::Format_ARGB6666_Premultiplied:
    case QImage::Format_RGB666:
    case QImage::Format_RGB888:
        return 3;
    default:
#ifndef QT_NO_DEBUG
        qFatal("QWSWindowSurface: Invalid backingstore format: %i",
               int(format));
#endif
        return 0;
    }
}

static inline int nextMulOf4(int n)
{
    return ((n + 3) & 0xfffffffc);
}

static inline void setImageMetrics(QImage &img, QWidget *window) {
    QScreen *myScreen = getScreen(window);
    if (myScreen) {
        int dpmx = myScreen->width()*1000 / myScreen->physicalWidth();
        int dpmy = myScreen->height()*1000 / myScreen->physicalHeight();
        img.setDotsPerMeterX(dpmx);
        img.setDotsPerMeterY(dpmy);
    }
}

void QWSWindowSurface::invalidateBuffer()
{

    QWidget *win = window();
    if (win) {
        win->d_func()->invalidateBuffer(win->rect());
#ifndef QT_NO_QWS_MANAGER
        QTLWExtra *topextra = win->d_func()->extra->topextra;
        QWSManager *manager = topextra->qwsManager;
        if (manager)
            manager->d_func()->dirtyRegion(QDecoration::All,
                                           QDecoration::Normal);
#endif
    }
}

QWSWindowSurfacePrivate::QWSWindowSurfacePrivate()
    : flags(0),
#ifdef QT_QWS_CLIENTBLIT
    directId(-1),
#endif
    winId(0)
{
}

void QWSWindowSurfacePrivate::setWinId(int id)
{
    winId = id;
}

int QWSWindowSurface::winId() const
{
    // XXX: the widget winId may change during the lifetime of the widget!!!

    const QWidget *win = window();
    if (win && win->isWindow())
        return win->internalWinId();

#ifdef Q_BACKINGSTORE_SUBSURFACES
    if (!d_ptr->winId) {
        QWSWindowSurface *that = const_cast<QWSWindowSurface*>(this);
        QWSDisplay *display = QWSDisplay::instance();
        const int id = display->takeId();
        qt_insertWindowSurface(id, that);
        that->d_ptr->winId = id;

        if (win)
            display->nameRegion(id, win->objectName(), win->windowTitle());
        else
            display->nameRegion(id, QString(), QString());

        display->setAltitude(id, 1, true); // XXX
    }
#endif

    return d_ptr->winId;
}

void QWSWindowSurface::setWinId(int id)
{
    d_ptr->winId = id;
}

/*!
    \class QWSWindowSurface
    \since 4.2
    \ingroup qws
    \preliminary
    \internal

    \brief The QWSWindowSurface class provides the drawing area for top-level
    windows in Qt for Embedded Linux.

    Note that this class is only available in Qt for Embedded Linux.

    In \l{Qt for Embedded Linux}, the default behavior is for each client to
    render its widgets into memory while the server is responsible for
    putting the contents of the memory onto the
    screen. QWSWindowSurface is used by the window system to implement
    the associated memory allocation.

    When a screen update is required, the server runs through all the
    top-level windows that intersect with the region that is about to
    be updated, and ensures that the associated clients have updated
    their memory buffer. Then the server uses the screen driver to
    copy the content of the memory to the screen. To locate the
    relevant parts of memory, the driver is provided with the list of
    top-level windows that intersect with the given region. Associated
    with each of the top-level windows there is a window surface
    representing the drawing area of the window.

    When deriving from the QWSWindowSurface class, e.g., when adding
    an \l {Adding an Accelerated Graphics Driver to Qt for Embedded Linux}
    {accelerated graphics driver}, there are several pure virtual
    functions that must be implemented. In addition, QWSWindowSurface
    provides several virtual functions that can be reimplemented to
    customize the drawing process.

    \tableofcontents

    \section1 Pure Virtual Functions

    There are in fact two window surface instances for each top-level
    window; one used by the application when drawing a window, and
    another used by the server application to perform window
    compositioning. Implement the attach() to create the server-side
    representation of the surface. The data() function must be
    implemented to provide the required data.

    Implement the key() function to uniquely identify the surface
    class, and the isValid() function to determine is a surface
    corresponds to a given widget.

    The geometry() function must be implemented to let the window
    system determine the area required by the window surface
    (QWSWindowSurface also provides a corresponding virtual
    setGeometry() function that is called whenever the area necessary
    for the top-level window to be drawn, changes). The image()
    function is called by the window system during window
    compositioning, and must be implemented to return an image of the
    top-level window.

    Finally, the paintDevice() function must be implemented to return
    the appropriate paint device, and the scroll() function must be
    implemented to scroll the given region of the surface the given
    number of pixels.

    \section1 Virtual Functions

    When painting onto the surface, the window system will always call
    the beginPaint() function before any painting operations are
    performed. Likewise the endPaint() function is automatically
    called when the painting is done. Reimplement the painterOffset()
    function to alter the offset that is applied when drawing.

    The window system uses the flush() function to put a given region
    of the widget onto the screen, and the release() function to
    deallocate the screen region corresponding to this window surface.

    \section1 Other Members

    QWSWindowSurface provides the window() function returning a
    pointer to the top-level window the surface is representing. The
    currently visible region of the associated widget can be retrieved
    and set using the clipRegion() and setClipRegion() functions,
    respectively.

    When the window system performs the window compositioning, it uses
    the SurfaceFlag enum describing the surface content. The currently
    set surface flags can be retrieved and altered using the
    surfaceFlags() and setSurfaceFlags() functions. In addition,
    QWSWindowSurface provides the isBuffered(), isOpaque() and
    isRegionReserved() convenience functions.

    \sa {Qt for Embedded Linux Architecture#Drawing on Screen}{Qt for
    Embedded Linux Architecture}
*/

/*!
    \enum QWSWindowSurface::SurfaceFlag

    This enum is used to describe the window surface's contents.  It
    is used by the screen driver to handle region allocation and
    composition.

    \value RegionReserved The surface contains a reserved area. Once
    allocated, a reserved area can not not be changed by the window
    system, i.e., no other widgets can be drawn on top of this.

    \value Buffered
    The surface is in a memory area which is not part of a framebuffer.
    (A top-level window with QWidget::windowOpacity() other than 1.0 must use
    a buffered surface in order to making blending with the background work.)

    \value Opaque
    The surface contains only opaque pixels.

    \sa surfaceFlags(), setSurfaceFlags()
*/

/*!
    \fn bool QWSWindowSurface::isValid() const
    \since 4.3

    Implement this function to return true if the surface is a valid
    surface for the given top-level \a window; otherwise return
    false.

    \sa window(), key()
*/

/*!
    \fn QString QWSWindowSurface::key() const

    Implement this function to return a string that uniquely
    identifies the class of this surface.

    \sa window(), isValid()
*/

/*!
    \fn QByteArray QWSWindowSurface::permanentState() const
    \since 4.3

    Implement this function to return the data required for creating a
    server-side representation of the surface.

    \sa attach()
*/

/*!
    \fn void QWSWindowSurface::setPermanentState(const QByteArray &data)
    \since 4.3

    Implement this function to attach a server-side surface instance
    to the corresponding client side instance using the given \a
    data. Return true if successful; otherwise return false.

    \sa data()
*/

/*!
    \fn const QImage QWSWindowSurface::image() const

    Implement this function to return an image of the top-level window.

    \sa geometry()
*/

/*!
    \fn bool QWSWindowSurface::isRegionReserved() const

    Returns true if the QWSWindowSurface::RegionReserved is set; otherwise
    returns false.

    \sa surfaceFlags()
*/

/*!
    \fn bool QWSWindowSurface::isBuffered() const

    Returns true if the QWSWindowSurface::Buffered is set; otherwise returns false.

    \sa surfaceFlags()
*/

/*!
    \fn bool QWSWindowSurface::isOpaque() const

    Returns true if the QWSWindowSurface::Opaque is set; otherwise
    returns false.

    \sa surfaceFlags()
*/


/*!
    Constructs an empty surface.
*/
QWSWindowSurface::QWSWindowSurface()
    : QWindowSurface(0), d_ptr(new QWSWindowSurfacePrivate)
{
}

/*!
    Constructs an empty surface for the given top-level \a widget.
*/
QWSWindowSurface::QWSWindowSurface(QWidget *widget)
    : QWindowSurface(widget), d_ptr(new QWSWindowSurfacePrivate)
{
}

QWSWindowSurface::~QWSWindowSurface()
{
#ifdef Q_BACKINGSTORE_SUBSURFACES
    if (d_ptr->winId)
        winIdToSurfaceMap()->remove(d_ptr->winId);
#endif

    delete d_ptr;
}

/*!
    Returns the offset to be used when painting.

    \sa paintDevice()
*/
QPoint QWSWindowSurface::painterOffset() const
{
    const QWidget *w = window();
    if (!w)
        return QPoint();
    return w->geometry().topLeft() - w->frameGeometry().topLeft();
}

void QWSWindowSurface::beginPaint(const QRegion &)
{
    lock();
}

void QWSWindowSurface::endPaint(const QRegion &)
{
    unlock();
}

// XXX: documentation!!!
QByteArray QWSWindowSurface::transientState() const
{
    return QByteArray();
}

QByteArray QWSWindowSurface::permanentState() const
{
    return QByteArray();
}

void QWSWindowSurface::setTransientState(const QByteArray &state)
{
    Q_UNUSED(state);
}

void QWSWindowSurface::setPermanentState(const QByteArray &state)
{
    Q_UNUSED(state);
}

bool QWSWindowSurface::lock(int timeout)
{
    Q_UNUSED(timeout);
    return true;
}

void QWSWindowSurface::unlock()
{
}

#ifdef QT_QWS_CLIENTBLIT
/*! \internal */
const QRegion QWSWindowSurface::directRegion() const
{
    return d_ptr->direct;
}

/*! \internal */
int QWSWindowSurface::directRegionId() const
{
    return d_ptr->directId;
}

/*! \internal */
void QWSWindowSurface::setDirectRegion(const QRegion &r, int id)
{
    d_ptr->direct = r;
    d_ptr->directId = id;
}
#endif

/*!
    Returns the region currently visible on the screen.

    \sa setClipRegion()
*/
const QRegion QWSWindowSurface::clipRegion() const
{
    return d_ptr->clip;
}

/*!
    Sets the region currently visible on the screen to be the given \a
    clip region.

    \sa clipRegion()
*/
void QWSWindowSurface::setClipRegion(const QRegion &clip)
{
    if (clip == d_ptr->clip)
        return;

    QRegion expose = (clip - d_ptr->clip);
    d_ptr->clip = clip;

    if (expose.isEmpty() || clip.isEmpty())
        return; // No repaint or flush required.

    QWidget *win = window();
    if (!win)
        return;

    if (isBuffered()) {
        // No repaint required. Flush exposed area via the backing store.
        win->d_func()->syncBackingStore(expose);
        return;
    }

#ifndef QT_NO_QWS_MANAGER
    // Invalidate exposed decoration area.
    if (win && win->isWindow()) {
        QTLWExtra *topextra = win->d_func()->extra->topextra;
        if (QWSManager *manager = topextra->qwsManager) {
            QRegion decorationExpose(manager->region());
            decorationExpose.translate(-win->geometry().topLeft());
            decorationExpose &= expose;
            if (!decorationExpose.isEmpty()) {
                expose -= decorationExpose;
                manager->d_func()->dirtyRegion(QDecoration::All, QDecoration::Normal, decorationExpose);
            }
        }
    }
#endif

    // Invalidate exposed widget area.
    win->d_func()->invalidateBuffer(expose);
}

/*!
    Returns the surface flags describing the contents of this surface.

    \sa isBuffered(), isOpaque(), isRegionReserved()
*/
QWSWindowSurface::SurfaceFlags QWSWindowSurface::surfaceFlags() const
{
    return d_ptr->flags;
}

/*!
    Sets the surface flags describing the contents of this surface, to
    be the given \a flags.

    \sa surfaceFlags()
*/
void QWSWindowSurface::setSurfaceFlags(SurfaceFlags flags)
{
    d_ptr->flags = flags;
}

void QWSWindowSurface::setGeometry(const QRect &rect)
{
    QRegion mask = rect;

    const QWidget *win = window();
    if (win) {
#ifndef QT_NO_QWS_MANAGER
        if (win->isWindow()) {
            QTLWExtra *topextra = win->d_func()->extra->topextra;
            QWSManager *manager = topextra->qwsManager;

            if (manager) {
                // The frame geometry is the bounding rect of manager->region,
                // which could be too much, so we need to clip.
                mask &= (manager->region() + win->geometry());
            }
        }
#endif

        const QRegion winMask = win->mask();
        if (!winMask.isEmpty())
            mask &= winMask.translated(win->geometry().topLeft());
    }

    setGeometry(rect, mask);
}

void QWSWindowSurface::setGeometry(const QRect &rect, const QRegion &mask)
{
    if (rect == geometry()) // XXX: && mask == prevMask
        return;

    const bool isResize = rect.size() != geometry().size();
    const bool needsRepaint = isResize || !isBuffered();

    QWindowSurface::setGeometry(rect);

    const QRegion region = mask & rect;
    QWidget *win = window();
    // Only request regions for widgets visible on the screen.
    // (Added the !win check for compatibility reasons, because
    // there was no "if (win)" check before).
    const bool requestQWSRegion = !win || !win->testAttribute(Qt::WA_DontShowOnScreen);
    if (requestQWSRegion)
        QWidget::qwsDisplay()->requestRegion(winId(), key(), permanentState(), region);

    if (needsRepaint)
        invalidateBuffer();

    if (!requestQWSRegion) {
        // We didn't request a region, hence we won't get a QWSRegionEvent::Allocation
        // event back from the server so we set the clip directly. We have to
        // do this after the invalidateBuffer() call above, as it might trigger a
        // backing store sync, resulting in too many update requests.
        setClipRegion(region);
    }
}

static inline void flushUpdate(QWidget *widget, const QRegion &region,
                               const QPoint &offset)
{
#ifdef QT_NO_PAINT_DEBUG
    Q_UNUSED(widget);
    Q_UNUSED(region);
    Q_UNUSED(offset);
#else
    static int delay = -1;

    if (delay == -1)
        delay = qgetenv("QT_FLUSH_UPDATE").toInt() * 10;

    if (delay == 0)
        return;

    static QWSYellowSurface surface(true);
    surface.setDelay(delay);
    surface.flush(widget, region, offset);
#endif // QT_NO_PAINT_DEBUG
}

void QWSWindowSurface::flush(QWidget *widget, const QRegion &region,
                             const QPoint &offset)
{
    const QWidget *win = window();
    if (!win)
        return;

#ifndef QT_NO_GRAPHICSVIEW
    QWExtra *extra = win->d_func()->extra;
    if (extra && extra->proxyWidget)
        return;
#endif //QT_NO_GRAPHICSVIEW

    Q_UNUSED(offset);

    const bool opaque = isOpaque();
#ifdef QT_QWS_DISABLE_FLUSHCLIPPING
    QRegion toFlush = region;
#else
    QRegion toFlush = region & d_ptr->clip;
#endif

    if (!toFlush.isEmpty()) {
        flushUpdate(widget, toFlush, QPoint(0, 0));
        QPoint globalZero = win->mapToGlobal(QPoint(0, 0));
        toFlush.translate(globalZero);

#ifdef QT_QWS_CLIENTBLIT
        bool needRepaint = true;
        if (opaque) {
            QScreen* widgetScreen = getScreen(widget);
            if (widgetScreen->supportsBlitInClients()) {

                QWSDisplay::grab();
                if(directRegion().intersected(toFlush) == toFlush) {
                    QPoint translate = -globalZero + painterOffset() + geometry().topLeft();
                    QRegion flushRegion = toFlush.translated(translate);
                    widgetScreen->blit(image(), geometry().topLeft(), flushRegion);
                    widgetScreen->setDirty(toFlush.boundingRect());
                    needRepaint = false;
                }
                QWSDisplay::ungrab();
            }
        }

        if(needRepaint)
#endif
            win->qwsDisplay()->repaintRegion(winId(), win->windowFlags(), opaque, toFlush);
    }
}

/*!
    Move the surface with the given \a offset.

    A subclass may reimplement this function to enable accelerated window move.
    It must return true if the move was successful and no repaint is necessary,
    false otherwise.

    The default implementation updates the QWindowSurface geometry and
    returns true if the surface is buffered; false otherwise.

    This function is called by the window system on the client instance.

    \sa isBuffered()
*/
bool QWSWindowSurface::move(const QPoint &offset)
{
    QWindowSurface::setGeometry(geometry().translated(offset));
    return isBuffered();
}

/*!
    Move the surface with the given \a offset.

    The new visible region after the window move is given by \a newClip
    in screen coordinates.

    A subclass may reimplement this function to enable accelerated window move.
    The returned region indicates the area that still needs to be composed
    on the screen.

    The default implementation updates the QWindowSurface geometry and
    returns the union of the old and new geometry.

    This function is called by the window system on the server instance.
*/
QRegion QWSWindowSurface::move(const QPoint &offset, const QRegion &newClip)
{
    const QRegion oldGeometry = geometry();
    QWindowSurface::setGeometry(geometry().translated(offset));
    return oldGeometry + newClip;
}

void QWSWindowSurface::releaseSurface()
{
}

bool QWSMemorySurface::lock(int timeout)
{
    Q_UNUSED(timeout);
#ifndef QT_NO_QWS_MULTIPROCESS
    if (memlock && !memlock->lock(QWSLock::BackingStore))
        return false;
#endif
#ifndef QT_NO_THREAD
    threadLock.lock();
#endif
    return true;
}

void QWSMemorySurface::unlock()
{
#ifndef QT_NO_THREAD
    threadLock.unlock();
#endif
#ifndef QT_NO_QWS_MULTIPROCESS
    if (memlock)
        memlock->unlock(QWSLock::BackingStore);
#endif
}

QWSMemorySurface::QWSMemorySurface()
    : QWSWindowSurface()
#ifndef QT_NO_QWS_MULTIPROCESS
    , memlock(0)
#endif
{
    setSurfaceFlags(Buffered);
}

QWSMemorySurface::QWSMemorySurface(QWidget *w)
    : QWSWindowSurface(w)
{
    SurfaceFlags flags = Buffered;
    if (isWidgetOpaque(w))
        flags |= Opaque;
    setSurfaceFlags(flags);

#ifndef QT_NO_QWS_MULTIPROCESS
    memlock = QWSDisplay::Data::getClientLock();
#endif
}

QWSMemorySurface::~QWSMemorySurface()
{
#ifndef QT_NO_QWS_MULTIPROCESS
    if (memlock != QWSDisplay::Data::getClientLock())
        delete memlock;
#endif
}


QImage::Format
QWSMemorySurface::preferredImageFormat(const QWidget *widget) const
{
    QScreen *screen = getScreen(widget);
    const int depth = screen->depth();
    const bool opaque = isWidgetOpaque(widget);

    if (!opaque) {
        if (depth <= 12)
            return QImage::Format_ARGB4444_Premultiplied;
        else if (depth <= 15)
            return QImage::Format_ARGB8555_Premultiplied;
        else if (depth <= 16)
            return QImage::Format_ARGB8565_Premultiplied;
        else if (depth <= 18)
            return QImage::Format_ARGB6666_Premultiplied;
        else
            return QImage::Format_ARGB32_Premultiplied;
    }

    QImage::Format format = screen->pixelFormat();
    if (format > QImage::Format_Indexed8) // ### assumes all new image formats supports a QPainter
        return format;

    if (depth <= 12)
        return QImage::Format_RGB444;
    else if (depth <= 15)
        return QImage::Format_RGB555;
    else if (depth <= 16)
        return QImage::Format_RGB16;
    else if (depth <= 18)
        return QImage::Format_RGB666;
    else if (depth <= 24)
        return QImage::Format_RGB888;
    else
        return QImage::Format_ARGB32_Premultiplied;
}

#ifndef QT_NO_QWS_MULTIPROCESS
void QWSMemorySurface::setLock(int lockId)
{
    if (memlock && memlock->id() == lockId)
        return;
    if (memlock != QWSDisplay::Data::getClientLock())
        delete memlock;
    memlock = (lockId == -1 ? 0 : new QWSLock(lockId));
}
#endif // QT_NO_QWS_MULTIPROCESS

bool QWSMemorySurface::isValid() const
{
    if (img.isNull())
        return true;

    const QWidget *win = window();
    if (preferredImageFormat(win) != img.format())
        return false;

    if (isOpaque() != isWidgetOpaque(win)) // XXX: use QWidgetPrivate::isOpaque()
        return false;

    return true;
}

// ### copied from qwindowsurface_raster.cpp -- should be cross-platform
void QWSMemorySurface::beginPaint(const QRegion &rgn)
{
    if (!isWidgetOpaque(window())) {
        QPainter p(&img);
        p.setCompositionMode(QPainter::CompositionMode_Source);
        const QVector<QRect> rects = rgn.rects();
        const QColor blank = Qt::transparent;
        for (QVector<QRect>::const_iterator it = rects.begin(); it != rects.end(); ++it) {
            QRect r = *it;
#ifdef Q_BACKINGSTORE_SUBSURFACES
            r.translate(painterOffset());
#endif
            p.fillRect(r, blank);
        }
    }
    QWSWindowSurface::beginPaint(rgn);
}

// from qwindowsurface.cpp
extern void qt_scrollRectInImage(QImage &img, const QRect &rect, const QPoint &offset);

bool QWSMemorySurface::scroll(const QRegion &area, int dx, int dy)
{
    const QVector<QRect> rects = area.rects();
    for (int i = 0; i < rects.size(); ++i)
        qt_scrollRectInImage(img, rects.at(i), QPoint(dx, dy));

    return true;
}

QPoint QWSMemorySurface::painterOffset() const
{
    const QWidget *w = window();
    if (!w)
        return QPoint();

    if (w->mask().isEmpty())
        return QWSWindowSurface::painterOffset();

    const QRegion region = w->mask()
                           & w->frameGeometry().translated(-w->geometry().topLeft());
    return -region.boundingRect().topLeft();
}

QWSLocalMemSurface::QWSLocalMemSurface()
    : QWSMemorySurface(), mem(0), memsize(0)
{
}

QWSLocalMemSurface::QWSLocalMemSurface(QWidget *w)
    : QWSMemorySurface(w), mem(0), memsize(0)
{
}

QWSLocalMemSurface::~QWSLocalMemSurface()
{
    if (memsize)
        delete[] mem;
}

void QWSLocalMemSurface::setGeometry(const QRect &rect)
{
    QSize size = rect.size();

    QWidget *win = window();
    if (win && !win->mask().isEmpty()) {
        const QRegion region = win->mask()
                               & rect.translated(-win->geometry().topLeft());
        size = region.boundingRect().size();
    }

    uchar *deleteLater = 0;

    if (img.size() != size) {
        if (size.isEmpty()) {
            if (memsize) {
                // In case of a Hide event we need to delete the memory after sending the
                // event to the server in order to let the server animate the event.
                deleteLater = mem;
                memsize = 0;
            }
            mem = 0;
            img = QImage();
        } else {
            const QImage::Format format = preferredImageFormat(win);
            const int bpl = nextMulOf4(bytesPerPixel(format) * size.width());
            const int imagesize = bpl * size.height();
            if (memsize < imagesize) {
                delete[] mem;
                memsize = imagesize;
                mem = new uchar[memsize];
            }
            img = QImage(mem, size.width(), size.height(), bpl, format);
            setImageMetrics(img, win);
        }
    }

    QWSWindowSurface::setGeometry(rect);

    delete[] deleteLater;
}

QByteArray QWSLocalMemSurface::permanentState() const
{
    QByteArray array(sizeof(uchar*) + 3 * sizeof(int) + sizeof(SurfaceFlags), Qt::Uninitialized);

    char *ptr = array.data();

    *reinterpret_cast<uchar**>(ptr) = mem;
    ptr += sizeof(uchar*);

    reinterpret_cast<int*>(ptr)[0] = img.width();
    reinterpret_cast<int*>(ptr)[1] = img.height();
    ptr += 2 * sizeof(int);

    *reinterpret_cast<int *>(ptr) = img.format();
    ptr += sizeof(int);

    *reinterpret_cast<SurfaceFlags*>(ptr) = surfaceFlags();

    return array;
}

void QWSLocalMemSurface::setPermanentState(const QByteArray &data)
{
    if (memsize) {
        delete[] mem;
        memsize = 0;
    }

    int width;
    int height;
    QImage::Format format;
    SurfaceFlags flags;

    const char *ptr = data.constData();

    mem = *reinterpret_cast<uchar* const*>(ptr);
    ptr += sizeof(uchar*);

    width = reinterpret_cast<const int*>(ptr)[0];
    height = reinterpret_cast<const int*>(ptr)[1];
    ptr += 2 * sizeof(int);

    format = QImage::Format(*reinterpret_cast<const int*>(ptr));
    ptr += sizeof(int);

    flags = *reinterpret_cast<const SurfaceFlags*>(ptr);

    const int bpl = nextMulOf4(bytesPerPixel(format) * width);
    QWSMemorySurface::img = QImage(mem, width, height, bpl, format);
    setSurfaceFlags(flags);
}

void QWSLocalMemSurface::releaseSurface()
{
    if (memsize) {
        delete[] mem;
        memsize = 0;
    }
    mem = 0;
    img = QImage();
}

#ifndef QT_NO_QWS_MULTIPROCESS

QWSSharedMemSurface::QWSSharedMemSurface()
    : QWSMemorySurface()
{
}

QWSSharedMemSurface::QWSSharedMemSurface(QWidget *widget)
    : QWSMemorySurface(widget)
{
}

QWSSharedMemSurface::~QWSSharedMemSurface()
{
    // mem.detach() is done automatically by ~QSharedMemory
}

bool QWSSharedMemSurface::setMemory(int memId)
{
    if (mem.id() == memId)
        return true;

    mem.detach();

    if (memId != -1 && !mem.attach(memId)) {
#ifndef QT_NO_DEBUG
        perror("QWSSharedMemSurface: attaching to shared memory");
        qCritical("QWSSharedMemSurface: Error attaching to shared memory 0x%x", memId);
#endif
        return false;
    }

    return true;
}

#ifdef QT_QWS_CLIENTBLIT
void QWSSharedMemSurface::setDirectRegion(const QRegion &r, int id)
{
    QWSMemorySurface::setDirectRegion(r, id);
    if (mem.address())
        *(uint *)mem.address() = id;
}

const QRegion QWSSharedMemSurface::directRegion() const
{
    if (mem.address() && *(uint *)mem.address() == uint(directRegionId())
        return QWSMemorySurface::directRegion();
    return QRegion();
}
#endif

void QWSSharedMemSurface::setPermanentState(const QByteArray &data)
{
    int memId;
    int width;
    int height;
    int lockId;
    QImage::Format format;
    SurfaceFlags flags;

    const int *ptr = reinterpret_cast<const int*>(data.constData());

    memId = ptr[0];
    width = ptr[1];
    height = ptr[2];
    lockId = ptr[3];
    format = QImage::Format(ptr[4]);
    flags = SurfaceFlags(ptr[5]);

    setSurfaceFlags(flags);
    setMemory(memId);
    setLock(lockId);

#ifdef QT_QWS_CLIENTBLIT
    uchar *base = static_cast<uchar*>(mem.address()) + sizeof(uint);
#else
    uchar *base = static_cast<uchar*>(mem.address());
#endif
    const int bpl = nextMulOf4(bytesPerPixel(format) * width);
    QWSMemorySurface::img = QImage(base, width, height, bpl, format);
}

void QWSSharedMemSurface::setGeometry(const QRect &rect)
{
    const QSize size = rect.size();
    if (img.size() != size) {
        if (size.isEmpty()) {
            mem.detach();
            img = QImage();
        } else {
            QWidget *win = window();
            const QImage::Format format = preferredImageFormat(win);
            const int bpl = nextMulOf4(bytesPerPixel(format) * size.width());
#ifdef QT_QWS_CLIENTBLIT
            const int imagesize = bpl * size.height() + sizeof(uint);
#else
            const int imagesize = bpl * size.height();
#endif
            if (mem.size() < imagesize) {
                mem.detach();
                if (!mem.create(imagesize)) {
                    perror("QWSSharedMemSurface::setGeometry allocating shared memory");
                    qFatal("Error creating shared memory of size %d", imagesize);
                }
            }
#ifdef QT_QWS_CLIENTBLIT
            *((uint *)mem.address()) = 0;
            uchar *base = static_cast<uchar*>(mem.address()) + sizeof(uint);
#else
            uchar *base = static_cast<uchar*>(mem.address());
#endif
            img = QImage(base, size.width(), size.height(), bpl, format);
            setImageMetrics(img, win);
        }
    }

    QWSWindowSurface::setGeometry(rect);
}

QByteArray QWSSharedMemSurface::permanentState() const
{
    QByteArray array(6 * sizeof(int), Qt::Uninitialized);

    int *ptr = reinterpret_cast<int*>(array.data());

    ptr[0] = mem.id();
    ptr[1] = img.width();
    ptr[2] = img.height();
    ptr[3] = (memlock ? memlock->id() : -1);
    ptr[4] = int(img.format());
    ptr[5] = int(surfaceFlags());

    return array;
}

void QWSSharedMemSurface::releaseSurface()
{
    mem.detach();
    img = QImage();
}

#endif // QT_NO_QWS_MULTIPROCESS

#ifndef QT_NO_PAINTONSCREEN

QWSOnScreenSurface::QWSOnScreenSurface(QWidget *w)
    : QWSMemorySurface(w)
{
    attachToScreen(getScreen(w));
    setSurfaceFlags(Opaque);
}

QWSOnScreenSurface::QWSOnScreenSurface()
    : QWSMemorySurface()
{
    setSurfaceFlags(Opaque);
}

void QWSOnScreenSurface::attachToScreen(const QScreen *s)
{
    screen = s;
    uchar *base = screen->base();
    QImage::Format format  = screen->pixelFormat();

    if (format == QImage::Format_Invalid || format == QImage::Format_Indexed8) {
        //### currently we have no paint engine for indexed image formats
        qFatal("QWSOnScreenSurface::attachToScreen(): screen depth %d "
               "not implemented", screen->depth());
        return;
    }
    QWSMemorySurface::img = QImage(base, screen->width(), screen->height(),
                                   screen->linestep(), format );
}

QWSOnScreenSurface::~QWSOnScreenSurface()
{
}

QPoint QWSOnScreenSurface::painterOffset() const
{
    return geometry().topLeft() + QWSWindowSurface::painterOffset();
}

bool QWSOnScreenSurface::isValid() const
{
    const QWidget *win = window();
    if (screen != getScreen(win))
        return false;
    if (img.isNull())
        return false;
    return QScreen::isWidgetPaintOnScreen(win);
}

QByteArray QWSOnScreenSurface::permanentState() const
{
    QByteArray array(sizeof(int), Qt::Uninitialized);

    int *ptr = reinterpret_cast<int*>(array.data());
    ptr[0] = QApplication::desktop()->screenNumber(window());
    return array;
}

void QWSOnScreenSurface::setPermanentState(const QByteArray &data)
{
    const int *ptr = reinterpret_cast<const int*>(data.constData());
    const int screenNo = ptr[0];

    QScreen *screen = qt_screen;
    if (screenNo > 0)
        screen = qt_screen->subScreens().at(screenNo);
    attachToScreen(screen);
}

#endif // QT_NO_PAINTONSCREEN

#ifndef QT_NO_PAINT_DEBUG

QWSYellowSurface::QWSYellowSurface(bool isClient)
    : QWSWindowSurface(), delay(10)
{
    if (isClient) {
        setWinId(QWidget::qwsDisplay()->takeId());
        QWidget::qwsDisplay()->nameRegion(winId(),
                                          QLatin1String("Debug flush paint"),
                                          QLatin1String("Silly yellow thing"));
        QWidget::qwsDisplay()->setAltitude(winId(), 1, true);
    }
    setSurfaceFlags(Buffered);
}

QWSYellowSurface::~QWSYellowSurface()
{
}

QByteArray QWSYellowSurface::permanentState() const
{
    QByteArray array(2 * sizeof(int), Qt::Uninitialized);

    int *ptr = reinterpret_cast<int*>(array.data());
    ptr[0] = surfaceSize.width();
    ptr[1] = surfaceSize.height();

    return array;
}

void QWSYellowSurface::setPermanentState(const QByteArray &data)
{
    const int *ptr = reinterpret_cast<const int*>(data.constData());

    const int width = ptr[0];
    const int height = ptr[1];

    img = QImage(width, height, QImage::Format_ARGB32);
    img.fill(qRgba(255,255,31,127));
}

void QWSYellowSurface::flush(QWidget *widget, const QRegion &region,
                             const QPoint &offset)
{
    Q_UNUSED(offset);

    QWSDisplay *display = QWidget::qwsDisplay();
    QRegion rgn = region;

    if (widget)
        rgn.translate(widget->mapToGlobal(QPoint(0, 0)));

    surfaceSize = region.boundingRect().size();

    const int id = winId();
    display->requestRegion(id, key(), permanentState(), rgn);
    display->setAltitude(id, 1, true);
    display->repaintRegion(id, 0, false, rgn);

    ::usleep(500 * delay);
    display->requestRegion(id, key(), permanentState(), QRegion());
    ::usleep(500 * delay);
}

#endif // QT_NO_PAINT_DEBUG

#ifndef QT_NO_DIRECTPAINTER

static inline QScreen *getPrimaryScreen()
{
    QScreen *screen = QScreen::instance();
    if (!screen->base()) {
        QList<QScreen*> subScreens = screen->subScreens();
        if (subScreens.size() < 1)
            return 0;
        screen = subScreens.at(0);
    }
    return screen;
}

QWSDirectPainterSurface::QWSDirectPainterSurface(bool isClient,
                                                 QDirectPainter::SurfaceFlag flags)
    : QWSWindowSurface(), flushingRegionEvents(false), doLocking(false)
{
    setSurfaceFlags(Opaque);
    synchronous = (flags == QDirectPainter::ReservedSynchronous);

    if (isClient) {
        setWinId(QWidget::qwsDisplay()->takeId());
        QWidget::qwsDisplay()->nameRegion(winId(),
                                          QLatin1String("QDirectPainter reserved space"),
                                          QLatin1String("reserved"));
    } else {
        setWinId(0);
    }
    _screen = QScreen::instance();
    if (!_screen->base()) {
        QList<QScreen*> subScreens = _screen->subScreens();
        if (subScreens.size() < 1)
            _screen = 0;
        else
            _screen = subScreens.at(0);
    }
}

QWSDirectPainterSurface::~QWSDirectPainterSurface()
{
    if (winId() && QWSDisplay::instance()) // make sure not in QApplication destructor
        QWidget::qwsDisplay()->destroyRegion(winId());
}

void QWSDirectPainterSurface::setRegion(const QRegion &region)
{
    const int id = winId();
    QWidget::qwsDisplay()->requestRegion(id, key(), permanentState(), region);
#ifndef QT_NO_QWS_MULTIPROCESS
    if (synchronous)
        QWSDisplay::instance()->d->waitForRegionAck(id);
#endif
}

void QWSDirectPainterSurface::flush(QWidget *, const QRegion &r, const QPoint &)
{
    QWSDisplay::instance()->repaintRegion(winId(), 0, true, r);
}

QByteArray QWSDirectPainterSurface::permanentState() const
{
    QByteArray res;
    if (isRegionReserved())
        res.append( 'r');
    return res;
}

void QWSDirectPainterSurface::setPermanentState(const QByteArray &ba)
{
    if (ba.size() > 0 && ba.at(0) == 'r')
        setReserved();
    setSurfaceFlags(surfaceFlags() | Opaque);
}

void QWSDirectPainterSurface::beginPaint(const QRegion &region)
{
    QWSWindowSurface::beginPaint(region);
#ifndef QT_NO_QWS_MULTIPROCESS
    if (!synchronous) {
        flushingRegionEvents = true;
        QWSDisplay::instance()->d->waitForRegionEvents(winId(), doLocking);
        flushingRegionEvents = false;
    }
#endif
}

bool QWSDirectPainterSurface::hasPendingRegionEvents() const
{
#ifndef QT_NO_QWS_MULTIPROCESS
    if (synchronous)
        return false;

    return QWSDisplay::instance()->d->hasPendingRegionEvents();
#else
    return false;
#endif
}

bool QWSDirectPainterSurface::lock(int timeout)
{
#ifndef QT_NO_THREAD
    threadLock.lock();
#endif
    Q_UNUSED(timeout);
    if (doLocking)
        QWSDisplay::grab(true);
    return true;
}

void QWSDirectPainterSurface::unlock()
{
    if (doLocking)
        QWSDisplay::ungrab();
#ifndef QT_NO_THREAD
    threadLock.unlock();
#endif
}

#endif // QT_NO_DIRECTPAINTER

QT_END_NAMESPACE