summaryrefslogtreecommitdiff
path: root/src/compositor/compositor-xrender.c
blob: 093e2c33d8c938249f1fa1c7574d513eacd11cf5 (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
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/*
 * Copyright (C) 2007 Iain Holmes
 * Based on xcompmgr - (c) 2003 Keith Packard
 *          xfwm4    - (c) 2005-2007 Olivier Fourdan
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#define _GNU_SOURCE
#define _XOPEN_SOURCE 500 /* for usleep() */

#include <config.h>

#ifdef HAVE_COMPOSITE_EXTENSIONS

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>

#include <gdk/gdk.h>

#include "display.h"
#include "screen.h"
#include "frame.h"
#include "errors.h"
#include "window.h"
#include "compositor-private.h"
#include "compositor-xrender.h"
#include "xprops.h"
#include <X11/Xatom.h>
#include <X11/extensions/shape.h>
#include <X11/extensions/Xcomposite.h>
#include <X11/extensions/Xdamage.h>
#include <X11/extensions/Xfixes.h>
#include <X11/extensions/Xrender.h>

#define USE_IDLE_REPAINT 1

typedef enum _MetaCompWindowType
{
  META_COMP_WINDOW_NORMAL,
  META_COMP_WINDOW_DND,
  META_COMP_WINDOW_DESKTOP,
  META_COMP_WINDOW_DOCK,
  META_COMP_WINDOW_MENU,
  META_COMP_WINDOW_DROP_DOWN_MENU,
  META_COMP_WINDOW_TOOLTIP,
} MetaCompWindowType;

typedef enum _MetaShadowType
{
  META_SHADOW_SMALL,
  META_SHADOW_MEDIUM,
  META_SHADOW_LARGE,
  LAST_SHADOW_TYPE
} MetaShadowType;

typedef struct _MetaCompositorXRender
{
  MetaCompositor compositor;

  MetaDisplay *display;

  Atom atom_x_root_pixmap;
  Atom atom_x_set_root;
  Atom atom_net_wm_window_opacity;
  Atom atom_net_wm_window_type_dnd;

  Atom atom_net_wm_window_type;
  Atom atom_net_wm_window_type_desktop;
  Atom atom_net_wm_window_type_dock;
  Atom atom_net_wm_window_type_menu;
  Atom atom_net_wm_window_type_dialog;
  Atom atom_net_wm_window_type_normal;
  Atom atom_net_wm_window_type_utility;
  Atom atom_net_wm_window_type_splash;
  Atom atom_net_wm_window_type_toolbar;
  Atom atom_net_wm_window_type_dropdown_menu;
  Atom atom_net_wm_window_type_tooltip;

#ifdef USE_IDLE_REPAINT
  guint repaint_id;
#endif
  guint enabled : 1;
  guint show_redraw : 1;
  guint debug : 1;
} MetaCompositorXRender;

typedef struct _conv
{
  int size;
  double *data;
} conv;

typedef struct _shadow
{
  conv *gaussian_map;
  guchar *shadow_corner;
  guchar *shadow_top;
} shadow;

typedef struct _MetaCompScreen
{
  MetaScreen *screen;
  GList *windows;
  GHashTable *windows_by_xid;

  MetaWindow *focus_window;

  Window output;

  gboolean have_shadows;
  shadow *shadows[LAST_SHADOW_TYPE];

  Picture root_picture;
  Picture root_buffer;
  Picture black_picture;
  Picture trans_black_picture;
  Picture root_tile;
  XserverRegion all_damage;

  guint overlays;
  gboolean compositor_active;
  gboolean clip_changed;

  GSList *dock_windows;
} MetaCompScreen;

typedef struct _MetaCompWindow
{
  MetaScreen *screen;
  MetaWindow *window; /* May be NULL if this window isn't managed by Marco */
  Window id;
  XWindowAttributes attrs;

  Pixmap back_pixmap;

  /* When the window is shaded back_pixmap will be replaced with the pixmap
     for the shaded window. This is a copy of the original unshaded window
     so that we can still see what the window looked like when it is needed
     for the _get_window_pixmap function */
  Pixmap shaded_back_pixmap;

  int mode;

  gboolean damaged;
  gboolean shaped;

  MetaCompWindowType type;

  Damage damage;
  Picture picture;
  Picture alpha_pict;

  gboolean needs_shadow;
  MetaShadowType shadow_type;
  Picture shadow_pict;

  XserverRegion border_size;
  XserverRegion extents;

  Picture shadow;
  int shadow_dx;
  int shadow_dy;
  int shadow_width;
  int shadow_height;

  guint opacity;

  XserverRegion border_clip;

  gboolean updates_frozen;
  gboolean update_pending;
} MetaCompWindow;

#define OPAQUE 0xffffffff

#define WINDOW_SOLID 0
#define WINDOW_ARGB 1

#define SHADOW_SMALL_RADIUS 3.0
#define SHADOW_MEDIUM_RADIUS 6.0
#define SHADOW_LARGE_RADIUS 12.0

#define SHADOW_SMALL_OFFSET_X (SHADOW_SMALL_RADIUS * -3 / 2)
#define SHADOW_SMALL_OFFSET_Y (SHADOW_SMALL_RADIUS * -3 / 2)
#define SHADOW_MEDIUM_OFFSET_X (SHADOW_MEDIUM_RADIUS * -3 / 2)
#define SHADOW_MEDIUM_OFFSET_Y (SHADOW_MEDIUM_RADIUS * -5 / 4)
#define SHADOW_LARGE_OFFSET_X -15
#define SHADOW_LARGE_OFFSET_Y -15

#define SHADOW_OPACITY 0.66

#define TRANS_OPACITY 0.75

#define DISPLAY_COMPOSITOR(display) ((MetaCompositorXRender *) meta_display_get_compositor (display))

/* Gaussian stuff for creating the shadows */
static double
gaussian (double r,
          double x,
          double y)
{
  return ((1 / (sqrt (2 * G_PI * r))) *
          exp ((- (x * x + y * y)) / (2 * r * r)));
}

static conv *
make_gaussian_map (double r)
{
  conv *c;
  int size, centre;
  int x, y;
  double t, g;

  size = ((int) ceil ((r * 3)) + 1) & ~1;
  centre = size / 2;
  c = g_malloc (sizeof (conv) + size * size * sizeof (double));
  c->size = size;
  c->data = (double *) (c + 1);
  t = 0.0;

  for (y = 0; y < size; y++)
    {
      for (x = 0; x < size; x++)
        {
          g = gaussian (r, (double) (x - centre), (double) (y - centre));
          t += g;
          c->data[y * size + x] = g;
        }
    }

  for (y = 0; y < size; y++)
    {
      for (x = 0; x < size; x++)
        {
          c->data[y * size + x] /= t;
        }
    }

  return c;
}

static void
dump_xserver_region (const char   *location,
                     MetaDisplay  *display,
                     XserverRegion region)
{
  MetaCompositorXRender *compositor = DISPLAY_COMPOSITOR (display);
  Display *xdisplay = meta_display_get_xdisplay (display);
  int nrects;
  XRectangle *rects;
  XRectangle bounds;

  if (!compositor->debug)
    return;

  if (region)
    {
      rects = XFixesFetchRegionAndBounds (xdisplay, region, &nrects, &bounds);
      if (nrects > 0)
        {
          int i;
          fprintf (stderr, "%s (XSR): %d rects, bounds: %d,%d (%d,%d)\n",
                   location, nrects, bounds.x, bounds.y, bounds.width, bounds.height);
          for (i = 1; i < nrects; i++)
            fprintf (stderr, "\t%d,%d (%d,%d)\n",
                     rects[i].x, rects[i].y, rects[i].width, rects[i].height);
        }
      else
        fprintf (stderr, "%s (XSR): empty\n", location);
      XFree (rects);
    }
  else
    fprintf (stderr, "%s (XSR): null\n", location);
}

/*
* A picture will help
*
*      -center   0                width  width+center
*  -center +-----+-------------------+-----+
*          |     |                   |     |
*          |     |                   |     |
*        0 +-----+-------------------+-----+
*          |     |                   |     |
*          |     |                   |     |
*          |     |                   |     |
*   height +-----+-------------------+-----+
*          |     |                   |     |
* height+  |     |                   |     |
*  center  +-----+-------------------+-----+
*/
static guchar
sum_gaussian (conv          *map,
              double         opacity,
              int            x,
              int            y,
              int            width,
              int            height)
{
  double *g_data, *g_line;
  double v;
  int fx, fy;
  int fx_start, fx_end;
  int fy_start, fy_end;
  int g_size, centre;

  g_line = map->data;
  g_size = map->size;
  centre = g_size / 2;
  fx_start = centre - x;
  if (fx_start < 0)
    fx_start = 0;

  fx_end = width + centre - x;
  if (fx_end > g_size)
    fx_end = g_size;

  fy_start = centre - y;
  if (fy_start < 0)
    fy_start = 0;

  fy_end = height + centre - y;
  if (fy_end > g_size)
    fy_end = g_size;

  g_line = g_line + fy_start * g_size + fx_start;

  v = 0.0;
  for (fy = fy_start; fy < fy_end; fy++)
    {
      g_data = g_line;
      g_line += g_size;

      for (fx = fx_start; fx < fx_end; fx++)
        v += *g_data++;
    }

  if (v > 1.0)
    v = 1.0;

  return ((guchar) (v * opacity * 255.0));
}

/* precompute shadow corners and sides to save time for large windows */
static void
presum_gaussian (shadow *shad)
{
  int centre;
  int opacity, x, y;
  int msize;
  conv *map;

  map = shad->gaussian_map;
  msize = map->size;
  centre = map->size / 2;

  if (shad->shadow_corner)
    g_free (shad->shadow_corner);
  if (shad->shadow_top)
    g_free (shad->shadow_top);

  shad->shadow_corner = (guchar *)(g_malloc ((msize + 1) * (msize + 1) * 26));
  shad->shadow_top = (guchar *) (g_malloc ((msize + 1) * 26));

  for (x = 0; x <= msize; x++)
    {

      shad->shadow_top[25 * (msize + 1) + x] =
        sum_gaussian (map, 1, x - centre, centre, msize * 2, msize * 2);
      for (opacity = 0; opacity < 25; opacity++)
        {
          shad->shadow_top[opacity * (msize + 1) + x] =
            shad->shadow_top[25 * (msize + 1) + x] * opacity / 25;
        }

      for (y = 0; y <= x; y++)
        {
          shad->shadow_corner[25 * (msize + 1) * (msize + 1)
                              + y * (msize + 1)
                              + x]
            = sum_gaussian (map, 1, x - centre, y - centre,
                            msize * 2, msize * 2);

          shad->shadow_corner[25 * (msize + 1) * (msize + 1)
                              + x * (msize + 1) + y] =
            shad->shadow_corner[25 * (msize + 1) * (msize + 1)
                                + y * (msize + 1) + x];

          for (opacity = 0; opacity < 25; opacity++)
            {
              shad->shadow_corner[opacity * (msize + 1) * (msize + 1)
                                  + y * (msize + 1) + x]
                = shad->shadow_corner[opacity * (msize + 1) * (msize + 1)
                                      + x * (msize + 1) + y]
                = shad->shadow_corner[25 * (msize + 1) * (msize + 1)
                                      + y * (msize + 1) + x] * opacity / 25;
            }
        }
    }
}

static void
generate_shadows (MetaCompScreen *info)
{
  double radii[LAST_SHADOW_TYPE] = {SHADOW_SMALL_RADIUS,
                                    SHADOW_MEDIUM_RADIUS,
                                    SHADOW_LARGE_RADIUS};
  int i;

  for (i = 0; i < LAST_SHADOW_TYPE; i++) {
    shadow *shad = g_new0 (shadow, 1);

    shad->gaussian_map = make_gaussian_map (radii[i]);
    presum_gaussian (shad);

    info->shadows[i] = shad;
  }
}

static XImage *
make_shadow (MetaDisplay   *display,
             MetaScreen    *screen,
             MetaShadowType shadow_type,
             double         opacity,
             int            width,
             int            height)
{
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XImage *ximage;
  guchar *data;
  shadow *shad;
  int msize;
  int ylimit, xlimit;
  int swidth, sheight;
  int centre;
  int x, y;
  guchar d;
  int x_diff;
  int opacity_int = (int)(opacity * 25);
  int screen_number = meta_screen_get_screen_number (screen);

  if (info==NULL)
    {
      return NULL;
    }

  shad = info->shadows[shadow_type];
  msize = shad->gaussian_map->size;
  swidth = width + msize;
  sheight = height + msize;
  centre = msize / 2;

  data = g_malloc (swidth * sheight * sizeof (guchar));

  ximage = XCreateImage (xdisplay, DefaultVisual (xdisplay, screen_number),
                         8, ZPixmap, 0, (char *) data,
                         swidth, sheight, 8, swidth * sizeof (guchar));
  if (!ximage)
    {
      g_free (data);
      return NULL;
    }

  /*
   * Build the gaussian in sections
   */

  /*
   * centre (fill the complete data array
   */
  if (msize > 0)
    d = shad->shadow_top[opacity_int * (msize + 1) + msize];
  else
    d = sum_gaussian (shad->gaussian_map, opacity, centre,
                      centre, width, height);
  memset (data, d, sheight * swidth);

  /*
   * corners
   */
  ylimit = msize;
  if (ylimit > sheight / 2)
    ylimit = (sheight + 1) / 2;

  xlimit = msize;
  if (xlimit > swidth / 2)
    xlimit = (swidth + 1) / 2;

  for (y = 0; y < ylimit; y++)
    {
      for (x = 0; x < xlimit; x++)
        {

          if (xlimit == msize && ylimit == msize)
            d = shad->shadow_corner[opacity_int * (msize + 1) * (msize + 1) + y * (msize + 1) + x];
          else
            d = sum_gaussian (shad->gaussian_map, opacity, x - centre,
                              y - centre, width, height);

          data[y * swidth + x] = d;
          data[(sheight - y - 1) * swidth + x] = d;
          data[(sheight - y - 1) * swidth + (swidth - x - 1)] = d;
          data[y * swidth + (swidth - x - 1)] = d;
        }
    }

  /* top/bottom */
  x_diff = swidth - (msize * 2);
  if (x_diff > 0 && ylimit > 0)
    {
      for (y = 0; y < ylimit; y++)
        {
          if (ylimit == msize)
            d = shad->shadow_top[opacity_int * (msize + 1) + y];
          else
            d = sum_gaussian (shad->gaussian_map, opacity, centre,
                              y - centre, width, height);

          memset (&data[y * swidth + msize], d, x_diff);
          memset (&data[(sheight - y - 1) * swidth + msize], d, x_diff);
        }
    }

  /*
   * sides
   */
  for (x = 0; x < xlimit; x++)
    {
      if (xlimit == msize)
        d = shad->shadow_top[opacity_int * (msize + 1) + x];
      else
        d = sum_gaussian (shad->gaussian_map, opacity, x - centre,
                          centre, width, height);

      for (y = msize; y < sheight - msize; y++)
        {
          data[y * swidth + x] = d;
          data[y * swidth + (swidth - x - 1)] = d;
        }
    }

  return ximage;
}

static Picture
shadow_picture (MetaDisplay   *display,
                MetaScreen    *screen,
                MetaShadowType shadow_type,
                double         opacity,
                Picture        alpha_pict,
                int            width,
                int            height,
                int           *wp,
                int           *hp)
{
  Display *xdisplay = meta_display_get_xdisplay (display);
  XImage *shadow_image;
  Pixmap shadow_pixmap;
  Picture shadow_picture;
  Window xroot = meta_screen_get_xroot (screen);
  GC gc;

  shadow_image = make_shadow (display, screen, shadow_type,
                              opacity, width, height);
  if (!shadow_image)
    return None;

  shadow_pixmap = XCreatePixmap (xdisplay, xroot,
                                 shadow_image->width, shadow_image->height, 8);
  if (!shadow_pixmap)
    {
      XDestroyImage (shadow_image);
      return None;
    }

  shadow_picture = XRenderCreatePicture (xdisplay, shadow_pixmap,
                                         XRenderFindStandardFormat (xdisplay,
PictStandardA8),
                                         0, 0);
  if (!shadow_picture)
    {
      XDestroyImage (shadow_image);
      XFreePixmap (xdisplay, shadow_pixmap);
      return None;
    }

  gc = XCreateGC (xdisplay, shadow_pixmap, 0, 0);
  if (!gc)
    {
      XDestroyImage (shadow_image);
      XFreePixmap (xdisplay, shadow_pixmap);
      XRenderFreePicture (xdisplay, shadow_picture);
      return None;
    }

  XPutImage (xdisplay, shadow_pixmap, gc, shadow_image, 0, 0, 0, 0,
             shadow_image->width, shadow_image->height);
  *wp = shadow_image->width;
  *hp = shadow_image->height;

  XFreeGC (xdisplay, gc);
  XDestroyImage (shadow_image);
  XFreePixmap (xdisplay, shadow_pixmap);

  return shadow_picture;
}

static MetaCompWindow *
find_window_for_screen (MetaScreen *screen,
                        Window      xwindow)
{
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);

  if (info == NULL)
    return NULL;

  return g_hash_table_lookup (info->windows_by_xid, (gpointer) xwindow);
}

