summaryrefslogtreecommitdiffstats
path: root/common/gdi_canvas.c
blob: 76a7674065e843e5cc3bbd27836302d9ef248abd (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
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
/* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/

#include <Windows.h>
#include <Wingdi.h>
#include "gdi_canvas.h"
#define GDI_CANVAS
#include "canvas_base.c"
#include "rop3.h"
#include "rect.h"
#include "region.h"
#include "threads.h"

typedef struct GdiCanvas GdiCanvas;

struct GdiCanvas {
    CanvasBase base;
    HDC dc;
    RecurciveMutex* lock;
};


struct BitmapData {
    HBITMAP hbitmap;
    HBITMAP prev_hbitmap;
    SpicePoint pos;
    uint8_t flags;
    HDC dc;
    int cache;
    int from_surface;
};

#define _rop3_brush 0xf0
#define _rop3_src 0xcc
#define _rop3_dest 0xaa

uint32_t raster_ops[] = {
    0x00000042,
    0x00010289,
    0x00020C89,
    0x000300AA,
    0x00040C88,
    0x000500A9,
    0x00060865,
    0x000702C5,
    0x00080F08,
    0x00090245,
    0x000A0329,
    0x000B0B2A,
    0x000C0324,
    0x000D0B25,
    0x000E08A5,
    0x000F0001,
    0x00100C85,
    0x001100A6,
    0x00120868,
    0x001302C8,
    0x00140869,
    0x001502C9,
    0x00165CCA,
    0x00171D54,
    0x00180D59,
    0x00191CC8,
    0x001A06C5,
    0x001B0768,
    0x001C06CA,
    0x001D0766,
    0x001E01A5,
    0x001F0385,
    0x00200F09,
    0x00210248,
    0x00220326,
    0x00230B24,
    0x00240D55,
    0x00251CC5,
    0x002606C8,
    0x00271868,
    0x00280369,
    0x002916CA,
    0x002A0CC9,
    0x002B1D58,
    0x002C0784,
    0x002D060A,
    0x002E064A,
    0x002F0E2A,
    0x0030032A,
    0x00310B28,
    0x00320688,
    0x00330008,
    0x003406C4,
    0x00351864,
    0x003601A8,
    0x00370388,
    0x0038078A, // PSDPoax
    0x00390604, // SPDnox
    0x003A0644, // SPDSxox
    0x003B0E24, // SPDnoan
    0x003C004A, // PSx
    0x003D18A4, // SPDSonox
    0x003E1B24, // SPDSnaox
    0x003F00EA, // PSan
    0x00400F0A, // PSDnaa
    0x00410249, // DPSxon
    0x00420D5D, // SDxPDxa
    0x00431CC4, // SPDSanaxn
    0x00440328, // SDna SRCERASE
    0x00450B29, // DPSnaon
    0x004606C6, // DSPDaox
    0x0047076A, // PSDPxaxn
    0x00480368, // SDPxa
    0x004916C5, // PDSPDaoxxn
    0x004A0789, // DPSDoax
    0x004B0605, // PDSnox
    0x004C0CC8, // SDPana
    0x004D1954, // SSPxDSxoxn
    0x004E0645, // PDSPxox
    0x004F0E25, // PDSnoan
    0x00500325, // PDna
    0x00510B26, // DSPnaon
    0x005206C9, // DPSDaox
    0x00530764, // SPDSxaxn
    0x005408A9, // DPSonon
    0x00550009, // Dn DSTINVERT
    0x005601A9, // DPSox
    0x00570389, // DPSoan
    0x00580785, // PDSPoax
    0x00590609, // DPSnox
    0x005A0049, // DPx PATINVERT
    0x005B18A9, // DPSDonox
    0x005C0649, // DPSDxox
    0x005D0E29, // DPSnoan
    0x005E1B29, // DPSDnaox
    0x005F00E9, // DPan
    0x00600365, // PDSxa
    0x006116C6, // DSPDSaoxxn
    0x00620786, // DSPDoax
    0x00630608, // SDPnox
    0x00640788, // SDPSoax
    0x00650606, // DSPnox
    0x00660046, // DSx SRCINVERT
    0x006718A8, // SDPSonox
    0x006858A6, // DSPDSonoxxn
    0x00690145, // PDSxxn
    0x006A01E9, // DPSax
    0x006B178A, // PSDPSoaxxn
    0x006C01E8, // SDPax
    0x006D1785, // PDSPDoaxxn
    0x006E1E28, // SDPSnoax
    0x006F0C65, // PDSxnan
    0x00700CC5, // PDSana
    0x00711D5C, // SSDxPDxaxn
    0x00720648, // SDPSxox
    0x00730E28, // SDPnoan
    0x00740646, // DSPDxox
    0x00750E26, // DSPnoan
    0x00761B28, // SDPSnaox
    0x007700E6, // DSan
    0x007801E5, // PDSax
    0x00791786, // DSPDSoaxxn
    0x007A1E29, // DPSDnoax
    0x007B0C68, // SDPxnan
    0x007C1E24, // SPDSnoax
    0x007D0C69, // DPSxnan
    0x007E0955, // SPxDSxo
    0x007F03C9, // DPSaan
    0x008003E9, // DPSaa
    0x00810975, // SPxDSxon
    0x00820C49, // DPSxna
    0x00831E04, // SPDSnoaxn
    0x00840C48, // SDPxna
    0x00851E05, // PDSPnoaxn
    0x008617A6, // DSPDSoaxx
    0x008701C5, // PDSaxn
    0x008800C6, // DSa SRCAND
    0x00891B08, // SDPSnaoxn
    0x008A0E06, // DSPnoa
    0x008B0666, // DSPDxoxn
    0x008C0E08, // SDPnoa
    0x008D0668, // SDPSxoxn
    0x008E1D7C, // SSDxPDxax
    0x008F0CE5, // PDSanan
    0x00900C45, // PDSxna
    0x00911E08, // SDPSnoaxn
    0x009217A9, // DPSDPoaxx
    0x009301C4, // SPDaxn
    0x009417AA, // PSDPSoaxx
    0x009501C9, // DPSaxn
    0x00960169, // DPSxx
    0x0097588A, // PSDPSonoxx
    0x00981888, // SDPSonoxn
    0x00990066, // DSxn
    0x009A0709, // DPSnax
    0x009B07A8, // SDPSoaxn
    0x009C0704, // SPDnax
    0x009D07A6, // DSPDoaxn
    0x009E16E6, // DSPDSaoxx
    0x009F0345, // PDSxan
    0x00A000C9, // DPa
    0x00A11B05, // PDSPnaoxn
    0x00A20E09, // DPSnoa
    0x00A30669, // DPSDxoxn
    0x00A41885, // PDSPonoxn
    0x00A50065, // PDxn
    0x00A60706, // DSPnax
    0x00A707A5, // PDSPoaxn
    0x00A803A9, // DPSoa
    0x00A90189, // DPSoxn
    0x00AA0029, // D
    0x00AB0889, // DPSono
    0x00AC0744, // SPDSxax
    0x00AD06E9, // DPSDaoxn
    0x00AE0B06, // DSPnao
    0x00AF0229, // DPno
    0x00B00E05, // PDSnoa
    0x00B10665, // PDSPxoxn
    0x00B21974, // SSPxDSxox
    0x00B30CE8, // SDPanan
    0x00B4070A, // PSDnax
    0x00B507A9, // DPSDoaxn
    0x00B616E9, // DPSDPaoxx
    0x00B70348, // SDPxan
    0x00B8074A, // PSDPxax
    0x00B906E6, // DSPDaoxn
    0x00BA0B09, // DPSnao
    0x00BB0226, // DSno MERGEPAINT
    0x00BC1CE4, // SPDSanax
    0x00BD0D7D, // SDxPDxan
    0x00BE0269, // DPSxo
    0x00BF08C9, // DPSano
    0x00C000CA, // PSa MERGECOPY
    0x00C11B04, // SPDSnaoxn
    0x00C21884, // SPDSonoxn
    0x00C3006A, // PSxn
    0x00C40E04, // SPDnoa
    0x00C50664, // SPDSxoxn
    0x00C60708, // SDPnax
    0x00C707AA, // PSDPoaxn
    0x00C803A8, // SDPoa
    0x00C90184, // SPDoxn
    0x00CA0749, // DPSDxax
    0x00CB06E4, // SPDSaoxn
    0x00CC0020, // S SRCCOPY
    0x00CD0888, // SDPono
    0x00CE0B08, // SDPnao
    0x00CF0224, // SPno
    0x00D00E0A, // PSDnoa
    0x00D1066A, // PSDPxoxn
    0x00D20705, // PDSnax
    0x00D307A4, // SPDSoaxn
    0x00D41D78, // SSPxPDxax
    0x00D50CE9, // DPSanan
    0x00D616EA, // PSDPSaoxx
    0x00D70349, // DPSxan
    0x00D80745, // PDSPxax
    0x00D906E8, // SDPSaoxn
    0x00DA1CE9, // DPSDanax
    0x00DB0D75, // SPxDSxan
    0x00DC0B04, // SPDnao
    0x00DD0228, // SDno
    0x00DE0268, // SDPxo
    0x00DF08C8, // SDPano
    0x00E003A5, // PDSoa
    0x00E10185, // PDSoxn
    0x00E20746, // DSPDxax
    0x00E306EA, // PSDPaoxn
    0x00E40748, // SDPSxax
    0x00E506E5, // PDSPaoxn
    0x00E61CE8, // SDPSanax
    0x00E70D79, // SPxPDxan
    0x00E81D74, // SSPxDSxax
    0x00E95CE6, // DSPDSanaxxn
    0x00EA02E9, // DPSao
    0x00EB0849, // DPSxno
    0x00EC02E8, // SDPao
    0x00ED0848, // SDPxno
    0x00EE0086, // DSo SRCPAINT
    0x00EF0A08, // SDPnoo
    0x00F00021, // P PATCOPY
    0x00F10885, // PDSono
    0x00F20B05, // PDSnao
    0x00F3022A, // PSno
    0x00F40B0A, // PSDnao
    0x00F50225, // PDno
    0x00F60265, // PDSxo
    0x00F708C5, // PDSano
    0x00F802E5, // PDSao
    0x00F90845, // PDSxno
    0x00FA0089, // DPo
    0x00FB0A09, // DPSnoo PATPAINT
    0x00FC008A, // PSo
    0x00FD0A0A, // PSDnoo
    0x00FE02A9, // DPSoo
    0x00FF0062 // 1 WHITENESS
};

static void set_path(GdiCanvas *canvas, void *addr)
{
    uint32_t* data_size = (uint32_t*)addr;
    uint32_t more = *data_size;

    SpicePathSeg* seg = (SpicePathSeg*)(data_size + 1);

    do {
        uint32_t flags = seg->flags;
        SpicePointFix* point = (SpicePointFix*)seg->data;
        SpicePointFix* end_point = point + seg->count;
        ASSERT(point < end_point);
        more -= ((unsigned long)end_point - (unsigned long)seg);
        seg = (SpicePathSeg*)end_point;

        if (flags & SPICE_PATH_BEGIN) {
            BeginPath(canvas->dc);
            if (!MoveToEx(canvas->dc, (int)fix_to_double(point->x), (int)fix_to_double(point->y),
                          NULL)) {
                CANVAS_ERROR("MoveToEx failed");
                return;
            }
            point++;
        }

        if (flags & SPICE_PATH_BEZIER) {
            ASSERT((point - end_point) % 3 == 0);
            for (; point + 2 < end_point; point += 3) {
                POINT points[3];

                points[0].x = (int)fix_to_double(point[0].x);
                points[0].y = (int)fix_to_double(point[0].y);
                points[1].x = (int)fix_to_double(point[1].x);
                points[1].y = (int)fix_to_double(point[1].y);
                points[2].x = (int)fix_to_double(point[2].x);
                points[2].y = (int)fix_to_double(point[2].y);
                if (!PolyBezierTo(canvas->dc, points, 3)) {
                    CANVAS_ERROR("PolyBezierTo failed");
                    return;
                }
            }
        } else {
            for (; point < end_point; point++) {
                if (!LineTo(canvas->dc, (int)fix_to_double(point->x),
                            (int)fix_to_double(point->y))) {
                    CANVAS_ERROR("LineTo failed");
                }
            }
        }

        if (flags & SPICE_PATH_END) {

            if (flags & SPICE_PATH_CLOSE) {
                if (!CloseFigure(canvas->dc)) {
                    CANVAS_ERROR("CloseFigure failed");
                }
            }

            if (!EndPath(canvas->dc)) {
                CANVAS_ERROR("EndPath failed");
            }
        }

    } while (more);
}

static void set_scale_mode(GdiCanvas *canvas, uint8_t scale_mode)
{
    if (scale_mode == SPICE_IMAGE_SCALE_MODE_INTERPOLATE) {
        SetStretchBltMode(canvas->dc, HALFTONE);
    } else if (scale_mode == SPICE_IMAGE_SCALE_MODE_NEAREST) {
        SetStretchBltMode(canvas->dc, COLORONCOLOR);
    } else {
        CANVAS_ERROR("Unknown ScaleMode");
    }
}

static void set_clip(GdiCanvas *canvas, SpiceClip *clip)
{
    switch (clip->type) {
    case SPICE_CLIP_TYPE_NONE:
        if (SelectClipRgn(canvas->dc, NULL) == ERROR) {
            CANVAS_ERROR("SelectClipRgn failed");
        }
        break;
    case SPICE_CLIP_TYPE_RECTS: {
        uint32_t *n = (uint32_t *)SPICE_GET_ADDRESS(clip->data);

        SpiceRect *now = (SpiceRect *)(n + 1);
        SpiceRect *end = now + *n;

        if (now < end) {
            HRGN main_hrgn;

            main_hrgn = CreateRectRgn(now->left, now->top, now->right, now->bottom);
            if (!main_hrgn) {
                return;
            }
            now++;
            for (; now < end; now++) {
                HRGN combaine_hrgn;
                combaine_hrgn = CreateRectRgn(now->left, now->top, now->right,
                                              now->bottom);
                if (!combaine_hrgn) {
                    CANVAS_ERROR("Unable to CreateRectRgn");
                    DeleteObject(main_hrgn);
                    return;
                }
                if (CombineRgn(main_hrgn, main_hrgn, combaine_hrgn, RGN_OR) == ERROR) {
                    CANVAS_ERROR("Unable to CombineRgn");
                    DeleteObject(combaine_hrgn);
                    return;
                }
                DeleteObject(combaine_hrgn);
            }
            if (SelectClipRgn(canvas->dc, main_hrgn) == ERROR) {
                CANVAS_ERROR("Unable to SelectClipRgn");
            }
            DeleteObject(main_hrgn);
        }
        break;
    }
    case SPICE_CLIP_TYPE_PATH:
        set_path(canvas, SPICE_GET_ADDRESS(clip->data));
        if (SelectClipPath(canvas->dc, RGN_COPY) == ERROR) {
            CANVAS_ERROR("Unable to SelectClipPath");
        }
        break;
    default:
        CANVAS_ERROR("invalid clip type");
    }
}

static void copy_bitmap(const uint8_t *src_image, int height, int src_stride,
                        uint8_t *dest_bitmap, int dest_stride)
{
    int copy_width = MIN(dest_stride, src_stride);
    int y = 0;

    ASSERT(dest_stride >= 0 && src_stride >= 0);
    while (y < height) {
        memcpy(dest_bitmap, src_image, copy_width);
        src_image += src_stride;
        dest_bitmap += dest_stride;
        y++;
    }
}

static void copy_bitmap_alpha(const uint8_t *src_alpha, int height, int width, int src_stride,
                              uint8_t *dest_bitmap, int dest_stride, int alpha_bits_size)
{
    int y = 0;
    uint8_t i_offset;
    int i_count = 0;
    int i = 0;
    int width_div_stride;

    width_div_stride = width / src_stride;

    if (alpha_bits_size == 1) {
        i_offset = 1;
    } else {
        i_offset = 8;
    }


    while (y < height) {
        int x;

        for (x = 0; x < width; ++x) {
            uint8_t alphaval;
            double alpha;

            alphaval = src_alpha[i];
            alphaval = alphaval >> (i_count * i_offset);
            alphaval = alphaval &= ((uint8_t)0xff >> (8 - i_offset));
            alphaval = ((255 * alphaval) / ((uint8_t)0xff >> (8 - i_offset)));

            dest_bitmap[x * 4 + 3] = alphaval;
            alpha = (double)alphaval / 0xff;
            dest_bitmap[x * 4 + 2] = (uint8_t)(alpha * dest_bitmap[x * 4 + 2]);
            dest_bitmap[x * 4 + 1] = (uint8_t)(alpha * dest_bitmap[x * 4 + 1]);
            dest_bitmap[x * 4] = (uint8_t)(alpha * dest_bitmap[x * 4]);

            i_count++;
            if (i_count == (8 / i_offset)) {
                i++;
                i_count = 0;
            }
        }

        dest_bitmap += width * 4;
        i = 0;
        src_alpha += src_stride;
        i_count = 0;
        y++;
    }
}

static uint8_t *create_bitmap(HBITMAP *bitmap, HBITMAP *prev_bitmap, HDC *dc,
                              const uint8_t *bitmap_data, int width, int height,
                              int stride, int bits, int rotate)
{
    uint8_t *data;
    const uint8_t *src_data;
    uint32_t nstride;
    struct {
        BITMAPINFO inf;
        RGBQUAD palette[255];
    } bitmap_info;

    memset(&bitmap_info, 0, sizeof(bitmap_info));
    bitmap_info.inf.bmiHeader.biSize = sizeof(bitmap_info.inf.bmiHeader);
    bitmap_info.inf.bmiHeader.biWidth = width;
    if (stride < 0) {
        bitmap_info.inf.bmiHeader.biHeight = height;
    } else {
        bitmap_info.inf.bmiHeader.biHeight = -height;
    }

    if (rotate) {
        bitmap_info.inf.bmiHeader.biHeight = -bitmap_info.inf.bmiHeader.biHeight;
    }

    bitmap_info.inf.bmiHeader.biPlanes = 1;
    bitmap_info.inf.bmiHeader.biBitCount = bits;
    bitmap_info.inf.bmiHeader.biCompression = BI_RGB;

    *dc = create_compatible_dc();
    if (!*dc) {
        CANVAS_ERROR("create_compatible_dc() failed");
        return NULL;
    }

    *bitmap = CreateDIBSection(*dc, &bitmap_info.inf, 0, (VOID **)&data, NULL, 0);
    if (!*bitmap) {
        CANVAS_ERROR("Unable to CreateDIBSection");
        DeleteDC(*dc);
        return NULL;
    }
    *prev_bitmap = (HBITMAP)SelectObject(*dc, *bitmap);

    if (stride < 0) {
        src_data = bitmap_data - (height - 1) * -stride;
    } else {
        src_data = bitmap_data;
    }

    switch (bits) {
    case 1:
        nstride = SPICE_ALIGN(width, 32) / 8;
        break;
    case 8:
        nstride = SPICE_ALIGN(width, 4);
        break;
    case 16:
        nstride = SPICE_ALIGN(width * 2, 4);
        break;
    case 32:
        nstride = width * 4;
        break;
    default:
        CANVAS_ERROR("invalid bitmap bits size");
    }

    if (bitmap_data) {
        if (stride < 0) {
            copy_bitmap(src_data, height, -stride, data, nstride);
        } else {
            copy_bitmap(src_data, height, stride, data, nstride);
        }
    }

    return data;
}

static uint8_t *create_bitmap_from_pixman(HBITMAP *bitmap, HBITMAP *prev_bitmap, HDC *dc,
										   pixman_image_t *surface, int rotate)
{
    return create_bitmap(bitmap, prev_bitmap, dc,
                         (uint8_t*)pixman_image_get_data(surface),
						 pixman_image_get_width(surface),
						 pixman_image_get_height(surface),
						 pixman_image_get_stride(surface),
						 spice_pixman_image_get_bpp(surface),
						 rotate);
}


static void release_bitmap(HDC dc, HBITMAP bitmap, HBITMAP prev_bitmap, int cache)
{
    bitmap = (HBITMAP)SelectObject(dc, prev_bitmap);
    if (!cache) {
        DeleteObject(bitmap);
    }
    DeleteDC(dc);
}

static inline uint8_t get_converted_color(uint8_t color)
{
    uint8_t msb;

    msb = color & 0xE0;
    msb = msb >> 5;
    color |= msb;
    return color;
}

static inline COLORREF get_color_ref(GdiCanvas *canvas, uint32_t color)
{
    int shift = canvas->base.color_shift == 8 ? 0 : 3;
    uint8_t r, g, b;

    b = (color & canvas->base.color_mask);
    color >>= canvas->base.color_shift;
    g = (color & canvas->base.color_mask);
    color >>= canvas->base.color_shift;
    r = (color & canvas->base.color_mask);
    if (shift) {
        r = get_converted_color(r << shift);
        g = get_converted_color(g << shift);
        b = get_converted_color(b << shift);
    }
    return RGB(r, g, b);
}

static HBRUSH get_brush(GdiCanvas *canvas, SpiceBrush *brush, RecurciveMutex **brush_lock)
{
    HBRUSH hbrush;

    *brush_lock = NULL;

    switch (brush->type) {
    case SPICE_BRUSH_TYPE_SOLID:
        if (!(hbrush = CreateSolidBrush(get_color_ref(canvas, brush->u.color)))) {
            CANVAS_ERROR("CreateSolidBrush failed");
        }
        return hbrush;
    case SPICE_BRUSH_TYPE_PATTERN: { 
        GdiCanvas *gdi_surface = NULL;
        HBRUSH hbrush;
        pixman_image_t *surface;
        HDC dc;
        HBITMAP bitmap;
        HBITMAP prev_bitmap;

        gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, brush->u.pattern.pat);
        if (gdi_surface) {
            bitmap = (HBITMAP)GetCurrentObject(gdi_surface->dc, OBJ_BITMAP);
            if (!bitmap) {
                CANVAS_ERROR("GetCurrentObject failed");
            }
            *brush_lock = gdi_surface->lock;
        } else {
            surface = canvas_get_image(&canvas->base, brush->u.pattern.pat, FALSE);
            if (!create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, surface, 0)) {
                CANVAS_ERROR("create_bitmap failed");
                return NULL;
            }
        }

        if (!(hbrush = CreatePatternBrush(bitmap))) {
            CANVAS_ERROR("CreatePatternBrush failed");
        }

        if (!gdi_surface) {
            release_bitmap(dc, bitmap, prev_bitmap, 0);
            pixman_image_unref(surface);
        }
        return hbrush;
    }
    case SPICE_BRUSH_TYPE_NONE:
        return NULL;
    default:
        CANVAS_ERROR("invalid brush type");
        return NULL;
    }
}

static HBRUSH set_brush(HDC dc, HBRUSH hbrush, SpiceBrush *brush)
{
    switch (brush->type) {
    case SPICE_BRUSH_TYPE_SOLID: {
        return (HBRUSH)SelectObject(dc, hbrush);
    }
    case SPICE_BRUSH_TYPE_PATTERN: {
        HBRUSH prev_hbrush;
        prev_hbrush = (HBRUSH)SelectObject(dc, hbrush);
        if (!SetBrushOrgEx(dc, brush->u.pattern.pos.x, brush->u.pattern.pos.y, NULL)) {
            CANVAS_ERROR("SetBrushOrgEx failed");
        }
        return prev_hbrush;
    }
    default:
        CANVAS_ERROR("invalid brush type");
        return NULL;
    }
}

static void unset_brush(HDC dc, HBRUSH prev_hbrush)
{
    if (!prev_hbrush) {
        return;
    }
    prev_hbrush = (HBRUSH)SelectObject(dc, prev_hbrush);
    DeleteObject(prev_hbrush);
}

uint8_t calc_rop3(uint16_t rop3_bits, int brush)
{
    uint8_t rop3 = 0;
    uint8_t rop3_src = _rop3_src;
    uint8_t rop3_dest = _rop3_dest;
    uint8_t rop3_brush = _rop3_brush;
    uint8_t rop3_src_brush;

    if (rop3_bits & SPICE_ROPD_INVERS_SRC) {
        rop3_src = ~rop3_src;
    }
    if (rop3_bits & SPICE_ROPD_INVERS_BRUSH) {
        rop3_brush = ~rop3_brush;
    }
    if (rop3_bits & SPICE_ROPD_INVERS_DEST) {
        rop3_dest = ~rop3_dest;
    }

    if (brush) {
        rop3_src_brush = rop3_brush;
    } else {
        rop3_src_brush = rop3_src;
    }

    if (rop3_bits & SPICE_ROPD_OP_PUT) {
        rop3 = rop3_src_brush;
    }
    if (rop3_bits & SPICE_ROPD_OP_OR) {
        rop3 = rop3_src_brush | rop3_dest;
    }
    if (rop3_bits & SPICE_ROPD_OP_AND) {
        rop3 = rop3_src_brush & rop3_dest;
    }
    if (rop3_bits & SPICE_ROPD_OP_XOR) {
        rop3 = rop3_src_brush ^ rop3_dest;
    }
    if (rop3_bits & SPICE_ROPD_INVERS_RES) {
        rop3 = ~rop3_dest;
    }

    if (rop3_bits & SPICE_ROPD_OP_BLACKNESS || rop3_bits & SPICE_ROPD_OP_WHITENESS ||
        rop3_bits & SPICE_ROPD_OP_INVERS) {
        CANVAS_ERROR("invalid rop3 type");
    }
    return rop3;
}

uint8_t calc_rop3_src_brush(uint16_t rop3_bits)
{
    uint8_t rop3 = 0;
    uint8_t rop3_src = _rop3_src;
    uint8_t rop3_brush = _rop3_brush;

    if (rop3_bits & SPICE_ROPD_INVERS_SRC) {
        rop3_src = ~rop3_src;
    }
    if (rop3_bits & SPICE_ROPD_INVERS_BRUSH) {
        rop3_brush = ~rop3_brush;
    }

    if (rop3_bits & SPICE_ROPD_OP_OR) {
        rop3 = rop3_src | rop3_brush;
    }
    if (rop3_bits & SPICE_ROPD_OP_AND) {
        rop3 = rop3_src & rop3_brush;
    }
    if (rop3_bits & SPICE_ROPD_OP_XOR) {
        rop3 = rop3_src ^ rop3_brush;
    }

    return rop3;
}

static struct BitmapData get_mask_bitmap(struct GdiCanvas *canvas, struct SpiceQMask *mask)
{
    GdiCanvas *gdi_surface;
    pixman_image_t *surface;
    struct BitmapData bitmap;
    PixmanData *pixman_data;

    bitmap.hbitmap = NULL;
    if (!mask->bitmap) {
        return bitmap;
    }

    gdi_surface = (GdiCanvas *)canvas_get_surface_mask(&canvas->base, mask->bitmap);
    if (gdi_surface) {
        	HBITMAP _bitmap;

            _bitmap = (HBITMAP)GetCurrentObject(gdi_surface->dc, OBJ_BITMAP);
            if (!_bitmap) {
                CANVAS_ERROR ("GetCurrentObject failed");
            }
            bitmap.dc = gdi_surface->dc;
            bitmap.hbitmap = _bitmap;
            bitmap.prev_hbitmap = (HBITMAP)0;
            bitmap.cache = 0;
            bitmap.from_surface = 1;
    } else {
    
        if (!(surface = canvas_get_mask(&canvas->base, mask, NULL))) {
            return bitmap;
        }
    
        pixman_data = (PixmanData *)pixman_image_get_destroy_data (surface);
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            bitmap.dc = create_compatible_dc();
            bitmap.prev_hbitmap = (HBITMAP)SelectObject(bitmap.dc, pixman_data->bitmap);
            bitmap.hbitmap = pixman_data->bitmap;
            ReleaseMutex(pixman_data->mutex);
            bitmap.cache = 1;
        } else if (!create_bitmap_from_pixman(&bitmap.hbitmap, &bitmap.prev_hbitmap, &bitmap.dc,
                                               surface, 0)) {
            bitmap.hbitmap = NULL;
        } else {
            bitmap.cache = 0;
        }

        bitmap.from_surface = 0;
    }

    bitmap.flags = mask->flags;
    bitmap.pos = mask->pos;

    return bitmap;
}

static void gdi_draw_bitmap(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
                            HDC src_dc, struct BitmapData *bitmapmask, uint32_t rop3_val)
{
    uint32_t rast_oper;

    rast_oper = raster_ops[rop3_val];

    if (!bitmapmask || !bitmapmask->hbitmap) {
        if ((dest->right - dest->left) == (src->right - src->left) &&
                                           (dest->bottom - dest->top) == (src->bottom - src->top)) {
            if (!BitBlt(dest_dc, dest->left, dest->top, dest->right - dest->left,
                        dest->bottom - dest->top, src_dc, src->left, src->top, rast_oper)) {
                CANVAS_ERROR("BitBlt failed");
            }
        } else {
            if (!StretchBlt(dest_dc, dest->left, dest->top, dest->right - dest->left,
                            dest->bottom - dest->top, src_dc, src->left, src->top,
                            src->right - src->left, src->bottom - src->top, rast_oper)) {
                CANVAS_ERROR("StretchBlt failed");
            }
        }
    } else {
        rast_oper = MAKEROP4(rast_oper, raster_ops[_rop3_dest]);

        if (!MaskBlt(dest_dc, dest->left, dest->top, dest->right - dest->left,
                     dest->bottom - dest->top, src_dc, src->left, src->top,
                     bitmapmask->hbitmap, bitmapmask->pos.x, bitmapmask->pos.y,
                     rast_oper)) {
            CANVAS_ERROR("MaskBlt failed");
        }
    }
}

static void gdi_draw_bitmap_redrop(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
                                   HDC src_dc, struct BitmapData *bitmapmask,
                                   uint16_t rop, int brush)
{
    uint32_t rop3_val;

    rop3_val = calc_rop3(rop, brush);
    gdi_draw_bitmap(dest_dc, src, dest, src_dc, bitmapmask, rop3_val);
}

static void free_mask(struct BitmapData *bitmap)
{
    if (bitmap->hbitmap) {
        if (!bitmap->from_surface) {
            release_bitmap(bitmap->dc, bitmap->hbitmap, bitmap->prev_hbitmap, bitmap->cache);
        }
    }
}

static void draw_str_mask_bitmap(struct GdiCanvas *canvas,
                                 SpiceString *str, int n, SpiceRect *dest,
                                 SpiceRect *src, SpiceBrush *brush)
{
    pixman_image_t *surface;
    struct BitmapData bitmap;
    SpicePoint pos;
    int dest_stride;
    uint8_t *bitmap_data;
    HBRUSH prev_hbrush;
    HBRUSH hbrush;
    RecurciveMutex *brush_lock;

    bitmap.hbitmap = (HBITMAP)1;
    if (!(surface = canvas_get_str_mask(&canvas->base, str, n, &pos))) {
        CANVAS_ERROR("unable to canvas_get_str_mask");
        return;
    }

    bitmap.from_surface = 0;
    bitmap.cache = 0;
    bitmap_data = create_bitmap(&bitmap.hbitmap, &bitmap.prev_hbitmap,
                                &bitmap.dc, NULL,
                                pixman_image_get_width(surface),
                                pixman_image_get_height(surface),
                                pixman_image_get_stride(surface), 32, 0);

    if (!bitmap_data) {
        return;
    }

    bitmap.flags = 0;
    bitmap.pos.x = 0;
    bitmap.pos.y = 0;

    dest->left = pos.x;
    dest->top = pos.y;
    dest->right = pos.x + pixman_image_get_width(surface);
    dest->bottom = pos.y + pixman_image_get_height(surface);
    src->left = 0;
    src->top = 0;
    src->right = pixman_image_get_width(surface);
    src->bottom = pixman_image_get_height(surface);

    dest_stride = pixman_image_get_width(surface);
    switch (n) {
    case 1:
        dest_stride = dest_stride / 8;
        break;
    case 4:
        dest_stride = dest_stride / 2;
        break;
    case 32:
        dest_stride = dest_stride * 4;
        break;
    default:
        CANVAS_ERROR("unsupported bitmap bits size");
    }
    dest_stride = dest_stride + 3;
    dest_stride = dest_stride & ~3;

    hbrush = get_brush(canvas, brush, &brush_lock);
    prev_hbrush = set_brush(bitmap.dc, hbrush, brush);
    if (brush_lock) {
        RecurciveLock b_lock(*brush_lock);
        gdi_draw_bitmap(bitmap.dc, src, src, bitmap.dc, NULL, _rop3_brush);
    } else {
        gdi_draw_bitmap(bitmap.dc, src, src, bitmap.dc, NULL, _rop3_brush);
    }

    unset_brush(bitmap.dc, prev_hbrush);

    copy_bitmap_alpha((uint8_t *)pixman_image_get_data(surface),
                      pixman_image_get_height(surface),
                      pixman_image_get_width(surface),
                      pixman_image_get_stride(surface),
                      bitmap_data, dest_stride, n);

    BLENDFUNCTION bf = {AC_SRC_OVER, 0, 255, AC_SRC_ALPHA};

    RecurciveLock lock(*canvas->lock);
    AlphaBlend(canvas->dc, dest->left, dest->top, dest->right - dest->left,
               dest->bottom - dest->top, bitmap.dc, src->left, src->top,
               src->right - src->left, src->bottom - src->top, bf);

    free_mask(&bitmap);
}

static void gdi_draw_image(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
                           pixman_image_t *image, struct BitmapData *bitmapmask, uint16_t rop,
                           int rotate)
{
    HDC dc;
    HBITMAP bitmap;
    HBITMAP prev_bitmap;

    create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, image, rotate);

    gdi_draw_bitmap_redrop(dest_dc, src, dest, dc, bitmapmask, rop, 0);

    release_bitmap(dc, bitmap, prev_bitmap, 0);
}

static void gdi_draw_image_rop3(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
                                pixman_image_t *image, struct BitmapData *bitmapmask, uint8_t rop3,
                                int rotate)
{
    HDC dc;
    HBITMAP bitmap;
    HBITMAP prev_bitmap;

    create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, image, rotate);

    gdi_draw_bitmap(dest_dc, src, dest, dc, bitmapmask, rop3);

    release_bitmap(dc, bitmap, prev_bitmap, 0);
}

static void gdi_canvas_draw_fill(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceFill *fill)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    HBRUSH prev_hbrush;
    HBRUSH brush;
    struct BitmapData bitmapmask;
    RecurciveMutex *brush_lock;

    RecurciveLock lock(*canvas->lock);

    if (!(brush = get_brush(canvas, &fill->brush, &brush_lock))) {
        CANVAS_ERROR("no braash");
        return;
    }

    bitmapmask = get_mask_bitmap(canvas, &fill->mask);

    set_clip(canvas, clip);
    prev_hbrush = set_brush(canvas->dc, brush, &fill->brush);
    if (brush_lock) {
        RecurciveLock b_lock(*brush_lock);
        gdi_draw_bitmap_redrop(canvas->dc, bbox, bbox, canvas->dc, &bitmapmask,
                               fill->rop_decriptor, fill->brush.type != SPICE_BRUSH_TYPE_NONE);
    } else {
        gdi_draw_bitmap_redrop(canvas->dc, bbox, bbox, canvas->dc, &bitmapmask,
                               fill->rop_decriptor, fill->brush.type != SPICE_BRUSH_TYPE_NONE);
    }

    free_mask(&bitmapmask);
    unset_brush(canvas->dc, prev_hbrush);
}

static void gdi_canvas_draw_copy(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceCopy *copy)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    GdiCanvas *gdi_surface;
    pixman_image_t *surface;
    struct BitmapData bitmapmask;
    PixmanData *pixman_data;

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, copy->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        bitmapmask = get_mask_bitmap(canvas, &copy->mask);
        set_scale_mode(canvas, copy->scale_mode);
        set_clip(canvas, clip);
        gdi_draw_bitmap_redrop(canvas->dc, &copy->src_area, bbox, gdi_surface->dc,
                               &bitmapmask, copy->rop_decriptor, 0);
    } else {
        surface = canvas_get_image(&canvas->base, copy->src_bitmap, FALSE);
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
    
        RecurciveLock lock(*canvas->lock);
        bitmapmask = get_mask_bitmap(canvas, &copy->mask);
        set_scale_mode(canvas, copy->scale_mode);
        set_clip(canvas, clip);
    
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            gdi_draw_bitmap_redrop(canvas->dc, &copy->src_area, bbox, dc,
                                   &bitmapmask, copy->rop_decriptor, 0);
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            gdi_draw_image(canvas->dc, &copy->src_area, bbox, surface, &bitmapmask,
                           copy->rop_decriptor, 0);
        }

        pixman_image_unref(surface);
    
    }
    free_mask(&bitmapmask);
}

static void gdi_canvas_put_image(SpiceCanvas *spice_canvas, HDC dc, const SpiceRect *dest, const uint8_t *src_data,
                                 uint32_t src_width, uint32_t src_height, int src_stride,
                                 const QRegion *clip)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    SpiceRect src;
    src.top = 0;
    src.bottom = src_height;
    src.left = 0;
    src.right = src_width;
    int num_rects;
    pixman_box32_t *rects;

    RecurciveLock lock(*canvas->lock);
    set_scale_mode(canvas, SPICE_IMAGE_SCALE_MODE_NEAREST);
    if (clip) {
        rects = pixman_region32_rectangles((pixman_region32_t*)clip, &num_rects);
        if (num_rects == 0) {
            return;
        } else {
            HRGN main_hrgn;
            int i;

            main_hrgn = CreateRectRgn(rects[0].x1, rects[0].y1, rects[0].x2,
                                      rects[0].y2);
            if (!main_hrgn) {
                return;
            }

            for (i = 1; i < num_rects; i++) {
                HRGN combaine_hrgn;

                combaine_hrgn = CreateRectRgn(rects[i].x1, rects[i].y1,
                                              rects[i].x2,
                                              rects[i].y2);
                if (!combaine_hrgn) {
                    CANVAS_ERROR("CreateRectRgn failed");
                    DeleteObject(main_hrgn);
                    return;
                }
                if (!CombineRgn(main_hrgn, main_hrgn, combaine_hrgn, RGN_OR)) {
                    CANVAS_ERROR("CombineRgn failed in put_image");
                    return;
                }
                DeleteObject(combaine_hrgn);
            }
            if (SelectClipRgn(canvas->dc, main_hrgn) == ERROR) {
                CANVAS_ERROR("SelectClipRgn failed in put_image");
                DeleteObject(main_hrgn);
                return;
            }
            DeleteObject(main_hrgn);
        }
    } else {
        SelectClipRgn(canvas->dc, NULL);
    }

    if (dc) {
        gdi_draw_bitmap_redrop(canvas->dc, &src, dest, dc,
                               NULL, SPICE_ROPD_OP_PUT, 0);
    } else {
		pixman_image_t *image = pixman_image_create_bits(PIXMAN_a8r8g8b8, src_width, src_height, 
			                                             (uint32_t *)src_data, src_stride);
        gdi_draw_image(canvas->dc, &src, dest, image, NULL, SPICE_ROPD_OP_PUT, 0);
		pixman_image_unref(image);
    }
}

static void gdi_draw_bitmap_transparent(GdiCanvas *canvas, HDC dest_dc, const SpiceRect *src,
                                        const SpiceRect *dest, HDC src_dc, uint32_t color)
{
    TransparentBlt(dest_dc, dest->left, dest->top, dest->right - dest->left,
                   dest->bottom - dest->top, src_dc, src->left, src->top,
                   src->right - src->left, src->bottom - src->top,
                   RGB(((uint8_t*)&color)[2], ((uint8_t*)&color)[1], ((uint8_t*)&color)[0]));
}

static void gdi_draw_image_transparent(GdiCanvas *canvas, HDC dest_dc, const SpiceRect *src,
                                       const SpiceRect *dest, pixman_image_t *image, 
                                       uint32_t color, int rotate)
{
    HDC dc;
    HBITMAP bitmap;
    HBITMAP prev_bitmap;

    create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, image, rotate);

    gdi_draw_bitmap_transparent(canvas, dest_dc, src, dest, dc, color);

    release_bitmap(dc, bitmap, prev_bitmap, 0);
}

static void gdi_canvas_draw_transparent(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip,
                                        SpiceTransparent* transparent)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    GdiCanvas *gdi_surface;
    pixman_image_t *surface;
    PixmanData *pixman_data;

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, transparent->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        set_clip(canvas, clip);
        gdi_draw_bitmap_transparent(canvas, canvas->dc, &transparent->src_area, bbox,
                                    gdi_surface->dc, transparent->true_color);
    } else {
        surface = canvas_get_image(&canvas->base, transparent->src_bitmap, FALSE);
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
        RecurciveLock lock(*canvas->lock);
        set_clip(canvas, clip);
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            gdi_draw_bitmap_transparent(canvas, canvas->dc, &transparent->src_area, bbox, dc,
                                        transparent->true_color);
    
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            gdi_draw_image_transparent(canvas, canvas->dc, &transparent->src_area, bbox, surface,
                                       transparent->true_color, 0);
        }
    
        pixman_image_unref(surface);
    }
}

static void gdi_draw_bitmap_alpha(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
                                  HDC src_dc, uint8_t alpha, int use_bitmap_alpha)
{
    BLENDFUNCTION bf;

    bf.BlendOp = AC_SRC_OVER;
    bf.BlendFlags = 0;
    bf.SourceConstantAlpha = alpha;

    if (use_bitmap_alpha) {
        bf.AlphaFormat = AC_SRC_ALPHA;
    } else {
        bf.AlphaFormat = 0;
    }

    if (!AlphaBlend(dest_dc, dest->left, dest->top, dest->right - dest->left,
                    dest->bottom - dest->top, src_dc, src->left, src->top,
                    src->right - src->left, src->bottom - src->top, bf)) {
        CANVAS_ERROR("AlphaBlend failed");
    }
}

static void gdi_draw_image_alpha(HDC dest_dc, const SpiceRect *src, const SpiceRect *dest,
								 pixman_image_t *image, uint8_t alpha,
                                 int rotate, int use_bitmap_alpha)
{
    HDC dc;
    HBITMAP bitmap;
    HBITMAP prev_bitmap;

    create_bitmap_from_pixman(&bitmap, &prev_bitmap, &dc, image, rotate);

    gdi_draw_bitmap_alpha(dest_dc, src, dest, dc, alpha, use_bitmap_alpha);

    release_bitmap(dc, bitmap, prev_bitmap, 0);
}

static void gdi_canvas_draw_alpha_blend(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceAlphaBlnd* alpha_blend)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    GdiCanvas *gdi_surface;
    pixman_image_t *surface;
    PixmanData *pixman_data;
    int use_bitmap_alpha;

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, alpha_blend->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        set_clip(canvas, clip);
        use_bitmap_alpha = alpha_blend->alpha_flags & SPICE_ALPHA_FLAGS_SRC_SURFACE_HAS_ALPHA;
        gdi_draw_bitmap_alpha(canvas->dc, &alpha_blend->src_area, bbox, gdi_surface->dc,
                              alpha_blend->alpha, use_bitmap_alpha);
    } else {
        surface = canvas_get_image(&canvas->base, alpha_blend->src_bitmap, TRUE);
        use_bitmap_alpha = pixman_image_get_depth(surface) == 32;
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
    
        RecurciveLock lock(*canvas->lock);
        set_clip(canvas, clip);
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            gdi_draw_bitmap_alpha(canvas->dc, &alpha_blend->src_area, bbox, dc, alpha_blend->alpha,
                                  use_bitmap_alpha);
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            gdi_draw_image_alpha(canvas->dc, &alpha_blend->src_area, bbox, surface,
                                 alpha_blend->alpha, 0, use_bitmap_alpha);
        }
    
        pixman_image_unref(surface);
    }
}

static void gdi_canvas_draw_opaque(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceOpaque *opaque)
{
    GdiCanvas *gdi_surface;
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    pixman_image_t *surface;
    struct BitmapData bitmapmask;
    PixmanData *pixman_data;
    HBRUSH prev_hbrush;
    HBRUSH hbrush;
    uint8_t rop3;
    RecurciveMutex *brush_lock;

    rop3 = calc_rop3_src_brush(opaque->rop_decriptor);

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, opaque->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        bitmapmask = get_mask_bitmap(canvas, &opaque->mask);
        hbrush = get_brush(canvas, &opaque->brush, &brush_lock);
        set_scale_mode(canvas, opaque->scale_mode);
        set_clip(canvas, clip);
        prev_hbrush = set_brush(canvas->dc, hbrush, &opaque->brush);
        if (brush_lock) {
            RecurciveLock b_lock(*brush_lock);
            gdi_draw_bitmap(canvas->dc, &opaque->src_area, bbox, gdi_surface->dc, &bitmapmask, rop3);
        } else {
            gdi_draw_bitmap(canvas->dc, &opaque->src_area, bbox, gdi_surface->dc, &bitmapmask, rop3);
        }
        unset_brush(canvas->dc, prev_hbrush);
    } else {
        surface = canvas_get_image(&canvas->base, opaque->src_bitmap, FALSE);
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
    
        RecurciveLock lock(*canvas->lock);
        bitmapmask = get_mask_bitmap(canvas, &opaque->mask);
        hbrush = get_brush(canvas, &opaque->brush, &brush_lock);
        set_scale_mode(canvas, opaque->scale_mode);
        set_clip(canvas, clip);
        prev_hbrush = set_brush(canvas->dc, hbrush, &opaque->brush);
    
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            if (brush_lock) {
                RecurciveLock b_lock(*brush_lock);
                gdi_draw_bitmap(canvas->dc, &opaque->src_area, bbox, dc, &bitmapmask, rop3);
            } else {
                gdi_draw_bitmap(canvas->dc, &opaque->src_area, bbox, dc, &bitmapmask, rop3);
            }
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            if (brush_lock) {
                RecurciveLock b_lock(*brush_lock);
                gdi_draw_image_rop3(canvas->dc, &opaque->src_area, bbox, surface, &bitmapmask, rop3, 0);
            } else {
                gdi_draw_image_rop3(canvas->dc, &opaque->src_area, bbox, surface, &bitmapmask, rop3, 0);
            }
        }
        unset_brush(canvas->dc, prev_hbrush);
        pixman_image_unref(surface);
    }

    free_mask(&bitmapmask);
}

static void gdi_canvas_draw_blend(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceBlend *blend)
{
    GdiCanvas *gdi_surface;
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    pixman_image_t *surface;
    struct BitmapData bitmapmask;
    PixmanData *pixman_data;

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, blend->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        bitmapmask = get_mask_bitmap(canvas, &blend->mask);
        set_scale_mode(canvas, blend->scale_mode);
        set_clip(canvas, clip);
        gdi_draw_bitmap_redrop(canvas->dc, &blend->src_area, bbox, gdi_surface->dc,
                               &bitmapmask, blend->rop_decriptor, 0);
    }  else {
        surface = canvas_get_image(&canvas->base, blend->src_bitmap, FALSE);
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
    
        RecurciveLock lock(*canvas->lock);
        bitmapmask = get_mask_bitmap(canvas, &blend->mask);
        set_scale_mode(canvas, blend->scale_mode);
        set_clip(canvas, clip);
    
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            gdi_draw_bitmap_redrop(canvas->dc, &blend->src_area, bbox, dc,
                                   &bitmapmask, blend->rop_decriptor, 0);
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            gdi_draw_image(canvas->dc, &blend->src_area, bbox, surface,
				           &bitmapmask, blend->rop_decriptor, 0);
        }

        pixman_image_unref(surface);
    }

    free_mask(&bitmapmask);
}

static void gdi_canvas_draw_blackness(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceBlackness *blackness)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    struct BitmapData bitmapmask;

    RecurciveLock lock(*canvas->lock);
    bitmapmask = get_mask_bitmap(canvas, &blackness->mask);
    set_clip(canvas, clip);
    gdi_draw_bitmap(canvas->dc, bbox, bbox, canvas->dc, &bitmapmask, 0x0);

    free_mask(&bitmapmask);
}

static void gdi_canvas_draw_invers(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceInvers *invers)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    struct BitmapData bitmapmask;

    RecurciveLock lock(*canvas->lock);
    bitmapmask = get_mask_bitmap(canvas, &invers->mask);
    set_clip(canvas, clip);
    gdi_draw_bitmap(canvas->dc, bbox, bbox, canvas->dc, &bitmapmask, 0x55);

    free_mask(&bitmapmask);
}

static void gdi_canvas_draw_whiteness(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceWhiteness *whiteness)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    struct BitmapData bitmapmask;

    RecurciveLock lock(*canvas->lock);
    bitmapmask = get_mask_bitmap(canvas, &whiteness->mask);
    set_clip(canvas, clip);
    gdi_draw_bitmap(canvas->dc, bbox, bbox, canvas->dc, &bitmapmask, 0xff);

    free_mask(&bitmapmask);
}

static void gdi_canvas_draw_rop3(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceRop3 *rop3)
{
    GdiCanvas *gdi_surface;
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    pixman_image_t *surface;
    struct BitmapData bitmapmask;
    HBRUSH prev_hbrush;
    HBRUSH hbrush;
    PixmanData *pixman_data;
    RecurciveMutex *brush_lock;

    gdi_surface = (GdiCanvas *)canvas_get_surface(&canvas->base, rop3->src_bitmap);
    if (gdi_surface) {
        RecurciveLock lock(*canvas->lock);
        RecurciveLock s_lock(*gdi_surface->lock);
        hbrush = get_brush(canvas, &rop3->brush, &brush_lock);
        bitmapmask = get_mask_bitmap(canvas, &rop3->mask);
        set_scale_mode(canvas, rop3->scale_mode);
        set_clip(canvas, clip);
        prev_hbrush = set_brush(canvas->dc, hbrush, &rop3->brush);
        if (brush_lock) {
            RecurciveLock b_lock(*brush_lock);
            gdi_draw_bitmap(canvas->dc, &rop3->src_area, bbox, gdi_surface->dc,
                            &bitmapmask, rop3->rop3);
        } else {
            gdi_draw_bitmap(canvas->dc, &rop3->src_area, bbox, gdi_surface->dc,
                            &bitmapmask, rop3->rop3);
        }
        unset_brush(canvas->dc, prev_hbrush);
    } else {
        surface = canvas_get_image(&canvas->base, rop3->src_bitmap, FALSE);
        pixman_data = (PixmanData *)pixman_image_get_destroy_data(surface);
        RecurciveLock lock(*canvas->lock);
        hbrush = get_brush(canvas, &rop3->brush, &brush_lock);
        bitmapmask = get_mask_bitmap(canvas, &rop3->mask);
        set_scale_mode(canvas, rop3->scale_mode);
        set_clip(canvas, clip);
        prev_hbrush = set_brush(canvas->dc, hbrush, &rop3->brush);
    
        if (pixman_data && (WaitForSingleObject(pixman_data->mutex, INFINITE) != WAIT_FAILED)) {
            HDC dc;
            HBITMAP prev_bitmap;
    
            dc = create_compatible_dc();
            prev_bitmap = (HBITMAP)SelectObject(dc, pixman_data->bitmap);
            if (brush_lock) {
                RecurciveLock b_lock(*brush_lock);
                gdi_draw_bitmap(canvas->dc, &rop3->src_area, bbox, dc,
                                &bitmapmask, rop3->rop3);
            } else {
                gdi_draw_bitmap(canvas->dc, &rop3->src_area, bbox, dc,
                                &bitmapmask, rop3->rop3);
            }
            SelectObject(dc, prev_bitmap);
            DeleteObject(dc);
            ReleaseMutex(pixman_data->mutex);
        } else {
            if (brush_lock) {
                RecurciveLock b_lock(*brush_lock);
                gdi_draw_image_rop3(canvas->dc, &rop3->src_area, bbox, surface, &bitmapmask, rop3->rop3, 0);
            } else {
                gdi_draw_image_rop3(canvas->dc, &rop3->src_area, bbox, surface, &bitmapmask, rop3->rop3, 0);
            }
        }

        unset_brush(canvas->dc, prev_hbrush);
        pixman_image_unref(surface);
    }

    free_mask(&bitmapmask);
}

static void gdi_canvas_copy_bits(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpicePoint *src_pos)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    RecurciveLock lock(*canvas->lock);

    set_clip(canvas, clip);

    BitBlt(canvas->dc, bbox->left, bbox->top, bbox->right - bbox->left,
           bbox->bottom - bbox->top, canvas->dc, src_pos->x, src_pos->y, SRCCOPY);
}

static void gdi_canvas_draw_text(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceText *text)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    SpiceString *str;
    RecurciveMutex *brush_lock;

    RecurciveLock lock(*canvas->lock);
    set_clip(canvas, clip);
    lock.unlock();

    if (!rect_is_empty(&text->back_area)) {
        HBRUSH prev_hbrush;
        HBRUSH hbrush;

        RecurciveLock lock(*canvas->lock);
        hbrush = get_brush(canvas, &text->back_brush, &brush_lock);
        prev_hbrush = set_brush(canvas->dc, hbrush, &text->back_brush);
        if (brush_lock) {
            RecurciveLock b_lock(*brush_lock);
            gdi_draw_bitmap_redrop(canvas->dc, bbox, bbox, canvas->dc, NULL,
                                   text->back_mode, 1);
        } else {
            gdi_draw_bitmap_redrop(canvas->dc, bbox, bbox, canvas->dc, NULL,
                                   text->back_mode, 1);
        }
        unset_brush(canvas->dc, prev_hbrush);
    }

    str = (SpiceString *)SPICE_GET_ADDRESS(text->str);

    if (str->flags & SPICE_STRING_FLAGS_RASTER_A1) {
        SpiceRect dest;
        SpiceRect src;

        draw_str_mask_bitmap(canvas, str, 1, &dest, &src, &text->fore_brush);
    } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A4) {
        SpiceRect dest;
        SpiceRect src;

        draw_str_mask_bitmap(canvas, str, 4, &dest, &src, &text->fore_brush);
    } else if (str->flags & SPICE_STRING_FLAGS_RASTER_A8) {
        WARN("untested path A8 glyphs, doing nothing");
        if (0) {
            SpiceRect dest;
            SpiceRect src;

            draw_str_mask_bitmap(canvas, str, 8, &dest, &src, &text->fore_brush);
        }
    } else {
        WARN("untested path vector glyphs, doing nothing");
        if (0) {
        }
    }
}

static int get_join_style(uint8_t join_style)
{
    switch (join_style) {
    case SPICE_LINE_JOIN_ROUND:
        return PS_JOIN_ROUND;
    case SPICE_LINE_JOIN_BEVEL:
        return PS_JOIN_BEVEL;
    case SPICE_LINE_JOIN_MITER:
        return PS_JOIN_MITER;
    default:
        CANVAS_ERROR("bad join style %d", join_style);
    }
}

static int get_cap(int end_style)
{
    switch (end_style) {
    case SPICE_LINE_CAP_ROUND:
        return PS_ENDCAP_ROUND;
    case SPICE_LINE_CAP_SQUARE:
        return PS_ENDCAP_SQUARE;
    case SPICE_LINE_CAP_BUTT:
        return PS_ENDCAP_FLAT;
    default:
        CANVAS_ERROR("bad end style %d", end_style);
    }
}

static uint32_t *gdi_get_userstyle(GdiCanvas *canvas, uint8_t nseg, SPICE_ADDRESS addr, int start_is_gap)
{
    SPICE_FIXED28_4* style = (SPICE_FIXED28_4*)SPICE_GET_ADDRESS(addr);
    double offset = 0;
    uint32_t *local_style;
    int i;

    if (nseg == 0) {
        CANVAS_ERROR("bad nseg");
    }
    local_style = spice_new(uint32_t , nseg);

    if (start_is_gap) {
        offset = (uint32_t)fix_to_double(*style);
        local_style[nseg - 1] = (uint32_t)fix_to_double(*style);
        style++;

        for (i = 0; i < nseg - 1; i++, style++) {
            local_style[i] = (uint32_t)fix_to_double(*style);
        }
    } else {
        for (i = 0; i < nseg; i++, style++) {
            local_style[i] = (uint32_t)fix_to_double(*style);
        }
    }

    return local_style;
}

static void gdi_canvas_draw_stroke(SpiceCanvas *spice_canvas, SpiceRect *bbox, SpiceClip *clip, SpiceStroke *stroke)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    HPEN hpen;
    HPEN prev_hpen;
    LOGBRUSH logbrush;
    int ps_join = 0;
    int line_cap = 0;
    uint32_t *user_style = NULL;
    pixman_image_t *surface = NULL;

    if (stroke->brush.type == SPICE_BRUSH_TYPE_PATTERN) {
        surface = canvas_get_image(&canvas->base, stroke->brush.u.pattern.pat, FALSE);
    }

    RecurciveLock lock(*canvas->lock);
    set_clip(canvas, clip);

    switch (stroke->fore_mode) {
    case SPICE_ROPD_OP_WHITENESS:
        SetROP2(canvas->dc, R2_WHITE);    //0
        break;
    case SPICE_ROPD_OP_BLACKNESS:
        SetROP2(canvas->dc, R2_BLACK);    //1
        break;
    case SPICE_ROPD_OP_INVERS:
        SetROP2(canvas->dc, R2_NOT);    //Dn
        break;
    case SPICE_ROPD_OP_PUT:
        SetROP2(canvas->dc, R2_COPYPEN);    //P
        break;
    case SPICE_ROPD_OP_OR:
        SetROP2(canvas->dc, R2_MERGEPEN);    //DPo
        break;
    case SPICE_ROPD_OP_XOR:
        SetROP2(canvas->dc, R2_XORPEN);    //DPx
        break;
    case SPICE_ROPD_OP_AND:
        SetROP2(canvas->dc, R2_MASKPEN);    //DPa
        break;
    case SPICE_ROPD_INVERS_BRUSH | SPICE_ROPD_OP_PUT:    //Pn
        SetROP2(canvas->dc, R2_NOTCOPYPEN);
        break;
    case SPICE_ROPD_OP_XOR | SPICE_ROPD_INVERS_RES:
        SetROP2(canvas->dc, R2_NOTXORPEN);    //DPxn
        break;
    case SPICE_ROPD_OP_OR | SPICE_ROPD_INVERS_RES:
        SetROP2(canvas->dc, R2_NOTMERGEPEN);    //DPon
        break;
    case SPICE_ROPD_OP_AND | SPICE_ROPD_INVERS_RES:
        SetROP2(canvas->dc, R2_NOTMASKPEN);    //DPan
        break;
    case SPICE_ROPD_INVERS_DEST | SPICE_ROPD_OP_AND:
        SetROP2(canvas->dc, R2_MASKPENNOT);    //PDna
        break;
    case SPICE_ROPD_INVERS_BRUSH | SPICE_ROPD_OP_AND:
        SetROP2(canvas->dc, R2_MASKNOTPEN);    //DPna
        break;
    case SPICE_ROPD_OP_OR | SPICE_ROPD_INVERS_BRUSH:
        SetROP2(canvas->dc, R2_MERGENOTPEN);    //DPno
        break;
    case SPICE_ROPD_OP_OR | SPICE_ROPD_INVERS_DEST:
        SetROP2(canvas->dc, R2_MERGEPENNOT);    //PDno
        break;
    default:
        SetROP2(canvas->dc, R2_NOP);    //D
    }


    if (stroke->brush.type == SPICE_BRUSH_TYPE_SOLID) {
        logbrush.lbStyle = BS_SOLID | DIB_RGB_COLORS;
        logbrush.lbHatch = 0;
        logbrush.lbColor = get_color_ref(canvas, stroke->brush.u.color);
    } else if (stroke->brush.type == SPICE_BRUSH_TYPE_PATTERN) {
#if 0
        struct {
            BITMAPINFO inf;
            RGBQUAD palette[255];
        } bitmap_info;
        GdiImage image;
#endif
        //CANVAS_ERROR("untested path stroke brush with pattern");
#if 0
        ASSERT(surface)
        surface_to_image(surface, &image);

        memset(&bitmap_info, 0, sizeof(bitmap_info));
        bitmap_info.inf.bmiHeader.biSize = sizeof(bitmap_info.inf.bmiHeader);
        bitmap_info.inf.bmiHeader.biWidth = image.width;
        if (image.stride < 0) {
            bitmap_info.inf.bmiHeader.biHeight = image.height;
        } else {
            bitmap_info.inf.bmiHeader.biHeight = -image.height;
        }
        bitmap_info.inf.bmiHeader.biPlanes = 1;
        bitmap_info.inf.bmiHeader.biBitCount = 32;
        bitmap_info.inf.bmiHeader.biCompression = BI_RGB;

        if (image.stride < 0) {
            logbrush.lbHatch = (LONG)GlobalAlloc(GMEM_MOVEABLE,
                                                 image.height * -image.stride + sizeof(BITMAPINFO));
            if (!logbrush.lbHatch) {
                CANVAS_ERROR("GlobalAlloc failed");
            }
            copy_bitmap(image.pixels - (image.height - 1) * -image.stride,
                        image.height, -image.stride,
                        (uint8_t *)logbrush.lbHatch, image.width);
        } else {
            logbrush.lbHatch = (LONG)GlobalAlloc(GMEM_MOVEABLE,
                                                 image.height * image.stride + sizeof(BITMAPINFO));
            if (!logbrush.lbHatch) {
                CANVAS_ERROR("GlobalAlloc failed");
            }
            copy_bitmap(image.pixels, image.height, image.stride,
                        (uint8_t *)logbrush.lbHatch, image.width);
        }

        memcpy((void *)logbrush.lbHatch, &bitmap_info.inf, sizeof(BITMAPINFO));

        logbrush.lbStyle = BS_DIBPATTERN | DIB_RGB_COLORS;
        logbrush.lbColor = 0;
#endif
        pixman_image_unref(surface);
    }

#if 0
    ps_join = get_join_style(stroke->attr.join_style);
    line_cap = get_cap(stroke->attr.end_style);

    SetMiterLimit(canvas->dc, (FLOAT)fix_to_double(stroke->attr.miter_limit), &old_miter);
#endif

    if (stroke->attr.flags & SPICE_LINE_FLAGS_STYLED) {
        user_style = gdi_get_userstyle(canvas, stroke->attr.style_nseg,
                                       stroke->attr.style,
                                       !!(stroke->attr.flags & SPICE_LINE_FLAGS_START_WITH_GAP));
        hpen = ExtCreatePen(PS_GEOMETRIC | ps_join | line_cap | PS_USERSTYLE,
                            (uint32_t)fix_to_double(stroke->attr.width),
                            &logbrush, stroke->attr.style_nseg, (DWORD *)user_style);
    } else {
        hpen = ExtCreatePen(PS_GEOMETRIC | ps_join | line_cap,
                            (uint32_t)fix_to_double(stroke->attr.width),
                            &logbrush, 0, NULL);
    }
    prev_hpen = (HPEN)SelectObject(canvas->dc, hpen);

    set_path(canvas, SPICE_GET_ADDRESS(stroke->path));

    StrokePath(canvas->dc);

    SelectObject(canvas->dc, prev_hpen);
    DeleteObject(hpen);

#if 0
    if (stroke->brush.type == SPICE_BRUSH_TYPE_PATTERN) {
        GlobalFree((HGLOBAL)logbrush.lbHatch);
    }
#endif

    if (user_style) {
        free(user_style);
    }
}

static void gdi_canvas_clear(SpiceCanvas *spice_canvas)
{
}

static void gdi_canvas_destroy(SpiceCanvas *spice_canvas)
{
    GdiCanvas *canvas = (GdiCanvas *)spice_canvas;
    if (!canvas) {
        return;
    }
    canvas_base_destroy(&canvas->base);
    free(canvas);
}

static int need_init = 1;
static SpiceCanvasOps gdi_canvas_ops;

SpiceCanvas *gdi_canvas_create(int width, int height,
                               HDC dc, RecurciveMutex* lock, uint32_t format
#ifdef SW_CANVAS_CACHE
                             , SpiceImageCache *bits_cache
                             , SpicePaletteCache *palette_cache
#elif defined(SW_CANVAS_IMAGE_CACHE)
                             , SpiceImageCache *bits_cache
#endif
                            , SpiceImageSurfaces *surfaces
                            , SpiceGlzDecoder *glz_decoder
                            , SpiceJpegDecoder *jpeg_decoder
                            , SpiceZlibDecoder *zlib_decoder
                            )
{
    GdiCanvas *canvas;
    int init_ok;

    if (need_init) {
        return NULL;
    }
    canvas = spice_new0(GdiCanvas, 1);
    init_ok = canvas_base_init(&canvas->base, &gdi_canvas_ops,
                               width, height, format
#ifdef SW_CANVAS_CACHE
                               ,bits_cache
                               ,palette_cache
#elif defined(SW_CANVAS_IMAGE_CACHE)
                               , bits_cache
#endif
                               , surfaces
                               , glz_decoder
                               , jpeg_decoder
                               , zlib_decoder);
    canvas->dc = dc;
    canvas->lock = lock;
    return (SpiceCanvas *)canvas;
}

void gdi_canvas_init() //unsafe global function
{
    if (!need_init) {
        return;
    }
    need_init = 0;

    canvas_base_init_ops(&gdi_canvas_ops);
    gdi_canvas_ops.draw_fill = gdi_canvas_draw_fill;
    gdi_canvas_ops.draw_copy = gdi_canvas_draw_copy;
    gdi_canvas_ops.draw_opaque = gdi_canvas_draw_opaque;
    gdi_canvas_ops.copy_bits = gdi_canvas_copy_bits;
    gdi_canvas_ops.draw_text = gdi_canvas_draw_text;
    gdi_canvas_ops.draw_stroke = gdi_canvas_draw_stroke;
    gdi_canvas_ops.draw_rop3 = gdi_canvas_draw_rop3;
    gdi_canvas_ops.draw_blend = gdi_canvas_draw_blend;
    gdi_canvas_ops.draw_blackness = gdi_canvas_draw_blackness;
    gdi_canvas_ops.draw_whiteness = gdi_canvas_draw_whiteness;
    gdi_canvas_ops.draw_invers = gdi_canvas_draw_invers;
    gdi_canvas_ops.draw_transparent = gdi_canvas_draw_transparent;
    gdi_canvas_ops.draw_alpha_blend = gdi_canvas_draw_alpha_blend;
    gdi_canvas_ops.put_image = gdi_canvas_put_image;
    gdi_canvas_ops.clear = gdi_canvas_clear;
    gdi_canvas_ops.destroy = gdi_canvas_destroy;

    rop3_init();
}