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
|
/*
* Copyright (C) 2009, 2010, 2011, 2012 Research In Motion Limited. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#include "BackingStore.h"
#include "BackingStoreClient.h"
#include "BackingStoreTile.h"
#include "BackingStoreVisualizationViewportAccessor.h"
#include "BackingStore_p.h"
#include "FatFingers.h"
#include "Frame.h"
#include "FrameView.h"
#include "GraphicsContext.h"
#include "InspectorController.h"
#include "InspectorInstrumentation.h"
#include "Page.h"
#include "SurfacePool.h"
#include "WebPage.h"
#include "WebPageClient.h"
#include "WebPageCompositorClient.h"
#include "WebPageCompositor_p.h"
#include "WebPage_p.h"
#include "WebSettings.h"
#include <BlackBerryPlatformExecutableMessage.h>
#include <BlackBerryPlatformGraphics.h>
#include <BlackBerryPlatformIntRectRegion.h>
#include <BlackBerryPlatformLog.h>
#include <BlackBerryPlatformMessage.h>
#include <BlackBerryPlatformMessageClient.h>
#include <BlackBerryPlatformScreen.h>
#include <BlackBerryPlatformSettings.h>
#include <BlackBerryPlatformViewportAccessor.h>
#include <BlackBerryPlatformWindow.h>
#include <SkImageDecoder.h>
#include <wtf/CurrentTime.h>
#include <wtf/MathExtras.h>
#include <wtf/NotFound.h>
#define SUPPRESS_NON_VISIBLE_REGULAR_RENDER_JOBS 0
#define ENABLE_SCROLLBARS 1
#define ENABLE_REPAINTONSCROLL 1
#define DEBUG_BACKINGSTORE 0
#define DEBUG_CHECKERBOARD 0
#define DEBUG_WEBCORE_REQUESTS 0
#define DEBUG_VISUALIZE 0
#define DEBUG_TILEMATRIX 0
using namespace WebCore;
using namespace std;
using BlackBerry::Platform::Graphics::Window;
using BlackBerry::Platform::IntRect;
using BlackBerry::Platform::IntPoint;
using BlackBerry::Platform::IntSize;
namespace BlackBerry {
namespace WebKit {
WebPage* BackingStorePrivate::s_currentBackingStoreOwner = 0;
typedef std::pair<int, int> Divisor;
typedef Vector<Divisor> DivisorList;
// FIXME: Cache this and/or use a smarter algorithm.
static DivisorList divisors(unsigned n)
{
DivisorList divisors;
for (unsigned i = 1; i <= n; ++i)
if (!(n % i))
divisors.append(std::make_pair(i, n / i));
return divisors;
}
static bool divisorIsPerfectWidth(Divisor divisor, Platform::IntSize size, int tileWidth)
{
return size.width() <= divisor.first * tileWidth && abs(size.width() - divisor.first * tileWidth) < tileWidth;
}
static bool divisorIsPerfectHeight(Divisor divisor, Platform::IntSize size, int tileHeight)
{
return size.height() <= divisor.second * tileHeight && abs(size.height() - divisor.second * tileHeight) < tileHeight;
}
static bool divisorIsPreferredDirection(Divisor divisor, BackingStorePrivate::TileMatrixDirection direction)
{
if (direction == BackingStorePrivate::Vertical)
return divisor.second > divisor.first;
return divisor.first > divisor.second;
}
// Compute best divisor given the ratio determined by size.
static Divisor bestDivisor(Platform::IntSize size, int tileWidth, int tileHeight,
int minimumNumberOfTilesWide, int minimumNumberOfTilesHigh,
BackingStorePrivate::TileMatrixDirection direction)
{
// The point of this function is to determine the number of tiles in each
// dimension. We do this by looking to match the tile matrix width/height
// ratio as closely as possible with the width/height ratio of the contents.
// We also look at the direction passed to give preference to one dimension
// over another. This method could probably be made faster, but it gets the
// job done.
SurfacePool* surfacePool = SurfacePool::globalSurfacePool();
ASSERT(!surfacePool->isEmpty());
// Store a static list of possible divisors.
static DivisorList divisorList = divisors(surfacePool->size());
// The ratio we're looking to best imitate.
float ratio = static_cast<float>(size.width()) / static_cast<float>(size.height());
Divisor bestDivisor;
for (size_t i = 0; i < divisorList.size(); ++i) {
Divisor divisor = divisorList[i];
const bool isPerfectWidth = divisorIsPerfectWidth(divisor, size, tileWidth);
const bool isPerfectHeight = divisorIsPerfectHeight(divisor, size, tileHeight);
const bool isValidWidth = divisor.first >= minimumNumberOfTilesWide || isPerfectWidth;
const bool isValidHeight = divisor.second >= minimumNumberOfTilesHigh || isPerfectHeight;
if (!isValidWidth || !isValidHeight)
continue;
if (isPerfectWidth || isPerfectHeight) {
bestDivisor = divisor; // Found a perfect fit!
#if DEBUG_TILEMATRIX
BBLOG(BlackBerry::Platform::LogLevelCritical, "bestDivisor found perfect size isPerfectWidth=%s isPerfectHeight=%s",
isPerfectWidth ? "true" : "false",
isPerfectHeight ? "true" : "false");
#endif
break;
}
// Store basis of comparison.
if (!bestDivisor.first || !bestDivisor.second) {
bestDivisor = divisor;
continue;
}
// If the current best divisor agrees with the preferred tile matrix direction,
// then continue if the current candidate does not.
if (divisorIsPreferredDirection(bestDivisor, direction) && !divisorIsPreferredDirection(divisor, direction))
continue;
// Compare ratios.
float diff1 = fabs((static_cast<float>(divisor.first) / static_cast<float>(divisor.second)) - ratio);
float diff2 = fabs((static_cast<float>(bestDivisor.first) / static_cast<float>(bestDivisor.second)) - ratio);
if (diff1 < diff2)
bestDivisor = divisor;
}
return bestDivisor;
}
struct BackingStoreMutexLocker {
BackingStoreMutexLocker(BackingStorePrivate* backingStorePrivate)
: m_backingStorePrivate(backingStorePrivate)
{
m_backingStorePrivate->lockBackingStore();
}
~BackingStoreMutexLocker()
{
m_backingStorePrivate->unlockBackingStore();
}
private:
BackingStorePrivate* m_backingStorePrivate;
};
Platform::IntRect BackingStoreGeometry::backingStoreRect() const
{
return Platform::IntRect(backingStoreOffset(), backingStoreSize());
}
Platform::IntSize BackingStoreGeometry::backingStoreSize() const
{
return Platform::IntSize(numberOfTilesWide() * BackingStorePrivate::tileWidth(), numberOfTilesHigh() * BackingStorePrivate::tileHeight());
}
BackingStorePrivate::BackingStorePrivate()
: m_suspendScreenUpdates(0)
, m_suspendBackingStoreUpdates(0)
, m_resumeOperation(BackingStore::None)
, m_suspendRenderJobs(false)
, m_suspendRegularRenderJobs(false)
, m_isScrollingOrZooming(false)
, m_webPage(0)
, m_client(0)
, m_renderQueue(adoptPtr(new RenderQueue(this)))
, m_defersBlit(true)
, m_hasBlitJobs(false)
, m_currentWindowBackBuffer(0)
, m_preferredTileMatrixDimension(Vertical)
#if USE(ACCELERATED_COMPOSITING)
, m_isDirectRenderingAnimationMessageScheduled(false)
#endif
{
m_frontState = reinterpret_cast<unsigned>(new BackingStoreGeometry);
m_backState = reinterpret_cast<unsigned>(new BackingStoreGeometry);
// Need a recursive mutex to achieve a global lock.
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&m_mutex, &attr);
pthread_mutexattr_destroy(&attr);
}
BackingStorePrivate::~BackingStorePrivate()
{
BackingStoreGeometry* front = reinterpret_cast<BackingStoreGeometry*>(m_frontState);
delete front;
m_frontState = 0;
BackingStoreGeometry* back = reinterpret_cast<BackingStoreGeometry*>(m_backState);
delete back;
m_backState = 0;
pthread_mutex_destroy(&m_mutex);
}
void BackingStorePrivate::instrumentBeginFrame()
{
WebCore::InspectorInstrumentation::didBeginFrame(WebPagePrivate::core(m_webPage));
}
void BackingStorePrivate::instrumentCancelFrame()
{
WebCore::InspectorInstrumentation::didCancelFrame(WebPagePrivate::core(m_webPage));
}
bool BackingStorePrivate::shouldDirectRenderingToWindow() const
{
// Direct rendering doesn't work with OpenGL compositing code paths due to
// a race condition on which thread's EGL context gets to make the surface
// current, see PR 105750.
// As a workaround, we will be using compositor to draw the root layer.
if (isOpenGLCompositing())
return false;
if (m_webPage->settings()->isDirectRenderingToWindowEnabled())
return true;
// If the BackingStore is inactive, see if there's a compositor to do the
// work of rendering the root layer.
if (!isActive())
return !m_webPage->d->compositorDrawsRootLayer();
const BackingStoreGeometry* currentState = frontState();
const unsigned tilesNecessary = minimumNumberOfTilesWide() * minimumNumberOfTilesHigh();
const unsigned tilesAvailable = currentState->numberOfTilesWide() * currentState->numberOfTilesHigh();
return tilesAvailable < tilesNecessary;
}
bool BackingStorePrivate::isOpenGLCompositing() const
{
if (Window* window = m_webPage->client()->window())
return window->windowUsage() == Window::GLES2Usage;
// If there's no window, OpenGL rendering is currently the only option.
return true;
}
void BackingStorePrivate::suspendScreenAndBackingStoreUpdates()
{
if (m_suspendScreenUpdates) {
BBLOG(BlackBerry::Platform::LogLevelInfo,
"Screen and backingstore already suspended, increasing suspend counter.");
}
++m_suspendBackingStoreUpdates;
// Make sure the user interface thread gets the message before we proceed
// because blitVisibleContents() can be called from the user interface
// thread and it must honor this flag.
++m_suspendScreenUpdates;
BlackBerry::Platform::userInterfaceThreadMessageClient()->syncToCurrentMessage();
}
void BackingStorePrivate::resumeScreenAndBackingStoreUpdates(BackingStore::ResumeUpdateOperation op)
{
ASSERT(m_suspendScreenUpdates);
ASSERT(m_suspendBackingStoreUpdates);
// Both variables are similar except for the timing of setting them.
ASSERT(m_suspendScreenUpdates == m_suspendBackingStoreUpdates);
if (!m_suspendScreenUpdates || !m_suspendBackingStoreUpdates) {
BlackBerry::Platform::logAlways(BlackBerry::Platform::LogLevelCritical,
"Call mismatch: Screen and backingstore haven't been suspended, therefore won't resume!");
return;
}
// Out of all nested resume calls, resume with the maximum-impact operation.
if (op == BackingStore::RenderAndBlit
|| (m_resumeOperation == BackingStore::None && op == BackingStore::Blit))
m_resumeOperation = op;
if (m_suspendScreenUpdates >= 2 && m_suspendBackingStoreUpdates >= 2) { // we're still suspended
BBLOG(BlackBerry::Platform::LogLevelInfo,
"Screen and backingstore still suspended, decreasing suspend counter.");
--m_suspendBackingStoreUpdates;
--m_suspendScreenUpdates;
return;
}
--m_suspendBackingStoreUpdates;
op = m_resumeOperation;
m_resumeOperation = BackingStore::None;
#if USE(ACCELERATED_COMPOSITING)
if (op != BackingStore::None) {
if (isOpenGLCompositing() && !isActive()) {
m_webPage->d->setCompositorDrawsRootLayer(true);
m_webPage->d->setNeedsOneShotDrawingSynchronization();
--m_suspendScreenUpdates;
BlackBerry::Platform::userInterfaceThreadMessageClient()->syncToCurrentMessage();
return;
}
m_webPage->d->setNeedsOneShotDrawingSynchronization();
}
#endif
// For the direct rendering case, there is no such operation as blit,
// we have to render to get anything to the screen.
if (shouldDirectRenderingToWindow() && op == BackingStore::Blit)
op = BackingStore::RenderAndBlit;
// Do some rendering if necessary.
if (op == BackingStore::RenderAndBlit)
renderVisibleContents();
// Make sure the user interface thread gets the message before we proceed
// because blitVisibleContents() can be called from the user interface
// thread and it must honor this flag.
--m_suspendScreenUpdates;
BlackBerry::Platform::userInterfaceThreadMessageClient()->syncToCurrentMessage();
if (op == BackingStore::None)
return;
#if USE(ACCELERATED_COMPOSITING)
// This will also blit since we set the OSDS flag above.
m_webPage->d->commitRootLayerIfNeeded();
#else
// Do some blitting if necessary.
if ((op == BackingStore::Blit || op == BackingStore::RenderAndBlit) && !shouldDirectRenderingToWindow())
blitVisibleContents();
#endif
}
void BackingStorePrivate::repaint(const Platform::IntRect& windowRect,
bool contentChanged, bool immediate)
{
if (m_suspendBackingStoreUpdates)
return;
// If immediate is true, then we're being asked to perform synchronously.
// NOTE: WebCore::ScrollView will call this method with immediate:true and contentChanged:false.
// This is a special case introduced specifically for the Apple's windows port and can be safely ignored I believe.
// Now this method will be called from WebPagePrivate::repaint().
if (contentChanged && !windowRect.isEmpty()) {
// This windowRect is in untransformed coordinates relative to the viewport, but
// it needs to be transformed coordinates relative to the transformed contents.
Platform::IntRect rect = m_webPage->d->mapToTransformed(m_client->mapFromViewportToContents(windowRect));
rect.inflate(1 /*dx*/, 1 /*dy*/); // Account for anti-aliasing of previous rendering runs.
// FIXME: This should not explicitely depend on WebCore::.
WebCore::IntRect tmpRect = rect;
m_client->clipToTransformedContentsRect(tmpRect);
rect = tmpRect;
if (rect.isEmpty())
return;
#if DEBUG_WEBCORE_REQUESTS
BBLOG(BlackBerry::Platform::LogLevelCritical,
"BackingStorePrivate::repaint rect=%d,%d %dx%d contentChanged=%s immediate=%s",
rect.x(), rect.y(), rect.width(), rect.height(),
(contentChanged ? "true" : "false"),
(immediate ? "true" : "false"));
#endif
if (immediate) {
if (render(rect)) {
if (!shouldDirectRenderingToWindow() && !m_webPage->d->commitRootLayerIfNeeded())
blitVisibleContents();
m_webPage->d->m_client->notifyContentRendered(rect);
}
} else
m_renderQueue->addToQueue(RenderQueue::RegularRender, rect);
}
}
void BackingStorePrivate::slowScroll(const Platform::IntSize& delta, const Platform::IntRect& windowRect, bool immediate)
{
ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
#if DEBUG_BACKINGSTORE
// Start the time measurement...
double time = WTF::currentTime();
#endif
scrollingStartedHelper(delta);
// This windowRect is in untransformed coordinates relative to the viewport, but
// it needs to be transformed coordinates relative to the transformed contents.
Platform::IntRect rect = m_webPage->d->mapToTransformed(m_client->mapFromViewportToContents(windowRect));
if (immediate) {
if (render(rect) && !isSuspended() && !shouldDirectRenderingToWindow() && !m_webPage->d->commitRootLayerIfNeeded())
blitVisibleContents();
} else {
m_renderQueue->addToQueue(RenderQueue::VisibleScroll, rect);
// We only blit here if the client did not generate the scroll as the client
// now supports blitting asynchronously during scroll operations.
if (!m_client->isClientGeneratedScroll() && !shouldDirectRenderingToWindow())
blitVisibleContents();
}
#if DEBUG_BACKINGSTORE
// Stop the time measurement.
double elapsed = WTF::currentTime() - time;
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::slowScroll elapsed=%f", elapsed);
#endif
}
void BackingStorePrivate::scroll(const Platform::IntSize& delta,
const Platform::IntRect& scrollViewRect,
const Platform::IntRect& clipRect)
{
ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
// If we are direct rendering then we are forced to go down the slow path
// to scrolling.
if (shouldDirectRenderingToWindow()) {
Platform::IntRect viewportRect(Platform::IntPoint(0, 0), m_webPage->d->transformedViewportSize());
slowScroll(delta, m_webPage->d->mapFromTransformed(viewportRect), true /*immediate*/);
return;
}
#if DEBUG_BACKINGSTORE
// Start the time measurement...
double time = WTF::currentTime();
#endif
scrollingStartedHelper(delta);
// We only blit here if the client did not generate the scroll as the client
// now supports blitting asynchronously during scroll operations.
if (!m_client->isClientGeneratedScroll())
blitVisibleContents();
#if DEBUG_BACKINGSTORE
// Stop the time measurement.
double elapsed = WTF::currentTime() - time;
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::scroll dx=%d, dy=%d elapsed=%f", delta.width(), delta.height(), elapsed);
#endif
}
void BackingStorePrivate::scrollingStartedHelper(const Platform::IntSize& delta)
{
// Notify the render queue so that it can shuffle accordingly.
m_renderQueue->updateSortDirection(delta.width(), delta.height());
m_renderQueue->visibleContentChanged(visibleContentsRect());
// Scroll the actual backingstore.
scrollBackingStore(delta.width(), delta.height());
// Add any newly visible tiles that have not been previously rendered to the queue
// and check if the tile was previously rendered by regular render job.
updateTilesForScrollOrNotRenderedRegion();
}
bool BackingStorePrivate::shouldSuppressNonVisibleRegularRenderJobs() const
{
#if SUPPRESS_NON_VISIBLE_REGULAR_RENDER_JOBS
return true;
#else
// Always suppress when loading as this drastically decreases page loading
// time...
return m_client->isLoading();
#endif
}
bool BackingStorePrivate::shouldPerformRenderJobs() const
{
return (m_webPage->isVisible() || shouldDirectRenderingToWindow()) && !m_suspendRenderJobs && !m_suspendBackingStoreUpdates && !m_renderQueue->isEmpty(!m_suspendRegularRenderJobs);
}
bool BackingStorePrivate::shouldPerformRegularRenderJobs() const
{
return shouldPerformRenderJobs() && !m_suspendRegularRenderJobs;
}
static const BlackBerry::Platform::Message::Type RenderJobMessageType = BlackBerry::Platform::Message::generateUniqueMessageType();
class RenderJobMessage : public BlackBerry::Platform::ExecutableMessage
{
public:
RenderJobMessage(BlackBerry::Platform::MessageDelegate* delegate)
: BlackBerry::Platform::ExecutableMessage(delegate, BlackBerry::Platform::ExecutableMessage::UniqueCoalescing, RenderJobMessageType)
{ }
};
void BackingStorePrivate::dispatchRenderJob()
{
BlackBerry::Platform::MessageDelegate* messageDelegate = BlackBerry::Platform::createMethodDelegate(&BackingStorePrivate::renderJob, this);
BlackBerry::Platform::webKitThreadMessageClient()->dispatchMessage(new RenderJobMessage(messageDelegate));
}
void BackingStorePrivate::renderJob()
{
if (!shouldPerformRenderJobs())
return;
instrumentBeginFrame();
#if DEBUG_BACKINGSTORE
BlackBerry::Platform::logAlways(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::renderJob");
#endif
m_renderQueue->render(!m_suspendRegularRenderJobs);
#if USE(ACCELERATED_COMPOSITING)
m_webPage->d->commitRootLayerIfNeeded();
#endif
if (shouldPerformRenderJobs())
dispatchRenderJob();
}
Platform::IntRect BackingStorePrivate::expandedContentsRect() const
{
return Platform::IntRect(Platform::IntPoint(0, 0), expandedContentsSize());
}
Platform::IntRect BackingStorePrivate::visibleContentsRect() const
{
return intersection(m_client->transformedVisibleContentsRect(),
Platform::IntRect(Platform::IntPoint(0, 0), m_client->transformedContentsSize()));
}
Platform::IntRect BackingStorePrivate::unclippedVisibleContentsRect() const
{
return m_client->transformedVisibleContentsRect();
}
bool BackingStorePrivate::shouldMoveLeft(const Platform::IntRect& backingStoreRect) const
{
return canMoveX(backingStoreRect)
&& backingStoreRect.x() > visibleContentsRect().x()
&& backingStoreRect.x() > expandedContentsRect().x();
}
bool BackingStorePrivate::shouldMoveRight(const Platform::IntRect& backingStoreRect) const
{
return canMoveX(backingStoreRect)
&& backingStoreRect.right() < visibleContentsRect().right()
&& backingStoreRect.right() < expandedContentsRect().right();
}
bool BackingStorePrivate::shouldMoveUp(const Platform::IntRect& backingStoreRect) const
{
return canMoveY(backingStoreRect)
&& backingStoreRect.y() > visibleContentsRect().y()
&& backingStoreRect.y() > expandedContentsRect().y();
}
bool BackingStorePrivate::shouldMoveDown(const Platform::IntRect& backingStoreRect) const
{
return canMoveY(backingStoreRect)
&& backingStoreRect.bottom() < visibleContentsRect().bottom()
&& backingStoreRect.bottom() < expandedContentsRect().bottom();
}
bool BackingStorePrivate::canMoveX(const Platform::IntRect& backingStoreRect) const
{
return backingStoreRect.width() > visibleContentsRect().width();
}
bool BackingStorePrivate::canMoveY(const Platform::IntRect& backingStoreRect) const
{
return backingStoreRect.height() > visibleContentsRect().height();
}
bool BackingStorePrivate::canMoveLeft(const Platform::IntRect& rect) const
{
Platform::IntRect backingStoreRect = rect;
Platform::IntRect visibleContentsRect = this->visibleContentsRect();
Platform::IntRect contentsRect = this->expandedContentsRect();
backingStoreRect.move(-tileWidth(), 0);
return backingStoreRect.right() >= visibleContentsRect.right()
&& backingStoreRect.x() >= contentsRect.x();
}
bool BackingStorePrivate::canMoveRight(const Platform::IntRect& rect) const
{
Platform::IntRect backingStoreRect = rect;
Platform::IntRect visibleContentsRect = this->visibleContentsRect();
Platform::IntRect contentsRect = this->expandedContentsRect();
backingStoreRect.move(tileWidth(), 0);
return backingStoreRect.x() <= visibleContentsRect.x()
&& (backingStoreRect.right() <= contentsRect.right()
|| (backingStoreRect.right() - contentsRect.right()) < tileWidth());
}
bool BackingStorePrivate::canMoveUp(const Platform::IntRect& rect) const
{
Platform::IntRect backingStoreRect = rect;
Platform::IntRect visibleContentsRect = this->visibleContentsRect();
Platform::IntRect contentsRect = this->expandedContentsRect();
backingStoreRect.move(0, -tileHeight());
return backingStoreRect.bottom() >= visibleContentsRect.bottom()
&& backingStoreRect.y() >= contentsRect.y();
}
bool BackingStorePrivate::canMoveDown(const Platform::IntRect& rect) const
{
Platform::IntRect backingStoreRect = rect;
Platform::IntRect visibleContentsRect = this->visibleContentsRect();
Platform::IntRect contentsRect = this->expandedContentsRect();
backingStoreRect.move(0, tileHeight());
return backingStoreRect.y() <= visibleContentsRect.y()
&& (backingStoreRect.bottom() <= contentsRect.bottom()
|| (backingStoreRect.bottom() - contentsRect.bottom()) < tileHeight());
}
Platform::IntRect BackingStorePrivate::backingStoreRectForScroll(int deltaX, int deltaY, const Platform::IntRect& rect) const
{
// The current rect.
Platform::IntRect backingStoreRect = rect;
// This method uses the delta values to describe the backingstore rect
// given the current scroll direction and the viewport position. However,
// this method can be called with no deltas whatsoever for instance when
// the contents size changes or the orientation changes. In this case, we
// want to use the previous scroll direction to describe the backingstore
// rect. This will result in less checkerboard.
if (!deltaX && !deltaY) {
deltaX = m_previousDelta.width();
deltaY = m_previousDelta.height();
}
m_previousDelta = Platform::IntSize(deltaX, deltaY);
// Return to origin if need be.
if (!canMoveX(backingStoreRect) && backingStoreRect.x())
backingStoreRect.setX(0);
if (!canMoveY(backingStoreRect) && backingStoreRect.y())
backingStoreRect.setY(0);
// Move the rect left.
while (shouldMoveLeft(backingStoreRect) || (deltaX > 0 && canMoveLeft(backingStoreRect)))
backingStoreRect.move(-tileWidth(), 0);
// Move the rect right.
while (shouldMoveRight(backingStoreRect) || (deltaX < 0 && canMoveRight(backingStoreRect)))
backingStoreRect.move(tileWidth(), 0);
// Move the rect up.
while (shouldMoveUp(backingStoreRect) || (deltaY > 0 && canMoveUp(backingStoreRect)))
backingStoreRect.move(0, -tileHeight());
// Move the rect down.
while (shouldMoveDown(backingStoreRect) || (deltaY < 0 && canMoveDown(backingStoreRect)))
backingStoreRect.move(0, tileHeight());
return backingStoreRect;
}
void BackingStorePrivate::setBackingStoreRect(const Platform::IntRect& backingStoreRect, double scale)
{
if (!m_webPage->isVisible())
return;
if (!isActive()) {
m_webPage->d->setShouldResetTilesWhenShown(true);
return;
}
Platform::IntRect currentBackingStoreRect = frontState()->backingStoreRect();
double currentScale = frontState()->scale();
if (backingStoreRect == currentBackingStoreRect && scale == currentScale)
return;
#if DEBUG_TILEMATRIX
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::setBackingStoreRect changed from (%d,%d %dx%d) to (%d,%d %dx%d)",
currentBackingStoreRect.x(),
currentBackingStoreRect.y(),
currentBackingStoreRect.width(),
currentBackingStoreRect.height(),
backingStoreRect.x(),
backingStoreRect.y(),
backingStoreRect.width(),
backingStoreRect.height());
#endif
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
TileIndexList indexesToFill = indexesForBackingStoreRect(backingStoreRect);
ASSERT(static_cast<int>(indexesToFill.size()) == currentMap.size());
TileMap newTileMap;
TileMap leftOverTiles;
// Iterate through our current tile map and add tiles that are rendered with
// our new backing store rect.
TileMap::const_iterator tileMapEnd = currentMap.end();
for (TileMap::const_iterator it = currentMap.begin(); it != tileMapEnd; ++it) {
TileIndex oldIndex = it->key;
BackingStoreTile* tile = it->value;
// Reset the old index.
resetTile(oldIndex, tile, false /*resetBackground*/);
// Origin of last committed render for tile in transformed content coordinates.
Platform::IntPoint origin = originOfLastRenderForTile(oldIndex, tile, currentBackingStoreRect);
// If the new backing store rect contains this origin, then insert the tile there
// and mark it as no longer shifted. Note: Platform::IntRect::contains checks for a 1x1 rect
// below and to the right of the origin so it is correct usage here.
if (backingStoreRect.contains(origin)) {
TileIndex newIndex = indexOfTile(origin, backingStoreRect);
Platform::IntRect rect(origin, tileSize());
if (m_renderQueue->regularRenderJobsPreviouslyAttemptedButNotRendered(rect)) {
// If the render queue previously tried to render this tile, but the
// backingstore wasn't in the correct place or the tile wasn't visible
// at the time then we can't simply restore the tile since the content
// is now invalid as far as WebKit is concerned. Instead, we clear
// the tile here of the region and then put the tile in the render
// queue again.
// Intersect the tile with the not rendered region to get the areas
// of the tile that we need to clear.
Platform::IntRectRegion tileNotRenderedRegion = Platform::IntRectRegion::intersectRegions(m_renderQueue->regularRenderJobsNotRenderedRegion(), rect);
clearAndUpdateTileOfNotRenderedRegion(newIndex, tile, tileNotRenderedRegion, backingStoreRect);
#if DEBUG_BACKINGSTORE
Platform::IntRect extents = tileNotRenderedRegion.extents();
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::setBackingStoreRect did clear tile %d,%d %dx%d",
extents.x(), extents.y(), extents.width(), extents.height());
#endif
} else {
// Mark as needing update.
if (!tile->frontBuffer()->isRendered(currentState->scale())
&& !isCurrentVisibleJob(newIndex, tile, backingStoreRect))
updateTile(origin, false /*immediate*/);
}
// Do some bookkeeping with shifting tiles...
tile->clearShift();
tile->setCommitted(true);
size_t i = indexesToFill.find(newIndex);
ASSERT(i != WTF::notFound);
indexesToFill.remove(i);
newTileMap.add(newIndex, tile);
} else {
// Store this tile and index so we can add it to the remaining left over spots...
leftOverTiles.add(oldIndex, tile);
}
}
ASSERT(static_cast<int>(indexesToFill.size()) == leftOverTiles.size());
size_t i = 0;
TileMap::const_iterator leftOverEnd = leftOverTiles.end();
for (TileMap::const_iterator it = leftOverTiles.begin(); it != leftOverEnd; ++it) {
TileIndex oldIndex = it->key;
BackingStoreTile* tile = it->value;
if (i >= indexesToFill.size()) {
ASSERT_NOT_REACHED();
break;
}
TileIndex newIndex = indexesToFill.at(i);
// Origin of last committed render for tile in transformed content coordinates.
Platform::IntPoint originOfOld = originOfLastRenderForTile(oldIndex, tile, currentBackingStoreRect);
// Origin of the new index for the new backing store rect.
Platform::IntPoint originOfNew = originOfTile(newIndex, backingStoreRect);
// Mark as needing update.
updateTile(originOfNew, false /*immediate*/);
tile->clearShift();
tile->setCommitted(false);
tile->setHorizontalShift((originOfOld.x() - originOfNew.x()) / tileWidth());
tile->setVerticalShift((originOfOld.y() - originOfNew.y()) / tileHeight());
newTileMap.add(newIndex, tile);
++i;
}
// Checks to make sure we haven't lost any tiles.
ASSERT(currentMap.size() == newTileMap.size());
backState()->setScale(scale);
backState()->setNumberOfTilesWide(backingStoreRect.width() / tileWidth());
backState()->setNumberOfTilesHigh(backingStoreRect.height() / tileHeight());
backState()->setBackingStoreOffset(backingStoreRect.location());
backState()->setTileMap(newTileMap);
swapState();
}
BackingStorePrivate::TileIndexList BackingStorePrivate::indexesForBackingStoreRect(const Platform::IntRect& backingStoreRect) const
{
TileIndexList indexes;
int numberOfTilesWide = backingStoreRect.width() / tileWidth();
int numberOfTilesHigh = backingStoreRect.height() / tileHeight();
for (int y = 0; y < numberOfTilesHigh; ++y) {
for (int x = 0; x < numberOfTilesWide; ++x) {
TileIndex index(x, y);
indexes.append(index);
}
}
return indexes;
}
Platform::IntPoint BackingStorePrivate::originOfLastRenderForTile(const TileIndex& index,
BackingStoreTile* tile,
const Platform::IntRect& backingStoreRect) const
{
return originOfTile(indexOfLastRenderForTile(index, tile), backingStoreRect);
}
TileIndex BackingStorePrivate::indexOfLastRenderForTile(const TileIndex& index, BackingStoreTile* tile) const
{
return TileIndex(index.i() + tile->horizontalShift(), index.j() + tile->verticalShift());
}
TileIndex BackingStorePrivate::indexOfTile(const Platform::IntPoint& origin,
const Platform::IntRect& backingStoreRect) const
{
int offsetX = origin.x() - backingStoreRect.x();
int offsetY = origin.y() - backingStoreRect.y();
if (offsetX)
offsetX = offsetX / tileWidth();
if (offsetY)
offsetY = offsetY / tileHeight();
return TileIndex(offsetX, offsetY);
}
void BackingStorePrivate::clearAndUpdateTileOfNotRenderedRegion(const TileIndex& index, BackingStoreTile* tile,
const Platform::IntRectRegion& tileNotRenderedRegion,
const Platform::IntRect& backingStoreRect,
bool update)
{
if (tileNotRenderedRegion.isEmpty())
return;
// Intersect the tile with the not rendered region to get the areas
// of the tile that we need to clear.
IntRectList tileNotRenderedRegionRects = tileNotRenderedRegion.rects();
for (size_t i = 0; i < tileNotRenderedRegionRects.size(); ++i) {
Platform::IntRect tileNotRenderedRegionRect = tileNotRenderedRegionRects.at(i);
// Clear the render queue of this rect.
m_renderQueue->clear(tileNotRenderedRegionRect, true /*clearRegularRenderJobs*/);
if (update) {
// Add it again as a regular render job.
m_renderQueue->addToQueue(RenderQueue::RegularRender, tileNotRenderedRegionRect);
}
}
// Find the origin of this tile.
Platform::IntPoint origin = originOfTile(index, backingStoreRect);
// Map to tile coordinates.
Platform::IntRectRegion translatedRegion(tileNotRenderedRegion);
translatedRegion.move(-origin.x(), -origin.y());
// If the region in question is already marked as not rendered, return early
if (Platform::IntRectRegion::intersectRegions(tile->frontBuffer()->renderedRegion(), translatedRegion).isEmpty())
return;
// Clear the tile of this region. The back buffer region is invalid anyway, but the front
// buffer must not be manipulated without synchronization with the compositing thread, or
// we have a race.
// Instead of using the customary sequence of copy-back, modify and swap, we send a synchronous
// message to the compositing thread to avoid the copy-back step and save memory bandwidth.
// The trade-off is that the WebKit thread might wait a little longer for the compositing thread
// than it would from a waitForCurrentMessage() call.
ASSERT(Platform::webKitThreadMessageClient()->isCurrentThread());
if (!Platform::webKitThreadMessageClient()->isCurrentThread())
return;
Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
Platform::createMethodCallMessage(&BackingStorePrivate::clearRenderedRegion,
this, tile, translatedRegion));
}
void BackingStorePrivate::clearRenderedRegion(BackingStoreTile* tile, const Platform::IntRectRegion& region)
{
ASSERT(Platform::userInterfaceThreadMessageClient()->isCurrentThread());
if (!Platform::userInterfaceThreadMessageClient()->isCurrentThread())
return;
tile->frontBuffer()->clearRenderedRegion(region);
}
bool BackingStorePrivate::isCurrentVisibleJob(const TileIndex& index, BackingStoreTile* tile, const Platform::IntRect& backingStoreRect) const
{
// First check if the whole rect is in the queue.
Platform::IntRect wholeRect = Platform::IntRect(originOfTile(index, backingStoreRect), tileSize());
if (m_renderQueue->isCurrentVisibleScrollJob(wholeRect) || m_renderQueue->isCurrentVisibleScrollJobCompleted(wholeRect))
return true;
// Second check if the individual parts of the non-rendered region are in the regular queue.
IntRectList tileNotRenderedRegionRects = tile->frontBuffer()->notRenderedRegion().rects();
for (size_t i = 0; i < tileNotRenderedRegionRects.size(); ++i) {
Platform::IntRect tileNotRenderedRegionRect = tileNotRenderedRegionRects.at(i);
Platform::IntPoint origin = originOfTile(index, backingStoreRect);
// Map to transformed contents coordinates.
tileNotRenderedRegionRect.move(origin.x(), origin.y());
if (!m_renderQueue->isCurrentRegularRenderJob(tileNotRenderedRegionRect))
return false;
}
return true;
}
void BackingStorePrivate::scrollBackingStore(int deltaX, int deltaY)
{
ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
if (!m_webPage->isVisible())
return;
if (!isActive()) {
m_webPage->d->setShouldResetTilesWhenShown(true);
return;
}
// Calculate our new preferred matrix dimension.
if (deltaX || deltaY)
m_preferredTileMatrixDimension = abs(deltaX) > abs(deltaY) ? Horizontal : Vertical;
// Calculate our preferred matrix geometry.
Divisor divisor = bestDivisor(expandedContentsSize(),
tileWidth(), tileHeight(),
minimumNumberOfTilesWide(), minimumNumberOfTilesHigh(),
m_preferredTileMatrixDimension);
#if DEBUG_TILEMATRIX
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::scrollBackingStore divisor %dx%d",
divisor.first,
divisor.second);
#endif
// Initialize a rect with that new geometry.
Platform::IntRect backingStoreRect(0, 0, divisor.first * tileWidth(), divisor.second * tileHeight());
// Scroll that rect so that it fits our contents and viewport and scroll delta.
backingStoreRect = backingStoreRectForScroll(deltaX, deltaY, backingStoreRect);
ASSERT(!backingStoreRect.isEmpty());
setBackingStoreRect(backingStoreRect, m_webPage->d->currentScale());
}
bool BackingStorePrivate::renderDirectToWindow(const Platform::IntRect& rect)
{
requestLayoutIfNeeded();
Platform::IntRect dirtyRect = rect;
dirtyRect.intersect(unclippedVisibleContentsRect());
if (dirtyRect.isEmpty())
return false;
Platform::IntRect screenRect = m_client->mapFromTransformedContentsToTransformedViewport(dirtyRect);
windowFrontBufferState()->clearBlittedRegion(screenRect);
paintDefaultBackground(dirtyRect, m_webPage->webkitThreadViewportAccessor(), true /*flush*/);
const Platform::IntPoint origin = unclippedVisibleContentsRect().location();
// We don't need a buffer since we're direct rendering to window.
renderContents(0, origin, dirtyRect);
windowBackBufferState()->addBlittedRegion(screenRect);
#if USE(ACCELERATED_COMPOSITING)
m_isDirectRenderingAnimationMessageScheduled = false;
if (m_webPage->d->isAcceleratedCompositingActive()) {
BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
BlackBerry::Platform::createMethodCallMessage(
&BackingStorePrivate::drawAndBlendLayersForDirectRendering,
this, dirtyRect));
}
#endif
invalidateWindow(screenRect);
return true;
}
bool BackingStorePrivate::render(const Platform::IntRect& rect)
{
if (!m_webPage->isVisible())
return false;
requestLayoutIfNeeded();
if (shouldDirectRenderingToWindow())
return renderDirectToWindow(rect);
// If direct rendering is off, even though we're not active, someone else
// has to render the root layer. There are no tiles available for us to
// draw to.
if (!isActive())
return false;
TileRectList tileRectList = mapFromTransformedContentsToTiles(rect);
if (tileRectList.isEmpty())
return false;
#if DEBUG_BACKINGSTORE
BBLOG(BlackBerry::Platform::LogLevelCritical,
"BackingStorePrivate::render rect=(%d,%d %dx%d), m_suspendBackingStoreUpdates = %s",
rect.x(), rect.y(), rect.width(), rect.height(),
m_suspendBackingStoreUpdates ? "true" : "false");
#endif
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
double currentScale = currentState->scale();
for (size_t i = 0; i < tileRectList.size(); ++i) {
TileRect tileRect = tileRectList[i];
TileIndex index = tileRect.first;
Platform::IntRect dirtyTileRect = tileRect.second;
BackingStoreTile* tile = currentMap.get(index);
// This dirty tile rect is in tile coordinates, but it needs to be in
// transformed contents coordinates.
Platform::IntRect dirtyRect = mapFromTilesToTransformedContents(tileRect);
// If the tile has been created, but this is the first time we are painting on it
// then it hasn't been given a default background yet so that we can save time during
// startup. That's why we are doing it here instead...
if (!tile->backgroundPainted())
tile->paintBackground();
tile->backBuffer()->setScale(currentScale);
// Paint default background if contents rect is empty.
if (!expandedContentsRect().isEmpty()) {
// Otherwise we should clip the contents size and render the content.
dirtyRect.intersect(expandedContentsRect());
dirtyTileRect.intersect(tileContentsRect(index, expandedContentsRect(), currentState));
// We probably have extra tiles since the contents size is so small.
// Save some cycles here...
if (dirtyRect.isEmpty())
continue;
}
BlackBerry::Platform::Graphics::Buffer* nativeBuffer
= tile->backBuffer()->nativeBuffer();
// TODO: This code is only needed for EGLImage code path, but preferrably BackingStore
// should not know that, and the synchronization should be in BlackBerry::Platform::Graphics
// if possible.
if (isOpenGLCompositing())
SurfacePool::globalSurfacePool()->waitForBuffer(tile->backBuffer());
// Modify the buffer only after we've waited for the buffer to become available above.
// If we're not yet committed, then commit only after the tile has back buffer has been
// swapped in so it has some valid content.
// Otherwise the compositing thread could pick up the tile while its front buffer is still invalid.
bool wasCommitted = tile->isCommitted();
if (wasCommitted)
copyPreviousContentsToBackSurfaceOfTile(dirtyTileRect, tile);
else
tile->backBuffer()->clearRenderedRegion();
// FIXME: modify render to take a Vector<IntRect> parameter so we're not recreating
// GraphicsContext on the stack each time.
renderContents(nativeBuffer, originOfTile(index), dirtyRect);
// Add the newly rendered region to the tile so it can keep track for blits.
tile->backBuffer()->addRenderedRegion(dirtyTileRect);
// Thanks to the copyPreviousContentsToBackSurfaceOfTile() call above, we know that
// the rendered region of the back buffer contains the rendered region of the front buffer.
// Assert this just to make sure.
// For previously uncommitted tiles, the front buffer's rendered region is not relevant.
ASSERT(!wasCommitted || tile->backBuffer()->isRendered(tile->frontBuffer()->renderedRegion(), currentScale));
// We will need a swap here because of the shared back buffer.
tile->swapBuffers();
if (!wasCommitted) {
// Commit the tile only after it has valid front buffer contents. Now, the compositing thread
// can finally start blitting this tile.
tile->clearShift();
tile->setCommitted(true);
}
// Before clearing the render region, wait for the compositing thread to stop using the
// buffer, in order to avoid a race on its rendered region.
BlackBerry::Platform::userInterfaceThreadMessageClient()->syncToCurrentMessage();
tile->backBuffer()->clearRenderedRegion();
}
return true;
}
void BackingStorePrivate::requestLayoutIfNeeded() const
{
m_webPage->d->requestLayoutIfNeeded();
}
bool BackingStorePrivate::renderVisibleContents()
{
Platform::IntRect renderRect = shouldDirectRenderingToWindow() ? visibleContentsRect() : visibleTilesRect();
if (render(renderRect)) {
m_renderQueue->clear(renderRect, true /*clearRegularRenderJobs*/);
return true;
}
return false;
}
bool BackingStorePrivate::renderBackingStore()
{
return render(frontState()->backingStoreRect());
}
void BackingStorePrivate::copyPreviousContentsToBackSurfaceOfWindow()
{
Platform::IntRectRegion previousContentsRegion
= Platform::IntRectRegion::subtractRegions(windowFrontBufferState()->blittedRegion(), windowBackBufferState()->blittedRegion());
if (previousContentsRegion.isEmpty())
return;
if (Window* window = m_webPage->client()->window())
window->copyFromFrontToBack(previousContentsRegion);
windowBackBufferState()->addBlittedRegion(previousContentsRegion);
}
void BackingStorePrivate::copyPreviousContentsToBackSurfaceOfTile(const Platform::IntRect& rect,
BackingStoreTile* tile)
{
Platform::IntRectRegion previousContentsRegion
= Platform::IntRectRegion::subtractRegions(tile->frontBuffer()->renderedRegion(), rect);
IntRectList previousContentsRects = previousContentsRegion.rects();
for (size_t i = 0; i < previousContentsRects.size(); ++i) {
Platform::IntRect previousContentsRect = previousContentsRects.at(i);
tile->backBuffer()->addRenderedRegion(previousContentsRect);
BlackBerry::Platform::Graphics::blitToBuffer(
tile->backBuffer()->nativeBuffer(), previousContentsRect,
tile->frontBuffer()->nativeBuffer(), previousContentsRect);
}
}
void BackingStorePrivate::paintDefaultBackground(const Platform::IntRect& dstRect, Platform::ViewportAccessor* viewportAccessor, bool flush)
{
Platform::IntRect clippedDstRect = dstRect;
// Because of rounding it is possible that overScrollRect could be off-by-one larger
// than the surface size of the window. We prevent this here, by clamping
// it to ensure that can't happen.
clippedDstRect.intersect(Platform::IntRect(Platform::IntPoint(0, 0), surfaceSize()));
if (clippedDstRect.isEmpty())
return;
// We have to paint the default background in the case of overzoom and
// make sure it is invalidated.
const Platform::IntRect pixelContentsRect = viewportAccessor->pixelContentsRect();
Platform::IntRectRegion overScrollRegion = Platform::IntRectRegion::subtractRegions(
dstRect, viewportAccessor->pixelViewportFromContents(pixelContentsRect));
IntRectList overScrollRects = overScrollRegion.rects();
for (size_t i = 0; i < overScrollRects.size(); ++i) {
Platform::IntRect overScrollRect = overScrollRects.at(i);
if (m_webPage->settings()->isEnableDefaultOverScrollBackground()) {
fillWindow(BlackBerry::Platform::Graphics::DefaultBackgroundPattern,
overScrollRect, overScrollRect.location(), 1.0 /*contentsScale*/);
} else {
Color color(m_webPage->settings()->overScrollColor());
clearWindow(overScrollRect, color.red(), color.green(), color.blue(), color.alpha());
}
}
}
void BackingStorePrivate::blitVisibleContents(bool force)
{
// Blitting must never happen for direct rendering case.
// Use invalidateWindow() instead.
ASSERT(!shouldDirectRenderingToWindow());
if (shouldDirectRenderingToWindow()) {
BlackBerry::Platform::logAlways(BlackBerry::Platform::LogLevelCritical,
"BackingStore::blitVisibleContents operation not supported in direct rendering mode");
return;
}
if (!m_webPage->isVisible() || m_suspendScreenUpdates) {
// Avoid client going into busy loop while blit is impossible.
if (force)
m_hasBlitJobs = false;
return;
}
if (!BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread()) {
BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchMessage(
BlackBerry::Platform::createMethodCallMessage(
&BackingStorePrivate::blitVisibleContents, this, force));
return;
}
if (m_defersBlit && !force) {
#if USE(ACCELERATED_COMPOSITING)
// If there's a WebPageCompositorClient, let it schedule the blit.
if (WebPageCompositorPrivate* compositor = m_webPage->d->compositor()) {
if (WebPageCompositorClient* client = compositor->client()) {
client->invalidate(0);
return;
}
}
#endif
m_hasBlitJobs = true;
return;
}
m_hasBlitJobs = false;
Platform::ViewportAccessor* viewportAccessor = m_webPage->client()->userInterfaceViewportAccessor();
const Platform::IntRect dstRect = viewportAccessor->destinationSurfaceRect();
const Platform::IntRect pixelViewportRect = viewportAccessor->pixelViewportRect();
const Platform::FloatRect documentViewportRect = viewportAccessor->documentFromPixelContents(pixelViewportRect);
Platform::IntRect pixelSrcRect = pixelViewportRect;
Platform::FloatRect documentSrcRect = documentViewportRect;
#if DEBUG_VISUALIZE
// Substitute a srcRect that consists of the whole backingstore geometry
// instead of the normal viewport so we can visualize the entire
// backingstore and what it is doing when we scroll and zoom!
Platform::ViewportAccessor* debugViewportAccessor = new BackingStoreVisualizationViewportAccessor(viewportAccessor, this);
if (isActive()) {
viewportAccessor = debugViewportAccessor;
documentSrcRect = debugViewportAccessor->documentViewportRect();
pixelSrcRect = debugViewportAccessor->pixelViewportRect();
}
#endif
#if DEBUG_BACKINGSTORE
BlackBerry::Platform::logAlways(BlackBerry::Platform::LogLevelCritical,
"BackingStorePrivate::blitVisibleContents(): dstRect=(%d,%d) %dx%d, documentSrcRect=(%f, %f) %f x %f, scale=%f",
dstRect.x(), dstRect.y(), dstRect.width(), dstRect.height(),
documentSrcRect.x(), documentSrcRect.y(), documentSrcRect.width(), documentSrcRect.height(),
viewportAccessor->scale());
#endif
Vector<TileBuffer*> blittedTiles;
if (isActive() && !m_webPage->d->compositorDrawsRootLayer()) {
paintDefaultBackground(dstRect, viewportAccessor, false /*flush*/);
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
double currentScale = currentState->scale();
const Platform::IntRect transformedContentsRect = Platform::IntRect(Platform::IntPoint(0, 0), m_client->transformedContentsSize());
// For blitting backingstore tiles, we need the srcRect to be specified
// in backingstore tile pixel coordinates. If our viewport accessor is
// at a different scale, we calculate those coordinates by ourselves.
const Platform::IntRect transformedSrcRect = currentScale == viewportAccessor->scale()
? pixelSrcRect
: viewportAccessor->roundFromDocumentContents(documentSrcRect, currentScale);
Platform::IntRect clippedTransformedSrcRect = transformedSrcRect;
const Platform::IntPoint origin = transformedSrcRect.location();
// FIXME: This should not explicitly depend on WebCore::.
TransformationMatrix transformation;
if (!transformedSrcRect.isEmpty())
transformation = TransformationMatrix::rectToRect(FloatRect(FloatPoint(0.0, 0.0), WebCore::IntSize(transformedSrcRect.size())), WebCore::IntRect(dstRect));
#if DEBUG_CHECKERBOARD
bool blitCheckered = false;
#endif
// Don't clip to contents if it is empty so we can still paint default background.
if (!transformedContentsRect.isEmpty()) {
clippedTransformedSrcRect.intersect(transformedContentsRect);
if (clippedTransformedSrcRect.isEmpty()) {
invalidateWindow(dstRect);
return;
}
Platform::IntRectRegion transformedSrcRegion = clippedTransformedSrcRect;
Platform::IntRectRegion backingStoreRegion = currentState->backingStoreRect();
Platform::IntRectRegion checkeredRegion
= Platform::IntRectRegion::subtractRegions(transformedSrcRegion, backingStoreRegion);
// Blit checkered to those parts that are not covered by the backingStoreRect.
IntRectList checkeredRects = checkeredRegion.rects();
for (size_t i = 0; i < checkeredRects.size(); ++i) {
Platform::IntRect clippedDstRect = transformation.mapRect(Platform::IntRect(
Platform::IntPoint(checkeredRects.at(i).x() - origin.x(), checkeredRects.at(i).y() - origin.y()),
checkeredRects.at(i).size()));
// To eliminate 1 pixel inflation due to transformation rounding.
clippedDstRect.intersect(dstRect);
#if DEBUG_CHECKERBOARD
blitCheckered = true;
#endif
fillWindow(BlackBerry::Platform::Graphics::CheckerboardPattern,
clippedDstRect, checkeredRects.at(i).location(), transformation.a());
}
}
// Get the list of tile rects that makeup the content.
TileRectList tileRectList = mapFromTransformedContentsToTiles(clippedTransformedSrcRect, currentState);
for (size_t i = 0; i < tileRectList.size(); ++i) {
TileRect tileRect = tileRectList[i];
TileIndex index = tileRect.first;
Platform::IntRect dirtyTileRect = tileRect.second;
BackingStoreTile* tile = currentMap.get(index);
TileBuffer* tileBuffer = tile->frontBuffer();
// This dirty rect is in tile coordinates, but it needs to be in
// transformed contents coordinates.
Platform::IntRect dirtyRect
= mapFromTilesToTransformedContents(tileRect, currentState->backingStoreRect());
// Don't clip to contents if it is empty so we can still paint default background.
if (!transformedContentsRect.isEmpty()) {
// Otherwise we should clip the contents size and blit.
dirtyRect.intersect(transformedContentsRect);
// We probably have extra tiles since the contents size is so small.
// Save some cycles here...
if (dirtyRect.isEmpty())
continue;
}
// Now, this dirty rect is in transformed coordinates relative to the
// transformed contents, but ultimately it needs to be transformed
// coordinates relative to the viewport.
dirtyRect.move(-origin.x(), -origin.y());
// Save some cycles here...
if (dirtyRect.isEmpty() || dirtyTileRect.isEmpty())
continue;
TileRect wholeTileRect;
wholeTileRect.first = index;
wholeTileRect.second = this->tileRect();
bool committed = tile->isCommitted();
bool rendered = tileBuffer->isRendered(dirtyTileRect, currentScale);
bool paintCheckered = !committed || !rendered;
if (paintCheckered) {
Platform::IntRect dirtyRectT = transformation.mapRect(dirtyRect);
if (!transformation.isIdentity()) {
// Because of rounding it is possible that dirtyRect could be off-by-one larger
// than the surface size of the dst buffer. We prevent this here, by clamping
// it to ensure that can't happen.
dirtyRectT.intersect(Platform::IntRect(Platform::IntPoint(0, 0), surfaceSize()));
}
const Platform::IntPoint contentsOrigin(dirtyRect.x() + origin.x(), dirtyRect.y() + origin.y());
#if DEBUG_CHECKERBOARD
blitCheckered = true;
#endif
fillWindow(BlackBerry::Platform::Graphics::CheckerboardPattern,
dirtyRectT, contentsOrigin, transformation.a());
}
// Blit the visible buffer here if we have visible zoom jobs.
if (m_renderQueue->hasCurrentVisibleZoomJob()) {
// Needs to be in same coordinate system as dirtyRect.
Platform::IntRect visibleTileBufferRect = m_visibleTileBufferRect;
visibleTileBufferRect.move(-origin.x(), -origin.y());
// Clip to the visibleTileBufferRect.
dirtyRect.intersect(visibleTileBufferRect);
// Clip to the dirtyRect.
visibleTileBufferRect.intersect(dirtyRect);
if (!dirtyRect.isEmpty() && !visibleTileBufferRect.isEmpty()) {
BackingStoreTile* visibleTileBuffer
= SurfacePool::globalSurfacePool()->visibleTileBuffer();
ASSERT(visibleTileBuffer->size() == visibleContentsRect().size());
// The offset of the current viewport with the visble tile buffer.
Platform::IntPoint difference = origin - m_visibleTileBufferRect.location();
Platform::IntSize offset = Platform::IntSize(difference.x(), difference.y());
// Map to the visibleTileBuffer coordinates.
Platform::IntRect dirtyTileRect = visibleTileBufferRect;
dirtyTileRect.move(offset.width(), offset.height());
Platform::IntRect dirtyRectT = transformation.mapRect(dirtyRect);
if (!transformation.isIdentity()) {
// Because of rounding it is possible that dirtyRect could be off-by-one larger
// than the surface size of the dst buffer. We prevent this here, by clamping
// it to ensure that can't happen.
dirtyRectT.intersect(Platform::IntRect(Platform::IntPoint(0, 0), surfaceSize()));
}
blitToWindow(dirtyRectT,
visibleTileBuffer->frontBuffer()->nativeBuffer(),
dirtyTileRect,
false /*blend*/, 255);
}
} else if (committed) {
// Intersect the rendered region.
Platform::IntRectRegion renderedRegion = tileBuffer->renderedRegion();
IntRectList dirtyRenderedRects = renderedRegion.rects();
for (size_t i = 0; i < dirtyRenderedRects.size(); ++i) {
TileRect tileRect;
tileRect.first = index;
tileRect.second = intersection(dirtyTileRect, dirtyRenderedRects.at(i));
if (tileRect.second.isEmpty())
continue;
// Blit the rendered parts.
blitTileRect(tileBuffer, tileRect, origin, transformation, currentState);
}
blittedTiles.append(tileBuffer);
}
}
}
// TODO: This code is only needed for EGLImage code path, but preferrably BackingStore
// should not know that, and the synchronization should be in BlackBerry::Platform::Graphics
// if possible.
if (isOpenGLCompositing())
SurfacePool::globalSurfacePool()->notifyBuffersComposited(blittedTiles);
#if USE(ACCELERATED_COMPOSITING)
if (WebPageCompositorPrivate* compositor = m_webPage->d->compositor()) {
compositor->drawLayers(dstRect, documentSrcRect);
if (compositor->drawsRootLayer())
paintDefaultBackground(dstRect, viewportAccessor, false /*flush*/);
}
#endif
#if ENABLE_SCROLLBARS
if (isScrollingOrZooming() && m_client->isMainFrame()) {
blitHorizontalScrollbar();
blitVerticalScrollbar();
}
#endif
#if DEBUG_VISUALIZE
if (debugViewportAccessor) {
Platform::Graphics::Buffer* targetBuffer = buffer();
Platform::IntRect wkViewport = debugViewportAccessor->roundToPixelFromDocumentContents(Platform::IntRect(m_client->visibleContentsRect()));
Platform::IntRect uiViewport = debugViewportAccessor->roundToPixelFromDocumentContents(documentViewportRect);
wkViewport.move(-pixelSrcRect.x(), -pixelSrcRect.y());
uiViewport.move(-pixelSrcRect.x(), -pixelSrcRect.y());
// Draw a blue rect for the webkit thread viewport.
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(wkViewport.x(), wkViewport.y(), wkViewport.width(), 1), 0, 0, 255, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(wkViewport.x(), wkViewport.y(), 1, wkViewport.height()), 0, 0, 255, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(wkViewport.x(), wkViewport.bottom() - 1, wkViewport.width(), 1), 0, 0, 255, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(wkViewport.right() - 1, wkViewport.y(), 1, wkViewport.height()), 0, 0, 255, 255);
// Draw a red rect for the ui thread viewport.
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(uiViewport.x(), uiViewport.y(), uiViewport.width(), 1), 255, 0, 0, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(uiViewport.x(), uiViewport.y(), 1, uiViewport.height()), 255, 0, 0, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(uiViewport.x(), uiViewport.bottom() - 1, uiViewport.width(), 1), 255, 0, 0, 255);
Platform::Graphics::clearBuffer(targetBuffer, Platform::IntRect(uiViewport.right() - 1, uiViewport.y(), 1, uiViewport.height()), 255, 0, 0, 255);
delete debugViewportAccessor;
}
#endif
#if DEBUG_CHECKERBOARD
static double lastCheckeredTime = 0;
if (blitCheckered && !lastCheckeredTime) {
lastCheckeredTime = WTF::currentTime();
BBLOG(BlackBerry::Platform::LogLevelCritical,
"Blitting checkered pattern at %f\n", lastCheckeredTime);
} else if (blitCheckered && lastCheckeredTime) {
BBLOG(BlackBerry::Platform::LogLevelCritical,
"Blitting checkered pattern at %f\n", WTF::currentTime());
} else if (!blitCheckered && lastCheckeredTime) {
double time = WTF::currentTime();
BBLOG(BlackBerry::Platform::LogLevelCritical,
"Blitting over checkered pattern at %f took %f\n", time, time - lastCheckeredTime);
lastCheckeredTime = 0;
}
#endif
invalidateWindow(dstRect);
}
#if USE(ACCELERATED_COMPOSITING)
void BackingStorePrivate::compositeContents(WebCore::LayerRenderer* layerRenderer, const WebCore::TransformationMatrix& transform, const WebCore::FloatRect& contents, bool contentsOpaque)
{
const Platform::IntRect transformedContentsRect = Platform::IntRect(Platform::IntPoint(0, 0), m_client->transformedContentsSize());
Platform::IntRect transformedContents = enclosingIntRect(m_webPage->d->m_transformationMatrix->mapRect(contents));
transformedContents.intersect(transformedContentsRect);
if (transformedContents.isEmpty())
return;
if (!isActive())
return;
if (m_webPage->d->compositorDrawsRootLayer())
return;
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
Vector<TileBuffer*> compositedTiles;
Platform::IntRectRegion transformedContentsRegion = transformedContents;
Platform::IntRectRegion backingStoreRegion = currentState->backingStoreRect();
Platform::IntRectRegion checkeredRegion
= Platform::IntRectRegion::subtractRegions(transformedContentsRegion, backingStoreRegion);
// Blit checkered to those parts that are not covered by the backingStoreRect.
IntRectList checkeredRects = checkeredRegion.rects();
for (size_t i = 0; i < checkeredRects.size(); ++i)
layerRenderer->drawCheckerboardPattern(transform, m_webPage->d->mapFromTransformedFloatRect(WebCore::IntRect(checkeredRects.at(i))));
// Get the list of tile rects that makeup the content.
TileRectList tileRectList = mapFromTransformedContentsToTiles(transformedContents, currentState);
for (size_t i = 0; i < tileRectList.size(); ++i) {
TileRect tileRect = tileRectList[i];
TileIndex index = tileRect.first;
Platform::IntRect dirtyTileRect = tileRect.second;
BackingStoreTile* tile = currentMap.get(index);
TileBuffer* tileBuffer = tile->frontBuffer();
// This dirty rect is in tile coordinates, but it needs to be in
// transformed contents coordinates.
Platform::IntRect dirtyRect = mapFromTilesToTransformedContents(tileRect, currentState->backingStoreRect());
if (!dirtyRect.intersects(transformedContents))
continue;
TileRect wholeTileRect;
wholeTileRect.first = index;
wholeTileRect.second = this->tileRect();
Platform::IntRect wholeRect = mapFromTilesToTransformedContents(wholeTileRect, currentState->backingStoreRect());
bool committed = tile->isCommitted();
if (!committed)
layerRenderer->drawCheckerboardPattern(transform, m_webPage->d->mapFromTransformedFloatRect(Platform::FloatRect(dirtyRect)));
else {
layerRenderer->compositeBuffer(transform, m_webPage->d->mapFromTransformedFloatRect(Platform::FloatRect(wholeRect)), tileBuffer->nativeBuffer(), contentsOpaque, 1.0f);
compositedTiles.append(tileBuffer);
// Intersect the rendered region.
Platform::IntRectRegion notRenderedRegion = Platform::IntRectRegion::subtractRegions(dirtyTileRect, tileBuffer->renderedRegion());
IntRectList notRenderedRects = notRenderedRegion.rects();
for (size_t i = 0; i < notRenderedRects.size(); ++i)
layerRenderer->drawCheckerboardPattern(transform, m_webPage->d->mapFromTransformedFloatRect(Platform::FloatRect(notRenderedRects.at(i))));
}
}
SurfacePool::globalSurfacePool()->notifyBuffersComposited(compositedTiles);
}
#endif
Platform::IntRect BackingStorePrivate::blitTileRect(TileBuffer* tileBuffer,
const TileRect& tileRect,
const Platform::IntPoint& origin,
const WebCore::TransformationMatrix& matrix,
BackingStoreGeometry* state)
{
if (!m_webPage->isVisible() || !isActive())
return Platform::IntRect();
Platform::IntRect dirtyTileRect = tileRect.second;
// This dirty rect is in tile coordinates, but it needs to be in
// transformed contents coordinates.
Platform::IntRect dirtyRect = mapFromTilesToTransformedContents(tileRect, state->backingStoreRect());
// Now, this dirty rect is in transformed coordinates relative to the
// transformed contents, but ultimately it needs to be transformed
// coordinates relative to the viewport.
dirtyRect.move(-origin.x(), -origin.y());
dirtyRect = matrix.mapRect(dirtyRect);
if (!matrix.isIdentity()) {
// Because of rounding it is possible that dirtyRect could be off-by-one larger
// than the surface size of the dst buffer. We prevent this here, by clamping
// it to ensure that can't happen.
dirtyRect.intersect(Platform::IntRect(Platform::IntPoint(0, 0), surfaceSize()));
}
ASSERT(!dirtyRect.isEmpty());
ASSERT(!dirtyTileRect.isEmpty());
if (dirtyRect.isEmpty() || dirtyTileRect.isEmpty())
return Platform::IntRect();
blitToWindow(dirtyRect, tileBuffer->nativeBuffer(), dirtyTileRect,
false /*blend*/, 255);
return dirtyRect;
}
void BackingStorePrivate::blitHorizontalScrollbar()
{
if (!m_webPage->isVisible())
return;
m_webPage->client()->drawHorizontalScrollbar();
}
void BackingStorePrivate::blitVerticalScrollbar()
{
if (!m_webPage->isVisible())
return;
m_webPage->client()->drawVerticalScrollbar();
}
bool BackingStorePrivate::isTileVisible(const TileIndex& index) const
{
TileRect tileRect;
tileRect.first = index;
tileRect.second = this->tileRect();
return mapFromTilesToTransformedContents(tileRect).intersects(visibleContentsRect());
}
bool BackingStorePrivate::isTileVisible(const Platform::IntPoint& origin) const
{
return Platform::IntRect(origin, tileSize()).intersects(visibleContentsRect());
}
Platform::IntRect BackingStorePrivate::visibleTilesRect() const
{
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
Platform::IntRect rect;
TileMap::const_iterator end = currentMap.end();
for (TileMap::const_iterator it = currentMap.begin(); it != end; ++it) {
TileRect tileRect;
tileRect.first = it->key;
tileRect.second = this->tileRect();
Platform::IntRect tile = mapFromTilesToTransformedContents(tileRect);
if (tile.intersects(visibleContentsRect()))
rect = Platform::unionOfRects(rect, tile);
}
return rect;
}
Platform::IntRect BackingStorePrivate::tileVisibleContentsRect(const TileIndex& index) const
{
if (!isTileVisible(index))
return Platform::IntRect();
return tileContentsRect(index, visibleContentsRect());
}
Platform::IntRect BackingStorePrivate::tileUnclippedVisibleContentsRect(const TileIndex& index) const
{
if (!isTileVisible(index))
return Platform::IntRect();
return tileContentsRect(index, unclippedVisibleContentsRect());
}
Platform::IntRect BackingStorePrivate::tileContentsRect(const TileIndex& index,
const Platform::IntRect& contents) const
{
return tileContentsRect(index, contents, frontState());
}
Platform::IntRect BackingStorePrivate::tileContentsRect(const TileIndex& index,
const Platform::IntRect& contents,
BackingStoreGeometry* state) const
{
TileRectList tileRectList = mapFromTransformedContentsToTiles(contents, state);
for (size_t i = 0; i < tileRectList.size(); ++i) {
TileRect tileRect = tileRectList[i];
if (index == tileRect.first)
return tileRect.second;
}
return Platform::IntRect();
}
void BackingStorePrivate::resetRenderQueue()
{
m_renderQueue->reset();
}
void BackingStorePrivate::clearVisibleZoom()
{
m_renderQueue->clearVisibleZoom();
}
void BackingStorePrivate::resetTiles(bool resetBackground)
{
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
TileMap::const_iterator end = currentMap.end();
for (TileMap::const_iterator it = currentMap.begin(); it != end; ++it)
resetTile(it->key, it->value, resetBackground);
}
void BackingStorePrivate::updateTiles(bool updateVisible, bool immediate)
{
if (!isActive())
return;
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
TileMap::const_iterator end = currentMap.end();
for (TileMap::const_iterator it = currentMap.begin(); it != end; ++it) {
bool isVisible = isTileVisible(it->key);
if (!updateVisible && isVisible)
continue;
updateTile(it->key, immediate);
}
}
void BackingStorePrivate::updateTilesForScrollOrNotRenderedRegion(bool checkLoading)
{
// This method looks at all the tiles and if they are visible, but not completely
// rendered or we are loading, then it updates them. For all tiles, visible and
// non-visible, if a previous attempt was made to render them during a regular
// render job, but they were not visible at the time, then update them and if
// they are currently visible, reset them.
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
Platform::IntRect backingStoreRect = currentState->backingStoreRect();
bool isLoading = m_client->loadState() == WebPagePrivate::Committed;
bool forceVisible = checkLoading && isLoading;
TileMap::const_iterator end = currentMap.end();
for (TileMap::const_iterator it = currentMap.begin(); it != end; ++it) {
TileIndex index = it->key;
BackingStoreTile* tile = it->value;
bool isVisible = isTileVisible(index);
// The rect in transformed contents coordinates.
Platform::IntRect rect(originOfTile(index), tileSize());
if (tile->isCommitted()
&& m_renderQueue->regularRenderJobsPreviouslyAttemptedButNotRendered(rect)) {
// If the render queue previously tried to render this tile, but the
// tile wasn't visible at the time we can't simply restore the tile
// since the content is now invalid as far as WebKit is concerned.
// Instead, we clear that part of the tile if it is visible and then
// put the tile in the render queue again.
if (isVisible) {
// Intersect the tile with the not rendered region to get the areas
// of the tile that we need to clear.
Platform::IntRectRegion tileNotRenderedRegion
= Platform::IntRectRegion::intersectRegions(
m_renderQueue->regularRenderJobsNotRenderedRegion(),
rect);
clearAndUpdateTileOfNotRenderedRegion(index,
tile,
tileNotRenderedRegion,
backingStoreRect,
false /*update*/);
#if DEBUG_BACKINGSTORE
Platform::IntRect extents = tileNotRenderedRegion.extents();
BBLOG(BlackBerry::Platform::LogLevelCritical,
"BackingStorePrivate::updateTilesForScroll did clear tile %d,%d %dx%d",
extents.x(), extents.y(), extents.width(), extents.height());
#endif
}
updateTile(index, false /*immediate*/);
} else if (isVisible
&& (forceVisible || !tile->frontBuffer()->isRendered(tileVisibleContentsRect(index), currentState->scale()))
&& !isCurrentVisibleJob(index, tile, backingStoreRect))
updateTile(index, false /*immediate*/);
}
}
void BackingStorePrivate::resetTile(const TileIndex& index, BackingStoreTile* tile, bool resetBackground)
{
if (!m_webPage->isVisible())
return;
if (!isActive()) {
m_webPage->d->setShouldResetTilesWhenShown(true);
return;
}
TileRect tileRect;
tileRect.first = index;
tileRect.second = this->tileRect();
// Only clear regular render jobs if we're clearing the background too.
m_renderQueue->clear(mapFromTilesToTransformedContents(tileRect), resetBackground /*clearRegularRenderJobs*/);
if (resetBackground)
tile->reset();
}
void BackingStorePrivate::updateTile(const TileIndex& index, bool immediate)
{
if (!isActive())
return;
TileRect tileRect;
tileRect.first = index;
tileRect.second = this->tileRect();
Platform::IntRect updateRect = mapFromTilesToTransformedContents(tileRect);
RenderQueue::JobType jobType = isTileVisible(index) ? RenderQueue::VisibleScroll : RenderQueue::NonVisibleScroll;
if (immediate)
render(updateRect);
else
m_renderQueue->addToQueue(jobType, updateRect);
}
void BackingStorePrivate::updateTile(const Platform::IntPoint& origin, bool immediate)
{
if (!isActive())
return;
Platform::IntRect updateRect = Platform::IntRect(origin, tileSize());
RenderQueue::JobType jobType = isTileVisible(origin) ? RenderQueue::VisibleScroll : RenderQueue::NonVisibleScroll;
if (immediate)
render(updateRect);
else
m_renderQueue->addToQueue(jobType, updateRect);
}
Platform::IntRect BackingStorePrivate::mapFromTilesToTransformedContents(const BackingStorePrivate::TileRect& tileRect) const
{
return mapFromTilesToTransformedContents(tileRect, frontState()->backingStoreRect());
}
Platform::IntRect BackingStorePrivate::mapFromTilesToTransformedContents(const BackingStorePrivate::TileRect& tileRect, const Platform::IntRect& backingStoreRect) const
{
TileIndex index = tileRect.first;
Platform::IntRect rect = tileRect.second;
// The origin of the tile including the backing store offset.
const Platform::IntPoint originOfTile = this->originOfTile(index, backingStoreRect);
rect.move(originOfTile.x(), originOfTile.y());
return rect;
}
BackingStorePrivate::TileRectList BackingStorePrivate::mapFromTransformedContentsToAbsoluteTileBoundaries(const Platform::IntRect& rect) const
{
if (!m_webPage->isVisible() || !isActive()) {
ASSERT_NOT_REACHED();
return TileRectList();
}
TileRectList tileRectList;
int firstXOffset = rect.x() / tileWidth();
int firstYOffset = rect.y() / tileHeight();
int lastXOffset = (rect.right() - 1) / tileWidth();
int lastYOffset = (rect.bottom() - 1) / tileHeight();
for (int i = firstXOffset; i <= lastXOffset; ++i) {
for (int j = firstYOffset; j <= lastYOffset; ++j) {
const int dstX = (i == firstXOffset) ? rect.x() : i * tileWidth();
const int dstY = (j == firstYOffset) ? rect.y() : j * tileHeight();
const int dstRight = (i == lastXOffset) ? rect.right() : (i + 1) * tileWidth();
const int dstBottom = (j == lastYOffset) ? rect.bottom() : (j + 1) * tileHeight();
const int srcX = dstX % tileWidth();
const int srcY = dstY % tileHeight();
TileRect tileRect;
tileRect.first = TileIndex(i, j);
tileRect.second = Platform::IntRect(srcX, srcY, dstRight - dstX, dstBottom - dstY);
tileRectList.append(tileRect);
}
}
return tileRectList;
}
BackingStorePrivate::TileRectList BackingStorePrivate::mapFromTransformedContentsToTiles(const Platform::IntRect& rect) const
{
return mapFromTransformedContentsToTiles(rect, frontState());
}
BackingStorePrivate::TileRectList BackingStorePrivate::mapFromTransformedContentsToTiles(const Platform::IntRect& rect, BackingStoreGeometry* state) const
{
TileMap tileMap = state->tileMap();
TileRectList tileRectList;
TileMap::const_iterator end = tileMap.end();
for (TileMap::const_iterator it = tileMap.begin(); it != end; ++it) {
TileIndex index = it->key;
BackingStoreTile* tile = it->value;
// Need to map the rect to tile coordinates.
Platform::IntRect r = rect;
// The origin of the tile including the backing store offset.
const Platform::IntPoint originOfTile = this->originOfTile(index, state->backingStoreRect());
r.move(-(originOfTile.x()), -(originOfTile.y()));
// Do we intersect the current tile or no?
r.intersect(tile->rect());
if (r.isEmpty())
continue;
// If we do append to list and Voila!
TileRect tileRect;
tileRect.first = index;
tileRect.second = r;
tileRectList.append(tileRect);
}
return tileRectList;
}
void BackingStorePrivate::updateTileMatrixIfNeeded()
{
ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
// This will update the tile matrix.
scrollBackingStore(0, 0);
}
void BackingStorePrivate::contentsSizeChanged(const Platform::IntSize&)
{
updateTileMatrixIfNeeded();
}
void BackingStorePrivate::scrollChanged(const Platform::IntPoint&)
{
// FIXME: Need to do anything here?
}
void BackingStorePrivate::transformChanged()
{
if (!m_webPage->isVisible())
return;
if (!isActive()) {
m_renderQueue->reset();
m_renderQueue->addToQueue(RenderQueue::VisibleZoom, visibleContentsRect());
m_webPage->d->setShouldResetTilesWhenShown(true);
return;
}
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
bool hasCurrentVisibleZoomJob = m_renderQueue->hasCurrentVisibleZoomJob();
bool isLoading = m_client->isLoading();
if (isLoading) {
if (!hasCurrentVisibleZoomJob)
m_visibleTileBufferRect = visibleContentsRect(); // Cache this for blitVisibleContents.
// Add the currently visible tiles to the render queue as visible zoom jobs.
TileRectList tileRectList = mapFromTransformedContentsToTiles(visibleContentsRect());
for (size_t i = 0; i < tileRectList.size(); ++i) {
TileRect tileRect = tileRectList[i];
TileIndex index = tileRect.first;
Platform::IntRect dirtyTileRect = tileRect.second;
BackingStoreTile* tile = currentMap.get(index);
// Invalidate the whole rect.
tileRect.second = this->tileRect();
Platform::IntRect wholeRect = mapFromTilesToTransformedContents(tileRect);
m_renderQueue->addToQueue(RenderQueue::VisibleZoom, wholeRect);
// Copy the visible contents into the visibleTileBuffer if we don't have
// any current visible zoom jobs.
if (!hasCurrentVisibleZoomJob) {
// Map to the destination's coordinate system.
Platform::IntPoint difference = this->originOfTile(index) - m_visibleTileBufferRect.location();
Platform::IntSize offset = Platform::IntSize(difference.x(), difference.y());
Platform::IntRect dirtyRect = dirtyTileRect;
dirtyRect.move(offset.width(), offset.height());
BackingStoreTile* visibleTileBuffer
= SurfacePool::globalSurfacePool()->visibleTileBuffer();
ASSERT(visibleTileBuffer->size() == Platform::IntSize(m_webPage->d->transformedViewportSize()));
BlackBerry::Platform::Graphics::blitToBuffer(
visibleTileBuffer->frontBuffer()->nativeBuffer(), dirtyRect,
tile->frontBuffer()->nativeBuffer(), dirtyTileRect);
}
}
}
m_renderQueue->reset();
resetTiles(true /*resetBackground*/);
}
void BackingStorePrivate::orientationChanged()
{
ASSERT(BlackBerry::Platform::webKitThreadMessageClient()->isCurrentThread());
updateTileMatrixIfNeeded();
createVisibleTileBuffer();
}
void BackingStorePrivate::actualVisibleSizeChanged(const Platform::IntSize& size)
{
}
static void createVisibleTileBufferForWebPage(WebPagePrivate* page)
{
ASSERT(page);
SurfacePool* surfacePool = SurfacePool::globalSurfacePool();
surfacePool->initializeVisibleTileBuffer(page->transformedViewportSize());
}
void BackingStorePrivate::createSurfaces()
{
BackingStoreGeometry* currentState = frontState();
TileMap currentMap = currentState->tileMap();
ASSERT(currentMap.isEmpty());
if (m_webPage->isVisible()) {
// This method is only to be called as part of setting up a new web page instance and
// before said instance is made visible so as to ensure a consistent definition of web
// page visibility. That is, a web page is said to be visible when explicitly made visible.
ASSERT_NOT_REACHED();
return;
}
SurfacePool* surfacePool = SurfacePool::globalSurfacePool();
surfacePool->initialize(tileSize());
if (surfacePool->isEmpty()) // Settings specify 0 tiles / no backing store.
return;
const Divisor divisor = bestDivisor(expandedContentsSize(), tileWidth(), tileHeight(), minimumNumberOfTilesWide(), minimumNumberOfTilesHigh(), m_preferredTileMatrixDimension);
int numberOfTilesWide = divisor.first;
int numberOfTilesHigh = divisor.second;
const SurfacePool::TileList tileList = surfacePool->tileList();
ASSERT(static_cast<int>(tileList.size()) >= (numberOfTilesWide * numberOfTilesHigh));
TileMap newTileMap;
for (int y = 0; y < numberOfTilesHigh; ++y) {
for (int x = 0; x < numberOfTilesWide; ++x) {
TileIndex index(x, y);
newTileMap.add(index, tileList.at(x + y * numberOfTilesWide));
}
}
// Set the initial state of the backingstore geometry.
backState()->setScale(m_webPage->d->currentScale());
backState()->setNumberOfTilesWide(divisor.first);
backState()->setNumberOfTilesHigh(divisor.second);
backState()->setTileMap(newTileMap);
// Swap back/front state.
swapState();
createVisibleTileBufferForWebPage(m_webPage->d);
// Don't try to blit to screen unless we have a buffer.
if (!buffer())
suspendScreenAndBackingStoreUpdates();
}
void BackingStorePrivate::createVisibleTileBuffer()
{
if (!m_webPage->isVisible() || !isActive())
return;
createVisibleTileBufferForWebPage(m_webPage->d);
}
Platform::IntPoint BackingStorePrivate::originOfTile(const TileIndex& index) const
{
return originOfTile(index, frontState()->backingStoreRect());
}
Platform::IntPoint BackingStorePrivate::originOfTile(const TileIndex& index, const Platform::IntRect& backingStoreRect) const
{
return Platform::IntPoint(backingStoreRect.x() + (index.i() * tileWidth()),
backingStoreRect.y() + (index.j() * tileHeight()));
}
int BackingStorePrivate::minimumNumberOfTilesWide() const
{
// The minimum number of tiles wide required to fill the viewport + 1 tile extra to allow scrolling.
return static_cast<int>(ceilf(m_client->transformedViewportSize().width() / static_cast<float>(tileWidth()))) + 1;
}
int BackingStorePrivate::minimumNumberOfTilesHigh() const
{
// The minimum number of tiles high required to fill the viewport + 1 tile extra to allow scrolling.
return static_cast<int>(ceilf(m_client->transformedViewportSize().height() / static_cast<float>(tileHeight()))) + 1;
}
Platform::IntSize BackingStorePrivate::expandedContentsSize() const
{
return m_client->transformedContentsSize().expandedTo(m_client->transformedViewportSize());
}
int BackingStorePrivate::tileWidth()
{
static int tileWidth = BlackBerry::Platform::Graphics::Screen::primaryScreen()->landscapeWidth();
return tileWidth;
}
int BackingStorePrivate::tileHeight()
{
static int tileHeight = BlackBerry::Platform::Graphics::Screen::primaryScreen()->landscapeHeight();
return tileHeight;
}
Platform::IntSize BackingStorePrivate::tileSize()
{
return Platform::IntSize(tileWidth(), tileHeight());
}
Platform::IntRect BackingStorePrivate::tileRect()
{
return Platform::IntRect(0, 0, tileWidth(), tileHeight());
}
void BackingStorePrivate::renderContents(Platform::Graphics::Drawable* drawable,
const Platform::IntRect& contentsRect,
const Platform::IntSize& destinationSize) const
{
if (!drawable || contentsRect.isEmpty())
return;
requestLayoutIfNeeded();
PlatformGraphicsContext* platformGraphicsContext = SurfacePool::globalSurfacePool()->createPlatformGraphicsContext(drawable);
GraphicsContext graphicsContext(platformGraphicsContext);
graphicsContext.translate(-contentsRect.x(), -contentsRect.y());
WebCore::IntRect transformedContentsRect(contentsRect.x(), contentsRect.y(), contentsRect.width(), contentsRect.height());
float widthScale = static_cast<float>(destinationSize.width()) / contentsRect.width();
float heightScale = static_cast<float>(destinationSize.height()) / contentsRect.height();
if (widthScale != 1.0 && heightScale != 1.0) {
TransformationMatrix matrix;
matrix.scaleNonUniform(1.0 / widthScale, 1.0 / heightScale);
transformedContentsRect = matrix.mapRect(transformedContentsRect);
// We extract from the contentsRect but draw a slightly larger region than
// we were told to, in order to avoid pixels being rendered only partially.
const int atLeastOneDevicePixel = static_cast<int>(ceilf(std::max(1.0 / widthScale, 1.0 / heightScale)));
transformedContentsRect.inflate(atLeastOneDevicePixel);
graphicsContext.scale(FloatSize(widthScale, heightScale));
}
graphicsContext.clip(transformedContentsRect);
m_client->frame()->view()->paintContents(&graphicsContext, transformedContentsRect);
delete platformGraphicsContext;
}
void BackingStorePrivate::renderContents(BlackBerry::Platform::Graphics::Buffer* tileBuffer,
const Platform::IntPoint& surfaceOffset,
const Platform::IntRect& contentsRect) const
{
// If tileBuffer == 0, we render directly to the window.
if (!m_webPage->isVisible() && tileBuffer)
return;
#if DEBUG_BACKINGSTORE
BBLOG(BlackBerry::Platform::LogLevelCritical,
"BackingStorePrivate::renderContents tileBuffer=0x%x surfaceOffset=(%d,%d) contentsRect=(%d,%d %dx%d)",
tileBuffer, surfaceOffset.x(), surfaceOffset.y(),
contentsRect.x(), contentsRect.y(), contentsRect.width(), contentsRect.height());
#endif
// It is up to callers of this method to perform layout themselves!
ASSERT(!m_webPage->d->mainFrame()->view()->needsLayout());
Platform::IntSize contentsSize = m_client->contentsSize();
Color backgroundColor(m_webPage->settings()->backgroundColor());
BlackBerry::Platform::Graphics::Buffer* targetBuffer = tileBuffer
? tileBuffer
: buffer();
if (contentsSize.isEmpty()
|| !Platform::IntRect(Platform::IntPoint(0, 0), m_client->transformedContentsSize()).contains(contentsRect)
|| backgroundColor.hasAlpha()) {
// Clear the area if it's not fully covered by (opaque) contents.
BlackBerry::Platform::IntRect clearRect = BlackBerry::Platform::IntRect(
contentsRect.x() - surfaceOffset.x(), contentsRect.y() - surfaceOffset.y(),
contentsRect.width(), contentsRect.height());
BlackBerry::Platform::Graphics::clearBuffer(targetBuffer, clearRect,
backgroundColor.red(), backgroundColor.green(),
backgroundColor.blue(), backgroundColor.alpha());
}
if (contentsSize.isEmpty())
return;
BlackBerry::Platform::Graphics::Drawable* bufferDrawable =
BlackBerry::Platform::Graphics::lockBufferDrawable(targetBuffer);
PlatformGraphicsContext* bufferPlatformGraphicsContext = bufferDrawable
? SurfacePool::globalSurfacePool()->createPlatformGraphicsContext(bufferDrawable)
: 0;
PlatformGraphicsContext* targetPlatformGraphicsContext = bufferPlatformGraphicsContext
? bufferPlatformGraphicsContext
: SurfacePool::globalSurfacePool()->lockTileRenderingSurface();
ASSERT(targetPlatformGraphicsContext);
{
GraphicsContext graphicsContext(targetPlatformGraphicsContext);
// Believe it or not this is important since the WebKit Skia backend
// doesn't store the original state unless you call save first :P
graphicsContext.save();
// Translate context according to offset.
graphicsContext.translate(-surfaceOffset.x(), -surfaceOffset.y());
// Add our transformation matrix as the global transform.
AffineTransform affineTransform(
m_webPage->d->transformationMatrix()->a(),
m_webPage->d->transformationMatrix()->b(),
m_webPage->d->transformationMatrix()->c(),
m_webPage->d->transformationMatrix()->d(),
m_webPage->d->transformationMatrix()->e(),
m_webPage->d->transformationMatrix()->f());
graphicsContext.concatCTM(affineTransform);
// Now that the matrix is applied we need untranformed contents coordinates.
Platform::IntRect untransformedContentsRect = m_webPage->d->mapFromTransformed(contentsRect);
// We extract from the contentsRect but draw a slightly larger region than
// we were told to, in order to avoid pixels being rendered only partially.
const int atLeastOneDevicePixel =
static_cast<int>(ceilf(1.0 / m_webPage->d->transformationMatrix()->a()));
untransformedContentsRect.inflate(atLeastOneDevicePixel, atLeastOneDevicePixel);
// Make sure the untransformed rectangle for the (slightly larger than
// initially requested) repainted region is within the bounds of the page.
untransformedContentsRect.intersect(Platform::IntRect(Platform::IntPoint(0, 0), contentsSize));
// Some WebKit painting backends *cough* Skia *cough* don't set this automatically
// to the dirtyRect so do so here explicitly.
graphicsContext.clip(untransformedContentsRect);
// Take care of possible left overflow on RTL page.
if (int leftOverFlow = m_client->frame()->view()->minimumScrollPosition().x()) {
untransformedContentsRect.move(leftOverFlow, 0);
graphicsContext.translate(-leftOverFlow, 0);
}
// Let WebCore render the page contents into the drawing surface.
m_client->frame()->view()->paintContents(&graphicsContext, untransformedContentsRect);
graphicsContext.restore();
}
// Grab the requested region from the drawing surface into the tile image.
delete bufferPlatformGraphicsContext;
if (bufferDrawable)
releaseBufferDrawable(targetBuffer);
else {
const Platform::IntPoint dstPoint(contentsRect.x() - surfaceOffset.x(),
contentsRect.y() - surfaceOffset.y());
const Platform::IntRect dstRect(dstPoint, contentsRect.size());
const Platform::IntRect srcRect = dstRect;
// If we couldn't directly draw to the buffer, copy from the drawing surface.
SurfacePool::globalSurfacePool()->releaseTileRenderingSurface(targetPlatformGraphicsContext);
BlackBerry::Platform::Graphics::blitToBuffer(targetBuffer, dstRect, BlackBerry::Platform::Graphics::drawingSurface(), srcRect);
}
}
#if DEBUG_FAT_FINGERS
static void drawDebugRect(BlackBerry::Platform::Graphics::Buffer* dstBuffer, const Platform::IntRect& dstRect, const Platform::IntRect& srcRect, unsigned char red, unsigned char green, unsigned char blue)
{
Platform::IntRect drawRect(srcRect);
drawRect.intersect(dstRect);
if (!drawRect.isEmpty())
BlackBerry::Platform::Graphics::clearBuffer(dstBuffer, drawRect, red, green, blue, 128);
}
#endif
void BackingStorePrivate::blitToWindow(const Platform::IntRect& dstRect,
const BlackBerry::Platform::Graphics::Buffer* srcBuffer,
const Platform::IntRect& srcRect,
bool blend,
unsigned char globalAlpha)
{
ASSERT(BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread());
windowFrontBufferState()->clearBlittedRegion(dstRect);
windowBackBufferState()->addBlittedRegion(dstRect);
BlackBerry::Platform::Graphics::Buffer* dstBuffer = buffer();
ASSERT(dstBuffer);
ASSERT(srcBuffer);
if (!dstBuffer)
BBLOG(BlackBerry::Platform::LogLevelWarn, "Empty window buffer, couldn't blitToWindow");
BlackBerry::Platform::Graphics::BlendMode blendMode = blend
? BlackBerry::Platform::Graphics::SourceOver
: BlackBerry::Platform::Graphics::SourceCopy;
BlackBerry::Platform::Graphics::blitToBuffer(dstBuffer, dstRect, srcBuffer, srcRect, blendMode, globalAlpha);
#if DEBUG_FAT_FINGERS
drawDebugRect(dstBuffer, dstRect, FatFingers::m_debugFatFingerRect, 210, 210, 250);
drawDebugRect(dstBuffer, dstRect, Platform::IntRect(FatFingers::m_debugFatFingerClickPosition, Platform::IntSize(3, 3)), 0, 0, 0);
drawDebugRect(dstBuffer, dstRect, Platform::IntRect(FatFingers::m_debugFatFingerAdjustedPosition, Platform::IntSize(5, 5)), 100, 100, 100);
#endif
}
void BackingStorePrivate::fillWindow(Platform::Graphics::FillPattern pattern,
const Platform::IntRect& dstRect,
const Platform::IntPoint& contentsOrigin,
double contentsScale)
{
ASSERT(BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread());
windowFrontBufferState()->clearBlittedRegion(dstRect);
windowBackBufferState()->addBlittedRegion(dstRect);
BlackBerry::Platform::Graphics::Buffer* dstBuffer = buffer();
ASSERT(dstBuffer);
if (!dstBuffer)
BBLOG(BlackBerry::Platform::LogLevelWarn, "Empty window buffer, couldn't fillWindow");
BlackBerry::Platform::Graphics::fillBuffer(dstBuffer, pattern, dstRect, contentsOrigin, contentsScale);
}
void BackingStorePrivate::invalidateWindow()
{
// Grab a rect appropriate for the current thread.
if (BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread())
invalidateWindow(m_webPage->client()->userInterfaceViewportAccessor()->destinationSurfaceRect());
else
invalidateWindow(Platform::IntRect(Platform::IntPoint(0, 0), m_client->transformedViewportSize()));
}
void BackingStorePrivate::invalidateWindow(const Platform::IntRect& dst)
{
if (dst.isEmpty())
return;
if (!BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread() && !shouldDirectRenderingToWindow()) {
// This needs to be sync in order to swap the recently drawn thing...
// This will only be called from WebKit thread during direct rendering.
typedef void (BlackBerry::WebKit::BackingStorePrivate::*FunctionType)(const Platform::IntRect&);
BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchSyncMessage(
BlackBerry::Platform::createMethodCallMessage<FunctionType, BackingStorePrivate, Platform::IntRect>(
&BackingStorePrivate::invalidateWindow, this, dst));
return;
}
#if DEBUG_BACKINGSTORE
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::invalidateWindow dst = %s", dst.toString().c_str());
#endif
// Since our window may also be double buffered, we need to also copy the
// front buffer's contents to the back buffer before we swap them. It is
// analogous to what we do with our double buffered tiles by calling
// copyPreviousContentsToBackingSurfaceOfTile(). It only affects partial
// screen updates since when we are scrolling or zooming, the whole window
// is invalidated anyways and no copying is needed.
copyPreviousContentsToBackSurfaceOfWindow();
Platform::IntRect dstRect = dst;
Platform::IntRect viewportRect(Platform::IntPoint(0, 0), m_client->transformedViewportSize());
dstRect.intersect(viewportRect);
if (dstRect.width() <= 0 || dstRect.height() <= 0)
return;
#if DEBUG_BACKINGSTORE
BBLOG(BlackBerry::Platform::LogLevelCritical, "BackingStorePrivate::invalidateWindow posting = %s", dstRect.toString().c_str());
#endif
m_currentWindowBackBuffer = (m_currentWindowBackBuffer + 1) % 2;
if (Window* window = m_webPage->client()->window())
window->post(dstRect);
}
void BackingStorePrivate::clearWindow(const Platform::IntRect& rect,
unsigned char red,
unsigned char green,
unsigned char blue,
unsigned char alpha)
{
if (!BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread() && !shouldDirectRenderingToWindow()) {
typedef void (BlackBerry::WebKit::BackingStorePrivate::*FunctionType)(const Platform::IntRect&,
unsigned char,
unsigned char,
unsigned char,
unsigned char);
BlackBerry::Platform::userInterfaceThreadMessageClient()->dispatchMessage(
BlackBerry::Platform::createMethodCallMessage<FunctionType,
BackingStorePrivate,
Platform::IntRect,
unsigned char,
unsigned char,
unsigned char,
unsigned char>(
&BackingStorePrivate::clearWindow, this, rect, red, green, blue, alpha));
return;
}
BlackBerry::Platform::Graphics::Buffer* dstBuffer = buffer();
ASSERT(dstBuffer);
if (!dstBuffer)
BBLOG(BlackBerry::Platform::LogLevelWarn, "Empty window buffer, couldn't clearWindow");
windowFrontBufferState()->clearBlittedRegion(rect);
windowBackBufferState()->addBlittedRegion(rect);
BlackBerry::Platform::Graphics::clearBuffer(dstBuffer, rect, red, green, blue, alpha);
}
bool BackingStorePrivate::isScrollingOrZooming() const
{
BackingStoreMutexLocker locker(const_cast<BackingStorePrivate*>(this));
return m_isScrollingOrZooming;
}
void BackingStorePrivate::setScrollingOrZooming(bool scrollingOrZooming, bool shouldBlit)
{
{
BackingStoreMutexLocker locker(this);
m_isScrollingOrZooming = scrollingOrZooming;
}
#if !ENABLE_REPAINTONSCROLL
m_suspendRenderJobs = scrollingOrZooming; // Suspend the rendering of everything.
#endif
if (!m_webPage->settings()->shouldRenderAnimationsOnScrollOrZoom())
m_suspendRegularRenderJobs = scrollingOrZooming; // Suspend the rendering of animations.
m_webPage->d->m_mainFrame->view()->setConstrainsScrollingToContentEdge(!scrollingOrZooming);
// Clear this flag since we don't care if the render queue is under pressure
// or not since we are scrolling and it is more important to not lag than
// it is to ensure animations achieve better framerates!
if (scrollingOrZooming)
m_renderQueue->setCurrentRegularRenderJobBatchUnderPressure(false);
#if ENABLE_SCROLLBARS
else if (shouldBlit && !shouldDirectRenderingToWindow())
blitVisibleContents();
#endif
}
void BackingStorePrivate::lockBackingStore()
{
pthread_mutex_lock(&m_mutex);
}
void BackingStorePrivate::unlockBackingStore()
{
pthread_mutex_unlock(&m_mutex);
}
BackingStoreGeometry* BackingStorePrivate::frontState() const
{
return reinterpret_cast<BackingStoreGeometry*>(m_frontState);
}
BackingStoreGeometry* BackingStorePrivate::backState() const
{
return reinterpret_cast<BackingStoreGeometry*>(m_backState);
}
void BackingStorePrivate::swapState()
{
unsigned front = reinterpret_cast<unsigned>(frontState());
unsigned back = reinterpret_cast<unsigned>(backState());
// Atomic change.
_smp_xchg(&m_frontState, back);
_smp_xchg(&m_backState, front);
BlackBerry::Platform::userInterfaceThreadMessageClient()->syncToCurrentMessage();
}
BackingStoreWindowBufferState* BackingStorePrivate::windowFrontBufferState() const
{
return &m_windowBufferState[(m_currentWindowBackBuffer + 1) % 2];
}
BackingStoreWindowBufferState* BackingStorePrivate::windowBackBufferState() const
{
return &m_windowBufferState[m_currentWindowBackBuffer];
}
#if USE(ACCELERATED_COMPOSITING)
void BackingStorePrivate::drawAndBlendLayersForDirectRendering(const Platform::IntRect& dirtyRect)
{
ASSERT(BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread());
if (!BlackBerry::Platform::userInterfaceThreadMessageClient()->isCurrentThread())
return;
// Because we're being called sync from the WebKit thread, we can use
// regular WebPage size and transformation functions without concerns.
WebCore::IntRect contentsRect = visibleContentsRect();
WebCore::FloatRect untransformedContentsRect = m_webPage->d->mapFromTransformedFloatRect(WebCore::FloatRect(contentsRect));
WebCore::IntRect contentsScreenRect = m_client->mapFromTransformedContentsToTransformedViewport(contentsRect);
WebCore::IntRect dstRect = intersection(contentsScreenRect,
WebCore::IntRect(WebCore::IntPoint(0, 0), m_webPage->d->transformedViewportSize()));
// Check if rendering caused a commit and we need to redraw the layers.
if (WebPageCompositorPrivate* compositor = m_webPage->d->compositor())
compositor->drawLayers(dstRect, untransformedContentsRect);
}
#endif
bool BackingStorePrivate::isActive() const
{
return BackingStorePrivate::s_currentBackingStoreOwner == m_webPage && SurfacePool::globalSurfacePool()->isActive();
}
void BackingStorePrivate::didRenderContent(const Platform::IntRect& renderedRect)
{
if (isScrollingOrZooming())
return;
if (!shouldDirectRenderingToWindow()) {
if (!m_webPage->d->needsOneShotDrawingSynchronization())
blitVisibleContents();
} else
invalidateWindow();
m_webPage->client()->notifyContentRendered(renderedRect);
}
BackingStore::BackingStore(WebPage* webPage, BackingStoreClient* client)
: d(new BackingStorePrivate)
{
d->m_webPage = webPage;
d->m_client = client;
}
BackingStore::~BackingStore()
{
deleteGuardedObject(d);
d = 0;
}
void BackingStore::createSurface()
{
static bool initialized = false;
if (!initialized) {
BlackBerry::Platform::Graphics::initialize();
initialized = true;
}
// Triggers creation of surfaces in backingstore.
d->createSurfaces();
// Focusing the WebPage triggers a repaint, so while we want it to be
// focused initially this has to happen after creation of the surface.
d->m_webPage->setFocused(true);
}
void BackingStore::suspendScreenAndBackingStoreUpdates()
{
d->suspendScreenAndBackingStoreUpdates();
}
void BackingStore::resumeScreenAndBackingStoreUpdates(ResumeUpdateOperation op)
{
d->resumeScreenAndBackingStoreUpdates(op);
}
bool BackingStore::isScrollingOrZooming() const
{
return d->isScrollingOrZooming();
}
void BackingStore::setScrollingOrZooming(bool scrollingOrZooming)
{
d->setScrollingOrZooming(scrollingOrZooming);
}
void BackingStore::blitVisibleContents()
{
d->blitVisibleContents(false /*force*/);
}
void BackingStore::repaint(int x, int y, int width, int height,
bool contentChanged, bool immediate)
{
d->repaint(Platform::IntRect(x, y, width, height), contentChanged, immediate);
}
bool BackingStore::isDirectRenderingToWindow() const
{
BackingStoreMutexLocker locker(d);
return d->shouldDirectRenderingToWindow();
}
void BackingStore::createBackingStoreMemory()
{
if (BackingStorePrivate::s_currentBackingStoreOwner == d->m_webPage)
SurfacePool::globalSurfacePool()->createBuffers();
}
void BackingStore::releaseBackingStoreMemory()
{
if (BackingStorePrivate::s_currentBackingStoreOwner == d->m_webPage)
SurfacePool::globalSurfacePool()->releaseBuffers();
}
bool BackingStore::defersBlit() const
{
return d->m_defersBlit;
}
void BackingStore::setDefersBlit(bool b)
{
d->m_defersBlit = b;
}
bool BackingStore::hasBlitJobs() const
{
#if USE(ACCELERATED_COMPOSITING)
// If there's a WebPageCompositorClient, let it schedule the blit.
WebPageCompositorPrivate* compositor = d->m_webPage->d->compositor();
if (compositor && compositor->client())
return false;
#endif
// Normally, this would be called from the compositing thread,
// and the flag is set on the compositing thread, so no need for
// synchronization.
return d->m_hasBlitJobs;
}
void BackingStore::blitOnIdle()
{
#if USE(ACCELERATED_COMPOSITING)
// If there's a WebPageCompositorClient, let it schedule the blit.
WebPageCompositorPrivate* compositor = d->m_webPage->d->compositor();
if (compositor && compositor->client())
return;
#endif
d->blitVisibleContents(true /*force*/);
}
Platform::IntSize BackingStorePrivate::surfaceSize() const
{
if (Window* window = m_webPage->client()->window())
return window->surfaceSize();
#if USE(ACCELERATED_COMPOSITING)
if (WebPageCompositorPrivate* compositor = m_webPage->d->compositor())
return compositor->context()->surfaceSize();
#endif
return Platform::IntSize();
}
Platform::Graphics::Buffer* BackingStorePrivate::buffer() const
{
if (Window* window = m_webPage->client()->window())
return window->buffer();
#if USE(ACCELERATED_COMPOSITING)
if (WebPageCompositorPrivate* compositor = m_webPage->d->compositor())
return compositor->context() ? compositor->context()->buffer() : 0;
#endif
return 0;
}
void BackingStore::drawContents(Platform::Graphics::Drawable* drawable, const Platform::IntRect& contentsRect, const Platform::IntSize& destinationSize)
{
d->renderContents(drawable, contentsRect, destinationSize);
}
}
}
|