static MetaCompWindow *
find_window_in_display (MetaDisplay *display,
                        Window       xwindow)
{
  GSList *index;

  for (index = meta_display_get_screens (display); index; index = index->next)
    {
      MetaCompWindow *cw = find_window_for_screen (index->data, xwindow);

      if (cw != NULL)
        return cw;
    }

  return NULL;
}

static MetaCompWindow *
find_window_for_child_window_in_display (MetaDisplay *display,
                                         Window       xwindow)
{
  Window ignored1, *ignored2;
  Window parent;
  guint ignored_children;

  XQueryTree (meta_display_get_xdisplay (display), xwindow, &ignored1,
              &parent, &ignored2, &ignored_children);

  if (parent != None)
    return find_window_in_display (display, parent);

  return NULL;
}

static Picture
solid_picture (MetaDisplay *display,
               MetaScreen  *screen,
               gboolean     argb,
               double       a,
               double       r,
               double       g,
               double       b)
{
  Display *xdisplay = meta_display_get_xdisplay (display);
  Pixmap pixmap;
  Picture picture;
  XRenderPictureAttributes pa;
  XRenderPictFormat *render_format;
  XRenderColor c;
  Window xroot = meta_screen_get_xroot (screen);

  render_format = XRenderFindStandardFormat (xdisplay,
                                             argb ? PictStandardARGB32 : PictStandardA8);

  pixmap = XCreatePixmap (xdisplay, xroot, 1, 1, argb ? 32 : 8);
  g_return_val_if_fail (pixmap != None, None);

  pa.repeat = TRUE;
  picture = XRenderCreatePicture (xdisplay, pixmap, render_format,
                                  CPRepeat, &pa);
  if (picture == None)
    {
      XFreePixmap (xdisplay, pixmap);
      g_warning ("(picture != None) failed");
      return None;
    }

  c.alpha = a * 0xffff;
  c.red = r * 0xffff;
  c.green = g * 0xffff;
  c.blue = b * 0xffff;

  XRenderFillRectangle (xdisplay, PictOpSrc, picture, &c, 0, 0, 1, 1);
  XFreePixmap (xdisplay, pixmap);

  return picture;
}

static Picture
root_tile (MetaScreen *screen)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  Picture picture;
  Pixmap pixmap;
  gboolean fill = FALSE;
  XRenderPictureAttributes pa;
  XRenderPictFormat *format;
  int p;
  Atom background_atoms[2];
  Atom pixmap_atom;
  int screen_number = meta_screen_get_screen_number (screen);
  Window xroot = meta_screen_get_xroot (screen);

  pixmap = None;
  background_atoms[0] = DISPLAY_COMPOSITOR (display)->atom_x_root_pixmap;
  background_atoms[1] = DISPLAY_COMPOSITOR (display)->atom_x_set_root;

  pixmap_atom = XInternAtom (xdisplay, "PIXMAP", False);
  for (p = 0; p < 2; p++)
    {
      Atom actual_type;
      int actual_format;
      gulong nitems, bytes_after;
      guchar *prop;

      if (XGetWindowProperty (xdisplay, xroot,
                              background_atoms[p],
                              0, 4, FALSE, AnyPropertyType,
                              &actual_type, &actual_format,
                              &nitems, &bytes_after, &prop) == Success)
        {
          if (actual_type == pixmap_atom &&
              actual_format == 32 &&
              nitems == 1)
            {
              memcpy (&pixmap, prop, 4);
              XFree (prop);
              fill = FALSE;
              break;
            }
        }
    }

  if (!pixmap)
    {
      pixmap = XCreatePixmap (xdisplay, xroot, 1, 1,
                              DefaultDepth (xdisplay, screen_number));
      g_return_val_if_fail (pixmap != None, None);
      fill = TRUE;
    }

  pa.repeat = TRUE;
  format = XRenderFindVisualFormat (xdisplay, DefaultVisual (xdisplay,
                                                             screen_number));
  g_return_val_if_fail (format != NULL, None);

  picture = XRenderCreatePicture (xdisplay, pixmap, format, CPRepeat, &pa);
  if ((picture != None) && (fill))
    {
      XRenderColor c;

      /* Background default to just plain black */
      c.red = 0x0000;
      c.green = 0x0000;
      c.blue = 0x0000;
      c.alpha = 0xffff;

      XRenderFillRectangle (xdisplay, PictOpSrc, picture, &c, 0, 0, 1, 1);
      XFreePixmap (xdisplay, pixmap);
    }

  return picture;
}

static Picture
create_root_buffer (MetaScreen *screen)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  Picture pict;
  XRenderPictFormat *format;
  Pixmap root_pixmap;
  Visual *visual;
  int depth, screen_width, screen_height, screen_number;

  if (info == NULL)
    {
      return None;
    }

  meta_screen_get_size (screen, &screen_width, &screen_height);
  screen_number = meta_screen_get_screen_number (screen);
  visual = DefaultVisual (xdisplay, screen_number);
  depth = DefaultDepth (xdisplay, screen_number);

  format = XRenderFindVisualFormat (xdisplay, visual);
  g_return_val_if_fail (format != NULL, None);

  root_pixmap = XCreatePixmap (xdisplay, info->output,
                               screen_width, screen_height, depth);
  g_return_val_if_fail (root_pixmap != None, None);

  pict = XRenderCreatePicture (xdisplay, root_pixmap, format, 0, NULL);
  XFreePixmap (xdisplay, root_pixmap);

  return pict;
}

static void
paint_root (MetaScreen *screen,
            Picture     root_buffer)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  int width, height;

  if (info == NULL)
    {
      return;
    }

  g_return_if_fail (root_buffer != None);

  if (info->root_tile == None)
    {
      info->root_tile = root_tile (screen);
      g_return_if_fail (info->root_tile != None);
    }

  meta_screen_get_size (screen, &width, &height);
  XRenderComposite (xdisplay, PictOpSrc, info->root_tile, None, root_buffer,
                    0, 0, 0, 0, 0, 0, width, height);
}

static gboolean
window_has_shadow (MetaCompWindow *cw)
{
  MetaCompScreen *info = meta_screen_get_compositor_data (cw->screen);

  if (info == NULL || info->have_shadows == FALSE)
    return FALSE;

  /* Always put a shadow around windows with a frame - This should override
     the restriction about not putting a shadow around shaped windows
     as the frame might be the reason the window is shaped */
  if (cw->window)
    {
      /* Do not add shadows for maximized windows */
      if (meta_window_is_maximized (cw->window))
        {
          meta_verbose ("Window has no shadow because it is maximized\n");
          return FALSE;
        }

      if (meta_window_get_frame (cw->window)) {
        meta_verbose ("Window has shadow because it has a frame\n");
        return TRUE;
      }
    }

  /* Do not add shadows to ARGB windows */
  if (cw->mode == WINDOW_ARGB) {
    meta_verbose ("Window has no shadow as it is ARGB\n");
    return FALSE;
  }

  /* Never put a shadow around shaped windows */
  if (cw->shaped) {
    meta_verbose ("Window has no shadow as it is shaped\n");
    return FALSE;
  }

  /* Don't put shadow around DND icon windows */
  if (cw->type == META_COMP_WINDOW_DND ||
      cw->type == META_COMP_WINDOW_DESKTOP) {
    meta_verbose ("Window has no shadow as it is DND or Desktop\n");
    return FALSE;
  }

  if (cw->mode != WINDOW_ARGB) {
    meta_verbose ("Window has shadow as it is not ARGB\n");
    return TRUE;
  }

  if (cw->type == META_COMP_WINDOW_MENU ||
      cw->type == META_COMP_WINDOW_DROP_DOWN_MENU) {
    meta_verbose ("Window has shadow as it is a menu\n");
    return TRUE;
  }

  if (cw->type == META_COMP_WINDOW_TOOLTIP) {
    meta_verbose ("Window has shadow as it is a tooltip\n");
    return TRUE;
  }

  meta_verbose ("Window has no shadow as it fell through\n");
  return FALSE;
}

double shadow_offsets_x[LAST_SHADOW_TYPE] = {SHADOW_SMALL_OFFSET_X,
                                             SHADOW_MEDIUM_OFFSET_X,
                                             SHADOW_LARGE_OFFSET_X};
double shadow_offsets_y[LAST_SHADOW_TYPE] = {SHADOW_SMALL_OFFSET_Y,
                                             SHADOW_MEDIUM_OFFSET_Y,
                                             SHADOW_LARGE_OFFSET_Y};
static XserverRegion
win_extents (MetaCompWindow *cw)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XRectangle r;

  r.x = cw->attrs.x;
  r.y = cw->attrs.y;
  r.width = cw->attrs.width + cw->attrs.border_width * 2;
  r.height = cw->attrs.height + cw->attrs.border_width * 2;

  if (cw->needs_shadow)
    {
      XRectangle sr;

      cw->shadow_dx = shadow_offsets_x [cw->shadow_type];
      cw->shadow_dy = shadow_offsets_y [cw->shadow_type];

      if (!cw->shadow)
        {
          double opacity = SHADOW_OPACITY;
          if (cw->opacity != (guint) OPAQUE)
            opacity = opacity * ((double) cw->opacity) / ((double) OPAQUE);

          cw->shadow = shadow_picture (display, screen, cw->shadow_type,
                                       opacity, cw->alpha_pict,
                                       cw->attrs.width + cw->attrs.border_width * 2,
                                       cw->attrs.height + cw->attrs.border_width * 2,
                                       &cw->shadow_width, &cw->shadow_height);
        }

      sr.x = cw->attrs.x + cw->shadow_dx;
      sr.y = cw->attrs.y + cw->shadow_dy;
      sr.width = cw->shadow_width;
      sr.height = cw->shadow_height;

      if (sr.x < r.x)
        {
          r.width = (r.x + r.width) - sr.x;
          r.x = sr.x;
        }

      if (sr.y < r.y)
        {
          r.height = (r.y + r.height) - sr.y;
          r.y = sr.y;
        }

      if (sr.x + sr.width > r.x + r.width)
        r.width = sr.x + sr.width - r.x;

      if (sr.y + sr.height > r.y + r.height)
        r.height = sr.y + sr.height - r.y;
    }

  return XFixesCreateRegion (xdisplay, &r, 1);
}

static XserverRegion
border_size (MetaCompWindow *cw)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion border;

  meta_error_trap_push (display);
  border = XFixesCreateRegionFromWindow (xdisplay, cw->id,
                                         WindowRegionBounding);
  meta_error_trap_pop (display, FALSE);

  g_return_val_if_fail (border != None, None);
  XFixesTranslateRegion (xdisplay, border,
                         cw->attrs.x + cw->attrs.border_width,
                         cw->attrs.y + cw->attrs.border_width);
  return border;
}

static XRenderPictFormat *
get_window_format (MetaCompWindow *cw)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XRenderPictFormat *format;
  int screen_number = meta_screen_get_screen_number (screen);

  format = XRenderFindVisualFormat (xdisplay, cw->attrs.visual);
  if (!format)
    format = XRenderFindVisualFormat (xdisplay,
                                      DefaultVisual (xdisplay, screen_number));
  return format;
}

static Picture
get_window_picture (MetaCompWindow *cw)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XRenderPictureAttributes pa;
  XRenderPictFormat *format;
  Drawable draw;
  int error_code;

  draw = cw->id;

  meta_error_trap_push (display);

  if (cw->back_pixmap == None)
    cw->back_pixmap = XCompositeNameWindowPixmap (xdisplay, cw->id);

  error_code = meta_error_trap_pop_with_return (display, FALSE);
  if (error_code != 0)
    cw->back_pixmap = None;

  if (cw->back_pixmap != None)
    draw = cw->back_pixmap;

  format = get_window_format (cw);
  if (format)
    {
      Picture pict;

      pa.subwindow_mode = IncludeInferiors;

      meta_error_trap_push (display);
      pict = XRenderCreatePicture (xdisplay, draw, format, CPSubwindowMode, &pa);
      meta_error_trap_pop (display, FALSE);

      return pict;
    }

  return None;
}

static void
paint_dock_shadows (MetaScreen   *screen,
                    Picture       root_buffer,
                    XserverRegion region)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  GSList *d;

  if (info == NULL)
    {
      return;
    }

  for (d = info->dock_windows; d; d = d->next)
    {
      MetaCompWindow *cw = d->data;
      XserverRegion shadow_clip;

      if (cw->shadow)
        {
          shadow_clip = XFixesCreateRegion (xdisplay, NULL, 0);
          XFixesIntersectRegion (xdisplay, shadow_clip,
                                 cw->border_clip, region);

          XFixesSetPictureClipRegion (xdisplay, root_buffer, 0, 0, shadow_clip);

          XRenderComposite (xdisplay, PictOpOver, info->black_picture,
                            cw->shadow, root_buffer,
                            0, 0, 0, 0,
                            cw->attrs.x + cw->shadow_dx,
                            cw->attrs.y + cw->shadow_dy,
                            cw->shadow_width, cw->shadow_height);
          XFixesDestroyRegion (xdisplay, shadow_clip);
        }
    }
}

static void
paint_windows (MetaScreen   *screen,
               GList        *windows,
               Picture       root_buffer,
               XserverRegion region)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  GList *index, *last;
  int screen_width, screen_height;
  MetaCompWindow *cw;
  XserverRegion paint_region, desktop_region;

  if (info == NULL)
    {
      return;
    }

  meta_screen_get_size (screen, &screen_width, &screen_height);

  if (region == None)
    {
      XRectangle r;
      r.x = 0;
      r.y = 0;
      r.width = screen_width;
      r.height = screen_height;
      paint_region = XFixesCreateRegion (xdisplay, &r, 1);
    }
  else
    {
      paint_region = XFixesCreateRegion (xdisplay, NULL, 0);
      XFixesCopyRegion (xdisplay, paint_region, region);
    }

  desktop_region = None;

  /*
   * Painting from top to bottom, reducing the clipping area at
   * each iteration. Only the opaque windows are painted 1st.
   */
  last = NULL;
  for (index = windows; index; index = index->next)
    {
      /* Store the last window we dealt with */
      last = index;

      cw = (MetaCompWindow *) index->data;
      if (!cw->damaged)
        {
          /* Not damaged */
          continue;
        }

#if 0
      if ((cw->attrs.x + cw->attrs.width < 1) ||
          (cw->attrs.y + cw->attrs.height < 1) ||
          (cw->attrs.x >= screen_width) || (cw->attrs.y >= screen_height))
        {
          /* Off screen */
          continue;
        }
#endif

      if (cw->picture == None)
        cw->picture = get_window_picture (cw);

      /* If the clip region of the screen has been changed
         then we need to recreate the extents of the window */
      if (info->clip_changed)
        {
          if (cw->border_size)
            {
              XFixesDestroyRegion (xdisplay, cw->border_size);
              cw->border_size = None;
            }

#if 0
          if (cw->extents)
            {
              XFixesDestroyRegion (xdisplay, cw->extents);
              cw->extents = None;
            }
#endif
        }

      if (cw->border_size == None)
        cw->border_size = border_size (cw);

      if (cw->extents == None)
        cw->extents = win_extents (cw);

      if (cw->mode == WINDOW_SOLID)
        {
          int x, y, wid, hei;

          x = cw->attrs.x;
          y = cw->attrs.y;
          wid = cw->attrs.width + cw->attrs.border_width * 2;
          hei = cw->attrs.height + cw->attrs.border_width * 2;

          XFixesSetPictureClipRegion (xdisplay, root_buffer,
                                      0, 0, paint_region);
          XRenderComposite (xdisplay, PictOpSrc, cw->picture,
                            None, root_buffer, 0, 0, 0, 0,
                            x, y, wid, hei);

          if (cw->type == META_COMP_WINDOW_DESKTOP)
            {
              desktop_region = XFixesCreateRegion (xdisplay, 0, 0);
              XFixesCopyRegion (xdisplay, desktop_region, paint_region);
            }

          XFixesSubtractRegion (xdisplay, paint_region,
                                paint_region, cw->border_size);
        }

      if (!cw->border_clip)
        {
          cw->border_clip = XFixesCreateRegion (xdisplay, 0, 0);
          XFixesCopyRegion (xdisplay, cw->border_clip, paint_region);
        }
    }

  XFixesSetPictureClipRegion (xdisplay, root_buffer, 0, 0, paint_region);
  paint_root (screen, root_buffer);

  paint_dock_shadows (screen, root_buffer, desktop_region == None ?
                      paint_region : desktop_region);
  if (desktop_region != None)
    XFixesDestroyRegion (xdisplay, desktop_region);

  /*
   * Painting from bottom to top, translucent windows and shadows are painted
   */
  for (index = last; index; index = index->prev)
    {
      cw = (MetaCompWindow *) index->data;

      if (cw->picture)
        {
          if (cw->shadow && cw->type != META_COMP_WINDOW_DOCK)
            {
              XserverRegion shadow_clip;

              shadow_clip = XFixesCreateRegion (xdisplay, NULL, 0);
              XFixesSubtractRegion (xdisplay, shadow_clip, cw->border_clip,
                                    cw->border_size);
              XFixesSetPictureClipRegion (xdisplay, root_buffer, 0, 0,
                                          shadow_clip);

              XRenderComposite (xdisplay, PictOpOver, info->black_picture,
                                cw->shadow, root_buffer,
                                0, 0, 0, 0,
                                cw->attrs.x + cw->shadow_dx,
                                cw->attrs.y + cw->shadow_dy,
                                cw->shadow_width, cw->shadow_height);
              if (shadow_clip)
                XFixesDestroyRegion (xdisplay, shadow_clip);
            }

          if ((cw->opacity != (guint) OPAQUE) && !(cw->alpha_pict))
            {
              cw->alpha_pict = solid_picture (display, screen, FALSE,
                                              (double) cw->opacity / OPAQUE,
                                              0, 0, 0);
            }

          XFixesIntersectRegion (xdisplay, cw->border_clip, cw->border_clip,
                                 cw->border_size);
          XFixesSetPictureClipRegion (xdisplay, root_buffer, 0, 0,
                                      cw->border_clip);
          if (cw->mode == WINDOW_ARGB)
            {
              int x, y, wid, hei;

              x = cw->attrs.x;
              y = cw->attrs.y;
              wid = cw->attrs.width + cw->attrs.border_width * 2;
              hei = cw->attrs.height + cw->attrs.border_width * 2;

              XRenderComposite (xdisplay, PictOpOver, cw->picture,
                                cw->alpha_pict, root_buffer, 0, 0, 0, 0,
                                x, y, wid, hei);
            }
        }

      if (cw->border_clip)
        {
          XFixesDestroyRegion (xdisplay, cw->border_clip);
          cw->border_clip = None;
        }
    }

  XFixesDestroyRegion (xdisplay, paint_region);
}

static void
paint_all (MetaScreen   *screen,
           XserverRegion region)
{
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  int screen_width, screen_height;

  /* Set clipping to the given region */
  XFixesSetPictureClipRegion (xdisplay, info->root_picture, 0, 0, region);

  meta_screen_get_size (screen, &screen_width, &screen_height);

  if (DISPLAY_COMPOSITOR (display)->show_redraw)
    {
      Picture overlay;

      dump_xserver_region ("paint_all", display, region);

      /* Make a random colour overlay */
      overlay = solid_picture (display, screen, TRUE, 1, /* 0.3, alpha */
                               ((double) (rand () % 100)) / 100.0,
                               ((double) (rand () % 100)) / 100.0,
                               ((double) (rand () % 100)) / 100.0);

      XRenderComposite (xdisplay, PictOpOver, overlay, None, info->root_picture,
                        0, 0, 0, 0, 0, 0, screen_width, screen_height);
      XRenderFreePicture (xdisplay, overlay);
      XFlush (xdisplay);
      usleep (100 * 1000);
    }

  if (info->root_buffer == None)
    info->root_buffer = create_root_buffer (screen);

  paint_windows (screen, info->windows, info->root_buffer, region);

  XFixesSetPictureClipRegion (xdisplay, info->root_buffer, 0, 0, region);
  XRenderComposite (xdisplay, PictOpSrc, info->root_buffer, None,
                    info->root_picture, 0, 0, 0, 0, 0, 0,
                    screen_width, screen_height);
}

static void
repair_screen (MetaScreen *screen)
{
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);

  if (info!=NULL && info->all_damage != None)
    {
      meta_error_trap_push (display);
      paint_all (screen, info->all_damage);
      XFixesDestroyRegion (xdisplay, info->all_damage);
      info->all_damage = None;
      info->clip_changed = FALSE;
      meta_error_trap_pop (display, FALSE);
    }
}

static void
repair_display (MetaDisplay *display)
{
  GSList *screens = meta_display_get_screens (display);
  MetaCompositorXRender *compositor = DISPLAY_COMPOSITOR (display);

#ifdef USE_IDLE_REPAINT
  if (compositor->repaint_id > 0)
    {
      g_source_remove (compositor->repaint_id);
      compositor->repaint_id = 0;
    }
#endif

  for (; screens; screens = screens->next)
    repair_screen ((MetaScreen *) screens->data);
}

#ifdef USE_IDLE_REPAINT
static gboolean
compositor_idle_cb (gpointer data)
{
  MetaCompositorXRender *compositor = (MetaCompositorXRender *) data;

  compositor->repaint_id = 0;
  repair_display (compositor->display);

  return FALSE;
}

static void
add_repair (MetaDisplay *display)
{
  MetaCompositorXRender *compositor = DISPLAY_COMPOSITOR (display);

  if (compositor->repaint_id > 0)
    return;

#if 1
  compositor->repaint_id = g_idle_add_full (G_PRIORITY_HIGH_IDLE,
                                            compositor_idle_cb, compositor,
                                            NULL);
#else
  /* Limit it to 50fps */
  compositor->repaint_id = g_timeout_add_full (G_PRIORITY_HIGH, 20,
                                               compositor_idle_cb, compositor,
                                               NULL);
#endif
}
#endif

static void
add_damage (MetaScreen     *screen,
            XserverRegion   damage)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);

  /*  dump_xserver_region ("add_damage", display, damage); */

  if (info != NULL)
    {
      if (info->all_damage)
        {
          XFixesUnionRegion (xdisplay, info->all_damage, info->all_damage, damage);
          XFixesDestroyRegion (xdisplay, damage);
        }
      else
        {
          info->all_damage = damage;
        }
    }

#ifdef USE_IDLE_REPAINT
  add_repair (display);
#endif
}

static void
damage_screen (MetaScreen *screen)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion region;
  int width, height;
  XRectangle r;

  r.x = 0;
  r.y = 0;
  meta_screen_get_size (screen, &width, &height);
  r.width = width;
  r.height = height;

  region = XFixesCreateRegion (xdisplay, &r, 1);
  dump_xserver_region ("damage_screen", display, region);
  add_damage (screen, region);
}

static void
repair_win (MetaCompWindow *cw)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion parts;

  meta_error_trap_push (display);
  if (!cw->damaged)
    {
      parts = win_extents (cw);
      XDamageSubtract (xdisplay, cw->damage, None, None);
    }
  else
    {
      parts = XFixesCreateRegion (xdisplay, 0, 0);
      XDamageSubtract (xdisplay, cw->damage, None, parts);
      XFixesTranslateRegion (xdisplay, parts,
                             cw->attrs.x + cw->attrs.border_width,
                             cw->attrs.y + cw->attrs.border_width);
    }

  meta_error_trap_pop (display, FALSE);

  dump_xserver_region ("repair_win", display, parts);
  add_damage (screen, parts);
  cw->damaged = TRUE;
}

static void
free_win (MetaCompWindow *cw,
          gboolean        destroy)
{
  MetaDisplay *display = meta_screen_get_display (cw->screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (cw->screen);

  /* See comment in map_win */
  if (cw->back_pixmap && destroy)
    {
      XFreePixmap (xdisplay, cw->back_pixmap);
      cw->back_pixmap = None;
    }

  if (cw->shaded_back_pixmap && destroy)
    {
      XFreePixmap (xdisplay, cw->shaded_back_pixmap);
      cw->shaded_back_pixmap = None;
    }

  if (cw->picture)
    {
      XRenderFreePicture (xdisplay, cw->picture);
      cw->picture = None;
    }

  if (cw->shadow)
    {
      XRenderFreePicture (xdisplay, cw->shadow);
      cw->shadow = None;
    }

  if (cw->alpha_pict)
    {
      XRenderFreePicture (xdisplay, cw->alpha_pict);
      cw->alpha_pict = None;
    }

  if (cw->shadow_pict)
    {
      XRenderFreePicture (xdisplay, cw->shadow_pict);
      cw->shadow_pict = None;
    }

  if (cw->border_size)
    {
      XFixesDestroyRegion (xdisplay, cw->border_size);
      cw->border_size = None;
    }

  if (cw->border_clip)
    {
      XFixesDestroyRegion (xdisplay, cw->border_clip);
      cw->border_clip = None;
    }

  if (cw->extents)
    {
      XFixesDestroyRegion (xdisplay, cw->extents);
      cw->extents = None;
    }

  if (destroy)
    {
      if (cw->damage != None) {
        meta_error_trap_push (display);
        XDamageDestroy (xdisplay, cw->damage);
        meta_error_trap_pop (display, FALSE);

        cw->damage = None;
      }

      /* The window may not have been added to the list in this case,
         but we can check anyway */
      if (info!=NULL && cw->type == META_COMP_WINDOW_DOCK)
        info->dock_windows = g_slist_remove (info->dock_windows, cw);

      g_free (cw);
    }
}

static void
map_win (MetaDisplay *display,
         MetaScreen  *screen,
         Window       id)
{
  MetaCompWindow *cw = find_window_for_screen (screen, id);
  Display *xdisplay = meta_display_get_xdisplay (display);

  if (cw == NULL)
    return;

  /* The reason we deallocate this here and not in unmap
     is so that we will still have a valid pixmap for
     whenever the window is unmapped */
  if (cw->back_pixmap)
    {
      XFreePixmap (xdisplay, cw->back_pixmap);
      cw->back_pixmap = None;
    }

  if (cw->shaded_back_pixmap)
    {
      XFreePixmap (xdisplay, cw->shaded_back_pixmap);
      cw->shaded_back_pixmap = None;
    }

  cw->attrs.map_state = IsViewable;
  cw->damaged = FALSE;
}

static void
unmap_win (MetaDisplay *display,
           MetaScreen  *screen,
           Window       id)
{
  MetaCompWindow *cw = find_window_for_screen (screen, id);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);

  if (cw == NULL || info == NULL)
    {
      return;
    }

  if (cw->window && cw->window == info->focus_window)
    info->focus_window = NULL;

  cw->attrs.map_state = IsUnmapped;
  cw->damaged = FALSE;

  if (cw->extents != None)
    {
      dump_xserver_region ("unmap_win", display, cw->extents);
      add_damage (screen, cw->extents);
      cw->extents = None;
    }

  free_win (cw, FALSE);
  info->clip_changed = TRUE;
}

static void
determine_mode (MetaDisplay    *display,
                MetaScreen     *screen,
                MetaCompWindow *cw)
{
  XRenderPictFormat *format;
  Display *xdisplay = meta_display_get_xdisplay (display);

  if (cw->alpha_pict)
    {
      XRenderFreePicture (xdisplay, cw->alpha_pict);
      cw->alpha_pict = None;
    }

  if (cw->shadow_pict)
    {
      XRenderFreePicture (xdisplay, cw->shadow_pict);
      cw->shadow_pict = None;
    }

  if (cw->attrs.class == InputOnly)
    format = NULL;
  else
    format = XRenderFindVisualFormat (xdisplay, cw->attrs.visual);

  if ((format && format->type == PictTypeDirect && format->direct.alphaMask)
      || cw->opacity != (guint) OPAQUE)
    cw->mode = WINDOW_ARGB;
  else
    cw->mode = WINDOW_SOLID;

  if (cw->extents)
    {
      XserverRegion damage;
      damage = XFixesCreateRegion (xdisplay, NULL, 0);
      XFixesCopyRegion (xdisplay, damage, cw->extents);

      dump_xserver_region ("determine_mode", display, damage);
      add_damage (screen, damage);
    }
}

static gboolean
is_shaped (MetaDisplay *display,
           Window       xwindow)
{
  Display *xdisplay = meta_display_get_xdisplay (display);
  int xws, yws, xbs, ybs;
  unsigned wws, hws, wbs, hbs;
  int bounding_shaped, clip_shaped;

  if (meta_display_has_shape (display))
    {
      XShapeQueryExtents (xdisplay, xwindow, &bounding_shaped,
                          &xws, &yws, &wws, &hws, &clip_shaped,
                          &xbs, &ybs, &wbs, &hbs);
      return (bounding_shaped != 0);
    }

  return FALSE;
}

static void
get_window_type (MetaDisplay    *display,
                 MetaCompWindow *cw)
{
  MetaCompositorXRender *compositor = DISPLAY_COMPOSITOR (display);
  int n_atoms;
  Atom *atoms, type_atom;
  int i;

  type_atom = None;
  n_atoms = 0;
  atoms = NULL;

  meta_prop_get_atom_list (display, cw->id,
                           compositor->atom_net_wm_window_type,
                           &atoms, &n_atoms);

  for (i = 0; i < n_atoms; i++)
    {
      if (atoms[i] == compositor->atom_net_wm_window_type_dnd ||
          atoms[i] == compositor->atom_net_wm_window_type_desktop ||
          atoms[i] == compositor->atom_net_wm_window_type_dock ||
          atoms[i] == compositor->atom_net_wm_window_type_toolbar ||
          atoms[i] == compositor->atom_net_wm_window_type_menu ||
          atoms[i] == compositor->atom_net_wm_window_type_dialog ||
          atoms[i] == compositor->atom_net_wm_window_type_normal ||
          atoms[i] == compositor->atom_net_wm_window_type_utility ||
          atoms[i] == compositor->atom_net_wm_window_type_splash ||
          atoms[i] == compositor->atom_net_wm_window_type_dropdown_menu ||
          atoms[i] == compositor->atom_net_wm_window_type_tooltip)
        {
          type_atom = atoms[i];
          break;
        }
    }

  meta_XFree (atoms);

  if (type_atom == compositor->atom_net_wm_window_type_dnd)
    cw->type = META_COMP_WINDOW_DND;
  else if (type_atom == compositor->atom_net_wm_window_type_desktop)
    cw->type = META_COMP_WINDOW_DESKTOP;
  else if (type_atom == compositor->atom_net_wm_window_type_dock)
    cw->type = META_COMP_WINDOW_DOCK;
  else if (type_atom == compositor->atom_net_wm_window_type_menu)
    cw->type = META_COMP_WINDOW_MENU;
  else if (type_atom == compositor->atom_net_wm_window_type_dropdown_menu)
    cw->type = META_COMP_WINDOW_DROP_DOWN_MENU;
  else if (type_atom == compositor->atom_net_wm_window_type_tooltip)
    cw->type = META_COMP_WINDOW_TOOLTIP;
  else
    cw->type = META_COMP_WINDOW_NORMAL;

/*   meta_verbose ("Window is %d\n", cw->type); */
}

/* Must be called with an error trap in place */
static void
add_win (MetaScreen *screen,
         MetaWindow *window,
         Window     xwindow)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  MetaCompWindow *cw;
  gulong event_mask;

  if (info == NULL)
    return;

  if (xwindow == info->output)
    return;

  /* If already added, ignore */
  if (find_window_for_screen (screen, xwindow) != NULL)
    return;

  cw = g_new0 (MetaCompWindow, 1);
  cw->screen = screen;
  cw->window = window;
  cw->id = xwindow;

  if (!XGetWindowAttributes (xdisplay, xwindow, &cw->attrs))
    {
      g_free (cw);
      return;
    }
  get_window_type (display, cw);

  /* If Marco has decided not to manage this window then the input events
     won't have been set on the window */
  event_mask = cw->attrs.your_event_mask | PropertyChangeMask;

  XSelectInput (xdisplay, xwindow, event_mask);

  cw->back_pixmap = None;
  cw->shaded_back_pixmap = None;

  cw->damaged = FALSE;
  cw->shaped = is_shaped (display, xwindow);

  if (cw->attrs.class == InputOnly)
    cw->damage = None;
  else
    cw->damage = XDamageCreate (xdisplay, xwindow, XDamageReportNonEmpty);

  cw->alpha_pict = None;
  cw->shadow_pict = None;
  cw->border_size = None;
  cw->extents = None;
  cw->shadow = None;
  cw->shadow_dx = 0;
  cw->shadow_dy = 0;
  cw->shadow_width = 0;
  cw->shadow_height = 0;

  if (window && meta_window_has_focus (window))
    cw->shadow_type = META_SHADOW_LARGE;
  else
    cw->shadow_type = META_SHADOW_MEDIUM;

  cw->opacity = OPAQUE;

  cw->border_clip = None;

  determine_mode (display, screen, cw);
  cw->needs_shadow = window_has_shadow (cw);

  /* Only add the window to the list of docks if it needs a shadow */
  if (cw->type == META_COMP_WINDOW_DOCK && cw->needs_shadow)
    {
      meta_verbose ("Appending %p to dock windows\n", cw);
      info->dock_windows = g_slist_append (info->dock_windows, cw);
    }

  /* Add this to the list at the top of the stack
     before it is mapped so that map_win can find it again */
  info->windows = g_list_prepend (info->windows, cw);
  g_hash_table_insert (info->windows_by_xid, (gpointer) xwindow, cw);

  if (cw->attrs.map_state == IsViewable)
    map_win (display, screen, xwindow);
}

static void
destroy_win (MetaDisplay *display,
             Window       xwindow,
             gboolean     gone)
{
  MetaScreen *screen;
  MetaCompScreen *info;
  MetaCompWindow *cw;

  cw = find_window_in_display (display, xwindow);

  if (cw == NULL)
    return;

  screen = cw->screen;

  if (cw->extents != None)
    {
      dump_xserver_region ("destroy_win", display, cw->extents);
      add_damage (screen, cw->extents);
      cw->extents = None;
    }

  info = meta_screen_get_compositor_data (screen);
  if (info != NULL)
    {
      info->windows = g_list_remove (info->windows, (gconstpointer) cw);
      g_hash_table_remove (info->windows_by_xid, (gpointer) xwindow);
    }

  free_win (cw, TRUE);
}

static void
restack_win (MetaCompWindow *cw,
             Window          above)
{
  MetaScreen *screen;
  MetaCompScreen *info;
  Window previous_above;
  GList *sibling, *next;

  screen = cw->screen;
  info = meta_screen_get_compositor_data (screen);

  if (info == NULL)
    {
      return;
    }

  sibling = g_list_find (info->windows, (gconstpointer) cw);
  next = g_list_next (sibling);
  previous_above = None;

  if (next)
    {
      MetaCompWindow *ncw = (MetaCompWindow *) next->data;
      previous_above = ncw->id;
    }

  /* If above is set to None, the window whose state was changed is on
   * the bottom of the stack with respect to sibling.
   */
  if (above == None)
    {
      /* Insert at bottom of window stack */
      info->windows = g_list_delete_link (info->windows, sibling);
      info->windows = g_list_append (info->windows, cw);
    }
  else if (previous_above != above)
    {
      GList *index;

      for (index = info->windows; index; index = index->next) {
        MetaCompWindow *cw2 = (MetaCompWindow *) index->data;
        if (cw2->id == above)
          break;
      }

      if (index != NULL)
        {
          info->windows = g_list_delete_link (info->windows, sibling);
          info->windows = g_list_insert_before (info->windows, index, cw);
        }
    }
}

static void
resize_win (MetaCompWindow *cw,
            int             x,
            int             y,
            int             width,
            int             height,
            int             border_width,
            gboolean        override_redirect)
{
  MetaScreen *screen = cw->screen;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info = meta_screen_get_compositor_data (screen);
  XserverRegion damage;
  gboolean debug;

  debug = DISPLAY_COMPOSITOR (display)->debug;

  if (cw->extents)
    {
      damage = XFixesCreateRegion (xdisplay, NULL, 0);
      XFixesCopyRegion (xdisplay, damage, cw->extents);
    }
  else
    {
      damage = None;
      if (debug)
        fprintf (stderr, "no extents to damage !\n");
    }

  /*  { // Damage whole screen each time ! ;-)
    XRectangle r;

    r.x = 0;
    r.y = 0;
    meta_screen_get_size (screen, &r.width, &r.height);
    fprintf (stderr, "Damage whole screen %d,%d (%d %d)\n",
             r.x, r.y, r.width, r.height);

    damage = XFixesCreateRegion (xdisplay, &r, 1);
    } */

  cw->attrs.x = x;
  cw->attrs.y = y;

  if (cw->attrs.width != width || cw->attrs.height != height)
    {
      if (cw->shaded_back_pixmap)
        {
          XFreePixmap (xdisplay, cw->shaded_back_pixmap);
          cw->shaded_back_pixmap = None;
        }

      if (cw->back_pixmap)
        {
          /* If the window is shaded, we store the old backing pixmap
             so we can return a proper image of the window */
          if (cw->window && meta_window_is_shaded (cw->window))
            {
              cw->shaded_back_pixmap = cw->back_pixmap;
              cw->back_pixmap = None;
            }
          else
            {
              XFreePixmap (xdisplay, cw->back_pixmap);
              cw->back_pixmap = None;
            }
        }

      if (cw->picture)
        {
          XRenderFreePicture (xdisplay, cw->picture);
          cw->picture = None;
        }

      if (cw->shadow)
        {
          XRenderFreePicture (xdisplay, cw->shadow);
          cw->shadow = None;
        }
    }

  cw->attrs.width = width;
  cw->attrs.height = height;
  cw->attrs.border_width = border_width;
  cw->attrs.override_redirect = override_redirect;

  if (cw->extents)
    XFixesDestroyRegion (xdisplay, cw->extents);

  cw->extents = win_extents (cw);

  if (damage)
    {
      if (debug)
        fprintf (stderr, "Inexplicable intersection with new extents!\n");

      XFixesUnionRegion (xdisplay, damage, damage, cw->extents);
    }
  else
    {
      damage = XFixesCreateRegion (xdisplay, NULL, 0);
      XFixesCopyRegion (xdisplay, damage, cw->extents);
    }

  dump_xserver_region ("resize_win", display, damage);
  add_damage (screen, damage);

  if (info != NULL)
    {
      info->clip_changed = TRUE;
    }
}

/* event processors must all be called with an error trap in place */
static void
process_circulate_notify (MetaCompositorXRender  *compositor,
                          XCirculateEvent        *event)
{
  MetaCompWindow *cw = find_window_in_display (compositor->display,
                                               event->window);
  MetaCompWindow *top;
  MetaCompScreen *info;
  MetaScreen *screen;
  GList *first;
  Window above;

  if (!cw)
    return;

  screen = cw->screen;
  info = meta_screen_get_compositor_data (screen);
  first = info->windows;
  top = (MetaCompWindow *) first->data;

  if ((event->place == PlaceOnTop) && top)
    above = top->id;
  else
    above = None;
  restack_win (cw, above);

  if (info != NULL)
    {
      info->clip_changed = TRUE;
    }

#ifdef USE_IDLE_REPAINT
  add_repair (compositor->display);
#endif
}

static void
process_configure_notify (MetaCompositorXRender  *compositor,
                          XConfigureEvent        *event)
{
  MetaDisplay *display = compositor->display;
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompWindow *cw = find_window_in_display (display, event->window);

  if (cw)
    {
#if 0
      int x = -1, y = -1, width = -1, height = -1;
      int ex = -1, ey = -1, ewidth = -1, eheight = -1;
      MetaRectangle *rect;

      if (cw->window) {
        rect = meta_window_get_rect (cw->window);
        x = rect->x;
        y = rect->y;
        width = rect->width;
        height = rect->height;
      }
      fprintf (stderr, "configure notify xy (%d %d) -> (%d %d), wh (%d %d) -> (%d %d)\n",
               x, y, event->x, event->y,
               width, height, event->width, event->height);
#endif

      if (compositor->debug)
        {
          fprintf (stderr, "configure notify %d %d %d\n", cw->damaged,
                   cw->shaped, cw->needs_shadow);
          dump_xserver_region ("\textents", display, cw->extents);
          fprintf (stderr, "\txy (%d %d), wh (%d %d)\n",
                   event->x, event->y, event->width, event->height);
        }

      restack_win (cw, event->above);
      resize_win (cw, event->x, event->y, event->width, event->height,
                  event->border_width, event->override_redirect);
    }
  else
    {
      MetaScreen *screen;
      MetaCompScreen *info;

      /* Might be the root window? */
      screen = meta_display_screen_for_root (display, event->window);
      if (screen == NULL)
        return;

      info = meta_screen_get_compositor_data (screen);
      if (info != NULL && info->root_buffer)
        {
          XRenderFreePicture (xdisplay, info->root_buffer);
          info->root_buffer = None;
        }

      damage_screen (screen);
    }
}

static void
process_property_notify (MetaCompositorXRender *compositor,
                         XPropertyEvent        *event)
{
  MetaDisplay *display = compositor->display;
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaScreen *screen;
  int p;
  Atom background_atoms[2];

  /* Check for the background property changing */
  background_atoms[0] = compositor->atom_x_root_pixmap;
  background_atoms[1] = compositor->atom_x_set_root;

  for (p = 0; p < 2; p++)
    {
      if (event->atom == background_atoms[p])
        {
          screen = meta_display_screen_for_root (display, event->window);
          if (screen)
            {
              MetaCompScreen *info = meta_screen_get_compositor_data (screen);
              Window xroot = meta_screen_get_xroot (screen);

              if (info != NULL && info->root_tile)
                {
                  XClearArea (xdisplay, xroot, 0, 0, 0, 0, TRUE);
                  XRenderFreePicture (xdisplay, info->root_tile);
                  info->root_tile = None;

                  /* Damage the whole screen as we may need to redraw the
                     background ourselves */
                  damage_screen (screen);
#ifdef USE_IDLE_REPAINT
                  add_repair (display);
#endif

                  return;
                }
            }
        }
    }

  /* Check for the opacity changing */
  if (event->atom == compositor->atom_net_wm_window_opacity)
    {
      MetaCompWindow *cw = find_window_in_display (display, event->window);
      gulong value;

      if (!cw)
        {
          /* Applications can set this for their toplevel windows, so
           * this must be propagated to the window managed by the compositor
           */
          cw = find_window_for_child_window_in_display (display, event->window);
        }

      if (!cw)
        return;

      if (meta_prop_get_cardinal (display, event->window,
                                  compositor->atom_net_wm_window_opacity,
                                  &value) == FALSE)
        value = OPAQUE;

      cw->opacity = (guint)value;
      determine_mode (display, cw->screen, cw);
      cw->needs_shadow = window_has_shadow (cw);

      if (cw->shadow)
        {
          XRenderFreePicture (xdisplay, cw->shadow);
          cw->shadow = None;
        }

      if (cw->extents)
        XFixesDestroyRegion (xdisplay, cw->extents);
      cw->extents = win_extents (cw);

      cw->damaged = TRUE;
#ifdef USE_IDLE_REPAINT
      add_repair (display);
#endif

      return;
    }

  if (event->atom == compositor->atom_net_wm_window_type) {
    MetaCompWindow *cw = find_window_in_display (display, event->window);

    if (!cw)
      return;

    get_window_type (display, cw);
    cw->needs_shadow = window_has_shadow (cw);
    return;
  }
}

static void
expose_area (MetaScreen *screen,
             XRectangle *rects,
             int         nrects)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion region;

  region = XFixesCreateRegion (xdisplay, rects, nrects);

  dump_xserver_region ("expose_area", display, region);
  add_damage (screen, region);
}

static void
process_expose (MetaCompositorXRender *compositor,
                XExposeEvent          *event)
{
  MetaCompWindow *cw = find_window_in_display (compositor->display,
                                               event->window);
  MetaScreen *screen = NULL;
  XRectangle rect[1];
  int origin_x = 0, origin_y = 0;

  if (cw != NULL)
    {
      screen = cw->screen;
      origin_x = cw->attrs.x; /* + cw->attrs.border_width; ? */
      origin_y = cw->attrs.y; /* + cw->attrs.border_width; ? */
    }
  else
    {
      screen = meta_display_screen_for_root (compositor->display,
                                             event->window);
      if (screen == NULL)
        return;
    }

  rect[0].x = event->x + origin_x;
  rect[0].y = event->y + origin_y;
  rect[0].width = event->width;
  rect[0].height = event->height;

  expose_area (screen, rect, 1);
}

static void
process_unmap (MetaCompositorXRender *compositor,
               XUnmapEvent           *event)
{
  MetaCompWindow *cw;

  if (event->from_configure)
    {
      /* Ignore unmap caused by parent's resize */
      return;
    }


  cw = find_window_in_display (compositor->display, event->window);
  if (cw)
    unmap_win (compositor->display, cw->screen, event->window);
}

static void
process_map (MetaCompositorXRender *compositor,
             XMapEvent             *event)
{
  MetaCompWindow *cw = find_window_in_display (compositor->display,
                                               event->window);

  if (cw)
    map_win (compositor->display, cw->screen, event->window);
}

static void
process_reparent (MetaCompositorXRender *compositor,
                  XReparentEvent        *event,
                  MetaWindow            *window)
{
  MetaScreen *screen;

  screen = meta_display_screen_for_root (compositor->display, event->parent);
  if (screen != NULL)
    add_win (screen, window, event->window);
  else
    destroy_win (compositor->display, event->window, FALSE);
}

static void
process_create (MetaCompositorXRender *compositor,
                XCreateWindowEvent    *event,
                MetaWindow            *window)
{
  MetaScreen *screen;
  /* We are only interested in top level windows, others will
     be caught by normal marco functions */

  screen = meta_display_screen_for_root (compositor->display, event->parent);
  if (screen == NULL)
    return;

  if (!find_window_in_display (compositor->display, event->window))
    add_win (screen, window, event->window);
}

static void
process_destroy (MetaCompositorXRender *compositor,
                 XDestroyWindowEvent   *event)
{
  destroy_win (compositor->display, event->window, FALSE);
}

static void
process_damage (MetaCompositorXRender *compositor,
                XDamageNotifyEvent    *event)
{
  MetaCompWindow *cw = find_window_in_display (compositor->display,
                                               event->drawable);
  if (cw == NULL)
    return;

  repair_win (cw);

#ifdef USE_IDLE_REPAINT
  if (event->more == FALSE)
    add_repair (compositor->display);
#endif
}

static void
process_shape (MetaCompositorXRender *compositor,
               XShapeEvent           *event)
{
  MetaCompWindow *cw = find_window_in_display (compositor->display,
                                               event->window);

  if (cw == NULL)
    return;

  if (event->kind == ShapeBounding)
    {
      if (!event->shaped && cw->shaped)
        cw->shaped = FALSE;

      resize_win (cw, cw->attrs.x, cw->attrs.y,
                  event->width + event->x, event->height + event->y,
                  cw->attrs.border_width, cw->attrs.override_redirect);

      if (event->shaped && !cw->shaped)
        cw->shaped = TRUE;
    }
}

static int
timeout_debug (MetaCompositorXRender *compositor)
{
  compositor->show_redraw = (g_getenv ("MARCO_DEBUG_REDRAWS") != NULL);
  compositor->debug = (g_getenv ("MARCO_DEBUG_COMPOSITOR") != NULL);

  return FALSE;
}

static void
xrender_add_window (MetaCompositor    *compositor,
                    MetaWindow        *window,
                    Window             xwindow,
                    XWindowAttributes *attrs)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompositorXRender *xrc = (MetaCompositorXRender *) compositor;
  MetaScreen *screen = meta_screen_for_x_screen (attrs->screen);

  meta_error_trap_push (xrc->display);
  add_win (screen, window, xwindow);
  meta_error_trap_pop (xrc->display, FALSE);
#endif
}

static void
xrender_remove_window (MetaCompositor *compositor,
                       Window          xwindow)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
#endif
}

static void
show_overlay_window (MetaScreen *screen,
                     Window      cow)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion region;

  region = XFixesCreateRegion (xdisplay, NULL, 0);

  XFixesSetWindowShapeRegion (xdisplay, cow, ShapeBounding, 0, 0, 0);
  XFixesSetWindowShapeRegion (xdisplay, cow, ShapeInput, 0, 0, region);

  XFixesDestroyRegion (xdisplay, region);

  damage_screen (screen);
}

static void
hide_overlay_window (MetaScreen *screen,
                     Window      cow)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XserverRegion region;

  region = XFixesCreateRegion (xdisplay, NULL, 0);
  XFixesSetWindowShapeRegion (xdisplay, cow, ShapeBounding, 0, 0, region);
  XFixesDestroyRegion (xdisplay, region);
}

static Window
get_output_window (MetaScreen *screen)
{
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  Window output, xroot;

  xroot = meta_screen_get_xroot (screen);

  output = XCompositeGetOverlayWindow (xdisplay, xroot);
  XSelectInput (xdisplay, output, ExposureMask);

  return output;
}

static void
xrender_manage_screen (MetaCompositor *compositor,
                       MetaScreen     *screen)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompScreen *info;
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  XRenderPictureAttributes pa;
  XRenderPictFormat *visual_format;
  int screen_number = meta_screen_get_screen_number (screen);
  Window xroot = meta_screen_get_xroot (screen);

  /* Check if the screen is already managed */
  if (meta_screen_get_compositor_data (screen))
    return;

  gdk_error_trap_push ();
  XCompositeRedirectSubwindows (xdisplay, xroot, CompositeRedirectManual);
  XSync (xdisplay, FALSE);

  if (gdk_error_trap_pop ())
    {
      g_warning ("Another compositing manager is running on screen %i",
                 screen_number);
      return;
    }

  info = g_new0 (MetaCompScreen, 1);
  info->screen = screen;

  meta_screen_set_compositor_data (screen, info);

  visual_format = XRenderFindVisualFormat (xdisplay, DefaultVisual (xdisplay,
                                                                    screen_number));
  if (!visual_format)
    {
      g_warning ("Cannot find visual format on screen %i", screen_number);
      return;
    }

  info->output = get_output_window (screen);

  pa.subwindow_mode = IncludeInferiors;
  info->root_picture = XRenderCreatePicture (xdisplay, info->output,
                                             visual_format,
                                             CPSubwindowMode, &pa);
  if (info->root_picture == None)
    {
      g_warning ("Cannot create root picture on screen %i", screen_number);
      return;
    }

  info->root_buffer = None;
  info->black_picture = solid_picture (display, screen, TRUE, 1, 0, 0, 0);

  info->root_tile = None;
  info->all_damage = None;

  info->windows = NULL;
  info->windows_by_xid = g_hash_table_new (g_direct_hash, g_direct_equal);

  info->focus_window = meta_display_get_focus_window (display);

  info->compositor_active = TRUE;
  info->overlays = 0;
  info->clip_changed = TRUE;

  info->have_shadows = (g_getenv("META_DEBUG_NO_SHADOW") == NULL);
  if (info->have_shadows)
    {
      meta_verbose ("Enabling shadows\n");
      generate_shadows (info);
    }
  else
    meta_verbose ("Disabling shadows\n");

  XClearArea (xdisplay, info->output, 0, 0, 0, 0, TRUE);

  meta_screen_set_cm_selection (screen);

  /* Now we're up and running we can show the output if needed */
  show_overlay_window (screen, info->output);
#endif
}

static void
xrender_unmanage_screen (MetaCompositor *compositor,
                         MetaScreen     *screen)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaDisplay *display = meta_screen_get_display (screen);
  Display *xdisplay = meta_display_get_xdisplay (display);
  MetaCompScreen *info;
  Window xroot = meta_screen_get_xroot (screen);
  GList *index;

  info = meta_screen_get_compositor_data (screen);

  /* This screen isn't managed */
  if (info == NULL)
    return;

  hide_overlay_window (screen, info->output);

  /* Destroy the windows */
  for (index = info->windows; index; index = index->next)
    {
      MetaCompWindow *cw = (MetaCompWindow *) index->data;
      free_win (cw, TRUE);
    }
  g_list_free (info->windows);
  g_hash_table_destroy (info->windows_by_xid);

  if (info->root_picture)
    XRenderFreePicture (xdisplay, info->root_picture);

  if (info->black_picture)
    XRenderFreePicture (xdisplay, info->black_picture);

  if (info->have_shadows)
    {
      int i;

      for (i = 0; i < LAST_SHADOW_TYPE; i++)
        g_free (info->shadows[i]->gaussian_map);
    }

  XCompositeUnredirectSubwindows (xdisplay, xroot,
                                  CompositeRedirectManual);
  meta_screen_unset_cm_selection (screen);

  XCompositeReleaseOverlayWindow (xdisplay, info->output);

  g_free (info);

  meta_screen_set_compositor_data (screen, NULL);
#endif
}

static void
xrender_set_updates (MetaCompositor *compositor,
                     MetaWindow     *window,
                     gboolean        updates)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS

#endif
}

static void
xrender_destroy (MetaCompositor *compositor)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  g_free (compositor);
#endif
}

#if 0
/* Taking these out because they're empty and never called, and the
 * compiler complains -- tthurman
 */

static void
xrender_begin_move (MetaCompositor *compositor,
                    MetaWindow     *window,
                    MetaRectangle  *initial,
                    int             grab_x,
                    int             grab_y)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
#endif
}

static void
xrender_update_move (MetaCompositor *compositor,
                     MetaWindow     *window,
                     int             x,
                     int             y)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
#endif
}

static void
xrender_end_move (MetaCompositor *compositor,
                  MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
#endif
}
#endif /* 0 */

static void
xrender_free_window (MetaCompositor *compositor,
                     MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompositorXRender *xrc;
  MetaFrame *frame;
  Window xwindow;

  xrc = (MetaCompositorXRender *) compositor;
  frame = meta_window_get_frame (window);
  xwindow = None;

  if (frame)
    {
      xwindow = meta_frame_get_xwindow (frame);
    }
  else
    {
      /* FIXME: When an undecorated window is hidden this is called, but the
       * window does not get readded if it is subsequentally shown again. See:
       * http://bugzilla.gnome.org/show_bug.cgi?id=504876
       */
      /* xwindow = meta_window_get_xwindow (window); */
    }

  if (xwindow != None)
    destroy_win (xrc->display, xwindow, FALSE);
#endif
}

static void
xrender_process_event (MetaCompositor *compositor,
                       XEvent         *event,
                       MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompositorXRender *xrc = (MetaCompositorXRender *) compositor;
  /*
   * This trap is so that none of the compositor functions cause
   * X errors. This is really a hack, but I'm afraid I don't understand
   * enough about Marco/X to know how else you are supposed to do it
   */
  meta_error_trap_push (xrc->display);
  switch (event->type)
    {
    case CirculateNotify:
      process_circulate_notify (xrc, (XCirculateEvent *) event);
      break;

    case ConfigureNotify:
      process_configure_notify (xrc, (XConfigureEvent *) event);
      break;

    case PropertyNotify:
      process_property_notify (xrc, (XPropertyEvent *) event);
      break;

    case Expose:
      process_expose (xrc, (XExposeEvent *) event);
      break;

    case UnmapNotify:
      process_unmap (xrc, (XUnmapEvent *) event);
      break;

    case MapNotify:
      process_map (xrc, (XMapEvent *) event);
      break;

    case ReparentNotify:
      process_reparent (xrc, (XReparentEvent *) event, window);
      break;

    case CreateNotify:
      process_create (xrc, (XCreateWindowEvent *) event, window);
      break;

    case DestroyNotify:
      process_destroy (xrc, (XDestroyWindowEvent *) event);
      break;

    default:
      if (event->type == meta_display_get_damage_event_base (xrc->display) + XDamageNotify)
        process_damage (xrc, (XDamageNotifyEvent *) event);
#ifdef HAVE_SHAPE
      else if (event->type == meta_display_get_shape_event_base (xrc->display) + ShapeNotify)
        process_shape (xrc, (XShapeEvent *) event);
#endif /* HAVE_SHAPE */
      else
        {
          meta_error_trap_pop (xrc->display, FALSE);
          return;
        }
      break;
    }

  meta_error_trap_pop (xrc->display, FALSE);
#ifndef USE_IDLE_REPAINT
  repair_display (xrc->display);
#endif

  return;
#endif
}

static Pixmap
xrender_get_window_pixmap (MetaCompositor *compositor,
                           MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompWindow *cw = NULL;
  MetaScreen *screen = meta_window_get_screen (window);
  MetaFrame *frame = meta_window_get_frame (window);

  cw = find_window_for_screen (screen, frame ? meta_frame_get_xwindow (frame) :
                               meta_window_get_xwindow (window));
  if (cw == NULL)
    return None;

  if (meta_window_is_shaded (window))
    return cw->shaded_back_pixmap;
  else
    return cw->back_pixmap;
#endif
}

static void
xrender_set_active_window (MetaCompositor *compositor,
                           MetaScreen     *screen,
                           MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaCompositorXRender *xrc = (MetaCompositorXRender *) compositor;
  MetaDisplay *display;
  Display *xdisplay;
  MetaCompWindow *old_focus = NULL, *new_focus = NULL;
  MetaCompScreen *info = NULL;
  MetaWindow *old_focus_win = NULL;

  if (compositor == NULL)
    return;

  display = xrc->display;
  xdisplay = meta_display_get_xdisplay (display);
  info = meta_screen_get_compositor_data (screen);

  if (info != NULL)
    {
      old_focus_win = info->focus_window;
    }

  if (old_focus_win)
    {
      MetaFrame *f = meta_window_get_frame (old_focus_win);

      old_focus = find_window_for_screen (screen,
                                          f ? meta_frame_get_xwindow (f) :
                                          meta_window_get_xwindow (old_focus_win));
    }

  if (window)
    {
      MetaFrame *f = meta_window_get_frame (window);
      new_focus = find_window_for_screen (screen,
                                          f ? meta_frame_get_xwindow (f) :
                                          meta_window_get_xwindow (window));
    }

  if (info != NULL)
    {
      info->focus_window = window;
    }

  if (old_focus)
    {
      XserverRegion damage;

      /* Tear down old shadows */
      old_focus->shadow_type = META_SHADOW_MEDIUM;
      determine_mode (display, screen, old_focus);
      old_focus->needs_shadow = window_has_shadow (old_focus);

      if (old_focus->attrs.map_state == IsViewable)
        {
          if (old_focus->shadow)
            {
              XRenderFreePicture (xdisplay, old_focus->shadow);
              old_focus->shadow = None;
            }

          if (old_focus->extents)
            {
              damage = XFixesCreateRegion (xdisplay, NULL, 0);
              XFixesCopyRegion (xdisplay, damage, old_focus->extents);
              XFixesDestroyRegion (xdisplay, old_focus->extents);
            }
          else
            damage = None;

          /* Build new extents */
          old_focus->extents = win_extents (old_focus);

          if (damage)
            XFixesUnionRegion (xdisplay, damage, damage, old_focus->extents);
          else
            {
              damage = XFixesCreateRegion (xdisplay, NULL, 0);
              XFixesCopyRegion (xdisplay, damage, old_focus->extents);
            }

          dump_xserver_region ("resize_win", display, damage);
          add_damage (screen, damage);

          if (info != NULL)
            {
              info->clip_changed = TRUE;
            }
        }
    }

  if (new_focus)
    {
      XserverRegion damage;

      new_focus->shadow_type = META_SHADOW_LARGE;
      determine_mode (display, screen, new_focus);
      new_focus->needs_shadow = window_has_shadow (new_focus);

      if (new_focus->shadow)
        {
          XRenderFreePicture (xdisplay, new_focus->shadow);
          new_focus->shadow = None;
        }

      if (new_focus->extents)
        {
          damage = XFixesCreateRegion (xdisplay, NULL, 0);
          XFixesCopyRegion (xdisplay, damage, new_focus->extents);
          XFixesDestroyRegion (xdisplay, new_focus->extents);
        }
      else
        damage = None;

      /* Build new extents */
      new_focus->extents = win_extents (new_focus);

      if (damage)
        XFixesUnionRegion (xdisplay, damage, damage, new_focus->extents);
      else
        {
          damage = XFixesCreateRegion (xdisplay, NULL, 0);
          XFixesCopyRegion (xdisplay, damage, new_focus->extents);
        }

      dump_xserver_region ("resize_win", display, damage);
      add_damage (screen, damage);

      if (info != NULL)
        {
          info->clip_changed = TRUE;
        }
    }
#ifdef USE_IDLE_REPAINT
  add_repair (display);
#endif
#endif
}

static void
xrender_maximize_window (MetaCompositor *compositor,
                         MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaFrame *frame = meta_window_get_frame (window);
  Window xid = frame ? meta_frame_get_xwindow (frame) : meta_window_get_xwindow (window);
  MetaCompWindow *cw = find_window_in_display (meta_window_get_display (window), xid);

  if (!cw)
    return;

  cw->needs_shadow = window_has_shadow (cw);
#endif
}

static void
xrender_unmaximize_window (MetaCompositor *compositor,
                           MetaWindow     *window)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  MetaFrame *frame = meta_window_get_frame (window);
  Window xid = frame ? meta_frame_get_xwindow (frame) : meta_window_get_xwindow (window);
  MetaCompWindow *cw = find_window_in_display (meta_window_get_display (window), xid);

  if (!cw)
    return;

  cw->needs_shadow = window_has_shadow (cw);
#endif
}

static MetaCompositor comp_info = {
  xrender_destroy,
  xrender_manage_screen,
  xrender_unmanage_screen,
  xrender_add_window,
  xrender_remove_window,
  xrender_set_updates,
  xrender_process_event,
  xrender_get_window_pixmap,
  xrender_set_active_window,
  xrender_free_window,
  xrender_maximize_window,
  xrender_unmaximize_window,
};

MetaCompositor *
meta_compositor_xrender_new (MetaDisplay *display)
{
#ifdef HAVE_COMPOSITE_EXTENSIONS
  char *atom_names[] = {
    "_XROOTPMAP_ID",
    "_XSETROOT_ID",
    "_NET_WM_WINDOW_OPACITY",
    "_NET_WM_WINDOW_TYPE_DND",
    "_NET_WM_WINDOW_TYPE",
    "_NET_WM_WINDOW_TYPE_DESKTOP",
    "_NET_WM_WINDOW_TYPE_DOCK",
    "_NET_WM_WINDOW_TYPE_MENU",
    "_NET_WM_WINDOW_TYPE_DIALOG",
    "_NET_WM_WINDOW_TYPE_NORMAL",
    "_NET_WM_WINDOW_TYPE_UTILITY",
    "_NET_WM_WINDOW_TYPE_SPLASH",
    "_NET_WM_WINDOW_TYPE_TOOLBAR",
    "_NET_WM_WINDOW_TYPE_DROPDOWN_MENU",
    "_NET_WM_WINDOW_TYPE_TOOLTIP"
  };
  Atom atoms[G_N_ELEMENTS(atom_names)];
  MetaCompositorXRender *xrc;
  MetaCompositor *compositor;
  Display *xdisplay = meta_display_get_xdisplay (display);

  xrc = g_new (MetaCompositorXRender, 1);
  xrc->compositor = comp_info;

  compositor = (MetaCompositor *) xrc;

  xrc->display = display;

  meta_verbose ("Creating %d atoms\n", (int) G_N_ELEMENTS (atom_names));
  XInternAtoms (xdisplay, atom_names, G_N_ELEMENTS (atom_names),
                False, atoms);

  xrc->atom_x_root_pixmap = atoms[0];
  xrc->atom_x_set_root = atoms[1];
  xrc->atom_net_wm_window_opacity = atoms[2];
  xrc->atom_net_wm_window_type_dnd = atoms[3];
  xrc->atom_net_wm_window_type = atoms[4];
  xrc->atom_net_wm_window_type_desktop = atoms[5];
  xrc->atom_net_wm_window_type_dock = atoms[6];
  xrc->atom_net_wm_window_type_menu = atoms[7];
  xrc->atom_net_wm_window_type_dialog = atoms[8];
  xrc->atom_net_wm_window_type_normal = atoms[9];
  xrc->atom_net_wm_window_type_utility = atoms[10];
  xrc->atom_net_wm_window_type_splash = atoms[11];
  xrc->atom_net_wm_window_type_toolbar = atoms[12];
  xrc->atom_net_wm_window_type_dropdown_menu = atoms[13];
  xrc->atom_net_wm_window_type_tooltip = atoms[14];
  xrc->show_redraw = FALSE;
  xrc->debug = FALSE;

#ifdef USE_IDLE_REPAINT
  meta_verbose ("Using idle repaint\n");
  xrc->repaint_id = 0;
#endif

  xrc->enabled = TRUE;
  g_timeout_add (2000, (GSourceFunc) timeout_debug, xrc);

  return compositor;
#else
  return NULL;
#endif
}

#endif /* HAVE_COMPOSITE_EXTENSIONS */